summaryrefslogtreecommitdiff
path: root/lib/minitasn1/errors.c
blob: 271158d9d84f6653283f7548102f8f7f1613bf5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
 * Copyright (C) 2002, 2005, 2006, 2008, 2009, 2010 Free Software
 * Foundation, Inc.
 *
 * This file is part of LIBTASN1.
 *
 * The LIBTASN1 library is free software; you can redistribute it
 * and/or modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA
 */

#include <int.h>
#ifdef STDC_HEADERS
# include <stdarg.h>
#endif

#define LIBTASN1_ERROR_ENTRY(name) { #name, name }

struct libtasn1_error_entry
{
  const char *name;
  int number;
};
typedef struct libtasn1_error_entry libtasn1_error_entry;

static const libtasn1_error_entry error_algorithms[] = {
  LIBTASN1_ERROR_ENTRY (ASN1_SUCCESS),
  LIBTASN1_ERROR_ENTRY (ASN1_FILE_NOT_FOUND),
  LIBTASN1_ERROR_ENTRY (ASN1_ELEMENT_NOT_FOUND),
  LIBTASN1_ERROR_ENTRY (ASN1_IDENTIFIER_NOT_FOUND),
  LIBTASN1_ERROR_ENTRY (ASN1_DER_ERROR),
  LIBTASN1_ERROR_ENTRY (ASN1_VALUE_NOT_FOUND),
  LIBTASN1_ERROR_ENTRY (ASN1_GENERIC_ERROR),
  LIBTASN1_ERROR_ENTRY (ASN1_VALUE_NOT_VALID),
  LIBTASN1_ERROR_ENTRY (ASN1_TAG_ERROR),
  LIBTASN1_ERROR_ENTRY (ASN1_TAG_IMPLICIT),
  LIBTASN1_ERROR_ENTRY (ASN1_ERROR_TYPE_ANY),
  LIBTASN1_ERROR_ENTRY (ASN1_SYNTAX_ERROR),
  LIBTASN1_ERROR_ENTRY (ASN1_MEM_ERROR),
  LIBTASN1_ERROR_ENTRY (ASN1_MEM_ALLOC_ERROR),
  LIBTASN1_ERROR_ENTRY (ASN1_DER_OVERFLOW),
  LIBTASN1_ERROR_ENTRY (ASN1_NAME_TOO_LONG),
  LIBTASN1_ERROR_ENTRY (ASN1_ARRAY_ERROR),
  LIBTASN1_ERROR_ENTRY (ASN1_ELEMENT_NOT_EMPTY),
  {0, 0}
};

/**
 * asn1_perror:
 * @error: is an error returned by a libtasn1 function.
 *
 * Prints a string to stderr with a description of an error.  This
 * function is like perror().  The only difference is that it accepts
 * an error returned by a libtasn1 function.
 *
 * This function replaces libtasn1_perror() in older libtasn1.
 *
 * Since: 1.6
 **/
void
asn1_perror (asn1_retCode error)
{
  const char *str = asn1_strerror (error);
  fprintf (stderr, "LIBTASN1 ERROR: %s\n", str ? str : "(null)");
}

/**
 * asn1_strerror:
 * @error: is an error returned by a libtasn1 function.
 *
 * Returns a string with a description of an error.  This function is
 * similar to strerror.  The only difference is that it accepts an
 * error (number) returned by a libtasn1 function.
 *
 * This function replaces libtasn1_strerror() in older libtasn1.
 *
 * Returns: Pointer to static (0)-terminated string describing error
 *   code.
 *
 * Since: 1.6
 **/
const char *
asn1_strerror (asn1_retCode error)
{
  const libtasn1_error_entry *p;

  for (p = error_algorithms; p->name != NULL; p++)
    if (p->number == error)
      return p->name + sizeof ("ASN1_") - 1;

  return NULL;
}

#ifndef ASN1_DISABLE_DEPRECATED

/* Compatibility mappings to preserve ABI. */

/**
 * libtasn1_perror:
 * @error: is an error returned by a libtasn1 function.
 *
 * Prints a string to stderr with a description of an error.  This
 * function is like perror(). The only difference is that it accepts
 * an error returned by a libtasn1 function.
 *
 * Deprecated: Use asn1_perror() instead.
 **/
void
libtasn1_perror (asn1_retCode error)
{
  asn1_perror (error);
}

/**
 * libtasn1_strerror:
 * @error: is an error returned by a libtasn1 function.
 *
 * Returns a string with a description of an error.  This function is
 * similar to strerror.  The only difference is that it accepts an
 * error (number) returned by a libtasn1 function.
 *
 * Returns: Pointer to static (0)-terminated string describing error
 *   code.
 *
 * Deprecated: Use asn1_strerror() instead.
 **/
const char *
libtasn1_strerror (asn1_retCode error)
{
  return asn1_strerror (error);
}

#endif