aboutsummaryrefslogblamecommitdiffstats
path: root/libwc/detect.c
blob: 3fdf98093ca7c52b547c2221ad8324f2761fb555 (plain) (tree)
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544




































































































                                                                                                
                    

























































































































































































































































































































































































































































                                                                          
#include "wc.h"
#include "iso2022.h"
#include "sjis.h"
#include "big5.h"
#include "hz.h"
#include "viet.h"
#ifdef USE_UNICODE
#include "utf8.h"
#include "utf7.h"
#endif

wc_uint8 WC_DETECT_MAP[ 0x100 ] = {
    0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0, 0, 0, 
    1, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 1, 
};

#define DETECT_NORMAL	0
#define DETECT_POSSIBLE	1
#define DETECT_OK	2
#define DETECT_BROKEN	4
#define DETECT_ERROR	8
#define SET_DETECT(x,y) ((x) |= (y))
#define SET_BROKEN_ERROR(x) ((x) = ((x) & DETECT_BROKEN) ? DETECT_ERROR : ((x) | DETECT_BROKEN))

void
wc_create_detect_map(wc_ces ces, wc_bool esc)
{
    static wc_ces detect_ces = WC_CES_US_ASCII;
    int i;

    if (ces != detect_ces) {
	if (ces & WC_CES_T_VIET) {
	    wc_uint8 *map = NULL;
	    switch (ces) {
	    case WC_CES_TCVN_5712:
		map = wc_c0_tcvn57122_map;
		break;
	    case WC_CES_VISCII_11:
		map = wc_c0_viscii112_map;
		break;
	    case WC_CES_VPS:
		map = wc_c0_vps2_map;
		break;
	    }
	    for (i = 0; i < 0x20; i++)
		WC_DETECT_MAP[i] = map[i] ? 1 : 0;
	} else {
	    for (i = 0; i < 0x20; i++)
		WC_DETECT_MAP[i] = 0;
	    WC_DETECT_MAP[WC_C_HZ_TILDA] = (ces == WC_CES_HZ_GB_2312) ? 1 : 0;
#ifdef USE_UNICODE
	    WC_DETECT_MAP[WC_C_UTF7_PLUS] = (ces == WC_CES_UTF_7) ? 1 : 0;
#endif
	}
	detect_ces = ces;
    }
    WC_DETECT_MAP[WC_C_ESC] = (esc || (ces & WC_CES_T_ISO_2022)) ? 1 : 0;
    return;
}

wc_ces
wc_auto_detect(char *is, size_t len, wc_ces hint)
{
    wc_uchar *p = (wc_uchar *)is;
    wc_uchar *ep = p + len;
    wc_uchar *q;
    wc_ces euc = 0, priv = 0;
    wc_status st;
    int euc_state = 0, sjis_state = 0, big5_state = 0, hz_state = 0;
    int iso_detect = DETECT_ERROR, euc_detect = DETECT_ERROR,
	sjis_detect = DETECT_ERROR, big5_detect = DETECT_ERROR,
	hz_detect = DETECT_ERROR, latin_detect = DETECT_ERROR,
	priv_detect = DETECT_ERROR;
    int possible = 0;
    wc_bool iso2022jp2 = WC_FALSE, iso2022jp3 = WC_FALSE,
	iso2022cn = WC_FALSE, iso2022kr = WC_FALSE, ok = WC_FALSE;
#ifdef USE_UNICODE
    int utf8_state = 0;
    int utf8_detect = DETECT_ERROR;
    int utf8_next = 0;
#endif

    wc_create_detect_map(hint, WC_TRUE);
    for (; p < ep && ! WC_DETECT_MAP[*p]; p++)
	;
    if (p == ep)
	return hint;

    switch (hint) {
    case WC_CES_ISO_2022_JP:
    case WC_CES_ISO_2022_JP_2:
    case WC_CES_ISO_2022_JP_3:
    case WC_CES_EUC_JP:
    case WC_CES_SHIFT_JIS:
    case WC_CES_SHIFT_JISX0213:
	euc = WC_CES_EUC_JP;
	euc_state = WC_EUC_NOSTATE;
	sjis_state = WC_SJIS_NOSTATE;
	iso_detect = euc_detect = sjis_detect = DETECT_NORMAL;
	possible = 3;
	break;
    case WC_CES_ISO_2022_CN:
    case WC_CES_EUC_CN:
	euc = WC_CES_EUC_CN;
	euc_state = WC_EUC_NOSTATE;
	big5_state = WC_BIG5_NOSTATE;
	iso_detect = euc_detect = big5_detect = DETECT_NORMAL;
	possible = 3;
	break;
    case WC_CES_EUC_TW:
    case WC_CES_BIG5:
	euc = WC_CES_EUC_TW;
	euc_state = WC_EUC_NOSTATE;
	big5_state = WC_BIG5_NOSTATE;
	iso_detect = euc_detect = big5_detect = DETECT_NORMAL;
	possible = 3;
	break;
    case WC_CES_HZ_GB_2312:
	euc = WC_CES_EUC_CN;
	euc_state = WC_EUC_NOSTATE;
	hz_state = WC_HZ_NOSTATE;
	iso_detect = euc_detect = big5_detect = hz_detect = DETECT_NORMAL;
	possible = 4;
	break;
    case WC_CES_ISO_2022_KR:
    case WC_CES_EUC_KR:
	euc = WC_CES_EUC_KR;
	euc_state = WC_EUC_NOSTATE;
	iso_detect = euc_detect = DETECT_NORMAL;
	possible = 3;
	break;
#ifdef USE_UNICODE
    case WC_CES_UTF_8:
	iso_detect = DETECT_NORMAL;
	possible = 1;
	break;
#endif
    case WC_CES_US_ASCII:
	iso_detect = latin_detect = DETECT_NORMAL;
	possible = 2;
	break;
    default:
	if (hint & WC_CES_T_ISO_8859) {
	    iso_detect = latin_detect = DETECT_NORMAL;
	    possible = 2;
	} else {
	    iso_detect = priv_detect = DETECT_NORMAL;
	    priv = hint;	/* for TVCN, VISCII, VPS */
	    possible = 2;
	}
	break;
    }
#ifdef USE_UNICODE
    if (priv_detect == DETECT_ERROR) {
	utf8_detect = DETECT_NORMAL;
	possible++;
    }
#endif

    wc_input_init(WC_CES_US_ASCII, &st);

    for (; p < ep; p++) {
	if (possible == 0 || (possible == 1 && ok))
	    break;
	if (iso_detect != DETECT_ERROR) {
	    switch (*p) {
	    case WC_C_ESC:
		if (*(p+1) == WC_C_MBCS) {
		    q = p;
		    if (! wc_parse_iso2022_esc(&q, &st))
			break;
		    if (st.design[0] == WC_CCS_JIS_C_6226 ||
			st.design[0] == WC_CCS_JIS_X_0208)
			;
		    else if (st.design[0] == WC_CCS_JIS_X_0213_1 ||
			     st.design[0] == WC_CCS_JIS_X_0213_2)
			iso2022jp3 = WC_TRUE;
		    else if (WC_CCS_TYPE(st.design[0]) == WC_CCS_A_CS94W)
			iso2022jp2 = WC_TRUE;
		    if (st.design[1] == WC_CCS_KS_X_1001)
			iso2022kr = WC_TRUE;
		    else if (st.design[1] == WC_CCS_GB_2312 ||
			     st.design[1] == WC_CCS_ISO_IR_165 ||
			     st.design[1] == WC_CCS_CNS_11643_1)
			iso2022cn = WC_TRUE;
		    if (WC_CCS_TYPE(st.design[2]) == WC_CCS_A_CS94W ||
			WC_CCS_TYPE(st.design[3]) == WC_CCS_A_CS94W)
			iso2022cn = WC_TRUE;
		} else if (*(p+1) == WC_C_G2_CS96) {
		    q = p;
		    if (! wc_parse_iso2022_esc(&q, &st))
			break;
		    if (WC_CCS_TYPE(st.design[2]) == WC_CCS_A_CS96)
			iso2022jp2 = WC_TRUE;
		} else if (*(p+1) == WC_C_CSWSR) {
		    q = p;
		    if (! wc_parse_iso2022_esc(&q, &st))
			break;
		    possible = 0;
		    iso_detect = DETECT_BROKEN;
		    continue;
		}
		iso_detect = DETECT_OK;
		ok = WC_TRUE;
		break;
	    case WC_C_SI:
	    case WC_C_SO:
		iso_detect = DETECT_OK;
		ok = WC_TRUE;
		iso2022cn = WC_TRUE;
		iso2022kr = WC_TRUE;
		break;
	    default:
		if (*p & 0x80) {
		    iso_detect = DETECT_ERROR;
		    possible--;
		}
		break;
	    }
	}
	if (euc_detect != DETECT_ERROR) {
	    switch (euc_state) {
	    case WC_EUC_NOSTATE:
		switch (WC_ISO_MAP[*p]) {
		case WC_ISO_MAP_GR:
		    euc_state = WC_EUC_MBYTE1;
		    break;
		case WC_ISO_MAP_SS2:
		    if (euc == WC_CES_EUC_JP)
			euc_state = WC_EUC_MBYTE1;
		    else if (euc == WC_CES_EUC_TW)
			euc_state = WC_EUC_TW_SS2;
		    else
			euc_detect = DETECT_ERROR;
		    break;
		case WC_ISO_MAP_SS3:
		    if (euc == WC_CES_EUC_JP &&
			WC_ISO_MAP[*(p+1)] == WC_ISO_MAP_GR)
			;
		    else
			euc_detect = DETECT_ERROR;
		    break;
		case WC_ISO_MAP_C1:
		case WC_ISO_MAP_GR96:
		    euc_detect = DETECT_ERROR;
		    break;
		}
		break;
	    case WC_EUC_MBYTE1:
		if (WC_ISO_MAP[*p] == WC_ISO_MAP_GR) {
		    SET_DETECT(euc_detect, DETECT_OK);
		    ok = WC_TRUE;
		} else
		    SET_BROKEN_ERROR(euc_detect);
		euc_state = WC_EUC_NOSTATE;
		break;
	    case WC_EUC_TW_SS2:
		if (!( 0xa0 <= *p && *p <= 0xb0) ||
		    WC_ISO_MAP[*(p+1)] != WC_ISO_MAP_GR)
		    euc_detect = DETECT_ERROR;
		euc_state = WC_EUC_NOSTATE;
		break;
	    }
	    if (euc_detect == DETECT_ERROR)
		possible--;
	}
	if (sjis_detect != DETECT_ERROR) {
	    switch (sjis_state) {
	    case WC_SJIS_NOSTATE:
		switch (WC_SJIS_MAP[*p]) {
		case WC_SJIS_MAP_SL:
		case WC_SJIS_MAP_SH:
		    sjis_state = WC_SJIS_SHIFT_L;
		    break;
		case WC_SJIS_MAP_SK:
		    SET_DETECT(sjis_detect, DETECT_POSSIBLE);
		    break;
		case WC_SJIS_MAP_SX:
		    if (WcOption.use_jisx0213) {
			sjis_state = WC_SJIS_SHIFT_X;
			break;
		    }
		case WC_SJIS_MAP_80:
		case WC_SJIS_MAP_A0:
		case WC_SJIS_MAP_C1:
		    sjis_detect = DETECT_ERROR;
		    break;
		}
		break;
	    case WC_SJIS_SHIFT_L:
		if (WC_SJIS_MAP[*p] & WC_SJIS_MAP_LB) {
		    SET_DETECT(sjis_detect, DETECT_OK);
		    ok = WC_TRUE;
		} else
		    SET_BROKEN_ERROR(sjis_detect);
		sjis_state = WC_SJIS_NOSTATE;
		break;
	    case WC_SJIS_SHIFT_X:
		if (WC_SJIS_MAP[*p] & WC_SJIS_MAP_LB)
		    SET_DETECT(sjis_detect, DETECT_POSSIBLE);
		else
		    sjis_detect = DETECT_ERROR;
		sjis_state = WC_SJIS_NOSTATE;
		break;
	    }
	    if (sjis_detect == DETECT_ERROR)
		possible--;
	}
	if (big5_detect != DETECT_ERROR) {
	    switch (big5_state) {
	    case WC_BIG5_NOSTATE:
		switch (WC_BIG5_MAP[*p]) {
		case WC_BIG5_MAP_UB:
		    big5_state = WC_BIG5_MBYTE1;
		    break;
		case WC_BIG5_MAP_C1:
		    big5_detect = DETECT_ERROR;
		    break;
		}
		break;
	    case WC_BIG5_MBYTE1:
		if (WC_BIG5_MAP[*p] & WC_BIG5_MAP_LB) {
		    SET_DETECT(big5_detect, DETECT_OK);
		    ok = WC_TRUE;
		} else
		    SET_BROKEN_ERROR(big5_detect);
		big5_state = WC_BIG5_NOSTATE;
		break;
	    }
	    if (big5_detect == DETECT_ERROR)
		possible--;
	}
	if (hz_detect != DETECT_ERROR) {
	  if (*p & 0x80) {
		hz_detect = DETECT_ERROR;
		possible--;
	  } else {
	    switch (hz_state) {
	    case WC_HZ_NOSTATE:
		if (*p == WC_C_HZ_TILDA)
		    hz_state = WC_HZ_TILDA;
		break;
	    case WC_HZ_TILDA:
		if (*p == WC_C_HZ_SI)
		    hz_state = WC_HZ_MBYTE;
		else
		    hz_state = WC_HZ_NOSTATE;
		break;
	    case WC_HZ_TILDA_MB:
		if (*p == WC_C_HZ_SO)
		    hz_state = WC_HZ_NOSTATE;
		else
		    hz_state = WC_HZ_MBYTE;
		break;
	    case WC_HZ_MBYTE:
		if (*p == WC_C_HZ_TILDA)
		    hz_state = WC_HZ_TILDA_MB;
		else
		    hz_state = WC_HZ_MBYTE1;
		break;
	    case WC_HZ_MBYTE1:
		hz_detect = DETECT_OK;
		ok = WC_TRUE;
		hz_state = WC_HZ_NOSTATE;
		break;
	    }
	  }
	}
	if (latin_detect != DETECT_ERROR) {
	    switch (WC_ISO_MAP[*p] & WC_ISO_MAP_CG) {
	    case WC_ISO_MAP_GR:
	    case WC_ISO_MAP_GR96:
		SET_DETECT(latin_detect, DETECT_OK);
		ok = WC_TRUE;
		break;
	    case WC_ISO_MAP_C1:
		latin_detect = DETECT_ERROR;
		break;
	    }
	    if (latin_detect == DETECT_ERROR)
		possible--;
	}
	if (priv_detect != DETECT_ERROR) {
	    if (*p != WC_C_ESC && WC_DETECT_MAP[*p]) {
		SET_DETECT(priv_detect, DETECT_OK);
		ok = WC_TRUE;
	    }
/*
	    if (priv_detect == DETECT_ERROR)
		possible--;
*/
	}
#ifdef USE_UNICODE
	if (utf8_detect != DETECT_ERROR) {
	    switch (utf8_state) {
	    case WC_UTF8_NOSTATE:
		switch (utf8_next = WC_UTF8_MAP[*p]) {
		case 1:
		case 8:
		    break;
		case 0:
		case 7:
		    utf8_detect = DETECT_ERROR;
		    break;
		default:
		    utf8_next--;
		    utf8_state = WC_UTF8_NEXT;
		    break;
		}
		break;
	    case WC_UTF8_NEXT:
		if (WC_UTF8_MAP[*p]) {
		    utf8_detect = DETECT_ERROR;
		    utf8_state = WC_UTF8_NOSTATE;
		    break;
		}
		utf8_next--;
		if (! utf8_next) {
		    SET_DETECT(utf8_detect, DETECT_OK);
		    ok = WC_TRUE;
		    utf8_state = WC_UTF8_NOSTATE;
		}
		break;
	    }
	    if (utf8_detect == DETECT_ERROR)
		possible--;
	}
#endif
    }

    if (iso_detect != DETECT_ERROR) {
	if (iso_detect == DETECT_NORMAL) {
	   if (hz_detect == DETECT_OK)
		return WC_CES_HZ_GB_2312;
	   if (priv_detect == DETECT_OK)
		return priv;
	   return WC_CES_US_ASCII;
	}
	switch (euc) {
	case WC_CES_EUC_CN:
	case WC_CES_EUC_TW:
	    if (iso2022cn)
		return WC_CES_ISO_2022_CN;
	    break;
	case WC_CES_EUC_KR:
	    if (iso2022kr)
		return WC_CES_ISO_2022_KR;
	    break;
	}
	if (iso2022jp3)
	    return WC_CES_ISO_2022_JP_3;
	if (iso2022jp2)
	    return WC_CES_ISO_2022_JP_2;
	if (iso2022cn)
	    return WC_CES_ISO_2022_CN;
	if (iso2022kr)
	    return WC_CES_ISO_2022_KR;
	return WC_CES_ISO_2022_JP;
    }
    switch (hint) {
    case WC_CES_ISO_2022_JP:
    case WC_CES_ISO_2022_JP_2:
    case WC_CES_ISO_2022_JP_3:
    case WC_CES_ISO_2022_KR:
    case WC_CES_ISO_2022_CN:
	break;
    case WC_CES_EUC_JP:
    case WC_CES_EUC_CN:
    case WC_CES_EUC_TW:
    case WC_CES_EUC_KR:
	if (euc_detect != DETECT_ERROR)
	    return hint;
	break;
    case WC_CES_SHIFT_JIS:
    case WC_CES_SHIFT_JISX0213:
	if (sjis_detect != DETECT_ERROR)
	    return hint;
	break;
    case WC_CES_BIG5:
	if (big5_detect != DETECT_ERROR)
	    return hint;
	break;
#ifdef USE_UNICODE
    case WC_CES_UTF_8:
	return hint;
#endif
    case WC_CES_US_ASCII:
#ifdef USE_UNICODE
	if (utf8_detect != DETECT_ERROR)
	    return hint;
#endif
	if (latin_detect != DETECT_ERROR)
	    return WC_CES_ISO_8859_1;
	return hint;
    default:
	if (latin_detect != DETECT_ERROR)
	    return hint;
	if (priv_detect != DETECT_ERROR)
	    return hint;
#ifdef USE_UNICODE
	if (utf8_detect != DETECT_ERROR)
	    return WC_CES_UTF_8;
#endif
	return hint;
    }
    if (euc_detect == DETECT_OK)
	return euc;
    if (sjis_detect == DETECT_OK)
	return WC_CES_SHIFT_JIS;
    if (big5_detect == DETECT_OK)
	return WC_CES_BIG5;
#ifdef USE_UNICODE
    if (utf8_detect == DETECT_OK)
	return WC_CES_UTF_8;
    if (sjis_detect & DETECT_POSSIBLE)
	return WC_CES_SHIFT_JIS;
#endif
    if (euc_detect != DETECT_ERROR)
	return euc;
    if (sjis_detect != DETECT_ERROR)
	return WC_CES_SHIFT_JIS;
    if (big5_detect != DETECT_ERROR)
	return WC_CES_BIG5;
#ifdef USE_UNICODE
    if (utf8_detect != DETECT_ERROR)
	return WC_CES_UTF_8;
#endif
    return hint;
}