OSDN Git Service

Use nkf_isblank and nkf_isspace.
[nkf/nkf.git] / nkf.c
diff --git a/nkf.c b/nkf.c
index feaf57c..014b3fd 100644 (file)
--- a/nkf.c
+++ b/nkf.c
@@ -1,42 +1,30 @@
-/** Network Kanji Filter. (PDS Version)
- ** -*- coding: ISO-2022-JP -*-
- ************************************************************************
- ** Copyright (C) 1987, Fujitsu LTD. (Itaru ICHIKAWA)
- ** \e$BO"Mm@h!'\e(B \e$B!J3t!KIY;NDL8&5f=j!!%=%U%H#38&!!;T@n!!;j\e(B
- ** \e$B!J\e(BE-Mail Address: ichikawa@flab.fujitsu.co.jp\e$B!K\e(B
- ** Copyright (C) 1996,1998
- ** Copyright (C) 2002
- ** \e$BO"Mm@h!'\e(B \e$BN05eBg3X>pJs9)3X2J\e(B \e$B2OLn\e(B \e$B??<#\e(B  mime/X0208 support
- ** \e$B!J\e(BE-Mail Address: kono@ie.u-ryukyu.ac.jp\e$B!K\e(B
- ** \e$BO"Mm@h!'\e(B COW for DOS & Win16 & Win32 & OS/2
- ** \e$B!J\e(BE-Mail Address: GHG00637@niftyserve.or.p\e$B!K\e(B
- **
- **    \e$B$3$N%=!<%9$N$$$+$J$kJ#<L!$2~JQ!$=$@5$b5vBz$7$^$9!#$?$@$7!"\e(B
- **    \e$B$=$N:]$K$O!"C/$,9W8%$7$?$r<($9$3$NItJ,$r;D$9$3$H!#\e(B
- **    \e$B:FG[I[$d;(;o$NIUO?$J$I$NLd$$9g$o$;$bI,MW$"$j$^$;$s!#\e(B
- **    \e$B1DMxMxMQ$b>e5-$KH?$7$J$$HO0O$G5v2D$7$^$9!#\e(B
- **    \e$B%P%$%J%j$NG[I[$N:]$K$O\e(Bversion message\e$B$rJ]B8$9$k$3$H$r>r7o$H$7$^$9!#\e(B
- **    \e$B$3$N%W%m%0%i%`$K$D$$$F$OFC$K2?$NJ]>Z$b$7$J$$!"0-$7$+$i$:!#\e(B
- **
- **    Everyone is permitted to do anything on this program
- **    including copying, modifying, improving,
- **    as long as you don't try to pretend that you wrote it.
- **    i.e., the above copyright notice has to appear in all copies.
- **    Binary distribution requires original version messages.
- **    You don't have to ask before copying, redistribution or publishing.
- **    THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE.
- ***********************************************************************/
-
-/***********************************************************************
- * \e$B8=:_!"\e(Bnkf \e$B$O\e(B SorceForge \e$B$K$F%a%s%F%J%s%9$,B3$1$i$l$F$$$^$9!#\e(B
- * http://sourceforge.jp/projects/nkf/
- ***********************************************************************/
-#define NKF_IDENT "$Id: nkf.c,v 1.192 2008/11/09 23:09:22 naruse Exp $"
+/*
+ * Copyright (c) 1987, Fujitsu LTD. (Itaru ICHIKAWA).
+ * Copyright (c) 1996-2009, The nkf Project.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ * 
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
 #define NKF_VERSION "2.0.8"
-#define NKF_RELEASE_DATE "2008-12-25"
+#define NKF_RELEASE_DATE "2009-01-05"
 #define COPY_RIGHT \
-    "Copyright (C) 1987, FUJITSU LTD. (I.Ichikawa),2000 S. Kono, COW\n" \
-    "Copyright (C) 2002-2008 Kono, Furukawa, Naruse, mastodon"
+    "Copyright (C) 1987, FUJITSU LTD. (I.Ichikawa).\n" \
+    "Copyright (C) 1996-2009, The nkf Project."
 
 #include "config.h"
 #include "nkf.h"
@@ -52,9 +40,6 @@
 #endif
 #include <assert.h>
 
-#define nkf_debug(fmt, ...) \
-    fprintf(stderr, "%s(%s)%d: " fmt "\n", __FILE__, __func__, __LINE__, __VA_ARGS__)
-
 
 /* state of output_mode and input_mode
 
@@ -144,7 +129,7 @@ enum nkf_encodings {
     JIS_X_0212        = 0x1159, /* D */
     /* JIS_X_0213_2000_1 = 0x1228, */ /* O */
     JIS_X_0213_2 = 0x1229, /* P */
-    JIS_X_0213_1 = 0x1233, /* Q */
+    JIS_X_0213_1 = 0x1233 /* Q */
 };
 
 static nkf_char s_iconv(nkf_char c2, nkf_char c1, nkf_char c0);
@@ -664,12 +649,8 @@ static nkf_char prev_cr = 0; /* CR or 0 */
 static int             end_check;
 #endif /*Easy Win */
 
-#define STD_GC_BUFSIZE (256)
-nkf_char std_gc_buf[STD_GC_BUFSIZE];
-nkf_char std_gc_ndx;
-
 static void *
-nkf_malloc(size_t size)
+nkf_xmalloc(size_t size)
 {
     void *ptr;
 
@@ -685,7 +666,7 @@ nkf_malloc(size_t size)
 }
 
 static void *
-nkf_realloc(void *ptr, size_t size)
+nkf_xrealloc(void *ptr, size_t size)
 {
     if (size == 0) size = 1;
 
@@ -698,7 +679,7 @@ nkf_realloc(void *ptr, size_t size)
     return ptr;
 }
 
-#define nkf_free(ptr) free(ptr)
+#define nkf_xfree(ptr) free(ptr)
 
 static int
 nkf_str_caseeql(const char *src, const char *target)
@@ -818,6 +799,62 @@ nkf_default_encoding()
     return enc;
 }
 
+typedef struct {
+    long capa;
+    long len;
+    unsigned char *ptr;
+} nkf_buf_t;
+
+static nkf_buf_t *
+nkf_buf_new(int length)
+{
+    nkf_buf_t *buf = nkf_xmalloc(sizeof(nkf_buf_t));
+    buf->ptr = nkf_xmalloc(length);
+    buf->capa = length;
+    buf->len = 0;
+    return buf;
+} 
+
+static void
+nkf_buf_dispose(nkf_buf_t *buf)
+{
+    nkf_xfree(buf->ptr);
+    nkf_xfree(buf);
+}
+
+#define nkf_buf_length(buf) ((buf)->len)
+#define nkf_buf_empty_p(buf) ((buf)->len == 0)
+
+static unsigned char
+nkf_buf_at(nkf_buf_t *buf, int index)
+{
+    assert(index <= buf->len);
+    return buf->ptr[index];
+}
+
+static void
+nkf_buf_clear(nkf_buf_t *buf)
+{
+    buf->len = 0;
+}
+
+static void
+nkf_buf_push(nkf_buf_t *buf, unsigned char c)
+{
+    if (buf->capa <= buf->len) {
+       exit(EXIT_FAILURE);
+    }
+    buf->ptr[buf->len++] = c;
+}
+
+static unsigned char
+nkf_buf_pop(nkf_buf_t *buf)
+{
+    assert(!nkf_buf_empty_p(buf));
+    return buf->ptr[--buf->len];
+}
+
+/* Normalization Form C */
 #ifndef PERL_XS
 #ifdef WIN32DLL
 #define fprintf dllprintf
@@ -845,6 +882,8 @@ usage(void)
            "         After 'W' you can add more options. -W[ 8, 16 [BL] ] \n"
 #endif
            "t        no conversion\n"
+           );
+    fprintf(HELP_OUTPUT,
            "i[@B]    Specify the Esc Seq for JIS X 0208-1978/83 (DEFAULT B)\n"
            "o[BJH]   Specify the Esc Seq for ASCII/Roman        (DEFAULT B)\n"
            "r        {de/en}crypt ROT13/47\n"
@@ -853,11 +892,15 @@ usage(void)
            "M[BQ]    MIME encode [B:base64 Q:quoted]\n"
            "l        ISO8859-1 (Latin-1) support\n"
            "f/F      Folding: -f60 or -f or -f60-10 (fold margin 10) F preserve nl\n"
+           );
+    fprintf(HELP_OUTPUT,
            "Z[0-4]   Default/0: Convert JISX0208 Alphabet to ASCII\n"
            "         1: Kankaku to one space  2: to two spaces  3: HTML Entity\n"
            "         4: JISX0208 Katakana to JISX0201 Katakana\n"
            "X,x      Assume X0201 kana in MS-Kanji, -x preserves X0201\n"
            "B[0-2]   Broken input  0: missing ESC,1: any X on ESC-[($]-X,2: ASCII on NL\n"
+           );
+    fprintf(HELP_OUTPUT,
 #ifdef MSDOS
            "T        Text mode output\n"
 #endif
@@ -866,7 +909,8 @@ usage(void)
            "d,c      Convert line breaks  -d: LF  -c: CRLF\n"
            "-L[uwm]  line mode u:LF w:CRLF m:CR (DEFAULT noconversion)\n"
            "v, V     Show this usage. V: show configuration\n"
-           "\n"
+           "\n");
+    fprintf(HELP_OUTPUT,
            "Long name options\n"
            " --ic=<input codeset>  --oc=<output codeset>\n"
            "                   Specify the input or output codeset\n"
@@ -876,6 +920,8 @@ usage(void)
            " --hiragana  --katakana  --katakana-hiragana\n"
            "                   To Hiragana/Katakana Conversion\n"
            " --prefix=         Insert escape before troublesome characters of Shift_JIS\n"
+           );
+    fprintf(HELP_OUTPUT,
 #ifdef INPUT_OPTION
            " --cap-input, --url-input  Convert hex after ':' or '%%'\n"
 #endif
@@ -886,6 +932,8 @@ usage(void)
            " --fb-{skip, html, xml, perl, java, subchar}\n"
            "                   Specify how nkf handles unassigned characters\n"
 #endif
+           );
+    fprintf(HELP_OUTPUT,
 #ifdef OVERWRITE
            " --in-place[=SUFFIX]  --overwrite[=SUFFIX]\n"
            "                   Overwrite original listed files by filtered result\n"
@@ -903,8 +951,6 @@ show_configuration(void)
 {
     fprintf(HELP_OUTPUT,
            "Summary of my nkf " NKF_VERSION " (" NKF_RELEASE_DATE ") configuration:\n"
-           "  nkf identity:\n"
-           "    " NKF_IDENT "\n"
            "  Compile-time options:\n"
            "    Compiled at:                 " __DATE__ " " __TIME__ "\n"
           );
@@ -966,7 +1012,7 @@ get_backup_filename(const char *suffix, const char *filename)
     }
 
     if(asterisk_count){
-       backup_filename = nkf_malloc(strlen(suffix) + (asterisk_count * (filename_length - 1)) + 1);
+       backup_filename = nkf_xmalloc(strlen(suffix) + (asterisk_count * (filename_length - 1)) + 1);
        for(i = 0, j = 0; suffix[i];){
            if(suffix[i] == '*'){
                backup_filename[j] = '\0';
@@ -980,7 +1026,7 @@ get_backup_filename(const char *suffix, const char *filename)
        backup_filename[j] = '\0';
     }else{
        j = filename_length + strlen(suffix);
-       backup_filename = nkf_malloc(j + 1);
+       backup_filename = nkf_xmalloc(j + 1);
        strcpy(backup_filename, filename);
        strcat(backup_filename, suffix);
        backup_filename[j] = '\0';
@@ -2622,25 +2668,25 @@ w_oconv32(nkf_char c2, nkf_char c1)
 }
 #endif
 
-#define SCORE_L2       (1)                   /* \e$BBh\e(B2\e$B?e=`4A;z\e(B */
-#define SCORE_KANA     (SCORE_L2 << 1)       /* \e$B$$$o$f$kH>3Q%+%J\e(B */
-#define SCORE_DEPEND   (SCORE_KANA << 1)     /* \e$B5!<o0MB8J8;z\e(B */
-#define SCORE_CP932    (SCORE_DEPEND << 1)   /* CP932 \e$B$K$h$kFI$_49$(\e(B (IBM extended characters) */
+#define SCORE_L2       (1)                   /* Kanji Level 2 */
+#define SCORE_KANA     (SCORE_L2 << 1)       /* Halfwidth Katakana */
+#define SCORE_DEPEND   (SCORE_KANA << 1)     /* MD Characters */
+#define SCORE_CP932    (SCORE_DEPEND << 1)   /* IBM extended characters */
 #define SCORE_X0212    (SCORE_CP932 << 1)    /* JIS X 0212 */
-#define SCORE_NO_EXIST (SCORE_X0212 << 1)    /* \e$BB8:_$7$J$$J8;z\e(B */
-#define SCORE_iMIME    (SCORE_NO_EXIST << 1) /* MIME \e$B$K$h$k;XDj\e(B */
-#define SCORE_ERROR    (SCORE_iMIME << 1) /* \e$B%(%i!<\e(B */
+#define SCORE_NO_EXIST (SCORE_X0212 << 1)    /* Undefined Characters */
+#define SCORE_iMIME    (SCORE_NO_EXIST << 1) /* MIME selected */
+#define SCORE_ERROR    (SCORE_iMIME << 1) /* Error */
 
 #define SCORE_INIT (SCORE_iMIME)
 
-static const char score_table_A0[] = {
+static const nkf_char score_table_A0[] = {
     0, 0, 0, 0,
     0, 0, 0, 0,
     0, SCORE_DEPEND, SCORE_DEPEND, SCORE_DEPEND,
     SCORE_DEPEND, SCORE_DEPEND, SCORE_DEPEND, SCORE_NO_EXIST,
 };
 
-static const char score_table_F0[] = {
+static const nkf_char score_table_F0[] = {
     SCORE_L2, SCORE_L2, SCORE_L2, SCORE_L2,
     SCORE_L2, SCORE_DEPEND, SCORE_NO_EXIST, SCORE_NO_EXIST,
     SCORE_DEPEND, SCORE_DEPEND, SCORE_CP932, SCORE_CP932,
@@ -2949,12 +2995,39 @@ code_status(nkf_char c)
     }
 }
 
+typedef struct {
+    nkf_buf_t *std_gc_buf;
+    nkf_char broken_state;
+    nkf_buf_t *broken_buf;
+    nkf_char mimeout_state;
+} nkf_state_t;
+
+static nkf_state_t *nkf_state = NULL;
+
+#define STD_GC_BUFSIZE (256)
+
+static void
+nkf_state_init(void)
+{
+    if (nkf_state) {
+       nkf_buf_clear(nkf_state->std_gc_buf);
+       nkf_buf_clear(nkf_state->broken_buf);
+    }
+    else {
+       nkf_state = nkf_xmalloc(sizeof(nkf_state_t));
+       nkf_state->std_gc_buf = nkf_buf_new(STD_GC_BUFSIZE);
+       nkf_state->broken_buf = nkf_buf_new(3);
+    }
+    nkf_state->broken_state = 0;
+    nkf_state->mimeout_state = 0;
+}
+
 #ifndef WIN32DLL
 static nkf_char
 std_getc(FILE *f)
 {
-    if (std_gc_ndx){
-       return std_gc_buf[--std_gc_ndx];
+    if (!nkf_buf_empty_p(nkf_state->std_gc_buf)){
+       return nkf_buf_pop(nkf_state->std_gc_buf);
     }
     return getc(f);
 }
@@ -2963,10 +3036,7 @@ std_getc(FILE *f)
 static nkf_char
 std_ungetc(nkf_char c, FILE *f)
 {
-    if (std_gc_ndx == STD_GC_BUFSIZE){
-       return EOF;
-    }
-    std_gc_buf[std_gc_ndx++] = c;
+    nkf_buf_push(nkf_state->std_gc_buf, c);
     return c;
 }
 
@@ -3214,65 +3284,41 @@ check_bom(FILE *f)
     }
 }
 
-static struct {
-    int count;
-    nkf_char status;
-    nkf_char buf[3];
-} broken_state;
-
-static void
-init_broken_state(void)
-{
-    memset(&broken_state, 0, sizeof(broken_state));
-}
-
-static void
-push_broken_buf(c)
-{
-    broken_state.buf[broken_state.count++] = c;
-}
-
-static nkf_char
-pop_broken_buf(void)
-{
-    return broken_state.buf[--broken_state.count];
-}
-
 static nkf_char
 broken_getc(FILE *f)
 {
     nkf_char c, c1;
 
-    if (broken_state.count > 0) {
-       return pop_broken_buf();
+    if (!nkf_buf_empty_p(nkf_state->broken_buf)) {
+       return nkf_buf_pop(nkf_state->broken_buf);
     }
     c = (*i_bgetc)(f);
-    if (c=='$' && broken_state.status != ESC
+    if (c=='$' && nkf_state->broken_state != ESC
        && (input_mode == ASCII || input_mode == JIS_X_0201_1976_K)) {
        c1= (*i_bgetc)(f);
-       broken_state.status = 0;
+       nkf_state->broken_state = 0;
        if (c1=='@'|| c1=='B') {
-           push_broken_buf(c1);
-           push_broken_buf(c);
+           nkf_buf_push(nkf_state->broken_buf, c1);
+           nkf_buf_push(nkf_state->broken_buf, c);
            return ESC;
        } else {
            (*i_bungetc)(c1,f);
            return c;
        }
-    } else if (c=='(' && broken_state.status != ESC
+    } else if (c=='(' && nkf_state->broken_state != ESC
               && (input_mode == JIS_X_0208 || input_mode == JIS_X_0201_1976_K)) {
        c1= (*i_bgetc)(f);
-       broken_state.status = 0;
+       nkf_state->broken_state = 0;
        if (c1=='J'|| c1=='B') {
-           push_broken_buf(c1);
-           push_broken_buf(c);
+           nkf_buf_push(nkf_state->broken_buf, c1);
+           nkf_buf_push(nkf_state->broken_buf, c);
            return ESC;
        } else {
            (*i_bungetc)(c1,f);
            return c;
        }
     } else {
-       broken_state.status = c;
+       nkf_state->broken_state = c;
        return c;
     }
 }
@@ -3280,8 +3326,8 @@ broken_getc(FILE *f)
 static nkf_char
 broken_ungetc(nkf_char c, FILE *f)
 {
-    if (broken_state.count < 2)
-       push_broken_buf(c);
+    if (nkf_buf_length(nkf_state->broken_buf) < 2)
+       nkf_buf_push(nkf_state->broken_buf, c);
     return c;
 }
 
@@ -3382,9 +3428,7 @@ fold_conv(nkf_char c2, nkf_char c1)
        f_prev = LF;
        f_line = 0;
        fold_state =  LF;            /* output newline and clear */
-    } else if ( (c2==0  && c1==SP)||
-              (c2==0  && c1==TAB)||
-              (c2=='!'&& c1=='!')) {
+    } else if ((c2==0 && nkf_isblank(c1)) || (c2 == '!' && c1 == '!')) {
        /* X0208 kankaku or ascii space */
        if (f_prev == SP) {
            fold_state = 0;         /* remove duplicate spaces */
@@ -3582,7 +3626,7 @@ z_conv(nkf_char c2, nkf_char c1)
     if (alpha_f & 16) {
        /* JIS X 0208 Katakana to JIS X 0201 Katakana */
        if (c2 == 0x21) {
-           char c = 0;
+           nkf_char c = 0;
            switch (c1) {
            case 0x23:
                /* U+3002 (0x8142) Ideographic Full Stop -> U+FF61 (0xA1) Halfwidth Ideographic Full Stop */
@@ -4222,91 +4266,36 @@ numchar_ungetc(nkf_char c, FILE *f)
 
 #ifdef UNICODE_NORMALIZATION
 
-typedef struct {
-    unsigned char *ary;
-    int max_length;
-    int count;
-} nkf_ary;
-
-static nkf_ary *
-nkf_ary_new(int length)
-{
-    nkf_ary *ary = nkf_malloc(sizeof(nkf_ary));
-    ary->ary = nkf_malloc(length);
-    ary->max_length = length;
-    ary->count = 0;
-    return ary;
-} 
-
-static void
-nkf_ary_dispose(nkf_ary *ary)
-{
-    nkf_free(ary->ary);
-    nkf_free(ary);
-}
-
-#define nkf_ary_length(ary) ((ary)->count)
-#define nkf_ary_empty_p(ary) ((ary)->count == 0)
-
-static unsigned char
-nkf_ary_at(nkf_ary *ary, int index)
-{
-    assert(index <= ary->count);
-    return ary->ary[index];
-}
-
-static void
-nkf_ary_clear(nkf_ary *ary)
-{
-    ary->count = 0;
-}
-
-static unsigned char
-nkf_ary_push(nkf_ary *ary, nkf_char c)
-{
-    assert(ary->max_length > ary->count);
-    ary->ary[ary->count++] = c;
-    return ary->count;
-}
-
-static unsigned char
-nkf_ary_pop(nkf_ary *ary)
-{
-    assert(0 < ary->count);
-    return ary->ary[--ary->count];
-}
-
-/* Normalization Form C */
 static nkf_char
 nfc_getc(FILE *f)
 {
     nkf_char (*g)(FILE *f) = i_nfc_getc;
     nkf_char (*u)(nkf_char c ,FILE *f) = i_nfc_ungetc;
-    nkf_ary *buf = nkf_ary_new(9);
+    nkf_buf_t *buf = nkf_buf_new(9);
     const unsigned char *array;
     int lower=0, upper=NORMALIZATION_TABLE_LENGTH-1;
     nkf_char c = (*g)(f);
 
     if (c == EOF || c > 0xFF || (c & 0xc0) == 0x80) return c;
 
-    nkf_ary_push(buf, (unsigned char)c);
+    nkf_buf_push(buf, (unsigned char)c);
     do {
        while (lower <= upper) {
            int mid = (lower+upper) / 2;
            int len;
            array = normalization_table[mid].nfd;
            for (len=0; len < NORMALIZATION_TABLE_NFD_LENGTH && array[len]; len++) {
-               if (len >= nkf_ary_length(buf)) {
+               if (len >= nkf_buf_length(buf)) {
                    c = (*g)(f);
                    if (c == EOF) {
                        len = 0;
                        lower = 1, upper = 0;
                        break;
                    }
-                   nkf_ary_push(buf, c);
+                   nkf_buf_push(buf, c);
                }
-               if (array[len] != nkf_ary_at(buf, len)) {
-                   if (array[len] < nkf_ary_at(buf, len)) lower = mid + 1;
+               if (array[len] != nkf_buf_at(buf, len)) {
+                   if (array[len] < nkf_buf_at(buf, len)) lower = mid + 1;
                    else  upper = mid - 1;
                    len = 0;
                    break;
@@ -4315,17 +4304,17 @@ nfc_getc(FILE *f)
            if (len > 0) {
                int i;
                array = normalization_table[mid].nfc;
-               nkf_ary_clear(buf);
+               nkf_buf_clear(buf);
                for (i=0; i < NORMALIZATION_TABLE_NFC_LENGTH && array[i]; i++)
-                   nkf_ary_push(buf, array[i]);
+                   nkf_buf_push(buf, array[i]);
                break;
            }
        }
     } while (lower <= upper);
 
-    while (nkf_ary_length(buf) > 1) (*u)(nkf_ary_pop(buf), f);
-    c = nkf_ary_pop(buf);
-    nkf_ary_dispose(buf);
+    while (nkf_buf_length(buf) > 1) (*u)(nkf_buf_pop(buf), f);
+    c = nkf_buf_pop(buf);
+    nkf_buf_dispose(buf);
 
     return c;
 }
@@ -4401,13 +4390,13 @@ mime_getc(FILE *f)
            /* end Q encoding */
            input_mode = exit_mode;
            lwsp_count = 0;
-           lwsp_buf = nkf_malloc((lwsp_size+5)*sizeof(char));
+           lwsp_buf = nkf_xmalloc((lwsp_size+5)*sizeof(char));
            while ((c1=(*i_getc)(f))!=EOF) {
                switch (c1) {
                case LF:
                case CR:
                    if (c1==LF) {
-                       if ((c1=(*i_getc)(f))!=EOF && (c1==SP||c1==TAB)) {
+                       if ((c1=(*i_getc)(f))!=EOF && nkf_isblank(c1)) {
                            i_ungetc(SP,f);
                            continue;
                        } else {
@@ -4416,7 +4405,7 @@ mime_getc(FILE *f)
                        c1 = LF;
                    } else {
                        if ((c1=(*i_getc)(f))!=EOF && c1 == LF) {
-                           if ((c1=(*i_getc)(f))!=EOF && (c1==SP||c1==TAB)) {
+                           if ((c1=(*i_getc)(f))!=EOF && nkf_isblank(c1)) {
                                i_ungetc(SP,f);
                                continue;
                            } else {
@@ -4434,7 +4423,7 @@ mime_getc(FILE *f)
                    lwsp_buf[lwsp_count] = (unsigned char)c1;
                    if (lwsp_count++>lwsp_size){
                        lwsp_size <<= 1;
-                       lwsp_buf_new = nkf_realloc(lwsp_buf, (lwsp_size+5)*sizeof(char));
+                       lwsp_buf_new = nkf_xrealloc(lwsp_buf, (lwsp_size+5)*sizeof(char));
                        lwsp_buf = lwsp_buf_new;
                    }
                    continue;
@@ -4447,7 +4436,7 @@ mime_getc(FILE *f)
                    i_ungetc(lwsp_buf[lwsp_count],f);
                c1 = lwsp_buf[0];
            }
-           nkf_free(lwsp_buf);
+           nkf_xfree(lwsp_buf);
            return c1;
        }
        if (c1=='='&&c2<SP) { /* this is soft wrap */
@@ -4500,13 +4489,13 @@ mime_getc(FILE *f)
     if ((c1 == '?') && (c2 == '=')) {
        input_mode = ASCII;
        lwsp_count = 0;
-       lwsp_buf = nkf_malloc((lwsp_size+5)*sizeof(char));
+       lwsp_buf = nkf_xmalloc((lwsp_size+5)*sizeof(char));
        while ((c1=(*i_getc)(f))!=EOF) {
            switch (c1) {
            case LF:
            case CR:
                if (c1==LF) {
-                   if ((c1=(*i_getc)(f))!=EOF && (c1==SP||c1==TAB)) {
+                   if ((c1=(*i_getc)(f))!=EOF && nkf_isblank(c1)) {
                        i_ungetc(SP,f);
                        continue;
                    } else {
@@ -4518,7 +4507,7 @@ mime_getc(FILE *f)
                        if (c1==SP) {
                            i_ungetc(SP,f);
                            continue;
-                       } else if ((c1=(*i_getc)(f))!=EOF && (c1==SP||c1==TAB)) {
+                       } else if ((c1=(*i_getc)(f))!=EOF && nkf_isblank(c1)) {
                            i_ungetc(SP,f);
                            continue;
                        } else {
@@ -4536,7 +4525,7 @@ mime_getc(FILE *f)
                lwsp_buf[lwsp_count] = (unsigned char)c1;
                if (lwsp_count++>lwsp_size){
                    lwsp_size <<= 1;
-                   lwsp_buf_new = nkf_realloc(lwsp_buf, (lwsp_size+5)*sizeof(char));
+                   lwsp_buf_new = nkf_xrealloc(lwsp_buf, (lwsp_size+5)*sizeof(char));
                    lwsp_buf = lwsp_buf_new;
                }
                continue;
@@ -4549,7 +4538,7 @@ mime_getc(FILE *f)
                i_ungetc(lwsp_buf[lwsp_count],f);
            c1 = lwsp_buf[0];
        }
-       nkf_free(lwsp_buf);
+       nkf_xfree(lwsp_buf);
        return c1;
     }
   mime_c3_retry:
@@ -4600,7 +4589,6 @@ static const char basis_64[] =
 static struct {
     char buf[MIMEOUT_BUF_LENGTH+1];
     int count;
-    nkf_char state;
 } mimeout_state;
 
 /*nkf_char mime_lastchar2, mime_lastchar1;*/
@@ -4628,15 +4616,12 @@ open_mime(nkf_char mode)
        PUT_NEWLINE((*o_mputc));
        (*o_mputc)(SP);
        base64_count = 1;
-       if (mimeout_state.count>0
-           && (mimeout_state.buf[i]==SP || mimeout_state.buf[i]==TAB
-               || mimeout_state.buf[i]==CR || mimeout_state.buf[i]==LF)) {
+       if (mimeout_state.count>0 && nkf_isspace(mimeout_state.buf[i])) {
            i++;
        }
     }
     for (;i<mimeout_state.count;i++) {
-       if (mimeout_state.buf[i]==SP || mimeout_state.buf[i]==TAB
-           || mimeout_state.buf[i]==CR || mimeout_state.buf[i]==LF) {
+       if (nkf_isspace(mimeout_state.buf[i])) {
            (*o_mputc)(mimeout_state.buf[i]);
            base64_count ++;
        } else {
@@ -4704,13 +4689,13 @@ eof_mime(void)
     case 'B':
        break;
     case 2:
-       (*o_mputc)(basis_64[((mimeout_state.state & 0x3)<< 4)]);
+       (*o_mputc)(basis_64[((nkf_state->mimeout_state & 0x3)<< 4)]);
        (*o_mputc)('=');
        (*o_mputc)('=');
        base64_count += 3;
        break;
     case 1:
-       (*o_mputc)(basis_64[((mimeout_state.state & 0xF) << 2)]);
+       (*o_mputc)(basis_64[((nkf_state->mimeout_state & 0xF) << 2)]);
        (*o_mputc)('=');
        base64_count += 2;
        break;
@@ -4742,19 +4727,19 @@ mimeout_addchar(nkf_char c)
        }
        break;
     case 'B':
-       mimeout_state.state=c;
+       nkf_state->mimeout_state=c;
        (*o_mputc)(basis_64[c>>2]);
        mimeout_mode=2;
        base64_count ++;
        break;
     case 2:
-       (*o_mputc)(basis_64[((mimeout_state.state & 0x3)<< 4) | ((c & 0xF0) >> 4)]);
-       mimeout_state.state=c;
+       (*o_mputc)(basis_64[((nkf_state->mimeout_state & 0x3)<< 4) | ((c & 0xF0) >> 4)]);
+       nkf_state->mimeout_state=c;
        mimeout_mode=1;
        base64_count ++;
        break;
     case 1:
-       (*o_mputc)(basis_64[((mimeout_state.state & 0xF) << 2) | ((c & 0xC0) >>6)]);
+       (*o_mputc)(basis_64[((nkf_state->mimeout_state & 0xF) << 2) | ((c & 0xC0) >>6)]);
        (*o_mputc)(basis_64[c & 0x3F]);
        mimeout_mode='B';
        base64_count += 2;
@@ -4982,7 +4967,7 @@ mime_putc(nkf_char c)
                mimeout_state.buf[mimeout_state.count++] = (char)c;
                return;
            }
-           if (c==SP || c==TAB || c==CR || c==LF) {
+           if (nkf_isspace(c)) {
                for (i=0;i<mimeout_state.count;i++) {
                    if (SP<mimeout_state.buf[i] && mimeout_state.buf[i]<DEL) {
                        eof_mime();
@@ -5059,9 +5044,9 @@ nkf_iconv_new(char *tocode, char *fromcode)
     nkf_iconv_t converter;
 
     converter->input_buffer_size = IOBUF_SIZE;
-    converter->input_buffer = nkf_malloc(converter->input_buffer_size);
+    converter->input_buffer = nkf_xmalloc(converter->input_buffer_size);
     converter->output_buffer_size = IOBUF_SIZE * 2;
-    converter->output_buffer = nkf_malloc(converter->output_buffer_size);
+    converter->output_buffer = nkf_xmalloc(converter->output_buffer_size);
     converter->cd = iconv_open(tocode, fromcode);
     if (converter->cd == (iconv_t)-1)
     {
@@ -5128,8 +5113,8 @@ nkf_iconv_convert(nkf_iconv_t *converter, FILE *input)
 static void
 nkf_iconv_close(nkf_iconv_t *convert)
 {
-    nkf_free(converter->inbuf);
-    nkf_free(converter->outbuf);
+    nkf_xfree(converter->inbuf);
+    nkf_xfree(converter->outbuf);
     iconv_close(converter->cd);
 }
 #endif
@@ -5240,7 +5225,6 @@ reinit(void)
     input_eol = 0;
     prev_cr = 0;
     option_mode = 0;
-    init_broken_state();
     z_prev2=0,z_prev1=0;
 #ifdef CHECK_OPTION
     iconv_for_check = 0;
@@ -5248,6 +5232,7 @@ reinit(void)
     input_codename = NULL;
     input_encoding = NULL;
     output_encoding = NULL;
+    nkf_state_init();
 #ifdef WIN32DLL
     reinitdll();
 #endif /*WIN32DLL*/
@@ -6401,6 +6386,8 @@ main(int argc, char **argv)
 #ifdef DEFAULT_CODE_LOCALE
     setlocale(LC_CTYPE, "");
 #endif
+    nkf_state_init();
+
     for (argc--,argv++; (argc > 0) && **argv == '-'; argc--, argv++) {
        cp = (unsigned char *)*argv;
        options(cp);
@@ -6508,7 +6495,7 @@ main(int argc, char **argv)
                if (file_out_f == TRUE) {
 #ifdef OVERWRITE
                    if (overwrite_f){
-                       outfname = nkf_malloc(strlen(origfname)
+                       outfname = nkf_xmalloc(strlen(origfname)
                                          + strlen(".nkftmpXXXXXX")
                                          + 1);
                        strcpy(outfname, origfname);
@@ -6624,7 +6611,7 @@ main(int argc, char **argv)
                            fprintf(stderr, "Can't rename %s to %s\n",
                                    origfname, backup_filename);
                        }
-                       nkf_free(backup_filename);
+                       nkf_xfree(backup_filename);
                    }else{
 #ifdef MSDOS
                        if (unlink(origfname)){
@@ -6637,7 +6624,7 @@ main(int argc, char **argv)
                        fprintf(stderr, "Can't rename %s to %s\n",
                                outfname, origfname);
                    }
-                   nkf_free(outfname);
+                   nkf_xfree(outfname);
                }
 #endif
            }