File: errors.c

package info (click to toggle)
oath-toolkit 2.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 20,824 kB
  • ctags: 9,237
  • sloc: ansic: 54,713; sh: 13,738; yacc: 1,254; xml: 756; makefile: 353
file content (110 lines) | stat: -rw-r--r-- 3,229 bytes parent folder | download | duplicates (2)
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
/*
 * error.c - libpskc error handling helpers.
 * Copyright (C) 2012-2015 Simon Josefsson
 *
 * This 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 <config.h>

#include <pskc/pskc.h>
#include "internal.h"

#define ERR(name, desc) { name, #name, desc }

typedef struct
{
  int rc;
  const char *name;
  const char *description;
} err_t;

static const err_t errors[] = {
  ERR (PSKC_OK, "success"),
  ERR (PSKC_MALLOC_ERROR, "memory allocation failed"),
  ERR (PSKC_XML_ERROR, "error returned from XML library"),
  ERR (PSKC_PARSE_ERROR, "error parsing PSKC data"),
  ERR (PSKC_BASE64_ERROR, "error base64 decoding data"),
  ERR (PSKC_UNKNOWN_OUTPUT_FORMAT, "unknown output format"),
  ERR (PSKC_XMLSEC_ERROR, "error returned from XMLSec library")
};

/**
 * pskc_strerror:
 * @err: error code, a #pskc_rc value.
 *
 * Convert return code to human readable string explanation of the
 * reason for the particular error code.
 *
 * This string can be used to output a diagnostic message to the user.
 *
 * This function is one of few in the library that can be used without
 * a successful call to pskc_init().
 *
 * Return value: Returns a pointer to a statically allocated string
 *   containing an explanation of the error code @err.
 **/
const char *
pskc_strerror (int err)
{
  static const char *unknown = "Libpskc unknown error";
  const char *p;

  if (-err < 0 || -err >= (int) (sizeof (errors) / sizeof (errors[0])))
    {
      _pskc_debug ("out of range error code %d", err);
      return unknown;
    }

  p = errors[-err].description;
  if (!p)
    {
      _pskc_debug ("missing description field for error code %d", err);
      p = unknown;
    }

  return p;
}


/**
 * pskc_strerror_name:
 * @err: error code, a #pskc_rc value.
 *
 * Convert return code to human readable string representing the error
 * code symbol itself.  For example, pskc_strerror_name(%PSKC_OK)
 * returns the string "PSKC_OK".
 *
 * This string can be used to output a diagnostic message to the user.
 *
 * This function is one of few in the library that can be used without
 * a successful call to pskc_init().
 *
 * Return value: Returns a pointer to a statically allocated string
 *   containing a string version of the error code @err, or NULL if
 *   the error code is not known.
 **/
const char *
pskc_strerror_name (int err)
{
  if (-err < 0 || -err >= (int) (sizeof (errors) / sizeof (errors[0])))
    {
      _pskc_debug ("attempted naming out of range error code %d", err);
      return NULL;
    }

  return errors[-err].name;
}