/* SJIS to internal (EUC/SJIS/UPTEX) code conversion */ long fromSJIS(long kcode) { if (is_internalSJIS()) return kcode; return fromJIS(SJIStoJIS(kcode)); }
static int do_check_and_conv (unsigned char *to, unsigned char *from) { static unsigned char tmp[BUFSIZ]; unsigned char *tmp_p = &tmp[0]; int p1, p2, i, j; int kanji = TRUE; switch (DetectKanjiCode (from)) { case NEW: debug ("Kanji code is New JIS."); do_convert (&tmp_p, &from, NEWJISSTR); break; case OLD: debug ("Kanji code is Old JIS."); do_convert (&tmp_p, &from, OLDJISSTR); break; case ESCI: debug ("This string includes Hankaku-Kana (jisx0201) escape sequence [ESC] + ( + I."); do_convert (&tmp_p, &from, NEWJISSTR); break; case NEC: debug ("Kanji code is NEC Kanji."); error ("cannot convert NEC Kanji."); ustrcpy (tmp, from); kanji = FALSE; break; case EUC: debug ("Kanji code is EUC."); ustrcpy (tmp, from); break; case SJIS: debug ("Kanji code is SJIS."); do_convert (&tmp_p, &from, SJISSTR); break; case EUCORSJIS: debug ("Kanji code is EUC or SJIS."); ustrcpy (tmp, from); kanji = FALSE; break; case ASCII: debug ("This is ASCII string."); ustrcpy (tmp, from); kanji = FALSE; break; default: debug ("This string includes unknown code."); ustrcpy (tmp, from); kanji = FALSE; break; } /* Hankaku Kana ---> Zenkaku Kana */ if (kanji) { j = 0; for (i = 0; tmp[i] != '\0' && j < BUFSIZ; i++) { if (tmp[i] == SS2) { p1 = tmp[++i]; if (tmp[i + 1] == SS2) { p2 = tmp[i + 2]; if (p2 == 222 || p2 == 223) i += 2; else p2 = 0; } else p2 = 0; han2zen (&p1, &p2); SJIStoJIS (&p1, &p2); to[j++] = p1 + 128; to[j++] = p2 + 128; } else to[j++] = tmp[i]; } if (j >= BUFSIZ) { error ("output buffer overflow at Hankaku --> Zenkaku"); ustrcpy (to, tmp); } else to[j] = '\0'; } else ustrcpy (to, tmp); return kanji; }
/* internal (EUC/SJIS/UPTEX) to JIS code conversion */ long toJIS(long kcode) { if (is_internalUPTEX()) return UCS2toJIS(UPTEXtoUCS(kcode)); if (is_internalSJIS()) return SJIStoJIS(kcode); /* EUC */ return EUCtoJIS(kcode); }
static void do_convert (unsigned char **to_p, unsigned char **from_p, const char *code) { unsigned char *to = *to_p; unsigned char *from = *from_p; #ifdef HAVE_ICONV iconv_t cd; size_t from_len, to_len; if ((cd = iconv_open (EUCSTR, code)) == (iconv_t) - 1) { error ("iconv_open() error"); #ifdef HAVE_ERRNO_H if (errno == EINVAL) error ("invalid code specification: \"%s\" or \"%s\"", EUCSTR, code); #endif ustrcpy (to, from); return; } from_len = strlen ((const char *)from) + 1; to_len = BUFSIZ; if ((int) (iconv (cd, (char **)from_p, &from_len, (char **)to_p, &to_len)) == -1) { #ifdef HAVE_ERRNO_H if (errno == EINVAL) error ("invalid end of input string"); else if (errno == EILSEQ) error ("invalid code in input string"); else if (errno == E2BIG) error ("output buffer overflow at do_convert()"); else #endif error ("something happen"); ustrcpy (to, from); return; } if (iconv_close (cd) != 0) { error ("iconv_close() error"); } #else int p1, p2, i, j; int jisx0208 = FALSE; int hankaku = FALSE; j = 0; if (strcmp (code, NEWJISSTR) == 0 || strcmp (code, OLDJISSTR) == 0) { for (i = 0; from[i] != '\0' && j < BUFSIZ; i++) { if (from[i] == ESC) { i++; if (from[i] == '$') { jisx0208 = TRUE; hankaku = FALSE; i++; } else if (from[i] == '(') { jisx0208 = FALSE; i++; if (from[i] == 'I') /* Hankaku Kana */ hankaku = TRUE; else hankaku = FALSE; } } else { if (jisx0208) to[j++] = from[i] + 128; else if (hankaku) { to[j++] = SS2; to[j++] = from[i] + 128; } else to[j++] = from[i]; } } } else if (strcmp (code, SJISSTR) == 0) { for (i = 0; from[i] != '\0' && j < BUFSIZ; i++) { p1 = from[i]; if (p1 < 127) to[j++] = p1; else if ((p1 >= 161) && (p1 <= 223)) { /* Hankaku Kana */ to[j++] = SS2; to[j++] = p1; } else { p2 = from[++i]; SJIStoJIS (&p1, &p2); to[j++] = p1 + 128; to[j++] = p2 + 128; } } } else { error ("invalid code specification: \"%s\"", code); return; } if (j >= BUFSIZ) { error ("output buffer overflow at do_convert()"); ustrcpy (to, from); } else to[j] = '\0'; #endif /* HAVE_ICONV */ }