winduni.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  1. /* winduni.c -- unicode support for the windres program.
  2. Copyright (C) 1997-2015 Free Software Foundation, Inc.
  3. Written by Ian Lance Taylor, Cygnus Support.
  4. Rewritten by Kai Tietz, Onevision.
  5. This file is part of GNU Binutils.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
  17. 02110-1301, USA. */
  18. /* This file contains unicode support routines for the windres
  19. program. Ideally, we would have generic unicode support which
  20. would work on all systems. However, we don't. Instead, on a
  21. Windows host, we are prepared to call some Windows routines. This
  22. means that we will generate different output on Windows and Unix
  23. hosts, but that seems better than not really supporting unicode at
  24. all. */
  25. #include "sysdep.h"
  26. #include "bfd.h"
  27. #include "libiberty.h" /* for xstrdup */
  28. #include "bucomm.h"
  29. /* Must be include before windows.h and winnls.h. */
  30. #if defined (_WIN32) || defined (__CYGWIN__)
  31. #include <windows.h>
  32. #include <winnls.h>
  33. #endif
  34. #include "winduni.h"
  35. #include "safe-ctype.h"
  36. #if HAVE_ICONV
  37. #include <iconv.h>
  38. #endif
  39. static rc_uint_type wind_WideCharToMultiByte (rc_uint_type, const unichar *, char *, rc_uint_type);
  40. static rc_uint_type wind_MultiByteToWideChar (rc_uint_type, const char *, unichar *, rc_uint_type);
  41. static int unichar_isascii (const unichar *, rc_uint_type);
  42. /* Convert an ASCII string to a unicode string. We just copy it,
  43. expanding chars to shorts, rather than doing something intelligent. */
  44. #if !defined (_WIN32) && !defined (__CYGWIN__)
  45. /* Codepages mapped. */
  46. static local_iconv_map codepages[] =
  47. {
  48. { 0, "MS-ANSI" },
  49. { 1, "WINDOWS-1252" },
  50. { 437, "MS-ANSI" },
  51. { 737, "MS-GREEK" },
  52. { 775, "WINBALTRIM" },
  53. { 850, "MS-ANSI" },
  54. { 852, "MS-EE" },
  55. { 857, "MS-TURK" },
  56. { 862, "CP862" },
  57. { 864, "CP864" },
  58. { 866, "MS-CYRL" },
  59. { 874, "WINDOWS-874" },
  60. { 932, "CP932" },
  61. { 936, "CP936" },
  62. { 949, "CP949" },
  63. { 950, "CP950" },
  64. { 1250, "WINDOWS-1250" },
  65. { 1251, "WINDOWS-1251" },
  66. { 1252, "WINDOWS-1252" },
  67. { 1253, "WINDOWS-1253" },
  68. { 1254, "WINDOWS-1254" },
  69. { 1255, "WINDOWS-1255" },
  70. { 1256, "WINDOWS-1256" },
  71. { 1257, "WINDOWS-1257" },
  72. { 1258, "WINDOWS-1258" },
  73. { CP_UTF7, "UTF-7" },
  74. { CP_UTF8, "UTF-8" },
  75. { CP_UTF16, "UTF-16LE" },
  76. { (rc_uint_type) -1, NULL }
  77. };
  78. /* Languages supported. */
  79. static const wind_language_t languages[] =
  80. {
  81. { 0x0000, 437, 1252, "Neutral", "Neutral" },
  82. { 0x0401, 864, 1256, "Arabic", "Saudi Arabia" }, { 0x0402, 866, 1251, "Bulgarian", "Bulgaria" },
  83. { 0x0403, 850, 1252, "Catalan", "Spain" }, { 0x0404, 950, 950, "Chinese", "Taiwan" },
  84. { 0x0405, 852, 1250, "Czech", "Czech Republic" }, { 0x0406, 850, 1252, "Danish", "Denmark" },
  85. { 0x0407, 850, 1252, "German", "Germany" }, { 0x0408, 737, 1253, "Greek", "Greece" },
  86. { 0x0409, 437, 1252, "English", "United States" }, { 0x040A, 850, 1252, "Spanish - Traditional Sort", "Spain" },
  87. { 0x040B, 850, 1252, "Finnish", "Finland" }, { 0x040C, 850, 1252, "French", "France" },
  88. { 0x040D, 862, 1255, "Hebrew", "Israel" }, { 0x040E, 852, 1250, "Hungarian", "Hungary" },
  89. { 0x040F, 850, 1252, "Icelandic", "Iceland" }, { 0x0410, 850, 1252, "Italian", "Italy" },
  90. { 0x0411, 932, 932, "Japanese", "Japan" }, { 0x0412, 949, 949, "Korean", "Korea (south)" },
  91. { 0x0413, 850, 1252, "Dutch", "Netherlands" }, { 0x0414, 850, 1252, "Norwegian (Bokm\345l)", "Norway" },
  92. { 0x0415, 852, 1250, "Polish", "Poland" }, { 0x0416, 850, 1252, "Portuguese", "Brazil" },
  93. { 0x0418, 852, 1250, "Romanian", "Romania" }, { 0x0419, 866, 1251, "Russian", "Russia" },
  94. { 0x041A, 852, 1250, "Croatian", "Croatia" }, { 0x041B, 852, 1250, "Slovak", "Slovakia" },
  95. { 0x041C, 852, 1250, "Albanian", "Albania" }, { 0x041D, 850, 1252, "Swedish", "Sweden" },
  96. { 0x041E, 874, 874, "Thai", "Thailand" }, { 0x041F, 857, 1254, "Turkish", "Turkey" },
  97. { 0x0421, 850, 1252, "Indonesian", "Indonesia" }, { 0x0422, 866, 1251, "Ukrainian", "Ukraine" },
  98. { 0x0423, 866, 1251, "Belarusian", "Belarus" }, { 0x0424, 852, 1250, "Slovene", "Slovenia" },
  99. { 0x0425, 775, 1257, "Estonian", "Estonia" }, { 0x0426, 775, 1257, "Latvian", "Latvia" },
  100. { 0x0427, 775, 1257, "Lithuanian", "Lithuania" },
  101. { 0x0429, 864, 1256, "Arabic", "Farsi" }, { 0x042A,1258, 1258, "Vietnamese", "Vietnam" },
  102. { 0x042D, 850, 1252, "Basque", "Spain" },
  103. { 0x042F, 866, 1251, "Macedonian", "Former Yugoslav Republic of Macedonia" },
  104. { 0x0436, 850, 1252, "Afrikaans", "South Africa" },
  105. { 0x0438, 850, 1252, "Faroese", "Faroe Islands" },
  106. { 0x043C, 437, 1252, "Irish", "Ireland" },
  107. { 0x043E, 850, 1252, "Malay", "Malaysia" },
  108. { 0x0801, 864, 1256, "Arabic", "Iraq" },
  109. { 0x0804, 936, 936, "Chinese (People's republic of China)", "People's republic of China" },
  110. { 0x0807, 850, 1252, "German", "Switzerland" },
  111. { 0x0809, 850, 1252, "English", "United Kingdom" }, { 0x080A, 850, 1252, "Spanish", "Mexico" },
  112. { 0x080C, 850, 1252, "French", "Belgium" },
  113. { 0x0810, 850, 1252, "Italian", "Switzerland" },
  114. { 0x0813, 850, 1252, "Dutch", "Belgium" }, { 0x0814, 850, 1252, "Norwegian (Nynorsk)", "Norway" },
  115. { 0x0816, 850, 1252, "Portuguese", "Portugal" },
  116. { 0x081A, 852, 1252, "Serbian (latin)", "Yugoslavia" },
  117. { 0x081D, 850, 1252, "Swedish (Finland)", "Finland" },
  118. { 0x0C01, 864, 1256, "Arabic", "Egypt" },
  119. { 0x0C04, 950, 950, "Chinese", "Hong Kong" },
  120. { 0x0C07, 850, 1252, "German", "Austria" },
  121. { 0x0C09, 850, 1252, "English", "Australia" }, { 0x0C0A, 850, 1252, "Spanish - International Sort", "Spain" },
  122. { 0x0C0C, 850, 1252, "French", "Canada"},
  123. { 0x0C1A, 855, 1251, "Serbian (Cyrillic)", "Serbia" },
  124. { 0x1001, 864, 1256, "Arabic", "Libya" },
  125. { 0x1004, 936, 936, "Chinese", "Singapore" },
  126. { 0x1007, 850, 1252, "German", "Luxembourg" },
  127. { 0x1009, 850, 1252, "English", "Canada" },
  128. { 0x100A, 850, 1252, "Spanish", "Guatemala" },
  129. { 0x100C, 850, 1252, "French", "Switzerland" },
  130. { 0x1401, 864, 1256, "Arabic", "Algeria" },
  131. { 0x1407, 850, 1252, "German", "Liechtenstein" },
  132. { 0x1409, 850, 1252, "English", "New Zealand" }, { 0x140A, 850, 1252, "Spanish", "Costa Rica" },
  133. { 0x140C, 850, 1252, "French", "Luxembourg" },
  134. { 0x1801, 864, 1256, "Arabic", "Morocco" },
  135. { 0x1809, 850, 1252, "English", "Ireland" }, { 0x180A, 850, 1252, "Spanish", "Panama" },
  136. { 0x180C, 850, 1252, "French", "Monaco" },
  137. { 0x1C01, 864, 1256, "Arabic", "Tunisia" },
  138. { 0x1C09, 437, 1252, "English", "South Africa" }, { 0x1C0A, 850, 1252, "Spanish", "Dominican Republic" },
  139. { 0x2001, 864, 1256, "Arabic", "Oman" },
  140. { 0x2009, 850, 1252, "English", "Jamaica" }, { 0x200A, 850, 1252, "Spanish", "Venezuela" },
  141. { 0x2401, 864, 1256, "Arabic", "Yemen" },
  142. { 0x2409, 850, 1252, "English", "Caribbean" }, { 0x240A, 850, 1252, "Spanish", "Colombia" },
  143. { 0x2801, 864, 1256, "Arabic", "Syria" },
  144. { 0x2809, 850, 1252, "English", "Belize" }, { 0x280A, 850, 1252, "Spanish", "Peru" },
  145. { 0x2C01, 864, 1256, "Arabic", "Jordan" },
  146. { 0x2C09, 437, 1252, "English", "Trinidad & Tobago" },{ 0x2C0A, 850, 1252, "Spanish", "Argentina" },
  147. { 0x3001, 864, 1256, "Arabic", "Lebanon" },
  148. { 0x3009, 437, 1252, "English", "Zimbabwe" }, { 0x300A, 850, 1252, "Spanish", "Ecuador" },
  149. { 0x3401, 864, 1256, "Arabic", "Kuwait" },
  150. { 0x3409, 437, 1252, "English", "Philippines" }, { 0x340A, 850, 1252, "Spanish", "Chile" },
  151. { 0x3801, 864, 1256, "Arabic", "United Arab Emirates" },
  152. { 0x380A, 850, 1252, "Spanish", "Uruguay" },
  153. { 0x3C01, 864, 1256, "Arabic", "Bahrain" },
  154. { 0x3C0A, 850, 1252, "Spanish", "Paraguay" },
  155. { 0x4001, 864, 1256, "Arabic", "Qatar" },
  156. { 0x400A, 850, 1252, "Spanish", "Bolivia" },
  157. { 0x440A, 850, 1252, "Spanish", "El Salvador" },
  158. { 0x480A, 850, 1252, "Spanish", "Honduras" },
  159. { 0x4C0A, 850, 1252, "Spanish", "Nicaragua" },
  160. { 0x500A, 850, 1252, "Spanish", "Puerto Rico" },
  161. { (unsigned) -1, 0, 0, NULL, NULL }
  162. };
  163. #endif
  164. /* Specifies the default codepage to be used for unicode
  165. transformations. By default this is CP_ACP. */
  166. rc_uint_type wind_default_codepage = CP_ACP;
  167. /* Specifies the currently used codepage for unicode
  168. transformations. By default this is CP_ACP. */
  169. rc_uint_type wind_current_codepage = CP_ACP;
  170. /* Convert an ASCII string to a unicode string. We just copy it,
  171. expanding chars to shorts, rather than doing something intelligent. */
  172. void
  173. unicode_from_ascii (rc_uint_type *length, unichar **unicode, const char *ascii)
  174. {
  175. unicode_from_codepage (length, unicode, ascii, wind_current_codepage);
  176. }
  177. /* Convert an ASCII string with length A_LENGTH to a unicode string. We just
  178. copy it, expanding chars to shorts, rather than doing something intelligent.
  179. This routine converts also \0 within a string. */
  180. void
  181. unicode_from_ascii_len (rc_uint_type *length, unichar **unicode, const char *ascii, rc_uint_type a_length)
  182. {
  183. char *tmp, *p;
  184. rc_uint_type tlen, elen, idx = 0;
  185. *unicode = NULL;
  186. if (!a_length)
  187. {
  188. if (length)
  189. *length = 0;
  190. return;
  191. }
  192. /* Make sure we have zero terminated string. */
  193. p = tmp = (char *) alloca (a_length + 1);
  194. memcpy (tmp, ascii, a_length);
  195. tmp[a_length] = 0;
  196. while (a_length > 0)
  197. {
  198. unichar *utmp, *up;
  199. tlen = strlen (p);
  200. if (tlen > a_length)
  201. tlen = a_length;
  202. if (*p == 0)
  203. {
  204. /* Make room for one more character. */
  205. utmp = (unichar *) res_alloc (sizeof (unichar) * (idx + 1));
  206. if (idx > 0)
  207. {
  208. memcpy (utmp, *unicode, idx * sizeof (unichar));
  209. }
  210. *unicode = utmp;
  211. utmp[idx++] = 0;
  212. --a_length;
  213. p++;
  214. continue;
  215. }
  216. utmp = NULL;
  217. elen = 0;
  218. elen = wind_MultiByteToWideChar (wind_current_codepage, p, NULL, 0);
  219. if (elen)
  220. {
  221. utmp = ((unichar *) res_alloc (elen + sizeof (unichar) * 2));
  222. wind_MultiByteToWideChar (wind_current_codepage, p, utmp, elen);
  223. elen /= sizeof (unichar);
  224. elen --;
  225. }
  226. else
  227. {
  228. /* Make room for one more character. */
  229. utmp = (unichar *) res_alloc (sizeof (unichar) * (idx + 1));
  230. if (idx > 0)
  231. {
  232. memcpy (utmp, *unicode, idx * sizeof (unichar));
  233. }
  234. *unicode = utmp;
  235. utmp[idx++] = ((unichar) *p) & 0xff;
  236. --a_length;
  237. p++;
  238. continue;
  239. }
  240. p += tlen;
  241. a_length -= tlen;
  242. up = (unichar *) res_alloc (sizeof (unichar) * (idx + elen));
  243. if (idx > 0)
  244. memcpy (up, *unicode, idx * sizeof (unichar));
  245. *unicode = up;
  246. if (elen)
  247. memcpy (&up[idx], utmp, sizeof (unichar) * elen);
  248. idx += elen;
  249. }
  250. if (length)
  251. *length = idx;
  252. }
  253. /* Convert an unicode string to an ASCII string. We just copy it,
  254. shrink shorts to chars, rather than doing something intelligent.
  255. Shorts with not within the char range are replaced by '_'. */
  256. void
  257. ascii_from_unicode (rc_uint_type *length, const unichar *unicode, char **ascii)
  258. {
  259. codepage_from_unicode (length, unicode, ascii, wind_current_codepage);
  260. }
  261. /* Print the unicode string UNICODE to the file E. LENGTH is the
  262. number of characters to print, or -1 if we should print until the
  263. end of the string. FIXME: On a Windows host, we should be calling
  264. some Windows function, probably WideCharToMultiByte. */
  265. void
  266. unicode_print (FILE *e, const unichar *unicode, rc_uint_type length)
  267. {
  268. while (1)
  269. {
  270. unichar ch;
  271. if (length == 0)
  272. return;
  273. if ((bfd_signed_vma) length > 0)
  274. --length;
  275. ch = *unicode;
  276. if (ch == 0 && (bfd_signed_vma) length < 0)
  277. return;
  278. ++unicode;
  279. if ((ch & 0x7f) == ch)
  280. {
  281. if (ch == '\\')
  282. fputs ("\\\\", e);
  283. else if (ch == '"')
  284. fputs ("\"\"", e);
  285. else if (ISPRINT (ch))
  286. putc (ch, e);
  287. else
  288. {
  289. switch (ch)
  290. {
  291. case ESCAPE_A:
  292. fputs ("\\a", e);
  293. break;
  294. case ESCAPE_B:
  295. fputs ("\\b", e);
  296. break;
  297. case ESCAPE_F:
  298. fputs ("\\f", e);
  299. break;
  300. case ESCAPE_N:
  301. fputs ("\\n", e);
  302. break;
  303. case ESCAPE_R:
  304. fputs ("\\r", e);
  305. break;
  306. case ESCAPE_T:
  307. fputs ("\\t", e);
  308. break;
  309. case ESCAPE_V:
  310. fputs ("\\v", e);
  311. break;
  312. default:
  313. fprintf (e, "\\%03o", (unsigned int) ch);
  314. break;
  315. }
  316. }
  317. }
  318. else if ((ch & 0xff) == ch)
  319. fprintf (e, "\\%03o", (unsigned int) ch);
  320. else
  321. fprintf (e, "\\x%04x", (unsigned int) ch);
  322. }
  323. }
  324. /* Print a unicode string to a file. */
  325. void
  326. ascii_print (FILE *e, const char *s, rc_uint_type length)
  327. {
  328. while (1)
  329. {
  330. char ch;
  331. if (length == 0)
  332. return;
  333. if ((bfd_signed_vma) length > 0)
  334. --length;
  335. ch = *s;
  336. if (ch == 0 && (bfd_signed_vma) length < 0)
  337. return;
  338. ++s;
  339. if ((ch & 0x7f) == ch)
  340. {
  341. if (ch == '\\')
  342. fputs ("\\\\", e);
  343. else if (ch == '"')
  344. fputs ("\"\"", e);
  345. else if (ISPRINT (ch))
  346. putc (ch, e);
  347. else
  348. {
  349. switch (ch)
  350. {
  351. case ESCAPE_A:
  352. fputs ("\\a", e);
  353. break;
  354. case ESCAPE_B:
  355. fputs ("\\b", e);
  356. break;
  357. case ESCAPE_F:
  358. fputs ("\\f", e);
  359. break;
  360. case ESCAPE_N:
  361. fputs ("\\n", e);
  362. break;
  363. case ESCAPE_R:
  364. fputs ("\\r", e);
  365. break;
  366. case ESCAPE_T:
  367. fputs ("\\t", e);
  368. break;
  369. case ESCAPE_V:
  370. fputs ("\\v", e);
  371. break;
  372. default:
  373. fprintf (e, "\\%03o", (unsigned int) ch);
  374. break;
  375. }
  376. }
  377. }
  378. else
  379. fprintf (e, "\\%03o", (unsigned int) ch & 0xff);
  380. }
  381. }
  382. rc_uint_type
  383. unichar_len (const unichar *unicode)
  384. {
  385. rc_uint_type r = 0;
  386. if (unicode)
  387. while (unicode[r] != 0)
  388. r++;
  389. else
  390. --r;
  391. return r;
  392. }
  393. unichar *
  394. unichar_dup (const unichar *unicode)
  395. {
  396. unichar *r;
  397. int len;
  398. if (! unicode)
  399. return NULL;
  400. for (len = 0; unicode[len] != 0; ++len)
  401. ;
  402. ++len;
  403. r = ((unichar *) res_alloc (len * sizeof (unichar)));
  404. memcpy (r, unicode, len * sizeof (unichar));
  405. return r;
  406. }
  407. unichar *
  408. unichar_dup_uppercase (const unichar *u)
  409. {
  410. unichar *r = unichar_dup (u);
  411. int i;
  412. if (! r)
  413. return NULL;
  414. for (i = 0; r[i] != 0; ++i)
  415. {
  416. if (r[i] >= 'a' && r[i] <= 'z')
  417. r[i] &= 0xdf;
  418. }
  419. return r;
  420. }
  421. static int
  422. unichar_isascii (const unichar *u, rc_uint_type len)
  423. {
  424. rc_uint_type i;
  425. if ((bfd_signed_vma) len < 0)
  426. {
  427. if (u)
  428. len = (rc_uint_type) unichar_len (u);
  429. else
  430. len = 0;
  431. }
  432. for (i = 0; i < len; i++)
  433. if ((u[i] & 0xff80) != 0)
  434. return 0;
  435. return 1;
  436. }
  437. void
  438. unicode_print_quoted (FILE *e, const unichar *u, rc_uint_type len)
  439. {
  440. if (! unichar_isascii (u, len))
  441. fputc ('L', e);
  442. fputc ('"', e);
  443. unicode_print (e, u, len);
  444. fputc ('"', e);
  445. }
  446. int
  447. unicode_is_valid_codepage (rc_uint_type cp)
  448. {
  449. if ((cp & 0xffff) != cp)
  450. return 0;
  451. if (cp == CP_UTF16 || cp == CP_ACP)
  452. return 1;
  453. #if !defined (_WIN32) && !defined (__CYGWIN__)
  454. if (! wind_find_codepage_info (cp))
  455. return 0;
  456. return 1;
  457. #else
  458. return !! IsValidCodePage ((UINT) cp);
  459. #endif
  460. }
  461. #if defined (_WIN32) || defined (__CYGWIN__)
  462. #define max_cp_string_len 6
  463. static unsigned int
  464. codepage_from_langid (unsigned short langid)
  465. {
  466. char cp_string [max_cp_string_len];
  467. int c;
  468. memset (cp_string, 0, max_cp_string_len);
  469. /* LOCALE_RETURN_NUMBER flag would avoid strtoul conversion,
  470. but is unavailable on Win95. */
  471. c = GetLocaleInfoA (MAKELCID (langid, SORT_DEFAULT),
  472. LOCALE_IDEFAULTANSICODEPAGE,
  473. cp_string, max_cp_string_len);
  474. /* If codepage data for an LCID is not installed on users's system,
  475. GetLocaleInfo returns an empty string. Fall back to system ANSI
  476. default. */
  477. if (c == 0)
  478. return CP_ACP;
  479. return strtoul (cp_string, 0, 10);
  480. }
  481. static unsigned int
  482. wincodepage_from_langid (unsigned short langid)
  483. {
  484. char cp_string [max_cp_string_len];
  485. int c;
  486. memset (cp_string, 0, max_cp_string_len);
  487. /* LOCALE_RETURN_NUMBER flag would avoid strtoul conversion,
  488. but is unavailable on Win95. */
  489. c = GetLocaleInfoA (MAKELCID (langid, SORT_DEFAULT),
  490. LOCALE_IDEFAULTCODEPAGE,
  491. cp_string, max_cp_string_len);
  492. /* If codepage data for an LCID is not installed on users's system,
  493. GetLocaleInfo returns an empty string. Fall back to system ANSI
  494. default. */
  495. if (c == 0)
  496. return CP_OEM;
  497. return strtoul (cp_string, 0, 10);
  498. }
  499. static char *
  500. lang_from_langid (unsigned short langid)
  501. {
  502. char cp_string[261];
  503. int c;
  504. memset (cp_string, 0, 261);
  505. c = GetLocaleInfoA (MAKELCID (langid, SORT_DEFAULT),
  506. LOCALE_SENGLANGUAGE,
  507. cp_string, 260);
  508. /* If codepage data for an LCID is not installed on users's system,
  509. GetLocaleInfo returns an empty string. Fall back to system ANSI
  510. default. */
  511. if (c == 0)
  512. strcpy (cp_string, "Neutral");
  513. return xstrdup (cp_string);
  514. }
  515. static char *
  516. country_from_langid (unsigned short langid)
  517. {
  518. char cp_string[261];
  519. int c;
  520. memset (cp_string, 0, 261);
  521. c = GetLocaleInfoA (MAKELCID (langid, SORT_DEFAULT),
  522. LOCALE_SENGCOUNTRY,
  523. cp_string, 260);
  524. /* If codepage data for an LCID is not installed on users's system,
  525. GetLocaleInfo returns an empty string. Fall back to system ANSI
  526. default. */
  527. if (c == 0)
  528. strcpy (cp_string, "Neutral");
  529. return xstrdup (cp_string);
  530. }
  531. #endif
  532. const wind_language_t *
  533. wind_find_language_by_id (unsigned id)
  534. {
  535. #if !defined (_WIN32) && !defined (__CYGWIN__)
  536. int i;
  537. if (! id)
  538. return NULL;
  539. for (i = 0; languages[i].id != (unsigned) -1 && languages[i].id != id; i++)
  540. ;
  541. if (languages[i].id == id)
  542. return &languages[i];
  543. return NULL;
  544. #else
  545. static wind_language_t wl;
  546. wl.id = id;
  547. wl.doscp = codepage_from_langid ((unsigned short) id);
  548. wl.wincp = wincodepage_from_langid ((unsigned short) id);
  549. wl.name = lang_from_langid ((unsigned short) id);
  550. wl.country = country_from_langid ((unsigned short) id);
  551. return & wl;
  552. #endif
  553. }
  554. const local_iconv_map *
  555. wind_find_codepage_info (unsigned cp)
  556. {
  557. #if !defined (_WIN32) && !defined (__CYGWIN__)
  558. int i;
  559. for (i = 0; codepages[i].codepage != (rc_uint_type) -1 && codepages[i].codepage != cp; i++)
  560. ;
  561. if (codepages[i].codepage == (rc_uint_type) -1)
  562. return NULL;
  563. return &codepages[i];
  564. #else
  565. static local_iconv_map lim;
  566. if (!unicode_is_valid_codepage (cp))
  567. return NULL;
  568. lim.codepage = cp;
  569. lim.iconv_name = "";
  570. return & lim;
  571. #endif
  572. }
  573. /* Convert an Codepage string to a unicode string. */
  574. void
  575. unicode_from_codepage (rc_uint_type *length, unichar **u, const char *src, rc_uint_type cp)
  576. {
  577. rc_uint_type len;
  578. len = wind_MultiByteToWideChar (cp, src, NULL, 0);
  579. if (len)
  580. {
  581. *u = ((unichar *) res_alloc (len));
  582. wind_MultiByteToWideChar (cp, src, *u, len);
  583. }
  584. /* Discount the trailing '/0'. If MultiByteToWideChar failed,
  585. this will set *length to -1. */
  586. len -= sizeof (unichar);
  587. if (length != NULL)
  588. *length = len / sizeof (unichar);
  589. }
  590. /* Convert an unicode string to an codepage string. */
  591. void
  592. codepage_from_unicode (rc_uint_type *length, const unichar *unicode, char **ascii, rc_uint_type cp)
  593. {
  594. rc_uint_type len;
  595. len = wind_WideCharToMultiByte (cp, unicode, NULL, 0);
  596. if (len)
  597. {
  598. *ascii = (char *) res_alloc (len * sizeof (char));
  599. wind_WideCharToMultiByte (cp, unicode, *ascii, len);
  600. }
  601. /* Discount the trailing '/0'. If MultiByteToWideChar failed,
  602. this will set *length to -1. */
  603. len--;
  604. if (length != NULL)
  605. *length = len;
  606. }
  607. #if defined (HAVE_ICONV) && !defined (_WIN32) && !defined (__CYGWIN__)
  608. static int
  609. iconv_onechar (iconv_t cd, ICONV_CONST char *s, char *d, int d_len, const char **n_s, char **n_d)
  610. {
  611. int i;
  612. for (i = 1; i <= 32; i++)
  613. {
  614. char *tmp_d = d;
  615. ICONV_CONST char *tmp_s = s;
  616. size_t ret;
  617. size_t s_left = (size_t) i;
  618. size_t d_left = (size_t) d_len;
  619. ret = iconv (cd, & tmp_s, & s_left, & tmp_d, & d_left);
  620. if (ret != (size_t) -1)
  621. {
  622. *n_s = tmp_s;
  623. *n_d = tmp_d;
  624. return 0;
  625. }
  626. }
  627. return 1;
  628. }
  629. static const char *
  630. wind_iconv_cp (rc_uint_type cp)
  631. {
  632. const local_iconv_map *lim = wind_find_codepage_info (cp);
  633. if (!lim)
  634. return NULL;
  635. return lim->iconv_name;
  636. }
  637. #endif /* HAVE_ICONV */
  638. static rc_uint_type
  639. wind_MultiByteToWideChar (rc_uint_type cp, const char *mb,
  640. unichar *u, rc_uint_type u_len)
  641. {
  642. rc_uint_type ret = 0;
  643. #if defined (_WIN32) || defined (__CYGWIN__)
  644. rc_uint_type conv_flags = MB_PRECOMPOSED;
  645. /* MB_PRECOMPOSED is not allowed for UTF-7 or UTF-8.
  646. MultiByteToWideChar will set the last error to
  647. ERROR_INVALID_FLAGS if we do. */
  648. if (cp == CP_UTF8 || cp == CP_UTF7)
  649. conv_flags = 0;
  650. ret = (rc_uint_type) MultiByteToWideChar (cp, conv_flags,
  651. mb, -1, u, u_len);
  652. /* Convert to bytes. */
  653. ret *= sizeof (unichar);
  654. #elif defined (HAVE_ICONV)
  655. int first = 1;
  656. char tmp[32];
  657. char *p_tmp;
  658. const char *iconv_name = wind_iconv_cp (cp);
  659. if (!mb || !iconv_name)
  660. return 0;
  661. iconv_t cd = iconv_open ("UTF-16LE", iconv_name);
  662. while (1)
  663. {
  664. int iret;
  665. const char *n_mb = "";
  666. char *n_tmp = "";
  667. p_tmp = tmp;
  668. iret = iconv_onechar (cd, (ICONV_CONST char *) mb, p_tmp, 32, & n_mb, & n_tmp);
  669. if (first)
  670. {
  671. first = 0;
  672. continue;
  673. }
  674. if (!iret)
  675. {
  676. size_t l_tmp = (size_t) (n_tmp - p_tmp);
  677. if (u)
  678. {
  679. if ((size_t) u_len < l_tmp)
  680. break;
  681. memcpy (u, tmp, l_tmp);
  682. u += l_tmp/2;
  683. u_len -= l_tmp;
  684. }
  685. ret += l_tmp;
  686. }
  687. else
  688. break;
  689. if (tmp[0] == 0 && tmp[1] == 0)
  690. break;
  691. mb = n_mb;
  692. }
  693. iconv_close (cd);
  694. #else
  695. if (cp)
  696. ret = 0;
  697. ret = strlen (mb) + 1;
  698. ret *= sizeof (unichar);
  699. if (u != NULL && u_len != 0)
  700. {
  701. do
  702. {
  703. *u++ = ((unichar) *mb) & 0xff;
  704. --u_len; mb++;
  705. }
  706. while (u_len != 0 && mb[-1] != 0);
  707. }
  708. if (u != NULL && u_len != 0)
  709. *u = 0;
  710. #endif
  711. return ret;
  712. }
  713. static rc_uint_type
  714. wind_WideCharToMultiByte (rc_uint_type cp, const unichar *u, char *mb, rc_uint_type mb_len)
  715. {
  716. rc_uint_type ret = 0;
  717. #if defined (_WIN32) || defined (__CYGWIN__)
  718. WINBOOL used_def = FALSE;
  719. ret = (rc_uint_type) WideCharToMultiByte (cp, 0, u, -1, mb, mb_len,
  720. NULL, & used_def);
  721. #elif defined (HAVE_ICONV)
  722. int first = 1;
  723. char tmp[32];
  724. char *p_tmp;
  725. const char *iconv_name = wind_iconv_cp (cp);
  726. if (!u || !iconv_name)
  727. return 0;
  728. iconv_t cd = iconv_open (iconv_name, "UTF-16LE");
  729. while (1)
  730. {
  731. int iret;
  732. const char *n_u = "";
  733. char *n_tmp = "";
  734. p_tmp = tmp;
  735. iret = iconv_onechar (cd, (ICONV_CONST char *) u, p_tmp, 32, &n_u, & n_tmp);
  736. if (first)
  737. {
  738. first = 0;
  739. continue;
  740. }
  741. if (!iret)
  742. {
  743. size_t l_tmp = (size_t) (n_tmp - p_tmp);
  744. if (mb)
  745. {
  746. if ((size_t) mb_len < l_tmp)
  747. break;
  748. memcpy (mb, tmp, l_tmp);
  749. mb += l_tmp;
  750. mb_len -= l_tmp;
  751. }
  752. ret += l_tmp;
  753. }
  754. else
  755. break;
  756. if (u[0] == 0)
  757. break;
  758. u = (const unichar *) n_u;
  759. }
  760. iconv_close (cd);
  761. #else
  762. if (cp)
  763. ret = 0;
  764. while (u[ret] != 0)
  765. ++ret;
  766. ++ret;
  767. if (mb)
  768. {
  769. while (*u != 0 && mb_len != 0)
  770. {
  771. if (u[0] == (u[0] & 0x7f))
  772. *mb++ = (char) u[0];
  773. else
  774. *mb++ = '_';
  775. ++u; --mb_len;
  776. }
  777. if (mb_len != 0)
  778. *mb = 0;
  779. }
  780. #endif
  781. return ret;
  782. }