conv_jis.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. #ifdef PLAN9
  2. #include <u.h>
  3. #include <libc.h>
  4. #include <bio.h>
  5. #else
  6. #include <stdio.h>
  7. #include <unistd.h>
  8. #include "plan9.h"
  9. #endif
  10. #include "hdr.h"
  11. #include "conv.h"
  12. #include "kuten208.h"
  13. #include "jis.h"
  14. /*
  15. a state machine for interpreting all sorts of encodings
  16. */
  17. static void
  18. alljis(int c, Rune **r, long input_loc)
  19. {
  20. static enum { state0, state1, state2, state3, state4 } state = state0;
  21. static int set8 = 0;
  22. static int japan646 = 0;
  23. static int lastc;
  24. int n;
  25. long l;
  26. again:
  27. switch(state)
  28. {
  29. case state0: /* idle state */
  30. if(c == ESC){ state = state1; return; }
  31. if(c < 0) return;
  32. if(!set8 && (c < 128)){
  33. if(japan646){
  34. switch(c)
  35. {
  36. case '\\': emit(0xA5); return; /* yen */
  37. case '~': emit(0xAF); return; /* spacing macron */
  38. default: emit(c); return;
  39. }
  40. } else {
  41. emit(c);
  42. return;
  43. }
  44. }
  45. if(c < 0x21){ /* guard against bogus characters in JIS mode */
  46. if(squawk)
  47. EPR "%s: non-JIS character %02x in %s near byte %ld\n", argv0, c, file, input_loc);
  48. emit(c);
  49. return;
  50. }
  51. lastc = c; state = state4; return;
  52. case state1: /* seen an escape */
  53. if(c == '$'){ state = state2; return; }
  54. if(c == '('){ state = state3; return; }
  55. emit(ESC); state = state0; goto again;
  56. case state2: /* may be shifting into JIS */
  57. if((c == '@') || (c == 'B')){
  58. set8 = 1; state = state0; return;
  59. }
  60. emit(ESC); emit('$'); state = state0; goto again;
  61. case state3: /* may be shifting out of JIS */
  62. if((c == 'J') || (c == 'H') || (c == 'B')){
  63. japan646 = (c == 'J');
  64. set8 = 0; state = state0; return;
  65. }
  66. emit(ESC); emit('('); state = state0; goto again;
  67. case state4: /* two part char */
  68. if(c < 0){
  69. if(squawk)
  70. EPR "%s: unexpected EOF in %s\n", argv0, file);
  71. c = 0x21 | (lastc&0x80);
  72. }
  73. if(CANS2J(lastc, c)){ /* ms dos sjis */
  74. int hi = lastc, lo = c;
  75. S2J(hi, lo); /* convert to 208 */
  76. n = hi*100 + lo - 3232; /* convert to kuten208 */
  77. } else
  78. n = (lastc&0x7F)*100 + (c&0x7f) - 3232; /* kuten208 */
  79. if((n >= KUTEN208MAX) || ((l = tabkuten208[n]) == -1)){
  80. nerrors++;
  81. if(squawk)
  82. EPR "%s: unknown kuten208 %d (from 0x%x,0x%x) near byte %ld in %s\n", argv0, n, lastc, c, input_loc, file);
  83. if(!clean)
  84. emit(BADMAP);
  85. } else {
  86. if(l < 0){
  87. l = -l;
  88. if(squawk)
  89. EPR "%s: ambiguous kuten208 %d (mapped to 0x%lx) near byte %ld in %s\n", argv0, n, l, input_loc, file);
  90. }
  91. emit(l);
  92. }
  93. state = state0;
  94. }
  95. }
  96. /*
  97. a state machine for interpreting ms-kanji == shift-jis.
  98. */
  99. static void
  100. ms(int c, Rune **r, long input_loc)
  101. {
  102. static enum { state0, state1, state2, state3, state4 } state = state0;
  103. static int set8 = 0;
  104. static int japan646 = 0;
  105. static int lastc;
  106. int n;
  107. long l;
  108. again:
  109. switch(state)
  110. {
  111. case state0: /* idle state */
  112. if(c == ESC){ state = state1; return; }
  113. if(c < 0) return;
  114. if(!set8 && (c < 128)){
  115. if(japan646){
  116. switch(c)
  117. {
  118. case '\\': emit(0xA5); return; /* yen */
  119. case '~': emit(0xAF); return; /* spacing macron */
  120. default: emit(c); return;
  121. }
  122. } else {
  123. emit(c);
  124. return;
  125. }
  126. }
  127. lastc = c; state = state4; return;
  128. case state1: /* seen an escape */
  129. if(c == '$'){ state = state2; return; }
  130. if(c == '('){ state = state3; return; }
  131. emit(ESC); state = state0; goto again;
  132. case state2: /* may be shifting into JIS */
  133. if((c == '@') || (c == 'B')){
  134. set8 = 1; state = state0; return;
  135. }
  136. emit(ESC); emit('$'); state = state0; goto again;
  137. case state3: /* may be shifting out of JIS */
  138. if((c == 'J') || (c == 'H') || (c == 'B')){
  139. japan646 = (c == 'J');
  140. set8 = 0; state = state0; return;
  141. }
  142. emit(ESC); emit('('); state = state0; goto again;
  143. case state4: /* two part char */
  144. if(c < 0){
  145. if(squawk)
  146. EPR "%s: unexpected EOF in %s\n", argv0, file);
  147. c = 0x21 | (lastc&0x80);
  148. }
  149. if(CANS2J(lastc, c)){ /* ms dos sjis */
  150. int hi = lastc, lo = c;
  151. S2J(hi, lo); /* convert to 208 */
  152. n = hi*100 + lo - 3232; /* convert to kuten208 */
  153. } else {
  154. nerrors++;
  155. if(squawk)
  156. EPR "%s: illegal byte pair (0x%x,0x%x) near byte %ld in %s\n", argv0, lastc, c, input_loc, file);
  157. if(!clean)
  158. emit(BADMAP);
  159. state = state0;
  160. goto again;
  161. }
  162. if((n >= KUTEN208MAX) || ((l = tabkuten208[n]) == -1)){
  163. nerrors++;
  164. if(squawk)
  165. EPR "%s: unknown kuten208 %d (from 0x%x,0x%x) near byte %ld in %s\n", argv0, n, lastc, c, input_loc, file);
  166. if(!clean)
  167. emit(BADMAP);
  168. } else {
  169. if(l < 0){
  170. l = -l;
  171. if(squawk)
  172. EPR "%s: ambiguous kuten208 %d (mapped to 0x%lx) near byte %ld in %s\n", argv0, n, l, input_loc, file);
  173. }
  174. emit(l);
  175. }
  176. state = state0;
  177. }
  178. }
  179. /*
  180. a state machine for interpreting ujis == EUC
  181. */
  182. static void
  183. ujis(int c, Rune **r, long input_loc)
  184. {
  185. static enum { state0, state1 } state = state0;
  186. static int lastc;
  187. int n;
  188. long l;
  189. switch(state)
  190. {
  191. case state0: /* idle state */
  192. if(c < 0) return;
  193. if(c < 128){
  194. emit(c);
  195. return;
  196. }
  197. if(c == 0x8e){ /* codeset 2 */
  198. nerrors++;
  199. if(squawk)
  200. EPR "%s: unknown codeset 2 near byte %ld in %s\n", argv0, input_loc, file);
  201. if(!clean)
  202. emit(BADMAP);
  203. return;
  204. }
  205. if(c == 0x8f){ /* codeset 3 */
  206. nerrors++;
  207. if(squawk)
  208. EPR "%s: unknown codeset 3 near byte %ld in %s\n", argv0, input_loc, file);
  209. if(!clean)
  210. emit(BADMAP);
  211. return;
  212. }
  213. lastc = c;
  214. state = state1;
  215. return;
  216. case state1: /* two part char */
  217. if(c < 0){
  218. if(squawk)
  219. EPR "%s: unexpected EOF in %s\n", argv0, file);
  220. c = 0xA1;
  221. }
  222. n = (lastc&0x7F)*100 + (c&0x7F) - 3232; /* kuten208 */
  223. if((n >= KUTEN208MAX) || ((l = tabkuten208[n]) == -1)){
  224. nerrors++;
  225. if(squawk)
  226. EPR "%s: unknown kuten208 %d (from 0x%x,0x%x) near byte %ld in %s\n", argv0, n, lastc, c, input_loc, file);
  227. if(!clean)
  228. emit(BADMAP);
  229. } else {
  230. if(l < 0){
  231. l = -l;
  232. if(squawk)
  233. EPR "%s: ambiguous kuten208 %d (mapped to 0x%lx) near byte %ld in %s\n", argv0, n, l, input_loc, file);
  234. }
  235. emit(l);
  236. }
  237. state = state0;
  238. }
  239. }
  240. /*
  241. a state machine for interpreting jis-kanji == 2022-JP
  242. */
  243. static void
  244. jis(int c, Rune **r, long input_loc)
  245. {
  246. static enum { state0, state1, state2, state3, state4 } state = state0;
  247. static int set8 = 0;
  248. static int japan646 = 0;
  249. static int lastc;
  250. int n;
  251. long l;
  252. again:
  253. switch(state)
  254. {
  255. case state0: /* idle state */
  256. if(c == ESC){ state = state1; return; }
  257. if(c < 0) return;
  258. if(!set8 && (c < 128)){
  259. if(japan646){
  260. switch(c)
  261. {
  262. case '\\': emit(0xA5); return; /* yen */
  263. case '~': emit(0xAF); return; /* spacing macron */
  264. default: emit(c); return;
  265. }
  266. } else {
  267. emit(c);
  268. return;
  269. }
  270. }
  271. lastc = c; state = state4; return;
  272. case state1: /* seen an escape */
  273. if(c == '$'){ state = state2; return; }
  274. if(c == '('){ state = state3; return; }
  275. emit(ESC); state = state0; goto again;
  276. case state2: /* may be shifting into JIS */
  277. if((c == '@') || (c == 'B')){
  278. set8 = 1; state = state0; return;
  279. }
  280. emit(ESC); emit('$'); state = state0; goto again;
  281. case state3: /* may be shifting out of JIS */
  282. if((c == 'J') || (c == 'H') || (c == 'B')){
  283. japan646 = (c == 'J');
  284. set8 = 0; state = state0; return;
  285. }
  286. emit(ESC); emit('('); state = state0; goto again;
  287. case state4: /* two part char */
  288. if(c < 0){
  289. if(squawk)
  290. EPR "%s: unexpected EOF in %s\n", argv0, file);
  291. c = 0x21 | (lastc&0x80);
  292. }
  293. if((lastc&0x80) != (c&0x80)){ /* guard against latin1 in jis */
  294. emit(lastc);
  295. state = state0;
  296. goto again;
  297. }
  298. n = (lastc&0x7F)*100 + (c&0x7f) - 3232; /* kuten208 */
  299. if((n >= KUTEN208MAX) || ((l = tabkuten208[n]) == -1)){
  300. nerrors++;
  301. if(squawk)
  302. EPR "%s: unknown kuten208 %d (from 0x%x,0x%x) near byte %ld in %s\n", argv0, n, lastc, c, input_loc, file);
  303. if(!clean)
  304. emit(BADMAP);
  305. } else {
  306. if(l < 0){
  307. l = -l;
  308. if(squawk)
  309. EPR "%s: ambiguous kuten208 %d (mapped to 0x%lx) near byte %ld in %s\n", argv0, n, l, input_loc, file);
  310. }
  311. emit(l);
  312. }
  313. state = state0;
  314. }
  315. }
  316. static void
  317. do_in(int fd, void (*procfn)(int, Rune **, long), struct convert *out)
  318. {
  319. Rune ob[N];
  320. Rune *r, *re;
  321. uchar ibuf[N];
  322. int n, i;
  323. long nin;
  324. r = ob;
  325. re = ob+N-3;
  326. nin = 0;
  327. while((n = read(fd, ibuf, sizeof ibuf)) > 0){
  328. for(i = 0; i < n; i++){
  329. (*procfn)(ibuf[i], &r, nin++);
  330. if(r >= re){
  331. OUT(out, ob, r-ob);
  332. r = ob;
  333. }
  334. }
  335. if(r > ob){
  336. OUT(out, ob, r-ob);
  337. r = ob;
  338. }
  339. }
  340. (*procfn)(-1, &r, nin);
  341. if(r > ob)
  342. OUT(out, ob, r-ob);
  343. }
  344. void
  345. jis_in(int fd, long *notused, struct convert *out)
  346. {
  347. USED(notused);
  348. do_in(fd, alljis, out);
  349. }
  350. void
  351. ujis_in(int fd, long *notused, struct convert *out)
  352. {
  353. USED(notused);
  354. do_in(fd, ujis, out);
  355. }
  356. void
  357. msjis_in(int fd, long *notused, struct convert *out)
  358. {
  359. USED(notused);
  360. do_in(fd, ms, out);
  361. }
  362. void
  363. jisjis_in(int fd, long *notused, struct convert *out)
  364. {
  365. USED(notused);
  366. do_in(fd, jis, out);
  367. }
  368. static int first = 1;
  369. static void
  370. tab_init(void)
  371. {
  372. int i;
  373. long l;
  374. first = 0;
  375. for(i = 0; i < NRUNE; i++)
  376. tab[i] = -1;
  377. for(i = 0; i < KUTEN208MAX; i++)
  378. if((l = tabkuten208[i]) != -1){
  379. if(l < 0)
  380. tab[-l] = i;
  381. else
  382. tab[l] = i;
  383. }
  384. }
  385. /* jis-kanji, or ISO 2022-JP */
  386. void
  387. jisjis_out(Rune *base, int n, long *notused)
  388. {
  389. char *p;
  390. int i;
  391. Rune r;
  392. static enum { ascii, japan646, jp2022 } state = ascii;
  393. USED(notused);
  394. if(first)
  395. tab_init();
  396. nrunes += n;
  397. p = obuf;
  398. for(i = 0; i < n; i++){
  399. r = base[i];
  400. if(r < 128){
  401. if(state == jp2022){
  402. *p++ = ESC; *p++ = '('; *p++ = 'B';
  403. state = ascii;
  404. }
  405. *p++ = r;
  406. } else {
  407. if(tab[r] != -1){
  408. if(state != jp2022){
  409. *p++ = ESC; *p++ = '$'; *p++ = 'B';
  410. state = jp2022;
  411. }
  412. *p++ = tab[r]/100 + ' ';
  413. *p++ = tab[r]%100 + ' ';
  414. continue;
  415. }
  416. if(squawk)
  417. EPR "%s: rune 0x%x not in output cs\n", argv0, r);
  418. nerrors++;
  419. if(clean)
  420. continue;
  421. *p++ = BYTEBADMAP;
  422. }
  423. }
  424. noutput += p-obuf;
  425. if(p > obuf)
  426. write(1, obuf, p-obuf);
  427. }
  428. /* ms-kanji, or Shift-JIS */
  429. void
  430. msjis_out(Rune *base, int n, long *notused)
  431. {
  432. char *p;
  433. int i, hi, lo;
  434. Rune r;
  435. USED(notused);
  436. if(first)
  437. tab_init();
  438. nrunes += n;
  439. p = obuf;
  440. for(i = 0; i < n; i++){
  441. r = base[i];
  442. if(r < 128)
  443. *p++ = r;
  444. else {
  445. if(tab[r] != -1){
  446. hi = tab[r]/100 + ' ';
  447. lo = tab[r]%100 + ' ';
  448. J2S(hi, lo);
  449. *p++ = hi;
  450. *p++ = lo;
  451. continue;
  452. }
  453. if(squawk)
  454. EPR "%s: rune 0x%x not in output cs\n", argv0, r);
  455. nerrors++;
  456. if(clean)
  457. continue;
  458. *p++ = BYTEBADMAP;
  459. }
  460. }
  461. noutput += p-obuf;
  462. if(p > obuf)
  463. write(1, obuf, p-obuf);
  464. }
  465. /* ujis, or EUC */
  466. void
  467. ujis_out(Rune *base, int n, long *notused)
  468. {
  469. char *p;
  470. int i;
  471. Rune r;
  472. USED(notused);
  473. if(first)
  474. tab_init();
  475. nrunes += n;
  476. p = obuf;
  477. for(i = 0; i < n; i++){
  478. r = base[i];
  479. if(r < 128)
  480. *p++ = r;
  481. else {
  482. if(tab[r] != -1){
  483. *p++ = 0x80 | (tab[r]/100 + ' ');
  484. *p++ = 0x80 | (tab[r]%100 + ' ');
  485. continue;
  486. }
  487. if(squawk)
  488. EPR "%s: rune 0x%x not in output cs\n", argv0, r);
  489. nerrors++;
  490. if(clean)
  491. continue;
  492. *p++ = BYTEBADMAP;
  493. }
  494. }
  495. noutput += p-obuf;
  496. if(p > obuf)
  497. write(1, obuf, p-obuf);
  498. }