OSDN Git Service

Ignore generated files.
[nkf/nkf.git] / nkf.c
diff --git a/nkf.c b/nkf.c
index ce68917..feaf57c 100644 (file)
--- a/nkf.c
+++ b/nkf.c
@@ -31,9 +31,9 @@
  * \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.189 2008/11/09 18:28:52 naruse Exp $"
+#define NKF_IDENT "$Id: nkf.c,v 1.192 2008/11/09 23:09:22 naruse Exp $"
 #define NKF_VERSION "2.0.8"
-#define NKF_RELEASE_DATE "2008-11-10"
+#define NKF_RELEASE_DATE "2008-12-25"
 #define COPY_RIGHT \
     "Copyright (C) 1987, FUJITSU LTD. (I.Ichikawa),2000 S. Kono, COW\n" \
     "Copyright (C) 2002-2008 Kono, Furukawa, Naruse, mastodon"
 #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
 
    c2           0 means ASCII
@@ -664,6 +668,38 @@ static int             end_check;
 nkf_char std_gc_buf[STD_GC_BUFSIZE];
 nkf_char std_gc_ndx;
 
+static void *
+nkf_malloc(size_t size)
+{
+    void *ptr;
+
+    if (size == 0) size = 1;
+
+    ptr = malloc(size);
+    if (ptr == NULL) {
+       perror("can't malloc");
+       exit(EXIT_FAILURE);
+    }
+
+    return ptr;
+}
+
+static void *
+nkf_realloc(void *ptr, size_t size)
+{
+    if (size == 0) size = 1;
+
+    ptr = realloc(ptr, size);
+    if (ptr == NULL) {
+       perror("can't realloc");
+       exit(EXIT_FAILURE);
+    }
+
+    return ptr;
+}
+
+#define nkf_free(ptr) free(ptr)
+
 static int
 nkf_str_caseeql(const char *src, const char *target)
 {
@@ -724,22 +760,15 @@ nkf_enc_find(const char *name)
                                nkf_enc_to_index(enc) == CP50222)
 
 #ifdef DEFAULT_CODE_LOCALE
-static char*
+static const char*
 nkf_locale_charmap()
 {
 #ifdef HAVE_LANGINFO_H
     return nl_langinfo(CODESET);
 #elif defined(__WIN32__)
-    char buf[16];
-    char *str;
-    int len = sprintf(buf, "CP%d", GetACP());
-    if (len > 0) {
-      str = malloc(len + 1);
-      strcpy(str, buf);
-      str[len] = '\0';
-      return str;
-    }
-    else return NULL;
+    static char buf[16];
+    sprintf(buf, "CP%d", GetACP());
+    return buf;
 #elif defined(__OS2__)
 # if defined(INT_IS_SHORT)
     /* OS/2 1.x */
@@ -755,16 +784,15 @@ nkf_locale_charmap()
         sprintf(buf, "CP%lu", ulCP[0]);
     return buf;
 # endif
-#else
-    return NULL;
 #endif
+    return NULL;
 }
 
 static nkf_encoding*
 nkf_locale_encoding()
 {
     nkf_encoding *enc = 0;
-    char *encname = nkf_locale_charmap();
+    const char *encname = nkf_locale_charmap();
     if (encname)
        enc = nkf_enc_find(encname);
     return enc;
@@ -938,12 +966,7 @@ get_backup_filename(const char *suffix, const char *filename)
     }
 
     if(asterisk_count){
-       backup_filename = malloc(strlen(suffix) + (asterisk_count * (filename_length - 1)) + 1);
-       if (!backup_filename){
-           perror("Can't malloc backup filename.");
-           return NULL;
-       }
-
+       backup_filename = nkf_malloc(strlen(suffix) + (asterisk_count * (filename_length - 1)) + 1);
        for(i = 0, j = 0; suffix[i];){
            if(suffix[i] == '*'){
                backup_filename[j] = '\0';
@@ -957,7 +980,7 @@ get_backup_filename(const char *suffix, const char *filename)
        backup_filename[j] = '\0';
     }else{
        j = filename_length + strlen(suffix);
-       backup_filename = malloc(j + 1);
+       backup_filename = nkf_malloc(j + 1);
        strcpy(backup_filename, filename);
        strcat(backup_filename, suffix);
        backup_filename[j] = '\0';
@@ -4200,50 +4223,57 @@ numchar_ungetc(nkf_char c, FILE *f)
 #ifdef UNICODE_NORMALIZATION
 
 typedef struct {
-    unsigned char *buf;
+    unsigned char *ary;
     int max_length;
     int count;
-} nkf_buffer;
+} nkf_ary;
 
-static nkf_buffer *
-nkf_buf_new(int length)
+static nkf_ary *
+nkf_ary_new(int length)
 {
-    nkf_buffer *buf = malloc(sizeof(nkf_buffer));
-    buf->buf = malloc(length);
-    buf->max_length = length;
-    buf->count = 0;
-    return buf;
+    nkf_ary *ary = nkf_malloc(sizeof(nkf_ary));
+    ary->ary = nkf_malloc(length);
+    ary->max_length = length;
+    ary->count = 0;
+    return ary;
 } 
 
-#define nkf_buf_count(buf) ((buf)->count)
-#define nkf_buf_empty_p(buf) ((buf)->count == 0)
+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_buf_push(nkf_buffer *buf, nkf_char c)
+nkf_ary_at(nkf_ary *ary, int index)
 {
-    assert(buf->max_length > buf->count);
-    buf->buf[buf->count++] = c;
-    return buf->count;
+    assert(index <= ary->count);
+    return ary->ary[index];
 }
 
-static unsigned char
-nkf_buf_pop(nkf_buffer *buf)
+static void
+nkf_ary_clear(nkf_ary *ary)
 {
-    assert(0 < buf->count);
-    return buf->buf[--buf->count];
+    ary->count = 0;
 }
 
 static unsigned char
-nkf_buf_at(nkf_buffer *buf, int index)
+nkf_ary_push(nkf_ary *ary, nkf_char c)
 {
-    assert(index <= buf->count);
-    return buf->buf[index];
+    assert(ary->max_length > ary->count);
+    ary->ary[ary->count++] = c;
+    return ary->count;
 }
 
-static void
-nkf_buf_clear(nkf_buffer *buf)
+static unsigned char
+nkf_ary_pop(nkf_ary *ary)
 {
-    buf->count = 0;
+    assert(0 < ary->count);
+    return ary->ary[--ary->count];
 }
 
 /* Normalization Form C */
@@ -4252,49 +4282,52 @@ nfc_getc(FILE *f)
 {
     nkf_char (*g)(FILE *f) = i_nfc_getc;
     nkf_char (*u)(nkf_char c ,FILE *f) = i_nfc_ungetc;
-    nkf_buffer *buf = nkf_buf_new(9);
+    nkf_ary *buf = nkf_ary_new(9);
     const unsigned char *array;
     int lower=0, upper=NORMALIZATION_TABLE_LENGTH-1;
-    int c = (*g)(f);
+    nkf_char c = (*g)(f);
 
-    if (c == EOF || c > 0xFF || (c & 0xc0) != 0x80) return c;
+    if (c == EOF || c > 0xFF || (c & 0xc0) == 0x80) return c;
 
-    nkf_buf_push(buf, (unsigned char)c);
+    nkf_ary_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 (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;
-               } else if (len >= nkf_buf_count(buf)) {
+               if (len >= nkf_ary_length(buf)) {
                    c = (*g)(f);
                    if (c == EOF) {
                        len = 0;
                        lower = 1, upper = 0;
                        break;
                    }
-                   nkf_buf_push(buf, c);
+                   nkf_ary_push(buf, c);
+               }
+               if (array[len] != nkf_ary_at(buf, len)) {
+                   if (array[len] < nkf_ary_at(buf, len)) lower = mid + 1;
+                   else  upper = mid - 1;
+                   len = 0;
+                   break;
                }
            }
            if (len > 0) {
                int i;
                array = normalization_table[mid].nfc;
-               nkf_buf_clear(buf);
+               nkf_ary_clear(buf);
                for (i=0; i < NORMALIZATION_TABLE_NFC_LENGTH && array[i]; i++)
-                   nkf_buf_push(buf, array[i]);
+                   nkf_ary_push(buf, array[i]);
                break;
            }
        }
     } while (lower <= upper);
 
-    while (nkf_buf_count(buf) > 1) (*u)(nkf_buf_pop(buf), f);
+    while (nkf_ary_length(buf) > 1) (*u)(nkf_ary_pop(buf), f);
+    c = nkf_ary_pop(buf);
+    nkf_ary_dispose(buf);
 
-    return nkf_buf_pop(buf);
+    return c;
 }
 
 static nkf_char
@@ -4368,11 +4401,7 @@ mime_getc(FILE *f)
            /* end Q encoding */
            input_mode = exit_mode;
            lwsp_count = 0;
-           lwsp_buf = malloc((lwsp_size+5)*sizeof(char));
-           if (lwsp_buf==NULL) {
-               perror("can't malloc");
-               return -1;
-           }
+           lwsp_buf = nkf_malloc((lwsp_size+5)*sizeof(char));
            while ((c1=(*i_getc)(f))!=EOF) {
                switch (c1) {
                case LF:
@@ -4405,12 +4434,7 @@ mime_getc(FILE *f)
                    lwsp_buf[lwsp_count] = (unsigned char)c1;
                    if (lwsp_count++>lwsp_size){
                        lwsp_size <<= 1;
-                       lwsp_buf_new = realloc(lwsp_buf, (lwsp_size+5)*sizeof(char));
-                       if (lwsp_buf_new==NULL) {
-                           free(lwsp_buf);
-                           perror("can't realloc");
-                           return -1;
-                       }
+                       lwsp_buf_new = nkf_realloc(lwsp_buf, (lwsp_size+5)*sizeof(char));
                        lwsp_buf = lwsp_buf_new;
                    }
                    continue;
@@ -4423,7 +4447,7 @@ mime_getc(FILE *f)
                    i_ungetc(lwsp_buf[lwsp_count],f);
                c1 = lwsp_buf[0];
            }
-           free(lwsp_buf);
+           nkf_free(lwsp_buf);
            return c1;
        }
        if (c1=='='&&c2<SP) { /* this is soft wrap */
@@ -4476,11 +4500,7 @@ mime_getc(FILE *f)
     if ((c1 == '?') && (c2 == '=')) {
        input_mode = ASCII;
        lwsp_count = 0;
-       lwsp_buf = malloc((lwsp_size+5)*sizeof(char));
-       if (lwsp_buf==NULL) {
-           perror("can't malloc");
-           return -1;
-       }
+       lwsp_buf = nkf_malloc((lwsp_size+5)*sizeof(char));
        while ((c1=(*i_getc)(f))!=EOF) {
            switch (c1) {
            case LF:
@@ -4516,12 +4536,7 @@ mime_getc(FILE *f)
                lwsp_buf[lwsp_count] = (unsigned char)c1;
                if (lwsp_count++>lwsp_size){
                    lwsp_size <<= 1;
-                   lwsp_buf_new = realloc(lwsp_buf, (lwsp_size+5)*sizeof(char));
-                   if (lwsp_buf_new==NULL) {
-                       free(lwsp_buf);
-                       perror("can't realloc");
-                       return -1;
-                   }
+                   lwsp_buf_new = nkf_realloc(lwsp_buf, (lwsp_size+5)*sizeof(char));
                    lwsp_buf = lwsp_buf_new;
                }
                continue;
@@ -4534,7 +4549,7 @@ mime_getc(FILE *f)
                i_ungetc(lwsp_buf[lwsp_count],f);
            c1 = lwsp_buf[0];
        }
-       free(lwsp_buf);
+       nkf_free(lwsp_buf);
        return c1;
     }
   mime_c3_retry:
@@ -4581,7 +4596,7 @@ mime_getc(FILE *f)
 static const char basis_64[] =
     "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
 
-#define MIMEOUT_BUF_LENGTH (60)
+#define MIMEOUT_BUF_LENGTH 74
 static struct {
     char buf[MIMEOUT_BUF_LENGTH+1];
     int count;
@@ -4891,11 +4906,36 @@ mime_putc(nkf_char c)
                if (base64_count > 1
                    && base64_count + mimeout_state.count > 76
                    && mimeout_state.buf[0] != CR && mimeout_state.buf[0] != LF){
-                   PUT_NEWLINE((*o_mputc));
-                   base64_count = 0;
-                   if (!nkf_isspace(mimeout_state.buf[0])){
-                       (*o_mputc)(SP);
-                       base64_count++;
+                   static const char *str = "boundary=\"";
+                   static int len = 10;
+                   i = 0;
+
+                   for (; i < mimeout_state.count - len; ++i) {
+                       if (!strncmp(mimeout_state.buf+i, str, len)) {
+                           i += len - 2;
+                           break;
+                       }
+                   }
+
+                   if (i == 0 || i == mimeout_state.count - len) {
+                       PUT_NEWLINE((*o_mputc));
+                       base64_count = 0;
+                       if (!nkf_isspace(mimeout_state.buf[0])){
+                           (*o_mputc)(SP);
+                           base64_count++;
+                       }
+                   }
+                   else {
+                       int j;
+                       for (j = 0; j <= i; ++j) {
+                           (*o_mputc)(mimeout_state.buf[j]);
+                       }
+                       PUT_NEWLINE((*o_mputc));
+                       base64_count = 1;
+                       for (; j <= mimeout_state.count; ++j) {
+                           mimeout_state.buf[j - i] = mimeout_state.buf[j];
+                       }
+                       mimeout_state.count -= i;
                    }
                }
                mimeout_state.buf[mimeout_state.count++] = (char)c;
@@ -5019,15 +5059,9 @@ nkf_iconv_new(char *tocode, char *fromcode)
     nkf_iconv_t converter;
 
     converter->input_buffer_size = IOBUF_SIZE;
-    converter->input_buffer = malloc(converter->input_buffer_size);
-    if (converter->input_buffer == NULL)
-       perror("can't malloc");
-
+    converter->input_buffer = nkf_malloc(converter->input_buffer_size);
     converter->output_buffer_size = IOBUF_SIZE * 2;
-    converter->output_buffer = malloc(converter->output_buffer_size);
-    if (converter->output_buffer == NULL)
-       perror("can't malloc");
-
+    converter->output_buffer = nkf_malloc(converter->output_buffer_size);
     converter->cd = iconv_open(tocode, fromcode);
     if (converter->cd == (iconv_t)-1)
     {
@@ -5094,8 +5128,8 @@ nkf_iconv_convert(nkf_iconv_t *converter, FILE *input)
 static void
 nkf_iconv_close(nkf_iconv_t *convert)
 {
-    free(converter->inbuf);
-    free(converter->outbuf);
+    nkf_free(converter->inbuf);
+    nkf_free(converter->outbuf);
     iconv_close(converter->cd);
 }
 #endif
@@ -5872,8 +5906,7 @@ options(unsigned char *cp)
                    overwrite_f = TRUE;
                    preserve_time_f = TRUE;
                    backup_f = TRUE;
-                   backup_suffix = malloc(strlen((char *) p) + 1);
-                   strcpy(backup_suffix, (char *) p);
+                   backup_suffix = (char *)p;
                    continue;
                }
                if (strcmp(long_option[i].name, "in-place") == 0){
@@ -5887,8 +5920,7 @@ options(unsigned char *cp)
                    overwrite_f = TRUE;
                    preserve_time_f = FALSE;
                    backup_f = TRUE;
-                   backup_suffix = malloc(strlen((char *) p) + 1);
-                   strcpy(backup_suffix, (char *) p);
+                   backup_suffix = (char *)p;
                    continue;
                }
 #endif
@@ -6476,13 +6508,9 @@ main(int argc, char **argv)
                if (file_out_f == TRUE) {
 #ifdef OVERWRITE
                    if (overwrite_f){
-                       outfname = malloc(strlen(origfname)
+                       outfname = nkf_malloc(strlen(origfname)
                                          + strlen(".nkftmpXXXXXX")
                                          + 1);
-                       if (!outfname){
-                           perror(origfname);
-                           return -1;
-                       }
                        strcpy(outfname, origfname);
 #ifdef MSDOS
                        {
@@ -6596,7 +6624,7 @@ main(int argc, char **argv)
                            fprintf(stderr, "Can't rename %s to %s\n",
                                    origfname, backup_filename);
                        }
-                       free(backup_filename);
+                       nkf_free(backup_filename);
                    }else{
 #ifdef MSDOS
                        if (unlink(origfname)){
@@ -6609,7 +6637,7 @@ main(int argc, char **argv)
                        fprintf(stderr, "Can't rename %s to %s\n",
                                outfname, origfname);
                    }
-                   free(outfname);
+                   nkf_free(outfname);
                }
 #endif
            }