michael@0: /* michael@0: ********************************************************************** michael@0: * Copyright (C) 2005-2013, 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 "csrucode.h" michael@0: #include "csmatch.h" michael@0: michael@0: U_NAMESPACE_BEGIN michael@0: michael@0: CharsetRecog_Unicode::~CharsetRecog_Unicode() michael@0: { michael@0: // nothing to do michael@0: } michael@0: michael@0: CharsetRecog_UTF_16_BE::~CharsetRecog_UTF_16_BE() michael@0: { michael@0: // nothing to do michael@0: } michael@0: michael@0: const char *CharsetRecog_UTF_16_BE::getName() const michael@0: { michael@0: return "UTF-16BE"; michael@0: } michael@0: michael@0: UBool CharsetRecog_UTF_16_BE::match(InputText* textIn, CharsetMatch *results) const michael@0: { michael@0: const uint8_t *input = textIn->fRawInput; michael@0: int32_t confidence = 0; michael@0: int32_t length = textIn->fRawLength; michael@0: michael@0: if (length >=2 && input[0] == 0xFE && input[1] == 0xFF) { michael@0: confidence = 100; michael@0: } michael@0: michael@0: // TODO: Do some statastics to check for unsigned UTF-16BE michael@0: results->set(textIn, this, confidence); michael@0: return (confidence > 0); michael@0: } michael@0: michael@0: CharsetRecog_UTF_16_LE::~CharsetRecog_UTF_16_LE() michael@0: { michael@0: // nothing to do michael@0: } michael@0: michael@0: const char *CharsetRecog_UTF_16_LE::getName() const michael@0: { michael@0: return "UTF-16LE"; michael@0: } michael@0: michael@0: UBool CharsetRecog_UTF_16_LE::match(InputText* textIn, CharsetMatch *results) const michael@0: { michael@0: const uint8_t *input = textIn->fRawInput; michael@0: int32_t confidence = 0; michael@0: int32_t length = textIn->fRawLength; michael@0: michael@0: if (length >= 4 && input[0] == 0xFF && input[1] == 0xFE && (input[2] != 0x00 || input[3] != 0x00)) { michael@0: confidence = 100; michael@0: } michael@0: michael@0: // TODO: Do some statastics to check for unsigned UTF-16LE michael@0: results->set(textIn, this, confidence); michael@0: return (confidence > 0); michael@0: } michael@0: michael@0: CharsetRecog_UTF_32::~CharsetRecog_UTF_32() michael@0: { michael@0: // nothing to do michael@0: } michael@0: michael@0: UBool CharsetRecog_UTF_32::match(InputText* textIn, CharsetMatch *results) const michael@0: { michael@0: const uint8_t *input = textIn->fRawInput; michael@0: int32_t limit = (textIn->fRawLength / 4) * 4; michael@0: int32_t numValid = 0; michael@0: int32_t numInvalid = 0; michael@0: bool hasBOM = FALSE; michael@0: int32_t confidence = 0; michael@0: michael@0: if (limit > 0 && getChar(input, 0) == 0x0000FEFFUL) { michael@0: hasBOM = TRUE; michael@0: } michael@0: michael@0: for(int32_t i = 0; i < limit; i += 4) { michael@0: int32_t ch = getChar(input, i); michael@0: michael@0: if (ch < 0 || ch >= 0x10FFFF || (ch >= 0xD800 && ch <= 0xDFFF)) { michael@0: numInvalid += 1; michael@0: } else { michael@0: numValid += 1; michael@0: } michael@0: } michael@0: michael@0: michael@0: // Cook up some sort of confidence score, based on presense of a BOM michael@0: // and the existence of valid and/or invalid multi-byte sequences. michael@0: if (hasBOM && numInvalid==0) { michael@0: confidence = 100; michael@0: } else if (hasBOM && numValid > numInvalid*10) { michael@0: confidence = 80; michael@0: } else if (numValid > 3 && numInvalid == 0) { michael@0: confidence = 100; michael@0: } else if (numValid > 0 && numInvalid == 0) { michael@0: confidence = 80; michael@0: } else if (numValid > numInvalid*10) { michael@0: // Probably corruput UTF-32BE data. Valid sequences aren't likely by chance. michael@0: confidence = 25; michael@0: } michael@0: michael@0: results->set(textIn, this, confidence); michael@0: return (confidence > 0); michael@0: } michael@0: michael@0: CharsetRecog_UTF_32_BE::~CharsetRecog_UTF_32_BE() michael@0: { michael@0: // nothing to do michael@0: } michael@0: michael@0: const char *CharsetRecog_UTF_32_BE::getName() const michael@0: { michael@0: return "UTF-32BE"; michael@0: } michael@0: michael@0: int32_t CharsetRecog_UTF_32_BE::getChar(const uint8_t *input, int32_t index) const michael@0: { michael@0: return input[index + 0] << 24 | input[index + 1] << 16 | michael@0: input[index + 2] << 8 | input[index + 3]; michael@0: } michael@0: michael@0: CharsetRecog_UTF_32_LE::~CharsetRecog_UTF_32_LE() michael@0: { michael@0: // nothing to do michael@0: } michael@0: michael@0: const char *CharsetRecog_UTF_32_LE::getName() const michael@0: { michael@0: return "UTF-32LE"; michael@0: } michael@0: michael@0: int32_t CharsetRecog_UTF_32_LE::getChar(const uint8_t *input, int32_t index) const michael@0: { michael@0: return input[index + 3] << 24 | input[index + 2] << 16 | michael@0: input[index + 1] << 8 | input[index + 0]; michael@0: } michael@0: michael@0: U_NAMESPACE_END michael@0: #endif michael@0: