Mercurial > audlegacy
annotate src/libguess/cjk_impl.c @ 3497:6f34cc4217b9 trunk
Added "date" field to basic fields.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Wed, 05 Sep 2007 10:09:11 +0300 |
parents | 1b9251ab3655 |
children | 9418f74acdb7 |
rev | line source |
---|---|
3213
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
1 /* |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
2 * This code is derivative of guess.c of Gauche-0.8.3. |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
3 * The following is the original copyright notice. |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
4 */ |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
5 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
6 /* |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
7 * guess.c - guessing character encoding |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
8 * |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
9 * Copyright (c) 2000-2003 Shiro Kawai, All rights reserved. |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
10 * |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
11 * Redistribution and use in source and binary forms, with or without |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
12 * modification, are permitted provided that the following conditions |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
13 * are met: |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
14 * |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
15 * 1. Redistributions of source code must retain the above copyright |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
16 * notice, this list of conditions and the following disclaimer. |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
17 * |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
18 * 2. Redistributions in binary form must reproduce the above copyright |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
19 * notice, this list of conditions and the following disclaimer in the |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
20 * documentation and/or other materials provided with the distribution. |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
21 * |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
22 * 3. Neither the name of the authors nor the names of its contributors |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
23 * may be used to endorse or promote products derived from this |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
24 * software without specific prior written permission. |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
25 * |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
27 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
28 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
29 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
30 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
31 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
32 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
33 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
34 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
35 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
36 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
37 * |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
38 */ |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
39 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
40 #include "libguess.h" |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
41 #define NULL ((void *)0) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
42 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
43 /* take precedence if scores are same. you can customize the order as: */ |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
44 /* ORDER_** &highest, &second, ... &lowest */ |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
45 #define ORDER_JP &utf8, &sjis, &eucj |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
46 #define ORDER_TW &utf8, &big5 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
47 #define ORDER_CN &utf8, &gb2312, &gb18030 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
48 #define ORDER_KR &utf8, &euck, &johab |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
49 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
50 /* workaround for that glib's g_convert can't convert |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
51 properly from UCS-2BE/LE trailing after BOM. */ |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
52 #define WITH_G_CONVERT 1 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
53 /* #undef WITH_G_CONVERT */ |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
54 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
55 #ifdef WITH_G_CONVERT |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
56 const char UCS_2BE[] = "UTF-16"; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
57 const char UCS_2LE[] = "UTF-16"; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
58 #else |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
59 const char UCS_2BE[] = "UCS-2BE"; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
60 const char UCS_2LE[] = "UCS-2LE"; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
61 #endif |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
62 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
63 /* data types */ |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
64 typedef struct guess_arc_rec |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
65 { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
66 unsigned int next; /* next state */ |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
67 double score; /* score */ |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
68 } guess_arc; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
69 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
70 typedef struct guess_dfa_rec |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
71 { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
72 signed char (*states)[256]; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
73 guess_arc *arcs; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
74 int state; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
75 double score; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
76 } guess_dfa; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
77 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
78 /* macros */ |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
79 #define DFA_INIT(st, ar) \ |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
80 { st, ar, 0, 1.0 } |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
81 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
82 #define DFA_NEXT(dfa, ch) \ |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
83 do { \ |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
84 int arc__; \ |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
85 if (dfa.state >= 0) { \ |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
86 arc__ = dfa.states[dfa.state][ch]; \ |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
87 if (arc__ < 0) { \ |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
88 dfa.state = -1; \ |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
89 } else { \ |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
90 dfa.state = dfa.arcs[arc__].next; \ |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
91 dfa.score *= dfa.arcs[arc__].score; \ |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
92 } \ |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
93 } \ |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
94 } while (0) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
95 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
96 #define DFA_ALIVE(dfa) (dfa.state >= 0) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
97 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
98 /* include DFA table generated by guess.scm */ |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
99 #include "guess_tab.c" |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
100 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
101 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
102 int dfa_validate_utf8(const char *buf, int buflen) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
103 { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
104 int i; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
105 guess_dfa utf8 = DFA_INIT(guess_utf8_st, guess_utf8_ar); |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
106 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
107 for (i = 0; i < buflen; i++) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
108 int c = (unsigned char) buf[i]; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
109 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
110 if (DFA_ALIVE(utf8)) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
111 DFA_NEXT(utf8, c); |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
112 else |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
113 break; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
114 } |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
115 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
116 if(DFA_ALIVE(utf8)) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
117 return 1; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
118 else |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
119 return 0; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
120 } |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
121 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
122 const char *guess_jp(const char *buf, int buflen) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
123 { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
124 int i; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
125 guess_dfa eucj = DFA_INIT(guess_eucj_st, guess_eucj_ar); |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
126 guess_dfa sjis = DFA_INIT(guess_sjis_st, guess_sjis_ar); |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
127 guess_dfa utf8 = DFA_INIT(guess_utf8_st, guess_utf8_ar); |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
128 guess_dfa *top = NULL; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
129 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
130 guess_dfa *order[] = { ORDER_JP, NULL }; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
131 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
132 for (i = 0; i < buflen; i++) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
133 int c = (unsigned char) buf[i]; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
134 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
135 /* special treatment of iso-2022 escape sequence */ |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
136 if (c == 0x1b) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
137 if (i < buflen - 1) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
138 c = (unsigned char) buf[++i]; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
139 if (c == '$' || c == '(') |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
140 return "ISO-2022-JP"; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
141 } |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
142 } |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
143 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
144 /* special treatment of BOM */ |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
145 if (i == 0 && c == 0xff) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
146 if (i < buflen - 1) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
147 c = (unsigned char) buf[i + 1]; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
148 if (c == 0xfe) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
149 return UCS_2LE; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
150 } |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
151 } |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
152 if (i == 0 && c == 0xfe) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
153 if (i < buflen - 1) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
154 c = (unsigned char) buf[i + 1]; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
155 if (c == 0xff) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
156 return UCS_2BE; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
157 } |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
158 } |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
159 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
160 if (DFA_ALIVE(eucj)) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
161 if (!DFA_ALIVE(sjis) && !DFA_ALIVE(utf8)) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
162 return "EUC-JP"; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
163 DFA_NEXT(eucj, c); |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
164 } |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
165 if (DFA_ALIVE(sjis)) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
166 if (!DFA_ALIVE(eucj) && !DFA_ALIVE(utf8)) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
167 return "SJIS"; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
168 DFA_NEXT(sjis, c); |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
169 } |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
170 if (DFA_ALIVE(utf8)) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
171 if (!DFA_ALIVE(sjis) && !DFA_ALIVE(eucj)) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
172 return "UTF-8"; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
173 DFA_NEXT(utf8, c); |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
174 } |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
175 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
176 if (!DFA_ALIVE(eucj) && !DFA_ALIVE(sjis) && !DFA_ALIVE(utf8)) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
177 /* we ran out the possibilities */ |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
178 return NULL; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
179 } |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
180 } |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
181 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
182 /* Now, we have ambigous code. Pick the highest score. If more than |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
183 one candidate tie, pick the default encoding. */ |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
184 for (i = 0; order[i] != NULL; i++) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
185 if (order[i]->state >= 0) { //DFA_ALIVE() |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
186 if (top == NULL || order[i]->score > top->score) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
187 top = order[i]; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
188 } |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
189 } |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
190 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
191 if (top == &eucj) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
192 return "EUC-JP"; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
193 if (top == &utf8) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
194 return "UTF-8"; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
195 if (top == &sjis) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
196 return "SJIS"; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
197 return NULL; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
198 } |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
199 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
200 const char *guess_tw(const char *buf, int buflen) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
201 { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
202 int i; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
203 guess_dfa big5 = DFA_INIT(guess_big5_st, guess_big5_ar); |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
204 guess_dfa utf8 = DFA_INIT(guess_utf8_st, guess_utf8_ar); |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
205 guess_dfa *top = NULL; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
206 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
207 guess_dfa *order[] = { ORDER_TW, NULL }; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
208 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
209 for (i = 0; i < buflen; i++) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
210 int c = (unsigned char) buf[i]; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
211 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
212 /* special treatment of iso-2022 escape sequence */ |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
213 if (c == 0x1b) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
214 if (i < buflen - 1) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
215 c = (unsigned char) buf[++i]; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
216 if (c == '$' || c == '(') |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
217 return "ISO-2022-TW"; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
218 } |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
219 } |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
220 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
221 /* special treatment of BOM */ |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
222 if (i == 0 && c == 0xff) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
223 if (i < buflen - 1) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
224 c = (unsigned char) buf[i + 1]; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
225 if (c == 0xfe) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
226 return UCS_2LE; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
227 } |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
228 } |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
229 if (i == 0 && c == 0xfe) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
230 if (i < buflen - 1) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
231 c = (unsigned char) buf[i + 1]; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
232 if (c == 0xff) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
233 return UCS_2BE; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
234 } |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
235 } |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
236 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
237 if (DFA_ALIVE(big5)) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
238 if (!DFA_ALIVE(utf8)) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
239 return "BIG5"; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
240 DFA_NEXT(big5, c); |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
241 } |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
242 if (DFA_ALIVE(utf8)) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
243 if (!DFA_ALIVE(big5)) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
244 return "UTF-8"; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
245 DFA_NEXT(utf8, c); |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
246 } |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
247 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
248 if (!DFA_ALIVE(big5) && !DFA_ALIVE(utf8)) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
249 /* we ran out the possibilities */ |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
250 return NULL; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
251 } |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
252 } |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
253 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
254 /* Now, we have ambigous code. Pick the highest score. If more than |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
255 one candidate tie, pick the default encoding. */ |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
256 for (i = 0; order[i] != NULL; i++) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
257 if (order[i]->state >= 0) { //DFA_ALIVE() |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
258 if (top == NULL || order[i]->score > top->score) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
259 top = order[i]; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
260 } |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
261 } |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
262 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
263 if (top == &big5) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
264 return "BIG5"; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
265 if (top == &utf8) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
266 return "UTF-8"; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
267 return NULL; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
268 } |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
269 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
270 const char *guess_cn(const char *buf, int buflen) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
271 { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
272 int i; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
273 guess_dfa gb2312 = DFA_INIT(guess_gb2312_st, guess_gb2312_ar); |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
274 guess_dfa utf8 = DFA_INIT(guess_utf8_st, guess_utf8_ar); |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
275 guess_dfa gb18030 = DFA_INIT(guess_gb18030_st, guess_gb18030_ar); |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
276 guess_dfa *top = NULL; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
277 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
278 guess_dfa *order[] = { ORDER_CN, NULL }; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
279 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
280 for (i = 0; i < buflen; i++) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
281 int c = (unsigned char) buf[i]; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
282 int c2; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
283 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
284 /* special treatment of iso-2022 escape sequence */ |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
285 if (c == 0x1b) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
286 if (i < buflen - 1) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
287 c = (unsigned char) buf[i + 1]; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
288 c2 = (unsigned char) buf[i + 2]; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
289 if (c == '$' && (c2 == ')' || c2 == '+')) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
290 return "ISO-2022-CN"; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
291 } |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
292 } |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
293 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
294 /* special treatment of BOM */ |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
295 if (i == 0 && c == 0xff) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
296 if (i < buflen - 1) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
297 c = (unsigned char) buf[i + 1]; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
298 if (c == 0xfe) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
299 return UCS_2LE; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
300 } |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
301 } |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
302 if (i == 0 && c == 0xfe) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
303 if (i < buflen - 1) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
304 c = (unsigned char) buf[i + 1]; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
305 if (c == 0xff) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
306 return UCS_2BE; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
307 } |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
308 } |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
309 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
310 if (DFA_ALIVE(gb2312)) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
311 if (!DFA_ALIVE(utf8) && !DFA_ALIVE(gb18030)) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
312 return "GB2312"; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
313 DFA_NEXT(gb2312, c); |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
314 } |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
315 if (DFA_ALIVE(utf8)) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
316 if (!DFA_ALIVE(gb2312) && !DFA_ALIVE(gb18030)) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
317 return "UTF-8"; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
318 DFA_NEXT(utf8, c); |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
319 } |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
320 if (DFA_ALIVE(gb18030)) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
321 if (!DFA_ALIVE(utf8) && !DFA_ALIVE(gb2312)) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
322 return "GB18030"; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
323 DFA_NEXT(gb18030, c); |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
324 } |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
325 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
326 if (!DFA_ALIVE(gb2312) && !DFA_ALIVE(utf8) && !DFA_ALIVE(gb18030)) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
327 /* we ran out the possibilities */ |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
328 return NULL; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
329 } |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
330 } |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
331 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
332 /* Now, we have ambigous code. Pick the highest score. If more than |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
333 one candidate tie, pick the default encoding. */ |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
334 for (i = 0; order[i] != NULL; i++) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
335 if (order[i]->state >= 0) { //DFA_ALIVE() |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
336 if (top == NULL || order[i]->score > top->score) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
337 top = order[i]; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
338 } |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
339 } |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
340 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
341 if (top == &gb2312) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
342 return "GB2312"; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
343 if (top == &utf8) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
344 return "UTF-8"; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
345 if (top == &gb18030) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
346 return "GB18030"; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
347 return NULL; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
348 } |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
349 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
350 const char *guess_kr(const char *buf, int buflen) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
351 { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
352 int i; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
353 guess_dfa euck = DFA_INIT(guess_euck_st, guess_euck_ar); |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
354 guess_dfa utf8 = DFA_INIT(guess_utf8_st, guess_utf8_ar); |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
355 guess_dfa johab = DFA_INIT(guess_johab_st, guess_johab_ar); |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
356 guess_dfa *top = NULL; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
357 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
358 guess_dfa *order[] = { ORDER_KR, NULL }; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
359 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
360 for (i = 0; i < buflen; i++) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
361 int c = (unsigned char) buf[i]; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
362 int c2; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
363 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
364 /* special treatment of iso-2022 escape sequence */ |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
365 if (c == 0x1b) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
366 if (i < buflen - 1) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
367 c = (unsigned char) buf[i + 1]; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
368 c2 = (unsigned char) buf[i + 2]; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
369 if (c == '$' && c2 == ')') |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
370 return "ISO-2022-KR"; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
371 } |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
372 } |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
373 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
374 /* special treatment of BOM */ |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
375 if (i == 0 && c == 0xff) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
376 if (i < buflen - 1) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
377 c = (unsigned char) buf[i + 1]; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
378 if (c == 0xfe) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
379 return UCS_2LE; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
380 } |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
381 } |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
382 if (i == 0 && c == 0xfe) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
383 if (i < buflen - 1) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
384 c = (unsigned char) buf[i + 1]; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
385 if (c == 0xff) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
386 return UCS_2BE; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
387 } |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
388 } |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
389 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
390 if (DFA_ALIVE(euck)) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
391 if (!DFA_ALIVE(johab) && !DFA_ALIVE(utf8)) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
392 return "EUC-KR"; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
393 DFA_NEXT(euck, c); |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
394 } |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
395 if (DFA_ALIVE(johab)) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
396 if (!DFA_ALIVE(euck) && !DFA_ALIVE(utf8)) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
397 return "JOHAB"; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
398 DFA_NEXT(johab, c); |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
399 } |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
400 if (DFA_ALIVE(utf8)) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
401 if (!DFA_ALIVE(euck) && !DFA_ALIVE(johab)) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
402 return "UTF-8"; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
403 DFA_NEXT(utf8, c); |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
404 } |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
405 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
406 if (!DFA_ALIVE(euck) && !DFA_ALIVE(johab) && !DFA_ALIVE(utf8)) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
407 /* we ran out the possibilities */ |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
408 return NULL; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
409 } |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
410 } |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
411 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
412 /* Now, we have ambigous code. Pick the highest score. If more than |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
413 one candidate tie, pick the default encoding. */ |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
414 for (i = 0; order[i] != NULL; i++) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
415 if (order[i]->state >= 0) { //DFA_ALIVE() |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
416 if (top == NULL || order[i]->score > top->score) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
417 top = order[i]; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
418 } |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
419 } |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
420 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
421 if (top == &euck) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
422 return "EUC-KR"; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
423 if (top == &utf8) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
424 return "UTF-8"; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
425 if (top == &johab) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
426 return "JOHAB"; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
427 return NULL; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
428 } |