utils.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. /*
  2. * Utility routines
  3. *
  4. * Copyright 1998 Bertho A. Stultiens
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  19. */
  20. #include "config.h"
  21. #include <assert.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <stdarg.h>
  25. #include <string.h>
  26. #include <ctype.h>
  27. #include "../tools.h"
  28. #include "wrc.h"
  29. #include "utils.h"
  30. #include "parser.h"
  31. /* #define WANT_NEAR_INDICATION */
  32. #ifdef WANT_NEAR_INDICATION
  33. void make_print(char *str)
  34. {
  35. while(*str)
  36. {
  37. if(!isprint(*str))
  38. *str = ' ';
  39. str++;
  40. }
  41. }
  42. #endif
  43. static void generic_msg(const char *s, const char *t, const char *n, va_list ap)
  44. {
  45. fprintf(stderr, "%s:%d:%d: %s: ", input_name ? input_name : "stdin", line_number, char_number, t);
  46. vfprintf(stderr, s, ap);
  47. #ifdef WANT_NEAR_INDICATION
  48. {
  49. char *cpy;
  50. if(n)
  51. {
  52. cpy = xstrdup(n);
  53. make_print(cpy);
  54. fprintf(stderr, " near '%s'", cpy);
  55. free(cpy);
  56. }
  57. }
  58. #endif
  59. }
  60. int parser_error(const char *s, ...)
  61. {
  62. va_list ap;
  63. va_start(ap, s);
  64. generic_msg(s, "Error", parser_text, ap);
  65. fputc( '\n', stderr );
  66. va_end(ap);
  67. exit(1);
  68. return 1;
  69. }
  70. int parser_warning(const char *s, ...)
  71. {
  72. va_list ap;
  73. va_start(ap, s);
  74. generic_msg(s, "Warning", parser_text, ap);
  75. va_end(ap);
  76. return 0;
  77. }
  78. void fatal_perror( const char *msg, ... )
  79. {
  80. va_list valist;
  81. va_start( valist, msg );
  82. fprintf(stderr, "Error: ");
  83. vfprintf( stderr, msg, valist );
  84. perror( " " );
  85. va_end( valist );
  86. exit(2);
  87. }
  88. void error(const char *s, ...)
  89. {
  90. va_list ap;
  91. va_start(ap, s);
  92. fprintf(stderr, "Error: ");
  93. vfprintf(stderr, s, ap);
  94. va_end(ap);
  95. exit(2);
  96. }
  97. void warning(const char *s, ...)
  98. {
  99. va_list ap;
  100. va_start(ap, s);
  101. fprintf(stderr, "Warning: ");
  102. vfprintf(stderr, s, ap);
  103. va_end(ap);
  104. }
  105. void chat(const char *s, ...)
  106. {
  107. if(debuglevel & DEBUGLEVEL_CHAT)
  108. {
  109. va_list ap;
  110. va_start(ap, s);
  111. fprintf(stderr, "FYI: ");
  112. vfprintf(stderr, s, ap);
  113. va_end(ap);
  114. }
  115. }
  116. int compare_striA( const char *str1, const char *str2 )
  117. {
  118. for (;;)
  119. {
  120. /* only the A-Z range is case-insensitive */
  121. char ch1 = (*str1 >= 'a' && *str1 <= 'z') ? *str1 + 'A' - 'a' : *str1;
  122. char ch2 = (*str2 >= 'a' && *str2 <= 'z') ? *str2 + 'A' - 'a' : *str2;
  123. if (!ch1 || ch1 != ch2) return ch1 - ch2;
  124. str1++;
  125. str2++;
  126. }
  127. }
  128. int compare_striW( const WCHAR *str1, const WCHAR *str2 )
  129. {
  130. for (;;)
  131. {
  132. /* only the A-Z range is case-insensitive */
  133. WCHAR ch1 = (*str1 >= 'a' && *str1 <= 'z') ? *str1 + 'A' - 'a' : *str1;
  134. WCHAR ch2 = (*str2 >= 'a' && *str2 <= 'z') ? *str2 + 'A' - 'a' : *str2;
  135. if (!ch1 || ch1 != ch2) return ch1 - ch2;
  136. str1++;
  137. str2++;
  138. }
  139. }
  140. /*
  141. *****************************************************************************
  142. * Function : compare_name_id
  143. * Syntax : int compare_name_id(const name_id_t *n1, const name_id_t *n2)
  144. * Input :
  145. * Output :
  146. * Description :
  147. * Remarks :
  148. *****************************************************************************
  149. */
  150. int compare_name_id(const name_id_t *n1, const name_id_t *n2)
  151. {
  152. switch (n1->type)
  153. {
  154. case name_ord:
  155. if (n2->type == name_ord) return n1->name.i_name - n2->name.i_name;
  156. return 1;
  157. case name_str:
  158. if (n2->type == name_str)
  159. {
  160. if(n1->name.s_name->type == str_char
  161. && n2->name.s_name->type == str_char)
  162. {
  163. return compare_striA(n1->name.s_name->str.cstr, n2->name.s_name->str.cstr);
  164. }
  165. else
  166. {
  167. assert( n1->name.s_name->type == str_unicode );
  168. assert( n2->name.s_name->type == str_unicode );
  169. return compare_striW(n1->name.s_name->str.wstr, n2->name.s_name->str.wstr);
  170. }
  171. }
  172. return -1;
  173. }
  174. return 0; /* Keep the compiler happy */
  175. }
  176. #ifdef _WIN32
  177. int is_valid_codepage(int id)
  178. {
  179. return IsValidCodePage( id );
  180. }
  181. static WCHAR *codepage_to_unicode( int codepage, const char *src, int srclen, int *dstlen )
  182. {
  183. WCHAR *dst = xmalloc( (srclen + 1) * sizeof(WCHAR) );
  184. DWORD ret = MultiByteToWideChar( codepage, MB_ERR_INVALID_CHARS, src, srclen, dst, srclen );
  185. if (!ret) return NULL;
  186. dst[ret] = 0;
  187. *dstlen = ret;
  188. return dst;
  189. }
  190. #else /* _WIN32 */
  191. struct nls_info
  192. {
  193. unsigned short codepage;
  194. unsigned short unidef;
  195. unsigned short trans_unidef;
  196. unsigned short *cp2uni;
  197. unsigned short *dbcs_offsets;
  198. };
  199. static struct nls_info nlsinfo[128];
  200. static void init_nls_info( struct nls_info *info, unsigned short *ptr )
  201. {
  202. unsigned short hdr_size = ptr[0];
  203. info->codepage = ptr[1];
  204. info->unidef = ptr[4];
  205. info->trans_unidef = ptr[6];
  206. ptr += hdr_size;
  207. info->cp2uni = ++ptr;
  208. ptr += 256;
  209. if (*ptr++) ptr += 256; /* glyph table */
  210. info->dbcs_offsets = *ptr ? ptr + 1 : NULL;
  211. }
  212. static const struct nls_info *get_nls_info( unsigned int codepage )
  213. {
  214. struct stat st;
  215. unsigned short *data;
  216. char *path;
  217. unsigned int i;
  218. int fd;
  219. for (i = 0; i < ARRAY_SIZE(nlsinfo) && nlsinfo[i].codepage; i++)
  220. if (nlsinfo[i].codepage == codepage) return &nlsinfo[i];
  221. assert( i < ARRAY_SIZE(nlsinfo) );
  222. for (i = 0; nlsdirs[i]; i++)
  223. {
  224. path = strmake( "%s/c_%03u.nls", nlsdirs[i], codepage );
  225. if ((fd = open( path, O_RDONLY )) != -1) break;
  226. free( path );
  227. }
  228. if (!nlsdirs[i]) return NULL;
  229. fstat( fd, &st );
  230. data = xmalloc( st.st_size );
  231. if (read( fd, data, st.st_size ) != st.st_size) error( "failed to load %s\n", path );
  232. close( fd );
  233. free( path );
  234. init_nls_info( &nlsinfo[i], data );
  235. return &nlsinfo[i];
  236. }
  237. int is_valid_codepage(int cp)
  238. {
  239. return cp == CP_UTF8 || get_nls_info( cp );
  240. }
  241. static WCHAR *codepage_to_unicode( int codepage, const char *src, int srclen, int *dstlen )
  242. {
  243. const struct nls_info *info = get_nls_info( codepage );
  244. unsigned int i;
  245. WCHAR dbch, *dst = xmalloc( (srclen + 1) * sizeof(WCHAR) );
  246. if (!info) error( "codepage %u not supported\n", codepage );
  247. if (info->dbcs_offsets)
  248. {
  249. for (i = 0; srclen; i++, srclen--, src++)
  250. {
  251. unsigned short off = info->dbcs_offsets[(unsigned char)*src];
  252. if (off)
  253. {
  254. if (srclen == 1) return NULL;
  255. dbch = (src[0] << 8) | (unsigned char)src[1];
  256. src++;
  257. srclen--;
  258. dst[i] = info->dbcs_offsets[off + (unsigned char)*src];
  259. if (dst[i] == info->unidef && dbch != info->trans_unidef) return NULL;
  260. }
  261. else
  262. {
  263. dst[i] = info->cp2uni[(unsigned char)*src];
  264. if (dst[i] == info->unidef && *src != info->trans_unidef) return NULL;
  265. }
  266. }
  267. }
  268. else
  269. {
  270. for (i = 0; i < srclen; i++)
  271. {
  272. dst[i] = info->cp2uni[(unsigned char)src[i]];
  273. if (dst[i] == info->unidef && src[i] != info->trans_unidef) return NULL;
  274. }
  275. }
  276. dst[i] = 0;
  277. *dstlen = i;
  278. return dst;
  279. }
  280. #endif /* _WIN32 */
  281. static WCHAR *utf8_to_unicode( const char *src, int srclen, int *dstlen )
  282. {
  283. static const char utf8_length[128] =
  284. {
  285. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x80-0x8f */
  286. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x90-0x9f */
  287. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xa0-0xaf */
  288. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xb0-0xbf */
  289. 0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0xc0-0xcf */
  290. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0xd0-0xdf */
  291. 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, /* 0xe0-0xef */
  292. 3,3,3,3,3,0,0,0,0,0,0,0,0,0,0,0 /* 0xf0-0xff */
  293. };
  294. static const unsigned char utf8_mask[4] = { 0x7f, 0x1f, 0x0f, 0x07 };
  295. const char *srcend = src + srclen;
  296. int len, res;
  297. WCHAR *ret, *dst;
  298. dst = ret = xmalloc( (srclen + 1) * sizeof(WCHAR) );
  299. while (src < srcend)
  300. {
  301. unsigned char ch = *src++;
  302. if (ch < 0x80) /* special fast case for 7-bit ASCII */
  303. {
  304. *dst++ = ch;
  305. continue;
  306. }
  307. len = utf8_length[ch - 0x80];
  308. if (len && src + len <= srcend)
  309. {
  310. res = ch & utf8_mask[len];
  311. switch (len)
  312. {
  313. case 3:
  314. if ((ch = *src ^ 0x80) >= 0x40) break;
  315. res = (res << 6) | ch;
  316. src++;
  317. if (res < 0x10) break;
  318. case 2:
  319. if ((ch = *src ^ 0x80) >= 0x40) break;
  320. res = (res << 6) | ch;
  321. if (res >= 0x110000 >> 6) break;
  322. src++;
  323. if (res < 0x20) break;
  324. if (res >= 0xd800 >> 6 && res <= 0xdfff >> 6) break;
  325. case 1:
  326. if ((ch = *src ^ 0x80) >= 0x40) break;
  327. res = (res << 6) | ch;
  328. src++;
  329. if (res < 0x80) break;
  330. if (res <= 0xffff) *dst++ = res;
  331. else
  332. {
  333. res -= 0x10000;
  334. *dst++ = 0xd800 | (res >> 10);
  335. *dst++ = 0xdc00 | (res & 0x3ff);
  336. }
  337. continue;
  338. }
  339. }
  340. *dst++ = 0xfffd;
  341. }
  342. *dst = 0;
  343. *dstlen = dst - ret;
  344. return ret;
  345. }
  346. static char *unicode_to_utf8( const WCHAR *src, int srclen, int *dstlen )
  347. {
  348. char *ret, *dst;
  349. dst = ret = xmalloc( srclen * 3 + 1 );
  350. for ( ; srclen; srclen--, src++)
  351. {
  352. unsigned int ch = *src;
  353. if (ch < 0x80) /* 0x00-0x7f: 1 byte */
  354. {
  355. *dst++ = ch;
  356. continue;
  357. }
  358. if (ch < 0x800) /* 0x80-0x7ff: 2 bytes */
  359. {
  360. dst[1] = 0x80 | (ch & 0x3f);
  361. ch >>= 6;
  362. dst[0] = 0xc0 | ch;
  363. dst += 2;
  364. continue;
  365. }
  366. if (ch >= 0xd800 && ch <= 0xdbff && srclen > 1 && src[1] >= 0xdc00 && src[1] <= 0xdfff)
  367. {
  368. /* 0x10000-0x10ffff: 4 bytes */
  369. ch = 0x10000 + ((ch & 0x3ff) << 10) + (src[1] & 0x3ff);
  370. dst[3] = 0x80 | (ch & 0x3f);
  371. ch >>= 6;
  372. dst[2] = 0x80 | (ch & 0x3f);
  373. ch >>= 6;
  374. dst[1] = 0x80 | (ch & 0x3f);
  375. ch >>= 6;
  376. dst[0] = 0xf0 | ch;
  377. dst += 4;
  378. src++;
  379. srclen--;
  380. continue;
  381. }
  382. if (ch >= 0xd800 && ch <= 0xdfff) ch = 0xfffd; /* invalid surrogate pair */
  383. /* 0x800-0xffff: 3 bytes */
  384. dst[2] = 0x80 | (ch & 0x3f);
  385. ch >>= 6;
  386. dst[1] = 0x80 | (ch & 0x3f);
  387. ch >>= 6;
  388. dst[0] = 0xe0 | ch;
  389. dst += 3;
  390. }
  391. *dst = 0;
  392. *dstlen = dst - ret;
  393. return ret;
  394. }
  395. string_t *convert_string_unicode( const string_t *str, int codepage )
  396. {
  397. string_t *ret = xmalloc(sizeof(*ret));
  398. ret->type = str_unicode;
  399. ret->loc = str->loc;
  400. if (str->type == str_char)
  401. {
  402. if (!codepage) parser_error( "Current language is Unicode only, cannot convert string" );
  403. if (codepage == CP_UTF8)
  404. ret->str.wstr = utf8_to_unicode( str->str.cstr, str->size, &ret->size );
  405. else
  406. ret->str.wstr = codepage_to_unicode( codepage, str->str.cstr, str->size, &ret->size );
  407. if (!ret->str.wstr) parser_error( "Invalid character in string '%.*s' for codepage %u",
  408. str->size, str->str.cstr, codepage );
  409. }
  410. else
  411. {
  412. ret->size = str->size;
  413. ret->str.wstr = xmalloc(sizeof(WCHAR)*(ret->size+1));
  414. memcpy( ret->str.wstr, str->str.wstr, ret->size * sizeof(WCHAR) );
  415. ret->str.wstr[ret->size] = 0;
  416. }
  417. return ret;
  418. }
  419. char *convert_string_utf8( const string_t *str, int codepage )
  420. {
  421. int len;
  422. string_t *wstr = convert_string_unicode( str, codepage );
  423. char *ret = unicode_to_utf8( wstr->str.wstr, wstr->size, &len );
  424. free_string( wstr );
  425. return ret;
  426. }
  427. void free_string(string_t *str)
  428. {
  429. if (str->type == str_unicode) free( str->str.wstr );
  430. else free( str->str.cstr );
  431. free( str );
  432. }
  433. /* check if the string is valid utf8 despite a different codepage being in use */
  434. int check_valid_utf8( const string_t *str, int codepage )
  435. {
  436. int i, count;
  437. WCHAR *wstr;
  438. if (!check_utf8) return 0;
  439. if (!codepage) return 0;
  440. if (codepage == CP_UTF8) return 0;
  441. if (!is_valid_codepage( codepage )) return 0;
  442. for (i = count = 0; i < str->size; i++)
  443. {
  444. if ((unsigned char)str->str.cstr[i] >= 0xf5) goto done;
  445. if ((unsigned char)str->str.cstr[i] >= 0xc2) { count++; continue; }
  446. if ((unsigned char)str->str.cstr[i] >= 0x80) goto done;
  447. }
  448. if (!count) return 0; /* no 8-bit chars at all */
  449. wstr = utf8_to_unicode( str->str.cstr, str->size, &count );
  450. for (i = 0; i < count; i++) if (wstr[i] == 0xfffd) break;
  451. free( wstr );
  452. return (i == count);
  453. done:
  454. check_utf8 = 0; /* at least one 8-bit non-utf8 string found, stop checking */
  455. return 0;
  456. }
  457. struct lang2cp
  458. {
  459. unsigned short lang;
  460. unsigned short sublang;
  461. unsigned int cp;
  462. };
  463. /* language to codepage conversion table */
  464. /* specific sublanguages need only be specified if their codepage */
  465. /* differs from the default (SUBLANG_NEUTRAL) */
  466. static const struct lang2cp lang2cps[] =
  467. {
  468. { LANG_AFRIKAANS, SUBLANG_NEUTRAL, 1252 },
  469. { LANG_ALBANIAN, SUBLANG_NEUTRAL, 1250 },
  470. { LANG_ALSATIAN, SUBLANG_NEUTRAL, 1252 },
  471. { LANG_AMHARIC, SUBLANG_NEUTRAL, 0 },
  472. { LANG_ARABIC, SUBLANG_NEUTRAL, 1256 },
  473. { LANG_ARMENIAN, SUBLANG_NEUTRAL, 0 },
  474. { LANG_ASSAMESE, SUBLANG_NEUTRAL, 0 },
  475. { LANG_ASTURIAN, SUBLANG_NEUTRAL, 1252 },
  476. { LANG_AZERI, SUBLANG_NEUTRAL, 1254 },
  477. { LANG_AZERI, SUBLANG_AZERI_CYRILLIC, 1251 },
  478. { LANG_BASHKIR, SUBLANG_NEUTRAL, 1251 },
  479. { LANG_BASQUE, SUBLANG_NEUTRAL, 1252 },
  480. { LANG_BELARUSIAN, SUBLANG_NEUTRAL, 1251 },
  481. { LANG_BENGALI, SUBLANG_NEUTRAL, 0 },
  482. { LANG_BOSNIAN, SUBLANG_NEUTRAL, 1250 },
  483. { LANG_BOSNIAN, SUBLANG_BOSNIAN_BOSNIA_HERZEGOVINA_CYRILLIC, 1251 },
  484. { LANG_BRETON, SUBLANG_NEUTRAL, 1252 },
  485. { LANG_BULGARIAN, SUBLANG_NEUTRAL, 1251 },
  486. { LANG_CATALAN, SUBLANG_NEUTRAL, 1252 },
  487. { LANG_CHINESE, SUBLANG_NEUTRAL, 950 },
  488. { LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED, 936 },
  489. { LANG_CHINESE, SUBLANG_CHINESE_SINGAPORE, 936 },
  490. #ifdef LANG_CORNISH
  491. { LANG_CORNISH, SUBLANG_NEUTRAL, 1252 },
  492. #endif /* LANG_CORNISH */
  493. { LANG_CORSICAN, SUBLANG_NEUTRAL, 1252 },
  494. { LANG_CROATIAN, SUBLANG_NEUTRAL, 1250 },
  495. { LANG_CZECH, SUBLANG_NEUTRAL, 1250 },
  496. { LANG_DANISH, SUBLANG_NEUTRAL, 1252 },
  497. { LANG_DARI, SUBLANG_NEUTRAL, 1256 },
  498. { LANG_DIVEHI, SUBLANG_NEUTRAL, 0 },
  499. { LANG_DUTCH, SUBLANG_NEUTRAL, 1252 },
  500. { LANG_ENGLISH, SUBLANG_NEUTRAL, 1252 },
  501. #ifdef LANG_ESPERANTO
  502. { LANG_ESPERANTO, SUBLANG_NEUTRAL, 1252 },
  503. #endif /* LANG_ESPERANTO */
  504. { LANG_ESTONIAN, SUBLANG_NEUTRAL, 1257 },
  505. { LANG_FAEROESE, SUBLANG_NEUTRAL, 1252 },
  506. { LANG_FILIPINO, SUBLANG_NEUTRAL, 1252 },
  507. { LANG_FINNISH, SUBLANG_NEUTRAL, 1252 },
  508. { LANG_FRENCH, SUBLANG_NEUTRAL, 1252 },
  509. { LANG_FRISIAN, SUBLANG_NEUTRAL, 1252 },
  510. #ifdef LANG_MANX_GAELIC
  511. { LANG_MANX_GAELIC, SUBLANG_NEUTRAL, 1252 },
  512. #endif /* LANG_MANX_GAELIC */
  513. { LANG_GALICIAN, SUBLANG_NEUTRAL, 1252 },
  514. { LANG_GEORGIAN, SUBLANG_NEUTRAL, 0 },
  515. { LANG_GERMAN, SUBLANG_NEUTRAL, 1252 },
  516. { LANG_GREEK, SUBLANG_NEUTRAL, 1253 },
  517. { LANG_GREENLANDIC, SUBLANG_NEUTRAL, 1252 },
  518. { LANG_GUJARATI, SUBLANG_NEUTRAL, 0 },
  519. { LANG_HAUSA, SUBLANG_NEUTRAL, 1252 },
  520. { LANG_HEBREW, SUBLANG_NEUTRAL, 1255 },
  521. { LANG_HINDI, SUBLANG_NEUTRAL, 0 },
  522. { LANG_HUNGARIAN, SUBLANG_NEUTRAL, 1250 },
  523. { LANG_ICELANDIC, SUBLANG_NEUTRAL, 1252 },
  524. { LANG_IGBO, SUBLANG_NEUTRAL, 1252 },
  525. { LANG_INDONESIAN, SUBLANG_NEUTRAL, 1252 },
  526. { LANG_INUKTITUT, SUBLANG_NEUTRAL, 0 },
  527. { LANG_INUKTITUT, SUBLANG_INUKTITUT_CANADA_LATIN, 0 },
  528. { LANG_INVARIANT, SUBLANG_NEUTRAL, 0 },
  529. { LANG_IRISH, SUBLANG_NEUTRAL, 1252 },
  530. { LANG_ITALIAN, SUBLANG_NEUTRAL, 1252 },
  531. { LANG_JAPANESE, SUBLANG_NEUTRAL, 932 },
  532. { LANG_KANNADA, SUBLANG_NEUTRAL, 0 },
  533. { LANG_KAZAK, SUBLANG_NEUTRAL, 1251 },
  534. { LANG_KHMER, SUBLANG_NEUTRAL, 0 },
  535. { LANG_KICHE, SUBLANG_NEUTRAL, 1252 },
  536. { LANG_KINYARWANDA, SUBLANG_NEUTRAL, 1252 },
  537. { LANG_KONKANI, SUBLANG_NEUTRAL, 0 },
  538. { LANG_KOREAN, SUBLANG_NEUTRAL, 949 },
  539. { LANG_KYRGYZ, SUBLANG_NEUTRAL, 1251 },
  540. { LANG_LAO, SUBLANG_NEUTRAL, 0 },
  541. { LANG_LATVIAN, SUBLANG_NEUTRAL, 1257 },
  542. { LANG_LITHUANIAN, SUBLANG_NEUTRAL, 1257 },
  543. { LANG_LOWER_SORBIAN, SUBLANG_NEUTRAL, 1252 },
  544. { LANG_LUXEMBOURGISH, SUBLANG_NEUTRAL, 1252 },
  545. { LANG_MACEDONIAN, SUBLANG_NEUTRAL, 1251 },
  546. { LANG_MALAY, SUBLANG_NEUTRAL, 1252 },
  547. { LANG_MALAYALAM, SUBLANG_NEUTRAL, 0 },
  548. { LANG_MALTESE, SUBLANG_NEUTRAL, 0 },
  549. { LANG_MAORI, SUBLANG_NEUTRAL, 0 },
  550. { LANG_MAPUDUNGUN, SUBLANG_NEUTRAL, 1252 },
  551. { LANG_MARATHI, SUBLANG_NEUTRAL, 0 },
  552. { LANG_MOHAWK, SUBLANG_NEUTRAL, 1252 },
  553. { LANG_MONGOLIAN, SUBLANG_NEUTRAL, 1251 },
  554. { LANG_NEPALI, SUBLANG_NEUTRAL, 0 },
  555. { LANG_NEUTRAL, SUBLANG_NEUTRAL, 1252 },
  556. { LANG_NORWEGIAN, SUBLANG_NEUTRAL, 1252 },
  557. { LANG_OCCITAN, SUBLANG_NEUTRAL, 1252 },
  558. { LANG_ORIYA, SUBLANG_NEUTRAL, 0 },
  559. { LANG_PASHTO, SUBLANG_NEUTRAL, 0 },
  560. { LANG_PERSIAN, SUBLANG_NEUTRAL, 1256 },
  561. { LANG_POLISH, SUBLANG_NEUTRAL, 1250 },
  562. { LANG_PORTUGUESE, SUBLANG_NEUTRAL, 1252 },
  563. { LANG_PUNJABI, SUBLANG_NEUTRAL, 0 },
  564. { LANG_QUECHUA, SUBLANG_NEUTRAL, 1252 },
  565. { LANG_ROMANIAN, SUBLANG_NEUTRAL, 1250 },
  566. { LANG_ROMANSH, SUBLANG_NEUTRAL, 1252 },
  567. { LANG_RUSSIAN, SUBLANG_NEUTRAL, 1251 },
  568. { LANG_SAMI, SUBLANG_NEUTRAL, 1252 },
  569. { LANG_SANSKRIT, SUBLANG_NEUTRAL, 0 },
  570. { LANG_SCOTTISH_GAELIC,SUBLANG_NEUTRAL, 1252 },
  571. { LANG_SERBIAN, SUBLANG_NEUTRAL, 1250 },
  572. { LANG_SERBIAN, SUBLANG_SERBIAN_CYRILLIC, 1251 },
  573. { LANG_SINHALESE, SUBLANG_NEUTRAL, 0 },
  574. { LANG_SLOVAK, SUBLANG_NEUTRAL, 1250 },
  575. { LANG_SLOVENIAN, SUBLANG_NEUTRAL, 1250 },
  576. { LANG_SOTHO, SUBLANG_NEUTRAL, 1252 },
  577. { LANG_SPANISH, SUBLANG_NEUTRAL, 1252 },
  578. { LANG_SWAHILI, SUBLANG_NEUTRAL, 1252 },
  579. { LANG_SWEDISH, SUBLANG_NEUTRAL, 1252 },
  580. { LANG_SYRIAC, SUBLANG_NEUTRAL, 0 },
  581. { LANG_TAJIK, SUBLANG_NEUTRAL, 1251 },
  582. { LANG_TAMAZIGHT, SUBLANG_NEUTRAL, 1252 },
  583. { LANG_TAMIL, SUBLANG_NEUTRAL, 0 },
  584. { LANG_TATAR, SUBLANG_NEUTRAL, 1251 },
  585. { LANG_TELUGU, SUBLANG_NEUTRAL, 0 },
  586. { LANG_THAI, SUBLANG_NEUTRAL, 874 },
  587. { LANG_TIBETAN, SUBLANG_NEUTRAL, 0 },
  588. { LANG_TSWANA, SUBLANG_NEUTRAL, 1252 },
  589. { LANG_TURKISH, SUBLANG_NEUTRAL, 1254 },
  590. { LANG_TURKMEN, SUBLANG_NEUTRAL, 1250 },
  591. { LANG_UIGHUR, SUBLANG_NEUTRAL, 1256 },
  592. { LANG_UKRAINIAN, SUBLANG_NEUTRAL, 1251 },
  593. { LANG_UPPER_SORBIAN, SUBLANG_NEUTRAL, 1252 },
  594. { LANG_URDU, SUBLANG_NEUTRAL, 1256 },
  595. { LANG_UZBEK, SUBLANG_NEUTRAL, 1254 },
  596. { LANG_UZBEK, SUBLANG_UZBEK_CYRILLIC, 1251 },
  597. { LANG_VIETNAMESE, SUBLANG_NEUTRAL, 1258 },
  598. #ifdef LANG_WALON
  599. { LANG_WALON, SUBLANG_NEUTRAL, 1252 },
  600. #endif /* LANG_WALON */
  601. { LANG_WELSH, SUBLANG_NEUTRAL, 1252 },
  602. { LANG_WOLOF, SUBLANG_NEUTRAL, 1252 },
  603. { LANG_XHOSA, SUBLANG_NEUTRAL, 1252 },
  604. { LANG_YAKUT, SUBLANG_NEUTRAL, 1251 },
  605. { LANG_YI, SUBLANG_NEUTRAL, 0 },
  606. { LANG_YORUBA, SUBLANG_NEUTRAL, 1252 },
  607. { LANG_ZULU, SUBLANG_NEUTRAL, 1252 }
  608. };
  609. int get_language_codepage( unsigned short lang, unsigned short sublang )
  610. {
  611. unsigned int i;
  612. int cp = -1, defcp = -1;
  613. for (i = 0; i < ARRAY_SIZE(lang2cps); i++)
  614. {
  615. if (lang2cps[i].lang != lang) continue;
  616. if (lang2cps[i].sublang == sublang)
  617. {
  618. cp = lang2cps[i].cp;
  619. break;
  620. }
  621. if (lang2cps[i].sublang == SUBLANG_NEUTRAL) defcp = lang2cps[i].cp;
  622. }
  623. if (cp == -1) cp = defcp;
  624. return cp;
  625. }