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