2 /* $NetBSD: iconv.c,v 1.16 2009/02/20 15:28:21 yamt Exp $ */
5 * SPDX-License-Identifier: BSD-2-Clause
7 * Copyright (c)2003 Citrus Project,
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
32 #include <sys/cdefs.h>
33 #include <sys/capsicum.h>
35 #include <capsicum_helpers.h>
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;
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}
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());
71 #define INBUFSIZE 1024
72 #define OUTBUFSIZE (INBUFSIZE * 2)
74 do_conv(FILE *fp, iconv_t cd, bool silent, bool hide_invalid)
76 char inbuf[INBUFSIZE], outbuf[OUTBUFSIZE], *in, *out;
77 unsigned long long invalids;
78 size_t inbytes, outbytes, ret;
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);
85 while ((inbytes = fread(inbuf, 1, INBUFSIZE, fp)) > 0) {
91 outbytes = OUTBUFSIZE;
92 ret = __iconv(cd, &in, &inbytes, &out, &outbytes,
95 if (outbytes < OUTBUFSIZE)
96 (void)fwrite(outbuf, 1, OUTBUFSIZE - outbytes,
98 if (ret == (size_t)-1 && errno != E2BIG) {
99 if (errno != EINVAL || in == inbuf)
100 err(EXIT_FAILURE, "iconv()");
102 /* incomplete input character */
103 (void)memmove(inbuf, in, inbytes);
104 ret = fread(inbuf + inbytes, 1,
105 INBUFSIZE - inbytes, fp);
110 "unexpected end of file; "
111 "the last character is "
114 err(EXIT_FAILURE, "fread()");
121 /* reset the shift state of the output buffer */
122 outbytes = OUTBUFSIZE;
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);
130 if (invalids > 0 && !silent)
131 warnx("warning: invalid characters: %llu", invalids);
133 return (invalids > 0);
137 do_list(unsigned int n, const char * const *list, void *data __unused)
141 for(i = 0; i < n; i++) {
142 printf("%s", list[i]);
152 main(int argc, char **argv)
156 const char *opt_f, *opt_t;
158 bool opt_c = false, opt_s = false;
162 setlocale(LC_ALL, "");
163 setprogname(argv[0]);
165 while ((ch = getopt_long(argc, argv, "csLlf:t:",
166 long_options, NULL)) != -1) {
176 if (opt_s || opt_c || strcmp(opt_f, "") != 0 ||
177 strcmp(opt_t, "") != 0) {
178 warnx("-l is not allowed with other flags.");
181 iconvlist(do_list, NULL);
182 return (EXIT_SUCCESS);
199 if ((strcmp(opt_f, "") == 0) && (strcmp(opt_t, "") == 0))
202 if (caph_limit_stdio() < 0)
203 err(EXIT_FAILURE, "capsicum");
206 * Cache NLS data, for strerror, for err(3), before entering capability
209 caph_cache_catpages();
212 * Cache iconv conversion handle before entering sandbox.
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);
219 if (caph_enter() < 0)
220 err(EXIT_FAILURE, "unable to enter capability mode");
221 res = do_conv(stdin, cd, opt_s, opt_c);
224 for (i = 0; i < argc; i++) {
225 fp = (strcmp(argv[i], "-") != 0) ?
226 fopen(argv[i], "r") : stdin;
228 err(EXIT_FAILURE, "Cannot open `%s'",
230 /* Enter Capsicum sandbox for final input file. */
231 if (i + 1 == argc && caph_enter() < 0)
233 "unable to enter capability mode");
234 res |= do_conv(fp, cd, opt_s, opt_c);
237 /* Reset iconv descriptor state. */
238 (void)iconv(cd, NULL, NULL, NULL, NULL);
242 return (res == 0 ? EXIT_SUCCESS : EXIT_FAILURE);