winduni.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  1. /* winduni.c -- unicode support for the windres program.
  2. Copyright (C) 1997-2022 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, "cp1252" },
  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 *) xmalloc (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. free (tmp);
  253. }
  254. /* Convert an unicode string to an ASCII string. We just copy it,
  255. shrink shorts to chars, rather than doing something intelligent.
  256. Shorts with not within the char range are replaced by '_'. */
  257. void
  258. ascii_from_unicode (rc_uint_type *length, const unichar *unicode, char **ascii)
  259. {
  260. codepage_from_unicode (length, unicode, ascii, wind_current_codepage);
  261. }
  262. /* Print the unicode string UNICODE to the file E. LENGTH is the
  263. number of characters to print, or -1 if we should print until the
  264. end of the string. FIXME: On a Windows host, we should be calling
  265. some Windows function, probably WideCharToMultiByte. */
  266. void
  267. unicode_print (FILE *e, const unichar *unicode, rc_uint_type length)
  268. {
  269. while (1)
  270. {
  271. unichar ch;
  272. if (length == 0)
  273. return;
  274. if ((bfd_signed_vma) length > 0)
  275. --length;
  276. ch = *unicode;
  277. if (ch == 0 && (bfd_signed_vma) length < 0)
  278. return;
  279. ++unicode;
  280. if ((ch & 0x7f) == ch)
  281. {
  282. if (ch == '\\')
  283. fputs ("\\\\", e);
  284. else if (ch == '"')
  285. fputs ("\"\"", e);
  286. else if (ISPRINT (ch))
  287. putc (ch, e);
  288. else
  289. {
  290. switch (ch)
  291. {
  292. case ESCAPE_A:
  293. fputs ("\\a", e);
  294. break;
  295. case ESCAPE_B:
  296. fputs ("\\b", e);
  297. break;
  298. case ESCAPE_F:
  299. fputs ("\\f", e);
  300. break;
  301. case ESCAPE_N:
  302. fputs ("\\n", e);
  303. break;
  304. case ESCAPE_R:
  305. fputs ("\\r", e);
  306. break;
  307. case ESCAPE_T:
  308. fputs ("\\t", e);
  309. break;
  310. case ESCAPE_V:
  311. fputs ("\\v", e);
  312. break;
  313. default:
  314. fprintf (e, "\\%03o", (unsigned int) ch);
  315. break;
  316. }
  317. }
  318. }
  319. else if ((ch & 0xff) == ch)
  320. fprintf (e, "\\%03o", (unsigned int) ch);
  321. else
  322. fprintf (e, "\\x%04x", (unsigned int) ch);
  323. }
  324. }
  325. /* Print a unicode string to a file. */
  326. void
  327. ascii_print (FILE *e, const char *s, rc_uint_type length)
  328. {
  329. while (1)
  330. {
  331. char ch;
  332. if (length == 0)
  333. return;
  334. if ((bfd_signed_vma) length > 0)
  335. --length;
  336. ch = *s;
  337. if (ch == 0 && (bfd_signed_vma) length < 0)
  338. return;
  339. ++s;
  340. if ((ch & 0x7f) == ch)
  341. {
  342. if (ch == '\\')
  343. fputs ("\\\\", e);
  344. else if (ch == '"')
  345. fputs ("\"\"", e);
  346. else if (ISPRINT (ch))
  347. putc (ch, e);
  348. else
  349. {
  350. switch (ch)
  351. {
  352. case ESCAPE_A:
  353. fputs ("\\a", e);
  354. break;
  355. case ESCAPE_B:
  356. fputs ("\\b", e);
  357. break;
  358. case ESCAPE_F:
  359. fputs ("\\f", e);
  360. break;
  361. case ESCAPE_N:
  362. fputs ("\\n", e);
  363. break;
  364. case ESCAPE_R:
  365. fputs ("\\r", e);
  366. break;
  367. case ESCAPE_T:
  368. fputs ("\\t", e);
  369. break;
  370. case ESCAPE_V:
  371. fputs ("\\v", e);
  372. break;
  373. default:
  374. fprintf (e, "\\%03o", (unsigned int) ch);
  375. break;
  376. }
  377. }
  378. }
  379. else
  380. fprintf (e, "\\%03o", (unsigned int) ch & 0xff);
  381. }
  382. }
  383. rc_uint_type
  384. unichar_len (const unichar *unicode)
  385. {
  386. rc_uint_type r = 0;
  387. if (unicode)
  388. while (unicode[r] != 0)
  389. r++;
  390. else
  391. --r;
  392. return r;
  393. }
  394. unichar *
  395. unichar_dup (const unichar *unicode)
  396. {
  397. unichar *r;
  398. int len;
  399. if (! unicode)
  400. return NULL;
  401. for (len = 0; unicode[len] != 0; ++len)
  402. ;
  403. ++len;
  404. r = ((unichar *) res_alloc (len * sizeof (unichar)));
  405. memcpy (r, unicode, len * sizeof (unichar));
  406. return r;
  407. }
  408. unichar *
  409. unichar_dup_uppercase (const unichar *u)
  410. {
  411. unichar *r = unichar_dup (u);
  412. int i;
  413. if (! r)
  414. return NULL;
  415. for (i = 0; r[i] != 0; ++i)
  416. {
  417. if (r[i] >= 'a' && r[i] <= 'z')
  418. r[i] &= 0xdf;
  419. }
  420. return r;
  421. }
  422. static int
  423. unichar_isascii (const unichar *u, rc_uint_type len)
  424. {
  425. rc_uint_type i;
  426. if ((bfd_signed_vma) len < 0)
  427. {
  428. if (u)
  429. len = (rc_uint_type) unichar_len (u);
  430. else
  431. len = 0;
  432. }
  433. for (i = 0; i < len; i++)
  434. if ((u[i] & 0xff80) != 0)
  435. return 0;
  436. return 1;
  437. }
  438. void
  439. unicode_print_quoted (FILE *e, const unichar *u, rc_uint_type len)
  440. {
  441. if (! unichar_isascii (u, len))
  442. fputc ('L', e);
  443. fputc ('"', e);
  444. unicode_print (e, u, len);
  445. fputc ('"', e);
  446. }
  447. int
  448. unicode_is_valid_codepage (rc_uint_type cp)
  449. {
  450. if ((cp & 0xffff) != cp)
  451. return 0;
  452. if (cp == CP_UTF16 || cp == CP_ACP)
  453. return 1;
  454. #if !defined (_WIN32) && !defined (__CYGWIN__)
  455. if (! wind_find_codepage_info (cp))
  456. return 0;
  457. return 1;
  458. #else
  459. return !! IsValidCodePage ((UINT) cp);
  460. #endif
  461. }
  462. #if defined (_WIN32) || defined (__CYGWIN__)
  463. #define max_cp_string_len 6
  464. static unsigned int
  465. codepage_from_langid (unsigned short langid)
  466. {
  467. char cp_string [max_cp_string_len];
  468. int c;
  469. memset (cp_string, 0, max_cp_string_len);
  470. /* LOCALE_RETURN_NUMBER flag would avoid strtoul conversion,
  471. but is unavailable on Win95. */
  472. c = GetLocaleInfoA (MAKELCID (langid, SORT_DEFAULT),
  473. LOCALE_IDEFAULTANSICODEPAGE,
  474. cp_string, max_cp_string_len);
  475. /* If codepage data for an LCID is not installed on users's system,
  476. GetLocaleInfo returns an empty string. Fall back to system ANSI
  477. default. */
  478. if (c == 0)
  479. return CP_ACP;
  480. return strtoul (cp_string, 0, 10);
  481. }
  482. static unsigned int
  483. wincodepage_from_langid (unsigned short langid)
  484. {
  485. char cp_string [max_cp_string_len];
  486. int c;
  487. memset (cp_string, 0, max_cp_string_len);
  488. /* LOCALE_RETURN_NUMBER flag would avoid strtoul conversion,
  489. but is unavailable on Win95. */
  490. c = GetLocaleInfoA (MAKELCID (langid, SORT_DEFAULT),
  491. LOCALE_IDEFAULTCODEPAGE,
  492. cp_string, max_cp_string_len);
  493. /* If codepage data for an LCID is not installed on users's system,
  494. GetLocaleInfo returns an empty string. Fall back to system ANSI
  495. default. */
  496. if (c == 0)
  497. return CP_OEM;
  498. return strtoul (cp_string, 0, 10);
  499. }
  500. static char *
  501. lang_from_langid (unsigned short langid)
  502. {
  503. char cp_string[261];
  504. int c;
  505. memset (cp_string, 0, 261);
  506. c = GetLocaleInfoA (MAKELCID (langid, SORT_DEFAULT),
  507. LOCALE_SENGLANGUAGE,
  508. cp_string, 260);
  509. /* If codepage data for an LCID is not installed on users's system,
  510. GetLocaleInfo returns an empty string. Fall back to system ANSI
  511. default. */
  512. if (c == 0)
  513. strcpy (cp_string, "Neutral");
  514. return xstrdup (cp_string);
  515. }
  516. static char *
  517. country_from_langid (unsigned short langid)
  518. {
  519. char cp_string[261];
  520. int c;
  521. memset (cp_string, 0, 261);
  522. c = GetLocaleInfoA (MAKELCID (langid, SORT_DEFAULT),
  523. LOCALE_SENGCOUNTRY,
  524. cp_string, 260);
  525. /* If codepage data for an LCID is not installed on users's system,
  526. GetLocaleInfo returns an empty string. Fall back to system ANSI
  527. default. */
  528. if (c == 0)
  529. strcpy (cp_string, "Neutral");
  530. return xstrdup (cp_string);
  531. }
  532. #endif
  533. const wind_language_t *
  534. wind_find_language_by_id (unsigned id)
  535. {
  536. #if !defined (_WIN32) && !defined (__CYGWIN__)
  537. int i;
  538. if (! id)
  539. return NULL;
  540. for (i = 0; languages[i].id != (unsigned) -1 && languages[i].id != id; i++)
  541. ;
  542. if (languages[i].id == id)
  543. return &languages[i];
  544. return NULL;
  545. #else
  546. static wind_language_t wl;
  547. wl.id = id;
  548. wl.doscp = codepage_from_langid ((unsigned short) id);
  549. wl.wincp = wincodepage_from_langid ((unsigned short) id);
  550. wl.name = lang_from_langid ((unsigned short) id);
  551. wl.country = country_from_langid ((unsigned short) id);
  552. return & wl;
  553. #endif
  554. }
  555. const local_iconv_map *
  556. wind_find_codepage_info (unsigned cp)
  557. {
  558. #if !defined (_WIN32) && !defined (__CYGWIN__)
  559. int i;
  560. for (i = 0; codepages[i].codepage != (rc_uint_type) -1 && codepages[i].codepage != cp; i++)
  561. ;
  562. if (codepages[i].codepage == (rc_uint_type) -1)
  563. return NULL;
  564. return &codepages[i];
  565. #else
  566. static local_iconv_map lim;
  567. if (!unicode_is_valid_codepage (cp))
  568. return NULL;
  569. lim.codepage = cp;
  570. lim.iconv_name = "";
  571. return & lim;
  572. #endif
  573. }
  574. /* Convert an Codepage string to a unicode string. */
  575. void
  576. unicode_from_codepage (rc_uint_type *length, unichar **u, const char *src, rc_uint_type cp)
  577. {
  578. rc_uint_type len;
  579. len = wind_MultiByteToWideChar (cp, src, NULL, 0);
  580. if (len)
  581. {
  582. *u = ((unichar *) res_alloc (len));
  583. wind_MultiByteToWideChar (cp, src, *u, len);
  584. }
  585. /* Discount the trailing '/0'. If MultiByteToWideChar failed,
  586. this will set *length to -1. */
  587. len -= sizeof (unichar);
  588. if (length != NULL)
  589. *length = len / sizeof (unichar);
  590. }
  591. /* Convert an unicode string to an codepage string. */
  592. void
  593. codepage_from_unicode (rc_uint_type *length, const unichar *unicode, char **ascii, rc_uint_type cp)
  594. {
  595. rc_uint_type len;
  596. len = wind_WideCharToMultiByte (cp, unicode, NULL, 0);
  597. if (len)
  598. {
  599. *ascii = (char *) res_alloc (len * sizeof (char));
  600. wind_WideCharToMultiByte (cp, unicode, *ascii, len);
  601. }
  602. /* Discount the trailing '/0'. If MultiByteToWideChar failed,
  603. this will set *length to -1. */
  604. len--;
  605. if (length != NULL)
  606. *length = len;
  607. }
  608. #if defined (HAVE_ICONV) && !defined (_WIN32) && !defined (__CYGWIN__)
  609. static int
  610. iconv_onechar (iconv_t cd, ICONV_CONST char *s, char *d, int d_len, const char **n_s, char **n_d)
  611. {
  612. int i;
  613. for (i = 1; i <= 32; i++)
  614. {
  615. char *tmp_d = d;
  616. ICONV_CONST char *tmp_s = s;
  617. size_t ret;
  618. size_t s_left = (size_t) i;
  619. size_t d_left = (size_t) d_len;
  620. ret = iconv (cd, & tmp_s, & s_left, & tmp_d, & d_left);
  621. if (ret != (size_t) -1)
  622. {
  623. *n_s = tmp_s;
  624. *n_d = tmp_d;
  625. return 0;
  626. }
  627. }
  628. return 1;
  629. }
  630. static const char *
  631. wind_iconv_cp (rc_uint_type cp)
  632. {
  633. const local_iconv_map *lim = wind_find_codepage_info (cp);
  634. if (!lim)
  635. return NULL;
  636. return lim->iconv_name;
  637. }
  638. #endif /* HAVE_ICONV */
  639. static rc_uint_type
  640. wind_MultiByteToWideChar (rc_uint_type cp, const char *mb,
  641. unichar *u, rc_uint_type u_len)
  642. {
  643. rc_uint_type ret = 0;
  644. #if defined (_WIN32) || defined (__CYGWIN__)
  645. rc_uint_type conv_flags = MB_PRECOMPOSED;
  646. /* MB_PRECOMPOSED is not allowed for UTF-7 or UTF-8.
  647. MultiByteToWideChar will set the last error to
  648. ERROR_INVALID_FLAGS if we do. */
  649. if (cp == CP_UTF8 || cp == CP_UTF7)
  650. conv_flags = 0;
  651. ret = (rc_uint_type) MultiByteToWideChar (cp, conv_flags,
  652. mb, -1, u, u_len);
  653. /* Convert to bytes. */
  654. ret *= sizeof (unichar);
  655. #elif defined (HAVE_ICONV)
  656. int first = 1;
  657. char tmp[32];
  658. char *p_tmp;
  659. const char *iconv_name = wind_iconv_cp (cp);
  660. if (!mb || !iconv_name)
  661. return 0;
  662. iconv_t cd = iconv_open ("UTF-16LE", iconv_name);
  663. while (1)
  664. {
  665. int iret;
  666. const char *n_mb = "";
  667. char *n_tmp = "";
  668. p_tmp = tmp;
  669. iret = iconv_onechar (cd, (ICONV_CONST char *) mb, p_tmp, 32, & n_mb, & n_tmp);
  670. if (first)
  671. {
  672. first = 0;
  673. continue;
  674. }
  675. if (!iret)
  676. {
  677. size_t l_tmp = (size_t) (n_tmp - p_tmp);
  678. if (u)
  679. {
  680. if ((size_t) u_len < l_tmp)
  681. break;
  682. memcpy (u, tmp, l_tmp);
  683. u += l_tmp/2;
  684. u_len -= l_tmp;
  685. }
  686. ret += l_tmp;
  687. }
  688. else
  689. break;
  690. if (tmp[0] == 0 && tmp[1] == 0)
  691. break;
  692. mb = n_mb;
  693. }
  694. iconv_close (cd);
  695. #else
  696. if (cp)
  697. ret = 0;
  698. ret = strlen (mb) + 1;
  699. ret *= sizeof (unichar);
  700. if (u != NULL && u_len != 0)
  701. {
  702. do
  703. {
  704. *u++ = ((unichar) *mb) & 0xff;
  705. --u_len; mb++;
  706. }
  707. while (u_len != 0 && mb[-1] != 0);
  708. }
  709. if (u != NULL && u_len != 0)
  710. *u = 0;
  711. #endif
  712. return ret;
  713. }
  714. static rc_uint_type
  715. wind_WideCharToMultiByte (rc_uint_type cp, const unichar *u, char *mb, rc_uint_type mb_len)
  716. {
  717. rc_uint_type ret = 0;
  718. #if defined (_WIN32) || defined (__CYGWIN__)
  719. WINBOOL used_def = false;
  720. ret = (rc_uint_type) WideCharToMultiByte (cp, 0, u, -1, mb, mb_len,
  721. NULL, & used_def);
  722. #elif defined (HAVE_ICONV)
  723. int first = 1;
  724. char tmp[32];
  725. char *p_tmp;
  726. const char *iconv_name = wind_iconv_cp (cp);
  727. if (!u || !iconv_name)
  728. return 0;
  729. iconv_t cd = iconv_open (iconv_name, "UTF-16LE");
  730. while (1)
  731. {
  732. int iret;
  733. const char *n_u = "";
  734. char *n_tmp = "";
  735. p_tmp = tmp;
  736. iret = iconv_onechar (cd, (ICONV_CONST char *) u, p_tmp, 32, &n_u, & n_tmp);
  737. if (first)
  738. {
  739. first = 0;
  740. continue;
  741. }
  742. if (!iret)
  743. {
  744. size_t l_tmp = (size_t) (n_tmp - p_tmp);
  745. if (mb)
  746. {
  747. if ((size_t) mb_len < l_tmp)
  748. break;
  749. memcpy (mb, tmp, l_tmp);
  750. mb += l_tmp;
  751. mb_len -= l_tmp;
  752. }
  753. ret += l_tmp;
  754. }
  755. else
  756. break;
  757. if (u[0] == 0)
  758. break;
  759. u = (const unichar *) n_u;
  760. }
  761. iconv_close (cd);
  762. #else
  763. if (cp)
  764. ret = 0;
  765. while (u[ret] != 0)
  766. ++ret;
  767. ++ret;
  768. if (mb)
  769. {
  770. while (*u != 0 && mb_len != 0)
  771. {
  772. if (u[0] == (u[0] & 0x7f))
  773. *mb++ = (char) u[0];
  774. else
  775. *mb++ = '_';
  776. ++u; --mb_len;
  777. }
  778. if (mb_len != 0)
  779. *mb = 0;
  780. }
  781. #endif
  782. return ret;
  783. }