scuffed-code/icu4c/source/i18n/csrmbcs.cpp
2006-02-06 18:03:11 +00:00

439 lines
14 KiB
C++

/*
**********************************************************************
* Copyright (C) 2005-2006, International Business Machines
* Corporation and others. All Rights Reserved.
**********************************************************************
*/
#include "unicode/utypes.h"
#include "csrmbcs.h"
#include <math.h>
U_NAMESPACE_BEGIN
#define min(x,y) ((x<y)?(x):(y))
const int32_t CharsetRecog_sjis::commonChars []=
// 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.
{0x82cc, 0x82c5, 0x82a2, 0x815b, 0x8141, 0x82b5, 0x82c9, 0x82b7, 0x8142, 0x82c4,
0x82c6, 0x82cd, 0x82dc, 0x82f0, 0x82e9, 0x82c8, 0x82aa, 0x8393, 0x82bd, 0x8358,
0x82ea, 0x8343, 0x82a4, 0x82a9, 0x8367, 0x82b1, 0x8145, 0x82e0, 0x838b, 0x834e,
0x82e8, 0x82e7, 0x8140, 0x8362, 0x8389, 0x82c1, 0x838a, 0x82ab, 0x8376, 0x82b3,
0x82f1, 0x82a0, 0x8368, 0x93fa, 0x8175, 0x8176, 0x835e, 0x82e6, 0x8357, 0x82ad,
0x8381, 0x82a6, 0x82b9, 0x82bb, 0x82be, 0x8341, 0x8374, 0x82af, 0x9056, 0x82a8,
0x82c2, 0x8354, 0x8e67, 0x8375, 0x82c7, 0x95f1, 0x8356, 0x967b, 0x92e8, 0x8345,
0x82ce, 0x8385, 0x9770, 0x82df, 0x82dd, 0x836f, 0x8342, 0x8ca7, 0x8352, 0x837d,
0x838d, 0x8346, 0x834f, 0x8380, 0x82ed, 0x8d73, 0x8349, 0x8365, 0x8fee, 0x95b6,
0x8169, 0x816a, 0x836a, 0x8dec, 0x82bf, 0x834c, 0x8366, 0x82e2, 0x838c, 0x945c};
const int32_t CharsetRecog_euc_jp::commonChars[] =
// 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.
{0xa4ce, 0xa4c7, 0xa4a4, 0xa1bc, 0xa1a2, 0xa4b7, 0xa4cb, 0xa4b9, 0xa1a3, 0xa4c6,
0xa4c8, 0xa4cf, 0xa4de, 0xa4f2, 0xa4eb, 0xa4ca, 0xa4ac, 0xa5f3, 0xa4bf, 0xa5b9,
0xa4ec, 0xa5a4, 0xa4a6, 0xa4ab, 0xa5c8, 0xa4b3, 0xa1a6, 0xa4e2, 0xa5eb, 0xa5af,
0xa4ea, 0xa4e9, 0xa1a1, 0xa5c3, 0xa5e9, 0xa4c3, 0xa5ea, 0xa4ad, 0xa5d7, 0xa4b5,
0xa4f3, 0xa4a2, 0xa5c9, 0xc6fc, 0xa1d6, 0xa1d7, 0xa5bf, 0xa4e8, 0xa5b8, 0xa4af,
0xa5e1, 0xa4a8, 0xa4bb, 0xa4bd, 0xa4c0, 0xa5a2, 0xa5d5, 0xa4b1, 0xbfb7, 0xa4aa,
0xa4c4, 0xa5b5, 0xbbc8, 0xa5d6, 0xa4c9, 0xcaf3, 0xa5b7, 0xcbdc, 0xc4ea, 0xa5a6,
0xa4d0, 0xa5e5, 0xcdd1, 0xa4e1, 0xa4df, 0xa5d0, 0xa5a3, 0xb8a9, 0xa5b3, 0xa5de,
0xa5ed, 0xa5a7, 0xa5b0, 0xa5e0, 0xa4ef, 0xb9d4, 0xa5aa, 0xa5c6, 0xbef0, 0xcab8,
0xa1ca, 0xa1cb, 0xa5cb, 0xbaee, 0xa4c1, 0xa5ad, 0xa5c7, 0xa4e4, 0xa5ec, 0xc7bd};
const int32_t CharsetRecog_euc_kr::commonChars[] =
// 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.
{0xc0cc, 0xb4d9, 0xb4c2, 0xc0c7, 0xbfa1, 0xc7cf, 0xb0a1, 0xb0ed, 0xc7d1, 0xc1f6,
0xc0bb, 0xb7ce, 0xb1e2, 0xbcad, 0xc0ba, 0xbbe7, 0xc1a4, 0xc0da, 0xb5b5, 0xb8a6,
0xbeee, 0xb4cf, 0xbcf6, 0xbdc3, 0xb1d7, 0xb4eb, 0xb8ae, 0xc0ce, 0xb3aa, 0xbec6,
0xc0d6, 0xbab8, 0xb5e9, 0xb6f3, 0xc7d8, 0xb0cd, 0xc0cf, 0xbdba, 0xc0b8, 0xb1b9,
0xc1a6, 0xb9fd, 0xbbf3, 0xb0d4, 0xb8e9, 0xb8b8, 0xb0fa, 0xc0fb, 0xbace, 0xc1d6,
0xbfa9, 0xc0fc, 0xbfeb, 0xb9ae, 0xc6ae, 0xbbfd, 0xbcba, 0xc0a7, 0xbff8, 0xb5c7,
0xbfe4, 0xbfec, 0xbdc5, 0xc7d2, 0xc7e5, 0xb0fc, 0xb1b8, 0xbaf1, 0xbedf, 0xc5cd,
0xb8b6, 0xbdc0, 0xb7af, 0xb5bf, 0xb3bb, 0xc8ad, 0xc0bd, 0xb0b3, 0xc4a1, 0xb7c2,
0xb9ab, 0xc0af, 0xbef8, 0xb5a5, 0xbcd2, 0xb9ce, 0xc1df, 0xbfc0, 0xc1f8, 0xb0e6,
0xb1c7, 0xbad0, 0xbefa, 0xc0e5, 0xbec8, 0xc1b6, 0xb8bb, 0xb0f8, 0xb9cc, 0xb0c5};
const int32_t CharsetRecog_gb_18030::commonChars[] =
// 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.
{0xa3ac, 0xb5c4, 0xa1a1, 0xa1a4, 0xa1a3, 0xcac7, 0xd2bb, 0xb4f3, 0xd4da, 0xd6d0,
0xcafd, 0xd3d0, 0xa1f3, 0xb2bb, 0xa3ba, 0xbbfa, 0xc8cb, 0xa1a2, 0xd3c3, 0xd1a7,
0xc8d5, 0xbedd, 0xb8f6, 0xd0c2, 0xcdf8, 0xd2aa, 0xb9fa, 0xc1cb, 0xc9cf, 0xa1b0,
0xa1b1, 0xced2, 0xbcfe, 0xcec4, 0xd2d4, 0xc4dc, 0xc0b4, 0xd4c2, 0xcab1, 0xd0d0,
0xbdcc, 0xbfc9, 0xb6d4, 0xbcdb, 0xb1be, 0xb3f6, 0xb8b4, 0xc9fa, 0xb1b8, 0xbcbc,
0xcfc2, 0xbacd, 0xbecd, 0xb3c9, 0xd5e2, 0xb8df, 0xb7d6, 0xc5cc, 0xbfc6, 0xbbe1,
0xceaa, 0xc8e7, 0xcfb5, 0xa1f1, 0xc4ea, 0xb1a8, 0xb6af, 0xc0ed, 0xd3fd, 0xb7a2,
0xc8ab, 0xb7bd, 0xcee5, 0xc2db, 0xbba7, 0xd0d4, 0xb9c9, 0xc3c7, 0xb9fd, 0xcad0,
0xb5e3, 0xbbd6, 0xcfd6, 0xcab5, 0xd2b2, 0xbfb4, 0xb6e0, 0xccec, 0xc7f8, 0xd0c5,
0xcad6, 0xb9d8, 0xb5bd, 0xb7dd, 0xc6f7, 0xcaf5, 0xa3a1, 0xb7a8, 0xb9ab, 0xd2b5,
0xcbf9, 0xcdbc, 0xc6e4, 0xd3da, 0xd0a1, 0xd1a1, 0xd3ce, 0xbfaa, 0xb4e6, 0xc4bf,
0xd7f7, 0xb5e7, 0xcdb3, 0xc7e9, 0xd7ee, 0xc6c0, 0xcfdf, 0xb5d8, 0xb5c0, 0xbead,
0xb4c5, 0xc6b7, 0xc4da, 0xd0c4, 0xb9a4, 0xd4aa, 0xc2bc, 0xc3c0, 0xbaf3, 0xcabd,
0xbcd2, 0xcef1, 0xbdab, 0xa3ad, 0xa3bf, 0xb3a4, 0xb9fb, 0xd6ae, 0xc1bf, 0xbbd8,
0xb8f1, 0xb6f8, 0xb6a8, 0xcde2, 0xbac3, 0xb3cc, 0xccd8, 0xd7d4, 0xcbb5};
const int32_t CharsetRecog_sjis::commonCharsLen =
sizeof(CharsetRecog_sjis::commonChars) / sizeof(CharsetRecog_sjis::commonChars[0]);
const int32_t CharsetRecog_euc_jp::commonCharsLen =
sizeof(CharsetRecog_euc_jp::commonChars) / sizeof(CharsetRecog_euc_jp::commonChars[0]);
const int32_t CharsetRecog_euc_kr::commonCharsLen =
sizeof(CharsetRecog_euc_kr::commonChars) / sizeof(CharsetRecog_euc_kr::commonChars[0]);
const int32_t CharsetRecog_gb_18030::commonCharsLen =
sizeof(CharsetRecog_gb_18030::commonChars) / sizeof(CharsetRecog_gb_18030::commonChars[0]);
static int32_t binarySearch(const int32_t *array, int32_t len, int32_t value)
{
int32_t start = 0, end = len-1;
int32_t mid = (start+end)/2;
while(start <= end) {
if(array[mid] == value) {
return mid;
}
if(array[mid] < value){
start = mid+1;
} else {
end = mid-1;
}
mid = (start+end)/2;
}
return -1;
}
iteratedChar::iteratedChar():charValue(0), index(0), nextIndex(0), error(FALSE), done(FALSE)
{
// nothing else to do.
}
void iteratedChar::reset()
{
charValue = 0;
index = -1;
nextIndex = 0;
error = FALSE;
done = FALSE;
}
int32_t iteratedChar::nextByte(InputText *det)
{
if (nextIndex >= det->fRawLength) {
done = TRUE;
return -1;
}
return det->fRawInput[nextIndex++];
}
CharsetRecog_mbcs::~CharsetRecog_mbcs()
{
// 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++;
} else {
if (iter->charValue <= 0xFF) {
singleByteCharCount += 1;
} else {
doubleByteCharCount += 1;
if (commonChars != 0) {
if (binarySearch(commonChars, commonCharsLen, iter->charValue) >= 0){
commonCharCount += 1;
}
}
}
}
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 == 0 && badCharCount == 0) {
// No multi-byte chars.
// ASCII file? It's probably not our encoding,
// but is not incompatible with our encoding, so don't give it a zero.
confidence = 10;
return confidence;
}
//
// No match if there are too many characters that don't fit the encoding scheme.
// (should we have zero tolerance for these?)
//
if (doubleByteCharCount < 20*badCharCount) {
confidence = 0;
return confidence;
}
if (commonChars == 0) {
// We have no statistics on frequently occuring characters.
// Assess confidence purely on having a reasonable number of
// multi-byte characters (the more the better
confidence = 30 + doubleByteCharCount - 20*badCharCount;
if (confidence > 100) {
confidence = 100;
}
} else {
//
// Frequency of occurence statistics exist.
//
double maxVal = log10((float)doubleByteCharCount / 4);
double scaleFactor = 90.0 / maxVal;
confidence = (int32_t)(log10((double)commonCharCount+1) * scaleFactor + 10.0);
confidence = min(confidence, 100);
if(confidence<0)confidence=0;
}
return confidence;
}
CharsetRecog_sjis::~CharsetRecog_sjis()
{
// nothing to do
}
UBool CharsetRecog_sjis::nextChar(iteratedChar* it, InputText* det) {
it->index = it->nextIndex;
it->error = FALSE;
int32_t firstByte = it->charValue = it->nextByte(det);
if (firstByte < 0) {
return FALSE;
}
if (firstByte <= 0x7F || (firstByte > 0xA0 && firstByte <= 0xDF)) {
return TRUE;
}
int32_t secondByte = it->nextByte(det);
if (secondByte < 0) {
return FALSE;
}
it->charValue = (firstByte << 8) | secondByte;
if (! ((secondByte >= 0x40 && secondByte <= 0x7F) || (secondByte >= 0x80 && secondByte <= 0xFF))) {
// Illegal second byte value.
it->error = TRUE;
}
return TRUE;
}
int32_t CharsetRecog_sjis::match(InputText* det)
{
return match_mbcs(det, commonChars, commonCharsLen);
}
const char *CharsetRecog_sjis::getName() const
{
return "Shift_JIS";
}
CharsetRecog_euc::~CharsetRecog_euc()
{
// nothing to do
}
UBool CharsetRecog_euc::nextChar(iteratedChar* it, InputText* det) {
int32_t firstByte = 0;
int32_t secondByte = 0;
int32_t thirdByte = 0;
// int32_t fourthByte = 0;
it->index = it->nextIndex;
it->error = FALSE;
firstByte = it->charValue = it->nextByte(det);
if (firstByte < 0) {
// Ran off the end of the input data
it->done = TRUE;
return (! it->done);
}
if (firstByte <= 0x8D) {
// single byte char
return (! it->done);
}
secondByte = it->nextByte(det);
it->charValue = (it->charValue << 8) | secondByte;
if (firstByte >= 0xA1 && firstByte <= 0xFE) {
// Two byte Char
if (secondByte < 0xA1) {
it->error = TRUE;
}
return (! it->done);
}
if (firstByte == 0x8E) {
// Code Set 2.
// In EUC-JP, total char size is 2 bytes, only one byte of actual char value.
// In EUC-TW, total char size is 4 bytes, three bytes contribute to char value.
// We don't know which we've got.
// Treat it like EUC-JP. If the data really was EUC-TW, the following two
// bytes will look like a well formed 2 byte char.
if (secondByte < 0xA1) {
it->error = TRUE;
}
return (! it->done);
}
if (firstByte == 0x8F) {
// Code set 3.
// Three byte total char size, two bytes of actual char value.
thirdByte = it->nextByte(det);
it->charValue = (it->charValue << 8) | thirdByte;
if (thirdByte < 0xa1) {
it->error = TRUE;
}
}
return (! it->done);
}
CharsetRecog_euc_jp::~CharsetRecog_euc_jp()
{
// nothing to do
}
const char *CharsetRecog_euc_jp::getName() const
{
return "EUC-JP";
}
int32_t CharsetRecog_euc_jp::match(InputText *det)
{
return match_mbcs(det, commonChars, commonCharsLen);
}
CharsetRecog_euc_kr::~CharsetRecog_euc_kr()
{
// nothing to do
}
const char *CharsetRecog_euc_kr::getName() const
{
return "EUC-KR";
}
int32_t CharsetRecog_euc_kr::match(InputText *det)
{
return match_mbcs(det, commonChars, commonCharsLen);
}
CharsetRecog_gb_18030::~CharsetRecog_gb_18030()
{
// nothing to do
}
UBool CharsetRecog_gb_18030::nextChar(iteratedChar* it, InputText* det) {
int32_t firstByte = 0;
int32_t secondByte = 0;
int32_t thirdByte = 0;
int32_t fourthByte = 0;
it->index = it->nextIndex;
it->error = FALSE;
firstByte = it->charValue = it->nextByte(det);
if (firstByte < 0) {
// Ran off the end of the input data
it->done = TRUE;
return (! it->done);
}
if (firstByte <= 0x80) {
// single byte char
return (! it->done);
}
secondByte = it->nextByte(det);
it->charValue = (it->charValue << 8) | secondByte;
if (firstByte >= 0x81 && firstByte <= 0xFE) {
// Two byte Char
if ((secondByte >= 0x40 && secondByte <= 0x7E) || (secondByte >=80 && secondByte <= 0xFE)) {
return (! it->done);
}
// Four byte char
if (secondByte >= 0x30 && secondByte <= 0x39) {
thirdByte = it->nextByte(det);
if (thirdByte >= 0x81 && thirdByte <= 0xFE) {
fourthByte = it->nextByte(det);
if (fourthByte >= 0x30 && fourthByte <= 0x39) {
it->charValue = (it->charValue << 16) | (thirdByte << 8) | fourthByte;
return (! it->done);
}
}
}
it->error = TRUE;
return (! it->done);
}
return (! it->done);
}
const char *CharsetRecog_gb_18030::getName() const
{
return "GB18030";
}
int32_t CharsetRecog_gb_18030::match(InputText *det)
{
return match_mbcs(det, commonChars, commonCharsLen);
}
U_NAMESPACE_END