* dbxread.c (process_one_symbol): Constify section_offsets parameter.
[deliverable/binutils-gdb.git] / binutils / winduni.c
index f811de7d73256bfd9897e329607b863c7d8ba979..8a96ac0edfaf9f606be9f8f4f2d6233598902d55 100644 (file)
@@ -1,6 +1,5 @@
 /* winduni.c -- unicode support for the windres program.
-   Copyright 1997, 1998, 2000, 2001, 2003, 2007, 2009
-   Free Software Foundation, Inc.
+   Copyright 1997-2013 Free Software Foundation, Inc.
    Written by Ian Lance Taylor, Cygnus Support.
    Rewritten by Kai Tietz, Onevision.
 
@@ -85,7 +84,7 @@ static local_iconv_map codepages[] =
   { 1258, "WINDOWS-1258" },
   { CP_UTF7, "UTF-7" },
   { CP_UTF8, "UTF-8" },
-  { CP_UTF16, "UTF-16" },
+  { CP_UTF16, "UTF-16LE" },
   { (rc_uint_type) -1, NULL }
 };
 
@@ -194,6 +193,94 @@ unicode_from_ascii (rc_uint_type *length, unichar **unicode, const char *ascii)
   unicode_from_codepage (length, unicode, ascii, wind_current_codepage);
 }
 
+/* Convert an ASCII string with length A_LENGTH to a unicode string.  We just
+   copy it, expanding chars to shorts, rather than doing something intelligent.
+   This routine converts also \0 within a string.  */
+
+void
+unicode_from_ascii_len (rc_uint_type *length, unichar **unicode, const char *ascii, rc_uint_type a_length)
+{
+  char *tmp, *p;
+  rc_uint_type tlen, elen, idx = 0;
+
+  *unicode = NULL;
+
+  if (!a_length)
+    {
+      if (length)
+        *length = 0;
+      return;
+    }
+
+  /* Make sure we have zero terminated string.  */
+  p = tmp = (char *) alloca (a_length + 1);
+  memcpy (tmp, ascii, a_length);
+  tmp[a_length] = 0;
+
+  while (a_length > 0)
+    {
+      unichar *utmp, *up;
+
+      tlen = strlen (p);
+
+      if (tlen > a_length)
+        tlen = a_length;
+      if (*p == 0)
+        {
+         /* Make room for one more character.  */
+         utmp = (unichar *) res_alloc (sizeof (unichar) * (idx + 1));
+         if (idx > 0)
+           {
+             memcpy (utmp, *unicode, idx * sizeof (unichar));
+           }
+         *unicode = utmp;
+         utmp[idx++] = 0;
+         --a_length;
+         p++;
+         continue;
+       }
+      utmp = NULL;
+      elen = 0;
+      elen = wind_MultiByteToWideChar (wind_current_codepage, p, NULL, 0);
+      if (elen)
+       {
+         utmp = ((unichar *) res_alloc (elen + sizeof (unichar) * 2));
+         wind_MultiByteToWideChar (wind_current_codepage, p, utmp, elen);
+         elen /= sizeof (unichar);
+         elen --;
+       }
+      else
+        {
+         /* Make room for one more character.  */
+         utmp = (unichar *) res_alloc (sizeof (unichar) * (idx + 1));
+         if (idx > 0)
+           {
+             memcpy (utmp, *unicode, idx * sizeof (unichar));
+           }
+         *unicode = utmp;
+         utmp[idx++] = ((unichar) *p) & 0xff;
+         --a_length;
+         p++;
+         continue;
+       }
+      p += tlen;
+      a_length -= tlen;
+
+      up = (unichar *) res_alloc (sizeof (unichar) * (idx + elen));
+      if (idx > 0)
+       memcpy (up, *unicode, idx * sizeof (unichar));
+
+      *unicode = up;
+      if (elen)
+       memcpy (&up[idx], utmp, sizeof (unichar) * elen);
+
+      idx += elen;
+    }
+
+  if (length)
+    *length = idx;
+}
+
 /* Convert an unicode string to an ASCII string.  We just copy it,
    shrink shorts to chars, rather than doing something intelligent.
    Shorts with not within the char range are replaced by '_'.  */
@@ -661,7 +748,15 @@ wind_MultiByteToWideChar (rc_uint_type cp, const char *mb,
   rc_uint_type ret = 0;
 
 #if defined (_WIN32) || defined (__CYGWIN__)
-  ret = (rc_uint_type) MultiByteToWideChar (cp, MB_PRECOMPOSED,
+  rc_uint_type conv_flags = MB_PRECOMPOSED;
+
+  /* MB_PRECOMPOSED is not allowed for UTF-7 or UTF-8.  
+     MultiByteToWideChar will set the last error to
+     ERROR_INVALID_FLAGS if we do. */
+  if (cp == CP_UTF8 || cp == CP_UTF7)
+    conv_flags = 0;
+
+  ret = (rc_uint_type) MultiByteToWideChar (cp, conv_flags,
                                            mb, -1, u, u_len);
   /* Convert to bytes. */
   ret *= sizeof (unichar);
@@ -674,7 +769,7 @@ wind_MultiByteToWideChar (rc_uint_type cp, const char *mb,
 
   if (!mb || !iconv_name)
     return 0;
-  iconv_t cd = iconv_open ("UTF-16", iconv_name);
+  iconv_t cd = iconv_open ("UTF-16LE", iconv_name);
 
   while (1)
     {
@@ -747,7 +842,7 @@ wind_WideCharToMultiByte (rc_uint_type cp, const unichar *u, char *mb, rc_uint_t
 
   if (!u || !iconv_name)
     return 0;
-  iconv_t cd = iconv_open (iconv_name, "UTF-16");
+  iconv_t cd = iconv_open (iconv_name, "UTF-16LE");
 
   while (1)
     {
This page took 0.024567 seconds and 4 git commands to generate.