1 /* Recode strings between character sets, using iconv.
2 Copyright (C) 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
26 /* Get strcmp, strdup. */
39 /* Get MB_LEN_MAX, CHAR_BIT. */
44 # define SIZE_MAX ((size_t) -1)
47 /* Convert a zero-terminated string STR from the FROM_CODSET code set
48 to the TO_CODESET code set. The returned string is allocated using
49 malloc, and must be dellocated by the caller using free. On
50 failure, NULL is returned and errno holds the error reason. Note
51 that if TO_CODESET uses \0 for anything but to terminate the
52 string, the caller of this function may have difficulties finding
53 out the length of the output string. */
55 iconv_string (const char *str, const char *from_codeset,
56 const char *to_codeset)
63 if (strcmp (to_codeset, from_codeset) == 0)
67 cd = iconv_open (to_codeset, from_codeset);
68 if (cd == (iconv_t) -1)
71 dest = iconv_alloc (cd, str);
75 int saved_errno = errno;
81 if (iconv_close (cd) < 0)
83 int saved_errno2 = errno;
84 /* If we didn't have a real error before, make sure we restore
85 the iconv_close error below. */
98 /* Convert a zero-terminated string STR using iconv descriptor CD.
99 The returned string is allocated using malloc, and must be
100 dellocated by the caller using free. On failure, NULL is returned
101 and errno holds the error reason. Note that if the target
102 character set uses \0 for anything but to terminate the string,
103 the caller of this function may have difficulties finding
104 out the length of the output string. */
107 iconv_alloc (iconv_t cd, const char *str)
110 char *p = (char *) str;
112 size_t inbytes_remaining = strlen (p);
113 /* Guess the maximum length the output string can have. */
114 size_t outbuf_size = inbytes_remaining + 1;
115 size_t outbytes_remaining;
119 /* Use a worst-case output size guess, so long as that wouldn't be
120 too large for comfort. It's OK if the guess is wrong so long as
122 size_t approx_sqrt_SIZE_MAX = SIZE_MAX >> (sizeof (size_t) * CHAR_BIT / 2);
123 if (outbuf_size <= approx_sqrt_SIZE_MAX / MB_LEN_MAX)
124 outbuf_size *= MB_LEN_MAX;
125 outbytes_remaining = outbuf_size - 1;
127 outp = dest = (char *) malloc (outbuf_size);
134 /* Avoid glibc-2.1 bug and Solaris 2.7-2.9 bug. */
135 # if defined _LIBICONV_VERSION \
136 || !((__GLIBC__ - 0 == 2 && __GLIBC_MINOR__ - 0 <= 1) || defined __sun)
137 /* Set to the initial state. */
138 iconv (cd, NULL, NULL, NULL, NULL);
142 err = iconv (cd, &p, &inbytes_remaining, &outp, &outbytes_remaining);
144 if (err == (size_t) -1)
149 /* Incomplete text, do not report an error */
154 size_t used = outp - dest;
155 size_t newsize = outbuf_size * 2;
158 if (newsize <= outbuf_size)
164 newdest = (char *) realloc (dest, newsize);
172 outbuf_size = newsize;
175 outbytes_remaining = outbuf_size - used - 1; /* -1 for NUL */
190 # if !defined _LIBICONV_VERSION && (defined sgi || defined __sgi)
191 /* Irix iconv() inserts a NUL byte if it cannot convert. */
201 err = iconv (cd, NULL, NULL, &outp, &outbytes_remaining);
203 if (err == (size_t) -1)
209 size_t used = outp - dest;
210 size_t newsize = outbuf_size * 2;
213 if (newsize <= outbuf_size)
219 newdest = (char *) realloc (dest, newsize);
227 outbuf_size = newsize;
230 outbytes_remaining = outbuf_size - used - 1; /* -1 for NUL */
241 # if !defined _LIBICONV_VERSION && (defined sgi || defined __sgi)
242 /* Irix iconv() inserts a NUL byte if it cannot convert. */
253 /* Give away unused memory. */
254 if (outp - dest < outbuf_size)
256 char *newdest = (char *) realloc (dest, outp - dest);
265 int save_errno = errno;