OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-old/] [binutils-2.18.50/] [binutils/] [winduni.c] - Diff between revs 156 and 816

Go to most recent revision | Only display areas with differences | Details | Blame | View Log

Rev 156 Rev 816
/* winduni.c -- unicode support for the windres program.
/* winduni.c -- unicode support for the windres program.
   Copyright 1997, 1998, 2000, 2001, 2003, 2007
   Copyright 1997, 1998, 2000, 2001, 2003, 2007
   Free Software Foundation, Inc.
   Free Software Foundation, Inc.
   Written by Ian Lance Taylor, Cygnus Support.
   Written by Ian Lance Taylor, Cygnus Support.
   Rewritten by Kai Tietz, Onevision.
   Rewritten by Kai Tietz, Onevision.
 
 
   This file is part of GNU Binutils.
   This file is part of GNU Binutils.
 
 
   This program is free software; you can redistribute it and/or modify
   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
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.
   (at your option) any later version.
 
 
   This program is distributed in the hope that it will be useful,
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   GNU General Public License for more details.
 
 
   You should have received a copy of the GNU General Public License
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
   02110-1301, USA.  */
   02110-1301, USA.  */
 
 
 
 
/* This file contains unicode support routines for the windres
/* This file contains unicode support routines for the windres
   program.  Ideally, we would have generic unicode support which
   program.  Ideally, we would have generic unicode support which
   would work on all systems.  However, we don't.  Instead, on a
   would work on all systems.  However, we don't.  Instead, on a
   Windows host, we are prepared to call some Windows routines.  This
   Windows host, we are prepared to call some Windows routines.  This
   means that we will generate different output on Windows and Unix
   means that we will generate different output on Windows and Unix
   hosts, but that seems better than not really supporting unicode at
   hosts, but that seems better than not really supporting unicode at
   all.  */
   all.  */
 
 
#include "sysdep.h"
#include "sysdep.h"
#include "bfd.h"
#include "bfd.h"
#include "libiberty.h" /* for xstrdup */
#include "libiberty.h" /* for xstrdup */
#include "bucomm.h"
#include "bucomm.h"
/* Must be include before windows.h and winnls.h.  */
/* Must be include before windows.h and winnls.h.  */
#if defined (_WIN32) || defined (__CYGWIN__)
#if defined (_WIN32) || defined (__CYGWIN__)
#include <windows.h>
#include <windows.h>
#include <winnls.h>
#include <winnls.h>
#endif
#endif
#include "winduni.h"
#include "winduni.h"
#include "safe-ctype.h"
#include "safe-ctype.h"
 
 
#if HAVE_ICONV_H
#if HAVE_ICONV_H
#include <iconv.h>
#include <iconv.h>
#endif
#endif
 
 
static rc_uint_type wind_WideCharToMultiByte (rc_uint_type, const unichar *, char *, rc_uint_type);
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);
static rc_uint_type wind_MultiByteToWideChar (rc_uint_type, const char *, unichar *, rc_uint_type);
static int unichar_isascii (const unichar *, rc_uint_type);
static int unichar_isascii (const unichar *, rc_uint_type);
 
 
/* Convert an ASCII string to a unicode string.  We just copy it,
/* Convert an ASCII string to a unicode string.  We just copy it,
   expanding chars to shorts, rather than doing something intelligent.  */
   expanding chars to shorts, rather than doing something intelligent.  */
 
 
#if !defined (_WIN32) && !defined (__CYGWIN__)
#if !defined (_WIN32) && !defined (__CYGWIN__)
 
 
/* Codepages mapped.  */
/* Codepages mapped.  */
static local_iconv_map codepages[] =
static local_iconv_map codepages[] =
{
{
  { 0, "MS-ANSI" },
  { 0, "MS-ANSI" },
  { 1, "WINDOWS-1252" },
  { 1, "WINDOWS-1252" },
  { 437, "MS-ANSI" },
  { 437, "MS-ANSI" },
  { 737, "MS-GREEK" },
  { 737, "MS-GREEK" },
  { 775, "WINBALTRIM" },
  { 775, "WINBALTRIM" },
  { 850, "MS-ANSI" },
  { 850, "MS-ANSI" },
  { 852, "MS-EE" },
  { 852, "MS-EE" },
  { 857, "MS-TURK" },
  { 857, "MS-TURK" },
  { 862, "CP862" },
  { 862, "CP862" },
  { 864, "CP864" },
  { 864, "CP864" },
  { 866, "MS-CYRL" },
  { 866, "MS-CYRL" },
  { 874, "WINDOWS-874" },
  { 874, "WINDOWS-874" },
  { 932, "CP932" },
  { 932, "CP932" },
  { 936, "CP936" },
  { 936, "CP936" },
  { 949, "CP949" },
  { 949, "CP949" },
  { 950, "CP950" },
  { 950, "CP950" },
  { 1250, "WINDOWS-1250" },
  { 1250, "WINDOWS-1250" },
  { 1251, "WINDOWS-1251" },
  { 1251, "WINDOWS-1251" },
  { 1252, "WINDOWS-1252" },
  { 1252, "WINDOWS-1252" },
  { 1253, "WINDOWS-1253" },
  { 1253, "WINDOWS-1253" },
  { 1254, "WINDOWS-1254" },
  { 1254, "WINDOWS-1254" },
  { 1255, "WINDOWS-1255" },
  { 1255, "WINDOWS-1255" },
  { 1256, "WINDOWS-1256" },
  { 1256, "WINDOWS-1256" },
  { 1257, "WINDOWS-1257" },
  { 1257, "WINDOWS-1257" },
  { 1258, "WINDOWS-1258" },
  { 1258, "WINDOWS-1258" },
  { CP_UTF7, "UTF-7" },
  { CP_UTF7, "UTF-7" },
  { CP_UTF8, "UTF-8" },
  { CP_UTF8, "UTF-8" },
  { CP_UTF16, "UTF-16" },
  { CP_UTF16, "UTF-16" },
  { (rc_uint_type) -1, NULL }
  { (rc_uint_type) -1, NULL }
};
};
 
 
/* Languages supported.  */
/* Languages supported.  */
static const wind_language_t languages[] =
static const wind_language_t languages[] =
{
{
  { 0x0000, 437, 1252, "Neutral", "Neutral" },
  { 0x0000, 437, 1252, "Neutral", "Neutral" },
  { 0x0401, 864, 1256, "Arabic", "Saudi Arabia" },    { 0x0402, 866, 1251, "Bulgarian", "Bulgaria" },
  { 0x0401, 864, 1256, "Arabic", "Saudi Arabia" },    { 0x0402, 866, 1251, "Bulgarian", "Bulgaria" },
  { 0x0403, 850, 1252, "Catalan", "Spain" },          { 0x0404, 950,  950, "Chinese", "Taiwan" },
  { 0x0403, 850, 1252, "Catalan", "Spain" },          { 0x0404, 950,  950, "Chinese", "Taiwan" },
  { 0x0405, 852, 1250, "Czech", "Czech Republic" },   { 0x0406, 850, 1252, "Danish", "Denmark" },
  { 0x0405, 852, 1250, "Czech", "Czech Republic" },   { 0x0406, 850, 1252, "Danish", "Denmark" },
  { 0x0407, 850, 1252, "German", "Germany" },         { 0x0408, 737, 1253, "Greek", "Greece" },
  { 0x0407, 850, 1252, "German", "Germany" },         { 0x0408, 737, 1253, "Greek", "Greece" },
  { 0x0409, 437, 1252, "English", "United States" },  { 0x040A, 850, 1252, "Spanish - Traditional Sort", "Spain" },
  { 0x0409, 437, 1252, "English", "United States" },  { 0x040A, 850, 1252, "Spanish - Traditional Sort", "Spain" },
  { 0x040B, 850, 1252, "Finnish", "Finland" },        { 0x040C, 850, 1252, "French", "France" },
  { 0x040B, 850, 1252, "Finnish", "Finland" },        { 0x040C, 850, 1252, "French", "France" },
  { 0x040D, 862, 1255, "Hebrew", "Israel" },          { 0x040E, 852, 1250, "Hungarian", "Hungary" },
  { 0x040D, 862, 1255, "Hebrew", "Israel" },          { 0x040E, 852, 1250, "Hungarian", "Hungary" },
  { 0x040F, 850, 1252, "Icelandic", "Iceland" },      { 0x0410, 850, 1252, "Italian", "Italy" },
  { 0x040F, 850, 1252, "Icelandic", "Iceland" },      { 0x0410, 850, 1252, "Italian", "Italy" },
  { 0x0411, 932,  932, "Japanese", "Japan" },         { 0x0412, 949,  949, "Korean", "Korea (south)" },
  { 0x0411, 932,  932, "Japanese", "Japan" },         { 0x0412, 949,  949, "Korean", "Korea (south)" },
  { 0x0413, 850, 1252, "Dutch", "Netherlands" },      { 0x0414, 850, 1252, "Norwegian (Bokmål)", "Norway" },
  { 0x0413, 850, 1252, "Dutch", "Netherlands" },      { 0x0414, 850, 1252, "Norwegian (Bokmål)", "Norway" },
  { 0x0415, 852, 1250, "Polish", "Poland" },          { 0x0416, 850, 1252, "Portuguese", "Brazil" },
  { 0x0415, 852, 1250, "Polish", "Poland" },          { 0x0416, 850, 1252, "Portuguese", "Brazil" },
  { 0x0418, 852, 1250, "Romanian", "Romania" },       { 0x0419, 866, 1251, "Russian", "Russia" },
  { 0x0418, 852, 1250, "Romanian", "Romania" },       { 0x0419, 866, 1251, "Russian", "Russia" },
  { 0x041A, 852, 1250, "Croatian", "Croatia" },       { 0x041B, 852, 1250, "Slovak", "Slovakia" },
  { 0x041A, 852, 1250, "Croatian", "Croatia" },       { 0x041B, 852, 1250, "Slovak", "Slovakia" },
  { 0x041C, 852, 1250, "Albanian", "Albania" },       { 0x041D, 850, 1252, "Swedish", "Sweden" },
  { 0x041C, 852, 1250, "Albanian", "Albania" },       { 0x041D, 850, 1252, "Swedish", "Sweden" },
  { 0x041E, 874,  874, "Thai", "Thailand" },          { 0x041F, 857, 1254, "Turkish", "Turkey" },
  { 0x041E, 874,  874, "Thai", "Thailand" },          { 0x041F, 857, 1254, "Turkish", "Turkey" },
  { 0x0421, 850, 1252, "Indonesian", "Indonesia" },   { 0x0422, 866, 1251, "Ukrainian", "Ukraine" },
  { 0x0421, 850, 1252, "Indonesian", "Indonesia" },   { 0x0422, 866, 1251, "Ukrainian", "Ukraine" },
  { 0x0423, 866, 1251, "Belarusian", "Belarus" },     { 0x0424, 852, 1250, "Slovene", "Slovenia" },
  { 0x0423, 866, 1251, "Belarusian", "Belarus" },     { 0x0424, 852, 1250, "Slovene", "Slovenia" },
  { 0x0425, 775, 1257, "Estonian", "Estonia" },       { 0x0426, 775, 1257, "Latvian", "Latvia" },
  { 0x0425, 775, 1257, "Estonian", "Estonia" },       { 0x0426, 775, 1257, "Latvian", "Latvia" },
  { 0x0427, 775, 1257, "Lithuanian", "Lithuania" },
  { 0x0427, 775, 1257, "Lithuanian", "Lithuania" },
  { 0x0429, 864, 1256, "Arabic", "Farsi" },           { 0x042A,1258, 1258, "Vietnamese", "Vietnam" },
  { 0x0429, 864, 1256, "Arabic", "Farsi" },           { 0x042A,1258, 1258, "Vietnamese", "Vietnam" },
  { 0x042D, 850, 1252, "Basque", "Spain" },
  { 0x042D, 850, 1252, "Basque", "Spain" },
  { 0x042F, 866, 1251, "Macedonian", "Former Yugoslav Republic of Macedonia" },
  { 0x042F, 866, 1251, "Macedonian", "Former Yugoslav Republic of Macedonia" },
  { 0x0436, 850, 1252, "Afrikaans", "South Africa" },
  { 0x0436, 850, 1252, "Afrikaans", "South Africa" },
  { 0x0438, 850, 1252, "Faroese", "Faroe Islands" },
  { 0x0438, 850, 1252, "Faroese", "Faroe Islands" },
  { 0x043C, 437, 1252, "Irish", "Ireland" },
  { 0x043C, 437, 1252, "Irish", "Ireland" },
  { 0x043E, 850, 1252, "Malay", "Malaysia" },
  { 0x043E, 850, 1252, "Malay", "Malaysia" },
  { 0x0801, 864, 1256, "Arabic", "Iraq" },
  { 0x0801, 864, 1256, "Arabic", "Iraq" },
  { 0x0804, 936,  936, "Chinese (People's republic of China)", "People's republic of China" },
  { 0x0804, 936,  936, "Chinese (People's republic of China)", "People's republic of China" },
  { 0x0807, 850, 1252, "German", "Switzerland" },
  { 0x0807, 850, 1252, "German", "Switzerland" },
  { 0x0809, 850, 1252, "English", "United Kingdom" }, { 0x080A, 850, 1252, "Spanish", "Mexico" },
  { 0x0809, 850, 1252, "English", "United Kingdom" }, { 0x080A, 850, 1252, "Spanish", "Mexico" },
  { 0x080C, 850, 1252, "French", "Belgium" },
  { 0x080C, 850, 1252, "French", "Belgium" },
  { 0x0810, 850, 1252, "Italian", "Switzerland" },
  { 0x0810, 850, 1252, "Italian", "Switzerland" },
  { 0x0813, 850, 1252, "Dutch", "Belgium" },          { 0x0814, 850, 1252, "Norwegian (Nynorsk)", "Norway" },
  { 0x0813, 850, 1252, "Dutch", "Belgium" },          { 0x0814, 850, 1252, "Norwegian (Nynorsk)", "Norway" },
  { 0x0816, 850, 1252, "Portuguese", "Portugal" },
  { 0x0816, 850, 1252, "Portuguese", "Portugal" },
  { 0x081A, 852, 1252, "Serbian (latin)", "Yugoslavia" },
  { 0x081A, 852, 1252, "Serbian (latin)", "Yugoslavia" },
  { 0x081D, 850, 1252, "Swedish (Finland)", "Finland" },
  { 0x081D, 850, 1252, "Swedish (Finland)", "Finland" },
  { 0x0C01, 864, 1256, "Arabic", "Egypt" },
  { 0x0C01, 864, 1256, "Arabic", "Egypt" },
  { 0x0C04, 950,  950, "Chinese", "Hong Kong" },
  { 0x0C04, 950,  950, "Chinese", "Hong Kong" },
  { 0x0C07, 850, 1252, "German", "Austria" },
  { 0x0C07, 850, 1252, "German", "Austria" },
  { 0x0C09, 850, 1252, "English", "Australia" },      { 0x0C0A, 850, 1252, "Spanish - International Sort", "Spain" },
  { 0x0C09, 850, 1252, "English", "Australia" },      { 0x0C0A, 850, 1252, "Spanish - International Sort", "Spain" },
  { 0x0C0C, 850, 1252, "French", "Canada"},
  { 0x0C0C, 850, 1252, "French", "Canada"},
  { 0x0C1A, 855, 1251, "Serbian (Cyrillic)", "Serbia" },
  { 0x0C1A, 855, 1251, "Serbian (Cyrillic)", "Serbia" },
  { 0x1001, 864, 1256, "Arabic", "Libya" },
  { 0x1001, 864, 1256, "Arabic", "Libya" },
  { 0x1004, 936,  936, "Chinese", "Singapore" },
  { 0x1004, 936,  936, "Chinese", "Singapore" },
  { 0x1007, 850, 1252, "German", "Luxembourg" },
  { 0x1007, 850, 1252, "German", "Luxembourg" },
  { 0x1009, 850, 1252, "English", "Canada" },
  { 0x1009, 850, 1252, "English", "Canada" },
  { 0x100A, 850, 1252, "Spanish", "Guatemala" },
  { 0x100A, 850, 1252, "Spanish", "Guatemala" },
  { 0x100C, 850, 1252, "French", "Switzerland" },
  { 0x100C, 850, 1252, "French", "Switzerland" },
  { 0x1401, 864, 1256, "Arabic", "Algeria" },
  { 0x1401, 864, 1256, "Arabic", "Algeria" },
  { 0x1407, 850, 1252, "German", "Liechtenstein" },
  { 0x1407, 850, 1252, "German", "Liechtenstein" },
  { 0x1409, 850, 1252, "English", "New Zealand" },    { 0x140A, 850, 1252, "Spanish", "Costa Rica" },
  { 0x1409, 850, 1252, "English", "New Zealand" },    { 0x140A, 850, 1252, "Spanish", "Costa Rica" },
  { 0x140C, 850, 1252, "French", "Luxembourg" },
  { 0x140C, 850, 1252, "French", "Luxembourg" },
  { 0x1801, 864, 1256, "Arabic", "Morocco" },
  { 0x1801, 864, 1256, "Arabic", "Morocco" },
  { 0x1809, 850, 1252, "English", "Ireland" },        { 0x180A, 850, 1252, "Spanish", "Panama" },
  { 0x1809, 850, 1252, "English", "Ireland" },        { 0x180A, 850, 1252, "Spanish", "Panama" },
  { 0x180C, 850, 1252, "French", "Monaco" },
  { 0x180C, 850, 1252, "French", "Monaco" },
  { 0x1C01, 864, 1256, "Arabic", "Tunisia" },
  { 0x1C01, 864, 1256, "Arabic", "Tunisia" },
  { 0x1C09, 437, 1252, "English", "South Africa" },   { 0x1C0A, 850, 1252, "Spanish", "Dominican Republic" },
  { 0x1C09, 437, 1252, "English", "South Africa" },   { 0x1C0A, 850, 1252, "Spanish", "Dominican Republic" },
  { 0x2001, 864, 1256, "Arabic", "Oman" },
  { 0x2001, 864, 1256, "Arabic", "Oman" },
  { 0x2009, 850, 1252, "English", "Jamaica" },        { 0x200A, 850, 1252, "Spanish", "Venezuela" },
  { 0x2009, 850, 1252, "English", "Jamaica" },        { 0x200A, 850, 1252, "Spanish", "Venezuela" },
  { 0x2401, 864, 1256, "Arabic", "Yemen" },
  { 0x2401, 864, 1256, "Arabic", "Yemen" },
  { 0x2409, 850, 1252, "English", "Caribbean" },      { 0x240A, 850, 1252, "Spanish", "Colombia" },
  { 0x2409, 850, 1252, "English", "Caribbean" },      { 0x240A, 850, 1252, "Spanish", "Colombia" },
  { 0x2801, 864, 1256, "Arabic", "Syria" },
  { 0x2801, 864, 1256, "Arabic", "Syria" },
  { 0x2809, 850, 1252, "English", "Belize" },         { 0x280A, 850, 1252, "Spanish", "Peru" },
  { 0x2809, 850, 1252, "English", "Belize" },         { 0x280A, 850, 1252, "Spanish", "Peru" },
  { 0x2C01, 864, 1256, "Arabic", "Jordan" },
  { 0x2C01, 864, 1256, "Arabic", "Jordan" },
  { 0x2C09, 437, 1252, "English", "Trinidad & Tobago" },{ 0x2C0A, 850, 1252, "Spanish", "Argentina" },
  { 0x2C09, 437, 1252, "English", "Trinidad & Tobago" },{ 0x2C0A, 850, 1252, "Spanish", "Argentina" },
  { 0x3001, 864, 1256, "Arabic", "Lebanon" },
  { 0x3001, 864, 1256, "Arabic", "Lebanon" },
  { 0x3009, 437, 1252, "English", "Zimbabwe" },       { 0x300A, 850, 1252, "Spanish", "Ecuador" },
  { 0x3009, 437, 1252, "English", "Zimbabwe" },       { 0x300A, 850, 1252, "Spanish", "Ecuador" },
  { 0x3401, 864, 1256, "Arabic", "Kuwait" },
  { 0x3401, 864, 1256, "Arabic", "Kuwait" },
  { 0x3409, 437, 1252, "English", "Philippines" },    { 0x340A, 850, 1252, "Spanish", "Chile" },
  { 0x3409, 437, 1252, "English", "Philippines" },    { 0x340A, 850, 1252, "Spanish", "Chile" },
  { 0x3801, 864, 1256, "Arabic", "United Arab Emirates" },
  { 0x3801, 864, 1256, "Arabic", "United Arab Emirates" },
  { 0x380A, 850, 1252, "Spanish", "Uruguay" },
  { 0x380A, 850, 1252, "Spanish", "Uruguay" },
  { 0x3C01, 864, 1256, "Arabic", "Bahrain" },
  { 0x3C01, 864, 1256, "Arabic", "Bahrain" },
  { 0x3C0A, 850, 1252, "Spanish", "Paraguay" },
  { 0x3C0A, 850, 1252, "Spanish", "Paraguay" },
  { 0x4001, 864, 1256, "Arabic", "Qatar" },
  { 0x4001, 864, 1256, "Arabic", "Qatar" },
  { 0x400A, 850, 1252, "Spanish", "Bolivia" },
  { 0x400A, 850, 1252, "Spanish", "Bolivia" },
  { 0x440A, 850, 1252, "Spanish", "El Salvador" },
  { 0x440A, 850, 1252, "Spanish", "El Salvador" },
  { 0x480A, 850, 1252, "Spanish", "Honduras" },
  { 0x480A, 850, 1252, "Spanish", "Honduras" },
  { 0x4C0A, 850, 1252, "Spanish", "Nicaragua" },
  { 0x4C0A, 850, 1252, "Spanish", "Nicaragua" },
  { 0x500A, 850, 1252, "Spanish", "Puerto Rico" },
  { 0x500A, 850, 1252, "Spanish", "Puerto Rico" },
  { (unsigned) -1,  0,      0, NULL, NULL }
  { (unsigned) -1,  0,      0, NULL, NULL }
};
};
 
 
#endif
#endif
 
 
/* Specifies the default codepage to be used for unicode
/* Specifies the default codepage to be used for unicode
   transformations.  By default this is CP_ACP.  */
   transformations.  By default this is CP_ACP.  */
rc_uint_type wind_default_codepage = CP_ACP;
rc_uint_type wind_default_codepage = CP_ACP;
 
 
/* Specifies the currently used codepage for unicode
/* Specifies the currently used codepage for unicode
   transformations.  By default this is CP_ACP.  */
   transformations.  By default this is CP_ACP.  */
rc_uint_type wind_current_codepage = CP_ACP;
rc_uint_type wind_current_codepage = CP_ACP;
 
 
/* Convert an ASCII string to a unicode string.  We just copy it,
/* Convert an ASCII string to a unicode string.  We just copy it,
   expanding chars to shorts, rather than doing something intelligent.  */
   expanding chars to shorts, rather than doing something intelligent.  */
 
 
void
void
unicode_from_ascii (rc_uint_type *length, unichar **unicode, const char *ascii)
unicode_from_ascii (rc_uint_type *length, unichar **unicode, const char *ascii)
{
{
  unicode_from_codepage (length, unicode, ascii, wind_current_codepage);
  unicode_from_codepage (length, unicode, ascii, wind_current_codepage);
}
}
 
 
/* Convert an unicode string to an ASCII string.  We just copy it,
/* Convert an unicode string to an ASCII string.  We just copy it,
   shrink shorts to chars, rather than doing something intelligent.
   shrink shorts to chars, rather than doing something intelligent.
   Shorts with not within the char range are replaced by '_'.  */
   Shorts with not within the char range are replaced by '_'.  */
 
 
void
void
ascii_from_unicode (rc_uint_type *length, const unichar *unicode, char **ascii)
ascii_from_unicode (rc_uint_type *length, const unichar *unicode, char **ascii)
{
{
  codepage_from_unicode (length, unicode, ascii, wind_current_codepage);
  codepage_from_unicode (length, unicode, ascii, wind_current_codepage);
}
}
 
 
/* Print the unicode string UNICODE to the file E.  LENGTH is the
/* Print the unicode string UNICODE to the file E.  LENGTH is the
   number of characters to print, or -1 if we should print until the
   number of characters to print, or -1 if we should print until the
   end of the string.  FIXME: On a Windows host, we should be calling
   end of the string.  FIXME: On a Windows host, we should be calling
   some Windows function, probably WideCharToMultiByte.  */
   some Windows function, probably WideCharToMultiByte.  */
 
 
void
void
unicode_print (FILE *e, const unichar *unicode, rc_uint_type length)
unicode_print (FILE *e, const unichar *unicode, rc_uint_type length)
{
{
  while (1)
  while (1)
    {
    {
      unichar ch;
      unichar ch;
 
 
      if (length == 0)
      if (length == 0)
        return;
        return;
      if ((bfd_signed_vma) length > 0)
      if ((bfd_signed_vma) length > 0)
        --length;
        --length;
 
 
      ch = *unicode;
      ch = *unicode;
 
 
      if (ch == 0 && (bfd_signed_vma) length < 0)
      if (ch == 0 && (bfd_signed_vma) length < 0)
        return;
        return;
 
 
      ++unicode;
      ++unicode;
 
 
      if ((ch & 0x7f) == ch)
      if ((ch & 0x7f) == ch)
        {
        {
          if (ch == '\\')
          if (ch == '\\')
            fputs ("\\\\", e);
            fputs ("\\\\", e);
          else if (ch == '"')
          else if (ch == '"')
            fputs ("\"\"", e);
            fputs ("\"\"", e);
          else if (ISPRINT (ch))
          else if (ISPRINT (ch))
            putc (ch, e);
            putc (ch, e);
          else
          else
            {
            {
              switch (ch)
              switch (ch)
                {
                {
                case ESCAPE_A:
                case ESCAPE_A:
                  fputs ("\\a", e);
                  fputs ("\\a", e);
                  break;
                  break;
 
 
                case ESCAPE_B:
                case ESCAPE_B:
                  fputs ("\\b", e);
                  fputs ("\\b", e);
                  break;
                  break;
 
 
                case ESCAPE_F:
                case ESCAPE_F:
                  fputs ("\\f", e);
                  fputs ("\\f", e);
                  break;
                  break;
 
 
                case ESCAPE_N:
                case ESCAPE_N:
                  fputs ("\\n", e);
                  fputs ("\\n", e);
                  break;
                  break;
 
 
                case ESCAPE_R:
                case ESCAPE_R:
                  fputs ("\\r", e);
                  fputs ("\\r", e);
                  break;
                  break;
 
 
                case ESCAPE_T:
                case ESCAPE_T:
                  fputs ("\\t", e);
                  fputs ("\\t", e);
                  break;
                  break;
 
 
                case ESCAPE_V:
                case ESCAPE_V:
                  fputs ("\\v", e);
                  fputs ("\\v", e);
                  break;
                  break;
 
 
                default:
                default:
                  fprintf (e, "\\%03o", (unsigned int) ch);
                  fprintf (e, "\\%03o", (unsigned int) ch);
                  break;
                  break;
                }
                }
            }
            }
        }
        }
      else if ((ch & 0xff) == ch)
      else if ((ch & 0xff) == ch)
        fprintf (e, "\\%03o", (unsigned int) ch);
        fprintf (e, "\\%03o", (unsigned int) ch);
      else
      else
        fprintf (e, "\\x%04x", (unsigned int) ch);
        fprintf (e, "\\x%04x", (unsigned int) ch);
    }
    }
}
}
 
 
/* Print a unicode string to a file.  */
/* Print a unicode string to a file.  */
 
 
void
void
ascii_print (FILE *e, const char *s, rc_uint_type length)
ascii_print (FILE *e, const char *s, rc_uint_type length)
{
{
  while (1)
  while (1)
    {
    {
      char ch;
      char ch;
 
 
      if (length == 0)
      if (length == 0)
        return;
        return;
      if ((bfd_signed_vma) length > 0)
      if ((bfd_signed_vma) length > 0)
        --length;
        --length;
 
 
      ch = *s;
      ch = *s;
 
 
      if (ch == 0 && (bfd_signed_vma) length < 0)
      if (ch == 0 && (bfd_signed_vma) length < 0)
        return;
        return;
 
 
      ++s;
      ++s;
 
 
      if ((ch & 0x7f) == ch)
      if ((ch & 0x7f) == ch)
        {
        {
          if (ch == '\\')
          if (ch == '\\')
            fputs ("\\\\", e);
            fputs ("\\\\", e);
          else if (ch == '"')
          else if (ch == '"')
            fputs ("\"\"", e);
            fputs ("\"\"", e);
          else if (ISPRINT (ch))
          else if (ISPRINT (ch))
            putc (ch, e);
            putc (ch, e);
          else
          else
            {
            {
              switch (ch)
              switch (ch)
                {
                {
                case ESCAPE_A:
                case ESCAPE_A:
                  fputs ("\\a", e);
                  fputs ("\\a", e);
                  break;
                  break;
 
 
                case ESCAPE_B:
                case ESCAPE_B:
                  fputs ("\\b", e);
                  fputs ("\\b", e);
                  break;
                  break;
 
 
                case ESCAPE_F:
                case ESCAPE_F:
                  fputs ("\\f", e);
                  fputs ("\\f", e);
                  break;
                  break;
 
 
                case ESCAPE_N:
                case ESCAPE_N:
                  fputs ("\\n", e);
                  fputs ("\\n", e);
                  break;
                  break;
 
 
                case ESCAPE_R:
                case ESCAPE_R:
                  fputs ("\\r", e);
                  fputs ("\\r", e);
                  break;
                  break;
 
 
                case ESCAPE_T:
                case ESCAPE_T:
                  fputs ("\\t", e);
                  fputs ("\\t", e);
                  break;
                  break;
 
 
                case ESCAPE_V:
                case ESCAPE_V:
                  fputs ("\\v", e);
                  fputs ("\\v", e);
                  break;
                  break;
 
 
                default:
                default:
                  fprintf (e, "\\%03o", (unsigned int) ch);
                  fprintf (e, "\\%03o", (unsigned int) ch);
                  break;
                  break;
                }
                }
            }
            }
        }
        }
      else
      else
        fprintf (e, "\\%03o", (unsigned int) ch & 0xff);
        fprintf (e, "\\%03o", (unsigned int) ch & 0xff);
    }
    }
}
}
 
 
rc_uint_type
rc_uint_type
unichar_len (const unichar *unicode)
unichar_len (const unichar *unicode)
{
{
  rc_uint_type r = 0;
  rc_uint_type r = 0;
 
 
  if (unicode)
  if (unicode)
    while (unicode[r] != 0)
    while (unicode[r] != 0)
      r++;
      r++;
  else
  else
    --r;
    --r;
  return r;
  return r;
}
}
 
 
unichar *
unichar *
unichar_dup (const unichar *unicode)
unichar_dup (const unichar *unicode)
{
{
  unichar *r;
  unichar *r;
  int len;
  int len;
 
 
  if (! unicode)
  if (! unicode)
    return NULL;
    return NULL;
  for (len = 0; unicode[len] != 0; ++len)
  for (len = 0; unicode[len] != 0; ++len)
    ;
    ;
  ++len;
  ++len;
  r = ((unichar *) res_alloc (len * sizeof (unichar)));
  r = ((unichar *) res_alloc (len * sizeof (unichar)));
  memcpy (r, unicode, len * sizeof (unichar));
  memcpy (r, unicode, len * sizeof (unichar));
  return r;
  return r;
}
}
 
 
unichar *
unichar *
unichar_dup_uppercase (const unichar *u)
unichar_dup_uppercase (const unichar *u)
{
{
  unichar *r = unichar_dup (u);
  unichar *r = unichar_dup (u);
  int i;
  int i;
 
 
  if (! r)
  if (! r)
    return NULL;
    return NULL;
 
 
  for (i = 0; r[i] != 0; ++i)
  for (i = 0; r[i] != 0; ++i)
    {
    {
      if (r[i] >= 'a' && r[i] <= 'z')
      if (r[i] >= 'a' && r[i] <= 'z')
        r[i] &= 0xdf;
        r[i] &= 0xdf;
    }
    }
  return r;
  return r;
}
}
 
 
static int
static int
unichar_isascii (const unichar *u, rc_uint_type len)
unichar_isascii (const unichar *u, rc_uint_type len)
{
{
  rc_uint_type i;
  rc_uint_type i;
 
 
  if ((bfd_signed_vma) len < 0)
  if ((bfd_signed_vma) len < 0)
    {
    {
      if (u)
      if (u)
        len = (rc_uint_type) unichar_len (u);
        len = (rc_uint_type) unichar_len (u);
      else
      else
        len = 0;
        len = 0;
    }
    }
 
 
  for (i = 0; i < len; i++)
  for (i = 0; i < len; i++)
    if ((u[i] & 0xff80) != 0)
    if ((u[i] & 0xff80) != 0)
      return 0;
      return 0;
  return 1;
  return 1;
}
}
 
 
void
void
unicode_print_quoted (FILE *e, const unichar *u, rc_uint_type len)
unicode_print_quoted (FILE *e, const unichar *u, rc_uint_type len)
{
{
  if (! unichar_isascii (u, len))
  if (! unichar_isascii (u, len))
    fputc ('L', e);
    fputc ('L', e);
  fputc ('"', e);
  fputc ('"', e);
  unicode_print (e, u, len);
  unicode_print (e, u, len);
  fputc ('"', e);
  fputc ('"', e);
}
}
 
 
int
int
unicode_is_valid_codepage (rc_uint_type cp)
unicode_is_valid_codepage (rc_uint_type cp)
{
{
  if ((cp & 0xffff) != cp)
  if ((cp & 0xffff) != cp)
    return 0;
    return 0;
  if (cp == CP_UTF16 || cp == CP_ACP)
  if (cp == CP_UTF16 || cp == CP_ACP)
    return 1;
    return 1;
 
 
#if !defined (_WIN32) && !defined (__CYGWIN__)
#if !defined (_WIN32) && !defined (__CYGWIN__)
  if (! wind_find_codepage_info (cp))
  if (! wind_find_codepage_info (cp))
    return 0;
    return 0;
  return 1;
  return 1;
#else
#else
  return !! IsValidCodePage ((UINT) cp);
  return !! IsValidCodePage ((UINT) cp);
#endif
#endif
}
}
 
 
#if defined (_WIN32) || defined (__CYGWIN__)
#if defined (_WIN32) || defined (__CYGWIN__)
 
 
#define max_cp_string_len 6
#define max_cp_string_len 6
 
 
static unsigned int
static unsigned int
codepage_from_langid (unsigned short langid)
codepage_from_langid (unsigned short langid)
{
{
  char cp_string [max_cp_string_len];
  char cp_string [max_cp_string_len];
  int c;
  int c;
 
 
  memset (cp_string, 0, max_cp_string_len);
  memset (cp_string, 0, max_cp_string_len);
  /* LOCALE_RETURN_NUMBER flag would avoid strtoul conversion,
  /* LOCALE_RETURN_NUMBER flag would avoid strtoul conversion,
     but is unavailable on Win95.  */
     but is unavailable on Win95.  */
  c = GetLocaleInfoA (MAKELCID (langid, SORT_DEFAULT),
  c = GetLocaleInfoA (MAKELCID (langid, SORT_DEFAULT),
                      LOCALE_IDEFAULTANSICODEPAGE,
                      LOCALE_IDEFAULTANSICODEPAGE,
                      cp_string, max_cp_string_len);
                      cp_string, max_cp_string_len);
  /* If codepage data for an LCID is not installed on users's system,
  /* If codepage data for an LCID is not installed on users's system,
     GetLocaleInfo returns an empty string.  Fall back to system ANSI
     GetLocaleInfo returns an empty string.  Fall back to system ANSI
     default. */
     default. */
  if (c == 0)
  if (c == 0)
    return CP_ACP;
    return CP_ACP;
  return strtoul (cp_string, 0, 10);
  return strtoul (cp_string, 0, 10);
}
}
 
 
static unsigned int
static unsigned int
wincodepage_from_langid (unsigned short langid)
wincodepage_from_langid (unsigned short langid)
{
{
  char cp_string [max_cp_string_len];
  char cp_string [max_cp_string_len];
  int c;
  int c;
 
 
  memset (cp_string, 0, max_cp_string_len);
  memset (cp_string, 0, max_cp_string_len);
  /* LOCALE_RETURN_NUMBER flag would avoid strtoul conversion,
  /* LOCALE_RETURN_NUMBER flag would avoid strtoul conversion,
     but is unavailable on Win95.  */
     but is unavailable on Win95.  */
  c = GetLocaleInfoA (MAKELCID (langid, SORT_DEFAULT),
  c = GetLocaleInfoA (MAKELCID (langid, SORT_DEFAULT),
                      LOCALE_IDEFAULTCODEPAGE,
                      LOCALE_IDEFAULTCODEPAGE,
                      cp_string, max_cp_string_len);
                      cp_string, max_cp_string_len);
  /* If codepage data for an LCID is not installed on users's system,
  /* If codepage data for an LCID is not installed on users's system,
     GetLocaleInfo returns an empty string.  Fall back to system ANSI
     GetLocaleInfo returns an empty string.  Fall back to system ANSI
     default. */
     default. */
  if (c == 0)
  if (c == 0)
    return CP_OEM;
    return CP_OEM;
  return strtoul (cp_string, 0, 10);
  return strtoul (cp_string, 0, 10);
}
}
 
 
static char *
static char *
lang_from_langid (unsigned short langid)
lang_from_langid (unsigned short langid)
{
{
  char cp_string[261];
  char cp_string[261];
  int c;
  int c;
 
 
  memset (cp_string, 0, 261);
  memset (cp_string, 0, 261);
  c = GetLocaleInfoA (MAKELCID (langid, SORT_DEFAULT),
  c = GetLocaleInfoA (MAKELCID (langid, SORT_DEFAULT),
                      LOCALE_SENGLANGUAGE,
                      LOCALE_SENGLANGUAGE,
                      cp_string, 260);
                      cp_string, 260);
  /* If codepage data for an LCID is not installed on users's system,
  /* If codepage data for an LCID is not installed on users's system,
     GetLocaleInfo returns an empty string.  Fall back to system ANSI
     GetLocaleInfo returns an empty string.  Fall back to system ANSI
     default. */
     default. */
  if (c == 0)
  if (c == 0)
    strcpy (cp_string, "Neutral");
    strcpy (cp_string, "Neutral");
  return xstrdup (cp_string);
  return xstrdup (cp_string);
}
}
 
 
static char *
static char *
country_from_langid (unsigned short langid)
country_from_langid (unsigned short langid)
{
{
  char cp_string[261];
  char cp_string[261];
  int c;
  int c;
 
 
  memset (cp_string, 0, 261);
  memset (cp_string, 0, 261);
  c = GetLocaleInfoA (MAKELCID (langid, SORT_DEFAULT),
  c = GetLocaleInfoA (MAKELCID (langid, SORT_DEFAULT),
                      LOCALE_SENGCOUNTRY,
                      LOCALE_SENGCOUNTRY,
                      cp_string, 260);
                      cp_string, 260);
  /* If codepage data for an LCID is not installed on users's system,
  /* If codepage data for an LCID is not installed on users's system,
     GetLocaleInfo returns an empty string.  Fall back to system ANSI
     GetLocaleInfo returns an empty string.  Fall back to system ANSI
     default. */
     default. */
  if (c == 0)
  if (c == 0)
    strcpy (cp_string, "Neutral");
    strcpy (cp_string, "Neutral");
  return xstrdup (cp_string);
  return xstrdup (cp_string);
}
}
 
 
#endif
#endif
 
 
const wind_language_t *
const wind_language_t *
wind_find_language_by_id (unsigned id)
wind_find_language_by_id (unsigned id)
{
{
#if !defined (_WIN32) && !defined (__CYGWIN__)
#if !defined (_WIN32) && !defined (__CYGWIN__)
  int i;
  int i;
 
 
  if (! id)
  if (! id)
    return NULL;
    return NULL;
  for (i = 0; languages[i].id != (unsigned) -1 && languages[i].id != id; i++)
  for (i = 0; languages[i].id != (unsigned) -1 && languages[i].id != id; i++)
    ;
    ;
  if (languages[i].id == id)
  if (languages[i].id == id)
    return &languages[i];
    return &languages[i];
  return NULL;
  return NULL;
#else
#else
  static wind_language_t wl;
  static wind_language_t wl;
 
 
  wl.id = id;
  wl.id = id;
  wl.doscp = codepage_from_langid ((unsigned short) id);
  wl.doscp = codepage_from_langid ((unsigned short) id);
  wl.wincp = wincodepage_from_langid ((unsigned short) id);
  wl.wincp = wincodepage_from_langid ((unsigned short) id);
  wl.name = lang_from_langid ((unsigned short) id);
  wl.name = lang_from_langid ((unsigned short) id);
  wl.country = country_from_langid ((unsigned short) id);
  wl.country = country_from_langid ((unsigned short) id);
 
 
  return & wl;
  return & wl;
#endif
#endif
}
}
 
 
const local_iconv_map *
const local_iconv_map *
wind_find_codepage_info (unsigned cp)
wind_find_codepage_info (unsigned cp)
{
{
#if !defined (_WIN32) && !defined (__CYGWIN__)
#if !defined (_WIN32) && !defined (__CYGWIN__)
  int i;
  int i;
 
 
  for (i = 0; codepages[i].codepage != (rc_uint_type) -1 && codepages[i].codepage != cp; i++)
  for (i = 0; codepages[i].codepage != (rc_uint_type) -1 && codepages[i].codepage != cp; i++)
    ;
    ;
  if (codepages[i].codepage == (rc_uint_type) -1)
  if (codepages[i].codepage == (rc_uint_type) -1)
    return NULL;
    return NULL;
  return &codepages[i];
  return &codepages[i];
#else
#else
  static local_iconv_map lim;
  static local_iconv_map lim;
  if (!unicode_is_valid_codepage (cp))
  if (!unicode_is_valid_codepage (cp))
        return NULL;
        return NULL;
  lim.codepage = cp;
  lim.codepage = cp;
  lim.iconv_name = "";
  lim.iconv_name = "";
  return & lim;
  return & lim;
#endif
#endif
}
}
 
 
/* Convert an Codepage string to a unicode string.  */
/* Convert an Codepage string to a unicode string.  */
 
 
void
void
unicode_from_codepage (rc_uint_type *length, unichar **u, const char *src, rc_uint_type cp)
unicode_from_codepage (rc_uint_type *length, unichar **u, const char *src, rc_uint_type cp)
{
{
  rc_uint_type len;
  rc_uint_type len;
 
 
  len = wind_MultiByteToWideChar (cp, src, NULL, 0);
  len = wind_MultiByteToWideChar (cp, src, NULL, 0);
  if (len)
  if (len)
    {
    {
      *u = ((unichar *) res_alloc (len));
      *u = ((unichar *) res_alloc (len));
      wind_MultiByteToWideChar (cp, src, *u, len);
      wind_MultiByteToWideChar (cp, src, *u, len);
    }
    }
  /* Discount the trailing '/0'.  If MultiByteToWideChar failed,
  /* Discount the trailing '/0'.  If MultiByteToWideChar failed,
     this will set *length to -1.  */
     this will set *length to -1.  */
  len -= sizeof (unichar);
  len -= sizeof (unichar);
 
 
  if (length != NULL)
  if (length != NULL)
    *length = len / sizeof (unichar);
    *length = len / sizeof (unichar);
}
}
 
 
/* Convert an unicode string to an codepage string.  */
/* Convert an unicode string to an codepage string.  */
 
 
void
void
codepage_from_unicode (rc_uint_type *length, const unichar *unicode, char **ascii, rc_uint_type cp)
codepage_from_unicode (rc_uint_type *length, const unichar *unicode, char **ascii, rc_uint_type cp)
{
{
  rc_uint_type len;
  rc_uint_type len;
 
 
  len = wind_WideCharToMultiByte (cp, unicode, NULL, 0);
  len = wind_WideCharToMultiByte (cp, unicode, NULL, 0);
  if (len)
  if (len)
    {
    {
      *ascii = (char *) res_alloc (len * sizeof (char));
      *ascii = (char *) res_alloc (len * sizeof (char));
      wind_WideCharToMultiByte (cp, unicode, *ascii, len);
      wind_WideCharToMultiByte (cp, unicode, *ascii, len);
    }
    }
  /* Discount the trailing '/0'.  If MultiByteToWideChar failed,
  /* Discount the trailing '/0'.  If MultiByteToWideChar failed,
     this will set *length to -1.  */
     this will set *length to -1.  */
  len--;
  len--;
 
 
  if (length != NULL)
  if (length != NULL)
    *length = len;
    *length = len;
}
}
 
 
#ifdef HAVE_ICONV_H
#ifdef HAVE_ICONV_H
static int
static int
iconv_onechar (iconv_t cd, const char *s, char *d, int d_len, const char **n_s, char **n_d)
iconv_onechar (iconv_t cd, const char *s, char *d, int d_len, const char **n_s, char **n_d)
{
{
  int i;
  int i;
 
 
  for (i = 1; i <= 32; i++)
  for (i = 1; i <= 32; i++)
    {
    {
      char *tmp_d = d;
      char *tmp_d = d;
      const char *tmp_s = s;
      const char *tmp_s = s;
      size_t ret;
      size_t ret;
      size_t s_left = (size_t) i;
      size_t s_left = (size_t) i;
      size_t d_left = (size_t) d_len;
      size_t d_left = (size_t) d_len;
 
 
      ret = iconv (cd, & tmp_s, & s_left, & tmp_d, & d_left);
      ret = iconv (cd, & tmp_s, & s_left, & tmp_d, & d_left);
 
 
      if (ret != (size_t) -1)
      if (ret != (size_t) -1)
        {
        {
          *n_s = tmp_s;
          *n_s = tmp_s;
          *n_d = tmp_d;
          *n_d = tmp_d;
          return 0;
          return 0;
        }
        }
    }
    }
 
 
  return 1;
  return 1;
}
}
 
 
static const char *
static const char *
wind_iconv_cp (rc_uint_type cp)
wind_iconv_cp (rc_uint_type cp)
{
{
  const local_iconv_map *lim = wind_find_codepage_info (cp);
  const local_iconv_map *lim = wind_find_codepage_info (cp);
 
 
  if (!lim)
  if (!lim)
    return NULL;
    return NULL;
  return lim->iconv_name;
  return lim->iconv_name;
}
}
#endif /* HAVE_ICONV_H */
#endif /* HAVE_ICONV_H */
 
 
static rc_uint_type
static rc_uint_type
wind_MultiByteToWideChar (rc_uint_type cp, const char *mb,
wind_MultiByteToWideChar (rc_uint_type cp, const char *mb,
                          unichar *u, rc_uint_type u_len)
                          unichar *u, rc_uint_type u_len)
{
{
  rc_uint_type ret = 0;
  rc_uint_type ret = 0;
 
 
#if defined (_WIN32) || defined (__CYGWIN__)
#if defined (_WIN32) || defined (__CYGWIN__)
  ret = (rc_uint_type) MultiByteToWideChar (cp, MB_PRECOMPOSED,
  ret = (rc_uint_type) MultiByteToWideChar (cp, MB_PRECOMPOSED,
                                            mb, -1, u, u_len);
                                            mb, -1, u, u_len);
  /* Convert to bytes. */
  /* Convert to bytes. */
  ret *= sizeof (unichar);
  ret *= sizeof (unichar);
 
 
#elif defined (HAVE_ICONV_H)
#elif defined (HAVE_ICONV_H)
  int first = 1;
  int first = 1;
  char tmp[32];
  char tmp[32];
  char *p_tmp;
  char *p_tmp;
  const char *iconv_name = wind_iconv_cp (cp);
  const char *iconv_name = wind_iconv_cp (cp);
 
 
  if (!mb || !iconv_name)
  if (!mb || !iconv_name)
    return 0;
    return 0;
  iconv_t cd = iconv_open ("UTF-16", iconv_name);
  iconv_t cd = iconv_open ("UTF-16", iconv_name);
 
 
  while (1)
  while (1)
    {
    {
      int iret;
      int iret;
      const char *n_mb;
      const char *n_mb;
      char *n_tmp;
      char *n_tmp;
 
 
      p_tmp = tmp;
      p_tmp = tmp;
      iret = iconv_onechar (cd, (const char *) mb, p_tmp, 32, & n_mb, & n_tmp);
      iret = iconv_onechar (cd, (const char *) mb, p_tmp, 32, & n_mb, & n_tmp);
      if (first)
      if (first)
        {
        {
          first = 0;
          first = 0;
          continue;
          continue;
        }
        }
      if (!iret)
      if (!iret)
        {
        {
          size_t l_tmp = (size_t) (n_tmp - p_tmp);
          size_t l_tmp = (size_t) (n_tmp - p_tmp);
 
 
          if (u)
          if (u)
            {
            {
              if ((size_t) u_len < l_tmp)
              if ((size_t) u_len < l_tmp)
                break;
                break;
              memcpy (u, tmp, l_tmp);
              memcpy (u, tmp, l_tmp);
              u += l_tmp/2;
              u += l_tmp/2;
              u_len -= l_tmp;
              u_len -= l_tmp;
            }
            }
          ret += l_tmp;
          ret += l_tmp;
        }
        }
      else
      else
        break;
        break;
      if (tmp[0] == 0 && tmp[1] == 0)
      if (tmp[0] == 0 && tmp[1] == 0)
        break;
        break;
      mb = n_mb;
      mb = n_mb;
    }
    }
  iconv_close (cd);
  iconv_close (cd);
#else
#else
  if (cp)
  if (cp)
    ret = 0;
    ret = 0;
  ret = strlen (mb) + 1;
  ret = strlen (mb) + 1;
  ret *= sizeof (unichar);
  ret *= sizeof (unichar);
  if (u != NULL && u_len != 0)
  if (u != NULL && u_len != 0)
    {
    {
      do
      do
        {
        {
          *u++ = ((unichar) *mb) & 0xff;
          *u++ = ((unichar) *mb) & 0xff;
          --u_len; mb++;
          --u_len; mb++;
        }
        }
      while (u_len != 0 && mb[-1] != 0);
      while (u_len != 0 && mb[-1] != 0);
    }
    }
  if (u != NULL && u_len != 0)
  if (u != NULL && u_len != 0)
    *u = 0;
    *u = 0;
#endif
#endif
  return ret;
  return ret;
}
}
 
 
static rc_uint_type
static rc_uint_type
wind_WideCharToMultiByte (rc_uint_type cp, const unichar *u, char *mb, rc_uint_type mb_len)
wind_WideCharToMultiByte (rc_uint_type cp, const unichar *u, char *mb, rc_uint_type mb_len)
{
{
  rc_uint_type ret = 0;
  rc_uint_type ret = 0;
#if defined (_WIN32) || defined (__CYGWIN__)
#if defined (_WIN32) || defined (__CYGWIN__)
  WINBOOL used_def = FALSE;
  WINBOOL used_def = FALSE;
 
 
  ret = (rc_uint_type) WideCharToMultiByte (cp, 0, u, -1, mb, mb_len,
  ret = (rc_uint_type) WideCharToMultiByte (cp, 0, u, -1, mb, mb_len,
                                            NULL, & used_def);
                                            NULL, & used_def);
#elif defined (HAVE_ICONV_H)
#elif defined (HAVE_ICONV_H)
  int first = 1;
  int first = 1;
  char tmp[32];
  char tmp[32];
  char *p_tmp;
  char *p_tmp;
  const char *iconv_name = wind_iconv_cp (cp);
  const char *iconv_name = wind_iconv_cp (cp);
 
 
  if (!u || !iconv_name)
  if (!u || !iconv_name)
    return 0;
    return 0;
  iconv_t cd = iconv_open (iconv_name, "UTF-16");
  iconv_t cd = iconv_open (iconv_name, "UTF-16");
 
 
  while (1)
  while (1)
    {
    {
      int iret;
      int iret;
      const char *n_u;
      const char *n_u;
      char *n_tmp;
      char *n_tmp;
 
 
      p_tmp = tmp;
      p_tmp = tmp;
      iret = iconv_onechar (cd, (const char *) u, p_tmp, 32, &n_u, & n_tmp);
      iret = iconv_onechar (cd, (const char *) u, p_tmp, 32, &n_u, & n_tmp);
      if (first)
      if (first)
        {
        {
          first = 0;
          first = 0;
          continue;
          continue;
        }
        }
      if (!iret)
      if (!iret)
        {
        {
          size_t l_tmp = (size_t) (n_tmp - p_tmp);
          size_t l_tmp = (size_t) (n_tmp - p_tmp);
 
 
          if (mb)
          if (mb)
            {
            {
              if ((size_t) mb_len < l_tmp)
              if ((size_t) mb_len < l_tmp)
                break;
                break;
              memcpy (mb, tmp, l_tmp);
              memcpy (mb, tmp, l_tmp);
              mb += l_tmp;
              mb += l_tmp;
              mb_len -= l_tmp;
              mb_len -= l_tmp;
            }
            }
          ret += l_tmp;
          ret += l_tmp;
        }
        }
      else
      else
        break;
        break;
      if (u[0] == 0)
      if (u[0] == 0)
        break;
        break;
      u = (const unichar *) n_u;
      u = (const unichar *) n_u;
    }
    }
  iconv_close (cd);
  iconv_close (cd);
#else
#else
  if (cp)
  if (cp)
    ret = 0;
    ret = 0;
 
 
  while (u[ret] != 0)
  while (u[ret] != 0)
    ++ret;
    ++ret;
 
 
  ++ret;
  ++ret;
 
 
  if (mb)
  if (mb)
    {
    {
      while (*u != 0 && mb_len != 0)
      while (*u != 0 && mb_len != 0)
        {
        {
          if (u[0] == (u[0] & 0x7f))
          if (u[0] == (u[0] & 0x7f))
            *mb++ = (char) u[0];
            *mb++ = (char) u[0];
          else
          else
            *mb++ = '_';
            *mb++ = '_';
          ++u; --mb_len;
          ++u; --mb_len;
        }
        }
      if (mb_len != 0)
      if (mb_len != 0)
        *mb = 0;
        *mb = 0;
    }
    }
#endif
#endif
  return ret;
  return ret;
}
}
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.