iso2022_jp3.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. /*
  2. * Copyright (C) 1999-2004, 2008, 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. * ISO-2022-JP-3
  21. */
  22. #include "jisx0213.h"
  23. #define ESC 0x1b
  24. /*
  25. * The state is composed of one of the following values
  26. */
  27. #define STATE_ASCII 0 /* Esc ( B */
  28. #define STATE_JISX0201ROMAN 1 /* Esc ( J */
  29. #define STATE_JISX0201KATAKANA 2 /* Esc ( I */
  30. #define STATE_JISX0208 3 /* Esc $ @ or Esc $ B */
  31. #define STATE_JISX02131 4 /* Esc $ ( O or Esc $ ( Q*/
  32. #define STATE_JISX02132 5 /* Esc $ ( P */
  33. /*
  34. * In the ISO-2022-JP-3 to UCS-4 direction, the state also holds the last
  35. * character to be output, shifted by 3 bits.
  36. */
  37. static int
  38. iso2022_jp3_mbtowc (conv_t conv, ucs4_t *pwc, const unsigned char *s, size_t n)
  39. {
  40. ucs4_t last_wc = conv->istate >> 3;
  41. if (last_wc) {
  42. /* Output the buffered character. */
  43. conv->istate &= 7;
  44. *pwc = last_wc;
  45. return 0; /* Don't advance the input pointer. */
  46. } else {
  47. state_t state = conv->istate;
  48. int count = 0;
  49. unsigned char c;
  50. for (;;) {
  51. c = *s;
  52. if (c == ESC) {
  53. if (n < count+3)
  54. goto none;
  55. if (s[1] == '(') {
  56. if (s[2] == 'B') {
  57. state = STATE_ASCII;
  58. s += 3; count += 3;
  59. if (n < count+1)
  60. goto none;
  61. continue;
  62. }
  63. if (s[2] == 'J') {
  64. state = STATE_JISX0201ROMAN;
  65. s += 3; count += 3;
  66. if (n < count+1)
  67. goto none;
  68. continue;
  69. }
  70. if (s[2] == 'I') {
  71. state = STATE_JISX0201KATAKANA;
  72. s += 3; count += 3;
  73. if (n < count+1)
  74. goto none;
  75. continue;
  76. }
  77. goto ilseq;
  78. }
  79. if (s[1] == '$') {
  80. if (s[2] == '@' || s[2] == 'B') {
  81. /* We don't distinguish JIS X 0208-1978 and JIS X 0208-1983. */
  82. state = STATE_JISX0208;
  83. s += 3; count += 3;
  84. if (n < count+1)
  85. goto none;
  86. continue;
  87. }
  88. if (s[2] == '(') {
  89. if (n < count+4)
  90. goto none;
  91. if (s[3] == 'O' || s[3] == 'Q') {
  92. state = STATE_JISX02131;
  93. s += 4; count += 4;
  94. if (n < count+1)
  95. goto none;
  96. continue;
  97. }
  98. if (s[3] == 'P') {
  99. state = STATE_JISX02132;
  100. s += 4; count += 4;
  101. if (n < count+1)
  102. goto none;
  103. continue;
  104. }
  105. }
  106. goto ilseq;
  107. }
  108. goto ilseq;
  109. }
  110. break;
  111. }
  112. switch (state) {
  113. case STATE_ASCII:
  114. if (c < 0x80) {
  115. int ret = ascii_mbtowc(conv,pwc,s,1);
  116. if (ret == RET_ILSEQ)
  117. goto ilseq;
  118. if (ret != 1) abort();
  119. conv->istate = state;
  120. return count+1;
  121. } else
  122. goto ilseq;
  123. case STATE_JISX0201ROMAN:
  124. if (c < 0x80) {
  125. int ret = jisx0201_mbtowc(conv,pwc,s,1);
  126. if (ret == RET_ILSEQ)
  127. goto ilseq;
  128. if (ret != 1) abort();
  129. conv->istate = state;
  130. return count+1;
  131. } else
  132. goto ilseq;
  133. case STATE_JISX0201KATAKANA:
  134. if (c < 0x80) {
  135. unsigned char buf = c+0x80;
  136. int ret = jisx0201_mbtowc(conv,pwc,&buf,1);
  137. if (ret == RET_ILSEQ)
  138. goto ilseq;
  139. if (ret != 1) abort();
  140. conv->istate = state;
  141. return count+1;
  142. } else
  143. goto ilseq;
  144. case STATE_JISX0208:
  145. if (n < count+2)
  146. goto none;
  147. if (s[0] < 0x80 && s[1] < 0x80) {
  148. int ret = jisx0208_mbtowc(conv,pwc,s,2);
  149. if (ret == RET_ILSEQ)
  150. goto ilseq;
  151. if (ret != 2) abort();
  152. conv->istate = state;
  153. return count+2;
  154. } else
  155. goto ilseq;
  156. case STATE_JISX02131:
  157. case STATE_JISX02132:
  158. if (n < count+2)
  159. goto none;
  160. if (s[0] < 0x80 && s[1] < 0x80) {
  161. ucs4_t wc = jisx0213_to_ucs4(((state-STATE_JISX02131+1)<<8)+s[0],s[1]);
  162. if (wc) {
  163. if (wc < 0x80) {
  164. /* It's a combining character. */
  165. ucs4_t wc1 = jisx0213_to_ucs_combining[wc - 1][0];
  166. ucs4_t wc2 = jisx0213_to_ucs_combining[wc - 1][1];
  167. /* We cannot output two Unicode characters at once. So,
  168. output the first character and buffer the second one. */
  169. *pwc = wc1;
  170. conv->istate = (wc2 << 3) | state;
  171. } else {
  172. *pwc = wc;
  173. conv->istate = state;
  174. }
  175. return count+2;
  176. }
  177. }
  178. goto ilseq;
  179. default: abort();
  180. }
  181. none:
  182. conv->istate = state;
  183. return RET_TOOFEW(count);
  184. ilseq:
  185. conv->istate = state;
  186. return RET_SHIFT_ILSEQ(count);
  187. }
  188. }
  189. static int
  190. iso2022_jp3_flushwc (conv_t conv, ucs4_t *pwc)
  191. {
  192. ucs4_t last_wc = conv->istate >> 3;
  193. if (last_wc) {
  194. /* Output the buffered character. */
  195. conv->istate &= 7;
  196. *pwc = last_wc;
  197. return 1;
  198. } else
  199. return 0;
  200. }
  201. /*
  202. * In the UCS-4 to ISO-2022-JP-3 direction, the state also holds the last two
  203. * bytes to be output, shifted by 3 bits, and the STATE_xxxxx value that was
  204. * effective before this buffered character, shifted by 19 bits.
  205. */
  206. /* Composition tables for each of the relevant combining characters. */
  207. static const struct { unsigned short base; unsigned short composed; } iso2022_jp3_comp_table_data[] = {
  208. #define iso2022_jp3_comp_table02e5_idx 0
  209. #define iso2022_jp3_comp_table02e5_len 1
  210. { 0x2b64, 0x2b65 }, /* 0x12B65 = 0x12B64 U+02E5 */
  211. #define iso2022_jp3_comp_table02e9_idx (iso2022_jp3_comp_table02e5_idx+iso2022_jp3_comp_table02e5_len)
  212. #define iso2022_jp3_comp_table02e9_len 1
  213. { 0x2b60, 0x2b66 }, /* 0x12B66 = 0x12B60 U+02E9 */
  214. #define iso2022_jp3_comp_table0300_idx (iso2022_jp3_comp_table02e9_idx+iso2022_jp3_comp_table02e9_len)
  215. #define iso2022_jp3_comp_table0300_len 5
  216. { 0x295c, 0x2b44 }, /* 0x12B44 = 0x1295C U+0300 */
  217. { 0x2b38, 0x2b48 }, /* 0x12B48 = 0x12B38 U+0300 */
  218. { 0x2b37, 0x2b4a }, /* 0x12B4A = 0x12B37 U+0300 */
  219. { 0x2b30, 0x2b4c }, /* 0x12B4C = 0x12B30 U+0300 */
  220. { 0x2b43, 0x2b4e }, /* 0x12B4E = 0x12B43 U+0300 */
  221. #define iso2022_jp3_comp_table0301_idx (iso2022_jp3_comp_table0300_idx+iso2022_jp3_comp_table0300_len)
  222. #define iso2022_jp3_comp_table0301_len 4
  223. { 0x2b38, 0x2b49 }, /* 0x12B49 = 0x12B38 U+0301 */
  224. { 0x2b37, 0x2b4b }, /* 0x12B4B = 0x12B37 U+0301 */
  225. { 0x2b30, 0x2b4d }, /* 0x12B4D = 0x12B30 U+0301 */
  226. { 0x2b43, 0x2b4f }, /* 0x12B4F = 0x12B43 U+0301 */
  227. #define iso2022_jp3_comp_table309a_idx (iso2022_jp3_comp_table0301_idx+iso2022_jp3_comp_table0301_len)
  228. #define iso2022_jp3_comp_table309a_len 14
  229. { 0x242b, 0x2477 }, /* 0x12477 = 0x1242B U+309A */
  230. { 0x242d, 0x2478 }, /* 0x12478 = 0x1242D U+309A */
  231. { 0x242f, 0x2479 }, /* 0x12479 = 0x1242F U+309A */
  232. { 0x2431, 0x247a }, /* 0x1247A = 0x12431 U+309A */
  233. { 0x2433, 0x247b }, /* 0x1247B = 0x12433 U+309A */
  234. { 0x252b, 0x2577 }, /* 0x12577 = 0x1252B U+309A */
  235. { 0x252d, 0x2578 }, /* 0x12578 = 0x1252D U+309A */
  236. { 0x252f, 0x2579 }, /* 0x12579 = 0x1252F U+309A */
  237. { 0x2531, 0x257a }, /* 0x1257A = 0x12531 U+309A */
  238. { 0x2533, 0x257b }, /* 0x1257B = 0x12533 U+309A */
  239. { 0x253b, 0x257c }, /* 0x1257C = 0x1253B U+309A */
  240. { 0x2544, 0x257d }, /* 0x1257D = 0x12544 U+309A */
  241. { 0x2548, 0x257e }, /* 0x1257E = 0x12548 U+309A */
  242. { 0x2675, 0x2678 }, /* 0x12678 = 0x12675 U+309A */
  243. };
  244. #define SPLIT_STATE \
  245. unsigned short lasttwo = state >> 3; state_t prevstate = state >> 19; state &= 7
  246. #define COMBINE_STATE \
  247. state |= (prevstate << 19) | (lasttwo << 3)
  248. #define COMBINE_STATE_NO_LASTTWO \
  249. /* assume lasttwo == 0, then prevstate is ignored */
  250. static int
  251. iso2022_jp3_wctomb (conv_t conv, unsigned char *r, ucs4_t wc, size_t n)
  252. {
  253. int count = 0;
  254. unsigned char buf[2];
  255. unsigned short jch;
  256. int ret;
  257. state_t state = conv->ostate;
  258. SPLIT_STATE;
  259. if (lasttwo) {
  260. /* Attempt to combine the last character with this one. */
  261. unsigned int idx;
  262. unsigned int len;
  263. if (wc == 0x02e5)
  264. idx = iso2022_jp3_comp_table02e5_idx,
  265. len = iso2022_jp3_comp_table02e5_len;
  266. else if (wc == 0x02e9)
  267. idx = iso2022_jp3_comp_table02e9_idx,
  268. len = iso2022_jp3_comp_table02e9_len;
  269. else if (wc == 0x0300)
  270. idx = iso2022_jp3_comp_table0300_idx,
  271. len = iso2022_jp3_comp_table0300_len;
  272. else if (wc == 0x0301)
  273. idx = iso2022_jp3_comp_table0301_idx,
  274. len = iso2022_jp3_comp_table0301_len;
  275. else if (wc == 0x309a)
  276. idx = iso2022_jp3_comp_table309a_idx,
  277. len = iso2022_jp3_comp_table309a_len;
  278. else
  279. goto not_combining;
  280. do
  281. if (iso2022_jp3_comp_table_data[idx].base == lasttwo)
  282. break;
  283. while (++idx, --len > 0);
  284. if (len > 0) {
  285. /* Output the combined character. */
  286. /* We know the combined character is in JISX0213 plane 1, but
  287. the buffered character may have been in JISX0208 or in
  288. JISX0213 plane 1. */
  289. count = (state != STATE_JISX02131 ? 4 : 0) + 2;
  290. if (n < count)
  291. return RET_TOOSMALL;
  292. if (state != STATE_JISX02131) {
  293. r[0] = ESC;
  294. r[1] = '$';
  295. r[2] = '(';
  296. r[3] = 'Q';
  297. r += 4;
  298. state = STATE_JISX02131;
  299. }
  300. lasttwo = iso2022_jp3_comp_table_data[idx].composed;
  301. r[0] = (lasttwo >> 8) & 0xff;
  302. r[1] = lasttwo & 0xff;
  303. COMBINE_STATE_NO_LASTTWO;
  304. conv->ostate = state;
  305. return count;
  306. }
  307. not_combining:
  308. /* Output the buffered character. */
  309. /* We know it is in JISX0208 or in JISX0213 plane 1. */
  310. count = (prevstate != state ? 3 : 0) + 2;
  311. if (n < count)
  312. return RET_TOOSMALL;
  313. if (prevstate != state) {
  314. if (state != STATE_JISX0208) abort();
  315. r[0] = ESC;
  316. r[1] = '$';
  317. r[2] = 'B';
  318. r += 3;
  319. }
  320. r[0] = (lasttwo >> 8) & 0xff;
  321. r[1] = lasttwo & 0xff;
  322. r += 2;
  323. }
  324. /* Try ASCII. */
  325. ret = ascii_wctomb(conv,buf,wc,1);
  326. if (ret != RET_ILUNI) {
  327. if (ret != 1) abort();
  328. if (buf[0] < 0x80) {
  329. count += (state == STATE_ASCII ? 1 : 4);
  330. if (n < count)
  331. return RET_TOOSMALL;
  332. if (state != STATE_ASCII) {
  333. r[0] = ESC;
  334. r[1] = '(';
  335. r[2] = 'B';
  336. r += 3;
  337. state = STATE_ASCII;
  338. }
  339. r[0] = buf[0];
  340. COMBINE_STATE_NO_LASTTWO;
  341. conv->ostate = state;
  342. return count;
  343. }
  344. }
  345. /* Try JIS X 0201-1976 Roman. */
  346. ret = jisx0201_wctomb(conv,buf,wc,1);
  347. if (ret != RET_ILUNI) {
  348. if (ret != 1) abort();
  349. if (buf[0] < 0x80) {
  350. count += (state == STATE_JISX0201ROMAN ? 1 : 4);
  351. if (n < count)
  352. return RET_TOOSMALL;
  353. if (state != STATE_JISX0201ROMAN) {
  354. r[0] = ESC;
  355. r[1] = '(';
  356. r[2] = 'J';
  357. r += 3;
  358. state = STATE_JISX0201ROMAN;
  359. }
  360. r[0] = buf[0];
  361. COMBINE_STATE_NO_LASTTWO;
  362. conv->ostate = state;
  363. return count;
  364. }
  365. }
  366. jch = ucs4_to_jisx0213(wc);
  367. /* Try JIS X 0208-1990 in place of JIS X 0208-1978 and JIS X 0208-1983. */
  368. ret = jisx0208_wctomb(conv,buf,wc,2);
  369. if (ret != RET_ILUNI) {
  370. if (ret != 2) abort();
  371. if (buf[0] < 0x80 && buf[1] < 0x80) {
  372. if (jch & 0x0080) {
  373. /* A possible match in comp_table_data. Buffer it. */
  374. prevstate = state;
  375. lasttwo = jch & 0x7f7f;
  376. state = STATE_JISX0208;
  377. COMBINE_STATE;
  378. conv->ostate = state;
  379. return count;
  380. } else {
  381. count += (state == STATE_JISX0208 ? 2 : 5);
  382. if (n < count)
  383. return RET_TOOSMALL;
  384. if (state != STATE_JISX0208) {
  385. r[0] = ESC;
  386. r[1] = '$';
  387. r[2] = 'B';
  388. r += 3;
  389. state = STATE_JISX0208;
  390. }
  391. r[0] = buf[0];
  392. r[1] = buf[1];
  393. COMBINE_STATE_NO_LASTTWO;
  394. conv->ostate = state;
  395. return count;
  396. }
  397. }
  398. }
  399. /* Try JISX 0213 plane 1 and JISX 0213 plane 2. */
  400. if (jch != 0) {
  401. if (jch & 0x8000) {
  402. /* JISX 0213 plane 2. */
  403. if (state != STATE_JISX02132) {
  404. count += 4;
  405. if (n < count)
  406. return RET_TOOSMALL;
  407. r[0] = ESC;
  408. r[1] = '$';
  409. r[2] = '(';
  410. r[3] = 'P';
  411. r += 4;
  412. state = STATE_JISX02132;
  413. }
  414. } else {
  415. /* JISX 0213 plane 1. */
  416. if (state != STATE_JISX02131) {
  417. count += 4;
  418. if (n < count)
  419. return RET_TOOSMALL;
  420. r[0] = ESC;
  421. r[1] = '$';
  422. r[2] = '(';
  423. r[3] = 'Q';
  424. r += 4;
  425. state = STATE_JISX02131;
  426. }
  427. }
  428. if (jch & 0x0080) {
  429. /* A possible match in comp_table_data. We have to buffer it. */
  430. /* We know it's a JISX 0213 plane 1 character. */
  431. if (jch & 0x8000) abort();
  432. prevstate = state;
  433. lasttwo = jch & 0x7f7f;
  434. COMBINE_STATE;
  435. conv->ostate = state;
  436. return count;
  437. }
  438. count += 2;
  439. if (n < count)
  440. return RET_TOOSMALL;
  441. r[0] = (jch >> 8) & 0x7f;
  442. r[1] = jch & 0x7f;
  443. COMBINE_STATE_NO_LASTTWO;
  444. conv->ostate = state;
  445. return count;
  446. }
  447. /* Try JIS X 0201-1976 Katakana. This is not officially part of
  448. ISO-2022-JP-3. Therefore we try it after all other attempts. */
  449. ret = jisx0201_wctomb(conv,buf,wc,1);
  450. if (ret != RET_ILUNI) {
  451. if (ret != 1) abort();
  452. if (buf[0] >= 0x80) {
  453. count += (state == STATE_JISX0201KATAKANA ? 1 : 4);
  454. if (n < count)
  455. return RET_TOOSMALL;
  456. if (state != STATE_JISX0201KATAKANA) {
  457. r[0] = ESC;
  458. r[1] = '(';
  459. r[2] = 'I';
  460. r += 3;
  461. state = STATE_JISX0201KATAKANA;
  462. }
  463. r[0] = buf[0]-0x80;
  464. COMBINE_STATE_NO_LASTTWO;
  465. conv->ostate = state;
  466. return count;
  467. }
  468. }
  469. return RET_ILUNI;
  470. }
  471. static int
  472. iso2022_jp3_reset (conv_t conv, unsigned char *r, size_t n)
  473. {
  474. state_t state = conv->ostate;
  475. SPLIT_STATE;
  476. {
  477. int count =
  478. (lasttwo ? (prevstate != state ? 3 : 0) + 2 : 0)
  479. + (state != STATE_ASCII ? 3 : 0);
  480. if (n < count)
  481. return RET_TOOSMALL;
  482. if (lasttwo) {
  483. if (prevstate != state) {
  484. if (state != STATE_JISX0208) abort();
  485. r[0] = ESC;
  486. r[1] = '$';
  487. r[2] = 'B';
  488. r += 3;
  489. }
  490. r[0] = (lasttwo >> 8) & 0xff;
  491. r[1] = lasttwo & 0xff;
  492. r += 2;
  493. }
  494. if (state != STATE_ASCII) {
  495. r[0] = ESC;
  496. r[1] = '(';
  497. r[2] = 'B';
  498. }
  499. /* conv->ostate = 0; will be done by the caller */
  500. return count;
  501. }
  502. }
  503. #undef COMBINE_STATE_NO_LASTTWO
  504. #undef COMBINE_STATE
  505. #undef SPLIT_STATE
  506. #undef STATE_JISX02132
  507. #undef STATE_JISX02131
  508. #undef STATE_JISX0208
  509. #undef STATE_JISX0201KATAKANA
  510. #undef STATE_JISX0201ROMAN
  511. #undef STATE_ASCII