+// © 2016 and later: Unicode, Inc. and others.
+// License & terms of use: http://www.unicode.org/copyright.html
/*
**********************************************************************
- * Copyright (C) 2005-2006, International Business Machines
+ * Copyright (C) 2005-2016, International Business Machines
* Corporation and others. All Rights Reserved.
**********************************************************************
*/
#if !UCONFIG_NO_CONVERSION
+#include "cmemory.h"
+#include "csmatch.h"
#include "csrmbcs.h"
#include <math.h>
U_NAMESPACE_BEGIN
-#define ARRAY_SIZE(array) (sizeof array / sizeof array[0])
-
#define min(x,y) (((x)<(y))?(x):(y))
-const int32_t commonChars_sjis [] = {
+static const uint16_t commonChars_sjis [] = {
// TODO: This set of data comes from the character frequency-
// of-occurence analysis tool. The data needs to be moved
// into a resource and loaded from there.
0x8343, 0x834e, 0x834f, 0x8358, 0x835e, 0x8362, 0x8367, 0x8375, 0x8376, 0x8389,
0x838a, 0x838b, 0x838d, 0x8393, 0x8e96, 0x93fa, 0x95aa};
-const int32_t commonChars_euc_jp[] = {
+static const uint16_t commonChars_euc_jp[] = {
// TODO: This set of data comes from the character frequency-
// of-occurence analysis tool. The data needs to be moved
// into a resource and loaded from there.
0xa5e5, 0xa5e9, 0xa5ea, 0xa5eb, 0xa5ec, 0xa5ed, 0xa5f3, 0xb8a9, 0xb9d4, 0xbaee,
0xbbc8, 0xbef0, 0xbfb7, 0xc4ea, 0xc6fc, 0xc7bd, 0xcab8, 0xcaf3, 0xcbdc, 0xcdd1};
-const int32_t commonChars_euc_kr[] = {
+static const uint16_t commonChars_euc_kr[] = {
// TODO: This set of data comes from the character frequency-
// of-occurence analysis tool. The data needs to be moved
// into a resource and loaded from there.
0xc0da, 0xc0e5, 0xc0fb, 0xc0fc, 0xc1a4, 0xc1a6, 0xc1b6, 0xc1d6, 0xc1df, 0xc1f6,
0xc1f8, 0xc4a1, 0xc5cd, 0xc6ae, 0xc7cf, 0xc7d1, 0xc7d2, 0xc7d8, 0xc7e5, 0xc8ad};
-const int32_t commonChars_big5[] = {
+static const uint16_t commonChars_big5[] = {
// TODO: This set of data comes from the character frequency-
// of-occurence analysis tool. The data needs to be moved
// into a resource and loaded from there.
0xb5a5, 0xb5bd, 0xb5d0, 0xb5d8, 0xb671, 0xb7ed, 0xb867, 0xb944, 0xbad8, 0xbb44,
0xbba1, 0xbdd1, 0xc2c4, 0xc3b9, 0xc440, 0xc45f};
-const int32_t commonChars_gb_18030[] = {
+static const uint16_t commonChars_gb_18030[] = {
// TODO: This set of data comes from the character frequency-
// of-occurence analysis tool. The data needs to be moved
// into a resource and loaded from there.
0xcfb5, 0xcfc2, 0xcfd6, 0xd0c2, 0xd0c5, 0xd0d0, 0xd0d4, 0xd1a7, 0xd2aa, 0xd2b2,
0xd2b5, 0xd2bb, 0xd2d4, 0xd3c3, 0xd3d0, 0xd3fd, 0xd4c2, 0xd4da, 0xd5e2, 0xd6d0};
-static int32_t binarySearch(const int32_t *array, int32_t len, int32_t value)
+#if U_PLATFORM_IS_DARWIN_BASED
+static const uint8_t keyStrings_sjis[][MAX_KEY_STRING_WITH_NULL] = {
+ {0x82,0xa9,0x82,0xe7,0x91,0x97,0x90,0x4d,0}, // Signatures - Sent from my ...
+ {0x93,0x5d,0x91,0x97,0x83,0x81,0x83,0x62,0x83,0x5a,0x81,0x5b,0x83,0x57,0}, // forward
+ {0}
+};
+static const uint8_t keyStrings_euc_jp[][MAX_KEY_STRING_WITH_NULL] = {
+ {0xa4,0xab,0xa4,0xe9,0xc1,0xf7,0xbf,0xae,0}, // Signatures - Sent from my ...
+ {0xc5,0xbe,0xc1,0xf7,0xa5,0xe1,0xa5,0xc3,0xa5,0xbb,0xa1,0xbc,0xa5,0xb8,0}, // forward
+ {0}
+};
+static const uint8_t keyStrings_euc_kr[][MAX_KEY_STRING_WITH_NULL] = {
+ {0xb3,0xaa,0xc0,0xc7,0}, // Signatures - Sent from my ... #1
+ {0xbf,0xa1,0xbc,0xad,0x20,0xba,0xb8,0xb3,0xbf,0}, // Signatures - Sent from my ... #2
+ {0xc0,0xfc,0xb4,0xde,0xb5,0xc8,0x20,0xb8,0xde,0xbd,0xc3,0xc1,0xf6,0}, // forward
+ {0}
+};
+static const uint8_t keyStrings_big5[][MAX_KEY_STRING_WITH_NULL] = {
+ {0xb1,0x71,0xa7,0xda,0xaa,0xba,0}, // Signatures - Sent from my ... #1
+ {0xb6,0xc7,0xb0,0x65,0}, // Signatures - Sent from my ... #2
+ {0xb6,0x7d,0xa9,0x6c,0xc2,0xe0,0xb1,0x48,0xb6,0x6c,0xa5,0xf3,0}, // forward
+ {0}
+};
+static const uint8_t keyStrings_gb_18030[][MAX_KEY_STRING_WITH_NULL] = {
+ {0xb7,0xa2,0xd7,0xd4,0xce,0xd2,0xb5,0xc4,0}, // Signatures - Sent from my iP...
+ {0xd7,0xaa,0xb7,0xa2,0xb5,0xc4,0xd3,0xca,0xbc,0xfe,0}, // forward
+ {0}
+};
+#endif
+
+static int32_t binarySearch(const uint16_t *array, int32_t len, uint16_t value)
{
int32_t start = 0, end = len-1;
int32_t mid = (start+end)/2;
return -1;
}
-IteratedChar::IteratedChar():charValue(0), index(0), nextIndex(0), error(FALSE), done(FALSE)
+#if U_PLATFORM_IS_DARWIN_BASED
+// If testPrefix is a prefix of base, return its length, else return 0
+static int32_t isPrefix(const uint8_t *testPrefix, const uint8_t *base, const uint8_t *baseLimit) {
+ const uint8_t *testPrefixStart = testPrefix;
+ while (*testPrefix != 0 && base < baseLimit && *testPrefix == *base) {
+ testPrefix++;
+ base++;
+ }
+ return (*testPrefix == 0)? (int32_t)(testPrefix-testPrefixStart): 0;
+}
+#endif
+
+IteratedChar::IteratedChar() :
+charValue(0), index(-1), nextIndex(0), error(FALSE), done(FALSE)
{
// nothing else to do.
}
-void IteratedChar::reset()
+/*void IteratedChar::reset()
{
charValue = 0;
index = -1;
nextIndex = 0;
error = FALSE;
done = FALSE;
-}
+}*/
int32_t IteratedChar::nextByte(InputText *det)
{
// nothing to do.
}
-int32_t CharsetRecog_mbcs::match_mbcs(InputText *det, const int32_t commonChars[], int32_t commonCharsLen) {
- int singleByteCharCount = 0;
- int doubleByteCharCount = 0;
- int commonCharCount = 0;
- int badCharCount = 0;
- int totalCharCount = 0;
- int confidence = 0;
- IteratedChar *iter = new IteratedChar();
-
- // {
- for (iter->reset(); nextChar(iter, det);) {
- totalCharCount += 1;
-
- if (iter->error) {
- badCharCount += 1;
+#if U_PLATFORM_IS_DARWIN_BASED
+int32_t CharsetRecog_mbcs::match_mbcs(InputText *det, const uint16_t commonChars[], int32_t commonCharsLen, const uint8_t (*keyStrings)[MAX_KEY_STRING_WITH_NULL] ) const {
+#else
+int32_t CharsetRecog_mbcs::match_mbcs(InputText *det, const uint16_t commonChars[], int32_t commonCharsLen) const {
+#endif
+ int32_t singleByteCharCount = 0;
+ int32_t doubleByteCharCount = 0;
+ int32_t commonCharCount = 0;
+ int32_t badCharCount = 0;
+ int32_t totalCharCount = 0;
+ int32_t confidence = 0;
+#if U_PLATFORM_IS_DARWIN_BASED
+ int32_t confidenceFromKeys = 0;
+#endif
+ IteratedChar iter;
+
+ while (nextChar(&iter, det)) {
+ totalCharCount++;
+
+ if (iter.error) {
+ badCharCount++;
} else {
- if (iter->charValue <= 0xFF) {
- singleByteCharCount += 1;
+ if (iter.charValue <= 0xFF) {
+ singleByteCharCount++;
} else {
- doubleByteCharCount += 1;
+ doubleByteCharCount++;
if (commonChars != 0) {
- if (binarySearch(commonChars, commonCharsLen, iter->charValue) >= 0){
+ if (binarySearch(commonChars, commonCharsLen, iter.charValue) >= 0){
commonCharCount += 1;
}
}
+#if U_PLATFORM_IS_DARWIN_BASED
+ if (doubleByteCharCount <= 20) {
+ int32_t keyIndex;
+ for ( keyIndex = 0; keyStrings[keyIndex][0] != 0; keyIndex++ ) {
+ int32_t prefixLen = isPrefix(keyStrings[keyIndex], &det->fRawInput[iter.index], &det->fRawInput[det->fRawLength]);
+ confidenceFromKeys += prefixLen*5;
+ }
+ }
+#endif
}
}
if (badCharCount >= 2 && badCharCount*5 >= doubleByteCharCount) {
// Bail out early if the byte data is not matching the encoding scheme.
// break detectBlock;
- delete iter;
return confidence;
}
}
- delete iter;
-
if (doubleByteCharCount <= 10 && badCharCount == 0) {
// Not many multi-byte chars.
- // ASCII or ISO file? It's probably not our encoding,
- // but is not incompatible with our encoding, so don't give it a zero.
- confidence = 10;
+ if (doubleByteCharCount == 0 && totalCharCount < 10) {
+ // There weren't any multibyte sequences, and there was a low density of non-ASCII single bytes.
+ // We don't have enough data to have any confidence.
+ // Statistical analysis of single byte non-ASCII charcters would probably help here.
+ confidence = 0;
+ }
+ else {
+ // ASCII or ISO file? It's probably not our encoding,
+ // but is not incompatible with our encoding, so don't give it a zero.
+#if U_PLATFORM_IS_DARWIN_BASED
+ if (confidenceFromKeys > 90) {
+ confidenceFromKeys = 90;
+ } else if (confidenceFromKeys > 0 && confidenceFromKeys < 70) {
+ confidenceFromKeys += 20;
+ }
+ confidence = 10 + confidenceFromKeys;
+#else
+ confidence = 10;
+#endif
+ }
return confidence;
}
// Assess confidence purely on having a reasonable number of
// multi-byte characters (the more the better)
confidence = 30 + doubleByteCharCount - 20*badCharCount;
+#if U_PLATFORM_IS_DARWIN_BASED
+ confidence += confidenceFromKeys;
+#endif
if (confidence > 100) {
confidence = 100;
// Frequency of occurence statistics exist.
//
- double maxVal = log10((double)doubleByteCharCount / 4); /*(float)?*/
+ double maxVal = log((double)doubleByteCharCount / 4); /*(float)?*/
double scaleFactor = 90.0 / maxVal;
- confidence = (int32_t)(log10((double)commonCharCount+1) * scaleFactor + 10.0);
+ confidence = (int32_t)(log((double)commonCharCount+1) * scaleFactor + 10.0);
+#if U_PLATFORM_IS_DARWIN_BASED
+ confidence += confidenceFromKeys;
+#endif
confidence = min(confidence, 100);
}
// nothing to do
}
-UBool CharsetRecog_sjis::nextChar(IteratedChar* it, InputText* det) {
+UBool CharsetRecog_sjis::nextChar(IteratedChar* it, InputText* det) const {
it->index = it->nextIndex;
it->error = FALSE;
}
int32_t secondByte = it->nextByte(det);
-
- if (secondByte < 0) {
- return FALSE;
+ if (secondByte >= 0) {
+ it->charValue = (firstByte << 8) | secondByte;
}
- it->charValue = (firstByte << 8) | secondByte;
+ // else we'll handle the error later.
+
if (! ((secondByte >= 0x40 && secondByte <= 0x7F) || (secondByte >= 0x80 && secondByte <= 0xFE))) {
// Illegal second byte value.
it->error = TRUE;
return TRUE;
}
-int32_t CharsetRecog_sjis::match(InputText* det)
-{
- return match_mbcs(det, commonChars_sjis, ARRAY_SIZE(commonChars_sjis));
+UBool CharsetRecog_sjis::match(InputText* det, CharsetMatch *results) const {
+#if U_PLATFORM_IS_DARWIN_BASED
+ int32_t confidence = match_mbcs(det, commonChars_sjis, UPRV_LENGTHOF(commonChars_sjis), keyStrings_sjis);
+#else
+ int32_t confidence = match_mbcs(det, commonChars_sjis, UPRV_LENGTHOF(commonChars_sjis));
+#endif
+ results->set(det, this, confidence);
+ return (confidence > 0);
}
const char *CharsetRecog_sjis::getName() const
// nothing to do
}
-UBool CharsetRecog_euc::nextChar(IteratedChar* it, InputText* det) {
+UBool CharsetRecog_euc::nextChar(IteratedChar* it, InputText* det) const {
int32_t firstByte = 0;
int32_t secondByte = 0;
int32_t thirdByte = 0;
- // int32_t fourthByte = 0;
it->index = it->nextIndex;
it->error = FALSE;
if (firstByte < 0) {
// Ran off the end of the input data
- it->done = TRUE;
-
- return (! it->done);
+ return FALSE;
}
if (firstByte <= 0x8D) {
// single byte char
- return (! it->done);
+ return TRUE;
}
secondByte = it->nextByte(det);
- it->charValue = (it->charValue << 8) | secondByte;
+ if (secondByte >= 0) {
+ it->charValue = (it->charValue << 8) | secondByte;
+ }
+ // else we'll handle the error later.
if (firstByte >= 0xA1 && firstByte <= 0xFE) {
// Two byte Char
it->error = TRUE;
}
- return (! it->done);
+ return TRUE;
}
if (firstByte == 0x8E) {
it->error = TRUE;
}
- return (! it->done);
+ return TRUE;
}
if (firstByte == 0x8F) {
it->charValue = (it->charValue << 8) | thirdByte;
if (thirdByte < 0xa1) {
+ // Bad second byte or ran off the end of the input data with a non-ASCII first byte.
it->error = TRUE;
}
}
- return (! it->done);
+ return TRUE;
}
return "ja";
}
-int32_t CharsetRecog_euc_jp::match(InputText *det)
+UBool CharsetRecog_euc_jp::match(InputText *det, CharsetMatch *results) const
{
- return match_mbcs(det, commonChars_euc_jp, ARRAY_SIZE(commonChars_euc_jp));
+#if U_PLATFORM_IS_DARWIN_BASED
+ int32_t confidence = match_mbcs(det, commonChars_euc_jp, UPRV_LENGTHOF(commonChars_euc_jp), keyStrings_euc_jp);
+#else
+ int32_t confidence = match_mbcs(det, commonChars_euc_jp, UPRV_LENGTHOF(commonChars_euc_jp));
+#endif
+ results->set(det, this, confidence);
+ return (confidence > 0);
}
CharsetRecog_euc_kr::~CharsetRecog_euc_kr()
return "ko";
}
-int32_t CharsetRecog_euc_kr::match(InputText *det)
+UBool CharsetRecog_euc_kr::match(InputText *det, CharsetMatch *results) const
{
- return match_mbcs(det, commonChars_euc_kr, ARRAY_SIZE(commonChars_euc_kr));
+#if U_PLATFORM_IS_DARWIN_BASED
+ int32_t confidence = match_mbcs(det, commonChars_euc_kr, UPRV_LENGTHOF(commonChars_euc_kr), keyStrings_euc_kr);
+#else
+ int32_t confidence = match_mbcs(det, commonChars_euc_kr, UPRV_LENGTHOF(commonChars_euc_kr));
+#endif
+ results->set(det, this, confidence);
+ return (confidence > 0);
}
CharsetRecog_big5::~CharsetRecog_big5()
// nothing to do
}
-UBool CharsetRecog_big5::nextChar(IteratedChar* it, InputText* det)
+UBool CharsetRecog_big5::nextChar(IteratedChar* it, InputText* det) const
{
int32_t firstByte;
}
int32_t secondByte = it->nextByte(det);
-
- if (secondByte < 0) {
- return FALSE;
+ if (secondByte >= 0) {
+ it->charValue = (it->charValue << 8) | secondByte;
}
+ // else we'll handle the error later.
- it->charValue = (it->charValue << 8) | secondByte;
-
- if (secondByte < 0x40 ||
- secondByte == 0x7F ||
- secondByte == 0xFF) {
- it->error = TRUE;
+ if (secondByte < 0x40 || secondByte == 0x7F || secondByte == 0xFF) {
+ it->error = TRUE;
}
return TRUE;
return "zh";
}
-int32_t CharsetRecog_big5::match(InputText *det)
+UBool CharsetRecog_big5::match(InputText *det, CharsetMatch *results) const
{
- return match_mbcs(det, commonChars_big5, ARRAY_SIZE(commonChars_big5));
+#if U_PLATFORM_IS_DARWIN_BASED
+ int32_t confidence = match_mbcs(det, commonChars_big5, UPRV_LENGTHOF(commonChars_big5), keyStrings_big5);
+#else
+ int32_t confidence = match_mbcs(det, commonChars_big5, UPRV_LENGTHOF(commonChars_big5));
+#endif
+ results->set(det, this, confidence);
+ return (confidence > 0);
}
CharsetRecog_gb_18030::~CharsetRecog_gb_18030()
// nothing to do
}
-UBool CharsetRecog_gb_18030::nextChar(IteratedChar* it, InputText* det) {
+UBool CharsetRecog_gb_18030::nextChar(IteratedChar* it, InputText* det) const {
int32_t firstByte = 0;
int32_t secondByte = 0;
int32_t thirdByte = 0;
if (firstByte < 0) {
// Ran off the end of the input data
- it->done = TRUE;
-
- return (! it->done);
+ return FALSE;
}
if (firstByte <= 0x80) {
// single byte char
- return (! it->done);
+ return TRUE;
}
secondByte = it->nextByte(det);
- it->charValue = (it->charValue << 8) | secondByte;
+ if (secondByte >= 0) {
+ it->charValue = (it->charValue << 8) | secondByte;
+ }
+ // else we'll handle the error later.
if (firstByte >= 0x81 && firstByte <= 0xFE) {
// Two byte Char
if ((secondByte >= 0x40 && secondByte <= 0x7E) || (secondByte >=80 && secondByte <= 0xFE)) {
- return (! it->done);
+ return TRUE;
}
// Four byte char
if (fourthByte >= 0x30 && fourthByte <= 0x39) {
it->charValue = (it->charValue << 16) | (thirdByte << 8) | fourthByte;
- return (! it->done);
+ return TRUE;
}
}
}
+ // Something wasn't valid, or we ran out of data (-1).
it->error = TRUE;
-
- return (! it->done);
}
- return (! it->done);
+ return TRUE;
}
const char *CharsetRecog_gb_18030::getName() const
return "zh";
}
-int32_t CharsetRecog_gb_18030::match(InputText *det)
+UBool CharsetRecog_gb_18030::match(InputText *det, CharsetMatch *results) const
{
- return match_mbcs(det, commonChars_gb_18030, ARRAY_SIZE(commonChars_gb_18030));
+#if U_PLATFORM_IS_DARWIN_BASED
+ int32_t confidence = match_mbcs(det, commonChars_gb_18030, UPRV_LENGTHOF(commonChars_gb_18030), keyStrings_gb_18030);
+#else
+ int32_t confidence = match_mbcs(det, commonChars_gb_18030, UPRV_LENGTHOF(commonChars_gb_18030));
+#endif
+ results->set(det, this, confidence);
+ return (confidence > 0);
}
U_NAMESPACE_END