gb18030.h 14 KB

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