1 /* Character set conversion with error handling.
2 Copyright (C) 2001-2007 Free Software Foundation, Inc.
3 Written by Bruno Haible and Simon Josefsson.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
22 #include "striconveh.h"
31 # include "utf8-ucs4-safe.h"
32 # include "ucs4-utf8.h"
37 #include "c-strcase.h"
40 # define SIZE_MAX ((size_t) -1)
46 /* The caller must provide CD, CD1, CD2, not just CD, because when a conversion
47 error occurs, we may have to determine the Unicode representation of the
48 inconvertible character. */
50 /* iconv_carefully is like iconv, except that it stops as soon as it encounters
51 a conversion error, and it returns in *INCREMENTED a boolean telling whether
52 it has incremented the input pointers past the error location. */
53 # if !defined _LIBICONV_VERSION && !defined __GLIBC__
54 /* Irix iconv() inserts a NUL byte if it cannot convert.
55 NetBSD iconv() inserts a question mark if it cannot convert.
56 Only GNU libiconv and GNU libc are known to prefer to fail rather
57 than doing a lossy conversion. */
59 iconv_carefully (iconv_t cd,
60 const char **inbuf, size_t *inbytesleft,
61 char **outbuf, size_t *outbytesleft,
64 const char *inptr = *inbuf;
65 const char *inptr_end = inptr + *inbytesleft;
66 char *outptr = *outbuf;
67 size_t outsize = *outbytesleft;
68 const char *inptr_before;
78 for (insize = 1; inptr + insize <= inptr_end; insize++)
81 (ICONV_CONST char **) &inptr, &insize,
83 if (!(res == (size_t)(-1) && errno == EINVAL))
85 /* We expect that no input bytes have been consumed so far. */
86 if (inptr != inptr_before)
93 *outbytesleft = outsize;
96 while (res == 0 && inptr < inptr_end);
99 *inbytesleft = inptr_end - inptr;
100 if (res != (size_t)(-1) && res > 0)
102 /* iconv() has already incremented INPTR. We cannot go back to a
103 previous INPTR, otherwise the state inside CD would become invalid,
104 if FROM_CODESET is a stateful encoding. So, tell the caller that
105 *INBUF has already been incremented. */
106 *incremented = (inptr > inptr_before);
112 *incremented = false;
117 # define iconv_carefully(cd, inbuf, inbytesleft, outbuf, outbytesleft, incremented) \
118 (*(incremented) = false, \
119 iconv (cd, (ICONV_CONST char **) (inbuf), inbytesleft, outbuf, outbytesleft))
123 mem_cd_iconveh_internal (const char *src, size_t srclen,
124 iconv_t cd, iconv_t cd1, iconv_t cd2,
125 enum iconv_ilseq_handler handler,
127 char **resultp, size_t *lengthp)
129 /* When a conversion error occurs, we cannot start using CD1 and CD2 at
130 this point: FROM_CODESET may be a stateful encoding like ISO-2022-KR.
131 Instead, we have to start afresh from the beginning of SRC. */
132 /* Use a temporary buffer, so that for small strings, a single malloc()
133 call will be sufficient. */
134 # define tmpbufsize 4096
135 /* The alignment is needed when converting e.g. to glibc's WCHAR_T or
136 libiconv's UCS-4-INTERNAL encoding. */
137 union { unsigned int align; char buf[tmpbufsize]; } tmp;
138 # define tmpbuf tmp.buf
140 char *result = tmpbuf;
141 size_t allocated = sizeof (tmpbuf);
144 /* First, try a direct conversion, and see whether a conversion error
147 const char *inptr = src;
148 size_t insize = srclen;
150 /* Avoid glibc-2.1 bug and Solaris 2.7-2.9 bug. */
151 # if defined _LIBICONV_VERSION \
152 || !((__GLIBC__ - 0 == 2 && __GLIBC_MINOR__ - 0 <= 1) || defined __sun)
153 /* Set to the initial state. */
154 iconv (cd, NULL, NULL, NULL, NULL);
159 char *outptr = result + length;
160 size_t outsize = allocated - extra_alloc - length;
165 /* Use iconv_carefully instead of iconv here, because:
166 - If TO_CODESET is UTF-8, we can do the error handling in this loop,
167 no need for a second loop,
168 - With iconv() implementations other than GNU libiconv and GNU libc,
169 if we use iconv() in a big swoop, checking for an E2BIG return,
170 we lose the number of irreversible conversions. */
171 res = iconv_carefully (cd,
176 length = outptr - result;
177 grow = (length + extra_alloc > allocated / 2);
178 if (res == (size_t)(-1))
182 else if (errno == EINVAL)
184 else if (errno == EILSEQ && handler != iconveh_error)
186 if (cd2 == (iconv_t)(-1))
188 /* TO_CODESET is UTF-8. */
189 /* Error handling can produce up to 1 byte of output. */
190 if (length + 1 + extra_alloc > allocated)
194 allocated = 2 * allocated;
195 if (length + 1 + extra_alloc > allocated)
197 if (result == tmpbuf)
198 memory = (char *) malloc (allocated);
200 memory = (char *) realloc (result, allocated);
203 if (result != tmpbuf)
208 if (result == tmpbuf)
209 memcpy (memory, tmpbuf, length);
213 /* The input is invalid in FROM_CODESET. Eat up one byte
214 and emit a question mark. */
222 result[length] = '?';
230 if (result != tmpbuf)
232 int saved_errno = errno;
245 allocated = 2 * allocated;
246 if (result == tmpbuf)
247 memory = (char *) malloc (allocated);
249 memory = (char *) realloc (result, allocated);
252 if (result != tmpbuf)
257 if (result == tmpbuf)
258 memcpy (memory, tmpbuf, length);
264 /* Now get the conversion state back to the initial state.
265 But avoid glibc-2.1 bug and Solaris 2.7 bug. */
266 #if defined _LIBICONV_VERSION \
267 || !((__GLIBC__ == 2 && __GLIBC_MINOR__ <= 1) || defined __sun)
270 char *outptr = result + length;
271 size_t outsize = allocated - extra_alloc - length;
274 res = iconv (cd, NULL, NULL, &outptr, &outsize);
275 length = outptr - result;
276 if (res == (size_t)(-1))
282 allocated = 2 * allocated;
283 if (result == tmpbuf)
284 memory = (char *) malloc (allocated);
286 memory = (char *) realloc (result, allocated);
289 if (result != tmpbuf)
294 if (result == tmpbuf)
295 memcpy (memory, tmpbuf, length);
300 if (result != tmpbuf)
302 int saved_errno = errno;
314 /* The direct conversion succeeded. */
318 /* The direct conversion failed, handler != iconveh_error,
319 and cd2 != (iconv_t)(-1).
320 Use a conversion through UTF-8. */
323 # define utf8bufsize 4096 /* may also be smaller or larger than tmpbufsize */
324 char utf8buf[utf8bufsize + 1];
326 const char *in1ptr = src;
327 size_t in1size = srclen;
328 bool do_final_flush1 = true;
329 bool do_final_flush2 = true;
331 /* Avoid glibc-2.1 bug and Solaris 2.7-2.9 bug. */
332 # if defined _LIBICONV_VERSION \
333 || !((__GLIBC__ - 0 == 2 && __GLIBC_MINOR__ - 0 <= 1) || defined __sun)
334 /* Set to the initial state. */
335 if (cd1 != (iconv_t)(-1))
336 iconv (cd1, NULL, NULL, NULL, NULL);
337 iconv (cd2, NULL, NULL, NULL, NULL);
340 while (in1size > 0 || do_final_flush1 || utf8len > 0 || do_final_flush2)
342 char *out1ptr = utf8buf + utf8len;
343 size_t out1size = utf8bufsize - utf8len;
348 /* Conversion step 1: from FROM_CODESET to UTF-8. */
351 if (cd1 != (iconv_t)(-1))
352 res1 = iconv_carefully (cd1,
353 (ICONV_CONST char **) &in1ptr, &in1size,
358 /* FROM_CODESET is UTF-8. */
366 n = u8_mbtouc_safe (&uc, (const uint8_t *) in1ptr, in1size);
369 && (uint8_t)in1ptr[0] == 0xEF
370 && (uint8_t)in1ptr[1] == 0xBF
371 && (uint8_t)in1ptr[2] == 0xBD))
384 incremented1 = false;
387 m = u8_uctomb ((uint8_t *) out1ptr, uc, out1size);
392 incremented1 = false;
410 else if (do_final_flush1)
412 /* Now get the conversion state of CD1 back to the initial state.
413 But avoid glibc-2.1 bug and Solaris 2.7 bug. */
414 # if defined _LIBICONV_VERSION \
415 || !((__GLIBC__ == 2 && __GLIBC_MINOR__ <= 1) || defined __sun)
416 if (cd1 != (iconv_t)(-1))
417 res1 = iconv (cd1, NULL, NULL, &out1ptr, &out1size);
421 do_final_flush1 = false;
429 if (res1 == (size_t)(-1)
430 && !(errno == E2BIG || errno == EINVAL || errno == EILSEQ))
432 if (result != tmpbuf)
434 int saved_errno = errno;
440 if (res1 == (size_t)(-1)
441 && errno == EILSEQ && handler != iconveh_error)
443 /* The input is invalid in FROM_CODESET. Eat up one byte and
444 emit a question mark. Room for the question mark was allocated
445 at the end of utf8buf. */
453 utf8buf[utf8len++] = '?';
456 utf8len = out1ptr - utf8buf;
459 || utf8len > utf8bufsize / 2
460 || (res1 == (size_t)(-1) && errno1 == E2BIG))
462 /* Conversion step 2: from UTF-8 to TO_CODESET. */
463 const char *in2ptr = utf8buf;
464 size_t in2size = utf8len;
467 || (in1size == 0 && !do_final_flush1 && do_final_flush2))
469 char *out2ptr = result + length;
470 size_t out2size = allocated - extra_alloc - length;
476 res2 = iconv_carefully (cd2,
480 else /* in1size == 0 && !do_final_flush1
481 && in2size == 0 && do_final_flush2 */
483 /* Now get the conversion state of CD1 back to the initial
484 state. But avoid glibc-2.1 bug and Solaris 2.7 bug. */
485 # if defined _LIBICONV_VERSION \
486 || !((__GLIBC__ == 2 && __GLIBC_MINOR__ <= 1) || defined __sun)
487 res2 = iconv (cd2, NULL, NULL, &out2ptr, &out2size);
491 do_final_flush2 = false;
495 length = out2ptr - result;
496 grow = (length + extra_alloc > allocated / 2);
497 if (res2 == (size_t)(-1))
501 else if (errno == EINVAL)
503 else if (errno == EILSEQ && handler != iconveh_error)
505 /* Error handling can produce up to 10 bytes of ASCII
506 output. But TO_CODESET may be UCS-2, UTF-16 or
507 UCS-4, so use CD2 here as well. */
517 if (u8_prev (&uc, (const uint8_t *) in2ptr,
518 (const uint8_t *) utf8buf)
527 n = u8_mbtouc (&uc, (const uint8_t *) in2ptr,
533 if (handler == iconveh_escape_sequence)
535 static char hex[16] = "0123456789ABCDEF";
537 scratchbuf[scratchlen++] = '\\';
539 scratchbuf[scratchlen++] = 'u';
542 scratchbuf[scratchlen++] = 'U';
543 scratchbuf[scratchlen++] = hex[(uc>>28) & 15];
544 scratchbuf[scratchlen++] = hex[(uc>>24) & 15];
545 scratchbuf[scratchlen++] = hex[(uc>>20) & 15];
546 scratchbuf[scratchlen++] = hex[(uc>>16) & 15];
548 scratchbuf[scratchlen++] = hex[(uc>>12) & 15];
549 scratchbuf[scratchlen++] = hex[(uc>>8) & 15];
550 scratchbuf[scratchlen++] = hex[(uc>>4) & 15];
551 scratchbuf[scratchlen++] = hex[uc & 15];
562 (ICONV_CONST char **) &inptr, &insize,
563 &out2ptr, &out2size);
564 length = out2ptr - result;
565 if (res == (size_t)(-1) && errno == E2BIG)
569 allocated = 2 * allocated;
570 if (length + 1 + extra_alloc > allocated)
572 if (result == tmpbuf)
573 memory = (char *) malloc (allocated);
575 memory = (char *) realloc (result, allocated);
578 if (result != tmpbuf)
583 if (result == tmpbuf)
584 memcpy (memory, tmpbuf, length);
588 out2ptr = result + length;
589 out2size = allocated - extra_alloc - length;
591 (ICONV_CONST char **) &inptr, &insize,
592 &out2ptr, &out2size);
593 length = out2ptr - result;
595 # if !defined _LIBICONV_VERSION && !defined __GLIBC__
596 /* Irix iconv() inserts a NUL byte if it cannot convert.
597 NetBSD iconv() inserts a question mark if it cannot
599 Only GNU libiconv and GNU libc are known to prefer
600 to fail rather than doing a lossy conversion. */
601 if (res != (size_t)(-1) && res > 0)
607 if (res == (size_t)(-1))
609 /* Failure converting the ASCII replacement. */
610 if (result != tmpbuf)
612 int saved_errno = errno;
621 if (result != tmpbuf)
623 int saved_errno = errno;
631 || (in1size == 0 && !do_final_flush1 && do_final_flush2)))
637 allocated = 2 * allocated;
638 if (result == tmpbuf)
639 memory = (char *) malloc (allocated);
641 memory = (char *) realloc (result, allocated);
644 if (result != tmpbuf)
649 if (result == tmpbuf)
650 memcpy (memory, tmpbuf, length);
655 /* Move the remaining bytes to the beginning of utf8buf. */
657 memmove (utf8buf, in2ptr, in2size);
661 if (res1 == (size_t)(-1))
663 if (errno1 == EINVAL)
665 else if (errno1 == EILSEQ)
667 if (result != tmpbuf)
678 /* Now the final memory allocation. */
681 if (result == tmpbuf)
685 memory = (char *) malloc (length + extra_alloc);
688 memcpy (memory, tmpbuf, length);
697 else if (length + extra_alloc < allocated)
699 /* Shrink the allocated memory if possible. */
702 memory = (char *) realloc (result, length + extra_alloc);
710 if (result != tmpbuf)
721 mem_cd_iconveh (const char *src, size_t srclen,
722 iconv_t cd, iconv_t cd1, iconv_t cd2,
723 enum iconv_ilseq_handler handler,
724 char **resultp, size_t *lengthp)
726 return mem_cd_iconveh_internal (src, srclen, cd, cd1, cd2, handler, 0,
731 str_cd_iconveh (const char *src,
732 iconv_t cd, iconv_t cd1, iconv_t cd2,
733 enum iconv_ilseq_handler handler)
735 /* For most encodings, a trailing NUL byte in the input will be converted
736 to a trailing NUL byte in the output. But not for UTF-7. So that this
737 function is usable for UTF-7, we have to exclude the NUL byte from the
738 conversion and add it by hand afterwards. */
741 int retval = mem_cd_iconveh_internal (src, strlen (src),
742 cd, cd1, cd2, handler, 1,
749 int saved_errno = errno;
756 /* Add the terminating NUL byte. */
757 result[length] = '\0';
765 str_iconveh (const char *src,
766 const char *from_codeset, const char *to_codeset,
767 enum iconv_ilseq_handler handler)
769 if (c_strcasecmp (from_codeset, to_codeset) == 0)
779 /* Avoid glibc-2.1 bug with EUC-KR. */
780 # if (__GLIBC__ - 0 == 2 && __GLIBC_MINOR__ - 0 <= 1) && !defined _LIBICONV_VERSION
781 if (c_strcasecmp (from_codeset, "EUC-KR") == 0
782 || c_strcasecmp (to_codeset, "EUC-KR") == 0)
789 cd = iconv_open (to_codeset, from_codeset);
790 if (cd == (iconv_t)(-1))
793 if (c_strcasecmp (from_codeset, "UTF-8") == 0)
797 cd1 = iconv_open ("UTF-8", from_codeset);
798 if (cd1 == (iconv_t)(-1))
800 int saved_errno = errno;
807 if (c_strcasecmp (to_codeset, "UTF-8") == 0)
811 cd2 = iconv_open (to_codeset, "UTF-8");
812 if (cd2 == (iconv_t)(-1))
814 int saved_errno = errno;
815 if (cd1 != (iconv_t)(-1))
823 result = str_cd_iconveh (src, cd, cd1, cd2, handler);
827 /* Close cd, cd1, cd2, but preserve the errno from str_cd_iconv. */
828 int saved_errno = errno;
829 if (cd2 != (iconv_t)(-1))
831 if (cd1 != (iconv_t)(-1))
838 if (cd2 != (iconv_t)(-1) && iconv_close (cd2) < 0)
840 /* Return NULL, but free the allocated memory, and while doing
841 that, preserve the errno from iconv_close. */
842 int saved_errno = errno;
843 if (cd1 != (iconv_t)(-1))
850 if (cd1 != (iconv_t)(-1) && iconv_close (cd1) < 0)
852 /* Return NULL, but free the allocated memory, and while doing
853 that, preserve the errno from iconv_close. */
854 int saved_errno = errno;
860 if (iconv_close (cd) < 0)
862 /* Return NULL, but free the allocated memory, and while doing
863 that, preserve the errno from iconv_close. */
864 int saved_errno = errno;
872 /* This is a different error code than if iconv_open existed but didn't
873 support from_codeset and to_codeset, so that the caller can emit
874 an error message such as
875 "iconv() is not supported. Installing GNU libiconv and
876 then reinstalling this package would fix this." */