2017-01-20 00:20:31 +00:00
|
|
|
// © 2016 and later: Unicode, Inc. and others.
|
2016-06-15 18:58:17 +00:00
|
|
|
// License & terms of use: http://www.unicode.org/copyright.html
|
2006-02-06 18:03:11 +00:00
|
|
|
/*
|
|
|
|
**********************************************************************
|
2016-05-31 21:45:07 +00:00
|
|
|
* Copyright (C) 2005-2016, International Business Machines
|
|
|
|
* Corporation and others. All Rights Reserved.
|
2006-02-06 18:03:11 +00:00
|
|
|
**********************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "unicode/utypes.h"
|
|
|
|
|
2006-05-09 18:06:10 +00:00
|
|
|
#if !UCONFIG_NO_CONVERSION
|
|
|
|
|
2016-02-23 10:40:09 +00:00
|
|
|
#include "cmemory.h"
|
2006-02-06 18:03:11 +00:00
|
|
|
#include "cstring.h"
|
|
|
|
|
|
|
|
#include "csr2022.h"
|
2012-06-01 20:40:48 +00:00
|
|
|
#include "csmatch.h"
|
2006-02-06 18:03:11 +00:00
|
|
|
|
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Matching function shared among the 2022 detectors JP, CN and KR
|
2012-06-01 20:40:48 +00:00
|
|
|
* Counts up the number of legal and unrecognized escape sequences in
|
2006-02-06 18:03:11 +00:00
|
|
|
* the sample of text, and computes a score based on the total number &
|
|
|
|
* the proportion that fit the encoding.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param text the byte buffer containing text to analyse
|
|
|
|
* @param textLen the size of the text in the byte.
|
|
|
|
* @param escapeSequences the byte escape sequences to test for.
|
|
|
|
* @return match quality, in the range of 0-100.
|
|
|
|
*/
|
2012-06-01 20:40:48 +00:00
|
|
|
int32_t CharsetRecog_2022::match_2022(const uint8_t *text, int32_t textLen, const uint8_t escapeSequences[][5], int32_t escapeSequences_length) const
|
2006-02-06 18:03:11 +00:00
|
|
|
{
|
|
|
|
int32_t i, j;
|
|
|
|
int32_t escN;
|
|
|
|
int32_t hits = 0;
|
|
|
|
int32_t misses = 0;
|
|
|
|
int32_t shifts = 0;
|
|
|
|
int32_t quality;
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
while(i < textLen) {
|
|
|
|
if(text[i] == 0x1B) {
|
|
|
|
escN = 0;
|
|
|
|
while(escN < escapeSequences_length) {
|
|
|
|
const uint8_t *seq = escapeSequences[escN];
|
2009-11-11 15:47:22 +00:00
|
|
|
int32_t seq_length = (int32_t)uprv_strlen((const char *) seq);
|
2006-02-06 18:03:11 +00:00
|
|
|
|
2008-02-01 07:08:13 +00:00
|
|
|
if (textLen-i >= seq_length) {
|
|
|
|
j = 1;
|
|
|
|
while(j < seq_length) {
|
|
|
|
if(seq[j] != text[i+j]) {
|
|
|
|
goto checkEscapes;
|
|
|
|
}
|
|
|
|
|
|
|
|
j += 1;
|
2006-02-06 18:03:11 +00:00
|
|
|
}
|
|
|
|
|
2008-02-01 07:08:13 +00:00
|
|
|
hits += 1;
|
|
|
|
i += seq_length-1;
|
|
|
|
goto scanInput;
|
2006-02-06 18:03:11 +00:00
|
|
|
}
|
2008-02-01 07:08:13 +00:00
|
|
|
// else we ran out of string to compare this time.
|
2006-02-06 18:03:11 +00:00
|
|
|
checkEscapes:
|
|
|
|
escN += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
misses += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( text[i]== 0x0e || text[i] == 0x0f){
|
|
|
|
shifts += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
scanInput:
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hits == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Initial quality is based on relative proportion of recongized vs.
|
|
|
|
// unrecognized escape sequences.
|
|
|
|
// All good: quality = 100;
|
|
|
|
// half or less good: quality = 0;
|
|
|
|
// linear inbetween.
|
|
|
|
quality = (100*hits - 100*misses) / (hits + misses);
|
|
|
|
|
|
|
|
// Back off quality if there were too few escape sequences seen.
|
|
|
|
// Include shifts in this computation, so that KR does not get penalized
|
|
|
|
// for having only a single Escape sequence, but many shifts.
|
|
|
|
if (hits+shifts < 5) {
|
|
|
|
quality -= (5-(hits+shifts))*10;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (quality < 0) {
|
|
|
|
quality = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return quality;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-02-10 00:10:58 +00:00
|
|
|
static const uint8_t escapeSequences_2022JP[][5] = {
|
2006-02-06 18:03:11 +00:00
|
|
|
{0x1b, 0x24, 0x28, 0x43, 0x00}, // KS X 1001:1992
|
|
|
|
{0x1b, 0x24, 0x28, 0x44, 0x00}, // JIS X 212-1990
|
|
|
|
{0x1b, 0x24, 0x40, 0x00, 0x00}, // JIS C 6226-1978
|
|
|
|
{0x1b, 0x24, 0x41, 0x00, 0x00}, // GB 2312-80
|
|
|
|
{0x1b, 0x24, 0x42, 0x00, 0x00}, // JIS X 208-1983
|
|
|
|
{0x1b, 0x26, 0x40, 0x00, 0x00}, // JIS X 208 1990, 1997
|
|
|
|
{0x1b, 0x28, 0x42, 0x00, 0x00}, // ASCII
|
|
|
|
{0x1b, 0x28, 0x48, 0x00, 0x00}, // JIS-Roman
|
|
|
|
{0x1b, 0x28, 0x49, 0x00, 0x00}, // Half-width katakana
|
|
|
|
{0x1b, 0x28, 0x4a, 0x00, 0x00}, // JIS-Roman
|
|
|
|
{0x1b, 0x2e, 0x41, 0x00, 0x00}, // ISO 8859-1
|
|
|
|
{0x1b, 0x2e, 0x46, 0x00, 0x00} // ISO 8859-7
|
|
|
|
};
|
|
|
|
|
2015-02-20 19:31:33 +00:00
|
|
|
#if !UCONFIG_ONLY_HTML_CONVERSION
|
2006-02-10 00:10:58 +00:00
|
|
|
static const uint8_t escapeSequences_2022KR[][5] = {
|
2006-02-06 18:03:11 +00:00
|
|
|
{0x1b, 0x24, 0x29, 0x43, 0x00}
|
|
|
|
};
|
|
|
|
|
2006-02-10 00:10:58 +00:00
|
|
|
static const uint8_t escapeSequences_2022CN[][5] = {
|
2006-02-06 18:03:11 +00:00
|
|
|
{0x1b, 0x24, 0x29, 0x41, 0x00}, // GB 2312-80
|
|
|
|
{0x1b, 0x24, 0x29, 0x47, 0x00}, // CNS 11643-1992 Plane 1
|
|
|
|
{0x1b, 0x24, 0x2A, 0x48, 0x00}, // CNS 11643-1992 Plane 2
|
|
|
|
{0x1b, 0x24, 0x29, 0x45, 0x00}, // ISO-IR-165
|
|
|
|
{0x1b, 0x24, 0x2B, 0x49, 0x00}, // CNS 11643-1992 Plane 3
|
|
|
|
{0x1b, 0x24, 0x2B, 0x4A, 0x00}, // CNS 11643-1992 Plane 4
|
|
|
|
{0x1b, 0x24, 0x2B, 0x4B, 0x00}, // CNS 11643-1992 Plane 5
|
|
|
|
{0x1b, 0x24, 0x2B, 0x4C, 0x00}, // CNS 11643-1992 Plane 6
|
|
|
|
{0x1b, 0x24, 0x2B, 0x4D, 0x00}, // CNS 11643-1992 Plane 7
|
|
|
|
{0x1b, 0x4e, 0x00, 0x00, 0x00}, // SS2
|
|
|
|
{0x1b, 0x4f, 0x00, 0x00, 0x00}, // SS3
|
|
|
|
};
|
2015-02-20 19:31:33 +00:00
|
|
|
#endif
|
2006-02-06 18:03:11 +00:00
|
|
|
|
2011-07-26 05:32:25 +00:00
|
|
|
CharsetRecog_2022JP::~CharsetRecog_2022JP() {}
|
|
|
|
|
2012-06-01 20:40:48 +00:00
|
|
|
const char *CharsetRecog_2022JP::getName() const {
|
2006-02-06 18:03:11 +00:00
|
|
|
return "ISO-2022-JP";
|
|
|
|
}
|
|
|
|
|
2012-06-01 20:40:48 +00:00
|
|
|
UBool CharsetRecog_2022JP::match(InputText *textIn, CharsetMatch *results) const {
|
|
|
|
int32_t confidence = match_2022(textIn->fInputBytes,
|
|
|
|
textIn->fInputLen,
|
|
|
|
escapeSequences_2022JP,
|
2016-02-23 10:40:09 +00:00
|
|
|
UPRV_LENGTHOF(escapeSequences_2022JP));
|
2012-06-01 20:40:48 +00:00
|
|
|
results->set(textIn, this, confidence);
|
|
|
|
return (confidence > 0);
|
2006-02-06 18:03:11 +00:00
|
|
|
}
|
|
|
|
|
2015-02-20 19:31:33 +00:00
|
|
|
#if !UCONFIG_ONLY_HTML_CONVERSION
|
2011-07-26 05:32:25 +00:00
|
|
|
CharsetRecog_2022KR::~CharsetRecog_2022KR() {}
|
|
|
|
|
2012-06-01 20:40:48 +00:00
|
|
|
const char *CharsetRecog_2022KR::getName() const {
|
2006-02-06 18:03:11 +00:00
|
|
|
return "ISO-2022-KR";
|
|
|
|
}
|
|
|
|
|
2012-06-01 20:40:48 +00:00
|
|
|
UBool CharsetRecog_2022KR::match(InputText *textIn, CharsetMatch *results) const {
|
|
|
|
int32_t confidence = match_2022(textIn->fInputBytes,
|
|
|
|
textIn->fInputLen,
|
|
|
|
escapeSequences_2022KR,
|
2016-02-23 10:40:09 +00:00
|
|
|
UPRV_LENGTHOF(escapeSequences_2022KR));
|
2012-06-01 20:40:48 +00:00
|
|
|
results->set(textIn, this, confidence);
|
|
|
|
return (confidence > 0);
|
2006-02-06 18:03:11 +00:00
|
|
|
}
|
|
|
|
|
2011-07-26 05:32:25 +00:00
|
|
|
CharsetRecog_2022CN::~CharsetRecog_2022CN() {}
|
|
|
|
|
2012-06-01 20:40:48 +00:00
|
|
|
const char *CharsetRecog_2022CN::getName() const {
|
2006-02-06 18:03:11 +00:00
|
|
|
return "ISO-2022-CN";
|
|
|
|
}
|
|
|
|
|
2012-06-01 20:40:48 +00:00
|
|
|
UBool CharsetRecog_2022CN::match(InputText *textIn, CharsetMatch *results) const {
|
|
|
|
int32_t confidence = match_2022(textIn->fInputBytes,
|
|
|
|
textIn->fInputLen,
|
|
|
|
escapeSequences_2022CN,
|
2016-02-23 10:40:09 +00:00
|
|
|
UPRV_LENGTHOF(escapeSequences_2022CN));
|
2012-06-01 20:40:48 +00:00
|
|
|
results->set(textIn, this, confidence);
|
|
|
|
return (confidence > 0);
|
2006-02-06 18:03:11 +00:00
|
|
|
}
|
2015-02-20 19:31:33 +00:00
|
|
|
#endif
|
2006-02-06 18:03:11 +00:00
|
|
|
|
2012-06-01 20:40:48 +00:00
|
|
|
CharsetRecog_2022::~CharsetRecog_2022() {
|
2006-02-06 18:03:11 +00:00
|
|
|
// nothing to do
|
|
|
|
}
|
|
|
|
|
|
|
|
U_NAMESPACE_END
|
2006-05-09 18:06:10 +00:00
|
|
|
#endif
|