File: identify_encoding.c

package info (click to toggle)
libapache-mod-encoding 0.0.20021209-12
  • links: PTS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,700 kB
  • sloc: ansic: 94,406; sh: 6,693; makefile: 43
file content (296 lines) | stat: -rw-r--r-- 7,294 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
 *
 * ɤȽ̤iconv Ѥʸ󥳡ǥʸ֤
 *
 * 2001/10/24  Remove static variables
 *             Kazuhiko Iwama <iwama@ymc.ne.jp>
 * 2001/10/14  First version
 *             Kazuhiko Iwama <iwama@ymc.ne.jp>
 *
 */

#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "identify_encoding.h"

enum encoding_id {
    eid_KNOWN   = 0,
    eid_ASCII   = 1,
    eid_JIS     = 2,
    eid_SJIS    = 3,
    eid_EUCJP   = 4,
    eid_UTF8    = 5
};

const char *encoding_str[] = {
    "KNOWN", "ASCII", "ISO-2022-JP", "SJIS", "EUC-JP", "UTF-8"
};

static int
is_ascii(ie_state_t *st, const unsigned char *p)
{
    if ( !(isprint(*p) || isspace(*p)) || *p >= 0x80)  st->flag = 0;

    return st->flag;
}

static int
is_jis(ie_state_t *st, const unsigned char *p)
{
    if (*p >= 0x80)  st->flag = 0;

    return st->flag;
}

static int
is_sjis(ie_state_t *st, const unsigned char *p)
{
    switch (st->state){
    case 0:
	st->c_type = 0;
	if ( *p >= 0x80 ){
	    st->state = 1;
	    if      (*p >= 0x81 && *p <= 0x9f)  st->c_type = 1;
	    else if (*p >= 0xe0 && *p <= 0xef)  st->c_type = 1;
	    else if (*p >= 0xa1 && *p <= 0xdf)  st->state = 0;
	    else				st->flag = 1;
	}
	break;
    case 1:
	if      (*p >= 0x40 && *p <= 0x7e)  st->state = 0;
	else if (*p >= 0x80 && *p <= 0xfc)  st->state = 0;
	else                                st->flag = 0;
	break;
    default:
	st->flag = 0;
	break;
    }

    return st->flag;
}

static int
is_eucjp(ie_state_t *st, const unsigned char *p)
{
    switch (st->state){
    case 0:
	st->c_type = 0;
	if ( *p >= 0x80 ){
	    st->state = 1;
	    if      (*p >= 0xa1 && *p <= 0xfe)  st->c_type = 1;
	    else if (*p == 0x8e              )  st->c_type = 2;
	    else if (*p == 0x8f              )  st->c_type = 3;
	    else				st->flag = 0;
	}
	break;
    case 1:
	switch (st->c_type){
	case 1:
	    if (*p >= 0xa1 && *p <= 0xfe)  st->state = 0;
	    else                           st->flag = 0;
	    break;
	case 2:
	    if (*p >= 0x81 && *p <= 0xff)  st->state = 0;
	    else                           st->flag = 0;
	    break;
	case 3:
	    if (*p >= 0x81 && *p <= 0xff)  st->state = 2;
	    else                           st->flag = 0;
	    break;
	default:
	    st->flag = 0;
	    break;
	}
	break;
    case 2:
	if (*p >= 0x81 && *p <= 0xff)  st->state = 0;
	else                           st->flag = 0;
    default:
	st->flag = 0;
	break;
    }

    return st->flag;
}

static int
is_utf8(ie_state_t *st, const unsigned char *p)
{
    switch (st->state){
    case 0:
	st->c_type = 0;
	if ( *p >= 0x80 ){
	    st->state = 1;
	    if      (*p >= 0xc2 && *p <= 0xdf)  st->c_type = 1;
	    else if (*p == 0xe0              )  st->c_type = 2;
	    else if (*p >= 0xe1 && *p <= 0xef)  st->c_type = 3;
	    else if (*p == 0xf0              )  st->c_type = 4;
	    else if (*p >= 0xf1 && *p <= 0xf3)  st->c_type = 5;
	    else if (*p == 0xf4              )  st->c_type = 6;
	    else				st->flag = 0;
	}
	break;
    case 1:
	switch (st->c_type){
	case 1:
	    if (*p >= 0x80 && *p <= 0xbf)  st->state = 0;
	    else                           st->flag = 0;
	    break;
	case 2:
	    if (*p >= 0xa0 && *p <= 0xbf)  st->state = 2;
	    else                           st->flag = 0;
	    break;
	case 3:
	    if (*p >= 0x80 && *p <= 0xbf)  st->state = 2;
	    else                           st->flag = 0;
	    break;
	case 4:
	    if (*p >= 0x90 && *p <= 0xbf)  st->state = 2;
	    else                           st->flag = 0;
	    break;
	case 5:
	    if (*p >= 0x80 && *p <= 0xbf)  st->state = 2;
	    else                           st->flag = 0;
	    break;
	case 6:
	    if (*p >= 0x80 && *p <= 0x8f)  st->state = 2;
	    else                           st->flag = 0;
	    break;
	default:
	    st->flag = 0;
	    break;
	}
	break;
    case 2:
	if (st->c_type >= 2 && st->c_type <= 3){
	    if (*p >= 0x80 && *p <= 0xbf)  st->state = 0;
	    else                           st->flag = 0;
	} else if (st->c_type >= 4 && st->c_type <= 6){
	    if (*p >= 0x80 && *p <= 0xbf)  st->state = 3;
	    else                           st->flag = 0;
	} else {
	    st->flag = 0;
	}
	break;
    case 3:
	if (st->c_type >= 4 && st->c_type <= 6){
	    if (*p >= 0x80 && *p <= 0xbf)  st->state = 0;
	    else                           st->flag = 0;
	} else {
	    st->flag = 0;
	}
	break;
    default:
	st->flag = 0;
	break;
    }

    return st->flag;
}

identify_encoding_t*
identify_encoding_open(enum identify_encoding_order order)
{
    identify_encoding_t* cd;
    cd = (identify_encoding_t*)malloc(sizeof(identify_encoding_t));

    if (cd == NULL) {
        cd = (identify_encoding_t*)(-1);
    } else {
        cd->order = order;
        identify_encoding_reset(cd);
    }
    return cd;
}

void
identify_encoding_close(identify_encoding_t* cd)
{
    if (cd != (identify_encoding_t*)(-1) || cd != NULL){
        free(cd);
    }
}

static void
identify_encoding_reset_state(ie_state_t* st)
{
    st->flag = 1;
    st->state = 0;
    st->c_type = 0;
}

void
identify_encoding_reset(identify_encoding_t* cd)
{
    identify_encoding_reset_state(&(cd->st_ascii));
    identify_encoding_reset_state(&(cd->st_jis));
    identify_encoding_reset_state(&(cd->st_sjis));
    identify_encoding_reset_state(&(cd->st_eucjp));
    identify_encoding_reset_state(&(cd->st_utf8));
}

const char*
identify_encoding(identify_encoding_t *cd, char* instr)
{
    int n;
    unsigned char *p;
    enum encoding_id eid = eid_KNOWN;

    identify_encoding_reset(cd);

    for (n = 0, p = (unsigned char *)instr; *p != '\0' && n < IDENTIFY_MAX_LENGTH; p++, n++){
        if (cd->st_ascii.flag == 1)  is_ascii(&(cd->st_ascii), p);
        if (cd->st_jis.flag   == 1)  is_jis(&(cd->st_jis), p);
        if (cd->st_sjis.flag  == 1)  is_sjis(&(cd->st_sjis), p);
        if (cd->st_eucjp.flag == 1)  is_eucjp(&(cd->st_eucjp), p);
        if (cd->st_utf8.flag  == 1)  is_utf8(&(cd->st_utf8), p);
    }

    if      (cd->st_ascii.flag == 1)  eid = eid_ASCII;
    else if (cd->st_jis.flag   == 1)  eid = eid_JIS;
    else if (cd->st_utf8.flag  == 1)  eid = eid_UTF8;
    else if (cd->order == ieo_EUCJP) {
        if      (cd->st_eucjp.flag == 1)  eid = eid_EUCJP;
        else if (cd->st_sjis.flag  == 1)  eid = eid_SJIS;
    } else {
        if      (cd->st_sjis.flag  == 1)  eid = eid_SJIS;
        else if (cd->st_eucjp.flag == 1)  eid = eid_EUCJP;
    }

    return encoding_str[ eid ];
}

const char *
autodetect_encoding(char* instr, char* default_enc)
{
    identify_encoding_t *cd;
    enum identify_encoding_order order;
    const char* enc;
    int ms_flag;

    if (*default_enc == '\0')  enc = "JA-AUTO";
    else                       enc = default_enc;
    ms_flag = 0;

    if (strncasecmp("JA-AUTO", enc, 7) == 0){
        enc += 7;
        if (strncasecmp("-SJIS", enc, 5) == 0){
            enc += 5;
            order = ieo_SJIS;
        } else {
            order = ieo_EUCJP;
        }
        if (strncasecmp("-MS", enc, 3) == 0)  ms_flag = 1;

        cd = identify_encoding_open(order);
        if ( cd != (identify_encoding_t *)(-1) ){
            enc = identify_encoding(cd, instr);
            if (ms_flag == 1 && strcasecmp("SJIS", enc) == 0) enc = "MSSJIS";
            identify_encoding_close(cd);
        }
    }

    return enc;
}