/src/libtasn1/lib/errors.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (C) 2002-2026 Free Software Foundation, Inc. |
3 | | * |
4 | | * This file is part of LIBTASN1. |
5 | | * |
6 | | * The LIBTASN1 library is free software; you can redistribute it |
7 | | * and/or modify it under the terms of the GNU Lesser General Public |
8 | | * License as published by the Free Software Foundation; either |
9 | | * version 2.1 of the License, or (at your option) any later version. |
10 | | * |
11 | | * This library is distributed in the hope that it will be useful, but |
12 | | * WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | | * Lesser General Public License for more details. |
15 | | * |
16 | | * You should have received a copy of the GNU Lesser General Public |
17 | | * License along with this library; if not, see |
18 | | * <https://www.gnu.org/licenses/>. |
19 | | */ |
20 | | |
21 | | #include <config.h> |
22 | | |
23 | | #include "int.h" |
24 | | #include <stdarg.h> |
25 | | |
26 | | #define LIBTASN1_ERROR_ENTRY(name) { #name, name } |
27 | | |
28 | | struct libtasn1_error_entry |
29 | | { |
30 | | const char *name; |
31 | | int number; |
32 | | }; |
33 | | typedef struct libtasn1_error_entry libtasn1_error_entry; |
34 | | |
35 | | static const libtasn1_error_entry error_algorithms[] = { |
36 | | LIBTASN1_ERROR_ENTRY (ASN1_SUCCESS), |
37 | | LIBTASN1_ERROR_ENTRY (ASN1_FILE_NOT_FOUND), |
38 | | LIBTASN1_ERROR_ENTRY (ASN1_ELEMENT_NOT_FOUND), |
39 | | LIBTASN1_ERROR_ENTRY (ASN1_IDENTIFIER_NOT_FOUND), |
40 | | LIBTASN1_ERROR_ENTRY (ASN1_DER_ERROR), |
41 | | LIBTASN1_ERROR_ENTRY (ASN1_VALUE_NOT_FOUND), |
42 | | LIBTASN1_ERROR_ENTRY (ASN1_GENERIC_ERROR), |
43 | | LIBTASN1_ERROR_ENTRY (ASN1_VALUE_NOT_VALID), |
44 | | LIBTASN1_ERROR_ENTRY (ASN1_TAG_ERROR), |
45 | | LIBTASN1_ERROR_ENTRY (ASN1_TAG_IMPLICIT), |
46 | | LIBTASN1_ERROR_ENTRY (ASN1_ERROR_TYPE_ANY), |
47 | | LIBTASN1_ERROR_ENTRY (ASN1_SYNTAX_ERROR), |
48 | | LIBTASN1_ERROR_ENTRY (ASN1_MEM_ERROR), |
49 | | LIBTASN1_ERROR_ENTRY (ASN1_MEM_ALLOC_ERROR), |
50 | | LIBTASN1_ERROR_ENTRY (ASN1_DER_OVERFLOW), |
51 | | LIBTASN1_ERROR_ENTRY (ASN1_NAME_TOO_LONG), |
52 | | LIBTASN1_ERROR_ENTRY (ASN1_ARRAY_ERROR), |
53 | | LIBTASN1_ERROR_ENTRY (ASN1_ELEMENT_NOT_EMPTY), |
54 | | LIBTASN1_ERROR_ENTRY (ASN1_TIME_ENCODING_ERROR), |
55 | | LIBTASN1_ERROR_ENTRY (ASN1_RECURSION), |
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 | | * Since: 1.6 |
68 | | **/ |
69 | | void |
70 | | asn1_perror (int error) |
71 | 0 | { |
72 | 0 | const char *str = asn1_strerror (error); |
73 | 0 | fprintf (stderr, "LIBTASN1 ERROR: %s\n", str ? str : "(null)"); |
74 | 0 | } |
75 | | |
76 | | /** |
77 | | * asn1_strerror: |
78 | | * @error: is an error returned by a libtasn1 function. |
79 | | * |
80 | | * Returns a string with a description of an error. This function is |
81 | | * similar to strerror. The only difference is that it accepts an |
82 | | * error (number) returned by a libtasn1 function. |
83 | | * |
84 | | * Returns: Pointer to static zero-terminated string describing error |
85 | | * code. |
86 | | * |
87 | | * Since: 1.6 |
88 | | **/ |
89 | | const char * |
90 | | asn1_strerror (int error) |
91 | 0 | { |
92 | 0 | const libtasn1_error_entry *p; |
93 | |
|
94 | 0 | for (p = error_algorithms; p->name != NULL; p++) |
95 | 0 | if (p->number == error) |
96 | 0 | return p->name + sizeof ("ASN1_") - 1; |
97 | | |
98 | 0 | return NULL; |
99 | 0 | } |