File: | src/idn.c |
Warning: | line 201, column 11 Potential leak of memory pointed to by 'line' |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | /* idn.c --- Command line interface to libidn. | |||
2 | * Copyright (C) 2003-2020 Simon Josefsson | |||
3 | * | |||
4 | * This file is part of GNU Libidn. | |||
5 | * | |||
6 | * This program is free software: you can redistribute it and/or modify | |||
7 | * it under the terms of the GNU General Public License as published by | |||
8 | * the Free Software Foundation, either version 3 of the License, or | |||
9 | * (at your option) any later version. | |||
10 | * | |||
11 | * This program is distributed in the hope that it will be useful, | |||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
14 | * GNU General Public License for more details. | |||
15 | * | |||
16 | * You should have received a copy of the GNU General Public License | |||
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
18 | * | |||
19 | */ | |||
20 | ||||
21 | #ifdef HAVE_CONFIG_H1 | |||
22 | # include "config.h" | |||
23 | #endif | |||
24 | ||||
25 | #include <stdio.h> | |||
26 | #include <stdlib.h> | |||
27 | #include <string.h> | |||
28 | #include <errno(*__errno_location ()).h> | |||
29 | #include <locale.h> | |||
30 | #include <unistd.h> | |||
31 | ||||
32 | /* Gnulib headers. */ | |||
33 | #include "error.h" | |||
34 | #include "gettext.h" | |||
35 | #define _(String)dcgettext ("libidn", String, 5) dgettext (PACKAGE, String)dcgettext ("libidn", String, 5) | |||
36 | #define N_(String)String gettext_noop (String)String | |||
37 | #include "progname.h" | |||
38 | #include "version-etc.h" | |||
39 | ||||
40 | /* Libidn headers. */ | |||
41 | #include <stringprep.h> | |||
42 | #include <punycode.h> | |||
43 | #include <idna.h> | |||
44 | #ifdef WITH_TLD1 | |||
45 | # include <tld.h> | |||
46 | #endif | |||
47 | ||||
48 | #include "idn_cmd.h" | |||
49 | ||||
50 | #define GREETING"Copyright 2002-2015 Simon Josefsson.\n" "GNU Libidn is free software with ABSOLUTELY NO WARRANTY. For more\n" "information about these matters, see <http://www.gnu.org/licenses/>.\n" \ | |||
51 | "Copyright 2002-2015 Simon Josefsson.\n" \ | |||
52 | "GNU Libidn is free software with ABSOLUTELY NO WARRANTY. For more\n" \ | |||
53 | "information about these matters, see <http://www.gnu.org/licenses/>.\n" | |||
54 | ||||
55 | const char version_etc_copyright[] = | |||
56 | /* Do *not* mark this string for translation. %s is a copyright | |||
57 | symbol suitable for this locale, and %d is the copyright | |||
58 | year. */ | |||
59 | "Copyright %s %d Simon Josefsson."; | |||
60 | ||||
61 | static void | |||
62 | usage (int status) | |||
63 | { | |||
64 | if (status != EXIT_SUCCESS0) | |||
65 | fprintf (stderrstderr, _("Try '%s --help' for more information.\n")dcgettext ("libidn", "Try '%s --help' for more information.\n" , 5), | |||
66 | program_name); | |||
67 | else | |||
68 | { | |||
69 | printf (_("\dcgettext ("libidn", "Usage: %s [OPTION]... [STRINGS]...\n", 5 ) | |||
70 | Usage: %s [OPTION]... [STRINGS]...\n\dcgettext ("libidn", "Usage: %s [OPTION]... [STRINGS]...\n", 5 ) | |||
71 | ")dcgettext ("libidn", "Usage: %s [OPTION]... [STRINGS]...\n", 5 ), program_name); | |||
72 | fputs (_("\dcgettext ("libidn", "Internationalized Domain Name (IDN) convert STRINGS, or standard input.\n\n" , 5) | |||
73 | Internationalized Domain Name (IDN) convert STRINGS, or standard input.\n\dcgettext ("libidn", "Internationalized Domain Name (IDN) convert STRINGS, or standard input.\n\n" , 5) | |||
74 | \n\dcgettext ("libidn", "Internationalized Domain Name (IDN) convert STRINGS, or standard input.\n\n" , 5) | |||
75 | ")dcgettext ("libidn", "Internationalized Domain Name (IDN) convert STRINGS, or standard input.\n\n" , 5), stdoutstdout); | |||
76 | fputs (_("\dcgettext ("libidn", "Command line interface to the internationalized domain name library.\n\nAll strings are expected to be encoded in the preferred charset used\nby your locale. Use --debug to find out what this charset is. You\ncan override the charset used by setting environment variable CHARSET.\n\nTo process a string that starts with '-', for example '-foo', use '--'\nto signal the end of parameters, as in: idn --quiet -a -- -foo\n\nMandatory arguments to long options are mandatory for short options too.\n" , 5) | |||
77 | Command line interface to the internationalized domain name library.\n\dcgettext ("libidn", "Command line interface to the internationalized domain name library.\n\nAll strings are expected to be encoded in the preferred charset used\nby your locale. Use --debug to find out what this charset is. You\ncan override the charset used by setting environment variable CHARSET.\n\nTo process a string that starts with '-', for example '-foo', use '--'\nto signal the end of parameters, as in: idn --quiet -a -- -foo\n\nMandatory arguments to long options are mandatory for short options too.\n" , 5) | |||
78 | \n\dcgettext ("libidn", "Command line interface to the internationalized domain name library.\n\nAll strings are expected to be encoded in the preferred charset used\nby your locale. Use --debug to find out what this charset is. You\ncan override the charset used by setting environment variable CHARSET.\n\nTo process a string that starts with '-', for example '-foo', use '--'\nto signal the end of parameters, as in: idn --quiet -a -- -foo\n\nMandatory arguments to long options are mandatory for short options too.\n" , 5) | |||
79 | All strings are expected to be encoded in the preferred charset used\n\dcgettext ("libidn", "Command line interface to the internationalized domain name library.\n\nAll strings are expected to be encoded in the preferred charset used\nby your locale. Use --debug to find out what this charset is. You\ncan override the charset used by setting environment variable CHARSET.\n\nTo process a string that starts with '-', for example '-foo', use '--'\nto signal the end of parameters, as in: idn --quiet -a -- -foo\n\nMandatory arguments to long options are mandatory for short options too.\n" , 5) | |||
80 | by your locale. Use --debug to find out what this charset is. You\n\dcgettext ("libidn", "Command line interface to the internationalized domain name library.\n\nAll strings are expected to be encoded in the preferred charset used\nby your locale. Use --debug to find out what this charset is. You\ncan override the charset used by setting environment variable CHARSET.\n\nTo process a string that starts with '-', for example '-foo', use '--'\nto signal the end of parameters, as in: idn --quiet -a -- -foo\n\nMandatory arguments to long options are mandatory for short options too.\n" , 5) | |||
81 | can override the charset used by setting environment variable CHARSET.\n\dcgettext ("libidn", "Command line interface to the internationalized domain name library.\n\nAll strings are expected to be encoded in the preferred charset used\nby your locale. Use --debug to find out what this charset is. You\ncan override the charset used by setting environment variable CHARSET.\n\nTo process a string that starts with '-', for example '-foo', use '--'\nto signal the end of parameters, as in: idn --quiet -a -- -foo\n\nMandatory arguments to long options are mandatory for short options too.\n" , 5) | |||
82 | \n\dcgettext ("libidn", "Command line interface to the internationalized domain name library.\n\nAll strings are expected to be encoded in the preferred charset used\nby your locale. Use --debug to find out what this charset is. You\ncan override the charset used by setting environment variable CHARSET.\n\nTo process a string that starts with '-', for example '-foo', use '--'\nto signal the end of parameters, as in: idn --quiet -a -- -foo\n\nMandatory arguments to long options are mandatory for short options too.\n" , 5) | |||
83 | To process a string that starts with '-', for example '-foo', use '--'\n\dcgettext ("libidn", "Command line interface to the internationalized domain name library.\n\nAll strings are expected to be encoded in the preferred charset used\nby your locale. Use --debug to find out what this charset is. You\ncan override the charset used by setting environment variable CHARSET.\n\nTo process a string that starts with '-', for example '-foo', use '--'\nto signal the end of parameters, as in: idn --quiet -a -- -foo\n\nMandatory arguments to long options are mandatory for short options too.\n" , 5) | |||
84 | to signal the end of parameters, as in: idn --quiet -a -- -foo\n\dcgettext ("libidn", "Command line interface to the internationalized domain name library.\n\nAll strings are expected to be encoded in the preferred charset used\nby your locale. Use --debug to find out what this charset is. You\ncan override the charset used by setting environment variable CHARSET.\n\nTo process a string that starts with '-', for example '-foo', use '--'\nto signal the end of parameters, as in: idn --quiet -a -- -foo\n\nMandatory arguments to long options are mandatory for short options too.\n" , 5) | |||
85 | \n\dcgettext ("libidn", "Command line interface to the internationalized domain name library.\n\nAll strings are expected to be encoded in the preferred charset used\nby your locale. Use --debug to find out what this charset is. You\ncan override the charset used by setting environment variable CHARSET.\n\nTo process a string that starts with '-', for example '-foo', use '--'\nto signal the end of parameters, as in: idn --quiet -a -- -foo\n\nMandatory arguments to long options are mandatory for short options too.\n" , 5) | |||
86 | Mandatory arguments to long options are mandatory for short options too.\n\dcgettext ("libidn", "Command line interface to the internationalized domain name library.\n\nAll strings are expected to be encoded in the preferred charset used\nby your locale. Use --debug to find out what this charset is. You\ncan override the charset used by setting environment variable CHARSET.\n\nTo process a string that starts with '-', for example '-foo', use '--'\nto signal the end of parameters, as in: idn --quiet -a -- -foo\n\nMandatory arguments to long options are mandatory for short options too.\n" , 5) | |||
87 | ")dcgettext ("libidn", "Command line interface to the internationalized domain name library.\n\nAll strings are expected to be encoded in the preferred charset used\nby your locale. Use --debug to find out what this charset is. You\ncan override the charset used by setting environment variable CHARSET.\n\nTo process a string that starts with '-', for example '-foo', use '--'\nto signal the end of parameters, as in: idn --quiet -a -- -foo\n\nMandatory arguments to long options are mandatory for short options too.\n" , 5), stdoutstdout); | |||
88 | fputs (_("\dcgettext ("libidn", " -h, --help Print help and exit\n -V, --version Print version and exit\n" , 5) | |||
89 | -h, --help Print help and exit\n\dcgettext ("libidn", " -h, --help Print help and exit\n -V, --version Print version and exit\n" , 5) | |||
90 | -V, --version Print version and exit\n\dcgettext ("libidn", " -h, --help Print help and exit\n -V, --version Print version and exit\n" , 5) | |||
91 | ")dcgettext ("libidn", " -h, --help Print help and exit\n -V, --version Print version and exit\n" , 5), stdoutstdout); | |||
92 | fputs (_("\dcgettext ("libidn", " -s, --stringprep Prepare string according to nameprep profile\n -d, --punycode-decode Decode Punycode\n -e, --punycode-encode Encode Punycode\n -a, --idna-to-ascii Convert to ACE according to IDNA (default mode)\n -u, --idna-to-unicode Convert from ACE according to IDNA\n" , 5) | |||
93 | -s, --stringprep Prepare string according to nameprep profile\n\dcgettext ("libidn", " -s, --stringprep Prepare string according to nameprep profile\n -d, --punycode-decode Decode Punycode\n -e, --punycode-encode Encode Punycode\n -a, --idna-to-ascii Convert to ACE according to IDNA (default mode)\n -u, --idna-to-unicode Convert from ACE according to IDNA\n" , 5) | |||
94 | -d, --punycode-decode Decode Punycode\n\dcgettext ("libidn", " -s, --stringprep Prepare string according to nameprep profile\n -d, --punycode-decode Decode Punycode\n -e, --punycode-encode Encode Punycode\n -a, --idna-to-ascii Convert to ACE according to IDNA (default mode)\n -u, --idna-to-unicode Convert from ACE according to IDNA\n" , 5) | |||
95 | -e, --punycode-encode Encode Punycode\n\dcgettext ("libidn", " -s, --stringprep Prepare string according to nameprep profile\n -d, --punycode-decode Decode Punycode\n -e, --punycode-encode Encode Punycode\n -a, --idna-to-ascii Convert to ACE according to IDNA (default mode)\n -u, --idna-to-unicode Convert from ACE according to IDNA\n" , 5) | |||
96 | -a, --idna-to-ascii Convert to ACE according to IDNA (default mode)\n\dcgettext ("libidn", " -s, --stringprep Prepare string according to nameprep profile\n -d, --punycode-decode Decode Punycode\n -e, --punycode-encode Encode Punycode\n -a, --idna-to-ascii Convert to ACE according to IDNA (default mode)\n -u, --idna-to-unicode Convert from ACE according to IDNA\n" , 5) | |||
97 | -u, --idna-to-unicode Convert from ACE according to IDNA\n\dcgettext ("libidn", " -s, --stringprep Prepare string according to nameprep profile\n -d, --punycode-decode Decode Punycode\n -e, --punycode-encode Encode Punycode\n -a, --idna-to-ascii Convert to ACE according to IDNA (default mode)\n -u, --idna-to-unicode Convert from ACE according to IDNA\n" , 5) | |||
98 | ")dcgettext ("libidn", " -s, --stringprep Prepare string according to nameprep profile\n -d, --punycode-decode Decode Punycode\n -e, --punycode-encode Encode Punycode\n -a, --idna-to-ascii Convert to ACE according to IDNA (default mode)\n -u, --idna-to-unicode Convert from ACE according to IDNA\n" , 5), stdoutstdout); | |||
99 | fputs (_("\dcgettext ("libidn", " --allow-unassigned Toggle IDNA AllowUnassigned flag (default off)\n --usestd3asciirules Toggle IDNA UseSTD3ASCIIRules flag (default off)\n" , 5) | |||
100 | --allow-unassigned Toggle IDNA AllowUnassigned flag (default off)\n\dcgettext ("libidn", " --allow-unassigned Toggle IDNA AllowUnassigned flag (default off)\n --usestd3asciirules Toggle IDNA UseSTD3ASCIIRules flag (default off)\n" , 5) | |||
101 | --usestd3asciirules Toggle IDNA UseSTD3ASCIIRules flag (default off)\n\dcgettext ("libidn", " --allow-unassigned Toggle IDNA AllowUnassigned flag (default off)\n --usestd3asciirules Toggle IDNA UseSTD3ASCIIRules flag (default off)\n" , 5) | |||
102 | ")dcgettext ("libidn", " --allow-unassigned Toggle IDNA AllowUnassigned flag (default off)\n --usestd3asciirules Toggle IDNA UseSTD3ASCIIRules flag (default off)\n" , 5), stdoutstdout); | |||
103 | fputs (_("\dcgettext ("libidn", " --no-tld Don't check string for TLD specific rules\n Only for --idna-to-ascii and --idna-to-unicode\n" , 5) | |||
104 | --no-tld Don't check string for TLD specific rules\n\dcgettext ("libidn", " --no-tld Don't check string for TLD specific rules\n Only for --idna-to-ascii and --idna-to-unicode\n" , 5) | |||
105 | Only for --idna-to-ascii and --idna-to-unicode\n\dcgettext ("libidn", " --no-tld Don't check string for TLD specific rules\n Only for --idna-to-ascii and --idna-to-unicode\n" , 5) | |||
106 | ")dcgettext ("libidn", " --no-tld Don't check string for TLD specific rules\n Only for --idna-to-ascii and --idna-to-unicode\n" , 5), stdoutstdout); | |||
107 | fputs (_("\dcgettext ("libidn", " -n, --nfkc Normalize string according to Unicode v3.2 NFKC\n" , 5) | |||
108 | -n, --nfkc Normalize string according to Unicode v3.2 NFKC\n\dcgettext ("libidn", " -n, --nfkc Normalize string according to Unicode v3.2 NFKC\n" , 5) | |||
109 | ")dcgettext ("libidn", " -n, --nfkc Normalize string according to Unicode v3.2 NFKC\n" , 5), stdoutstdout); | |||
110 | fputs (_("\dcgettext ("libidn", " -p, --profile=STRING Use specified stringprep profile instead\n Valid stringprep profiles: Nameprep\n iSCSI Nodeprep Resourceprep trace SASLprep\n" , 5) | |||
111 | -p, --profile=STRING Use specified stringprep profile instead\n\dcgettext ("libidn", " -p, --profile=STRING Use specified stringprep profile instead\n Valid stringprep profiles: Nameprep\n iSCSI Nodeprep Resourceprep trace SASLprep\n" , 5) | |||
112 | Valid stringprep profiles: Nameprep\n\dcgettext ("libidn", " -p, --profile=STRING Use specified stringprep profile instead\n Valid stringprep profiles: Nameprep\n iSCSI Nodeprep Resourceprep trace SASLprep\n" , 5) | |||
113 | iSCSI Nodeprep Resourceprep trace SASLprep\n\dcgettext ("libidn", " -p, --profile=STRING Use specified stringprep profile instead\n Valid stringprep profiles: Nameprep\n iSCSI Nodeprep Resourceprep trace SASLprep\n" , 5) | |||
114 | ")dcgettext ("libidn", " -p, --profile=STRING Use specified stringprep profile instead\n Valid stringprep profiles: Nameprep\n iSCSI Nodeprep Resourceprep trace SASLprep\n" , 5), stdoutstdout); | |||
115 | fputs (_("\dcgettext ("libidn", " --debug Print debugging information\n --quiet Silent operation\n" , 5) | |||
116 | --debug Print debugging information\n\dcgettext ("libidn", " --debug Print debugging information\n --quiet Silent operation\n" , 5) | |||
117 | --quiet Silent operation\n\dcgettext ("libidn", " --debug Print debugging information\n --quiet Silent operation\n" , 5) | |||
118 | ")dcgettext ("libidn", " --debug Print debugging information\n --quiet Silent operation\n" , 5), stdoutstdout); | |||
119 | emit_bug_reporting_address (); | |||
120 | } | |||
121 | exit (status); | |||
122 | } | |||
123 | ||||
124 | int | |||
125 | main (int argc, char *argv[]) | |||
126 | { | |||
127 | struct gengetopt_args_info args_info; | |||
128 | char *line = NULL((void*)0); | |||
129 | size_t linelen = 0; | |||
130 | char *p, *r; | |||
131 | uint32_t *q; | |||
132 | unsigned cmdn = 0; | |||
133 | int rc; | |||
134 | ||||
135 | setlocale (LC_ALL6, ""); | |||
136 | set_program_name (argv[0]); | |||
137 | bindtextdomain (PACKAGE"libidn", LOCALEDIR"/usr/local/share/locale"); | |||
138 | textdomain (PACKAGE"libidn"); | |||
139 | ||||
140 | if (cmdline_parser (argc, argv, &args_info) != 0) | |||
| ||||
141 | usage (EXIT_FAILURE1); | |||
142 | ||||
143 | if (args_info.version_given) | |||
144 | { | |||
145 | version_etc (stdoutstdout, "idn", PACKAGE_NAME"GNU Libidn", VERSION"1.36", | |||
146 | "Simon Josefsson", (char *) NULL((void*)0)); | |||
147 | return EXIT_SUCCESS0; | |||
148 | } | |||
149 | ||||
150 | if (args_info.help_given) | |||
151 | usage (EXIT_SUCCESS0); | |||
152 | ||||
153 | /* Backwards compatibility: -n has always been the documented short | |||
154 | form for --nfkc but, before v1.10, -k was the implemented short | |||
155 | form. We now accept both to avoid documentation changes. */ | |||
156 | if (args_info.hidden_nfkc_given) | |||
157 | args_info.nfkc_given = 1; | |||
158 | ||||
159 | if (!args_info.stringprep_given && | |||
160 | !args_info.punycode_encode_given && !args_info.punycode_decode_given && | |||
161 | !args_info.idna_to_ascii_given && !args_info.idna_to_unicode_given && | |||
162 | !args_info.nfkc_given) | |||
163 | args_info.idna_to_ascii_given = 1; | |||
164 | ||||
165 | if ((args_info.stringprep_given ? 1 : 0) + | |||
166 | (args_info.punycode_encode_given ? 1 : 0) + | |||
167 | (args_info.punycode_decode_given ? 1 : 0) + | |||
168 | (args_info.idna_to_ascii_given ? 1 : 0) + | |||
169 | (args_info.idna_to_unicode_given ? 1 : 0) + | |||
170 | (args_info.nfkc_given ? 1 : 0) != 1) | |||
171 | { | |||
172 | error (0, 0, | |||
173 | _("only one of -s, -e, -d, -a, -u or -n can be specified")dcgettext ("libidn", "only one of -s, -e, -d, -a, -u or -n can be specified" , 5)); | |||
174 | usage (EXIT_FAILURE1); | |||
175 | } | |||
176 | ||||
177 | if (!args_info.quiet_given | |||
178 | && args_info.inputs_num == 0 && isatty (fileno (stdinstdin))) | |||
179 | fprintf (stderrstderr, "%s %s\n" GREETING"Copyright 2002-2015 Simon Josefsson.\n" "GNU Libidn is free software with ABSOLUTELY NO WARRANTY. For more\n" "information about these matters, see <http://www.gnu.org/licenses/>.\n", PACKAGE"libidn", VERSION"1.36"); | |||
180 | ||||
181 | if (args_info.debug_given) | |||
182 | fprintf (stderrstderr, _("Charset: %s\n")dcgettext ("libidn", "Charset: %s\n", 5), stringprep_locale_charset ()); | |||
183 | ||||
184 | if (!args_info.quiet_given | |||
185 | && args_info.inputs_num == 0 && isatty (fileno (stdinstdin))) | |||
186 | fprintf (stderrstderr, _("Type each input string on a line by itself, "dcgettext ("libidn", "Type each input string on a line by itself, " "terminated by a newline character.\n", 5) | |||
187 | "terminated by a newline character.\n")dcgettext ("libidn", "Type each input string on a line by itself, " "terminated by a newline character.\n", 5)); | |||
188 | ||||
189 | do | |||
190 | { | |||
191 | if (cmdn < args_info.inputs_num) | |||
192 | line = strdup (args_info.inputs[cmdn++]); | |||
193 | else if (getline (&line, &linelen, stdinstdin) == -1) | |||
194 | { | |||
195 | if (feof (stdinstdin)) | |||
196 | break; | |||
197 | ||||
198 | error (EXIT_FAILURE1, errno(*__errno_location ()), _("input error")dcgettext ("libidn", "input error", 5)); | |||
199 | } | |||
200 | ||||
201 | if (strlen (line) > 0) | |||
| ||||
202 | if (line[strlen (line) - 1] == '\n') | |||
203 | line[strlen (line) - 1] = '\0'; | |||
204 | ||||
205 | if (args_info.stringprep_given) | |||
206 | { | |||
207 | p = stringprep_locale_to_utf8 (line); | |||
208 | if (!p) | |||
209 | error (EXIT_FAILURE1, 0, _("could not convert from %s to UTF-8")dcgettext ("libidn", "could not convert from %s to UTF-8", 5), | |||
210 | stringprep_locale_charset ()); | |||
211 | ||||
212 | q = stringprep_utf8_to_ucs4 (p, -1, NULL((void*)0)); | |||
213 | if (!q) | |||
214 | { | |||
215 | free (p); | |||
216 | error (EXIT_FAILURE1, 0, | |||
217 | _("could not convert from UTF-8 to UCS-4")dcgettext ("libidn", "could not convert from UTF-8 to UCS-4", 5)); | |||
218 | } | |||
219 | ||||
220 | if (args_info.debug_given) | |||
221 | { | |||
222 | size_t i; | |||
223 | for (i = 0; q[i]; i++) | |||
224 | fprintf (stderrstderr, "input[%lu] = U+%04x\n", | |||
225 | (unsigned long) i, q[i]); | |||
226 | } | |||
227 | free (q); | |||
228 | ||||
229 | rc = stringprep_profile (p, &r, | |||
230 | args_info.profile_given ? | |||
231 | args_info.profile_arg : "Nameprep", 0); | |||
232 | free (p); | |||
233 | if (rc != STRINGPREP_OK) | |||
234 | error (EXIT_FAILURE1, 0, _("stringprep_profile: %s")dcgettext ("libidn", "stringprep_profile: %s", 5), | |||
235 | stringprep_strerror (rc)); | |||
236 | ||||
237 | q = stringprep_utf8_to_ucs4 (r, -1, NULL((void*)0)); | |||
238 | if (!q) | |||
239 | { | |||
240 | free (r); | |||
241 | error (EXIT_FAILURE1, 0, | |||
242 | _("could not convert from UTF-8 to UCS-4")dcgettext ("libidn", "could not convert from UTF-8 to UCS-4", 5)); | |||
243 | } | |||
244 | ||||
245 | if (args_info.debug_given) | |||
246 | { | |||
247 | size_t i; | |||
248 | for (i = 0; q[i]; i++) | |||
249 | fprintf (stderrstderr, "output[%lu] = U+%04x\n", | |||
250 | (unsigned long) i, q[i]); | |||
251 | } | |||
252 | free (q); | |||
253 | ||||
254 | p = stringprep_utf8_to_locale (r); | |||
255 | free (r); | |||
256 | if (!p) | |||
257 | error (EXIT_FAILURE1, 0, _("could not convert from UTF-8 to %s")dcgettext ("libidn", "could not convert from UTF-8 to %s", 5), | |||
258 | stringprep_locale_charset ()); | |||
259 | ||||
260 | fprintf (stdoutstdout, "%s\n", p); | |||
261 | ||||
262 | free (p); | |||
263 | } | |||
264 | ||||
265 | if (args_info.punycode_encode_given) | |||
266 | { | |||
267 | char encbuf[BUFSIZ8192]; | |||
268 | size_t len, len2; | |||
269 | ||||
270 | p = stringprep_locale_to_utf8 (line); | |||
271 | if (!p) | |||
272 | error (EXIT_FAILURE1, 0, _("could not convert from %s to UTF-8")dcgettext ("libidn", "could not convert from %s to UTF-8", 5), | |||
273 | stringprep_locale_charset ()); | |||
274 | ||||
275 | q = stringprep_utf8_to_ucs4 (p, -1, &len); | |||
276 | free (p); | |||
277 | if (!q) | |||
278 | error (EXIT_FAILURE1, 0, | |||
279 | _("could not convert from UTF-8 to UCS-4")dcgettext ("libidn", "could not convert from UTF-8 to UCS-4", 5)); | |||
280 | ||||
281 | if (args_info.debug_given) | |||
282 | { | |||
283 | size_t i; | |||
284 | for (i = 0; i < len; i++) | |||
285 | fprintf (stderrstderr, "input[%lu] = U+%04x\n", | |||
286 | (unsigned long) i, q[i]); | |||
287 | } | |||
288 | ||||
289 | len2 = BUFSIZ8192 - 1; | |||
290 | rc = punycode_encode (len, q, NULL((void*)0), &len2, encbuf); | |||
291 | free (q); | |||
292 | if (rc != PUNYCODE_SUCCESS) | |||
293 | error (EXIT_FAILURE1, 0, _("punycode_encode: %s")dcgettext ("libidn", "punycode_encode: %s", 5), | |||
294 | punycode_strerror (rc)); | |||
295 | ||||
296 | encbuf[len2] = '\0'; | |||
297 | ||||
298 | p = stringprep_utf8_to_locale (encbuf); | |||
299 | if (!p) | |||
300 | error (EXIT_FAILURE1, 0, _("could not convert from UTF-8 to %s")dcgettext ("libidn", "could not convert from UTF-8 to %s", 5), | |||
301 | stringprep_locale_charset ()); | |||
302 | ||||
303 | fprintf (stdoutstdout, "%s\n", p); | |||
304 | ||||
305 | free (p); | |||
306 | } | |||
307 | ||||
308 | if (args_info.punycode_decode_given) | |||
309 | { | |||
310 | size_t len; | |||
311 | ||||
312 | len = BUFSIZ8192; | |||
313 | q = (uint32_t *) malloc (len * sizeof (q[0])); | |||
314 | if (!q) | |||
315 | error (EXIT_FAILURE1, ENOMEM12, N_("malloc")"malloc"); | |||
316 | ||||
317 | rc = punycode_decode (strlen (line), line, &len, q, NULL((void*)0)); | |||
318 | if (rc != PUNYCODE_SUCCESS) | |||
319 | { | |||
320 | free (q); | |||
321 | error (EXIT_FAILURE1, 0, _("punycode_decode: %s")dcgettext ("libidn", "punycode_decode: %s", 5), | |||
322 | punycode_strerror (rc)); | |||
323 | } | |||
324 | ||||
325 | if (args_info.debug_given) | |||
326 | { | |||
327 | size_t i; | |||
328 | for (i = 0; i < len; i++) | |||
329 | fprintf (stderrstderr, "output[%lu] = U+%04x\n", | |||
330 | (unsigned long) i, q[i]); | |||
331 | } | |||
332 | ||||
333 | q[len] = 0; | |||
334 | r = stringprep_ucs4_to_utf8 (q, -1, NULL((void*)0), NULL((void*)0)); | |||
335 | free (q); | |||
336 | if (!r) | |||
337 | error (EXIT_FAILURE1, 0, | |||
338 | _("could not convert from UCS-4 to UTF-8")dcgettext ("libidn", "could not convert from UCS-4 to UTF-8", 5)); | |||
339 | ||||
340 | p = stringprep_utf8_to_locale (r); | |||
341 | free (r); | |||
342 | if (!r) | |||
343 | error (EXIT_FAILURE1, 0, _("could not convert from UTF-8 to %s")dcgettext ("libidn", "could not convert from UTF-8 to %s", 5), | |||
344 | stringprep_locale_charset ()); | |||
345 | ||||
346 | fprintf (stdoutstdout, "%s\n", p); | |||
347 | ||||
348 | free (p); | |||
349 | } | |||
350 | ||||
351 | if (args_info.idna_to_ascii_given) | |||
352 | { | |||
353 | p = stringprep_locale_to_utf8 (line); | |||
354 | if (!p) | |||
355 | error (EXIT_FAILURE1, 0, _("could not convert from %s to UTF-8")dcgettext ("libidn", "could not convert from %s to UTF-8", 5), | |||
356 | stringprep_locale_charset ()); | |||
357 | ||||
358 | q = stringprep_utf8_to_ucs4 (p, -1, NULL((void*)0)); | |||
359 | free (p); | |||
360 | if (!q) | |||
361 | error (EXIT_FAILURE1, 0, | |||
362 | _("could not convert from UCS-4 to UTF-8")dcgettext ("libidn", "could not convert from UCS-4 to UTF-8", 5)); | |||
363 | ||||
364 | if (args_info.debug_given) | |||
365 | { | |||
366 | size_t i; | |||
367 | for (i = 0; q[i]; i++) | |||
368 | fprintf (stderrstderr, "input[%lu] = U+%04x\n", | |||
369 | (unsigned long) i, q[i]); | |||
370 | } | |||
371 | ||||
372 | rc = idna_to_ascii_4z (q, &p, | |||
373 | (args_info.allow_unassigned_given ? | |||
374 | IDNA_ALLOW_UNASSIGNED : 0) | | |||
375 | (args_info.usestd3asciirules_given ? | |||
376 | IDNA_USE_STD3_ASCII_RULES : 0)); | |||
377 | free (q); | |||
378 | if (rc != IDNA_SUCCESS) | |||
379 | error (EXIT_FAILURE1, 0, _("idna_to_ascii_4z: %s")dcgettext ("libidn", "idna_to_ascii_4z: %s", 5), | |||
380 | idna_strerror (rc)); | |||
381 | ||||
382 | #ifdef WITH_TLD1 | |||
383 | if (args_info.tld_flag && !args_info.no_tld_flag) | |||
384 | { | |||
385 | size_t errpos; | |||
386 | ||||
387 | rc = idna_to_unicode_8z4z (p, &q, | |||
388 | (args_info.allow_unassigned_given ? | |||
389 | IDNA_ALLOW_UNASSIGNED : 0) | | |||
390 | (args_info.usestd3asciirules_given ? | |||
391 | IDNA_USE_STD3_ASCII_RULES : 0)); | |||
392 | if (rc != IDNA_SUCCESS) | |||
393 | error (EXIT_FAILURE1, 0, _("idna_to_unicode_8z4z (TLD): %s")dcgettext ("libidn", "idna_to_unicode_8z4z (TLD): %s", 5), | |||
394 | idna_strerror (rc)); | |||
395 | ||||
396 | if (args_info.debug_given) | |||
397 | { | |||
398 | size_t i; | |||
399 | for (i = 0; q[i]; i++) | |||
400 | fprintf (stderrstderr, "tld[%lu] = U+%04x\n", | |||
401 | (unsigned long) i, q[i]); | |||
402 | } | |||
403 | ||||
404 | rc = tld_check_4z (q, &errpos, NULL((void*)0)); | |||
405 | free (q); | |||
406 | if (rc == TLD_INVALID) | |||
407 | error (EXIT_FAILURE1, 0, _("tld_check_4z (position %lu): %s")dcgettext ("libidn", "tld_check_4z (position %lu): %s", 5), | |||
408 | (unsigned long) errpos, tld_strerror (rc)); | |||
409 | if (rc != TLD_SUCCESS) | |||
410 | error (EXIT_FAILURE1, 0, _("tld_check_4z: %s")dcgettext ("libidn", "tld_check_4z: %s", 5), | |||
411 | tld_strerror (rc)); | |||
412 | } | |||
413 | #endif | |||
414 | ||||
415 | if (args_info.debug_given) | |||
416 | { | |||
417 | size_t i; | |||
418 | for (i = 0; p[i]; i++) | |||
419 | fprintf (stderrstderr, "output[%lu] = U+%04x\n", | |||
420 | (unsigned long) i, (unsigned) p[i]); | |||
421 | } | |||
422 | ||||
423 | fprintf (stdoutstdout, "%s\n", p); | |||
424 | ||||
425 | free (p); | |||
426 | } | |||
427 | ||||
428 | if (args_info.idna_to_unicode_given) | |||
429 | { | |||
430 | p = stringprep_locale_to_utf8 (line); | |||
431 | if (!p) | |||
432 | error (EXIT_FAILURE1, 0, _("could not convert from %s to UTF-8")dcgettext ("libidn", "could not convert from %s to UTF-8", 5), | |||
433 | stringprep_locale_charset ()); | |||
434 | ||||
435 | q = stringprep_utf8_to_ucs4 (p, -1, NULL((void*)0)); | |||
436 | if (!q) | |||
437 | { | |||
438 | free (p); | |||
439 | error (EXIT_FAILURE1, 0, | |||
440 | _("could not convert from UCS-4 to UTF-8")dcgettext ("libidn", "could not convert from UCS-4 to UTF-8", 5)); | |||
441 | } | |||
442 | ||||
443 | if (args_info.debug_given) | |||
444 | { | |||
445 | size_t i; | |||
446 | for (i = 0; q[i]; i++) | |||
447 | fprintf (stderrstderr, "input[%lu] = U+%04x\n", | |||
448 | (unsigned long) i, q[i]); | |||
449 | } | |||
450 | free (q); | |||
451 | ||||
452 | rc = idna_to_unicode_8z4z (p, &q, | |||
453 | (args_info.allow_unassigned_given ? | |||
454 | IDNA_ALLOW_UNASSIGNED : 0) | | |||
455 | (args_info.usestd3asciirules_given ? | |||
456 | IDNA_USE_STD3_ASCII_RULES : 0)); | |||
457 | free (p); | |||
458 | if (rc != IDNA_SUCCESS) | |||
459 | error (EXIT_FAILURE1, 0, _("idna_to_unicode_8z4z: %s")dcgettext ("libidn", "idna_to_unicode_8z4z: %s", 5), | |||
460 | idna_strerror (rc)); | |||
461 | ||||
462 | if (args_info.debug_given) | |||
463 | { | |||
464 | size_t i; | |||
465 | for (i = 0; q[i]; i++) | |||
466 | fprintf (stderrstderr, "output[%lu] = U+%04x\n", | |||
467 | (unsigned long) i, q[i]); | |||
468 | } | |||
469 | ||||
470 | #ifdef WITH_TLD1 | |||
471 | if (args_info.tld_flag) | |||
472 | { | |||
473 | size_t errpos; | |||
474 | ||||
475 | rc = tld_check_4z (q, &errpos, NULL((void*)0)); | |||
476 | if (rc == TLD_INVALID) | |||
477 | { | |||
478 | free (q); | |||
479 | error (EXIT_FAILURE1, 0, | |||
480 | _("tld_check_4z (position %lu): %s")dcgettext ("libidn", "tld_check_4z (position %lu): %s", 5), | |||
481 | (unsigned long) errpos, tld_strerror (rc)); | |||
482 | } | |||
483 | if (rc != TLD_SUCCESS) | |||
484 | { | |||
485 | free (q); | |||
486 | error (EXIT_FAILURE1, 0, _("tld_check_4z: %s")dcgettext ("libidn", "tld_check_4z: %s", 5), | |||
487 | tld_strerror (rc)); | |||
488 | } | |||
489 | } | |||
490 | #endif | |||
491 | ||||
492 | r = stringprep_ucs4_to_utf8 (q, -1, NULL((void*)0), NULL((void*)0)); | |||
493 | free (q); | |||
494 | if (!r) | |||
495 | error (EXIT_FAILURE1, 0, | |||
496 | _("could not convert from UTF-8 to UCS-4")dcgettext ("libidn", "could not convert from UTF-8 to UCS-4", 5)); | |||
497 | ||||
498 | p = stringprep_utf8_to_locale (r); | |||
499 | free (r); | |||
500 | if (!p) | |||
501 | error (EXIT_FAILURE1, 0, _("could not convert from UTF-8 to %s")dcgettext ("libidn", "could not convert from UTF-8 to %s", 5), | |||
502 | stringprep_locale_charset ()); | |||
503 | ||||
504 | fprintf (stdoutstdout, "%s\n", p); | |||
505 | ||||
506 | free (p); | |||
507 | } | |||
508 | ||||
509 | if (args_info.nfkc_given) | |||
510 | { | |||
511 | p = stringprep_locale_to_utf8 (line); | |||
512 | if (!p) | |||
513 | error (EXIT_FAILURE1, 0, _("could not convert from %s to UTF-8")dcgettext ("libidn", "could not convert from %s to UTF-8", 5), | |||
514 | stringprep_locale_charset ()); | |||
515 | ||||
516 | if (args_info.debug_given) | |||
517 | { | |||
518 | size_t i; | |||
519 | ||||
520 | q = stringprep_utf8_to_ucs4 (p, -1, NULL((void*)0)); | |||
521 | if (!q) | |||
522 | { | |||
523 | free (p); | |||
524 | error (EXIT_FAILURE1, 0, | |||
525 | _("could not convert from UTF-8 to UCS-4")dcgettext ("libidn", "could not convert from UTF-8 to UCS-4", 5)); | |||
526 | } | |||
527 | ||||
528 | for (i = 0; q[i]; i++) | |||
529 | fprintf (stderrstderr, "input[%lu] = U+%04x\n", | |||
530 | (unsigned long) i, q[i]); | |||
531 | ||||
532 | free (q); | |||
533 | } | |||
534 | ||||
535 | r = stringprep_utf8_nfkc_normalize (p, -1); | |||
536 | free (p); | |||
537 | if (!r) | |||
538 | error (EXIT_FAILURE1, 0, _("could not do NFKC normalization")dcgettext ("libidn", "could not do NFKC normalization", 5)); | |||
539 | ||||
540 | if (args_info.debug_given) | |||
541 | { | |||
542 | size_t i; | |||
543 | ||||
544 | q = stringprep_utf8_to_ucs4 (r, -1, NULL((void*)0)); | |||
545 | if (!q) | |||
546 | { | |||
547 | free (r); | |||
548 | error (EXIT_FAILURE1, 0, | |||
549 | _("could not convert from UTF-8 to UCS-4")dcgettext ("libidn", "could not convert from UTF-8 to UCS-4", 5)); | |||
550 | } | |||
551 | ||||
552 | for (i = 0; q[i]; i++) | |||
553 | fprintf (stderrstderr, "output[%lu] = U+%04x\n", | |||
554 | (unsigned long) i, q[i]); | |||
555 | ||||
556 | free (q); | |||
557 | } | |||
558 | ||||
559 | p = stringprep_utf8_to_locale (r); | |||
560 | free (r); | |||
561 | if (!p) | |||
562 | error (EXIT_FAILURE1, 0, _("could not convert from UTF-8 to %s")dcgettext ("libidn", "could not convert from UTF-8 to %s", 5), | |||
563 | stringprep_locale_charset ()); | |||
564 | ||||
565 | fprintf (stdoutstdout, "%s\n", p); | |||
566 | ||||
567 | free (p); | |||
568 | } | |||
569 | ||||
570 | fflush (stdoutstdout); | |||
571 | } | |||
572 | while (!feof (stdinstdin) && !ferror (stdinstdin) && (args_info.inputs_num == 0 || | |||
573 | cmdn < args_info.inputs_num)); | |||
574 | ||||
575 | free (line); | |||
576 | ||||
577 | return EXIT_SUCCESS0; | |||
578 | } |