86
|
1 |
/*
|
|
2 |
* Copyright 2004 Sun Microsystems, Inc. All rights reserved.
|
|
3 |
* Use is subject to license terms.
|
|
4 |
*/
|
|
5 |
|
|
6 |
#pragma ident "@(#)idnconv.c 1.2 04/06/18 SMI"
|
|
7 |
|
|
8 |
#include <stdlib.h>
|
|
9 |
#include <stdio.h>
|
|
10 |
#include <errno.h>
|
|
11 |
#include <sys/types.h>
|
|
12 |
#include <sys/utsname.h>
|
|
13 |
#include <ctype.h>
|
|
14 |
#include <strings.h>
|
|
15 |
#include <string.h>
|
|
16 |
#include <idn/api.h>
|
|
17 |
#include <locale.h>
|
|
18 |
#include <langinfo.h>
|
|
19 |
#include <limits.h>
|
|
20 |
#include <iconv.h>
|
|
21 |
#include "idnconv.h"
|
|
22 |
|
|
23 |
|
|
24 |
/* Function prototypes: */
|
|
25 |
void idnconv_usage(int);
|
|
26 |
void idnconv_version(void);
|
|
27 |
void doit(FILE *, int, char *, char *);
|
|
28 |
char *increase_sz(char *, size_t *, FILE *, iconv_t);
|
|
29 |
void really_doit(int, char *, char *, size_t, FILE *);
|
|
30 |
char *simpler_iconv(char *, size_t *, char *, char *, FILE *);
|
|
31 |
void cleanup_and_exit(int, FILE *, iconv_t);
|
|
32 |
|
|
33 |
/* File-scope global variables: */
|
|
34 |
static char *cs;
|
|
35 |
static char *name;
|
|
36 |
static size_t namesz;
|
|
37 |
static char *icvbuf;
|
|
38 |
static size_t icvbufsz;
|
|
39 |
static char *outbuf;
|
|
40 |
static size_t outbufsz;
|
|
41 |
|
|
42 |
|
|
43 |
int
|
|
44 |
main(int ac, char **av)
|
|
45 |
{
|
|
46 |
int actions;
|
|
47 |
char *fromcode = (char *)NULL;
|
|
48 |
char *tocode = (char *)NULL;
|
|
49 |
int i;
|
|
50 |
char *p;
|
|
51 |
FILE *fi;
|
|
52 |
|
|
53 |
(void) setlocale(LC_ALL, "");
|
|
54 |
|
|
55 |
#if !defined(TEXT_DOMAIN)
|
|
56 |
#define TEXT_DOMAIN "TEST_SUNW_I18N_CMD"
|
|
57 |
#endif
|
|
58 |
(void) textdomain(TEXT_DOMAIN);
|
|
59 |
|
|
60 |
/* The default actions for the IDN conversions. */
|
|
61 |
actions = IDN_DELIMMAP | IDN_NAMEPREP | IDN_UNASCHECK | \
|
|
62 |
IDN_ASCCHECK | IDN_IDNCONV | IDN_LENCHECK;
|
|
63 |
|
|
64 |
/*
|
|
65 |
* Let's process options and option arguments first; if there is
|
|
66 |
* anything that starts without '-', that means it's the start of
|
|
67 |
* operand(s).
|
|
68 |
*/
|
|
69 |
for (i = 1; i < ac; i++) {
|
|
70 |
if (av[i][0] == '-' && av[i][1] == '-') {
|
|
71 |
/*
|
|
72 |
* The "--" alone means there will be no more
|
|
73 |
* options but operand(s) from now on.
|
|
74 |
*/
|
|
75 |
if (av[i][2] == '\0') {
|
|
76 |
++i;
|
|
77 |
break;
|
|
78 |
}
|
|
79 |
|
|
80 |
if (OPTION("in") || OPTION("from")) {
|
|
81 |
if (++i >= ac) {
|
|
82 |
(void) fprintf(stderr, gettext(
|
|
83 |
"idnconv: error: in-code option argument is missing.\n"));
|
|
84 |
idnconv_usage(0);
|
|
85 |
exit(IDNCONV_ILLEGAL_OPTION);
|
|
86 |
}
|
|
87 |
fromcode = av[i];
|
|
88 |
} else if (OPTION("out") || OPTION("to")) {
|
|
89 |
if (++i >= ac) {
|
|
90 |
(void) fprintf(stderr, gettext(
|
|
91 |
"idnconv: error: out-code option argument is missing.\n"));
|
|
92 |
idnconv_usage(0);
|
|
93 |
exit(IDNCONV_ILLEGAL_OPTION);
|
|
94 |
}
|
|
95 |
tocode = av[i];
|
|
96 |
} else if (OPTION("asciicheck") ||
|
|
97 |
OPTION("ascii-check")) {
|
|
98 |
actions |= IDN_ASCCHECK;
|
|
99 |
} else if (OPTION("noasciicheck") ||
|
|
100 |
OPTION("no-ascii-check")) {
|
|
101 |
actions &= (~(IDN_ASCCHECK));
|
|
102 |
} else if (OPTION("bidicheck") ||
|
|
103 |
OPTION("bidi-check")) {
|
|
104 |
actions |= IDN_BIDICHECK;
|
|
105 |
} else if (OPTION("nobidicheck") ||
|
|
106 |
OPTION("no-bidi-check")) {
|
|
107 |
actions &= (~(IDN_BIDICHECK));
|
|
108 |
} else if (OPTION("lengthcheck") ||
|
|
109 |
OPTION("length-check")) {
|
|
110 |
actions |= IDN_LENCHECK;
|
|
111 |
} else if (OPTION("nolengthcheck") ||
|
|
112 |
OPTION("no-length-check")) {
|
|
113 |
actions &= (~(IDN_LENCHECK));
|
|
114 |
} else if (OPTION("nameprep")) {
|
|
115 |
actions |= IDN_NAMEPREP;
|
|
116 |
} else if (OPTION("nonameprep") ||
|
|
117 |
OPTION("no-nameprep")) {
|
|
118 |
actions &= (~(IDN_NAMEPREP));
|
|
119 |
} else if (OPTION("unassigncheck") ||
|
|
120 |
OPTION("unassign-check")) {
|
|
121 |
actions |= IDN_UNASCHECK;
|
|
122 |
} else if (OPTION("nounassigncheck") ||
|
|
123 |
OPTION("no-unassign-check")) {
|
|
124 |
actions &= (~(IDN_UNASCHECK));
|
|
125 |
} else if (OPTION("help")) {
|
|
126 |
idnconv_usage(1);
|
|
127 |
exit(IDNCONV_SUCCESS);
|
|
128 |
} else if (OPTION("version")) {
|
|
129 |
idnconv_version();
|
|
130 |
exit(IDNCONV_SUCCESS);
|
|
131 |
} else {
|
|
132 |
(void) fprintf(stderr, gettext(
|
|
133 |
"idnconv: error: unknown option \"%s\" specified.\n"), av[i]);
|
|
134 |
idnconv_usage(0);
|
|
135 |
exit(IDNCONV_ILLEGAL_OPTION);
|
|
136 |
}
|
|
137 |
} else if (av[i][0] == '-') {
|
|
138 |
if (av[i][1] == '\0') {
|
|
139 |
(void) fprintf(stderr, gettext(
|
|
140 |
"idnconv: error: option letter missing.\n"));
|
|
141 |
idnconv_usage(0);
|
|
142 |
exit(IDNCONV_ILLEGAL_OPTION);
|
|
143 |
}
|
|
144 |
for (p = av[i] + 1; *p; p++) {
|
|
145 |
switch (*p) {
|
|
146 |
case 'i':
|
|
147 |
case 'f':
|
|
148 |
if (++i >= ac) {
|
|
149 |
(void) fprintf(stderr, gettext(
|
|
150 |
"idnconv: error: in-code option argument is missing.\n"));
|
|
151 |
idnconv_usage(0);
|
|
152 |
exit(IDNCONV_ILLEGAL_OPTION);
|
|
153 |
}
|
|
154 |
fromcode = av[i];
|
|
155 |
break;
|
|
156 |
case 'o':
|
|
157 |
case 't':
|
|
158 |
if (++i >= ac) {
|
|
159 |
(void) fprintf(stderr, gettext(
|
|
160 |
"idnconv: error: out-code option argument is missing.\n"));
|
|
161 |
idnconv_usage(0);
|
|
162 |
exit(IDNCONV_ILLEGAL_OPTION);
|
|
163 |
}
|
|
164 |
tocode = av[i];
|
|
165 |
break;
|
|
166 |
case 'a':
|
|
167 |
actions |= IDN_ASCCHECK;
|
|
168 |
break;
|
|
169 |
case 'A':
|
|
170 |
actions &= (~(IDN_ASCCHECK));
|
|
171 |
break;
|
|
172 |
case 'b':
|
|
173 |
actions |= IDN_BIDICHECK;
|
|
174 |
break;
|
|
175 |
case 'B':
|
|
176 |
actions &= (~(IDN_BIDICHECK));
|
|
177 |
break;
|
|
178 |
case 'l':
|
|
179 |
actions |= IDN_LENCHECK;
|
|
180 |
break;
|
|
181 |
case 'L':
|
|
182 |
actions &= (~(IDN_LENCHECK));
|
|
183 |
break;
|
|
184 |
case 'n':
|
|
185 |
actions |= IDN_NAMEPREP;
|
|
186 |
break;
|
|
187 |
case 'N':
|
|
188 |
actions &= (~(IDN_NAMEPREP));
|
|
189 |
break;
|
|
190 |
case 'u':
|
|
191 |
actions |= IDN_UNASCHECK;
|
|
192 |
break;
|
|
193 |
case 'U':
|
|
194 |
actions &= (~(IDN_UNASCHECK));
|
|
195 |
break;
|
|
196 |
case 'h':
|
|
197 |
idnconv_usage(1);
|
|
198 |
exit(IDNCONV_SUCCESS);
|
|
199 |
case 'v':
|
|
200 |
idnconv_version();
|
|
201 |
exit(IDNCONV_SUCCESS);
|
|
202 |
default:
|
|
203 |
(void) fprintf(stderr, gettext(
|
|
204 |
"idnconv: error: unknown option letter '%c' specified.\n"), *p);
|
|
205 |
idnconv_usage(0);
|
|
206 |
exit(IDNCONV_ILLEGAL_OPTION);
|
|
207 |
}
|
|
208 |
}
|
|
209 |
} else
|
|
210 |
break;
|
|
211 |
}
|
|
212 |
|
|
213 |
/*
|
|
214 |
* Allocate the first memory blocks for the buffers that will be used.
|
|
215 |
*/
|
|
216 |
namesz = icvbufsz = outbufsz = BUFSIZ;
|
|
217 |
name = (char *)malloc(namesz);
|
|
218 |
icvbuf = (char *)malloc(icvbufsz);
|
|
219 |
outbuf = (char *)malloc(outbufsz);
|
|
220 |
if (name == (char *)NULL || icvbuf == (char *)NULL ||
|
|
221 |
outbuf == (char *)NULL) {
|
|
222 |
(void) fprintf(stderr, gettext(
|
|
223 |
"idnconv: error: no more memory.\n"));
|
|
224 |
exit(IDNCONV_NOT_ENOUGH_MEMORY);
|
|
225 |
}
|
|
226 |
|
|
227 |
/*
|
|
228 |
* Collect the codeset name of the current locale for later uses.
|
|
229 |
* In some old systems, the nl_langinfo(CODESET) will return NULL or
|
|
230 |
* an empty string. For that case, we fallback to C locale's "646" for
|
|
231 |
* the completeness and correctness.
|
|
232 |
*/
|
|
233 |
cs = nl_langinfo(CODESET);
|
|
234 |
if (cs == (char *)NULL || *cs == '\0') {
|
|
235 |
cs = "646";
|
|
236 |
}
|
|
237 |
|
|
238 |
/*
|
|
239 |
* If there is no fromcode or tocode specified by the user, use
|
|
240 |
* the current locale's codeset.
|
|
241 |
*/
|
|
242 |
if (fromcode == (char *)NULL)
|
|
243 |
fromcode = cs;
|
|
244 |
if (tocode == (char *)NULL)
|
|
245 |
tocode = cs;
|
|
246 |
|
|
247 |
/*
|
|
248 |
* Process any possible operand(s) and then do the actual conversion(s)
|
|
249 |
* for each operand.
|
|
250 |
*/
|
|
251 |
if (i >= ac) {
|
|
252 |
doit(stdin, actions, fromcode, tocode);
|
|
253 |
} else {
|
|
254 |
for (; i < ac; i++) {
|
|
255 |
fi = fopen(av[i], "r");
|
|
256 |
if (fi == (FILE *)NULL) {
|
|
257 |
(void) fprintf(stderr, gettext(
|
|
258 |
"idnconv: error: cannot access the file \"%s\".\n"), av[i]);
|
|
259 |
cleanup_and_exit(IDNCONV_INPUT_FILE_NOT_FOUND,
|
|
260 |
fi, (iconv_t)-1);
|
|
261 |
}
|
|
262 |
|
|
263 |
doit(fi, actions, fromcode, tocode);
|
|
264 |
|
|
265 |
(void) fclose(fi);
|
|
266 |
}
|
|
267 |
}
|
|
268 |
|
|
269 |
cleanup_and_exit(IDNCONV_SUCCESS, (FILE *)NULL, (iconv_t)-1);
|
|
270 |
}
|
|
271 |
|
|
272 |
|
|
273 |
void
|
|
274 |
idnconv_usage(int help)
|
|
275 |
{
|
|
276 |
(void) fprintf(stderr, gettext(
|
|
277 |
"Usage: idnconv [ -i in-code | --in in-code | -f in-code | --from in-code ]\n\
|
|
278 |
[ -o out-code | --out out-code | -t out-code | --to out-code ]\n\
|
|
279 |
[ -a | --asciicheck | --ascii-check ]\n\
|
|
280 |
[ -A | --noasciicheck | --no-ascii-check ]\n\
|
|
281 |
[ -b | --bidicheck | --bidi-check ]\n\
|
|
282 |
[ -B | --nobidicheck | --no-bidi-check ]\n\
|
|
283 |
[ -l | --lengthcheck | --length-check ]\n\
|
|
284 |
[ -L | --nolengthcheck | --no-length-check ]\n\
|
|
285 |
[ -n | --nameprep ]\n\
|
|
286 |
[ -N | --nonameprep | --no-nameprep ]\n\
|
|
287 |
[ -u | --unassigncheck | --unassign-check ]\n\
|
|
288 |
[ -U | --nounassigncheck | --no-unassign-check ]\n\
|
|
289 |
[ -h | --help ]\n\
|
|
290 |
[ -v | --version ]\n\
|
|
291 |
[ file ... ]\n"));
|
|
292 |
|
|
293 |
if (help) {
|
|
294 |
(void) fprintf(stderr, gettext(
|
|
295 |
"\nThe 'idnconv' Internationalized Domain Name encoding conversion utility\n\
|
|
296 |
reads from specified file(s) or stdin and writes converted result to stdout.\n\
|
|
297 |
Refer to idnconv(1) man page for more detail.\n"));
|
|
298 |
}
|
|
299 |
}
|
|
300 |
|
|
301 |
|
|
302 |
void
|
|
303 |
idnconv_version(void)
|
|
304 |
{
|
|
305 |
(void) fprintf(stderr, gettext(
|
|
306 |
"idnconv 1.0\n\n\
|
|
307 |
Copyright 2004 Sun Microsystems, Inc. All rights reserved.\n\
|
|
308 |
Use is subject to license terms.\n"));
|
|
309 |
}
|
|
310 |
|
|
311 |
|
|
312 |
void
|
|
313 |
doit(FILE *fi, int actions, char *from, char *to)
|
|
314 |
{
|
|
315 |
char ib[BUFSIZ];
|
|
316 |
size_t len;
|
|
317 |
size_t i, j;
|
|
318 |
|
|
319 |
/*
|
|
320 |
* Initialize the indices and then continuously read a buffer amount
|
|
321 |
* and process name(s) in the input buffer; sometimes, a name could be
|
|
322 |
* splitted between two input buffer contents over two fread(3C) calls.
|
|
323 |
* For each end of the process, reset the 'i' index for the next
|
|
324 |
* new input buffer.
|
|
325 |
*/
|
|
326 |
for (i = j = 0; (len = fread((void *)ib, 1, BUFSIZ, fi)) > 0; i = 0) {
|
|
327 |
for (; i < len; i++) {
|
|
328 |
/*
|
|
329 |
* In each loop, this extracts and accumulates a byte
|
|
330 |
* as a part of a name until there is either
|
|
331 |
* a white-space character of the POSIX/C locale or
|
|
332 |
* a user-supplied NULL byte embedded in the input,
|
|
333 |
* i.e., a delimiter.
|
|
334 |
*
|
|
335 |
* Any non-printables such as control characters
|
|
336 |
* will be screened out by idn_encodename(3EXT) and
|
|
337 |
* idn_decodename(3EXT) functions so don't worry
|
|
338 |
* about such characters here.
|
|
339 |
*/
|
|
340 |
if (ib[i] == '\0' || WHITESPACE(ib[i])) {
|
|
341 |
if (j > 0) {
|
|
342 |
really_doit(actions, from, to, j, fi);
|
|
343 |
j = 0;
|
|
344 |
}
|
|
345 |
/*
|
|
346 |
* We make sure to putchar whatever we've
|
|
347 |
* gotten at here so that the output will
|
|
348 |
* resemble as much as possible of the input
|
|
349 |
* except the IDN names.
|
|
350 |
*/
|
|
351 |
putchar(ib[i]);
|
|
352 |
} else {
|
|
353 |
if (j >= namesz)
|
|
354 |
name = increase_sz(name, &namesz, fi,
|
|
355 |
(iconv_t)-1);
|
|
356 |
name[j++] = ib[i];
|
|
357 |
}
|
|
358 |
}
|
|
359 |
}
|
|
360 |
|
|
361 |
/* We might still have a name to process. */
|
|
362 |
if (j > 0) {
|
|
363 |
really_doit(actions, from, to, j, fi);
|
|
364 |
}
|
|
365 |
}
|
|
366 |
|
|
367 |
|
|
368 |
char *
|
|
369 |
increase_sz(char *s, size_t *sz, FILE *fi, iconv_t cd)
|
|
370 |
{
|
|
371 |
*sz += BUFSIZ;
|
|
372 |
s = (char *)realloc((void *)s, *sz);
|
|
373 |
if (s == (char *)NULL) {
|
|
374 |
(void) fprintf(stderr, gettext(
|
|
375 |
"idnconv: error: no more memory.\n"));
|
|
376 |
|
|
377 |
cleanup_and_exit(IDNCONV_NOT_ENOUGH_MEMORY, fi, cd);
|
|
378 |
}
|
|
379 |
|
|
380 |
return (s);
|
|
381 |
}
|
|
382 |
|
|
383 |
|
|
384 |
void
|
|
385 |
really_doit(int actions, char *from, char *to, size_t len, FILE *fi)
|
|
386 |
{
|
|
387 |
char *ib;
|
|
388 |
char *tmpp;
|
|
389 |
idn_result_t idnres;
|
|
390 |
int ret;
|
|
391 |
int i;
|
|
392 |
int to_encode;
|
|
393 |
|
|
394 |
/* First, we properly terminate the name. */
|
|
395 |
if (len >= namesz)
|
|
396 |
name = increase_sz(name, &namesz, fi, (iconv_t)-1);
|
|
397 |
name[len] = '\0';
|
|
398 |
|
|
399 |
/*
|
|
400 |
* The idn_encodename(3EXT) and the idn_decodename(3EXT) expect
|
|
401 |
* a UTF-8 input argument string; if the "from" isn't UTF-8, then,
|
|
402 |
* it need to be converted to a UTF-8 string via simpler_iconv().
|
|
403 |
*/
|
|
404 |
if (strcasecmp(from, "UTF-8") == 0 || strcasecmp(from, "UTF8") == 0 ||
|
|
405 |
strcasecmp(from, "ACE") == 0 || strcasecmp(from, "Punycode") == 0 ||
|
|
406 |
strcasecmp(from, "ASCII") == 0 || strcasecmp(from, "646") == 0 ||
|
|
407 |
strcasecmp(from, "US-ASCII") == 0) {
|
|
408 |
ib = name;
|
|
409 |
} else {
|
|
410 |
ib = simpler_iconv(name, &len, "UTF-8", from, fi);
|
|
411 |
}
|
|
412 |
|
|
413 |
/*
|
|
414 |
* Let's find out on whether to use the idn_decodename(3EXT), i.e.,
|
|
415 |
* ACE name to non-ACE IDN name, or the idn_encodename(3EXT),
|
|
416 |
* non-ACE IDN name to ACE name.
|
|
417 |
*/
|
|
418 |
if (strcasecmp(from, "ACE") == 0 || strcasecmp(from, "Punycode") == 0)
|
|
419 |
to_encode = 0;
|
|
420 |
else {
|
|
421 |
to_encode = 1;
|
|
422 |
|
|
423 |
/*
|
|
424 |
* Need to also check actual contents of the name and
|
|
425 |
* decide if it should be a decode or an encode process.
|
|
426 |
*/
|
|
427 |
if (len > 4 && strncasecmp(ib, ACE_PREFIX, 4) == 0) {
|
|
428 |
to_encode = 0;
|
|
429 |
for (tmpp = ib + 4; *tmpp; tmpp++)
|
|
430 |
if (! isascii(*tmpp)) {
|
|
431 |
to_encode = 1;
|
|
432 |
break;
|
|
433 |
}
|
|
434 |
}
|
|
435 |
}
|
|
436 |
|
|
437 |
/* And, the "to" value finally decides on what to do. */
|
|
438 |
if (strcasecmp(to, "ACE") == 0 || strcasecmp(to, "Punycode") == 0)
|
|
439 |
to_encode = 1;
|
|
440 |
|
|
441 |
/*
|
|
442 |
* Do the IDN conversion.
|
|
443 |
*
|
|
444 |
* If output buffer size is too small, we increase the output buffer
|
|
445 |
* size and then do the IDN conversion again. We only repeat such for
|
|
446 |
* MAX_CONV_LOOP times since doing such for the same input buffer
|
|
447 |
* for more than the MAX_CONV_LOOP times wouldn't really possible.
|
|
448 |
*/
|
|
449 |
for (i = 0; i <= MAX_CONV_LOOP; i++) {
|
|
450 |
if (to_encode) {
|
|
451 |
/*
|
|
452 |
* For the encoding process, we also do local mappings.
|
|
453 |
*/
|
|
454 |
actions |= IDN_LOCALMAP;
|
|
455 |
idnres = idn_encodename(actions, ib, outbuf, outbufsz);
|
|
456 |
} else {
|
|
457 |
/*
|
|
458 |
* For the decoding process, we also do round trip
|
|
459 |
* checking as specified in the RFC 3490.
|
|
460 |
* We also clean IDN_LENCHECK bit since it is not
|
|
461 |
* a valid action for idn_decodename(3EXT).
|
|
462 |
*/
|
|
463 |
actions |= IDN_RTCHECK;
|
|
464 |
actions &= (~(IDN_LENCHECK));
|
|
465 |
idnres = idn_decodename(actions, ib, outbuf, outbufsz);
|
|
466 |
}
|
|
467 |
|
|
468 |
if (idnres != idn_buffer_overflow)
|
|
469 |
break;
|
|
470 |
|
|
471 |
outbuf = increase_sz(outbuf, &outbufsz, fi, (iconv_t)-1);
|
|
472 |
}
|
|
473 |
|
|
474 |
if (idnres == idn_success) {
|
|
475 |
if (strcasecmp(to, "UTF-8") == 0 ||
|
|
476 |
strcasecmp(to, "UTF8") == 0 ||
|
|
477 |
strcasecmp(to, "ACE") == 0 ||
|
|
478 |
strcasecmp(to, "Punycode") == 0) {
|
|
479 |
tmpp = outbuf;
|
|
480 |
} else {
|
|
481 |
len = strlen(outbuf);
|
|
482 |
tmpp = simpler_iconv(outbuf, &len, to, "UTF-8", fi);
|
|
483 |
}
|
|
484 |
|
|
485 |
/*
|
|
486 |
* Since fprintf(3C) functions use locale methods, we better
|
|
487 |
* use fputc(3C) to avoid any possible collision with
|
|
488 |
* underlying locale methods.
|
|
489 |
*/
|
|
490 |
while (*tmpp)
|
|
491 |
(void) putc(*tmpp++, stdout);
|
|
492 |
|
|
493 |
return;
|
|
494 |
} else if (idnres == idn_invalid_encoding || idnres == idn_nomapping) {
|
|
495 |
(void) fprintf(stderr, gettext(
|
|
496 |
"idnconv: error: internal code conversion failed due to possible incomplete or illegal character(s).\n"));
|
|
497 |
ret = IDNCONV_ICONV_INTERNAL_ERROR;
|
|
498 |
} else if (idnres == idn_invalid_name) {
|
|
499 |
(void) fprintf(stderr, gettext(
|
|
500 |
"idnconv: error: not supported internal code conversion name.\n"));
|
|
501 |
ret = IDNCONV_UNSUPPORTED_INCODE_OR_OUTCODE;
|
|
502 |
} else if (idnres == idn_invalid_length) {
|
|
503 |
(void) fprintf(stderr, gettext(
|
|
504 |
"idnconv: error: label length checking has failed.\n"));
|
|
505 |
ret = IDNCONV_LABEL_LENGTH_CHECK_FAILED;
|
|
506 |
} else if (idnres == idn_nomemory) {
|
|
507 |
(void) fprintf(stderr, gettext(
|
|
508 |
"idnconv: error: no more memory.\n"));
|
|
509 |
ret = IDNCONV_NOT_ENOUGH_MEMORY;
|
|
510 |
} else if (idnres == idn_prohibited) {
|
|
511 |
(void) fprintf(stderr, gettext(
|
|
512 |
"idnconv: error: prohibited, not allowed, or unassigned character(s) found.\n"));
|
|
513 |
ret = IDNCONV_UNASSIGNED_CHARACTER_FOUND;
|
|
514 |
} else {
|
|
515 |
(void) fprintf(stderr, gettext(
|
|
516 |
"idnconv: error: unspecified failure happened.\n"));
|
|
517 |
ret = IDNCONV_UNKNOWN_FAILURE;
|
|
518 |
}
|
|
519 |
|
|
520 |
cleanup_and_exit(ret, fi, (iconv_t)-1);
|
|
521 |
}
|
|
522 |
|
|
523 |
|
|
524 |
char *
|
|
525 |
simpler_iconv(char *s, size_t *len, char *tocode, char *fromcode, FILE *fi)
|
|
526 |
{
|
|
527 |
iconv_t cd;
|
|
528 |
char *ibp;
|
|
529 |
char *obp;
|
|
530 |
size_t ileft, oleft;
|
|
531 |
size_t ret;
|
|
532 |
int i, j;
|
|
533 |
|
|
534 |
cd = iconv_open((const char *)tocode, (const char *)fromcode);
|
|
535 |
if (cd == (iconv_t)-1) {
|
|
536 |
(void) fprintf(stderr, gettext(
|
|
537 |
"idnconv: error: iconv from \"%s\" to \"%s\" is not supported.\n"), fromcode, tocode);
|
|
538 |
cleanup_and_exit(IDNCONV_UNSUPPORTED_INCODE_OR_OUTCODE, fi, cd);
|
|
539 |
}
|
|
540 |
|
|
541 |
ibp = s;
|
|
542 |
ileft = *len;
|
|
543 |
obp = icvbuf;
|
|
544 |
oleft = icvbufsz;
|
|
545 |
|
|
546 |
for (i = 0; i <= MAX_CONV_LOOP; i++) {
|
|
547 |
ret = iconv(cd, (const char **)&ibp, &ileft, &obp, &oleft);
|
|
548 |
if (ret == 0)
|
|
549 |
break;
|
|
550 |
else if (ret > 0) {
|
|
551 |
/*
|
|
552 |
* We had non-identical code conversion and that ain't
|
|
553 |
* good to be used as host or domain name; as the man
|
|
554 |
* page specifies, exit with a failure code.
|
|
555 |
*/
|
|
556 |
(void) fprintf(stderr, gettext(
|
|
557 |
"idnconv: error: internal iconv code conversion encountered character(s) that cannot be converted to \"%s\" codeset.\n"), tocode);
|
|
558 |
|
|
559 |
cleanup_and_exit(IDNCONV_ICONV_NON_IDENTICAL_MAPPING,
|
|
560 |
fi, cd);
|
|
561 |
}
|
|
562 |
|
|
563 |
/*
|
|
564 |
* The only valid errno that we expect is E2BIG; if that's
|
|
565 |
* the case indeed, we increase the output buffer size and
|
|
566 |
* try again. (We will do this only MAX_CONV_LOOP times;
|
|
567 |
* if it needs more than MAX_CONV_LOOP times of output buffer
|
|
568 |
* size increases, that means the underlying iconv code
|
|
569 |
* conversion is most likely bad or something like that;
|
|
570 |
* we abort in that case.)
|
|
571 |
*
|
|
572 |
* Any other errno means we have encountered a problem, e.g.,
|
|
573 |
* an illegal character in the input buffer, an incomplete
|
|
574 |
* character at the end of the input buffer, conversion
|
|
575 |
* descriptor 'cd' is somehow bad or corrupted, or something
|
|
576 |
* unknown.
|
|
577 |
*/
|
|
578 |
if (errno == E2BIG) {
|
|
579 |
j = icvbufsz - oleft;
|
|
580 |
|
|
581 |
icvbuf = increase_sz(icvbuf, &icvbufsz, fi, cd);
|
|
582 |
|
|
583 |
obp = icvbuf + j;
|
|
584 |
oleft += BUFSIZ;
|
|
585 |
} else {
|
|
586 |
(void) fprintf(stderr, gettext(
|
|
587 |
"idnconv: error: internal iconv code conversion failed.\n"));
|
|
588 |
cleanup_and_exit(IDNCONV_ICONV_INTERNAL_ERROR, fi, cd);
|
|
589 |
}
|
|
590 |
}
|
|
591 |
|
|
592 |
if (i > MAX_CONV_LOOP) {
|
|
593 |
(void) fprintf(stderr, gettext(
|
|
594 |
"idnconv: error: internal iconv code conversion failed.\n"));
|
|
595 |
cleanup_and_exit(IDNCONV_ICONV_INTERNAL_ERROR, fi, cd);
|
|
596 |
}
|
|
597 |
|
|
598 |
/*
|
|
599 |
* We now reset the conversion state and collect any state reset
|
|
600 |
* values or remnants in the conversion before closing the iconv.
|
|
601 |
* It is quite unlikely that we will get out more than 256 bytes
|
|
602 |
* for the reset operation; if so, it will be treated as an error...
|
|
603 |
*/
|
|
604 |
ibp = (char *)NULL;
|
|
605 |
ileft = 0;
|
|
606 |
if (oleft < 256) {
|
|
607 |
j = icvbufsz - oleft;
|
|
608 |
|
|
609 |
icvbuf = increase_sz(icvbuf, &icvbufsz, fi, cd);
|
|
610 |
|
|
611 |
obp = icvbuf + j;
|
|
612 |
oleft += BUFSIZ;
|
|
613 |
}
|
|
614 |
|
|
615 |
ret = iconv(cd, (const char **)&ibp, &ileft, &obp, &oleft);
|
|
616 |
if (ret > 0) {
|
|
617 |
(void) fprintf(stderr, gettext(
|
|
618 |
"idnconv: error: internal iconv code conversion encountered character(s) that cannot be converted to \"%s\".\n"), tocode);
|
|
619 |
|
|
620 |
cleanup_and_exit(IDNCONV_ICONV_NON_IDENTICAL_MAPPING, fi, cd);
|
|
621 |
} else if (ret == (size_t)-1) {
|
|
622 |
(void) fprintf(stderr, gettext(
|
|
623 |
"idnconv: error: internal iconv code conversion failed.\n"));
|
|
624 |
cleanup_and_exit(IDNCONV_ICONV_INTERNAL_ERROR, fi, cd);
|
|
625 |
}
|
|
626 |
|
|
627 |
(void) iconv_close(cd);
|
|
628 |
|
|
629 |
/*
|
|
630 |
* Now calculate the length and then append '\0' at the end of
|
|
631 |
* the icvbuf to make it a string.
|
|
632 |
*/
|
|
633 |
*len = icvbufsz - oleft;
|
|
634 |
if (*len >= icvbufsz)
|
|
635 |
icvbuf = increase_sz(icvbuf, &icvbufsz, fi, (iconv_t)-1);
|
|
636 |
icvbuf[*len] = '\0';
|
|
637 |
|
|
638 |
return (icvbuf);
|
|
639 |
}
|
|
640 |
|
|
641 |
|
|
642 |
void
|
|
643 |
cleanup_and_exit(int ret, FILE *fp, iconv_t cd)
|
|
644 |
{
|
|
645 |
free((void *)name);
|
|
646 |
free((void *)icvbuf);
|
|
647 |
free((void *)outbuf);
|
|
648 |
|
|
649 |
(void) fflush(stdout);
|
|
650 |
|
|
651 |
if (fp)
|
|
652 |
(void) fclose(fp);
|
|
653 |
|
|
654 |
if (cd != (iconv_t)-1)
|
|
655 |
(void) iconv_close(cd);
|
|
656 |
|
|
657 |
exit(ret);
|
|
658 |
}
|