1 /* Convert using charmaps and possibly iconv().
2 Copyright (C) 2001, 2005, 2006, 2008, 2012 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 <http://www.gnu.org/licenses/>. */
31 #include "iconv_prog.h"
34 /* Prototypes for a few program-wide used functions. */
35 extern void *xmalloc (size_t n
)
36 __attribute_malloc__
__attribute_alloc_size (1);
37 extern void *xcalloc (size_t n
, size_t s
)
38 __attribute_malloc__
__attribute_alloc_size (1, 2);
46 struct convtable
*sub
;
52 static inline struct convtable
*
55 return (struct convtable
*) xcalloc (1, sizeof (struct convtable
));
60 is_term (struct convtable
*tbl
, unsigned int idx
)
62 return tbl
->term
[idx
/ 8] & (1 << (idx
% 8));
67 clear_term (struct convtable
*tbl
, unsigned int idx
)
69 tbl
->term
[idx
/ 8] &= ~(1 << (idx
% 8));
74 set_term (struct convtable
*tbl
, unsigned int idx
)
76 tbl
->term
[idx
/ 8] |= 1 << (idx
% 8);
80 /* Generate the conversion table. */
81 static struct convtable
*use_from_charmap (struct charmap_t
*from_charmap
,
83 static struct convtable
*use_to_charmap (const char *from_code
,
84 struct charmap_t
*to_charmap
);
85 static struct convtable
*use_both_charmaps (struct charmap_t
*from_charmap
,
86 struct charmap_t
*to_charmap
);
88 /* Prototypes for the functions doing the actual work. */
89 static int process_block (struct convtable
*tbl
, char *addr
, size_t len
,
91 static int process_fd (struct convtable
*tbl
, int fd
, FILE *output
);
92 static int process_file (struct convtable
*tbl
, FILE *input
, FILE *output
);
96 charmap_conversion (const char *from_code
, struct charmap_t
*from_charmap
,
97 const char *to_code
, struct charmap_t
*to_charmap
,
98 int argc
, int remaining
, char *argv
[],
99 const char *output_file
)
101 struct convtable
*cvtbl
;
102 int status
= EXIT_SUCCESS
;
104 /* We have three different cases to handle:
106 - both, from_charmap and to_charmap, are available. This means we
107 can assume that the symbolic names match and use them to create
110 - only from_charmap is available. In this case we can only hope that
111 the symbolic names used are of the <Uxxxx> form in which case we
112 can use a UCS4->"to_code" iconv() conversion for the second step.
114 - only to_charmap is available. This is similar, only that we would
115 use iconv() for the "to_code"->UCS4 conversion.
117 We first create a table which maps input bytes into output bytes.
118 Once this is done we can handle all three of the cases above
120 if (from_charmap
!= NULL
)
122 if (to_charmap
== NULL
)
123 cvtbl
= use_from_charmap (from_charmap
, to_code
);
125 cvtbl
= use_both_charmaps (from_charmap
, to_charmap
);
129 assert (to_charmap
!= NULL
);
130 cvtbl
= use_to_charmap (from_code
, to_charmap
);
133 /* If we couldn't generate a table stop now. */
137 /* Determine output file. */
139 if (output_file
!= NULL
&& strcmp (output_file
, "-") != 0)
141 output
= fopen (output_file
, "w");
143 error (EXIT_FAILURE
, errno
, _("cannot open output file"));
148 /* We can now start the conversion. */
149 if (remaining
== argc
)
151 if (process_file (cvtbl
, stdin
, output
) != 0)
152 status
= EXIT_FAILURE
;
162 printf ("%s:\n", argv
[remaining
]);
163 if (strcmp (argv
[remaining
], "-") == 0)
167 fd
= open (argv
[remaining
], O_RDONLY
);
171 error (0, errno
, _("cannot open input file `%s'"),
173 status
= EXIT_FAILURE
;
178 #ifdef _POSIX_MAPPED_FILES
179 /* We have possibilities for reading the input file. First try
180 to mmap() it since this will provide the fastest solution. */
181 if (fstat (fd
, &st
) == 0
182 && ((addr
= mmap (NULL
, st
.st_size
, PROT_READ
, MAP_PRIVATE
,
183 fd
, 0)) != MAP_FAILED
))
185 /* Yes, we can use mmap(). The descriptor is not needed
188 error (EXIT_FAILURE
, errno
,
189 _("error while closing input `%s'"), argv
[remaining
]);
191 if (process_block (cvtbl
, addr
, st
.st_size
, output
) < 0)
193 /* Something went wrong. */
194 status
= EXIT_FAILURE
;
196 /* We don't need the input data anymore. */
197 munmap ((void *) addr
, st
.st_size
);
199 /* We cannot go on with producing output since it might
200 lead to problem because the last output might leave
201 the output stream in an undefined state. */
205 /* We don't need the input data anymore. */
206 munmap ((void *) addr
, st
.st_size
);
209 #endif /* _POSIX_MAPPED_FILES */
211 /* Read the file in pieces. */
212 if (process_fd (cvtbl
, fd
, output
) != 0)
214 /* Something went wrong. */
215 status
= EXIT_FAILURE
;
217 /* We don't need the input file anymore. */
220 /* We cannot go on with producing output since it might
221 lead to problem because the last output might leave
222 the output stream in an undefined state. */
226 /* Now close the file. */
230 while (++remaining
< argc
);
238 add_bytes (struct convtable
*tbl
, struct charseq
*in
, struct charseq
*out
)
243 assert (in
->nbytes
> 0);
245 byte
= ((unsigned char *) in
->bytes
)[n
];
246 while (n
+ 1 < in
->nbytes
)
248 if (is_term (tbl
, byte
) || tbl
->val
[byte
].sub
== NULL
)
250 /* Note that we simply ignore a definition for a byte sequence
251 which is also the prefix for a longer one. */
252 clear_term (tbl
, byte
);
254 (struct convtable
*) xcalloc (1, sizeof (struct convtable
));
257 tbl
= tbl
->val
[byte
].sub
;
259 byte
= ((unsigned char *) in
->bytes
)[++n
];
262 /* Only add the new sequence if there is none yet and the byte sequence
263 is not part of an even longer one. */
264 if (! is_term (tbl
, byte
) && tbl
->val
[byte
].sub
== NULL
)
266 set_term (tbl
, byte
);
267 tbl
->val
[byte
].out
= out
;
272 static struct convtable
*
273 use_from_charmap (struct charmap_t
*from_charmap
, const char *to_code
)
275 /* We iterate over all entries in the from_charmap and for those which
276 have a known UCS4 representation we use an iconv() call to determine
277 the mapping to the to_code charset. */
278 struct convtable
*rettbl
;
285 cd
= iconv_open (to_code
, "WCHAR_T");
286 if (cd
== (iconv_t
) -1)
287 /* We cannot do anything. */
290 rettbl
= allocate_table ();
292 while (iterate_table (&from_charmap
->char_table
, &ptr
, &key
, &keylen
, &data
)
295 struct charseq
*in
= (struct charseq
*) data
;
297 if (in
->ucs4
!= UNINITIALIZED_CHAR_VALUE
)
299 /* There is a chance. Try the iconv module. */
300 wchar_t inbuf
[1] = { in
->ucs4
};
301 unsigned char outbuf
[64];
302 char *inptr
= (char *) inbuf
;
303 size_t inlen
= sizeof (inbuf
);
304 char *outptr
= (char *) outbuf
;
305 size_t outlen
= sizeof (outbuf
);
307 (void) iconv (cd
, &inptr
, &inlen
, &outptr
, &outlen
);
309 if (outptr
!= (char *) outbuf
)
311 /* We got some output. Good, use it. */
312 struct charseq
*newp
;
314 outlen
= sizeof (outbuf
) - outlen
;
315 assert ((char *) outbuf
+ outlen
== outptr
);
317 newp
= (struct charseq
*) xmalloc (sizeof (struct charseq
)
319 newp
->name
= in
->name
;
320 newp
->ucs4
= in
->ucs4
;
321 newp
->nbytes
= outlen
;
322 memcpy (newp
->bytes
, outbuf
, outlen
);
324 add_bytes (rettbl
, in
, newp
);
327 /* Clear any possible state left behind. */
328 (void) iconv (cd
, NULL
, NULL
, NULL
, NULL
);
338 static struct convtable
*
339 use_to_charmap (const char *from_code
, struct charmap_t
*to_charmap
)
341 /* We iterate over all entries in the to_charmap and for those which
342 have a known UCS4 representation we use an iconv() call to determine
343 the mapping to the from_code charset. */
344 struct convtable
*rettbl
;
351 /* Note that the conversion we use here is the reverse direction. Without
352 exhaustive search we cannot figure out which input yields the UCS4
353 character we are looking for. Therefore we determine it the other
355 cd
= iconv_open (from_code
, "WCHAR_T");
356 if (cd
== (iconv_t
) -1)
357 /* We cannot do anything. */
360 rettbl
= allocate_table ();
362 while (iterate_table (&to_charmap
->char_table
, &ptr
, &key
, &keylen
, &data
)
365 struct charseq
*out
= (struct charseq
*) data
;
367 if (out
->ucs4
!= UNINITIALIZED_CHAR_VALUE
)
369 /* There is a chance. Try the iconv module. */
370 wchar_t inbuf
[1] = { out
->ucs4
};
371 unsigned char outbuf
[64];
372 char *inptr
= (char *) inbuf
;
373 size_t inlen
= sizeof (inbuf
);
374 char *outptr
= (char *) outbuf
;
375 size_t outlen
= sizeof (outbuf
);
377 (void) iconv (cd
, &inptr
, &inlen
, &outptr
, &outlen
);
379 if (outptr
!= (char *) outbuf
)
381 /* We got some output. Good, use it. */
390 unsigned char bytes
[outlen
];
394 outlen
= sizeof (outbuf
) - outlen
;
395 assert ((char *) outbuf
+ outlen
== outptr
);
397 new.mem
.name
= out
->name
;
398 new.mem
.ucs4
= out
->ucs4
;
399 new.mem
.nbytes
= outlen
;
400 memcpy (new.mem
.bytes
, outbuf
, outlen
);
402 add_bytes (rettbl
, &new.seq
, out
);
405 /* Clear any possible state left behind. */
406 (void) iconv (cd
, NULL
, NULL
, NULL
, NULL
);
416 static struct convtable
*
417 use_both_charmaps (struct charmap_t
*from_charmap
,
418 struct charmap_t
*to_charmap
)
420 /* In this case we iterate over all the entries in the from_charmap,
421 determine the internal name, and find an appropriate entry in the
422 to_charmap (if it exists). */
423 struct convtable
*rettbl
= allocate_table ();
429 while (iterate_table (&from_charmap
->char_table
, &ptr
, &key
, &keylen
, &data
)
432 struct charseq
*in
= (struct charseq
*) data
;
433 struct charseq
*out
= charmap_find_value (to_charmap
, key
, keylen
);
436 add_bytes (rettbl
, in
, out
);
444 process_block (struct convtable
*tbl
, char *addr
, size_t len
, FILE *output
)
450 struct convtable
*cur
= tbl
;
451 unsigned char *curp
= (unsigned char *) addr
;
452 unsigned int byte
= *curp
;
456 while (! is_term (cur
, byte
))
457 if (cur
->val
[byte
].sub
== NULL
)
459 /* This is a invalid sequence. Skip the first byte if we are
460 ignoring errors. Otherwise punt. */
463 error (0, 0, _("illegal input sequence at position %Zd"), n
);
467 n
-= curp
- (unsigned char *) addr
;
469 byte
= *(curp
= (unsigned char *) ++addr
);
478 cur
= cur
->val
[byte
].sub
;
483 incomplete character or shift sequence at end of buffer"));
490 /* We found a final byte. Write the output bytes. */
491 out
= cur
->val
[byte
].out
;
492 for (cnt
= 0; cnt
< out
->nbytes
; ++cnt
)
493 fputc_unlocked (out
->bytes
[cnt
], output
);
495 addr
= (char *) curp
+ 1;
504 process_fd (struct convtable
*tbl
, int fd
, FILE *output
)
506 /* We have a problem with reading from a descriptor since we must not
507 provide the iconv() function an incomplete character or shift
508 sequence at the end of the buffer. Since we have to deal with
509 arbitrary encodings we must read the whole text in a buffer and
510 process it in one step. */
511 static char *inbuf
= NULL
;
512 static size_t maxlen
= 0;
516 while (actlen
< maxlen
)
518 ssize_t n
= read (fd
, inptr
, maxlen
- actlen
);
521 /* No more text to read. */
526 /* Error while reading. */
527 error (0, errno
, _("error while reading the input"));
535 if (actlen
== maxlen
)
541 /* Increase the buffer. */
542 new_inbuf
= (char *) realloc (inbuf
, maxlen
+ 32768);
543 if (new_inbuf
== NULL
)
545 error (0, errno
, _("unable to allocate buffer for input"));
550 inptr
= inbuf
+ actlen
;
554 n
= read (fd
, inptr
, maxlen
- actlen
);
557 /* No more text to read. */
562 /* Error while reading. */
563 error (0, errno
, _("error while reading the input"));
570 while (actlen
< maxlen
);
573 /* Break again so we leave both loops. */
577 /* Now we have all the input in the buffer. Process it in one run. */
578 return process_block (tbl
, inbuf
, actlen
, output
);
583 process_file (struct convtable
*tbl
, FILE *input
, FILE *output
)
585 /* This should be safe since we use this function only for `stdin' and
586 we haven't read anything so far. */
587 return process_fd (tbl
, fileno (input
), output
);