Branch data Line data Source code
1 : : /* error.c - libidn2 error handling helpers.
2 : : Copyright (C) 2011 Simon Josefsson
3 : :
4 : : This program is free software: you can redistribute it and/or modify
5 : : it under the terms of the GNU General Public License as published by
6 : : the Free Software Foundation, either version 3 of the License, or
7 : : (at your option) any later version.
8 : :
9 : : This program is distributed in the hope that it will be useful,
10 : : but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : : GNU General Public License for more details.
13 : :
14 : : You should have received a copy of the GNU General Public License
15 : : along with this program. If not, see <http://www.gnu.org/licenses/>.
16 : : */
17 : :
18 : : #include <config.h>
19 : :
20 : : #include "idn2.h"
21 : :
22 : : /* Prepare for gettext. */
23 : : #define _(x) x
24 : : #define bindtextdomain(a,b) 0
25 : :
26 : : /**
27 : : * idn2_strerror:
28 : : * @rc: return code from another libidn2 function.
29 : : *
30 : : * Convert internal libidn2 error code to a humanly readable string.
31 : : * The returned pointer must not be de-allocated by the caller.
32 : : *
33 : : * Return value: A humanly readable string describing error.
34 : : **/
35 : : const char *
36 : 0 : idn2_strerror (int rc)
37 : : {
38 : : const char *p;
39 : :
40 : : bindtextdomain (PACKAGE, LOCALEDIR);
41 : :
42 [ # # # # # : 0 : switch (rc)
# # # # #
# # # # #
# # # # #
# # # # # ]
43 : : {
44 : : case IDN2_OK:
45 : 0 : p = _("success");
46 : 0 : break;
47 : :
48 : : case IDN2_MALLOC:
49 : 0 : p = _("out of memory");
50 : 0 : break;
51 : :
52 : : case IDN2_NO_CODESET:
53 : 0 : p = _("could not determine locale encoding format");
54 : 0 : break;
55 : :
56 : : case IDN2_ICONV_FAIL:
57 : 0 : p = _("could not convert string to UTF-8");
58 : 0 : break;
59 : :
60 : : case IDN2_ENCODING_ERROR:
61 : 0 : p = _("string encoding error");
62 : 0 : break;
63 : :
64 : : case IDN2_NFC:
65 : 0 : p = _("string could not be NFC normalized");
66 : 0 : break;
67 : :
68 : : case IDN2_PUNYCODE_BAD_INPUT:
69 : 0 : p = _("string contains invalid punycode data");
70 : 0 : break;
71 : :
72 : : case IDN2_PUNYCODE_BIG_OUTPUT:
73 : 0 : p = _("punycode encoded data will be too large");
74 : 0 : break;
75 : :
76 : : case IDN2_PUNYCODE_OVERFLOW:
77 : 0 : p = _("punycode conversion resulted in overflow");
78 : 0 : break;
79 : :
80 : : case IDN2_TOO_BIG_DOMAIN:
81 : 0 : p = _("domain name longer than 255 characters");
82 : 0 : break;
83 : :
84 : : case IDN2_TOO_BIG_LABEL:
85 : 0 : p = _("domain label longer than 63 characters");
86 : 0 : break;
87 : :
88 : : case IDN2_INVALID_ALABEL:
89 : 0 : p = _("input A-label is not valid");
90 : 0 : break;
91 : :
92 : : case IDN2_UALABEL_MISMATCH:
93 : 0 : p = _("input A-label and U-label does not match");
94 : 0 : break;
95 : :
96 : : case IDN2_NOT_NFC:
97 : 0 : p = _("string is not in Unicode NFC format");
98 : 0 : break;
99 : :
100 : : case IDN2_2HYPHEN:
101 : 0 : p = _("string contains forbidden two hyphens pattern");
102 : 0 : break;
103 : :
104 : : case IDN2_HYPHEN_STARTEND:
105 : 0 : p = _("string start/ends with forbidden hyphen");
106 : 0 : break;
107 : :
108 : : case IDN2_LEADING_COMBINING:
109 : 0 : p = _("string contains a forbidden leading combining character");
110 : 0 : break;
111 : :
112 : : case IDN2_DISALLOWED:
113 : 0 : p = _("string contains a disallowed character");
114 : 0 : break;
115 : :
116 : : case IDN2_CONTEXTJ:
117 : 0 : p = _("string contains a forbidden context-j character");
118 : 0 : break;
119 : :
120 : : case IDN2_CONTEXTJ_NO_RULE:
121 : 0 : p = _("string contains a context-j character with null rule");
122 : 0 : break;
123 : :
124 : : case IDN2_CONTEXTO:
125 : 0 : p = _("string contains a forbidden context-o character");
126 : 0 : break;
127 : :
128 : : case IDN2_CONTEXTO_NO_RULE:
129 : 0 : p = _("string contains a context-o character with null rule");
130 : 0 : break;
131 : :
132 : : case IDN2_UNASSIGNED:
133 : 0 : p = _("string contains unassigned code point");
134 : 0 : break;
135 : :
136 : : case IDN2_BIDI:
137 : 0 : p = _("string has forbidden bi-directional properties");
138 : 0 : break;
139 : :
140 : : default:
141 : 0 : p = _("Unknown error");
142 : : break;
143 : : }
144 : :
145 : 0 : return p;
146 : : }
147 : :
148 : : #define ERR2STR(name) #name
149 : :
150 : : /**
151 : : * idn2_strerror_name:
152 : : * @rc: return code from another libidn2 function.
153 : : *
154 : : * Convert internal libidn2 error code to a string corresponding to
155 : : * internal header file symbols. For example,
156 : : * idn2_strerror_name(IDN2_MALLOC) will return the string
157 : : * "IDN2_MALLOC".
158 : : *
159 : : * The caller must not attempt to de-allocate the returned string.
160 : : *
161 : : * Return value: A string corresponding to error code symbol.
162 : : **/
163 : : const char *
164 : 482 : idn2_strerror_name (int rc)
165 : : {
166 : : const char *p;
167 : :
168 [ + - - - + : 482 : switch (rc)
- - + - +
+ + - + +
- + + + -
+ - + + - ]
169 : : {
170 : : case IDN2_OK:
171 : 347 : p = ERR2STR (IDN2_OK);
172 : 347 : break;
173 : :
174 : : case IDN2_MALLOC:
175 : 0 : p = ERR2STR (IDN2_MALLOC);
176 : 0 : break;
177 : :
178 : : case IDN2_NO_CODESET:
179 : 0 : p = ERR2STR (IDN2_NO_NODESET);
180 : 0 : break;
181 : :
182 : : case IDN2_ICONV_FAIL:
183 : 0 : p = ERR2STR (IDN2_ICONV_FAIL);
184 : 0 : break;
185 : :
186 : : case IDN2_ENCODING_ERROR:
187 : 12 : p = ERR2STR (IDN2_ENCODING_ERROR);
188 : 12 : break;
189 : :
190 : : case IDN2_NFC:
191 : 0 : p = ERR2STR (IDN2_NFC);
192 : 0 : break;
193 : :
194 : : case IDN2_PUNYCODE_BAD_INPUT:
195 : 0 : p = ERR2STR (IDN2_PUNYCODE_BAD_INPUT);
196 : 0 : break;
197 : :
198 : : case IDN2_PUNYCODE_BIG_OUTPUT:
199 : 4 : p = ERR2STR (IDN2_PUNYCODE_BIG_OUTPUT);
200 : 4 : break;
201 : :
202 : : case IDN2_PUNYCODE_OVERFLOW:
203 : 0 : p = ERR2STR (IDN2_PUNYCODE_OVERFLOW);
204 : 0 : break;
205 : :
206 : : case IDN2_TOO_BIG_DOMAIN:
207 : 2 : p = ERR2STR (IDN2_TOO_BIG_DOMAIN);
208 : 2 : break;
209 : :
210 : : case IDN2_TOO_BIG_LABEL:
211 : 3 : p = ERR2STR (IDN2_TOO_BIG_LABEL);
212 : 3 : break;
213 : :
214 : : case IDN2_INVALID_ALABEL:
215 : 1 : p = ERR2STR (IDN2_INVALID_ALABEL);
216 : 1 : break;
217 : :
218 : : case IDN2_UALABEL_MISMATCH:
219 : 0 : p = ERR2STR (IDN2_UALABEL_MISMATCH);
220 : 0 : break;
221 : :
222 : : case IDN2_NOT_NFC:
223 : 11 : p = ERR2STR (IDN2_NOT_NFC);
224 : 11 : break;
225 : :
226 : : case IDN2_2HYPHEN:
227 : 2 : p = ERR2STR (IDN2_2HYPHEN);
228 : 2 : break;
229 : :
230 : : case IDN2_HYPHEN_STARTEND:
231 : 0 : p = ERR2STR (IDN2_HYPHEN_STARTEND);
232 : 0 : break;
233 : :
234 : : case IDN2_LEADING_COMBINING:
235 : 5 : p = ERR2STR (IDN2_LEADING_COMBINING);
236 : 5 : break;
237 : :
238 : : case IDN2_DISALLOWED:
239 : 54 : p = ERR2STR (IDN2_DISALLOWED);
240 : 54 : break;
241 : :
242 : : case IDN2_CONTEXTJ:
243 : 5 : p = ERR2STR (IDN2_CONTEXTJ);
244 : 5 : break;
245 : :
246 : : case IDN2_CONTEXTJ_NO_RULE:
247 : 0 : p = ERR2STR (IDN2_CONTEXTJ_NO_RULE);
248 : 0 : break;
249 : :
250 : : case IDN2_CONTEXTO:
251 : 22 : p = ERR2STR (IDN2_CONTEXTO);
252 : 22 : break;
253 : :
254 : : case IDN2_CONTEXTO_NO_RULE:
255 : 0 : p = ERR2STR (IDN2_CONTEXTO_NO_RULE);
256 : 0 : break;
257 : :
258 : : case IDN2_UNASSIGNED:
259 : 5 : p = ERR2STR (IDN2_UNASSIGNED);
260 : 5 : break;
261 : :
262 : : case IDN2_BIDI:
263 : 9 : p = ERR2STR (IDN2_BIDI);
264 : 9 : break;
265 : :
266 : : default:
267 : 0 : p = "IDN2_UNKNOWN";
268 : : break;
269 : : }
270 : :
271 : 482 : return p;
272 : : }
|