comparison src/libguess/guess.c @ 2313:3149d4b1a9a9 trunk

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