@@ -463,20 +463,11 @@ static THREAD_LS_T struct wc_error_queue wc_errors;
463463#define ERRQ_LOCK () 0
464464#define ERRQ_UNLOCK () (void)0
465465
466- /* Internal function that is called by wolfCrypt_Init() */
467- int wc_LoggingInit (void )
468- {
469- return 0 ;
470- }
471-
472- /* internal function that is called by wolfCrypt_Cleanup */
473- int wc_LoggingCleanup (void )
474- {
475- /* clear logging entries */
476- wc_ClearErrorNodes ();
477- return 0 ;
478- }
479-
466+ /**
467+ * Given a relative index (from head of the error list), return
468+ * the absolute index in the `wc_errors->entries` array for
469+ * the entry or -1 if no such entry exists/is present.
470+ */
480471static int get_abs_idx (int relative_idx )
481472{
482473 if ((wc_errors .count == 0 ) || (relative_idx >= (int )wc_errors .count )) {
@@ -489,6 +480,10 @@ static int get_abs_idx(int relative_idx)
489480 return (int )((wc_errors .head_idx + relative_idx ) % ERROR_QUEUE_MAX );
490481}
491482
483+ /**
484+ * Return the error entry at the given relative index, if
485+ * it exists, e.g. `relative_idx` is in a valid range.
486+ */
492487static struct wc_error_entry * get_entry (int relative_idx )
493488{
494489 int abs_idx ;
@@ -500,6 +495,11 @@ static struct wc_error_entry *get_entry(int relative_idx)
500495 return & wc_errors .entries [abs_idx ];
501496}
502497
498+ /**
499+ * Return the error code in the given error `entry` and populate
500+ * `file`, `reason` and `line` with its values.
501+ * `entry` may be NULL, in which case BAD_STATE_E is returned.
502+ */
503503static int pass_entry (struct wc_error_entry * entry ,
504504 const char * * file , const char * * reason ,
505505 int * line )
@@ -520,6 +520,9 @@ static int pass_entry(struct wc_error_entry *entry,
520520 return entry -> err ;
521521}
522522
523+ /**
524+ * Assign entry with values, resets all previously present values.
525+ */
523526static void set_entry (struct wc_error_entry * entry , int error ,
524527 const char * file , const char * reason , int line )
525528{
@@ -548,12 +551,34 @@ static void set_entry(struct wc_error_entry *entry, int error,
548551 }
549552}
550553
554+ /* Internal function that is called by wolfCrypt_Init() */
555+ int wc_LoggingInit (void )
556+ {
557+ return 0 ;
558+ }
559+
560+ /* internal function that is called by wolfCrypt_Cleanup */
561+ int wc_LoggingCleanup (void )
562+ {
563+ /* clear logging entries */
564+ wc_ClearErrorNodes ();
565+ return 0 ;
566+ }
567+
568+ /**
569+ * Get the values from the HEAD of the ERR queue, but keep it in place.
570+ * If the queue is empty, return BAD_STATE_E.
571+ */
551572int wc_PeekErrorNode (int idx , const char * * file , const char * * reason ,
552573 int * line )
553574{
554575 return pass_entry (get_entry (idx ), file , reason , line );
555576}
556577
578+ /**
579+ * Get the values from the HEAD of the ERR queue and remove it.
580+ * If the queue is empty, return BAD_STATE_E.
581+ */
557582int wc_PullErrorNode (const char * * file , const char * * reason , int * line )
558583{
559584 struct wc_error_entry * entry ;
@@ -589,6 +614,11 @@ int wc_AddErrorNode(int error, int line, char* reason, char* file)
589614 return 0 ;
590615}
591616
617+ /**
618+ * Remove the entry at relative position `relative_idx` from the ERR queue.
619+ * For `relative_idx == 0` it removes the queue's head entry, for -1
620+ * it removes the last entry in the queue.
621+ */
592622void wc_RemoveErrorNode (int relative_idx )
593623{
594624 int last_idx , abs_idx = get_abs_idx (relative_idx );
@@ -624,6 +654,9 @@ void wc_RemoveErrorNode(int relative_idx)
624654 }
625655}
626656
657+ /**
658+ * Clear the ERR queue.
659+ */
627660void wc_ClearErrorNodes (void )
628661{
629662 if (wc_errors .count > 0 ) {
@@ -643,6 +676,19 @@ int wc_ERR_remove_state(void)
643676 return 0 ;
644677}
645678
679+ /**
680+ * Get the first entry's values in the ERR queue that is not filtered
681+ * by the provided `ignore_err` callback. All ignored entries are removed,
682+ * making the returned entry the head of the ERR queue afterwards.
683+ *
684+ * In case all entries are ignored, the ERR queue will be empty afterwards.
685+ * For an empty ERR queue 0 is returned.
686+ *
687+ * ìgnore_err` may be NULL, in which case this returns the HEAD values.
688+ *
689+ * `flags` is present for OpenSSL compatibility, but will always be
690+ * set to 0, since we do not keep flags at ERR entries.
691+ */
646692unsigned long wc_PeekErrorNodeLineData (const char * * file , int * line ,
647693 const char * * data , int * flags ,
648694 int (* ignore_err )(int err ))
@@ -679,6 +725,10 @@ unsigned long wc_PeekErrorNodeLineData(const char **file, int *line,
679725 }
680726}
681727
728+ /**
729+ * Get the error value at the HEAD of the ERR queue or 0 if the queue
730+ * is emtpy. The HEAD entry is removed by this call.
731+ */
682732unsigned long wc_GetErrorNodeErr (void )
683733{
684734 int ret ;
0 commit comments