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