~ubuntu-branches/ubuntu/trusty/libtasn1-3/trusty

« back to all changes in this revision

Viewing changes to lib/errors.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Metzler
  • Date: 2006-06-07 20:14:52 UTC
  • Revision ID: james.westby@ubuntu.com-20060607201452-7a9dojyu7i2kfbyp
Tags: upstream-0.3.4
ImportĀ upstreamĀ versionĀ 0.3.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *      Copyright (C) 2006 Free Software Foundation, Inc.
 
3
 *      Copyright (C) 2002, 2005 Fabio Fiorina
 
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
#include "errors.h"
 
25
#ifdef STDC_HEADERS
 
26
# include <stdarg.h>
 
27
#endif
 
28
 
 
29
 
 
30
#define LIBTASN1_ERROR_ENTRY(name) \
 
31
        { #name, name }
 
32
 
 
33
struct libtasn1_error_entry
 
34
{
 
35
  const char *name;
 
36
  int number;
 
37
};
 
38
typedef struct libtasn1_error_entry libtasn1_error_entry;
 
39
 
 
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}
 
60
};
 
61
 
 
62
#define LIBTASN1_ERROR_LOOP(b) \
 
63
        const libtasn1_error_entry *p; \
 
64
                for(p = error_algorithms; p->name != NULL; p++) { b ; }
 
65
 
 
66
#define LIBTASN1_ERROR_ALG_LOOP(a) \
 
67
                        LIBTASN1_ERROR_LOOP( if(p->number == error) { a; break; } )
 
68
 
 
69
 
 
70
 
 
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.
 
74
  *
 
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)
 
80
{
 
81
  const char *ret = NULL;
 
82
 
 
83
  /* avoid prefix */
 
84
  LIBTASN1_ERROR_ALG_LOOP (ret = p->name + sizeof ("ASN1_") - 1);
 
85
 
 
86
  fprintf (stderr, "LIBTASN1 ERROR: %s\n", ret);
 
87
 
 
88
}
 
89
 
 
90
 
 
91
/**
 
92
  * libtasn1_strerror - Returns a string with a description of an error
 
93
  * @error: is an error returned by a libtasn1 function.
 
94
  *
 
95
  * This function is similar to strerror(). The only difference is
 
96
  * that it accepts an error (number) returned by a libtasn1 function.
 
97
  *
 
98
  * Returns: Pointer to static zero-terminated string describing error
 
99
  *   code.
 
100
  **/
 
101
const char *
 
102
libtasn1_strerror (asn1_retCode error)
 
103
{
 
104
  const char *ret = NULL;
 
105
 
 
106
  /* avoid prefix */
 
107
  LIBTASN1_ERROR_ALG_LOOP (ret = p->name + sizeof ("ASN1_") - 1);
 
108
 
 
109
  return ret;
 
110
}
 
111
 
 
112
/* this function will output a message.
 
113
 */
 
114
#ifdef LIBTASN1_DEBUG
 
115
void
 
116
_libtasn1_log (const char *fmt, ...)
 
117
{
 
118
  va_list args;
 
119
  char str[MAX_LOG_SIZE];
 
120
 
 
121
  va_start (args, fmt);
 
122
  vsprintf (str, fmt, args);    /* Flawfinder: ignore */
 
123
  va_end (args);
 
124
 
 
125
  fprintf (stderr, str);
 
126
 
 
127
  return;
 
128
}
 
129
#else /* not DEBUG */
 
130
void
 
131
_libtasn1_log (const char *fmt, ...)
 
132
{
 
133
  return;
 
134
}
 
135
#endif /* DEBUG */