** E-Mail: furukawa@tcp-ip.or.jp
** \e$B$^$G8fO"Mm$r$*4j$$$7$^$9!#\e(B
***********************************************************************/
-/* $Id: nkf.c,v 1.42 2004/11/20 23:30:26 rei_furukawa Exp $ */
+/* $Id: nkf.c,v 1.50 2004/12/29 21:04:23 naruse Exp $ */
#define NKF_VERSION "2.0.4"
-#define NKF_RELEASE_DATE "2004-11-15"
+#define NKF_RELEASE_DATE "2004-12-01"
#include "config.h"
static char *CopyRight =
#define X0201 2
#define ISO8859_1 8
#define NO_X0201 3
+#define X0212 16
/* Input Assumption */
STATIC void w_oconv16 PROTO((int c2,int c1));
#endif
STATIC void e_oconv PROTO((int c2,int c1));
-STATIC void e2s_conv PROTO((int c2, int c1, int *p2, int *p1));
+STATIC int e2s_conv PROTO((int c2, int c1, int *p2, int *p1));
STATIC void s_oconv PROTO((int c2,int c1));
STATIC void j_oconv PROTO((int c2,int c1));
STATIC void fold_conv PROTO((int c2,int c1));
#define CP932_TABLE_BEGIN (0xfa)
#define CP932_TABLE_END (0xfc)
-STATIC int cp932inv_f = FALSE;
+STATIC int cp932inv_f = TRUE;
#define CP932INV_TABLE_BEGIN (0xed)
#define CP932INV_TABLE_END (0xee)
+/* STATIC int cp932_conv PROTO((int c2, int c1)); */
#endif /* SHIFTJIS_CP932 */
+#ifdef X0212_ENABLE
+STATIC int x0212_f = FALSE;
+static int x0212_shift PROTO((int c));
+static int x0212_unshift PROTO((int c));
+#endif
+
STATIC unsigned char prefix_table[256];
STATIC void e_status PROTO((struct input_code *, int));
FILE *fin;
unsigned char *cp;
- char *outfname;
+ char *outfname = NULL;
char *origfname;
#ifdef EASYWIN /*Easy Win */
return(-1);
} else {
#ifdef OVERWRITE
- int fd;
- int fd_backup;
+ int fd = 0;
+ int fd_backup = 0;
#endif
/* reopen file for stdout */
{"katakana","h2"},
{"katakana-hiragana","h3"},
{"guess", "g"},
+ {"cp932", ""},
+ {"no-cp932", ""},
+#ifdef X0212_ENABLE
+ {"x0212", ""},
+#endif
#ifdef UTF8_OUTPUT_ENABLE
{"utf8", "w"},
{"utf16", "w16"},
{"debug", ""},
#endif
#ifdef SHIFTJIS_CP932
- {"no-cp932", ""},
{"cp932inv", ""},
#endif
#ifdef EXEC_IO
unsigned char *cp;
{
int i;
- unsigned char *p;
+ unsigned char *p = NULL;
if (option_mode==1)
return;
continue;
}
#endif
+ if (strcmp(long_option[i].name, "cp932") == 0){
#ifdef SHIFTJIS_CP932
+ cp932_f = TRUE;
+ cp932inv_f = TRUE;
+#endif
+#ifdef UTF8_OUTPUT_ENABLE
+ ms_ucs_map_f = TRUE;
+#endif
+ continue;
+ }
if (strcmp(long_option[i].name, "no-cp932") == 0){
+#ifdef SHIFTJIS_CP932
cp932_f = FALSE;
+ cp932inv_f = FALSE;
+#endif
+#ifdef UTF8_OUTPUT_ENABLE
+ ms_ucs_map_f = FALSE;
+#endif
continue;
}
+#ifdef SHIFTJIS_CP932
if (strcmp(long_option[i].name, "cp932inv") == 0){
cp932inv_f = TRUE;
continue;
}
#endif
+
+#ifdef X0212_ENABLE
+ if (strcmp(long_option[i].name, "x0212") == 0){
+ x0212_f = TRUE;
+ continue;
+ }
+#endif
+
#ifdef EXEC_IO
if (strcmp(long_option[i].name, "exec-in") == 0){
exec_f = 1;
ptr->stat = 2;
status_push_ch(ptr, c);
#endif /* SHIFTJIS_CP932 */
+#ifdef X0212_ENABLE
+ }else if (x0212_f && 0xf0 <= c && c <= 0xfc){
+ ptr->stat = 1;
+ status_push_ch(ptr, c);
+#endif /* X0212_ENABLE */
}else{
status_disable(ptr);
}
status_disable(ptr);
}
break;
-#ifdef SHIFTJIS_CP932
case 2:
+#ifdef SHIFTJIS_CP932
if ((0x40 <= c && c <= 0x7e) || (0x80 <= c && c <= 0xfc)){
status_push_ch(ptr, c);
if (s2e_conv(ptr->buf[0], ptr->buf[1], &ptr->buf[0], &ptr->buf[1]) == 0){
break;
}
}
+#endif /* SHIFTJIS_CP932 */
+#ifndef X0212_ENABLE
status_disable(ptr);
break;
-#endif /* SHIFTJIS_CP932 */
+#endif
}
}
}else if (SSO == c || (0xa1 <= c && c <= 0xfe)){
ptr->stat = 1;
status_push_ch(ptr, c);
+#ifdef X0212_ENABLE
+ }else if (0x8f == c){
+ ptr->stat = 2;
+ status_push_ch(ptr, c);
+#endif /* X0212_ENABLE */
}else{
status_disable(ptr);
}
status_disable(ptr);
}
break;
+#ifdef X0212_ENABLE
+ case 2:
+ if (0xa1 <= c && c <= 0xfe){
+ ptr->stat = 1;
+ status_push_ch(ptr, c);
+ }else{
+ status_disable(ptr);
+ }
+#endif /* X0212_ENABLE */
}
}
input_mode = X0208;
shift_mode = FALSE;
NEXT;
+#ifdef X0212_ENABLE
+ } else if (c1 == 'D'){
+ input_mode = X0212;
+ shift_mode = FALSE;
+ NEXT;
+#endif /* X0212_ENABLE */
} else {
/* could be some special code */
(*oconv)(0, ESC);
} else if ((c1 == NL || c1 == CR) && broken_f&4) {
input_mode = ASCII; set_iconv(FALSE, 0);
SEND;
+ /*
} else if (c1 == NL && mime_f && !mime_decode_mode ) {
if ((c1=(*i_getc)(f))!=EOF && c1 == SPACE) {
i_ungetc(SPACE,f);
}
c1 = CR;
SEND;
+ */
} else
SEND;
}
/* send: */
if (input_mode == X0208)
(*oconv)(c2, c1); /* this is JIS, not SJIS/EUC case */
+#ifdef X0212_ENABLE
+ else if (input_mode == X0212)
+ (*oconv)((0x8f << 8) | c2, c1);
+#endif /* X0212_ENABLE */
else if (input_mode)
(*oconv)(input_mode, c1); /* other special case */
else if ((*iconv)(c2, c1, 0) < 0){ /* can be EUC/SJIS */
int c2, c1;
int *p2, *p1;
{
+ int val;
#ifdef SHIFTJIS_CP932
if (cp932_f && CP932_TABLE_BEGIN <= c2 && c2 <= CP932_TABLE_END){
extern unsigned short shiftjis_cp932[3][189];
- c1 = shiftjis_cp932[c2 - CP932_TABLE_BEGIN][c1 - 0x40];
- if (c1 == 0) return 1;
- c2 = c1 >> 8;
- c1 &= 0xff;
+ val = shiftjis_cp932[c2 - CP932_TABLE_BEGIN][c1 - 0x40];
+ if (val){
+ c2 = val >> 8;
+ c1 = val & 0xff;
+ }
}
#endif /* SHIFTJIS_CP932 */
+#ifdef X0212_ENABLE
+ if (x0212_f && 0xfa <= c2 && c2 <= 0xfc){
+ extern unsigned short shiftjis_x0212[3][189];
+ val = shiftjis_x0212[c2 - 0xfa][c1 - 0x40];
+ if (val){
+ if (val & 0x8000){
+ c2 = (0x8f << 8) | (val >> 8);
+ c1 = val & 0xff;
+ }else{
+ c2 = val >> 8;
+ c1 = val & 0xff;
+ }
+ if (p2) *p2 = c2;
+ if (p1) *p1 = c1;
+ return 0;
+ }
+ }
+#endif
c2 = c2 + c2 - ((c2 <= 0x9f) ? SJ0162 : SJ6394);
if (c1 < 0x9f)
c1 = c1 - ((c1 > DEL) ? SPACE : 0x1f);
c1 = c1 - 0x7e;
c2++;
}
+
+#ifdef X0212_ENABLE
+ if (x0212_f){
+ c2 = x0212_unshift(c2);
+ }
+#endif
if (p2) *p2 = c2;
if (p1) *p1 = c1;
return 0;
{
if (c2 == X0201) {
c1 &= 0x7f;
+#ifdef X0212_ENABLE
+ }else if (c2 == 0x8f){
+ if (c0 == 0){
+ return -1;
+ }
+ c2 = (c2 << 8) | (c1 & 0x7f);
+ c1 = c0 & 0x7f;
+#ifdef SHIFTJIS_CP932
+ if (cp932_f){
+ int s2, s1;
+ if (e2s_conv(c2, c1, &s2, &s1) == 0){
+ s2e_conv(s2, s1, &c2, &c1);
+ if ((c2 & 0xff00) == 0){
+ c1 &= 0x7f;
+ c2 &= 0x7f;
+ }
+ }
+ }
+#endif /* SHIFTJIS_CP932 */
+#endif /* X0212_ENABLE */
} else if (c2 == SSO){
c2 = X0201;
c1 &= 0x7f;
val |= (c0 & 0x3f);
}else if (c2 >= 0xc0){
val = (c2 & 0x1f) << 6;
- val |= (c1 & 0x3f) << 6;
+ val |= (c1 & 0x3f);
}else{
val = c2;
}
ret = 0;
}
#endif
+ }else{
+ *p2 = 0;
+ *p1 = c2;
}
return ret;
}
if (val == 0) return 1;
c2 = val >> 8;
+ if (val & 0x8000){
+ c2 &= 0x7f;
+ c2 |= 0x8f00;
+ }
if (c2 == SO) c2 = X0201;
c1 = val & 0x7f;
if (p2) *p2 = c2;
if (c2 == X0201) {
p = euc_to_utf8_1byte;
+#ifdef X0212_ENABLE
+ } else if (c2 >> 8 == 0x8f){
+ extern unsigned short * x0212_to_utf8_2bytes[];
+ c2 = (c2&0x7f) - 0x21;
+ if (0<=c2 && c2<sizeof_euc_to_utf8_2bytes)
+ p = x0212_to_utf8_2bytes[c2];
+ else
+ return 0;
+#endif
} else {
c2 &= 0x7f;
c2 = (c2&0x7f) - 0x21;
c1;
{
int c0;
-#ifdef NUMCHAR_OPTION
- if (c2 == 0 && (c1 & CLASS_MASK) == CLASS_UTF16){
- w16w_conv(c1, &c2, &c1, &c0);
- (*o_putc)(c2);
- if (c1){
- (*o_putc)(c1);
- if (c0) (*o_putc)(c0);
- }
- }
-#endif
if (c2 == EOF) {
(*o_putc)(EOF);
return;
unicode_bom_f=1;
}
+#ifdef NUMCHAR_OPTION
+ if (c2 == 0 && (c1 & CLASS_MASK) == CLASS_UTF16){
+ w16w_conv(c1, &c2, &c1, &c0);
+ (*o_putc)(c2);
+ if (c1){
+ (*o_putc)(c1);
+ if (c0) (*o_putc)(c0);
+ }
+ return;
+ }
+#endif
+
if (c2 == 0) {
output_mode = ASCII;
(*o_putc)(c1);
output_mode = ISO8859_1;
(*o_putc)(c1 | 0x080);
} else {
+ unsigned short val;
output_mode = UTF8;
- w16w_conv((unsigned short)e2w_conv(c2, c1), &c2, &c1, &c0);
- (*o_putc)(c2);
- if (c1){
- (*o_putc)(c1);
- if (c0) (*o_putc)(c0);
+ val = e2w_conv(c2, c1);
+ if (val){
+ w16w_conv(val, &c2, &c1, &c0);
+ (*o_putc)(c2);
+ if (c1){
+ (*o_putc)(c1);
+ if (c0) (*o_putc)(c0);
+ }
}
}
}
c1 &= 0xff;
#endif
} else if (c2) {
- unsigned short val = (unsigned short)e2w_conv(c2, c1);
+ unsigned short val = e2w_conv(c2, c1);
c2 = (val >> 8) & 0xff;
c1 = val & 0xff;
}
#ifdef NUMCHAR_OPTION
if (c2 == 0 && (c1 & CLASS_MASK) == CLASS_UTF16){
w16e_conv(c1, &c2, &c1);
+ if (c2 == 0 && (c1 & CLASS_MASK) == CLASS_UTF16){
+ return;
+ }
}
#endif
if (c2 == EOF) {
} else if (c2 == ISO8859_1) {
output_mode = ISO8859_1;
(*o_putc)(c1 | 0x080);
+#ifdef X0212_ENABLE
+ } else if ((c2 & 0xff00) >> 8 == 0x8f){
+ output_mode = JAPANESE_EUC;
+#ifdef SHIFTJIS_CP932
+ if (cp932_f){
+ int s2, s1;
+ if (e2s_conv(c2, c1, &s2, &s1) == 0){
+ s2e_conv(s2, s1, &c2, &c1);
+ }
+ }
+#endif
+ if ((c2 & 0xff00) >> 8 == 0x8f){
+ if (x0212_f){
+ (*o_putc)(0x8f);
+ (*o_putc)((c2 & 0x7f) | 0x080);
+ (*o_putc)(c1 | 0x080);
+ }
+ }else{
+ (*o_putc)((c2 & 0x7f) | 0x080);
+ (*o_putc)(c1 | 0x080);
+ }
+#endif
} else {
if ((c1<0x21 || 0x7e<c1) ||
(c2<0x21 || 0x7e<c2)) {
}
}
-void
+#ifdef X0212_ENABLE
+int x0212_shift(c)
+ int c;
+{
+ int ret = c;
+ c &= 0x7f;
+ if ((ret & 0xff00) == 0x8f00){
+ if (0x75 <= c && c <= 0x7f){
+ ret = c + (0x109 - 0x75);
+ }
+ }else{
+ if (0x75 <= c && c <= 0x7f){
+ ret = c + (0x113 - 0x75);
+ }
+ }
+ return ret;
+}
+
+
+int x0212_unshift(c)
+ int c;
+{
+ int ret = c;
+ if (0x7f <= c && c <= 0x88){
+ ret = c + (0x75 - 0x7f);
+ }else if (0x89 <= c && c <= 0x92){
+ ret = (0x8f << 8) | 0x80 | (c + (0x75 - 0x89));
+ }
+ return ret;
+}
+#endif /* X0212_ENABLE */
+
+int
e2s_conv(c2, c1, p2, p1)
int c2, c1, *p2, *p1;
{
+ int val = 0;
+ unsigned short *ptr;
+ int ndx;
+ extern unsigned short *x0212_shiftjis[];
+#ifdef X0212_ENABLE
+ if ((c2 & 0xff00) == 0x8f00){
+ ndx = c2 & 0x7f;
+ if (0x21 <= ndx && ndx <= 0x7e){
+ ptr = x0212_shiftjis[ndx - 0x21];
+ if (ptr){
+ val = ptr[(c1 & 0x7f) - 0x21];
+ }
+ if (val){
+ c2 = val >> 8;
+ c1 = val & 0xff;
+ if (p2) *p2 = c2;
+ if (p1) *p1 = c1;
+ return 0;
+ }
+ }
+ c2 = x0212_shift(c2);
+ }
+#endif /* X0212_ENABLE */
+ if ((c2 & 0xff00) == 0x8f00){
+ return 1;
+ }
if (p2) *p2 = ((c2 - 1) >> 1) + ((c2 <= 0x5e) ? 0x71 : 0xb1);
if (p1) *p1 = c1 + ((c2 & 1) ? ((c1 < 0x60) ? 0x1f : 0x20) : 0x7e);
+ return 0;
}
void
} else if (c2 == ISO8859_1) {
output_mode = ISO8859_1;
(*o_putc)(c1 | 0x080);
+#ifdef X0212_ENABLE
+ } else if ((c2 & 0xff00) >> 8 == 0x8f){
+ output_mode = SHIFT_JIS;
+ if (e2s_conv(c2, c1, &c2, &c1) == 0){
+ (*o_putc)(c2);
+ (*o_putc)(c1);
+ }
+#endif
} else {
if ((c1<0x20 || 0x7e<c1) ||
(c2<0x20 || 0x7e<c2)) {
output_mode = ASCII;
}
(*o_putc)(EOF);
+#ifdef X0212_ENABLE
+ } else if ((c2 & 0xff00) >> 8 == 0x8f){
+ if (output_mode!=X0212) {
+ output_mode = X0212;
+ (*o_putc)(ESC);
+ (*o_putc)('$');
+ (*o_putc)('(');
+ (*o_putc)('D');
+ }
+ (*o_putc)(c2 & 0x7f);
+ (*o_putc)(c1);
+#endif
} else if (c2==X0201) {
if (output_mode!=X0201) {
output_mode = X0201;
mimeout_buf_count = 0;
}
mimeout_buf[mimeout_buf_count++] = c;
- if (mimeout_buf_count>75) {
+ if (mimeout_buf_count>MIMEOUT_BUF_LENGTH) {
open_mime(output_mode);
}
return;
}
open_mime(output_mode);
}
- } else { /* c==EOF */
+ } else if (c == EOF) { /* c==EOF */
j = mimeout_buf_count;
i = 0;
for (;i<j;i++) {
#endif
#ifdef SHIFTJIS_CP932
cp932_f = TRUE;
- cp932inv_f = FALSE;
+ cp932inv_f = TRUE;
#endif
{
int i;
#ifdef UTF8_INPUT_ENABLE
utf16_mode = UTF16LE_INPUT;
#endif
+ mimeout_buf_count = 0;
mimeout_mode = 0;
base64_count = 0;
f_line = 0;
fprintf(stderr," --fj,--unix,--mac,--windows convert for the system\n");
fprintf(stderr," --jis,--euc,--sjis,--utf8,--utf16,--mime,--base64 convert for the code\n");
fprintf(stderr," --hiragana, --katakana Hiragana/Katakana Conversion\n");
+ fprintf(stderr," --cp932, --no-cp932 CP932 compatible\n");
#ifdef INPUT_OPTION
fprintf(stderr," --cap-input, --url-input Convert hex after ':' or '%%'\n");
#endif
#ifdef NUMCHAR_OPTION
fprintf(stderr," --numchar-input Convert Unicode Character Reference\n");
#endif
-#ifdef SHIFTJIS_CP932
- fprintf(stderr," --no-cp932 Don't convert Shift_JIS FAxx-FCxx to equivalnet CP932\n");
- fprintf(stderr," --cp932inv convert Shift_JIS EDxx-EFxx to equivalnet CP932 FAxx-FCxx\n");
-#endif
#ifdef UTF8_OUTPUT_ENABLE
fprintf(stderr," --ms-ucs-map Microsoft UCS Mapping Compatible\n");
#endif