errors.c
#include <int.h>
#include "errors.h"
#ifdef STDC_HEADERS
# include <stdarg.h>
#endif
#define LIBTASN1_ERROR_ENTRY(name) \
{ #name, name }
struct libtasn1_error_entry {
const char *name;
int number;
};
typedef struct libtasn1_error_entry libtasn1_error_entry;
static libtasn1_error_entry error_algorithms[] = {
LIBTASN1_ERROR_ENTRY( ASN1_SUCCESS ),
LIBTASN1_ERROR_ENTRY( ASN1_FILE_NOT_FOUND ),
LIBTASN1_ERROR_ENTRY( ASN1_ELEMENT_NOT_FOUND ),
LIBTASN1_ERROR_ENTRY( ASN1_IDENTIFIER_NOT_FOUND ),
LIBTASN1_ERROR_ENTRY( ASN1_DER_ERROR ),
LIBTASN1_ERROR_ENTRY( ASN1_VALUE_NOT_FOUND ),
LIBTASN1_ERROR_ENTRY( ASN1_GENERIC_ERROR ),
LIBTASN1_ERROR_ENTRY( ASN1_VALUE_NOT_VALID ),
LIBTASN1_ERROR_ENTRY( ASN1_TAG_ERROR ),
LIBTASN1_ERROR_ENTRY( ASN1_TAG_IMPLICIT ),
LIBTASN1_ERROR_ENTRY( ASN1_ERROR_TYPE_ANY ),
LIBTASN1_ERROR_ENTRY( ASN1_SYNTAX_ERROR ),
LIBTASN1_ERROR_ENTRY( ASN1_MEM_ERROR ),
LIBTASN1_ERROR_ENTRY( ASN1_MEM_ALLOC_ERROR ),
LIBTASN1_ERROR_ENTRY( ASN1_DER_OVERFLOW ),
LIBTASN1_ERROR_ENTRY( ASN1_NAME_TOO_LONG ),
LIBTASN1_ERROR_ENTRY( ASN1_ARRAY_ERROR ),
LIBTASN1_ERROR_ENTRY( ASN1_ELEMENT_NOT_EMPTY ),
{0}
};
#define LIBTASN1_ERROR_LOOP(b) \
const libtasn1_error_entry *p; \
for(p = error_algorithms; p->name != NULL; p++) { b ; }
#define LIBTASN1_ERROR_ALG_LOOP(a) \
LIBTASN1_ERROR_LOOP( if(p->number == error) { a; break; } )
void libtasn1_perror(asn1_retCode error)
{
const char *ret = NULL;
LIBTASN1_ERROR_ALG_LOOP(ret =
p->name + sizeof("ASN1_") - 1);
_libtasn1_log( "LIBTASN1 ERROR: %s\n", ret);
}
const char* libtasn1_strerror(asn1_retCode error)
{
const char *ret = NULL;
LIBTASN1_ERROR_ALG_LOOP(ret =
p->name + sizeof("ASN1_") - 1);
return ret;
}
#ifdef LIBTASN1_DEBUG
void _libtasn1_log( const char *fmt, ...) {
va_list args;
char str[MAX_LOG_SIZE];
va_start(args,fmt);
vsprintf( str,fmt,args);
va_end(args);
fprintf(stderr, str);
return;
}
#else
# ifndef C99_MACROS
void _libtasn1_null_log( void* x, ...) { return; }
# endif
#endif