New module 'unistr/u16-to-u8'.
[gnulib.git] / lib / iconvme.c
index 4962195..7403242 100644 (file)
@@ -1,5 +1,5 @@
 /* Recode strings between character sets, using iconv.
-   Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
+   Copyright (C) 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -15,9 +15,7 @@
    with this program; if not, write to the Free Software Foundation,
    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
 
-#ifdef HAVE_CONFIG_H
-# include <config.h>
-#endif
+#include <config.h>
 
 /* Get prototype. */
 #include "iconvme.h"
@@ -63,8 +61,57 @@ iconv_string (const char *str, const char *from_codeset,
   char *dest = NULL;
 #if HAVE_ICONV
   iconv_t cd;
-  char *outp;
+#endif
+
+  if (strcmp (to_codeset, from_codeset) == 0)
+    return strdup (str);
+
+#if HAVE_ICONV
+  cd = iconv_open (to_codeset, from_codeset);
+  if (cd == (iconv_t) -1)
+    return NULL;
+
+  dest = iconv_alloc (cd, str);
+
+  if (dest == NULL)
+    {
+      int saved_errno = errno;
+      iconv_close (cd);
+      errno = saved_errno;
+    }
+  else
+    {
+      if (iconv_close (cd) < 0)
+       {
+         int saved_errno2 = errno;
+         /* If we didn't have a real error before, make sure we restore
+            the iconv_close error below. */
+         free (dest);
+         dest = NULL;
+         errno = saved_errno2;
+       }
+    }
+#else
+  errno = ENOSYS;
+#endif
+
+  return dest;
+}
+
+/* Convert a zero-terminated string STR using iconv descriptor CD.
+   The returned string is allocated using malloc, and must be
+   dellocated by the caller using free.  On failure, NULL is returned
+   and errno holds the error reason.  Note that if the target
+   character set uses \0 for anything but to terminate the string,
+   the caller of this function may have difficulties finding
+   out the length of the output string.  */
+#if HAVE_ICONV
+char *
+iconv_alloc (iconv_t cd, const char *str)
+{
+  char *dest;
   char *p = (char *) str;
+  char *outp;
   size_t inbytes_remaining = strlen (p);
   /* Guess the maximum length the output string can have.  */
   size_t outbuf_size = inbytes_remaining + 1;
@@ -79,24 +126,25 @@ iconv_string (const char *str, const char *from_codeset,
   if (outbuf_size <= approx_sqrt_SIZE_MAX / MB_LEN_MAX)
     outbuf_size *= MB_LEN_MAX;
   outbytes_remaining = outbuf_size - 1;
-#endif
-
-  if (strcmp (to_codeset, from_codeset) == 0)
-    return strdup (str);
-
-#if HAVE_ICONV
-  cd = iconv_open (to_codeset, from_codeset);
-  if (cd == (iconv_t) -1)
-    return NULL;
 
   outp = dest = (char *) malloc (outbuf_size);
   if (dest == NULL)
-    goto out;
+    {
+      errno = ENOMEM;
+      return NULL;
+    }
+
+  /* Avoid glibc-2.1 bug and Solaris 2.7-2.9 bug.  */
+# if defined _LIBICONV_VERSION \
+    || !((__GLIBC__ - 0 == 2 && __GLIBC_MINOR__ - 0 <= 1) || defined __sun)
+  /* Set to the initial state.  */
+  iconv (cd, NULL, NULL, NULL, NULL);
+# endif
 
 again:
   err = iconv (cd, &p, &inbytes_remaining, &outp, &outbytes_remaining);
 
-  if (err == (size_t) - 1)
+  if (err == (size_t) -1)
     {
       switch (errno)
        {
@@ -119,6 +167,7 @@ again:
            newdest = (char *) realloc (dest, newsize);
            if (newdest == NULL)
              {
+               errno = ENOMEM;
                have_error = 1;
                goto out;
              }
@@ -141,31 +190,87 @@ again:
          break;
        }
     }
+# if !defined _LIBICONV_VERSION && (defined sgi || defined __sgi)
+  /* Irix iconv() inserts a NUL byte if it cannot convert.  */
+  else if (err > 0)
+    {
+      errno = EILSEQ;
+      have_error = 1;
+      goto out;
+    }
+# endif
 
-  *outp = '\0';
+again2:
+  err = iconv (cd, NULL, NULL, &outp, &outbytes_remaining);
+
+  if (err == (size_t) -1)
+    {
+      switch (errno)
+       {
+       case E2BIG:
+         {
+           size_t used = outp - dest;
+           size_t newsize = outbuf_size * 2;
+           char *newdest;
+
+           if (newsize <= outbuf_size)
+             {
+               errno = ENOMEM;
+               have_error = 1;
+               goto out;
+             }
+           newdest = (char *) realloc (dest, newsize);
+           if (newdest == NULL)
+             {
+               errno = ENOMEM;
+               have_error = 1;
+               goto out;
+             }
+           dest = newdest;
+           outbuf_size = newsize;
+
+           outp = dest + used;
+           outbytes_remaining = outbuf_size - used - 1;        /* -1 for NUL */
+
+           goto again2;
+         }
+         break;
+
+       default:
+         have_error = 1;
+         break;
+       }
+    }
+# if !defined _LIBICONV_VERSION && (defined sgi || defined __sgi)
+  /* Irix iconv() inserts a NUL byte if it cannot convert.  */
+  else if (err > 0)
+    {
+      errno = EILSEQ;
+      have_error = 1;
+      goto out;
+    }
+# endif
+
+  *outp++ = '\0';
+
+  /* Give away unused memory.  */
+  if (outp - dest < outbuf_size)
+    {
+      char *newdest = (char *) realloc (dest, outp - dest);
+
+      if (newdest != NULL)
+       dest = newdest;
+    }
 
 out:
-  {
-    int save_errno = errno;
-
-    if (iconv_close (cd) < 0 && !have_error)
-      {
-       /* If we didn't have a real error before, make sure we restore
-          the iconv_close error below. */
-       save_errno = errno;
-       have_error = 1;
-      }
-
-    if (have_error && dest)
-      {
-       free (dest);
-       dest = NULL;
-       errno = save_errno;
-      }
-  }
-#else
-  errno = ENOSYS;
-#endif
+  if (have_error)
+    {
+      int save_errno = errno;
+      free (dest);
+      errno = save_errno;
+      dest = NULL;
+    }
 
   return dest;
 }
+#endif