File: errors.c

package info (click to toggle)
oath-toolkit 2.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 20,192 kB
  • ctags: 9,804
  • sloc: ansic: 52,211; sh: 13,751; yacc: 1,252; xml: 756; makefile: 350
file content (122 lines) | stat: -rw-r--r-- 4,205 bytes parent folder | download
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
/* error.c - liboath error handling helpers.
 * Copyright (C) 2011-2013 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 "oath.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 (OATH_OK, "Successful return"),
  ERR (OATH_CRYPTO_ERROR, "Internal error in crypto functions"),
  ERR (OATH_INVALID_DIGITS, "Unsupported number of OTP digits"),
  ERR (OATH_PRINTF_ERROR, "Error from system printf call"),
  ERR (OATH_INVALID_HEX, "Hex string is invalid"),
  ERR (OATH_TOO_SMALL_BUFFER, "The output buffer is too small"),
  ERR (OATH_INVALID_OTP, "The OTP is not valid"),
  ERR (OATH_REPLAYED_OTP, "The OTP has been replayed"),
  ERR (OATH_BAD_PASSWORD, "The password does not match"),
  ERR (OATH_INVALID_COUNTER, "The counter value is corrupt"),
  ERR (OATH_INVALID_TIMESTAMP, "The timestamp is corrupt"),
  ERR (OATH_NO_SUCH_FILE, "The supplied filename does not exist"),
  ERR (OATH_UNKNOWN_USER, "Cannot find information about user"),
  ERR (OATH_FILE_SEEK_ERROR, "System error when seeking in file"),
  ERR (OATH_FILE_CREATE_ERROR, "System error when creating file"),
  ERR (OATH_FILE_LOCK_ERROR, "System error when locking file"),
  ERR (OATH_FILE_RENAME_ERROR, "System error when renaming file"),
  ERR (OATH_FILE_UNLINK_ERROR, "System error when removing file"),
  ERR (OATH_TIME_ERROR, "System error for time manipulation"),
  ERR (OATH_STRCMP_ERROR, "A strcmp callback returned an error"),
  ERR (OATH_INVALID_BASE32, "Base32 string is invalid"),
  ERR (OATH_BASE32_OVERFLOW, "Base32 encoding would overflow"),
  ERR (OATH_MALLOC_ERROR, "Memory allocation failed"),
  ERR (OATH_FILE_FLUSH_ERROR, "System error when flushing file buffer"),
  ERR (OATH_FILE_SYNC_ERROR, "System error when syncing file to disk"),
  ERR (OATH_FILE_CLOSE_ERROR, "System error when closing file")
};

/**
 * oath_strerror:
 * @err: liboath error code
 *
 * 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 oath_init().
 *
 * Return value: Returns a pointer to a statically allocated string
 *   containing an explanation of the error code @err.
 *
 * Since: 1.8.0
 **/
const char *
oath_strerror (int err)
{
  static const char *unknown = "Liboath unknown error";
  const char *p;

  if (-err < 0 || -err >= (int) (sizeof (errors) / sizeof (errors[0])))
    return unknown;

  p = errors[-err].description;
  if (!p)
    p = unknown;

  return p;
}


/**
 * oath_strerror_name:
 * @err: liboath error code
 *
 * Convert return code to human readable string representing the error
 * code symbol itself.  For example, oath_strerror_name(%OATH_OK)
 * returns the string "OATH_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 oath_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.
 *
 * Since: 1.8.0
 **/
const char *
oath_strerror_name (int err)
{
  if (-err < 0 || -err >= (int) (sizeof (errors) / sizeof (errors[0])))
    return NULL;

  return errors[-err].name;
}