00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifdef HAVE_CONFIG_H
00019 # include <config.h>
00020 #endif
00021
00022
00023 #include "iconvme.h"
00024
00025
00026 #include <stdlib.h>
00027
00028
00029 #include <string.h>
00030
00031
00032 #include <errno.h>
00033
00034 #ifdef _LIBC
00035 # define HAVE_ICONV 1
00036 #else
00037
00038 # include "strdup.h"
00039 #endif
00040
00041 #if HAVE_ICONV
00042
00043 # include <iconv.h>
00044
00045 # include <limits.h>
00046 #endif
00047
00048 #ifndef SIZE_MAX
00049 # define SIZE_MAX ((size_t) -1)
00050 #endif
00051
00052
00053
00054
00055
00056
00057
00058
00059 char *
00060 iconv_string (const char *str, const char *from_codeset,
00061 const char *to_codeset)
00062 {
00063 char *dest = NULL;
00064 #if HAVE_ICONV
00065 iconv_t cd;
00066 char *outp;
00067 char *p = (char *) str;
00068 size_t inbytes_remaining = strlen (p);
00069
00070 size_t outbuf_size = inbytes_remaining + 1;
00071 size_t outbytes_remaining;
00072 size_t err;
00073 int have_error = 0;
00074
00075
00076
00077
00078 size_t approx_sqrt_SIZE_MAX = SIZE_MAX >> (sizeof (size_t) * CHAR_BIT / 2);
00079 if (outbuf_size <= approx_sqrt_SIZE_MAX / MB_LEN_MAX)
00080 outbuf_size *= MB_LEN_MAX;
00081 outbytes_remaining = outbuf_size - 1;
00082 #endif
00083
00084 if (strcmp (to_codeset, from_codeset) == 0)
00085 return strdup (str);
00086
00087 #if HAVE_ICONV
00088 cd = iconv_open (to_codeset, from_codeset);
00089 if (cd == (iconv_t) -1)
00090 return NULL;
00091
00092 outp = dest = (char *) malloc (outbuf_size);
00093 if (dest == NULL)
00094 goto out;
00095
00096 again:
00097 err = iconv (cd, &p, &inbytes_remaining, &outp, &outbytes_remaining);
00098
00099 if (err == (size_t) - 1)
00100 {
00101 switch (errno)
00102 {
00103 case EINVAL:
00104
00105 break;
00106
00107 case E2BIG:
00108 {
00109 size_t used = outp - dest;
00110 size_t newsize = outbuf_size * 2;
00111 char *newdest;
00112
00113 if (newsize <= outbuf_size)
00114 {
00115 errno = ENOMEM;
00116 have_error = 1;
00117 goto out;
00118 }
00119 newdest = (char *) realloc (dest, newsize);
00120 if (newdest == NULL)
00121 {
00122 have_error = 1;
00123 goto out;
00124 }
00125 dest = newdest;
00126 outbuf_size = newsize;
00127
00128 outp = dest + used;
00129 outbytes_remaining = outbuf_size - used - 1;
00130
00131 goto again;
00132 }
00133 break;
00134
00135 case EILSEQ:
00136 have_error = 1;
00137 break;
00138
00139 default:
00140 have_error = 1;
00141 break;
00142 }
00143 }
00144
00145 *outp = '\0';
00146
00147 out:
00148 {
00149 int save_errno = errno;
00150
00151 if (iconv_close (cd) < 0 && !have_error)
00152 {
00153
00154
00155 save_errno = errno;
00156 have_error = 1;
00157 }
00158
00159 if (have_error && dest)
00160 {
00161 free (dest);
00162 dest = NULL;
00163 errno = save_errno;
00164 }
00165 }
00166 #else
00167 errno = ENOSYS;
00168 #endif
00169
00170 return dest;
00171 }