12#if defined(HAVE_ICONV_H) || defined(HAVE_ICONV)
17#if defined(HAVE_ICONV_H) && !defined(HAVE_ICONV)
21#define LIBNAME "any2eucjp()"
23#if defined(__MSC__) || defined(__BORLANDC__) || defined(__TURBOC__) || defined(_Windows) || defined(MSDOS)
48#define NEWJISSTR "JIS7"
49#define OLDJISSTR "jis"
57debug (
const char *format,...)
62 va_start (
args, format);
71error (
const char *format,...)
76 va_start(
args, format);
86DetectKanjiCode (
unsigned char *str)
88 static int whatcode =
ASCII;
104 if ((c = str[i++]) !=
'\0')
126 else if ((c >= 129 && c <= 141) || (c >= 143 && c <= 159))
131 if ((c >= 64 && c <= 126) || (c >= 128 && c <= 160) || (c >= 224 && c <= 252))
133 else if (c >= 161 && c <= 223)
136 else if (c >= 161 && c <= 223)
139 if (c >= 240 && c <= 254)
141 else if (c >= 161 && c <= 223)
143 else if (c >= 224 && c <= 239)
150 if (c <= 141 || (c >= 143 && c <= 159))
152 else if (c >= 253 && c <= 254)
161 else if (c >= 240 && c <= 254)
163 else if (c >= 224 && c <= 239)
166 if ((c >= 64 && c <= 126) || (c >= 128 && c <= 160))
168 else if (c >= 253 && c <= 254)
170 else if (c >= 161 && c <= 252)
177 if (whatcode ==
ASCII)
178 debug (
"Kanji code not included.");
180 debug (
"Kanji code not detected.");
182 debug (
"Kanji code detected at %d byte.", i);
192 else if (
getenv (
"LC_CTYPE"))
193 lang =
getenv (
"LC_CTYPE");
199 if (
strcmp (lang,
"ja_JP.SJIS") == 0 ||
201 strcmp (lang,
"japanese") == 0 ||
203 strcmp (lang,
"ja_JP.mscode") == 0 ||
204 strcmp (lang,
"ja_JP.PCK") == 0)
206 else if (
strncmp (lang,
"ja", 2) == 0)
228SJIStoJIS (
int *p1,
int *p2)
230 register unsigned char c1 = *p1;
231 register unsigned char c2 = *p2;
232 register int adjust = c2 < 159;
233 register int rowOffset = c1 < 160 ? 112 : 176;
234 register int cellOffset = adjust ? (31 + (c2 > 127)) : 126;
236 *p1 = ((c1 - rowOffset) << 1) - adjust;
242#define IS_DAKU(c) ((c >= 182 && c <= 196) || (c >= 202 && c <= 206) || (c == 179))
243#define IS_HANDAKU(c) (c >= 202 && c <= 206)
246han2zen (
int *p1,
int *p2)
318 if (*p2 == 222 &&
IS_DAKU (*p1))
323 *p1 = mtable[c - 161][0];
324 *p2 = mtable[c - 161][1];
328 if ((*p2 >= 74 && *p2 <= 103) || (*p2 >= 110 && *p2 <= 122))
330 else if (*p2 == 131 || *p2 == 69)
333 else if (handaku && *p2 >= 110 && *p2 <= 122)
338#define ustrcpy(A,B) (strcpy((char*)(A),(const char*)(B)))
341do_convert (
unsigned char *to,
unsigned char *from,
const char *code)
345 size_t from_len, to_len;
347 if ((cd = iconv_open (
EUCSTR, code)) == (iconv_t) - 1)
349 error (
"iconv_open() error");
351 error (
"invalid code specification: \"%s\" or \"%s\"",
353 strcpy ((
char *) to, (
const char *) from);
357 from_len =
strlen ((
const char *) from) + 1;
360 if ((
int)
iconv(cd, (
char **) &from, &from_len, (
char **) &to, &to_len) == -1)
363 error (
"invalid end of input string");
364 else if (
errno == EILSEQ)
365 error (
"invalid code in input string");
366 else if (
errno == E2BIG)
367 error (
"output buffer overflow at do_convert()");
369 error (
"something happen");
370 strcpy ((
char *) to, (
const char *) from);
374 if (iconv_close (cd) != 0)
376 error (
"iconv_close() error");
380 int jisx0208 =
FALSE;
386 for (i = 0; from[i] !=
'\0' &&
j < BUFSIZ; i++)
397 else if (from[i] ==
'(')
410 to[
j++] = from[i] + 128;
414 to[
j++] = from[i] + 128;
423 for (i = 0; from[i] !=
'\0' &&
j < BUFSIZ; i++)
428 else if ((p1 >= 161) && (p1 <= 223))
436 SJIStoJIS (&p1, &p2);
444 error (
"invalid code specification: \"%s\"", code);
450 error (
"output buffer overflow at do_convert()");
459do_check_and_conv (
unsigned char *to,
unsigned char *from)
461 static unsigned char tmp[BUFSIZ];
465 switch (DetectKanjiCode (from))
468 debug (
"Kanji code is New JIS.");
472 debug (
"Kanji code is Old JIS.");
476 debug (
"This string includes Hankaku-Kana (jisx0201) escape sequence [ESC] + ( + I.");
480 debug (
"Kanji code is NEC Kanji.");
481 error (
"cannot convert NEC Kanji.");
486 debug (
"Kanji code is EUC.");
490 debug (
"Kanji code is SJIS.");
491 do_convert (tmp, from,
SJISSTR);
494 debug (
"Kanji code is EUC or SJIS.");
499 debug (
"This is ASCII string.");
504 debug (
"This string includes unknown code.");
514 for (i = 0; tmp[i] !=
'\0' &&
j < BUFSIZ; i++)
519 if (tmp[i + 1] ==
SS2)
522 if (p2 == 222 || p2 == 223)
530 SJIStoJIS (&p1, &p2);
540 error (
"output buffer overflow at Hankaku --> Zenkaku");
553any2eucjp (
unsigned char *dest,
unsigned char *src,
unsigned int dest_max)
555 static unsigned char tmp_dest[BUFSIZ];
558 if (
strlen ((
const char *) src) >= BUFSIZ)
560 error (
"input string too large");
563 if (dest_max > BUFSIZ)
565 error (
"invalid maximum size of destination\nit should be less than %d.", BUFSIZ);
568 ret = do_check_and_conv (tmp_dest, src);
569 if (
strlen ((
const char *) tmp_dest) >= dest_max)
571 error (
"output buffer overflow");
581strwidth (
unsigned char *
s)
586 t = (
unsigned char *)
gdMalloc (BUFSIZ);
597 unsigned char input[BUFSIZ];
598 unsigned char *output;
602 while ((c =
fgetc (stdin)) !=
'\n' && i < BUFSIZ)
606 printf (
"input : %d bytes\n",
strlen ((
const char *) input));
607 printf (
"output: %d bytes\n", strwidth (input));
609 output = (
unsigned char *)
gdMalloc (BUFSIZ);
fprintf($stream, string $format, mixed ... $values)
getenv(?string $name=null, bool $local_only=false)
printf(string $format, mixed ... $values)
vfprintf($stream, string $format, array $values)
int any2eucjp(unsigned char *dest, unsigned char *src, unsigned int dest_max)
iconv(string $from_encoding, string $to_encoding, string $string)
PHPAPI ZEND_COLD void php_error_docref(const char *docref, int type, const char *format,...)
strncmp(string $string1, string $string2, int $length)
strcmp(string $string1, string $string2)