gdkanji.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. /* gdkanji.c (Kanji code converter) */
  2. /* written by Masahito Yamaga (ma@yama-ga.com) */
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include "gd.h"
  7. #include "gdhelpers.h"
  8. #include <stdarg.h>
  9. #if defined(HAVE_ICONV_H) || defined(HAVE_ICONV)
  10. #include <iconv.h>
  11. #include <errno.h>
  12. #endif
  13. #if defined(HAVE_ICONV_H) && !defined(HAVE_ICONV)
  14. #define HAVE_ICONV 1
  15. #endif
  16. #define LIBNAME "any2eucjp()"
  17. #if defined(__MSC__) || defined(__BORLANDC__) || defined(__TURBOC__) || defined(_Windows) || defined(MSDOS)
  18. #ifndef SJISPRE
  19. #define SJISPRE 1
  20. #endif
  21. #endif
  22. #ifdef TRUE
  23. #undef TRUE
  24. #endif
  25. #ifdef FALSE
  26. #undef FALSE
  27. #endif
  28. #define TRUE 1
  29. #define FALSE 0
  30. #define NEW 1
  31. #define OLD 2
  32. #define ESCI 3
  33. #define NEC 4
  34. #define EUC 5
  35. #define SJIS 6
  36. #define EUCORSJIS 7
  37. #define ASCII 8
  38. #define NEWJISSTR "JIS7"
  39. #define OLDJISSTR "jis"
  40. #define EUCSTR "eucJP"
  41. #define SJISSTR "SJIS"
  42. #define ESC 27
  43. #define SS2 142
  44. static void
  45. debug (const char *format,...)
  46. {
  47. #ifdef DEBUG
  48. va_list args;
  49. va_start (args, format);
  50. fprintf (stdout, "%s: ", LIBNAME);
  51. vfprintf (stdout, format, args);
  52. fprintf (stdout, "\n");
  53. va_end (args);
  54. #endif
  55. }
  56. static void
  57. error (const char *format,...)
  58. {
  59. va_list args;
  60. char *tmp;
  61. va_start(args, format);
  62. vspprintf(&tmp, 0, format, args);
  63. va_end(args);
  64. php_error_docref(NULL, E_WARNING, "%s: %s", LIBNAME, tmp);
  65. efree(tmp);
  66. }
  67. /* DetectKanjiCode() derived from DetectCodeType() by Ken Lunde. */
  68. static int
  69. DetectKanjiCode (unsigned char *str)
  70. {
  71. static int whatcode = ASCII;
  72. int oldcode = ASCII;
  73. int c, i;
  74. char *lang = NULL;
  75. c = '\1';
  76. i = 0;
  77. if (whatcode != EUCORSJIS && whatcode != ASCII)
  78. {
  79. oldcode = whatcode;
  80. whatcode = ASCII;
  81. }
  82. while ((whatcode == EUCORSJIS || whatcode == ASCII) && c != '\0')
  83. {
  84. if ((c = str[i++]) != '\0')
  85. {
  86. if (c == ESC)
  87. {
  88. c = str[i++];
  89. if (c == '$')
  90. {
  91. c = str[i++];
  92. if (c == 'B')
  93. whatcode = NEW;
  94. else if (c == '@')
  95. whatcode = OLD;
  96. }
  97. else if (c == '(')
  98. {
  99. c = str[i++];
  100. if (c == 'I')
  101. whatcode = ESCI;
  102. }
  103. else if (c == 'K')
  104. whatcode = NEC;
  105. }
  106. else if ((c >= 129 && c <= 141) || (c >= 143 && c <= 159))
  107. whatcode = SJIS;
  108. else if (c == SS2)
  109. {
  110. c = str[i++];
  111. if ((c >= 64 && c <= 126) || (c >= 128 && c <= 160) || (c >= 224 && c <= 252))
  112. whatcode = SJIS;
  113. else if (c >= 161 && c <= 223)
  114. whatcode = EUCORSJIS;
  115. }
  116. else if (c >= 161 && c <= 223)
  117. {
  118. c = str[i++];
  119. if (c >= 240 && c <= 254)
  120. whatcode = EUC;
  121. else if (c >= 161 && c <= 223)
  122. whatcode = EUCORSJIS;
  123. else if (c >= 224 && c <= 239)
  124. {
  125. whatcode = EUCORSJIS;
  126. while (c >= 64 && whatcode == EUCORSJIS)
  127. {
  128. if (c >= 129)
  129. {
  130. if (c <= 141 || (c >= 143 && c <= 159))
  131. whatcode = SJIS;
  132. else if (c >= 253 && c <= 254)
  133. whatcode = EUC;
  134. }
  135. c = str[i++];
  136. }
  137. }
  138. else if (c <= 159)
  139. whatcode = SJIS;
  140. }
  141. else if (c >= 240 && c <= 254)
  142. whatcode = EUC;
  143. else if (c >= 224 && c <= 239)
  144. {
  145. c = str[i++];
  146. if ((c >= 64 && c <= 126) || (c >= 128 && c <= 160))
  147. whatcode = SJIS;
  148. else if (c >= 253 && c <= 254)
  149. whatcode = EUC;
  150. else if (c >= 161 && c <= 252)
  151. whatcode = EUCORSJIS;
  152. }
  153. }
  154. }
  155. #ifdef DEBUG
  156. if (whatcode == ASCII)
  157. debug ("Kanji code not included.");
  158. else if (whatcode == EUCORSJIS)
  159. debug ("Kanji code not detected.");
  160. else
  161. debug ("Kanji code detected at %d byte.", i);
  162. #endif
  163. if (whatcode == EUCORSJIS && oldcode != ASCII)
  164. whatcode = oldcode;
  165. if (whatcode == EUCORSJIS)
  166. {
  167. if (getenv ("LC_ALL"))
  168. lang = getenv ("LC_ALL");
  169. else if (getenv ("LC_CTYPE"))
  170. lang = getenv ("LC_CTYPE");
  171. else if (getenv ("LANG"))
  172. lang = getenv ("LANG");
  173. if (lang)
  174. {
  175. if (strcmp (lang, "ja_JP.SJIS") == 0 ||
  176. #ifdef hpux
  177. strcmp (lang, "japanese") == 0 ||
  178. #endif
  179. strcmp (lang, "ja_JP.mscode") == 0 ||
  180. strcmp (lang, "ja_JP.PCK") == 0)
  181. whatcode = SJIS;
  182. else if (strncmp (lang, "ja", 2) == 0)
  183. #ifdef SJISPRE
  184. whatcode = SJIS;
  185. #else
  186. whatcode = EUC;
  187. #endif
  188. }
  189. }
  190. if (whatcode == EUCORSJIS)
  191. #ifdef SJISPRE
  192. whatcode = SJIS;
  193. #else
  194. whatcode = EUC;
  195. #endif
  196. return whatcode;
  197. }
  198. /* SJIStoJIS() is sjis2jis() by Ken Lunde. */
  199. static void
  200. SJIStoJIS (int *p1, int *p2)
  201. {
  202. register unsigned char c1 = *p1;
  203. register unsigned char c2 = *p2;
  204. register int adjust = c2 < 159;
  205. register int rowOffset = c1 < 160 ? 112 : 176;
  206. register int cellOffset = adjust ? (31 + (c2 > 127)) : 126;
  207. *p1 = ((c1 - rowOffset) << 1) - adjust;
  208. *p2 -= cellOffset;
  209. }
  210. /* han2zen() was derived from han2zen() written by Ken Lunde. */
  211. #define IS_DAKU(c) ((c >= 182 && c <= 196) || (c >= 202 && c <= 206) || (c == 179))
  212. #define IS_HANDAKU(c) (c >= 202 && c <= 206)
  213. static void
  214. han2zen (int *p1, int *p2)
  215. {
  216. int c = *p1;
  217. int daku = FALSE;
  218. int handaku = FALSE;
  219. int mtable[][2] =
  220. {
  221. {129, 66},
  222. {129, 117},
  223. {129, 118},
  224. {129, 65},
  225. {129, 69},
  226. {131, 146},
  227. {131, 64},
  228. {131, 66},
  229. {131, 68},
  230. {131, 70},
  231. {131, 72},
  232. {131, 131},
  233. {131, 133},
  234. {131, 135},
  235. {131, 98},
  236. {129, 91},
  237. {131, 65},
  238. {131, 67},
  239. {131, 69},
  240. {131, 71},
  241. {131, 73},
  242. {131, 74},
  243. {131, 76},
  244. {131, 78},
  245. {131, 80},
  246. {131, 82},
  247. {131, 84},
  248. {131, 86},
  249. {131, 88},
  250. {131, 90},
  251. {131, 92},
  252. {131, 94},
  253. {131, 96},
  254. {131, 99},
  255. {131, 101},
  256. {131, 103},
  257. {131, 105},
  258. {131, 106},
  259. {131, 107},
  260. {131, 108},
  261. {131, 109},
  262. {131, 110},
  263. {131, 113},
  264. {131, 116},
  265. {131, 119},
  266. {131, 122},
  267. {131, 125},
  268. {131, 126},
  269. {131, 128},
  270. {131, 129},
  271. {131, 130},
  272. {131, 132},
  273. {131, 134},
  274. {131, 136},
  275. {131, 137},
  276. {131, 138},
  277. {131, 139},
  278. {131, 140},
  279. {131, 141},
  280. {131, 143},
  281. {131, 147},
  282. {129, 74},
  283. {129, 75}
  284. };
  285. if (*p2 == 222 && IS_DAKU (*p1))
  286. daku = TRUE; /* Daku-ten */
  287. else if (*p2 == 223 && IS_HANDAKU (*p1))
  288. handaku = TRUE; /* Han-daku-ten */
  289. *p1 = mtable[c - 161][0];
  290. *p2 = mtable[c - 161][1];
  291. if (daku)
  292. {
  293. if ((*p2 >= 74 && *p2 <= 103) || (*p2 >= 110 && *p2 <= 122))
  294. (*p2)++;
  295. else if (*p2 == 131 || *p2 == 69)
  296. *p2 = 148;
  297. }
  298. else if (handaku && *p2 >= 110 && *p2 <= 122)
  299. (*p2) += 2;
  300. }
  301. /* Recast strcpy to handle unsigned chars used below. */
  302. #define ustrcpy(A,B) (strcpy((char*)(A),(const char*)(B)))
  303. static void
  304. do_convert (unsigned char *to, unsigned char *from, const char *code)
  305. {
  306. #ifdef HAVE_ICONV
  307. iconv_t cd;
  308. size_t from_len, to_len;
  309. if ((cd = iconv_open (EUCSTR, code)) == (iconv_t) - 1)
  310. {
  311. error ("iconv_open() error");
  312. if (errno == EINVAL)
  313. error ("invalid code specification: \"%s\" or \"%s\"",
  314. EUCSTR, code);
  315. strcpy ((char *) to, (const char *) from);
  316. return;
  317. }
  318. from_len = strlen ((const char *) from) + 1;
  319. to_len = BUFSIZ;
  320. if ((int) iconv(cd, (char **) &from, &from_len, (char **) &to, &to_len) == -1)
  321. {
  322. if (errno == EINVAL)
  323. error ("invalid end of input string");
  324. else if (errno == EILSEQ)
  325. error ("invalid code in input string");
  326. else if (errno == E2BIG)
  327. error ("output buffer overflow at do_convert()");
  328. else
  329. error ("something happen");
  330. strcpy ((char *) to, (const char *) from);
  331. return;
  332. }
  333. if (iconv_close (cd) != 0)
  334. {
  335. error ("iconv_close() error");
  336. }
  337. #else
  338. int p1, p2, i, j;
  339. int jisx0208 = FALSE;
  340. int hankaku = FALSE;
  341. j = 0;
  342. if (strcmp (code, NEWJISSTR) == 0 || strcmp (code, OLDJISSTR) == 0)
  343. {
  344. for (i = 0; from[i] != '\0' && j < BUFSIZ; i++)
  345. {
  346. if (from[i] == ESC)
  347. {
  348. i++;
  349. if (from[i] == '$')
  350. {
  351. jisx0208 = TRUE;
  352. hankaku = FALSE;
  353. i++;
  354. }
  355. else if (from[i] == '(')
  356. {
  357. jisx0208 = FALSE;
  358. i++;
  359. if (from[i] == 'I') /* Hankaku Kana */
  360. hankaku = TRUE;
  361. else
  362. hankaku = FALSE;
  363. }
  364. }
  365. else
  366. {
  367. if (jisx0208)
  368. to[j++] = from[i] + 128;
  369. else if (hankaku)
  370. {
  371. to[j++] = SS2;
  372. to[j++] = from[i] + 128;
  373. }
  374. else
  375. to[j++] = from[i];
  376. }
  377. }
  378. }
  379. else if (strcmp (code, SJISSTR) == 0)
  380. {
  381. for (i = 0; from[i] != '\0' && j < BUFSIZ; i++)
  382. {
  383. p1 = from[i];
  384. if (p1 < 127)
  385. to[j++] = p1;
  386. else if ((p1 >= 161) && (p1 <= 223))
  387. { /* Hankaku Kana */
  388. to[j++] = SS2;
  389. to[j++] = p1;
  390. }
  391. else
  392. {
  393. p2 = from[++i];
  394. SJIStoJIS (&p1, &p2);
  395. to[j++] = p1 + 128;
  396. to[j++] = p2 + 128;
  397. }
  398. }
  399. }
  400. else
  401. {
  402. error ("invalid code specification: \"%s\"", code);
  403. return;
  404. }
  405. if (j >= BUFSIZ)
  406. {
  407. error ("output buffer overflow at do_convert()");
  408. ustrcpy (to, from);
  409. }
  410. else
  411. to[j] = '\0';
  412. #endif /* HAVE_ICONV */
  413. }
  414. static int
  415. do_check_and_conv (unsigned char *to, unsigned char *from)
  416. {
  417. static unsigned char tmp[BUFSIZ];
  418. int p1, p2, i, j;
  419. int kanji = TRUE;
  420. switch (DetectKanjiCode (from))
  421. {
  422. case NEW:
  423. debug ("Kanji code is New JIS.");
  424. do_convert (tmp, from, NEWJISSTR);
  425. break;
  426. case OLD:
  427. debug ("Kanji code is Old JIS.");
  428. do_convert (tmp, from, OLDJISSTR);
  429. break;
  430. case ESCI:
  431. debug ("This string includes Hankaku-Kana (jisx0201) escape sequence [ESC] + ( + I.");
  432. do_convert (tmp, from, NEWJISSTR);
  433. break;
  434. case NEC:
  435. debug ("Kanji code is NEC Kanji.");
  436. error ("cannot convert NEC Kanji.");
  437. ustrcpy (tmp, from);
  438. kanji = FALSE;
  439. break;
  440. case EUC:
  441. debug ("Kanji code is EUC.");
  442. ustrcpy (tmp, from);
  443. break;
  444. case SJIS:
  445. debug ("Kanji code is SJIS.");
  446. do_convert (tmp, from, SJISSTR);
  447. break;
  448. case EUCORSJIS:
  449. debug ("Kanji code is EUC or SJIS.");
  450. ustrcpy (tmp, from);
  451. kanji = FALSE;
  452. break;
  453. case ASCII:
  454. debug ("This is ASCII string.");
  455. ustrcpy (tmp, from);
  456. kanji = FALSE;
  457. break;
  458. default:
  459. debug ("This string includes unknown code.");
  460. ustrcpy (tmp, from);
  461. kanji = FALSE;
  462. break;
  463. }
  464. /* Hankaku Kana ---> Zenkaku Kana */
  465. if (kanji)
  466. {
  467. j = 0;
  468. for (i = 0; tmp[i] != '\0' && j < BUFSIZ; i++)
  469. {
  470. if (tmp[i] == SS2)
  471. {
  472. p1 = tmp[++i];
  473. if (tmp[i + 1] == SS2)
  474. {
  475. p2 = tmp[i + 2];
  476. if (p2 == 222 || p2 == 223)
  477. i += 2;
  478. else
  479. p2 = 0;
  480. }
  481. else
  482. p2 = 0;
  483. han2zen (&p1, &p2);
  484. SJIStoJIS (&p1, &p2);
  485. to[j++] = p1 + 128;
  486. to[j++] = p2 + 128;
  487. }
  488. else
  489. to[j++] = tmp[i];
  490. }
  491. if (j >= BUFSIZ)
  492. {
  493. error ("output buffer overflow at Hankaku --> Zenkaku");
  494. ustrcpy (to, tmp);
  495. }
  496. else
  497. to[j] = '\0';
  498. }
  499. else
  500. ustrcpy (to, tmp);
  501. return kanji;
  502. }
  503. int
  504. any2eucjp (unsigned char *dest, unsigned char *src, unsigned int dest_max)
  505. {
  506. static unsigned char tmp_dest[BUFSIZ];
  507. int ret;
  508. if (strlen ((const char *) src) >= BUFSIZ)
  509. {
  510. error ("input string too large");
  511. return -1;
  512. }
  513. if (dest_max > BUFSIZ)
  514. {
  515. error ("invalid maximum size of destination\nit should be less than %d.", BUFSIZ);
  516. return -1;
  517. }
  518. ret = do_check_and_conv (tmp_dest, src);
  519. if (strlen ((const char *) tmp_dest) >= dest_max)
  520. {
  521. error ("output buffer overflow");
  522. ustrcpy (dest, src);
  523. return -1;
  524. }
  525. ustrcpy (dest, tmp_dest);
  526. return ret;
  527. }
  528. #if 0
  529. unsigned int
  530. strwidth (unsigned char *s)
  531. {
  532. unsigned char *t;
  533. unsigned int i;
  534. t = (unsigned char *) gdMalloc (BUFSIZ);
  535. any2eucjp (t, s, BUFSIZ);
  536. i = strlen (t);
  537. gdFree (t);
  538. return i;
  539. }
  540. #ifdef DEBUG
  541. int
  542. main ()
  543. {
  544. unsigned char input[BUFSIZ];
  545. unsigned char *output;
  546. unsigned char *str;
  547. int c, i = 0;
  548. while ((c = fgetc (stdin)) != '\n' && i < BUFSIZ)
  549. input[i++] = c;
  550. input[i] = '\0';
  551. printf ("input : %d bytes\n", strlen ((const char *) input));
  552. printf ("output: %d bytes\n", strwidth (input));
  553. output = (unsigned char *) gdMalloc (BUFSIZ);
  554. any2eucjp (output, input, BUFSIZ);
  555. str = output;
  556. while (*str != '\0')
  557. putchar (*(str++));
  558. putchar ('\n');
  559. gdFree (output);
  560. return 0;
  561. }
  562. #endif
  563. #endif