gb18030.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /*
  2. * Copyright (C) 1999-2001, 2005 Free Software Foundation, Inc.
  3. * This file is part of the GNU LIBICONV Library.
  4. *
  5. * The GNU LIBICONV Library is free software; you can redistribute it
  6. * and/or modify it under the terms of the GNU Library General Public
  7. * License as published by the Free Software Foundation; either version 2
  8. * of the License, or (at your option) any later version.
  9. *
  10. * The GNU LIBICONV Library is distributed in the hope that it will be
  11. * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Library General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Library General Public
  16. * License along with the GNU LIBICONV Library; see the file COPYING.LIB.
  17. * If not, write to the Free Software Foundation, Inc., 51 Franklin Street,
  18. * Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. /*
  21. * GB18030
  22. */
  23. /*
  24. * GB18030, as specified in the GB18030 standard, is an extension of GBK.
  25. *
  26. * In what follows, page numbers refer to the GB18030 standard (second
  27. * printing).
  28. *
  29. *
  30. * It consists of the following parts:
  31. *
  32. * One-byte range:
  33. * ASCII p. 2 0x{00..7F}
  34. *
  35. * Two-byte range:
  36. * GBK part 1 p. 10..12 0x{A1..A9}{A1..FE}
  37. * GBK part 2 p. 13..36 0x{B0..F7}{A1..FE}
  38. * GBK part 3 p. 37..52 0x{81..A0}{40..7E,80..FE}
  39. * GBK part 4 p. 53..81 0x{AA..FE}{40..7E,80..A0}
  40. * GBK part 5 p. 82 0x{A8..A9}{40..7E,80..A0}
  41. * UDA part 1 p. 83..84 0x{AA..AF}{A1..FE} U+E000..U+E233
  42. * UDA part 2 p. 85..87 0x{F8..FE}{A1..FE} U+E234..U+E4C5
  43. * UDA part 3 p. 88..90 0x{A1..A7}{40..7E,80..A0} U+E4C6..U+E765
  44. *
  45. * Four-byte range:
  46. * BMP rest p. 94..283 0x{81..84}{30..39}{81..FE}{30..39}
  47. * rest of U+0080..U+FFFF
  48. * Planes 1-16 p. 5 0x{90..FE}{30..39}{81..FE}{30..39}
  49. * U+10000..U+10FFFF
  50. *
  51. * To GBK part 1 were added:
  52. * 1. 0xA2E3, 0xA8BF.
  53. * 2. Characters mapped to the Unicode PUA
  54. * 0xA2AB..0xA2B0 U+E766..U+E76B
  55. * 0xA2E4 U+E76D
  56. * 0xA2EF..0xA2F0 U+E76E..U+E76F
  57. * 0xA2FD..0xA2FE U+E770..U+E771
  58. * 0xA4F4..0xA4FE U+E772..U+E77C
  59. * 0xA5F7..0xA5FE U+E77D..U+E784
  60. * 0xA6B9..0xA6C0 U+E785..U+E78C
  61. * 0xA6D9..0xA6DF [glyphs here!!] U+E78D..U+E793
  62. * 0xA6EC..0xA6ED [glyphs here!!] U+E794..U+E795
  63. * 0xA6F3 [glyphs here!!] U+E796
  64. * 0xA6F6..0xA6FE U+E797..U+E79F
  65. * 0xA7C2..0xA7D0 U+E7A0..U+E7AE
  66. * 0xA7F2..0xA7FE U+E7AF..U+E7BB
  67. * 0xA8BC [glyphs here!!] U+E7C7
  68. * 0xA8C1..0xA8C4 U+E7C9..U+E7CC
  69. * 0xA8EA..0xA8FE U+E7CD..U+E7E1
  70. * 0xA9A1..0xA9A3 U+E7FE..U+E800
  71. * 0xA9F0..0xA9FE U+E801..U+E80F
  72. *
  73. * To GBK part 2 were added:
  74. * 3. Characters mapped to the Unicode PUA
  75. * 0xD7FA..0xD7FE U+E810..0xE814
  76. *
  77. * To GBK part 3 nothing was added.
  78. *
  79. * To GBK part 4 were added:
  80. * 4. 0xFE{50,54..58,5A..60,62..65,68..6B,6E..75,77..7D,80..8F,92..9F}.
  81. * 5. Characters mapped to the Unicode PUA
  82. * 0xFE51..0xFE53 [glyphs here!!] U+E816..U+E818
  83. * 0xFE59 [glyphs here!!] U+E81E
  84. * 0xFE61 [glyphs here!!] U+E826
  85. * 0xFE66..0xFE67 [glyphs here!!] U+E82B..U+E82C
  86. * 0xFE6C..0xFE6D [glyphs here!!] U+E831..U+E832
  87. * 0xFE76 [glyphs here!!] U+E83B
  88. * 0xFE7E [glyphs here!!] U+E843
  89. * 0xFE90..0xFE91 [glyphs here!!] U+E854..U+E855
  90. * 0xFEA0 [glyphs here!!] U+E864
  91. *
  92. * To GBK part 5 were added:
  93. * 6. 0xA98A..0xA995.
  94. * 7. Characters mapped to the Unicode PUA
  95. * 0xA896..0xA8A0 U+E7BC..U+E7C6
  96. * 0xA958 U+E7E2
  97. * 0xA95B U+E7E3
  98. * 0xA95D..0xA95F U+E7E4..U+E7E6
  99. * 0xA997..0xA9A0 U+E7F4..U+E7FD
  100. *
  101. * UDA part 1 contains the user-defined characters, mapped to the Unicode PUA
  102. * U+E000..U+E233 in ascending order.
  103. *
  104. * UDA part 2 contains the user-defined characters, mapped to the Unicode PUA
  105. * U+E234..U+E4C5 in ascending order.
  106. *
  107. * UDA part 3 contains the user-defined characters, mapped to the Unicode PUA
  108. * U+E4C6..U+E765 in ascending order.
  109. *
  110. * The four-byte range 0x{81..84}{30..39}{81..FE}{30..39}
  111. * contains the rest of the Unicode BMP in ascending order.
  112. * Start: 0x81308130 = 0x0080
  113. * End: 0x8431A439 = 0xFFFF
  114. *
  115. * The four-byte range 0x{90..E3}{30..39}{81..FE}{30..39}
  116. * contains the remaining 16 Unicode planes in Unicode order.
  117. * Start: 0x90308130 = 0x010000
  118. * End: 0xE3329A35 = 0x10FFFF
  119. *
  120. *
  121. * Unassigned Unicode characters are mapped. For example,
  122. * U+173F = 0x8134BF35 (p. 120)
  123. * U+2EFF = 0x81398B31 (p. 148)
  124. * U+FFFE = 0x8431A438 (p. 283)
  125. *
  126. *
  127. * The Unicode PUA (U+E000..U+F8FF) is mapped as follows:
  128. * p. 83..84 0x{AA..AF}{A1..FE} U+E000..U+E233
  129. * p. 85..87 0x{F8..FE}{A1..FE} U+E234..U+E4C5
  130. * p. 88..90 0x{A1..A7}{40..7E,80..A0} U+E4C6..U+E765
  131. * p. 10 0xA2AB..0xA2B0 U+E766..U+E76B
  132. * p. 255 0x8336C739 U+E76C
  133. * p. 10 0xA2E4 U+E76D
  134. * p. 10 0xA2EF..0xA2F0 U+E76E..U+E76F
  135. * p. 10 0xA2FD..0xA2FE U+E770..U+E771
  136. * p. 11 0xA4F4..0xA4FE U+E772..U+E77C
  137. * p. 11 0xA5F7..0xA5FE U+E77D..U+E784
  138. * p. 11 0xA6B9..0xA6C0 U+E785..U+E78C
  139. * p. 11 0xA6D9..0xA6DF [glyphs here!!] U+E78D..U+E793
  140. * p. 11 0xA6EC..0xA6ED [glyphs here!!] U+E794..U+E795
  141. * p. 11 0xA6F3 [glyphs here!!] U+E796
  142. * p. 11 0xA6F6..0xA6FE U+E797..U+E79F
  143. * p. 12 0xA7C2..0xA7D0 U+E7A0..U+E7AE
  144. * p. 12 0xA7F2..0xA7FE U+E7AF..U+E7BB
  145. * p. 82 0xA896..0xA8A0 U+E7BC..U+E7C6
  146. * p. 12 0xA8BC [glyphs here!!] U+E7C7
  147. * p. 255 0x8336C830 U+E7C8
  148. * p. 12 0xA8C1..0xA8C4 U+E7C9..U+E7CC
  149. * p. 12 0xA8EA..0xA8FE U+E7CD..U+E7E1
  150. * p. 82 0xA958 U+E7E2
  151. * p. 82 0xA95B U+E7E3
  152. * p. 82 0xA95D..0xA95F U+E7E4..U+E7E6
  153. * p. 255 0x8336C831..0x8336C933 U+E7E7..U+E7F3
  154. * p. 82 0xA997..0xA9A0 U+E7F4..U+E7FD
  155. * p. 12 0xA9A1..0xA9A3 U+E7FE..U+E800
  156. * p. 12 0xA9F0..0xA9FE U+E801..U+E80F
  157. * p. 26 0xD7FA..0xD7FE U+E810..0xE814
  158. * p. 255 0x8336C934 U+E815
  159. * p. 81 0xFE51..0xFE53 [glyphs here!!] U+E816..U+E818
  160. * p. 255 0x8336C935..0x8336C939 U+E819..U+E81D
  161. * p. 81 0xFE59 [glyphs here!!] U+E81E
  162. * p. 255 0x8336CA30..0x8336CA36 U+E81F..U+E825
  163. * p. 81 0xFE61 [glyphs here!!] U+E826
  164. * p. 255 0x8336CA37..0x8336CB30 U+E827..U+E82A
  165. * p. 81 0xFE66..0xFE67 [glyphs here!!] U+E82B..U+E82C
  166. * p. 255 0x8336CB31..0x8336CB34 U+E82D..U+E830
  167. * p. 81 0xFE6C..0xFE6D [glyphs here!!] U+E831..U+E832
  168. * p. 255 0x8336CB35..0x8336CC32 U+E833..U+E83A
  169. * p. 81 0xFE76 [glyphs here!!] U+E83B
  170. * p. 255 0x8336CC33..0x8336CC39 U+E83C..U+E842
  171. * p. 81 0xFE7E [glyphs here!!] U+E843
  172. * p. 255 0x8336CD30..0x8336CE35 U+E844..U+E853
  173. * p. 81 0xFE90..0xFE91 [glyphs here!!] U+E854..U+E855
  174. * p. 255 0x8336CE36..0x8336CF39 U+E856..U+E863
  175. * p. 81 0xFEA0 [glyphs here!!] U+E864
  176. * p. 255..276 0x8336D030..0x84308130 U+E865..U+F8FF
  177. *
  178. *
  179. * The Unicode surrogate area (U+D800..U+DFFF) is not mapped. (p. 255)
  180. *
  181. */
  182. #include "gb18030ext.h"
  183. #include "gb18030uni.h"
  184. static int
  185. gb18030_mbtowc (conv_t conv, ucs4_t *pwc, const unsigned char *s, int n)
  186. {
  187. int ret;
  188. /* Code set 0 (ASCII) */
  189. if (*s < 0x80)
  190. return ascii_mbtowc(conv,pwc,s,n);
  191. /* Code set 1 (GBK extended) */
  192. ret = gbk_mbtowc(conv,pwc,s,n);
  193. if (ret != RET_ILSEQ)
  194. return ret;
  195. ret = gb18030ext_mbtowc(conv,pwc,s,n);
  196. if (ret != RET_ILSEQ)
  197. return ret;
  198. /* Code set 2 (remainder of Unicode U+0000..U+FFFF), including
  199. User-defined characters, two-byte part of range U+E766..U+E864 */
  200. ret = gb18030uni_mbtowc(conv,pwc,s,n);
  201. if (ret != RET_ILSEQ)
  202. return ret;
  203. /* User-defined characters range U+E000..U+E765 */
  204. {
  205. unsigned char c1 = s[0];
  206. if ((c1 >= 0xaa && c1 <= 0xaf) || (c1 >= 0xf8 && c1 <= 0xfe)) {
  207. if (n >= 2) {
  208. unsigned char c2 = s[1];
  209. if (c2 >= 0xa1 && c2 <= 0xfe) {
  210. *pwc = 0xe000 + 94 * (c1 >= 0xf8 ? c1 - 0xf2 : c1 - 0xaa) + (c2 - 0xa1);
  211. return 2;
  212. }
  213. } else
  214. return RET_TOOFEW(0);
  215. } else if (c1 >= 0xa1 && c1 <= 0xa7) {
  216. if (n >= 2) {
  217. unsigned char c2 = s[1];
  218. if (c2 >= 0x40 && c2 <= 0xa1 && c2 != 0x7f) {
  219. *pwc = 0xe4c6 + 96 * (c1 - 0xa1) + c2 - (c2 >= 0x80 ? 0x41 : 0x40);
  220. return 2;
  221. }
  222. } else
  223. return RET_TOOFEW(0);
  224. }
  225. }
  226. /* Code set 3 (Unicode U+10000..U+10FFFF) */
  227. {
  228. unsigned char c1 = s[0];
  229. if (c1 >= 0x90 && c1 <= 0xe3) {
  230. if (n >= 2) {
  231. unsigned char c2 = s[1];
  232. if (c2 >= 0x30 && c2 <= 0x39) {
  233. if (n >= 3) {
  234. unsigned char c3 = s[2];
  235. if (c3 >= 0x81 && c3 <= 0xfe) {
  236. if (n >= 4) {
  237. unsigned char c4 = s[3];
  238. if (c4 >= 0x30 && c4 <= 0x39) {
  239. unsigned int i = (((c1 - 0x90) * 10 + (c2 - 0x30)) * 126 + (c3 - 0x81)) * 10 + (c4 - 0x30);
  240. if (i >= 0 && i < 0x100000) {
  241. *pwc = (ucs4_t) (0x10000 + i);
  242. return 4;
  243. }
  244. }
  245. return RET_ILSEQ;
  246. }
  247. return RET_TOOFEW(0);
  248. }
  249. return RET_ILSEQ;
  250. }
  251. return RET_TOOFEW(0);
  252. }
  253. return RET_ILSEQ;
  254. }
  255. return RET_TOOFEW(0);
  256. }
  257. return RET_ILSEQ;
  258. }
  259. }
  260. static const unsigned short gb18030_pua2charset[32*3] = {
  261. /* Unicode range GB18030 range */
  262. 0xe766, 0xe76b, 0xa2ab, /*.. 0xa2b0, */
  263. 0xe76d, 0xe76d, 0xa2e4,
  264. 0xe76e, 0xe76f, 0xa2ef, /*.. 0xa2f0, */
  265. 0xe770, 0xe771, 0xa2fd, /*.. 0xa2fe, */
  266. 0xe772, 0xe77c, 0xa4f4, /*.. 0xa4fe, */
  267. 0xe77d, 0xe784, 0xa5f7, /*.. 0xa5fe, */
  268. 0xe785, 0xe78c, 0xa6b9, /*.. 0xa6c0, */
  269. 0xe78d, 0xe793, 0xa6d9, /*.. 0xa6df, */
  270. 0xe794, 0xe795, 0xa6ec, /*.. 0xa6ed, */
  271. 0xe796, 0xe796, 0xa6f3,
  272. 0xe797, 0xe79f, 0xa6f6, /*.. 0xa6fe, */
  273. 0xe7a0, 0xe7ae, 0xa7c2, /*.. 0xa7d0, */
  274. 0xe7af, 0xe7bb, 0xa7f2, /*.. 0xa7fe, */
  275. 0xe7bc, 0xe7c6, 0xa896, /*.. 0xa8a0, */
  276. 0xe7c7, 0xe7c7, 0xa8bc,
  277. 0xe7c9, 0xe7cc, 0xa8c1, /*.. 0xa8c4, */
  278. 0xe7cd, 0xe7e1, 0xa8ea, /*.. 0xa8fe, */
  279. 0xe7e2, 0xe7e2, 0xa958,
  280. 0xe7e3, 0xe7e3, 0xa95b,
  281. 0xe7e4, 0xe7e6, 0xa95d, /*.. 0xa95f, */
  282. 0xe7f4, 0xe800, 0xa997, /*.. 0xa9a3, */
  283. 0xe801, 0xe80f, 0xa9f0, /*.. 0xa9fe, */
  284. 0xe810, 0xe814, 0xd7fa, /*.. 0xd7fe, */
  285. 0xe816, 0xe818, 0xfe51, /*.. 0xfe53, */
  286. 0xe81e, 0xe81e, 0xfe59,
  287. 0xe826, 0xe826, 0xfe61,
  288. 0xe82b, 0xe82c, 0xfe66, /*.. 0xfe67, */
  289. 0xe831, 0xe832, 0xfe6c, /*.. 0xfe6d, */
  290. 0xe83b, 0xe83b, 0xfe76,
  291. 0xe843, 0xe843, 0xfe7e,
  292. 0xe854, 0xe855, 0xfe90, /*.. 0xfe91, */
  293. 0xe864, 0xe864, 0xfea0,
  294. };
  295. static int
  296. gb18030_wctomb (conv_t conv, unsigned char *r, ucs4_t wc, int n)
  297. {
  298. int ret;
  299. /* Code set 0 (ASCII) */
  300. ret = ascii_wctomb(conv,r,wc,n);
  301. if (ret != RET_ILUNI)
  302. return ret;
  303. /* Code set 1 (GBK extended) */
  304. ret = gbk_wctomb(conv,r,wc,n);
  305. if (ret != RET_ILUNI)
  306. return ret;
  307. ret = gb18030ext_wctomb(conv,r,wc,n);
  308. if (ret != RET_ILUNI)
  309. return ret;
  310. /* Code set 2 (remainder of Unicode U+0000..U+FFFF) */
  311. if (wc >= 0xe000 && wc <= 0xe864) {
  312. if (n >= 2) {
  313. if (wc < 0xe766) {
  314. /* User-defined characters range U+E000..U+E765 */
  315. if (wc < 0xe4c6) {
  316. unsigned int i = wc - 0xe000;
  317. r[1] = (i % 94) + 0xa1; i = i / 94;
  318. r[0] = (i < 6 ? i + 0xaa : i + 0xf2);
  319. return 2;
  320. } else {
  321. unsigned int i = wc - 0xe4c6;
  322. r[0] = (i / 96) + 0xa1; i = i % 96;
  323. r[1] = i + (i >= 0x3f ? 0x41 : 0x40);
  324. return 2;
  325. }
  326. } else {
  327. /* User-defined characters, two-byte part of range U+E766..U+E864 */
  328. unsigned int k1 = 0;
  329. unsigned int k2 = 32;
  330. /* Invariant: We know that if wc occurs in Unicode interval in
  331. gb18030_pua2charset, it does so at a k with k1 <= k < k2. */
  332. while (k1 < k2) {
  333. unsigned int k = (k1 + k2) / 2;
  334. if (wc < gb18030_pua2charset[k*3+0])
  335. k2 = k;
  336. else if (wc > gb18030_pua2charset[k*3+1])
  337. k1 = k + 1;
  338. else {
  339. unsigned short c =
  340. gb18030_pua2charset[k*3+2] + (wc - gb18030_pua2charset[k*3+0]);
  341. r[0] = (c >> 8);
  342. r[1] = (c & 0xff);
  343. return 2;
  344. }
  345. }
  346. }
  347. } else
  348. return RET_TOOSMALL;
  349. }
  350. ret = gb18030uni_wctomb(conv,r,wc,n);
  351. if (ret != RET_ILUNI)
  352. return ret;
  353. /* Code set 3 (Unicode U+10000..U+10FFFF) */
  354. if (n >= 4) {
  355. if (wc >= 0x10000 && wc < 0x110000) {
  356. unsigned int i = wc - 0x10000;
  357. r[3] = (i % 10) + 0x30; i = i / 10;
  358. r[2] = (i % 126) + 0x81; i = i / 126;
  359. r[1] = (i % 10) + 0x30; i = i / 10;
  360. r[0] = i + 0x90;
  361. return 4;
  362. }
  363. return RET_ILUNI;
  364. }
  365. return RET_TOOSMALL;
  366. }