Mercurial > audlegacy
annotate src/libguess/cjk_impl.c @ 3655:047829635612
- -rpath will be automatically set up when --prefix or --exec-prefix is specified. it can be forced to be disabled through --disable-rpath.
- relink audacious if libguess.a is relinked. (do not drop this dependency! thanks.)
author | Yoshiki Yazawa <yaz@cc.rim.or.jp> |
---|---|
date | Thu, 27 Sep 2007 23:27:30 +0900 |
parents | 9418f74acdb7 |
children | 92c12d945d99 |
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 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
42 /* 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
|
43 /* ORDER_** &highest, &second, ... &lowest */ |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
44 #define ORDER_JP &utf8, &sjis, &eucj |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
45 #define ORDER_TW &utf8, &big5 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
46 #define ORDER_CN &utf8, &gb2312, &gb18030 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
47 #define ORDER_KR &utf8, &euck, &johab |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
48 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
49 /* 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
|
50 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
|
51 #define WITH_G_CONVERT 1 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
52 /* #undef WITH_G_CONVERT */ |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
53 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
54 #ifdef WITH_G_CONVERT |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
55 const char UCS_2BE[] = "UTF-16"; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
56 const char UCS_2LE[] = "UTF-16"; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
57 #else |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
58 const char UCS_2BE[] = "UCS-2BE"; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
59 const char UCS_2LE[] = "UCS-2LE"; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
60 #endif |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
61 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
62 /* data types */ |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
63 typedef struct guess_arc_rec |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
64 { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
65 unsigned int next; /* next state */ |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
66 double score; /* score */ |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
67 } guess_arc; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
68 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
69 typedef struct guess_dfa_rec |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
70 { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
71 signed char (*states)[256]; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
72 guess_arc *arcs; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
73 int state; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
74 double score; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
75 } guess_dfa; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
76 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
77 /* macros */ |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
78 #define DFA_INIT(st, ar) \ |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
79 { st, ar, 0, 1.0 } |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
80 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
81 #define DFA_NEXT(dfa, ch) \ |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
82 do { \ |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
83 int arc__; \ |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
84 if (dfa.state >= 0) { \ |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
85 arc__ = dfa.states[dfa.state][ch]; \ |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
86 if (arc__ < 0) { \ |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
87 dfa.state = -1; \ |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
88 } else { \ |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
89 dfa.state = dfa.arcs[arc__].next; \ |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
90 dfa.score *= dfa.arcs[arc__].score; \ |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
91 } \ |
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 } while (0) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
94 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
95 #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
|
96 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
97 /* 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
|
98 #include "guess_tab.c" |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
99 |
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 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
|
102 { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
103 int i; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
104 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
|
105 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
106 for (i = 0; i < buflen; i++) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
107 int c = (unsigned char) buf[i]; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
108 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
109 if (DFA_ALIVE(utf8)) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
110 DFA_NEXT(utf8, c); |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
111 else |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
112 break; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
113 } |
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 if(DFA_ALIVE(utf8)) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
116 return 1; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
117 else |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
118 return 0; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
119 } |
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 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
|
122 { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
123 int i; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
124 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
|
125 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
|
126 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
|
127 guess_dfa *top = NULL; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
128 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
129 guess_dfa *order[] = { ORDER_JP, NULL }; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
130 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
131 for (i = 0; i < buflen; i++) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
132 int c = (unsigned char) buf[i]; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
133 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
134 /* 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
|
135 if (c == 0x1b) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
136 if (i < buflen - 1) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
137 c = (unsigned char) buf[++i]; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
138 if (c == '$' || c == '(') |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
139 return "ISO-2022-JP"; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
140 } |
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 /* special treatment of BOM */ |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
144 if (i == 0 && c == 0xff) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
145 if (i < buflen - 1) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
146 c = (unsigned char) buf[i + 1]; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
147 if (c == 0xfe) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
148 return UCS_2LE; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
149 } |
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 if (i == 0 && c == 0xfe) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
152 if (i < buflen - 1) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
153 c = (unsigned char) buf[i + 1]; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
154 if (c == 0xff) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
155 return UCS_2BE; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
156 } |
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 if (DFA_ALIVE(eucj)) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
160 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
|
161 return "EUC-JP"; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
162 DFA_NEXT(eucj, c); |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
163 } |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
164 if (DFA_ALIVE(sjis)) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
165 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
|
166 return "SJIS"; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
167 DFA_NEXT(sjis, c); |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
168 } |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
169 if (DFA_ALIVE(utf8)) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
170 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
|
171 return "UTF-8"; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
172 DFA_NEXT(utf8, c); |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
173 } |
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 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
|
176 /* we ran out the possibilities */ |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
177 return NULL; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
178 } |
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 /* 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
|
182 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
|
183 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
|
184 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
|
185 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
|
186 top = order[i]; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
187 } |
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 if (top == &eucj) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
191 return "EUC-JP"; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
192 if (top == &utf8) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
193 return "UTF-8"; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
194 if (top == &sjis) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
195 return "SJIS"; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
196 return NULL; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
197 } |
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 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
|
200 { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
201 int i; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
202 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
|
203 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
|
204 guess_dfa *top = NULL; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
205 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
206 guess_dfa *order[] = { ORDER_TW, NULL }; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
207 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
208 for (i = 0; i < buflen; i++) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
209 int c = (unsigned char) buf[i]; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
210 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
211 /* 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
|
212 if (c == 0x1b) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
213 if (i < buflen - 1) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
214 c = (unsigned char) buf[++i]; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
215 if (c == '$' || c == '(') |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
216 return "ISO-2022-TW"; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
217 } |
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 /* special treatment of BOM */ |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
221 if (i == 0 && c == 0xff) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
222 if (i < buflen - 1) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
223 c = (unsigned char) buf[i + 1]; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
224 if (c == 0xfe) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
225 return UCS_2LE; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
226 } |
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 if (i == 0 && c == 0xfe) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
229 if (i < buflen - 1) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
230 c = (unsigned char) buf[i + 1]; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
231 if (c == 0xff) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
232 return UCS_2BE; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
233 } |
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 if (DFA_ALIVE(big5)) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
237 if (!DFA_ALIVE(utf8)) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
238 return "BIG5"; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
239 DFA_NEXT(big5, c); |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
240 } |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
241 if (DFA_ALIVE(utf8)) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
242 if (!DFA_ALIVE(big5)) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
243 return "UTF-8"; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
244 DFA_NEXT(utf8, c); |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
245 } |
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 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
|
248 /* we ran out the possibilities */ |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
249 return NULL; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
250 } |
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 /* 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
|
254 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
|
255 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
|
256 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
|
257 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
|
258 top = order[i]; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
259 } |
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 if (top == &big5) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
263 return "BIG5"; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
264 if (top == &utf8) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
265 return "UTF-8"; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
266 return NULL; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
267 } |
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 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
|
270 { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
271 int i; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
272 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
|
273 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
|
274 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
|
275 guess_dfa *top = NULL; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
276 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
277 guess_dfa *order[] = { ORDER_CN, NULL }; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
278 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
279 for (i = 0; i < buflen; i++) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
280 int c = (unsigned char) buf[i]; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
281 int c2; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
282 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
283 /* 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
|
284 if (c == 0x1b) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
285 if (i < buflen - 1) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
286 c = (unsigned char) buf[i + 1]; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
287 c2 = (unsigned char) buf[i + 2]; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
288 if (c == '$' && (c2 == ')' || c2 == '+')) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
289 return "ISO-2022-CN"; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
290 } |
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 /* special treatment of BOM */ |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
294 if (i == 0 && c == 0xff) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
295 if (i < buflen - 1) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
296 c = (unsigned char) buf[i + 1]; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
297 if (c == 0xfe) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
298 return UCS_2LE; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
299 } |
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 if (i == 0 && c == 0xfe) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
302 if (i < buflen - 1) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
303 c = (unsigned char) buf[i + 1]; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
304 if (c == 0xff) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
305 return UCS_2BE; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
306 } |
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 if (DFA_ALIVE(gb2312)) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
310 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
|
311 return "GB2312"; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
312 DFA_NEXT(gb2312, c); |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
313 } |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
314 if (DFA_ALIVE(utf8)) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
315 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
|
316 return "UTF-8"; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
317 DFA_NEXT(utf8, c); |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
318 } |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
319 if (DFA_ALIVE(gb18030)) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
320 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
|
321 return "GB18030"; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
322 DFA_NEXT(gb18030, c); |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
323 } |
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 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
|
326 /* we ran out the possibilities */ |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
327 return NULL; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
328 } |
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 /* 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
|
332 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
|
333 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
|
334 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
|
335 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
|
336 top = order[i]; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
337 } |
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 if (top == &gb2312) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
341 return "GB2312"; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
342 if (top == &utf8) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
343 return "UTF-8"; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
344 if (top == &gb18030) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
345 return "GB18030"; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
346 return NULL; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
347 } |
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 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
|
350 { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
351 int i; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
352 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
|
353 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
|
354 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
|
355 guess_dfa *top = NULL; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
356 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
357 guess_dfa *order[] = { ORDER_KR, NULL }; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
358 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
359 for (i = 0; i < buflen; i++) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
360 int c = (unsigned char) buf[i]; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
361 int c2; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
362 |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
363 /* 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
|
364 if (c == 0x1b) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
365 if (i < buflen - 1) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
366 c = (unsigned char) buf[i + 1]; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
367 c2 = (unsigned char) buf[i + 2]; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
368 if (c == '$' && c2 == ')') |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
369 return "ISO-2022-KR"; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
370 } |
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 /* special treatment of BOM */ |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
374 if (i == 0 && c == 0xff) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
375 if (i < buflen - 1) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
376 c = (unsigned char) buf[i + 1]; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
377 if (c == 0xfe) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
378 return UCS_2LE; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
379 } |
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 if (i == 0 && c == 0xfe) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
382 if (i < buflen - 1) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
383 c = (unsigned char) buf[i + 1]; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
384 if (c == 0xff) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
385 return UCS_2BE; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
386 } |
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 if (DFA_ALIVE(euck)) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
390 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
|
391 return "EUC-KR"; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
392 DFA_NEXT(euck, c); |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
393 } |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
394 if (DFA_ALIVE(johab)) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
395 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
|
396 return "JOHAB"; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
397 DFA_NEXT(johab, c); |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
398 } |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
399 if (DFA_ALIVE(utf8)) { |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
400 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
|
401 return "UTF-8"; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
402 DFA_NEXT(utf8, c); |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
403 } |
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 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
|
406 /* we ran out the possibilities */ |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
407 return NULL; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
408 } |
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 /* 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
|
412 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
|
413 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
|
414 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
|
415 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
|
416 top = order[i]; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
417 } |
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 if (top == &euck) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
421 return "EUC-KR"; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
422 if (top == &utf8) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
423 return "UTF-8"; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
424 if (top == &johab) |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
425 return "JOHAB"; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
426 return NULL; |
1b9251ab3655
Split off CJK implementation from guess.c logic.
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
427 } |