xref: /freebsd/usr.bin/iconv/iconv.c (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
1 /* $NetBSD: iconv.c,v 1.16 2009/02/20 15:28:21 yamt Exp $ */
2 
3 /*-
4  * SPDX-License-Identifier: BSD-2-Clause
5  *
6  * Copyright (c)2003 Citrus Project,
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include <sys/cdefs.h>
32 #include <sys/capsicum.h>
33 
34 #include <capsicum_helpers.h>
35 #include <err.h>
36 #include <errno.h>
37 #include <getopt.h>
38 #include <iconv.h>
39 #include <limits.h>
40 #include <locale.h>
41 #include <stdbool.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
46 
47 static int		do_conv(FILE *, iconv_t, bool, bool);
48 static int		do_list(unsigned int, const char * const *, void *);
49 static void		usage(void) __dead2;
50 
51 static const struct option long_options[] = {
52 	{"from-code",		required_argument,	NULL, 'f'},
53 	{"list",		no_argument,		NULL, 'l'},
54 	{"silent",		no_argument,		NULL, 's'},
55         {"to-code",		required_argument,	NULL, 't'},
56         {NULL,                  no_argument,            NULL, 0}
57 };
58 
59 static void
60 usage(void)
61 {
62 	(void)fprintf(stderr,
63 	    "Usage:\t%1$s [-cs] -f <from_code> -t <to_code> [file ...]\n"
64 	    "\t%1$s -f <from_code> [-cs] [-t <to_code>] [file ...]\n"
65 	    "\t%1$s -t <to_code> [-cs] [-f <from_code>] [file ...]\n"
66 	    "\t%1$s -l\n", getprogname());
67 	exit(1);
68 }
69 
70 #define INBUFSIZE 1024
71 #define OUTBUFSIZE (INBUFSIZE * 2)
72 static int
73 do_conv(FILE *fp, iconv_t cd, bool silent, bool hide_invalid)
74 {
75 	char inbuf[INBUFSIZE], outbuf[OUTBUFSIZE], *in, *out;
76 	unsigned long long invalids;
77 	size_t inbytes, outbytes, ret;
78 
79 	/*
80 	 * Don't touch ICONV_SET_DISCARD_ILSEQ if -c wasn't specified.  It may
81 	 * be that the user has specified //IGNORE in the -t specification, and
82 	 * we don't want to clobber that.
83 	 */
84 	if (hide_invalid) {
85 		int arg = (int)hide_invalid;
86 		if (iconvctl(cd, ICONV_SET_DISCARD_ILSEQ, (void *)&arg) == -1)
87 			err(EXIT_FAILURE, "iconvctl(DISCARD_ILSEQ, %d)", arg);
88 	}
89 
90 	invalids = 0;
91 	while ((inbytes = fread(inbuf, 1, INBUFSIZE, fp)) > 0) {
92 		in = inbuf;
93 		while (inbytes > 0) {
94 			size_t inval;
95 
96 			out = outbuf;
97 			outbytes = OUTBUFSIZE;
98 			ret = __iconv(cd, &in, &inbytes, &out, &outbytes,
99 			    0, &inval);
100 			invalids += inval;
101 			if (outbytes < OUTBUFSIZE)
102 				(void)fwrite(outbuf, 1, OUTBUFSIZE - outbytes,
103 				    stdout);
104 			if (ret == (size_t)-1 && errno != E2BIG) {
105 				if (errno != EINVAL || in == inbuf)
106 					err(EXIT_FAILURE, "iconv()");
107 
108 				/* incomplete input character */
109 				(void)memmove(inbuf, in, inbytes);
110 				ret = fread(inbuf + inbytes, 1,
111 				    INBUFSIZE - inbytes, fp);
112 				if (ret == 0) {
113 					fflush(stdout);
114 					if (feof(fp))
115 						errx(EXIT_FAILURE,
116 						    "unexpected end of file; "
117 						    "the last character is "
118 						    "incomplete.");
119 					else
120 						err(EXIT_FAILURE, "fread()");
121 				}
122 				in = inbuf;
123 				inbytes += ret;
124 			}
125 		}
126 	}
127 	/* reset the shift state of the output buffer */
128 	outbytes = OUTBUFSIZE;
129 	out = outbuf;
130 	ret = iconv(cd, NULL, NULL, &out, &outbytes);
131 	if (ret == (size_t)-1)
132 		err(EXIT_FAILURE, "iconv()");
133 	if (outbytes < OUTBUFSIZE)
134 		(void)fwrite(outbuf, 1, OUTBUFSIZE - outbytes, stdout);
135 
136 	if (invalids > 0 && !silent)
137 		warnx("warning: invalid characters: %llu", invalids);
138 
139 	return (invalids > 0);
140 }
141 
142 static int
143 do_list(unsigned int n, const char * const *list, void *data __unused)
144 {
145 	unsigned int i;
146 
147 	for(i = 0; i < n; i++) {
148 		printf("%s", list[i]);
149 		if (i < n - 1)
150 			printf(" ");
151 	}
152 	printf("\n");
153 
154 	return (1);
155 }
156 
157 int
158 main(int argc, char **argv)
159 {
160 	iconv_t cd;
161 	FILE *fp;
162 	const char *opt_f, *opt_t;
163 	int ch, i, res;
164 	bool opt_c = false, opt_s = false;
165 
166 	opt_f = opt_t = "";
167 
168 	setlocale(LC_ALL, "");
169 	setprogname(argv[0]);
170 
171 	while ((ch = getopt_long(argc, argv, "csLlf:t:",
172 	    long_options, NULL)) != -1) {
173 		switch (ch) {
174 		case 'c':
175 			opt_c = true;
176 			break;
177 		case 's':
178 			opt_s = true;
179 			break;
180 		case 'l':
181 			/* list */
182 			if (opt_s || opt_c || strcmp(opt_f, "") != 0 ||
183 			    strcmp(opt_t, "") != 0) {
184 				warnx("-l is not allowed with other flags.");
185 				usage();
186 			}
187 			iconvlist(do_list, NULL);
188 			return (EXIT_SUCCESS);
189 		case 'f':
190 			/* from */
191 			if (optarg != NULL)
192 				opt_f = optarg;
193 			break;
194 		case 't':
195 			/* to */
196 			if (optarg != NULL)
197 				opt_t = optarg;
198 			break;
199 		default:
200 			usage();
201 		}
202 	}
203 	argc -= optind;
204 	argv += optind;
205 	if ((strcmp(opt_f, "") == 0) && (strcmp(opt_t, "") == 0))
206 		usage();
207 
208 	if (caph_limit_stdio() < 0)
209 		err(EXIT_FAILURE, "capsicum");
210 
211 	/*
212 	 * Cache NLS data, for strerror, for err(3), before entering capability
213 	 * mode.
214 	 */
215 	caph_cache_catpages();
216 
217 	/*
218 	 * Cache iconv conversion handle before entering sandbox.
219 	 */
220 	cd = iconv_open(opt_t, opt_f);
221 	if (cd == (iconv_t)-1)
222 		err(EXIT_FAILURE, "iconv_open(%s, %s)", opt_t, opt_f);
223 
224 	if (argc == 0) {
225 		if (caph_enter() < 0)
226 			err(EXIT_FAILURE, "unable to enter capability mode");
227 		res = do_conv(stdin, cd, opt_s, opt_c);
228 	} else {
229 		res = 0;
230 		for (i = 0; i < argc; i++) {
231 			fp = (strcmp(argv[i], "-") != 0) ?
232 			    fopen(argv[i], "r") : stdin;
233 			if (fp == NULL)
234 				err(EXIT_FAILURE, "Cannot open `%s'",
235 				    argv[i]);
236 			/* Enter Capsicum sandbox for final input file. */
237 			if (i + 1 == argc && caph_enter() < 0)
238 				err(EXIT_FAILURE,
239 				    "unable to enter capability mode");
240 			res |= do_conv(fp, cd, opt_s, opt_c);
241 			(void)fclose(fp);
242 
243 			/* Reset iconv descriptor state. */
244 			(void)iconv(cd, NULL, NULL, NULL, NULL);
245 		}
246 	}
247 	iconv_close(cd);
248 	return (res == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
249 }
250