summaryrefslogtreecommitdiffstats
path: root/binutils/winduni.c
diff options
context:
space:
mode:
authorNick Clifton <nickc@redhat.com>2007-06-05 13:10:30 +0000
committerNick Clifton <nickc@redhat.com>2007-06-05 13:10:30 +0000
commit5ba684e24cc4737088e295c4f3d1bcd69c1bfdf6 (patch)
treeed96f4d1cb6aecb8a29233dcf59208a426b17f6a /binutils/winduni.c
parent353ab8610a05f3a40b32c4fc1e11a44b53d3051d (diff)
downloadppe42-binutils-5ba684e24cc4737088e295c4f3d1bcd69c1bfdf6.tar.gz
ppe42-binutils-5ba684e24cc4737088e295c4f3d1bcd69c1bfdf6.zip
* Makefile.am: Add LIBICONV to windres.
* acinclude.m4: Added missing "lib-*.m4" and "lt*.m4" files. * configure.in: Add AC_CHECK_HEADER for iconv.h and use AM_ICONV. * config.in: Add ICONV defines. * aclocal: Regenerate. * confugure: Regenerate. * winduni.c: (local_iconv_map codepages, wind_language_t, languages, unicode_is$ (ascii_from_unicode): Use codepage_from_unicode. (ascii_from_unicode): Use unicode_from_codepage. Use for cygwin windows API for unicode transformation. * winduni.h: (CP_ACP, CP_UTF7, CP_UTF8, CP_OEM, CP_UTF16): New macros. (wind_language_t, local_iconv_map): New types. (wind_find_language_by_id, wind_find_language_by_codepage, (unicode_is_valid_codepage, wind_find_codepage_info, unicode_from_codepage, c$
Diffstat (limited to 'binutils/winduni.c')
-rw-r--r--binutils/winduni.c587
1 files changed, 521 insertions, 66 deletions
diff --git a/binutils/winduni.c b/binutils/winduni.c
index 9def8dff54..c4f4b1bce9 100644
--- a/binutils/winduni.c
+++ b/binutils/winduni.c
@@ -31,50 +31,157 @@
#include "sysdep.h"
#include "bfd.h"
+#include "libiberty.h" /* for xstrdup */
#include "bucomm.h"
+/* Must be include before windows.h and winnls.h. */
+#if defined (_WIN32) || defined (__CYGWIN__)
+#include <windows.h>
+#include <winnls.h>
+#endif
#include "winduni.h"
#include "safe-ctype.h"
-#ifdef _WIN32
-#include <windows.h>
+#if HAVE_ICONV_H
+#include <iconv.h>
#endif
+static rc_uint_type wind_WideCharToMultiByte (rc_uint_type, const unichar *, char *, rc_uint_type);
+static rc_uint_type wind_MultiByteToWideChar (rc_uint_type, const char *, unichar *, rc_uint_type);
+
/* Prototypes. */
static int unichar_isascii (const unichar *, rc_uint_type);
+#if !defined (_WIN32) && !defined (__CYGWIN__)
+
+/* Codepages mapped. */
+static local_iconv_map codepages[] =
+{
+ { 0, "MS-ANSI" },
+ { 1, "WINDOWS-1252" },
+ { 437, "MS-ANSI" },
+ { 737, "MS-GREEK" },
+ { 775, "WINBALTRIM" },
+ { 850, "MS-ANSI" },
+ { 852, "MS-EE" },
+ { 857, "MS-TURK" },
+ { 862, "CP862" },
+ { 864, "CP864" },
+ { 866, "MS-CYRL" },
+ { 874, "WINDOWS-874" },
+ { 932, "CP932" },
+ { 936, "CP936" },
+ { 949, "CP949" },
+ { 950, "CP950" },
+ { 1250, "WINDOWS-1250" },
+ { 1251, "WINDOWS-1251" },
+ { 1252, "WINDOWS-1252" },
+ { 1253, "WINDOWS-1253" },
+ { 1254, "WINDOWS-1254" },
+ { 1255, "WINDOWS-1255" },
+ { 1256, "WINDOWS-1256" },
+ { 1257, "WINDOWS-1257" },
+ { 1258, "WINDOWS-1258" },
+ { CP_UTF7, "UTF-7" },
+ { CP_UTF8, "UTF-8" },
+ { CP_UTF16, "UTF-16" },
+ { (rc_uint_type) -1, NULL }
+};
+
+/* Languages supported. */
+static const wind_language_t languages[] =
+{
+ { 0x0000, 437, 1252, "Neutral", "Neutral" },
+ { 0x0401, 864, 1256, "Arabic", "Saudi Arabia" }, { 0x0402, 866, 1251, "Bulgarian", "Bulgaria" },
+ { 0x0403, 850, 1252, "Catalan", "Spain" }, { 0x0404, 950, 950, "Chinese", "Taiwan" },
+ { 0x0405, 852, 1250, "Czech", "Czech Republic" }, { 0x0406, 850, 1252, "Danish", "Denmark" },
+ { 0x0407, 850, 1252, "German", "Germany" }, { 0x0408, 737, 1253, "Greek", "Greece" },
+ { 0x0409, 437, 1252, "English", "United States" }, { 0x040A, 850, 1252, "Spanish - Traditional Sort", "Spain" },
+ { 0x040B, 850, 1252, "Finnish", "Finland" }, { 0x040C, 850, 1252, "French", "France" },
+ { 0x040D, 862, 1255, "Hebrew", "Israel" }, { 0x040E, 852, 1250, "Hungarian", "Hungary" },
+ { 0x040F, 850, 1252, "Icelandic", "Iceland" }, { 0x0410, 850, 1252, "Italian", "Italy" },
+ { 0x0411, 932, 932, "Japanese", "Japan" }, { 0x0412, 949, 949, "Korean", "Korea (south)" },
+ { 0x0413, 850, 1252, "Dutch", "Netherlands" }, { 0x0414, 850, 1252, "Norwegian (Bokmål)", "Norway" },
+ { 0x0415, 852, 1250, "Polish", "Poland" }, { 0x0416, 850, 1252, "Portuguese", "Brazil" },
+ { 0x0418, 852, 1250, "Romanian", "Romania" }, { 0x0419, 866, 1251, "Russian", "Russia" },
+ { 0x041A, 852, 1250, "Croatian", "Croatia" }, { 0x041B, 852, 1250, "Slovak", "Slovakia" },
+ { 0x041C, 852, 1250, "Albanian", "Albania" }, { 0x041D, 850, 1252, "Swedish", "Sweden" },
+ { 0x041E, 874, 874, "Thai", "Thailand" }, { 0x041F, 857, 1254, "Turkish", "Turkey" },
+ { 0x0421, 850, 1252, "Indonesian", "Indonesia" }, { 0x0422, 866, 1251, "Ukrainian", "Ukraine" },
+ { 0x0423, 866, 1251, "Belarusian", "Belarus" }, { 0x0424, 852, 1250, "Slovene", "Slovenia" },
+ { 0x0425, 775, 1257, "Estonian", "Estonia" }, { 0x0426, 775, 1257, "Latvian", "Latvia" },
+ { 0x0427, 775, 1257, "Lithuanian", "Lithuania" },
+ { 0x0429, 864, 1256, "Arabic", "Farsi" }, { 0x042A,1258, 1258, "Vietnamese", "Vietnam" },
+ { 0x042D, 850, 1252, "Basque", "Spain" },
+ { 0x042F, 866, 1251, "Macedonian", "Former Yugoslav Republic of Macedonia" },
+ { 0x0436, 850, 1252, "Afrikaans", "South Africa" },
+ { 0x0438, 850, 1252, "Faroese", "Faroe Islands" },
+ { 0x043C, 437, 1252, "Irish", "Ireland" },
+ { 0x043E, 850, 1252, "Malay", "Malaysia" },
+ { 0x0801, 864, 1256, "Arabic", "Iraq" },
+ { 0x0804, 936, 936, "Chinese (People's republic of China)", "People's republic of China" },
+ { 0x0807, 850, 1252, "German", "Switzerland" },
+ { 0x0809, 850, 1252, "English", "United Kingdom" }, { 0x080A, 850, 1252, "Spanish", "Mexico" },
+ { 0x080C, 850, 1252, "French", "Belgium" },
+ { 0x0810, 850, 1252, "Italian", "Switzerland" },
+ { 0x0813, 850, 1252, "Dutch", "Belgium" }, { 0x0814, 850, 1252, "Norwegian (Nynorsk)", "Norway" },
+ { 0x0816, 850, 1252, "Portuguese", "Portugal" },
+ { 0x081A, 852, 1252, "Serbian (latin)", "Yugoslavia" },
+ { 0x081D, 850, 1252, "Swedish (Finland)", "Finland" },
+ { 0x0C01, 864, 1256, "Arabic", "Egypt" },
+ { 0x0C04, 950, 950, "Chinese", "Hong Kong" },
+ { 0x0C07, 850, 1252, "German", "Austria" },
+ { 0x0C09, 850, 1252, "English", "Australia" }, { 0x0C0A, 850, 1252, "Spanish - International Sort", "Spain" },
+ { 0x0C0C, 850, 1252, "French", "Canada"},
+ { 0x0C1A, 855, 1251, "Serbian (Cyrillic)", "Serbia" },
+ { 0x1001, 864, 1256, "Arabic", "Libya" },
+ { 0x1004, 936, 936, "Chinese", "Singapore" },
+ { 0x1007, 850, 1252, "German", "Luxembourg" },
+ { 0x1009, 850, 1252, "English", "Canada" },
+ { 0x100A, 850, 1252, "Spanish", "Guatemala" },
+ { 0x100C, 850, 1252, "French", "Switzerland" },
+ { 0x1401, 864, 1256, "Arabic", "Algeria" },
+ { 0x1407, 850, 1252, "German", "Liechtenstein" },
+ { 0x1409, 850, 1252, "English", "New Zealand" }, { 0x140A, 850, 1252, "Spanish", "Costa Rica" },
+ { 0x140C, 850, 1252, "French", "Luxembourg" },
+ { 0x1801, 864, 1256, "Arabic", "Morocco" },
+ { 0x1809, 850, 1252, "English", "Ireland" }, { 0x180A, 850, 1252, "Spanish", "Panama" },
+ { 0x180C, 850, 1252, "French", "Monaco" },
+ { 0x1C01, 864, 1256, "Arabic", "Tunisia" },
+ { 0x1C09, 437, 1252, "English", "South Africa" }, { 0x1C0A, 850, 1252, "Spanish", "Dominican Republic" },
+ { 0x2001, 864, 1256, "Arabic", "Oman" },
+ { 0x2009, 850, 1252, "English", "Jamaica" }, { 0x200A, 850, 1252, "Spanish", "Venezuela" },
+ { 0x2401, 864, 1256, "Arabic", "Yemen" },
+ { 0x2409, 850, 1252, "English", "Caribbean" }, { 0x240A, 850, 1252, "Spanish", "Colombia" },
+ { 0x2801, 864, 1256, "Arabic", "Syria" },
+ { 0x2809, 850, 1252, "English", "Belize" }, { 0x280A, 850, 1252, "Spanish", "Peru" },
+ { 0x2C01, 864, 1256, "Arabic", "Jordan" },
+ { 0x2C09, 437, 1252, "English", "Trinidad & Tobago" },{ 0x2C0A, 850, 1252, "Spanish", "Argentina" },
+ { 0x3001, 864, 1256, "Arabic", "Lebanon" },
+ { 0x3009, 437, 1252, "English", "Zimbabwe" }, { 0x300A, 850, 1252, "Spanish", "Ecuador" },
+ { 0x3401, 864, 1256, "Arabic", "Kuwait" },
+ { 0x3409, 437, 1252, "English", "Philippines" }, { 0x340A, 850, 1252, "Spanish", "Chile" },
+ { 0x3801, 864, 1256, "Arabic", "United Arab Emirates" },
+ { 0x380A, 850, 1252, "Spanish", "Uruguay" },
+ { 0x3C01, 864, 1256, "Arabic", "Bahrain" },
+ { 0x3C0A, 850, 1252, "Spanish", "Paraguay" },
+ { 0x4001, 864, 1256, "Arabic", "Qatar" },
+ { 0x400A, 850, 1252, "Spanish", "Bolivia" },
+ { 0x440A, 850, 1252, "Spanish", "El Salvador" },
+ { 0x480A, 850, 1252, "Spanish", "Honduras" },
+ { 0x4C0A, 850, 1252, "Spanish", "Nicaragua" },
+ { 0x500A, 850, 1252, "Spanish", "Puerto Rico" },
+ { (unsigned) -1, 0, 0, NULL, NULL }
+};
+
+#endif
+
/* Convert an ASCII string to a unicode string. We just copy it,
expanding chars to shorts, rather than doing something intelligent. */
void
unicode_from_ascii (rc_uint_type *length, unichar **unicode, const char *ascii)
{
- rc_uint_type len;
-#ifndef _WIN32
- const char *s;
- unsigned short *w;
-
- len = strlen (ascii);
- *unicode = ((unichar *) res_alloc ((len + 1) * sizeof (unichar)));
- for (s = ascii, w = *unicode; *s != '\0'; s++, w++)
- *w = *s & 0xff;
- *w = 0;
-#else
- /* We use MultiByteToWideChar rather than strlen to get the unicode
- string length to allow multibyte "ascii" chars. The value returned
- by this function includes the trailing '\0'. */
- len = (rc_uint_type) MultiByteToWideChar (CP_ACP, 0, ascii, -1, NULL, 0);
- if (len)
- {
- *unicode = ((unichar *) res_alloc (len * sizeof (unichar)));
- MultiByteToWideChar (CP_ACP, 0, ascii, -1, *unicode, (int) len);
- }
- /* Discount the trailing '/0'. If MultiByteToWideChar failed,
- this will set *length to -1. */
- len--;
-#endif
-
- if (length != NULL)
- *length = len;
+ unicode_from_codepage (length, unicode, ascii, 0 /*CP_ACP*/);
}
/* Convert an unicode string to an ASCII string. We just copy it,
@@ -84,43 +191,7 @@ unicode_from_ascii (rc_uint_type *length, unichar **unicode, const char *ascii)
void
ascii_from_unicode (rc_uint_type *length, const unichar *unicode, char **ascii)
{
- rc_uint_type len;
-#ifndef _WIN32
- char *s;
- const unsigned short *w;
-
- len = 0;
- while (unicode[len] != 0)
- ++len;
- *ascii = ((char *) res_alloc (len + 1));
- for (s = *ascii, w = unicode; *w != '\0'; w++, s++)
- {
- if(w[0]==(w[0]&0xff))
- *s = (char) w[0];
- else
- *s = '_';
- }
- *s = 0;
-#else
- WINBOOL used_def = FALSE;
- /* We use MultiByteToWideChar rather than strlen to get the unicode
- string length to allow multibyte "ascii" chars. The value returned
- by this function includes the trailing '\0'. */
- len = (rc_uint_type) WideCharToMultiByte (CP_ACP, WC_DEFAULTCHAR, unicode, -1, NULL,
- 0, "_", &used_def);
- if (len)
- {
- *ascii = (char *) res_alloc (len * sizeof (char));
- WideCharToMultiByte (CP_ACP, WC_DEFAULTCHAR, unicode, -1, *ascii, (int) len,
- "_", &used_def);
- }
- /* Discount the trailing '/0'. If MultiByteToWideChar failed,
- this will set *length to -1. */
- len--;
-#endif
-
- if (length != NULL)
- *length = len;
+ codepage_from_unicode (length, unicode, ascii, 0/*CP_ACP*/);
}
/* Print the unicode string UNICODE to the file E. LENGTH is the
@@ -201,6 +272,7 @@ unicode_print (FILE *e, const unichar *unicode, rc_uint_type length)
}
/* Print a unicode string to a file. */
+
void
ascii_print (FILE *e, const char *s, rc_uint_type length)
{
@@ -275,6 +347,7 @@ rc_uint_type
unichar_len (const unichar *unicode)
{
rc_uint_type r = 0;
+
if (unicode)
while (unicode[r] != 0)
r++;
@@ -320,6 +393,7 @@ static int
unichar_isascii (const unichar *u, rc_uint_type len)
{
rc_uint_type i;
+
if ((bfd_signed_vma) len < 0)
{
if (u)
@@ -343,3 +417,384 @@ unicode_print_quoted (FILE *e, const unichar *u, rc_uint_type len)
unicode_print (e, u, len);
fputc ('"', e);
}
+
+int
+unicode_is_valid_codepage (rc_uint_type cp)
+{
+ if ((cp & 0xffff) != cp)
+ return 0;
+ if (cp == CP_UTF16 || cp == CP_ACP)
+ return 1;
+
+#if !defined (_WIN32) && !defined (__CYGWIN__)
+ if (! wind_find_codepage_info (cp))
+ return 0;
+ return 1;
+#else
+ return !! IsValidCodePage ((UINT) cp);
+#endif
+}
+
+#if defined (_WIN32) || defined (__CYGWIN__)
+
+#define max_cp_string_len 6
+
+static unsigned int
+codepage_from_langid (unsigned short langid)
+{
+ char cp_string [max_cp_string_len];
+ int c;
+
+ memset (cp_string, 0, max_cp_string_len);
+ /* LOCALE_RETURN_NUMBER flag would avoid strtoul conversion,
+ but is unavailable on Win95. */
+ c = GetLocaleInfoA (MAKELCID (langid, SORT_DEFAULT),
+ LOCALE_IDEFAULTANSICODEPAGE,
+ cp_string, max_cp_string_len);
+ /* If codepage data for an LCID is not installed on users's system,
+ GetLocaleInfo returns an empty string. Fall back to system ANSI
+ default. */
+ if (c == 0)
+ return CP_ACP;
+ return strtoul (cp_string, 0, 10);
+}
+
+static unsigned int
+wincodepage_from_langid (unsigned short langid)
+{
+ char cp_string [max_cp_string_len];
+ int c;
+
+ memset (cp_string, 0, max_cp_string_len);
+ /* LOCALE_RETURN_NUMBER flag would avoid strtoul conversion,
+ but is unavailable on Win95. */
+ c = GetLocaleInfoA (MAKELCID (langid, SORT_DEFAULT),
+ LOCALE_IDEFAULTCODEPAGE,
+ cp_string, max_cp_string_len);
+ /* If codepage data for an LCID is not installed on users's system,
+ GetLocaleInfo returns an empty string. Fall back to system ANSI
+ default. */
+ if (c == 0)
+ return CP_OEM;
+ return strtoul (cp_string, 0, 10);
+}
+
+static char *
+lang_from_langid (unsigned short langid)
+{
+ char cp_string[261];
+ int c;
+
+ memset (cp_string, 0, 261);
+ c = GetLocaleInfoA (MAKELCID (langid, SORT_DEFAULT),
+ LOCALE_SENGLANGUAGE,
+ cp_string, 260);
+ /* If codepage data for an LCID is not installed on users's system,
+ GetLocaleInfo returns an empty string. Fall back to system ANSI
+ default. */
+ if (c == 0)
+ strcpy (cp_string, "Neutral");
+ return xstrdup (cp_string);
+}
+
+static char *
+country_from_langid (unsigned short langid)
+{
+ char cp_string[261];
+ int c;
+
+ memset (cp_string, 0, 261);
+ c = GetLocaleInfoA (MAKELCID (langid, SORT_DEFAULT),
+ LOCALE_SENGCOUNTRY,
+ cp_string, 260);
+ /* If codepage data for an LCID is not installed on users's system,
+ GetLocaleInfo returns an empty string. Fall back to system ANSI
+ default. */
+ if (c == 0)
+ strcpy (cp_string, "Neutral");
+ return xstrdup (cp_string);
+}
+
+#endif
+
+const wind_language_t *
+wind_find_language_by_id (unsigned id)
+{
+#if !defined (_WIN32) && !defined (__CYGWIN__)
+ int i;
+
+ if (! id)
+ return NULL;
+ for (i = 0; languages[i].id != (unsigned) -1 && languages[i].id != id; i++)
+ ;
+ if (languages[i].id == id)
+ return &languages[i];
+ return NULL;
+#else
+ static wind_language_t wl;
+
+ wl.id = id;
+ wl.doscp = codepage_from_langid ((unsigned short) id);
+ wl.wincp = wincodepage_from_langid ((unsigned short) id);
+ wl.name = lang_from_langid ((unsigned short) id);
+ wl.country = country_from_langid ((unsigned short) id);
+
+ return & wl;
+#endif
+}
+
+const local_iconv_map *
+wind_find_codepage_info (unsigned cp)
+{
+#if !defined (_WIN32) && !defined (__CYGWIN__)
+ int i;
+
+ for (i = 0; codepages[i].codepage != (rc_uint_type) -1 && codepages[i].codepage != cp; i++)
+ ;
+ if (codepages[i].codepage == (rc_uint_type) -1)
+ return NULL;
+ return &codepages[i];
+#else
+ static local_iconv_map lim;
+ if (!unicode_is_valid_codepage (cp))
+ return NULL;
+ lim.codepage = cp;
+ lim.iconv_name = "";
+ return & lim;
+#endif
+}
+
+/* Convert an Codepage string to a unicode string. */
+
+void
+unicode_from_codepage (rc_uint_type *length, unichar **u, const char *src, rc_uint_type cp)
+{
+ rc_uint_type len;
+
+ len = wind_MultiByteToWideChar (cp, src, NULL, 0);
+ if (len)
+ {
+ *u = ((unichar *) res_alloc (len));
+ wind_MultiByteToWideChar (cp, src, *u, len);
+ }
+ /* Discount the trailing '/0'. If MultiByteToWideChar failed,
+ this will set *length to -1. */
+ len -= sizeof (unichar);
+
+ if (length != NULL)
+ *length = len / sizeof (unichar);
+}
+
+/* Convert an unicode string to an codepage string. */
+
+void
+codepage_from_unicode (rc_uint_type *length, const unichar *unicode, char **ascii, rc_uint_type cp)
+{
+ rc_uint_type len;
+
+ len = wind_WideCharToMultiByte (cp, unicode, NULL, 0);
+ if (len)
+ {
+ *ascii = (char *) res_alloc (len * sizeof (char));
+ wind_WideCharToMultiByte (cp, unicode, *ascii, len);
+ }
+ /* Discount the trailing '/0'. If MultiByteToWideChar failed,
+ this will set *length to -1. */
+ len--;
+
+ if (length != NULL)
+ *length = len;
+}
+
+#ifdef HAVE_ICONV_H
+static int
+iconv_onechar (iconv_t cd, const char *s, char *d, int d_len, const char **n_s, char **n_d)
+{
+ int i;
+
+ for (i = 1; i <= 32; i++)
+ {
+ char *tmp_d = d;
+ const char *tmp_s = s;
+ size_t ret;
+ size_t s_left = (size_t) i;
+ size_t d_left = (size_t) d_len;
+
+ ret = iconv (cd, & tmp_s, & s_left, & tmp_d, & d_left);
+
+ if (ret != (size_t) -1)
+ {
+ *n_s = tmp_s;
+ *n_d = tmp_d;
+ return 0;
+ }
+ }
+
+ return 1;
+}
+
+static const char *
+wind_iconv_cp (rc_uint_type cp)
+{
+ const local_iconv_map *lim = wind_find_codepage_info (cp);
+
+ if (!lim)
+ return NULL;
+ return lim->iconv_name;
+}
+#endif /* HAVE_ICONV_H */
+
+static rc_uint_type
+wind_MultiByteToWideChar (rc_uint_type cp, const char *mb,
+ unichar *u, rc_uint_type u_len)
+{
+ rc_uint_type ret = 0;
+
+#if defined (_WIN32) || defined (__CYGWIN__)
+ ret = (rc_uint_type) MultiByteToWideChar (cp, MB_PRECOMPOSED,
+ mb, -1, u, u_len);
+ /* Convert to bytes. */
+ ret *= sizeof (unichar);
+
+#elif defined (HAVE_ICONV_H)
+ int first = 1;
+ char tmp[32];
+ char *p_tmp;
+ const char *iconv_name = wind_iconv_cp (cp);
+
+ if (!mb || !iconv_name)
+ return 0;
+ iconv_t cd = iconv_open ("UTF-16", iconv_name);
+
+ while (1)
+ {
+ int iret;
+ const char *n_mb;
+ char *n_tmp;
+
+ p_tmp = tmp;
+ iret = iconv_onechar (cd, (const char *) mb, p_tmp, 32, & n_mb, & n_tmp);
+ if (first)
+ {
+ first = 0;
+ continue;
+ }
+ if (!iret)
+ {
+ size_t l_tmp = (size_t) (n_tmp - p_tmp);
+
+ if (u)
+ {
+ if ((size_t) u_len < l_tmp)
+ break;
+ memcpy (u, tmp, l_tmp);
+ u += l_tmp/2;
+ u_len -= l_tmp;
+ }
+ ret += l_tmp;
+ }
+ else
+ break;
+ if (tmp[0] == 0 && tmp[1] == 0)
+ break;
+ mb = n_mb;
+ }
+ iconv_close (cd);
+#else
+ if (cp)
+ ret = 0;
+ ret = strlen (mb) + 1;
+ ret *= sizeof (unichar);
+ if (u != NULL && u_len != 0)
+ {
+ do
+ {
+ *u++ = ((unichar) *mb) & 0xff;
+ --u_len; mb++;
+ }
+ while (u_len != 0 && mb[-1] != 0);
+ }
+ if (u != NULL && u_len != 0)
+ *u = 0;
+#endif
+ return ret;
+}
+
+static rc_uint_type
+wind_WideCharToMultiByte (rc_uint_type cp, const unichar *u, char *mb, rc_uint_type mb_len)
+{
+ rc_uint_type ret = 0;
+#if defined (_WIN32) || defined (__CYGWIN__)
+ WINBOOL used_def = FALSE;
+
+ ret = (rc_uint_type) WideCharToMultiByte (cp, 0, u, -1, mb, mb_len,
+ NULL, & used_def);
+#elif defined (HAVE_ICONV_H)
+ int first = 1;
+ char tmp[32];
+ char *p_tmp;
+ const char *iconv_name = wind_iconv_cp (cp);
+
+ if (!u || !iconv_name)
+ return 0;
+ iconv_t cd = iconv_open (iconv_name, "UTF-16");
+
+ while (1)
+ {
+ int iret;
+ const char *n_u;
+ char *n_tmp;
+
+ p_tmp = tmp;
+ iret = iconv_onechar (cd, (const char *) u, p_tmp, 32, &n_u, & n_tmp);
+ if (first)
+ {
+ first = 0;
+ continue;
+ }
+ if (!iret)
+ {
+ size_t l_tmp = (size_t) (n_tmp - p_tmp);
+
+ if (mb)
+ {
+ if ((size_t) mb_len < l_tmp)
+ break;
+ memcpy (mb, tmp, l_tmp);
+ mb += l_tmp;
+ mb_len -= l_tmp;
+ }
+ ret += l_tmp;
+ }
+ else
+ break;
+ if (u[0] == 0)
+ break;
+ u = (const unichar *) n_u;
+ }
+ iconv_close (cd);
+#else
+ if (cp)
+ ret = 0;
+
+ while (u[ret] != 0)
+ ++ret;
+
+ ++ret;
+
+ if (mb)
+ {
+ while (*u != 0 && mb_len != 0)
+ {
+ if (u[0] == (u[0] & 0x7f))
+ *mb++ = (char) u[0];
+ else
+ *mb++ = '_';
+ ++u; --mb_len;
+ }
+ if (mb_len != 0)
+ *mb = 0;
+ }
+#endif
+ return ret;
+}
OpenPOWER on IntegriCloud