2 * Copyright (c) 1987, Fujitsu LTD. (Itaru ICHIKAWA).
3 * Copyright (c) 1996-2009, The nkf Project.
5 * This software is provided 'as-is', without any express or implied
6 * warranty. In no event will the authors be held liable for any damages
7 * arising from the use of this software.
9 * Permission is granted to anyone to use this software for any purpose,
10 * including commercial applications, and to alter it and redistribute it
11 * freely, subject to the following restrictions:
13 * 1. The origin of this software must not be misrepresented; you must not
14 * claim that you wrote the original software. If you use this software
15 * in a product, an acknowledgment in the product documentation would be
16 * appreciated but is not required.
18 * 2. Altered source versions must be plainly marked as such, and must not be
19 * misrepresented as being the original software.
21 * 3. This notice may not be removed or altered from any source distribution.
23 #define NKF_VERSION "2.1.1"
24 #define NKF_RELEASE_DATE "2010-01-15"
26 "Copyright (C) 1987, FUJITSU LTD. (I.Ichikawa).\n" \
27 "Copyright (C) 1996-2010, The nkf Project."
38 # define INCL_DOSERRORS
44 /* state of output_mode and input_mode
123 NKF_ENCODING_TABLE_SIZE,
124 JIS_X_0201_1976_K = 0x1013, /* I */ /* JIS C 6220-1969 */
125 /* JIS_X_0201_1976_R = 0x1014, */ /* J */ /* JIS C 6220-1969 */
126 /* JIS_X_0208_1978 = 0x1040, */ /* @ */ /* JIS C 6226-1978 */
127 /* JIS_X_0208_1983 = 0x1087, */ /* B */ /* JIS C 6226-1983 */
128 JIS_X_0208 = 0x1168, /* @B */
129 JIS_X_0212 = 0x1159, /* D */
130 /* JIS_X_0213_2000_1 = 0x1228, */ /* O */
131 JIS_X_0213_2 = 0x1229, /* P */
132 JIS_X_0213_1 = 0x1233 /* Q */
135 static nkf_char s_iconv(nkf_char c2, nkf_char c1, nkf_char c0);
136 static nkf_char e_iconv(nkf_char c2, nkf_char c1, nkf_char c0);
137 static nkf_char w_iconv(nkf_char c2, nkf_char c1, nkf_char c0);
138 static nkf_char w_iconv16(nkf_char c2, nkf_char c1, nkf_char c0);
139 static nkf_char w_iconv32(nkf_char c2, nkf_char c1, nkf_char c0);
140 static void j_oconv(nkf_char c2, nkf_char c1);
141 static void s_oconv(nkf_char c2, nkf_char c1);
142 static void e_oconv(nkf_char c2, nkf_char c1);
143 static void w_oconv(nkf_char c2, nkf_char c1);
144 static void w_oconv16(nkf_char c2, nkf_char c1);
145 static void w_oconv32(nkf_char c2, nkf_char c1);
149 nkf_char (*iconv)(nkf_char c2, nkf_char c1, nkf_char c0);
150 void (*oconv)(nkf_char c2, nkf_char c1);
151 } nkf_native_encoding;
153 nkf_native_encoding NkfEncodingASCII = { "ASCII", e_iconv, e_oconv };
154 nkf_native_encoding NkfEncodingISO_2022_JP = { "ISO-2022-JP", e_iconv, j_oconv };
155 nkf_native_encoding NkfEncodingShift_JIS = { "Shift_JIS", s_iconv, s_oconv };
156 nkf_native_encoding NkfEncodingEUC_JP = { "EUC-JP", e_iconv, e_oconv };
157 nkf_native_encoding NkfEncodingUTF_8 = { "UTF-8", w_iconv, w_oconv };
158 nkf_native_encoding NkfEncodingUTF_16 = { "UTF-16", w_iconv16, w_oconv16 };
159 nkf_native_encoding NkfEncodingUTF_32 = { "UTF-32", w_iconv32, w_oconv32 };
164 const nkf_native_encoding *base_encoding;
167 nkf_encoding nkf_encoding_table[] = {
168 {ASCII, "US-ASCII", &NkfEncodingASCII},
169 {ISO_8859_1, "ISO-8859-1", &NkfEncodingASCII},
170 {ISO_2022_JP, "ISO-2022-JP", &NkfEncodingISO_2022_JP},
171 {CP50220, "CP50220", &NkfEncodingISO_2022_JP},
172 {CP50221, "CP50221", &NkfEncodingISO_2022_JP},
173 {CP50222, "CP50222", &NkfEncodingISO_2022_JP},
174 {ISO_2022_JP_1, "ISO-2022-JP-1", &NkfEncodingISO_2022_JP},
175 {ISO_2022_JP_3, "ISO-2022-JP-3", &NkfEncodingISO_2022_JP},
176 {ISO_2022_JP_2004, "ISO-2022-JP-2004", &NkfEncodingISO_2022_JP},
177 {SHIFT_JIS, "Shift_JIS", &NkfEncodingShift_JIS},
178 {WINDOWS_31J, "Windows-31J", &NkfEncodingShift_JIS},
179 {CP10001, "CP10001", &NkfEncodingShift_JIS},
180 {EUC_JP, "EUC-JP", &NkfEncodingEUC_JP},
181 {EUCJP_NKF, "eucJP-nkf", &NkfEncodingEUC_JP},
182 {CP51932, "CP51932", &NkfEncodingEUC_JP},
183 {EUCJP_MS, "eucJP-MS", &NkfEncodingEUC_JP},
184 {EUCJP_ASCII, "eucJP-ASCII", &NkfEncodingEUC_JP},
185 {SHIFT_JISX0213, "Shift_JISX0213", &NkfEncodingShift_JIS},
186 {SHIFT_JIS_2004, "Shift_JIS-2004", &NkfEncodingShift_JIS},
187 {EUC_JISX0213, "EUC-JISX0213", &NkfEncodingEUC_JP},
188 {EUC_JIS_2004, "EUC-JIS-2004", &NkfEncodingEUC_JP},
189 {UTF_8, "UTF-8", &NkfEncodingUTF_8},
190 {UTF_8N, "UTF-8N", &NkfEncodingUTF_8},
191 {UTF_8_BOM, "UTF-8-BOM", &NkfEncodingUTF_8},
192 {UTF8_MAC, "UTF8-MAC", &NkfEncodingUTF_8},
193 {UTF_16, "UTF-16", &NkfEncodingUTF_16},
194 {UTF_16BE, "UTF-16BE", &NkfEncodingUTF_16},
195 {UTF_16BE_BOM, "UTF-16BE-BOM", &NkfEncodingUTF_16},
196 {UTF_16LE, "UTF-16LE", &NkfEncodingUTF_16},
197 {UTF_16LE_BOM, "UTF-16LE-BOM", &NkfEncodingUTF_16},
198 {UTF_32, "UTF-32", &NkfEncodingUTF_32},
199 {UTF_32BE, "UTF-32BE", &NkfEncodingUTF_32},
200 {UTF_32BE_BOM, "UTF-32BE-BOM", &NkfEncodingUTF_32},
201 {UTF_32LE, "UTF-32LE", &NkfEncodingUTF_32},
202 {UTF_32LE_BOM, "UTF-32LE-BOM", &NkfEncodingUTF_32},
203 {BINARY, "BINARY", &NkfEncodingASCII},
210 } encoding_name_to_id_table[] = {
215 {"ISO-2022-JP", ISO_2022_JP},
216 {"ISO2022JP-CP932", CP50220},
217 {"CP50220", CP50220},
218 {"CP50221", CP50221},
219 {"CSISO2022JP", CP50221},
220 {"CP50222", CP50222},
221 {"ISO-2022-JP-1", ISO_2022_JP_1},
222 {"ISO-2022-JP-3", ISO_2022_JP_3},
223 {"ISO-2022-JP-2004", ISO_2022_JP_2004},
224 {"SHIFT_JIS", SHIFT_JIS},
227 {"WINDOWS-31J", WINDOWS_31J},
228 {"CSWINDOWS31J", WINDOWS_31J},
229 {"CP932", WINDOWS_31J},
230 {"MS932", WINDOWS_31J},
231 {"CP10001", CP10001},
234 {"EUCJP-NKF", EUCJP_NKF},
235 {"CP51932", CP51932},
236 {"EUC-JP-MS", EUCJP_MS},
237 {"EUCJP-MS", EUCJP_MS},
238 {"EUCJPMS", EUCJP_MS},
239 {"EUC-JP-ASCII", EUCJP_ASCII},
240 {"EUCJP-ASCII", EUCJP_ASCII},
241 {"SHIFT_JISX0213", SHIFT_JISX0213},
242 {"SHIFT_JIS-2004", SHIFT_JIS_2004},
243 {"EUC-JISX0213", EUC_JISX0213},
244 {"EUC-JIS-2004", EUC_JIS_2004},
247 {"UTF-8-BOM", UTF_8_BOM},
248 {"UTF8-MAC", UTF8_MAC},
249 {"UTF-8-MAC", UTF8_MAC},
251 {"UTF-16BE", UTF_16BE},
252 {"UTF-16BE-BOM", UTF_16BE_BOM},
253 {"UTF-16LE", UTF_16LE},
254 {"UTF-16LE-BOM", UTF_16LE_BOM},
256 {"UTF-32BE", UTF_32BE},
257 {"UTF-32BE-BOM", UTF_32BE_BOM},
258 {"UTF-32LE", UTF_32LE},
259 {"UTF-32LE-BOM", UTF_32LE_BOM},
264 #if defined(DEFAULT_CODE_JIS)
265 #define DEFAULT_ENCIDX ISO_2022_JP
266 #elif defined(DEFAULT_CODE_SJIS)
267 #define DEFAULT_ENCIDX SHIFT_JIS
268 #elif defined(DEFAULT_CODE_WINDOWS_31J)
269 #define DEFAULT_ENCIDX WINDOWS_31J
270 #elif defined(DEFAULT_CODE_EUC)
271 #define DEFAULT_ENCIDX EUC_JP
272 #elif defined(DEFAULT_CODE_UTF8)
273 #define DEFAULT_ENCIDX UTF_8
277 #define is_alnum(c) \
278 (('a'<=c && c<='z')||('A'<= c && c<='Z')||('0'<=c && c<='9'))
280 /* I don't trust portablity of toupper */
281 #define nkf_toupper(c) (('a'<=c && c<='z')?(c-('a'-'A')):c)
282 #define nkf_isoctal(c) ('0'<=c && c<='7')
283 #define nkf_isdigit(c) ('0'<=c && c<='9')
284 #define nkf_isxdigit(c) (nkf_isdigit(c) || ('a'<=c && c<='f') || ('A'<=c && c <= 'F'))
285 #define nkf_isblank(c) (c == SP || c == TAB)
286 #define nkf_isspace(c) (nkf_isblank(c) || c == CR || c == LF)
287 #define nkf_isalpha(c) (('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z'))
288 #define nkf_isalnum(c) (nkf_isdigit(c) || nkf_isalpha(c))
289 #define nkf_isprint(c) (SP<=c && c<='~')
290 #define nkf_isgraph(c) ('!'<=c && c<='~')
291 #define hex2bin(c) (('0'<=c&&c<='9') ? (c-'0') : \
292 ('A'<=c&&c<='F') ? (c-'A'+10) : \
293 ('a'<=c&&c<='f') ? (c-'a'+10) : 0)
294 #define bin2hex(c) ("0123456789ABCDEF"[c&15])
295 #define is_eucg3(c2) (((unsigned short)c2 >> 8) == SS3)
296 #define nkf_noescape_mime(c) ((c == CR) || (c == LF) || \
297 ((c > SP) && (c < DEL) && (c != '?') && (c != '=') && (c != '_') \
298 && (c != '(') && (c != ')') && (c != '.') && (c != 0x22)))
300 #define is_ibmext_in_sjis(c2) (CP932_TABLE_BEGIN <= c2 && c2 <= CP932_TABLE_END)
301 #define nkf_byte_jisx0201_katakana_p(c) (SP <= c && c <= 0x5F)
303 #define HOLD_SIZE 1024
304 #if defined(INT_IS_SHORT)
305 #define IOBUF_SIZE 2048
307 #define IOBUF_SIZE 16384
310 #define DEFAULT_J 'B'
311 #define DEFAULT_R 'B'
318 /* MIME preprocessor */
320 #ifdef EASYWIN /*Easy Win */
321 extern POINT _BufferSize;
330 void (*status_func)(struct input_code *, nkf_char);
331 nkf_char (*iconv_func)(nkf_char c2, nkf_char c1, nkf_char c0);
335 static const char *input_codename = NULL; /* NULL: unestablished, "": BINARY */
336 static nkf_encoding *input_encoding = NULL;
337 static nkf_encoding *output_encoding = NULL;
339 #if defined(UTF8_INPUT_ENABLE) || defined(UTF8_OUTPUT_ENABLE)
341 * 0: Shift_JIS, eucJP-ascii
346 #define UCS_MAP_ASCII 0
348 #define UCS_MAP_CP932 2
349 #define UCS_MAP_CP10001 3
350 static int ms_ucs_map_f = UCS_MAP_ASCII;
352 #ifdef UTF8_INPUT_ENABLE
353 /* no NEC special, NEC-selected IBM extended and IBM extended characters */
354 static int no_cp932ext_f = FALSE;
355 /* ignore ZERO WIDTH NO-BREAK SPACE */
356 static int no_best_fit_chars_f = FALSE;
357 static int input_endian = ENDIAN_BIG;
358 static nkf_char unicode_subchar = '?'; /* the regular substitution character */
359 static void (*encode_fallback)(nkf_char c) = NULL;
360 static void w_status(struct input_code *, nkf_char);
362 #ifdef UTF8_OUTPUT_ENABLE
363 static int output_bom_f = FALSE;
364 static int output_endian = ENDIAN_BIG;
367 static void std_putc(nkf_char c);
368 static nkf_char std_getc(FILE *f);
369 static nkf_char std_ungetc(nkf_char c,FILE *f);
371 static nkf_char broken_getc(FILE *f);
372 static nkf_char broken_ungetc(nkf_char c,FILE *f);
374 static nkf_char mime_getc(FILE *f);
376 static void mime_putc(nkf_char c);
380 #if !defined(PERL_XS) && !defined(WIN32DLL)
381 static unsigned char stdibuf[IOBUF_SIZE];
382 static unsigned char stdobuf[IOBUF_SIZE];
386 static int unbuf_f = FALSE;
387 static int estab_f = FALSE;
388 static int nop_f = FALSE;
389 static int binmode_f = TRUE; /* binary mode */
390 static int rot_f = FALSE; /* rot14/43 mode */
391 static int hira_f = FALSE; /* hira/kata henkan */
392 static int alpha_f = FALSE; /* convert JIx0208 alphbet to ASCII */
393 static int mime_f = MIME_DECODE_DEFAULT; /* convert MIME B base64 or Q */
394 static int mime_decode_f = FALSE; /* mime decode is explicitly on */
395 static int mimebuf_f = FALSE; /* MIME buffered input */
396 static int broken_f = FALSE; /* convert ESC-less broken JIS */
397 static int iso8859_f = FALSE; /* ISO8859 through */
398 static int mimeout_f = FALSE; /* base64 mode */
399 static int x0201_f = X0201_DEFAULT; /* convert JIS X 0201 */
400 static int iso2022jp_f = FALSE; /* replace non ISO-2022-JP with GETA */
402 #ifdef UNICODE_NORMALIZATION
403 static int nfc_f = FALSE;
404 static nkf_char (*i_nfc_getc)(FILE *) = std_getc; /* input of ugetc */
405 static nkf_char (*i_nfc_ungetc)(nkf_char c ,FILE *f) = std_ungetc;
409 static int cap_f = FALSE;
410 static nkf_char (*i_cgetc)(FILE *) = std_getc; /* input of cgetc */
411 static nkf_char (*i_cungetc)(nkf_char c ,FILE *f) = std_ungetc;
413 static int url_f = FALSE;
414 static nkf_char (*i_ugetc)(FILE *) = std_getc; /* input of ugetc */
415 static nkf_char (*i_uungetc)(nkf_char c ,FILE *f) = std_ungetc;
418 #define PREFIX_EUCG3 NKF_INT32_C(0x8F00)
419 #define CLASS_MASK NKF_INT32_C(0xFF000000)
420 #define CLASS_UNICODE NKF_INT32_C(0x01000000)
421 #define VALUE_MASK NKF_INT32_C(0x00FFFFFF)
422 #define UNICODE_BMP_MAX NKF_INT32_C(0x0000FFFF)
423 #define UNICODE_MAX NKF_INT32_C(0x0010FFFF)
424 #define nkf_char_euc3_new(c) ((c) | PREFIX_EUCG3)
425 #define nkf_char_unicode_new(c) ((c) | CLASS_UNICODE)
426 #define nkf_char_unicode_p(c) ((c & CLASS_MASK) == CLASS_UNICODE)
427 #define nkf_char_unicode_bmp_p(c) ((c & VALUE_MASK) <= UNICODE_BMP_MAX)
428 #define nkf_char_unicode_value_p(c) ((c & VALUE_MASK) <= UNICODE_MAX)
430 #ifdef NUMCHAR_OPTION
431 static int numchar_f = FALSE;
432 static nkf_char (*i_ngetc)(FILE *) = std_getc; /* input of ugetc */
433 static nkf_char (*i_nungetc)(nkf_char c ,FILE *f) = std_ungetc;
437 static int noout_f = FALSE;
438 static void no_putc(nkf_char c);
439 static int debug_f = FALSE;
440 static void debug(const char *str);
441 static nkf_char (*iconv_for_check)(nkf_char c2,nkf_char c1,nkf_char c0) = 0;
444 static int guess_f = 0; /* 0: OFF, 1: ON, 2: VERBOSE */
445 static void set_input_codename(const char *codename);
448 static int exec_f = 0;
451 #ifdef SHIFTJIS_CP932
452 /* invert IBM extended characters to others */
453 static int cp51932_f = FALSE;
455 /* invert NEC-selected IBM extended characters to IBM extended characters */
456 static int cp932inv_f = TRUE;
458 /* static nkf_char cp932_conv(nkf_char c2, nkf_char c1); */
459 #endif /* SHIFTJIS_CP932 */
461 static int x0212_f = FALSE;
462 static int x0213_f = FALSE;
464 static unsigned char prefix_table[256];
466 static void e_status(struct input_code *, nkf_char);
467 static void s_status(struct input_code *, nkf_char);
469 struct input_code input_code_list[] = {
470 {"EUC-JP", 0, 0, 0, {0, 0, 0}, e_status, e_iconv, 0},
471 {"Shift_JIS", 0, 0, 0, {0, 0, 0}, s_status, s_iconv, 0},
472 #ifdef UTF8_INPUT_ENABLE
473 {"UTF-8", 0, 0, 0, {0, 0, 0}, w_status, w_iconv, 0},
474 {"UTF-16", 0, 0, 0, {0, 0, 0}, NULL, w_iconv16, 0},
475 {"UTF-32", 0, 0, 0, {0, 0, 0}, NULL, w_iconv32, 0},
480 static int mimeout_mode = 0; /* 0, -1, 'Q', 'B', 1, 2 */
481 static int base64_count = 0;
483 /* X0208 -> ASCII converter */
486 static int f_line = 0; /* chars in line */
487 static int f_prev = 0;
488 static int fold_preserve_f = FALSE; /* preserve new lines */
489 static int fold_f = FALSE;
490 static int fold_len = 0;
493 static unsigned char kanji_intro = DEFAULT_J;
494 static unsigned char ascii_intro = DEFAULT_R;
498 #define FOLD_MARGIN 10
499 #define DEFAULT_FOLD 60
501 static int fold_margin = FOLD_MARGIN;
503 /* process default */
506 no_connection2(nkf_char c2, nkf_char c1, nkf_char c0)
508 fprintf(stderr,"nkf internal module connection failure.\n");
514 no_connection(nkf_char c2, nkf_char c1)
516 no_connection2(c2,c1,0);
519 static nkf_char (*iconv)(nkf_char c2,nkf_char c1,nkf_char c0) = no_connection2;
520 static void (*oconv)(nkf_char c2,nkf_char c1) = no_connection;
522 static void (*o_zconv)(nkf_char c2,nkf_char c1) = no_connection;
523 static void (*o_fconv)(nkf_char c2,nkf_char c1) = no_connection;
524 static void (*o_eol_conv)(nkf_char c2,nkf_char c1) = no_connection;
525 static void (*o_rot_conv)(nkf_char c2,nkf_char c1) = no_connection;
526 static void (*o_hira_conv)(nkf_char c2,nkf_char c1) = no_connection;
527 static void (*o_base64conv)(nkf_char c2,nkf_char c1) = no_connection;
528 static void (*o_iso2022jp_check_conv)(nkf_char c2,nkf_char c1) = no_connection;
530 /* static redirections */
532 static void (*o_putc)(nkf_char c) = std_putc;
534 static nkf_char (*i_getc)(FILE *f) = std_getc; /* general input */
535 static nkf_char (*i_ungetc)(nkf_char c,FILE *f) =std_ungetc;
537 static nkf_char (*i_bgetc)(FILE *) = std_getc; /* input of mgetc */
538 static nkf_char (*i_bungetc)(nkf_char c ,FILE *f) = std_ungetc;
540 static void (*o_mputc)(nkf_char c) = std_putc ; /* output of mputc */
542 static nkf_char (*i_mgetc)(FILE *) = std_getc; /* input of mgetc */
543 static nkf_char (*i_mungetc)(nkf_char c ,FILE *f) = std_ungetc;
545 /* for strict mime */
546 static nkf_char (*i_mgetc_buf)(FILE *) = std_getc; /* input of mgetc_buf */
547 static nkf_char (*i_mungetc_buf)(nkf_char c,FILE *f) = std_ungetc;
550 static int output_mode = ASCII; /* output kanji mode */
551 static int input_mode = ASCII; /* input kanji mode */
552 static int mime_decode_mode = FALSE; /* MIME mode B base64, Q hex */
554 /* X0201 / X0208 conversion tables */
556 /* X0201 kana conversion table */
558 static const unsigned char cv[]= {
559 0x21,0x21,0x21,0x23,0x21,0x56,0x21,0x57,
560 0x21,0x22,0x21,0x26,0x25,0x72,0x25,0x21,
561 0x25,0x23,0x25,0x25,0x25,0x27,0x25,0x29,
562 0x25,0x63,0x25,0x65,0x25,0x67,0x25,0x43,
563 0x21,0x3c,0x25,0x22,0x25,0x24,0x25,0x26,
564 0x25,0x28,0x25,0x2a,0x25,0x2b,0x25,0x2d,
565 0x25,0x2f,0x25,0x31,0x25,0x33,0x25,0x35,
566 0x25,0x37,0x25,0x39,0x25,0x3b,0x25,0x3d,
567 0x25,0x3f,0x25,0x41,0x25,0x44,0x25,0x46,
568 0x25,0x48,0x25,0x4a,0x25,0x4b,0x25,0x4c,
569 0x25,0x4d,0x25,0x4e,0x25,0x4f,0x25,0x52,
570 0x25,0x55,0x25,0x58,0x25,0x5b,0x25,0x5e,
571 0x25,0x5f,0x25,0x60,0x25,0x61,0x25,0x62,
572 0x25,0x64,0x25,0x66,0x25,0x68,0x25,0x69,
573 0x25,0x6a,0x25,0x6b,0x25,0x6c,0x25,0x6d,
574 0x25,0x6f,0x25,0x73,0x21,0x2b,0x21,0x2c,
578 /* X0201 kana conversion table for daguten */
580 static const unsigned char dv[]= {
581 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
582 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
583 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
584 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
585 0x00,0x00,0x00,0x00,0x00,0x00,0x25,0x74,
586 0x00,0x00,0x00,0x00,0x25,0x2c,0x25,0x2e,
587 0x25,0x30,0x25,0x32,0x25,0x34,0x25,0x36,
588 0x25,0x38,0x25,0x3a,0x25,0x3c,0x25,0x3e,
589 0x25,0x40,0x25,0x42,0x25,0x45,0x25,0x47,
590 0x25,0x49,0x00,0x00,0x00,0x00,0x00,0x00,
591 0x00,0x00,0x00,0x00,0x25,0x50,0x25,0x53,
592 0x25,0x56,0x25,0x59,0x25,0x5c,0x00,0x00,
593 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
594 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
595 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
596 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
599 /* X0201 kana conversion table for han-daguten */
601 static const unsigned char ev[]= {
602 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
603 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
604 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
605 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
606 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
607 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
608 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
609 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
610 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
611 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
612 0x00,0x00,0x00,0x00,0x25,0x51,0x25,0x54,
613 0x25,0x57,0x25,0x5a,0x25,0x5d,0x00,0x00,
614 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
615 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
616 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
617 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
621 /* X0208 kigou conversion table */
622 /* 0x8140 - 0x819e */
623 static const unsigned char fv[] = {
625 0x00,0x00,0x00,0x00,0x2c,0x2e,0x00,0x3a,
626 0x3b,0x3f,0x21,0x00,0x00,0x27,0x60,0x00,
627 0x5e,0x00,0x5f,0x00,0x00,0x00,0x00,0x00,
628 0x00,0x00,0x00,0x00,0x00,0x2d,0x00,0x2f,
629 0x5c,0x00,0x00,0x7c,0x00,0x00,0x60,0x27,
630 0x22,0x22,0x28,0x29,0x00,0x00,0x5b,0x5d,
631 0x7b,0x7d,0x3c,0x3e,0x00,0x00,0x00,0x00,
632 0x00,0x00,0x00,0x00,0x2b,0x2d,0x00,0x00,
633 0x00,0x3d,0x00,0x3c,0x3e,0x00,0x00,0x00,
634 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
635 0x24,0x00,0x00,0x25,0x23,0x26,0x2a,0x40,
636 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
641 static int option_mode = 0;
642 static int file_out_f = FALSE;
644 static int overwrite_f = FALSE;
645 static int preserve_time_f = FALSE;
646 static int backup_f = FALSE;
647 static char *backup_suffix = "";
650 static int eolmode_f = 0; /* CR, LF, CRLF */
651 static int input_eol = 0; /* 0: unestablished, EOF: MIXED */
652 static nkf_char prev_cr = 0; /* CR or 0 */
653 #ifdef EASYWIN /*Easy Win */
654 static int end_check;
658 nkf_xmalloc(size_t size)
662 if (size == 0) size = 1;
666 perror("can't malloc");
674 nkf_xrealloc(void *ptr, size_t size)
676 if (size == 0) size = 1;
678 ptr = realloc(ptr, size);
680 perror("can't realloc");
687 #define nkf_xfree(ptr) free(ptr)
690 nkf_str_caseeql(const char *src, const char *target)
693 for (i = 0; src[i] && target[i]; i++) {
694 if (nkf_toupper(src[i]) != nkf_toupper(target[i])) return FALSE;
696 if (src[i] || target[i]) return FALSE;
701 nkf_enc_from_index(int idx)
703 if (idx < 0 || NKF_ENCODING_TABLE_SIZE <= idx) {
706 return &nkf_encoding_table[idx];
710 nkf_enc_find_index(const char *name)
713 if (name[0] == 'X' && *(name+1) == '-') name += 2;
714 for (i = 0; encoding_name_to_id_table[i].id >= 0; i++) {
715 if (nkf_str_caseeql(encoding_name_to_id_table[i].name, name)) {
716 return encoding_name_to_id_table[i].id;
723 nkf_enc_find(const char *name)
726 idx = nkf_enc_find_index(name);
727 if (idx < 0) return 0;
728 return nkf_enc_from_index(idx);
731 #define nkf_enc_name(enc) (enc)->name
732 #define nkf_enc_to_index(enc) (enc)->id
733 #define nkf_enc_to_base_encoding(enc) (enc)->base_encoding
734 #define nkf_enc_to_iconv(enc) nkf_enc_to_base_encoding(enc)->iconv
735 #define nkf_enc_to_oconv(enc) nkf_enc_to_base_encoding(enc)->oconv
736 #define nkf_enc_asciicompat(enc) (\
737 nkf_enc_to_base_encoding(enc) == &NkfEncodingASCII ||\
738 nkf_enc_to_base_encoding(enc) == &NkfEncodingISO_2022_JP)
739 #define nkf_enc_unicode_p(enc) (\
740 nkf_enc_to_base_encoding(enc) == &NkfEncodingUTF_8 ||\
741 nkf_enc_to_base_encoding(enc) == &NkfEncodingUTF_16 ||\
742 nkf_enc_to_base_encoding(enc) == &NkfEncodingUTF_32)
743 #define nkf_enc_cp5022x_p(enc) (\
744 nkf_enc_to_index(enc) == CP50220 ||\
745 nkf_enc_to_index(enc) == CP50221 ||\
746 nkf_enc_to_index(enc) == CP50222)
748 #ifdef DEFAULT_CODE_LOCALE
752 #ifdef HAVE_LANGINFO_H
753 return nl_langinfo(CODESET);
754 #elif defined(__WIN32__)
756 sprintf(buf, "CP%d", GetACP());
758 #elif defined(__OS2__)
759 # if defined(INT_IS_SHORT)
765 ULONG ulCP[1], ulncp;
766 DosQueryCp(sizeof(ulCP), ulCP, &ulncp);
767 if (ulCP[0] == 932 || ulCP[0] == 943)
768 strcpy(buf, "Shift_JIS");
770 sprintf(buf, "CP%lu", ulCP[0]);
778 nkf_locale_encoding()
780 nkf_encoding *enc = 0;
781 const char *encname = nkf_locale_charmap();
783 enc = nkf_enc_find(encname);
786 #endif /* DEFAULT_CODE_LOCALE */
791 return &nkf_encoding_table[UTF_8];
795 nkf_default_encoding()
797 nkf_encoding *enc = 0;
798 #ifdef DEFAULT_CODE_LOCALE
799 enc = nkf_locale_encoding();
800 #elif defined(DEFAULT_ENCIDX)
801 enc = nkf_enc_from_index(DEFAULT_ENCIDX);
803 if (!enc) enc = nkf_utf8_encoding();
814 nkf_buf_new(int length)
816 nkf_buf_t *buf = nkf_xmalloc(sizeof(nkf_buf_t));
817 buf->ptr = nkf_xmalloc(length);
825 nkf_buf_dispose(nkf_buf_t *buf)
832 #define nkf_buf_length(buf) ((buf)->len)
833 #define nkf_buf_empty_p(buf) ((buf)->len == 0)
836 nkf_buf_at(nkf_buf_t *buf, int index)
838 assert(index <= buf->len);
839 return buf->ptr[index];
843 nkf_buf_clear(nkf_buf_t *buf)
849 nkf_buf_push(nkf_buf_t *buf, nkf_char c)
851 if (buf->capa <= buf->len) {
854 buf->ptr[buf->len++] = c;
858 nkf_buf_pop(nkf_buf_t *buf)
860 assert(!nkf_buf_empty_p(buf));
861 return buf->ptr[--buf->len];
864 /* Normalization Form C */
867 #define fprintf dllprintf
873 fprintf(HELP_OUTPUT,"Network Kanji Filter Version " NKF_VERSION " (" NKF_RELEASE_DATE ") \n" COPY_RIGHT "\n");
880 "Usage: nkf -[flags] [--] [in file] .. [out file for -O flag]\n"
881 #ifdef UTF8_OUTPUT_ENABLE
882 " j/s/e/w Specify output encoding ISO-2022-JP, Shift_JIS, EUC-JP\n"
883 " UTF options is -w[8[0],{16,32}[{B,L}[0]]]\n"
886 #ifdef UTF8_INPUT_ENABLE
887 " J/S/E/W Specify input encoding ISO-2022-JP, Shift_JIS, EUC-JP\n"
888 " UTF option is -W[8,[16,32][B,L]]\n"
890 " J/S/E Specify output encoding ISO-2022-JP, Shift_JIS, EUC-JP\n"
894 " m[BQSN0] MIME decode [B:base64,Q:quoted,S:strict,N:nonstrict,0:no decode]\n"
895 " M[BQ] MIME encode [B:base64 Q:quoted]\n"
896 " f/F Folding: -f60 or -f or -f60-10 (fold margin 10) F preserve nl\n"
899 " Z[0-4] Default/0: Convert JISX0208 Alphabet to ASCII\n"
900 " 1: Kankaku to one space 2: to two spaces 3: HTML Entity\n"
901 " 4: JISX0208 Katakana to JISX0201 Katakana\n"
902 " X,x Assume X0201 kana in MS-Kanji, -x preserves X0201\n"
905 " O Output to File (DEFAULT 'nkf.out')\n"
906 " L[uwm] Line mode u:LF w:CRLF m:CR (DEFAULT noconversion)\n"
909 " --ic=<encoding> Specify the input encoding\n"
910 " --oc=<encoding> Specify the output encoding\n"
911 " --hiragana --katakana Hiragana/Katakana Conversion\n"
912 " --katakana-hiragana Converts each other\n"
916 " --{cap, url}-input Convert hex after ':' or '%%'\n"
918 #ifdef NUMCHAR_OPTION
919 " --numchar-input Convert Unicode Character Reference\n"
921 #ifdef UTF8_INPUT_ENABLE
922 " --fb-{skip, html, xml, perl, java, subchar}\n"
923 " Specify unassigned character's replacement\n"
928 " --in-place[=SUF] Overwrite original files\n"
929 " --overwrite[=SUF] Preserve timestamp of original files\n"
931 " -g --guess Guess the input code\n"
932 " -v --version Print the version\n"
933 " --help/-V Print this help / configuration\n"
939 show_configuration(void)
942 "Summary of my nkf " NKF_VERSION " (" NKF_RELEASE_DATE ") configuration:\n"
943 " Compile-time options:\n"
944 " Compiled at: " __DATE__ " " __TIME__ "\n"
947 " Default output encoding: "
948 #ifdef DEFAULT_CODE_LOCALE
949 "LOCALE (%s)\n", nkf_enc_name(nkf_default_encoding())
950 #elif defined(DEFAULT_ENCIDX)
951 "CONFIG (%s)\n", nkf_enc_name(nkf_default_encoding())
957 " Default output end of line: "
958 #if DEFAULT_NEWLINE == CR
960 #elif DEFAULT_NEWLINE == CRLF
966 " Decode MIME encoded string: "
967 #if MIME_DECODE_DEFAULT
973 " Convert JIS X 0201 Katakana: "
980 " --help, --version output: "
981 #if HELP_OUTPUT_HELP_OUTPUT
992 get_backup_filename(const char *suffix, const char *filename)
994 char *backup_filename;
995 int asterisk_count = 0;
997 int filename_length = strlen(filename);
999 for(i = 0; suffix[i]; i++){
1000 if(suffix[i] == '*') asterisk_count++;
1004 backup_filename = nkf_xmalloc(strlen(suffix) + (asterisk_count * (filename_length - 1)) + 1);
1005 for(i = 0, j = 0; suffix[i];){
1006 if(suffix[i] == '*'){
1007 backup_filename[j] = '\0';
1008 strncat(backup_filename, filename, filename_length);
1010 j += filename_length;
1012 backup_filename[j++] = suffix[i++];
1015 backup_filename[j] = '\0';
1017 j = filename_length + strlen(suffix);
1018 backup_filename = nkf_xmalloc(j + 1);
1019 strcpy(backup_filename, filename);
1020 strcat(backup_filename, suffix);
1021 backup_filename[j] = '\0';
1023 return backup_filename;
1027 #ifdef UTF8_INPUT_ENABLE
1029 nkf_each_char_to_hex(void (*f)(nkf_char c2,nkf_char c1), nkf_char c)
1034 if(c >= NKF_INT32_C(1)<<shift){
1036 (*f)(0, bin2hex(c>>shift));
1047 encode_fallback_html(nkf_char c)
1052 if(c >= NKF_INT32_C(1000000))
1053 (*oconv)(0, 0x30+(c/NKF_INT32_C(1000000))%10);
1054 if(c >= NKF_INT32_C(100000))
1055 (*oconv)(0, 0x30+(c/NKF_INT32_C(100000) )%10);
1057 (*oconv)(0, 0x30+(c/10000 )%10);
1059 (*oconv)(0, 0x30+(c/1000 )%10);
1061 (*oconv)(0, 0x30+(c/100 )%10);
1063 (*oconv)(0, 0x30+(c/10 )%10);
1065 (*oconv)(0, 0x30+ c %10);
1071 encode_fallback_xml(nkf_char c)
1076 nkf_each_char_to_hex(oconv, c);
1082 encode_fallback_java(nkf_char c)
1086 if(!nkf_char_unicode_bmp_p(c)){
1090 (*oconv)(0, bin2hex(c>>20));
1091 (*oconv)(0, bin2hex(c>>16));
1095 (*oconv)(0, bin2hex(c>>12));
1096 (*oconv)(0, bin2hex(c>> 8));
1097 (*oconv)(0, bin2hex(c>> 4));
1098 (*oconv)(0, bin2hex(c ));
1103 encode_fallback_perl(nkf_char c)
1108 nkf_each_char_to_hex(oconv, c);
1114 encode_fallback_subchar(nkf_char c)
1116 c = unicode_subchar;
1117 (*oconv)((c>>8)&0xFF, c&0xFF);
1122 static const struct {
1146 {"katakana-hiragana","h3"},
1154 #ifdef UTF8_OUTPUT_ENABLE
1164 {"fb-subchar=", ""},
1166 #ifdef UTF8_INPUT_ENABLE
1167 {"utf8-input", "W"},
1168 {"utf16-input", "W16"},
1169 {"no-cp932ext", ""},
1170 {"no-best-fit-chars",""},
1172 #ifdef UNICODE_NORMALIZATION
1173 {"utf8mac-input", ""},
1185 #ifdef NUMCHAR_OPTION
1186 {"numchar-input", ""},
1192 #ifdef SHIFTJIS_CP932
1203 set_input_encoding(nkf_encoding *enc)
1205 switch (nkf_enc_to_index(enc)) {
1213 #ifdef SHIFTJIS_CP932
1216 #ifdef UTF8_OUTPUT_ENABLE
1217 ms_ucs_map_f = UCS_MAP_CP932;
1227 case ISO_2022_JP_2004:
1235 #ifdef SHIFTJIS_CP932
1238 #ifdef UTF8_OUTPUT_ENABLE
1239 ms_ucs_map_f = UCS_MAP_CP932;
1244 #ifdef SHIFTJIS_CP932
1247 #ifdef UTF8_OUTPUT_ENABLE
1248 ms_ucs_map_f = UCS_MAP_CP10001;
1257 #ifdef SHIFTJIS_CP932
1260 #ifdef UTF8_OUTPUT_ENABLE
1261 ms_ucs_map_f = UCS_MAP_CP932;
1265 #ifdef SHIFTJIS_CP932
1268 #ifdef UTF8_OUTPUT_ENABLE
1269 ms_ucs_map_f = UCS_MAP_MS;
1273 #ifdef SHIFTJIS_CP932
1276 #ifdef UTF8_OUTPUT_ENABLE
1277 ms_ucs_map_f = UCS_MAP_ASCII;
1280 case SHIFT_JISX0213:
1281 case SHIFT_JIS_2004:
1283 #ifdef SHIFTJIS_CP932
1290 #ifdef SHIFTJIS_CP932
1294 #ifdef UTF8_INPUT_ENABLE
1295 #ifdef UNICODE_NORMALIZATION
1303 input_endian = ENDIAN_BIG;
1307 input_endian = ENDIAN_LITTLE;
1312 input_endian = ENDIAN_BIG;
1316 input_endian = ENDIAN_LITTLE;
1323 set_output_encoding(nkf_encoding *enc)
1325 switch (nkf_enc_to_index(enc)) {
1328 #ifdef SHIFTJIS_CP932
1329 if (cp932inv_f == TRUE) cp932inv_f = FALSE;
1331 #ifdef UTF8_OUTPUT_ENABLE
1332 ms_ucs_map_f = UCS_MAP_CP932;
1337 #ifdef SHIFTJIS_CP932
1338 if (cp932inv_f == TRUE) cp932inv_f = FALSE;
1340 #ifdef UTF8_OUTPUT_ENABLE
1341 ms_ucs_map_f = UCS_MAP_CP932;
1345 #ifdef SHIFTJIS_CP932
1346 if (cp932inv_f == TRUE) cp932inv_f = FALSE;
1351 #ifdef SHIFTJIS_CP932
1352 if (cp932inv_f == TRUE) cp932inv_f = FALSE;
1358 #ifdef SHIFTJIS_CP932
1359 if (cp932inv_f == TRUE) cp932inv_f = FALSE;
1366 #ifdef UTF8_OUTPUT_ENABLE
1367 ms_ucs_map_f = UCS_MAP_CP932;
1371 #ifdef UTF8_OUTPUT_ENABLE
1372 ms_ucs_map_f = UCS_MAP_CP10001;
1377 #ifdef SHIFTJIS_CP932
1378 if (cp932inv_f == TRUE) cp932inv_f = FALSE;
1380 #ifdef UTF8_OUTPUT_ENABLE
1381 ms_ucs_map_f = UCS_MAP_ASCII;
1386 #ifdef SHIFTJIS_CP932
1387 if (cp932inv_f == TRUE) cp932inv_f = FALSE;
1389 #ifdef UTF8_OUTPUT_ENABLE
1390 ms_ucs_map_f = UCS_MAP_ASCII;
1395 #ifdef SHIFTJIS_CP932
1396 if (cp932inv_f == TRUE) cp932inv_f = FALSE;
1398 #ifdef UTF8_OUTPUT_ENABLE
1399 ms_ucs_map_f = UCS_MAP_CP932;
1404 #ifdef UTF8_OUTPUT_ENABLE
1405 ms_ucs_map_f = UCS_MAP_MS;
1410 #ifdef UTF8_OUTPUT_ENABLE
1411 ms_ucs_map_f = UCS_MAP_ASCII;
1414 case SHIFT_JISX0213:
1415 case SHIFT_JIS_2004:
1417 #ifdef SHIFTJIS_CP932
1418 if (cp932inv_f == TRUE) cp932inv_f = FALSE;
1425 #ifdef SHIFTJIS_CP932
1426 if (cp932inv_f == TRUE) cp932inv_f = FALSE;
1429 #ifdef UTF8_OUTPUT_ENABLE
1431 output_bom_f = TRUE;
1435 output_bom_f = TRUE;
1438 output_endian = ENDIAN_LITTLE;
1439 output_bom_f = FALSE;
1442 output_endian = ENDIAN_LITTLE;
1443 output_bom_f = TRUE;
1447 output_bom_f = TRUE;
1450 output_endian = ENDIAN_LITTLE;
1451 output_bom_f = FALSE;
1454 output_endian = ENDIAN_LITTLE;
1455 output_bom_f = TRUE;
1461 static struct input_code*
1462 find_inputcode_byfunc(nkf_char (*iconv_func)(nkf_char c2,nkf_char c1,nkf_char c0))
1465 struct input_code *p = input_code_list;
1467 if (iconv_func == p->iconv_func){
1477 set_iconv(nkf_char f, nkf_char (*iconv_func)(nkf_char c2,nkf_char c1,nkf_char c0))
1479 #ifdef INPUT_CODE_FIX
1480 if (f || !input_encoding)
1487 #ifdef INPUT_CODE_FIX
1488 && (f == -TRUE || !input_encoding) /* -TRUE means "FORCE" */
1494 if (estab_f && iconv_for_check != iconv){
1495 struct input_code *p = find_inputcode_byfunc(iconv);
1497 set_input_codename(p->name);
1500 iconv_for_check = iconv;
1507 x0212_shift(nkf_char c)
1512 if (0x75 <= c && c <= 0x7f){
1513 ret = c + (0x109 - 0x75);
1516 if (0x75 <= c && c <= 0x7f){
1517 ret = c + (0x113 - 0x75);
1525 x0212_unshift(nkf_char c)
1528 if (0x7f <= c && c <= 0x88){
1529 ret = c + (0x75 - 0x7f);
1530 }else if (0x89 <= c && c <= 0x92){
1531 ret = PREFIX_EUCG3 | 0x80 | (c + (0x75 - 0x89));
1535 #endif /* X0212_ENABLE */
1538 e2s_conv(nkf_char c2, nkf_char c1, nkf_char *p2, nkf_char *p1)
1544 if((0x21 <= ndx && ndx <= 0x2F)){
1545 if (p2) *p2 = ((ndx - 1) >> 1) + 0xec - ndx / 8 * 3;
1546 if (p1) *p1 = c1 + ((ndx & 1) ? ((c1 < 0x60) ? 0x1f : 0x20) : 0x7e);
1548 }else if(0x6E <= ndx && ndx <= 0x7E){
1549 if (p2) *p2 = ((ndx - 1) >> 1) + 0xbe;
1550 if (p1) *p1 = c1 + ((ndx & 1) ? ((c1 < 0x60) ? 0x1f : 0x20) : 0x7e);
1556 else if(nkf_isgraph(ndx)){
1558 const unsigned short *ptr;
1559 ptr = x0212_shiftjis[ndx - 0x21];
1561 val = ptr[(c1 & 0x7f) - 0x21];
1570 c2 = x0212_shift(c2);
1572 #endif /* X0212_ENABLE */
1574 if(0x7F < c2) return 1;
1575 if (p2) *p2 = ((c2 - 1) >> 1) + ((c2 <= 0x5e) ? 0x71 : 0xb1);
1576 if (p1) *p1 = c1 + ((c2 & 1) ? ((c1 < 0x60) ? 0x1f : 0x20) : 0x7e);
1581 s2e_conv(nkf_char c2, nkf_char c1, nkf_char *p2, nkf_char *p1)
1583 #if defined(SHIFTJIS_CP932) || defined(X0212_ENABLE)
1586 static const char shift_jisx0213_s1a3_table[5][2] ={ { 1, 8}, { 3, 4}, { 5,12}, {13,14}, {15, 0} };
1587 if (0xFC < c1) return 1;
1588 #ifdef SHIFTJIS_CP932
1589 if (!cp932inv_f && is_ibmext_in_sjis(c2)){
1590 val = shiftjis_cp932[c2 - CP932_TABLE_BEGIN][c1 - 0x40];
1597 && CP932INV_TABLE_BEGIN <= c2 && c2 <= CP932INV_TABLE_END){
1598 val = cp932inv[c2 - CP932INV_TABLE_BEGIN][c1 - 0x40];
1604 #endif /* SHIFTJIS_CP932 */
1606 if (!x0213_f && is_ibmext_in_sjis(c2)){
1607 val = shiftjis_x0212[c2 - 0xfa][c1 - 0x40];
1610 c2 = PREFIX_EUCG3 | ((val >> 8) & 0x7f);
1623 if(x0213_f && c2 >= 0xF0){
1624 if(c2 <= 0xF3 || (c2 == 0xF4 && c1 < 0x9F)){ /* k=1, 3<=k<=5, k=8, 12<=k<=15 */
1625 c2 = PREFIX_EUCG3 | 0x20 | shift_jisx0213_s1a3_table[c2 - 0xF0][0x9E < c1];
1626 }else{ /* 78<=k<=94 */
1627 c2 = PREFIX_EUCG3 | (c2 * 2 - 0x17B);
1628 if (0x9E < c1) c2++;
1631 #define SJ0162 0x00e1 /* 01 - 62 ku offset */
1632 #define SJ6394 0x0161 /* 63 - 94 ku offset */
1633 c2 = c2 + c2 - ((c2 <= 0x9F) ? SJ0162 : SJ6394);
1634 if (0x9E < c1) c2++;
1637 c1 = c1 - ((c1 > DEL) ? SP : 0x1F);
1644 c2 = x0212_unshift(c2);
1651 #if defined(UTF8_INPUT_ENABLE) || defined(UTF8_OUTPUT_ENABLE)
1653 nkf_unicode_to_utf8(nkf_char val, nkf_char *p1, nkf_char *p2, nkf_char *p3, nkf_char *p4)
1661 }else if (val < 0x800){
1662 *p1 = 0xc0 | (val >> 6);
1663 *p2 = 0x80 | (val & 0x3f);
1666 } else if (nkf_char_unicode_bmp_p(val)) {
1667 *p1 = 0xe0 | (val >> 12);
1668 *p2 = 0x80 | ((val >> 6) & 0x3f);
1669 *p3 = 0x80 | ( val & 0x3f);
1671 } else if (nkf_char_unicode_value_p(val)) {
1672 *p1 = 0xf0 | (val >> 18);
1673 *p2 = 0x80 | ((val >> 12) & 0x3f);
1674 *p3 = 0x80 | ((val >> 6) & 0x3f);
1675 *p4 = 0x80 | ( val & 0x3f);
1685 nkf_utf8_to_unicode(nkf_char c1, nkf_char c2, nkf_char c3, nkf_char c4)
1692 else if (c1 <= 0xC3) {
1693 /* trail byte or invalid */
1696 else if (c1 <= 0xDF) {
1698 wc = (c1 & 0x1F) << 6;
1701 else if (c1 <= 0xEF) {
1703 wc = (c1 & 0x0F) << 12;
1704 wc |= (c2 & 0x3F) << 6;
1707 else if (c2 <= 0xF4) {
1709 wc = (c1 & 0x0F) << 18;
1710 wc |= (c2 & 0x3F) << 12;
1711 wc |= (c3 & 0x3F) << 6;
1721 #ifdef UTF8_INPUT_ENABLE
1723 unicode_to_jis_common2(nkf_char c1, nkf_char c0,
1724 const unsigned short *const *pp, nkf_char psize,
1725 nkf_char *p2, nkf_char *p1)
1728 const unsigned short *p;
1731 if (pp == 0) return 1;
1734 if (c1 < 0 || psize <= c1) return 1;
1736 if (p == 0) return 1;
1739 if (c0 < 0 || sizeof_utf8_to_euc_C2 <= c0) return 1;
1741 if (val == 0) return 1;
1742 if (no_cp932ext_f && (
1743 (val>>8) == 0x2D || /* NEC special characters */
1744 val > NKF_INT32_C(0xF300) /* IBM extended characters */
1752 if (c2 == SO) c2 = JIS_X_0201_1976_K;
1760 unicode_to_jis_common(nkf_char c2, nkf_char c1, nkf_char c0, nkf_char *p2, nkf_char *p1)
1762 const unsigned short *const *pp;
1763 const unsigned short *const *const *ppp;
1764 static const char no_best_fit_chars_table_C2[] =
1765 {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1766 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1767 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 2, 1, 1, 2,
1768 0, 0, 1, 1, 0, 1, 0, 1, 2, 1, 1, 1, 1, 1, 1, 1};
1769 static const char no_best_fit_chars_table_C2_ms[] =
1770 {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1771 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1772 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0,
1773 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0};
1774 static const char no_best_fit_chars_table_932_C2[] =
1775 {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1776 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1777 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1,
1778 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0};
1779 static const char no_best_fit_chars_table_932_C3[] =
1780 {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1781 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1,
1782 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1783 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1};
1789 }else if(c2 < 0xe0){
1790 if(no_best_fit_chars_f){
1791 if(ms_ucs_map_f == UCS_MAP_CP932){
1794 if(no_best_fit_chars_table_932_C2[c1&0x3F]) return 1;
1797 if(no_best_fit_chars_table_932_C3[c1&0x3F]) return 1;
1800 }else if(!cp932inv_f){
1803 if(no_best_fit_chars_table_C2[c1&0x3F]) return 1;
1806 if(no_best_fit_chars_table_932_C3[c1&0x3F]) return 1;
1809 }else if(ms_ucs_map_f == UCS_MAP_MS){
1810 if(c2 == 0xC2 && no_best_fit_chars_table_C2_ms[c1&0x3F]) return 1;
1811 }else if(ms_ucs_map_f == UCS_MAP_CP10001){
1829 ms_ucs_map_f == UCS_MAP_CP932 ? utf8_to_euc_2bytes_932 :
1830 ms_ucs_map_f == UCS_MAP_MS ? utf8_to_euc_2bytes_ms :
1831 ms_ucs_map_f == UCS_MAP_CP10001 ? utf8_to_euc_2bytes_mac :
1833 ret = unicode_to_jis_common2(c2, c1, pp, sizeof_utf8_to_euc_2bytes, p2, p1);
1834 }else if(c0 < 0xF0){
1835 if(no_best_fit_chars_f){
1836 if(ms_ucs_map_f == UCS_MAP_CP932){
1837 if(c2 == 0xE3 && c1 == 0x82 && c0 == 0x94) return 1;
1838 }else if(ms_ucs_map_f == UCS_MAP_MS){
1843 if(c0 == 0x94 || c0 == 0x96 || c0 == 0xBE) return 1;
1846 if(c0 == 0x92) return 1;
1851 if(c1 == 0x80 || c0 == 0x9C) return 1;
1854 }else if(ms_ucs_map_f == UCS_MAP_CP10001){
1859 if(c0 == 0x94) return 1;
1862 if(c0 == 0xBB) return 1;
1872 if(c0 == 0x95) return 1;
1875 if(c0 == 0xA5) return 1;
1882 if(c0 == 0x8D) return 1;
1885 if(c0 == 0x9E && !cp932inv_f) return 1;
1888 if(0xA0 <= c0 && c0 <= 0xA5) return 1;
1896 ms_ucs_map_f == UCS_MAP_CP932 ? utf8_to_euc_3bytes_932 :
1897 ms_ucs_map_f == UCS_MAP_MS ? utf8_to_euc_3bytes_ms :
1898 ms_ucs_map_f == UCS_MAP_CP10001 ? utf8_to_euc_3bytes_mac :
1900 ret = unicode_to_jis_common2(c1, c0, ppp[c2 - 0xE0], sizeof_utf8_to_euc_C2, p2, p1);
1902 #ifdef SHIFTJIS_CP932
1903 if (!ret && !cp932inv_f && is_eucg3(*p2)) {
1905 if (e2s_conv(*p2, *p1, &s2, &s1) == 0) {
1906 s2e_conv(s2, s1, p2, p1);
1915 #ifdef UTF8_OUTPUT_ENABLE
1917 e2w_conv(nkf_char c2, nkf_char c1)
1919 const unsigned short *p;
1921 if (c2 == JIS_X_0201_1976_K) {
1922 if (ms_ucs_map_f == UCS_MAP_CP10001) {
1930 p = euc_to_utf8_1byte;
1932 } else if (is_eucg3(c2)){
1933 if(ms_ucs_map_f == UCS_MAP_ASCII&& c2 == NKF_INT32_C(0x8F22) && c1 == 0x43){
1936 c2 = (c2&0x7f) - 0x21;
1937 if (0<=c2 && c2<sizeof_euc_to_utf8_2bytes)
1938 p = x0212_to_utf8_2bytes[c2];
1944 c2 = (c2&0x7f) - 0x21;
1945 if (0<=c2 && c2<sizeof_euc_to_utf8_2bytes)
1947 ms_ucs_map_f == UCS_MAP_ASCII ? euc_to_utf8_2bytes[c2] :
1948 ms_ucs_map_f == UCS_MAP_CP10001 ? euc_to_utf8_2bytes_mac[c2] :
1949 euc_to_utf8_2bytes_ms[c2];
1954 c1 = (c1 & 0x7f) - 0x21;
1955 if (0<=c1 && c1<sizeof_euc_to_utf8_1byte)
1962 w2e_conv(nkf_char c2, nkf_char c1, nkf_char c0, nkf_char *p2, nkf_char *p1)
1969 }else if (0xc0 <= c2 && c2 <= 0xef) {
1970 ret = unicode_to_jis_common(c2, c1, c0, p2, p1);
1971 #ifdef NUMCHAR_OPTION
1974 if (p1) *p1 = nkf_char_unicode_new(nkf_utf8_to_unicode(c2, c1, c0, 0));
1982 #ifdef UTF8_INPUT_ENABLE
1984 w16e_conv(nkf_char val, nkf_char *p2, nkf_char *p1)
1986 nkf_char c1, c2, c3, c4;
1993 else if (nkf_char_unicode_bmp_p(val)){
1994 nkf_unicode_to_utf8(val, &c1, &c2, &c3, &c4);
1995 ret = unicode_to_jis_common(c1, c2, c3, p2, p1);
1998 *p1 = nkf_char_unicode_new(val);
2004 *p1 = nkf_char_unicode_new(val);
2011 e_iconv(nkf_char c2, nkf_char c1, nkf_char c0)
2013 if (c2 == JIS_X_0201_1976_K || c2 == SS2){
2014 if (iso2022jp_f && !x0201_f) {
2015 c2 = GETA1; c1 = GETA2;
2017 c2 = JIS_X_0201_1976_K;
2021 }else if (c2 == 0x8f){
2025 if (!cp51932_f && !x0213_f && 0xF5 <= c1 && c1 <= 0xFE && 0xA1 <= c0 && c0 <= 0xFE) {
2026 /* encoding is eucJP-ms, so invert to Unicode Private User Area */
2027 c1 = nkf_char_unicode_new((c1 - 0xF5) * 94 + c0 - 0xA1 + 0xE3AC);
2030 c2 = (c2 << 8) | (c1 & 0x7f);
2032 #ifdef SHIFTJIS_CP932
2035 if (e2s_conv(c2, c1, &s2, &s1) == 0){
2036 s2e_conv(s2, s1, &c2, &c1);
2043 #endif /* SHIFTJIS_CP932 */
2045 #endif /* X0212_ENABLE */
2046 } else if ((c2 == EOF) || (c2 == 0) || c2 < SP || c2 == ISO_8859_1) {
2049 if (!cp51932_f && ms_ucs_map_f && 0xF5 <= c2 && c2 <= 0xFE && 0xA1 <= c1 && c1 <= 0xFE) {
2050 /* encoding is eucJP-ms, so invert to Unicode Private User Area */
2051 c1 = nkf_char_unicode_new((c2 - 0xF5) * 94 + c1 - 0xA1 + 0xE000);
2056 #ifdef SHIFTJIS_CP932
2057 if (cp51932_f && 0x79 <= c2 && c2 <= 0x7c){
2059 if (e2s_conv(c2, c1, &s2, &s1) == 0){
2060 s2e_conv(s2, s1, &c2, &c1);
2067 #endif /* SHIFTJIS_CP932 */
2075 s_iconv(nkf_char c2, nkf_char c1, nkf_char c0)
2077 if (c2 == JIS_X_0201_1976_K || (0xA1 <= c2 && c2 <= 0xDF)) {
2078 if (iso2022jp_f && !x0201_f) {
2079 c2 = GETA1; c1 = GETA2;
2083 } else if ((c2 == EOF) || (c2 == 0) || c2 < SP) {
2085 } else if (!x0213_f && 0xF0 <= c2 && c2 <= 0xF9 && 0x40 <= c1 && c1 <= 0xFC) {
2087 if(c1 == 0x7F) return 0;
2088 c1 = nkf_char_unicode_new((c2 - 0xF0) * 188 + (c1 - 0x40 - (0x7E < c1)) + 0xE000);
2091 nkf_char ret = s2e_conv(c2, c1, &c2, &c1);
2092 if (ret) return ret;
2099 w_iconv(nkf_char c1, nkf_char c2, nkf_char c3)
2101 nkf_char ret = 0, c4 = 0;
2102 static const char w_iconv_utf8_1st_byte[] =
2104 20, 20, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
2105 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
2106 30, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 32, 33, 33,
2107 40, 41, 41, 41, 42, 43, 43, 43, 50, 50, 50, 50, 60, 60, 70, 70};
2114 if (c1 < 0 || 0xff < c1) {
2115 }else if (c1 == 0) { /* 0 : 1 byte*/
2117 } else if ((c1 & 0xC0) == 0x80) { /* 0x80-0xbf : trail byte */
2120 switch (w_iconv_utf8_1st_byte[c1 - 0xC0]) {
2122 if (c2 < 0x80 || 0xBF < c2) return 0;
2125 if (c3 == 0) return -1;
2126 if (c2 < 0xA0 || 0xBF < c2 || (c3 & 0xC0) != 0x80)
2131 if (c3 == 0) return -1;
2132 if ((c2 & 0xC0) != 0x80 || (c3 & 0xC0) != 0x80)
2136 if (c3 == 0) return -1;
2137 if (c2 < 0x80 || 0x9F < c2 || (c3 & 0xC0) != 0x80)
2141 if (c3 == 0) return -2;
2142 if (c2 < 0x90 || 0xBF < c2 || (c3 & 0xC0) != 0x80 || (c4 & 0xC0) != 0x80)
2146 if (c3 == 0) return -2;
2147 if (c2 < 0x80 || 0xBF < c2 || (c3 & 0xC0) != 0x80 || (c4 & 0xC0) != 0x80)
2151 if (c3 == 0) return -2;
2152 if (c2 < 0x80 || 0x8F < c2 || (c3 & 0xC0) != 0x80 || (c4 & 0xC0) != 0x80)
2160 if (c1 == 0 || c1 == EOF){
2161 } else if ((c1 & 0xf8) == 0xf0) { /* 4 bytes */
2162 c2 = nkf_char_unicode_new(nkf_utf8_to_unicode(c1, c2, c3, c4));
2165 ret = w2e_conv(c1, c2, c3, &c1, &c2);
2173 #define NKF_ICONV_INVALID_CODE_RANGE -13
2175 unicode_iconv(nkf_char wc)
2183 }else if ((wc>>11) == 27) {
2184 /* unpaired surrogate */
2185 return NKF_ICONV_INVALID_CODE_RANGE;
2186 }else if (wc < 0xFFFF) {
2187 ret = w16e_conv(wc, &c2, &c1);
2188 if (ret) return ret;
2189 }else if (wc < 0x10FFFF) {
2191 c1 = nkf_char_unicode_new(wc);
2193 return NKF_ICONV_INVALID_CODE_RANGE;
2199 #define NKF_ICONV_NEED_ONE_MORE_BYTE -1
2200 #define NKF_ICONV_NEED_TWO_MORE_BYTES -2
2201 #define UTF16_TO_UTF32(lead, trail) (((lead) << 10) + (trail) - NKF_INT32_C(0x35FDC00))
2203 nkf_iconv_utf_16(nkf_char c1, nkf_char c2, nkf_char c3, nkf_char c4)
2212 if (input_endian == ENDIAN_BIG) {
2213 if (0xD8 <= c1 && c1 <= 0xDB) {
2214 if (0xDC <= c3 && c3 <= 0xDF) {
2215 wc = UTF16_TO_UTF32(c1 << 8 | c2, c3 << 8 | c4);
2216 } else return NKF_ICONV_NEED_TWO_MORE_BYTES;
2221 if (0xD8 <= c2 && c2 <= 0xDB) {
2222 if (0xDC <= c4 && c4 <= 0xDF) {
2223 wc = UTF16_TO_UTF32(c2 << 8 | c1, c4 << 8 | c3);
2224 } else return NKF_ICONV_NEED_TWO_MORE_BYTES;
2230 return (*unicode_iconv)(wc);
2234 w_iconv16(nkf_char c2, nkf_char c1, nkf_char c0)
2237 return 16; /* different from w_iconv32 */
2241 w_iconv32(nkf_char c2, nkf_char c1, nkf_char c0)
2244 return 32; /* different from w_iconv16 */
2248 nkf_iconv_utf_32(nkf_char c1, nkf_char c2, nkf_char c3, nkf_char c4)
2257 switch(input_endian){
2259 wc = c2 << 16 | c3 << 8 | c4;
2262 wc = c3 << 16 | c2 << 8 | c1;
2265 wc = c1 << 16 | c4 << 8 | c3;
2268 wc = c4 << 16 | c1 << 8 | c2;
2271 return NKF_ICONV_INVALID_CODE_RANGE;
2274 return (*unicode_iconv)(wc);
2278 #define output_ascii_escape_sequence(mode) do { \
2279 if (output_mode != ASCII && output_mode != ISO_8859_1) { \
2282 (*o_putc)(ascii_intro); \
2283 output_mode = mode; \
2288 output_escape_sequence(int mode)
2290 if (output_mode == mode)
2298 case JIS_X_0201_1976_K:
2306 (*o_putc)(kanji_intro);
2331 j_oconv(nkf_char c2, nkf_char c1)
2333 #ifdef NUMCHAR_OPTION
2334 if (c2 == 0 && nkf_char_unicode_p(c1)){
2335 w16e_conv(c1, &c2, &c1);
2336 if (c2 == 0 && nkf_char_unicode_p(c1)){
2337 c2 = c1 & VALUE_MASK;
2338 if (ms_ucs_map_f && 0xE000 <= c2 && c2 <= 0xE757) {
2341 c2 = 0x7F + c1 / 94;
2342 c1 = 0x21 + c1 % 94;
2344 if (encode_fallback) (*encode_fallback)(c1);
2351 output_ascii_escape_sequence(ASCII);
2354 else if (c2 == EOF) {
2355 output_ascii_escape_sequence(ASCII);
2358 else if (c2 == ISO_8859_1) {
2359 output_ascii_escape_sequence(ISO_8859_1);
2362 else if (c2 == JIS_X_0201_1976_K) {
2363 output_escape_sequence(JIS_X_0201_1976_K);
2366 } else if (is_eucg3(c2)){
2367 output_escape_sequence(x0213_f ? JIS_X_0213_2 : JIS_X_0212);
2368 (*o_putc)(c2 & 0x7f);
2373 ? c2<0x20 || 0x92<c2 || c1<0x20 || 0x7e<c1
2374 : c2<0x20 || 0x7e<c2 || c1<0x20 || 0x7e<c1) return;
2375 output_escape_sequence(x0213_f ? JIS_X_0213_1 : JIS_X_0208);
2382 e_oconv(nkf_char c2, nkf_char c1)
2384 if (c2 == 0 && nkf_char_unicode_p(c1)){
2385 w16e_conv(c1, &c2, &c1);
2386 if (c2 == 0 && nkf_char_unicode_p(c1)){
2387 c2 = c1 & VALUE_MASK;
2388 if (x0212_f && 0xE000 <= c2 && c2 <= 0xE757) {
2392 c2 += c2 < 10 ? 0x75 : 0x8FEB;
2393 c1 = 0x21 + c1 % 94;
2396 (*o_putc)((c2 & 0x7f) | 0x080);
2397 (*o_putc)(c1 | 0x080);
2399 (*o_putc)((c2 & 0x7f) | 0x080);
2400 (*o_putc)(c1 | 0x080);
2404 if (encode_fallback) (*encode_fallback)(c1);
2412 } else if (c2 == 0) {
2413 output_mode = ASCII;
2415 } else if (c2 == JIS_X_0201_1976_K) {
2416 output_mode = EUC_JP;
2417 (*o_putc)(SS2); (*o_putc)(c1|0x80);
2418 } else if (c2 == ISO_8859_1) {
2419 output_mode = ISO_8859_1;
2420 (*o_putc)(c1 | 0x080);
2422 } else if (is_eucg3(c2)){
2423 output_mode = EUC_JP;
2424 #ifdef SHIFTJIS_CP932
2427 if (e2s_conv(c2, c1, &s2, &s1) == 0){
2428 s2e_conv(s2, s1, &c2, &c1);
2433 output_mode = ASCII;
2435 }else if (is_eucg3(c2)){
2438 (*o_putc)((c2 & 0x7f) | 0x080);
2439 (*o_putc)(c1 | 0x080);
2442 (*o_putc)((c2 & 0x7f) | 0x080);
2443 (*o_putc)(c1 | 0x080);
2447 if (!nkf_isgraph(c1) || !nkf_isgraph(c2)) {
2448 set_iconv(FALSE, 0);
2449 return; /* too late to rescue this char */
2451 output_mode = EUC_JP;
2452 (*o_putc)(c2 | 0x080);
2453 (*o_putc)(c1 | 0x080);
2458 s_oconv(nkf_char c2, nkf_char c1)
2460 #ifdef NUMCHAR_OPTION
2461 if (c2 == 0 && nkf_char_unicode_p(c1)){
2462 w16e_conv(c1, &c2, &c1);
2463 if (c2 == 0 && nkf_char_unicode_p(c1)){
2464 c2 = c1 & VALUE_MASK;
2465 if (!x0213_f && 0xE000 <= c2 && c2 <= 0xE757) {
2468 c2 = c1 / 188 + (cp932inv_f ? 0xF0 : 0xEB);
2470 c1 += 0x40 + (c1 > 0x3e);
2475 if(encode_fallback)(*encode_fallback)(c1);
2484 } else if (c2 == 0) {
2485 output_mode = ASCII;
2487 } else if (c2 == JIS_X_0201_1976_K) {
2488 output_mode = SHIFT_JIS;
2490 } else if (c2 == ISO_8859_1) {
2491 output_mode = ISO_8859_1;
2492 (*o_putc)(c1 | 0x080);
2494 } else if (is_eucg3(c2)){
2495 output_mode = SHIFT_JIS;
2496 if (e2s_conv(c2, c1, &c2, &c1) == 0){
2502 if (!nkf_isprint(c1) || !nkf_isprint(c2)) {
2503 set_iconv(FALSE, 0);
2504 return; /* too late to rescue this char */
2506 output_mode = SHIFT_JIS;
2507 e2s_conv(c2, c1, &c2, &c1);
2509 #ifdef SHIFTJIS_CP932
2511 && CP932INV_TABLE_BEGIN <= c2 && c2 <= CP932INV_TABLE_END){
2512 nkf_char c = cp932inv[c2 - CP932INV_TABLE_BEGIN][c1 - 0x40];
2518 #endif /* SHIFTJIS_CP932 */
2521 if (prefix_table[(unsigned char)c1]){
2522 (*o_putc)(prefix_table[(unsigned char)c1]);
2528 #ifdef UTF8_OUTPUT_ENABLE
2530 w_oconv(nkf_char c2, nkf_char c1)
2536 output_bom_f = FALSE;
2547 if (c2 == 0 && nkf_char_unicode_p(c1)){
2548 val = c1 & VALUE_MASK;
2549 nkf_unicode_to_utf8(val, &c1, &c2, &c3, &c4);
2551 if (c2) (*o_putc)(c2);
2552 if (c3) (*o_putc)(c3);
2553 if (c4) (*o_putc)(c4);
2560 val = e2w_conv(c2, c1);
2562 nkf_unicode_to_utf8(val, &c1, &c2, &c3, &c4);
2564 if (c2) (*o_putc)(c2);
2565 if (c3) (*o_putc)(c3);
2566 if (c4) (*o_putc)(c4);
2572 w_oconv16(nkf_char c2, nkf_char c1)
2575 output_bom_f = FALSE;
2576 if (output_endian == ENDIAN_LITTLE){
2590 if (c2 == 0 && nkf_char_unicode_p(c1)) {
2591 if (nkf_char_unicode_bmp_p(c1)) {
2592 c2 = (c1 >> 8) & 0xff;
2596 if (c1 <= UNICODE_MAX) {
2597 c2 = (c1 >> 10) + NKF_INT32_C(0xD7C0); /* high surrogate */
2598 c1 = (c1 & 0x3FF) + NKF_INT32_C(0xDC00); /* low surrogate */
2599 if (output_endian == ENDIAN_LITTLE){
2600 (*o_putc)(c2 & 0xff);
2601 (*o_putc)((c2 >> 8) & 0xff);
2602 (*o_putc)(c1 & 0xff);
2603 (*o_putc)((c1 >> 8) & 0xff);
2605 (*o_putc)((c2 >> 8) & 0xff);
2606 (*o_putc)(c2 & 0xff);
2607 (*o_putc)((c1 >> 8) & 0xff);
2608 (*o_putc)(c1 & 0xff);
2614 nkf_char val = e2w_conv(c2, c1);
2615 c2 = (val >> 8) & 0xff;
2620 if (output_endian == ENDIAN_LITTLE){
2630 w_oconv32(nkf_char c2, nkf_char c1)
2633 output_bom_f = FALSE;
2634 if (output_endian == ENDIAN_LITTLE){
2652 if (c2 == ISO_8859_1) {
2654 } else if (c2 == 0 && nkf_char_unicode_p(c1)) {
2657 c1 = e2w_conv(c2, c1);
2660 if (output_endian == ENDIAN_LITTLE){
2661 (*o_putc)( c1 & 0xFF);
2662 (*o_putc)((c1 >> 8) & 0xFF);
2663 (*o_putc)((c1 >> 16) & 0xFF);
2667 (*o_putc)((c1 >> 16) & 0xFF);
2668 (*o_putc)((c1 >> 8) & 0xFF);
2669 (*o_putc)( c1 & 0xFF);
2674 #define SCORE_L2 (1) /* Kanji Level 2 */
2675 #define SCORE_KANA (SCORE_L2 << 1) /* Halfwidth Katakana */
2676 #define SCORE_DEPEND (SCORE_KANA << 1) /* MD Characters */
2677 #define SCORE_CP932 (SCORE_DEPEND << 1) /* IBM extended characters */
2678 #define SCORE_X0212 (SCORE_CP932 << 1) /* JIS X 0212 */
2679 #define SCORE_NO_EXIST (SCORE_X0212 << 1) /* Undefined Characters */
2680 #define SCORE_iMIME (SCORE_NO_EXIST << 1) /* MIME selected */
2681 #define SCORE_ERROR (SCORE_iMIME << 1) /* Error */
2683 #define SCORE_INIT (SCORE_iMIME)
2685 static const nkf_char score_table_A0[] = {
2688 0, SCORE_DEPEND, SCORE_DEPEND, SCORE_DEPEND,
2689 SCORE_DEPEND, SCORE_DEPEND, SCORE_DEPEND, SCORE_NO_EXIST,
2692 static const nkf_char score_table_F0[] = {
2693 SCORE_L2, SCORE_L2, SCORE_L2, SCORE_L2,
2694 SCORE_L2, SCORE_DEPEND, SCORE_NO_EXIST, SCORE_NO_EXIST,
2695 SCORE_DEPEND, SCORE_DEPEND, SCORE_CP932, SCORE_CP932,
2696 SCORE_CP932, SCORE_NO_EXIST, SCORE_NO_EXIST, SCORE_ERROR,
2700 set_code_score(struct input_code *ptr, nkf_char score)
2703 ptr->score |= score;
2708 clr_code_score(struct input_code *ptr, nkf_char score)
2711 ptr->score &= ~score;
2716 code_score(struct input_code *ptr)
2718 nkf_char c2 = ptr->buf[0];
2719 #ifdef UTF8_OUTPUT_ENABLE
2720 nkf_char c1 = ptr->buf[1];
2723 set_code_score(ptr, SCORE_ERROR);
2724 }else if (c2 == SS2){
2725 set_code_score(ptr, SCORE_KANA);
2726 }else if (c2 == 0x8f){
2727 set_code_score(ptr, SCORE_X0212);
2728 #ifdef UTF8_OUTPUT_ENABLE
2729 }else if (!e2w_conv(c2, c1)){
2730 set_code_score(ptr, SCORE_NO_EXIST);
2732 }else if ((c2 & 0x70) == 0x20){
2733 set_code_score(ptr, score_table_A0[c2 & 0x0f]);
2734 }else if ((c2 & 0x70) == 0x70){
2735 set_code_score(ptr, score_table_F0[c2 & 0x0f]);
2736 }else if ((c2 & 0x70) >= 0x50){
2737 set_code_score(ptr, SCORE_L2);
2742 status_disable(struct input_code *ptr)
2747 if (iconv == ptr->iconv_func) set_iconv(FALSE, 0);
2751 status_push_ch(struct input_code *ptr, nkf_char c)
2753 ptr->buf[ptr->index++] = c;
2757 status_clear(struct input_code *ptr)
2764 status_reset(struct input_code *ptr)
2767 ptr->score = SCORE_INIT;
2771 status_reinit(struct input_code *ptr)
2774 ptr->_file_stat = 0;
2778 status_check(struct input_code *ptr, nkf_char c)
2780 if (c <= DEL && estab_f){
2786 s_status(struct input_code *ptr, nkf_char c)
2790 status_check(ptr, c);
2795 }else if (nkf_char_unicode_p(c)){
2797 }else if (0xa1 <= c && c <= 0xdf){
2798 status_push_ch(ptr, SS2);
2799 status_push_ch(ptr, c);
2802 }else if ((0x81 <= c && c < 0xa0) || (0xe0 <= c && c <= 0xea)){
2804 status_push_ch(ptr, c);
2805 }else if (0xed <= c && c <= 0xee){
2807 status_push_ch(ptr, c);
2808 #ifdef SHIFTJIS_CP932
2809 }else if (is_ibmext_in_sjis(c)){
2811 status_push_ch(ptr, c);
2812 #endif /* SHIFTJIS_CP932 */
2814 }else if (0xf0 <= c && c <= 0xfc){
2816 status_push_ch(ptr, c);
2817 #endif /* X0212_ENABLE */
2819 status_disable(ptr);
2823 if ((0x40 <= c && c <= 0x7e) || (0x80 <= c && c <= 0xfc)){
2824 status_push_ch(ptr, c);
2825 s2e_conv(ptr->buf[0], ptr->buf[1], &ptr->buf[0], &ptr->buf[1]);
2829 status_disable(ptr);
2833 #ifdef SHIFTJIS_CP932
2834 if ((0x40 <= c && c <= 0x7e) || (0x80 <= c && c <= 0xfc)) {
2835 status_push_ch(ptr, c);
2836 if (s2e_conv(ptr->buf[0], ptr->buf[1], &ptr->buf[0], &ptr->buf[1]) == 0) {
2837 set_code_score(ptr, SCORE_CP932);
2842 #endif /* SHIFTJIS_CP932 */
2843 status_disable(ptr);
2846 if ((0x40 <= c && c <= 0x7e) || (0x80 <= c && c <= 0xfc)){
2847 status_push_ch(ptr, c);
2848 s2e_conv(ptr->buf[0], ptr->buf[1], &ptr->buf[0], &ptr->buf[1]);
2849 set_code_score(ptr, SCORE_CP932);
2852 status_disable(ptr);
2859 e_status(struct input_code *ptr, nkf_char c)
2863 status_check(ptr, c);
2868 }else if (nkf_char_unicode_p(c)){
2870 }else if (SS2 == c || (0xa1 <= c && c <= 0xfe)){
2872 status_push_ch(ptr, c);
2874 }else if (0x8f == c){
2876 status_push_ch(ptr, c);
2877 #endif /* X0212_ENABLE */
2879 status_disable(ptr);
2883 if (0xa1 <= c && c <= 0xfe){
2884 status_push_ch(ptr, c);
2888 status_disable(ptr);
2893 if (0xa1 <= c && c <= 0xfe){
2895 status_push_ch(ptr, c);
2897 status_disable(ptr);
2899 #endif /* X0212_ENABLE */
2903 #ifdef UTF8_INPUT_ENABLE
2905 w_status(struct input_code *ptr, nkf_char c)
2909 status_check(ptr, c);
2914 }else if (nkf_char_unicode_p(c)){
2916 }else if (0xc0 <= c && c <= 0xdf){
2918 status_push_ch(ptr, c);
2919 }else if (0xe0 <= c && c <= 0xef){
2921 status_push_ch(ptr, c);
2922 }else if (0xf0 <= c && c <= 0xf4){
2924 status_push_ch(ptr, c);
2926 status_disable(ptr);
2931 if (0x80 <= c && c <= 0xbf){
2932 status_push_ch(ptr, c);
2933 if (ptr->index > ptr->stat){
2934 int bom = (ptr->buf[0] == 0xef && ptr->buf[1] == 0xbb
2935 && ptr->buf[2] == 0xbf);
2936 w2e_conv(ptr->buf[0], ptr->buf[1], ptr->buf[2],
2937 &ptr->buf[0], &ptr->buf[1]);
2944 status_disable(ptr);
2948 if (0x80 <= c && c <= 0xbf){
2949 if (ptr->index < ptr->stat){
2950 status_push_ch(ptr, c);
2955 status_disable(ptr);
2963 code_status(nkf_char c)
2965 int action_flag = 1;
2966 struct input_code *result = 0;
2967 struct input_code *p = input_code_list;
2969 if (!p->status_func) {
2973 if (!p->status_func)
2975 (p->status_func)(p, c);
2978 }else if(p->stat == 0){
2989 if (result && !estab_f){
2990 set_iconv(TRUE, result->iconv_func);
2991 }else if (c <= DEL){
2992 struct input_code *ptr = input_code_list;
3002 nkf_buf_t *std_gc_buf;
3003 nkf_char broken_state;
3004 nkf_buf_t *broken_buf;
3005 nkf_char mimeout_state;
3009 static nkf_state_t *nkf_state = NULL;
3011 #define STD_GC_BUFSIZE (256)
3014 nkf_state_init(void)
3017 nkf_buf_clear(nkf_state->std_gc_buf);
3018 nkf_buf_clear(nkf_state->broken_buf);
3019 nkf_buf_clear(nkf_state->nfc_buf);
3022 nkf_state = nkf_xmalloc(sizeof(nkf_state_t));
3023 nkf_state->std_gc_buf = nkf_buf_new(STD_GC_BUFSIZE);
3024 nkf_state->broken_buf = nkf_buf_new(3);
3025 nkf_state->nfc_buf = nkf_buf_new(9);
3027 nkf_state->broken_state = 0;
3028 nkf_state->mimeout_state = 0;
3035 if (!nkf_buf_empty_p(nkf_state->std_gc_buf)){
3036 return nkf_buf_pop(nkf_state->std_gc_buf);
3043 std_ungetc(nkf_char c, FILE *f)
3045 nkf_buf_push(nkf_state->std_gc_buf, c);
3051 std_putc(nkf_char c)
3058 static unsigned char hold_buf[HOLD_SIZE*2];
3059 static int hold_count = 0;
3061 push_hold_buf(nkf_char c2)
3063 if (hold_count >= HOLD_SIZE*2)
3065 hold_buf[hold_count++] = (unsigned char)c2;
3066 return ((hold_count >= HOLD_SIZE*2) ? EOF : hold_count);
3070 h_conv(FILE *f, nkf_char c1, nkf_char c2)
3076 /** it must NOT be in the kanji shifte sequence */
3077 /** it must NOT be written in JIS7 */
3078 /** and it must be after 2 byte 8bit code */
3084 while ((c2 = (*i_getc)(f)) != EOF) {
3090 if (push_hold_buf(c2) == EOF || estab_f) {
3096 struct input_code *p = input_code_list;
3097 struct input_code *result = p;
3102 if (p->status_func && p->score < result->score) {
3107 set_iconv(TRUE, result->iconv_func);
3112 ** 1) EOF is detected, or
3113 ** 2) Code is established, or
3114 ** 3) Buffer is FULL (but last word is pushed)
3116 ** in 1) and 3) cases, we continue to use
3117 ** Kanji codes by oconv and leave estab_f unchanged.
3122 while (hold_index < hold_count){
3123 c1 = hold_buf[hold_index++];
3127 }else if (iconv == s_iconv && 0xa1 <= c1 && c1 <= 0xdf){
3128 (*iconv)(JIS_X_0201_1976_K, c1, 0);
3131 if (hold_index < hold_count){
3132 c2 = hold_buf[hold_index++];
3142 switch ((*iconv)(c1, c2, 0)) { /* can be EUC/SJIS/UTF-8 */
3145 if (hold_index < hold_count){
3146 c3 = hold_buf[hold_index++];
3147 } else if ((c3 = (*i_getc)(f)) == EOF) {
3152 if (hold_index < hold_count){
3153 c4 = hold_buf[hold_index++];
3154 } else if ((c4 = (*i_getc)(f)) == EOF) {
3159 (*iconv)(c1, c2, (c3<<8)|c4);
3162 /* 3 bytes EUC or UTF-8 */
3163 if (hold_index < hold_count){
3164 c3 = hold_buf[hold_index++];
3165 } else if ((c3 = (*i_getc)(f)) == EOF) {
3171 (*iconv)(c1, c2, c3);
3174 if (c3 == EOF) break;
3180 * Check and Ignore BOM
3186 switch(c2 = (*i_getc)(f)){
3188 if((c2 = (*i_getc)(f)) == 0x00){
3189 if((c2 = (*i_getc)(f)) == 0xFE){
3190 if((c2 = (*i_getc)(f)) == 0xFF){
3191 if(!input_encoding){
3192 set_iconv(TRUE, w_iconv32);
3194 if (iconv == w_iconv32) {
3195 input_endian = ENDIAN_BIG;
3198 (*i_ungetc)(0xFF,f);
3199 }else (*i_ungetc)(c2,f);
3200 (*i_ungetc)(0xFE,f);
3201 }else if(c2 == 0xFF){
3202 if((c2 = (*i_getc)(f)) == 0xFE){
3203 if(!input_encoding){
3204 set_iconv(TRUE, w_iconv32);
3206 if (iconv == w_iconv32) {
3207 input_endian = ENDIAN_2143;
3210 (*i_ungetc)(0xFF,f);
3211 }else (*i_ungetc)(c2,f);
3212 (*i_ungetc)(0xFF,f);
3213 }else (*i_ungetc)(c2,f);
3214 (*i_ungetc)(0x00,f);
3215 }else (*i_ungetc)(c2,f);
3216 (*i_ungetc)(0x00,f);
3219 if((c2 = (*i_getc)(f)) == 0xBB){
3220 if((c2 = (*i_getc)(f)) == 0xBF){
3221 if(!input_encoding){
3222 set_iconv(TRUE, w_iconv);
3224 if (iconv == w_iconv) {
3227 (*i_ungetc)(0xBF,f);
3228 }else (*i_ungetc)(c2,f);
3229 (*i_ungetc)(0xBB,f);
3230 }else (*i_ungetc)(c2,f);
3231 (*i_ungetc)(0xEF,f);
3234 if((c2 = (*i_getc)(f)) == 0xFF){
3235 if((c2 = (*i_getc)(f)) == 0x00){
3236 if((c2 = (*i_getc)(f)) == 0x00){
3237 if(!input_encoding){
3238 set_iconv(TRUE, w_iconv32);
3240 if (iconv == w_iconv32) {
3241 input_endian = ENDIAN_3412;
3244 (*i_ungetc)(0x00,f);
3245 }else (*i_ungetc)(c2,f);
3246 (*i_ungetc)(0x00,f);
3247 }else (*i_ungetc)(c2,f);
3248 if(!input_encoding){
3249 set_iconv(TRUE, w_iconv16);
3251 if (iconv == w_iconv16) {
3252 input_endian = ENDIAN_BIG;
3255 (*i_ungetc)(0xFF,f);
3256 }else (*i_ungetc)(c2,f);
3257 (*i_ungetc)(0xFE,f);
3260 if((c2 = (*i_getc)(f)) == 0xFE){
3261 if((c2 = (*i_getc)(f)) == 0x00){
3262 if((c2 = (*i_getc)(f)) == 0x00){
3263 if(!input_encoding){
3264 set_iconv(TRUE, w_iconv32);
3266 if (iconv == w_iconv32) {
3267 input_endian = ENDIAN_LITTLE;
3270 (*i_ungetc)(0x00,f);
3271 }else (*i_ungetc)(c2,f);
3272 (*i_ungetc)(0x00,f);
3273 }else (*i_ungetc)(c2,f);
3274 if(!input_encoding){
3275 set_iconv(TRUE, w_iconv16);
3277 if (iconv == w_iconv16) {
3278 input_endian = ENDIAN_LITTLE;
3281 (*i_ungetc)(0xFE,f);
3282 }else (*i_ungetc)(c2,f);
3283 (*i_ungetc)(0xFF,f);
3292 broken_getc(FILE *f)
3296 if (!nkf_buf_empty_p(nkf_state->broken_buf)) {
3297 return nkf_buf_pop(nkf_state->broken_buf);
3300 if (c=='$' && nkf_state->broken_state != ESC
3301 && (input_mode == ASCII || input_mode == JIS_X_0201_1976_K)) {
3303 nkf_state->broken_state = 0;
3304 if (c1=='@'|| c1=='B') {
3305 nkf_buf_push(nkf_state->broken_buf, c1);
3306 nkf_buf_push(nkf_state->broken_buf, c);
3312 } else if (c=='(' && nkf_state->broken_state != ESC
3313 && (input_mode == JIS_X_0208 || input_mode == JIS_X_0201_1976_K)) {
3315 nkf_state->broken_state = 0;
3316 if (c1=='J'|| c1=='B') {
3317 nkf_buf_push(nkf_state->broken_buf, c1);
3318 nkf_buf_push(nkf_state->broken_buf, c);
3325 nkf_state->broken_state = c;
3331 broken_ungetc(nkf_char c, FILE *f)
3333 if (nkf_buf_length(nkf_state->broken_buf) < 2)
3334 nkf_buf_push(nkf_state->broken_buf, c);
3339 eol_conv(nkf_char c2, nkf_char c1)
3341 if (guess_f && input_eol != EOF) {
3342 if (c2 == 0 && c1 == LF) {
3343 if (!input_eol) input_eol = prev_cr ? CRLF : LF;
3344 else if (input_eol != (prev_cr ? CRLF : LF)) input_eol = EOF;
3345 } else if (c2 == 0 && c1 == CR && input_eol == LF) input_eol = EOF;
3347 else if (!input_eol) input_eol = CR;
3348 else if (input_eol != CR) input_eol = EOF;
3350 if (prev_cr || (c2 == 0 && c1 == LF)) {
3352 if (eolmode_f != LF) (*o_eol_conv)(0, CR);
3353 if (eolmode_f != CR) (*o_eol_conv)(0, LF);
3355 if (c2 == 0 && c1 == CR) prev_cr = CR;
3356 else if (c2 != 0 || c1 != LF) (*o_eol_conv)(c2, c1);
3360 put_newline(void (*func)(nkf_char))
3362 switch (eolmode_f ? eolmode_f : DEFAULT_NEWLINE) {
3377 oconv_newline(void (*func)(nkf_char, nkf_char))
3379 switch (eolmode_f ? eolmode_f : DEFAULT_NEWLINE) {
3394 Return value of fold_conv()
3396 LF add newline and output char
3397 CR add newline and output nothing
3400 1 (or else) normal output
3402 fold state in prev (previous character)
3404 >0x80 Japanese (X0208/X0201)
3409 This fold algorthm does not preserve heading space in a line.
3410 This is the main difference from fmt.
3413 #define char_size(c2,c1) (c2?2:1)
3416 fold_conv(nkf_char c2, nkf_char c1)
3419 nkf_char fold_state;
3421 if (c1== CR && !fold_preserve_f) {
3422 fold_state=0; /* ignore cr */
3423 }else if (c1== LF&&f_prev==CR && fold_preserve_f) {
3425 fold_state=0; /* ignore cr */
3426 } else if (c1== BS) {
3427 if (f_line>0) f_line--;
3429 } else if (c2==EOF && f_line != 0) { /* close open last line */
3431 } else if ((c1==LF && !fold_preserve_f)
3432 || ((c1==CR||(c1==LF&&f_prev!=CR))
3433 && fold_preserve_f)) {
3435 if (fold_preserve_f) {
3439 } else if ((f_prev == c1 && !fold_preserve_f)
3440 || (f_prev == LF && fold_preserve_f)
3441 ) { /* duplicate newline */
3444 fold_state = LF; /* output two newline */
3450 if (f_prev&0x80) { /* Japanese? */
3452 fold_state = 0; /* ignore given single newline */
3453 } else if (f_prev==SP) {
3457 if (++f_line<=fold_len)
3461 fold_state = CR; /* fold and output nothing */
3465 } else if (c1=='\f') {
3468 fold_state = LF; /* output newline and clear */
3469 } else if ((c2==0 && nkf_isblank(c1)) || (c2 == '!' && c1 == '!')) {
3470 /* X0208 kankaku or ascii space */
3472 fold_state = 0; /* remove duplicate spaces */
3475 if (++f_line<=fold_len)
3476 fold_state = SP; /* output ASCII space only */
3478 f_prev = SP; f_line = 0;
3479 fold_state = CR; /* fold and output nothing */
3483 prev0 = f_prev; /* we still need this one... , but almost done */
3485 if (c2 || c2 == JIS_X_0201_1976_K)
3486 f_prev |= 0x80; /* this is Japanese */
3487 f_line += char_size(c2,c1);
3488 if (f_line<=fold_len) { /* normal case */
3491 if (f_line>fold_len+fold_margin) { /* too many kinsoku suspension */
3492 f_line = char_size(c2,c1);
3493 fold_state = LF; /* We can't wait, do fold now */
3494 } else if (c2 == JIS_X_0201_1976_K) {
3495 /* simple kinsoku rules return 1 means no folding */
3496 if (c1==(0xde&0x7f)) fold_state = 1; /*
\e$B!+
\e(B*/
3497 else if (c1==(0xdf&0x7f)) fold_state = 1; /*
\e$B!,
\e(B*/
3498 else if (c1==(0xa4&0x7f)) fold_state = 1; /*
\e$B!#
\e(B*/
3499 else if (c1==(0xa3&0x7f)) fold_state = 1; /*
\e$B!$
\e(B*/
3500 else if (c1==(0xa1&0x7f)) fold_state = 1; /*
\e$B!W
\e(B*/
3501 else if (c1==(0xb0&0x7f)) fold_state = 1; /* - */
3502 else if (SP<=c1 && c1<=(0xdf&0x7f)) { /* X0201 */
3504 fold_state = LF;/* add one new f_line before this character */
3507 fold_state = LF;/* add one new f_line before this character */
3510 /* kinsoku point in ASCII */
3511 if ( c1==')'|| /* { [ ( */
3522 /* just after special */
3523 } else if (!is_alnum(prev0)) {
3524 f_line = char_size(c2,c1);
3526 } else if ((prev0==SP) || /* ignored new f_line */
3527 (prev0==LF)|| /* ignored new f_line */
3528 (prev0&0x80)) { /* X0208 - ASCII */
3529 f_line = char_size(c2,c1);
3530 fold_state = LF;/* add one new f_line before this character */
3532 fold_state = 1; /* default no fold in ASCII */
3536 if (c1=='"') fold_state = 1; /*
\e$B!"
\e(B */
3537 else if (c1=='#') fold_state = 1; /*
\e$B!#
\e(B */
3538 else if (c1=='W') fold_state = 1; /*
\e$B!W
\e(B */
3539 else if (c1=='K') fold_state = 1; /*
\e$B!K
\e(B */
3540 else if (c1=='$') fold_state = 1; /*
\e$B!$
\e(B */
3541 else if (c1=='%') fold_state = 1; /*
\e$B!%
\e(B */
3542 else if (c1=='\'') fold_state = 1; /*
\e$B!\
\e(B */
3543 else if (c1=='(') fold_state = 1; /*
\e$B!(
\e(B */
3544 else if (c1==')') fold_state = 1; /*
\e$B!)
\e(B */
3545 else if (c1=='*') fold_state = 1; /*
\e$B!*
\e(B */
3546 else if (c1=='+') fold_state = 1; /*
\e$B!+
\e(B */
3547 else if (c1==',') fold_state = 1; /*
\e$B!,
\e(B */
3548 /* default no fold in kinsoku */
3551 f_line = char_size(c2,c1);
3552 /* add one new f_line before this character */
3555 f_line = char_size(c2,c1);
3557 /* add one new f_line before this character */
3562 /* terminator process */
3563 switch(fold_state) {
3565 oconv_newline(o_fconv);
3571 oconv_newline(o_fconv);
3582 static nkf_char z_prev2=0,z_prev1=0;
3585 z_conv(nkf_char c2, nkf_char c1)
3588 /* if (c2) c1 &= 0x7f; assertion */
3590 if (c2 == JIS_X_0201_1976_K && (c1 == 0x20 || c1 == 0x7D || c1 == 0x7E)) {
3596 if (z_prev2 == JIS_X_0201_1976_K) {
3597 if (c2 == JIS_X_0201_1976_K) {
3598 if (c1 == (0xde&0x7f)) { /*
\e$BByE@
\e(B */
3600 (*o_zconv)(dv[(z_prev1-SP)*2], dv[(z_prev1-SP)*2+1]);
3602 } else if (c1 == (0xdf&0x7f) && ev[(z_prev1-SP)*2]) { /*
\e$BH>ByE@
\e(B */
3604 (*o_zconv)(ev[(z_prev1-SP)*2], ev[(z_prev1-SP)*2+1]);
3609 (*o_zconv)(cv[(z_prev1-SP)*2], cv[(z_prev1-SP)*2+1]);
3611 if (c2 == JIS_X_0201_1976_K) {
3612 if (dv[(c1-SP)*2] || ev[(c1-SP)*2]) {
3613 /* wait for
\e$BByE@
\e(B or
\e$BH>ByE@
\e(B */
3618 (*o_zconv)(cv[(c1-SP)*2], cv[(c1-SP)*2+1]);
3629 if (alpha_f&1 && c2 == 0x23) {
3630 /* JISX0208 Alphabet */
3632 } else if (c2 == 0x21) {
3633 /* JISX0208 Kigou */
3638 } else if (alpha_f&4) {
3643 } else if (alpha_f&1 && 0x20<c1 && c1<0x7f && fv[c1-0x20]) {
3649 if (alpha_f&8 && c2 == 0) {
3651 const char *entity = 0;
3653 case '>': entity = ">"; break;
3654 case '<': entity = "<"; break;
3655 case '\"': entity = """; break;
3656 case '&': entity = "&"; break;
3659 while (*entity) (*o_zconv)(0, *entity++);
3665 /* JIS X 0208 Katakana to JIS X 0201 Katakana */
3670 /* U+3002 (0x8142) Ideographic Full Stop -> U+FF61 (0xA1) Halfwidth Ideographic Full Stop */
3674 /* U+300C (0x8175) Left Corner Bracket -> U+FF62 (0xA2) Halfwidth Left Corner Bracket */
3678 /* U+300D (0x8176) Right Corner Bracket -> U+FF63 (0xA3) Halfwidth Right Corner Bracket */
3682 /* U+3001 (0x8141) Ideographic Comma -> U+FF64 (0xA4) Halfwidth Ideographic Comma */
3686 /* U+30FB (0x8145) Katakana Middle Dot -> U+FF65 (0xA5) Halfwidth Katakana Middle Dot */
3690 /* U+30FC (0x815B) Katakana-Hiragana Prolonged Sound Mark -> U+FF70 (0xB0) Halfwidth Katakana-Hiragana Prolonged Sound Mark */
3694 /* U+309B (0x814A) Katakana-Hiragana Voiced Sound Mark -> U+FF9E (0xDE) Halfwidth Katakana Voiced Sound Mark */
3698 /* U+309C (0x814B) Katakana-Hiragana Semi-Voiced Sound Mark -> U+FF9F (0xDF) Halfwidth Katakana Semi-Voiced Sound Mark */
3703 (*o_zconv)(JIS_X_0201_1976_K, c);
3706 } else if (c2 == 0x25) {
3707 /* JISX0208 Katakana */
3708 static const int fullwidth_to_halfwidth[] =
3710 0x0000, 0x2700, 0x3100, 0x2800, 0x3200, 0x2900, 0x3300, 0x2A00,
3711 0x3400, 0x2B00, 0x3500, 0x3600, 0x365E, 0x3700, 0x375E, 0x3800,
3712 0x385E, 0x3900, 0x395E, 0x3A00, 0x3A5E, 0x3B00, 0x3B5E, 0x3C00,
3713 0x3C5E, 0x3D00, 0x3D5E, 0x3E00, 0x3E5E, 0x3F00, 0x3F5E, 0x4000,
3714 0x405E, 0x4100, 0x415E, 0x2F00, 0x4200, 0x425E, 0x4300, 0x435E,
3715 0x4400, 0x445E, 0x4500, 0x4600, 0x4700, 0x4800, 0x4900, 0x4A00,
3716 0x4A5E, 0x4A5F, 0x4B00, 0x4B5E, 0x4B5F, 0x4C00, 0x4C5E, 0x4C5F,
3717 0x4D00, 0x4D5E, 0x4D5F, 0x4E00, 0x4E5E, 0x4E5F, 0x4F00, 0x5000,
3718 0x5100, 0x5200, 0x5300, 0x2C00, 0x5400, 0x2D00, 0x5500, 0x2E00,
3719 0x5600, 0x5700, 0x5800, 0x5900, 0x5A00, 0x5B00, 0x0000, 0x5C00,
3720 0x0000, 0x0000, 0x2600, 0x5D00, 0x335E, 0x0000, 0x0000, 0x0000,
3721 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000
3723 if (fullwidth_to_halfwidth[c1-0x20]){
3724 c2 = fullwidth_to_halfwidth[c1-0x20];
3725 (*o_zconv)(JIS_X_0201_1976_K, c2>>8);
3727 (*o_zconv)(JIS_X_0201_1976_K, c2&0xFF);
3737 #define rot13(c) ( \
3739 (c <= 'M') ? (c + 13): \
3740 (c <= 'Z') ? (c - 13): \
3742 (c <= 'm') ? (c + 13): \
3743 (c <= 'z') ? (c - 13): \
3747 #define rot47(c) ( \
3749 ( c <= 'O') ? (c + 47) : \
3750 ( c <= '~') ? (c - 47) : \
3755 rot_conv(nkf_char c2, nkf_char c1)
3757 if (c2 == 0 || c2 == JIS_X_0201_1976_K || c2 == ISO_8859_1) {
3763 (*o_rot_conv)(c2,c1);
3767 hira_conv(nkf_char c2, nkf_char c1)
3771 if (0x20 < c1 && c1 < 0x74) {
3773 (*o_hira_conv)(c2,c1);
3775 } else if (c1 == 0x74 && nkf_enc_unicode_p(output_encoding)) {
3777 c1 = nkf_char_unicode_new(0x3094);
3778 (*o_hira_conv)(c2,c1);
3781 } else if (c2 == 0x21 && (c1 == 0x33 || c1 == 0x34)) {
3783 (*o_hira_conv)(c2,c1);
3788 if (c2 == 0 && c1 == nkf_char_unicode_new(0x3094)) {
3791 } else if (c2 == 0x24 && 0x20 < c1 && c1 < 0x74) {
3793 } else if (c2 == 0x21 && (c1 == 0x35 || c1 == 0x36)) {
3797 (*o_hira_conv)(c2,c1);
3802 iso2022jp_check_conv(nkf_char c2, nkf_char c1)
3804 #define RANGE_NUM_MAX 18
3805 static const nkf_char range[RANGE_NUM_MAX][2] = {
3826 nkf_char start, end, c;
3828 if(c2 >= 0x00 && c2 <= 0x20 && c1 >= 0x7f && c1 <= 0xff) {
3832 if((c2 >= 0x29 && c2 <= 0x2f) || (c2 >= 0x75 && c2 <= 0x7e)) {
3837 for (i = 0; i < RANGE_NUM_MAX; i++) {
3838 start = range[i][0];
3841 if (c >= start && c <= end) {
3846 (*o_iso2022jp_check_conv)(c2,c1);
3850 /* This converts =?ISO-2022-JP?B?HOGE HOGE?= */
3852 static const unsigned char *mime_pattern[] = {
3853 (const unsigned char *)"\075?EUC-JP?B?",
3854 (const unsigned char *)"\075?SHIFT_JIS?B?",
3855 (const unsigned char *)"\075?ISO-8859-1?Q?",
3856 (const unsigned char *)"\075?ISO-8859-1?B?",
3857 (const unsigned char *)"\075?ISO-2022-JP?B?",
3858 (const unsigned char *)"\075?ISO-2022-JP?B?",
3859 (const unsigned char *)"\075?ISO-2022-JP?Q?",
3860 #if defined(UTF8_INPUT_ENABLE)
3861 (const unsigned char *)"\075?UTF-8?B?",
3862 (const unsigned char *)"\075?UTF-8?Q?",
3864 (const unsigned char *)"\075?US-ASCII?Q?",
3869 /*
\e$B3:Ev$9$k%3!<%I$NM%@hEY$r>e$2$k$?$a$NL\0u
\e(B */
3870 nkf_char (*mime_priority_func[])(nkf_char c2, nkf_char c1, nkf_char c0) = {
3871 e_iconv, s_iconv, 0, 0, 0, 0,
3872 #if defined(UTF8_INPUT_ENABLE)
3878 static const nkf_char mime_encode[] = {
3879 EUC_JP, SHIFT_JIS, ISO_8859_1, ISO_8859_1, JIS_X_0208, JIS_X_0201_1976_K, JIS_X_0201_1976_K,
3880 #if defined(UTF8_INPUT_ENABLE)
3887 static const nkf_char mime_encode_method[] = {
3888 'B', 'B','Q', 'B', 'B', 'B', 'Q',
3889 #if defined(UTF8_INPUT_ENABLE)
3897 /* MIME preprocessor fifo */
3899 #define MIME_BUF_SIZE (1024) /* 2^n ring buffer */
3900 #define MIME_BUF_MASK (MIME_BUF_SIZE-1)
3901 #define mime_input_buf(n) mime_input_state.buf[(n)&MIME_BUF_MASK]
3903 unsigned char buf[MIME_BUF_SIZE];
3905 unsigned int last; /* decoded */
3906 unsigned int input; /* undecoded */
3908 static nkf_char (*mime_iconv_back)(nkf_char c2,nkf_char c1,nkf_char c0) = NULL;
3910 #define MAXRECOVER 20
3913 mime_input_buf_unshift(nkf_char c)
3915 mime_input_buf(--mime_input_state.top) = (unsigned char)c;
3919 mime_ungetc(nkf_char c, FILE *f)
3921 mime_input_buf_unshift(c);
3926 mime_ungetc_buf(nkf_char c, FILE *f)
3929 (*i_mungetc_buf)(c,f);
3931 mime_input_buf(--mime_input_state.input) = (unsigned char)c;
3936 mime_getc_buf(FILE *f)
3938 /* we don't keep eof of mime_input_buf, becase it contains ?= as
3939 a terminator. It was checked in mime_integrity. */
3940 return ((mimebuf_f)?
3941 (*i_mgetc_buf)(f):mime_input_buf(mime_input_state.input++));
3945 switch_mime_getc(void)
3947 if (i_getc!=mime_getc) {
3948 i_mgetc = i_getc; i_getc = mime_getc;
3949 i_mungetc = i_ungetc; i_ungetc = mime_ungetc;
3950 if(mime_f==STRICT_MIME) {
3951 i_mgetc_buf = i_mgetc; i_mgetc = mime_getc_buf;
3952 i_mungetc_buf = i_mungetc; i_mungetc = mime_ungetc_buf;
3958 unswitch_mime_getc(void)
3960 if(mime_f==STRICT_MIME) {
3961 i_mgetc = i_mgetc_buf;
3962 i_mungetc = i_mungetc_buf;
3965 i_ungetc = i_mungetc;
3966 if(mime_iconv_back)set_iconv(FALSE, mime_iconv_back);
3967 mime_iconv_back = NULL;
3971 mime_integrity(FILE *f, const unsigned char *p)
3975 /* In buffered mode, read until =? or NL or buffer full
3977 mime_input_state.input = mime_input_state.top;
3978 mime_input_state.last = mime_input_state.top;
3980 while(*p) mime_input_buf(mime_input_state.input++) = *p++;
3982 q = mime_input_state.input;
3983 while((c=(*i_getc)(f))!=EOF) {
3984 if (((mime_input_state.input-mime_input_state.top)&MIME_BUF_MASK)==0) {
3985 break; /* buffer full */
3987 if (c=='=' && d=='?') {
3988 /* checked. skip header, start decode */
3989 mime_input_buf(mime_input_state.input++) = (unsigned char)c;
3990 /* mime_last_input = mime_input_state.input; */
3991 mime_input_state.input = q;
3995 if (!( (c=='+'||c=='/'|| c=='=' || c=='?' || is_alnum(c))))
3997 /* Should we check length mod 4? */
3998 mime_input_buf(mime_input_state.input++) = (unsigned char)c;
4001 /* In case of Incomplete MIME, no MIME decode */
4002 mime_input_buf(mime_input_state.input++) = (unsigned char)c;
4003 mime_input_state.last = mime_input_state.input; /* point undecoded buffer */
4004 mime_decode_mode = 1; /* no decode on mime_input_buf last in mime_getc */
4005 switch_mime_getc(); /* anyway we need buffered getc */
4010 mime_begin_strict(FILE *f)
4014 const unsigned char *p,*q;
4015 nkf_char r[MAXRECOVER]; /* recovery buffer, max mime pattern length */
4017 mime_decode_mode = FALSE;
4018 /* =? has been checked */
4020 p = mime_pattern[j];
4023 for(i=2;p[i]>SP;i++) { /* start at =? */
4024 if (((r[i] = c1 = (*i_getc)(f))==EOF) || nkf_toupper(c1) != p[i]) {
4025 /* pattern fails, try next one */
4027 while (mime_pattern[++j]) {
4028 p = mime_pattern[j];
4029 for(k=2;k<i;k++) /* assume length(p) > i */
4030 if (p[k]!=q[k]) break;
4031 if (k==i && nkf_toupper(c1)==p[k]) break;
4033 p = mime_pattern[j];
4034 if (p) continue; /* found next one, continue */
4035 /* all fails, output from recovery buffer */
4043 mime_decode_mode = p[i-2];
4045 mime_iconv_back = iconv;
4046 set_iconv(FALSE, mime_priority_func[j]);
4047 clr_code_score(find_inputcode_byfunc(mime_priority_func[j]), SCORE_iMIME);
4049 if (mime_decode_mode=='B') {
4050 mimebuf_f = unbuf_f;
4052 /* do MIME integrity check */
4053 return mime_integrity(f,mime_pattern[j]);
4067 /* In NONSTRICT mode, only =? is checked. In case of failure, we */
4068 /* re-read and convert again from mime_buffer. */
4070 /* =? has been checked */
4071 k = mime_input_state.last;
4072 mime_input_buf(mime_input_state.last++)='='; mime_input_buf(mime_input_state.last++)='?';
4073 for(i=2;i<MAXRECOVER;i++) { /* start at =? */
4074 /* We accept any character type even if it is breaked by new lines */
4075 c1 = (*i_getc)(f); mime_input_buf(mime_input_state.last++) = (unsigned char)c1;
4076 if (c1==LF||c1==SP||c1==CR||
4077 c1=='-'||c1=='_'||is_alnum(c1)) continue;
4079 /* Failed. But this could be another MIME preemble */
4081 mime_input_state.last--;
4087 c1 = (*i_getc)(f); mime_input_buf(mime_input_state.last++) = (unsigned char)c1;
4088 if (!(++i<MAXRECOVER) || c1==EOF) break;
4089 if (c1=='b'||c1=='B') {
4090 mime_decode_mode = 'B';
4091 } else if (c1=='q'||c1=='Q') {
4092 mime_decode_mode = 'Q';
4096 c1 = (*i_getc)(f); mime_input_buf(mime_input_state.last++) = (unsigned char)c1;
4097 if (!(++i<MAXRECOVER) || c1==EOF) break;
4099 mime_decode_mode = FALSE;
4105 if (!mime_decode_mode) {
4106 /* false MIME premble, restart from mime_buffer */
4107 mime_decode_mode = 1; /* no decode, but read from the mime_buffer */
4108 /* Since we are in MIME mode until buffer becomes empty, */
4109 /* we never go into mime_begin again for a while. */
4112 /* discard mime preemble, and goto MIME mode */
4113 mime_input_state.last = k;
4114 /* do no MIME integrity check */
4115 return c1; /* used only for checking EOF */
4126 debug(const char *str)
4129 fprintf(stderr, "%s\n", str ? str : "NULL");
4135 set_input_codename(const char *codename)
4137 if (!input_codename) {
4138 input_codename = codename;
4139 } else if (strcmp(codename, input_codename) != 0) {
4140 input_codename = "";
4145 get_guessed_code(void)
4147 if (input_codename && !*input_codename) {
4148 input_codename = "BINARY";
4150 struct input_code *p = find_inputcode_byfunc(iconv);
4151 if (!input_codename) {
4152 input_codename = "ASCII";
4153 } else if (strcmp(input_codename, "Shift_JIS") == 0) {
4154 if (p->score & (SCORE_DEPEND|SCORE_CP932))
4155 input_codename = "CP932";
4156 } else if (strcmp(input_codename, "EUC-JP") == 0) {
4157 if (p->score & (SCORE_X0212))
4158 input_codename = "EUCJP-MS";
4159 else if (p->score & (SCORE_DEPEND|SCORE_CP932))
4160 input_codename = "CP51932";
4161 } else if (strcmp(input_codename, "ISO-2022-JP") == 0) {
4162 if (p->score & (SCORE_KANA))
4163 input_codename = "CP50221";
4164 else if (p->score & (SCORE_DEPEND|SCORE_CP932))
4165 input_codename = "CP50220";
4168 return input_codename;
4171 #if !defined(PERL_XS) && !defined(WIN32DLL)
4173 print_guessed_code(char *filename)
4175 if (filename != NULL) printf("%s: ", filename);
4176 if (input_codename && !*input_codename) {
4179 input_codename = get_guessed_code();
4181 printf("%s\n", input_codename);
4185 input_eol == CR ? " (CR)" :
4186 input_eol == LF ? " (LF)" :
4187 input_eol == CRLF ? " (CRLF)" :
4188 input_eol == EOF ? " (MIXED NL)" :
4198 hex_getc(nkf_char ch, FILE *f, nkf_char (*g)(FILE *f), nkf_char (*u)(nkf_char c, FILE *f))
4200 nkf_char c1, c2, c3;
4206 if (!nkf_isxdigit(c2)){
4211 if (!nkf_isxdigit(c3)){
4216 return (hex2bin(c2) << 4) | hex2bin(c3);
4222 return hex_getc(':', f, i_cgetc, i_cungetc);
4226 cap_ungetc(nkf_char c, FILE *f)
4228 return (*i_cungetc)(c, f);
4234 return hex_getc('%', f, i_ugetc, i_uungetc);
4238 url_ungetc(nkf_char c, FILE *f)
4240 return (*i_uungetc)(c, f);
4244 #ifdef NUMCHAR_OPTION
4246 numchar_getc(FILE *f)
4248 nkf_char (*g)(FILE *) = i_ngetc;
4249 nkf_char (*u)(nkf_char c ,FILE *f) = i_nungetc;
4260 if (buf[i] == 'x' || buf[i] == 'X'){
4261 for (j = 0; j < 7; j++){
4263 if (!nkf_isxdigit(buf[i])){
4270 c |= hex2bin(buf[i]);
4273 for (j = 0; j < 8; j++){
4277 if (!nkf_isdigit(buf[i])){
4284 c += hex2bin(buf[i]);
4290 return nkf_char_unicode_new(c);
4300 numchar_ungetc(nkf_char c, FILE *f)
4302 return (*i_nungetc)(c, f);
4306 #ifdef UNICODE_NORMALIZATION
4311 nkf_char (*g)(FILE *f) = i_nfc_getc;
4312 nkf_char (*u)(nkf_char c ,FILE *f) = i_nfc_ungetc;
4313 nkf_buf_t *buf = nkf_state->nfc_buf;
4314 const unsigned char *array;
4315 int lower=0, upper=NORMALIZATION_TABLE_LENGTH-1;
4316 nkf_char c = (*g)(f);
4318 if (c == EOF || c > 0xFF || (c & 0xc0) == 0x80) return c;
4320 nkf_buf_push(buf, c);
4322 while (lower <= upper) {
4323 int mid = (lower+upper) / 2;
4325 array = normalization_table[mid].nfd;
4326 for (len=0; len < NORMALIZATION_TABLE_NFD_LENGTH && array[len]; len++) {
4327 if (len >= nkf_buf_length(buf)) {
4331 lower = 1, upper = 0;
4334 nkf_buf_push(buf, c);
4336 if (array[len] != nkf_buf_at(buf, len)) {
4337 if (array[len] < nkf_buf_at(buf, len)) lower = mid + 1;
4338 else upper = mid - 1;
4345 array = normalization_table[mid].nfc;
4347 for (i=0; i < NORMALIZATION_TABLE_NFC_LENGTH && array[i]; i++)
4348 nkf_buf_push(buf, array[i]);
4352 } while (lower <= upper);
4354 while (nkf_buf_length(buf) > 1) (*u)(nkf_buf_pop(buf), f);
4355 c = nkf_buf_pop(buf);
4361 nfc_ungetc(nkf_char c, FILE *f)
4363 return (*i_nfc_ungetc)(c, f);
4365 #endif /* UNICODE_NORMALIZATION */
4369 base64decode(nkf_char c)
4374 i = c - 'A'; /* A..Z 0-25 */
4375 } else if (c == '_') {
4376 i = '?' /* 63 */ ; /* _ 63 */
4378 i = c - 'G' /* - 'a' + 26 */ ; /* a..z 26-51 */
4380 } else if (c > '/') {
4381 i = c - '0' + '4' /* - '0' + 52 */ ; /* 0..9 52-61 */
4382 } else if (c == '+' || c == '-') {
4383 i = '>' /* 62 */ ; /* + and - 62 */
4385 i = '?' /* 63 */ ; /* / 63 */
4393 nkf_char c1, c2, c3, c4, cc;
4394 nkf_char t1, t2, t3, t4, mode, exit_mode;
4395 nkf_char lwsp_count;
4398 nkf_char lwsp_size = 128;
4400 if (mime_input_state.top != mime_input_state.last) { /* Something is in FIFO */
4401 return mime_input_buf(mime_input_state.top++);
4403 if (mime_decode_mode==1 ||mime_decode_mode==FALSE) {
4404 mime_decode_mode=FALSE;
4405 unswitch_mime_getc();
4406 return (*i_getc)(f);
4409 if (mimebuf_f == FIXED_MIME)
4410 exit_mode = mime_decode_mode;
4413 if (mime_decode_mode == 'Q') {
4414 if ((c1 = (*i_mgetc)(f)) == EOF) return (EOF);
4416 if (c1=='_' && mimebuf_f != FIXED_MIME) return SP;
4417 if (c1<=SP || DEL<=c1) {
4418 mime_decode_mode = exit_mode; /* prepare for quit */
4421 if (c1!='=' && (c1!='?' || mimebuf_f == FIXED_MIME)) {
4425 mime_decode_mode = exit_mode; /* prepare for quit */
4426 if ((c2 = (*i_mgetc)(f)) == EOF) return (EOF);
4427 if (c1=='?'&&c2=='=' && mimebuf_f != FIXED_MIME) {
4428 /* end Q encoding */
4429 input_mode = exit_mode;
4431 lwsp_buf = nkf_xmalloc((lwsp_size+5)*sizeof(char));
4432 while ((c1=(*i_getc)(f))!=EOF) {
4437 if ((c1=(*i_getc)(f))!=EOF && nkf_isblank(c1)) {
4445 if ((c1=(*i_getc)(f))!=EOF && c1 == LF) {
4446 if ((c1=(*i_getc)(f))!=EOF && nkf_isblank(c1)) {
4461 lwsp_buf[lwsp_count] = (unsigned char)c1;
4462 if (lwsp_count++>lwsp_size){
4464 lwsp_buf_new = nkf_xrealloc(lwsp_buf, (lwsp_size+5)*sizeof(char));
4465 lwsp_buf = lwsp_buf_new;
4471 if (lwsp_count > 0 && (c1 != '=' || (lwsp_buf[lwsp_count-1] != SP && lwsp_buf[lwsp_count-1] != TAB))) {
4473 for(lwsp_count--;lwsp_count>0;lwsp_count--)
4474 i_ungetc(lwsp_buf[lwsp_count],f);
4477 nkf_xfree(lwsp_buf);
4480 if (c1=='='&&c2<SP) { /* this is soft wrap */
4481 while((c1 = (*i_mgetc)(f)) <=SP) {
4482 if (c1 == EOF) return (EOF);
4484 mime_decode_mode = 'Q'; /* still in MIME */
4485 goto restart_mime_q;
4488 mime_decode_mode = 'Q'; /* still in MIME */
4492 if ((c3 = (*i_mgetc)(f)) == EOF) return (EOF);
4493 if (c2<=SP) return c2;
4494 mime_decode_mode = 'Q'; /* still in MIME */
4495 return ((hex2bin(c2)<<4) + hex2bin(c3));
4498 if (mime_decode_mode != 'B') {
4499 mime_decode_mode = FALSE;
4500 return (*i_mgetc)(f);
4504 /* Base64 encoding */
4506 MIME allows line break in the middle of
4507 Base64, but we are very pessimistic in decoding
4508 in unbuf mode because MIME encoded code may broken by
4509 less or editor's control sequence (such as ESC-[-K in unbuffered
4510 mode. ignore incomplete MIME.
4512 mode = mime_decode_mode;
4513 mime_decode_mode = exit_mode; /* prepare for quit */
4515 while ((c1 = (*i_mgetc)(f))<=SP) {
4520 if ((c2 = (*i_mgetc)(f))<=SP) {
4523 if (mime_f != STRICT_MIME) goto mime_c2_retry;
4524 if (mimebuf_f!=FIXED_MIME) input_mode = ASCII;
4527 if ((c1 == '?') && (c2 == '=')) {
4530 lwsp_buf = nkf_xmalloc((lwsp_size+5)*sizeof(char));
4531 while ((c1=(*i_getc)(f))!=EOF) {
4536 if ((c1=(*i_getc)(f))!=EOF && nkf_isblank(c1)) {
4544 if ((c1=(*i_getc)(f))!=EOF) {
4548 } else if ((c1=(*i_getc)(f))!=EOF && nkf_isblank(c1)) {
4563 lwsp_buf[lwsp_count] = (unsigned char)c1;
4564 if (lwsp_count++>lwsp_size){
4566 lwsp_buf_new = nkf_xrealloc(lwsp_buf, (lwsp_size+5)*sizeof(char));
4567 lwsp_buf = lwsp_buf_new;
4573 if (lwsp_count > 0 && (c1 != '=' || (lwsp_buf[lwsp_count-1] != SP && lwsp_buf[lwsp_count-1] != TAB))) {
4575 for(lwsp_count--;lwsp_count>0;lwsp_count--)
4576 i_ungetc(lwsp_buf[lwsp_count],f);
4579 nkf_xfree(lwsp_buf);
4583 if ((c3 = (*i_mgetc)(f))<=SP) {
4586 if (mime_f != STRICT_MIME) goto mime_c3_retry;
4587 if (mimebuf_f!=FIXED_MIME) input_mode = ASCII;
4591 if ((c4 = (*i_mgetc)(f))<=SP) {
4594 if (mime_f != STRICT_MIME) goto mime_c4_retry;
4595 if (mimebuf_f!=FIXED_MIME) input_mode = ASCII;
4599 mime_decode_mode = mode; /* still in MIME sigh... */
4601 /* BASE 64 decoding */
4603 t1 = 0x3f & base64decode(c1);
4604 t2 = 0x3f & base64decode(c2);
4605 t3 = 0x3f & base64decode(c3);
4606 t4 = 0x3f & base64decode(c4);
4607 cc = ((t1 << 2) & 0x0fc) | ((t2 >> 4) & 0x03);
4609 mime_input_buf(mime_input_state.last++) = (unsigned char)cc;
4610 cc = ((t2 << 4) & 0x0f0) | ((t3 >> 2) & 0x0f);
4612 mime_input_buf(mime_input_state.last++) = (unsigned char)cc;
4613 cc = ((t3 << 6) & 0x0c0) | (t4 & 0x3f);
4615 mime_input_buf(mime_input_state.last++) = (unsigned char)cc;
4620 return mime_input_buf(mime_input_state.top++);
4623 static const char basis_64[] =
4624 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
4626 #define MIMEOUT_BUF_LENGTH 74
4628 char buf[MIMEOUT_BUF_LENGTH+1];
4632 /*nkf_char mime_lastchar2, mime_lastchar1;*/
4635 open_mime(nkf_char mode)
4637 const unsigned char *p;
4640 p = mime_pattern[0];
4641 for(i=0;mime_pattern[i];i++) {
4642 if (mode == mime_encode[i]) {
4643 p = mime_pattern[i];
4647 mimeout_mode = mime_encode_method[i];
4649 if (base64_count>45) {
4650 if (mimeout_state.count>0 && nkf_isblank(mimeout_state.buf[i])){
4651 (*o_mputc)(mimeout_state.buf[i]);
4654 put_newline(o_mputc);
4657 if (mimeout_state.count>0 && nkf_isspace(mimeout_state.buf[i])) {
4661 for (;i<mimeout_state.count;i++) {
4662 if (nkf_isspace(mimeout_state.buf[i])) {
4663 (*o_mputc)(mimeout_state.buf[i]);
4673 j = mimeout_state.count;
4674 mimeout_state.count = 0;
4676 mime_putc(mimeout_state.buf[i]);
4681 mime_prechar(nkf_char c2, nkf_char c1)
4683 if (mimeout_mode > 0){
4685 if (base64_count + mimeout_state.count/3*4> 73){
4686 (*o_base64conv)(EOF,0);
4687 oconv_newline(o_base64conv);
4688 (*o_base64conv)(0,SP);
4692 if (!(c2 == 0 && (c1 == CR || c1 == LF)) &&
4693 base64_count + mimeout_state.count/3*4> 66) {
4694 (*o_base64conv)(EOF,0);
4695 oconv_newline(o_base64conv);
4696 (*o_base64conv)(0,SP);
4702 if (c2 != EOF && base64_count + mimeout_state.count/3*4> 60) {
4703 mimeout_mode = (output_mode==ASCII ||output_mode == ISO_8859_1) ? 'Q' : 'B';
4704 open_mime(output_mode);
4705 (*o_base64conv)(EOF,0);
4706 oconv_newline(o_base64conv);
4707 (*o_base64conv)(0,SP);
4726 switch(mimeout_mode) {
4731 (*o_mputc)(basis_64[((nkf_state->mimeout_state & 0x3)<< 4)]);
4737 (*o_mputc)(basis_64[((nkf_state->mimeout_state & 0xF) << 2)]);
4742 if (mimeout_mode > 0) {
4743 if (mimeout_f!=FIXED_MIME) {
4745 } else if (mimeout_mode != 'Q')
4751 mimeout_addchar(nkf_char c)
4753 switch(mimeout_mode) {
4758 } else if(!nkf_isalnum(c)) {
4760 (*o_mputc)(bin2hex(((c>>4)&0xf)));
4761 (*o_mputc)(bin2hex((c&0xf)));
4769 nkf_state->mimeout_state=c;
4770 (*o_mputc)(basis_64[c>>2]);
4775 (*o_mputc)(basis_64[((nkf_state->mimeout_state & 0x3)<< 4) | ((c & 0xF0) >> 4)]);
4776 nkf_state->mimeout_state=c;
4781 (*o_mputc)(basis_64[((nkf_state->mimeout_state & 0xF) << 2) | ((c & 0xC0) >>6)]);
4782 (*o_mputc)(basis_64[c & 0x3F]);
4794 mime_putc(nkf_char c)
4799 if (mimeout_f == FIXED_MIME){
4800 if (mimeout_mode == 'Q'){
4801 if (base64_count > 71){
4802 if (c!=CR && c!=LF) {
4804 put_newline(o_mputc);
4809 if (base64_count > 71){
4811 put_newline(o_mputc);
4814 if (c == EOF) { /* c==EOF */
4818 if (c != EOF) { /* c==EOF */
4824 /* mimeout_f != FIXED_MIME */
4826 if (c == EOF) { /* c==EOF */
4827 if (mimeout_mode == -1 && mimeout_state.count > 1) open_mime(output_mode);
4828 j = mimeout_state.count;
4829 mimeout_state.count = 0;
4831 if (mimeout_mode > 0) {
4832 if (!nkf_isblank(mimeout_state.buf[j-1])) {
4834 if (nkf_isspace(mimeout_state.buf[i]) && base64_count < 71){
4837 mimeout_addchar(mimeout_state.buf[i]);
4841 mimeout_addchar(mimeout_state.buf[i]);
4845 mimeout_addchar(mimeout_state.buf[i]);
4851 mimeout_addchar(mimeout_state.buf[i]);
4857 if (mimeout_state.count > 0){
4858 lastchar = mimeout_state.buf[mimeout_state.count - 1];
4863 if (mimeout_mode=='Q') {
4864 if (c <= DEL && (output_mode==ASCII ||output_mode == ISO_8859_1)) {
4865 if (c == CR || c == LF) {
4870 } else if (c <= SP) {
4872 if (base64_count > 70) {
4873 put_newline(o_mputc);
4876 if (!nkf_isblank(c)) {
4881 if (base64_count > 70) {
4883 put_newline(o_mputc);
4886 open_mime(output_mode);
4888 if (!nkf_noescape_mime(c)) {
4901 if (mimeout_mode <= 0) {
4902 if (c <= DEL && (output_mode==ASCII || output_mode == ISO_8859_1 ||
4903 output_mode == UTF_8)) {
4904 if (nkf_isspace(c)) {
4906 if (mimeout_mode == -1) {
4909 if (c==CR || c==LF) {
4911 open_mime(output_mode);
4917 for (i=0;i<mimeout_state.count;i++) {
4918 (*o_mputc)(mimeout_state.buf[i]);
4919 if (mimeout_state.buf[i] == CR || mimeout_state.buf[i] == LF){
4930 mimeout_state.buf[0] = (char)c;
4931 mimeout_state.count = 1;
4933 if (base64_count > 1
4934 && base64_count + mimeout_state.count > 76
4935 && mimeout_state.buf[0] != CR && mimeout_state.buf[0] != LF){
4936 static const char *str = "boundary=\"";
4937 static int len = 10;
4940 for (; i < mimeout_state.count - len; ++i) {
4941 if (!strncmp(mimeout_state.buf+i, str, len)) {
4947 if (i == 0 || i == mimeout_state.count - len) {
4948 put_newline(o_mputc);
4950 if (!nkf_isspace(mimeout_state.buf[0])){
4957 for (j = 0; j <= i; ++j) {
4958 (*o_mputc)(mimeout_state.buf[j]);
4960 put_newline(o_mputc);
4962 for (; j <= mimeout_state.count; ++j) {
4963 mimeout_state.buf[j - i] = mimeout_state.buf[j];
4965 mimeout_state.count -= i;
4968 mimeout_state.buf[mimeout_state.count++] = (char)c;
4969 if (mimeout_state.count>MIMEOUT_BUF_LENGTH) {
4970 open_mime(output_mode);
4975 if (lastchar==CR || lastchar == LF){
4976 for (i=0;i<mimeout_state.count;i++) {
4977 (*o_mputc)(mimeout_state.buf[i]);
4980 mimeout_state.count = 0;
4983 for (i=0;i<mimeout_state.count-1;i++) {
4984 (*o_mputc)(mimeout_state.buf[i]);
4987 mimeout_state.buf[0] = SP;
4988 mimeout_state.count = 1;
4990 open_mime(output_mode);
4993 /* mimeout_mode == 'B', 1, 2 */
4994 if (c <= DEL && (output_mode==ASCII || output_mode == ISO_8859_1 ||
4995 output_mode == UTF_8)) {
4996 if (lastchar == CR || lastchar == LF){
4997 if (nkf_isblank(c)) {
4998 for (i=0;i<mimeout_state.count;i++) {
4999 mimeout_addchar(mimeout_state.buf[i]);
5001 mimeout_state.count = 0;
5004 for (i=0;i<mimeout_state.count;i++) {
5005 (*o_mputc)(mimeout_state.buf[i]);
5008 mimeout_state.count = 0;
5010 mimeout_state.buf[mimeout_state.count++] = (char)c;
5013 if (nkf_isspace(c)) {
5014 for (i=0;i<mimeout_state.count;i++) {
5015 if (SP<mimeout_state.buf[i] && mimeout_state.buf[i]<DEL) {
5017 for (i=0;i<mimeout_state.count;i++) {
5018 (*o_mputc)(mimeout_state.buf[i]);
5021 mimeout_state.count = 0;
5024 mimeout_state.buf[mimeout_state.count++] = (char)c;
5025 if (mimeout_state.count>MIMEOUT_BUF_LENGTH) {
5027 for (i=0;i<mimeout_state.count;i++) {
5028 (*o_mputc)(mimeout_state.buf[i]);
5031 mimeout_state.count = 0;
5035 if (mimeout_state.count>0 && SP<c && c!='=') {
5036 mimeout_state.buf[mimeout_state.count++] = (char)c;
5037 if (mimeout_state.count>MIMEOUT_BUF_LENGTH) {
5038 j = mimeout_state.count;
5039 mimeout_state.count = 0;
5041 mimeout_addchar(mimeout_state.buf[i]);
5048 if (mimeout_state.count>0) {
5049 j = mimeout_state.count;
5050 mimeout_state.count = 0;
5052 if (mimeout_state.buf[i]==CR || mimeout_state.buf[i]==LF)
5054 mimeout_addchar(mimeout_state.buf[i]);
5060 (*o_mputc)(mimeout_state.buf[i]);
5062 open_mime(output_mode);
5069 base64_conv(nkf_char c2, nkf_char c1)
5071 mime_prechar(c2, c1);
5072 (*o_base64conv)(c2,c1);
5076 typedef struct nkf_iconv_t {
5079 size_t input_buffer_size;
5080 char *output_buffer;
5081 size_t output_buffer_size;
5085 nkf_iconv_new(char *tocode, char *fromcode)
5087 nkf_iconv_t converter;
5089 converter->input_buffer_size = IOBUF_SIZE;
5090 converter->input_buffer = nkf_xmalloc(converter->input_buffer_size);
5091 converter->output_buffer_size = IOBUF_SIZE * 2;
5092 converter->output_buffer = nkf_xmalloc(converter->output_buffer_size);
5093 converter->cd = iconv_open(tocode, fromcode);
5094 if (converter->cd == (iconv_t)-1)
5098 perror(fprintf("iconv doesn't support %s to %s conversion.", fromcode, tocode));
5101 perror("can't iconv_open");
5107 nkf_iconv_convert(nkf_iconv_t *converter, FILE *input)
5109 size_t invalid = (size_t)0;
5110 char *input_buffer = converter->input_buffer;
5111 size_t input_length = (size_t)0;
5112 char *output_buffer = converter->output_buffer;
5113 size_t output_length = converter->output_buffer_size;
5118 while ((c = (*i_getc)(f)) != EOF) {
5119 input_buffer[input_length++] = c;
5120 if (input_length < converter->input_buffer_size) break;
5124 size_t ret = iconv(converter->cd, &input_buffer, &input_length, &output_buffer, &output_length);
5125 while (output_length-- > 0) {
5126 (*o_putc)(output_buffer[converter->output_buffer_size-output_length]);
5128 if (ret == (size_t) - 1) {
5131 if (input_buffer != converter->input_buffer)
5132 memmove(converter->input_buffer, input_buffer, input_length);
5135 converter->output_buffer_size *= 2;
5136 output_buffer = realloc(converter->outbuf, converter->output_buffer_size);
5137 if (output_buffer == NULL) {
5138 perror("can't realloc");
5141 converter->output_buffer = output_buffer;
5144 perror("can't iconv");
5157 nkf_iconv_close(nkf_iconv_t *convert)
5159 nkf_xfree(converter->inbuf);
5160 nkf_xfree(converter->outbuf);
5161 iconv_close(converter->cd);
5170 struct input_code *p = input_code_list;
5182 mime_f = MIME_DECODE_DEFAULT;
5183 mime_decode_f = FALSE;
5188 x0201_f = X0201_DEFAULT;
5189 iso2022jp_f = FALSE;
5190 #if defined(UTF8_INPUT_ENABLE) || defined(UTF8_OUTPUT_ENABLE)
5191 ms_ucs_map_f = UCS_MAP_ASCII;
5193 #ifdef UTF8_INPUT_ENABLE
5194 no_cp932ext_f = FALSE;
5195 no_best_fit_chars_f = FALSE;
5196 encode_fallback = NULL;
5197 unicode_subchar = '?';
5198 input_endian = ENDIAN_BIG;
5200 #ifdef UTF8_OUTPUT_ENABLE
5201 output_bom_f = FALSE;
5202 output_endian = ENDIAN_BIG;
5204 #ifdef UNICODE_NORMALIZATION
5220 #ifdef SHIFTJIS_CP932
5230 for (i = 0; i < 256; i++){
5231 prefix_table[i] = 0;
5235 mimeout_state.count = 0;
5240 fold_preserve_f = FALSE;
5243 kanji_intro = DEFAULT_J;
5244 ascii_intro = DEFAULT_R;
5245 fold_margin = FOLD_MARGIN;
5246 o_zconv = no_connection;
5247 o_fconv = no_connection;
5248 o_eol_conv = no_connection;
5249 o_rot_conv = no_connection;
5250 o_hira_conv = no_connection;
5251 o_base64conv = no_connection;
5252 o_iso2022jp_check_conv = no_connection;
5255 i_ungetc = std_ungetc;
5257 i_bungetc = std_ungetc;
5260 i_mungetc = std_ungetc;
5261 i_mgetc_buf = std_getc;
5262 i_mungetc_buf = std_ungetc;
5263 output_mode = ASCII;
5265 mime_decode_mode = FALSE;
5271 z_prev2=0,z_prev1=0;
5273 iconv_for_check = 0;
5275 input_codename = NULL;
5276 input_encoding = NULL;
5277 output_encoding = NULL;
5285 module_connection(void)
5287 if (input_encoding) set_input_encoding(input_encoding);
5288 if (!output_encoding) {
5289 output_encoding = nkf_default_encoding();
5291 if (!output_encoding) {
5292 if (noout_f || guess_f) output_encoding = nkf_enc_from_index(ISO_2022_JP);
5295 set_output_encoding(output_encoding);
5296 oconv = nkf_enc_to_oconv(output_encoding);
5298 if (nkf_enc_unicode_p(output_encoding))
5299 output_mode = UTF_8;
5301 /* replace continucation module, from output side */
5303 /* output redicrection */
5305 if (noout_f || guess_f){
5312 if (mimeout_f == TRUE) {
5313 o_base64conv = oconv; oconv = base64_conv;
5315 /* base64_count = 0; */
5318 if (eolmode_f || guess_f) {
5319 o_eol_conv = oconv; oconv = eol_conv;
5322 o_rot_conv = oconv; oconv = rot_conv;
5325 o_iso2022jp_check_conv = oconv; oconv = iso2022jp_check_conv;
5328 o_hira_conv = oconv; oconv = hira_conv;
5331 o_fconv = oconv; oconv = fold_conv;
5334 if (alpha_f || x0201_f) {
5335 o_zconv = oconv; oconv = z_conv;
5339 i_ungetc = std_ungetc;
5340 /* input redicrection */
5343 i_cgetc = i_getc; i_getc = cap_getc;
5344 i_cungetc = i_ungetc; i_ungetc= cap_ungetc;
5347 i_ugetc = i_getc; i_getc = url_getc;
5348 i_uungetc = i_ungetc; i_ungetc= url_ungetc;
5351 #ifdef NUMCHAR_OPTION
5353 i_ngetc = i_getc; i_getc = numchar_getc;
5354 i_nungetc = i_ungetc; i_ungetc= numchar_ungetc;
5357 #ifdef UNICODE_NORMALIZATION
5359 i_nfc_getc = i_getc; i_getc = nfc_getc;
5360 i_nfc_ungetc = i_ungetc; i_ungetc= nfc_ungetc;
5363 if (mime_f && mimebuf_f==FIXED_MIME) {
5364 i_mgetc = i_getc; i_getc = mime_getc;
5365 i_mungetc = i_ungetc; i_ungetc = mime_ungetc;
5368 i_bgetc = i_getc; i_getc = broken_getc;
5369 i_bungetc = i_ungetc; i_ungetc = broken_ungetc;
5371 if (input_encoding) {
5372 set_iconv(-TRUE, nkf_enc_to_iconv(input_encoding));
5374 set_iconv(FALSE, e_iconv);
5378 struct input_code *p = input_code_list;
5387 Conversion main loop. Code detection only.
5390 #if !defined(PERL_XS) && !defined(WIN32DLL)
5397 module_connection();
5398 while ((c = (*i_getc)(f)) != EOF)
5405 #define NEXT continue /* no output, get next */
5406 #define SKIP c2=0;continue /* no output, get next */
5407 #define MORE c2=c1;continue /* need one more byte */
5408 #define SEND ; /* output c1 and c2, get next */
5409 #define LAST break /* end of loop, go closing */
5410 #define set_input_mode(mode) do { \
5411 input_mode = mode; \
5413 set_input_codename("ISO-2022-JP"); \
5414 debug("ISO-2022-JP"); \
5418 kanji_convert(FILE *f)
5420 nkf_char c1=0, c2=0, c3=0, c4=0;
5421 int shift_mode = 0; /* 0, 1, 2, 3 */
5423 int is_8bit = FALSE;
5425 if (input_encoding && !nkf_enc_asciicompat(input_encoding)) {
5430 output_mode = ASCII;
5432 if (module_connection() < 0) {
5433 #if !defined(PERL_XS) && !defined(WIN32DLL)
5434 fprintf(stderr, "no output encoding given\n");
5440 #ifdef UTF8_INPUT_ENABLE
5441 if(iconv == w_iconv32){
5442 while ((c1 = (*i_getc)(f)) != EOF &&
5443 (c2 = (*i_getc)(f)) != EOF &&
5444 (c3 = (*i_getc)(f)) != EOF &&
5445 (c4 = (*i_getc)(f)) != EOF) {
5446 nkf_iconv_utf_32(c1, c2, c3, c4);
5450 else if (iconv == w_iconv16) {
5451 while ((c1 = (*i_getc)(f)) != EOF &&
5452 (c2 = (*i_getc)(f)) != EOF) {
5453 if (nkf_iconv_utf_16(c1, c2, 0, 0) == -2 &&
5454 (c3 = (*i_getc)(f)) != EOF &&
5455 (c4 = (*i_getc)(f)) != EOF) {
5456 nkf_iconv_utf_16(c1, c2, c3, c4);
5463 while ((c1 = (*i_getc)(f)) != EOF) {
5464 #ifdef INPUT_CODE_FIX
5465 if (!input_encoding)
5471 /* in case of 8th bit is on */
5472 if (!estab_f&&!mime_decode_mode) {
5473 /* in case of not established yet */
5474 /* It is still ambiguious */
5475 if (h_conv(f, c2, c1)==EOF) {
5483 /* in case of already established */
5485 /* ignore bogus code */
5493 /* 2nd byte of 7 bit code or SJIS */
5497 else if (nkf_char_unicode_p(c1)) {
5503 if (input_mode == JIS_X_0208 && DEL <= c1 && c1 < 0x92) {
5506 }else if (input_codename && input_codename[0] == 'I' &&
5507 0xA1 <= c1 && c1 <= 0xDF) {
5508 /* JIS X 0201 Katakana in 8bit JIS */
5509 c2 = JIS_X_0201_1976_K;
5512 } else if (c1 > DEL) {
5514 if (!estab_f && !iso8859_f) {
5515 /* not established yet */
5517 } else { /* estab_f==TRUE */
5523 else if ((iconv == s_iconv && 0xA0 <= c1 && c1 <= 0xDF) ||
5524 (ms_ucs_map_f == UCS_MAP_CP10001 && (c1 == 0xFD || c1 == 0xFE))) {
5526 c2 = JIS_X_0201_1976_K;
5531 /* already established */
5535 } else if (SP < c1 && c1 < DEL) {
5536 /* in case of Roman characters */
5538 /* output 1 shifted byte */
5542 } else if (nkf_byte_jisx0201_katakana_p(c1)){
5543 /* output 1 shifted byte */
5544 c2 = JIS_X_0201_1976_K;
5547 /* look like bogus code */
5550 } else if (input_mode == JIS_X_0208 || input_mode == JIS_X_0212 ||
5551 input_mode == JIS_X_0213_1 || input_mode == JIS_X_0213_2) {
5552 /* in case of Kanji shifted */
5554 } else if (c1 == '=' && mime_f && !mime_decode_mode) {
5555 /* Check MIME code */
5556 if ((c1 = (*i_getc)(f)) == EOF) {
5559 } else if (c1 == '?') {
5560 /* =? is mime conversion start sequence */
5561 if(mime_f == STRICT_MIME) {
5562 /* check in real detail */
5563 if (mime_begin_strict(f) == EOF)
5566 } else if (mime_begin(f) == EOF)
5575 /* normal ASCII code */
5578 } else if (c1 == SI && (!is_8bit || mime_decode_mode)) {
5581 } else if (c1 == SO && (!is_8bit || mime_decode_mode)) {
5584 } else if (c1 == ESC && (!is_8bit || mime_decode_mode)) {
5585 if ((c1 = (*i_getc)(f)) == EOF) {
5589 else if (c1 == '&') {
5591 if ((c1 = (*i_getc)(f)) == EOF) {
5597 else if (c1 == '$') {
5599 if ((c1 = (*i_getc)(f)) == EOF) {
5600 /* don't send bogus code
5602 (*oconv)(0, '$'); */
5604 } else if (c1 == '@' || c1 == 'B') {
5606 set_input_mode(JIS_X_0208);
5608 } else if (c1 == '(') {
5610 if ((c1 = (*i_getc)(f)) == EOF) {
5611 /* don't send bogus code
5617 } else if (c1 == '@'|| c1 == 'B') {
5619 set_input_mode(JIS_X_0208);
5622 } else if (c1 == 'D'){
5623 set_input_mode(JIS_X_0212);
5625 #endif /* X0212_ENABLE */
5626 } else if (c1 == 'O' || c1 == 'Q'){
5627 set_input_mode(JIS_X_0213_1);
5629 } else if (c1 == 'P'){
5630 set_input_mode(JIS_X_0213_2);
5633 /* could be some special code */
5640 } else if (broken_f&0x2) {
5641 /* accept any ESC-(-x as broken code ... */
5642 input_mode = JIS_X_0208;
5651 } else if (c1 == '(') {
5653 if ((c1 = (*i_getc)(f)) == EOF) {
5654 /* don't send bogus code
5656 (*oconv)(0, '('); */
5659 else if (c1 == 'I') {
5660 /* JIS X 0201 Katakana */
5661 set_input_mode(JIS_X_0201_1976_K);
5664 else if (c1 == 'B' || c1 == 'J' || c1 == 'H') {
5665 /* ISO-646IRV:1983 or JIS X 0201 Roman or JUNET */
5666 set_input_mode(ASCII);
5669 else if (broken_f&0x2) {
5670 set_input_mode(ASCII);
5679 else if (c1 == '.') {
5681 if ((c1 = (*i_getc)(f)) == EOF) {
5684 else if (c1 == 'A') {
5695 else if (c1 == 'N') {
5698 if (g2 == ISO_8859_1) {
5713 } else if (c1 == ESC && iconv == s_iconv) {
5714 /* ESC in Shift_JIS */
5715 if ((c1 = (*i_getc)(f)) == EOF) {
5718 } else if (c1 == '$') {
5720 if ((c1 = (*i_getc)(f)) == EOF) {
5722 } else if (('E' <= c1 && c1 <= 'G') ||
5723 ('O' <= c1 && c1 <= 'Q')) {
5731 static const nkf_char jphone_emoji_first_table[7] =
5732 {0xE1E0, 0xDFE0, 0xE2E0, 0xE3E0, 0xE4E0, 0xDFE0, 0xE0E0};
5733 c3 = nkf_char_unicode_new(jphone_emoji_first_table[c1 % 7]);
5734 if ((c1 = (*i_getc)(f)) == EOF) LAST;
5735 while (SP <= c1 && c1 <= 'z') {
5736 (*oconv)(0, c1 + c3);
5737 if ((c1 = (*i_getc)(f)) == EOF) LAST;
5752 } else if (c1 == LF || c1 == CR) {
5754 input_mode = ASCII; set_iconv(FALSE, 0);
5756 } else if (mime_decode_f && !mime_decode_mode){
5758 if ((c1=(*i_getc)(f))!=EOF && c1 == SP) {
5766 } else { /* if (c1 == CR)*/
5767 if ((c1=(*i_getc)(f))!=EOF) {
5771 } else if (c1 == LF && (c1=(*i_getc)(f))!=EOF && c1 == SP) {
5791 switch ((*iconv)(c2, c1, 0)) { /* can be EUC / SJIS / UTF-8 */
5794 if ((c3 = (*i_getc)(f)) != EOF) {
5797 if ((c4 = (*i_getc)(f)) != EOF) {
5799 (*iconv)(c2, c1, c3|c4);
5804 /* 3 bytes EUC or UTF-8 */
5805 if ((c3 = (*i_getc)(f)) != EOF) {
5807 (*iconv)(c2, c1, c3);
5815 0x7F <= c2 && c2 <= 0x92 &&
5816 0x21 <= c1 && c1 <= 0x7E) {
5818 c1 = nkf_char_unicode_new((c2 - 0x7F) * 94 + c1 - 0x21 + 0xE000);
5821 (*oconv)(c2, c1); /* this is JIS, not SJIS/EUC case */
5825 (*oconv)(PREFIX_EUCG3 | c2, c1);
5827 #endif /* X0212_ENABLE */
5829 (*oconv)(PREFIX_EUCG3 | c2, c1);
5832 (*oconv)(input_mode, c1); /* other special case */
5838 /* goto next_word */
5843 (*iconv)(EOF, 0, 0);
5844 if (!input_codename)
5847 struct input_code *p = input_code_list;
5848 struct input_code *result = p;
5850 if (p->score < result->score) result = p;
5853 set_input_codename(result->name);
5855 debug(result->name);
5863 * int options(unsigned char *cp)
5870 options(unsigned char *cp)
5874 unsigned char *cp_back = NULL;
5879 while(*cp && *cp++!='-');
5880 while (*cp || cp_back) {
5888 case '-': /* literal options */
5889 if (!*cp || *cp == SP) { /* ignore the rest of arguments */
5893 for (i=0;i<sizeof(long_option)/sizeof(long_option[0]);i++) {
5894 p = (unsigned char *)long_option[i].name;
5895 for (j=0;*p && *p != '=' && *p == cp[j];p++, j++);
5896 if (*p == cp[j] || cp[j] == SP){
5903 #if !defined(PERL_XS) && !defined(WIN32DLL)
5904 fprintf(stderr, "unknown long option: --%s\n", cp);
5908 while(*cp && *cp != SP && cp++);
5909 if (long_option[i].alias[0]){
5911 cp = (unsigned char *)long_option[i].alias;
5914 if (strcmp(long_option[i].name, "help") == 0){
5919 if (strcmp(long_option[i].name, "ic=") == 0){
5920 enc = nkf_enc_find((char *)p);
5922 input_encoding = enc;
5925 if (strcmp(long_option[i].name, "oc=") == 0){
5926 enc = nkf_enc_find((char *)p);
5927 /* if (enc <= 0) continue; */
5929 output_encoding = enc;
5932 if (strcmp(long_option[i].name, "guess=") == 0){
5933 if (p[0] == '0' || p[0] == '1') {
5941 if (strcmp(long_option[i].name, "overwrite") == 0){
5944 preserve_time_f = TRUE;
5947 if (strcmp(long_option[i].name, "overwrite=") == 0){
5950 preserve_time_f = TRUE;
5952 backup_suffix = (char *)p;
5955 if (strcmp(long_option[i].name, "in-place") == 0){
5958 preserve_time_f = FALSE;
5961 if (strcmp(long_option[i].name, "in-place=") == 0){
5964 preserve_time_f = FALSE;
5966 backup_suffix = (char *)p;
5971 if (strcmp(long_option[i].name, "cap-input") == 0){
5975 if (strcmp(long_option[i].name, "url-input") == 0){
5980 #ifdef NUMCHAR_OPTION
5981 if (strcmp(long_option[i].name, "numchar-input") == 0){
5987 if (strcmp(long_option[i].name, "no-output") == 0){
5991 if (strcmp(long_option[i].name, "debug") == 0){
5996 if (strcmp(long_option[i].name, "cp932") == 0){
5997 #ifdef SHIFTJIS_CP932
6001 #ifdef UTF8_OUTPUT_ENABLE
6002 ms_ucs_map_f = UCS_MAP_CP932;
6006 if (strcmp(long_option[i].name, "no-cp932") == 0){
6007 #ifdef SHIFTJIS_CP932
6011 #ifdef UTF8_OUTPUT_ENABLE
6012 ms_ucs_map_f = UCS_MAP_ASCII;
6016 #ifdef SHIFTJIS_CP932
6017 if (strcmp(long_option[i].name, "cp932inv") == 0){
6024 if (strcmp(long_option[i].name, "x0212") == 0){
6031 if (strcmp(long_option[i].name, "exec-in") == 0){
6035 if (strcmp(long_option[i].name, "exec-out") == 0){
6040 #if defined(UTF8_OUTPUT_ENABLE) && defined(UTF8_INPUT_ENABLE)
6041 if (strcmp(long_option[i].name, "no-cp932ext") == 0){
6042 no_cp932ext_f = TRUE;
6045 if (strcmp(long_option[i].name, "no-best-fit-chars") == 0){
6046 no_best_fit_chars_f = TRUE;
6049 if (strcmp(long_option[i].name, "fb-skip") == 0){
6050 encode_fallback = NULL;
6053 if (strcmp(long_option[i].name, "fb-html") == 0){
6054 encode_fallback = encode_fallback_html;
6057 if (strcmp(long_option[i].name, "fb-xml") == 0){
6058 encode_fallback = encode_fallback_xml;
6061 if (strcmp(long_option[i].name, "fb-java") == 0){
6062 encode_fallback = encode_fallback_java;
6065 if (strcmp(long_option[i].name, "fb-perl") == 0){
6066 encode_fallback = encode_fallback_perl;
6069 if (strcmp(long_option[i].name, "fb-subchar") == 0){
6070 encode_fallback = encode_fallback_subchar;
6073 if (strcmp(long_option[i].name, "fb-subchar=") == 0){
6074 encode_fallback = encode_fallback_subchar;
6075 unicode_subchar = 0;
6077 /* decimal number */
6078 for (i = 0; i < 7 && nkf_isdigit(p[i]); i++){
6079 unicode_subchar *= 10;
6080 unicode_subchar += hex2bin(p[i]);
6082 }else if(p[1] == 'x' || p[1] == 'X'){
6083 /* hexadecimal number */
6084 for (i = 2; i < 8 && nkf_isxdigit(p[i]); i++){
6085 unicode_subchar <<= 4;
6086 unicode_subchar |= hex2bin(p[i]);
6090 for (i = 1; i < 8 && nkf_isoctal(p[i]); i++){
6091 unicode_subchar *= 8;
6092 unicode_subchar += hex2bin(p[i]);
6095 w16e_conv(unicode_subchar, &i, &j);
6096 unicode_subchar = i<<8 | j;
6100 #ifdef UTF8_OUTPUT_ENABLE
6101 if (strcmp(long_option[i].name, "ms-ucs-map") == 0){
6102 ms_ucs_map_f = UCS_MAP_MS;
6106 #ifdef UNICODE_NORMALIZATION
6107 if (strcmp(long_option[i].name, "utf8mac-input") == 0){
6112 if (strcmp(long_option[i].name, "prefix=") == 0){
6113 if (nkf_isgraph(p[0])){
6114 for (i = 1; nkf_isgraph(p[i]); i++){
6115 prefix_table[p[i]] = p[0];
6120 #if !defined(PERL_XS) && !defined(WIN32DLL)
6121 fprintf(stderr, "unsupported long option: --%s\n", long_option[i].name);
6126 case 'b': /* buffered mode */
6129 case 'u': /* non bufferd mode */
6132 case 't': /* transparent mode */
6137 } else if (*cp=='2') {
6141 * nkf -t2MB hoge.bin | nkf -t2mB | diff -s - hoge.bin
6149 case 'j': /* JIS output */
6151 output_encoding = nkf_enc_from_index(ISO_2022_JP);
6153 case 'e': /* AT&T EUC output */
6154 output_encoding = nkf_enc_from_index(EUCJP_NKF);
6156 case 's': /* SJIS output */
6157 output_encoding = nkf_enc_from_index(SHIFT_JIS);
6159 case 'l': /* ISO8859 Latin-1 support, no conversion */
6160 iso8859_f = TRUE; /* Only compatible with ISO-2022-JP */
6161 input_encoding = nkf_enc_from_index(ISO_8859_1);
6163 case 'i': /* Kanji IN ESC-$-@/B */
6164 if (*cp=='@'||*cp=='B')
6165 kanji_intro = *cp++;
6167 case 'o': /* ASCII IN ESC-(-J/B/H */
6168 /* ESC ( H was used in initial JUNET messages */
6169 if (*cp=='J'||*cp=='B'||*cp=='H')
6170 ascii_intro = *cp++;
6174 bit:1 katakana->hiragana
6175 bit:2 hiragana->katakana
6177 if ('9'>= *cp && *cp>='0')
6178 hira_f |= (*cp++ -'0');
6185 #if defined(MSDOS) || defined(__OS2__)
6192 show_configuration();
6200 #ifdef UTF8_OUTPUT_ENABLE
6201 case 'w': /* UTF-8 output */
6206 output_encoding = nkf_enc_from_index(UTF_8N);
6208 output_bom_f = TRUE;
6209 output_encoding = nkf_enc_from_index(UTF_8_BOM);
6213 if ('1'== cp[0] && '6'==cp[1]) {
6216 } else if ('3'== cp[0] && '2'==cp[1]) {
6220 output_encoding = nkf_enc_from_index(UTF_8);
6225 output_endian = ENDIAN_LITTLE;
6226 } else if (cp[0] == 'B') {
6231 enc_idx = enc_idx == UTF_16
6232 ? (output_endian == ENDIAN_LITTLE ? UTF_16LE : UTF_16BE)
6233 : (output_endian == ENDIAN_LITTLE ? UTF_32LE : UTF_32BE);
6235 output_bom_f = TRUE;
6236 enc_idx = enc_idx == UTF_16
6237 ? (output_endian == ENDIAN_LITTLE ? UTF_16LE_BOM : UTF_16BE_BOM)
6238 : (output_endian == ENDIAN_LITTLE ? UTF_32LE_BOM : UTF_32BE_BOM);
6240 output_encoding = nkf_enc_from_index(enc_idx);
6244 #ifdef UTF8_INPUT_ENABLE
6245 case 'W': /* UTF input */
6248 input_encoding = nkf_enc_from_index(UTF_8);
6251 if ('1'== cp[0] && '6'==cp[1]) {
6253 input_endian = ENDIAN_BIG;
6255 } else if ('3'== cp[0] && '2'==cp[1]) {
6257 input_endian = ENDIAN_BIG;
6260 input_encoding = nkf_enc_from_index(UTF_8);
6265 input_endian = ENDIAN_LITTLE;
6266 } else if (cp[0] == 'B') {
6268 input_endian = ENDIAN_BIG;
6270 enc_idx = (enc_idx == UTF_16
6271 ? (input_endian == ENDIAN_LITTLE ? UTF_16LE : UTF_16BE)
6272 : (input_endian == ENDIAN_LITTLE ? UTF_32LE : UTF_32BE));
6273 input_encoding = nkf_enc_from_index(enc_idx);
6277 /* Input code assumption */
6278 case 'J': /* ISO-2022-JP input */
6279 input_encoding = nkf_enc_from_index(ISO_2022_JP);
6281 case 'E': /* EUC-JP input */
6282 input_encoding = nkf_enc_from_index(EUCJP_NKF);
6284 case 'S': /* Shift_JIS input */
6285 input_encoding = nkf_enc_from_index(SHIFT_JIS);
6287 case 'Z': /* Convert X0208 alphabet to asii */
6289 bit:0 Convert JIS X 0208 Alphabet to ASCII
6290 bit:1 Convert Kankaku to one space
6291 bit:2 Convert Kankaku to two spaces
6292 bit:3 Convert HTML Entity
6293 bit:4 Convert JIS X 0208 Katakana to JIS X 0201 Katakana
6295 while ('0'<= *cp && *cp <='4') {
6296 alpha_f |= 1 << (*cp++ - '0');
6300 case 'x': /* Convert X0201 kana to X0208 or X0201 Conversion */
6301 x0201_f = FALSE; /* No X0201->X0208 conversion */
6303 ESC-(-I in JIS, EUC, MS Kanji
6304 SI/SO in JIS, EUC, MS Kanji
6305 SS2 in EUC, JIS, not in MS Kanji
6306 MS Kanji (0xa0-0xdf)
6308 ESC-(-I in JIS (0x20-0x5f)
6309 SS2 in EUC (0xa0-0xdf)
6310 0xa0-0xd in MS Kanji (0xa0-0xdf)
6313 case 'X': /* Convert X0201 kana to X0208 */
6316 case 'F': /* prserve new lines */
6317 fold_preserve_f = TRUE;
6318 case 'f': /* folding -f60 or -f */
6321 while('0'<= *cp && *cp <='9') { /* we don't use atoi here */
6323 fold_len += *cp++ - '0';
6325 if (!(0<fold_len && fold_len<BUFSIZ))
6326 fold_len = DEFAULT_FOLD;
6330 while('0'<= *cp && *cp <='9') { /* we don't use atoi here */
6332 fold_margin += *cp++ - '0';
6336 case 'm': /* MIME support */
6337 /* mime_decode_f = TRUE; */ /* this has too large side effects... */
6338 if (*cp=='B'||*cp=='Q') {
6339 mime_decode_mode = *cp++;
6340 mimebuf_f = FIXED_MIME;
6341 } else if (*cp=='N') {
6342 mime_f = TRUE; cp++;
6343 } else if (*cp=='S') {
6344 mime_f = STRICT_MIME; cp++;
6345 } else if (*cp=='0') {
6346 mime_decode_f = FALSE;
6347 mime_f = FALSE; cp++;
6349 mime_f = STRICT_MIME;
6352 case 'M': /* MIME output */
6355 mimeout_f = FIXED_MIME; cp++;
6356 } else if (*cp=='Q') {
6358 mimeout_f = FIXED_MIME; cp++;
6363 case 'B': /* Broken JIS support */
6365 bit:1 allow any x on ESC-(-x or ESC-$-x
6366 bit:2 reset to ascii on NL
6368 if ('9'>= *cp && *cp>='0')
6369 broken_f |= 1<<(*cp++ -'0');
6374 case 'O':/* for Output file */
6378 case 'c':/* add cr code */
6381 case 'd':/* delete cr code */
6384 case 'I': /* ISO-2022-JP output */
6387 case 'L': /* line mode */
6388 if (*cp=='u') { /* unix */
6389 eolmode_f = LF; cp++;
6390 } else if (*cp=='m') { /* mac */
6391 eolmode_f = CR; cp++;
6392 } else if (*cp=='w') { /* windows */
6393 eolmode_f = CRLF; cp++;
6394 } else if (*cp=='0') { /* no conversion */
6395 eolmode_f = 0; cp++;
6400 if ('2' <= *cp && *cp <= '9') {
6403 } else if (*cp == '0' || *cp == '1') {
6412 /* module muliple options in a string are allowed for Perl moudle */
6413 while(*cp && *cp++!='-');
6416 #if !defined(PERL_XS) && !defined(WIN32DLL)
6417 fprintf(stderr, "unknown option: -%c\n", *(cp-1));
6419 /* bogus option but ignored */
6427 #include "nkf32dll.c"
6428 #elif defined(PERL_XS)
6429 #else /* WIN32DLL */
6431 main(int argc, char **argv)
6436 char *outfname = NULL;
6439 #ifdef EASYWIN /*Easy Win */
6440 _BufferSize.y = 400;/*Set Scroll Buffer Size*/
6442 #ifdef DEFAULT_CODE_LOCALE
6443 setlocale(LC_CTYPE, "");
6447 for (argc--,argv++; (argc > 0) && **argv == '-'; argc--, argv++) {
6448 cp = (unsigned char *)*argv;
6453 if (pipe(fds) < 0 || (pid = fork()) < 0){
6464 execvp(argv[1], &argv[1]);
6481 int debug_f_back = debug_f;
6484 int exec_f_back = exec_f;
6487 int x0212_f_back = x0212_f;
6489 int x0213_f_back = x0213_f;
6490 int guess_f_back = guess_f;
6492 guess_f = guess_f_back;
6495 debug_f = debug_f_back;
6498 exec_f = exec_f_back;
6500 x0212_f = x0212_f_back;
6501 x0213_f = x0213_f_back;
6504 if (binmode_f == TRUE)
6505 #if defined(__OS2__) && (defined(__IBMC__) || defined(__IBMCPP__))
6506 if (freopen("","wb",stdout) == NULL)
6513 setbuf(stdout, (char *) NULL);
6515 setvbuffer(stdout, (char *) stdobuf, IOBUF_SIZE);
6518 if (binmode_f == TRUE)
6519 #if defined(__OS2__) && (defined(__IBMC__) || defined(__IBMCPP__))
6520 if (freopen("","rb",stdin) == NULL) return (-1);
6524 setvbuffer(stdin, (char *) stdibuf, IOBUF_SIZE);
6528 kanji_convert(stdin);
6529 if (guess_f) print_guessed_code(NULL);
6533 int is_argument_error = FALSE;
6535 input_codename = NULL;
6538 iconv_for_check = 0;
6540 if ((fin = fopen((origfname = *argv++), "r")) == NULL) {
6542 is_argument_error = TRUE;
6550 /* reopen file for stdout */
6551 if (file_out_f == TRUE) {
6554 outfname = nkf_xmalloc(strlen(origfname)
6555 + strlen(".nkftmpXXXXXX")
6557 strcpy(outfname, origfname);
6561 for (i = strlen(outfname); i; --i){
6562 if (outfname[i - 1] == '/'
6563 || outfname[i - 1] == '\\'){
6569 strcat(outfname, "ntXXXXXX");
6571 fd = open(outfname, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL,
6572 S_IREAD | S_IWRITE);
6574 strcat(outfname, ".nkftmpXXXXXX");
6575 fd = mkstemp(outfname);
6578 || (fd_backup = dup(fileno(stdout))) < 0
6579 || dup2(fd, fileno(stdout)) < 0
6590 outfname = "nkf.out";
6593 if(freopen(outfname, "w", stdout) == NULL) {
6597 if (binmode_f == TRUE) {
6598 #if defined(__OS2__) && (defined(__IBMC__) || defined(__IBMCPP__))
6599 if (freopen("","wb",stdout) == NULL)
6606 if (binmode_f == TRUE)
6607 #if defined(__OS2__) && (defined(__IBMC__) || defined(__IBMCPP__))
6608 if (freopen("","rb",fin) == NULL)
6613 setvbuffer(fin, (char *) stdibuf, IOBUF_SIZE);
6617 char *filename = NULL;
6619 if (nfiles > 1) filename = origfname;
6620 if (guess_f) print_guessed_code(filename);
6626 #if defined(MSDOS) && !defined(__MINGW32__) && !defined(__WIN32__) && !defined(__WATCOMC__) && !defined(__EMX__) && !defined(__OS2__) && !defined(__DJGPP__)
6634 if (dup2(fd_backup, fileno(stdout)) < 0){
6637 if (stat(origfname, &sb)) {
6638 fprintf(stderr, "Can't stat %s\n", origfname);
6640 /*
\e$B%Q!<%_%C%7%g%s$rI|85
\e(B */
6641 if (chmod(outfname, sb.st_mode)) {
6642 fprintf(stderr, "Can't set permission %s\n", outfname);
6645 /*
\e$B%?%$%`%9%?%s%W$rI|85
\e(B */
6646 if(preserve_time_f){
6647 #if defined(MSDOS) && !defined(__MINGW32__) && !defined(__WIN32__) && !defined(__WATCOMC__) && !defined(__EMX__) && !defined(__OS2__) && !defined(__DJGPP__)
6648 tb[0] = tb[1] = sb.st_mtime;
6649 if (utime(outfname, tb)) {
6650 fprintf(stderr, "Can't set timestamp %s\n", outfname);
6653 tb.actime = sb.st_atime;
6654 tb.modtime = sb.st_mtime;
6655 if (utime(outfname, &tb)) {
6656 fprintf(stderr, "Can't set timestamp %s\n", outfname);
6661 char *backup_filename = get_backup_filename(backup_suffix, origfname);
6663 unlink(backup_filename);
6665 if (rename(origfname, backup_filename)) {
6666 perror(backup_filename);
6667 fprintf(stderr, "Can't rename %s to %s\n",
6668 origfname, backup_filename);
6670 nkf_xfree(backup_filename);
6673 if (unlink(origfname)){
6678 if (rename(outfname, origfname)) {
6680 fprintf(stderr, "Can't rename %s to %s\n",
6681 outfname, origfname);
6683 nkf_xfree(outfname);
6688 if (is_argument_error)
6691 #ifdef EASYWIN /*Easy Win */
6692 if (file_out_f == FALSE)
6693 scanf("%d",&end_check);
6696 #else /* for Other OS */
6697 if (file_out_f == TRUE)
6699 #endif /*Easy Win */
6702 #endif /* WIN32DLL */