annotate libguess/guess.c @ 1105:4be4d74db123 trunk

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