iso2022_cnext.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. /*
  2. * Copyright (C) 1999-2001, 2008 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. * ISO-2022-CN-EXT
  22. */
  23. /* Specification: RFC 1922 */
  24. #define ESC 0x1b
  25. #define SO 0x0e
  26. #define SI 0x0f
  27. /*
  28. * The state is composed of one of the following values
  29. */
  30. #define STATE_ASCII 0
  31. #define STATE_TWOBYTE 1
  32. /*
  33. * and one of the following values, << 8
  34. */
  35. #define STATE2_NONE 0
  36. #define STATE2_DESIGNATED_GB2312 1
  37. #define STATE2_DESIGNATED_CNS11643_1 2
  38. #define STATE2_DESIGNATED_ISO_IR_165 3
  39. /*
  40. * and one of the following values, << 16
  41. */
  42. #define STATE3_NONE 0
  43. #define STATE3_DESIGNATED_CNS11643_2 1
  44. /*
  45. * and one of the following values, << 24
  46. */
  47. #define STATE4_NONE 0
  48. #define STATE4_DESIGNATED_CNS11643_3 1
  49. #define STATE4_DESIGNATED_CNS11643_4 2
  50. #define STATE4_DESIGNATED_CNS11643_5 3
  51. #define STATE4_DESIGNATED_CNS11643_6 4
  52. #define STATE4_DESIGNATED_CNS11643_7 5
  53. #define SPLIT_STATE \
  54. unsigned int state1 = state & 0xff, state2 = (state >> 8) & 0xff, state3 = (state >> 16) & 0xff, state4 = state >> 24
  55. #define COMBINE_STATE \
  56. state = (state4 << 24) | (state3 << 16) | (state2 << 8) | state1
  57. static int
  58. iso2022_cn_ext_mbtowc (conv_t conv, ucs4_t *pwc, const unsigned char *s, int n)
  59. {
  60. state_t state = conv->istate;
  61. SPLIT_STATE;
  62. int count = 0;
  63. unsigned char c;
  64. for (;;) {
  65. c = *s;
  66. if (c == ESC) {
  67. if (n < count+4)
  68. goto none;
  69. if (s[1] == '$') {
  70. if (s[2] == ')') {
  71. if (s[3] == 'A') {
  72. state2 = STATE2_DESIGNATED_GB2312;
  73. s += 4; count += 4;
  74. if (n < count+1)
  75. goto none;
  76. continue;
  77. }
  78. if (s[3] == 'G') {
  79. state2 = STATE2_DESIGNATED_CNS11643_1;
  80. s += 4; count += 4;
  81. if (n < count+1)
  82. goto none;
  83. continue;
  84. }
  85. if (s[3] == 'E') {
  86. state2 = STATE2_DESIGNATED_ISO_IR_165;
  87. s += 4; count += 4;
  88. if (n < count+1)
  89. goto none;
  90. continue;
  91. }
  92. }
  93. if (s[2] == '*') {
  94. if (s[3] == 'H') {
  95. state3 = STATE3_DESIGNATED_CNS11643_2;
  96. s += 4; count += 4;
  97. if (n < count+1)
  98. goto none;
  99. continue;
  100. }
  101. }
  102. if (s[2] == '+') {
  103. if (s[3] == 'I') {
  104. state4 = STATE4_DESIGNATED_CNS11643_3;
  105. s += 4; count += 4;
  106. if (n < count+1)
  107. goto none;
  108. continue;
  109. }
  110. if (s[3] == 'J') {
  111. state4 = STATE4_DESIGNATED_CNS11643_4;
  112. s += 4; count += 4;
  113. if (n < count+1)
  114. goto none;
  115. continue;
  116. }
  117. if (s[3] == 'K') {
  118. state4 = STATE4_DESIGNATED_CNS11643_5;
  119. s += 4; count += 4;
  120. if (n < count+1)
  121. goto none;
  122. continue;
  123. }
  124. if (s[3] == 'L') {
  125. state4 = STATE4_DESIGNATED_CNS11643_6;
  126. s += 4; count += 4;
  127. if (n < count+1)
  128. goto none;
  129. continue;
  130. }
  131. if (s[3] == 'M') {
  132. state4 = STATE4_DESIGNATED_CNS11643_7;
  133. s += 4; count += 4;
  134. if (n < count+1)
  135. goto none;
  136. continue;
  137. }
  138. }
  139. }
  140. if (s[1] == 'N') {
  141. switch (state3) {
  142. case STATE3_NONE:
  143. goto ilseq;
  144. case STATE3_DESIGNATED_CNS11643_2:
  145. if (s[2] < 0x80 && s[3] < 0x80) {
  146. int ret = cns11643_2_mbtowc(conv,pwc,s+2,2);
  147. if (ret == RET_ILSEQ)
  148. goto ilseq;
  149. if (ret != 2) abort();
  150. COMBINE_STATE;
  151. conv->istate = state;
  152. return count+4;
  153. } else
  154. goto ilseq;
  155. default: abort();
  156. }
  157. }
  158. if (s[1] == 'O') {
  159. switch (state4) {
  160. case STATE4_NONE:
  161. goto ilseq;
  162. case STATE4_DESIGNATED_CNS11643_3:
  163. if (s[2] < 0x80 && s[3] < 0x80) {
  164. int ret = cns11643_3_mbtowc(conv,pwc,s+2,2);
  165. if (ret == RET_ILSEQ)
  166. goto ilseq;
  167. if (ret != 2) abort();
  168. COMBINE_STATE;
  169. conv->istate = state;
  170. return count+4;
  171. } else
  172. goto ilseq;
  173. case STATE4_DESIGNATED_CNS11643_4:
  174. if (s[2] < 0x80 && s[3] < 0x80) {
  175. int ret = cns11643_4_mbtowc(conv,pwc,s+2,2);
  176. if (ret == RET_ILSEQ)
  177. goto ilseq;
  178. if (ret != 2) abort();
  179. COMBINE_STATE;
  180. conv->istate = state;
  181. return count+4;
  182. } else
  183. goto ilseq;
  184. case STATE4_DESIGNATED_CNS11643_5:
  185. if (s[2] < 0x80 && s[3] < 0x80) {
  186. int ret = cns11643_5_mbtowc(conv,pwc,s+2,2);
  187. if (ret == RET_ILSEQ)
  188. goto ilseq;
  189. if (ret != 2) abort();
  190. COMBINE_STATE;
  191. conv->istate = state;
  192. return count+4;
  193. } else
  194. goto ilseq;
  195. case STATE4_DESIGNATED_CNS11643_6:
  196. if (s[2] < 0x80 && s[3] < 0x80) {
  197. int ret = cns11643_6_mbtowc(conv,pwc,s+2,2);
  198. if (ret == RET_ILSEQ)
  199. goto ilseq;
  200. if (ret != 2) abort();
  201. COMBINE_STATE;
  202. conv->istate = state;
  203. return count+4;
  204. } else
  205. goto ilseq;
  206. case STATE4_DESIGNATED_CNS11643_7:
  207. if (s[2] < 0x80 && s[3] < 0x80) {
  208. int ret = cns11643_7_mbtowc(conv,pwc,s+2,2);
  209. if (ret == RET_ILSEQ)
  210. goto ilseq;
  211. if (ret != 2) abort();
  212. COMBINE_STATE;
  213. conv->istate = state;
  214. return count+4;
  215. } else
  216. goto ilseq;
  217. default: abort();
  218. }
  219. }
  220. goto ilseq;
  221. }
  222. if (c == SO) {
  223. if (state2 != STATE2_DESIGNATED_GB2312 && state2 != STATE2_DESIGNATED_CNS11643_1 && state2 != STATE2_DESIGNATED_ISO_IR_165)
  224. goto ilseq;
  225. state1 = STATE_TWOBYTE;
  226. s++; count++;
  227. if (n < count+1)
  228. goto none;
  229. continue;
  230. }
  231. if (c == SI) {
  232. state1 = STATE_ASCII;
  233. s++; count++;
  234. if (n < count+1)
  235. goto none;
  236. continue;
  237. }
  238. break;
  239. }
  240. switch (state1) {
  241. case STATE_ASCII:
  242. if (c < 0x80) {
  243. int ret = ascii_mbtowc(conv,pwc,s,1);
  244. if (ret == RET_ILSEQ)
  245. goto ilseq;
  246. if (ret != 1) abort();
  247. if (*pwc == 0x000a || *pwc == 0x000d) {
  248. state2 = STATE2_NONE; state3 = STATE3_NONE; state4 = STATE3_NONE;
  249. }
  250. COMBINE_STATE;
  251. conv->istate = state;
  252. return count+1;
  253. } else
  254. goto ilseq;
  255. case STATE_TWOBYTE:
  256. if (n < count+2)
  257. goto none;
  258. if (s[0] < 0x80 && s[1] < 0x80) {
  259. int ret;
  260. switch (state2) {
  261. case STATE2_NONE:
  262. goto ilseq;
  263. case STATE2_DESIGNATED_GB2312:
  264. ret = gb2312_mbtowc(conv,pwc,s,2); break;
  265. case STATE2_DESIGNATED_CNS11643_1:
  266. ret = cns11643_1_mbtowc(conv,pwc,s,2); break;
  267. case STATE2_DESIGNATED_ISO_IR_165:
  268. ret = isoir165_mbtowc(conv,pwc,s,2); break;
  269. default: abort();
  270. }
  271. if (ret == RET_ILSEQ)
  272. goto ilseq;
  273. if (ret != 2) abort();
  274. COMBINE_STATE;
  275. conv->istate = state;
  276. return count+2;
  277. } else
  278. goto ilseq;
  279. default: abort();
  280. }
  281. none:
  282. COMBINE_STATE;
  283. conv->istate = state;
  284. return RET_TOOFEW(count);
  285. ilseq:
  286. COMBINE_STATE;
  287. conv->istate = state;
  288. return RET_SHIFT_ILSEQ(count);
  289. }
  290. static int
  291. iso2022_cn_ext_wctomb (conv_t conv, unsigned char *r, ucs4_t wc, int n)
  292. {
  293. state_t state = conv->ostate;
  294. SPLIT_STATE;
  295. unsigned char buf[3];
  296. int ret;
  297. /* There is no need to handle Unicode 3.1 tag characters and to look for
  298. "zh-CN" or "zh-TW" tags, because GB2312 and CNS11643 are disjoint. */
  299. /* Try ASCII. */
  300. ret = ascii_wctomb(conv,buf,wc,1);
  301. if (ret != RET_ILUNI) {
  302. if (ret != 1) abort();
  303. if (buf[0] < 0x80) {
  304. int count = (state1 == STATE_ASCII ? 1 : 2);
  305. if (n < count)
  306. return RET_TOOSMALL;
  307. if (state1 != STATE_ASCII) {
  308. r[0] = SI;
  309. r += 1;
  310. state1 = STATE_ASCII;
  311. }
  312. r[0] = buf[0];
  313. if (wc == 0x000a || wc == 0x000d) {
  314. state2 = STATE2_NONE; state3 = STATE3_NONE; state4 = STATE3_NONE;
  315. }
  316. COMBINE_STATE;
  317. conv->ostate = state;
  318. return count;
  319. }
  320. }
  321. /* Try GB 2312-1980. */
  322. ret = gb2312_wctomb(conv,buf,wc,2);
  323. if (ret != RET_ILUNI) {
  324. if (ret != 2) abort();
  325. if (buf[0] < 0x80 && buf[1] < 0x80) {
  326. int count = (state2 == STATE2_DESIGNATED_GB2312 ? 0 : 4) + (state1 == STATE_TWOBYTE ? 0 : 1) + 2;
  327. if (n < count)
  328. return RET_TOOSMALL;
  329. if (state2 != STATE2_DESIGNATED_GB2312) {
  330. r[0] = ESC;
  331. r[1] = '$';
  332. r[2] = ')';
  333. r[3] = 'A';
  334. r += 4;
  335. state2 = STATE2_DESIGNATED_GB2312;
  336. }
  337. if (state1 != STATE_TWOBYTE) {
  338. r[0] = SO;
  339. r += 1;
  340. state1 = STATE_TWOBYTE;
  341. }
  342. r[0] = buf[0];
  343. r[1] = buf[1];
  344. COMBINE_STATE;
  345. conv->ostate = state;
  346. return count;
  347. }
  348. }
  349. ret = cns11643_wctomb(conv,buf,wc,3);
  350. if (ret != RET_ILUNI) {
  351. if (ret != 3) abort();
  352. /* Try CNS 11643-1992 Plane 1. */
  353. if (buf[0] == 1 && buf[1] < 0x80 && buf[2] < 0x80) {
  354. int count = (state2 == STATE2_DESIGNATED_CNS11643_1 ? 0 : 4) + (state1 == STATE_TWOBYTE ? 0 : 1) + 2;
  355. if (n < count)
  356. return RET_TOOSMALL;
  357. if (state2 != STATE2_DESIGNATED_CNS11643_1) {
  358. r[0] = ESC;
  359. r[1] = '$';
  360. r[2] = ')';
  361. r[3] = 'G';
  362. r += 4;
  363. state2 = STATE2_DESIGNATED_CNS11643_1;
  364. }
  365. if (state1 != STATE_TWOBYTE) {
  366. r[0] = SO;
  367. r += 1;
  368. state1 = STATE_TWOBYTE;
  369. }
  370. r[0] = buf[1];
  371. r[1] = buf[2];
  372. COMBINE_STATE;
  373. conv->ostate = state;
  374. return count;
  375. }
  376. /* Try CNS 11643-1992 Plane 2. */
  377. if (buf[0] == 2 && buf[1] < 0x80 && buf[2] < 0x80) {
  378. int count = (state3 == STATE3_DESIGNATED_CNS11643_2 ? 0 : 4) + 4;
  379. if (n < count)
  380. return RET_TOOSMALL;
  381. if (state3 != STATE3_DESIGNATED_CNS11643_2) {
  382. r[0] = ESC;
  383. r[1] = '$';
  384. r[2] = '*';
  385. r[3] = 'H';
  386. r += 4;
  387. state3 = STATE3_DESIGNATED_CNS11643_2;
  388. }
  389. r[0] = ESC;
  390. r[1] = 'N';
  391. r[2] = buf[1];
  392. r[3] = buf[2];
  393. COMBINE_STATE;
  394. conv->ostate = state;
  395. return count;
  396. }
  397. /* Try CNS 11643-1992 Plane 3. */
  398. if (buf[0] == 3 && buf[1] < 0x80 && buf[2] < 0x80) {
  399. int count = (state4 == STATE4_DESIGNATED_CNS11643_3 ? 0 : 4) + 4;
  400. if (n < count)
  401. return RET_TOOSMALL;
  402. if (state4 != STATE4_DESIGNATED_CNS11643_3) {
  403. r[0] = ESC;
  404. r[1] = '$';
  405. r[2] = '+';
  406. r[3] = 'I';
  407. r += 4;
  408. state4 = STATE4_DESIGNATED_CNS11643_3;
  409. }
  410. r[0] = ESC;
  411. r[1] = 'O';
  412. r[2] = buf[1];
  413. r[3] = buf[2];
  414. COMBINE_STATE;
  415. conv->ostate = state;
  416. return count;
  417. }
  418. /* Try CNS 11643-1992 Plane 4. */
  419. if (buf[0] == 4 && buf[1] < 0x80 && buf[2] < 0x80) {
  420. int count = (state4 == STATE4_DESIGNATED_CNS11643_4 ? 0 : 4) + 4;
  421. if (n < count)
  422. return RET_TOOSMALL;
  423. if (state4 != STATE4_DESIGNATED_CNS11643_4) {
  424. r[0] = ESC;
  425. r[1] = '$';
  426. r[2] = '+';
  427. r[3] = 'J';
  428. r += 4;
  429. state4 = STATE4_DESIGNATED_CNS11643_4;
  430. }
  431. r[0] = ESC;
  432. r[1] = 'O';
  433. r[2] = buf[1];
  434. r[3] = buf[2];
  435. COMBINE_STATE;
  436. conv->ostate = state;
  437. return count;
  438. }
  439. /* Try CNS 11643-1992 Plane 5. */
  440. if (buf[0] == 5 && buf[1] < 0x80 && buf[2] < 0x80) {
  441. int count = (state4 == STATE4_DESIGNATED_CNS11643_5 ? 0 : 4) + 4;
  442. if (n < count)
  443. return RET_TOOSMALL;
  444. if (state4 != STATE4_DESIGNATED_CNS11643_5) {
  445. r[0] = ESC;
  446. r[1] = '$';
  447. r[2] = '+';
  448. r[3] = 'K';
  449. r += 4;
  450. state4 = STATE4_DESIGNATED_CNS11643_5;
  451. }
  452. r[0] = ESC;
  453. r[1] = 'O';
  454. r[2] = buf[1];
  455. r[3] = buf[2];
  456. COMBINE_STATE;
  457. conv->ostate = state;
  458. return count;
  459. }
  460. /* Try CNS 11643-1992 Plane 6. */
  461. if (buf[0] == 6 && buf[1] < 0x80 && buf[2] < 0x80) {
  462. int count = (state4 == STATE4_DESIGNATED_CNS11643_6 ? 0 : 4) + 4;
  463. if (n < count)
  464. return RET_TOOSMALL;
  465. if (state4 != STATE4_DESIGNATED_CNS11643_6) {
  466. r[0] = ESC;
  467. r[1] = '$';
  468. r[2] = '+';
  469. r[3] = 'L';
  470. r += 4;
  471. state4 = STATE4_DESIGNATED_CNS11643_6;
  472. }
  473. r[0] = ESC;
  474. r[1] = 'O';
  475. r[2] = buf[1];
  476. r[3] = buf[2];
  477. COMBINE_STATE;
  478. conv->ostate = state;
  479. return count;
  480. }
  481. /* Try CNS 11643-1992 Plane 7. */
  482. if (buf[0] == 7 && buf[1] < 0x80 && buf[2] < 0x80) {
  483. int count = (state4 == STATE4_DESIGNATED_CNS11643_7 ? 0 : 4) + 4;
  484. if (n < count)
  485. return RET_TOOSMALL;
  486. if (state4 != STATE4_DESIGNATED_CNS11643_7) {
  487. r[0] = ESC;
  488. r[1] = '$';
  489. r[2] = '+';
  490. r[3] = 'M';
  491. r += 4;
  492. state4 = STATE4_DESIGNATED_CNS11643_7;
  493. }
  494. r[0] = ESC;
  495. r[1] = 'O';
  496. r[2] = buf[1];
  497. r[3] = buf[2];
  498. COMBINE_STATE;
  499. conv->ostate = state;
  500. return count;
  501. }
  502. }
  503. /* Try ISO-IR-165. */
  504. ret = isoir165_wctomb(conv,buf,wc,2);
  505. if (ret != RET_ILUNI) {
  506. if (ret != 2) abort();
  507. if (buf[0] < 0x80 && buf[1] < 0x80) {
  508. int count = (state2 == STATE2_DESIGNATED_ISO_IR_165 ? 0 : 4) + (state1 == STATE_TWOBYTE ? 0 : 1) + 2;
  509. if (n < count)
  510. return RET_TOOSMALL;
  511. if (state2 != STATE2_DESIGNATED_ISO_IR_165) {
  512. r[0] = ESC;
  513. r[1] = '$';
  514. r[2] = ')';
  515. r[3] = 'E';
  516. r += 4;
  517. state2 = STATE2_DESIGNATED_ISO_IR_165;
  518. }
  519. if (state1 != STATE_TWOBYTE) {
  520. r[0] = SO;
  521. r += 1;
  522. state1 = STATE_TWOBYTE;
  523. }
  524. r[0] = buf[0];
  525. r[1] = buf[1];
  526. COMBINE_STATE;
  527. conv->ostate = state;
  528. return count;
  529. }
  530. }
  531. return RET_ILUNI;
  532. }
  533. static int
  534. iso2022_cn_ext_reset (conv_t conv, unsigned char *r, int n)
  535. {
  536. state_t state = conv->ostate;
  537. SPLIT_STATE;
  538. (void)state2;
  539. (void)state3;
  540. (void)state4;
  541. if (state1 != STATE_ASCII) {
  542. if (n < 1)
  543. return RET_TOOSMALL;
  544. r[0] = SI;
  545. /* conv->ostate = 0; will be done by the caller */
  546. return 1;
  547. } else
  548. return 0;
  549. }
  550. #undef COMBINE_STATE
  551. #undef SPLIT_STATE
  552. #undef STATE4_DESIGNATED_CNS11643_7
  553. #undef STATE4_DESIGNATED_CNS11643_6
  554. #undef STATE4_DESIGNATED_CNS11643_5
  555. #undef STATE4_DESIGNATED_CNS11643_4
  556. #undef STATE4_DESIGNATED_CNS11643_3
  557. #undef STATE4_NONE
  558. #undef STATE3_DESIGNATED_CNS11643_2
  559. #undef STATE3_NONE
  560. #undef STATE2_DESIGNATED_ISO_IR_165
  561. #undef STATE2_DESIGNATED_CNS11643_1
  562. #undef STATE2_DESIGNATED_GB2312
  563. #undef STATE2_NONE
  564. #undef STATE_TWOBYTE
  565. #undef STATE_ASCII