Initialize Tizen 2.3
[external/libtasn1.git] / lib / errors.c
1 /*
2  * Copyright (C) 2002, 2005, 2006, 2008, 2009, 2010 Free Software
3  * Foundation, Inc.
4  *
5  * This file is part of LIBTASN1.
6  *
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.
11  *
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.
16  *
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
21  */
22
23 #include <int.h>
24 #ifdef STDC_HEADERS
25 # include <stdarg.h>
26 #endif
27
28 #define LIBTASN1_ERROR_ENTRY(name) { #name, name }
29
30 struct libtasn1_error_entry
31 {
32   const char *name;
33   int number;
34 };
35 typedef struct libtasn1_error_entry libtasn1_error_entry;
36
37 static const libtasn1_error_entry error_algorithms[] = {
38   LIBTASN1_ERROR_ENTRY (ASN1_SUCCESS),
39   LIBTASN1_ERROR_ENTRY (ASN1_FILE_NOT_FOUND),
40   LIBTASN1_ERROR_ENTRY (ASN1_ELEMENT_NOT_FOUND),
41   LIBTASN1_ERROR_ENTRY (ASN1_IDENTIFIER_NOT_FOUND),
42   LIBTASN1_ERROR_ENTRY (ASN1_DER_ERROR),
43   LIBTASN1_ERROR_ENTRY (ASN1_VALUE_NOT_FOUND),
44   LIBTASN1_ERROR_ENTRY (ASN1_GENERIC_ERROR),
45   LIBTASN1_ERROR_ENTRY (ASN1_VALUE_NOT_VALID),
46   LIBTASN1_ERROR_ENTRY (ASN1_TAG_ERROR),
47   LIBTASN1_ERROR_ENTRY (ASN1_TAG_IMPLICIT),
48   LIBTASN1_ERROR_ENTRY (ASN1_ERROR_TYPE_ANY),
49   LIBTASN1_ERROR_ENTRY (ASN1_SYNTAX_ERROR),
50   LIBTASN1_ERROR_ENTRY (ASN1_MEM_ERROR),
51   LIBTASN1_ERROR_ENTRY (ASN1_MEM_ALLOC_ERROR),
52   LIBTASN1_ERROR_ENTRY (ASN1_DER_OVERFLOW),
53   LIBTASN1_ERROR_ENTRY (ASN1_NAME_TOO_LONG),
54   LIBTASN1_ERROR_ENTRY (ASN1_ARRAY_ERROR),
55   LIBTASN1_ERROR_ENTRY (ASN1_ELEMENT_NOT_EMPTY),
56   {0, 0}
57 };
58
59 /**
60  * asn1_perror:
61  * @error: is an error returned by a libtasn1 function.
62  *
63  * Prints a string to stderr with a description of an error.  This
64  * function is like perror().  The only difference is that it accepts
65  * an error returned by a libtasn1 function.
66  *
67  * This function replaces libtasn1_perror() in older libtasn1.
68  *
69  * Since: 1.6
70  **/
71 void
72 asn1_perror (asn1_retCode error)
73 {
74   const char *str = asn1_strerror (error);
75   fprintf (stderr, "LIBTASN1 ERROR: %s\n", str ? str : "(null)");
76 }
77
78 /**
79  * asn1_strerror:
80  * @error: is an error returned by a libtasn1 function.
81  *
82  * Returns a string with a description of an error.  This function is
83  * similar to strerror.  The only difference is that it accepts an
84  * error (number) returned by a libtasn1 function.
85  *
86  * This function replaces libtasn1_strerror() in older libtasn1.
87  *
88  * Returns: Pointer to static zero-terminated string describing error
89  *   code.
90  *
91  * Since: 1.6
92  **/
93 const char *
94 asn1_strerror (asn1_retCode error)
95 {
96   const libtasn1_error_entry *p;
97
98   for (p = error_algorithms; p->name != NULL; p++)
99     if (p->number == error)
100       return p->name + sizeof ("ASN1_") - 1;
101
102   return NULL;
103 }
104
105 #ifndef ASN1_DISABLE_DEPRECATED
106
107 /* Compatibility mappings to preserve ABI. */
108
109 /**
110  * libtasn1_perror:
111  * @error: is an error returned by a libtasn1 function.
112  *
113  * Prints a string to stderr with a description of an error.  This
114  * function is like perror(). The only difference is that it accepts
115  * an error returned by a libtasn1 function.
116  *
117  * Deprecated: Use asn1_perror() instead.
118  **/
119 void
120 libtasn1_perror (asn1_retCode error)
121 {
122   asn1_perror (error);
123 }
124
125 /**
126  * libtasn1_strerror:
127  * @error: is an error returned by a libtasn1 function.
128  *
129  * Returns a string with a description of an error.  This function is
130  * similar to strerror.  The only difference is that it accepts an
131  * error (number) returned by a libtasn1 function.
132  *
133  * Returns: Pointer to static zero-terminated string describing error
134  *   code.
135  *
136  * Deprecated: Use asn1_strerror() instead.
137  **/
138 const char *
139 libtasn1_strerror (asn1_retCode error)
140 {
141   return asn1_strerror (error);
142 }
143
144 #endif