Coverage Report

Created: 2025-01-28 06:58

/src/libidn2/lib/error.c
Line
Count
Source (jump to first uncovered line)
1
/* error.c - libidn2 error handling helpers.
2
   Copyright (C) 2011-2024 Simon Josefsson
3
4
   Libidn2 is free software: you can redistribute it and/or modify it
5
   under the terms of either:
6
7
     * the GNU Lesser General Public License as published by the Free
8
       Software Foundation; either version 3 of the License, or (at
9
       your option) any later version.
10
11
   or
12
13
     * the GNU General Public License as published by the Free
14
       Software Foundation; either version 2 of the License, or (at
15
       your option) any later version.
16
17
   or both in parallel, as here.
18
19
   This program is distributed in the hope that it will be useful,
20
   but WITHOUT ANY WARRANTY; without even the implied warranty of
21
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
   GNU General Public License for more details.
23
24
   You should have received copies of the GNU General Public License and
25
   the GNU Lesser General Public License along with this program.  If
26
   not, see <http://www.gnu.org/licenses/>.
27
*/
28
29
#include <config.h>
30
31
#include "idn2.h"
32
33
/* Prepare for gettext. */
34
14.4k
#define _(x) x
35
#define bindtextdomain(a,b)
36
37
/**
38
 * idn2_strerror:
39
 * @rc: return code from another libidn2 function.
40
 *
41
 * Convert internal libidn2 error code to a humanly readable string.
42
 * The returned pointer must not be de-allocated by the caller.
43
 *
44
 * Return value: A humanly readable string describing error.
45
 **/
46
const char *
47
idn2_strerror (int rc)
48
14.4k
{
49
14.4k
  bindtextdomain (PACKAGE, LOCALEDIR);
50
51
14.4k
  switch (rc)
52
14.4k
    {
53
0
    case IDN2_OK:
54
0
      return _("success");
55
0
    case IDN2_MALLOC:
56
0
      return _("out of memory");
57
0
    case IDN2_NO_CODESET:
58
0
      return _("could not determine locale encoding format");
59
0
    case IDN2_ICONV_FAIL:
60
0
      return _("could not convert string to UTF-8");
61
8.61k
    case IDN2_ENCODING_ERROR:
62
8.61k
      return _("string encoding error");
63
0
    case IDN2_NFC:
64
0
      return _("string could not be NFC normalized");
65
1.01k
    case IDN2_PUNYCODE_BAD_INPUT:
66
1.01k
      return _("string contains invalid punycode data");
67
1.03k
    case IDN2_PUNYCODE_BIG_OUTPUT:
68
1.03k
      return _("punycode encoded data will be too large");
69
295
    case IDN2_PUNYCODE_OVERFLOW:
70
295
      return _("punycode conversion resulted in overflow");
71
422
    case IDN2_TOO_BIG_DOMAIN:
72
422
      return _("domain name longer than 255 characters");
73
307
    case IDN2_TOO_BIG_LABEL:
74
307
      return _("domain label longer than 63 characters");
75
0
    case IDN2_INVALID_ALABEL:
76
0
      return _("input A-label is not valid");
77
0
    case IDN2_UALABEL_MISMATCH:
78
0
      return _("input A-label and U-label does not match");
79
679
    case IDN2_NOT_NFC:
80
679
      return _("string is not in Unicode NFC format");
81
350
    case IDN2_2HYPHEN:
82
350
      return _("string contains forbidden two hyphens pattern");
83
462
    case IDN2_HYPHEN_STARTEND:
84
462
      return _("string start/ends with forbidden hyphen");
85
421
    case IDN2_LEADING_COMBINING:
86
421
      return _("string contains a forbidden leading combining character");
87
326
    case IDN2_DISALLOWED:
88
326
      return _("string contains a disallowed character");
89
0
    case IDN2_CONTEXTJ:
90
0
      return _("string contains a forbidden context-j character");
91
0
    case IDN2_CONTEXTJ_NO_RULE:
92
0
      return _("string contains a context-j character with null rule");
93
0
    case IDN2_CONTEXTO:
94
0
      return _("string contains a forbidden context-o character");
95
0
    case IDN2_CONTEXTO_NO_RULE:
96
0
      return _("string contains a context-o character with null rule");
97
0
    case IDN2_UNASSIGNED:
98
0
      return _("string contains unassigned code point");
99
0
    case IDN2_BIDI:
100
0
      return _("string has forbidden bi-directional properties");
101
0
    case IDN2_DOT_IN_LABEL:
102
0
      return _("domain label has forbidden dot (TR46)");
103
0
    case IDN2_INVALID_TRANSITIONAL:
104
0
      return
105
0
  _("domain label has character forbidden in transitional mode (TR46)");
106
504
    case IDN2_INVALID_NONTRANSITIONAL:
107
504
      return
108
504
  _
109
0
  ("domain label has character forbidden in non-transitional mode (TR46)");
110
0
    case IDN2_ALABEL_ROUNDTRIP_FAILED:
111
0
      return _("A-label roundtrip failed");
112
0
    default:
113
0
      return _("Unknown error");
114
14.4k
    }
115
14.4k
}
116
117
0
#define ERR2STR(name) #name
118
119
/**
120
 * idn2_strerror_name:
121
 * @rc: return code from another libidn2 function.
122
 *
123
 * Convert internal libidn2 error code to a string corresponding to
124
 * internal header file symbols.  For example,
125
 * idn2_strerror_name(IDN2_MALLOC) will return the string
126
 * "IDN2_MALLOC".
127
 *
128
 * The caller must not attempt to de-allocate the returned string.
129
 *
130
 * Return value: A string corresponding to error code symbol.
131
 **/
132
const char *
133
idn2_strerror_name (int rc)
134
0
{
135
0
  switch (rc)
136
0
    {
137
0
    case IDN2_OK:
138
0
      return ERR2STR (IDN2_OK);
139
0
    case IDN2_MALLOC:
140
0
      return ERR2STR (IDN2_MALLOC);
141
0
    case IDN2_NO_CODESET:
142
0
      return ERR2STR (IDN2_NO_NODESET);
143
0
    case IDN2_ICONV_FAIL:
144
0
      return ERR2STR (IDN2_ICONV_FAIL);
145
0
    case IDN2_ENCODING_ERROR:
146
0
      return ERR2STR (IDN2_ENCODING_ERROR);
147
0
    case IDN2_NFC:
148
0
      return ERR2STR (IDN2_NFC);
149
0
    case IDN2_PUNYCODE_BAD_INPUT:
150
0
      return ERR2STR (IDN2_PUNYCODE_BAD_INPUT);
151
0
    case IDN2_PUNYCODE_BIG_OUTPUT:
152
0
      return ERR2STR (IDN2_PUNYCODE_BIG_OUTPUT);
153
0
    case IDN2_PUNYCODE_OVERFLOW:
154
0
      return ERR2STR (IDN2_PUNYCODE_OVERFLOW);
155
0
    case IDN2_TOO_BIG_DOMAIN:
156
0
      return ERR2STR (IDN2_TOO_BIG_DOMAIN);
157
0
    case IDN2_TOO_BIG_LABEL:
158
0
      return ERR2STR (IDN2_TOO_BIG_LABEL);
159
0
    case IDN2_INVALID_ALABEL:
160
0
      return ERR2STR (IDN2_INVALID_ALABEL);
161
0
    case IDN2_UALABEL_MISMATCH:
162
0
      return ERR2STR (IDN2_UALABEL_MISMATCH);
163
0
    case IDN2_INVALID_FLAGS:
164
0
      return ERR2STR (IDN2_INVALID_FLAGS);
165
0
    case IDN2_NOT_NFC:
166
0
      return ERR2STR (IDN2_NOT_NFC);
167
0
    case IDN2_2HYPHEN:
168
0
      return ERR2STR (IDN2_2HYPHEN);
169
0
    case IDN2_HYPHEN_STARTEND:
170
0
      return ERR2STR (IDN2_HYPHEN_STARTEND);
171
0
    case IDN2_LEADING_COMBINING:
172
0
      return ERR2STR (IDN2_LEADING_COMBINING);
173
0
    case IDN2_DISALLOWED:
174
0
      return ERR2STR (IDN2_DISALLOWED);
175
0
    case IDN2_CONTEXTJ:
176
0
      return ERR2STR (IDN2_CONTEXTJ);
177
0
    case IDN2_CONTEXTJ_NO_RULE:
178
0
      return ERR2STR (IDN2_CONTEXTJ_NO_RULE);
179
0
    case IDN2_CONTEXTO:
180
0
      return ERR2STR (IDN2_CONTEXTO);
181
0
    case IDN2_CONTEXTO_NO_RULE:
182
0
      return ERR2STR (IDN2_CONTEXTO_NO_RULE);
183
0
    case IDN2_UNASSIGNED:
184
0
      return ERR2STR (IDN2_UNASSIGNED);
185
0
    case IDN2_BIDI:
186
0
      return ERR2STR (IDN2_BIDI);
187
0
    case IDN2_DOT_IN_LABEL:
188
0
      return ERR2STR (IDN2_DOT_IN_LABEL);
189
0
    case IDN2_INVALID_TRANSITIONAL:
190
0
      return ERR2STR (IDN2_INVALID_TRANSITIONAL);
191
0
    case IDN2_INVALID_NONTRANSITIONAL:
192
0
      return ERR2STR (IDN2_INVALID_NONTRANSITIONAL);
193
0
    case IDN2_ALABEL_ROUNDTRIP_FAILED:
194
0
      return ERR2STR (IDN2_ALABEL_ROUNDTRIP_FAILED);
195
0
    default:
196
0
      return "IDN2_UNKNOWN";
197
0
    }
198
0
}