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