]>
git.ipfire.org Git - thirdparty/glibc.git/blob - iconv/iconv_charmap.c
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
);
242 /* Add the IN->OUT mapping to TBL. OUT is potentially stored in the table.
243 IN is used only here, so it need not be kept live afterwards. */
245 add_bytes (struct convtable
*tbl
, const struct charseq
*in
, struct charseq
*out
)
250 assert (in
->nbytes
> 0);
252 byte
= ((unsigned char *) in
->bytes
)[n
];
253 while (n
+ 1 < in
->nbytes
)
255 if (is_term (tbl
, byte
) || tbl
->val
[byte
].sub
== NULL
)
257 /* Note that we simply ignore a definition for a byte sequence
258 which is also the prefix for a longer one. */
259 clear_term (tbl
, byte
);
261 (struct convtable
*) xcalloc (1, sizeof (struct convtable
));
264 tbl
= tbl
->val
[byte
].sub
;
266 byte
= ((unsigned char *) in
->bytes
)[++n
];
269 /* Only add the new sequence if there is none yet and the byte sequence
270 is not part of an even longer one. */
271 if (! is_term (tbl
, byte
) && tbl
->val
[byte
].sub
== NULL
)
273 set_term (tbl
, byte
);
274 tbl
->val
[byte
].out
= out
;
278 /* Try to convert SEQ from WCHAR_T format using CD.
279 Returns a malloc'd struct or NULL. */
280 static struct charseq
*
281 convert_charseq (iconv_t cd
, const struct charseq
*seq
)
283 struct charseq
*result
= NULL
;
285 if (seq
->ucs4
!= UNINITIALIZED_CHAR_VALUE
)
287 /* There is a chance. Try the iconv module. */
288 wchar_t inbuf
[1] = { seq
->ucs4
};
289 unsigned char outbuf
[64];
290 char *inptr
= (char *) inbuf
;
291 size_t inlen
= sizeof (inbuf
);
292 char *outptr
= (char *) outbuf
;
293 size_t outlen
= sizeof (outbuf
);
295 (void) iconv (cd
, &inptr
, &inlen
, &outptr
, &outlen
);
297 if (outptr
!= (char *) outbuf
)
299 /* We got some output. Good, use it. */
300 outlen
= sizeof (outbuf
) - outlen
;
301 assert ((char *) outbuf
+ outlen
== outptr
);
303 result
= xmalloc (sizeof (struct charseq
) + outlen
);
304 result
->name
= seq
->name
;
305 result
->ucs4
= seq
->ucs4
;
306 result
->nbytes
= outlen
;
307 memcpy (result
->bytes
, outbuf
, outlen
);
310 /* Clear any possible state left behind. */
311 (void) iconv (cd
, NULL
, NULL
, NULL
, NULL
);
318 static struct convtable
*
319 use_from_charmap (struct charmap_t
*from_charmap
, const char *to_code
)
321 /* We iterate over all entries in the from_charmap and for those which
322 have a known UCS4 representation we use an iconv() call to determine
323 the mapping to the to_code charset. */
324 struct convtable
*rettbl
;
331 cd
= iconv_open (to_code
, "WCHAR_T");
332 if (cd
== (iconv_t
) -1)
333 /* We cannot do anything. */
336 rettbl
= allocate_table ();
338 while (iterate_table (&from_charmap
->char_table
, &ptr
, &key
, &keylen
, &data
)
341 struct charseq
*in
= data
;
342 struct charseq
*newp
= convert_charseq (cd
, in
);
344 add_bytes (rettbl
, in
, newp
);
353 static struct convtable
*
354 use_to_charmap (const char *from_code
, struct charmap_t
*to_charmap
)
356 /* We iterate over all entries in the to_charmap and for those which
357 have a known UCS4 representation we use an iconv() call to determine
358 the mapping to the from_code charset. */
359 struct convtable
*rettbl
;
366 /* Note that the conversion we use here is the reverse direction. Without
367 exhaustive search we cannot figure out which input yields the UCS4
368 character we are looking for. Therefore we determine it the other
370 cd
= iconv_open (from_code
, "WCHAR_T");
371 if (cd
== (iconv_t
) -1)
372 /* We cannot do anything. */
375 rettbl
= allocate_table ();
377 while (iterate_table (&to_charmap
->char_table
, &ptr
, &key
, &keylen
, &data
)
380 struct charseq
*out
= data
;
381 struct charseq
*newp
= convert_charseq (cd
, out
);
384 add_bytes (rettbl
, newp
, out
);
395 static struct convtable
*
396 use_both_charmaps (struct charmap_t
*from_charmap
,
397 struct charmap_t
*to_charmap
)
399 /* In this case we iterate over all the entries in the from_charmap,
400 determine the internal name, and find an appropriate entry in the
401 to_charmap (if it exists). */
402 struct convtable
*rettbl
= allocate_table ();
408 while (iterate_table (&from_charmap
->char_table
, &ptr
, &key
, &keylen
, &data
)
411 struct charseq
*in
= (struct charseq
*) data
;
412 struct charseq
*out
= charmap_find_value (to_charmap
, key
, keylen
);
415 add_bytes (rettbl
, in
, out
);
423 process_block (struct convtable
*tbl
, char *addr
, size_t len
, FILE *output
)
429 struct convtable
*cur
= tbl
;
430 unsigned char *curp
= (unsigned char *) addr
;
431 unsigned int byte
= *curp
;
435 while (! is_term (cur
, byte
))
436 if (cur
->val
[byte
].sub
== NULL
)
438 /* This is an invalid sequence. Skip the first byte if we are
439 ignoring errors. Otherwise punt. */
442 error (0, 0, _("illegal input sequence at position %Zd"), n
);
446 n
-= curp
- (unsigned char *) addr
;
448 byte
= *(curp
= (unsigned char *) ++addr
);
457 cur
= cur
->val
[byte
].sub
;
462 incomplete character or shift sequence at end of buffer"));
469 /* We found a final byte. Write the output bytes. */
470 out
= cur
->val
[byte
].out
;
471 for (cnt
= 0; cnt
< out
->nbytes
; ++cnt
)
472 fputc_unlocked (out
->bytes
[cnt
], output
);
474 addr
= (char *) curp
+ 1;
483 process_fd (struct convtable
*tbl
, int fd
, FILE *output
)
485 /* We have a problem with reading from a descriptor since we must not
486 provide the iconv() function an incomplete character or shift
487 sequence at the end of the buffer. Since we have to deal with
488 arbitrary encodings we must read the whole text in a buffer and
489 process it in one step. */
490 static char *inbuf
= NULL
;
491 static size_t maxlen
= 0;
495 while (actlen
< maxlen
)
497 ssize_t n
= read (fd
, inptr
, maxlen
- actlen
);
500 /* No more text to read. */
505 /* Error while reading. */
506 error (0, errno
, _("error while reading the input"));
514 if (actlen
== maxlen
)
520 /* Increase the buffer. */
521 new_inbuf
= (char *) realloc (inbuf
, maxlen
+ 32768);
522 if (new_inbuf
== NULL
)
524 error (0, errno
, _("unable to allocate buffer for input"));
529 inptr
= inbuf
+ actlen
;
533 n
= read (fd
, inptr
, maxlen
- actlen
);
536 /* No more text to read. */
541 /* Error while reading. */
542 error (0, errno
, _("error while reading the input"));
549 while (actlen
< maxlen
);
552 /* Break again so we leave both loops. */
556 /* Now we have all the input in the buffer. Process it in one run. */
557 return process_block (tbl
, inbuf
, actlen
, output
);
562 process_file (struct convtable
*tbl
, FILE *input
, FILE *output
)
564 /* This should be safe since we use this function only for `stdin' and
565 we haven't read anything so far. */
566 return process_fd (tbl
, fileno (input
), output
);