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 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(LSI_C)) && !defined(MSDOS)
113 #if defined(MSDOS) || defined(__OS2__)
120 #define setbinmode(fp) fsetbin(fp)
121 #else /* Microsoft C, Turbo C */
122 #define setbinmode(fp) setmode(fileno(fp), O_BINARY)
124 #else /* UNIX,OS/2 */
125 #define setbinmode(fp)
128 #ifdef _IOFBF /* SysV and MSDOS */
129 #define setvbuffer(fp, buf, size) setvbuf(fp, buf, _IOFBF, size)
131 #define setvbuffer(fp, buf, size) setbuffer(fp, buf, size)
134 /*Borland C++ 4.5 EasyWin*/
135 #if defined(__TURBOC__) && defined(_Windows) && !defined(__WIN32__) /*Easy Win */
141 /* added by satoru@isoternet.org */
142 #include <sys/stat.h>
152 /* state of output_mode and input_mode
169 /* Input Assumption */
173 #define LATIN1_INPUT 6
175 #define STRICT_MIME 8
180 #define JAPANESE_EUC 10
184 #define UTF8_INPUT 13
185 #define UTF16_INPUT 14
186 #define UTF16BE_INPUT 15
204 #define is_alnum(c) \
205 (('a'<=c && c<='z')||('A'<= c && c<='Z')||('0'<=c && c<='9'))
207 #define HOLD_SIZE 1024
208 #define IOBUF_SIZE 16384
210 #define DEFAULT_J 'B'
211 #define DEFAULT_R 'B'
213 #define SJ0162 0x00e1 /* 01 - 62 ku offset */
214 #define SJ6394 0x0161 /* 63 - 94 ku offset */
216 #define RANGE_NUM_MAX 18
221 #if defined( UTF8_OUTPUT_ENABLE ) || defined( UTF8_INPUT_ENABLE )
222 #define sizeof_euc_utf8 94
223 #define sizeof_euc_to_utf8_1byte 94
224 #define sizeof_euc_to_utf8_2bytes 94
225 #define sizeof_utf8_to_euc_C2 64
226 #define sizeof_utf8_to_euc_E5B8 64
227 #define sizeof_utf8_to_euc_2bytes 112
228 #define sizeof_utf8_to_euc_3bytes 112
231 /* MIME preprocessor */
234 #ifdef EASYWIN /*Easy Win */
235 extern POINT _BufferSize;
238 /* function prototype */
240 #ifdef ANSI_C_PROTOTYPE
242 #define STATIC static
248 STATIC int noconvert PROTO((FILE *f));
249 STATIC int kanji_convert PROTO((FILE *f));
250 STATIC int h_conv PROTO((FILE *f,int c2,int c1));
251 STATIC int push_hold_buf PROTO((int c2));
252 STATIC void set_iconv PROTO((int f, int (*iconv_func)()));
253 STATIC int s_iconv PROTO((int c2,int c1,int c0));
254 STATIC int s2e_conv PROTO((int c2, int c1, int *p2, int *p1));
255 STATIC int e_iconv PROTO((int c2,int c1,int c0));
256 #ifdef UTF8_INPUT_ENABLE
257 STATIC int w2e_conv PROTO((int c2,int c1,int c0,int *p2,int *p1));
258 STATIC int w_iconv PROTO((int c2,int c1,int c0));
259 STATIC int w_iconv16 PROTO((int c2,int c1,int c0));
260 STATIC int w_iconv_common PROTO((int c1,int c0,unsigned short **pp,int psize,int *p2,int *p1));
262 #ifdef UTF8_OUTPUT_ENABLE
263 STATIC int e2w_conv PROTO((int c2,int c1));
264 STATIC void w_oconv PROTO((int c2,int c1));
265 STATIC void w_oconv16 PROTO((int c2,int c1));
267 STATIC void e_oconv PROTO((int c2,int c1));
268 STATIC void s_oconv PROTO((int c2,int c1));
269 STATIC void j_oconv PROTO((int c2,int c1));
270 STATIC void fold_conv PROTO((int c2,int c1));
271 STATIC void cr_conv PROTO((int c2,int c1));
272 STATIC void z_conv PROTO((int c2,int c1));
273 STATIC void rot_conv PROTO((int c2,int c1));
274 STATIC void hira_conv PROTO((int c2,int c1));
275 STATIC void base64_conv PROTO((int c2,int c1));
276 STATIC void iso2022jp_check_conv PROTO((int c2,int c1));
277 STATIC void no_connection PROTO((int c2,int c1));
278 STATIC int no_connection2 PROTO((int c2,int c1,int c0));
280 STATIC int code_score PROTO((int c2,int c1,int s));
281 STATIC void code_status PROTO((int c));
283 STATIC void std_putc PROTO((int c));
284 STATIC int std_getc PROTO((FILE *f));
285 STATIC int std_ungetc PROTO((int c,FILE *f));
287 STATIC int broken_getc PROTO((FILE *f));
288 STATIC int broken_ungetc PROTO((int c,FILE *f));
290 STATIC int mime_begin PROTO((FILE *f));
291 STATIC int mime_getc PROTO((FILE *f));
292 STATIC int mime_ungetc PROTO((int c,FILE *f));
294 STATIC int mime_begin_strict PROTO((FILE *f));
295 STATIC int mime_getc_buf PROTO((FILE *f));
296 STATIC int mime_ungetc_buf PROTO((int c,FILE *f));
297 STATIC int mime_integrity PROTO((FILE *f,unsigned char *p));
299 STATIC int base64decode PROTO((int c));
300 STATIC void mime_putc PROTO((int c));
301 STATIC void open_mime PROTO((int c));
302 STATIC void close_mime PROTO(());
303 STATIC void usage PROTO(());
304 STATIC void version PROTO(());
305 STATIC void options PROTO((unsigned char *c));
307 STATIC void reinit PROTO(());
312 static unsigned char stdibuf[IOBUF_SIZE];
313 static unsigned char stdobuf[IOBUF_SIZE];
314 static unsigned char hold_buf[HOLD_SIZE*2];
315 static int hold_count;
317 /* MIME preprocessor fifo */
319 #define MIME_BUF_SIZE (1024) /* 2^n ring buffer */
320 #define MIME_BUF_MASK (MIME_BUF_SIZE-1)
321 #define Fifo(n) mime_buf[(n)&MIME_BUF_MASK]
322 static unsigned char mime_buf[MIME_BUF_SIZE];
323 static unsigned int mime_top = 0;
324 static unsigned int mime_last = 0; /* decoded */
325 static unsigned int mime_input = 0; /* undecoded */
328 static int unbuf_f = FALSE;
329 static int estab_f = FALSE;
330 static int nop_f = FALSE;
331 static int binmode_f = TRUE; /* binary mode */
332 static int rot_f = FALSE; /* rot14/43 mode */
333 static int hira_f = FALSE; /* hira/kata henkan */
334 static int input_f = FALSE; /* non fixed input code */
335 static int alpha_f = FALSE; /* convert JIx0208 alphbet to ASCII */
336 static int mime_f = STRICT_MIME; /* convert MIME B base64 or Q */
337 static int mimebuf_f = FALSE; /* MIME buffered input */
338 static int broken_f = FALSE; /* convert ESC-less broken JIS */
339 static int iso8859_f = FALSE; /* ISO8859 through */
340 static int mimeout_f = FALSE; /* base64 mode */
341 #if defined(MSDOS) || defined(__OS2__)
342 static int x0201_f = TRUE; /* Assume JISX0201 kana */
344 static int x0201_f = NO_X0201; /* Assume NO JISX0201 */
346 static int iso2022jp_f = FALSE; /* convert ISO-2022-JP */
347 #ifdef UTF8_OUTPUT_ENABLE
348 static int w_oconv16_begin_f= 0; /* utf-16 header */
352 #ifdef CAP_URL_OPTION
353 static int cap_f = FALSE;
354 static int (*i_cgetc)PROTO((FILE *)) = std_getc; /* input of cgetc */
355 static int (*i_cungetc)PROTO((int c ,FILE *f)) = std_ungetc;
356 STATIC int cap_getc PROTO((FILE *f));
357 STATIC int cap_ungetc PROTO((int c,FILE *f));
359 static int url_f = FALSE;
360 static int (*i_ugetc)PROTO((FILE *)) = std_getc; /* input of ugetc */
361 static int (*i_uungetc)PROTO((int c ,FILE *f)) = std_ungetc;
362 STATIC int url_getc PROTO((FILE *f));
363 STATIC int url_ungetc PROTO((int c,FILE *f));
367 static int noout_f = FALSE;
368 STATIC void no_putc PROTO((int c));
369 static int debug_f = FALSE;
370 STATIC void debug PROTO((char *str));
373 static int e_stat = 0;
374 static int e_score = 0;
376 static int s_stat = 0;
377 static int s_score = 0;
379 #ifdef UTF8_INPUT_ENABLE
380 static int w_stat = 0;
381 static int w_score = 0;
383 static int utf16_mode = UTF16_INPUT;
385 static int w_stat = -1;
388 static int mimeout_mode = 0;
389 static int base64_count = 0;
391 /* X0208 -> ASCII converter */
394 static int f_line = 0; /* chars in line */
395 static int f_prev = 0;
396 static int fold_preserve_f = FALSE; /* preserve new lines */
397 static int fold_f = FALSE;
398 static int fold_len = 0;
401 static unsigned char kanji_intro = DEFAULT_J,
402 ascii_intro = DEFAULT_R;
406 #define FOLD_MARGIN 10
407 #define DEFAULT_FOLD 60
409 static int fold_margin = FOLD_MARGIN;
413 #ifdef DEFAULT_CODE_JIS
414 # define DEFAULT_CONV j_oconv
416 #ifdef DEFAULT_CODE_SJIS
417 # define DEFAULT_CONV s_oconv
419 #ifdef DEFAULT_CODE_EUC
420 # define DEFAULT_CONV e_oconv
422 #ifdef DEFAULT_CODE_UTF8
423 # define DEFAULT_CONV w_oconv
426 /* process default */
427 static void (*output_conv)PROTO((int c2,int c1)) = DEFAULT_CONV;
429 static void (*oconv)PROTO((int c2,int c1)) = no_connection;
430 /* s_iconv or oconv */
431 static int (*iconv)PROTO((int c2,int c1,int c0)) = no_connection2;
433 static void (*o_zconv)PROTO((int c2,int c1)) = no_connection;
434 static void (*o_fconv)PROTO((int c2,int c1)) = no_connection;
435 static void (*o_crconv)PROTO((int c2,int c1)) = no_connection;
436 static void (*o_rot_conv)PROTO((int c2,int c1)) = no_connection;
437 static void (*o_hira_conv)PROTO((int c2,int c1)) = no_connection;
438 static void (*o_base64conv)PROTO((int c2,int c1)) = no_connection;
439 static void (*o_iso2022jp_check_conv)PROTO((int c2,int c1)) = no_connection;
441 /* static redirections */
443 static void (*o_putc)PROTO((int c)) = std_putc;
445 static int (*i_getc)PROTO((FILE *f)) = std_getc; /* general input */
446 static int (*i_ungetc)PROTO((int c,FILE *f)) =std_ungetc;
448 static int (*i_bgetc)PROTO((FILE *)) = std_getc; /* input of mgetc */
449 static int (*i_bungetc)PROTO((int c ,FILE *f)) = std_ungetc;
451 static void (*o_mputc)PROTO((int c)) = std_putc ; /* output of mputc */
453 static int (*i_mgetc)PROTO((FILE *)) = std_getc; /* input of mgetc */
454 static int (*i_mungetc)PROTO((int c ,FILE *f)) = std_ungetc;
456 /* for strict mime */
457 static int (*i_mgetc_buf)PROTO((FILE *)) = std_getc; /* input of mgetc_buf */
458 static int (*i_mungetc_buf)PROTO((int c,FILE *f)) = std_ungetc;
461 static int output_mode = ASCII, /* output kanji mode */
462 input_mode = ASCII, /* input kanji mode */
463 shift_mode = FALSE; /* TRUE shift out, or X0201 */
464 static int mime_decode_mode = FALSE; /* MIME mode B base64, Q hex */
466 /* X0201 / X0208 conversion tables */
468 /* X0201 kana conversion table */
471 unsigned char cv[]= {
472 0x21,0x21,0x21,0x23,0x21,0x56,0x21,0x57,
473 0x21,0x22,0x21,0x26,0x25,0x72,0x25,0x21,
474 0x25,0x23,0x25,0x25,0x25,0x27,0x25,0x29,
475 0x25,0x63,0x25,0x65,0x25,0x67,0x25,0x43,
476 0x21,0x3c,0x25,0x22,0x25,0x24,0x25,0x26,
477 0x25,0x28,0x25,0x2a,0x25,0x2b,0x25,0x2d,
478 0x25,0x2f,0x25,0x31,0x25,0x33,0x25,0x35,
479 0x25,0x37,0x25,0x39,0x25,0x3b,0x25,0x3d,
480 0x25,0x3f,0x25,0x41,0x25,0x44,0x25,0x46,
481 0x25,0x48,0x25,0x4a,0x25,0x4b,0x25,0x4c,
482 0x25,0x4d,0x25,0x4e,0x25,0x4f,0x25,0x52,
483 0x25,0x55,0x25,0x58,0x25,0x5b,0x25,0x5e,
484 0x25,0x5f,0x25,0x60,0x25,0x61,0x25,0x62,
485 0x25,0x64,0x25,0x66,0x25,0x68,0x25,0x69,
486 0x25,0x6a,0x25,0x6b,0x25,0x6c,0x25,0x6d,
487 0x25,0x6f,0x25,0x73,0x21,0x2b,0x21,0x2c,
491 /* X0201 kana conversion table for daguten */
494 unsigned char dv[]= {
495 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
496 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
497 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
498 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
499 0x00,0x00,0x00,0x00,0x00,0x00,0x25,0x74,
500 0x00,0x00,0x00,0x00,0x25,0x2c,0x25,0x2e,
501 0x25,0x30,0x25,0x32,0x25,0x34,0x25,0x36,
502 0x25,0x38,0x25,0x3a,0x25,0x3c,0x25,0x3e,
503 0x25,0x40,0x25,0x42,0x25,0x45,0x25,0x47,
504 0x25,0x49,0x00,0x00,0x00,0x00,0x00,0x00,
505 0x00,0x00,0x00,0x00,0x25,0x50,0x25,0x53,
506 0x25,0x56,0x25,0x59,0x25,0x5c,0x00,0x00,
507 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
508 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
509 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
510 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
513 /* X0201 kana conversion table for han-daguten */
516 unsigned char ev[]= {
517 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
518 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
519 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
520 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
521 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
522 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
523 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
524 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
525 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
526 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
527 0x00,0x00,0x00,0x00,0x25,0x51,0x25,0x54,
528 0x25,0x57,0x25,0x5a,0x25,0x5d,0x00,0x00,
529 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
530 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
531 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
532 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
536 /* X0208 kigou conversion table */
537 /* 0x8140 - 0x819e */
539 unsigned char fv[] = {
541 0x00,0x00,0x00,0x00,0x2c,0x2e,0x00,0x3a,
542 0x3b,0x3f,0x21,0x00,0x00,0x27,0x60,0x00,
543 0x5e,0x00,0x5f,0x00,0x00,0x00,0x00,0x00,
544 0x00,0x00,0x00,0x00,0x00,0x2d,0x00,0x2f,
545 0x5c,0x00,0x00,0x7c,0x00,0x00,0x60,0x27,
546 0x22,0x22,0x28,0x29,0x00,0x00,0x5b,0x5d,
547 0x7b,0x7d,0x3c,0x3e,0x00,0x00,0x00,0x00,
548 0x00,0x00,0x00,0x00,0x2b,0x2d,0x00,0x00,
549 0x00,0x3d,0x00,0x3c,0x3e,0x00,0x00,0x00,
550 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
551 0x24,0x00,0x00,0x25,0x23,0x26,0x2a,0x40,
552 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
558 static int file_out = FALSE;
560 static int overwrite = FALSE;
563 static int crmode_f = 0; /* CR, NL, CRLF */
564 #ifdef EASYWIN /*Easy Win */
565 static int end_check;
577 #ifdef EASYWIN /*Easy Win */
578 _BufferSize.y = 400;/*Set Scroll Buffer Size*/
581 for (argc--,argv++; (argc > 0) && **argv == '-'; argc--, argv++) {
582 cp = (unsigned char *)*argv;
585 if(x0201_f == WISH_TRUE)
586 x0201_f = ((!iso2022jp_f)? TRUE : NO_X0201);
588 if (binmode_f == TRUE)
590 if (freopen("","wb",stdout) == NULL)
597 setbuf(stdout, (char *) NULL);
599 setvbuffer(stdout, stdobuf, IOBUF_SIZE);
602 if (binmode_f == TRUE)
604 if (freopen("","rb",stdin) == NULL) return (-1);
608 setvbuffer(stdin, stdibuf, IOBUF_SIZE);
612 kanji_convert(stdin);
618 if ((fin = fopen((origfname = *argv++), "r")) == NULL) {
627 /* reopen file for stdout */
628 if (file_out == TRUE) {
631 outfname = malloc(strlen(origfname)
632 + strlen(".nkftmpXXXXXX")
638 strcpy(outfname, origfname);
642 for (i = strlen(outfname); i; --i){
643 if (outfname[i - 1] == '/'
644 || outfname[i - 1] == '\\'){
650 strcat(outfname, "ntXXXXXX");
652 fd = open(outfname, O_WRONLY | O_CREAT | O_TRUNC,
655 strcat(outfname, ".nkftmpXXXXXX");
656 fd = mkstemp(outfname);
659 || (fd_backup = dup(fileno(stdout))) < 0
660 || dup2(fd, fileno(stdout)) < 0
671 outfname = "nkf.out";
674 if(freopen(outfname, "w", stdout) == NULL) {
678 if (binmode_f == TRUE) {
680 if (freopen("","wb",stdout) == NULL)
687 if (binmode_f == TRUE)
689 if (freopen("","rb",fin) == NULL)
694 setvbuffer(fin, stdibuf, IOBUF_SIZE);
711 if (dup2(fd_backup, fileno(stdout)) < 0){
714 if (stat(origfname, &sb)) {
715 fprintf(stderr, "Can't stat %s\n", origfname);
717 /*
\e$B%Q!<%_%C%7%g%s$rI|85
\e(B */
718 if (chmod(outfname, sb.st_mode)) {
719 fprintf(stderr, "Can't set permission %s\n", outfname);
722 tb[0] = tb[1] = sb.st_mtime;
723 /*
\e$B%?%$%`%9%?%s%W$rI|85
\e(B */
724 if (utime(outfname, tb)) {
725 fprintf(stderr, "Can't set timestamp %s\n", outfname);
728 if (unlink(origfname)){
732 tb.actime = sb.st_atime;
733 tb.modtime = sb.st_mtime;
734 /*
\e$B%?%$%`%9%?%s%W$rI|85
\e(B */
735 if (utime(outfname, &tb)) {
736 fprintf(stderr, "Can't set timestamp %s\n", outfname);
739 if (rename(outfname, origfname)) {
741 fprintf(stderr, "Can't rename %s to %s\n",
742 outfname, origfname);
750 #ifdef EASYWIN /*Easy Win */
751 if (file_out == FALSE)
752 scanf("%d",&end_check);
755 #else /* for Other OS */
756 if (file_out == TRUE)
786 {"katakana-hiragana","h3"},
787 #ifdef UTF8_OUTPUT_ENABLE
791 #ifdef UTF8_INPUT_ENABLE
793 {"utf16-input", "W16"},
798 #ifdef CAP_URL_OPTION
808 static int option_mode;
823 case '-': /* literal options */
824 if (!*cp) { /* ignore the rest of arguments */
828 for (i=0;i<sizeof(long_option)/sizeof(long_option[0]);i++) {
830 p = (unsigned char *)long_option[i].name;
831 for (j=0;*p && *p++ == cp[j];j++);
832 if (! *p && !cp[j]) break;
835 cp = (unsigned char *)long_option[i].alias;
838 if (strcmp(long_option[i].name, "overwrite") == 0){
844 #ifdef CAP_URL_OPTION
845 if (strcmp(long_option[i].name, "cap-input") == 0){
849 if (strcmp(long_option[i].name, "url-input") == 0){
855 if (strcmp(long_option[i].name, "no-output") == 0){
859 if (strcmp(long_option[i].name, "debug") == 0){
866 case 'b': /* buffered mode */
869 case 'u': /* non bufferd mode */
872 case 't': /* transparent mode */
875 case 'j': /* JIS output */
877 output_conv = j_oconv;
879 case 'e': /* AT&T EUC output */
880 output_conv = e_oconv;
882 case 's': /* SJIS output */
883 output_conv = s_oconv;
885 case 'l': /* ISO8859 Latin-1 support, no conversion */
886 iso8859_f = TRUE; /* Only compatible with ISO-2022-JP */
887 input_f = LATIN1_INPUT;
889 case 'i': /* Kanji IN ESC-$-@/B */
890 if (*cp=='@'||*cp=='B')
893 case 'o': /* ASCII IN ESC-(-J/B */
894 if (*cp=='J'||*cp=='B'||*cp=='H')
902 if ('9'>= *cp && *cp>='0')
903 hira_f |= (*cp++ -'0');
910 #if defined(MSDOS) || defined(__OS2__)
925 #ifdef UTF8_OUTPUT_ENABLE
926 case 'w': /* UTF-8 output */
927 if ('1'== cp[0] && '6'==cp[1]) {
928 output_conv = w_oconv16; cp+=2;
930 w_oconv16_begin_f=2; cp++;
933 output_conv = w_oconv;
936 #ifdef UTF8_INPUT_ENABLE
937 case 'W': /* UTF-8 input */
938 if ('1'== cp[0] && '6'==cp[1]) {
939 input_f = UTF16_INPUT;
941 input_f = UTF8_INPUT;
944 /* Input code assumption */
945 case 'J': /* JIS input */
946 case 'E': /* AT&T EUC input */
949 case 'S': /* MS Kanji input */
950 input_f = SJIS_INPUT;
951 if (x0201_f==NO_X0201) x0201_f=TRUE;
953 case 'Z': /* Convert X0208 alphabet to asii */
954 /* bit:0 Convert X0208
955 bit:1 Convert Kankaku to one space
956 bit:2 Convert Kankaku to two spaces
957 bit:3 Convert HTML Entity
959 if ('9'>= *cp && *cp>='0')
960 alpha_f |= 1<<(*cp++ -'0');
964 case 'x': /* Convert X0201 kana to X0208 or X0201 Conversion */
965 x0201_f = FALSE; /* No X0201->X0208 conversion */
967 ESC-(-I in JIS, EUC, MS Kanji
968 SI/SO in JIS, EUC, MS Kanji
969 SSO in EUC, JIS, not in MS Kanji
972 ESC-(-I in JIS (0x20-0x5f)
973 SSO in EUC (0xa0-0xdf)
974 0xa0-0xd in MS Kanji (0xa0-0xdf)
977 case 'X': /* Assume X0201 kana */
978 /* Default value is NO_X0201 for EUC/MS-Kanji mix */
981 case 'F': /* prserve new lines */
982 fold_preserve_f = TRUE;
983 case 'f': /* folding -f60 or -f */
986 while('0'<= *cp && *cp <='9') { /* we don't use atoi here */
988 fold_len += *cp++ - '0';
990 if (!(0<fold_len && fold_len<BUFSIZ))
991 fold_len = DEFAULT_FOLD;
995 while('0'<= *cp && *cp <='9') { /* we don't use atoi here */
997 fold_margin += *cp++ - '0';
1001 case 'm': /* MIME support */
1002 if (*cp=='B'||*cp=='Q') {
1003 mime_decode_mode = *cp++;
1004 mimebuf_f = FIXED_MIME;
1005 } else if (*cp=='N') {
1006 mime_f = TRUE; cp++;
1007 } else if (*cp=='S') {
1008 mime_f = STRICT_MIME; cp++;
1009 } else if (*cp=='0') {
1013 case 'M': /* MIME output */
1016 mimeout_f = FIXED_MIME; cp++;
1017 } else if (*cp=='Q') {
1019 mimeout_f = FIXED_MIME; cp++;
1024 case 'B': /* Broken JIS support */
1026 bit:1 allow any x on ESC-(-x or ESC-$-x
1027 bit:2 reset to ascii on NL
1029 if ('9'>= *cp && *cp>='0')
1030 broken_f |= 1<<(*cp++ -'0');
1035 case 'O':/* for Output file */
1039 case 'c':/* add cr code */
1042 case 'd':/* delete cr code */
1045 case 'I': /* ISO-2022-JP output */
1048 case 'L': /* line mode */
1049 if (*cp=='u') { /* unix */
1051 } else if (*cp=='m') { /* mac */
1053 } else if (*cp=='w') { /* windows */
1055 } else if (*cp=='0') { /* no conversion */
1060 /* module muliple options in a string are allowed for Perl moudle */
1061 while(*cp && *cp!='-') cp++;
1065 /* bogus option but ignored */
1071 #ifdef ANSI_C_PROTOTYPE
1072 void set_iconv(int f, int (*iconv_func)(int c2,int c1,int c0))
1074 void set_iconv(f, iconv_func)
1076 int (*iconv_func)();
1080 static int (*iconv_for_check)() = 0;
1082 #ifdef INPUT_CODE_FIX
1090 #ifdef INPUT_CODE_FIX
1091 && (f == -TRUE || !input_f) /* -TRUE means "FORCE" */
1097 if (estab_f && iconv_for_check != iconv){
1098 #ifdef UTF8_INPUT_ENABLE
1099 if (iconv == w_iconv) debug("UTF-8\n");
1100 if (iconv == w_iconv16) debug("UTF-16\n");
1102 if (iconv == s_iconv) debug("Shift_JIS\n");
1103 if (iconv == e_iconv) debug("EUC-JP\n");
1104 iconv_for_check = iconv;
1109 #define SCORE_DEPEND (1) /*
\e$B5!<o0MB8J8;z
\e(B */
1110 #define SCORE_NO_EXIST (SCORE_DEPEND << 1) /*
\e$BB8:_$7$J$$J8;z
\e(B */
1111 #define SCORE_ERROR (SCORE_NO_EXIST << 1) /*
\e$B%(%i!<
\e(B */
1112 int score_table_A0[] = {
1115 0, SCORE_DEPEND, SCORE_DEPEND, SCORE_DEPEND,
1116 SCORE_DEPEND, SCORE_DEPEND, SCORE_DEPEND, SCORE_NO_EXIST,
1119 int score_table_F0[] = {
1121 0, SCORE_DEPEND, SCORE_NO_EXIST, SCORE_NO_EXIST,
1122 SCORE_DEPEND, SCORE_DEPEND, SCORE_DEPEND, SCORE_DEPEND,
1123 SCORE_DEPEND, SCORE_NO_EXIST, SCORE_NO_EXIST, SCORE_ERROR,
1126 int code_score(c2, c1, s)
1131 }else if ((c2 & 0xf0) == 0xa0){
1132 s |= score_table_A0[c2 & 0x0f];
1133 }else if ((c2 & 0xf0) == 0xf0){
1134 s |= score_table_F0[c2 & 0x0f];
1136 #ifdef UTF8_OUTPUT_ENABLE
1137 else if (!e2w_conv(c2, c1)){
1138 s |= SCORE_NO_EXIST;
1150 if (c <= DEL && estab_f){
1157 || (0xa1 <= c && c <= 0xef && iconv == s_iconv)){
1159 }else if ((0x81 <= c && c < 0xa0) || (0xe0 <= c && c <= 0xea)){
1164 s_score = code_score(-1, 0, s_score);
1165 if (iconv == s_iconv) set_iconv(FALSE, 0);
1169 if ((0x40 <= c && c <= 0x7e) || (0x80 <= c && c <= 0xfd)){
1172 s2e_conv(s_buf[1], s_buf[0], &s_buf[1], &s_buf[0]);
1173 s_score = code_score(s_buf[1], s_buf[0], s_score);
1176 s_score = code_score(-1, 0, s_score);
1177 if (iconv == s_iconv) set_iconv(FALSE, 0);
1183 if (c <= DEL && estab_f){
1191 }else if (SSO == c || (0xa1 <= c && c <= 0xfe)){
1196 e_score = code_score(-1, 0, e_score);
1197 if (iconv == e_iconv) set_iconv(FALSE, 0);
1201 if (0xa1 <= c && c <= 0xfe){
1204 e_score = code_score(e_buf[1], e_buf[0], e_score);
1207 if (iconv == e_iconv) set_iconv(FALSE, 0);
1208 e_score = code_score(-1, 0, e_score);
1212 #ifdef UTF8_INPUT_ENABLE
1215 if (c <= DEL && estab_f){
1223 }else if (0xc0 <= c && c <= 0xdf){
1227 }else if (0xe0 <= c && c <= 0xef){
1232 w_score = code_score(-1, 0, w_score);
1233 if (iconv == w_iconv) set_iconv(FALSE, 0);
1238 if (0x80 <= c && c <= 0xbf){
1243 w2e_conv(w_buf[2], w_buf[1], w_buf[0],
1244 &w_buf[1], &w_buf[0]);
1246 w2e_conv(w_buf[1], w_buf[0], 0,
1247 &w_buf[1], &w_buf[0]);
1249 w_score = code_score(w_buf[1], w_buf[0], w_score);
1253 w_score = code_score(-1, 0, w_score);
1254 if (iconv == w_iconv) set_iconv(FALSE, 0);
1259 if (s_stat < 0 && e_stat < 0 && w_stat == 0){
1260 set_iconv(TRUE, w_iconv);
1263 if (s_stat == 0 && e_stat < 0 && w_stat < 0){
1264 set_iconv(TRUE, s_iconv);
1266 if (s_stat < 0 && e_stat == 0 && w_stat < 0){
1267 set_iconv(TRUE, e_iconv);
1269 if (s_stat < 0 && e_stat < 0 && w_stat < 0){
1271 s_stat = e_stat = 0;
1272 #ifdef UTF8_INPUT_ENABLE
1307 while ((c = (*i_getc)(f)) != EOF)
1316 oconv = output_conv;
1319 /* replace continucation module, from output side */
1321 /* output redicrection */
1330 if (mimeout_f == TRUE) {
1331 o_base64conv = oconv; oconv = base64_conv;
1333 /* base64_count = 0; */
1337 o_crconv = oconv; oconv = cr_conv;
1340 o_rot_conv = oconv; oconv = rot_conv;
1343 o_iso2022jp_check_conv = oconv; oconv = iso2022jp_check_conv;
1346 o_hira_conv = oconv; oconv = hira_conv;
1349 o_fconv = oconv; oconv = fold_conv;
1352 if (alpha_f || x0201_f) {
1353 o_zconv = oconv; oconv = z_conv;
1357 /* input redicrection */
1358 #ifdef CAP_URL_OPTION
1360 i_cgetc = i_getc; i_getc = cap_getc;
1361 i_cungetc = i_ungetc; i_ungetc= cap_ungetc;
1364 i_ugetc = i_getc; i_getc = url_getc;
1365 i_uungetc = i_ungetc; i_ungetc= url_ungetc;
1368 if (mime_f && mimebuf_f==FIXED_MIME) {
1369 i_mgetc = i_getc; i_getc = mime_getc;
1370 i_mungetc = i_ungetc; i_ungetc = mime_ungetc;
1373 i_bgetc = i_getc; i_getc = broken_getc;
1374 i_bungetc = i_ungetc; i_ungetc = broken_ungetc;
1376 if (input_f == JIS_INPUT || input_f == LATIN1_INPUT) {
1377 set_iconv(-TRUE, e_iconv);
1378 } else if (input_f == SJIS_INPUT) {
1379 set_iconv(-TRUE, s_iconv);
1380 #ifdef UTF8_INPUT_ENABLE
1381 } else if (input_f == UTF8_INPUT) {
1382 set_iconv(-TRUE, w_iconv);
1383 } else if (input_f == UTF16_INPUT) {
1384 set_iconv(-TRUE, w_iconv16);
1387 set_iconv(FALSE, e_iconv);
1392 #ifdef UTF8_INPUT_ENABLE
1400 Conversion main loop. Code detection only.
1410 module_connection();
1415 output_mode = ASCII;
1418 #define NEXT continue /* no output, get next */
1419 #define SEND ; /* output c1 and c2, get next */
1420 #define LAST break /* end of loop, go closing */
1422 while ((c1 = (*i_getc)(f)) != EOF) {
1427 /* in case of 8th bit is on */
1429 /* in case of not established yet */
1430 /* It is still ambiguious */
1431 if (h_conv(f, c2, c1)==EOF)
1437 /* in case of already established */
1439 /* ignore bogus code */
1445 /* second byte, 7 bit code */
1446 /* it might be kanji shitfted */
1447 if ((c1 == DEL) || (c1 <= SPACE)) {
1448 /* ignore bogus first code */
1456 #ifdef UTF8_INPUT_ENABLE
1465 } else if (c1 > DEL) {
1467 if (!estab_f && !iso8859_f) {
1468 /* not established yet */
1471 } else { /* estab_f==TRUE */
1476 } else if (SSP<=c1 && c1<0xe0 && iconv == s_iconv) {
1477 /* SJIS X0201 Case... */
1478 if(iso2022jp_f && x0201_f==NO_X0201) {
1479 (*oconv)(GETA1, GETA2);
1486 } else if (c1==SSO && iconv != s_iconv) {
1487 /* EUC X0201 Case */
1488 c1 = (*i_getc)(f); /* skip SSO */
1490 if (SSP<=c1 && c1<0xe0) {
1491 if(iso2022jp_f && x0201_f==NO_X0201) {
1492 (*oconv)(GETA1, GETA2);
1499 } else { /* bogus code, skip SSO and one byte */
1503 /* already established */
1508 } else if ((c1 > SPACE) && (c1 != DEL)) {
1509 /* in case of Roman characters */
1511 /* output 1 shifted byte */
1515 } else if (SPACE<=c1 && c1<(0xe0&0x7f) ){
1516 /* output 1 shifted byte */
1517 if(iso2022jp_f && x0201_f==NO_X0201) {
1518 (*oconv)(GETA1, GETA2);
1525 /* look like bogus code */
1528 } else if (input_mode == X0208) {
1529 /* in case of Kanji shifted */
1532 } else if (c1 == '=' && mime_f && !mime_decode_mode ) {
1533 /* Check MIME code */
1534 if ((c1 = (*i_getc)(f)) == EOF) {
1537 } else if (c1 == '?') {
1538 /* =? is mime conversion start sequence */
1539 if(mime_f == STRICT_MIME) {
1540 /* check in real detail */
1541 if (mime_begin_strict(f) == EOF)
1545 } else if (mime_begin(f) == EOF)
1555 /* normal ASCII code */
1558 } else if (c1 == SI) {
1561 } else if (c1 == SO) {
1564 } else if (c1 == ESC ) {
1565 if ((c1 = (*i_getc)(f)) == EOF) {
1566 /* (*oconv)(0, ESC); don't send bogus code */
1568 } else if (c1 == '$') {
1569 if ((c1 = (*i_getc)(f)) == EOF) {
1571 (*oconv)(0, ESC); don't send bogus code
1572 (*oconv)(0, '$'); */
1574 } else if (c1 == '@'|| c1 == 'B') {
1575 /* This is kanji introduction */
1579 } else if (c1 == '(') {
1580 if ((c1 = (*i_getc)(f)) == EOF) {
1581 /* don't send bogus code
1587 } else if (c1 == '@'|| c1 == 'B') {
1588 /* This is kanji introduction */
1593 /* could be some special code */
1600 } else if (broken_f&0x2) {
1601 /* accept any ESC-(-x as broken code ... */
1611 } else if (c1 == '(') {
1612 if ((c1 = (*i_getc)(f)) == EOF) {
1613 /* don't send bogus code
1615 (*oconv)(0, '('); */
1619 /* This is X0201 kana introduction */
1620 input_mode = X0201; shift_mode = X0201;
1622 } else if (c1 == 'B' || c1 == 'J' || c1 == 'H') {
1623 /* This is X0208 kanji introduction */
1624 input_mode = ASCII; shift_mode = FALSE;
1626 } else if (broken_f&0x2) {
1627 input_mode = ASCII; shift_mode = FALSE;
1632 /* maintain various input_mode here */
1636 } else if ( c1 == 'N' || c1 == 'n' ){
1638 c1 = (*i_getc)(f); /* skip SS2 */
1639 if ( SPACE<=c1 && c1 < 0xe0 ) {
1648 } else if ((c1 == NL || c1 == CR) && broken_f&4) {
1649 input_mode = ASCII; set_iconv(FALSE, 0);
1655 if (input_mode == X0208)
1656 (*oconv)(c2, c1); /* this is JIS, not SJIS/EUC case */
1657 else if (input_mode)
1658 (*oconv)(input_mode, c1); /* other special case */
1659 else if ((*iconv)(c2, c1, 0) < 0){ /* can be EUC/SJIS */
1660 int c0 = (*i_getc)(f);
1663 (*iconv)(c2, c1, c0);
1669 /* goto next_word */
1673 (*iconv)(EOF, 0, 0);
1686 /** it must NOT be in the kanji shifte sequence */
1687 /** it must NOT be written in JIS7 */
1688 /** and it must be after 2 byte 8bit code */
1695 while ((c1 = (*i_getc)(f)) != EOF) {
1701 if (push_hold_buf(c1) == EOF || estab_f){
1707 if (e_score <= s_score
1708 #ifdef UTF8_INPUT_ENABLE
1709 && e_score <= w_score
1712 set_iconv(FALSE, e_iconv);
1714 else if (s_score <= e_score
1715 #ifdef UTF8_INPUT_ENABLE
1716 && s_score <= w_score
1719 set_iconv(FALSE, s_iconv);
1721 #ifdef UTF8_INPUT_ENABLE
1723 set_iconv(FALSE, w_iconv);
1730 ** 1) EOF is detected, or
1731 ** 2) Code is established, or
1732 ** 3) Buffer is FULL (but last word is pushed)
1734 ** in 1) and 3) cases, we continue to use
1735 ** Kanji codes by oconv and leave estab_f unchanged.
1739 while (wc < hold_count){
1740 c2 = hold_buf[wc++];
1744 }else if (iconv == s_iconv && 0xa1 <= c2 && c2 <= 0xdf){
1745 (*iconv)(X0201, c2, 0);
1748 if (wc < hold_count){
1749 c1 = hold_buf[wc++];
1752 if (c1 == EOF) break;
1755 if ((*iconv)(c2, c1, 0) < 0){
1757 if (wc < hold_count){
1758 c0 = hold_buf[wc++];
1761 if (c0 == EOF) break;
1764 (*iconv)(c2, c1, c0);
1778 if (hold_count >= HOLD_SIZE*2)
1780 hold_buf[hold_count++] = c2;
1781 return ((hold_count >= HOLD_SIZE*2) ? EOF : hold_count);
1784 int s2e_conv(c2, c1, p2, p1)
1788 c2 = c2 + c2 - ((c2 <= 0x9f) ? SJ0162 : SJ6394);
1790 c1 = c1 - ((c1 > DEL) ? SPACE : 0x1f);
1797 return (c2 << 8) | c1;
1807 } else if ((c2 == EOF) || (c2 == 0) || c2 < SPACE) {
1810 s2e_conv(c2, c1, &c2, &c1);
1823 } else if ((c2 == EOF) || (c2 == 0) || c2 < SPACE) {
1833 #ifdef UTF8_INPUT_ENABLE
1835 w2e_conv(c2, c1, c0, p2, p1)
1839 extern unsigned short * utf8_to_euc_2bytes[];
1840 extern unsigned short ** utf8_to_euc_3bytes[];
1842 if (0xc0 <= c2 && c2 <= 0xef) {
1843 unsigned short **pp;
1846 if (c0 == 0) return -1;
1847 pp = utf8_to_euc_3bytes[c2 - 0x80];
1848 return w_iconv_common(c1, c0, pp, sizeof_utf8_to_euc_C2, p2, p1);
1850 return w_iconv_common(c2, c1, utf8_to_euc_2bytes, sizeof_utf8_to_euc_2bytes, p2, p1);
1852 } else if (c2 == X0201) {
1865 int ret = w2e_conv(c2, c1, c0, &c2, &c1);
1873 w_iconv16(c2, c1, c0)
1876 extern unsigned short * utf8_to_euc_2bytes[];
1877 extern unsigned short ** utf8_to_euc_3bytes[];
1878 unsigned short **pp;
1883 if (c2==0376 && c1==0377){
1884 utf16_mode = UTF16_INPUT;
1886 } else if (c2==0377 && c1==0376){
1887 utf16_mode = UTF16BE_INPUT;
1890 if (utf16_mode == UTF16BE_INPUT) {
1892 tmp=c1; c1=c2; c2=tmp;
1894 if (c2==0 || c2==EOF) {
1898 val = ((c2<<8)&0xff00) + c1;
1900 c0 = (0x80 | (c1 & 0x3f));
1901 c1 = (0xc0 | (val >> 6));
1902 pp = utf8_to_euc_2bytes;
1903 psize = sizeof_utf8_to_euc_2bytes;
1905 c0 = (0x80 | (c1 & 0x3f));
1906 c2 = (0xe0 | (val >> 12));
1907 c1 = (0x80 | ((val >> 6) & 0x3f));
1908 if (c0 == 0) return -1;
1909 if (0<=c2-0x80 && c2-0x80 <sizeof_utf8_to_euc_3bytes){
1910 pp = utf8_to_euc_3bytes[c2 - 0x80];
1911 psize = sizeof_utf8_to_euc_C2;
1916 ret = w_iconv_common(c1, c0, pp, psize, &c2, &c1);
1917 if (ret) return ret;
1923 w_iconv_common(c1, c0, pp, psize, p2, p1)
1925 unsigned short **pp;
1933 if (pp == 0) return 1;
1936 if (c1 < 0 || psize <= c1) return 1;
1938 if (p == 0) return 1;
1941 if (c0 < 0 || sizeof_utf8_to_euc_E5B8 <= c0) return 1;
1943 if (val == 0) return 1;
1946 if (c2 == SO) c2 = X0201;
1955 #ifdef UTF8_OUTPUT_ENABLE
1960 extern unsigned short euc_to_utf8_1byte[];
1961 extern unsigned short * euc_to_utf8_2bytes[];
1965 p = euc_to_utf8_1byte;
1968 c2 = (c2&0x7f) - 0x21;
1969 if (0<=c2 && c2<sizeof_euc_to_utf8_2bytes)
1970 p = euc_to_utf8_2bytes[c2];
1975 c1 = (c1 & 0x7f) - 0x21;
1976 if (0<=c1 && c1<sizeof_euc_to_utf8_1byte)
1988 } else if (c2 == 0) {
1989 output_mode = ASCII;
1991 } else if (c2 == ISO8859_1) {
1992 output_mode = ISO8859_1;
1993 (*o_putc)(c1 | 0x080);
1995 unsigned short val = (unsigned short)e2w_conv(c2, c1);
1998 if (0 < val && val < 0x80){
2000 }else if (val < 0x800){
2001 (*o_putc)(0xc0 | (val >> 6));
2002 (*o_putc)(0x80 | (val & 0x3f));
2004 (*o_putc)(0xe0 | (val >> 12));
2005 (*o_putc)(0x80 | ((val >> 6) & 0x3f));
2006 (*o_putc)(0x80 | (val & 0x3f));
2017 if (w_oconv16_begin_f==2) {
2020 w_oconv16_begin_f=1;
2024 } else if (c2 == 0) {
2027 } else if (c2 == ISO8859_1) {
2029 (*o_putc)(c1 | 0x080);
2031 unsigned short val = (unsigned short)e2w_conv(c2, c1);
2032 (*o_putc)((val&0xff00)>>8);
2033 (*o_putc)(val&0xff);
2046 } else if (c2 == 0) {
2047 output_mode = ASCII;
2049 } else if (c2 == X0201) {
2050 output_mode = JAPANESE_EUC;
2051 (*o_putc)(SSO); (*o_putc)(c1|0x80);
2052 } else if (c2 == ISO8859_1) {
2053 output_mode = ISO8859_1;
2054 (*o_putc)(c1 | 0x080);
2056 if ((c1<0x20 || 0x7e<c1) ||
2057 (c2<0x20 || 0x7e<c2)) {
2058 set_iconv(FALSE, 0);
2059 return; /* too late to rescue this char */
2061 output_mode = JAPANESE_EUC;
2062 (*o_putc)(c2 | 0x080);
2063 (*o_putc)(c1 | 0x080);
2076 } else if (c2 == 0) {
2077 output_mode = ASCII;
2079 } else if (c2 == X0201) {
2080 output_mode = SHIFT_JIS;
2082 } else if (c2 == ISO8859_1) {
2083 output_mode = ISO8859_1;
2084 (*o_putc)(c1 | 0x080);
2086 if ((c1<0x20 || 0x7e<c1) ||
2087 (c2<0x20 || 0x7e<c2)) {
2088 set_iconv(FALSE, 0);
2089 return; /* too late to rescue this char */
2091 output_mode = SHIFT_JIS;
2092 (*o_putc)((((c2 - 1) >> 1) + ((c2 <= 0x5e) ? 0x71 : 0xb1)));
2093 (*o_putc)((c1 + ((c2 & 1) ? ((c1 < 0x60) ? 0x1f : 0x20) : 0x7e)));
2103 if (output_mode !=ASCII && output_mode!=ISO8859_1) {
2106 (*o_putc)(ascii_intro);
2107 output_mode = ASCII;
2109 } else if (c2==X0201) {
2110 if (output_mode!=X0201) {
2111 output_mode = X0201;
2117 } else if (c2==ISO8859_1) {
2118 /* iso8859 introduction, or 8th bit on */
2119 /* Can we convert in 7bit form using ESC-'-'-A ?
2121 output_mode = ISO8859_1;
2123 } else if (c2 == 0) {
2124 if (output_mode !=ASCII && output_mode!=ISO8859_1) {
2127 (*o_putc)(ascii_intro);
2128 output_mode = ASCII;
2132 if (output_mode != X0208) {
2133 output_mode = X0208;
2136 (*o_putc)(kanji_intro);
2138 if (c1<0x20 || 0x7e<c1)
2140 if (c2<0x20 || 0x7e<c2)
2152 if (base64_count>50 && !mimeout_mode && c2==0 && c1==SPACE) {
2154 } else if (base64_count>66 && mimeout_mode) {
2155 (*o_base64conv)(EOF,0);
2159 (*o_base64conv)(c2,c1);
2163 static int broken_buf[3];
2164 static int broken_counter = 0;
2165 static int broken_last = 0;
2172 if (broken_counter>0) {
2173 return broken_buf[--broken_counter];
2176 if (c=='$' && broken_last != ESC
2177 && (input_mode==ASCII || input_mode==X0201)) {
2180 if (c1=='@'|| c1=='B') {
2181 broken_buf[0]=c1; broken_buf[1]=c;
2188 } else if (c=='(' && broken_last != ESC
2189 && (input_mode==X0208 || input_mode==X0201)) { /* ) */
2192 if (c1=='J'|| c1=='B') {
2193 broken_buf[0]=c1; broken_buf[1]=c;
2211 if (broken_counter<2)
2212 broken_buf[broken_counter++]=c;
2216 static int prev_cr = 0;
2224 if (! (c2==0&&c1==NL) ) {
2230 } else if (c1=='\r') {
2232 } else if (c1=='\n') {
2233 if (crmode_f==CRLF) {
2234 (*o_crconv)(0,'\r');
2235 } else if (crmode_f==CR) {
2236 (*o_crconv)(0,'\r');
2240 } else if (c1!='\032' || crmode_f!=NL){
2246 Return value of fold_conv()
2248 \n add newline and output char
2249 \r add newline and output nothing
2252 1 (or else) normal output
2254 fold state in prev (previous character)
2256 >0x80 Japanese (X0208/X0201)
2261 This fold algorthm does not preserve heading space in a line.
2262 This is the main difference from fmt.
2265 #define char_size(c2,c1) (c2?2:1)
2275 fold_state=0; /* ignroe cr */
2276 } else if (c1== BS) {
2277 if (f_line>0) f_line--;
2279 } else if (c2==EOF && f_line != 0) { /* close open last line */
2281 } else if (c1=='\n') {
2283 if (fold_preserve_f) {
2286 } else if (f_prev == c1) { /* duplicate newline */
2289 fold_state = '\n'; /* output two newline */
2295 if (f_prev&0x80) { /* Japanese? */
2297 fold_state = 0; /* ignore given single newline */
2298 } else if (f_prev==' ') {
2302 if (++f_line<=fold_len)
2306 fold_state = '\r'; /* fold and output nothing */
2310 } else if (c1=='\f') {
2315 fold_state = '\n'; /* output newline and clear */
2316 } else if ( (c2==0 && c1==' ')||
2317 (c2==0 && c1=='\t')||
2318 (c2=='!'&& c1=='!')) {
2319 /* X0208 kankaku or ascii space */
2320 if (f_prev == ' ') {
2321 fold_state = 0; /* remove duplicate spaces */
2324 if (++f_line<=fold_len)
2325 fold_state = ' '; /* output ASCII space only */
2327 f_prev = ' '; f_line = 0;
2328 fold_state = '\r'; /* fold and output nothing */
2332 prev0 = f_prev; /* we still need this one... , but almost done */
2334 if (c2 || c2==X0201)
2335 f_prev |= 0x80; /* this is Japanese */
2336 f_line += char_size(c2,c1);
2337 if (f_line<=fold_len) { /* normal case */
2340 if (f_line>=fold_len+fold_margin) { /* too many kinsou suspension */
2341 f_line = char_size(c2,c1);
2342 fold_state = '\n'; /* We can't wait, do fold now */
2343 } else if (c2==X0201) {
2344 /* simple kinsoku rules return 1 means no folding */
2345 if (c1==(0xde&0x7f)) fold_state = 1; /*
\e$B!+
\e(B*/
2346 else if (c1==(0xdf&0x7f)) fold_state = 1; /*
\e$B!,
\e(B*/
2347 else if (c1==(0xa4&0x7f)) fold_state = 1; /*
\e$B!#
\e(B*/
2348 else if (c1==(0xa3&0x7f)) fold_state = 1; /*
\e$B!$
\e(B*/
2349 else if (c1==(0xa1&0x7f)) fold_state = 1; /*
\e$B!W
\e(B*/
2350 else if (c1==(0xb0&0x7f)) fold_state = 1; /* - */
2351 else if (SPACE<=c1 && c1<=(0xdf&0x7f)) { /* X0201 */
2353 fold_state = '\n';/* add one new f_line before this character */
2356 fold_state = '\n';/* add one new f_line before this character */
2359 /* kinsoku point in ASCII */
2360 if ( c1==')'|| /* { [ ( */
2371 /* just after special */
2372 } else if (!is_alnum(prev0)) {
2373 f_line = char_size(c2,c1);
2375 } else if ((prev0==' ') || /* ignored new f_line */
2376 (prev0=='\n')|| /* ignored new f_line */
2377 (prev0&0x80)) { /* X0208 - ASCII */
2378 f_line = char_size(c2,c1);
2379 fold_state = '\n';/* add one new f_line before this character */
2381 fold_state = 1; /* default no fold in ASCII */
2385 if (c1=='"') fold_state = 1; /*
\e$B!"
\e(B */
2386 else if (c1=='#') fold_state = 1; /*
\e$B!#
\e(B */
2387 else if (c1=='W') fold_state = 1; /*
\e$B!W
\e(B */
2388 else if (c1=='K') fold_state = 1; /*
\e$B!K
\e(B */
2389 else if (c1=='$') fold_state = 1; /*
\e$B!$
\e(B */
2390 else if (c1=='%') fold_state = 1; /*
\e$B!%
\e(B */
2391 else if (c1=='\'') fold_state = 1; /*
\e$B!\
\e(B */
2392 else if (c1=='(') fold_state = 1; /*
\e$B!(
\e(B */
2393 else if (c1==')') fold_state = 1; /*
\e$B!)
\e(B */
2394 else if (c1=='*') fold_state = 1; /*
\e$B!*
\e(B */
2395 else if (c1=='+') fold_state = 1; /*
\e$B!+
\e(B */
2396 else if (c1==',') fold_state = 1; /*
\e$B!,
\e(B */
2397 /* default no fold in kinsoku */
2400 f_line = char_size(c2,c1);
2401 /* add one new f_line before this character */
2404 f_line = char_size(c2,c1);
2406 /* add one new f_line before this character */
2411 /* terminator process */
2412 switch(fold_state) {
2431 int z_prev2=0,z_prev1=0;
2438 /* if (c2) c1 &= 0x7f; assertion */
2440 if (x0201_f && z_prev2==X0201) { /* X0201 */
2441 if (c1==(0xde&0x7f)) { /*
\e$BByE@
\e(B */
2443 (*o_zconv)(dv[(z_prev1-SPACE)*2],dv[(z_prev1-SPACE)*2+1]);
2445 } else if (c1==(0xdf&0x7f)&&ev[(z_prev1-SPACE)*2]) { /*
\e$BH>ByE@
\e(B */
2447 (*o_zconv)(ev[(z_prev1-SPACE)*2],ev[(z_prev1-SPACE)*2+1]);
2451 (*o_zconv)(cv[(z_prev1-SPACE)*2],cv[(z_prev1-SPACE)*2+1]);
2460 if (x0201_f && c2==X0201) {
2461 if (dv[(c1-SPACE)*2]||ev[(c1-SPACE)*2]) {
2462 /* wait for
\e$BByE@
\e(B or
\e$BH>ByE@
\e(B */
2463 z_prev1 = c1; z_prev2 = c2;
2466 (*o_zconv)(cv[(c1-SPACE)*2],cv[(c1-SPACE)*2+1]);
2471 /* JISX0208 Alphabet */
2472 if (alpha_f && c2 == 0x23 ) {
2474 } else if (alpha_f && c2 == 0x21 ) {
2475 /* JISX0208 Kigou */
2480 } else if (alpha_f&0x4) {
2485 } else if (0x20<c1 && c1<0x7f && fv[c1-0x20]) {
2491 case '>': entity = ">"; break;
2492 case '<': entity = "<"; break;
2493 case '\"': entity = """; break;
2494 case '&': entity = "&"; break;
2497 while (*entity) (*o_zconv)(0, *entity++);
2507 #define rot13(c) ( \
2509 (c <= 'M') ? (c + 13): \
2510 (c <= 'Z') ? (c - 13): \
2512 (c <= 'm') ? (c + 13): \
2513 (c <= 'z') ? (c - 13): \
2517 #define rot47(c) ( \
2519 ( c <= 'O' ) ? (c + 47) : \
2520 ( c <= '~' ) ? (c - 47) : \
2528 if (c2==0 || c2==X0201 || c2==ISO8859_1) {
2534 (*o_rot_conv)(c2,c1);
2541 if ((hira_f & 1) && c2==0x25 && 0x20<c1 && c1<0x74) {
2543 } else if ((hira_f & 2) && c2==0x24 && 0x20<c1 && c1<0x74) {
2546 (*o_hira_conv)(c2,c1);
2551 iso2022jp_check_conv(c2,c1)
2554 static int range[RANGE_NUM_MAX][2] = {
2577 if(c2 >= 0x00 && c2 <= 0x20 && c1 >= 0x7f && c1 <= 0xff) {
2581 if((c2 >= 0x29 && c2 <= 0x2f) || (c2 >= 0x75 && c2 <= 0x7e)) {
2586 for (i = 0; i < RANGE_NUM_MAX; i++) {
2587 start = range[i][0];
2590 if (c >= start && c <= end) {
2595 (*o_iso2022jp_check_conv)(c2,c1);
2599 /* This converts =?ISO-2022-JP?B?HOGE HOGE?= */
2601 unsigned char *mime_pattern[] = {
2602 (unsigned char *)"\075?EUC-JP?B?",
2603 (unsigned char *)"\075?SHIFT_JIS?B?",
2604 (unsigned char *)"\075?ISO-8859-1?Q?",
2605 (unsigned char *)"\075?ISO-2022-JP?B?",
2606 (unsigned char *)"\075?ISO-2022-JP?Q?",
2607 #if defined(UTF8_INPUT_ENABLE) || defined(UTF8_OUTPUT_ENABLE)
2608 (unsigned char *)"\075?UTF-8?B?",
2613 int mime_encode[] = {
2614 JAPANESE_EUC, SHIFT_JIS,ISO8859_1, X0208, X0201,
2615 #if defined(UTF8_INPUT_ENABLE) || defined(UTF8_OUTPUT_ENABLE)
2621 int mime_encode_method[] = {
2622 'B', 'B','Q', 'B', 'Q',
2623 #if defined(UTF8_INPUT_ENABLE) || defined(UTF8_OUTPUT_ENABLE)
2630 #define MAXRECOVER 20
2632 /* I don't trust portablity of toupper */
2633 #define nkf_toupper(c) (('a'<=c && c<='z')?(c-('a'-'A')):c)
2634 #define nkf_isdigit(c) ('0'<=c && c<='9')
2635 #define nkf_isxdigit(c) (nkf_isdigit(c) || ('a'<=c && c<='f') || ('A'<=c && c <= 'F'))
2640 if (i_getc!=mime_getc) {
2641 i_mgetc = i_getc; i_getc = mime_getc;
2642 i_mungetc = i_ungetc; i_ungetc = mime_ungetc;
2643 if(mime_f==STRICT_MIME) {
2644 i_mgetc_buf = i_mgetc; i_mgetc = mime_getc_buf;
2645 i_mungetc_buf = i_mungetc; i_mungetc = mime_ungetc_buf;
2651 unswitch_mime_getc()
2653 if(mime_f==STRICT_MIME) {
2654 i_mgetc = i_mgetc_buf;
2655 i_mungetc = i_mungetc_buf;
2658 i_ungetc = i_mungetc;
2662 mime_begin_strict(f)
2667 unsigned char *p,*q;
2668 int r[MAXRECOVER]; /* recovery buffer, max mime pattern lenght */
2670 mime_decode_mode = FALSE;
2671 /* =? has been checked */
2673 p = mime_pattern[j];
2676 for(i=2;p[i]>' ';i++) { /* start at =? */
2677 if ( ((r[i] = c1 = (*i_getc)(f))==EOF) || nkf_toupper(c1) != p[i] ) {
2678 /* pattern fails, try next one */
2680 while ((p = mime_pattern[++j])) {
2681 for(k=2;k<i;k++) /* assume length(p) > i */
2682 if (p[k]!=q[k]) break;
2683 if (k==i && nkf_toupper(c1)==p[k]) break;
2685 if (p) continue; /* found next one, continue */
2686 /* all fails, output from recovery buffer */
2694 mime_decode_mode = p[i-2];
2695 if (mime_decode_mode=='B') {
2696 mimebuf_f = unbuf_f;
2698 /* do MIME integrity check */
2699 return mime_integrity(f,mime_pattern[j]);
2711 /* we don't keep eof of Fifo, becase it contains ?= as
2712 a terminator. It was checked in mime_integrity. */
2713 return ((mimebuf_f)?
2714 (*i_mgetc_buf)(f):Fifo(mime_input++));
2718 mime_ungetc_buf(c,f)
2723 (*i_mungetc_buf)(c,f);
2725 Fifo(--mime_input)=c;
2736 /* In NONSTRICT mode, only =? is checked. In case of failure, we */
2737 /* re-read and convert again from mime_buffer. */
2739 /* =? has been checked */
2741 Fifo(mime_last++)='='; Fifo(mime_last++)='?';
2742 for(i=2;i<MAXRECOVER;i++) { /* start at =? */
2743 /* We accept any character type even if it is breaked by new lines */
2744 c1 = (*i_getc)(f); Fifo(mime_last++)= c1 ;
2745 if (c1=='\n'||c1==' '||c1=='\r'||
2746 c1=='-'||c1=='_'||is_alnum(c1) ) continue;
2748 /* Failed. But this could be another MIME preemble */
2756 c1 = (*i_getc)(f); Fifo(mime_last++) = c1;
2757 if (!(++i<MAXRECOVER) || c1==EOF) break;
2758 if (c1=='b'||c1=='B') {
2759 mime_decode_mode = 'B';
2760 } else if (c1=='q'||c1=='Q') {
2761 mime_decode_mode = 'Q';
2765 c1 = (*i_getc)(f); Fifo(mime_last++) = c1;
2766 if (!(++i<MAXRECOVER) || c1==EOF) break;
2768 mime_decode_mode = FALSE;
2774 if (!mime_decode_mode) {
2775 /* false MIME premble, restart from mime_buffer */
2776 mime_decode_mode = 1; /* no decode, but read from the mime_buffer */
2777 /* Since we are in MIME mode until buffer becomes empty, */
2778 /* we never go into mime_begin again for a while. */
2781 /* discard mime preemble, and goto MIME mode */
2783 /* do no MIME integrity check */
2784 return c1; /* used only for checking EOF */
2804 #ifdef CAP_URL_OPTION
2809 if (nkf_isdigit(x)) return x - '0';
2810 return nkf_toupper(x) - 'A' + 10;
2813 #ifdef ANSI_C_PROTOTYPE
2814 int hex_getc(int ch, FILE *f, int (*g)(FILE *f), int (*u)(int c, FILE *f))
2817 hex_getc(ch, f, g, u)
2830 if (!nkf_isxdigit(c2) == EOF){
2835 if (!nkf_isxdigit(c3) == EOF){
2840 return (hex2bin(c2) << 4) | hex2bin(c3);
2847 return hex_getc(':', f, i_cgetc, i_cungetc);
2855 return (*i_cungetc)(c, f);
2862 return hex_getc('%', f, i_ugetc, i_uungetc);
2870 return (*i_uungetc)(c, f);
2879 int c1, c2, c3, c4, cc;
2880 int t1, t2, t3, t4, mode, exit_mode;
2882 if (mime_top != mime_last) { /* Something is in FIFO */
2883 return Fifo(mime_top++);
2885 if (mime_decode_mode==1 ||mime_decode_mode==FALSE) {
2886 mime_decode_mode=FALSE;
2887 unswitch_mime_getc();
2888 return (*i_getc)(f);
2891 if (mimebuf_f == FIXED_MIME)
2892 exit_mode = mime_decode_mode;
2895 if (mime_decode_mode == 'Q') {
2896 if ((c1 = (*i_mgetc)(f)) == EOF) return (EOF);
2898 if (c1=='_') return ' ';
2899 if (c1!='=' && c1!='?') {
2903 mime_decode_mode = exit_mode; /* prepare for quit */
2904 if (c1<=' ') return c1;
2905 if ((c2 = (*i_mgetc)(f)) == EOF) return (EOF);
2906 if (c1=='?'&&c2=='=' && mimebuf_f != FIXED_MIME) {
2907 /* end Q encoding */
2908 input_mode = exit_mode;
2909 while((c1=(*i_getc)(f))!=EOF && c1==SPACE
2910 /* && (c1==NL||c1==TAB||c1=='\r') */ ) ;
2913 if (c1=='='&&c2<' ') { /* this is soft wrap */
2914 while((c1 = (*i_mgetc)(f)) <=' ') {
2915 if ((c1 = (*i_mgetc)(f)) == EOF) return (EOF);
2917 mime_decode_mode = 'Q'; /* still in MIME */
2918 goto restart_mime_q;
2921 mime_decode_mode = 'Q'; /* still in MIME */
2925 if ((c3 = (*i_mgetc)(f)) == EOF) return (EOF);
2926 if (c2<=' ') return c2;
2927 mime_decode_mode = 'Q'; /* still in MIME */
2928 #define hex(c) (('0'<=c&&c<='9')?(c-'0'):\
2929 ('A'<=c&&c<='F')?(c-'A'+10):('a'<=c&&c<='f')?(c-'a'+10):0)
2930 return ((hex(c2)<<4) + hex(c3));
2933 if (mime_decode_mode != 'B') {
2934 mime_decode_mode = FALSE;
2935 return (*i_mgetc)(f);
2939 /* Base64 encoding */
2941 MIME allows line break in the middle of
2942 Base64, but we are very pessimistic in decoding
2943 in unbuf mode because MIME encoded code may broken by
2944 less or editor's control sequence (such as ESC-[-K in unbuffered
2945 mode. ignore incomplete MIME.
2947 mode = mime_decode_mode;
2948 mime_decode_mode = exit_mode; /* prepare for quit */
2950 while ((c1 = (*i_mgetc)(f))<=' ') {
2955 if ((c2 = (*i_mgetc)(f))<=' ') {
2958 if (mime_f != STRICT_MIME) goto mime_c2_retry;
2959 if (mimebuf_f!=FIXED_MIME) input_mode = ASCII;
2962 if ((c1 == '?') && (c2 == '=')) {
2964 while((c1=(*i_getc)(f))!=EOF && c1==SPACE
2965 /* && (c1==NL||c1==TAB||c1=='\r') */ ) ;
2969 if ((c3 = (*i_mgetc)(f))<=' ') {
2972 if (mime_f != STRICT_MIME) goto mime_c3_retry;
2973 if (mimebuf_f!=FIXED_MIME) input_mode = ASCII;
2977 if ((c4 = (*i_mgetc)(f))<=' ') {
2980 if (mime_f != STRICT_MIME) goto mime_c4_retry;
2981 if (mimebuf_f!=FIXED_MIME) input_mode = ASCII;
2985 mime_decode_mode = mode; /* still in MIME sigh... */
2987 /* BASE 64 decoding */
2989 t1 = 0x3f & base64decode(c1);
2990 t2 = 0x3f & base64decode(c2);
2991 t3 = 0x3f & base64decode(c3);
2992 t4 = 0x3f & base64decode(c4);
2993 cc = ((t1 << 2) & 0x0fc) | ((t2 >> 4) & 0x03);
2995 Fifo(mime_last++) = cc;
2996 cc = ((t2 << 4) & 0x0f0) | ((t3 >> 2) & 0x0f);
2998 Fifo(mime_last++) = cc;
2999 cc = ((t3 << 6) & 0x0c0) | (t4 & 0x3f);
3001 Fifo(mime_last++) = cc;
3006 return Fifo(mime_top++);
3014 Fifo(--mime_top) = c;
3025 /* In buffered mode, read until =? or NL or buffer full
3027 mime_input = mime_top;
3028 mime_last = mime_top;
3029 while(*p) Fifo(mime_input++) = *p++;
3032 while((c=(*i_getc)(f))!=EOF) {
3033 if (((mime_input-mime_top)&MIME_BUF_MASK)==0) {
3034 break; /* buffer full */
3036 if (c=='=' && d=='?') {
3037 /* checked. skip header, start decode */
3038 Fifo(mime_input++) = c;
3039 /* mime_last_input = mime_input; */
3044 if (!( (c=='+'||c=='/'|| c=='=' || c=='?' || is_alnum(c))))
3046 /* Should we check length mod 4? */
3047 Fifo(mime_input++) = c;
3050 /* In case of Incomplete MIME, no MIME decode */
3051 Fifo(mime_input++) = c;
3052 mime_last = mime_input; /* point undecoded buffer */
3053 mime_decode_mode = 1; /* no decode on Fifo last in mime_getc */
3054 switch_mime_getc(); /* anyway we need buffered getc */
3065 i = c - 'A'; /* A..Z 0-25 */
3067 i = c - 'G' /* - 'a' + 26 */ ; /* a..z 26-51 */
3069 } else if (c > '/') {
3070 i = c - '0' + '4' /* - '0' + 52 */ ; /* 0..9 52-61 */
3071 } else if (c == '+') {
3072 i = '>' /* 62 */ ; /* + 62 */
3074 i = '?' /* 63 */ ; /* / 63 */
3079 static char basis_64[] =
3080 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
3090 p = mime_pattern[0];
3091 for(i=0;mime_encode[i];i++) {
3092 if (mode == mime_encode[i]) {
3093 p = mime_pattern[i];
3097 mimeout_mode = mime_encode_method[i];
3099 /* (*o_mputc)(' '); */
3116 #define itoh4(c) (c>=10?c+'A'-10:c+'0')
3122 if (mimeout_f==FIXED_MIME) {
3123 if (base64_count>71) {
3131 if ( c<=DEL &&(output_mode==ASCII ||output_mode == ISO8859_1 )
3132 && mimeout_f!=FIXED_MIME) {
3133 if (mimeout_mode=='Q') {
3140 if (mimeout_mode!='B' || c!=SPACE) {
3149 } else if (!mimeout_mode && mimeout_f!=FIXED_MIME) {
3150 open_mime(output_mode);
3152 } else { /* c==EOF */
3153 switch(mimeout_mode) {
3158 (*o_mputc)(basis_64[((b64c & 0x3)<< 4)]);
3164 (*o_mputc)(basis_64[((b64c & 0xF) << 2)]);
3170 if (mimeout_f!=FIXED_MIME) {
3172 } else if (mimeout_mode != 'Q')
3177 switch(mimeout_mode) {
3181 (*o_mputc)(itoh4(((c>>4)&0xf)));
3182 (*o_mputc)(itoh4((c&0xf)));
3189 (*o_mputc)(basis_64[c>>2]);
3194 (*o_mputc)(basis_64[((b64c & 0x3)<< 4) | ((c & 0xF0) >> 4)]);
3200 (*o_mputc)(basis_64[((b64c & 0xF) << 2) | ((c & 0xC0) >>6)]);
3201 (*o_mputc)(basis_64[c & 0x3F]);
3221 mime_f = STRICT_MIME;
3225 #if defined(MSDOS) || defined(__OS2__)
3230 iso2022jp_f = FALSE;
3232 kanji_intro = DEFAULT_J;
3233 ascii_intro = DEFAULT_R;
3235 output_conv = DEFAULT_CONV;
3236 oconv = DEFAULT_CONV;
3239 i_mungetc = std_ungetc;
3240 i_mgetc_buf = std_getc;
3241 i_mungetc_buf = std_ungetc;
3244 i_ungetc=std_ungetc;
3247 i_bungetc= std_ungetc;
3251 o_crconv = no_connection;
3252 o_rot_conv = no_connection;
3253 o_iso2022jp_check_conv = no_connection;
3254 o_hira_conv = no_connection;
3255 o_fconv = no_connection;
3256 o_zconv = no_connection;
3259 i_ungetc = std_ungetc;
3261 i_mungetc = std_ungetc;
3263 output_mode = ASCII;
3266 mime_decode_mode = FALSE;
3276 #ifdef UTF8_INPUT_ENABLE
3281 #ifdef UTF8_OUTPUT_ENABLE
3282 if (w_oconv16_begin_f) {
3283 w_oconv16_begin_f = 2;
3288 fold_preserve_f = FALSE;
3291 fold_margin = FOLD_MARGIN;
3294 z_prev2=0,z_prev1=0;
3300 no_connection(c2,c1)
3303 no_connection2(c2,c1,0);
3307 no_connection2(c2,c1,c0)
3310 fprintf(stderr,"nkf internal module connection failure.\n");
3318 fprintf(stderr,"USAGE: nkf(nkf32,wnkf,nkf2) -[flags] [in file] .. [out file for -O flag]\n");
3319 fprintf(stderr,"Flags:\n");
3320 fprintf(stderr,"b,u Output is bufferred (DEFAULT),Output is unbufferred\n");
3321 #ifdef DEFAULT_CODE_SJIS
3322 fprintf(stderr,"j,s,e,w Outout code is JIS 7 bit, Shift JIS (DEFAULT), AT&T JIS (EUC), UTF-8\n");
3324 #ifdef DEFAULT_CODE_JIS
3325 fprintf(stderr,"j,s,e,w Outout code is JIS 7 bit (DEFAULT), Shift JIS, AT&T JIS (EUC), UTF-8\n");
3327 #ifdef DEFAULT_CODE_EUC
3328 fprintf(stderr,"j,s,e,w Outout code is JIS 7 bit, Shift JIS, AT&T JIS (EUC) (DEFAULT), UTF-8\n");
3330 #ifdef DEFAULT_CODE_UTF8
3331 fprintf(stderr,"j,s,e,w Outout code is JIS 7 bit, Shift JIS, AT&T JIS (EUC), UTF-8 (DEFAULT)\n");
3333 fprintf(stderr,"J,S,E,W Input assumption is JIS 7 bit , Shift JIS, AT&T JIS (EUC), UTF-8\n");
3334 fprintf(stderr,"t no conversion\n");
3335 fprintf(stderr,"i_/o_ Output sequence to designate JIS-kanji/ASCII (DEFAULT B)\n");
3336 fprintf(stderr,"r {de/en}crypt ROT13/47\n");
3337 fprintf(stderr,"h 1 hirakana->katakana, 2 katakana->hirakana,3 both\n");
3338 fprintf(stderr,"v Show this usage. V: show version\n");
3339 fprintf(stderr,"m[BQN0] MIME decode [B:base64,Q:quoted,N:non-strict,0:no decode]\n");
3340 fprintf(stderr,"M[BQ] MIME encode [B:base64 Q:quoted]\n");
3341 fprintf(stderr,"l ISO8859-1 (Latin-1) support\n");
3342 fprintf(stderr,"f/F Folding: -f60 or -f or -f60-10 (fold margin 10) F preserve nl\n");
3343 fprintf(stderr,"Z[0-3] Convert X0208 alphabet to ASCII 1: Kankaku to space,2: 2 spaces,\n");
3344 fprintf(stderr," 3: Convert HTML Entity\n");
3345 fprintf(stderr,"X,x Assume X0201 kana in MS-Kanji, -x preserves X0201\n");
3346 fprintf(stderr,"B[0-2] Broken input 0: missing ESC,1: any X on ESC-[($]-X,2: ASCII on NL\n");
3348 fprintf(stderr,"T Text mode output\n");
3350 fprintf(stderr,"O Output to File (DEFAULT 'nkf.out')\n");
3351 fprintf(stderr,"d,c Delete \\r in line feed and \\032, Add \\r in line feed\n");
3352 fprintf(stderr,"I Convert non ISO-2022-JP charactor to GETA\n");
3353 fprintf(stderr,"-L[uwm] line mode u:LF w:CRLF m:CR (DEFAULT noconversion)\n");
3354 fprintf(stderr,"long name options\n");
3355 fprintf(stderr," --fj,--unix,--mac,--windows convert for the system\n");
3356 fprintf(stderr," --jis,--euc,--sjis,--utf8,--utf16,--mime,--base64 convert for the code\n");
3357 fprintf(stderr," --help,--version\n");
3364 fprintf(stderr,"Network Kanji Filter Version %s (%s) "
3365 #if defined(MSDOS) && !defined(_Windows)
3368 #if !defined(__WIN32__) && defined(_Windows)
3371 #if defined(__WIN32__) && defined(_Windows)
3377 ,Version,Patchlevel);
3378 fprintf(stderr,"\n%s\n",CopyRight);
3383 **
\e$B%Q%C%A@):n<T
\e(B
3384 ** void@merope.pleiades.or.jp (Kusakabe Youichi)
3385 ** NIDE Naoyuki <nide@ics.nara-wu.ac.jp>
3386 ** ohta@src.ricoh.co.jp (Junn Ohta)
3387 ** inouet@strl.nhk.or.jp (Tomoyuki Inoue)
3388 ** kiri@pulser.win.or.jp (Tetsuaki Kiriyama)
3389 ** Kimihiko Sato <sato@sail.t.u-tokyo.ac.jp>
3390 ** a_kuroe@kuroe.aoba.yokohama.jp (Akihiko Kuroe)
3391 ** kono@ie.u-ryukyu.ac.jp (Shinji Kono)
3392 ** GHG00637@nifty-serve.or.jp (COW)
3394 **
\e$B:G=*99?7F|
\e(B