~ubuntu-branches/ubuntu/quantal/ruby1.9.1/quantal

« back to all changes in this revision

Viewing changes to ext/nkf/nkf-utf8/nkf.c

  • Committer: Bazaar Package Importer
  • Author(s): Lucas Nussbaum
  • Date: 2010-07-31 17:08:39 UTC
  • mfrom: (1.1.4 upstream) (8.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20100731170839-j034dmpdqt1cc4p6
Tags: 1.9.2~svn28788-1
* New release based on upstream snapshot from the 1.9.2 branch,
  after 1.9.2 RC2. That branch is (supposed to be) binary-compatible
  with the 1.9.1 branch.
  + Builds fine on i386. Closes: #580852.
* Upgrade to Standards-Version: 3.9.1. No changes needed.
* Updated generated incs.
* Patches that still need work:
  + Unclear status, need more investigation:
   090729_fix_Makefile_deps.dpatch
   090803_exclude_rdoc.dpatch
   203_adjust_base_of_search_path.dpatch
   902_define_YAML_in_yaml_stringio.rb.dpatch
   919_common.mk_tweaks.dpatch
   931_libruby_suffix.dpatch
   940_test_thread_mutex_sync_shorter.dpatch
  + Maybe not needed anymore, keeping but not applying.
   102_skip_test_copy_stream.dpatch (test doesn't block anymore?)
   104_skip_btest_io.dpatch (test doesn't block anymore?)
   201_gem_prelude.dpatch (we don't use that rubygems anyway?)
   202_gem_default_dir.dpatch (we don't use that rubygems anyway?)
   940_test_file_exhaustive_fails_as_root.dpatch
   940_test_priority_fails.dpatch
   100518_load_libc_libm.dpatch
* Add disable-tests.diff: disable some tests that cause failures on FreeBSD.
  Closes: #590002, #543805, #542927.
* However, many new failures on FreeBSD. Since that version is still an
  improvement, add the check that makes test suite failures non-fatal on
  FreeBSD again. That still needs to be investigated.
* Re-add 903_skip_base_ruby_check.dpatch
* Add build-dependency on ruby1.8 and drop all pre-generated files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
 * Copyright (c) 1987, Fujitsu LTD. (Itaru ICHIKAWA).
3
 
 * Copyright (c) 1996-2009, The nkf Project.
 
3
 * Copyright (c) 1996-2010, The nkf Project.
4
4
 *
5
5
 * This software is provided 'as-is', without any express or implied
6
6
 * warranty. In no event will the authors be held liable for any damages
20
20
 *
21
21
 * 3. This notice may not be removed or altered from any source distribution.
22
22
 */
23
 
#define NKF_VERSION "2.0.9"
24
 
#define NKF_RELEASE_DATE "2009-01-20"
 
23
#define NKF_VERSION "2.1.1"
 
24
#define NKF_RELEASE_DATE "2010-04-28"
25
25
#define COPY_RIGHT \
26
26
    "Copyright (C) 1987, FUJITSU LTD. (I.Ichikawa).\n" \
27
 
    "Copyright (C) 1996-2009, The nkf Project."
 
27
    "Copyright (C) 1996-2010, The nkf Project."
28
28
 
29
29
#include "config.h"
30
30
#include "nkf.h"
210
210
} encoding_name_to_id_table[] = {
211
211
    {"US-ASCII",                ASCII},
212
212
    {"ASCII",                   ASCII},
 
213
    {"646",                     ASCII},
 
214
    {"ROMAN8",                  ASCII},
213
215
    {"ISO-2022-JP",             ISO_2022_JP},
214
216
    {"ISO2022JP-CP932",         CP50220},
215
217
    {"CP50220",                 CP50220},
221
223
    {"ISO-2022-JP-2004",        ISO_2022_JP_2004},
222
224
    {"SHIFT_JIS",               SHIFT_JIS},
223
225
    {"SJIS",                    SHIFT_JIS},
 
226
    {"MS_Kanji",                SHIFT_JIS},
 
227
    {"PCK",                     SHIFT_JIS},
224
228
    {"WINDOWS-31J",             WINDOWS_31J},
225
229
    {"CSWINDOWS31J",            WINDOWS_31J},
226
230
    {"CP932",                   WINDOWS_31J},
295
299
                               && (c != '(') && (c != ')') && (c != '.') && (c != 0x22)))
296
300
 
297
301
#define is_ibmext_in_sjis(c2) (CP932_TABLE_BEGIN <= c2 && c2 <= CP932_TABLE_END)
298
 
#define nkf_byte_jisx0201_katakana_p(c) (SP <= c && c < (0xE0&0x7F))
 
302
#define nkf_byte_jisx0201_katakana_p(c) (SP <= c && c <= 0x5F)
299
303
 
300
304
#define         HOLD_SIZE       1024
301
305
#if defined(INT_IS_SHORT)
379
383
static unsigned char   stdobuf[IOBUF_SIZE];
380
384
#endif
381
385
 
 
386
#define NKF_UNSPECIFIED (-TRUE)
 
387
 
382
388
/* flags */
383
389
static int             unbuf_f = FALSE;
384
390
static int             estab_f = FALSE;
393
399
static int             broken_f = FALSE;       /* convert ESC-less broken JIS */
394
400
static int             iso8859_f = FALSE;      /* ISO8859 through */
395
401
static int             mimeout_f = FALSE;       /* base64 mode */
396
 
static int             x0201_f = X0201_DEFAULT; /* convert JIS X 0201 */
 
402
static int             x0201_f = NKF_UNSPECIFIED;   /* convert JIS X 0201 */
397
403
static int             iso2022jp_f = FALSE;    /* replace non ISO-2022-JP with GETA */
398
404
 
399
405
#ifdef UNICODE_NORMALIZATION
468
474
    {"Shift_JIS", 0, 0, 0, {0, 0, 0}, s_status, s_iconv, 0},
469
475
#ifdef UTF8_INPUT_ENABLE
470
476
    {"UTF-8",     0, 0, 0, {0, 0, 0}, w_status, w_iconv, 0},
 
477
    {"UTF-16",     0, 0, 0, {0, 0, 0}, NULL, w_iconv16, 0},
 
478
    {"UTF-32",     0, 0, 0, {0, 0, 0}, NULL, w_iconv32, 0},
471
479
#endif
472
 
    {0}
 
480
    {NULL,        0, 0, 0, {0, 0, 0}, NULL, NULL, 0}
473
481
};
474
482
 
475
483
static int              mimeout_mode = 0; /* 0, -1, 'Q', 'B', 1, 2 */
802
810
typedef struct {
803
811
    long capa;
804
812
    long len;
805
 
    unsigned char *ptr;
 
813
    nkf_char *ptr;
806
814
} nkf_buf_t;
807
815
 
808
816
static nkf_buf_t *
809
817
nkf_buf_new(int length)
810
818
{
811
819
    nkf_buf_t *buf = nkf_xmalloc(sizeof(nkf_buf_t));
812
 
    buf->ptr = nkf_xmalloc(length);
 
820
    buf->ptr = nkf_xmalloc(sizeof(nkf_char) * length);
813
821
    buf->capa = length;
814
822
    buf->len = 0;
815
823
    return buf;
827
835
#define nkf_buf_length(buf) ((buf)->len)
828
836
#define nkf_buf_empty_p(buf) ((buf)->len == 0)
829
837
 
830
 
static unsigned char
 
838
static nkf_char
831
839
nkf_buf_at(nkf_buf_t *buf, int index)
832
840
{
833
841
    assert(index <= buf->len);
849
857
    buf->ptr[buf->len++] = c;
850
858
}
851
859
 
852
 
static unsigned char
 
860
static nkf_char
853
861
nkf_buf_pop(nkf_buf_t *buf)
854
862
{
855
863
    assert(!nkf_buf_empty_p(buf));
873
881
{
874
882
    fprintf(HELP_OUTPUT,
875
883
            "Usage:  nkf -[flags] [--] [in file] .. [out file for -O flag]\n"
876
 
            " j,s,e,w  Output code is ISO-2022-JP, Shift JIS, EUC-JP, UTF-8N\n"
877
884
#ifdef UTF8_OUTPUT_ENABLE
878
 
            "          After 'w' you can add more options. -w[ 8 [0], 16 [[BL] [0]] ]\n"
 
885
            " j/s/e/w  Specify output encoding ISO-2022-JP, Shift_JIS, EUC-JP\n"
 
886
            "          UTF options is -w[8[0],{16,32}[{B,L}[0]]]\n"
 
887
#else
879
888
#endif
880
 
            " J,S,E,W  Input assumption is JIS 7 bit , Shift JIS, EUC-JP, UTF-8\n"
881
889
#ifdef UTF8_INPUT_ENABLE
882
 
            "          After 'W' you can add more options. -W[ 8, 16 [BL] ] \n"
 
890
            " J/S/E/W  Specify input encoding ISO-2022-JP, Shift_JIS, EUC-JP\n"
 
891
            "          UTF option is -W[8,[16,32][B,L]]\n"
 
892
#else
 
893
            " J/S/E    Specify output encoding ISO-2022-JP, Shift_JIS, EUC-JP\n"
883
894
#endif
884
895
            );
885
896
    fprintf(HELP_OUTPUT,
886
 
            " m[BQSN0] MIME decode [B:base64,Q:quoted,S:strict,N:non-strict,0:no decode]\n"
 
897
            " m[BQSN0] MIME decode [B:base64,Q:quoted,S:strict,N:nonstrict,0:no decode]\n"
887
898
            " M[BQ]    MIME encode [B:base64 Q:quoted]\n"
888
899
            " f/F      Folding: -f60 or -f or -f60-10 (fold margin 10) F preserve nl\n"
889
900
            );
891
902
            " Z[0-4]   Default/0: Convert JISX0208 Alphabet to ASCII\n"
892
903
            "          1: Kankaku to one space  2: to two spaces  3: HTML Entity\n"
893
904
            "          4: JISX0208 Katakana to JISX0201 Katakana\n"
894
 
            " X,x      Assume X0201 kana in MS-Kanji, -x preserves X0201\n"
 
905
            " X,x      Convert Halfwidth Katakana to Fullwidth or preserve it\n"
895
906
            );
896
907
    fprintf(HELP_OUTPUT,
897
908
            " O        Output to File (DEFAULT 'nkf.out')\n"
898
909
            " L[uwm]   Line mode u:LF w:CRLF m:CR (DEFAULT noconversion)\n"
899
910
            );
900
911
    fprintf(HELP_OUTPUT,
901
 
            "Long name options\n"
902
 
            " --ic=<input codeset>  --oc=<output codeset>\n"
903
 
            "                   Specify the input or output codeset\n"
904
 
            " --hiragana  --katakana  --katakana-hiragana\n"
905
 
            "                   To Hiragana/Katakana Conversion\n"
 
912
            " --ic=<encoding>        Specify the input encoding\n"
 
913
            " --oc=<encoding>        Specify the output encoding\n"
 
914
            " --hiragana --katakana  Hiragana/Katakana Conversion\n"
 
915
            " --katakana-hiragana    Converts each other\n"
906
916
            );
907
917
    fprintf(HELP_OUTPUT,
908
918
#ifdef INPUT_OPTION
909
 
            " --cap-input, --url-input  Convert hex after ':' or '%%'\n"
 
919
            " --{cap, url}-input     Convert hex after ':' or '%%'\n"
910
920
#endif
911
921
#ifdef NUMCHAR_OPTION
912
 
            " --numchar-input   Convert Unicode Character Reference\n"
 
922
            " --numchar-input        Convert Unicode Character Reference\n"
913
923
#endif
914
924
#ifdef UTF8_INPUT_ENABLE
915
925
            " --fb-{skip, html, xml, perl, java, subchar}\n"
916
 
            "                   Specify how nkf handles unassigned characters\n"
 
926
            "                        Specify unassigned character's replacement\n"
917
927
#endif
918
928
            );
919
929
    fprintf(HELP_OUTPUT,
920
930
#ifdef OVERWRITE
921
 
            " --in-place[=SUF]  Overwrite original listed files by filtered result\n"
922
 
            " --overwrite[=SUF] in-place and preserve timestamp of original files\n"
 
931
            " --in-place[=SUF]       Overwrite original files\n"
 
932
            " --overwrite[=SUF]      Preserve timestamp of original files\n"
923
933
#endif
924
 
            " -g --guess        Guess the input code\n"
925
 
            " -v --version      print the version\n"
926
 
            " --help/-V         print this help / configuration\n"
 
934
            " -g --guess             Guess the input code\n"
 
935
            " -v --version           Print the version\n"
 
936
            " --help/-V              Print this help / configuration\n"
927
937
            );
928
938
    version();
929
939
}
1024
1034
    int shift = 20;
1025
1035
    c &= VALUE_MASK;
1026
1036
    while(shift >= 0){
1027
 
        if(c >= 1<<shift){
 
1037
        if(c >= NKF_INT32_C(1)<<shift){
1028
1038
            while(shift >= 0){
1029
1039
                (*f)(0, bin2hex(c>>shift));
1030
1040
                shift -= 4;
1199
1209
    case ISO_8859_1:
1200
1210
        iso8859_f = TRUE;
1201
1211
        break;
1202
 
    case CP50220:
1203
1212
    case CP50221:
1204
1213
    case CP50222:
 
1214
        if (x0201_f == NKF_UNSPECIFIED) x0201_f = FALSE;        /* -x specified implicitly */
 
1215
    case CP50220:
1205
1216
#ifdef SHIFTJIS_CP932
1206
1217
        cp51932_f = TRUE;
1207
1218
#endif
1223
1234
    case SHIFT_JIS:
1224
1235
        break;
1225
1236
    case WINDOWS_31J:
 
1237
        if (x0201_f == NKF_UNSPECIFIED) x0201_f = FALSE;        /* -x specified implicitly */
1226
1238
#ifdef SHIFTJIS_CP932
1227
1239
        cp51932_f = TRUE;
1228
1240
#endif
1244
1256
    case EUCJP_NKF:
1245
1257
        break;
1246
1258
    case CP51932:
 
1259
        if (x0201_f == NKF_UNSPECIFIED) x0201_f = FALSE;        /* -x specified implicitly */
1247
1260
#ifdef SHIFTJIS_CP932
1248
1261
        cp51932_f = TRUE;
1249
1262
#endif
1252
1265
#endif
1253
1266
        break;
1254
1267
    case EUCJP_MS:
 
1268
        if (x0201_f == NKF_UNSPECIFIED) x0201_f = FALSE;        /* -x specified implicitly */
1255
1269
#ifdef SHIFTJIS_CP932
1256
1270
        cp51932_f = FALSE;
1257
1271
#endif
1260
1274
#endif
1261
1275
        break;
1262
1276
    case EUCJP_ASCII:
 
1277
        if (x0201_f == NKF_UNSPECIFIED) x0201_f = FALSE;        /* -x specified implicitly */
1263
1278
#ifdef SHIFTJIS_CP932
1264
1279
        cp51932_f = FALSE;
1265
1280
#endif
1314
1329
{
1315
1330
    switch (nkf_enc_to_index(enc)) {
1316
1331
    case CP50220:
1317
 
        x0201_f = TRUE;
1318
1332
#ifdef SHIFTJIS_CP932
1319
1333
        if (cp932inv_f == TRUE) cp932inv_f = FALSE;
1320
1334
#endif
1323
1337
#endif
1324
1338
        break;
1325
1339
    case CP50221:
 
1340
        if (x0201_f == NKF_UNSPECIFIED) x0201_f = FALSE;        /* -x specified implicitly */
1326
1341
#ifdef SHIFTJIS_CP932
1327
1342
        if (cp932inv_f == TRUE) cp932inv_f = FALSE;
1328
1343
#endif
1330
1345
        ms_ucs_map_f = UCS_MAP_CP932;
1331
1346
#endif
1332
1347
        break;
 
1348
    case ISO_2022_JP:
 
1349
#ifdef SHIFTJIS_CP932
 
1350
        if (cp932inv_f == TRUE) cp932inv_f = FALSE;
 
1351
#endif
 
1352
        break;
1333
1353
    case ISO_2022_JP_1:
1334
1354
        x0212_f = TRUE;
1335
1355
#ifdef SHIFTJIS_CP932
1346
1366
    case SHIFT_JIS:
1347
1367
        break;
1348
1368
    case WINDOWS_31J:
 
1369
        if (x0201_f == NKF_UNSPECIFIED) x0201_f = FALSE;        /* -x specified implicitly */
1349
1370
#ifdef UTF8_OUTPUT_ENABLE
1350
1371
        ms_ucs_map_f = UCS_MAP_CP932;
1351
1372
#endif
1374
1395
#endif
1375
1396
        break;
1376
1397
    case CP51932:
 
1398
        if (x0201_f == NKF_UNSPECIFIED) x0201_f = FALSE;        /* -x specified implicitly */
1377
1399
#ifdef SHIFTJIS_CP932
1378
1400
        if (cp932inv_f == TRUE) cp932inv_f = FALSE;
1379
1401
#endif
1382
1404
#endif
1383
1405
        break;
1384
1406
    case EUCJP_MS:
 
1407
        if (x0201_f == NKF_UNSPECIFIED) x0201_f = FALSE;        /* -x specified implicitly */
1385
1408
        x0212_f = TRUE;
1386
1409
#ifdef UTF8_OUTPUT_ENABLE
1387
1410
        ms_ucs_map_f = UCS_MAP_MS;
1388
1411
#endif
1389
1412
        break;
1390
1413
    case EUCJP_ASCII:
 
1414
        if (x0201_f == NKF_UNSPECIFIED) x0201_f = FALSE;        /* -x specified implicitly */
1391
1415
        x0212_f = TRUE;
1392
1416
#ifdef UTF8_OUTPUT_ENABLE
1393
1417
        ms_ucs_map_f = UCS_MAP_ASCII;
1424
1448
        output_endian = ENDIAN_LITTLE;
1425
1449
        output_bom_f = TRUE;
1426
1450
        break;
 
1451
    case UTF_32:
1427
1452
    case UTF_32BE_BOM:
1428
1453
        output_bom_f = TRUE;
1429
1454
        break;
1650
1675
        *p3 = 0x80 | ( val        & 0x3f);
1651
1676
        *p4 = 0;
1652
1677
    } else if (nkf_char_unicode_value_p(val)) {
1653
 
        *p1 = 0xe0 |  (val >> 16);
 
1678
        *p1 = 0xf0 |  (val >> 18);
1654
1679
        *p2 = 0x80 | ((val >> 12) & 0x3f);
1655
1680
        *p3 = 0x80 | ((val >>  6) & 0x3f);
1656
1681
        *p4 = 0x80 | ( val        & 0x3f);
2177
2202
    return 0;
2178
2203
}
2179
2204
 
2180
 
#define NKF_ICONV_NEED_ONE_MORE_BYTE -1
2181
 
#define NKF_ICONV_NEED_TWO_MORE_BYTES -2
 
2205
#define NKF_ICONV_NEED_ONE_MORE_BYTE (size_t)-1
 
2206
#define NKF_ICONV_NEED_TWO_MORE_BYTES (size_t)-2
2182
2207
#define UTF16_TO_UTF32(lead, trail) (((lead) << 10) + (trail) - NKF_INT32_C(0x35FDC00))
2183
2208
static size_t
2184
2209
nkf_iconv_utf_16(nkf_char c1, nkf_char c2, nkf_char c3, nkf_char c4)
2214
2239
static nkf_char
2215
2240
w_iconv16(nkf_char c2, nkf_char c1, nkf_char c0)
2216
2241
{
2217
 
    return 0;
 
2242
    (*oconv)(c2, c1);
 
2243
    return 16; /* different from w_iconv32 */
2218
2244
}
2219
2245
 
2220
2246
static nkf_char
2221
2247
w_iconv32(nkf_char c2, nkf_char c1, nkf_char c0)
2222
2248
{
2223
 
    return 0;
 
2249
    (*oconv)(c2, c1);
 
2250
    return 32; /* different from w_iconv16 */
2224
2251
}
2225
2252
 
2226
2253
static size_t
3034
3061
}
3035
3062
#endif /*WIN32DLL*/
3036
3063
 
3037
 
static unsigned char   hold_buf[HOLD_SIZE*2];
 
3064
static nkf_char   hold_buf[HOLD_SIZE*2];
3038
3065
static int             hold_count = 0;
3039
3066
static nkf_char
3040
3067
push_hold_buf(nkf_char c2)
3041
3068
{
3042
3069
    if (hold_count >= HOLD_SIZE*2)
3043
3070
        return (EOF);
3044
 
    hold_buf[hold_count++] = (unsigned char)c2;
 
3071
    hold_buf[hold_count++] = c2;
3045
3072
    return ((hold_count >= HOLD_SIZE*2) ? EOF : hold_count);
3046
3073
}
3047
3074
 
3048
3075
static int
3049
 
h_conv(FILE *f, int c1, int c2)
 
3076
h_conv(FILE *f, nkf_char c1, nkf_char c2)
3050
3077
{
3051
 
    int ret, c4, c3;
 
3078
    int ret;
3052
3079
    int hold_index;
3053
 
 
 
3080
    nkf_char c3, c4;
3054
3081
 
3055
3082
    /** it must NOT be in the kanji shifte sequence      */
3056
3083
    /** it must NOT be written in JIS7                   */
3100
3127
    hold_index = 0;
3101
3128
    while (hold_index < hold_count){
3102
3129
        c1 = hold_buf[hold_index++];
3103
 
        if (c1 <= DEL){
 
3130
        if (nkf_char_unicode_p(c1)) {
 
3131
            (*oconv)(0, c1);
 
3132
            continue;
 
3133
        }
 
3134
        else if (c1 <= DEL){
3104
3135
            (*iconv)(0, c1, 0);
3105
3136
            continue;
3106
3137
        }else if (iconv == s_iconv && 0xa1 <= c1 && c1 <= 0xdf){
3126
3157
            } else if ((c3 = (*i_getc)(f)) == EOF) {
3127
3158
                ret = EOF;
3128
3159
                break;
3129
 
            } else {
3130
 
                code_status(c3);
3131
 
                if (hold_index < hold_count){
3132
 
                    c4 = hold_buf[hold_index++];
3133
 
                } else if ((c4 = (*i_getc)(f)) == EOF) {
3134
 
                    c3 = ret = EOF;
3135
 
                    break;
3136
 
                } else {
3137
 
                    code_status(c4);
3138
 
                    (*iconv)(c1, c2, (c3<<8)|c4);
3139
 
                }
3140
 
            }
 
3160
            }
 
3161
            code_status(c3);
 
3162
            if (hold_index < hold_count){
 
3163
                c4 = hold_buf[hold_index++];
 
3164
            } else if ((c4 = (*i_getc)(f)) == EOF) {
 
3165
                c3 = ret = EOF;
 
3166
                break;
 
3167
            }
 
3168
            code_status(c4);
 
3169
            (*iconv)(c1, c2, (c3<<8)|c4);
3141
3170
            break;
3142
3171
        case -1:
3143
3172
            /* 3 bytes EUC or UTF-8 */
3337
3366
    else if (c2 != 0 || c1 != LF) (*o_eol_conv)(c2, c1);
3338
3367
}
3339
3368
 
 
3369
static void
 
3370
put_newline(void (*func)(nkf_char))
 
3371
{
 
3372
    switch (eolmode_f ? eolmode_f : DEFAULT_NEWLINE) {
 
3373
      case CRLF:
 
3374
        (*func)(0x0D);
 
3375
        (*func)(0x0A);
 
3376
        break;
 
3377
      case CR:
 
3378
        (*func)(0x0D);
 
3379
        break;
 
3380
      case LF:
 
3381
        (*func)(0x0A);
 
3382
        break;
 
3383
    }
 
3384
}
 
3385
 
 
3386
static void
 
3387
oconv_newline(void (*func)(nkf_char, nkf_char))
 
3388
{
 
3389
    switch (eolmode_f ? eolmode_f : DEFAULT_NEWLINE) {
 
3390
      case CRLF:
 
3391
        (*func)(0, 0x0D);
 
3392
        (*func)(0, 0x0A);
 
3393
        break;
 
3394
      case CR:
 
3395
        (*func)(0, 0x0D);
 
3396
        break;
 
3397
      case LF:
 
3398
        (*func)(0, 0x0A);
 
3399
        break;
 
3400
    }
 
3401
}
 
3402
 
3340
3403
/*
3341
3404
   Return value of fold_conv()
3342
3405
 
3509
3572
    /* terminator process */
3510
3573
    switch(fold_state) {
3511
3574
    case LF:
3512
 
        OCONV_NEWLINE((*o_fconv));
 
3575
        oconv_newline(o_fconv);
3513
3576
        (*o_fconv)(c2,c1);
3514
3577
        break;
3515
3578
    case 0:
3516
3579
        return;
3517
3580
    case CR:
3518
 
        OCONV_NEWLINE((*o_fconv));
 
3581
        oconv_newline(o_fconv);
3519
3582
        break;
3520
3583
    case TAB:
3521
3584
    case SP:
3802
3865
    (const unsigned char *)"\075?ISO-8859-1?Q?",
3803
3866
    (const unsigned char *)"\075?ISO-8859-1?B?",
3804
3867
    (const unsigned char *)"\075?ISO-2022-JP?B?",
 
3868
    (const unsigned char *)"\075?ISO-2022-JP?B?",
3805
3869
    (const unsigned char *)"\075?ISO-2022-JP?Q?",
3806
3870
#if defined(UTF8_INPUT_ENABLE)
3807
3871
    (const unsigned char *)"\075?UTF-8?B?",
3822
3886
};
3823
3887
 
3824
3888
static const nkf_char mime_encode[] = {
3825
 
    EUC_JP, SHIFT_JIS, ISO_8859_1, ISO_8859_1, JIS_X_0208, JIS_X_0201_1976_K,
 
3889
    EUC_JP, SHIFT_JIS, ISO_8859_1, ISO_8859_1, JIS_X_0208, JIS_X_0201_1976_K, JIS_X_0201_1976_K,
3826
3890
#if defined(UTF8_INPUT_ENABLE)
3827
3891
    UTF_8, UTF_8,
3828
3892
#endif
3831
3895
};
3832
3896
 
3833
3897
static const nkf_char mime_encode_method[] = {
3834
 
    'B', 'B','Q', 'B', 'B', 'Q',
 
3898
    'B', 'B','Q', 'B', 'B', 'B', 'Q',
3835
3899
#if defined(UTF8_INPUT_ENABLE)
3836
3900
    'B', 'Q',
3837
3901
#endif
4263
4327
 
4264
4328
    if (c == EOF || c > 0xFF || (c & 0xc0) == 0x80) return c;
4265
4329
 
4266
 
    nkf_buf_push(buf, (unsigned char)c);
 
4330
    nkf_buf_push(buf, c);
4267
4331
    do {
4268
4332
        while (lower <= upper) {
4269
4333
            int mid = (lower+upper) / 2;
4425
4489
        }
4426
4490
        if (c1=='='&&c2<SP) { /* this is soft wrap */
4427
4491
            while((c1 =  (*i_mgetc)(f)) <=SP) {
4428
 
                if ((c1 = (*i_mgetc)(f)) == EOF) return (EOF);
 
4492
                if (c1 == EOF) return (EOF);
4429
4493
            }
4430
4494
            mime_decode_mode = 'Q'; /* still in MIME */
4431
4495
            goto restart_mime_q;
4597
4661
            (*o_mputc)(mimeout_state.buf[i]);
4598
4662
            i++;
4599
4663
        }
4600
 
        PUT_NEWLINE((*o_mputc));
 
4664
        put_newline(o_mputc);
4601
4665
        (*o_mputc)(SP);
4602
4666
        base64_count = 1;
4603
4667
        if (mimeout_state.count>0 && nkf_isspace(mimeout_state.buf[i])) {
4630
4694
        if (c2 == EOF){
4631
4695
            if (base64_count + mimeout_state.count/3*4> 73){
4632
4696
                (*o_base64conv)(EOF,0);
4633
 
                OCONV_NEWLINE((*o_base64conv));
 
4697
                oconv_newline(o_base64conv);
4634
4698
                (*o_base64conv)(0,SP);
4635
4699
                base64_count = 1;
4636
4700
            }
4637
4701
        } else {
4638
 
            if (base64_count + mimeout_state.count/3*4> 66) {
 
4702
            if ((c2 != 0 || c1 > DEL) && base64_count + mimeout_state.count/3*4> 66) {
4639
4703
                (*o_base64conv)(EOF,0);
4640
 
                OCONV_NEWLINE((*o_base64conv));
 
4704
                oconv_newline(o_base64conv);
4641
4705
                (*o_base64conv)(0,SP);
4642
4706
                base64_count = 1;
4643
4707
                mimeout_mode = -1;
4648
4712
            mimeout_mode =  (output_mode==ASCII ||output_mode == ISO_8859_1) ? 'Q' : 'B';
4649
4713
            open_mime(output_mode);
4650
4714
            (*o_base64conv)(EOF,0);
4651
 
            OCONV_NEWLINE((*o_base64conv));
 
4715
            oconv_newline(o_base64conv);
4652
4716
            (*o_base64conv)(0,SP);
4653
4717
            base64_count = 1;
4654
4718
            mimeout_mode = -1;
4746
4810
            if (base64_count > 71){
4747
4811
                if (c!=CR && c!=LF) {
4748
4812
                    (*o_mputc)('=');
4749
 
                    PUT_NEWLINE((*o_mputc));
 
4813
                    put_newline(o_mputc);
4750
4814
                }
4751
4815
                base64_count = 0;
4752
4816
            }
4753
4817
        }else{
4754
4818
            if (base64_count > 71){
4755
4819
                eof_mime();
4756
 
                PUT_NEWLINE((*o_mputc));
 
4820
                put_newline(o_mputc);
4757
4821
                base64_count = 0;
4758
4822
            }
4759
4823
            if (c == EOF) { /* c==EOF */
4815
4879
            } else if (c <= SP) {
4816
4880
                close_mime();
4817
4881
                if (base64_count > 70) {
4818
 
                    PUT_NEWLINE((*o_mputc));
 
4882
                    put_newline(o_mputc);
4819
4883
                    base64_count = 0;
4820
4884
                }
4821
4885
                if (!nkf_isblank(c)) {
4825
4889
            } else {
4826
4890
                if (base64_count > 70) {
4827
4891
                    close_mime();
4828
 
                    PUT_NEWLINE((*o_mputc));
 
4892
                    put_newline(o_mputc);
4829
4893
                    (*o_mputc)(SP);
4830
4894
                    base64_count = 1;
4831
4895
                    open_mime(output_mode);
4835
4899
                    return;
4836
4900
                }
4837
4901
            }
4838
 
            (*o_mputc)(c);
4839
 
            base64_count++;
 
4902
            if (c != 0x1B) {
 
4903
                (*o_mputc)(c);
 
4904
                base64_count++;
 
4905
                return;
 
4906
            }
4840
4907
        }
4841
 
        return;
4842
4908
    }
4843
4909
 
4844
4910
    if (mimeout_mode <= 0) {
4845
 
        if (c <= DEL && (output_mode==ASCII ||output_mode == ISO_8859_1)) {
 
4911
        if (c <= DEL && (output_mode==ASCII || output_mode == ISO_8859_1 ||
 
4912
                    output_mode == UTF_8)) {
4846
4913
            if (nkf_isspace(c)) {
4847
4914
                int flag = 0;
4848
4915
                if (mimeout_mode == -1) {
4887
4954
                    }
4888
4955
 
4889
4956
                    if (i == 0 || i == mimeout_state.count - len) {
4890
 
                        PUT_NEWLINE((*o_mputc));
 
4957
                        put_newline(o_mputc);
4891
4958
                        base64_count = 0;
4892
4959
                        if (!nkf_isspace(mimeout_state.buf[0])){
4893
4960
                            (*o_mputc)(SP);
4899
4966
                        for (j = 0; j <= i; ++j) {
4900
4967
                            (*o_mputc)(mimeout_state.buf[j]);
4901
4968
                        }
4902
 
                        PUT_NEWLINE((*o_mputc));
 
4969
                        put_newline(o_mputc);
4903
4970
                        base64_count = 1;
4904
4971
                        for (; j <= mimeout_state.count; ++j) {
4905
4972
                            mimeout_state.buf[j - i] = mimeout_state.buf[j];
4933
5000
        }
4934
5001
    }else{
4935
5002
        /* mimeout_mode == 'B', 1, 2 */
4936
 
        if ( c<=DEL && (output_mode==ASCII ||output_mode == ISO_8859_1)) {
 
5003
        if (c <= DEL && (output_mode==ASCII || output_mode == ISO_8859_1 ||
 
5004
                    output_mode == UTF_8)) {
4937
5005
            if (lastchar == CR || lastchar == LF){
4938
5006
                if (nkf_isblank(c)) {
4939
5007
                    for (i=0;i<mimeout_state.count;i++) {
4940
5008
                        mimeout_addchar(mimeout_state.buf[i]);
4941
5009
                    }
4942
5010
                    mimeout_state.count = 0;
4943
 
                } else if (SP<c && c<DEL) {
 
5011
                } else {
4944
5012
                    eof_mime();
4945
5013
                    for (i=0;i<mimeout_state.count;i++) {
4946
5014
                        (*o_mputc)(mimeout_state.buf[i]);
5126
5194
    broken_f = FALSE;
5127
5195
    iso8859_f = FALSE;
5128
5196
    mimeout_f = FALSE;
5129
 
    x0201_f = X0201_DEFAULT;
 
5197
    x0201_f = NKF_UNSPECIFIED;
5130
5198
    iso2022jp_f = FALSE;
5131
5199
#if defined(UTF8_INPUT_ENABLE) || defined(UTF8_OUTPUT_ENABLE)
5132
5200
    ms_ucs_map_f = UCS_MAP_ASCII;
5236
5304
    set_output_encoding(output_encoding);
5237
5305
    oconv = nkf_enc_to_oconv(output_encoding);
5238
5306
    o_putc = std_putc;
 
5307
    if (nkf_enc_unicode_p(output_encoding))
 
5308
        output_mode = UTF_8;
 
5309
 
 
5310
        if (x0201_f == NKF_UNSPECIFIED) {
 
5311
                x0201_f = X0201_DEFAULT;
 
5312
        }
5239
5313
 
5240
5314
    /* replace continucation module, from output side */
5241
5315
 
5344
5418
#define NEXT continue        /* no output, get next */
5345
5419
#define SKIP c2=0;continue        /* no output, get next */
5346
5420
#define MORE c2=c1;continue  /* need one more byte */
5347
 
#define SEND ;               /* output c1 and c2, get next */
 
5421
#define SEND (void)0         /* output c1 and c2, get next */
5348
5422
#define LAST break           /* end of loop, go closing  */
5349
5423
#define set_input_mode(mode) do { \
5350
5424
    input_mode = mode; \
5384
5458
               (c4 = (*i_getc)(f)) != EOF) {
5385
5459
            nkf_iconv_utf_32(c1, c2, c3, c4);
5386
5460
        }
5387
 
        (*i_ungetc)(EOF, f);
 
5461
        goto finished;
5388
5462
    }
5389
5463
    else if (iconv == w_iconv16) {
5390
5464
        while ((c1 = (*i_getc)(f)) != EOF &&
5391
5465
               (c2 = (*i_getc)(f)) != EOF) {
5392
 
            if (nkf_iconv_utf_16(c1, c2, 0, 0) == -2 &&
 
5466
            if (nkf_iconv_utf_16(c1, c2, 0, 0) == NKF_ICONV_NEED_TWO_MORE_BYTES &&
5393
5467
                (c3 = (*i_getc)(f)) != EOF &&
5394
5468
                (c4 = (*i_getc)(f)) != EOF) {
5395
5469
                nkf_iconv_utf_16(c1, c2, c3, c4);
5396
5470
            }
5397
5471
        }
5398
 
        (*i_ungetc)(EOF, f);
 
5472
        goto finished;
5399
5473
    }
5400
5474
#endif
5401
5475
 
5442
5516
            if (input_mode == JIS_X_0208 && DEL <= c1 && c1 < 0x92) {
5443
5517
                /* CP5022x */
5444
5518
                MORE;
 
5519
            }else if (input_codename && input_codename[0] == 'I' &&
 
5520
                    0xA1 <= c1 && c1 <= 0xDF) {
 
5521
                /* JIS X 0201 Katakana in 8bit JIS */
 
5522
                c2 = JIS_X_0201_1976_K;
 
5523
                c1 &= 0x7f;
 
5524
                SEND;
5445
5525
            } else if (c1 > DEL) {
5446
5526
                /* 8 bit code */
5447
5527
                if (!estab_f && !iso8859_f) {
5516
5596
                SKIP;
5517
5597
            } else if (c1 == ESC && (!is_8bit || mime_decode_mode)) {
5518
5598
                if ((c1 = (*i_getc)(f)) == EOF) {
5519
 
                    /*  (*oconv)(0, ESC); don't send bogus code */
 
5599
                    (*oconv)(0, ESC);
5520
5600
                    LAST;
5521
5601
                }
5522
5602
                else if (c1 == '&') {
5646
5726
            } else if (c1 == ESC && iconv == s_iconv) {
5647
5727
                /* ESC in Shift_JIS */
5648
5728
                if ((c1 = (*i_getc)(f)) == EOF) {
5649
 
                    /*  (*oconv)(0, ESC); don't send bogus code */
 
5729
                    (*oconv)(0, ESC);
5650
5730
                    LAST;
5651
5731
                } else if (c1 == '$') {
5652
5732
                    /* J-PHONE emoji */
5771
5851
        /* goto next_word */
5772
5852
    }
5773
5853
 
 
5854
finished:
5774
5855
    /* epilogue */
5775
5856
    (*iconv)(EOF, 0, 0);
5776
5857
    if (!input_codename)
5822
5903
                option_mode = 1;
5823
5904
                return 0;
5824
5905
            }
5825
 
            for (i=0;i<sizeof(long_option)/sizeof(long_option[0]);i++) {
 
5906
            for (i=0;i<(int)(sizeof(long_option)/sizeof(long_option[0]));i++) {
5826
5907
                p = (unsigned char *)long_option[i].name;
5827
5908
                for (j=0;*p && *p != '=' && *p == cp[j];p++, j++);
5828
5909
                if (*p == cp[j] || cp[j] == SP){
6130
6211
            break;
6131
6212
#endif
6132
6213
#ifdef UTF8_OUTPUT_ENABLE
6133
 
        case 'w':           /* UTF-8 output */
 
6214
        case 'w':           /* UTF-{8,16,32} output */
6134
6215
            if (cp[0] == '8') {
6135
6216
                cp++;
6136
6217
                if (cp[0] == '0'){
6155
6236
                if (cp[0]=='L') {
6156
6237
                    cp++;
6157
6238
                    output_endian = ENDIAN_LITTLE;
 
6239
                    output_bom_f = TRUE;
6158
6240
                } else if (cp[0] == 'B') {
6159
6241
                    cp++;
6160
 
                } else {
6161
 
                    output_encoding = nkf_enc_from_index(enc_idx);
6162
 
                    continue;
 
6242
                    output_bom_f = TRUE;
6163
6243
                }
6164
6244
                if (cp[0] == '0'){
 
6245
                    output_bom_f = FALSE;
6165
6246
                    cp++;
6166
6247
                    enc_idx = enc_idx == UTF_16
6167
6248
                        ? (output_endian == ENDIAN_LITTLE ? UTF_16LE : UTF_16BE)
6168
6249
                        : (output_endian == ENDIAN_LITTLE ? UTF_32LE : UTF_32BE);
6169
6250
                } else {
6170
 
                    output_bom_f = TRUE;
6171
6251
                    enc_idx = enc_idx == UTF_16
6172
6252
                        ? (output_endian == ENDIAN_LITTLE ? UTF_16LE_BOM : UTF_16BE_BOM)
6173
6253
                        : (output_endian == ENDIAN_LITTLE ? UTF_32LE_BOM : UTF_32BE_BOM);
6227
6307
               bit:3   Convert HTML Entity
6228
6308
               bit:4   Convert JIS X 0208 Katakana to JIS X 0201 Katakana
6229
6309
             */
6230
 
            while ('0'<= *cp && *cp <='9') {
 
6310
            while ('0'<= *cp && *cp <='4') {
6231
6311
                alpha_f |= 1 << (*cp++ - '0');
6232
6312
            }
6233
 
            if (!alpha_f) alpha_f = 1;
 
6313
            alpha_f |= 1;
6234
6314
            continue;
6235
6315
        case 'x':   /* Convert X0201 kana to X0208 or X0201 Conversion */
6236
6316
            x0201_f = FALSE;    /* No X0201->X0208 conversion */