6905b8b310ca927dcac9d95f694147cda476391d
[gnulib.git] / lib / iconvme.c
1 /* Recode strings between character sets, using iconv.
2    Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Lesser General Public License as
6    published by the Free Software Foundation; either version 2.1, or (at
7    your option) any later version.
8
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 Lesser General Public License for more details.
13
14    You should have received a copy of the GNU Lesser General Public License along
15    with this program; if not, write to the Free Software Foundation,
16    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
17
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21
22 /* Get prototype. */
23 #include "iconvme.h"
24
25 /* Get malloc. */
26 #include <stdlib.h>
27
28 /* Get strcmp. */
29 #include <string.h>
30
31 /* Get errno. */
32 #include <errno.h>
33
34 #ifdef _LIBC
35 # define HAVE_ICONV 1
36 #else
37 /* Get strdup. */
38 # include "strdup.h"
39 #endif
40
41 #if HAVE_ICONV
42 /* Get iconv etc. */
43 # include <iconv.h>
44 /* Get MB_LEN_MAX. */
45 # include <limits.h>
46 #endif
47
48 /* Convert a zero-terminated string STR from the FROM_CODSET code set
49    to the TO_CODESET code set.  The returned string is allocated using
50    malloc, and must be dellocated by the caller using free.  On
51    failure, NULL is returned and errno holds the error reason.  Note
52    that if TO_CODESET uses \0 for anything but to terminate the
53    string, the caller of this function may have difficulties finding
54    out the length of the output string.  */
55 char *
56 iconv_string (const char *str, const char *from_codeset,
57               const char *to_codeset)
58 {
59   char *dest = NULL;
60 #if HAVE_ICONV
61   iconv_t cd;
62   char *outp;
63   char *p = (char *) str;
64   size_t inbytes_remaining = strlen (p);
65   /* Guess the maximum length the output string can have.  */
66   size_t outbuf_size = (inbytes_remaining + 1) * MB_LEN_MAX;
67   size_t outbytes_remaining = outbuf_size - 1; /* -1 for NUL */
68   size_t err;
69   int have_error = 0;
70
71   if (1 < MB_LEN_MAX && SIZE_MAX / MB_LEN_MAX <= inbytes_remaining)
72     {
73       errno = ENOMEM;
74       return NULL;
75     }
76 #endif
77
78   if (strcmp (to_codeset, from_codeset) == 0)
79     return strdup (str);
80
81 #if HAVE_ICONV
82   cd = iconv_open (to_codeset, from_codeset);
83   if (cd == (iconv_t) -1)
84     return NULL;
85
86   outp = dest = (char *) malloc (outbuf_size);
87   if (dest == NULL)
88     goto out;
89
90 again:
91   err = iconv (cd, &p, &inbytes_remaining, &outp, &outbytes_remaining);
92
93   if (err == (size_t) - 1)
94     {
95       switch (errno)
96         {
97         case EINVAL:
98           /* Incomplete text, do not report an error */
99           break;
100
101         case E2BIG:
102           {
103             size_t used = outp - dest;
104             size_t newsize = outbuf_size * 2;
105             char *newdest;
106
107             if (newsize <= outbuf_size)
108               {
109                 errno = ENOMEM;
110                 have_error = 1;
111                 goto out;
112               }
113             newdest = (char *) realloc (dest, newsize);
114             if (newdest == NULL)
115               {
116                 have_error = 1;
117                 goto out;
118               }
119             dest = newdest;
120             outbuf_size = newsize;
121
122             outp = dest + used;
123             outbytes_remaining = outbuf_size - used - 1;        /* -1 for NUL */
124
125             goto again;
126           }
127           break;
128
129         case EILSEQ:
130           have_error = 1;
131           break;
132
133         default:
134           have_error = 1;
135           break;
136         }
137     }
138
139   *outp = '\0';
140
141 out:
142   {
143     int save_errno = errno;
144
145     if (iconv_close (cd) < 0 && !have_error)
146       {
147         /* If we didn't have a real error before, make sure we restore
148            the iconv_close error below. */
149         save_errno = errno;
150         have_error = 1;
151       }
152
153     if (have_error && dest)
154       {
155         free (dest);
156         dest = NULL;
157         errno = save_errno;
158       }
159   }
160 #else
161   errno = ENOSYS;
162 #endif
163
164   return dest;
165 }