Update.
[libtasn1.git] / lib / errors.c
blobb1224da03a37df1a6354c2f1e5ccda683552cc0d
1 /*
2 * Copyright (C) 2006 Free Software Foundation, Inc.
3 * Copyright (C) 2002, 2005 Fabio Fiorina
5 * This file is part of LIBTASN1.
7 * The LIBTASN1 library is free software; you can redistribute it
8 * and/or modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 * 02110-1301, USA
23 #include <int.h>
24 #include "errors.h"
25 #ifdef STDC_HEADERS
26 # include <stdarg.h>
27 #endif
30 #define LIBTASN1_ERROR_ENTRY(name) \
31 { #name, name }
33 struct libtasn1_error_entry {
34 const char *name;
35 int number;
37 typedef struct libtasn1_error_entry libtasn1_error_entry;
39 static const libtasn1_error_entry error_algorithms[] = {
40 LIBTASN1_ERROR_ENTRY( ASN1_SUCCESS ),
41 LIBTASN1_ERROR_ENTRY( ASN1_FILE_NOT_FOUND ),
42 LIBTASN1_ERROR_ENTRY( ASN1_ELEMENT_NOT_FOUND ),
43 LIBTASN1_ERROR_ENTRY( ASN1_IDENTIFIER_NOT_FOUND ),
44 LIBTASN1_ERROR_ENTRY( ASN1_DER_ERROR ),
45 LIBTASN1_ERROR_ENTRY( ASN1_VALUE_NOT_FOUND ),
46 LIBTASN1_ERROR_ENTRY( ASN1_GENERIC_ERROR ),
47 LIBTASN1_ERROR_ENTRY( ASN1_VALUE_NOT_VALID ),
48 LIBTASN1_ERROR_ENTRY( ASN1_TAG_ERROR ),
49 LIBTASN1_ERROR_ENTRY( ASN1_TAG_IMPLICIT ),
50 LIBTASN1_ERROR_ENTRY( ASN1_ERROR_TYPE_ANY ),
51 LIBTASN1_ERROR_ENTRY( ASN1_SYNTAX_ERROR ),
52 LIBTASN1_ERROR_ENTRY( ASN1_MEM_ERROR ),
53 LIBTASN1_ERROR_ENTRY( ASN1_MEM_ALLOC_ERROR ),
54 LIBTASN1_ERROR_ENTRY( ASN1_DER_OVERFLOW ),
55 LIBTASN1_ERROR_ENTRY( ASN1_NAME_TOO_LONG ),
56 LIBTASN1_ERROR_ENTRY( ASN1_ARRAY_ERROR ),
57 LIBTASN1_ERROR_ENTRY( ASN1_ELEMENT_NOT_EMPTY ),
58 {0}
61 #define LIBTASN1_ERROR_LOOP(b) \
62 const libtasn1_error_entry *p; \
63 for(p = error_algorithms; p->name != NULL; p++) { b ; }
65 #define LIBTASN1_ERROR_ALG_LOOP(a) \
66 LIBTASN1_ERROR_LOOP( if(p->number == error) { a; break; } )
70 /**
71 * libtasn1_perror - prints a string to stderr with a description of an error
72 * @error: is an error returned by a libtasn1 function.
74 * This function is like perror(). The only difference is that it
75 * accepts an error returned by a libtasn1 function.
76 **/
77 void libtasn1_perror(asn1_retCode error)
79 const char *ret = NULL;
81 /* avoid prefix */
82 LIBTASN1_ERROR_ALG_LOOP(ret =
83 p->name + sizeof("ASN1_") - 1);
85 _libtasn1_log( "LIBTASN1 ERROR: %s\n", ret);
90 /**
91 * libtasn1_strerror - Returns a string with a description of an error
92 * @error: is an error returned by a libtasn1 function.
94 * This function is similar to strerror(). The only difference is
95 * that it accepts an error (number) returned by a libtasn1 function.
97 * Returns: Pointer to static zero-terminated string describing error
98 * code.
99 **/
100 const char* libtasn1_strerror(asn1_retCode error)
102 const char *ret = NULL;
104 /* avoid prefix */
105 LIBTASN1_ERROR_ALG_LOOP(ret =
106 p->name + sizeof("ASN1_") - 1);
108 return ret;
111 /* this function will output a message.
113 #ifdef LIBTASN1_DEBUG
114 void _libtasn1_log( const char *fmt, ...) {
115 va_list args;
116 char str[MAX_LOG_SIZE];
118 va_start(args,fmt);
119 vsprintf( str,fmt,args); /* Flawfinder: ignore */
120 va_end(args);
122 fprintf(stderr, str);
124 return;
126 #else /* not DEBUG */
127 void _libtasn1_log( const char *fmt, ...) {
128 return;
130 #endif /* DEBUG */