OSDN Git Service

d3ed7818576d39f1e562545d493e6d4146226e3b
[nkf/nkf.git] / nkf.c
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
7 ** Copyright (C) 2002
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
12 **
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
19 **
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 ***********************************************************************/
28
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
34 **
35 **    \e$B$^$@%P%0$,$"$k2DG=@-$,9b$$$G$9!#\e(B
36 **    (\e$BFC$K<+F0H=JL!"%3!<%I:.:_!"%(%i!<=hM}7O\e(B)
37 **
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 ***********************************************************************/
42 /* $Id: nkf.c,v 1.115 2006/11/03 20:06:15 naruse Exp $ */
43 #define NKF_VERSION "2.0.8"
44 #define NKF_RELEASE_DATE "2006-11-04"
45 #include "config.h"
46 #include "utf8tbl.h"
47
48 #define COPY_RIGHT \
49     "Copyright (C) 1987, FUJITSU LTD. (I.Ichikawa),2000 S. Kono, COW\n" \
50     "Copyright (C) 2002-2006 Kono, Furukawa, Naruse, mastodon"
51
52
53 /*
54 **
55 **
56 **
57 ** USAGE:       nkf [flags] [file] 
58 **
59 ** Flags:
60 ** b    Output is buffered             (DEFAULT)
61 ** u    Output is unbuffered
62 **
63 ** t    no operation
64 **
65 ** j    Output code is JIS 7 bit        (DEFAULT SELECT) 
66 ** s    Output code is MS Kanji         (DEFAULT SELECT) 
67 ** e    Output code is AT&T JIS         (DEFAULT SELECT) 
68 ** w    Output code is AT&T JIS         (DEFAULT SELECT) 
69 ** l    Output code is JIS 7bit and ISO8859-1 Latin-1
70 **
71 ** m    MIME conversion for ISO-2022-JP
72 ** I    Convert non ISO-2022-JP charactor to GETA by Pekoe <pekoe@lair.net>
73 ** i_ Output sequence to designate JIS-kanji (DEFAULT_J)
74 ** o_ Output sequence to designate single-byte roman characters (DEFAULT_R)
75 ** M    MIME output conversion 
76 **
77 ** r  {de/en}crypt ROT13/47
78 **
79 ** v  display Version
80 **
81 ** T  Text mode output        (for MS-DOS)
82 **
83 ** x    Do not convert X0201 kana into X0208
84 ** Z    Convert X0208 alphabet to ASCII
85 **
86 ** f60  fold option
87 **
88 ** m    MIME decode
89 ** B    try to fix broken JIS, missing Escape
90 ** B[1-9]  broken level
91 **
92 ** O   Output to 'nkf.out' file or last file name
93 ** d   Delete \r in line feed 
94 ** c   Add \r in line feed 
95 ** -- other long option
96 ** -- ignore following option (don't use with -O )
97 **
98 **/
99
100 #if (defined(__TURBOC__) || defined(_MSC_VER) || defined(LSI_C) || defined(__MINGW32__) || defined(__EMX__) || defined(__MSDOS__) || defined(__WINDOWS__) || defined(__DOS__) || defined(__OS2__)) && !defined(MSDOS)
101 #define MSDOS
102 #if (defined(__Win32__) || defined(_WIN32)) && !defined(__WIN32__)
103 #define __WIN32__
104 #endif
105 #endif
106
107 #ifdef PERL_XS
108 #undef OVERWRITE
109 #endif
110
111 #ifndef PERL_XS
112 #include <stdio.h>
113 #endif
114
115 #include <stdlib.h>
116 #include <string.h>
117
118 #if defined(MSDOS) || defined(__OS2__)
119 #include <fcntl.h>
120 #include <io.h>
121 #if defined(_MSC_VER) || defined(__WATCOMC__)
122 #define mktemp _mktemp
123 #endif
124 #endif
125
126 #ifdef MSDOS
127 #ifdef LSI_C
128 #define setbinmode(fp) fsetbin(fp)
129 #elif defined(__DJGPP__)
130 #include <libc/dosio.h>
131 #define setbinmode(fp) djgpp_setbinmode(fp)
132 #else /* Microsoft C, Turbo C */
133 #define setbinmode(fp) setmode(fileno(fp), O_BINARY)
134 #endif
135 #else /* UNIX */
136 #define setbinmode(fp)
137 #endif
138
139 #if defined(__DJGPP__)
140 void  djgpp_setbinmode(FILE *fp)
141 {
142     /* we do not use libc's setmode(), which changes COOKED/RAW mode in device. */
143     int fd, m;
144     fd = fileno(fp);
145     m = (__file_handle_modes[fd] & (~O_TEXT)) | O_BINARY;
146     __file_handle_set(fd, m);
147 }
148 #endif
149
150 #ifdef _IOFBF /* SysV and MSDOS, Windows */
151 #define       setvbuffer(fp, buf, size)       setvbuf(fp, buf, _IOFBF, size)
152 #else /* BSD */
153 #define       setvbuffer(fp, buf, size)       setbuffer(fp, buf, size)
154 #endif
155
156 /*Borland C++ 4.5 EasyWin*/
157 #if defined(__TURBOC__) && defined(_Windows) && !defined(__WIN32__) /*Easy Win */
158 #define         EASYWIN
159 #ifndef __WIN16__
160 #define __WIN16__
161 #endif
162 #include <windows.h>
163 #endif
164
165 #ifdef OVERWRITE
166 /* added by satoru@isoternet.org */
167 #if defined(__EMX__)
168 #include <sys/types.h>
169 #endif
170 #include <sys/stat.h>
171 #if !defined(MSDOS) || defined(__DJGPP__) /* UNIX, djgpp */
172 #include <unistd.h>
173 #if defined(__WATCOMC__)
174 #include <sys/utime.h>
175 #else
176 #include <utime.h>
177 #endif
178 #else /* defined(MSDOS) */
179 #ifdef __WIN32__
180 #ifdef __BORLANDC__ /* BCC32 */
181 #include <utime.h>
182 #else /* !defined(__BORLANDC__) */
183 #include <sys/utime.h>
184 #endif /* (__BORLANDC__) */
185 #else /* !defined(__WIN32__) */
186 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(__WATCOMC__) || defined(__OS2__) || defined(__EMX__) || defined(__IBMC__) || defined(__IBMCPP__)  /* VC++, MinGW, Watcom, emx+gcc, IBM VAC++ */
187 #include <sys/utime.h>
188 #elif defined(__TURBOC__) /* BCC */
189 #include <utime.h>
190 #elif defined(LSI_C) /* LSI C */
191 #endif /* (__WIN32__) */
192 #endif
193 #endif
194 #endif
195
196 #define         FALSE   0
197 #define         TRUE    1
198
199 /* state of output_mode and input_mode  
200
201    c2           0 means ASCII
202                 X0201
203                 ISO8859_1
204                 X0208
205                 EOF      all termination
206    c1           32bit data
207
208  */
209
210 #define         ASCII           0
211 #define         X0208           1
212 #define         X0201           2
213 #define         ISO8859_1       8
214 #define         NO_X0201        3
215 #define         X0212      0x2844
216 #define         X0213_1    0x284F
217 #define         X0213_2    0x2850
218
219 /* Input Assumption */
220
221 #define         JIS_INPUT       4
222 #define         EUC_INPUT      16
223 #define         SJIS_INPUT      5
224 #define         LATIN1_INPUT    6
225 #define         FIXED_MIME      7
226 #define         STRICT_MIME     8
227
228 /* MIME ENCODE */
229
230 #define         ISO2022JP       9
231 #define         JAPANESE_EUC   10
232 #define         SHIFT_JIS      11
233
234 #define         UTF8           12
235 #define         UTF8_INPUT     13
236 #define         UTF16_INPUT    1015
237 #define         UTF32_INPUT    1017
238
239 /* byte order */
240
241 #define         ENDIAN_BIG      1234
242 #define         ENDIAN_LITTLE   4321
243 #define         ENDIAN_2143     2143
244 #define         ENDIAN_3412     3412
245
246 #define         WISH_TRUE      15
247
248 /* ASCII CODE */
249
250 #define         BS      0x08
251 #define         TAB     0x09
252 #define         NL      0x0a
253 #define         CR      0x0d
254 #define         ESC     0x1b
255 #define         SPACE   0x20
256 #define         AT      0x40
257 #define         SSP     0xa0
258 #define         DEL     0x7f
259 #define         SI      0x0f
260 #define         SO      0x0e
261 #define         SSO     0x8e
262 #define         SS3     0x8f
263
264 #define         is_alnum(c)  \
265             (('a'<=c && c<='z')||('A'<= c && c<='Z')||('0'<=c && c<='9'))
266
267 /* I don't trust portablity of toupper */
268 #define nkf_toupper(c)  (('a'<=c && c<='z')?(c-('a'-'A')):c)
269 #define nkf_isoctal(c)  ('0'<=c && c<='7')
270 #define nkf_isdigit(c)  ('0'<=c && c<='9')
271 #define nkf_isxdigit(c)  (nkf_isdigit(c) || ('a'<=c && c<='f') || ('A'<=c && c <= 'F'))
272 #define nkf_isblank(c) (c == SPACE || c == TAB)
273 #define nkf_isspace(c) (nkf_isblank(c) || c == CR || c == NL)
274 #define nkf_isalpha(c) (('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z'))
275 #define nkf_isalnum(c) (nkf_isdigit(c) || nkf_isalpha(c))
276 #define nkf_isprint(c) (' '<=c && c<='~')
277 #define nkf_isgraph(c) ('!'<=c && c<='~')
278 #define hex2bin(c) (('0'<=c&&c<='9') ? (c-'0') : \
279                     ('A'<=c&&c<='F') ? (c-'A'+10) : \
280                     ('a'<=c&&c<='f') ? (c-'a'+10) : 0 )
281 #define is_eucg3(c2) (((unsigned short)c2 >> 8) == SS3)
282
283 #define CP932_TABLE_BEGIN 0xFA
284 #define CP932_TABLE_END   0xFC
285 #define CP932INV_TABLE_BEGIN 0xED
286 #define CP932INV_TABLE_END   0xEE
287 #define is_ibmext_in_sjis(c2) (CP932_TABLE_BEGIN <= c2 && c2 <= CP932_TABLE_END)
288
289 #define         HOLD_SIZE       1024
290 #if defined(INT_IS_SHORT)
291 #define         IOBUF_SIZE      2048
292 #else
293 #define         IOBUF_SIZE      16384
294 #endif
295
296 #define         DEFAULT_J       'B'
297 #define         DEFAULT_R       'B'
298
299 #define         SJ0162  0x00e1          /* 01 - 62 ku offset */
300 #define         SJ6394  0x0161          /* 63 - 94 ku offset */
301
302 #define         RANGE_NUM_MAX   18
303 #define         GETA1   0x22
304 #define         GETA2   0x2e
305
306
307 #if defined(UTF8_OUTPUT_ENABLE) || defined(UTF8_INPUT_ENABLE)
308 #define sizeof_euc_to_utf8_1byte 94
309 #define sizeof_euc_to_utf8_2bytes 94
310 #define sizeof_utf8_to_euc_C2 64
311 #define sizeof_utf8_to_euc_E5B8 64
312 #define sizeof_utf8_to_euc_2bytes 112
313 #define sizeof_utf8_to_euc_3bytes 16
314 #endif
315
316 /* MIME preprocessor */
317
318 #ifdef EASYWIN /*Easy Win */
319 extern POINT _BufferSize;
320 #endif
321
322 struct input_code{
323     char *name;
324     nkf_char stat;
325     nkf_char score;
326     nkf_char index;
327     nkf_char buf[3];
328     void (*status_func)(struct input_code *, nkf_char);
329     nkf_char (*iconv_func)(nkf_char c2, nkf_char c1, nkf_char c0);
330     int _file_stat;
331 };
332
333 static char *input_codename = "";
334
335 #ifndef PERL_XS
336 static const char *CopyRight = COPY_RIGHT;
337 #endif
338 #if !defined(PERL_XS) && !defined(WIN32DLL)
339 static  nkf_char     noconvert(FILE *f);
340 #endif
341 static  void    module_connection(void);
342 static  nkf_char     kanji_convert(FILE *f);
343 static  nkf_char     h_conv(FILE *f,nkf_char c2,nkf_char c1);
344 static  nkf_char     push_hold_buf(nkf_char c2);
345 static  void    set_iconv(nkf_char f, nkf_char (*iconv_func)(nkf_char c2,nkf_char c1,nkf_char c0));
346 static  nkf_char     s_iconv(nkf_char c2,nkf_char c1,nkf_char c0);
347 static  nkf_char     s2e_conv(nkf_char c2, nkf_char c1, nkf_char *p2, nkf_char *p1);
348 static  nkf_char     e_iconv(nkf_char c2,nkf_char c1,nkf_char c0);
349 #if defined(UTF8_INPUT_ENABLE) || defined(UTF8_OUTPUT_ENABLE)
350 /* UCS Mapping
351  * 0: Shift_JIS, eucJP-ascii
352  * 1: eucJP-ms
353  * 2: CP932, CP51932
354  */
355 #define UCS_MAP_ASCII 0
356 #define UCS_MAP_MS    1
357 #define UCS_MAP_CP932 2
358 static int ms_ucs_map_f = UCS_MAP_ASCII;
359 #endif
360 #ifdef UTF8_INPUT_ENABLE
361 /* no NEC special, NEC-selected IBM extended and IBM extended characters */
362 static  int     no_cp932ext_f = FALSE;
363 /* ignore ZERO WIDTH NO-BREAK SPACE */
364 static  int     no_best_fit_chars_f = FALSE;
365 static  int     input_endian = ENDIAN_BIG;
366 static  nkf_char     unicode_subchar = '?'; /* the regular substitution character */
367 static  void    nkf_each_char_to_hex(void (*f)(nkf_char c2,nkf_char c1), nkf_char c);
368 static  void    encode_fallback_html(nkf_char c);
369 static  void    encode_fallback_xml(nkf_char c);
370 static  void    encode_fallback_java(nkf_char c);
371 static  void    encode_fallback_perl(nkf_char c);
372 static  void    encode_fallback_subchar(nkf_char c);
373 static  void    (*encode_fallback)(nkf_char c) = NULL;
374 static  nkf_char     w2e_conv(nkf_char c2,nkf_char c1,nkf_char c0,nkf_char *p2,nkf_char *p1);
375 static  nkf_char     w_iconv(nkf_char c2,nkf_char c1,nkf_char c0);
376 static  nkf_char     w_iconv16(nkf_char c2,nkf_char c1,nkf_char c0);
377 static  nkf_char     w_iconv32(nkf_char c2,nkf_char c1,nkf_char c0);
378 static  nkf_char        unicode_to_jis_common(nkf_char c2,nkf_char c1,nkf_char c0,nkf_char *p2,nkf_char *p1);
379 static  nkf_char        w_iconv_common(nkf_char c1,nkf_char c0,const unsigned short *const *pp,nkf_char psize,nkf_char *p2,nkf_char *p1);
380 static  void    w16w_conv(nkf_char val, nkf_char *p2, nkf_char *p1, nkf_char *p0);
381 static  nkf_char     ww16_conv(nkf_char c2, nkf_char c1, nkf_char c0);
382 static  nkf_char     w16e_conv(nkf_char val,nkf_char *p2,nkf_char *p1);
383 static  void    w_status(struct input_code *, nkf_char);
384 #endif
385 #ifdef UTF8_OUTPUT_ENABLE
386 static  int     output_bom_f = FALSE;
387 static  int     output_endian = ENDIAN_BIG;
388 static  nkf_char     e2w_conv(nkf_char c2,nkf_char c1);
389 static  void    w_oconv(nkf_char c2,nkf_char c1);
390 static  void    w_oconv16(nkf_char c2,nkf_char c1);
391 static  void    w_oconv32(nkf_char c2,nkf_char c1);
392 #endif
393 static  void    e_oconv(nkf_char c2,nkf_char c1);
394 static  nkf_char     e2s_conv(nkf_char c2, nkf_char c1, nkf_char *p2, nkf_char *p1);
395 static  void    s_oconv(nkf_char c2,nkf_char c1);
396 static  void    j_oconv(nkf_char c2,nkf_char c1);
397 static  void    fold_conv(nkf_char c2,nkf_char c1);
398 static  void    cr_conv(nkf_char c2,nkf_char c1);
399 static  void    z_conv(nkf_char c2,nkf_char c1);
400 static  void    rot_conv(nkf_char c2,nkf_char c1);
401 static  void    hira_conv(nkf_char c2,nkf_char c1);
402 static  void    base64_conv(nkf_char c2,nkf_char c1);
403 static  void    iso2022jp_check_conv(nkf_char c2,nkf_char c1);
404 static  void    no_connection(nkf_char c2,nkf_char c1);
405 static  nkf_char     no_connection2(nkf_char c2,nkf_char c1,nkf_char c0);
406
407 static  void    code_score(struct input_code *ptr);
408 static  void    code_status(nkf_char c);
409
410 static  void    std_putc(nkf_char c);
411 static  nkf_char     std_getc(FILE *f);
412 static  nkf_char     std_ungetc(nkf_char c,FILE *f);
413
414 static  nkf_char     broken_getc(FILE *f);
415 static  nkf_char     broken_ungetc(nkf_char c,FILE *f);
416
417 static  nkf_char     mime_begin(FILE *f);
418 static  nkf_char     mime_getc(FILE *f);
419 static  nkf_char     mime_ungetc(nkf_char c,FILE *f);
420
421 static  void    switch_mime_getc(void);
422 static  void    unswitch_mime_getc(void);
423 static  nkf_char     mime_begin_strict(FILE *f);
424 static  nkf_char     mime_getc_buf(FILE *f);
425 static  nkf_char     mime_ungetc_buf(nkf_char c,FILE *f);
426 static  nkf_char     mime_integrity(FILE *f,const unsigned char *p);
427
428 static  nkf_char     base64decode(nkf_char c);
429 static  void    mime_prechar(nkf_char c2, nkf_char c1);
430 static  void    mime_putc(nkf_char c);
431 static  void    open_mime(nkf_char c);
432 static  void    close_mime(void);
433 static  void    eof_mime(void);
434 static  void    mimeout_addchar(nkf_char c);
435 #ifndef PERL_XS
436 static  void    usage(void);
437 static  void    version(void);
438 #endif
439 static  void    options(unsigned char *c);
440 #if defined(PERL_XS) || defined(WIN32DLL)
441 static  void    reinit(void);
442 #endif
443
444 /* buffers */
445
446 #if !defined(PERL_XS) && !defined(WIN32DLL)
447 static unsigned char   stdibuf[IOBUF_SIZE];
448 static unsigned char   stdobuf[IOBUF_SIZE];
449 #endif
450 static unsigned char   hold_buf[HOLD_SIZE*2];
451 static int             hold_count = 0;
452
453 /* MIME preprocessor fifo */
454
455 #define MIME_BUF_SIZE   (1024)    /* 2^n ring buffer */
456 #define MIME_BUF_MASK   (MIME_BUF_SIZE-1)   
457 #define Fifo(n)         mime_buf[(n)&MIME_BUF_MASK]
458 static unsigned char           mime_buf[MIME_BUF_SIZE];
459 static unsigned int            mime_top = 0;
460 static unsigned int            mime_last = 0;  /* decoded */
461 static unsigned int            mime_input = 0; /* undecoded */
462 static nkf_char (*mime_iconv_back)(nkf_char c2,nkf_char c1,nkf_char c0) = NULL;
463
464 /* flags */
465 static int             unbuf_f = FALSE;
466 static int             estab_f = FALSE;
467 static int             nop_f = FALSE;
468 static int             binmode_f = TRUE;       /* binary mode */
469 static int             rot_f = FALSE;          /* rot14/43 mode */
470 static int             hira_f = FALSE;          /* hira/kata henkan */
471 static int             input_f = FALSE;        /* non fixed input code  */
472 static int             alpha_f = FALSE;        /* convert JIx0208 alphbet to ASCII */
473 static int             mime_f = STRICT_MIME;   /* convert MIME B base64 or Q */
474 static int             mime_decode_f = FALSE;  /* mime decode is explicitly on */
475 static int             mimebuf_f = FALSE;      /* MIME buffered input */
476 static int             broken_f = FALSE;       /* convert ESC-less broken JIS */
477 static int             iso8859_f = FALSE;      /* ISO8859 through */
478 static int             mimeout_f = FALSE;       /* base64 mode */
479 #if defined(MSDOS) || defined(__OS2__) 
480 static int             x0201_f = TRUE;         /* Assume JISX0201 kana */
481 #else
482 static int             x0201_f = NO_X0201;     /* Assume NO JISX0201 */
483 #endif
484 static int             iso2022jp_f = FALSE;    /* convert ISO-2022-JP */
485
486 #ifdef UNICODE_NORMALIZATION
487 static int nfc_f = FALSE;
488 static nkf_char (*i_nfc_getc)(FILE *) = std_getc; /* input of ugetc */
489 static nkf_char (*i_nfc_ungetc)(nkf_char c ,FILE *f) = std_ungetc;
490 static nkf_char nfc_getc(FILE *f);
491 static nkf_char nfc_ungetc(nkf_char c,FILE *f);
492 #endif
493
494 #ifdef INPUT_OPTION
495 static int cap_f = FALSE;
496 static nkf_char (*i_cgetc)(FILE *) = std_getc; /* input of cgetc */
497 static nkf_char (*i_cungetc)(nkf_char c ,FILE *f) = std_ungetc;
498 static nkf_char cap_getc(FILE *f);
499 static nkf_char cap_ungetc(nkf_char c,FILE *f);
500
501 static int url_f = FALSE;
502 static nkf_char (*i_ugetc)(FILE *) = std_getc; /* input of ugetc */
503 static nkf_char (*i_uungetc)(nkf_char c ,FILE *f) = std_ungetc;
504 static nkf_char url_getc(FILE *f);
505 static nkf_char url_ungetc(nkf_char c,FILE *f);
506 #endif
507
508 #if defined(INT_IS_SHORT)
509 #define NKF_INT32_C(n)   (n##L)
510 #else
511 #define NKF_INT32_C(n)   (n)
512 #endif
513 #define PREFIX_EUCG3    NKF_INT32_C(0x8F00)
514 #define CLASS_MASK      NKF_INT32_C(0xFF000000)
515 #define CLASS_UNICODE   NKF_INT32_C(0x01000000)
516 #define VALUE_MASK      NKF_INT32_C(0x00FFFFFF)
517 #define UNICODE_MAX     NKF_INT32_C(0x0010FFFF)
518 #define is_unicode_capsule(c) ((c & CLASS_MASK) == CLASS_UNICODE)
519 #define is_unicode_bmp(c) ((c & VALUE_MASK) <= NKF_INT32_C(0xFFFF))
520
521 #ifdef NUMCHAR_OPTION
522 static int numchar_f = FALSE;
523 static nkf_char (*i_ngetc)(FILE *) = std_getc; /* input of ugetc */
524 static nkf_char (*i_nungetc)(nkf_char c ,FILE *f) = std_ungetc;
525 static nkf_char numchar_getc(FILE *f);
526 static nkf_char numchar_ungetc(nkf_char c,FILE *f);
527 #endif
528
529 #ifdef CHECK_OPTION
530 static int noout_f = FALSE;
531 static void no_putc(nkf_char c);
532 static nkf_char debug_f = FALSE;
533 static void debug(const char *str);
534 static nkf_char (*iconv_for_check)(nkf_char c2,nkf_char c1,nkf_char c0) = 0;
535 #endif
536
537 static int guess_f = FALSE;
538 #if !defined PERL_XS
539 static  void    print_guessed_code(char *filename);
540 #endif
541 static  void    set_input_codename(char *codename);
542 static int is_inputcode_mixed = FALSE;
543 static int is_inputcode_set   = FALSE;
544
545 #ifdef EXEC_IO
546 static int exec_f = 0;
547 #endif
548
549 #ifdef SHIFTJIS_CP932
550 /* invert IBM extended characters to others */
551 static int cp51932_f = TRUE;
552
553 /* invert NEC-selected IBM extended characters to IBM extended characters */
554 static int cp932inv_f = TRUE;
555
556 /* static nkf_char cp932_conv(nkf_char c2, nkf_char c1); */
557 #endif /* SHIFTJIS_CP932 */
558
559 #ifdef X0212_ENABLE
560 static int x0212_f = FALSE;
561 static nkf_char x0212_shift(nkf_char c);
562 static nkf_char x0212_unshift(nkf_char c);
563 #endif
564 static int x0213_f = FALSE;
565
566 static unsigned char prefix_table[256];
567
568 static void set_code_score(struct input_code *ptr, nkf_char score);
569 static void clr_code_score(struct input_code *ptr, nkf_char score);
570 static void status_disable(struct input_code *ptr);
571 static void status_push_ch(struct input_code *ptr, nkf_char c);
572 static void status_clear(struct input_code *ptr);
573 static void status_reset(struct input_code *ptr);
574 static void status_reinit(struct input_code *ptr);
575 static void status_check(struct input_code *ptr, nkf_char c);
576 static void e_status(struct input_code *, nkf_char);
577 static void s_status(struct input_code *, nkf_char);
578
579 struct input_code input_code_list[] = {
580     {"EUC-JP",    0, 0, 0, {0, 0, 0}, e_status, e_iconv, 0},
581     {"Shift_JIS", 0, 0, 0, {0, 0, 0}, s_status, s_iconv, 0},
582 #ifdef UTF8_INPUT_ENABLE
583     {"UTF-8",     0, 0, 0, {0, 0, 0}, w_status, w_iconv, 0},
584     {"UTF-16",    0, 0, 0, {0, 0, 0},     NULL, w_iconv16, 0},
585     {"UTF-32",    0, 0, 0, {0, 0, 0},     NULL, w_iconv32, 0},
586 #endif
587     {0}
588 };
589
590 static int              mimeout_mode = 0;
591 static int              base64_count = 0;
592
593 /* X0208 -> ASCII converter */
594
595 /* fold parameter */
596 static int             f_line = 0;    /* chars in line */
597 static int             f_prev = 0;
598 static int             fold_preserve_f = FALSE; /* preserve new lines */
599 static int             fold_f  = FALSE;
600 static int             fold_len  = 0;
601
602 /* options */
603 static unsigned char   kanji_intro = DEFAULT_J;
604 static unsigned char   ascii_intro = DEFAULT_R;
605
606 /* Folding */
607
608 #define FOLD_MARGIN  10
609 #define DEFAULT_FOLD 60
610
611 static int             fold_margin  = FOLD_MARGIN;
612
613 /* converters */
614
615 #ifdef DEFAULT_CODE_JIS
616 #   define  DEFAULT_CONV j_oconv
617 #endif
618 #ifdef DEFAULT_CODE_SJIS
619 #   define  DEFAULT_CONV s_oconv
620 #endif
621 #ifdef DEFAULT_CODE_EUC
622 #   define  DEFAULT_CONV e_oconv
623 #endif
624 #ifdef DEFAULT_CODE_UTF8
625 #   define  DEFAULT_CONV w_oconv
626 #endif
627
628 /* process default */
629 static void (*output_conv)(nkf_char c2,nkf_char c1) = DEFAULT_CONV;
630
631 static void (*oconv)(nkf_char c2,nkf_char c1) = no_connection;
632 /* s_iconv or oconv */
633 static nkf_char (*iconv)(nkf_char c2,nkf_char c1,nkf_char c0) = no_connection2;
634
635 static void (*o_zconv)(nkf_char c2,nkf_char c1) = no_connection;
636 static void (*o_fconv)(nkf_char c2,nkf_char c1) = no_connection;
637 static void (*o_crconv)(nkf_char c2,nkf_char c1) = no_connection;
638 static void (*o_rot_conv)(nkf_char c2,nkf_char c1) = no_connection;
639 static void (*o_hira_conv)(nkf_char c2,nkf_char c1) = no_connection;
640 static void (*o_base64conv)(nkf_char c2,nkf_char c1) = no_connection;
641 static void (*o_iso2022jp_check_conv)(nkf_char c2,nkf_char c1) = no_connection;
642
643 /* static redirections */
644
645 static  void   (*o_putc)(nkf_char c) = std_putc;
646
647 static  nkf_char    (*i_getc)(FILE *f) = std_getc; /* general input */
648 static  nkf_char    (*i_ungetc)(nkf_char c,FILE *f) =std_ungetc;
649
650 static  nkf_char    (*i_bgetc)(FILE *) = std_getc; /* input of mgetc */
651 static  nkf_char    (*i_bungetc)(nkf_char c ,FILE *f) = std_ungetc;
652
653 static  void   (*o_mputc)(nkf_char c) = std_putc ; /* output of mputc */
654
655 static  nkf_char    (*i_mgetc)(FILE *) = std_getc; /* input of mgetc */
656 static  nkf_char    (*i_mungetc)(nkf_char c ,FILE *f) = std_ungetc;
657
658 /* for strict mime */
659 static  nkf_char    (*i_mgetc_buf)(FILE *) = std_getc; /* input of mgetc_buf */
660 static  nkf_char    (*i_mungetc_buf)(nkf_char c,FILE *f) = std_ungetc;
661
662 /* Global states */
663 static int output_mode = ASCII,    /* output kanji mode */
664            input_mode =  ASCII,    /* input kanji mode */
665            shift_mode =  FALSE;    /* TRUE shift out, or X0201  */
666 static int mime_decode_mode =   FALSE;    /* MIME mode B base64, Q hex */
667
668 /* X0201 / X0208 conversion tables */
669
670 /* X0201 kana conversion table */
671 /* 90-9F A0-DF */
672 static const
673 unsigned char cv[]= {
674     0x21,0x21,0x21,0x23,0x21,0x56,0x21,0x57,
675     0x21,0x22,0x21,0x26,0x25,0x72,0x25,0x21,
676     0x25,0x23,0x25,0x25,0x25,0x27,0x25,0x29,
677     0x25,0x63,0x25,0x65,0x25,0x67,0x25,0x43,
678     0x21,0x3c,0x25,0x22,0x25,0x24,0x25,0x26,
679     0x25,0x28,0x25,0x2a,0x25,0x2b,0x25,0x2d,
680     0x25,0x2f,0x25,0x31,0x25,0x33,0x25,0x35,
681     0x25,0x37,0x25,0x39,0x25,0x3b,0x25,0x3d,
682     0x25,0x3f,0x25,0x41,0x25,0x44,0x25,0x46,
683     0x25,0x48,0x25,0x4a,0x25,0x4b,0x25,0x4c,
684     0x25,0x4d,0x25,0x4e,0x25,0x4f,0x25,0x52,
685     0x25,0x55,0x25,0x58,0x25,0x5b,0x25,0x5e,
686     0x25,0x5f,0x25,0x60,0x25,0x61,0x25,0x62,
687     0x25,0x64,0x25,0x66,0x25,0x68,0x25,0x69,
688     0x25,0x6a,0x25,0x6b,0x25,0x6c,0x25,0x6d,
689     0x25,0x6f,0x25,0x73,0x21,0x2b,0x21,0x2c,
690     0x00,0x00};
691
692
693 /* X0201 kana conversion table for daguten */
694 /* 90-9F A0-DF */
695 static const
696 unsigned char dv[]= { 
697     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
698     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
699     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
700     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
701     0x00,0x00,0x00,0x00,0x00,0x00,0x25,0x74,
702     0x00,0x00,0x00,0x00,0x25,0x2c,0x25,0x2e,
703     0x25,0x30,0x25,0x32,0x25,0x34,0x25,0x36,
704     0x25,0x38,0x25,0x3a,0x25,0x3c,0x25,0x3e,
705     0x25,0x40,0x25,0x42,0x25,0x45,0x25,0x47,
706     0x25,0x49,0x00,0x00,0x00,0x00,0x00,0x00,
707     0x00,0x00,0x00,0x00,0x25,0x50,0x25,0x53,
708     0x25,0x56,0x25,0x59,0x25,0x5c,0x00,0x00,
709     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
710     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
711     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
712     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
713     0x00,0x00};
714
715 /* X0201 kana conversion table for han-daguten */
716 /* 90-9F A0-DF */
717 static const
718 unsigned char ev[]= { 
719     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
720     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
721     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
722     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
723     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
724     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
725     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
726     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
727     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
728     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
729     0x00,0x00,0x00,0x00,0x25,0x51,0x25,0x54,
730     0x25,0x57,0x25,0x5a,0x25,0x5d,0x00,0x00,
731     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
732     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
733     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
734     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
735     0x00,0x00};
736
737
738 /* X0208 kigou conversion table */
739 /* 0x8140 - 0x819e */
740 static const
741 unsigned char fv[] = {
742
743     0x00,0x00,0x00,0x00,0x2c,0x2e,0x00,0x3a,
744     0x3b,0x3f,0x21,0x00,0x00,0x27,0x60,0x00,
745     0x5e,0x00,0x5f,0x00,0x00,0x00,0x00,0x00,
746     0x00,0x00,0x00,0x00,0x00,0x2d,0x00,0x2f,
747     0x5c,0x00,0x00,0x7c,0x00,0x00,0x60,0x27,
748     0x22,0x22,0x28,0x29,0x00,0x00,0x5b,0x5d,
749     0x7b,0x7d,0x3c,0x3e,0x00,0x00,0x00,0x00,
750     0x00,0x00,0x00,0x00,0x2b,0x2d,0x00,0x00,
751     0x00,0x3d,0x00,0x3c,0x3e,0x00,0x00,0x00,
752     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
753     0x24,0x00,0x00,0x25,0x23,0x26,0x2a,0x40,
754     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
755 } ;
756
757
758 #define    CRLF      1
759
760 static int             file_out_f = FALSE;
761 #ifdef OVERWRITE
762 static int             overwrite_f = FALSE;
763 static int             preserve_time_f = FALSE;
764 static int             backup_f = FALSE;
765 static char            *backup_suffix = "";
766 static char *get_backup_filename(const char *suffix, const char *filename);
767 #endif
768
769 static int             crmode_f = 0;   /* CR, NL, CRLF */
770 #ifdef EASYWIN /*Easy Win */
771 static int             end_check;
772 #endif /*Easy Win */
773
774 #define STD_GC_BUFSIZE (256)
775 nkf_char std_gc_buf[STD_GC_BUFSIZE];
776 nkf_char std_gc_ndx;
777
778 #ifdef WIN32DLL
779 #include "nkf32dll.c"
780 #elif defined(PERL_XS)
781 #else /* WIN32DLL */
782 int main(int argc, char **argv)
783 {
784     FILE  *fin;
785     unsigned char  *cp;
786
787     char *outfname = NULL;
788     char *origfname;
789
790 #ifdef EASYWIN /*Easy Win */
791     _BufferSize.y = 400;/*Set Scroll Buffer Size*/
792 #endif
793
794     for (argc--,argv++; (argc > 0) && **argv == '-'; argc--, argv++) {
795         cp = (unsigned char *)*argv;
796         options(cp);
797 #ifdef EXEC_IO
798         if (exec_f){
799             int fds[2], pid;
800             if (pipe(fds) < 0 || (pid = fork()) < 0){
801                 abort();
802             }
803             if (pid == 0){
804                 if (exec_f > 0){
805                     close(fds[0]);
806                     dup2(fds[1], 1);
807                 }else{
808                     close(fds[1]);
809                     dup2(fds[0], 0);
810                 }
811                 execvp(argv[1], &argv[1]);
812             }
813             if (exec_f > 0){
814                 close(fds[1]);
815                 dup2(fds[0], 0);
816             }else{
817                 close(fds[0]);
818                 dup2(fds[1], 1);
819             }
820             argc = 0;
821             break;
822         }
823 #endif
824     }
825     if(x0201_f == WISH_TRUE)
826          x0201_f = ((!iso2022jp_f)? TRUE : NO_X0201);
827
828     if (binmode_f == TRUE)
829 #if defined(__OS2__) && (defined(__IBMC__) || defined(__IBMCPP__))
830     if (freopen("","wb",stdout) == NULL) 
831         return (-1);
832 #else
833     setbinmode(stdout);
834 #endif
835
836     if (unbuf_f)
837       setbuf(stdout, (char *) NULL);
838     else
839       setvbuffer(stdout, (char *) stdobuf, IOBUF_SIZE);
840
841     if (argc == 0) {
842       if (binmode_f == TRUE)
843 #if defined(__OS2__) && (defined(__IBMC__) || defined(__IBMCPP__))
844       if (freopen("","rb",stdin) == NULL) return (-1);
845 #else
846       setbinmode(stdin);
847 #endif
848       setvbuffer(stdin, (char *) stdibuf, IOBUF_SIZE);
849       if (nop_f)
850           noconvert(stdin);
851       else {
852           kanji_convert(stdin);
853           if (guess_f) print_guessed_code(NULL);
854       }
855     } else {
856       int nfiles = argc;
857         int is_argument_error = FALSE;
858       while (argc--) {
859             is_inputcode_mixed = FALSE;
860             is_inputcode_set   = FALSE;
861             input_codename = "";
862 #ifdef CHECK_OPTION
863             iconv_for_check = 0;
864 #endif
865           if ((fin = fopen((origfname = *argv++), "r")) == NULL) {
866               perror(*--argv);
867                 *argv++;
868                 is_argument_error = TRUE;
869                 continue;
870           } else {
871 #ifdef OVERWRITE
872               int fd = 0;
873               int fd_backup = 0;
874 #endif
875
876 /* reopen file for stdout */
877               if (file_out_f == TRUE) {
878 #ifdef OVERWRITE
879                   if (overwrite_f){
880                       outfname = malloc(strlen(origfname)
881                                         + strlen(".nkftmpXXXXXX")
882                                         + 1);
883                       if (!outfname){
884                           perror(origfname);
885                           return -1;
886                       }
887                       strcpy(outfname, origfname);
888 #ifdef MSDOS
889                       {
890                           int i;
891                           for (i = strlen(outfname); i; --i){
892                               if (outfname[i - 1] == '/'
893                                   || outfname[i - 1] == '\\'){
894                                   break;
895                               }
896                           }
897                           outfname[i] = '\0';
898                       }
899                       strcat(outfname, "ntXXXXXX");
900                       mktemp(outfname);
901                         fd = open(outfname, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL,
902                                 S_IREAD | S_IWRITE);
903 #else
904                       strcat(outfname, ".nkftmpXXXXXX");
905                       fd = mkstemp(outfname);
906 #endif
907                       if (fd < 0
908                           || (fd_backup = dup(fileno(stdout))) < 0
909                           || dup2(fd, fileno(stdout)) < 0
910                           ){
911                           perror(origfname);
912                           return -1;
913                       }
914                   }else
915 #endif
916                   if(argc == 1 ) {
917                       outfname = *argv++;
918                       argc--;
919                   } else {
920                       outfname = "nkf.out";
921                   }
922
923                   if(freopen(outfname, "w", stdout) == NULL) {
924                       perror (outfname);
925                       return (-1);
926                   }
927                   if (binmode_f == TRUE) {
928 #if defined(__OS2__) && (defined(__IBMC__) || defined(__IBMCPP__))
929                       if (freopen("","wb",stdout) == NULL) 
930                            return (-1);
931 #else
932                       setbinmode(stdout);
933 #endif
934                   }
935               }
936               if (binmode_f == TRUE)
937 #if defined(__OS2__) && (defined(__IBMC__) || defined(__IBMCPP__))
938                  if (freopen("","rb",fin) == NULL) 
939                     return (-1);
940 #else
941                  setbinmode(fin);
942 #endif 
943               setvbuffer(fin, (char *) stdibuf, IOBUF_SIZE);
944               if (nop_f)
945                   noconvert(fin);
946               else {
947                   char *filename = NULL;
948                   kanji_convert(fin);
949                   if (nfiles > 1) filename = origfname;
950                   if (guess_f) print_guessed_code(filename);
951               }
952               fclose(fin);
953 #ifdef OVERWRITE
954               if (overwrite_f) {
955                   struct stat     sb;
956 #if defined(MSDOS) && !defined(__MINGW32__) && !defined(__WIN32__) && !defined(__WATCOMC__) && !defined(__EMX__) && !defined(__OS2__) && !defined(__DJGPP__)
957                   time_t tb[2];
958 #else
959                   struct utimbuf  tb;
960 #endif
961
962                   fflush(stdout);
963                   close(fd);
964                   if (dup2(fd_backup, fileno(stdout)) < 0){
965                       perror("dup2");
966                   }
967                   if (stat(origfname, &sb)) {
968                       fprintf(stderr, "Can't stat %s\n", origfname);
969                   }
970                   /* \e$B%Q!<%_%C%7%g%s$rI|85\e(B */
971                   if (chmod(outfname, sb.st_mode)) {
972                       fprintf(stderr, "Can't set permission %s\n", outfname);
973                   }
974
975                   /* \e$B%?%$%`%9%?%s%W$rI|85\e(B */
976                     if(preserve_time_f){
977 #if defined(MSDOS) && !defined(__MINGW32__) && !defined(__WIN32__) && !defined(__WATCOMC__) && !defined(__EMX__) && !defined(__OS2__) && !defined(__DJGPP__)
978                         tb[0] = tb[1] = sb.st_mtime;
979                         if (utime(outfname, tb)) {
980                             fprintf(stderr, "Can't set timestamp %s\n", outfname);
981                         }
982 #else
983                         tb.actime  = sb.st_atime;
984                         tb.modtime = sb.st_mtime;
985                         if (utime(outfname, &tb)) {
986                             fprintf(stderr, "Can't set timestamp %s\n", outfname);
987                         }
988 #endif
989                     }
990                     if(backup_f){
991                         char *backup_filename = get_backup_filename(backup_suffix, origfname);
992 #ifdef MSDOS
993                         unlink(backup_filename);
994 #endif
995                         if (rename(origfname, backup_filename)) {
996                             perror(backup_filename);
997                             fprintf(stderr, "Can't rename %s to %s\n",
998                                     origfname, backup_filename);
999                         }
1000                     }else{
1001 #ifdef MSDOS
1002                         if (unlink(origfname)){
1003                             perror(origfname);
1004                         }
1005 #endif
1006                     }
1007                   if (rename(outfname, origfname)) {
1008                       perror(origfname);
1009                       fprintf(stderr, "Can't rename %s to %s\n",
1010                               outfname, origfname);
1011                   }
1012                   free(outfname);
1013               }
1014 #endif
1015           }
1016       }
1017         if (is_argument_error)
1018             return(-1);
1019     }
1020 #ifdef EASYWIN /*Easy Win */
1021     if (file_out_f == FALSE) 
1022         scanf("%d",&end_check);
1023     else 
1024         fclose(stdout);
1025 #else /* for Other OS */
1026     if (file_out_f == TRUE) 
1027         fclose(stdout);
1028 #endif /*Easy Win */
1029     return (0);
1030 }
1031 #endif /* WIN32DLL */
1032
1033 #ifdef OVERWRITE
1034 char *get_backup_filename(const char *suffix, const char *filename)
1035 {
1036     char *backup_filename;
1037     int asterisk_count = 0;
1038     int i, j;
1039     int filename_length = strlen(filename);
1040
1041     for(i = 0; suffix[i]; i++){
1042         if(suffix[i] == '*') asterisk_count++;
1043     }
1044
1045     if(asterisk_count){
1046         backup_filename = malloc(strlen(suffix) + (asterisk_count * (filename_length - 1)) + 1);
1047         if (!backup_filename){
1048             perror("Can't malloc backup filename.");
1049             return NULL;
1050         }
1051
1052         for(i = 0, j = 0; suffix[i];){
1053             if(suffix[i] == '*'){
1054                 backup_filename[j] = '\0';
1055                 strncat(backup_filename, filename, filename_length);
1056                 i++;
1057                 j += filename_length;
1058             }else{
1059                 backup_filename[j++] = suffix[i++];
1060             }
1061         }
1062         backup_filename[j] = '\0';
1063     }else{
1064         j = strlen(suffix) + filename_length;
1065         backup_filename = malloc( + 1);
1066         strcpy(backup_filename, filename);
1067         strcat(backup_filename, suffix);
1068         backup_filename[j] = '\0';
1069     }
1070     return backup_filename;
1071 }
1072 #endif
1073
1074 static const
1075 struct {
1076     const char *name;
1077     const char *alias;
1078 } long_option[] = {
1079     {"ic=", ""},
1080     {"oc=", ""},
1081     {"base64","jMB"},
1082     {"euc","e"},
1083     {"euc-input","E"},
1084     {"fj","jm"},
1085     {"help","v"},
1086     {"jis","j"},
1087     {"jis-input","J"},
1088     {"mac","sLm"},
1089     {"mime","jM"},
1090     {"mime-input","m"},
1091     {"msdos","sLw"},
1092     {"sjis","s"},
1093     {"sjis-input","S"},
1094     {"unix","eLu"},
1095     {"version","V"},
1096     {"windows","sLw"},
1097     {"hiragana","h1"},
1098     {"katakana","h2"},
1099     {"katakana-hiragana","h3"},
1100     {"guess", "g"},
1101     {"cp932", ""},
1102     {"no-cp932", ""},
1103 #ifdef X0212_ENABLE
1104     {"x0212", ""},
1105 #endif
1106 #ifdef UTF8_OUTPUT_ENABLE
1107     {"utf8", "w"},
1108     {"utf16", "w16"},
1109     {"ms-ucs-map", ""},
1110     {"fb-skip", ""},
1111     {"fb-html", ""},
1112     {"fb-xml", ""},
1113     {"fb-perl", ""},
1114     {"fb-java", ""},
1115     {"fb-subchar", ""},
1116     {"fb-subchar=", ""},
1117 #endif
1118 #ifdef UTF8_INPUT_ENABLE
1119     {"utf8-input", "W"},
1120     {"utf16-input", "W16"},
1121     {"no-cp932ext", ""},
1122     {"no-best-fit-chars",""},
1123 #endif
1124 #ifdef UNICODE_NORMALIZATION
1125     {"utf8mac-input", ""},
1126 #endif
1127 #ifdef OVERWRITE
1128     {"overwrite", ""},
1129     {"overwrite=", ""},
1130     {"in-place", ""},
1131     {"in-place=", ""},
1132 #endif
1133 #ifdef INPUT_OPTION
1134     {"cap-input", ""},
1135     {"url-input", ""},
1136 #endif
1137 #ifdef NUMCHAR_OPTION
1138     {"numchar-input", ""},
1139 #endif
1140 #ifdef CHECK_OPTION
1141     {"no-output", ""},
1142     {"debug", ""},
1143 #endif
1144 #ifdef SHIFTJIS_CP932
1145     {"cp932inv", ""},
1146 #endif
1147 #ifdef EXEC_IO
1148     {"exec-in", ""},
1149     {"exec-out", ""},
1150 #endif
1151     {"prefix=", ""},
1152 };
1153
1154 static int option_mode = 0;
1155
1156 void options(unsigned char *cp)
1157 {
1158     nkf_char i, j;
1159     unsigned char *p;
1160     unsigned char *cp_back = NULL;
1161     char codeset[32];
1162
1163     if (option_mode==1)
1164         return;
1165     while(*cp && *cp++!='-');
1166     while (*cp || cp_back) {
1167         if(!*cp){
1168             cp = cp_back;
1169             cp_back = NULL;
1170             continue;
1171         }
1172         p = 0;
1173         switch (*cp++) {
1174         case '-':  /* literal options */
1175             if (!*cp || *cp == SPACE) {        /* ignore the rest of arguments */
1176                 option_mode = 1;
1177                 return;
1178             }
1179             for (i=0;i<sizeof(long_option)/sizeof(long_option[0]);i++) {
1180                 p = (unsigned char *)long_option[i].name;
1181                 for (j=0;*p && *p != '=' && *p == cp[j];p++, j++);
1182                 if (*p == cp[j] || cp[j] == ' '){
1183                     p = &cp[j] + 1;
1184                     break;
1185                 }
1186                 p = 0;
1187             }
1188             if (p == 0) return;
1189             while(*cp && *cp != SPACE && cp++);
1190             if (long_option[i].alias[0]){
1191                 cp_back = cp;
1192                 cp = (unsigned char *)long_option[i].alias;
1193             }else{
1194                 if (strcmp(long_option[i].name, "ic=") == 0){
1195                     for (i=0; i < 16 && SPACE < p[i] && p[i] < DEL; i++){
1196                         codeset[i] = nkf_toupper(p[i]);
1197                     }
1198                     codeset[i] = 0;
1199                     if(strcmp(codeset, "ISO-2022-JP") == 0){
1200                         input_f = JIS_INPUT;
1201                     }else if(strcmp(codeset, "X-ISO2022JP-CP932") == 0 ||
1202                       strcmp(codeset, "CP50220") == 0 ||
1203                       strcmp(codeset, "CP50221") == 0 ||
1204                       strcmp(codeset, "CP50222") == 0){
1205                         input_f = JIS_INPUT;
1206 #ifdef SHIFTJIS_CP932
1207                         cp51932_f = TRUE;
1208 #endif
1209 #ifdef UTF8_OUTPUT_ENABLE
1210                         ms_ucs_map_f = UCS_MAP_CP932;
1211 #endif
1212                     }else if(strcmp(codeset, "ISO-2022-JP-1") == 0){
1213                         input_f = JIS_INPUT;
1214 #ifdef X0212_ENABLE
1215                         x0212_f = TRUE;
1216 #endif
1217                     }else if(strcmp(codeset, "ISO-2022-JP-3") == 0){
1218                         input_f = JIS_INPUT;
1219 #ifdef X0212_ENABLE
1220                         x0212_f = TRUE;
1221 #endif
1222                         x0213_f = TRUE;
1223                     }else if(strcmp(codeset, "SHIFT_JIS") == 0){
1224                         input_f = SJIS_INPUT;
1225                     }else if(strcmp(codeset, "WINDOWS-31J") == 0 ||
1226                              strcmp(codeset, "CSWINDOWS31J") == 0 ||
1227                              strcmp(codeset, "CP932") == 0 ||
1228                              strcmp(codeset, "MS932") == 0){
1229                         input_f = SJIS_INPUT;
1230 #ifdef SHIFTJIS_CP932
1231                         cp51932_f = TRUE;
1232 #endif
1233 #ifdef UTF8_OUTPUT_ENABLE
1234                         ms_ucs_map_f = UCS_MAP_CP932;
1235 #endif
1236                     }else if(strcmp(codeset, "EUCJP") == 0 ||
1237                              strcmp(codeset, "EUC-JP") == 0){
1238                         input_f = EUC_INPUT;
1239                     }else if(strcmp(codeset, "CP51932") == 0){
1240                         input_f = EUC_INPUT;
1241 #ifdef SHIFTJIS_CP932
1242                         cp51932_f = TRUE;
1243 #endif
1244 #ifdef UTF8_OUTPUT_ENABLE
1245                         ms_ucs_map_f = UCS_MAP_CP932;
1246 #endif
1247                     }else if(strcmp(codeset, "EUC-JP-MS") == 0 ||
1248                              strcmp(codeset, "EUCJP-MS") == 0 ||
1249                              strcmp(codeset, "EUCJPMS") == 0){
1250                         input_f = EUC_INPUT;
1251 #ifdef SHIFTJIS_CP932
1252                         cp51932_f = FALSE;
1253 #endif
1254 #ifdef UTF8_OUTPUT_ENABLE
1255                         ms_ucs_map_f = UCS_MAP_MS;
1256 #endif
1257                     }else if(strcmp(codeset, "EUC-JP-ASCII") == 0 ||
1258                              strcmp(codeset, "EUCJP-ASCII") == 0){
1259                         input_f = EUC_INPUT;
1260 #ifdef SHIFTJIS_CP932
1261                         cp51932_f = FALSE;
1262 #endif
1263 #ifdef UTF8_OUTPUT_ENABLE
1264                         ms_ucs_map_f = UCS_MAP_ASCII;
1265 #endif
1266                     }else if(strcmp(codeset, "SHIFT_JISX0213") == 0 ||
1267                              strcmp(codeset, "SHIFT_JIS-2004") == 0){
1268                         input_f = SJIS_INPUT;
1269                         x0213_f = TRUE;
1270 #ifdef SHIFTJIS_CP932
1271                         cp51932_f = FALSE;
1272                         cp932inv_f = FALSE;
1273 #endif
1274                     }else if(strcmp(codeset, "EUC-JISX0213") == 0 ||
1275                              strcmp(codeset, "EUC-JIS-2004") == 0){
1276                         input_f = EUC_INPUT;
1277                         x0213_f = TRUE;
1278 #ifdef SHIFTJIS_CP932
1279                         cp51932_f = FALSE;
1280                         cp932inv_f = FALSE;
1281 #endif
1282 #ifdef UTF8_INPUT_ENABLE
1283                     }else if(strcmp(codeset, "UTF-8") == 0 ||
1284                              strcmp(codeset, "UTF-8N") == 0 ||
1285                              strcmp(codeset, "UTF-8-BOM") == 0){
1286                         input_f = UTF8_INPUT;
1287 #ifdef UNICODE_NORMALIZATION
1288                     }else if(strcmp(codeset, "UTF8-MAC") == 0 ||
1289                              strcmp(codeset, "UTF-8-MAC") == 0){
1290                         input_f = UTF8_INPUT;
1291                         nfc_f = TRUE;
1292 #endif
1293                     }else if(strcmp(codeset, "UTF-16") == 0 ||
1294                              strcmp(codeset, "UTF-16BE") == 0 ||
1295                              strcmp(codeset, "UTF-16BE-BOM") == 0){
1296                         input_f = UTF16_INPUT;
1297                         input_endian = ENDIAN_BIG;
1298                     }else if(strcmp(codeset, "UTF-16LE") == 0 ||
1299                              strcmp(codeset, "UTF-16LE-BOM") == 0){
1300                         input_f = UTF16_INPUT;
1301                         input_endian = ENDIAN_LITTLE;
1302                     }else if(strcmp(codeset, "UTF-32") == 0 ||
1303                              strcmp(codeset, "UTF-32BE") == 0 ||
1304                              strcmp(codeset, "UTF-32BE-BOM") == 0){
1305                         input_f = UTF32_INPUT;
1306                         input_endian = ENDIAN_BIG;
1307                     }else if(strcmp(codeset, "UTF-32LE") == 0 ||
1308                              strcmp(codeset, "UTF-32LE-BOM") == 0){
1309                         input_f = UTF32_INPUT;
1310                         input_endian = ENDIAN_LITTLE;
1311 #endif
1312                     }
1313                     continue;
1314                 }
1315                 if (strcmp(long_option[i].name, "oc=") == 0){
1316                     for (i=0; i < 16 && SPACE < p[i] && p[i] < DEL; i++){
1317                         codeset[i] = nkf_toupper(p[i]);
1318                     }
1319                     codeset[i] = 0;
1320                     if(strcmp(codeset, "ISO-2022-JP") == 0){
1321                         output_conv = j_oconv;
1322                     }else if(strcmp(codeset, "X-ISO2022JP-CP932") == 0){
1323                         output_conv = j_oconv;
1324                         x0201_f = FALSE;
1325                         no_cp932ext_f = TRUE;
1326 #ifdef SHIFTJIS_CP932
1327                         cp51932_f = TRUE;
1328 #endif
1329 #ifdef UTF8_OUTPUT_ENABLE
1330                         ms_ucs_map_f = UCS_MAP_CP932;
1331 #endif
1332                     }else if(strcmp(codeset, "CP50220") == 0){
1333                         output_conv = j_oconv;
1334 #ifdef SHIFTJIS_CP932
1335                         cp51932_f = TRUE;
1336 #endif
1337 #ifdef UTF8_OUTPUT_ENABLE
1338                         ms_ucs_map_f = UCS_MAP_CP932;
1339 #endif
1340                     }else if(strcmp(codeset, "CP50221") == 0){
1341                         output_conv = j_oconv;
1342                         x0201_f = FALSE;
1343 #ifdef SHIFTJIS_CP932
1344                         cp51932_f = TRUE;
1345 #endif
1346 #ifdef UTF8_OUTPUT_ENABLE
1347                         ms_ucs_map_f = UCS_MAP_CP932;
1348 #endif
1349                     }else if(strcmp(codeset, "ISO-2022-JP-1") == 0){
1350                         output_conv = j_oconv;
1351 #ifdef X0212_ENABLE
1352                         x0212_f = TRUE;
1353 #endif
1354 #ifdef SHIFTJIS_CP932
1355                         cp51932_f = FALSE;
1356 #endif
1357                     }else if(strcmp(codeset, "ISO-2022-JP-3") == 0){
1358                         output_conv = j_oconv;
1359 #ifdef X0212_ENABLE
1360                         x0212_f = TRUE;
1361 #endif
1362                         x0213_f = TRUE;
1363 #ifdef SHIFTJIS_CP932
1364                         cp51932_f = FALSE;
1365 #endif
1366                     }else if(strcmp(codeset, "ISO-2022-JP-MS") == 0){
1367                         output_conv = j_oconv;
1368                         x0201_f = FALSE;
1369 #ifdef X0212_ENABLE
1370                         x0212_f = TRUE;
1371 #endif
1372 #ifdef SHIFTJIS_CP932
1373                         cp51932_f = FALSE;
1374 #endif
1375                     }else if(strcmp(codeset, "SHIFT_JIS") == 0){
1376                         output_conv = s_oconv;
1377                     }else if(strcmp(codeset, "WINDOWS-31J") == 0 ||
1378                              strcmp(codeset, "CSWINDOWS31J") == 0 ||
1379                              strcmp(codeset, "CP932") == 0 ||
1380                              strcmp(codeset, "MS932") == 0){
1381                         output_conv = s_oconv;
1382                         x0201_f = FALSE;
1383 #ifdef SHIFTJIS_CP932
1384                         cp51932_f = TRUE;
1385                         cp932inv_f = TRUE;
1386 #endif
1387 #ifdef UTF8_OUTPUT_ENABLE
1388                         ms_ucs_map_f = UCS_MAP_CP932;
1389 #endif
1390                     }else if(strcmp(codeset, "EUCJP") == 0 ||
1391                              strcmp(codeset, "EUC-JP") == 0){
1392                         output_conv = e_oconv;
1393                     }else if(strcmp(codeset, "CP51932") == 0){
1394                         output_conv = e_oconv;
1395                         x0201_f = FALSE;
1396 #ifdef SHIFTJIS_CP932
1397                         cp51932_f = TRUE;
1398 #endif
1399 #ifdef UTF8_OUTPUT_ENABLE
1400                         ms_ucs_map_f = UCS_MAP_CP932;
1401 #endif
1402                     }else if(strcmp(codeset, "EUC-JP-MS") == 0 ||
1403                              strcmp(codeset, "EUCJP-MS") == 0 ||
1404                              strcmp(codeset, "EUCJPMS") == 0){
1405                         output_conv = e_oconv;
1406                         x0201_f = FALSE;
1407 #ifdef X0212_ENABLE
1408                         x0212_f = TRUE;
1409 #endif
1410 #ifdef SHIFTJIS_CP932
1411                         cp51932_f = FALSE;
1412 #endif
1413 #ifdef UTF8_OUTPUT_ENABLE
1414                         ms_ucs_map_f = UCS_MAP_MS;
1415 #endif
1416                     }else if(strcmp(codeset, "EUC-JP-ASCII") == 0 ||
1417                              strcmp(codeset, "EUCJP-ASCII") == 0){
1418                         output_conv = e_oconv;
1419                         x0201_f = FALSE;
1420 #ifdef X0212_ENABLE
1421                         x0212_f = TRUE;
1422 #endif
1423 #ifdef SHIFTJIS_CP932
1424                         cp51932_f = FALSE;
1425 #endif
1426 #ifdef UTF8_OUTPUT_ENABLE
1427                         ms_ucs_map_f = UCS_MAP_ASCII;
1428 #endif
1429                     }else if(strcmp(codeset, "SHIFT_JISX0213") == 0 ||
1430                              strcmp(codeset, "SHIFT_JIS-2004") == 0){
1431                         output_conv = s_oconv;
1432                         x0213_f = TRUE;
1433 #ifdef SHIFTJIS_CP932
1434                         cp932inv_f = FALSE;
1435 #endif
1436                     }else if(strcmp(codeset, "EUC-JISX0213") == 0 ||
1437                              strcmp(codeset, "EUC-JIS-2004") == 0){
1438                         output_conv = e_oconv;
1439 #ifdef X0212_ENABLE
1440                         x0212_f = TRUE;
1441 #endif
1442                         x0213_f = TRUE;
1443 #ifdef SHIFTJIS_CP932
1444                         cp51932_f = FALSE;
1445 #endif
1446 #ifdef UTF8_OUTPUT_ENABLE
1447                     }else if(strcmp(codeset, "UTF-8") == 0){
1448                         output_conv = w_oconv;
1449                     }else if(strcmp(codeset, "UTF-8N") == 0){
1450                         output_conv = w_oconv;
1451                     }else if(strcmp(codeset, "UTF-8-BOM") == 0){
1452                         output_conv = w_oconv;
1453                         output_bom_f = TRUE;
1454                     }else if(strcmp(codeset, "UTF-16BE") == 0){
1455                         output_conv = w_oconv16;
1456                     }else if(strcmp(codeset, "UTF-16") == 0 ||
1457                              strcmp(codeset, "UTF-16BE-BOM") == 0){
1458                         output_conv = w_oconv16;
1459                         output_bom_f = TRUE;
1460                     }else if(strcmp(codeset, "UTF-16LE") == 0){
1461                         output_conv = w_oconv16;
1462                         output_endian = ENDIAN_LITTLE;
1463                     }else if(strcmp(codeset, "UTF-16LE-BOM") == 0){
1464                         output_conv = w_oconv16;
1465                         output_endian = ENDIAN_LITTLE;
1466                         output_bom_f = TRUE;
1467                     }else if(strcmp(codeset, "UTF-32") == 0 ||
1468                              strcmp(codeset, "UTF-32BE") == 0){
1469                         output_conv = w_oconv32;
1470                     }else if(strcmp(codeset, "UTF-32BE-BOM") == 0){
1471                         output_conv = w_oconv32;
1472                         output_bom_f = TRUE;
1473                     }else if(strcmp(codeset, "UTF-32LE") == 0){
1474                         output_conv = w_oconv32;
1475                         output_endian = ENDIAN_LITTLE;
1476                     }else if(strcmp(codeset, "UTF-32LE-BOM") == 0){
1477                         output_conv = w_oconv32;
1478                         output_endian = ENDIAN_LITTLE;
1479                         output_bom_f = TRUE;
1480 #endif
1481                     }
1482                     continue;
1483                 }
1484 #ifdef OVERWRITE
1485                 if (strcmp(long_option[i].name, "overwrite") == 0){
1486                     file_out_f = TRUE;
1487                     overwrite_f = TRUE;
1488                     preserve_time_f = TRUE;
1489                     continue;
1490                 }
1491                 if (strcmp(long_option[i].name, "overwrite=") == 0){
1492                     file_out_f = TRUE;
1493                     overwrite_f = TRUE;
1494                     preserve_time_f = TRUE;
1495                     backup_f = TRUE;
1496                     backup_suffix = malloc(strlen((char *) p) + 1);
1497                     strcpy(backup_suffix, (char *) p);
1498                     continue;
1499                 }
1500                 if (strcmp(long_option[i].name, "in-place") == 0){
1501                     file_out_f = TRUE;
1502                     overwrite_f = TRUE;
1503                     preserve_time_f = FALSE;
1504                     continue;
1505                 }
1506                 if (strcmp(long_option[i].name, "in-place=") == 0){
1507                     file_out_f = TRUE;
1508                     overwrite_f = TRUE;
1509                     preserve_time_f = FALSE;
1510                     backup_f = TRUE;
1511                     backup_suffix = malloc(strlen((char *) p) + 1);
1512                     strcpy(backup_suffix, (char *) p);
1513                     continue;
1514                 }
1515 #endif
1516 #ifdef INPUT_OPTION
1517                 if (strcmp(long_option[i].name, "cap-input") == 0){
1518                     cap_f = TRUE;
1519                     continue;
1520                 }
1521                 if (strcmp(long_option[i].name, "url-input") == 0){
1522                     url_f = TRUE;
1523                     continue;
1524                 }
1525 #endif
1526 #ifdef NUMCHAR_OPTION
1527                 if (strcmp(long_option[i].name, "numchar-input") == 0){
1528                     numchar_f = TRUE;
1529                     continue;
1530                 }
1531 #endif
1532 #ifdef CHECK_OPTION
1533                 if (strcmp(long_option[i].name, "no-output") == 0){
1534                     noout_f = TRUE;
1535                     continue;
1536                 }
1537                 if (strcmp(long_option[i].name, "debug") == 0){
1538                     debug_f = TRUE;
1539                     continue;
1540                 }
1541 #endif
1542                 if (strcmp(long_option[i].name, "cp932") == 0){
1543 #ifdef SHIFTJIS_CP932
1544                     cp51932_f = TRUE;
1545                     cp932inv_f = TRUE;
1546 #endif
1547 #ifdef UTF8_OUTPUT_ENABLE
1548                     ms_ucs_map_f = UCS_MAP_CP932;
1549 #endif
1550                     continue;
1551                 }
1552                 if (strcmp(long_option[i].name, "no-cp932") == 0){
1553 #ifdef SHIFTJIS_CP932
1554                     cp51932_f = FALSE;
1555                     cp932inv_f = FALSE;
1556 #endif
1557 #ifdef UTF8_OUTPUT_ENABLE
1558                     ms_ucs_map_f = UCS_MAP_ASCII;
1559 #endif
1560                     continue;
1561                 }
1562 #ifdef SHIFTJIS_CP932
1563                 if (strcmp(long_option[i].name, "cp932inv") == 0){
1564                     cp932inv_f = TRUE;
1565                     continue;
1566                 }
1567 #endif
1568
1569 #ifdef X0212_ENABLE
1570                 if (strcmp(long_option[i].name, "x0212") == 0){
1571                     x0212_f = TRUE;
1572                     continue;
1573                 }
1574 #endif
1575
1576 #ifdef EXEC_IO
1577                   if (strcmp(long_option[i].name, "exec-in") == 0){
1578                       exec_f = 1;
1579                       return;
1580                   }
1581                   if (strcmp(long_option[i].name, "exec-out") == 0){
1582                       exec_f = -1;
1583                       return;
1584                   }
1585 #endif
1586 #if defined(UTF8_OUTPUT_ENABLE) && defined(UTF8_INPUT_ENABLE)
1587                 if (strcmp(long_option[i].name, "no-cp932ext") == 0){
1588                     no_cp932ext_f = TRUE;
1589                     continue;
1590                 }
1591                 if (strcmp(long_option[i].name, "no-best-fit-chars") == 0){
1592                     no_best_fit_chars_f = TRUE;
1593                     continue;
1594                 }
1595                 if (strcmp(long_option[i].name, "fb-skip") == 0){
1596                     encode_fallback = NULL;
1597                     continue;
1598                 }
1599                 if (strcmp(long_option[i].name, "fb-html") == 0){
1600                     encode_fallback = encode_fallback_html;
1601                     continue;
1602                 }
1603                 if (strcmp(long_option[i].name, "fb-xml" ) == 0){
1604                     encode_fallback = encode_fallback_xml;
1605                     continue;
1606                 }
1607                 if (strcmp(long_option[i].name, "fb-java") == 0){
1608                     encode_fallback = encode_fallback_java;
1609                     continue;
1610                 }
1611                 if (strcmp(long_option[i].name, "fb-perl") == 0){
1612                     encode_fallback = encode_fallback_perl;
1613                     continue;
1614                 }
1615                 if (strcmp(long_option[i].name, "fb-subchar") == 0){
1616                     encode_fallback = encode_fallback_subchar;
1617                     continue;
1618                 }
1619                 if (strcmp(long_option[i].name, "fb-subchar=") == 0){
1620                     encode_fallback = encode_fallback_subchar;
1621                     unicode_subchar = 0;
1622                     if (p[0] != '0'){
1623                         /* decimal number */
1624                         for (i = 0; i < 7 && nkf_isdigit(p[i]); i++){
1625                             unicode_subchar *= 10;
1626                             unicode_subchar += hex2bin(p[i]);
1627                         }
1628                     }else if(p[1] == 'x' || p[1] == 'X'){
1629                         /* hexadecimal number */
1630                         for (i = 2; i < 8 && nkf_isxdigit(p[i]); i++){
1631                             unicode_subchar <<= 4;
1632                             unicode_subchar |= hex2bin(p[i]);
1633                         }
1634                     }else{
1635                         /* octal number */
1636                         for (i = 1; i < 8 && nkf_isoctal(p[i]); i++){
1637                             unicode_subchar *= 8;
1638                             unicode_subchar += hex2bin(p[i]);
1639                         }
1640                     }
1641                     w16e_conv(unicode_subchar, &i, &j);
1642                     unicode_subchar = i<<8 | j;
1643                     continue;
1644                 }
1645 #endif
1646 #ifdef UTF8_OUTPUT_ENABLE
1647                 if (strcmp(long_option[i].name, "ms-ucs-map") == 0){
1648                     ms_ucs_map_f = UCS_MAP_MS;
1649                     continue;
1650                 }
1651 #endif
1652 #ifdef UNICODE_NORMALIZATION
1653                 if (strcmp(long_option[i].name, "utf8mac-input") == 0){
1654                     input_f = UTF8_INPUT;
1655                     nfc_f = TRUE;
1656                     continue;
1657                 }
1658 #endif
1659                 if (strcmp(long_option[i].name, "prefix=") == 0){
1660                     if (nkf_isgraph(p[0])){
1661                         for (i = 1; nkf_isgraph(p[i]); i++){
1662                             prefix_table[p[i]] = p[0];
1663                         }
1664                     }
1665                     continue;
1666                 }
1667             }
1668             continue;
1669         case 'b':           /* buffered mode */
1670             unbuf_f = FALSE;
1671             continue;
1672         case 'u':           /* non bufferd mode */
1673             unbuf_f = TRUE;
1674             continue;
1675         case 't':           /* transparent mode */
1676             if (*cp=='1') {
1677                 /* alias of -t */
1678                 nop_f = TRUE;
1679                 *cp++;
1680             } else if (*cp=='2') {
1681                 /*
1682                  * -t with put/get
1683                  *
1684                  * nkf -t2MB hoge.bin | nkf -t2mB | diff -s - hoge.bin
1685                  *
1686                  */
1687                 nop_f = 2;
1688                 *cp++;
1689             } else
1690                 nop_f = TRUE;
1691             continue;
1692         case 'j':           /* JIS output */
1693         case 'n':
1694             output_conv = j_oconv;
1695             continue;
1696         case 'e':           /* AT&T EUC output */
1697             output_conv = e_oconv;
1698             continue;
1699         case 's':           /* SJIS output */
1700             output_conv = s_oconv;
1701             continue;
1702         case 'l':           /* ISO8859 Latin-1 support, no conversion */
1703             iso8859_f = TRUE;  /* Only compatible with ISO-2022-JP */
1704             input_f = LATIN1_INPUT;
1705             continue;
1706         case 'i':           /* Kanji IN ESC-$-@/B */
1707             if (*cp=='@'||*cp=='B') 
1708                 kanji_intro = *cp++;
1709             continue;
1710         case 'o':           /* ASCII IN ESC-(-J/B */
1711             if (*cp=='J'||*cp=='B'||*cp=='H') 
1712                 ascii_intro = *cp++;
1713             continue;
1714         case 'h':
1715             /*  
1716                 bit:1   katakana->hiragana
1717                 bit:2   hiragana->katakana
1718             */
1719             if ('9'>= *cp && *cp>='0') 
1720                 hira_f |= (*cp++ -'0');
1721             else 
1722                 hira_f |= 1;
1723             continue;
1724         case 'r':
1725             rot_f = TRUE;
1726             continue;
1727 #if defined(MSDOS) || defined(__OS2__) 
1728         case 'T':
1729             binmode_f = FALSE;
1730             continue;
1731 #endif
1732 #ifndef PERL_XS
1733         case 'V':
1734             version();
1735             exit(1);
1736             break;
1737         case 'v':
1738             usage();
1739             exit(1);
1740             break;
1741 #endif
1742 #ifdef UTF8_OUTPUT_ENABLE
1743         case 'w':           /* UTF-8 output */
1744             if (cp[0] == '8') {
1745                 output_conv = w_oconv; cp++;
1746                 if (cp[0] == '0'){
1747                     cp++;
1748                 } else {
1749                     output_bom_f = TRUE;
1750                 }
1751             } else {
1752                 if ('1'== cp[0] && '6'==cp[1]) {
1753                     output_conv = w_oconv16; cp+=2;
1754                 } else if ('3'== cp[0] && '2'==cp[1]) {
1755                     output_conv = w_oconv32; cp+=2;
1756                 } else {
1757                     output_conv = w_oconv;
1758                     continue;
1759                 }
1760                 if (cp[0]=='L') {
1761                     cp++;
1762                     output_endian = ENDIAN_LITTLE;
1763                 } else if (cp[0] == 'B') {
1764                     cp++;
1765                 } else {
1766                     continue;
1767                 }
1768                 if (cp[0] == '0'){
1769                     cp++;
1770                 } else {
1771                     output_bom_f = TRUE;
1772                 }
1773             }
1774             continue;
1775 #endif
1776 #ifdef UTF8_INPUT_ENABLE
1777         case 'W':           /* UTF input */
1778             if (cp[0] == '8') {
1779                 cp++;
1780                 input_f = UTF8_INPUT;
1781             }else{
1782                 if ('1'== cp[0] && '6'==cp[1]) {
1783                     cp += 2;
1784                     input_f = UTF16_INPUT;
1785                     input_endian = ENDIAN_BIG;
1786                 } else if ('3'== cp[0] && '2'==cp[1]) {
1787                     cp += 2;
1788                     input_f = UTF32_INPUT;
1789                     input_endian = ENDIAN_BIG;
1790                 } else {
1791                     input_f = UTF8_INPUT;
1792                     continue;
1793                 }
1794                 if (cp[0]=='L') {
1795                     cp++;
1796                     input_endian = ENDIAN_LITTLE;
1797                 } else if (cp[0] == 'B') {
1798                     cp++;
1799                 }
1800             }
1801             continue;
1802 #endif
1803         /* Input code assumption */
1804         case 'J':   /* JIS input */
1805             input_f = JIS_INPUT;
1806             continue;
1807         case 'E':   /* AT&T EUC input */
1808             input_f = EUC_INPUT;
1809             continue;
1810         case 'S':   /* MS Kanji input */
1811             input_f = SJIS_INPUT;
1812             if (x0201_f==NO_X0201) x0201_f=TRUE;
1813             continue;
1814         case 'Z':   /* Convert X0208 alphabet to asii */
1815             /*  bit:0   Convert X0208
1816                 bit:1   Convert Kankaku to one space
1817                 bit:2   Convert Kankaku to two spaces
1818                 bit:3   Convert HTML Entity
1819             */
1820             if ('9'>= *cp && *cp>='0') 
1821                 alpha_f |= 1<<(*cp++ -'0');
1822             else 
1823                 alpha_f |= TRUE;
1824             continue;
1825         case 'x':   /* Convert X0201 kana to X0208 or X0201 Conversion */
1826             x0201_f = FALSE;    /* No X0201->X0208 conversion */
1827             /* accept  X0201
1828                     ESC-(-I     in JIS, EUC, MS Kanji
1829                     SI/SO       in JIS, EUC, MS Kanji
1830                     SSO         in EUC, JIS, not in MS Kanji
1831                     MS Kanji (0xa0-0xdf) 
1832                output  X0201
1833                     ESC-(-I     in JIS (0x20-0x5f)
1834                     SSO         in EUC (0xa0-0xdf)
1835                     0xa0-0xd    in MS Kanji (0xa0-0xdf) 
1836             */
1837             continue;
1838         case 'X':   /* Assume X0201 kana */
1839             /* Default value is NO_X0201 for EUC/MS-Kanji mix */
1840             x0201_f = TRUE;
1841             continue;
1842         case 'F':   /* prserve new lines */
1843             fold_preserve_f = TRUE;
1844         case 'f':   /* folding -f60 or -f */
1845             fold_f = TRUE;
1846             fold_len = 0;
1847             while('0'<= *cp && *cp <='9') { /* we don't use atoi here */
1848                 fold_len *= 10;
1849                 fold_len += *cp++ - '0';
1850             }
1851             if (!(0<fold_len && fold_len<BUFSIZ)) 
1852                 fold_len = DEFAULT_FOLD;
1853             if (*cp=='-') {
1854                 fold_margin = 0;
1855                 cp++;
1856                 while('0'<= *cp && *cp <='9') { /* we don't use atoi here */
1857                     fold_margin *= 10;
1858                     fold_margin += *cp++ - '0';
1859                 }
1860             }
1861             continue;
1862         case 'm':   /* MIME support */
1863             /* mime_decode_f = TRUE; */ /* this has too large side effects... */
1864             if (*cp=='B'||*cp=='Q') {
1865                 mime_decode_mode = *cp++;
1866                 mimebuf_f = FIXED_MIME;
1867             } else if (*cp=='N') {
1868                 mime_f = TRUE; cp++;
1869             } else if (*cp=='S') {
1870                 mime_f = STRICT_MIME; cp++;
1871             } else if (*cp=='0') {
1872                 mime_decode_f = FALSE;
1873                 mime_f = FALSE; cp++;
1874             }
1875             continue;
1876         case 'M':   /* MIME output */
1877             if (*cp=='B') {
1878                 mimeout_mode = 'B';
1879                 mimeout_f = FIXED_MIME; cp++;
1880             } else if (*cp=='Q') {
1881                 mimeout_mode = 'Q';
1882                 mimeout_f = FIXED_MIME; cp++;
1883             } else {
1884                 mimeout_f = TRUE;
1885             }
1886             continue;
1887         case 'B':   /* Broken JIS support */
1888             /*  bit:0   no ESC JIS
1889                 bit:1   allow any x on ESC-(-x or ESC-$-x
1890                 bit:2   reset to ascii on NL
1891             */
1892             if ('9'>= *cp && *cp>='0') 
1893                 broken_f |= 1<<(*cp++ -'0');
1894             else 
1895                 broken_f |= TRUE;
1896             continue;
1897 #ifndef PERL_XS
1898         case 'O':/* for Output file */
1899             file_out_f = TRUE;
1900             continue;
1901 #endif
1902         case 'c':/* add cr code */
1903             crmode_f = CRLF;
1904             continue;
1905         case 'd':/* delete cr code */
1906             crmode_f = NL;
1907             continue;
1908         case 'I':   /* ISO-2022-JP output */
1909             iso2022jp_f = TRUE;
1910             continue;
1911         case 'L':  /* line mode */
1912             if (*cp=='u') {         /* unix */
1913                 crmode_f = NL; cp++;
1914             } else if (*cp=='m') { /* mac */
1915                 crmode_f = CR; cp++;
1916             } else if (*cp=='w') { /* windows */
1917                 crmode_f = CRLF; cp++;
1918             } else if (*cp=='0') { /* no conversion  */
1919                 crmode_f = 0; cp++;
1920             }
1921             continue;
1922         case 'g':
1923 #ifndef PERL_XS
1924             guess_f = TRUE;
1925 #endif
1926             continue;
1927         case ' ':    
1928         /* module muliple options in a string are allowed for Perl moudle  */
1929             while(*cp && *cp++!='-');
1930             continue;
1931         default:
1932             /* bogus option but ignored */
1933             continue;
1934         }
1935     }
1936 }
1937
1938 struct input_code * find_inputcode_byfunc(nkf_char (*iconv_func)(nkf_char c2,nkf_char c1,nkf_char c0))
1939 {
1940     if (iconv_func){
1941         struct input_code *p = input_code_list;
1942         while (p->name){
1943             if (iconv_func == p->iconv_func){
1944                 return p;
1945             }
1946             p++;
1947         }
1948     }
1949     return 0;
1950 }
1951
1952 void set_iconv(nkf_char f, nkf_char (*iconv_func)(nkf_char c2,nkf_char c1,nkf_char c0))
1953 {
1954 #ifdef INPUT_CODE_FIX
1955     if (f || !input_f)
1956 #endif
1957         if (estab_f != f){
1958             estab_f = f;
1959         }
1960
1961     if (iconv_func
1962 #ifdef INPUT_CODE_FIX
1963         && (f == -TRUE || !input_f) /* -TRUE means "FORCE" */
1964 #endif
1965         ){
1966         iconv = iconv_func;
1967     }
1968 #ifdef CHECK_OPTION
1969     if (estab_f && iconv_for_check != iconv){
1970         struct input_code *p = find_inputcode_byfunc(iconv);
1971         if (p){
1972             set_input_codename(p->name);
1973             debug(input_codename);
1974         }
1975         iconv_for_check = iconv;
1976     }
1977 #endif
1978 }
1979
1980 #define SCORE_L2       (1)                   /* \e$BBh\e(B2\e$B?e=`4A;z\e(B */
1981 #define SCORE_KANA     (SCORE_L2 << 1)       /* \e$B$$$o$f$kH>3Q%+%J\e(B */
1982 #define SCORE_DEPEND   (SCORE_KANA << 1)     /* \e$B5!<o0MB8J8;z\e(B */
1983 #ifdef SHIFTJIS_CP932
1984 #define SCORE_CP932    (SCORE_DEPEND << 1)   /* CP932 \e$B$K$h$kFI$_49$(\e(B */
1985 #define SCORE_NO_EXIST (SCORE_CP932 << 1)    /* \e$BB8:_$7$J$$J8;z\e(B */
1986 #else
1987 #define SCORE_NO_EXIST (SCORE_DEPEND << 1)   /* \e$BB8:_$7$J$$J8;z\e(B */
1988 #endif
1989 #define SCORE_iMIME    (SCORE_NO_EXIST << 1) /* MIME \e$B$K$h$k;XDj\e(B */
1990 #define SCORE_ERROR    (SCORE_iMIME << 1) /* \e$B%(%i!<\e(B */
1991
1992 #define SCORE_INIT (SCORE_iMIME)
1993
1994 const nkf_char score_table_A0[] = {
1995     0, 0, 0, 0,
1996     0, 0, 0, 0,
1997     0, SCORE_DEPEND, SCORE_DEPEND, SCORE_DEPEND,
1998     SCORE_DEPEND, SCORE_DEPEND, SCORE_DEPEND, SCORE_NO_EXIST,
1999 };
2000
2001 const nkf_char score_table_F0[] = {
2002     SCORE_L2, SCORE_L2, SCORE_L2, SCORE_L2,
2003     SCORE_L2, SCORE_DEPEND, SCORE_NO_EXIST, SCORE_NO_EXIST,
2004     SCORE_DEPEND, SCORE_DEPEND, SCORE_DEPEND, SCORE_DEPEND,
2005     SCORE_DEPEND, SCORE_NO_EXIST, SCORE_NO_EXIST, SCORE_ERROR,
2006 };
2007
2008 void set_code_score(struct input_code *ptr, nkf_char score)
2009 {
2010     if (ptr){
2011         ptr->score |= score;
2012     }
2013 }
2014
2015 void clr_code_score(struct input_code *ptr, nkf_char score)
2016 {
2017     if (ptr){
2018         ptr->score &= ~score;
2019     }
2020 }
2021
2022 void code_score(struct input_code *ptr)
2023 {
2024     nkf_char c2 = ptr->buf[0];
2025 #ifdef UTF8_OUTPUT_ENABLE
2026     nkf_char c1 = ptr->buf[1];
2027 #endif
2028     if (c2 < 0){
2029         set_code_score(ptr, SCORE_ERROR);
2030     }else if (c2 == SSO){
2031         set_code_score(ptr, SCORE_KANA);
2032 #ifdef UTF8_OUTPUT_ENABLE
2033     }else if (!e2w_conv(c2, c1)){
2034         set_code_score(ptr, SCORE_NO_EXIST);
2035 #endif
2036     }else if ((c2 & 0x70) == 0x20){
2037         set_code_score(ptr, score_table_A0[c2 & 0x0f]);
2038     }else if ((c2 & 0x70) == 0x70){
2039         set_code_score(ptr, score_table_F0[c2 & 0x0f]);
2040     }else if ((c2 & 0x70) >= 0x50){
2041         set_code_score(ptr, SCORE_L2);
2042     }
2043 }
2044
2045 void status_disable(struct input_code *ptr)
2046 {
2047     ptr->stat = -1;
2048     ptr->buf[0] = -1;
2049     code_score(ptr);
2050     if (iconv == ptr->iconv_func) set_iconv(FALSE, 0);
2051 }
2052
2053 void status_push_ch(struct input_code *ptr, nkf_char c)
2054 {
2055     ptr->buf[ptr->index++] = c;
2056 }
2057
2058 void status_clear(struct input_code *ptr)
2059 {
2060     ptr->stat = 0;
2061     ptr->index = 0;
2062 }
2063
2064 void status_reset(struct input_code *ptr)
2065 {
2066     status_clear(ptr);
2067     ptr->score = SCORE_INIT;
2068 }
2069
2070 void status_reinit(struct input_code *ptr)
2071 {
2072     status_reset(ptr);
2073     ptr->_file_stat = 0;
2074 }
2075
2076 void status_check(struct input_code *ptr, nkf_char c)
2077 {
2078     if (c <= DEL && estab_f){
2079         status_reset(ptr);
2080     }
2081 }
2082
2083 void s_status(struct input_code *ptr, nkf_char c)
2084 {
2085     switch(ptr->stat){
2086       case -1:
2087           status_check(ptr, c);
2088           break;
2089       case 0:
2090           if (c <= DEL){
2091               break;
2092 #ifdef NUMCHAR_OPTION
2093           }else if (is_unicode_capsule(c)){
2094               break;
2095 #endif
2096           }else if (0xa1 <= c && c <= 0xdf){
2097               status_push_ch(ptr, SSO);
2098               status_push_ch(ptr, c);
2099               code_score(ptr);
2100               status_clear(ptr);
2101           }else if ((0x81 <= c && c < 0xa0) || (0xe0 <= c && c <= 0xef)){
2102               ptr->stat = 1;
2103               status_push_ch(ptr, c);
2104 #ifdef SHIFTJIS_CP932
2105           }else if (cp51932_f
2106                     && is_ibmext_in_sjis(c)){
2107               ptr->stat = 2;
2108               status_push_ch(ptr, c);
2109 #endif /* SHIFTJIS_CP932 */
2110 #ifdef X0212_ENABLE
2111           }else if (x0212_f && 0xf0 <= c && c <= 0xfc){
2112               ptr->stat = 1;
2113               status_push_ch(ptr, c);
2114 #endif /* X0212_ENABLE */
2115           }else{
2116               status_disable(ptr);
2117           }
2118           break;
2119       case 1:
2120           if ((0x40 <= c && c <= 0x7e) || (0x80 <= c && c <= 0xfc)){
2121               status_push_ch(ptr, c);
2122               s2e_conv(ptr->buf[0], ptr->buf[1], &ptr->buf[0], &ptr->buf[1]);
2123               code_score(ptr);
2124               status_clear(ptr);
2125           }else{
2126               status_disable(ptr);
2127           }
2128           break;
2129       case 2:
2130 #ifdef SHIFTJIS_CP932
2131           if ((0x40 <= c && c <= 0x7e) || (0x80 <= c && c <= 0xfc)){
2132               status_push_ch(ptr, c);
2133               if (s2e_conv(ptr->buf[0], ptr->buf[1], &ptr->buf[0], &ptr->buf[1]) == 0){
2134                   set_code_score(ptr, SCORE_CP932);
2135                   status_clear(ptr);
2136                   break;
2137               }
2138           }
2139 #endif /* SHIFTJIS_CP932 */
2140 #ifndef X0212_ENABLE
2141           status_disable(ptr);
2142 #endif
2143           break;
2144     }
2145 }
2146
2147 void e_status(struct input_code *ptr, nkf_char c)
2148 {
2149     switch (ptr->stat){
2150       case -1:
2151           status_check(ptr, c);
2152           break;
2153       case 0:
2154           if (c <= DEL){
2155               break;
2156 #ifdef NUMCHAR_OPTION
2157           }else if (is_unicode_capsule(c)){
2158               break;
2159 #endif
2160           }else if (SSO == c || (0xa1 <= c && c <= 0xfe)){
2161               ptr->stat = 1;
2162               status_push_ch(ptr, c);
2163 #ifdef X0212_ENABLE
2164           }else if (0x8f == c){
2165               ptr->stat = 2;
2166               status_push_ch(ptr, c);
2167 #endif /* X0212_ENABLE */
2168           }else{
2169               status_disable(ptr);
2170           }
2171           break;
2172       case 1:
2173           if (0xa1 <= c && c <= 0xfe){
2174               status_push_ch(ptr, c);
2175               code_score(ptr);
2176               status_clear(ptr);
2177           }else{
2178               status_disable(ptr);
2179           }
2180           break;
2181 #ifdef X0212_ENABLE
2182       case 2:
2183           if (0xa1 <= c && c <= 0xfe){
2184               ptr->stat = 1;
2185               status_push_ch(ptr, c);
2186           }else{
2187               status_disable(ptr);
2188           }
2189 #endif /* X0212_ENABLE */
2190     }
2191 }
2192
2193 #ifdef UTF8_INPUT_ENABLE
2194 void w_status(struct input_code *ptr, nkf_char c)
2195 {
2196     switch (ptr->stat){
2197       case -1:
2198           status_check(ptr, c);
2199           break;
2200       case 0:
2201           if (c <= DEL){
2202               break;
2203 #ifdef NUMCHAR_OPTION
2204           }else if (is_unicode_capsule(c)){
2205               break;
2206 #endif
2207           }else if (0xc0 <= c && c <= 0xdf){
2208               ptr->stat = 1;
2209               status_push_ch(ptr, c);
2210           }else if (0xe0 <= c && c <= 0xef){
2211               ptr->stat = 2;
2212               status_push_ch(ptr, c);
2213           }else if (0xf0 <= c && c <= 0xf4){
2214               ptr->stat = 3;
2215               status_push_ch(ptr, c);
2216           }else{
2217               status_disable(ptr);
2218           }
2219           break;
2220       case 1:
2221       case 2:
2222           if (0x80 <= c && c <= 0xbf){
2223               status_push_ch(ptr, c);
2224               if (ptr->index > ptr->stat){
2225                   int bom = (ptr->buf[0] == 0xef && ptr->buf[1] == 0xbb
2226                              && ptr->buf[2] == 0xbf);
2227                   w2e_conv(ptr->buf[0], ptr->buf[1], ptr->buf[2],
2228                            &ptr->buf[0], &ptr->buf[1]);
2229                   if (!bom){
2230                       code_score(ptr);
2231                   }
2232                   status_clear(ptr);
2233               }
2234           }else{
2235               status_disable(ptr);
2236           }
2237           break;
2238       case 3:
2239         if (0x80 <= c && c <= 0xbf){
2240             if (ptr->index < ptr->stat){
2241                 status_push_ch(ptr, c);
2242             } else {
2243                 status_clear(ptr);
2244             }
2245           }else{
2246               status_disable(ptr);
2247           }
2248           break;
2249     }
2250 }
2251 #endif
2252
2253 void code_status(nkf_char c)
2254 {
2255     int action_flag = 1;
2256     struct input_code *result = 0;
2257     struct input_code *p = input_code_list;
2258     while (p->name){
2259         if (!p->status_func) {
2260             ++p;
2261             continue;
2262         }
2263         if (!p->status_func)
2264             continue;
2265         (p->status_func)(p, c);
2266         if (p->stat > 0){
2267             action_flag = 0;
2268         }else if(p->stat == 0){
2269             if (result){
2270                 action_flag = 0;
2271             }else{
2272                 result = p;
2273             }
2274         }
2275         ++p;
2276     }
2277
2278     if (action_flag){
2279         if (result && !estab_f){
2280             set_iconv(TRUE, result->iconv_func);
2281         }else if (c <= DEL){
2282             struct input_code *ptr = input_code_list;
2283             while (ptr->name){
2284                 status_reset(ptr);
2285                 ++ptr;
2286             }
2287         }
2288     }
2289 }
2290
2291 #ifndef WIN32DLL
2292 nkf_char std_getc(FILE *f)
2293 {
2294     if (std_gc_ndx){
2295         return std_gc_buf[--std_gc_ndx];
2296     }
2297     return getc(f);
2298 }
2299 #endif /*WIN32DLL*/
2300
2301 nkf_char std_ungetc(nkf_char c, FILE *f)
2302 {
2303     if (std_gc_ndx == STD_GC_BUFSIZE){
2304         return EOF;
2305     }
2306     std_gc_buf[std_gc_ndx++] = c;
2307     return c;
2308 }
2309
2310 #ifndef WIN32DLL
2311 void std_putc(nkf_char c)
2312 {
2313     if(c!=EOF)
2314       putchar(c);
2315 }
2316 #endif /*WIN32DLL*/
2317
2318 #if !defined(PERL_XS) && !defined(WIN32DLL)
2319 nkf_char noconvert(FILE *f)
2320 {
2321     nkf_char    c;
2322
2323     if (nop_f == 2)
2324         module_connection();
2325     while ((c = (*i_getc)(f)) != EOF)
2326       (*o_putc)(c);
2327     (*o_putc)(EOF);
2328     return 1;
2329 }
2330 #endif
2331
2332 void module_connection(void)
2333 {
2334     oconv = output_conv; 
2335     o_putc = std_putc;
2336
2337     /* replace continucation module, from output side */
2338
2339     /* output redicrection */
2340 #ifdef CHECK_OPTION
2341     if (noout_f || guess_f){
2342         o_putc = no_putc;
2343     }
2344 #endif
2345     if (mimeout_f) {
2346         o_mputc = o_putc;
2347         o_putc = mime_putc;
2348         if (mimeout_f == TRUE) {
2349             o_base64conv = oconv; oconv = base64_conv;
2350         }
2351         /* base64_count = 0; */
2352     }
2353
2354     if (crmode_f) {
2355         o_crconv = oconv; oconv = cr_conv;
2356     }
2357     if (rot_f) {
2358         o_rot_conv = oconv; oconv = rot_conv;
2359     }
2360     if (iso2022jp_f) {
2361         o_iso2022jp_check_conv = oconv; oconv = iso2022jp_check_conv;
2362     }
2363     if (hira_f) {
2364         o_hira_conv = oconv; oconv = hira_conv;
2365     }
2366     if (fold_f) {
2367         o_fconv = oconv; oconv = fold_conv;
2368         f_line = 0;
2369     }
2370     if (alpha_f || x0201_f) {
2371         o_zconv = oconv; oconv = z_conv;
2372     }
2373
2374     i_getc = std_getc;
2375     i_ungetc = std_ungetc;
2376     /* input redicrection */
2377 #ifdef INPUT_OPTION
2378     if (cap_f){
2379         i_cgetc = i_getc; i_getc = cap_getc;
2380         i_cungetc = i_ungetc; i_ungetc= cap_ungetc;
2381     }
2382     if (url_f){
2383         i_ugetc = i_getc; i_getc = url_getc;
2384         i_uungetc = i_ungetc; i_ungetc= url_ungetc;
2385     }
2386 #endif
2387 #ifdef NUMCHAR_OPTION
2388     if (numchar_f){
2389         i_ngetc = i_getc; i_getc = numchar_getc;
2390         i_nungetc = i_ungetc; i_ungetc= numchar_ungetc;
2391     }
2392 #endif
2393 #ifdef UNICODE_NORMALIZATION
2394     if (nfc_f && input_f == UTF8_INPUT){
2395         i_nfc_getc = i_getc; i_getc = nfc_getc;
2396         i_nfc_ungetc = i_ungetc; i_ungetc= nfc_ungetc;
2397     }
2398 #endif
2399     if (mime_f && mimebuf_f==FIXED_MIME) {
2400         i_mgetc = i_getc; i_getc = mime_getc;
2401         i_mungetc = i_ungetc; i_ungetc = mime_ungetc;
2402     }
2403     if (broken_f & 1) {
2404         i_bgetc = i_getc; i_getc = broken_getc;
2405         i_bungetc = i_ungetc; i_ungetc = broken_ungetc;
2406     }
2407     if (input_f == JIS_INPUT || input_f == EUC_INPUT || input_f == LATIN1_INPUT) {
2408         set_iconv(-TRUE, e_iconv);
2409     } else if (input_f == SJIS_INPUT) {
2410         set_iconv(-TRUE, s_iconv);
2411 #ifdef UTF8_INPUT_ENABLE
2412     } else if (input_f == UTF8_INPUT) {
2413         set_iconv(-TRUE, w_iconv);
2414     } else if (input_f == UTF16_INPUT) {
2415         set_iconv(-TRUE, w_iconv16);
2416     } else if (input_f == UTF32_INPUT) {
2417         set_iconv(-TRUE, w_iconv32);
2418 #endif
2419     } else {
2420         set_iconv(FALSE, e_iconv);
2421     }
2422
2423     {
2424         struct input_code *p = input_code_list;
2425         while (p->name){
2426             status_reinit(p++);
2427         }
2428     }
2429 }
2430
2431 /*
2432  * Check and Ignore BOM
2433  */
2434 void check_bom(FILE *f)
2435 {
2436     int c2;
2437     switch(c2 = (*i_getc)(f)){
2438     case 0x00:
2439         if((c2 = (*i_getc)(f)) == 0x00){
2440             if((c2 = (*i_getc)(f)) == 0xFE){
2441                 if((c2 = (*i_getc)(f)) == 0xFF){
2442                     if(!input_f){
2443                         set_iconv(TRUE, w_iconv32);
2444                     }
2445                     if (iconv == w_iconv32) {
2446                         input_endian = ENDIAN_BIG;
2447                         return;
2448                     }
2449                     (*i_ungetc)(0xFF,f);
2450                 }else (*i_ungetc)(c2,f);
2451                 (*i_ungetc)(0xFE,f);
2452             }else if(c2 == 0xFF){
2453                 if((c2 = (*i_getc)(f)) == 0xFE){
2454                     if(!input_f){
2455                         set_iconv(TRUE, w_iconv32);
2456                     }
2457                     if (iconv == w_iconv32) {
2458                         input_endian = ENDIAN_2143;
2459                         return;
2460                     }
2461                     (*i_ungetc)(0xFF,f);
2462                 }else (*i_ungetc)(c2,f);
2463                 (*i_ungetc)(0xFF,f);
2464             }else (*i_ungetc)(c2,f);
2465             (*i_ungetc)(0x00,f);
2466         }else (*i_ungetc)(c2,f);
2467         (*i_ungetc)(0x00,f);
2468         break;
2469     case 0xEF:
2470         if((c2 = (*i_getc)(f)) == 0xBB){
2471             if((c2 = (*i_getc)(f)) == 0xBF){
2472                 if(!input_f){
2473                     set_iconv(TRUE, w_iconv);
2474                 }
2475                 if (iconv == w_iconv) {
2476                     return;
2477                 }
2478                 (*i_ungetc)(0xBF,f);
2479             }else (*i_ungetc)(c2,f);
2480             (*i_ungetc)(0xBB,f);
2481         }else (*i_ungetc)(c2,f);
2482         (*i_ungetc)(0xEF,f);
2483         break;
2484     case 0xFE:
2485         if((c2 = (*i_getc)(f)) == 0xFF){
2486             if((c2 = (*i_getc)(f)) == 0x00){
2487                 if((c2 = (*i_getc)(f)) == 0x00){
2488                     if(!input_f){
2489                         set_iconv(TRUE, w_iconv32);
2490                     }
2491                     if (iconv == w_iconv32) {
2492                         input_endian = ENDIAN_3412;
2493                         return;
2494                     }
2495                     (*i_ungetc)(0x00,f);
2496                 }else (*i_ungetc)(c2,f);
2497                 (*i_ungetc)(0x00,f);
2498             }else (*i_ungetc)(c2,f);
2499             if(!input_f){
2500                 set_iconv(TRUE, w_iconv16);
2501             }
2502             if (iconv == w_iconv16) {
2503                 input_endian = ENDIAN_BIG;
2504                 return;
2505             }
2506             (*i_ungetc)(0xFF,f);
2507         }else (*i_ungetc)(c2,f);
2508         (*i_ungetc)(0xFE,f);
2509         break;
2510     case 0xFF:
2511         if((c2 = (*i_getc)(f)) == 0xFE){
2512             if((c2 = (*i_getc)(f)) == 0x00){
2513                 if((c2 = (*i_getc)(f)) == 0x00){
2514                     if(!input_f){
2515                         set_iconv(TRUE, w_iconv32);
2516                     }
2517                     if (iconv == w_iconv32) {
2518                         input_endian = ENDIAN_LITTLE;
2519                         return;
2520                     }
2521                     (*i_ungetc)(0x00,f);
2522                 }else (*i_ungetc)(c2,f);
2523                 (*i_ungetc)(0x00,f);
2524             }else (*i_ungetc)(c2,f);
2525             if(!input_f){
2526                 set_iconv(TRUE, w_iconv16);
2527             }
2528             if (iconv == w_iconv16) {
2529                 input_endian = ENDIAN_LITTLE;
2530                 return;
2531             }
2532             (*i_ungetc)(0xFE,f);
2533         }else (*i_ungetc)(c2,f);
2534         (*i_ungetc)(0xFF,f);
2535         break;
2536     default:
2537         (*i_ungetc)(c2,f);
2538         break;
2539     }
2540 }
2541
2542 /*
2543    Conversion main loop. Code detection only. 
2544  */
2545
2546 nkf_char kanji_convert(FILE *f)
2547 {
2548     nkf_char    c3, c2=0, c1, c0=0;
2549     int is_8bit = FALSE;
2550
2551     if(input_f == SJIS_INPUT || input_f == EUC_INPUT
2552 #ifdef UTF8_INPUT_ENABLE
2553        || input_f == UTF8_INPUT || input_f == UTF16_INPUT
2554 #endif
2555       ){
2556         is_8bit = TRUE;
2557     }
2558
2559     input_mode = ASCII;
2560     output_mode = ASCII;
2561     shift_mode = FALSE;
2562
2563 #define NEXT continue      /* no output, get next */
2564 #define SEND ;             /* output c1 and c2, get next */
2565 #define LAST break         /* end of loop, go closing  */
2566
2567     module_connection();
2568     check_bom(f);
2569
2570     while ((c1 = (*i_getc)(f)) != EOF) {
2571 #ifdef INPUT_CODE_FIX
2572         if (!input_f)
2573 #endif
2574             code_status(c1);
2575         if (c2) {
2576             /* second byte */
2577             if (c2 > DEL) {
2578                 /* in case of 8th bit is on */
2579                 if (!estab_f&&!mime_decode_mode) {
2580                     /* in case of not established yet */
2581                     /* It is still ambiguious */
2582                     if (h_conv(f, c2, c1)==EOF) 
2583                         LAST;
2584                     else 
2585                         c2 = 0;
2586                     NEXT;
2587                 } else {
2588                     /* in case of already established */
2589                     if (c1 < AT && !(X0208 && 0x80 <= c2 && c2 <= 0x92)) {
2590                         /* ignore bogus code and not CP5022x UCD */
2591                         c2 = 0;
2592                         NEXT;
2593                     } else {
2594                         SEND;
2595                     }
2596                 }
2597             } else
2598                 /* second byte, 7 bit code */
2599                 /* it might be kanji shitfted */
2600                 if ((c1 == DEL) || (c1 <= SPACE)) {
2601                     /* ignore bogus first code */
2602                     c2 = 0;
2603                     NEXT;
2604                 } else
2605                     SEND;
2606         } else {
2607             /* first byte */
2608 #ifdef UTF8_INPUT_ENABLE
2609             if (iconv == w_iconv16) {
2610                 if (input_endian == ENDIAN_BIG) {
2611                     c2 = c1;
2612                     if ((c1 = (*i_getc)(f)) != EOF) {
2613                         if (0xD8 <= c2 && c2 <= 0xDB) {
2614                             if ((c0 = (*i_getc)(f)) != EOF) {
2615                                 c0 <<= 8;
2616                                 if ((c3 = (*i_getc)(f)) != EOF) {
2617                                     c0 |= c3;
2618                                 } else c2 = EOF;
2619                             } else c2 = EOF;
2620                         }
2621                     } else c2 = EOF;
2622                 } else {
2623                     if ((c2 = (*i_getc)(f)) != EOF) {
2624                         if (0xD8 <= c2 && c2 <= 0xDB) {
2625                             if ((c3 = (*i_getc)(f)) != EOF) {
2626                                 if ((c0 = (*i_getc)(f)) != EOF) {
2627                                     c0 <<= 8;
2628                                     c0 |= c3;
2629                                 } else c2 = EOF;
2630                             } else c2 = EOF;
2631                         }
2632                     } else c2 = EOF;
2633                 }
2634                 SEND;
2635             } else if(iconv == w_iconv32){
2636                 int c3 = c1;
2637                 if((c2 = (*i_getc)(f)) != EOF &&
2638                    (c1 = (*i_getc)(f)) != EOF &&
2639                    (c0 = (*i_getc)(f)) != EOF){
2640                     switch(input_endian){
2641                     case ENDIAN_BIG:
2642                         c1 = (c2&0xFF)<<16 | (c1&0xFF)<<8 | (c0&0xFF);
2643                         break;
2644                     case ENDIAN_LITTLE:
2645                         c1 = (c3&0xFF) | (c2&0xFF)<<8 | (c1&0xFF)<<16;
2646                         break;
2647                     case ENDIAN_2143:
2648                         c1 = (c3&0xFF)<<16 | (c1&0xFF) | (c0&0xFF)<<8;
2649                         break;
2650                     case ENDIAN_3412:
2651                         c1 = (c3&0xFF)<<8 | (c2&0xFF) | (c0&0xFF)<<16;
2652                         break;
2653                     }
2654                     c2 = 0;
2655                 }else{
2656                     c2 = EOF;
2657                 }
2658                 SEND;
2659             } else
2660 #endif
2661 #ifdef NUMCHAR_OPTION
2662             if (is_unicode_capsule(c1)){
2663                 SEND;
2664             } else
2665 #endif
2666             if (c1 > DEL) {
2667                 /* 8 bit code */
2668                 if (!estab_f && !iso8859_f) {
2669                     /* not established yet */
2670                     c2 = c1;
2671                     NEXT;
2672                 } else { /* estab_f==TRUE */
2673                     if (iso8859_f) {
2674                         c2 = ISO8859_1;
2675                         c1 &= 0x7f;
2676                         SEND;
2677                     } else if (SSP<=c1 && c1<0xe0 && iconv == s_iconv) {
2678                         /* SJIS X0201 Case... */
2679                         if(iso2022jp_f && x0201_f==NO_X0201) {
2680                             (*oconv)(GETA1, GETA2);
2681                             NEXT;
2682                         } else {
2683                             c2 = X0201;
2684                             c1 &= 0x7f;
2685                             SEND;
2686                         }
2687                     } else if (c1==SSO && iconv != s_iconv) {
2688                         /* EUC X0201 Case */
2689                         c1 = (*i_getc)(f);  /* skip SSO */
2690                         code_status(c1);
2691                         if (SSP<=c1 && c1<0xe0) {
2692                             if(iso2022jp_f &&  x0201_f==NO_X0201) {
2693                                 (*oconv)(GETA1, GETA2);
2694                                 NEXT;
2695                             } else {
2696                                 c2 = X0201;
2697                                 c1 &= 0x7f;
2698                                 SEND;
2699                             }
2700                         } else  { /* bogus code, skip SSO and one byte */
2701                             NEXT;
2702                         }
2703                     } else {
2704                        /* already established */
2705                        c2 = c1;
2706                        NEXT;
2707                     }
2708                 }
2709             } else if ((c1 > SPACE) && (c1 != DEL)) {
2710                 /* in case of Roman characters */
2711                 if (shift_mode) { 
2712                     /* output 1 shifted byte */
2713                     if (iso8859_f) {
2714                         c2 = ISO8859_1;
2715                         SEND;
2716                     } else if (SPACE<=c1 && c1<(0xe0&0x7f) ){
2717                       /* output 1 shifted byte */
2718                         if(iso2022jp_f && x0201_f==NO_X0201) {
2719                             (*oconv)(GETA1, GETA2);
2720                             NEXT;
2721                         } else {
2722                             c2 = X0201;
2723                             SEND;
2724                         }
2725                     } else {
2726                         /* look like bogus code */
2727                         NEXT;
2728                     }
2729                 } else if (input_mode == X0208 || input_mode == X0212 ||
2730                            input_mode == X0213_1 || input_mode == X0213_2) {
2731                     /* in case of Kanji shifted */
2732                     c2 = c1;
2733                     NEXT;
2734                 } else if (c1 == '=' && mime_f && !mime_decode_mode ) {
2735                     /* Check MIME code */
2736                     if ((c1 = (*i_getc)(f)) == EOF) {
2737                         (*oconv)(0, '=');
2738                         LAST;
2739                     } else if (c1 == '?') {
2740                         /* =? is mime conversion start sequence */
2741                         if(mime_f == STRICT_MIME) {
2742                             /* check in real detail */
2743                             if (mime_begin_strict(f) == EOF) 
2744                                 LAST;
2745                             else
2746                                 NEXT;
2747                         } else if (mime_begin(f) == EOF) 
2748                             LAST;
2749                         else
2750                             NEXT;
2751                     } else {
2752                         (*oconv)(0, '=');
2753                         (*i_ungetc)(c1,f);
2754                         NEXT;
2755                     }
2756                 } else {
2757                     /* normal ASCII code */ 
2758                     SEND;
2759                 }
2760             } else if (!is_8bit && c1 == SI) {
2761                 shift_mode = FALSE; 
2762                 NEXT;
2763             } else if (!is_8bit && c1 == SO) {
2764                 shift_mode = TRUE; 
2765                 NEXT;
2766             } else if (!is_8bit && c1 == ESC ) {
2767                 if ((c1 = (*i_getc)(f)) == EOF) {
2768                     /*  (*oconv)(0, ESC); don't send bogus code */
2769                     LAST;
2770                 } else if (c1 == '$') {
2771                     if ((c1 = (*i_getc)(f)) == EOF) {
2772                         /*
2773                         (*oconv)(0, ESC); don't send bogus code 
2774                         (*oconv)(0, '$'); */
2775                         LAST;
2776                     } else if (c1 == '@'|| c1 == 'B') {
2777                         /* This is kanji introduction */
2778                         input_mode = X0208;
2779                         shift_mode = FALSE;
2780                         set_input_codename("ISO-2022-JP");
2781 #ifdef CHECK_OPTION
2782                         debug(input_codename);
2783 #endif
2784                         NEXT;
2785                     } else if (c1 == '(') {
2786                         if ((c1 = (*i_getc)(f)) == EOF) {
2787                             /* don't send bogus code 
2788                             (*oconv)(0, ESC);
2789                             (*oconv)(0, '$');
2790                             (*oconv)(0, '(');
2791                                 */
2792                             LAST;
2793                         } else if (c1 == '@'|| c1 == 'B') {
2794                             /* This is kanji introduction */
2795                             input_mode = X0208;
2796                             shift_mode = FALSE;
2797                             NEXT;
2798 #ifdef X0212_ENABLE
2799                         } else if (c1 == 'D'){
2800                             input_mode = X0212;
2801                             shift_mode = FALSE;
2802                             NEXT;
2803 #endif /* X0212_ENABLE */
2804                         } else if (c1 == (X0213_1&0x7F)){
2805                             input_mode = X0213_1;
2806                             shift_mode = FALSE;
2807                             NEXT;
2808                         } else if (c1 == (X0213_2&0x7F)){
2809                             input_mode = X0213_2;
2810                             shift_mode = FALSE;
2811                             NEXT;
2812                         } else {
2813                             /* could be some special code */
2814                             (*oconv)(0, ESC);
2815                             (*oconv)(0, '$');
2816                             (*oconv)(0, '(');
2817                             (*oconv)(0, c1);
2818                             NEXT;
2819                         }
2820                     } else if (broken_f&0x2) {
2821                         /* accept any ESC-(-x as broken code ... */
2822                         input_mode = X0208;
2823                         shift_mode = FALSE;
2824                         NEXT;
2825                     } else {
2826                         (*oconv)(0, ESC);
2827                         (*oconv)(0, '$');
2828                         (*oconv)(0, c1);
2829                         NEXT;
2830                     }
2831                 } else if (c1 == '(') {
2832                     if ((c1 = (*i_getc)(f)) == EOF) {
2833                         /* don't send bogus code 
2834                         (*oconv)(0, ESC);
2835                         (*oconv)(0, '('); */
2836                         LAST;
2837                     } else {
2838                         if (c1 == 'I') {
2839                             /* This is X0201 kana introduction */
2840                             input_mode = X0201; shift_mode = X0201;
2841                             NEXT;
2842                         } else if (c1 == 'B' || c1 == 'J' || c1 == 'H') {
2843                             /* This is X0208 kanji introduction */
2844                             input_mode = ASCII; shift_mode = FALSE;
2845                             NEXT;
2846                         } else if (broken_f&0x2) {
2847                             input_mode = ASCII; shift_mode = FALSE;
2848                             NEXT;
2849                         } else {
2850                             (*oconv)(0, ESC);
2851                             (*oconv)(0, '(');
2852                             /* maintain various input_mode here */
2853                             SEND;
2854                         }
2855                     }
2856                } else if ( c1 == 'N' || c1 == 'n' ){
2857                    /* SS2 */
2858                    c3 = (*i_getc)(f);  /* skip SS2 */
2859                    if ( (SPACE<=c3 && c3 < 0x60) || (0xa0<=c3 && c3 < 0xe0)){
2860                        c1 = c3;
2861                        c2 = X0201;
2862                        SEND;
2863                    }else{
2864                        (*i_ungetc)(c3, f);
2865                        /* lonely ESC  */
2866                        (*oconv)(0, ESC);
2867                        SEND;
2868                    }
2869                 } else {
2870                     /* lonely ESC  */
2871                     (*oconv)(0, ESC);
2872                     SEND;
2873                 }
2874             } else if ((c1 == NL || c1 == CR) && broken_f&4) {
2875                 input_mode = ASCII; set_iconv(FALSE, 0);
2876                 SEND;
2877             } else if (c1 == NL && mime_decode_f && !mime_decode_mode ) {
2878                 if ((c1=(*i_getc)(f))!=EOF && c1 == SPACE) {
2879                     i_ungetc(SPACE,f);
2880                     continue;
2881                 } else {
2882                     i_ungetc(c1,f);
2883                 }
2884                 c1 = NL;
2885                 SEND;
2886             } else if (c1 == CR && mime_decode_f && !mime_decode_mode ) {
2887                 if ((c1=(*i_getc)(f))!=EOF) {
2888                     if (c1==SPACE) {
2889                         i_ungetc(SPACE,f);
2890                         continue;
2891                     } else if (c1 == NL && (c1=(*i_getc)(f))!=EOF && c1 == SPACE) {
2892                         i_ungetc(SPACE,f);
2893                         continue;
2894                     } else {
2895                         i_ungetc(c1,f);
2896                     }
2897                     i_ungetc(NL,f);
2898                 } else {
2899                     i_ungetc(c1,f);
2900                 }
2901                 c1 = CR;
2902                 SEND;
2903             } else if (c1 == DEL && input_mode == X0208 ) {
2904                 /* CP5022x */
2905                 c2 = c1;
2906                 NEXT;
2907             } else 
2908                 SEND;
2909         }
2910         /* send: */
2911         switch(input_mode){
2912         case ASCII:
2913             switch ((*iconv)(c2, c1, c0)) {  /* can be EUC / SJIS / UTF-8 / UTF-16 */
2914             case -2:
2915                 /* 4 bytes UTF-8 */
2916                 if ((c0 = (*i_getc)(f)) != EOF) {
2917                     code_status(c0);
2918                     c0 <<= 8;
2919                     if ((c3 = (*i_getc)(f)) != EOF) {
2920                         code_status(c3);
2921                         (*iconv)(c2, c1, c0|c3);
2922                     }
2923                 }
2924                 break;
2925             case -1:
2926                 /* 3 bytes EUC or UTF-8 */
2927                 if ((c0 = (*i_getc)(f)) != EOF) {
2928                     code_status(c0);
2929                     (*iconv)(c2, c1, c0);
2930                 }
2931                 break;
2932             }
2933             break;
2934         case X0208:
2935         case X0213_1:
2936             if (ms_ucs_map_f == UCS_MAP_CP932 &&
2937                 0x7F <= c2 && c2 <= 0x92 &&
2938                 0x21 <= c1 && c1 <= 0x7E) {
2939                 /* CP932 UDC */
2940                 if(c1 == 0x7F) return 0;
2941                 c1 = (c2 - 0x7F) * 94 + c1 - 0x21 + 0xE000 + CLASS_UNICODE;
2942                 c2 = 0;
2943             }
2944             (*oconv)(c2, c1); /* this is JIS, not SJIS/EUC case */
2945             break;
2946 #ifdef X0212_ENABLE
2947         case X0212:
2948             (*oconv)(PREFIX_EUCG3 | c2, c1);
2949             break;
2950 #endif /* X0212_ENABLE */
2951         case X0213_2:
2952             (*oconv)(PREFIX_EUCG3 | c2, c1);
2953             break;
2954         default:
2955             (*oconv)(input_mode, c1);  /* other special case */
2956         }
2957
2958         c2 = 0;
2959         c0 = 0;
2960         continue;
2961         /* goto next_word */
2962     }
2963
2964     /* epilogue */
2965     (*iconv)(EOF, 0, 0);
2966     if (!is_inputcode_set)
2967     {
2968         if (is_8bit) {
2969             struct input_code *p = input_code_list;
2970             struct input_code *result = p;
2971             while (p->name){
2972                 if (p->score < result->score) result = p;
2973                 ++p;
2974             }
2975             set_input_codename(result->name);
2976         }
2977     }
2978     return 1;
2979 }
2980
2981 nkf_char
2982 h_conv(FILE *f, nkf_char c2, nkf_char c1)
2983 {
2984     nkf_char ret, c3, c0;
2985     int hold_index;
2986
2987
2988     /** it must NOT be in the kanji shifte sequence      */
2989     /** it must NOT be written in JIS7                   */
2990     /** and it must be after 2 byte 8bit code            */
2991
2992     hold_count = 0;
2993     push_hold_buf(c2);
2994     push_hold_buf(c1);
2995
2996     while ((c1 = (*i_getc)(f)) != EOF) {
2997         if (c1 == ESC){
2998             (*i_ungetc)(c1,f);
2999             break;
3000         }
3001         code_status(c1);
3002         if (push_hold_buf(c1) == EOF || estab_f){
3003             break;
3004         }
3005     }
3006
3007     if (!estab_f){
3008         struct input_code *p = input_code_list;
3009         struct input_code *result = p;
3010         if (c1 == EOF){
3011             code_status(c1);
3012         }
3013         while (p->name){
3014             if (p->score < result->score){
3015                 result = p;
3016             }
3017             ++p;
3018         }
3019         set_iconv(FALSE, result->iconv_func);
3020     }
3021
3022
3023     /** now,
3024      ** 1) EOF is detected, or
3025      ** 2) Code is established, or
3026      ** 3) Buffer is FULL (but last word is pushed)
3027      **
3028      ** in 1) and 3) cases, we continue to use
3029      ** Kanji codes by oconv and leave estab_f unchanged.
3030      **/
3031
3032     ret = c1;
3033     hold_index = 0;
3034     while (hold_index < hold_count){
3035         c2 = hold_buf[hold_index++];
3036         if (c2 <= DEL
3037 #ifdef NUMCHAR_OPTION
3038             || is_unicode_capsule(c2)
3039 #endif
3040             ){
3041             (*iconv)(0, c2, 0);
3042             continue;
3043         }else if (iconv == s_iconv && 0xa1 <= c2 && c2 <= 0xdf){
3044             (*iconv)(X0201, c2, 0);
3045             continue;
3046         }
3047         if (hold_index < hold_count){
3048             c1 = hold_buf[hold_index++];
3049         }else{
3050             c1 = (*i_getc)(f);
3051             if (c1 == EOF){
3052                 c3 = EOF;
3053                 break;
3054             }
3055             code_status(c1);
3056         }
3057         c0 = 0;
3058         switch ((*iconv)(c2, c1, 0)) {  /* can be EUC/SJIS/UTF-8 */
3059         case -2:
3060             /* 4 bytes UTF-8 */
3061             if (hold_index < hold_count){
3062                 c0 = hold_buf[hold_index++];
3063             } else if ((c0 = (*i_getc)(f)) == EOF) {
3064                 ret = EOF;
3065                 break;
3066             } else {
3067                 code_status(c0);
3068                 c0 <<= 8;
3069                 if (hold_index < hold_count){
3070                     c3 = hold_buf[hold_index++];
3071                 } else if ((c3 = (*i_getc)(f)) == EOF) {
3072                     c0 = ret = EOF;
3073                     break;
3074                 } else {
3075                     code_status(c3);
3076                     (*iconv)(c2, c1, c0|c3);
3077                 }
3078             }
3079             break;
3080         case -1:
3081             /* 3 bytes EUC or UTF-8 */
3082             if (hold_index < hold_count){
3083                 c0 = hold_buf[hold_index++];
3084             } else if ((c0 = (*i_getc)(f)) == EOF) {
3085                 ret = EOF;
3086                 break;
3087             } else {
3088                 code_status(c0);
3089             }
3090             (*iconv)(c2, c1, c0);
3091             break;
3092         }
3093         if (c0 == EOF) break;
3094     }
3095     return ret;
3096 }
3097
3098 nkf_char push_hold_buf(nkf_char c2)
3099 {
3100     if (hold_count >= HOLD_SIZE*2)
3101         return (EOF);
3102     hold_buf[hold_count++] = (unsigned char)c2;
3103     return ((hold_count >= HOLD_SIZE*2) ? EOF : hold_count);
3104 }
3105
3106 nkf_char s2e_conv(nkf_char c2, nkf_char c1, nkf_char *p2, nkf_char *p1)
3107 {
3108 #if defined(SHIFTJIS_CP932) || defined(X0212_ENABLE)
3109     nkf_char val;
3110 #endif
3111     static const nkf_char shift_jisx0213_s1a3_table[5][2] ={ { 1, 8}, { 3, 4}, { 5,12}, {13,14}, {15, 0} };
3112 #ifdef SHIFTJIS_CP932
3113     if (cp51932_f && is_ibmext_in_sjis(c2)){
3114 #if 0
3115         extern const unsigned short shiftjis_cp932[3][189];
3116 #endif
3117         val = shiftjis_cp932[c2 - CP932_TABLE_BEGIN][c1 - 0x40];
3118         if (val){
3119             c2 = val >> 8;
3120             c1 = val & 0xff;
3121         }
3122     }
3123 #endif /* SHIFTJIS_CP932 */
3124 #ifdef X0212_ENABLE
3125     if (!x0213_f && is_ibmext_in_sjis(c2)){
3126 #if 0
3127         extern const unsigned short shiftjis_x0212[3][189];
3128 #endif
3129         val = shiftjis_x0212[c2 - 0xfa][c1 - 0x40];
3130         if (val){
3131             if (val > 0x7FFF){
3132                 c2 = PREFIX_EUCG3 | (val >> 8);
3133                 c1 = val & 0xff;
3134             }else{
3135                 c2 = val >> 8;
3136                 c1 = val & 0xff;
3137             }
3138             if (p2) *p2 = c2;
3139             if (p1) *p1 = c1;
3140             return 0;
3141         }
3142     }
3143 #endif
3144     if(c2 >= 0x80){
3145         if(x0213_f && c2 >= 0xF0){
3146             if(c2 <= 0xF3 || (c2 == 0xF4 && c1 < 0x9F)){ /* k=1, 3<=k<=5, k=8, 12<=k<=15 */
3147                 c2 = PREFIX_EUCG3 | 0x20 | shift_jisx0213_s1a3_table[c2 - 0xF0][0x9E < c1];
3148             }else{ /* 78<=k<=94 */
3149                 c2 = PREFIX_EUCG3 | (c2 * 2 - 0x17B);
3150                 if (0x9E < c1) c2++;
3151             }
3152         }else{
3153             c2 = c2 + c2 - ((c2 <= 0x9F) ? SJ0162 : SJ6394);
3154             if (0x9E < c1) c2++;
3155         }
3156         if (c1 < 0x9F)
3157             c1 = c1 - ((c1 > DEL) ? SPACE : 0x1F);
3158         else {
3159             c1 = c1 - 0x7E;
3160         }
3161     }
3162
3163 #ifdef X0212_ENABLE
3164     c2 = x0212_unshift(c2);
3165 #endif
3166     if (p2) *p2 = c2;
3167     if (p1) *p1 = c1;
3168     return 0;
3169 }
3170
3171 nkf_char s_iconv(nkf_char c2, nkf_char c1, nkf_char c0)
3172 {
3173     if (c2 == X0201) {
3174         c1 &= 0x7f;
3175     } else if ((c2 == EOF) || (c2 == 0) || c2 < SPACE) {
3176         /* NOP */
3177     } else if (ms_ucs_map_f == UCS_MAP_CP932 &&
3178                0xF0 <= c2 && c2 <= 0xF9 &&
3179                0x40 <= c1 && c1 <= 0xFC) {
3180         /* CP932 UDC */
3181         if(c1 == 0x7F) return 0;
3182         c1 = (c2 - 0xF0) * 188 + (c1 - 0x40 - (0x7E < c1)) + 0xE000 + CLASS_UNICODE;
3183         c2 = 0;
3184     } else {
3185         nkf_char ret = s2e_conv(c2, c1, &c2, &c1);
3186         if (ret) return ret;
3187     }
3188     (*oconv)(c2, c1);
3189     return 0;
3190 }
3191
3192 nkf_char e_iconv(nkf_char c2, nkf_char c1, nkf_char c0)
3193 {
3194     if (c2 == X0201) {
3195         c1 &= 0x7f;
3196 #ifdef X0212_ENABLE
3197     }else if (c2 == 0x8f){
3198         if (c0 == 0){
3199             return -1;
3200         }
3201         if (ms_ucs_map_f == UCS_MAP_MS && 0xF5 <= c1 && c1 <= 0xFE && 0xA1 <= c0 && c0 <= 0xFE) {
3202             /* encoding is eucJP-ms, so invert to Unicode Private User Area */
3203             c1 = (c1 - 0xF5) * 94 + c0 - 0xA1 + 0xE3AC + CLASS_UNICODE;
3204             c2 = 0;
3205         } else {
3206             c2 = (c2 << 8) | (c1 & 0x7f);
3207             c1 = c0 & 0x7f;
3208 #ifdef SHIFTJIS_CP932
3209             if (cp51932_f){
3210                 nkf_char s2, s1;
3211                 if (e2s_conv(c2, c1, &s2, &s1) == 0){
3212                     s2e_conv(s2, s1, &c2, &c1);
3213                     if (c2 < 0x100){
3214                         c1 &= 0x7f;
3215                         c2 &= 0x7f;
3216                     }
3217                 }
3218             }
3219 #endif /* SHIFTJIS_CP932 */
3220         }
3221 #endif /* X0212_ENABLE */
3222     } else if (c2 == SSO){
3223         c2 = X0201;
3224         c1 &= 0x7f;
3225     } else if ((c2 == EOF) || (c2 == 0) || c2 < SPACE) {
3226         /* NOP */
3227     } else {
3228         if (ms_ucs_map_f == UCS_MAP_MS && 0xF5 <= c2 && c2 <= 0xFE && 0xA1 <= c1 && c1 <= 0xFE) {
3229             /* encoding is eucJP-ms, so invert to Unicode Private User Area */
3230             c1 = (c2 - 0xF5) * 94 + c1 - 0xA1 + 0xE000 + CLASS_UNICODE;
3231             c2 = 0;
3232         } else {
3233             c1 &= 0x7f;
3234             c2 &= 0x7f;
3235         }
3236     }
3237     (*oconv)(c2, c1);
3238     return 0;
3239 }
3240
3241 #ifdef UTF8_INPUT_ENABLE
3242 nkf_char w2e_conv(nkf_char c2, nkf_char c1, nkf_char c0, nkf_char *p2, nkf_char *p1)
3243 {
3244     nkf_char ret = 0;
3245
3246     if (!c1){
3247         *p2 = 0;
3248         *p1 = c2;
3249     }else if (0xc0 <= c2 && c2 <= 0xef) {
3250         ret =  unicode_to_jis_common(c2, c1, c0, p2, p1);
3251 #ifdef NUMCHAR_OPTION
3252         if (ret > 0){
3253             if (p2) *p2 = 0;
3254             if (p1) *p1 = CLASS_UNICODE | ww16_conv(c2, c1, c0);
3255             ret = 0;
3256         }
3257 #endif
3258     }
3259     return ret;
3260 }
3261
3262 nkf_char w_iconv(nkf_char c2, nkf_char c1, nkf_char c0)
3263 {
3264     nkf_char ret = 0;
3265     static const int w_iconv_utf8_1st_byte[] =
3266     { /* 0xC0 - 0xFF */
3267         20, 20, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
3268         21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
3269         30, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 32, 33, 33,
3270         40, 41, 41, 41, 42, 43, 43, 43, 50, 50, 50, 50, 60, 60, 70, 70};
3271     
3272     if (c2 < 0 || 0xff < c2) {
3273     }else if (c2 == 0) { /* 0 : 1 byte*/
3274         c0 = 0;
3275     } else if ((c2 & 0xc0) == 0x80) { /* 0x80-0xbf : trail byte */
3276         return 0;
3277     } else{
3278         switch (w_iconv_utf8_1st_byte[c2 - 0xC0]) {
3279         case 21:
3280             if (c1 < 0x80 || 0xBF < c1) return 0;
3281             break;
3282         case 30:
3283             if (c0 == 0) return -1;
3284             if (c1 < 0xA0 || 0xBF < c1 || (c0 & 0xc0) != 0x80)
3285                 return 0;
3286             break;
3287         case 31:
3288         case 33:
3289             if (c0 == 0) return -1;
3290             if ((c1 & 0xc0) != 0x80 || (c0 & 0xc0) != 0x80)
3291                 return 0;
3292             break;
3293         case 32:
3294             if (c0 == 0) return -1;
3295             if (c1 < 0x80 || 0x9F < c1 || (c0 & 0xc0) != 0x80)
3296                 return 0;
3297             break;
3298         case 40:
3299             if (c0 == 0) return -2;
3300             if (c1 < 0x90 || 0xBF < c1 || (c0 & 0xc0c0) != 0x8080)
3301                 return 0;
3302             break;
3303         case 41:
3304             if (c0 == 0) return -2;
3305             if (c1 < 0x80 || 0xBF < c1 || (c0 & 0xc0c0) != 0x8080)
3306                 return 0;
3307             break;
3308         case 42:
3309             if (c0 == 0) return -2;
3310             if (c1 < 0x80 || 0x8F < c1 || (c0 & 0xc0c0) != 0x8080)
3311                 return 0;
3312             break;
3313         default:
3314             return 0;
3315             break;
3316         }
3317     }
3318     if (c2 == 0 || c2 == EOF){
3319     } else if ((c2 & 0xf8) == 0xf0) { /* 4 bytes */
3320         c1 = CLASS_UNICODE | ww16_conv(c2, c1, c0);
3321         c2 = 0;
3322     } else {
3323         ret = w2e_conv(c2, c1, c0, &c2, &c1);
3324     }
3325     if (ret == 0){
3326         (*oconv)(c2, c1);
3327     }
3328     return ret;
3329 }
3330 #endif
3331
3332 #if defined(UTF8_INPUT_ENABLE) || defined(UTF8_OUTPUT_ENABLE)
3333 void w16w_conv(nkf_char val, nkf_char *p2, nkf_char *p1, nkf_char *p0)
3334 {
3335     val &= VALUE_MASK;
3336     if (val < 0x80){
3337         *p2 = val;
3338         *p1 = 0;
3339         *p0 = 0;
3340     }else if (val < 0x800){
3341         *p2 = 0xc0 | (val >> 6);
3342         *p1 = 0x80 | (val & 0x3f);
3343         *p0 = 0;
3344     } else if (val <= NKF_INT32_C(0xFFFF)) {
3345         *p2 = 0xe0 | (val >> 12);
3346         *p1 = 0x80 | ((val >> 6) & 0x3f);
3347         *p0 = 0x80 | (val        & 0x3f);
3348     } else if (val <= NKF_INT32_C(0x10FFFF)) {
3349         *p2 = 0xe0 |  (val >> 16);
3350         *p1 = 0x80 | ((val >> 12) & 0x3f);
3351         *p0 = 0x8080 | ((val << 2) & 0x3f00)| (val & 0x3f);
3352     } else {
3353         *p2 = 0;
3354         *p1 = 0;
3355         *p0 = 0;
3356     }
3357 }
3358 #endif
3359
3360 #ifdef UTF8_INPUT_ENABLE
3361 nkf_char ww16_conv(nkf_char c2, nkf_char c1, nkf_char c0)
3362 {
3363     nkf_char val;
3364     if (c2 >= 0xf8) {
3365         val = -1;
3366     } else if (c2 >= 0xf0){
3367         /* c2: 1st, c1: 2nd, c0: 3rd/4th */
3368         val = (c2 & 0x0f) << 18;
3369         val |= (c1 & 0x3f) << 12;
3370         val |= (c0 & 0x3f00) >> 2;
3371         val |= (c0 & 0x3f);
3372     }else if (c2 >= 0xe0){
3373         val = (c2 & 0x0f) << 12;
3374         val |= (c1 & 0x3f) << 6;
3375         val |= (c0 & 0x3f);
3376     }else if (c2 >= 0xc0){
3377         val = (c2 & 0x1f) << 6;
3378         val |= (c1 & 0x3f);
3379     }else{
3380         val = c2;
3381     }
3382     return val;
3383 }
3384
3385 nkf_char w16e_conv(nkf_char val, nkf_char *p2, nkf_char *p1)
3386 {
3387     nkf_char c2, c1, c0;
3388     nkf_char ret = 0;
3389     val &= VALUE_MASK;
3390     if (val < 0x80){
3391         *p2 = 0;
3392         *p1 = val;
3393     }else{
3394         w16w_conv(val, &c2, &c1, &c0);
3395         ret =  unicode_to_jis_common(c2, c1, c0, p2, p1);
3396 #ifdef NUMCHAR_OPTION
3397         if (ret > 0){
3398             *p2 = 0;
3399             *p1 = CLASS_UNICODE | val;
3400             ret = 0;
3401         }
3402 #endif
3403     }
3404     return ret;
3405 }
3406 #endif
3407
3408 #ifdef UTF8_INPUT_ENABLE
3409 nkf_char w_iconv16(nkf_char c2, nkf_char c1, nkf_char c0)
3410 {
3411     nkf_char ret = 0;
3412     if ((c2==0 && c1 < 0x80) || c2==EOF) {
3413         (*oconv)(c2, c1);
3414         return 0;
3415     }else if (0xD8 <= c2 && c2 <= 0xDB) {
3416         if (c0 < NKF_INT32_C(0xDC00) || NKF_INT32_C(0xDFFF) < c0)
3417             return -2;
3418         c1 =  CLASS_UNICODE | ((c2 << 18) + (c1 << 10) + c0 - NKF_INT32_C(0x35FDC00));
3419         c2 = 0;
3420     }else if ((c2>>3) == 27) { /* unpaired surrogate */
3421         /*
3422            return 2;
3423         */
3424         return 1;
3425     }else ret = w16e_conv(((c2 & 0xff)<<8) + c1, &c2, &c1);
3426     if (ret) return ret;
3427     (*oconv)(c2, c1);
3428     return 0;
3429 }
3430
3431 nkf_char w_iconv32(nkf_char c2, nkf_char c1, nkf_char c0)
3432 {
3433     int ret = 0;
3434
3435     if ((c2 == 0 && c1 < 0x80) || c2==EOF) {
3436     } else if (is_unicode_bmp(c1)) {
3437         ret = w16e_conv(c1, &c2, &c1);
3438     } else {
3439         c2 = 0;
3440         c1 =  CLASS_UNICODE | c1;
3441     }
3442     if (ret) return ret;
3443     (*oconv)(c2, c1);
3444     return 0;
3445 }
3446
3447 nkf_char unicode_to_jis_common(nkf_char c2, nkf_char c1, nkf_char c0, nkf_char *p2, nkf_char *p1)
3448 {
3449 #if 0
3450     extern const unsigned short *const utf8_to_euc_2bytes[];
3451     extern const unsigned short *const utf8_to_euc_2bytes_ms[];
3452     extern const unsigned short *const utf8_to_euc_2bytes_932[];
3453     extern const unsigned short *const *const utf8_to_euc_3bytes[];
3454     extern const unsigned short *const *const utf8_to_euc_3bytes_ms[];
3455     extern const unsigned short *const *const utf8_to_euc_3bytes_932[];
3456 #endif
3457     const unsigned short *const *pp;
3458     const unsigned short *const *const *ppp;
3459     static const int no_best_fit_chars_table_C2[] =
3460     {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
3461         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
3462         1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 2, 1, 1, 2,
3463         0, 0, 1, 1, 0, 1, 0, 1, 2, 1, 1, 1, 1, 1, 1, 1};
3464     static const int no_best_fit_chars_table_C2_ms[] =
3465     {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
3466         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
3467         1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0,
3468         0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0};
3469     static const int no_best_fit_chars_table_932_C2[] =
3470     {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
3471         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
3472         1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1,
3473         0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0};
3474     static const int no_best_fit_chars_table_932_C3[] =
3475     {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
3476         1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1,
3477         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
3478         1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1};
3479     nkf_char ret = 0;
3480
3481     if(c2 < 0x80){
3482         *p2 = 0;
3483         *p1 = c2;
3484     }else if(c2 < 0xe0){
3485         if(no_best_fit_chars_f){
3486             if(ms_ucs_map_f == UCS_MAP_CP932){
3487                 switch(c2){
3488                 case 0xC2:
3489                     if(no_best_fit_chars_table_932_C2[c1&0x3F]) return 1;
3490                     break;
3491                 case 0xC3:
3492                     if(no_best_fit_chars_table_932_C3[c1&0x3F]) return 1;
3493                     break;
3494                 }
3495             }else if(cp51932_f){
3496                 switch(c2){
3497                 case 0xC2:
3498                     if(no_best_fit_chars_table_C2[c1&0x3F]) return 1;
3499                     break;
3500                 case 0xC3:
3501                     if(no_best_fit_chars_table_932_C3[c1&0x3F]) return 1;
3502                     break;
3503                 }
3504             }else if(ms_ucs_map_f == UCS_MAP_MS){
3505                 if(c2 == 0xC2 && no_best_fit_chars_table_C2_ms[c1&0x3F]) return 1;
3506             }
3507         }
3508         pp =
3509             ms_ucs_map_f == UCS_MAP_CP932 ? utf8_to_euc_2bytes_932 :
3510             ms_ucs_map_f == UCS_MAP_MS ? utf8_to_euc_2bytes_ms :
3511             utf8_to_euc_2bytes;
3512         ret =  w_iconv_common(c2, c1, pp, sizeof_utf8_to_euc_2bytes, p2, p1);
3513     }else if(c0 < 0xF0){
3514         if(no_best_fit_chars_f){
3515             if(ms_ucs_map_f == UCS_MAP_CP932){
3516                 if(c2 == 0xE3 && c1 == 0x82 && c0 == 0x94) return 1;
3517             }else if(ms_ucs_map_f == UCS_MAP_MS){
3518                 switch(c2){
3519                 case 0xE2:
3520                     switch(c1){
3521                     case 0x80:
3522                         if(c0 == 0x94 || c0 == 0x96 || c0 == 0xBE) return 1;
3523                         break;
3524                     case 0x88:
3525                         if(c0 == 0x92) return 1;
3526                         break;
3527                     }
3528                     break;
3529                 case 0xE3:
3530                     if(c1 == 0x80 || c0 == 0x9C) return 1;
3531                     break;
3532                 }
3533             }else{
3534                 switch(c2){
3535                 case 0xE2:
3536                     switch(c1){
3537                     case 0x80:
3538                         if(c0 == 0x95) return 1;
3539                         break;
3540                     case 0x88:
3541                         if(c0 == 0xA5) return 1;
3542                         break;
3543                     }
3544                     break;
3545                 case 0xEF:
3546                     switch(c1){
3547                     case 0xBC:
3548                         if(c0 == 0x8D) return 1;
3549                         break;
3550                     case 0xBD:
3551                         if(c0 == 0x9E && cp51932_f) return 1;
3552                         break;
3553                     case 0xBF:
3554                         if(0xA0 <= c0 && c0 <= 0xA5) return 1;
3555                         break;
3556                     }
3557                     break;
3558                 }
3559             }
3560         }
3561         ppp =
3562             ms_ucs_map_f == UCS_MAP_CP932 ? utf8_to_euc_3bytes_932 :
3563             ms_ucs_map_f == UCS_MAP_MS ? utf8_to_euc_3bytes_ms :
3564             utf8_to_euc_3bytes;
3565         ret = w_iconv_common(c1, c0, ppp[c2 - 0xE0], sizeof_utf8_to_euc_C2, p2, p1);
3566     }else return -1;
3567 #ifdef SHIFTJIS_CP932
3568     if (!ret && cp51932_f && is_eucg3(*p2)) {
3569         nkf_char s2, s1;
3570         if (e2s_conv(*p2, *p1, &s2, &s1) == 0) {
3571             s2e_conv(s2, s1, p2, p1);
3572         }else{
3573             ret = 1;
3574         }
3575     }
3576 #endif
3577     return ret;
3578 }
3579
3580 nkf_char w_iconv_common(nkf_char c1, nkf_char c0, const unsigned short *const *pp, nkf_char psize, nkf_char *p2, nkf_char *p1)
3581 {
3582     nkf_char c2;
3583     const unsigned short *p;
3584     unsigned short val;
3585
3586     if (pp == 0) return 1;
3587
3588     c1 -= 0x80;
3589     if (c1 < 0 || psize <= c1) return 1;
3590     p = pp[c1];
3591     if (p == 0)  return 1;
3592
3593     c0 -= 0x80;
3594     if (c0 < 0 || sizeof_utf8_to_euc_C2 <= c0) return 1;
3595     val = p[c0];
3596     if (val == 0) return 1;
3597     if (no_cp932ext_f && (
3598         (val>>8) == 0x2D || /* NEC special characters */
3599         val > NKF_INT32_C(0xF300) /* IBM extended characters */
3600         )) return 1;
3601
3602     c2 = val >> 8;
3603    if (val > 0x7FFF){
3604         c2 &= 0x7f;
3605         c2 |= PREFIX_EUCG3;
3606     }
3607     if (c2 == SO) c2 = X0201;
3608     c1 = val & 0x7f;
3609     if (p2) *p2 = c2;
3610     if (p1) *p1 = c1;
3611     return 0;
3612 }
3613
3614 void nkf_each_char_to_hex(void (*f)(nkf_char c2,nkf_char c1), nkf_char c)
3615 {
3616     const char *hex = "0123456789ABCDEF";
3617     int shift = 20;
3618     c &= VALUE_MASK;
3619     while(shift >= 0){
3620         if(c >= 1<<shift){
3621             while(shift >= 0){
3622                 (*f)(0, hex[(c>>shift)&0xF]);
3623                 shift -= 4;
3624             }
3625         }else{
3626             shift -= 4;
3627         }
3628     }
3629     return;
3630 }
3631
3632 void encode_fallback_html(nkf_char c)
3633 {
3634     (*oconv)(0, '&');
3635     (*oconv)(0, '#');
3636     c &= VALUE_MASK;
3637     if(c >= NKF_INT32_C(1000000))
3638         (*oconv)(0, 0x30+(c/NKF_INT32_C(1000000))%10);
3639     if(c >= NKF_INT32_C(100000))
3640         (*oconv)(0, 0x30+(c/NKF_INT32_C(100000) )%10);
3641     if(c >= 10000)
3642         (*oconv)(0, 0x30+(c/10000  )%10);
3643     if(c >= 1000)
3644         (*oconv)(0, 0x30+(c/1000   )%10);
3645     if(c >= 100)
3646         (*oconv)(0, 0x30+(c/100    )%10);
3647     if(c >= 10)
3648         (*oconv)(0, 0x30+(c/10     )%10);
3649     if(c >= 0)
3650         (*oconv)(0, 0x30+ c         %10);
3651     (*oconv)(0, ';');
3652     return;
3653 }
3654
3655 void encode_fallback_xml(nkf_char c)
3656 {
3657     (*oconv)(0, '&');
3658     (*oconv)(0, '#');
3659     (*oconv)(0, 'x');
3660     nkf_each_char_to_hex(oconv, c);
3661     (*oconv)(0, ';');
3662     return;
3663 }
3664
3665 void encode_fallback_java(nkf_char c)
3666 {
3667     const char *hex = "0123456789ABCDEF";
3668     (*oconv)(0, '\\');
3669     c &= VALUE_MASK;
3670     if(!is_unicode_bmp(c)){
3671         (*oconv)(0, 'U');
3672         (*oconv)(0, '0');
3673         (*oconv)(0, '0');
3674         (*oconv)(0, hex[(c>>20)&0xF]);
3675         (*oconv)(0, hex[(c>>16)&0xF]);
3676     }else{
3677         (*oconv)(0, 'u');
3678     }
3679     (*oconv)(0, hex[(c>>12)&0xF]);
3680     (*oconv)(0, hex[(c>> 8)&0xF]);
3681     (*oconv)(0, hex[(c>> 4)&0xF]);
3682     (*oconv)(0, hex[ c     &0xF]);
3683     return;
3684 }
3685
3686 void encode_fallback_perl(nkf_char c)
3687 {
3688     (*oconv)(0, '\\');
3689     (*oconv)(0, 'x');
3690     (*oconv)(0, '{');
3691     nkf_each_char_to_hex(oconv, c);
3692     (*oconv)(0, '}');
3693     return;
3694 }
3695
3696 void encode_fallback_subchar(nkf_char c)
3697 {
3698     c = unicode_subchar;
3699     (*oconv)((c>>8)&0xFF, c&0xFF);
3700     return;
3701 }
3702 #endif
3703
3704 #ifdef UTF8_OUTPUT_ENABLE
3705 nkf_char e2w_conv(nkf_char c2, nkf_char c1)
3706 {
3707 #if 0
3708     extern const unsigned short euc_to_utf8_1byte[];
3709     extern const unsigned short *const euc_to_utf8_2bytes[];
3710     extern const unsigned short *const euc_to_utf8_2bytes_ms[];
3711     extern const unsigned short *const x0212_to_utf8_2bytes[];
3712 #endif
3713     const unsigned short *p;
3714
3715     if (c2 == X0201) {
3716         p = euc_to_utf8_1byte;
3717 #ifdef X0212_ENABLE
3718     } else if (is_eucg3(c2)){
3719         if(ms_ucs_map_f == UCS_MAP_ASCII&& c2 == NKF_INT32_C(0x8F22) && c1 == 0x43){
3720             return 0xA6;
3721         }
3722         c2 = (c2&0x7f) - 0x21;
3723         if (0<=c2 && c2<sizeof_euc_to_utf8_2bytes)
3724             p = x0212_to_utf8_2bytes[c2];
3725         else
3726             return 0;
3727 #endif
3728     } else {
3729         c2 &= 0x7f;
3730         c2 = (c2&0x7f) - 0x21;
3731         if (0<=c2 && c2<sizeof_euc_to_utf8_2bytes)
3732             p = ms_ucs_map_f != UCS_MAP_ASCII ? euc_to_utf8_2bytes_ms[c2] : euc_to_utf8_2bytes[c2];
3733         else
3734             return 0;
3735     }
3736     if (!p) return 0;
3737     c1 = (c1 & 0x7f) - 0x21;
3738     if (0<=c1 && c1<sizeof_euc_to_utf8_1byte)
3739         return p[c1];
3740     return 0;
3741 }
3742
3743 void w_oconv(nkf_char c2, nkf_char c1)
3744 {
3745     nkf_char c0;
3746     nkf_char val;
3747
3748     if (output_bom_f) {
3749         output_bom_f = FALSE;
3750         (*o_putc)('\357');
3751         (*o_putc)('\273');
3752         (*o_putc)('\277');
3753     }
3754
3755     if (c2 == EOF) {
3756         (*o_putc)(EOF);
3757         return;
3758     }
3759
3760 #ifdef NUMCHAR_OPTION
3761     if (c2 == 0 && is_unicode_capsule(c1)){
3762         val = c1 & VALUE_MASK;
3763         if (val < 0x80){
3764             (*o_putc)(val);
3765         }else if (val < 0x800){
3766             (*o_putc)(0xC0 | (val >> 6));
3767             (*o_putc)(0x80 | (val & 0x3f));
3768         } else if (val <= NKF_INT32_C(0xFFFF)) {
3769             (*o_putc)(0xE0 | (val >> 12));
3770             (*o_putc)(0x80 | ((val >> 6) & 0x3f));
3771             (*o_putc)(0x80 | (val        & 0x3f));
3772         } else if (val <= NKF_INT32_C(0x10FFFF)) {
3773             (*o_putc)(0xF0 | ( val>>18));
3774             (*o_putc)(0x80 | ((val>>12) & 0x3f));
3775             (*o_putc)(0x80 | ((val>> 6) & 0x3f));
3776             (*o_putc)(0x80 | ( val      & 0x3f));
3777         }
3778         return;
3779     }
3780 #endif
3781
3782     if (c2 == 0) { 
3783         output_mode = ASCII;
3784         (*o_putc)(c1);
3785     } else if (c2 == ISO8859_1) {
3786         output_mode = ISO8859_1;
3787         (*o_putc)(c1 | 0x080);
3788     } else {
3789         output_mode = UTF8;
3790         val = e2w_conv(c2, c1);
3791         if (val){
3792             w16w_conv(val, &c2, &c1, &c0);
3793             (*o_putc)(c2);
3794             if (c1){
3795                 (*o_putc)(c1);
3796                 if (c0) (*o_putc)(c0);
3797             }
3798         }
3799     }
3800 }
3801
3802 void w_oconv16(nkf_char c2, nkf_char c1)
3803 {
3804     if (output_bom_f) {
3805         output_bom_f = FALSE;
3806         if (output_endian == ENDIAN_LITTLE){
3807             (*o_putc)((unsigned char)'\377');
3808             (*o_putc)('\376');
3809         }else{
3810             (*o_putc)('\376');
3811             (*o_putc)((unsigned char)'\377');
3812         }
3813     }
3814
3815     if (c2 == EOF) {
3816         (*o_putc)(EOF);
3817         return;
3818     }
3819
3820     if (c2 == ISO8859_1) {
3821         c2 = 0;
3822         c1 |= 0x80;
3823 #ifdef NUMCHAR_OPTION
3824     } else if (c2 == 0 && is_unicode_capsule(c1)) {
3825         if (is_unicode_bmp(c1)) {
3826             c2 = (c1 >> 8) & 0xff;
3827             c1 &= 0xff;
3828         } else {
3829             c1 &= VALUE_MASK;
3830             if (c1 <= UNICODE_MAX) {
3831                 c2 = (c1 >> 10) + NKF_INT32_C(0xD7C0);   /* high surrogate */
3832                 c1 = (c1 & 0x3FF) + NKF_INT32_C(0xDC00); /* low surrogate */
3833                 if (output_endian == ENDIAN_LITTLE){
3834                     (*o_putc)(c2 & 0xff);
3835                     (*o_putc)((c2 >> 8) & 0xff);
3836                     (*o_putc)(c1 & 0xff);
3837                     (*o_putc)((c1 >> 8) & 0xff);
3838                 }else{
3839                     (*o_putc)((c2 >> 8) & 0xff);
3840                     (*o_putc)(c2 & 0xff);
3841                     (*o_putc)((c1 >> 8) & 0xff);
3842                     (*o_putc)(c1 & 0xff);
3843                 }
3844             }
3845             return;
3846         }
3847 #endif
3848     } else if (c2) {
3849         nkf_char val = e2w_conv(c2, c1);
3850         c2 = (val >> 8) & 0xff;
3851         c1 = val & 0xff;
3852         if (!val) return;
3853     }
3854     if (output_endian == ENDIAN_LITTLE){
3855         (*o_putc)(c1);
3856         (*o_putc)(c2);
3857     }else{
3858         (*o_putc)(c2);
3859         (*o_putc)(c1);
3860     }
3861 }
3862
3863 void w_oconv32(nkf_char c2, nkf_char c1)
3864 {
3865     if (output_bom_f) {
3866         output_bom_f = FALSE;
3867         if (output_endian == ENDIAN_LITTLE){
3868             (*o_putc)((unsigned char)'\377');
3869             (*o_putc)('\376');
3870             (*o_putc)('\000');
3871             (*o_putc)('\000');
3872         }else{
3873             (*o_putc)('\000');
3874             (*o_putc)('\000');
3875             (*o_putc)('\376');
3876             (*o_putc)((unsigned char)'\377');
3877         }
3878     }
3879
3880     if (c2 == EOF) {
3881         (*o_putc)(EOF);
3882         return;
3883     }
3884
3885     if (c2 == ISO8859_1) {
3886         c1 |= 0x80;
3887 #ifdef NUMCHAR_OPTION
3888     } else if (c2 == 0 && is_unicode_capsule(c1)) {
3889         c1 &= VALUE_MASK;
3890 #endif
3891     } else if (c2) {
3892         c1 = e2w_conv(c2, c1);
3893         if (!c1) return;
3894     }
3895     if (output_endian == ENDIAN_LITTLE){
3896         (*o_putc)( c1 & NKF_INT32_C(0x000000FF));
3897         (*o_putc)((c1 & NKF_INT32_C(0x0000FF00)) >>  8);
3898         (*o_putc)((c1 & NKF_INT32_C(0x00FF0000)) >> 16);
3899         (*o_putc)('\000');
3900     }else{
3901         (*o_putc)('\000');
3902         (*o_putc)((c1 & NKF_INT32_C(0x00FF0000)) >> 16);
3903         (*o_putc)((c1 & NKF_INT32_C(0x0000FF00)) >>  8);
3904         (*o_putc)( c1 & NKF_INT32_C(0x000000FF));
3905     }
3906 }
3907 #endif
3908
3909 void e_oconv(nkf_char c2, nkf_char c1)
3910 {
3911 #ifdef NUMCHAR_OPTION
3912     if (c2 == 0 && is_unicode_capsule(c1)){
3913         w16e_conv(c1, &c2, &c1);
3914         if (c2 == 0 && is_unicode_capsule(c1)){
3915             c2 = c1 & VALUE_MASK;
3916             if (ms_ucs_map_f == UCS_MAP_MS &&
3917                 0xE000 <= c2 && c2 <= 0xE757) {
3918                 /* eucJP-ms UDC */
3919                 c1 &= 0xFFF;
3920                 c2 = c1 / 94;
3921                 c2 += c2 < 10 ? 0x75 : 0x8FEB;
3922                 c1 = 0x21 + c1 % 94;
3923             } else {
3924                 if (encode_fallback) (*encode_fallback)(c1);
3925                 return;
3926             }
3927         }
3928     }
3929 #endif
3930     if (c2 == EOF) {
3931         (*o_putc)(EOF);
3932         return;
3933     } else if (c2 == 0) { 
3934         output_mode = ASCII;
3935         (*o_putc)(c1);
3936     } else if (c2 == X0201) {
3937         output_mode = JAPANESE_EUC;
3938         (*o_putc)(SSO); (*o_putc)(c1|0x80);
3939     } else if (c2 == ISO8859_1) {
3940         output_mode = ISO8859_1;
3941         (*o_putc)(c1 | 0x080);
3942 #ifdef X0212_ENABLE
3943     } else if (is_eucg3(c2)){
3944         output_mode = JAPANESE_EUC;
3945 #ifdef SHIFTJIS_CP932
3946         if (cp51932_f){
3947             nkf_char s2, s1;
3948             if (e2s_conv(c2, c1, &s2, &s1) == 0){
3949                 s2e_conv(s2, s1, &c2, &c1);
3950             }
3951         }
3952 #endif
3953         if (c2 == 0) {
3954             output_mode = ASCII;
3955             (*o_putc)(c1);
3956         }else if (is_eucg3(c2)){
3957             if (x0212_f){
3958                 (*o_putc)(0x8f);
3959                 (*o_putc)((c2 & 0x7f) | 0x080);
3960                 (*o_putc)(c1 | 0x080);
3961             }
3962         }else{
3963             (*o_putc)((c2 & 0x7f) | 0x080);
3964             (*o_putc)(c1 | 0x080);
3965         }
3966 #endif
3967     } else {
3968         if (!nkf_isgraph(c1) || !nkf_isgraph(c2)) {
3969             set_iconv(FALSE, 0);
3970             return; /* too late to rescue this char */
3971         }
3972         output_mode = JAPANESE_EUC;
3973         (*o_putc)(c2 | 0x080);
3974         (*o_putc)(c1 | 0x080);
3975     }
3976 }
3977
3978 #ifdef X0212_ENABLE
3979 nkf_char x0212_shift(nkf_char c)
3980 {
3981     nkf_char ret = c;
3982     c &= 0x7f;
3983     if (is_eucg3(ret)){
3984         if (0x75 <= c && c <= 0x7f){
3985             ret = c + (0x109 - 0x75);
3986         }
3987     }else{
3988         if (0x75 <= c && c <= 0x7f){
3989             ret = c + (0x113 - 0x75);
3990         }
3991     }
3992     return ret;
3993 }
3994
3995
3996 nkf_char x0212_unshift(nkf_char c)
3997 {
3998     nkf_char ret = c;
3999     if (0x7f <= c && c <= 0x88){
4000         ret = c + (0x75 - 0x7f);
4001     }else if (0x89 <= c && c <= 0x92){
4002         ret = PREFIX_EUCG3 | 0x80 | (c + (0x75 - 0x89));
4003     }
4004     return ret;
4005 }
4006 #endif /* X0212_ENABLE */
4007
4008 nkf_char e2s_conv(nkf_char c2, nkf_char c1, nkf_char *p2, nkf_char *p1)
4009 {
4010     nkf_char ndx;
4011     if (is_eucg3(c2)){
4012         ndx = c2 & 0xff;
4013         if (x0213_f){
4014             if((0x21 <= ndx && ndx <= 0x2F)){
4015                 if (p2) *p2 = ((ndx - 1) >> 1) + 0xec - ndx / 8 * 3;
4016                 if (p1) *p1 = c1 + ((ndx & 1) ? ((c1 < 0x60) ? 0x1f : 0x20) : 0x7e);
4017                 return 0;
4018             }else if(0x6E <= ndx && ndx <= 0x7E){
4019                 if (p2) *p2 = ((ndx - 1) >> 1) + 0xbe;
4020                 if (p1) *p1 = c1 + ((ndx & 1) ? ((c1 < 0x60) ? 0x1f : 0x20) : 0x7e);
4021                 return 0;
4022             }
4023             return 1;
4024         }
4025 #ifdef X0212_ENABLE
4026         else if(nkf_isgraph(ndx)){
4027             nkf_char val = 0;
4028             const unsigned short *ptr;
4029 #if 0
4030             extern const unsigned short *const x0212_shiftjis[];
4031 #endif
4032             ptr = x0212_shiftjis[ndx - 0x21];
4033             if (ptr){
4034                 val = ptr[(c1 & 0x7f) - 0x21];
4035             }
4036             if (val){
4037                 c2 = val >> 8;
4038                 c1 = val & 0xff;
4039                 if (p2) *p2 = c2;
4040                 if (p1) *p1 = c1;
4041                 return 0;
4042             }
4043             c2 = x0212_shift(c2);
4044         }
4045 #endif /* X0212_ENABLE */
4046     }
4047     if(0x7F < c2) return 1;
4048     if (p2) *p2 = ((c2 - 1) >> 1) + ((c2 <= 0x5e) ? 0x71 : 0xb1);
4049     if (p1) *p1 = c1 + ((c2 & 1) ? ((c1 < 0x60) ? 0x1f : 0x20) : 0x7e);
4050     return 0;
4051 }
4052
4053 void s_oconv(nkf_char c2, nkf_char c1)
4054 {
4055 #ifdef NUMCHAR_OPTION
4056     if (c2 == 0 && is_unicode_capsule(c1)){
4057         w16e_conv(c1, &c2, &c1);
4058         if (c2 == 0 && is_unicode_capsule(c1)){
4059             c2 = c1 & VALUE_MASK;
4060             if (ms_ucs_map_f == UCS_MAP_CP932 &&
4061                 0xE000 <= c2 && c2 <= 0xE757) {
4062                 /* CP932 UDC */
4063                 c1 &= 0xFFF;
4064                 c2 = c1 / 188 + 0xF0;
4065                 c1 = c1 % 188;
4066                 c1 += 0x40 + (c1 > 0x3e);
4067             } else {
4068                 if(encode_fallback)(*encode_fallback)(c1);
4069                 return;
4070             }
4071         }
4072     }
4073 #endif
4074     if (c2 == EOF) {
4075         (*o_putc)(EOF);
4076         return;
4077     } else if (c2 == 0) {
4078         output_mode = ASCII;
4079         (*o_putc)(c1);
4080     } else if (c2 == X0201) {
4081         output_mode = SHIFT_JIS;
4082         (*o_putc)(c1|0x80);
4083     } else if (c2 == ISO8859_1) {
4084         output_mode = ISO8859_1;
4085         (*o_putc)(c1 | 0x080);
4086 #ifdef X0212_ENABLE
4087     } else if (is_eucg3(c2)){
4088         output_mode = SHIFT_JIS;
4089         if (e2s_conv(c2, c1, &c2, &c1) == 0){
4090             (*o_putc)(c2);
4091             (*o_putc)(c1);
4092         }
4093 #endif
4094     } else {
4095         if (!nkf_isprint(c1) || !nkf_isprint(c2)) {
4096             set_iconv(FALSE, 0);
4097             return; /* too late to rescue this char */
4098         }
4099         output_mode = SHIFT_JIS;
4100         e2s_conv(c2, c1, &c2, &c1);
4101
4102 #ifdef SHIFTJIS_CP932
4103         if (cp932inv_f
4104             && CP932INV_TABLE_BEGIN <= c2 && c2 <= CP932INV_TABLE_END){
4105 #if 0
4106             extern const unsigned short cp932inv[2][189];
4107 #endif
4108             nkf_char c = cp932inv[c2 - CP932INV_TABLE_BEGIN][c1 - 0x40];
4109             if (c){
4110                 c2 = c >> 8;
4111                 c1 = c & 0xff;
4112             }
4113         }
4114 #endif /* SHIFTJIS_CP932 */
4115
4116         (*o_putc)(c2);
4117         if (prefix_table[(unsigned char)c1]){
4118             (*o_putc)(prefix_table[(unsigned char)c1]);
4119         }
4120         (*o_putc)(c1);
4121     }
4122 }
4123
4124 void j_oconv(nkf_char c2, nkf_char c1)
4125 {
4126 #ifdef NUMCHAR_OPTION
4127     if (c2 == 0 && is_unicode_capsule(c1)){
4128         w16e_conv(c1, &c2, &c1);
4129         if (c2 == 0 && is_unicode_capsule(c1)){
4130             c2 = c1 & VALUE_MASK;
4131             if (ms_ucs_map_f == UCS_MAP_CP932 &&
4132                 0xE000 <= c2 && c2 <= 0xE757) {
4133                 /* CP5022x UDC */
4134                 c1 &= 0xFFF;
4135                 c2 = 0x7F + c1 / 94;
4136                 c1 = 0x21 + c1 % 94;
4137             } else {
4138                 if (encode_fallback) (*encode_fallback)(c1);
4139                 return;
4140             }
4141         }
4142     }
4143 #endif
4144     if (c2 == EOF) {
4145         if (output_mode !=ASCII && output_mode!=ISO8859_1) {
4146             (*o_putc)(ESC);
4147             (*o_putc)('(');
4148             (*o_putc)(ascii_intro);
4149             output_mode = ASCII;
4150         }
4151         (*o_putc)(EOF);
4152 #ifdef X0212_ENABLE
4153     } else if (is_eucg3(c2)){
4154         if(x0213_f){
4155             if(output_mode!=X0213_2){
4156                 output_mode = X0213_2;
4157                 (*o_putc)(ESC);
4158                 (*o_putc)('$');
4159                 (*o_putc)('(');
4160                 (*o_putc)(X0213_2&0x7F);
4161             }
4162         }else{
4163             if(output_mode!=X0212){
4164                 output_mode = X0212;
4165                 (*o_putc)(ESC);
4166                 (*o_putc)('$');
4167                 (*o_putc)('(');
4168                 (*o_putc)(X0212&0x7F);
4169             }
4170         }
4171         (*o_putc)(c2 & 0x7f);
4172         (*o_putc)(c1);
4173 #endif
4174     } else if (c2==X0201) {
4175         if (output_mode!=X0201) {
4176             output_mode = X0201;
4177             (*o_putc)(ESC);
4178             (*o_putc)('(');
4179             (*o_putc)('I');
4180         }
4181         (*o_putc)(c1);
4182     } else if (c2==ISO8859_1) {
4183             /* iso8859 introduction, or 8th bit on */
4184             /* Can we convert in 7bit form using ESC-'-'-A ? 
4185                Is this popular? */
4186         output_mode = ISO8859_1;
4187         (*o_putc)(c1|0x80);
4188     } else if (c2 == 0) {
4189         if (output_mode !=ASCII && output_mode!=ISO8859_1) {
4190             (*o_putc)(ESC);
4191             (*o_putc)('(');
4192             (*o_putc)(ascii_intro);
4193             output_mode = ASCII;
4194         }
4195         (*o_putc)(c1);
4196     } else {
4197         if(ms_ucs_map_f == UCS_MAP_CP932
4198            ? c2<0x20 || 0x92<c2 || c1<0x20 || 0x7e<c1
4199            : c2<0x20 || 0x7e<c2 || c1<0x20 || 0x7e<c1) return;
4200         if(x0213_f){
4201             if (output_mode!=X0213_1) {
4202                 output_mode = X0213_1;
4203                 (*o_putc)(ESC);
4204                 (*o_putc)('$');
4205                 (*o_putc)('(');
4206                 (*o_putc)(X0213_1&0x7F);
4207             }
4208         }else if (output_mode != X0208) {
4209             output_mode = X0208;
4210             (*o_putc)(ESC);
4211             (*o_putc)('$');
4212             (*o_putc)(kanji_intro);
4213         }
4214         (*o_putc)(c2);
4215         (*o_putc)(c1);
4216     }
4217 }
4218
4219 void base64_conv(nkf_char c2, nkf_char c1)
4220 {
4221     mime_prechar(c2, c1);
4222     (*o_base64conv)(c2,c1);
4223 }
4224
4225
4226 static nkf_char broken_buf[3];
4227 static int broken_counter = 0;
4228 static int broken_last = 0;
4229 nkf_char broken_getc(FILE *f)
4230 {
4231     nkf_char c,c1;
4232
4233     if (broken_counter>0) {
4234         return broken_buf[--broken_counter];
4235     }
4236     c= (*i_bgetc)(f);
4237     if (c=='$' && broken_last != ESC 
4238             && (input_mode==ASCII || input_mode==X0201)) {
4239         c1= (*i_bgetc)(f);
4240         broken_last = 0;
4241         if (c1=='@'|| c1=='B') {
4242             broken_buf[0]=c1; broken_buf[1]=c; 
4243             broken_counter=2;
4244             return ESC;
4245         } else {
4246             (*i_bungetc)(c1,f);
4247             return c;
4248         }
4249     } else if (c=='(' && broken_last != ESC 
4250             && (input_mode==X0208 || input_mode==X0201)) { /* ) */
4251         c1= (*i_bgetc)(f);
4252         broken_last = 0;
4253         if (c1=='J'|| c1=='B') {
4254             broken_buf[0]=c1; broken_buf[1]=c;
4255             broken_counter=2;
4256             return ESC;
4257         } else {
4258             (*i_bungetc)(c1,f);
4259             return c;
4260         }
4261     } else {
4262         broken_last = c;
4263         return c;
4264     }
4265 }
4266
4267 nkf_char broken_ungetc(nkf_char c, FILE *f)
4268 {
4269     if (broken_counter<2)
4270         broken_buf[broken_counter++]=c;
4271     return c;
4272 }
4273
4274 static nkf_char prev_cr = 0;
4275
4276 void cr_conv(nkf_char c2, nkf_char c1)
4277 {
4278     if (prev_cr) {
4279         prev_cr = 0;
4280         if (! (c2==0&&c1==NL) ) {
4281             cr_conv(0,'\n');
4282         }
4283     }
4284     if (c2) {
4285         (*o_crconv)(c2,c1);
4286     } else if (c1=='\r') {
4287         prev_cr = c1;
4288     } else if (c1=='\n') {
4289         if (crmode_f==CRLF) {
4290             (*o_crconv)(0,'\r');
4291         } else if (crmode_f==CR) {
4292             (*o_crconv)(0,'\r');
4293             return;
4294         } 
4295         (*o_crconv)(0,NL);
4296     } else if (c1!='\032' || crmode_f!=NL){
4297         (*o_crconv)(c2,c1);
4298     }
4299 }
4300
4301 /* 
4302   Return value of fold_conv()
4303
4304        \n  add newline  and output char
4305        \r  add newline  and output nothing
4306        ' ' space
4307        0   skip  
4308        1   (or else) normal output 
4309
4310   fold state in prev (previous character)
4311
4312       >0x80 Japanese (X0208/X0201)
4313       <0x80 ASCII
4314       \n    new line 
4315       ' '   space
4316
4317   This fold algorthm does not preserve heading space in a line.
4318   This is the main difference from fmt.
4319 */
4320
4321 #define char_size(c2,c1) (c2?2:1)
4322
4323 void fold_conv(nkf_char c2, nkf_char c1)
4324
4325     nkf_char prev0;
4326     nkf_char fold_state;
4327
4328     if (c1== '\r' && !fold_preserve_f) {
4329         fold_state=0;  /* ignore cr */
4330     }else if (c1== '\n'&&f_prev=='\r' && fold_preserve_f) {
4331         f_prev = '\n';
4332         fold_state=0;  /* ignore cr */
4333     } else if (c1== BS) {
4334         if (f_line>0) f_line--;
4335         fold_state =  1;
4336     } else if (c2==EOF && f_line != 0) {    /* close open last line */
4337             fold_state = '\n';
4338     } else if ((c1=='\n' && !fold_preserve_f)
4339                || ((c1=='\r'||(c1=='\n'&&f_prev!='\r'))
4340                    && fold_preserve_f)) {
4341         /* new line */
4342         if (fold_preserve_f) { 
4343             f_prev = c1;
4344             f_line = 0;
4345             fold_state =  '\r';
4346         } else if ((f_prev == c1 && !fold_preserve_f)
4347                    || (f_prev == '\n' && fold_preserve_f)
4348                    ) {        /* duplicate newline */
4349             if (f_line) {
4350                 f_line = 0;
4351                 fold_state =  '\n';    /* output two newline */
4352             } else {
4353                 f_line = 0;
4354                 fold_state =  1;
4355             }
4356         } else  {
4357             if (f_prev&0x80) {     /* Japanese? */
4358                 f_prev = c1;
4359                 fold_state =  0;       /* ignore given single newline */
4360             } else if (f_prev==' ') {
4361                 fold_state =  0;
4362             } else {
4363                 f_prev = c1;
4364                 if (++f_line<=fold_len) 
4365                     fold_state =  ' ';
4366                 else {
4367                     f_line = 0;
4368                     fold_state =  '\r';        /* fold and output nothing */
4369                 }
4370             }
4371         }
4372     } else if (c1=='\f') {
4373         f_prev = '\n';
4374         f_line = 0;
4375         fold_state =  '\n';            /* output newline and clear */
4376     } else if ( (c2==0  && c1==' ')||
4377                (c2==0  && c1=='\t')||
4378                (c2=='!'&& c1=='!')) {
4379         /* X0208 kankaku or ascii space */
4380             if (f_prev == ' ') {
4381                 fold_state = 0;         /* remove duplicate spaces */
4382             } else {
4383                 f_prev = ' ';    
4384                 if (++f_line<=fold_len) 
4385                     fold_state = ' ';         /* output ASCII space only */
4386                 else {
4387                     f_prev = ' '; f_line = 0;
4388                     fold_state = '\r';        /* fold and output nothing */
4389                 }
4390             }
4391     } else {
4392         prev0 = f_prev; /* we still need this one... , but almost done */
4393         f_prev = c1;
4394         if (c2 || c2==X0201) 
4395             f_prev |= 0x80;  /* this is Japanese */
4396         f_line += char_size(c2,c1);
4397         if (f_line<=fold_len) {   /* normal case */
4398             fold_state = 1;
4399         } else {
4400             if (f_line>fold_len+fold_margin) { /* too many kinsoku suspension */
4401                 f_line = char_size(c2,c1);
4402                 fold_state =  '\n';       /* We can't wait, do fold now */
4403             } else if (c2==X0201) {
4404             /* simple kinsoku rules  return 1 means no folding  */
4405                 if (c1==(0xde&0x7f)) fold_state = 1; /* \e$B!+\e(B*/
4406                 else if (c1==(0xdf&0x7f)) fold_state = 1; /* \e$B!,\e(B*/
4407                 else if (c1==(0xa4&0x7f)) fold_state = 1; /* \e$B!#\e(B*/
4408                 else if (c1==(0xa3&0x7f)) fold_state = 1; /* \e$B!$\e(B*/
4409                 else if (c1==(0xa1&0x7f)) fold_state = 1; /* \e$B!W\e(B*/
4410                 else if (c1==(0xb0&0x7f)) fold_state = 1; /* - */
4411                 else if (SPACE<=c1 && c1<=(0xdf&0x7f)) {      /* X0201 */
4412                     f_line = 1;
4413                     fold_state = '\n';/* add one new f_line before this character */
4414                 } else {
4415                     f_line = 1;
4416                     fold_state = '\n';/* add one new f_line before this character */
4417                 }
4418             } else if (c2==0) {
4419                 /* kinsoku point in ASCII */ 
4420                 if (  c1==')'||    /* { [ ( */
4421                      c1==']'||
4422                      c1=='}'||
4423                      c1=='.'||
4424                      c1==','||
4425                      c1=='!'||
4426                      c1=='?'||
4427                      c1=='/'||
4428                      c1==':'||
4429                      c1==';' ) {
4430                     fold_state = 1;
4431                 /* just after special */
4432                 } else if (!is_alnum(prev0)) {
4433                     f_line = char_size(c2,c1);
4434                     fold_state = '\n';
4435                 } else if ((prev0==' ') ||   /* ignored new f_line */
4436                       (prev0=='\n')||        /* ignored new f_line */
4437                       (prev0&0x80)) {        /* X0208 - ASCII */
4438                     f_line = char_size(c2,c1);
4439                     fold_state = '\n';/* add one new f_line before this character */
4440                 } else {
4441                     fold_state = 1;  /* default no fold in ASCII */
4442                 }
4443             } else {
4444                 if (c2=='!') {
4445                     if (c1=='"')  fold_state = 1; /* \e$B!"\e(B */
4446                     else if (c1=='#')  fold_state = 1; /* \e$B!#\e(B */
4447                     else if (c1=='W')  fold_state = 1; /* \e$B!W\e(B */
4448                     else if (c1=='K')  fold_state = 1; /* \e$B!K\e(B */
4449                     else if (c1=='$')  fold_state = 1; /* \e$B!$\e(B */
4450                     else if (c1=='%')  fold_state = 1; /* \e$B!%\e(B */
4451                     else if (c1=='\'') fold_state = 1; /* \e$B!\\e(B */
4452                     else if (c1=='(')  fold_state = 1; /* \e$B!(\e(B */
4453                     else if (c1==')')  fold_state = 1; /* \e$B!)\e(B */
4454                     else if (c1=='*')  fold_state = 1; /* \e$B!*\e(B */
4455                     else if (c1=='+')  fold_state = 1; /* \e$B!+\e(B */
4456                     else if (c1==',')  fold_state = 1; /* \e$B!,\e(B */
4457                          /* default no fold in kinsoku */
4458                     else { 
4459                         fold_state = '\n';
4460                         f_line = char_size(c2,c1);
4461                         /* add one new f_line before this character */
4462                     }
4463                 } else {
4464                     f_line = char_size(c2,c1);
4465                     fold_state = '\n'; 
4466                     /* add one new f_line before this character */
4467                 }
4468             }
4469         }
4470     }
4471     /* terminator process */
4472     switch(fold_state) {
4473         case '\n': 
4474             (*o_fconv)(0,'\n');
4475             (*o_fconv)(c2,c1);
4476             break;
4477         case 0:    
4478             return;
4479         case '\r': 
4480             (*o_fconv)(0,'\n');
4481             break;
4482         case '\t': 
4483         case ' ': 
4484             (*o_fconv)(0,' ');
4485             break;
4486         default:
4487             (*o_fconv)(c2,c1);
4488     }
4489 }
4490
4491 nkf_char z_prev2=0,z_prev1=0;
4492
4493 void z_conv(nkf_char c2, nkf_char c1)
4494 {
4495
4496     /* if (c2) c1 &= 0x7f; assertion */
4497
4498     if (x0201_f && z_prev2==X0201) {  /* X0201 */
4499         if (c1==(0xde&0x7f)) { /* \e$BByE@\e(B */
4500             z_prev2=0;
4501             (*o_zconv)(dv[(z_prev1-SPACE)*2],dv[(z_prev1-SPACE)*2+1]);
4502             return;
4503         } else if (c1==(0xdf&0x7f)&&ev[(z_prev1-SPACE)*2]) {  /* \e$BH>ByE@\e(B */
4504             z_prev2=0;
4505             (*o_zconv)(ev[(z_prev1-SPACE)*2],ev[(z_prev1-SPACE)*2+1]);
4506             return;
4507         } else {
4508             z_prev2=0;
4509             (*o_zconv)(cv[(z_prev1-SPACE)*2],cv[(z_prev1-SPACE)*2+1]);
4510         }
4511     }
4512
4513     if (c2==EOF) {
4514         (*o_zconv)(c2,c1);
4515         return;
4516     }
4517
4518     if (x0201_f && c2==X0201) {
4519         if (dv[(c1-SPACE)*2]||ev[(c1-SPACE)*2]) {
4520             /* wait for \e$BByE@\e(B or \e$BH>ByE@\e(B */
4521             z_prev1 = c1; z_prev2 = c2;
4522             return;
4523         } else {
4524             (*o_zconv)(cv[(c1-SPACE)*2],cv[(c1-SPACE)*2+1]);
4525             return;
4526         }
4527     }
4528
4529     /* JISX0208 Alphabet */
4530     if (alpha_f && c2 == 0x23 ) {
4531         c2 = 0;
4532     } else if (alpha_f && c2 == 0x21 ) { 
4533     /* JISX0208 Kigou */
4534        if (0x21==c1) {
4535            if (alpha_f&0x2) {
4536                c1 = ' ';
4537                c2 = 0;
4538            } else if (alpha_f&0x4) {
4539                 (*o_zconv)(0,' ');
4540                 (*o_zconv)(0,' ');
4541                 return;
4542            } 
4543        } else if (0x20<c1 && c1<0x7f && fv[c1-0x20]) {
4544            c1 = fv[c1-0x20];
4545            c2 =  0;
4546            if (alpha_f&0x8) {
4547                char *entity = 0;
4548                switch (c1){
4549                  case '>': entity = "&gt;"; break;
4550                  case '<': entity = "&lt;"; break;
4551                  case '\"': entity = "&quot;"; break;
4552                  case '&': entity = "&amp;"; break;
4553                }
4554                if (entity){
4555                    while (*entity) (*o_zconv)(0, *entity++);
4556                    return;
4557                }
4558            }
4559        } 
4560     }
4561     (*o_zconv)(c2,c1);
4562 }
4563
4564
4565 #define rot13(c)  ( \
4566       ( c < 'A' ) ? c: \
4567       (c <= 'M')  ? (c + 13): \
4568       (c <= 'Z')  ? (c - 13): \
4569       (c < 'a')   ? (c): \
4570       (c <= 'm')  ? (c + 13): \
4571       (c <= 'z')  ? (c - 13): \
4572       (c) \
4573 )
4574
4575 #define  rot47(c) ( \
4576       ( c < '!' ) ? c: \
4577       ( c <= 'O' ) ? (c + 47) : \
4578       ( c <= '~' ) ?  (c - 47) : \
4579       c \
4580 )
4581
4582 void rot_conv(nkf_char c2, nkf_char c1)
4583 {
4584     if (c2==0 || c2==X0201 || c2==ISO8859_1) {
4585         c1 = rot13(c1);
4586     } else if (c2) {
4587         c1 = rot47(c1);
4588         c2 = rot47(c2);
4589     }
4590     (*o_rot_conv)(c2,c1);
4591 }
4592
4593 void hira_conv(nkf_char c2, nkf_char c1)
4594 {
4595     if (hira_f & 1) {
4596         if (c2 == 0x25) {
4597             if (0x20 < c1 && c1 < 0x74) {
4598                 c2 = 0x24;
4599                 (*o_hira_conv)(c2,c1);
4600                 return;
4601             } else if (c1 == 0x74 && (output_conv == w_oconv || output_conv == w_oconv16)) {
4602                 c2 = 0;
4603                 c1 = CLASS_UNICODE | 0x3094;
4604                 (*o_hira_conv)(c2,c1);
4605                 return;
4606             }
4607         } else if (c2 == 0x21 && (c1 == 0x33 || c1 == 0x34)) {
4608             c1 += 2;
4609             (*o_hira_conv)(c2,c1);
4610             return;
4611         }
4612     }
4613     if (hira_f & 2) {
4614         if (c2 == 0 && c1 == (CLASS_UNICODE | 0x3094)) {
4615             c2 = 0x25;
4616             c1 = 0x74;
4617         } else if (c2 == 0x24 && 0x20 < c1 && c1 < 0x74) {
4618             c2 = 0x25;
4619         } else if (c2 == 0x21 && (c1 == 0x35 || c1 == 0x36)) {
4620             c1 -= 2;
4621         }
4622     }
4623     (*o_hira_conv)(c2,c1);
4624 }
4625
4626
4627 void iso2022jp_check_conv(nkf_char c2, nkf_char c1)
4628 {
4629     static const nkf_char range[RANGE_NUM_MAX][2] = {
4630         {0x222f, 0x2239,},
4631         {0x2242, 0x2249,},
4632         {0x2251, 0x225b,},
4633         {0x226b, 0x2271,},
4634         {0x227a, 0x227d,},
4635         {0x2321, 0x232f,},
4636         {0x233a, 0x2340,},
4637         {0x235b, 0x2360,},
4638         {0x237b, 0x237e,},
4639         {0x2474, 0x247e,},
4640         {0x2577, 0x257e,},
4641         {0x2639, 0x2640,},
4642         {0x2659, 0x267e,},
4643         {0x2742, 0x2750,},
4644         {0x2772, 0x277e,},
4645         {0x2841, 0x287e,},
4646         {0x4f54, 0x4f7e,},
4647         {0x7425, 0x747e},
4648     };
4649     nkf_char i;
4650     nkf_char start, end, c;
4651
4652     if(c2 >= 0x00 && c2 <= 0x20 && c1 >= 0x7f && c1 <= 0xff) {
4653         c2 = GETA1;
4654         c1 = GETA2;
4655     }
4656     if((c2 >= 0x29 && c2 <= 0x2f) || (c2 >= 0x75 && c2 <= 0x7e)) {
4657         c2 = GETA1;
4658         c1 = GETA2;
4659     }
4660
4661     for (i = 0; i < RANGE_NUM_MAX; i++) {
4662         start = range[i][0];
4663         end   = range[i][1];
4664         c     = (c2 << 8) + c1;
4665         if (c >= start && c <= end) {
4666             c2 = GETA1;
4667             c1 = GETA2;
4668         }
4669     }
4670     (*o_iso2022jp_check_conv)(c2,c1);
4671 }
4672
4673
4674 /* This converts  =?ISO-2022-JP?B?HOGE HOGE?= */
4675
4676 const unsigned char *mime_pattern[] = {
4677     (const unsigned char *)"\075?EUC-JP?B?",
4678     (const unsigned char *)"\075?SHIFT_JIS?B?",
4679     (const unsigned char *)"\075?ISO-8859-1?Q?",
4680     (const unsigned char *)"\075?ISO-8859-1?B?",
4681     (const unsigned char *)"\075?ISO-2022-JP?B?",
4682     (const unsigned char *)"\075?ISO-2022-JP?Q?",
4683 #if defined(UTF8_INPUT_ENABLE)
4684     (const unsigned char *)"\075?UTF-8?B?",
4685     (const unsigned char *)"\075?UTF-8?Q?",
4686 #endif
4687     (const unsigned char *)"\075?US-ASCII?Q?",
4688     NULL
4689 };
4690
4691
4692 /* \e$B3:Ev$9$k%3!<%I$NM%@hEY$r>e$2$k$?$a$NL\0u\e(B */
4693 nkf_char (*mime_priority_func[])(nkf_char c2, nkf_char c1, nkf_char c0) = {
4694     e_iconv, s_iconv, 0, 0, 0, 0,
4695 #if defined(UTF8_INPUT_ENABLE)
4696     w_iconv, w_iconv,
4697 #endif
4698     0,
4699 };
4700
4701 const nkf_char mime_encode[] = {
4702     JAPANESE_EUC, SHIFT_JIS,ISO8859_1, ISO8859_1, X0208, X0201,
4703 #if defined(UTF8_INPUT_ENABLE)
4704     UTF8, UTF8,
4705 #endif
4706     ASCII,
4707     0
4708 };
4709
4710 const nkf_char mime_encode_method[] = {
4711     'B', 'B','Q', 'B', 'B', 'Q',
4712 #if defined(UTF8_INPUT_ENABLE)
4713     'B', 'Q',
4714 #endif
4715     'Q',
4716     0
4717 };
4718
4719
4720 #define MAXRECOVER 20
4721
4722 void switch_mime_getc(void)
4723 {
4724     if (i_getc!=mime_getc) {
4725         i_mgetc = i_getc; i_getc = mime_getc;
4726         i_mungetc = i_ungetc; i_ungetc = mime_ungetc;
4727         if(mime_f==STRICT_MIME) {
4728             i_mgetc_buf = i_mgetc; i_mgetc = mime_getc_buf;
4729             i_mungetc_buf = i_mungetc; i_mungetc = mime_ungetc_buf;
4730         }
4731     }
4732 }
4733
4734 void unswitch_mime_getc(void)
4735 {
4736     if(mime_f==STRICT_MIME) {
4737         i_mgetc = i_mgetc_buf;
4738         i_mungetc = i_mungetc_buf;
4739     }
4740     i_getc = i_mgetc;
4741     i_ungetc = i_mungetc;
4742     if(mime_iconv_back)set_iconv(FALSE, mime_iconv_back);
4743     mime_iconv_back = NULL;
4744 }
4745
4746 nkf_char mime_begin_strict(FILE *f)
4747 {
4748     nkf_char c1 = 0;
4749     int i,j,k;
4750     const unsigned char *p,*q;
4751     nkf_char r[MAXRECOVER];    /* recovery buffer, max mime pattern length */
4752
4753     mime_decode_mode = FALSE;
4754     /* =? has been checked */
4755     j = 0;
4756     p = mime_pattern[j];
4757     r[0]='='; r[1]='?';
4758
4759     for(i=2;p[i]>' ';i++) {                   /* start at =? */
4760         if ( ((r[i] = c1 = (*i_getc)(f))==EOF) || nkf_toupper(c1) != p[i] ) {
4761             /* pattern fails, try next one */
4762             q = p;
4763             while (mime_pattern[++j]) {
4764                 p = mime_pattern[j];
4765                 for(k=2;k<i;k++)              /* assume length(p) > i */
4766                     if (p[k]!=q[k]) break;
4767                 if (k==i && nkf_toupper(c1)==p[k]) break;
4768             }
4769             p = mime_pattern[j];
4770             if (p) continue;  /* found next one, continue */
4771             /* all fails, output from recovery buffer */
4772             (*i_ungetc)(c1,f);
4773             for(j=0;j<i;j++) {
4774                 (*oconv)(0,r[j]);
4775             }
4776             return c1;
4777         }
4778     }
4779     mime_decode_mode = p[i-2];
4780
4781     mime_iconv_back = iconv;
4782     set_iconv(FALSE, mime_priority_func[j]);
4783     clr_code_score(find_inputcode_byfunc(mime_priority_func[j]), SCORE_iMIME);
4784
4785     if (mime_decode_mode=='B') {
4786         mimebuf_f = unbuf_f;
4787         if (!unbuf_f) {
4788             /* do MIME integrity check */
4789             return mime_integrity(f,mime_pattern[j]);
4790         } 
4791     }
4792     switch_mime_getc();
4793     mimebuf_f = TRUE;
4794     return c1;
4795 }
4796
4797 nkf_char mime_getc_buf(FILE *f)
4798 {
4799     /* we don't keep eof of Fifo, becase it contains ?= as
4800        a terminator. It was checked in mime_integrity. */
4801     return ((mimebuf_f)?
4802         (*i_mgetc_buf)(f):Fifo(mime_input++));
4803 }
4804
4805 nkf_char mime_ungetc_buf(nkf_char c, FILE *f)
4806 {
4807     if (mimebuf_f)
4808         (*i_mungetc_buf)(c,f);
4809     else 
4810         Fifo(--mime_input) = (unsigned char)c;
4811     return c;
4812 }
4813
4814 nkf_char mime_begin(FILE *f)
4815 {
4816     nkf_char c1;
4817     int i,k;
4818
4819     /* In NONSTRICT mode, only =? is checked. In case of failure, we  */
4820     /* re-read and convert again from mime_buffer.  */
4821
4822     /* =? has been checked */
4823     k = mime_last;
4824     Fifo(mime_last++)='='; Fifo(mime_last++)='?';
4825     for(i=2;i<MAXRECOVER;i++) {                   /* start at =? */
4826         /* We accept any character type even if it is breaked by new lines */
4827         c1 = (*i_getc)(f); Fifo(mime_last++) = (unsigned char)c1;
4828         if (c1=='\n'||c1==' '||c1=='\r'||
4829                 c1=='-'||c1=='_'||is_alnum(c1) ) continue;
4830         if (c1=='=') {
4831             /* Failed. But this could be another MIME preemble */
4832             (*i_ungetc)(c1,f);
4833             mime_last--;
4834             break;
4835         }
4836         if (c1!='?') break;
4837         else {
4838             /* c1=='?' */
4839             c1 = (*i_getc)(f); Fifo(mime_last++) = (unsigned char)c1;
4840             if (!(++i<MAXRECOVER) || c1==EOF) break;
4841             if (c1=='b'||c1=='B') {
4842                 mime_decode_mode = 'B';
4843             } else if (c1=='q'||c1=='Q') {
4844                 mime_decode_mode = 'Q';
4845             } else {
4846                 break;
4847             }
4848             c1 = (*i_getc)(f); Fifo(mime_last++) = (unsigned char)c1;
4849             if (!(++i<MAXRECOVER) || c1==EOF) break;
4850             if (c1!='?') {
4851                 mime_decode_mode = FALSE;
4852             }
4853             break;
4854         }
4855     }
4856     switch_mime_getc();
4857     if (!mime_decode_mode) {
4858         /* false MIME premble, restart from mime_buffer */
4859         mime_decode_mode = 1;  /* no decode, but read from the mime_buffer */
4860         /* Since we are in MIME mode until buffer becomes empty,    */
4861         /* we never go into mime_begin again for a while.           */
4862         return c1;
4863     }
4864     /* discard mime preemble, and goto MIME mode */
4865     mime_last = k;
4866     /* do no MIME integrity check */
4867     return c1;   /* used only for checking EOF */
4868 }
4869
4870 #ifdef CHECK_OPTION
4871 void no_putc(nkf_char c)
4872 {
4873     ;
4874 }
4875
4876 void debug(const char *str)
4877 {
4878     if (debug_f){
4879         fprintf(stderr, "%s\n", str);
4880     }
4881 }
4882 #endif
4883
4884 void set_input_codename(char *codename)
4885 {
4886     if (guess_f && 
4887         is_inputcode_set &&
4888         strcmp(codename, "") != 0 && 
4889         strcmp(codename, input_codename) != 0)
4890     {
4891         is_inputcode_mixed = TRUE;
4892     }
4893     input_codename = codename;
4894     is_inputcode_set = TRUE;
4895 }
4896
4897 #if !defined(PERL_XS) && !defined(WIN32DLL)
4898 void print_guessed_code(char *filename)
4899 {
4900     char *codename = "BINARY";
4901     if (!is_inputcode_mixed) {
4902         if (strcmp(input_codename, "") == 0) {
4903             codename = "ASCII";
4904         } else {
4905             codename = input_codename;
4906         }
4907     }
4908     if (filename != NULL) printf("%s:", filename);
4909     printf("%s\n", codename);
4910 }
4911 #endif /*WIN32DLL*/
4912
4913 #ifdef INPUT_OPTION 
4914
4915 nkf_char hex_getc(nkf_char ch, FILE *f, nkf_char (*g)(FILE *f), nkf_char (*u)(nkf_char c, FILE *f))
4916 {
4917     nkf_char c1, c2, c3;
4918     c1 = (*g)(f);
4919     if (c1 != ch){
4920         return c1;
4921     }
4922     c2 = (*g)(f);
4923     if (!nkf_isxdigit(c2)){
4924         (*u)(c2, f);
4925         return c1;
4926     }
4927     c3 = (*g)(f);
4928     if (!nkf_isxdigit(c3)){
4929         (*u)(c2, f);
4930         (*u)(c3, f);
4931         return c1;
4932     }
4933     return (hex2bin(c2) << 4) | hex2bin(c3);
4934 }
4935
4936 nkf_char cap_getc(FILE *f)
4937 {
4938     return hex_getc(':', f, i_cgetc, i_cungetc);
4939 }
4940
4941 nkf_char cap_ungetc(nkf_char c, FILE *f)
4942 {
4943     return (*i_cungetc)(c, f);
4944 }
4945
4946 nkf_char url_getc(FILE *f)
4947 {
4948     return hex_getc('%', f, i_ugetc, i_uungetc);
4949 }
4950
4951 nkf_char url_ungetc(nkf_char c, FILE *f)
4952 {
4953     return (*i_uungetc)(c, f);
4954 }
4955 #endif
4956
4957 #ifdef NUMCHAR_OPTION
4958 nkf_char numchar_getc(FILE *f)
4959 {
4960     nkf_char (*g)(FILE *) = i_ngetc;
4961     nkf_char (*u)(nkf_char c ,FILE *f) = i_nungetc;
4962     int i = 0, j;
4963     nkf_char buf[8];
4964     long c = -1;
4965
4966     buf[i] = (*g)(f);
4967     if (buf[i] == '&'){
4968         buf[++i] = (*g)(f);
4969         if (buf[i] == '#'){
4970             c = 0;
4971             buf[++i] = (*g)(f);
4972             if (buf[i] == 'x' || buf[i] == 'X'){
4973                 for (j = 0; j < 7; j++){
4974                     buf[++i] = (*g)(f);
4975                     if (!nkf_isxdigit(buf[i])){
4976                         if (buf[i] != ';'){
4977                             c = -1;
4978                         }
4979                         break;
4980                     }
4981                     c <<= 4;
4982                     c |= hex2bin(buf[i]);
4983                 }
4984             }else{
4985                 for (j = 0; j < 8; j++){
4986                     if (j){
4987                         buf[++i] = (*g)(f);
4988                     }
4989                     if (!nkf_isdigit(buf[i])){
4990                         if (buf[i] != ';'){
4991                             c = -1;
4992                         }
4993                         break;
4994                     }
4995                     c *= 10;
4996                     c += hex2bin(buf[i]);
4997                 }
4998             }
4999         }
5000     }
5001     if (c != -1){
5002         return CLASS_UNICODE | c;
5003     }
5004     while (i > 0){
5005         (*u)(buf[i], f);
5006         --i;
5007     }
5008     return buf[0];
5009 }
5010
5011 nkf_char numchar_ungetc(nkf_char c, FILE *f)
5012 {
5013     return (*i_nungetc)(c, f);
5014 }
5015 #endif
5016
5017 #ifdef UNICODE_NORMALIZATION
5018
5019 /* Normalization Form C */
5020 nkf_char nfc_getc(FILE *f)
5021 {
5022     nkf_char (*g)(FILE *f) = i_nfc_getc;
5023     nkf_char (*u)(nkf_char c ,FILE *f) = i_nfc_ungetc;
5024     int i=0, j, k=1, lower, upper;
5025     nkf_char buf[9];
5026     const nkf_nfchar *array;
5027 #if 0
5028     extern const struct normalization_pair normalization_table[];
5029 #endif
5030     
5031     buf[i] = (*g)(f);
5032     while (k > 0 && ((buf[i] & 0xc0) != 0x80)){
5033         lower=0, upper=NORMALIZATION_TABLE_LENGTH-1;
5034         while (upper >= lower) {
5035             j = (lower+upper) / 2;
5036             array = normalization_table[j].nfd;
5037             for (k=0; k < NORMALIZATION_TABLE_NFD_LENGTH && array[k]; k++){
5038                 if (array[k] != buf[k]){
5039                     array[k] < buf[k] ? (lower = j + 1) : (upper = j - 1);
5040                     k = 0;
5041                     break;
5042                 } else if (k >= i)
5043                     buf[++i] = (*g)(f);
5044             }
5045             if (k > 0){
5046                 array = normalization_table[j].nfc;
5047                 for (i=0; i < NORMALIZATION_TABLE_NFC_LENGTH && array[i]; i++)
5048                     buf[i] = (nkf_char)(array[i]);
5049                 i--;
5050                 break;
5051             }
5052         }
5053         while (i > 0)
5054             (*u)(buf[i--], f);
5055     }
5056     return buf[0];
5057 }
5058
5059 nkf_char nfc_ungetc(nkf_char c, FILE *f)
5060 {
5061     return (*i_nfc_ungetc)(c, f);
5062 }
5063 #endif /* UNICODE_NORMALIZATION */
5064
5065
5066 nkf_char 
5067 mime_getc(FILE *f)
5068 {
5069     nkf_char c1, c2, c3, c4, cc;
5070     nkf_char t1, t2, t3, t4, mode, exit_mode;
5071     nkf_char lwsp_count;
5072     char *lwsp_buf;
5073     char *lwsp_buf_new;
5074     nkf_char lwsp_size = 128;
5075
5076     if (mime_top != mime_last) {  /* Something is in FIFO */
5077         return  Fifo(mime_top++);
5078     }
5079     if (mime_decode_mode==1 ||mime_decode_mode==FALSE) {
5080         mime_decode_mode=FALSE;
5081         unswitch_mime_getc();
5082         return (*i_getc)(f);
5083     }
5084
5085     if (mimebuf_f == FIXED_MIME)
5086         exit_mode = mime_decode_mode;
5087     else
5088         exit_mode = FALSE;
5089     if (mime_decode_mode == 'Q') {
5090         if ((c1 = (*i_mgetc)(f)) == EOF) return (EOF);
5091 restart_mime_q:
5092         if (c1=='_' && mimebuf_f != FIXED_MIME) return ' ';
5093         if (c1<=' ' || DEL<=c1) {
5094             mime_decode_mode = exit_mode; /* prepare for quit */
5095             return c1;
5096         }
5097         if (c1!='=' && (c1!='?' || mimebuf_f == FIXED_MIME)) {
5098             return c1;
5099         }
5100                 
5101         mime_decode_mode = exit_mode; /* prepare for quit */
5102         if ((c2 = (*i_mgetc)(f)) == EOF) return (EOF);
5103         if (c1=='?'&&c2=='=' && mimebuf_f != FIXED_MIME) {
5104             /* end Q encoding */
5105             input_mode = exit_mode;
5106             lwsp_count = 0;
5107             lwsp_buf = malloc((lwsp_size+5)*sizeof(char));
5108             if (lwsp_buf==NULL) {
5109                 perror("can't malloc");
5110                 return -1;
5111             }
5112             while ((c1=(*i_getc)(f))!=EOF) {
5113                 switch (c1) {
5114                 case NL:
5115                 case CR:
5116                     if (c1==NL) {
5117                         if ((c1=(*i_getc)(f))!=EOF && (c1==SPACE||c1==TAB)) {
5118                             i_ungetc(SPACE,f);
5119                             continue;
5120                         } else {
5121                             i_ungetc(c1,f);
5122                         }
5123                         c1 = NL;
5124                     } else {
5125                         if ((c1=(*i_getc)(f))!=EOF && c1 == NL) {
5126                             if ((c1=(*i_getc)(f))!=EOF && (c1==SPACE||c1==TAB)) {
5127                                 i_ungetc(SPACE,f);
5128                                 continue;
5129                             } else {
5130                                 i_ungetc(c1,f);
5131                             }
5132                             i_ungetc(NL,f);
5133                         } else {
5134                             i_ungetc(c1,f);
5135                         }
5136                         c1 = CR;
5137                     }
5138                     break;
5139                 case SPACE:
5140                 case TAB:
5141                     lwsp_buf[lwsp_count] = (unsigned char)c1;
5142                     if (lwsp_count++>lwsp_size){
5143                         lwsp_size <<= 1;
5144                         lwsp_buf_new = realloc(lwsp_buf, (lwsp_size+5)*sizeof(char));
5145                         if (lwsp_buf_new==NULL) {
5146                             free(lwsp_buf);
5147                             perror("can't realloc");
5148                             return -1;
5149                         }
5150                         lwsp_buf = lwsp_buf_new;
5151                     }
5152                     continue;
5153                 }
5154                 break;
5155             }
5156             if (lwsp_count > 0 && (c1 != '=' || (lwsp_buf[lwsp_count-1] != SPACE && lwsp_buf[lwsp_count-1] != TAB))) {
5157                 i_ungetc(c1,f);
5158                 for(lwsp_count--;lwsp_count>0;lwsp_count--)
5159                     i_ungetc(lwsp_buf[lwsp_count],f);
5160                 c1 = lwsp_buf[0];
5161             }
5162             free(lwsp_buf);
5163             return c1;
5164         }
5165         if (c1=='='&&c2<' ') { /* this is soft wrap */
5166             while((c1 =  (*i_mgetc)(f)) <=' ') {
5167                 if ((c1 = (*i_mgetc)(f)) == EOF) return (EOF);
5168             }
5169             mime_decode_mode = 'Q'; /* still in MIME */
5170             goto restart_mime_q;
5171         }
5172         if (c1=='?') {
5173             mime_decode_mode = 'Q'; /* still in MIME */
5174             (*i_mungetc)(c2,f);
5175             return c1;
5176         }
5177         if ((c3 = (*i_mgetc)(f)) == EOF) return (EOF);
5178         if (c2<=' ') return c2;
5179         mime_decode_mode = 'Q'; /* still in MIME */
5180         return ((hex2bin(c2)<<4) + hex2bin(c3));
5181     }
5182
5183     if (mime_decode_mode != 'B') {
5184         mime_decode_mode = FALSE;
5185         return (*i_mgetc)(f);
5186     }
5187
5188
5189     /* Base64 encoding */
5190     /* 
5191         MIME allows line break in the middle of 
5192         Base64, but we are very pessimistic in decoding
5193         in unbuf mode because MIME encoded code may broken by 
5194         less or editor's control sequence (such as ESC-[-K in unbuffered
5195         mode. ignore incomplete MIME.
5196     */
5197     mode = mime_decode_mode;
5198     mime_decode_mode = exit_mode;  /* prepare for quit */
5199
5200     while ((c1 = (*i_mgetc)(f))<=' ') {
5201         if (c1==EOF)
5202             return (EOF);
5203     }
5204 mime_c2_retry:
5205     if ((c2 = (*i_mgetc)(f))<=' ') {
5206         if (c2==EOF)
5207             return (EOF);
5208         if (mime_f != STRICT_MIME) goto mime_c2_retry;
5209         if (mimebuf_f!=FIXED_MIME) input_mode = ASCII;  
5210         return c2;
5211     }
5212     if ((c1 == '?') && (c2 == '=')) {
5213         input_mode = ASCII;
5214         lwsp_count = 0;
5215         lwsp_buf = malloc((lwsp_size+5)*sizeof(char));
5216         if (lwsp_buf==NULL) {
5217             perror("can't malloc");
5218             return -1;
5219         }
5220         while ((c1=(*i_getc)(f))!=EOF) {
5221             switch (c1) {
5222             case NL:
5223             case CR:
5224                 if (c1==NL) {
5225                     if ((c1=(*i_getc)(f))!=EOF && (c1==SPACE||c1==TAB)) {
5226                         i_ungetc(SPACE,f);
5227                         continue;
5228                     } else {
5229                         i_ungetc(c1,f);
5230                     }
5231                     c1 = NL;
5232                 } else {
5233                     if ((c1=(*i_getc)(f))!=EOF) {
5234                         if (c1==SPACE) {
5235                             i_ungetc(SPACE,f);
5236                             continue;
5237                         } else if ((c1=(*i_getc)(f))!=EOF && (c1==SPACE||c1==TAB)) {
5238                             i_ungetc(SPACE,f);
5239                             continue;
5240                         } else {
5241                             i_ungetc(c1,f);
5242                         }
5243                         i_ungetc(NL,f);
5244                     } else {
5245                         i_ungetc(c1,f);
5246                     }
5247                     c1 = CR;
5248                 }
5249                 break;
5250             case SPACE:
5251             case TAB:
5252                 lwsp_buf[lwsp_count] = (unsigned char)c1;
5253                 if (lwsp_count++>lwsp_size){
5254                     lwsp_size <<= 1;
5255                     lwsp_buf_new = realloc(lwsp_buf, (lwsp_size+5)*sizeof(char));
5256                     if (lwsp_buf_new==NULL) {
5257                         free(lwsp_buf);
5258                         perror("can't realloc");
5259                         return -1;
5260                     }
5261                     lwsp_buf = lwsp_buf_new;
5262                 }
5263                 continue;
5264             }
5265             break;
5266         }
5267         if (lwsp_count > 0 && (c1 != '=' || (lwsp_buf[lwsp_count-1] != SPACE && lwsp_buf[lwsp_count-1] != TAB))) {
5268             i_ungetc(c1,f);
5269             for(lwsp_count--;lwsp_count>0;lwsp_count--)
5270                 i_ungetc(lwsp_buf[lwsp_count],f);
5271             c1 = lwsp_buf[0];
5272         }
5273         free(lwsp_buf);
5274         return c1;
5275     }
5276 mime_c3_retry:
5277     if ((c3 = (*i_mgetc)(f))<=' ') {
5278         if (c3==EOF)
5279             return (EOF);
5280         if (mime_f != STRICT_MIME) goto mime_c3_retry;
5281         if (mimebuf_f!=FIXED_MIME) input_mode = ASCII;  
5282         return c3;
5283     }
5284 mime_c4_retry:
5285     if ((c4 = (*i_mgetc)(f))<=' ') {
5286         if (c4==EOF)
5287             return (EOF);
5288         if (mime_f != STRICT_MIME) goto mime_c4_retry;
5289         if (mimebuf_f!=FIXED_MIME) input_mode = ASCII;  
5290         return c4;
5291     }
5292
5293     mime_decode_mode = mode; /* still in MIME sigh... */
5294
5295     /* BASE 64 decoding */
5296
5297     t1 = 0x3f & base64decode(c1);
5298     t2 = 0x3f & base64decode(c2);
5299     t3 = 0x3f & base64decode(c3);
5300     t4 = 0x3f & base64decode(c4);
5301     cc = ((t1 << 2) & 0x0fc) | ((t2 >> 4) & 0x03);
5302     if (c2 != '=') {
5303         Fifo(mime_last++) = (unsigned char)cc;
5304         cc = ((t2 << 4) & 0x0f0) | ((t3 >> 2) & 0x0f);
5305         if (c3 != '=') {
5306             Fifo(mime_last++) = (unsigned char)cc;
5307             cc = ((t3 << 6) & 0x0c0) | (t4 & 0x3f);
5308             if (c4 != '=') 
5309                 Fifo(mime_last++) = (unsigned char)cc;
5310         }
5311     } else {
5312         return c1;
5313     }
5314     return  Fifo(mime_top++);
5315 }
5316
5317 nkf_char mime_ungetc(nkf_char c, FILE *f)
5318 {
5319     Fifo(--mime_top) = (unsigned char)c;
5320     return c;
5321 }
5322
5323 nkf_char mime_integrity(FILE *f, const unsigned char *p)
5324 {
5325     nkf_char c,d;
5326     unsigned int q;
5327     /* In buffered mode, read until =? or NL or buffer full
5328      */
5329     mime_input = mime_top;
5330     mime_last = mime_top;
5331     
5332     while(*p) Fifo(mime_input++) = *p++;
5333     d = 0;
5334     q = mime_input;
5335     while((c=(*i_getc)(f))!=EOF) {
5336         if (((mime_input-mime_top)&MIME_BUF_MASK)==0) {
5337             break;   /* buffer full */
5338         }
5339         if (c=='=' && d=='?') {
5340             /* checked. skip header, start decode */
5341             Fifo(mime_input++) = (unsigned char)c;
5342             /* mime_last_input = mime_input; */
5343             mime_input = q; 
5344             switch_mime_getc();
5345             return 1;
5346         }
5347         if (!( (c=='+'||c=='/'|| c=='=' || c=='?' || is_alnum(c))))
5348             break;
5349         /* Should we check length mod 4? */
5350         Fifo(mime_input++) = (unsigned char)c;
5351         d=c;
5352     }
5353     /* In case of Incomplete MIME, no MIME decode  */
5354     Fifo(mime_input++) = (unsigned char)c;
5355     mime_last = mime_input;     /* point undecoded buffer */
5356     mime_decode_mode = 1;              /* no decode on Fifo last in mime_getc */
5357     switch_mime_getc();         /* anyway we need buffered getc */
5358     return 1;
5359 }
5360
5361 nkf_char base64decode(nkf_char c)
5362 {
5363     int             i;
5364     if (c > '@') {
5365         if (c < '[') {
5366             i = c - 'A';                        /* A..Z 0-25 */
5367         } else {
5368             i = c - 'G'     /* - 'a' + 26 */ ;  /* a..z 26-51 */
5369         }
5370     } else if (c > '/') {
5371         i = c - '0' + '4'   /* - '0' + 52 */ ;  /* 0..9 52-61 */
5372     } else if (c == '+') {
5373         i = '>'             /* 62 */ ;          /* +  62 */
5374     } else {
5375         i = '?'             /* 63 */ ;          /* / 63 */
5376     }
5377     return (i);
5378 }
5379
5380 static const char basis_64[] =
5381    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
5382
5383 static nkf_char b64c;
5384 #define MIMEOUT_BUF_LENGTH (60)
5385 char mimeout_buf[MIMEOUT_BUF_LENGTH+1];
5386 int mimeout_buf_count = 0;
5387 int mimeout_preserve_space = 0;
5388 #define itoh4(c)   (c>=10?c+'A'-10:c+'0')
5389
5390 void open_mime(nkf_char mode)
5391 {
5392     const unsigned char *p;
5393     int i;
5394     int j;
5395     p  = mime_pattern[0];
5396     for(i=0;mime_encode[i];i++) {
5397         if (mode == mime_encode[i]) {
5398             p = mime_pattern[i];
5399             break;
5400         }
5401     }
5402     mimeout_mode = mime_encode_method[i];
5403     
5404     i = 0;
5405     if (base64_count>45) {
5406         if (mimeout_buf_count>0 && nkf_isblank(mimeout_buf[i])){
5407             (*o_mputc)(mimeout_buf[i]);
5408             i++;
5409         }
5410         (*o_mputc)(NL);
5411         (*o_mputc)(SPACE);
5412         base64_count = 1;
5413         if (!mimeout_preserve_space && mimeout_buf_count>0
5414             && (mimeout_buf[i]==SPACE || mimeout_buf[i]==TAB
5415                 || mimeout_buf[i]==CR || mimeout_buf[i]==NL )) {
5416             i++;
5417         }
5418     }
5419     if (!mimeout_preserve_space) {
5420         for (;i<mimeout_buf_count;i++) {
5421             if (mimeout_buf[i]==SPACE || mimeout_buf[i]==TAB
5422                 || mimeout_buf[i]==CR || mimeout_buf[i]==NL ) {
5423                 (*o_mputc)(mimeout_buf[i]);
5424                 base64_count ++;
5425             } else {
5426                 break;
5427             }
5428         }
5429     }
5430     mimeout_preserve_space = FALSE;
5431     
5432     while(*p) {
5433         (*o_mputc)(*p++);
5434         base64_count ++;
5435     }
5436     j = mimeout_buf_count;
5437     mimeout_buf_count = 0;
5438     for (;i<j;i++) {
5439         mime_putc(mimeout_buf[i]);
5440     }
5441 }
5442
5443 void close_mime(void)
5444 {
5445     (*o_mputc)('?');
5446     (*o_mputc)('=');
5447     base64_count += 2;
5448     mimeout_mode = 0;
5449 }
5450
5451 void eof_mime(void)
5452 {
5453     switch(mimeout_mode) {
5454     case 'Q':
5455     case 'B':
5456         break;
5457     case 2:
5458         (*o_mputc)(basis_64[((b64c & 0x3)<< 4)]);
5459         (*o_mputc)('=');
5460         (*o_mputc)('=');
5461         base64_count += 3;
5462         break;
5463     case 1:
5464         (*o_mputc)(basis_64[((b64c & 0xF) << 2)]);
5465         (*o_mputc)('=');
5466         base64_count += 2;
5467         break;
5468     }
5469     if (mimeout_mode) {
5470         if (mimeout_f!=FIXED_MIME) {
5471             close_mime(); 
5472         } else if (mimeout_mode != 'Q')
5473             mimeout_mode = 'B';
5474     }
5475 }
5476
5477 void mimeout_addchar(nkf_char c)
5478 {
5479     switch(mimeout_mode) {
5480     case 'Q':
5481         if (c==CR||c==NL) {
5482             (*o_mputc)(c);
5483             base64_count = 0;
5484         } else if(!nkf_isalnum(c)) {
5485             (*o_mputc)('=');
5486             (*o_mputc)(itoh4(((c>>4)&0xf)));
5487             (*o_mputc)(itoh4((c&0xf)));
5488             base64_count += 3;
5489         } else {
5490             (*o_mputc)(c);
5491             base64_count++;
5492         }
5493         break;
5494     case 'B':
5495         b64c=c;
5496         (*o_mputc)(basis_64[c>>2]);
5497         mimeout_mode=2;
5498         base64_count ++;
5499         break;
5500     case 2:
5501         (*o_mputc)(basis_64[((b64c & 0x3)<< 4) | ((c & 0xF0) >> 4)]);
5502         b64c=c;
5503         mimeout_mode=1;
5504         base64_count ++;
5505         break;
5506     case 1:
5507         (*o_mputc)(basis_64[((b64c & 0xF) << 2) | ((c & 0xC0) >>6)]);
5508         (*o_mputc)(basis_64[c & 0x3F]);
5509         mimeout_mode='B';
5510         base64_count += 2;
5511         break;
5512     default:
5513         (*o_mputc)(c);
5514         base64_count++;
5515         break;
5516     }
5517 }
5518
5519 nkf_char mime_lastchar2, mime_lastchar1;
5520
5521 void mime_prechar(nkf_char c2, nkf_char c1)
5522 {
5523     if (mimeout_mode){
5524         if (c2){
5525             if (base64_count + mimeout_buf_count/3*4> 66){
5526                 (*o_base64conv)(EOF,0);
5527                 (*o_base64conv)(0,NL);
5528                 (*o_base64conv)(0,SPACE);
5529             }
5530         }/*else if (mime_lastchar2){
5531             if (c1 <=DEL && !nkf_isspace(c1)){
5532                 (*o_base64conv)(0,SPACE);
5533             }
5534         }*/
5535     }/*else{
5536         if (c2 && mime_lastchar2 == 0
5537             && mime_lastchar1 && !nkf_isspace(mime_lastchar1)){
5538             (*o_base64conv)(0,SPACE);
5539         }
5540     }*/
5541     mime_lastchar2 = c2;
5542     mime_lastchar1 = c1;
5543 }
5544
5545 void mime_putc(nkf_char c)
5546 {
5547     int i, j;
5548     nkf_char lastchar;
5549
5550     if (mimeout_f == FIXED_MIME){
5551         if (mimeout_mode == 'Q'){
5552             if (base64_count > 71){
5553                 if (c!=CR && c!=NL) {
5554                     (*o_mputc)('=');
5555                     (*o_mputc)(NL);
5556                 }
5557                 base64_count = 0;
5558             }
5559         }else{
5560             if (base64_count > 71){
5561                 eof_mime();
5562                 (*o_mputc)(NL);
5563                 base64_count = 0;
5564             }
5565             if (c == EOF) { /* c==EOF */
5566                 eof_mime();
5567             }
5568         }
5569         if (c != EOF) { /* c==EOF */
5570             mimeout_addchar(c);
5571         }
5572         return;
5573     }
5574     
5575     /* mimeout_f != FIXED_MIME */
5576
5577     if (c == EOF) { /* c==EOF */
5578         j = mimeout_buf_count;
5579         mimeout_buf_count = 0;
5580         i = 0;
5581         if (mimeout_mode) {
5582             for (;i<j;i++) {
5583                 if (nkf_isspace(mimeout_buf[i]) && base64_count < 71){
5584                     break;
5585                 }
5586                 mimeout_addchar(mimeout_buf[i]);
5587             }
5588             eof_mime();
5589             for (;i<j;i++) {
5590                 mimeout_addchar(mimeout_buf[i]);
5591             }
5592         } else {
5593             for (;i<j;i++) {
5594                 mimeout_addchar(mimeout_buf[i]);
5595             }
5596         }
5597         return;
5598     }
5599
5600     if (mimeout_mode=='Q') {
5601         if (c <= DEL && (output_mode==ASCII ||output_mode == ISO8859_1 ) ) {
5602             if (c <= SPACE) {
5603                 close_mime();
5604                 (*o_mputc)(SPACE);
5605                 base64_count++;
5606             }
5607             (*o_mputc)(c);
5608             base64_count++;
5609         }
5610         return;
5611     }
5612
5613     if (mimeout_buf_count > 0){
5614         lastchar = mimeout_buf[mimeout_buf_count - 1];
5615     }else{
5616         lastchar = -1;
5617     }
5618
5619     if (!mimeout_mode) {
5620         if (c <= DEL && (output_mode==ASCII ||output_mode == ISO8859_1)) {
5621             if (nkf_isspace(c)) {
5622                 if (c==CR || c==NL) {
5623                     base64_count=0;
5624                 }
5625                 for (i=0;i<mimeout_buf_count;i++) {
5626                     (*o_mputc)(mimeout_buf[i]);
5627                     if (mimeout_buf[i] == CR || mimeout_buf[i] == NL){
5628                         base64_count = 0;
5629                     }else{
5630                         base64_count++;
5631                     }
5632                 }
5633                 mimeout_buf[0] = (char)c;
5634                 mimeout_buf_count = 1;
5635             }else{
5636                 if (base64_count > 1
5637                     && base64_count + mimeout_buf_count > 76){
5638                     (*o_mputc)(NL);
5639                     base64_count = 0;
5640                     if (!nkf_isspace(mimeout_buf[0])){
5641                         (*o_mputc)(SPACE);
5642                         base64_count++;
5643                     }
5644                 }
5645                 mimeout_buf[mimeout_buf_count++] = (char)c;
5646                 if (mimeout_buf_count>MIMEOUT_BUF_LENGTH) {
5647                     open_mime(output_mode);
5648                 }
5649             }
5650             return;
5651         }else{
5652             if (lastchar==CR || lastchar == NL){
5653                 for (i=0;i<mimeout_buf_count;i++) {
5654                     (*o_mputc)(mimeout_buf[i]);
5655                 }
5656                 base64_count = 0;
5657                 mimeout_buf_count = 0;
5658             }
5659             if (lastchar==SPACE) {
5660                 for (i=0;i<mimeout_buf_count-1;i++) {
5661                     (*o_mputc)(mimeout_buf[i]);
5662                     base64_count++;
5663                 }
5664                 mimeout_buf[0] = SPACE;
5665                 mimeout_buf_count = 1;
5666             }
5667             open_mime(output_mode);
5668         }
5669     }else{
5670         /* mimeout_mode == 'B', 1, 2 */
5671         if ( c<=DEL && (output_mode==ASCII ||output_mode == ISO8859_1 ) ) {
5672             if (lastchar == CR || lastchar == NL){
5673                 if (nkf_isblank(c)) {
5674                     for (i=0;i<mimeout_buf_count;i++) {
5675                         mimeout_addchar(mimeout_buf[i]);
5676                     }
5677                     mimeout_buf_count = 0;
5678                 } else if (SPACE<c && c<DEL) {
5679                     eof_mime();
5680                     for (i=0;i<mimeout_buf_count;i++) {
5681                         (*o_mputc)(mimeout_buf[i]);
5682                     }
5683                     base64_count = 0;
5684                     mimeout_buf_count = 0;
5685                 }
5686             }
5687             if (c==SPACE || c==TAB || c==CR || c==NL) {
5688                 for (i=0;i<mimeout_buf_count;i++) {
5689                     if (SPACE<mimeout_buf[i] && mimeout_buf[i]<DEL) {
5690                         eof_mime();
5691                         for (i=0;i<mimeout_buf_count;i++) {
5692                             (*o_mputc)(mimeout_buf[i]);
5693                             base64_count++;
5694                         }
5695                         mimeout_buf_count = 0;
5696                     }
5697                 }
5698                 mimeout_buf[mimeout_buf_count++] = (char)c;
5699                 if (mimeout_buf_count>MIMEOUT_BUF_LENGTH) {
5700                     eof_mime();
5701                     for (i=0;i<mimeout_buf_count;i++) {
5702                         (*o_mputc)(mimeout_buf[i]);
5703                         base64_count++;
5704                     }
5705                     mimeout_buf_count = 0;
5706                 }
5707                 return;
5708             }
5709             if (mimeout_buf_count>0 && SPACE<c && c!='=') {
5710                 mimeout_buf[mimeout_buf_count++] = (char)c;
5711                 if (mimeout_buf_count>MIMEOUT_BUF_LENGTH) {
5712                     j = mimeout_buf_count;
5713                     mimeout_buf_count = 0;
5714                     for (i=0;i<j;i++) {
5715                         mimeout_addchar(mimeout_buf[i]);
5716                     }
5717                 }
5718                 return;
5719             }
5720         }
5721     }
5722     if (mimeout_buf_count>0) {
5723         j = mimeout_buf_count;
5724         mimeout_buf_count = 0;
5725         for (i=0;i<j;i++) {
5726             if (mimeout_buf[i]==CR || mimeout_buf[i]==NL)
5727                 break;
5728             mimeout_addchar(mimeout_buf[i]);
5729         }
5730         if (i<j) {
5731             eof_mime();
5732             base64_count=0;
5733             for (;i<j;i++) {
5734                 (*o_mputc)(mimeout_buf[i]);
5735             }
5736             open_mime(output_mode);
5737         }
5738     }
5739     mimeout_addchar(c);
5740 }
5741
5742
5743 #if defined(PERL_XS) || defined(WIN32DLL)
5744 void reinit(void)
5745 {
5746     {
5747         struct input_code *p = input_code_list;
5748         while (p->name){
5749             status_reinit(p++);
5750         }
5751     }
5752     unbuf_f = FALSE;
5753     estab_f = FALSE;
5754     nop_f = FALSE;
5755     binmode_f = TRUE;
5756     rot_f = FALSE;
5757     hira_f = FALSE;
5758     input_f = FALSE;
5759     alpha_f = FALSE;
5760     mime_f = STRICT_MIME;
5761     mime_decode_f = FALSE;
5762     mimebuf_f = FALSE;
5763     broken_f = FALSE;
5764     iso8859_f = FALSE;
5765     mimeout_f = FALSE;
5766 #if defined(MSDOS) || defined(__OS2__)
5767      x0201_f = TRUE;
5768 #else
5769      x0201_f = NO_X0201;
5770 #endif
5771     iso2022jp_f = FALSE;
5772 #if defined(UTF8_INPUT_ENABLE) || defined(UTF8_OUTPUT_ENABLE)
5773     ms_ucs_map_f = UCS_MAP_ASCII;
5774 #endif
5775 #ifdef UTF8_INPUT_ENABLE
5776     no_cp932ext_f = FALSE;
5777     no_best_fit_chars_f = FALSE;
5778     encode_fallback = NULL;
5779     unicode_subchar  = '?';
5780     input_endian = ENDIAN_BIG;
5781 #endif
5782 #ifdef UTF8_OUTPUT_ENABLE
5783     output_bom_f = FALSE;
5784     output_endian = ENDIAN_BIG;
5785 #endif
5786 #ifdef UNICODE_NORMALIZATION
5787     nfc_f = FALSE;
5788 #endif
5789 #ifdef INPUT_OPTION
5790     cap_f = FALSE;
5791     url_f = FALSE;
5792     numchar_f = FALSE;
5793 #endif
5794 #ifdef CHECK_OPTION
5795     noout_f = FALSE;
5796     debug_f = FALSE;
5797 #endif
5798     guess_f = FALSE;
5799     is_inputcode_mixed = FALSE;
5800     is_inputcode_set   = FALSE;
5801 #ifdef EXEC_IO
5802     exec_f = 0;
5803 #endif
5804 #ifdef SHIFTJIS_CP932
5805     cp51932_f = TRUE;
5806     cp932inv_f = TRUE;
5807 #endif
5808 #ifdef X0212_ENABLE
5809     x0212_f = FALSE;
5810     x0213_f = FALSE;
5811 #endif
5812     {
5813         int i;
5814         for (i = 0; i < 256; i++){
5815             prefix_table[i] = 0;
5816         }
5817     }
5818     hold_count = 0;
5819     mimeout_buf_count = 0;
5820     mimeout_mode = 0;
5821     base64_count = 0;
5822     f_line = 0;
5823     f_prev = 0;
5824     fold_preserve_f = FALSE;
5825     fold_f = FALSE;
5826     fold_len = 0;
5827     kanji_intro = DEFAULT_J;
5828     ascii_intro = DEFAULT_R;
5829     fold_margin  = FOLD_MARGIN;
5830     output_conv = DEFAULT_CONV;
5831     oconv = DEFAULT_CONV;
5832     o_zconv = no_connection;
5833     o_fconv = no_connection;
5834     o_crconv = no_connection;
5835     o_rot_conv = no_connection;
5836     o_hira_conv = no_connection;
5837     o_base64conv = no_connection;
5838     o_iso2022jp_check_conv = no_connection;
5839     o_putc = std_putc;
5840     i_getc = std_getc;
5841     i_ungetc = std_ungetc;
5842     i_bgetc = std_getc;
5843     i_bungetc = std_ungetc;
5844     o_mputc = std_putc;
5845     i_mgetc = std_getc;
5846     i_mungetc  = std_ungetc;
5847     i_mgetc_buf = std_getc;
5848     i_mungetc_buf = std_ungetc;
5849     output_mode = ASCII;
5850     input_mode =  ASCII;
5851     shift_mode =  FALSE;
5852     mime_decode_mode = FALSE;
5853     file_out_f = FALSE;
5854     crmode_f = 0;
5855     option_mode = 0;
5856     broken_counter = 0;
5857     broken_last = 0;
5858     z_prev2=0,z_prev1=0;
5859 #ifdef CHECK_OPTION
5860     iconv_for_check = 0;
5861 #endif
5862     input_codename = "";
5863 #ifdef WIN32DLL
5864     reinitdll();
5865 #endif /*WIN32DLL*/
5866 }
5867 #endif
5868
5869 void no_connection(nkf_char c2, nkf_char c1)
5870 {
5871     no_connection2(c2,c1,0);
5872 }
5873
5874 nkf_char no_connection2(nkf_char c2, nkf_char c1, nkf_char c0)
5875 {
5876     fprintf(stderr,"nkf internal module connection failure.\n");
5877     exit(1);
5878     return 0; /* LINT */
5879 }
5880
5881 #ifndef PERL_XS
5882 #ifdef WIN32DLL
5883 #define fprintf dllprintf
5884 #endif
5885 void usage(void)
5886 {
5887     fprintf(stderr,"USAGE:  nkf(nkf32,wnkf,nkf2) -[flags] [in file] .. [out file for -O flag]\n");
5888     fprintf(stderr,"Flags:\n");
5889     fprintf(stderr,"b,u      Output is buffered (DEFAULT),Output is unbuffered\n");
5890 #ifdef DEFAULT_CODE_SJIS
5891     fprintf(stderr,"j,s,e,w  Output code is JIS 7 bit, Shift_JIS (DEFAULT), EUC-JP, UTF-8N\n");
5892 #endif
5893 #ifdef DEFAULT_CODE_JIS
5894     fprintf(stderr,"j,s,e,w  Output code is JIS 7 bit (DEFAULT), Shift JIS, EUC-JP, UTF-8N\n");
5895 #endif
5896 #ifdef DEFAULT_CODE_EUC
5897     fprintf(stderr,"j,s,e,w  Output code is JIS 7 bit, Shift JIS, EUC-JP (DEFAULT), UTF-8N\n");
5898 #endif
5899 #ifdef DEFAULT_CODE_UTF8
5900     fprintf(stderr,"j,s,e,w  Output code is JIS 7 bit, Shift JIS, EUC-JP, UTF-8N (DEFAULT)\n");
5901 #endif
5902 #ifdef UTF8_OUTPUT_ENABLE
5903     fprintf(stderr,"         After 'w' you can add more options. -w[ 8 [0], 16 [[BL] [0]] ]\n");
5904 #endif
5905     fprintf(stderr,"J,S,E,W  Input assumption is JIS 7 bit , Shift JIS, EUC-JP, UTF-8\n");
5906 #ifdef UTF8_INPUT_ENABLE
5907     fprintf(stderr,"         After 'W' you can add more options. -W[ 8, 16 [BL] ] \n");
5908 #endif
5909     fprintf(stderr,"t        no conversion\n");
5910     fprintf(stderr,"i[@B]    Specify the Esc Seq for JIS X 0208-1978/83 (DEFAULT B)\n");
5911     fprintf(stderr,"o[BJH]   Specify the Esc Seq for ASCII/Roman        (DEFAULT B)\n");
5912     fprintf(stderr,"r        {de/en}crypt ROT13/47\n");
5913     fprintf(stderr,"h        1 katakana->hiragana, 2 hiragana->katakana, 3 both\n");
5914     fprintf(stderr,"v        Show this usage. V: show version\n");
5915     fprintf(stderr,"m[BQN0]  MIME decode [B:base64,Q:quoted,N:non-strict,0:no decode]\n");
5916     fprintf(stderr,"M[BQ]    MIME encode [B:base64 Q:quoted]\n");
5917     fprintf(stderr,"l        ISO8859-1 (Latin-1) support\n");
5918     fprintf(stderr,"f/F      Folding: -f60 or -f or -f60-10 (fold margin 10) F preserve nl\n");
5919     fprintf(stderr,"Z[0-3]   Convert X0208 alphabet to ASCII\n");
5920     fprintf(stderr,"         1: Kankaku to 1 space  2: to 2 spaces  3: Convert to HTML Entity\n");
5921     fprintf(stderr,"X,x      Assume X0201 kana in MS-Kanji, -x preserves X0201\n");
5922     fprintf(stderr,"B[0-2]   Broken input  0: missing ESC,1: any X on ESC-[($]-X,2: ASCII on NL\n");
5923 #ifdef MSDOS
5924     fprintf(stderr,"T        Text mode output\n");
5925 #endif
5926     fprintf(stderr,"O        Output to File (DEFAULT 'nkf.out')\n");
5927     fprintf(stderr,"I        Convert non ISO-2022-JP charactor to GETA\n");
5928     fprintf(stderr,"d,c      Convert line breaks  -d: LF  -c: CRLF\n");
5929     fprintf(stderr,"-L[uwm]  line mode u:LF w:CRLF m:CR (DEFAULT noconversion)\n");
5930     fprintf(stderr,"\n");
5931     fprintf(stderr,"Long name options\n");
5932     fprintf(stderr," --ic=<input codeset>  --oc=<output codeset>\n");
5933     fprintf(stderr,"                   Specify the input or output codeset\n");
5934     fprintf(stderr," --fj  --unix --mac  --windows\n");
5935     fprintf(stderr," --jis  --euc  --sjis  --utf8  --utf16  --mime  --base64\n");
5936     fprintf(stderr,"                   Convert for the system or code\n");
5937     fprintf(stderr," --hiragana  --katakana  --katakana-hiragana\n");
5938     fprintf(stderr,"                   To Hiragana/Katakana Conversion\n");
5939     fprintf(stderr," --prefix=         Insert escape before troublesome characters of Shift_JIS\n");
5940 #ifdef INPUT_OPTION
5941     fprintf(stderr," --cap-input, --url-input  Convert hex after ':' or '%%'\n");
5942 #endif
5943 #ifdef NUMCHAR_OPTION
5944     fprintf(stderr," --numchar-input   Convert Unicode Character Reference\n");
5945 #endif
5946 #ifdef UTF8_INPUT_ENABLE
5947     fprintf(stderr," --fb-{skip, html, xml, perl, java, subchar}\n");
5948     fprintf(stderr,"                   Specify how nkf handles unassigned characters\n");
5949 #endif
5950 #ifdef OVERWRITE
5951     fprintf(stderr," --in-place[=SUFFIX]  --overwrite[=SUFFIX]\n");
5952     fprintf(stderr,"                   Overwrite original listed files by filtered result\n");
5953     fprintf(stderr,"                   --overwrite preserves timestamp of original files\n");
5954 #endif
5955     fprintf(stderr," -g  --guess       Guess the input code\n");
5956     fprintf(stderr," --help  --version Show this help/the version\n");
5957     fprintf(stderr,"                   For more information, see also man nkf\n");
5958     fprintf(stderr,"\n");
5959     version();
5960 }
5961
5962 void version(void)
5963 {
5964     fprintf(stderr,"Network Kanji Filter Version %s (%s) "
5965 #if defined(MSDOS) && !defined(__WIN32__) && !defined(__WIN16__) && !defined(__OS2__)
5966                   "for DOS"
5967 #endif
5968 #if defined(MSDOS) && defined(__WIN16__)
5969                   "for Win16"
5970 #endif
5971 #if defined(MSDOS) && defined(__WIN32__)
5972                   "for Win32"
5973 #endif
5974 #ifdef __OS2__
5975                   "for OS/2"
5976 #endif
5977                   ,NKF_VERSION,NKF_RELEASE_DATE);
5978     fprintf(stderr,"\n%s\n",CopyRight);
5979 }
5980 #endif /*PERL_XS*/
5981
5982 /**
5983  ** \e$B%Q%C%A@):n<T\e(B
5984  **  void@merope.pleiades.or.jp (Kusakabe Youichi)
5985  **  NIDE Naoyuki <nide@ics.nara-wu.ac.jp>
5986  **  ohta@src.ricoh.co.jp (Junn Ohta)
5987  **  inouet@strl.nhk.or.jp (Tomoyuki Inoue)
5988  **  kiri@pulser.win.or.jp (Tetsuaki Kiriyama)
5989  **  Kimihiko Sato <sato@sail.t.u-tokyo.ac.jp>
5990  **  a_kuroe@kuroe.aoba.yokohama.jp (Akihiko Kuroe)
5991  **  kono@ie.u-ryukyu.ac.jp (Shinji Kono)
5992  **  GHG00637@nifty-serve.or.jp (COW)
5993  **
5994  **/
5995
5996 /* end */