CharacterEncodingDetector.cpp revision 34fb29696b0f3abf61b10f8d053b1f33d501de0a
1db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin/*
2db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin * Copyright (C) 2013 The Android Open Source Project
3db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin *
4db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin * Licensed under the Apache License, Version 2.0 (the "License");
5db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin * you may not use this file except in compliance with the License.
6db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin * You may obtain a copy of the License at
7db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin *
8db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin *      http://www.apache.org/licenses/LICENSE-2.0
9db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin *
10db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin * Unless required by applicable law or agreed to in writing, software
11db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin * distributed under the License is distributed on an "AS IS" BASIS,
12db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin * See the License for the specific language governing permissions and
14db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin * limitations under the License.
15db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin */
16db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin
17db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin//#define LOG_NDEBUG 0
18db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin#define LOG_TAG "CharacterEncodingDector"
19db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin#include <utils/Log.h>
20bdf366cc70639b0e16b8f84eebe612a48a8b8b06Igor Murashkin
21db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin#include "CharacterEncodingDetector.h"
221e854c5fc817d31eef18e671b9b519bbb4b400d2Igor Murashkin#include "CharacterEncodingDetectorTables.h"
23db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin
24db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin#include "utils/Vector.h"
25db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin#include "StringArray.h"
26db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin
27db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin#include "unicode/ucnv.h"
28db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin#include "unicode/ucsdet.h"
29db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin#include "unicode/ustring.h"
30db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin
31db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkinnamespace android {
32db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin
33db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor MurashkinCharacterEncodingDetector::CharacterEncodingDetector() {
34db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin
35db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin    UErrorCode status = U_ZERO_ERROR;
36db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin    mUtf8Conv = ucnv_open("UTF-8", &status);
37db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin    if (U_FAILURE(status)) {
38db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin        ALOGE("could not create UConverter for UTF-8");
39db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin        mUtf8Conv = NULL;
40db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin    }
41db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin}
42db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin
43db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor MurashkinCharacterEncodingDetector::~CharacterEncodingDetector() {
44db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin    ucnv_close(mUtf8Conv);
45db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin}
46db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin
47db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkinvoid CharacterEncodingDetector::addTag(const char *name, const char *value) {
48fd887436bd111e4d2c7307578a51b5070025b7f2Eino-Ville Talvala    mNames.push_back(name);
49db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin    mValues.push_back(value);
50db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin}
51db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin
521e854c5fc817d31eef18e671b9b519bbb4b400d2Igor Murashkinsize_t CharacterEncodingDetector::size() {
531e854c5fc817d31eef18e671b9b519bbb4b400d2Igor Murashkin    return mNames.size();
54db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin}
551e854c5fc817d31eef18e671b9b519bbb4b400d2Igor Murashkin
561e854c5fc817d31eef18e671b9b519bbb4b400d2Igor Murashkinstatus_t CharacterEncodingDetector::getTag(int index, const char **name, const char**value) {
571e854c5fc817d31eef18e671b9b519bbb4b400d2Igor Murashkin    if (index >= mNames.size()) {
58db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin        return BAD_VALUE;
59db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin    }
60bdf366cc70639b0e16b8f84eebe612a48a8b8b06Igor Murashkin
611e854c5fc817d31eef18e671b9b519bbb4b400d2Igor Murashkin    *name = mNames.getEntry(index);
62bdf366cc70639b0e16b8f84eebe612a48a8b8b06Igor Murashkin    *value = mValues.getEntry(index);
631e854c5fc817d31eef18e671b9b519bbb4b400d2Igor Murashkin    return OK;
641e854c5fc817d31eef18e671b9b519bbb4b400d2Igor Murashkin}
651e854c5fc817d31eef18e671b9b519bbb4b400d2Igor Murashkin
661e854c5fc817d31eef18e671b9b519bbb4b400d2Igor Murashkinstatic bool isPrintableAscii(const char *value, size_t len) {
671e854c5fc817d31eef18e671b9b519bbb4b400d2Igor Murashkin    for (size_t i = 0; i < len; i++) {
681e854c5fc817d31eef18e671b9b519bbb4b400d2Igor Murashkin        if ((value[i] & 0x80) || value[i] < 0x20 || value[i] == 0x7f) {
69db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin            return false;
70db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin        }
71db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin    }
72db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin    return true;
73db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin}
74db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin
75db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkinvoid CharacterEncodingDetector::detectAndConvert() {
76db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin
77db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin    int size = mNames.size();
78db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin    ALOGV("%d tags before conversion", size);
791e854c5fc817d31eef18e671b9b519bbb4b400d2Igor Murashkin    for (int i = 0; i < size; i++) {
801e854c5fc817d31eef18e671b9b519bbb4b400d2Igor Murashkin        ALOGV("%s: %s", mNames.getEntry(i), mValues.getEntry(i));
81db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin    }
82db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin
83db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin    if (size && mUtf8Conv) {
84db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin
85db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin        UErrorCode status = U_ZERO_ERROR;
86db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin        UCharsetDetector *csd = ucsdet_open(&status);
87db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin        const UCharsetMatch *ucm;
88db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin
891e854c5fc817d31eef18e671b9b519bbb4b400d2Igor Murashkin        // try combined detection of artist/album/title etc.
901e854c5fc817d31eef18e671b9b519bbb4b400d2Igor Murashkin        char buf[1024];
91fd887436bd111e4d2c7307578a51b5070025b7f2Eino-Ville Talvala        buf[0] = 0;
92db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin        int idx;
93db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin        bool allprintable = true;
94db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin        for (int i = 0; i < size; i++) {
95db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin            const char *name = mNames.getEntry(i);
961e854c5fc817d31eef18e671b9b519bbb4b400d2Igor Murashkin            const char *value = mValues.getEntry(i);
97db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin            if (!isPrintableAscii(value, strlen(value)) && (
98db075afc85b6b50a5d3a988a17ed0d4e09ef0823Igor Murashkin                        !strcmp(name, "artist") ||
99                        !strcmp(name, "albumartist") ||
100                        !strcmp(name, "composer") ||
101                        !strcmp(name, "genre") ||
102                        !strcmp(name, "album") ||
103                        !strcmp(name, "title"))) {
104                strlcat(buf, value, sizeof(buf));
105                // separate tags by space so ICU's ngram detector can do its job
106                strlcat(buf, " ", sizeof(buf));
107                allprintable = false;
108            }
109        }
110
111        const char *combinedenc = "UTF-8";
112        if (allprintable) {
113            // since 'buf' is empty, ICU would return a UTF-8 matcher with low confidence, so
114            // no need to even call it
115            ALOGV("all tags are printable, assuming ascii (%zu)", strlen(buf));
116        } else {
117            ucsdet_setText(csd, buf, strlen(buf), &status);
118            int32_t matches;
119            const UCharsetMatch** ucma = ucsdet_detectAll(csd, &matches, &status);
120            bool goodmatch = true;
121            const UCharsetMatch* bestCombinedMatch = getPreferred(buf, strlen(buf),
122                    ucma, matches, &goodmatch);
123
124            if (!goodmatch && strlen(buf) < 20) {
125                ALOGV("not a good match, trying with more data");
126                // This string might be too short for ICU to do anything useful with.
127                // (real world example: "Björk" in ISO-8859-1 might be detected as GB18030, because
128                //  the ISO detector reports a confidence of 0, while the GB18030 detector reports
129                //  a confidence of 10 with no invalid characters)
130                // Append artist, album and title if they were previously omitted because they
131                // were printable ascii.
132                bool added = false;
133                for (int i = 0; i < size; i++) {
134                    const char *name = mNames.getEntry(i);
135                    const char *value = mValues.getEntry(i);
136                    if (isPrintableAscii(value, strlen(value)) && (
137                                !strcmp(name, "artist") ||
138                                !strcmp(name, "album") ||
139                                !strcmp(name, "title"))) {
140                        strlcat(buf, value, sizeof(buf));
141                        strlcat(buf, " ", sizeof(buf));
142                        added = true;
143                    }
144                }
145                if (added) {
146                    ucsdet_setText(csd, buf, strlen(buf), &status);
147                    ucma = ucsdet_detectAll(csd, &matches, &status);
148                    bestCombinedMatch = getPreferred(buf, strlen(buf),
149                            ucma, matches, &goodmatch);
150                    if (!goodmatch) {
151                        ALOGV("still not a good match after adding printable tags");
152                    }
153                } else {
154                    ALOGV("no printable tags to add");
155                }
156            }
157
158            if (bestCombinedMatch != NULL) {
159                combinedenc = ucsdet_getName(bestCombinedMatch, &status);
160            }
161        }
162
163        for (int i = 0; i < size; i++) {
164            const char *name = mNames.getEntry(i);
165            uint8_t* src = (uint8_t *)mValues.getEntry(i);
166            int len = strlen((char *)src);
167            uint8_t* dest = src;
168
169            ALOGV("@@@ checking %s", name);
170            const char *s = mValues.getEntry(i);
171            int32_t inputLength = strlen(s);
172            const char *enc;
173
174            if (!allprintable && (!strcmp(name, "artist") ||
175                    !strcmp(name, "albumartist") ||
176                    !strcmp(name, "composer") ||
177                    !strcmp(name, "genre") ||
178                    !strcmp(name, "album") ||
179                    !strcmp(name, "title"))) {
180                // use encoding determined from the combination of artist/album/title etc.
181                enc = combinedenc;
182            } else {
183                if (isPrintableAscii(s, inputLength)) {
184                    enc = "UTF-8";
185                    ALOGV("@@@@ %s is ascii", mNames.getEntry(i));
186                } else {
187                    ucsdet_setText(csd, s, inputLength, &status);
188                    ucm = ucsdet_detect(csd, &status);
189                    if (!ucm) {
190                        mValues.setEntry(i, "???");
191                        continue;
192                    }
193                    enc = ucsdet_getName(ucm, &status);
194                    ALOGV("@@@@ recognized charset: %s for %s confidence %d",
195                            enc, mNames.getEntry(i), ucsdet_getConfidence(ucm, &status));
196                }
197            }
198
199            if (strcmp(enc,"UTF-8") != 0) {
200                // only convert if the source encoding isn't already UTF-8
201                ALOGV("@@@ using converter %s for %s", enc, mNames.getEntry(i));
202                UConverter *conv = ucnv_open(enc, &status);
203                if (U_FAILURE(status)) {
204                    ALOGE("could not create UConverter for %s", enc);
205                    continue;
206                }
207
208                // convert from native encoding to UTF-8
209                const char* source = mValues.getEntry(i);
210                int targetLength = len * 3 + 1;
211                char* buffer = new char[targetLength];
212                // don't normally check for NULL, but in this case targetLength may be large
213                if (!buffer)
214                    break;
215                char* target = buffer;
216
217                ucnv_convertEx(mUtf8Conv, conv, &target, target + targetLength,
218                        &source, source + strlen(source),
219                        NULL, NULL, NULL, NULL, TRUE, TRUE, &status);
220
221                if (U_FAILURE(status)) {
222                    ALOGE("ucnv_convertEx failed: %d", status);
223                    mValues.setEntry(i, "???");
224                } else {
225                    // zero terminate
226                    *target = 0;
227                    mValues.setEntry(i, buffer);
228                }
229
230                delete[] buffer;
231
232                ucnv_close(conv);
233            }
234        }
235
236        for (int i = size - 1; i >= 0; --i) {
237            if (strlen(mValues.getEntry(i)) == 0) {
238                ALOGV("erasing %s because entry is empty", mNames.getEntry(i));
239                mNames.erase(i);
240                mValues.erase(i);
241            }
242        }
243
244        ucsdet_close(csd);
245    }
246}
247
248/*
249 * When ICU detects multiple encoding matches, apply additional heuristics to determine
250 * which one is the best match, since ICU can't always be trusted to make the right choice.
251 *
252 * What this method does is:
253 * - decode the input using each of the matches found
254 * - recalculate the starting confidence level for multibyte encodings using a different
255 *   algorithm and larger frequent character lists than ICU
256 * - devalue encoding where the conversion contains unlikely characters (symbols, reserved, etc)
257 * - pick the highest match
258 * - signal to the caller whether this match is considered good: confidence > 15, and confidence
259 *   delta with the next runner up > 15
260 */
261const UCharsetMatch *CharacterEncodingDetector::getPreferred(
262        const char *input, size_t len,
263        const UCharsetMatch** ucma, size_t nummatches,
264        bool *goodmatch) {
265
266    *goodmatch = false;
267    Vector<const UCharsetMatch*> matches;
268    UErrorCode status = U_ZERO_ERROR;
269
270    ALOGV("%zu matches", nummatches);
271    for (size_t i = 0; i < nummatches; i++) {
272        const char *encname = ucsdet_getName(ucma[i], &status);
273        int confidence = ucsdet_getConfidence(ucma[i], &status);
274        ALOGV("%zu: %s %d", i, encname, confidence);
275        matches.push_back(ucma[i]);
276    }
277
278    size_t num = matches.size();
279    if (num == 0) {
280        return NULL;
281    }
282    if (num == 1) {
283        int confidence = ucsdet_getConfidence(matches[0], &status);
284        if (confidence > 15) {
285            *goodmatch = true;
286        }
287        return matches[0];
288    }
289
290    ALOGV("considering %zu matches", num);
291
292    // keep track of how many "special" characters result when converting the input using each
293    // encoding
294    Vector<int> newconfidence;
295    for (size_t i = 0; i < num; i++) {
296        const uint16_t *freqdata = NULL;
297        float freqcoverage = 0;
298        status = U_ZERO_ERROR;
299        const char *encname = ucsdet_getName(matches[i], &status);
300        int confidence = ucsdet_getConfidence(matches[i], &status);
301        if (!strcmp("GB18030", encname)) {
302            freqdata = frequent_zhCN;
303            freqcoverage = frequent_zhCN_coverage;
304        } else if (!strcmp("Big5", encname)) {
305            freqdata = frequent_zhTW;
306            freqcoverage = frequent_zhTW_coverage;
307        } else if (!strcmp("EUC-KR", encname)) {
308            freqdata = frequent_ko;
309            freqcoverage = frequent_ko_coverage;
310        } else if (!strcmp("EUC-JP", encname)) {
311            freqdata = frequent_ja;
312            freqcoverage = frequent_ja_coverage;
313        } else if (!strcmp("Shift_JIS", encname)) {
314            freqdata = frequent_ja;
315            freqcoverage = frequent_ja_coverage;
316        }
317
318        ALOGV("%zu: %s %d", i, encname, confidence);
319        UConverter *conv = ucnv_open(encname, &status);
320        const char *source = input;
321        const char *sourceLimit = input + len;
322        status = U_ZERO_ERROR;
323        int demerit = 0;
324        int frequentchars = 0;
325        int totalchars = 0;
326        while (true) {
327            // demerit the current encoding for each "special" character found after conversion.
328            // The amount of demerit is somewhat arbitrarily chosen.
329            int inchar;
330            if (source != sourceLimit) {
331                inchar = (source[0] << 8) + source[1];
332            }
333            UChar32 c = ucnv_getNextUChar(conv, &source, sourceLimit, &status);
334            if (!U_SUCCESS(status)) {
335                break;
336            }
337            if (c < 0x20 || (c >= 0x7f && c <= 0x009f)) {
338                ALOGV("control character %x", c);
339                demerit += 100;
340            } else if ((c >= 0xa0 && c <= 0xbe)         // symbols, superscripts
341                    || (c == 0xd7) || (c == 0xf7)       // multiplication and division signs
342                    || (c >= 0x2000 && c <= 0x209f)) {  // punctuation, superscripts
343                ALOGV("unlikely character %x", c);
344                demerit += 10;
345            } else if (c >= 0xe000 && c <= 0xf8ff) {
346                ALOGV("private use character %x", c);
347                demerit += 30;
348            } else if (c >= 0x2190 && c <= 0x2bff) {
349                // this range comprises various symbol ranges that are unlikely to appear in
350                // music file metadata.
351                ALOGV("symbol %x", c);
352                demerit += 10;
353            } else if (c == 0xfffd) {
354                ALOGV("replacement character");
355                demerit += 50;
356            } else if (c >= 0xfff0 && c <= 0xfffc) {
357                ALOGV("unicode special %x", c);
358                demerit += 50;
359            } else if (freqdata != NULL) {
360                totalchars++;
361                if (isFrequent(freqdata, c)) {
362                    frequentchars++;
363                }
364            }
365        }
366        if (freqdata != NULL && totalchars != 0) {
367            int myconfidence = 10 + float((100 * frequentchars) / totalchars) / freqcoverage;
368            ALOGV("ICU confidence: %d, my confidence: %d (%d %d)", confidence, myconfidence,
369                    totalchars, frequentchars);
370            if (myconfidence > 100) myconfidence = 100;
371            if (myconfidence < 0) myconfidence = 0;
372            confidence = myconfidence;
373        }
374        ALOGV("%d-%d=%d", confidence, demerit, confidence - demerit);
375        newconfidence.push_back(confidence - demerit);
376        ucnv_close(conv);
377        if (i == 0 && (confidence - demerit) == 100) {
378            // no need to check any further, we'll end up using this match anyway
379            break;
380        }
381    }
382
383    // find match with highest confidence after adjusting for unlikely characters
384    int highest = newconfidence[0];
385    size_t highestidx = 0;
386    int runnerup = -10000;
387    int runnerupidx = -10000;
388    num = newconfidence.size();
389    for (size_t i = 1; i < num; i++) {
390        if (newconfidence[i] > highest) {
391            runnerup = highest;
392            runnerupidx = highestidx;
393            highest = newconfidence[i];
394            highestidx = i;
395        } else if (newconfidence[i] > runnerup){
396            runnerup = newconfidence[i];
397            runnerupidx = i;
398        }
399    }
400    status = U_ZERO_ERROR;
401    ALOGV("selecting: '%s' w/ %d confidence",
402            ucsdet_getName(matches[highestidx], &status), highest);
403    if (runnerupidx < 0) {
404        ALOGV("no runner up");
405        if (highest > 15) {
406            *goodmatch = true;
407        }
408    } else {
409        ALOGV("runner up: '%s' w/ %d confidence",
410                ucsdet_getName(matches[runnerupidx], &status), runnerup);
411        if ((highest - runnerup) > 15) {
412            *goodmatch = true;
413        }
414    }
415    return matches[highestidx];
416}
417
418
419bool CharacterEncodingDetector::isFrequent(const uint16_t *values, uint32_t c) {
420
421    int start = 0;
422    int end = 511; // All the tables have 512 entries
423    int mid = (start+end)/2;
424
425    while(start <= end) {
426        if(c == values[mid]) {
427            return true;
428        } else if (c > values[mid]) {
429            start = mid + 1;
430        } else {
431            end = mid - 1;
432        }
433
434        mid = (start + end) / 2;
435    }
436
437    return false;
438}
439
440
441}  // namespace android
442