*** empty log message ***
[libtasn1.git] / lib / errors.c
blobf1e0d5fca070773c4c78d5003353c503ece17266
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
35 const char *name;
36 int number;
38 typedef struct libtasn1_error_entry libtasn1_error_entry;
40 static const libtasn1_error_entry error_algorithms[] = {
41 LIBTASN1_ERROR_ENTRY (ASN1_SUCCESS),
42 LIBTASN1_ERROR_ENTRY (ASN1_FILE_NOT_FOUND),
43 LIBTASN1_ERROR_ENTRY (ASN1_ELEMENT_NOT_FOUND),
44 LIBTASN1_ERROR_ENTRY (ASN1_IDENTIFIER_NOT_FOUND),
45 LIBTASN1_ERROR_ENTRY (ASN1_DER_ERROR),
46 LIBTASN1_ERROR_ENTRY (ASN1_VALUE_NOT_FOUND),
47 LIBTASN1_ERROR_ENTRY (ASN1_GENERIC_ERROR),
48 LIBTASN1_ERROR_ENTRY (ASN1_VALUE_NOT_VALID),
49 LIBTASN1_ERROR_ENTRY (ASN1_TAG_ERROR),
50 LIBTASN1_ERROR_ENTRY (ASN1_TAG_IMPLICIT),
51 LIBTASN1_ERROR_ENTRY (ASN1_ERROR_TYPE_ANY),
52 LIBTASN1_ERROR_ENTRY (ASN1_SYNTAX_ERROR),
53 LIBTASN1_ERROR_ENTRY (ASN1_MEM_ERROR),
54 LIBTASN1_ERROR_ENTRY (ASN1_MEM_ALLOC_ERROR),
55 LIBTASN1_ERROR_ENTRY (ASN1_DER_OVERFLOW),
56 LIBTASN1_ERROR_ENTRY (ASN1_NAME_TOO_LONG),
57 LIBTASN1_ERROR_ENTRY (ASN1_ARRAY_ERROR),
58 LIBTASN1_ERROR_ENTRY (ASN1_ELEMENT_NOT_EMPTY),
59 {0}
62 #define LIBTASN1_ERROR_LOOP(b) \
63 const libtasn1_error_entry *p; \
64 for(p = error_algorithms; p->name != NULL; p++) { b ; }
66 #define LIBTASN1_ERROR_ALG_LOOP(a) \
67 LIBTASN1_ERROR_LOOP( if(p->number == error) { a; break; } )
71 /**
72 * libtasn1_perror - prints a string to stderr with a description of an error
73 * @error: is an error returned by a libtasn1 function.
75 * This function is like perror(). The only difference is that it
76 * accepts an error returned by a libtasn1 function.
77 **/
78 void
79 libtasn1_perror (asn1_retCode error)
81 const char *ret = NULL;
83 /* avoid prefix */
84 LIBTASN1_ERROR_ALG_LOOP (ret = p->name + sizeof ("ASN1_") - 1);
86 _libtasn1_log ("LIBTASN1 ERROR: %s\n", ret);
91 /**
92 * libtasn1_strerror - Returns a string with a description of an error
93 * @error: is an error returned by a libtasn1 function.
95 * This function is similar to strerror(). The only difference is
96 * that it accepts an error (number) returned by a libtasn1 function.
98 * Returns: Pointer to static zero-terminated string describing error
99 * code.
101 const char *
102 libtasn1_strerror (asn1_retCode error)
104 const char *ret = NULL;
106 /* avoid prefix */
107 LIBTASN1_ERROR_ALG_LOOP (ret = p->name + sizeof ("ASN1_") - 1);
109 return ret;
112 /* this function will output a message.
114 #ifdef LIBTASN1_DEBUG
115 void
116 _libtasn1_log (const char *fmt, ...)
118 va_list args;
119 char str[MAX_LOG_SIZE];
121 va_start (args, fmt);
122 vsprintf (str, fmt, args); /* Flawfinder: ignore */
123 va_end (args);
125 fprintf (stderr, str);
127 return;
129 #else /* not DEBUG */
130 void
131 _libtasn1_log (const char *fmt, ...)
133 return;
135 #endif /* DEBUG */