Doc fixes, from Martijn Koster <mak@greenhills.co.uk>.
[platform/upstream/libtasn1.git] / lib / errors.c
1 /*              Copyright (C) 2002 Fabio Fiorina
2  *
3  * This file is part of LIBASN1.
4  *
5  * The LIBTASN1 library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public   
7  * License as published by the Free Software Foundation; either 
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of 
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
18  */
19
20 #include <int.h>
21 #include "errors.h"
22 #ifdef STDC_HEADERS
23 # include <stdarg.h>
24 #endif
25
26
27 #define LIBTASN1_ERROR_ENTRY(name) \
28         { #name, name }
29
30 struct libtasn1_error_entry {
31         const char *name;
32         int  number;
33 };
34 typedef struct libtasn1_error_entry libtasn1_error_entry;
35
36 static libtasn1_error_entry error_algorithms[] = {
37         LIBTASN1_ERROR_ENTRY( ASN1_SUCCESS ),
38         LIBTASN1_ERROR_ENTRY( ASN1_FILE_NOT_FOUND ),
39         LIBTASN1_ERROR_ENTRY( ASN1_ELEMENT_NOT_FOUND ),
40         LIBTASN1_ERROR_ENTRY( ASN1_IDENTIFIER_NOT_FOUND ),
41         LIBTASN1_ERROR_ENTRY( ASN1_DER_ERROR ),
42         LIBTASN1_ERROR_ENTRY( ASN1_VALUE_NOT_FOUND ),
43         LIBTASN1_ERROR_ENTRY( ASN1_GENERIC_ERROR ),
44         LIBTASN1_ERROR_ENTRY( ASN1_VALUE_NOT_VALID ),
45         LIBTASN1_ERROR_ENTRY( ASN1_TAG_ERROR ),
46         LIBTASN1_ERROR_ENTRY( ASN1_TAG_IMPLICIT ),
47         LIBTASN1_ERROR_ENTRY( ASN1_ERROR_TYPE_ANY ),
48         LIBTASN1_ERROR_ENTRY( ASN1_SYNTAX_ERROR ),
49         LIBTASN1_ERROR_ENTRY( ASN1_MEM_ERROR ),
50         LIBTASN1_ERROR_ENTRY( ASN1_MEM_ALLOC_ERROR ),
51         LIBTASN1_ERROR_ENTRY( ASN1_DER_OVERFLOW ),
52         LIBTASN1_ERROR_ENTRY( ASN1_NAME_TOO_LONG ),
53         LIBTASN1_ERROR_ENTRY( ASN1_ARRAY_ERROR ),
54         LIBTASN1_ERROR_ENTRY( ASN1_ELEMENT_NOT_EMPTY ),
55         {0}
56 };
57
58 #define LIBTASN1_ERROR_LOOP(b) \
59         const libtasn1_error_entry *p; \
60                 for(p = error_algorithms; p->name != NULL; p++) { b ; }
61
62 #define LIBTASN1_ERROR_ALG_LOOP(a) \
63                         LIBTASN1_ERROR_LOOP( if(p->number == error) { a; break; } )
64
65
66
67 /**
68   * libtasn1_perror - prints a string to stderr with a description of an error
69   * @error: is an error returned by a libasn1 function. 
70   *
71   * This function is like perror(). The only difference is that it accepts an 
72   * error returned by a libasn1 function. 
73   **/
74 void libtasn1_perror(asn1_retCode error)
75 {
76         const char *ret = NULL;
77
78         /* avoid prefix */
79         LIBTASN1_ERROR_ALG_LOOP(ret =
80                               p->name + sizeof("ASN1_") - 1);
81
82         _libtasn1_log( "LIBTASN1 ERROR: %s\n", ret);
83         
84 }
85
86
87 /**
88   * libtasn1_strerror - Returns a string with a description of an error
89   * @error: is an error returned by a libtasn1 function.
90   *
91   * This function is similar to strerror(). The only difference is that it 
92   * accepts an error (number) returned by a libasn1 function. 
93   **/
94 const char* libtasn1_strerror(asn1_retCode error)
95 {
96         const char *ret = NULL;
97
98         /* avoid prefix */
99         LIBTASN1_ERROR_ALG_LOOP(ret =
100                               p->name + sizeof("ASN1_") - 1);
101
102         return ret;
103 }
104
105 /* this function will output a message.
106  */
107 #ifdef LIBTASN1_DEBUG
108 void _libtasn1_log( const char *fmt, ...) {
109  va_list args;
110  char str[MAX_LOG_SIZE];
111
112  va_start(args,fmt);
113  vsprintf( str,fmt,args); /* Flawfinder: ignore */
114  va_end(args);   
115
116  fprintf(stderr, str);
117
118  return;
119 }
120 #else /* not DEBUG */
121 # ifndef C99_MACROS
122
123 /* Without C99 macros these functions have to
124  * be called. This may affect performance.
125  */
126 void _libtasn1_null_log( void* x, ...) { return; }
127 # endif /* C99_MACROS */
128 #endif /* DEBUG */