* cycle-check.h (CYCLE_CHECK_REFLECT_CHDIR_UP): Abort if this
[gnulib.git] / lib / iconvme.c
index 68d7f1a..943380c 100644 (file)
@@ -13,7 +13,7 @@
 
    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation,
-   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
+   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
 #if HAVE_ICONV
 /* Get iconv etc. */
 # include <iconv.h>
-/* Get MB_LEN_MAX. */
+/* Get MB_LEN_MAX, CHAR_BIT.  */
 # include <limits.h>
 #endif
 
+#ifndef SIZE_MAX
+# define SIZE_MAX ((size_t) -1)
+#endif
+
 /* Convert a zero-terminated string STR from the FROM_CODSET code set
    to the TO_CODESET code set.  The returned string is allocated using
    malloc, and must be dellocated by the caller using free.  On
@@ -59,14 +63,6 @@ iconv_string (const char *str, const char *from_codeset,
   char *dest = NULL;
 #if HAVE_ICONV
   iconv_t cd;
-  char *outp;
-  char *p = (char *) str;
-  size_t inbytes_remaining = strlen (p);
-  /* Guess the maximum length the output string can have.  */
-  size_t outbuf_size = (inbytes_remaining + 1) * MB_LEN_MAX;
-  size_t outbytes_remaining = outbuf_size - 1; /* -1 for NUL */
-  size_t err;
-  int have_error = 0;
 #endif
 
   if (strcmp (to_codeset, from_codeset) == 0)
@@ -77,14 +73,67 @@ iconv_string (const char *str, const char *from_codeset,
   if (cd == (iconv_t) -1)
     return NULL;
 
+  dest = iconv_alloc (cd, str);
+
+  {
+    int save_errno = errno;
+
+    if (iconv_close (cd) < 0 && dest)
+      {
+       int save_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 = save_errno2;
+      }
+    else
+      errno = save_errno;
+  }
+#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;
+  size_t outbytes_remaining;
+  size_t err;
+  int have_error = 0;
+
+  /* Use a worst-case output size guess, so long as that wouldn't be
+     too large for comfort.  It's OK if the guess is wrong so long as
+     it's nonzero.  */
+  size_t approx_sqrt_SIZE_MAX = SIZE_MAX >> (sizeof (size_t) * CHAR_BIT / 2);
+  if (outbuf_size <= approx_sqrt_SIZE_MAX / MB_LEN_MAX)
+    outbuf_size *= MB_LEN_MAX;
+  outbytes_remaining = outbuf_size - 1;
+
   outp = dest = (char *) malloc (outbuf_size);
   if (dest == NULL)
-    goto out;
+    return NULL;
 
 again:
   err = iconv (cd, &p, &inbytes_remaining, &outp, &outbytes_remaining);
 
-  if (err == (size_t) - 1)
+  if (err == (size_t) -1)
     {
       switch (errno)
        {
@@ -133,27 +182,14 @@ again:
   *outp = '\0';
 
 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 && dest)
+    {
+      int save_errno = errno;
+      free (dest);
+      errno = save_errno;
+      dest = NULL;
+    }
 
   return dest;
 }
+#endif