1 /* Convert using charmaps and possibly iconv().
2 Copyright (C) 2001-2021 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@redhat.com>, 2001.
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
8 by the Free Software Foundation; version 2 of the License, or
9 (at your option) any later version.
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.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see <https://www.gnu.org/licenses/>. */
32 #include "iconv_prog.h"
35 /* Prototypes for a few program-wide used functions. */
36 #include <programs/xmalloc.h>
44 struct convtable *sub;
50 static inline struct convtable *
53 return (struct convtable *) xcalloc (1, sizeof (struct convtable));
57 free_table (struct convtable *tbl)
64 is_term (struct convtable *tbl, unsigned int idx)
66 return tbl->term[idx / 8] & (1 << (idx % 8));
71 clear_term (struct convtable *tbl, unsigned int idx)
73 tbl->term[idx / 8] &= ~(1 << (idx % 8));
78 set_term (struct convtable *tbl, unsigned int idx)
80 tbl->term[idx / 8] |= 1 << (idx % 8);
84 /* Generate the conversion table. */
85 static struct convtable *use_from_charmap (struct charmap_t *from_charmap,
87 static struct convtable *use_to_charmap (const char *from_code,
88 struct charmap_t *to_charmap);
89 static struct convtable *use_both_charmaps (struct charmap_t *from_charmap,
90 struct charmap_t *to_charmap);
92 /* Prototypes for the functions doing the actual work. */
93 static int process_block (struct convtable *tbl, char *addr, size_t len,
95 static int process_fd (struct convtable *tbl, int fd, FILE *output);
96 static int process_file (struct convtable *tbl, FILE *input, FILE *output);
100 charmap_conversion (const char *from_code, struct charmap_t *from_charmap,
101 const char *to_code, struct charmap_t *to_charmap,
102 int argc, int remaining, char *argv[],
103 const char *output_file)
105 struct convtable *cvtbl;
106 int status = EXIT_SUCCESS;
108 /* We have three different cases to handle:
110 - both, from_charmap and to_charmap, are available. This means we
111 can assume that the symbolic names match and use them to create
114 - only from_charmap is available. In this case we can only hope that
115 the symbolic names used are of the <Uxxxx> form in which case we
116 can use a UCS4->"to_code" iconv() conversion for the second step.
118 - only to_charmap is available. This is similar, only that we would
119 use iconv() for the "to_code"->UCS4 conversion.
121 We first create a table which maps input bytes into output bytes.
122 Once this is done we can handle all three of the cases above
124 if (from_charmap != NULL)
126 if (to_charmap == NULL)
127 cvtbl = use_from_charmap (from_charmap, to_code);
129 cvtbl = use_both_charmaps (from_charmap, to_charmap);
133 assert (to_charmap != NULL);
134 cvtbl = use_to_charmap (from_code, to_charmap);
137 /* If we couldn't generate a table stop now. */
141 /* Determine output file. */
143 if (output_file != NULL && strcmp (output_file, "-") != 0)
145 output = fopen (output_file, "w");
147 error (EXIT_FAILURE, errno, _("cannot open output file"));
152 /* We can now start the conversion. */
153 if (remaining == argc)
155 if (process_file (cvtbl, stdin, output) != 0)
156 status = EXIT_FAILURE;
164 printf ("%s:\n", argv[remaining]);
165 if (strcmp (argv[remaining], "-") == 0)
169 fd = open (argv[remaining], O_RDONLY);
173 error (0, errno, _("cannot open input file `%s'"),
175 status = EXIT_FAILURE;
180 #ifdef _POSIX_MAPPED_FILES
183 /* We have possibilities for reading the input file. First try
184 to mmap() it since this will provide the fastest solution. */
185 if (fstat64 (fd, &st) == 0
186 && ((addr = mmap (NULL, st.st_size, PROT_READ, MAP_PRIVATE,
187 fd, 0)) != MAP_FAILED))
189 /* Yes, we can use mmap(). The descriptor is not needed
192 error (EXIT_FAILURE, errno,
193 _("error while closing input `%s'"), argv[remaining]);
195 if (process_block (cvtbl, addr, st.st_size, output) < 0)
197 /* Something went wrong. */
198 status = EXIT_FAILURE;
200 /* We don't need the input data anymore. */
201 munmap ((void *) addr, st.st_size);
203 /* We cannot go on with producing output since it might
204 lead to problem because the last output might leave
205 the output stream in an undefined state. */
209 /* We don't need the input data anymore. */
210 munmap ((void *) addr, st.st_size);
213 #endif /* _POSIX_MAPPED_FILES */
215 /* Read the file in pieces. */
216 if (process_fd (cvtbl, fd, output) != 0)
218 /* Something went wrong. */
219 status = EXIT_FAILURE;
221 /* We don't need the input file anymore. */
224 /* We cannot go on with producing output since it might
225 lead to problem because the last output might leave
226 the output stream in an undefined state. */
230 /* Now close the file. */
234 while (++remaining < argc);
237 if (output != stdout)
244 /* Add the IN->OUT mapping to TBL. OUT is potentially stored in the table.
245 IN is used only here, so it need not be kept live afterwards. */
247 add_bytes (struct convtable *tbl, const struct charseq *in, struct charseq *out)
252 assert (in->nbytes > 0);
254 byte = ((unsigned char *) in->bytes)[n];
255 while (n + 1 < in->nbytes)
257 if (is_term (tbl, byte) || tbl->val[byte].sub == NULL)
259 /* Note that we simply ignore a definition for a byte sequence
260 which is also the prefix for a longer one. */
261 clear_term (tbl, byte);
263 (struct convtable *) xcalloc (1, sizeof (struct convtable));
266 tbl = tbl->val[byte].sub;
268 byte = ((unsigned char *) in->bytes)[++n];
271 /* Only add the new sequence if there is none yet and the byte sequence
272 is not part of an even longer one. */
273 if (! is_term (tbl, byte) && tbl->val[byte].sub == NULL)
275 set_term (tbl, byte);
276 tbl->val[byte].out = out;
280 /* Try to convert SEQ from WCHAR_T format using CD.
281 Returns a malloc'd struct or NULL. */
282 static struct charseq *
283 convert_charseq (iconv_t cd, const struct charseq *seq)
285 struct charseq *result = NULL;
287 if (seq->ucs4 != UNINITIALIZED_CHAR_VALUE)
289 /* There is a chance. Try the iconv module. */
290 wchar_t inbuf[1] = { seq->ucs4 };
291 unsigned char outbuf[64];
292 char *inptr = (char *) inbuf;
293 size_t inlen = sizeof (inbuf);
294 char *outptr = (char *) outbuf;
295 size_t outlen = sizeof (outbuf);
297 (void) iconv (cd, &inptr, &inlen, &outptr, &outlen);
299 if (outptr != (char *) outbuf)
301 /* We got some output. Good, use it. */
302 outlen = sizeof (outbuf) - outlen;
303 assert ((char *) outbuf + outlen == outptr);
305 result = xmalloc (sizeof (struct charseq) + outlen);
306 result->name = seq->name;
307 result->ucs4 = seq->ucs4;
308 result->nbytes = outlen;
309 memcpy (result->bytes, outbuf, outlen);
312 /* Clear any possible state left behind. */
313 (void) iconv (cd, NULL, NULL, NULL, NULL);
320 static struct convtable *
321 use_from_charmap (struct charmap_t *from_charmap, const char *to_code)
323 /* We iterate over all entries in the from_charmap and for those which
324 have a known UCS4 representation we use an iconv() call to determine
325 the mapping to the to_code charset. */
326 struct convtable *rettbl;
333 cd = iconv_open (to_code, "WCHAR_T");
334 if (cd == (iconv_t) -1)
335 /* We cannot do anything. */
338 rettbl = allocate_table ();
340 while (iterate_table (&from_charmap->char_table, &ptr, &key, &keylen, &data)
343 struct charseq *in = data;
344 struct charseq *newp = convert_charseq (cd, in);
346 add_bytes (rettbl, in, newp);
355 static struct convtable *
356 use_to_charmap (const char *from_code, struct charmap_t *to_charmap)
358 /* We iterate over all entries in the to_charmap and for those which
359 have a known UCS4 representation we use an iconv() call to determine
360 the mapping to the from_code charset. */
361 struct convtable *rettbl;
368 /* Note that the conversion we use here is the reverse direction. Without
369 exhaustive search we cannot figure out which input yields the UCS4
370 character we are looking for. Therefore we determine it the other
372 cd = iconv_open (from_code, "WCHAR_T");
373 if (cd == (iconv_t) -1)
374 /* We cannot do anything. */
377 rettbl = allocate_table ();
379 while (iterate_table (&to_charmap->char_table, &ptr, &key, &keylen, &data)
382 struct charseq *out = data;
383 struct charseq *newp = convert_charseq (cd, out);
386 add_bytes (rettbl, newp, out);
397 static struct convtable *
398 use_both_charmaps (struct charmap_t *from_charmap,
399 struct charmap_t *to_charmap)
401 /* In this case we iterate over all the entries in the from_charmap,
402 determine the internal name, and find an appropriate entry in the
403 to_charmap (if it exists). */
404 struct convtable *rettbl = allocate_table ();
410 while (iterate_table (&from_charmap->char_table, &ptr, &key, &keylen, &data)
413 struct charseq *in = (struct charseq *) data;
414 struct charseq *out = charmap_find_value (to_charmap, key, keylen);
417 add_bytes (rettbl, in, out);
425 process_block (struct convtable *tbl, char *addr, size_t len, FILE *output)
431 struct convtable *cur = tbl;
432 unsigned char *curp = (unsigned char *) addr;
433 unsigned int byte = *curp;
437 while (! is_term (cur, byte))
438 if (cur->val[byte].sub == NULL)
440 /* This is an invalid sequence. Skip the first byte if we are
441 ignoring errors. Otherwise punt. */
444 error (0, 0, _("illegal input sequence at position %Zd"), n);
448 n -= curp - (unsigned char *) addr;
450 byte = *(curp = (unsigned char *) ++addr);
459 cur = cur->val[byte].sub;
464 incomplete character or shift sequence at end of buffer"));
471 /* We found a final byte. Write the output bytes. */
472 out = cur->val[byte].out;
473 for (cnt = 0; cnt < out->nbytes; ++cnt)
474 fputc_unlocked (out->bytes[cnt], output);
476 addr = (char *) curp + 1;
485 process_fd (struct convtable *tbl, int fd, FILE *output)
487 /* We have a problem with reading from a descriptor since we must not
488 provide the iconv() function an incomplete character or shift
489 sequence at the end of the buffer. Since we have to deal with
490 arbitrary encodings we must read the whole text in a buffer and
491 process it in one step. */
492 static char *inbuf = NULL;
493 static size_t maxlen = 0;
497 while (actlen < maxlen)
499 ssize_t n = read (fd, inptr, maxlen - actlen);
502 /* No more text to read. */
507 /* Error while reading. */
508 error (0, errno, _("error while reading the input"));
516 if (actlen == maxlen)
522 /* Increase the buffer. */
523 new_inbuf = (char *) realloc (inbuf, maxlen + 32768);
524 if (new_inbuf == NULL)
526 error (0, errno, _("unable to allocate buffer for input"));
531 inptr = inbuf + actlen;
535 n = read (fd, inptr, maxlen - actlen);
538 /* No more text to read. */
543 /* Error while reading. */
544 error (0, errno, _("error while reading the input"));
551 while (actlen < maxlen);
554 /* Break again so we leave both loops. */
558 /* Now we have all the input in the buffer. Process it in one run. */
559 return process_block (tbl, inbuf, actlen, output);
564 process_file (struct convtable *tbl, FILE *input, FILE *output)
566 /* This should be safe since we use this function only for `stdin' and
567 we haven't read anything so far. */
568 return process_fd (tbl, fileno (input), output);