Sourcecode and documentation for libtasn1-2 version 0.2.10-3sarge1
show bar | Show file versions
Search packages:
| Sourcecode archive home

errors.c

00001 /*              Copyright (C) 2002 Fabio Fiorina
00002  *
00003  * This file is part of LIBASN1.
00004  *
00005  * The LIBTASN1 library is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU Lesser General Public   
00007  * License as published by the Free Software Foundation; either 
00008  * version 2.1 of the License, or (at your option) any later version.
00009  *
00010  * This library is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of 
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Lesser General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Lesser General Public
00016  * License along with this library; if not, write to the Free Software
00017  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
00018  */
00019 
00020 #include <int.h>
00021 #include "errors.h"
00022 #ifdef STDC_HEADERS
00023 # include <stdarg.h>
00024 #endif
00025 
00026 
00027 #define LIBTASN1_ERROR_ENTRY(name) \
00028       { #name, name }
00029 
00030 struct libtasn1_error_entry {
00031       const char *name;
00032       int  number;
00033 };
00034 typedef struct libtasn1_error_entry libtasn1_error_entry;
00035 
00036 static libtasn1_error_entry error_algorithms[] = {
00037       LIBTASN1_ERROR_ENTRY( ASN1_SUCCESS ),
00038       LIBTASN1_ERROR_ENTRY( ASN1_FILE_NOT_FOUND ),
00039       LIBTASN1_ERROR_ENTRY( ASN1_ELEMENT_NOT_FOUND ),
00040       LIBTASN1_ERROR_ENTRY( ASN1_IDENTIFIER_NOT_FOUND ),
00041       LIBTASN1_ERROR_ENTRY( ASN1_DER_ERROR ),
00042       LIBTASN1_ERROR_ENTRY( ASN1_VALUE_NOT_FOUND ),
00043       LIBTASN1_ERROR_ENTRY( ASN1_GENERIC_ERROR ),
00044       LIBTASN1_ERROR_ENTRY( ASN1_VALUE_NOT_VALID ),
00045       LIBTASN1_ERROR_ENTRY( ASN1_TAG_ERROR ),
00046       LIBTASN1_ERROR_ENTRY( ASN1_TAG_IMPLICIT ),
00047       LIBTASN1_ERROR_ENTRY( ASN1_ERROR_TYPE_ANY ),
00048       LIBTASN1_ERROR_ENTRY( ASN1_SYNTAX_ERROR ),
00049       LIBTASN1_ERROR_ENTRY( ASN1_MEM_ERROR ),
00050       LIBTASN1_ERROR_ENTRY( ASN1_MEM_ALLOC_ERROR ),
00051       LIBTASN1_ERROR_ENTRY( ASN1_DER_OVERFLOW ),
00052       LIBTASN1_ERROR_ENTRY( ASN1_NAME_TOO_LONG ),
00053       LIBTASN1_ERROR_ENTRY( ASN1_ARRAY_ERROR ),
00054       LIBTASN1_ERROR_ENTRY( ASN1_ELEMENT_NOT_EMPTY ),
00055       {0}
00056 };
00057 
00058 #define LIBTASN1_ERROR_LOOP(b) \
00059         const libtasn1_error_entry *p; \
00060                 for(p = error_algorithms; p->name != NULL; p++) { b ; }
00061 
00062 #define LIBTASN1_ERROR_ALG_LOOP(a) \
00063                         LIBTASN1_ERROR_LOOP( if(p->number == error) { a; break; } )
00064 
00065 
00066 
00067 /**
00068   * libtasn1_perror - prints a string to stderr with a description of an error
00069   * @error: is an error returned by a libasn1 function. 
00070   *
00071   * This function is like perror(). The only difference is that it accepts an 
00072   * error returned by a libasn1 function. 
00073   **/
00074 void libtasn1_perror(asn1_retCode error)
00075 {
00076       const char *ret = NULL;
00077 
00078       /* avoid prefix */
00079       LIBTASN1_ERROR_ALG_LOOP(ret =
00080                         p->name + sizeof("ASN1_") - 1);
00081 
00082       _libtasn1_log( "LIBTASN1 ERROR: %s\n", ret);
00083       
00084 }
00085 
00086 
00087 /**
00088   * libtasn1_strerror - Returns a string with a description of an error
00089   * @error: is an error returned by a libtasn1 function.
00090   *
00091   * This function is similar to strerror(). The only difference is that it 
00092   * accepts an error (number) returned by a libasn1 function. 
00093   **/
00094 const char* libtasn1_strerror(asn1_retCode error)
00095 {
00096       const char *ret = NULL;
00097 
00098       /* avoid prefix */
00099       LIBTASN1_ERROR_ALG_LOOP(ret =
00100                         p->name + sizeof("ASN1_") - 1);
00101 
00102       return ret;
00103 }
00104 
00105 /* this function will output a message.
00106  */
00107 #ifdef LIBTASN1_DEBUG
00108 void _libtasn1_log( const char *fmt, ...) {
00109  va_list args;
00110  char str[MAX_LOG_SIZE];
00111 
00112  va_start(args,fmt);
00113  vsprintf( str,fmt,args); /* Flawfinder: ignore */
00114  va_end(args);   
00115 
00116  fprintf(stderr, str);
00117 
00118  return;
00119 }
00120 #else /* not DEBUG */
00121 # ifndef C99_MACROS
00122 
00123 /* Without C99 macros these functions have to
00124  * be called. This may affect performance.
00125  */
00126 void _libtasn1_null_log( void* x, ...) { return; }
00127 # endif /* C99_MACROS */
00128 #endif /* DEBUG */
00129 

Generated by  Doxygen 1.5.1