1 /** Network Kanji Filter. (PDS Version)
2 ************************************************************************
3 ** Copyright (C) 1987, Fujitsu LTD. (Itaru ICHIKAWA)
4 **
\e$BO"Mm@h!'
\e(B
\e$B!J3t!KIY;NDL8&5f=j!!%=%U%H#38&!!;T@n!!;j
\e(B
5 **
\e$B!J
\e(BE-Mail Address: ichikawa@flab.fujitsu.co.jp
\e$B!K
\e(B
6 ** Copyright (C) 1996,1998
8 **
\e$BO"Mm@h!'
\e(B
\e$BN05eBg3X>pJs9)3X2J
\e(B
\e$B2OLn
\e(B
\e$B??<#
\e(B mime/X0208 support
9 **
\e$B!J
\e(BE-Mail Address: kono@ie.u-ryukyu.ac.jp
\e$B!K
\e(B
10 **
\e$BO"Mm@h!'
\e(B COW for DOS & Win16 & Win32 & OS/2
11 **
\e$B!J
\e(BE-Mail Address: GHG00637@niftyserve.or.p
\e$B!K
\e(B
13 **
\e$B$3$N%=!<%9$N$$$+$J$kJ#<L!$2~JQ!$=$@5$b5vBz$7$^$9!#$?$@$7!"
\e(B
14 **
\e$B$=$N:]$K$O!"C/$,9W8%$7$?$r<($9$3$NItJ,$r;D$9$3$H!#
\e(B
15 **
\e$B:FG[I[$d;(;o$NIUO?$J$I$NLd$$9g$o$;$bI,MW$"$j$^$;$s!#
\e(B
16 **
\e$B1DMxMxMQ$b>e5-$KH?$7$J$$HO0O$G5v2D$7$^$9!#
\e(B
17 **
\e$B%P%$%J%j$NG[I[$N:]$K$O
\e(Bversion message
\e$B$rJ]B8$9$k$3$H$r>r7o$H$7$^$9!#
\e(B
18 **
\e$B$3$N%W%m%0%i%`$K$D$$$F$OFC$K2?$NJ]>Z$b$7$J$$!"0-$7$+$i$:!#
\e(B
20 ** Everyone is permitted to do anything on this program
21 ** including copying, modifying, improving,
22 ** as long as you don't try to pretend that you wrote it.
23 ** i.e., the above copyright notice has to appear in all copies.
24 ** Binary distribution requires original version messages.
25 ** You don't have to ask before copying, redistribution or publishing.
26 ** THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE.
27 ***********************************************************************/
29 /***********************************************************************
30 ** UTF-8
\e$B%5%]!<%H$K$D$$$F
\e(B
31 **
\e$B=>Mh$N
\e(B nkf
\e$B$HF~$l$+$($F$=$N$^$^;H$($k$h$&$K$J$C$F$$$^$9
\e(B
32 ** nkf -e
\e$B$J$I$H$7$F5/F0$9$k$H!"<+F0H=JL$G
\e(B UTF-8
\e$B$HH=Dj$5$l$l$P!"
\e(B
33 **
\e$B$=$N$^$^
\e(B euc-jp
\e$B$KJQ49$5$l$^$9
\e(B
35 **
\e$B$^$@%P%0$,$"$k2DG=@-$,9b$$$G$9!#
\e(B
36 ** (
\e$BFC$K<+F0H=JL!"%3!<%I:.:_!"%(%i!<=hM}7O
\e(B)
38 **
\e$B2?$+LdBj$r8+$D$1$?$i!"
\e(B
39 ** E-Mail: furukawa@tcp-ip.or.jp
40 **
\e$B$^$G8fO"Mm$r$*4j$$$7$^$9!#
\e(B
41 ***********************************************************************/
44 static char *CopyRight =
45 "Copyright (C) 1987, FUJITSU LTD. (I.Ichikawa),2000 S. Kono, COW, 2002-2003 Kono, Furukawa";
46 static char *Version =
48 static char *Patchlevel =
55 ** USAGE: nkf [flags] [file]
58 ** b Output is bufferred (DEFAULT)
59 ** u Output is unbufferred
63 ** j Outout code is JIS 7 bit (DEFAULT SELECT)
64 ** s Output code is MS Kanji (DEFAULT SELECT)
65 ** e Output code is AT&T JIS (DEFAULT SELECT)
66 ** w Output code is AT&T JIS (DEFAULT SELECT)
67 ** l Output code is JIS 7bit and ISO8859-1 Latin-1
69 ** m MIME conversion for ISO-2022-JP
70 ** I Convert non ISO-2022-JP charactor to GETA by Pekoe <pekoe@lair.net>
71 ** i_ Output sequence to designate JIS-kanji (DEFAULT_J)
72 ** o_ Output sequence to designate single-byte roman characters (DEFAULT_R)
73 ** M MIME output conversion
75 ** r {de/en}crypt ROT13/47
79 ** T Text mode output (for MS-DOS)
81 ** x Do not convert X0201 kana into X0208
82 ** Z Convert X0208 alphabet to ASCII
87 ** B try to fix broken JIS, missing Escape
88 ** B[1-9] broken level
90 ** O Output to 'nkf.out' file or last file name
91 ** d Delete \r in line feed
92 ** c Add \r in line feed
93 ** -- other long option
94 ** -- ignore following option (don't use with -O )
98 #if (defined(__TURBOC__) || defined(_MSC_VER) || defined(LSI_C) || defined(__MINGW32__)) && !defined(MSDOS)
100 #if (defined(__Win32__) || defined(_WIN32)) && !defined(__WIN32__)
116 #if defined(MSDOS) || defined(__OS2__)
123 #define setbinmode(fp) fsetbin(fp)
124 #else /* Microsoft C, Turbo C */
125 #define setbinmode(fp) setmode(fileno(fp), O_BINARY)
127 #else /* UNIX,OS/2 */
128 #define setbinmode(fp)
131 #ifdef _IOFBF /* SysV and MSDOS, Windows */
132 #define setvbuffer(fp, buf, size) setvbuf(fp, buf, _IOFBF, size)
134 #define setvbuffer(fp, buf, size) setbuffer(fp, buf, size)
137 /*Borland C++ 4.5 EasyWin*/
138 #if defined(__TURBOC__) && defined(_Windows) && !defined(__WIN32__) /*Easy Win */
147 /* added by satoru@isoternet.org */
148 #include <sys/stat.h>
149 #ifndef MSDOS /* UNIX, OS/2 */
153 #if defined(_MSC_VER) || defined(__MINGW32__) /* VC++, MinGW */
154 #include <sys/utime.h>
155 #elif defined(__TURBOC__) /* BCC */
157 #elif defined(LSI_C) /* LSI C */
169 /* state of output_mode and input_mode
186 /* Input Assumption */
190 #define LATIN1_INPUT 6
192 #define STRICT_MIME 8
197 #define JAPANESE_EUC 10
201 #define UTF8_INPUT 13
202 #define UTF16_INPUT 14
203 #define UTF16BE_INPUT 15
221 #define is_alnum(c) \
222 (('a'<=c && c<='z')||('A'<= c && c<='Z')||('0'<=c && c<='9'))
224 #define HOLD_SIZE 1024
225 #define IOBUF_SIZE 16384
227 #define DEFAULT_J 'B'
228 #define DEFAULT_R 'B'
230 #define SJ0162 0x00e1 /* 01 - 62 ku offset */
231 #define SJ6394 0x0161 /* 63 - 94 ku offset */
233 #define RANGE_NUM_MAX 18
238 #if defined( UTF8_OUTPUT_ENABLE ) || defined( UTF8_INPUT_ENABLE )
239 #define sizeof_euc_utf8 94
240 #define sizeof_euc_to_utf8_1byte 94
241 #define sizeof_euc_to_utf8_2bytes 94
242 #define sizeof_utf8_to_euc_C2 64
243 #define sizeof_utf8_to_euc_E5B8 64
244 #define sizeof_utf8_to_euc_2bytes 112
245 #define sizeof_utf8_to_euc_3bytes 112
248 /* MIME preprocessor */
251 #ifdef EASYWIN /*Easy Win */
252 extern POINT _BufferSize;
255 /* function prototype */
257 #ifdef ANSI_C_PROTOTYPE
259 #define STATIC static
271 void (*status_func)PROTO((struct input_code *, int));
272 int (*iconv_func)PROTO((int c2, int c1, int c0));
276 STATIC char *input_codename = "";
278 STATIC int noconvert PROTO((FILE *f));
279 STATIC int kanji_convert PROTO((FILE *f));
280 STATIC int h_conv PROTO((FILE *f,int c2,int c1));
281 STATIC int push_hold_buf PROTO((int c2));
282 STATIC void set_iconv PROTO((int f, int (*iconv_func)()));
283 STATIC int s_iconv PROTO((int c2,int c1,int c0));
284 STATIC int s2e_conv PROTO((int c2, int c1, int *p2, int *p1));
285 STATIC int e_iconv PROTO((int c2,int c1,int c0));
286 #ifdef UTF8_INPUT_ENABLE
287 STATIC int w2e_conv PROTO((int c2,int c1,int c0,int *p2,int *p1));
288 STATIC int w_iconv PROTO((int c2,int c1,int c0));
289 STATIC int w_iconv16 PROTO((int c2,int c1,int c0));
290 STATIC int w_iconv_common PROTO((int c1,int c0,unsigned short **pp,int psize,int *p2,int *p1));
291 STATIC int ww16_conv PROTO((int c2, int c1, int c0));
293 #ifdef UTF8_OUTPUT_ENABLE
294 STATIC int e2w_conv PROTO((int c2,int c1));
295 STATIC void w_oconv PROTO((int c2,int c1));
296 STATIC void w_oconv16 PROTO((int c2,int c1));
298 STATIC void e_oconv PROTO((int c2,int c1));
299 STATIC void e2s_conv PROTO((int c2, int c1, int *p2, int *p1));
300 STATIC void s_oconv PROTO((int c2,int c1));
301 STATIC void j_oconv PROTO((int c2,int c1));
302 STATIC void fold_conv PROTO((int c2,int c1));
303 STATIC void cr_conv PROTO((int c2,int c1));
304 STATIC void z_conv PROTO((int c2,int c1));
305 STATIC void rot_conv PROTO((int c2,int c1));
306 STATIC void hira_conv PROTO((int c2,int c1));
307 STATIC void base64_conv PROTO((int c2,int c1));
308 STATIC void iso2022jp_check_conv PROTO((int c2,int c1));
309 STATIC void no_connection PROTO((int c2,int c1));
310 STATIC int no_connection2 PROTO((int c2,int c1,int c0));
312 STATIC void code_score PROTO((struct input_code *ptr));
313 STATIC void code_status PROTO((int c));
315 STATIC void std_putc PROTO((int c));
316 STATIC int std_getc PROTO((FILE *f));
317 STATIC int std_ungetc PROTO((int c,FILE *f));
319 STATIC int broken_getc PROTO((FILE *f));
320 STATIC int broken_ungetc PROTO((int c,FILE *f));
322 STATIC int mime_begin PROTO((FILE *f));
323 STATIC int mime_getc PROTO((FILE *f));
324 STATIC int mime_ungetc PROTO((int c,FILE *f));
326 STATIC int mime_begin_strict PROTO((FILE *f));
327 STATIC int mime_getc_buf PROTO((FILE *f));
328 STATIC int mime_ungetc_buf PROTO((int c,FILE *f));
329 STATIC int mime_integrity PROTO((FILE *f,unsigned char *p));
331 STATIC int base64decode PROTO((int c));
332 STATIC void mime_putc PROTO((int c));
333 STATIC void open_mime PROTO((int c));
334 STATIC void close_mime PROTO(());
335 STATIC void usage PROTO(());
336 STATIC void version PROTO(());
337 STATIC void options PROTO((unsigned char *c));
339 STATIC void reinit PROTO(());
344 static unsigned char stdibuf[IOBUF_SIZE];
345 static unsigned char stdobuf[IOBUF_SIZE];
346 static unsigned char hold_buf[HOLD_SIZE*2];
347 static int hold_count;
349 /* MIME preprocessor fifo */
351 #define MIME_BUF_SIZE (1024) /* 2^n ring buffer */
352 #define MIME_BUF_MASK (MIME_BUF_SIZE-1)
353 #define Fifo(n) mime_buf[(n)&MIME_BUF_MASK]
354 static unsigned char mime_buf[MIME_BUF_SIZE];
355 static unsigned int mime_top = 0;
356 static unsigned int mime_last = 0; /* decoded */
357 static unsigned int mime_input = 0; /* undecoded */
360 static int unbuf_f = FALSE;
361 static int estab_f = FALSE;
362 static int nop_f = FALSE;
363 static int binmode_f = TRUE; /* binary mode */
364 static int rot_f = FALSE; /* rot14/43 mode */
365 static int hira_f = FALSE; /* hira/kata henkan */
366 static int input_f = FALSE; /* non fixed input code */
367 static int alpha_f = FALSE; /* convert JIx0208 alphbet to ASCII */
368 static int mime_f = STRICT_MIME; /* convert MIME B base64 or Q */
369 static int mimebuf_f = FALSE; /* MIME buffered input */
370 static int broken_f = FALSE; /* convert ESC-less broken JIS */
371 static int iso8859_f = FALSE; /* ISO8859 through */
372 static int mimeout_f = FALSE; /* base64 mode */
373 #if defined(MSDOS) || defined(__OS2__)
374 static int x0201_f = TRUE; /* Assume JISX0201 kana */
376 static int x0201_f = NO_X0201; /* Assume NO JISX0201 */
378 static int iso2022jp_f = FALSE; /* convert ISO-2022-JP */
379 #ifdef UTF8_OUTPUT_ENABLE
380 static int w_oconv16_begin_f= 0; /* utf-16 header */
381 static int w_oconv16_LE = 0; /* utf-16 little endian */
385 #ifdef NUMCHAR_OPTION
387 #define CLASS_MASK 0x0f000000
388 #define CLASS_UTF16 0x01000000
392 static int cap_f = FALSE;
393 static int (*i_cgetc)PROTO((FILE *)) = std_getc; /* input of cgetc */
394 static int (*i_cungetc)PROTO((int c ,FILE *f)) = std_ungetc;
395 STATIC int cap_getc PROTO((FILE *f));
396 STATIC int cap_ungetc PROTO((int c,FILE *f));
398 static int url_f = FALSE;
399 static int (*i_ugetc)PROTO((FILE *)) = std_getc; /* input of ugetc */
400 static int (*i_uungetc)PROTO((int c ,FILE *f)) = std_ungetc;
401 STATIC int url_getc PROTO((FILE *f));
402 STATIC int url_ungetc PROTO((int c,FILE *f));
404 static int numchar_f = FALSE;
405 static int (*i_ngetc)PROTO((FILE *)) = std_getc; /* input of ugetc */
406 static int (*i_nungetc)PROTO((int c ,FILE *f)) = std_ungetc;
407 STATIC int numchar_getc PROTO((FILE *f));
408 STATIC int numchar_ungetc PROTO((int c,FILE *f));
412 static int noout_f = FALSE;
413 STATIC void no_putc PROTO((int c));
414 static int debug_f = FALSE;
415 STATIC void debug PROTO((char *str));
419 static int exec_f = 0;
422 #ifdef SHIFTJIS_CP932
423 STATIC int cp932_f = TRUE;
424 #define CP932_TABLE_BEGIN (0xfa)
425 #define CP932_TABLE_END (0xfc)
427 #endif /* SHIFTJIS_CP932 */
429 STATIC void e_status PROTO((struct input_code *, int));
430 STATIC void s_status PROTO((struct input_code *, int));
432 #ifdef UTF8_INPUT_ENABLE
433 STATIC void w_status PROTO((struct input_code *, int));
434 STATIC void w16_status PROTO((struct input_code *, int));
435 static int utf16_mode = UTF16_INPUT;
438 struct input_code input_code_list[] = {
439 {"EUC-JP", 0, 0, 0, {0, 0, 0}, e_status, e_iconv, 0},
440 {"Shift_JIS", 0, 0, 0, {0, 0, 0}, s_status, s_iconv, 0},
441 {"UTF-8", 0, 0, 0, {0, 0, 0}, w_status, w_iconv, 0},
442 {"UTF-16", 0, 0, 0, {0, 0, 0}, w16_status, w_iconv16, 0},
446 static int mimeout_mode = 0;
447 static int base64_count = 0;
449 /* X0208 -> ASCII converter */
452 static int f_line = 0; /* chars in line */
453 static int f_prev = 0;
454 static int fold_preserve_f = FALSE; /* preserve new lines */
455 static int fold_f = FALSE;
456 static int fold_len = 0;
459 static unsigned char kanji_intro = DEFAULT_J,
460 ascii_intro = DEFAULT_R;
464 #define FOLD_MARGIN 10
465 #define DEFAULT_FOLD 60
467 static int fold_margin = FOLD_MARGIN;
471 #ifdef DEFAULT_CODE_JIS
472 # define DEFAULT_CONV j_oconv
474 #ifdef DEFAULT_CODE_SJIS
475 # define DEFAULT_CONV s_oconv
477 #ifdef DEFAULT_CODE_EUC
478 # define DEFAULT_CONV e_oconv
480 #ifdef DEFAULT_CODE_UTF8
481 # define DEFAULT_CONV w_oconv
484 /* process default */
485 static void (*output_conv)PROTO((int c2,int c1)) = DEFAULT_CONV;
487 static void (*oconv)PROTO((int c2,int c1)) = no_connection;
488 /* s_iconv or oconv */
489 static int (*iconv)PROTO((int c2,int c1,int c0)) = no_connection2;
491 static void (*o_zconv)PROTO((int c2,int c1)) = no_connection;
492 static void (*o_fconv)PROTO((int c2,int c1)) = no_connection;
493 static void (*o_crconv)PROTO((int c2,int c1)) = no_connection;
494 static void (*o_rot_conv)PROTO((int c2,int c1)) = no_connection;
495 static void (*o_hira_conv)PROTO((int c2,int c1)) = no_connection;
496 static void (*o_base64conv)PROTO((int c2,int c1)) = no_connection;
497 static void (*o_iso2022jp_check_conv)PROTO((int c2,int c1)) = no_connection;
499 /* static redirections */
501 static void (*o_putc)PROTO((int c)) = std_putc;
503 static int (*i_getc)PROTO((FILE *f)) = std_getc; /* general input */
504 static int (*i_ungetc)PROTO((int c,FILE *f)) =std_ungetc;
506 static int (*i_bgetc)PROTO((FILE *)) = std_getc; /* input of mgetc */
507 static int (*i_bungetc)PROTO((int c ,FILE *f)) = std_ungetc;
509 static void (*o_mputc)PROTO((int c)) = std_putc ; /* output of mputc */
511 static int (*i_mgetc)PROTO((FILE *)) = std_getc; /* input of mgetc */
512 static int (*i_mungetc)PROTO((int c ,FILE *f)) = std_ungetc;
514 /* for strict mime */
515 static int (*i_mgetc_buf)PROTO((FILE *)) = std_getc; /* input of mgetc_buf */
516 static int (*i_mungetc_buf)PROTO((int c,FILE *f)) = std_ungetc;
519 static int output_mode = ASCII, /* output kanji mode */
520 input_mode = ASCII, /* input kanji mode */
521 shift_mode = FALSE; /* TRUE shift out, or X0201 */
522 static int mime_decode_mode = FALSE; /* MIME mode B base64, Q hex */
524 /* X0201 / X0208 conversion tables */
526 /* X0201 kana conversion table */
529 unsigned char cv[]= {
530 0x21,0x21,0x21,0x23,0x21,0x56,0x21,0x57,
531 0x21,0x22,0x21,0x26,0x25,0x72,0x25,0x21,
532 0x25,0x23,0x25,0x25,0x25,0x27,0x25,0x29,
533 0x25,0x63,0x25,0x65,0x25,0x67,0x25,0x43,
534 0x21,0x3c,0x25,0x22,0x25,0x24,0x25,0x26,
535 0x25,0x28,0x25,0x2a,0x25,0x2b,0x25,0x2d,
536 0x25,0x2f,0x25,0x31,0x25,0x33,0x25,0x35,
537 0x25,0x37,0x25,0x39,0x25,0x3b,0x25,0x3d,
538 0x25,0x3f,0x25,0x41,0x25,0x44,0x25,0x46,
539 0x25,0x48,0x25,0x4a,0x25,0x4b,0x25,0x4c,
540 0x25,0x4d,0x25,0x4e,0x25,0x4f,0x25,0x52,
541 0x25,0x55,0x25,0x58,0x25,0x5b,0x25,0x5e,
542 0x25,0x5f,0x25,0x60,0x25,0x61,0x25,0x62,
543 0x25,0x64,0x25,0x66,0x25,0x68,0x25,0x69,
544 0x25,0x6a,0x25,0x6b,0x25,0x6c,0x25,0x6d,
545 0x25,0x6f,0x25,0x73,0x21,0x2b,0x21,0x2c,
549 /* X0201 kana conversion table for daguten */
552 unsigned char dv[]= {
553 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
554 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
555 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
556 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
557 0x00,0x00,0x00,0x00,0x00,0x00,0x25,0x74,
558 0x00,0x00,0x00,0x00,0x25,0x2c,0x25,0x2e,
559 0x25,0x30,0x25,0x32,0x25,0x34,0x25,0x36,
560 0x25,0x38,0x25,0x3a,0x25,0x3c,0x25,0x3e,
561 0x25,0x40,0x25,0x42,0x25,0x45,0x25,0x47,
562 0x25,0x49,0x00,0x00,0x00,0x00,0x00,0x00,
563 0x00,0x00,0x00,0x00,0x25,0x50,0x25,0x53,
564 0x25,0x56,0x25,0x59,0x25,0x5c,0x00,0x00,
565 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
566 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
567 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
568 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
571 /* X0201 kana conversion table for han-daguten */
574 unsigned char ev[]= {
575 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
576 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
577 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
578 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
579 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
580 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
581 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
582 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
583 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
584 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
585 0x00,0x00,0x00,0x00,0x25,0x51,0x25,0x54,
586 0x25,0x57,0x25,0x5a,0x25,0x5d,0x00,0x00,
587 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
588 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
589 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
590 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
594 /* X0208 kigou conversion table */
595 /* 0x8140 - 0x819e */
597 unsigned char fv[] = {
599 0x00,0x00,0x00,0x00,0x2c,0x2e,0x00,0x3a,
600 0x3b,0x3f,0x21,0x00,0x00,0x27,0x60,0x00,
601 0x5e,0x00,0x5f,0x00,0x00,0x00,0x00,0x00,
602 0x00,0x00,0x00,0x00,0x00,0x2d,0x00,0x2f,
603 0x5c,0x00,0x00,0x7c,0x00,0x00,0x60,0x27,
604 0x22,0x22,0x28,0x29,0x00,0x00,0x5b,0x5d,
605 0x7b,0x7d,0x3c,0x3e,0x00,0x00,0x00,0x00,
606 0x00,0x00,0x00,0x00,0x2b,0x2d,0x00,0x00,
607 0x00,0x3d,0x00,0x3c,0x3e,0x00,0x00,0x00,
608 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
609 0x24,0x00,0x00,0x25,0x23,0x26,0x2a,0x40,
610 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
616 static int file_out = FALSE;
618 static int overwrite = FALSE;
621 static int crmode_f = 0; /* CR, NL, CRLF */
622 #ifdef EASYWIN /*Easy Win */
623 static int end_check;
635 #ifdef EASYWIN /*Easy Win */
636 _BufferSize.y = 400;/*Set Scroll Buffer Size*/
639 for (argc--,argv++; (argc > 0) && **argv == '-'; argc--, argv++) {
640 cp = (unsigned char *)*argv;
645 if (pipe(fds) < 0 || (pid = fork()) < 0){
656 execvp(argv[1], &argv[1]);
670 if(x0201_f == WISH_TRUE)
671 x0201_f = ((!iso2022jp_f)? TRUE : NO_X0201);
673 if (binmode_f == TRUE)
675 if (freopen("","wb",stdout) == NULL)
682 setbuf(stdout, (char *) NULL);
684 setvbuffer(stdout, stdobuf, IOBUF_SIZE);
687 if (binmode_f == TRUE)
689 if (freopen("","rb",stdin) == NULL) return (-1);
693 setvbuffer(stdin, stdibuf, IOBUF_SIZE);
697 kanji_convert(stdin);
703 if ((fin = fopen((origfname = *argv++), "r")) == NULL) {
712 /* reopen file for stdout */
713 if (file_out == TRUE) {
716 outfname = malloc(strlen(origfname)
717 + strlen(".nkftmpXXXXXX")
723 strcpy(outfname, origfname);
727 for (i = strlen(outfname); i; --i){
728 if (outfname[i - 1] == '/'
729 || outfname[i - 1] == '\\'){
735 strcat(outfname, "ntXXXXXX");
737 fd = open(outfname, O_WRONLY | O_CREAT | O_TRUNC,
740 strcat(outfname, ".nkftmpXXXXXX");
741 fd = mkstemp(outfname);
744 || (fd_backup = dup(fileno(stdout))) < 0
745 || dup2(fd, fileno(stdout)) < 0
756 outfname = "nkf.out";
759 if(freopen(outfname, "w", stdout) == NULL) {
763 if (binmode_f == TRUE) {
765 if (freopen("","wb",stdout) == NULL)
772 if (binmode_f == TRUE)
774 if (freopen("","rb",fin) == NULL)
779 setvbuffer(fin, stdibuf, IOBUF_SIZE);
788 #if defined(MSDOS) && !defined(__MINGW32__)
796 if (dup2(fd_backup, fileno(stdout)) < 0){
799 if (stat(origfname, &sb)) {
800 fprintf(stderr, "Can't stat %s\n", origfname);
802 /*
\e$B%Q!<%_%C%7%g%s$rI|85
\e(B */
803 if (chmod(outfname, sb.st_mode)) {
804 fprintf(stderr, "Can't set permission %s\n", outfname);
807 /*
\e$B%?%$%`%9%?%s%W$rI|85
\e(B */
808 #if defined(MSDOS) && !defined(__MINGW32__)
809 tb[0] = tb[1] = sb.st_mtime;
810 if (utime(outfname, tb)) {
811 fprintf(stderr, "Can't set timestamp %s\n", outfname);
814 tb.actime = sb.st_atime;
815 tb.modtime = sb.st_mtime;
816 if (utime(outfname, &tb)) {
817 fprintf(stderr, "Can't set timestamp %s\n", outfname);
821 if (unlink(origfname)){
825 if (rename(outfname, origfname)) {
827 fprintf(stderr, "Can't rename %s to %s\n",
828 outfname, origfname);
836 #ifdef EASYWIN /*Easy Win */
837 if (file_out == FALSE)
838 scanf("%d",&end_check);
841 #else /* for Other OS */
842 if (file_out == TRUE)
872 {"katakana-hiragana","h3"},
873 #ifdef UTF8_OUTPUT_ENABLE
877 #ifdef UTF8_INPUT_ENABLE
879 {"utf16-input", "W16"},
888 #ifdef NUMCHAR_OPTION
889 {"numchar-input", ""},
895 #ifdef SHIFTJIS_CP932
904 static int option_mode;
919 case '-': /* literal options */
920 if (!*cp) { /* ignore the rest of arguments */
924 for (i=0;i<sizeof(long_option)/sizeof(long_option[0]);i++) {
926 p = (unsigned char *)long_option[i].name;
927 for (j=0;*p && *p++ == cp[j];j++);
928 if (! *p && !cp[j]) break;
931 cp = (unsigned char *)long_option[i].alias;
934 if (strcmp(long_option[i].name, "overwrite") == 0){
941 if (strcmp(long_option[i].name, "cap-input") == 0){
945 if (strcmp(long_option[i].name, "url-input") == 0){
950 #ifdef NUMCHAR_OPTION
951 if (strcmp(long_option[i].name, "numchar-input") == 0){
957 if (strcmp(long_option[i].name, "no-output") == 0){
961 if (strcmp(long_option[i].name, "debug") == 0){
966 #ifdef SHIFTJIS_CP932
967 if (strcmp(long_option[i].name, "no-cp932") == 0){
973 if (strcmp(long_option[i].name, "exec-in") == 0){
977 if (strcmp(long_option[i].name, "exec-out") == 0){
984 case 'b': /* buffered mode */
987 case 'u': /* non bufferd mode */
990 case 't': /* transparent mode */
993 case 'j': /* JIS output */
995 output_conv = j_oconv;
997 case 'e': /* AT&T EUC output */
998 output_conv = e_oconv;
1000 case 's': /* SJIS output */
1001 output_conv = s_oconv;
1003 case 'l': /* ISO8859 Latin-1 support, no conversion */
1004 iso8859_f = TRUE; /* Only compatible with ISO-2022-JP */
1005 input_f = LATIN1_INPUT;
1007 case 'i': /* Kanji IN ESC-$-@/B */
1008 if (*cp=='@'||*cp=='B')
1009 kanji_intro = *cp++;
1011 case 'o': /* ASCII IN ESC-(-J/B */
1012 if (*cp=='J'||*cp=='B'||*cp=='H')
1013 ascii_intro = *cp++;
1020 if ('9'>= *cp && *cp>='0')
1021 hira_f |= (*cp++ -'0');
1028 #if defined(MSDOS) || defined(__OS2__)
1043 #ifdef UTF8_OUTPUT_ENABLE
1044 case 'w': /* UTF-8 output */
1045 if ('1'== cp[0] && '6'==cp[1]) {
1046 output_conv = w_oconv16; cp+=2;
1048 w_oconv16_begin_f=2; cp++;
1051 w_oconv16_begin_f=1; cp++;
1053 } else if (cp[0] == 'B') {
1054 w_oconv16_begin_f=2; cp++;
1056 w_oconv16_begin_f=1; cp++;
1060 output_conv = w_oconv;
1063 #ifdef UTF8_INPUT_ENABLE
1064 case 'W': /* UTF-8 input */
1065 if ('1'== cp[0] && '6'==cp[1]) {
1066 input_f = UTF16_INPUT;
1068 input_f = UTF8_INPUT;
1071 /* Input code assumption */
1072 case 'J': /* JIS input */
1073 case 'E': /* AT&T EUC input */
1074 input_f = JIS_INPUT;
1076 case 'S': /* MS Kanji input */
1077 input_f = SJIS_INPUT;
1078 if (x0201_f==NO_X0201) x0201_f=TRUE;
1080 case 'Z': /* Convert X0208 alphabet to asii */
1081 /* bit:0 Convert X0208
1082 bit:1 Convert Kankaku to one space
1083 bit:2 Convert Kankaku to two spaces
1084 bit:3 Convert HTML Entity
1086 if ('9'>= *cp && *cp>='0')
1087 alpha_f |= 1<<(*cp++ -'0');
1091 case 'x': /* Convert X0201 kana to X0208 or X0201 Conversion */
1092 x0201_f = FALSE; /* No X0201->X0208 conversion */
1094 ESC-(-I in JIS, EUC, MS Kanji
1095 SI/SO in JIS, EUC, MS Kanji
1096 SSO in EUC, JIS, not in MS Kanji
1097 MS Kanji (0xa0-0xdf)
1099 ESC-(-I in JIS (0x20-0x5f)
1100 SSO in EUC (0xa0-0xdf)
1101 0xa0-0xd in MS Kanji (0xa0-0xdf)
1104 case 'X': /* Assume X0201 kana */
1105 /* Default value is NO_X0201 for EUC/MS-Kanji mix */
1108 case 'F': /* prserve new lines */
1109 fold_preserve_f = TRUE;
1110 case 'f': /* folding -f60 or -f */
1113 while('0'<= *cp && *cp <='9') { /* we don't use atoi here */
1115 fold_len += *cp++ - '0';
1117 if (!(0<fold_len && fold_len<BUFSIZ))
1118 fold_len = DEFAULT_FOLD;
1122 while('0'<= *cp && *cp <='9') { /* we don't use atoi here */
1124 fold_margin += *cp++ - '0';
1128 case 'm': /* MIME support */
1129 if (*cp=='B'||*cp=='Q') {
1130 mime_decode_mode = *cp++;
1131 mimebuf_f = FIXED_MIME;
1132 } else if (*cp=='N') {
1133 mime_f = TRUE; cp++;
1134 } else if (*cp=='S') {
1135 mime_f = STRICT_MIME; cp++;
1136 } else if (*cp=='0') {
1137 mime_f = FALSE; cp++;
1140 case 'M': /* MIME output */
1143 mimeout_f = FIXED_MIME; cp++;
1144 } else if (*cp=='Q') {
1146 mimeout_f = FIXED_MIME; cp++;
1151 case 'B': /* Broken JIS support */
1153 bit:1 allow any x on ESC-(-x or ESC-$-x
1154 bit:2 reset to ascii on NL
1156 if ('9'>= *cp && *cp>='0')
1157 broken_f |= 1<<(*cp++ -'0');
1162 case 'O':/* for Output file */
1166 case 'c':/* add cr code */
1169 case 'd':/* delete cr code */
1172 case 'I': /* ISO-2022-JP output */
1175 case 'L': /* line mode */
1176 if (*cp=='u') { /* unix */
1177 crmode_f = NL; cp++;
1178 } else if (*cp=='m') { /* mac */
1179 crmode_f = CR; cp++;
1180 } else if (*cp=='w') { /* windows */
1181 crmode_f = CRLF; cp++;
1182 } else if (*cp=='0') { /* no conversion */
1187 /* module muliple options in a string are allowed for Perl moudle */
1188 while(*cp && *cp!='-') cp++;
1192 /* bogus option but ignored */
1198 #ifdef ANSI_C_PROTOTYPE
1199 struct input_code * find_inputcode_byfunc(int (*iconv_func)(int c2,int c1,int c0))
1201 struct input_code * find_inputcode_byfunc(iconv_func)
1202 int (*iconv_func)();
1206 struct input_code *p = input_code_list;
1208 if (iconv_func == p->iconv_func){
1217 #ifdef ANSI_C_PROTOTYPE
1218 void set_iconv(int f, int (*iconv_func)(int c2,int c1,int c0))
1220 void set_iconv(f, iconv_func)
1222 int (*iconv_func)();
1226 static int (*iconv_for_check)() = 0;
1228 #ifdef INPUT_CODE_FIX
1236 #ifdef INPUT_CODE_FIX
1237 && (f == -TRUE || !input_f) /* -TRUE means "FORCE" */
1243 if (estab_f && iconv_for_check != iconv){
1244 struct input_code *p = find_inputcode_byfunc(iconv);
1246 debug(input_codename = p->name);
1248 iconv_for_check = iconv;
1253 #define SCORE_KANA (1) /*
\e$B$$$o$f$kH>3Q%+%J
\e(B */
1254 #define SCORE_DEPEND (SCORE_KANA << 1) /*
\e$B5!<o0MB8J8;z
\e(B */
1255 #ifdef SHIFTJIS_CP932
1256 #define SCORE_CP932 (SCORE_DEPEND << 1) /* CP932
\e$B$K$h$kFI$_49$(
\e(B */
1257 #define SCORE_NO_EXIST (SCORE_CP932 << 1) /*
\e$BB8:_$7$J$$J8;z
\e(B */
1259 #define SCORE_NO_EXIST (SCORE_DEPEND << 1) /*
\e$BB8:_$7$J$$J8;z
\e(B */
1261 #define SCORE_iMIME (SCORE_NO_EXIST << 1) /* MIME
\e$B$K$h$k;XDj
\e(B */
1262 #define SCORE_ERROR (SCORE_iMIME << 1) /*
\e$B%(%i!<
\e(B */
1264 #define SCORE_INIT (SCORE_iMIME)
1266 int score_table_A0[] = {
1269 0, SCORE_DEPEND, SCORE_DEPEND, SCORE_DEPEND,
1270 SCORE_DEPEND, SCORE_DEPEND, SCORE_DEPEND, SCORE_NO_EXIST,
1273 int score_table_F0[] = {
1275 0, SCORE_DEPEND, SCORE_NO_EXIST, SCORE_NO_EXIST,
1276 SCORE_DEPEND, SCORE_DEPEND, SCORE_DEPEND, SCORE_DEPEND,
1277 SCORE_DEPEND, SCORE_NO_EXIST, SCORE_NO_EXIST, SCORE_ERROR,
1280 void set_code_score(ptr, score)
1281 struct input_code *ptr;
1285 ptr->score |= score;
1289 void clr_code_score(ptr, score)
1290 struct input_code *ptr;
1294 ptr->score &= ~score;
1298 void code_score(ptr)
1299 struct input_code *ptr;
1301 int c2 = ptr->buf[0];
1302 int c1 = ptr->buf[1];
1304 set_code_score(ptr, SCORE_ERROR);
1305 }else if ((c2 & 0xf0) == 0xa0){
1306 set_code_score(ptr, score_table_A0[c2 & 0x0f]);
1307 }else if ((c2 & 0xf0) == 0xf0){
1308 set_code_score(ptr, score_table_F0[c2 & 0x0f]);
1309 }else if (c2 == SSO){
1310 set_code_score(ptr, SCORE_KANA);
1312 #ifdef UTF8_OUTPUT_ENABLE
1313 else if (!e2w_conv(c2, c1)){
1314 set_code_score(ptr, SCORE_NO_EXIST);
1319 void status_disable(ptr)
1320 struct input_code *ptr;
1325 if (iconv == ptr->iconv_func) set_iconv(FALSE, 0);
1328 void status_push_ch(ptr, c)
1329 struct input_code *ptr;
1332 ptr->buf[ptr->index++] = c;
1335 void status_clear(ptr)
1336 struct input_code *ptr;
1339 ptr->score &= SCORE_INIT;
1343 void status_reset(ptr)
1344 struct input_code *ptr;
1347 ptr->score = SCORE_INIT;
1350 void status_reinit(ptr)
1351 struct input_code *ptr;
1354 ptr->_file_stat = 0;
1357 void status_check(ptr, c)
1358 struct input_code *ptr;
1361 if (c <= DEL && estab_f){
1366 void s_status(ptr, c)
1367 struct input_code *ptr;
1372 status_check(ptr, c);
1377 #ifdef NUMCHAR_OPTION
1378 }else if ((c & CLASS_MASK) == CLASS_UTF16){
1381 }else if (0xa1 <= c && c <= 0xdf){
1382 status_push_ch(ptr, SSO);
1383 status_push_ch(ptr, c);
1386 }else if ((0x81 <= c && c < 0xa0) || (0xe0 <= c && c <= 0xef)){
1388 status_push_ch(ptr, c);
1389 #ifdef SHIFTJIS_CP932
1391 && CP932_TABLE_BEGIN <= c && c <= CP932_TABLE_END){
1393 status_push_ch(ptr, c);
1394 #endif /* SHIFTJIS_CP932 */
1396 status_disable(ptr);
1400 if ((0x40 <= c && c <= 0x7e) || (0x80 <= c && c <= 0xfc)){
1401 status_push_ch(ptr, c);
1402 s2e_conv(ptr->buf[0], ptr->buf[1], &ptr->buf[0], &ptr->buf[1]);
1406 status_disable(ptr);
1409 #ifdef SHIFTJIS_CP932
1411 if ((0x40 <= c && c <= 0x7e) || (0x80 <= c && c <= 0xfc)){
1412 status_push_ch(ptr, c);
1413 if (s2e_conv(ptr->buf[0], ptr->buf[1], &ptr->buf[0], &ptr->buf[1]) == 0){
1414 set_code_score(ptr, SCORE_CP932);
1419 status_disable(ptr);
1421 #endif /* SHIFTJIS_CP932 */
1425 void e_status(ptr, c)
1426 struct input_code *ptr;
1431 status_check(ptr, c);
1436 #ifdef NUMCHAR_OPTION
1437 }else if ((c & CLASS_MASK) == CLASS_UTF16){
1440 }else if (SSO == c || (0xa1 <= c && c <= 0xfe)){
1442 status_push_ch(ptr, c);
1444 status_disable(ptr);
1448 if (0xa1 <= c && c <= 0xfe){
1449 status_push_ch(ptr, c);
1453 status_disable(ptr);
1459 #ifdef UTF8_INPUT_ENABLE
1460 void w16_status(ptr, c)
1461 struct input_code *ptr;
1468 if (ptr->_file_stat == 0){
1469 if (c == 0xfe || c == 0xff){
1471 status_push_ch(ptr, c);
1472 ptr->_file_stat = 1;
1474 status_disable(ptr);
1475 ptr->_file_stat = -1;
1477 }else if (ptr->_file_stat > 0){
1479 status_push_ch(ptr, c);
1480 }else if (ptr->_file_stat < 0){
1481 status_disable(ptr);
1487 status_disable(ptr);
1488 ptr->_file_stat = -1;
1490 status_push_ch(ptr, c);
1497 if (ptr->stat != c && (c == 0xfe || c == 0xff)){
1498 status_push_ch(ptr, c);
1501 status_disable(ptr);
1502 ptr->_file_stat = -1;
1508 void w_status(ptr, c)
1509 struct input_code *ptr;
1514 status_check(ptr, c);
1519 #ifdef NUMCHAR_OPTION
1520 }else if ((c & CLASS_MASK) == CLASS_UTF16){
1523 }else if (0xc0 <= c && c <= 0xdf){
1525 status_push_ch(ptr, c);
1526 }else if (0xe0 <= c && c <= 0xef){
1528 status_push_ch(ptr, c);
1530 status_disable(ptr);
1535 if (0x80 <= c && c <= 0xbf){
1536 status_push_ch(ptr, c);
1537 if (ptr->index > ptr->stat){
1538 w2e_conv(ptr->buf[0], ptr->buf[1], ptr->buf[2],
1539 &ptr->buf[0], &ptr->buf[1]);
1544 status_disable(ptr);
1555 int action_flag = 1;
1556 struct input_code *result = 0;
1557 struct input_code *p = input_code_list;
1559 (p->status_func)(p, c);
1562 }else if(p->stat == 0){
1573 if (result && !estab_f){
1574 set_iconv(TRUE, result->iconv_func);
1575 }else if (c <= DEL){
1576 struct input_code *ptr = input_code_list;
1586 #define STD_GC_BUFSIZE (256)
1587 int std_gc_buf[STD_GC_BUFSIZE];
1597 return std_gc_buf[--std_gc_ndx];
1609 if (std_gc_ndx == STD_GC_BUFSIZE){
1612 std_gc_buf[std_gc_ndx++] = c;
1632 while ((c = (*i_getc)(f)) != EOF)
1641 oconv = output_conv;
1644 /* replace continucation module, from output side */
1646 /* output redicrection */
1655 if (mimeout_f == TRUE) {
1656 o_base64conv = oconv; oconv = base64_conv;
1658 /* base64_count = 0; */
1662 o_crconv = oconv; oconv = cr_conv;
1665 o_rot_conv = oconv; oconv = rot_conv;
1668 o_iso2022jp_check_conv = oconv; oconv = iso2022jp_check_conv;
1671 o_hira_conv = oconv; oconv = hira_conv;
1674 o_fconv = oconv; oconv = fold_conv;
1677 if (alpha_f || x0201_f) {
1678 o_zconv = oconv; oconv = z_conv;
1682 /* input redicrection */
1685 i_cgetc = i_getc; i_getc = cap_getc;
1686 i_cungetc = i_ungetc; i_ungetc= cap_ungetc;
1689 i_ugetc = i_getc; i_getc = url_getc;
1690 i_uungetc = i_ungetc; i_ungetc= url_ungetc;
1693 #ifdef NUMCHAR_OPTION
1695 i_ngetc = i_getc; i_getc = numchar_getc;
1696 i_nungetc = i_ungetc; i_ungetc= numchar_ungetc;
1699 if (mime_f && mimebuf_f==FIXED_MIME) {
1700 i_mgetc = i_getc; i_getc = mime_getc;
1701 i_mungetc = i_ungetc; i_ungetc = mime_ungetc;
1704 i_bgetc = i_getc; i_getc = broken_getc;
1705 i_bungetc = i_ungetc; i_ungetc = broken_ungetc;
1707 if (input_f == JIS_INPUT || input_f == LATIN1_INPUT) {
1708 set_iconv(-TRUE, e_iconv);
1709 } else if (input_f == SJIS_INPUT) {
1710 set_iconv(-TRUE, s_iconv);
1711 #ifdef UTF8_INPUT_ENABLE
1712 } else if (input_f == UTF8_INPUT) {
1713 set_iconv(-TRUE, w_iconv);
1714 } else if (input_f == UTF16_INPUT) {
1715 set_iconv(-TRUE, w_iconv16);
1718 set_iconv(FALSE, e_iconv);
1722 struct input_code *p = input_code_list;
1730 Conversion main loop. Code detection only.
1740 module_connection();
1745 output_mode = ASCII;
1748 #define NEXT continue /* no output, get next */
1749 #define SEND ; /* output c1 and c2, get next */
1750 #define LAST break /* end of loop, go closing */
1752 while ((c1 = (*i_getc)(f)) != EOF) {
1757 /* in case of 8th bit is on */
1759 /* in case of not established yet */
1760 /* It is still ambiguious */
1761 if (h_conv(f, c2, c1)==EOF)
1767 /* in case of already established */
1769 /* ignore bogus code */
1775 /* second byte, 7 bit code */
1776 /* it might be kanji shitfted */
1777 if ((c1 == DEL) || (c1 <= SPACE)) {
1778 /* ignore bogus first code */
1786 #ifdef UTF8_INPUT_ENABLE
1795 #ifdef NUMCHAR_OPTION
1796 } else if ((c1 & CLASS_MASK) == CLASS_UTF16){
1799 } else if (c1 > DEL) {
1801 if (!estab_f && !iso8859_f) {
1802 /* not established yet */
1805 } else { /* estab_f==TRUE */
1810 } else if (SSP<=c1 && c1<0xe0 && iconv == s_iconv) {
1811 /* SJIS X0201 Case... */
1812 if(iso2022jp_f && x0201_f==NO_X0201) {
1813 (*oconv)(GETA1, GETA2);
1820 } else if (c1==SSO && iconv != s_iconv) {
1821 /* EUC X0201 Case */
1822 c1 = (*i_getc)(f); /* skip SSO */
1824 if (SSP<=c1 && c1<0xe0) {
1825 if(iso2022jp_f && x0201_f==NO_X0201) {
1826 (*oconv)(GETA1, GETA2);
1833 } else { /* bogus code, skip SSO and one byte */
1837 /* already established */
1842 } else if ((c1 > SPACE) && (c1 != DEL)) {
1843 /* in case of Roman characters */
1845 /* output 1 shifted byte */
1849 } else if (SPACE<=c1 && c1<(0xe0&0x7f) ){
1850 /* output 1 shifted byte */
1851 if(iso2022jp_f && x0201_f==NO_X0201) {
1852 (*oconv)(GETA1, GETA2);
1859 /* look like bogus code */
1862 } else if (input_mode == X0208) {
1863 /* in case of Kanji shifted */
1866 } else if (c1 == '=' && mime_f && !mime_decode_mode ) {
1867 /* Check MIME code */
1868 if ((c1 = (*i_getc)(f)) == EOF) {
1871 } else if (c1 == '?') {
1872 /* =? is mime conversion start sequence */
1873 if(mime_f == STRICT_MIME) {
1874 /* check in real detail */
1875 if (mime_begin_strict(f) == EOF)
1879 } else if (mime_begin(f) == EOF)
1889 /* normal ASCII code */
1892 } else if (c1 == SI) {
1895 } else if (c1 == SO) {
1898 } else if (c1 == ESC ) {
1899 if ((c1 = (*i_getc)(f)) == EOF) {
1900 /* (*oconv)(0, ESC); don't send bogus code */
1902 } else if (c1 == '$') {
1903 if ((c1 = (*i_getc)(f)) == EOF) {
1905 (*oconv)(0, ESC); don't send bogus code
1906 (*oconv)(0, '$'); */
1908 } else if (c1 == '@'|| c1 == 'B') {
1909 /* This is kanji introduction */
1912 debug(input_codename = "ISO-2022-JP");
1914 } else if (c1 == '(') {
1915 if ((c1 = (*i_getc)(f)) == EOF) {
1916 /* don't send bogus code
1922 } else if (c1 == '@'|| c1 == 'B') {
1923 /* This is kanji introduction */
1928 /* could be some special code */
1935 } else if (broken_f&0x2) {
1936 /* accept any ESC-(-x as broken code ... */
1946 } else if (c1 == '(') {
1947 if ((c1 = (*i_getc)(f)) == EOF) {
1948 /* don't send bogus code
1950 (*oconv)(0, '('); */
1954 /* This is X0201 kana introduction */
1955 input_mode = X0201; shift_mode = X0201;
1957 } else if (c1 == 'B' || c1 == 'J' || c1 == 'H') {
1958 /* This is X0208 kanji introduction */
1959 input_mode = ASCII; shift_mode = FALSE;
1961 } else if (broken_f&0x2) {
1962 input_mode = ASCII; shift_mode = FALSE;
1967 /* maintain various input_mode here */
1971 } else if ( c1 == 'N' || c1 == 'n' ){
1973 c1 = (*i_getc)(f); /* skip SS2 */
1974 if ( SPACE<=c1 && c1 < 0xe0 ) {
1983 } else if ((c1 == NL || c1 == CR) && broken_f&4) {
1984 input_mode = ASCII; set_iconv(FALSE, 0);
1990 if (input_mode == X0208)
1991 (*oconv)(c2, c1); /* this is JIS, not SJIS/EUC case */
1992 else if (input_mode)
1993 (*oconv)(input_mode, c1); /* other special case */
1994 else if ((*iconv)(c2, c1, 0) < 0){ /* can be EUC/SJIS */
1995 int c0 = (*i_getc)(f);
1998 (*iconv)(c2, c1, c0);
2004 /* goto next_word */
2008 (*iconv)(EOF, 0, 0);
2021 /** it must NOT be in the kanji shifte sequence */
2022 /** it must NOT be written in JIS7 */
2023 /** and it must be after 2 byte 8bit code */
2030 while ((c1 = (*i_getc)(f)) != EOF) {
2036 if (push_hold_buf(c1) == EOF || estab_f){
2042 struct input_code *p = input_code_list;
2043 struct input_code *result = p;
2048 if (p->score < result->score){
2053 set_iconv(FALSE, result->iconv_func);
2058 ** 1) EOF is detected, or
2059 ** 2) Code is established, or
2060 ** 3) Buffer is FULL (but last word is pushed)
2062 ** in 1) and 3) cases, we continue to use
2063 ** Kanji codes by oconv and leave estab_f unchanged.
2068 while (wc < hold_count){
2069 c2 = hold_buf[wc++];
2071 #ifdef NUMCHAR_OPTION
2072 || (c2 & CLASS_MASK) == CLASS_UTF16
2077 }else if (iconv == s_iconv && 0xa1 <= c2 && c2 <= 0xdf){
2078 (*iconv)(X0201, c2, 0);
2081 if (wc < hold_count){
2082 c1 = hold_buf[wc++];
2091 if ((*iconv)(c2, c1, 0) < 0){
2093 if (wc < hold_count){
2094 c0 = hold_buf[wc++];
2103 (*iconv)(c2, c1, c0);
2116 if (hold_count >= HOLD_SIZE*2)
2118 hold_buf[hold_count++] = c2;
2119 return ((hold_count >= HOLD_SIZE*2) ? EOF : hold_count);
2122 int s2e_conv(c2, c1, p2, p1)
2126 #ifdef SHIFTJIS_CP932
2127 if (CP932_TABLE_BEGIN <= c2 && c2 <= CP932_TABLE_END){
2128 extern unsigned short shiftjis_cp932[3][189];
2129 c1 = shiftjis_cp932[c2 - CP932_TABLE_BEGIN][c1 - 0x40];
2130 if (c1 == 0) return 1;
2134 #endif /* SHIFTJIS_CP932 */
2135 c2 = c2 + c2 - ((c2 <= 0x9f) ? SJ0162 : SJ6394);
2137 c1 = c1 - ((c1 > DEL) ? SPACE : 0x1f);
2154 } else if ((c2 == EOF) || (c2 == 0) || c2 < SPACE) {
2157 int ret = s2e_conv(c2, c1, &c2, &c1);
2158 if (ret) return ret;
2171 } else if (c2 == SSO){
2174 } else if ((c2 == EOF) || (c2 == 0) || c2 < SPACE) {
2184 #ifdef UTF8_INPUT_ENABLE
2186 w2e_conv(c2, c1, c0, p2, p1)
2190 extern unsigned short * utf8_to_euc_2bytes[];
2191 extern unsigned short ** utf8_to_euc_3bytes[];
2194 if (0xc0 <= c2 && c2 <= 0xef) {
2195 unsigned short **pp;
2198 if (c0 == 0) return -1;
2199 pp = utf8_to_euc_3bytes[c2 - 0x80];
2200 ret = w_iconv_common(c1, c0, pp, sizeof_utf8_to_euc_C2, p2, p1);
2202 ret = w_iconv_common(c2, c1, utf8_to_euc_2bytes, sizeof_utf8_to_euc_2bytes, p2, p1);
2204 #ifdef NUMCHAR_OPTION
2206 c1 = CLASS_UTF16 | ww16_conv(c2, c1, c0);
2212 } else if (c2 == X0201) {
2225 int ret = w2e_conv(c2, c1, c0, &c2, &c1);
2233 w16w_conv(val, p2, p1, p0)
2241 }else if (val < 0x800){
2242 *p2 = 0xc0 | (val >> 6);
2243 *p1 = 0x80 | (val & 0x3f);
2246 *p2 = 0xe0 | (val >> 12);
2247 *p1 = 0x80 | ((val >> 6) & 0x3f);
2248 *p0 = 0x80 | (val & 0x3f);
2253 ww16_conv(c2, c1, c0)
2258 val = (c2 & 0x0f) << 12;
2259 val |= (c1 & 0x3f) << 6;
2261 }else if (c2 >= 0xc0){
2262 val = (c2 & 0x1f) << 6;
2263 val |= (c1 & 0x3f) << 6;
2271 w16e_conv(val, p2, p1)
2275 extern unsigned short * utf8_to_euc_2bytes[];
2276 extern unsigned short ** utf8_to_euc_3bytes[];
2278 unsigned short **pp;
2282 w16w_conv(val, &c2, &c1, &c0);
2285 pp = utf8_to_euc_3bytes[c2 - 0x80];
2286 psize = sizeof_utf8_to_euc_C2;
2287 ret = w_iconv_common(c1, c0, pp, psize, p2, p1);
2289 pp = utf8_to_euc_2bytes;
2290 psize = sizeof_utf8_to_euc_2bytes;
2291 ret = w_iconv_common(c2, c1, pp, psize, p2, p1);
2293 #ifdef NUMCHAR_OPTION
2296 *p1 = CLASS_UTF16 | val;
2305 w_iconv16(c2, c1, c0)
2310 if (c2==0376 && c1==0377){
2311 utf16_mode = UTF16_INPUT;
2313 } else if (c2==0377 && c1==0376){
2314 utf16_mode = UTF16BE_INPUT;
2317 if (utf16_mode == UTF16BE_INPUT) {
2319 tmp=c1; c1=c2; c2=tmp;
2321 if ((c2==0 && c1 < 0x80) || c2==EOF) {
2325 ret = w16e_conv(((c2<<8)&0xff00) + c1, &c2, &c1);
2326 if (ret) return ret;
2332 w_iconv_common(c1, c0, pp, psize, p2, p1)
2334 unsigned short **pp;
2342 if (pp == 0) return 1;
2345 if (c1 < 0 || psize <= c1) return 1;
2347 if (p == 0) return 1;
2350 if (c0 < 0 || sizeof_utf8_to_euc_E5B8 <= c0) return 1;
2352 if (val == 0) return 1;
2355 if (c2 == SO) c2 = X0201;
2364 #ifdef UTF8_OUTPUT_ENABLE
2369 extern unsigned short euc_to_utf8_1byte[];
2370 extern unsigned short * euc_to_utf8_2bytes[];
2374 p = euc_to_utf8_1byte;
2377 c2 = (c2&0x7f) - 0x21;
2378 if (0<=c2 && c2<sizeof_euc_to_utf8_2bytes)
2379 p = euc_to_utf8_2bytes[c2];
2384 c1 = (c1 & 0x7f) - 0x21;
2385 if (0<=c1 && c1<sizeof_euc_to_utf8_1byte)
2396 #ifdef NUMCHAR_OPTION
2397 if (c2 == 0 && (c1 & CLASS_MASK) == CLASS_UTF16){
2398 w16w_conv(c1, &c2, &c1, &c0);
2402 if (c0) (*o_putc)(c0);
2409 } else if (c2 == 0) {
2410 output_mode = ASCII;
2412 } else if (c2 == ISO8859_1) {
2413 output_mode = ISO8859_1;
2414 (*o_putc)(c1 | 0x080);
2417 w16w_conv((unsigned short)e2w_conv(c2, c1), &c2, &c1, &c0);
2421 if (c0) (*o_putc)(c0);
2436 if (w_oconv16_begin_f==2) {
2438 (*o_putc)((unsigned char)'\377');
2442 (*o_putc)((unsigned char)'\377');
2444 w_oconv16_begin_f=1;
2447 if (c2 == ISO8859_1) {
2450 #ifdef NUMCHAR_OPTION
2451 } else if (c2 == 0 && (c1 & CLASS_MASK) == CLASS_UTF16) {
2452 c2 = (c1 >> 8) & 0xff;
2456 unsigned short val = (unsigned short)e2w_conv(c2, c1);
2457 c2 = (val >> 8) & 0xff;
2476 #ifdef NUMCHAR_OPTION
2477 if (c2 == 0 && (c1 & CLASS_MASK) == CLASS_UTF16){
2478 w16e_conv(c1, &c2, &c1);
2484 } else if (c2 == 0) {
2485 output_mode = ASCII;
2487 } else if (c2 == X0201) {
2488 output_mode = JAPANESE_EUC;
2489 (*o_putc)(SSO); (*o_putc)(c1|0x80);
2490 } else if (c2 == ISO8859_1) {
2491 output_mode = ISO8859_1;
2492 (*o_putc)(c1 | 0x080);
2494 if ((c1<0x20 || 0x7e<c1) ||
2495 (c2<0x20 || 0x7e<c2)) {
2496 set_iconv(FALSE, 0);
2497 return; /* too late to rescue this char */
2499 output_mode = JAPANESE_EUC;
2500 (*o_putc)(c2 | 0x080);
2501 (*o_putc)(c1 | 0x080);
2506 e2s_conv(c2, c1, p2, p1)
2507 int c2, c1, *p2, *p1;
2509 if (p2) *p2 = ((c2 - 1) >> 1) + ((c2 <= 0x5e) ? 0x71 : 0xb1);
2510 if (p1) *p1 = c1 + ((c2 & 1) ? ((c1 < 0x60) ? 0x1f : 0x20) : 0x7e);
2518 #ifdef NUMCHAR_OPTION
2519 if (c2 == 0 && (c1 & CLASS_MASK) == CLASS_UTF16){
2520 w16e_conv(c1, &c2, &c1);
2526 } else if (c2 == 0) {
2527 output_mode = ASCII;
2529 } else if (c2 == X0201) {
2530 output_mode = SHIFT_JIS;
2532 } else if (c2 == ISO8859_1) {
2533 output_mode = ISO8859_1;
2534 (*o_putc)(c1 | 0x080);
2536 if ((c1<0x20 || 0x7e<c1) ||
2537 (c2<0x20 || 0x7e<c2)) {
2538 set_iconv(FALSE, 0);
2539 return; /* too late to rescue this char */
2541 output_mode = SHIFT_JIS;
2542 e2s_conv(c2, c1, &c2, &c1);
2553 #ifdef NUMCHAR_OPTION
2554 if ((c1 & CLASS_MASK) == CLASS_UTF16){
2555 w16e_conv(c1, &c2, &c1);
2559 if (output_mode !=ASCII && output_mode!=ISO8859_1) {
2562 (*o_putc)(ascii_intro);
2563 output_mode = ASCII;
2566 } else if (c2==X0201) {
2567 if (output_mode!=X0201) {
2568 output_mode = X0201;
2574 } else if (c2==ISO8859_1) {
2575 /* iso8859 introduction, or 8th bit on */
2576 /* Can we convert in 7bit form using ESC-'-'-A ?
2578 output_mode = ISO8859_1;
2580 } else if (c2 == 0) {
2581 if (output_mode !=ASCII && output_mode!=ISO8859_1) {
2584 (*o_putc)(ascii_intro);
2585 output_mode = ASCII;
2589 if (output_mode != X0208) {
2590 output_mode = X0208;
2593 (*o_putc)(kanji_intro);
2595 if (c1<0x20 || 0x7e<c1)
2597 if (c2<0x20 || 0x7e<c2)
2609 if (base64_count>50 && !mimeout_mode && c2==0 && c1==SPACE) {
2611 } else if (base64_count>66 && mimeout_mode) {
2612 (*o_base64conv)(EOF,0);
2614 (*o_putc)('\t'); base64_count += 7;
2616 (*o_base64conv)(c2,c1);
2620 static int broken_buf[3];
2621 static int broken_counter = 0;
2622 static int broken_last = 0;
2629 if (broken_counter>0) {
2630 return broken_buf[--broken_counter];
2633 if (c=='$' && broken_last != ESC
2634 && (input_mode==ASCII || input_mode==X0201)) {
2637 if (c1=='@'|| c1=='B') {
2638 broken_buf[0]=c1; broken_buf[1]=c;
2645 } else if (c=='(' && broken_last != ESC
2646 && (input_mode==X0208 || input_mode==X0201)) { /* ) */
2649 if (c1=='J'|| c1=='B') {
2650 broken_buf[0]=c1; broken_buf[1]=c;
2668 if (broken_counter<2)
2669 broken_buf[broken_counter++]=c;
2673 static int prev_cr = 0;
2681 if (! (c2==0&&c1==NL) ) {
2687 } else if (c1=='\r') {
2689 } else if (c1=='\n') {
2690 if (crmode_f==CRLF) {
2691 (*o_crconv)(0,'\r');
2692 } else if (crmode_f==CR) {
2693 (*o_crconv)(0,'\r');
2697 } else if (c1!='\032' || crmode_f!=NL){
2703 Return value of fold_conv()
2705 \n add newline and output char
2706 \r add newline and output nothing
2709 1 (or else) normal output
2711 fold state in prev (previous character)
2713 >0x80 Japanese (X0208/X0201)
2718 This fold algorthm does not preserve heading space in a line.
2719 This is the main difference from fmt.
2722 #define char_size(c2,c1) (c2?2:1)
2731 if (c1== '\r' && !fold_preserve_f) {
2732 fold_state=0; /* ignore cr */
2733 }else if (c1== '\n'&&f_prev=='\r' && fold_preserve_f) {
2735 fold_state=0; /* ignore cr */
2736 } else if (c1== BS) {
2737 if (f_line>0) f_line--;
2739 } else if (c2==EOF && f_line != 0) { /* close open last line */
2741 } else if ((c1=='\n' && !fold_preserve_f)
2742 || ((c1=='\r'||(c1=='\n'&&f_prev!='\r'))
2743 && fold_preserve_f)) {
2745 if (fold_preserve_f) {
2749 } else if ((f_prev == c1 && !fold_preserve_f)
2750 || (f_prev == '\n' && fold_preserve_f)
2751 ) { /* duplicate newline */
2754 fold_state = '\n'; /* output two newline */
2760 if (f_prev&0x80) { /* Japanese? */
2762 fold_state = 0; /* ignore given single newline */
2763 } else if (f_prev==' ') {
2767 if (++f_line<=fold_len)
2771 fold_state = '\r'; /* fold and output nothing */
2775 } else if (c1=='\f') {
2780 fold_state = '\n'; /* output newline and clear */
2781 } else if ( (c2==0 && c1==' ')||
2782 (c2==0 && c1=='\t')||
2783 (c2=='!'&& c1=='!')) {
2784 /* X0208 kankaku or ascii space */
2785 if (f_prev == ' ') {
2786 fold_state = 0; /* remove duplicate spaces */
2789 if (++f_line<=fold_len)
2790 fold_state = ' '; /* output ASCII space only */
2792 f_prev = ' '; f_line = 0;
2793 fold_state = '\r'; /* fold and output nothing */
2797 prev0 = f_prev; /* we still need this one... , but almost done */
2799 if (c2 || c2==X0201)
2800 f_prev |= 0x80; /* this is Japanese */
2801 f_line += char_size(c2,c1);
2802 if (f_line<=fold_len) { /* normal case */
2805 if (f_line>=fold_len+fold_margin) { /* too many kinsou suspension */
2806 f_line = char_size(c2,c1);
2807 fold_state = '\n'; /* We can't wait, do fold now */
2808 } else if (c2==X0201) {
2809 /* simple kinsoku rules return 1 means no folding */
2810 if (c1==(0xde&0x7f)) fold_state = 1; /*
\e$B!+
\e(B*/
2811 else if (c1==(0xdf&0x7f)) fold_state = 1; /*
\e$B!,
\e(B*/
2812 else if (c1==(0xa4&0x7f)) fold_state = 1; /*
\e$B!#
\e(B*/
2813 else if (c1==(0xa3&0x7f)) fold_state = 1; /*
\e$B!$
\e(B*/
2814 else if (c1==(0xa1&0x7f)) fold_state = 1; /*
\e$B!W
\e(B*/
2815 else if (c1==(0xb0&0x7f)) fold_state = 1; /* - */
2816 else if (SPACE<=c1 && c1<=(0xdf&0x7f)) { /* X0201 */
2818 fold_state = '\n';/* add one new f_line before this character */
2821 fold_state = '\n';/* add one new f_line before this character */
2824 /* kinsoku point in ASCII */
2825 if ( c1==')'|| /* { [ ( */
2836 /* just after special */
2837 } else if (!is_alnum(prev0)) {
2838 f_line = char_size(c2,c1);
2840 } else if ((prev0==' ') || /* ignored new f_line */
2841 (prev0=='\n')|| /* ignored new f_line */
2842 (prev0&0x80)) { /* X0208 - ASCII */
2843 f_line = char_size(c2,c1);
2844 fold_state = '\n';/* add one new f_line before this character */
2846 fold_state = 1; /* default no fold in ASCII */
2850 if (c1=='"') fold_state = 1; /*
\e$B!"
\e(B */
2851 else if (c1=='#') fold_state = 1; /*
\e$B!#
\e(B */
2852 else if (c1=='W') fold_state = 1; /*
\e$B!W
\e(B */
2853 else if (c1=='K') fold_state = 1; /*
\e$B!K
\e(B */
2854 else if (c1=='$') fold_state = 1; /*
\e$B!$
\e(B */
2855 else if (c1=='%') fold_state = 1; /*
\e$B!%
\e(B */
2856 else if (c1=='\'') fold_state = 1; /*
\e$B!\
\e(B */
2857 else if (c1=='(') fold_state = 1; /*
\e$B!(
\e(B */
2858 else if (c1==')') fold_state = 1; /*
\e$B!)
\e(B */
2859 else if (c1=='*') fold_state = 1; /*
\e$B!*
\e(B */
2860 else if (c1=='+') fold_state = 1; /*
\e$B!+
\e(B */
2861 else if (c1==',') fold_state = 1; /*
\e$B!,
\e(B */
2862 /* default no fold in kinsoku */
2865 f_line = char_size(c2,c1);
2866 /* add one new f_line before this character */
2869 f_line = char_size(c2,c1);
2871 /* add one new f_line before this character */
2876 /* terminator process */
2877 switch(fold_state) {
2896 int z_prev2=0,z_prev1=0;
2903 /* if (c2) c1 &= 0x7f; assertion */
2905 if (x0201_f && z_prev2==X0201) { /* X0201 */
2906 if (c1==(0xde&0x7f)) { /*
\e$BByE@
\e(B */
2908 (*o_zconv)(dv[(z_prev1-SPACE)*2],dv[(z_prev1-SPACE)*2+1]);
2910 } else if (c1==(0xdf&0x7f)&&ev[(z_prev1-SPACE)*2]) { /*
\e$BH>ByE@
\e(B */
2912 (*o_zconv)(ev[(z_prev1-SPACE)*2],ev[(z_prev1-SPACE)*2+1]);
2916 (*o_zconv)(cv[(z_prev1-SPACE)*2],cv[(z_prev1-SPACE)*2+1]);
2925 if (x0201_f && c2==X0201) {
2926 if (dv[(c1-SPACE)*2]||ev[(c1-SPACE)*2]) {
2927 /* wait for
\e$BByE@
\e(B or
\e$BH>ByE@
\e(B */
2928 z_prev1 = c1; z_prev2 = c2;
2931 (*o_zconv)(cv[(c1-SPACE)*2],cv[(c1-SPACE)*2+1]);
2936 /* JISX0208 Alphabet */
2937 if (alpha_f && c2 == 0x23 ) {
2939 } else if (alpha_f && c2 == 0x21 ) {
2940 /* JISX0208 Kigou */
2945 } else if (alpha_f&0x4) {
2950 } else if (0x20<c1 && c1<0x7f && fv[c1-0x20]) {
2956 case '>': entity = ">"; break;
2957 case '<': entity = "<"; break;
2958 case '\"': entity = """; break;
2959 case '&': entity = "&"; break;
2962 while (*entity) (*o_zconv)(0, *entity++);
2972 #define rot13(c) ( \
2974 (c <= 'M') ? (c + 13): \
2975 (c <= 'Z') ? (c - 13): \
2977 (c <= 'm') ? (c + 13): \
2978 (c <= 'z') ? (c - 13): \
2982 #define rot47(c) ( \
2984 ( c <= 'O' ) ? (c + 47) : \
2985 ( c <= '~' ) ? (c - 47) : \
2993 if (c2==0 || c2==X0201 || c2==ISO8859_1) {
2999 (*o_rot_conv)(c2,c1);
3006 if ((hira_f & 1) && c2==0x25 && 0x20<c1 && c1<0x74) {
3008 } else if ((hira_f & 2) && c2==0x24 && 0x20<c1 && c1<0x74) {
3011 (*o_hira_conv)(c2,c1);
3016 iso2022jp_check_conv(c2,c1)
3019 static int range[RANGE_NUM_MAX][2] = {
3042 if(c2 >= 0x00 && c2 <= 0x20 && c1 >= 0x7f && c1 <= 0xff) {
3046 if((c2 >= 0x29 && c2 <= 0x2f) || (c2 >= 0x75 && c2 <= 0x7e)) {
3051 for (i = 0; i < RANGE_NUM_MAX; i++) {
3052 start = range[i][0];
3055 if (c >= start && c <= end) {
3060 (*o_iso2022jp_check_conv)(c2,c1);
3064 /* This converts =?ISO-2022-JP?B?HOGE HOGE?= */
3066 unsigned char *mime_pattern[] = {
3067 (unsigned char *)"\075?EUC-JP?B?",
3068 (unsigned char *)"\075?SHIFT_JIS?B?",
3069 (unsigned char *)"\075?ISO-8859-1?Q?",
3070 (unsigned char *)"\075?ISO-8859-1?B?",
3071 (unsigned char *)"\075?ISO-2022-JP?B?",
3072 (unsigned char *)"\075?ISO-2022-JP?Q?",
3073 #if defined(UTF8_INPUT_ENABLE) || defined(UTF8_OUTPUT_ENABLE)
3074 (unsigned char *)"\075?UTF-8?B?",
3076 (unsigned char *)"\075?US-ASCII?Q?",
3081 /*
\e$B3:Ev$9$k%3!<%I$NM%@hEY$r>e$2$k$?$a$NL\0u
\e(B */
3082 int (*mime_priority_func[])PROTO((int c2, int c1, int c0)) = {
3083 e_iconv, s_iconv, 0, 0, 0, 0,
3084 #if defined(UTF8_INPUT_ENABLE) || defined(UTF8_OUTPUT_ENABLE)
3090 int mime_encode[] = {
3091 JAPANESE_EUC, SHIFT_JIS,ISO8859_1, ISO8859_1, X0208, X0201,
3092 #if defined(UTF8_INPUT_ENABLE) || defined(UTF8_OUTPUT_ENABLE)
3099 int mime_encode_method[] = {
3100 'B', 'B','Q', 'B', 'B', 'Q',
3101 #if defined(UTF8_INPUT_ENABLE) || defined(UTF8_OUTPUT_ENABLE)
3109 #define MAXRECOVER 20
3111 /* I don't trust portablity of toupper */
3112 #define nkf_toupper(c) (('a'<=c && c<='z')?(c-('a'-'A')):c)
3113 #define nkf_isdigit(c) ('0'<=c && c<='9')
3114 #define nkf_isxdigit(c) (nkf_isdigit(c) || ('a'<=c && c<='f') || ('A'<=c && c <= 'F'))
3119 if (i_getc!=mime_getc) {
3120 i_mgetc = i_getc; i_getc = mime_getc;
3121 i_mungetc = i_ungetc; i_ungetc = mime_ungetc;
3122 if(mime_f==STRICT_MIME) {
3123 i_mgetc_buf = i_mgetc; i_mgetc = mime_getc_buf;
3124 i_mungetc_buf = i_mungetc; i_mungetc = mime_ungetc_buf;
3130 unswitch_mime_getc()
3132 if(mime_f==STRICT_MIME) {
3133 i_mgetc = i_mgetc_buf;
3134 i_mungetc = i_mungetc_buf;
3137 i_ungetc = i_mungetc;
3141 mime_begin_strict(f)
3146 unsigned char *p,*q;
3147 int r[MAXRECOVER]; /* recovery buffer, max mime pattern lenght */
3149 mime_decode_mode = FALSE;
3150 /* =? has been checked */
3152 p = mime_pattern[j];
3155 for(i=2;p[i]>' ';i++) { /* start at =? */
3156 if ( ((r[i] = c1 = (*i_getc)(f))==EOF) || nkf_toupper(c1) != p[i] ) {
3157 /* pattern fails, try next one */
3159 while ((p = mime_pattern[++j])) {
3160 for(k=2;k<i;k++) /* assume length(p) > i */
3161 if (p[k]!=q[k]) break;
3162 if (k==i && nkf_toupper(c1)==p[k]) break;
3164 if (p) continue; /* found next one, continue */
3165 /* all fails, output from recovery buffer */
3173 mime_decode_mode = p[i-2];
3175 clr_code_score(find_inputcode_byfunc(mime_priority_func[j]), SCORE_iMIME);
3177 if (mime_decode_mode=='B') {
3178 mimebuf_f = unbuf_f;
3180 /* do MIME integrity check */
3181 return mime_integrity(f,mime_pattern[j]);
3193 /* we don't keep eof of Fifo, becase it contains ?= as
3194 a terminator. It was checked in mime_integrity. */
3195 return ((mimebuf_f)?
3196 (*i_mgetc_buf)(f):Fifo(mime_input++));
3200 mime_ungetc_buf(c,f)
3205 (*i_mungetc_buf)(c,f);
3207 Fifo(--mime_input)=c;
3218 /* In NONSTRICT mode, only =? is checked. In case of failure, we */
3219 /* re-read and convert again from mime_buffer. */
3221 /* =? has been checked */
3223 Fifo(mime_last++)='='; Fifo(mime_last++)='?';
3224 for(i=2;i<MAXRECOVER;i++) { /* start at =? */
3225 /* We accept any character type even if it is breaked by new lines */
3226 c1 = (*i_getc)(f); Fifo(mime_last++)= c1 ;
3227 if (c1=='\n'||c1==' '||c1=='\r'||
3228 c1=='-'||c1=='_'||is_alnum(c1) ) continue;
3230 /* Failed. But this could be another MIME preemble */
3238 c1 = (*i_getc)(f); Fifo(mime_last++) = c1;
3239 if (!(++i<MAXRECOVER) || c1==EOF) break;
3240 if (c1=='b'||c1=='B') {
3241 mime_decode_mode = 'B';
3242 } else if (c1=='q'||c1=='Q') {
3243 mime_decode_mode = 'Q';
3247 c1 = (*i_getc)(f); Fifo(mime_last++) = c1;
3248 if (!(++i<MAXRECOVER) || c1==EOF) break;
3250 mime_decode_mode = FALSE;
3256 if (!mime_decode_mode) {
3257 /* false MIME premble, restart from mime_buffer */
3258 mime_decode_mode = 1; /* no decode, but read from the mime_buffer */
3259 /* Since we are in MIME mode until buffer becomes empty, */
3260 /* we never go into mime_begin again for a while. */
3263 /* discard mime preemble, and goto MIME mode */
3265 /* do no MIME integrity check */
3266 return c1; /* used only for checking EOF */
3281 fprintf(stderr, "%s\n", str);
3290 if (nkf_isdigit(x)) return x - '0';
3291 return nkf_toupper(x) - 'A' + 10;
3296 #ifdef ANSI_C_PROTOTYPE
3297 int hex_getc(int ch, FILE *f, int (*g)(FILE *f), int (*u)(int c, FILE *f))
3300 hex_getc(ch, f, g, u)
3313 if (!nkf_isxdigit(c2)){
3318 if (!nkf_isxdigit(c3)){
3323 return (hex2bin(c2) << 4) | hex2bin(c3);
3330 return hex_getc(':', f, i_cgetc, i_cungetc);
3338 return (*i_cungetc)(c, f);
3345 return hex_getc('%', f, i_ugetc, i_uungetc);
3353 return (*i_uungetc)(c, f);
3357 #ifdef NUMCHAR_OPTION
3362 int (*g)() = i_ngetc;
3363 int (*u)() = i_nungetc;
3374 if (buf[i] == 'x' || buf[i] == 'X'){
3375 for (j = 0; j < 5; j++){
3377 if (!nkf_isxdigit(buf[i])){
3384 c |= hex2bin(buf[i]);
3387 for (j = 0; j < 6; j++){
3391 if (!nkf_isdigit(buf[i])){
3398 c += hex2bin(buf[i]);
3404 return CLASS_UTF16 | c;
3414 numchar_ungetc(c, f)
3418 return (*i_nungetc)(c, f);
3427 int c1, c2, c3, c4, cc;
3428 int t1, t2, t3, t4, mode, exit_mode;
3430 if (mime_top != mime_last) { /* Something is in FIFO */
3431 return Fifo(mime_top++);
3433 if (mime_decode_mode==1 ||mime_decode_mode==FALSE) {
3434 mime_decode_mode=FALSE;
3435 unswitch_mime_getc();
3436 return (*i_getc)(f);
3439 if (mimebuf_f == FIXED_MIME)
3440 exit_mode = mime_decode_mode;
3443 if (mime_decode_mode == 'Q') {
3444 if ((c1 = (*i_mgetc)(f)) == EOF) return (EOF);
3446 if (c1=='_') return ' ';
3447 if (c1!='=' && c1!='?') {
3451 mime_decode_mode = exit_mode; /* prepare for quit */
3452 if (c1<=' ') return c1;
3453 if ((c2 = (*i_mgetc)(f)) == EOF) return (EOF);
3454 if (c1=='?'&&c2=='=' && mimebuf_f != FIXED_MIME) {
3455 /* end Q encoding */
3456 input_mode = exit_mode;
3457 while((c1=(*i_getc)(f))!=EOF && c1==SPACE
3458 /* && (c1==NL||c1==TAB||c1=='\r') */ ) ;
3461 if (c1=='='&&c2<' ') { /* this is soft wrap */
3462 while((c1 = (*i_mgetc)(f)) <=' ') {
3463 if ((c1 = (*i_mgetc)(f)) == EOF) return (EOF);
3465 mime_decode_mode = 'Q'; /* still in MIME */
3466 goto restart_mime_q;
3469 mime_decode_mode = 'Q'; /* still in MIME */
3473 if ((c3 = (*i_mgetc)(f)) == EOF) return (EOF);
3474 if (c2<=' ') return c2;
3475 mime_decode_mode = 'Q'; /* still in MIME */
3476 #define hex(c) (('0'<=c&&c<='9')?(c-'0'):\
3477 ('A'<=c&&c<='F')?(c-'A'+10):('a'<=c&&c<='f')?(c-'a'+10):0)
3478 return ((hex(c2)<<4) + hex(c3));
3481 if (mime_decode_mode != 'B') {
3482 mime_decode_mode = FALSE;
3483 return (*i_mgetc)(f);
3487 /* Base64 encoding */
3489 MIME allows line break in the middle of
3490 Base64, but we are very pessimistic in decoding
3491 in unbuf mode because MIME encoded code may broken by
3492 less or editor's control sequence (such as ESC-[-K in unbuffered
3493 mode. ignore incomplete MIME.
3495 mode = mime_decode_mode;
3496 mime_decode_mode = exit_mode; /* prepare for quit */
3498 while ((c1 = (*i_mgetc)(f))<=' ') {
3503 if ((c2 = (*i_mgetc)(f))<=' ') {
3506 if (mime_f != STRICT_MIME) goto mime_c2_retry;
3507 if (mimebuf_f!=FIXED_MIME) input_mode = ASCII;
3510 if ((c1 == '?') && (c2 == '=')) {
3512 while((c1=(*i_getc)(f))!=EOF && c1==SPACE
3513 /* && (c1==NL||c1==TAB||c1=='\r') */ ) ;
3517 if ((c3 = (*i_mgetc)(f))<=' ') {
3520 if (mime_f != STRICT_MIME) goto mime_c3_retry;
3521 if (mimebuf_f!=FIXED_MIME) input_mode = ASCII;
3525 if ((c4 = (*i_mgetc)(f))<=' ') {
3528 if (mime_f != STRICT_MIME) goto mime_c4_retry;
3529 if (mimebuf_f!=FIXED_MIME) input_mode = ASCII;
3533 mime_decode_mode = mode; /* still in MIME sigh... */
3535 /* BASE 64 decoding */
3537 t1 = 0x3f & base64decode(c1);
3538 t2 = 0x3f & base64decode(c2);
3539 t3 = 0x3f & base64decode(c3);
3540 t4 = 0x3f & base64decode(c4);
3541 cc = ((t1 << 2) & 0x0fc) | ((t2 >> 4) & 0x03);
3543 Fifo(mime_last++) = cc;
3544 cc = ((t2 << 4) & 0x0f0) | ((t3 >> 2) & 0x0f);
3546 Fifo(mime_last++) = cc;
3547 cc = ((t3 << 6) & 0x0c0) | (t4 & 0x3f);
3549 Fifo(mime_last++) = cc;
3554 return Fifo(mime_top++);
3562 Fifo(--mime_top) = c;
3573 /* In buffered mode, read until =? or NL or buffer full
3575 mime_input = mime_top;
3576 mime_last = mime_top;
3577 while(*p) Fifo(mime_input++) = *p++;
3580 while((c=(*i_getc)(f))!=EOF) {
3581 if (((mime_input-mime_top)&MIME_BUF_MASK)==0) {
3582 break; /* buffer full */
3584 if (c=='=' && d=='?') {
3585 /* checked. skip header, start decode */
3586 Fifo(mime_input++) = c;
3587 /* mime_last_input = mime_input; */
3592 if (!( (c=='+'||c=='/'|| c=='=' || c=='?' || is_alnum(c))))
3594 /* Should we check length mod 4? */
3595 Fifo(mime_input++) = c;
3598 /* In case of Incomplete MIME, no MIME decode */
3599 Fifo(mime_input++) = c;
3600 mime_last = mime_input; /* point undecoded buffer */
3601 mime_decode_mode = 1; /* no decode on Fifo last in mime_getc */
3602 switch_mime_getc(); /* anyway we need buffered getc */
3613 i = c - 'A'; /* A..Z 0-25 */
3615 i = c - 'G' /* - 'a' + 26 */ ; /* a..z 26-51 */
3617 } else if (c > '/') {
3618 i = c - '0' + '4' /* - '0' + 52 */ ; /* 0..9 52-61 */
3619 } else if (c == '+') {
3620 i = '>' /* 62 */ ; /* + 62 */
3622 i = '?' /* 63 */ ; /* / 63 */
3627 static char basis_64[] =
3628 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
3638 p = mime_pattern[0];
3639 for(i=0;mime_encode[i];i++) {
3640 if (mode == mime_encode[i]) {
3641 p = mime_pattern[i];
3645 mimeout_mode = mime_encode_method[i];
3647 /* (*o_mputc)(' '); */
3664 #define itoh4(c) (c>=10?c+'A'-10:c+'0')
3670 if (mimeout_f==FIXED_MIME) {
3671 if (base64_count>71) {
3679 if ( c<=DEL &&(output_mode==ASCII ||output_mode == ISO8859_1 )
3680 && mimeout_f!=FIXED_MIME) {
3681 if (mimeout_mode=='Q') {
3688 if (mimeout_mode!='B' || c!=SPACE) {
3697 } else if (!mimeout_mode && mimeout_f!=FIXED_MIME) {
3698 open_mime(output_mode);
3700 } else { /* c==EOF */
3701 switch(mimeout_mode) {
3706 (*o_mputc)(basis_64[((b64c & 0x3)<< 4)]);
3712 (*o_mputc)(basis_64[((b64c & 0xF) << 2)]);
3718 if (mimeout_f!=FIXED_MIME) {
3720 } else if (mimeout_mode != 'Q')
3725 switch(mimeout_mode) {
3729 (*o_mputc)(itoh4(((c>>4)&0xf)));
3730 (*o_mputc)(itoh4((c&0xf)));
3737 (*o_mputc)(basis_64[c>>2]);
3742 (*o_mputc)(basis_64[((b64c & 0x3)<< 4) | ((c & 0xF0) >> 4)]);
3748 (*o_mputc)(basis_64[((b64c & 0xF) << 2) | ((c & 0xC0) >>6)]);
3749 (*o_mputc)(basis_64[c & 0x3F]);
3769 mime_f = STRICT_MIME;
3773 #if defined(MSDOS) || defined(__OS2__)
3778 iso2022jp_f = FALSE;
3780 kanji_intro = DEFAULT_J;
3781 ascii_intro = DEFAULT_R;
3783 output_conv = DEFAULT_CONV;
3784 oconv = DEFAULT_CONV;
3787 i_mungetc = std_ungetc;
3788 i_mgetc_buf = std_getc;
3789 i_mungetc_buf = std_ungetc;
3792 i_ungetc=std_ungetc;
3795 i_bungetc= std_ungetc;
3799 o_crconv = no_connection;
3800 o_rot_conv = no_connection;
3801 o_iso2022jp_check_conv = no_connection;
3802 o_hira_conv = no_connection;
3803 o_fconv = no_connection;
3804 o_zconv = no_connection;
3807 i_ungetc = std_ungetc;
3809 i_mungetc = std_ungetc;
3811 output_mode = ASCII;
3814 mime_decode_mode = FALSE;
3823 struct input_code *p = input_code_list;
3828 #ifdef UTF8_OUTPUT_ENABLE
3829 if (w_oconv16_begin_f) {
3830 w_oconv16_begin_f = 2;
3835 fold_preserve_f = FALSE;
3838 fold_margin = FOLD_MARGIN;
3841 z_prev2=0,z_prev1=0;
3843 input_codename = "";
3848 no_connection(c2,c1)
3851 no_connection2(c2,c1,0);
3855 no_connection2(c2,c1,c0)
3858 fprintf(stderr,"nkf internal module connection failure.\n");
3866 fprintf(stderr,"USAGE: nkf(nkf32,wnkf,nkf2) -[flags] [in file] .. [out file for -O flag]\n");
3867 fprintf(stderr,"Flags:\n");
3868 fprintf(stderr,"b,u Output is bufferred (DEFAULT),Output is unbufferred\n");
3869 #ifdef DEFAULT_CODE_SJIS
3870 fprintf(stderr,"j,s,e,w Outout code is JIS 7 bit, Shift JIS (DEFAULT), AT&T JIS (EUC), UTF-8\n");
3872 #ifdef DEFAULT_CODE_JIS
3873 fprintf(stderr,"j,s,e,w Outout code is JIS 7 bit (DEFAULT), Shift JIS, AT&T JIS (EUC), UTF-8\n");
3875 #ifdef DEFAULT_CODE_EUC
3876 fprintf(stderr,"j,s,e,w Outout code is JIS 7 bit, Shift JIS, AT&T JIS (EUC) (DEFAULT), UTF-8\n");
3878 #ifdef DEFAULT_CODE_UTF8
3879 fprintf(stderr,"j,s,e,w Outout code is JIS 7 bit, Shift JIS, AT&T JIS (EUC), UTF-8 (DEFAULT)\n");
3881 fprintf(stderr,"J,S,E,W Input assumption is JIS 7 bit , Shift JIS, AT&T JIS (EUC), UTF-8\n");
3882 fprintf(stderr,"t no conversion\n");
3883 fprintf(stderr,"i_/o_ Output sequence to designate JIS-kanji/ASCII (DEFAULT B)\n");
3884 fprintf(stderr,"r {de/en}crypt ROT13/47\n");
3885 fprintf(stderr,"h 1 hirakana->katakana, 2 katakana->hirakana,3 both\n");
3886 fprintf(stderr,"v Show this usage. V: show version\n");
3887 fprintf(stderr,"m[BQN0] MIME decode [B:base64,Q:quoted,N:non-strict,0:no decode]\n");
3888 fprintf(stderr,"M[BQ] MIME encode [B:base64 Q:quoted]\n");
3889 fprintf(stderr,"l ISO8859-1 (Latin-1) support\n");
3890 fprintf(stderr,"f/F Folding: -f60 or -f or -f60-10 (fold margin 10) F preserve nl\n");
3891 fprintf(stderr,"Z[0-3] Convert X0208 alphabet to ASCII 1: Kankaku to space,2: 2 spaces,\n");
3892 fprintf(stderr," 3: Convert HTML Entity\n");
3893 fprintf(stderr,"X,x Assume X0201 kana in MS-Kanji, -x preserves X0201\n");
3894 fprintf(stderr,"B[0-2] Broken input 0: missing ESC,1: any X on ESC-[($]-X,2: ASCII on NL\n");
3896 fprintf(stderr,"T Text mode output\n");
3898 fprintf(stderr,"O Output to File (DEFAULT 'nkf.out')\n");
3899 fprintf(stderr,"d,c Delete \\r in line feed and \\032, Add \\r in line feed\n");
3900 fprintf(stderr,"I Convert non ISO-2022-JP charactor to GETA\n");
3901 fprintf(stderr,"-L[uwm] line mode u:LF w:CRLF m:CR (DEFAULT noconversion)\n");
3902 fprintf(stderr,"long name options\n");
3903 fprintf(stderr," --fj,--unix,--mac,--windows convert for the system\n");
3904 fprintf(stderr," --jis,--euc,--sjis,--utf8,--utf16,--mime,--base64 convert for the code\n");
3906 fprintf(stderr," --overwrite Overwrite original listed files by filtered result\n");
3908 fprintf(stderr," --help,--version\n");
3915 fprintf(stderr,"Network Kanji Filter Version %s (%s) "
3916 #if defined(MSDOS) && !defined(__WIN32__) && !defined(__WIN16__)
3919 #if defined(MSDOS) && defined(__WIN16__)
3922 #if defined(MSDOS) && defined(__WIN32__)
3928 ,Version,Patchlevel);
3929 fprintf(stderr,"\n%s\n",CopyRight);
3934 **
\e$B%Q%C%A@):n<T
\e(B
3935 ** void@merope.pleiades.or.jp (Kusakabe Youichi)
3936 ** NIDE Naoyuki <nide@ics.nara-wu.ac.jp>
3937 ** ohta@src.ricoh.co.jp (Junn Ohta)
3938 ** inouet@strl.nhk.or.jp (Tomoyuki Inoue)
3939 ** kiri@pulser.win.or.jp (Tetsuaki Kiriyama)
3940 ** Kimihiko Sato <sato@sail.t.u-tokyo.ac.jp>
3941 ** a_kuroe@kuroe.aoba.yokohama.jp (Akihiko Kuroe)
3942 ** kono@ie.u-ryukyu.ac.jp (Shinji Kono)
3943 ** GHG00637@nifty-serve.or.jp (COW)