📄 textcodecicu.cpp
字号:
/* * Copyright (C) 2004, 2006, 2007, 2008 Apple Inc. All rights reserved. * Copyright (C) 2006 Alexey Proskuryakov <ap@nypop.com> * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */#include "config.h"#include "TextCodecICU.h"#include "CharacterNames.h"#include "CString.h"#include "PlatformString.h"#include "ThreadGlobalData.h"#include <unicode/ucnv.h>#include <unicode/ucnv_cb.h>#include <wtf/Assertions.h>#include <wtf/StringExtras.h>#include <wtf/Threading.h>using std::auto_ptr;using std::min;namespace WebCore {const size_t ConversionBufferSize = 16384;ICUConverterWrapper::~ICUConverterWrapper(){ if (converter) ucnv_close(converter);}static UConverter*& cachedConverterICU(){ return threadGlobalData().cachedConverterICU().converter;}static auto_ptr<TextCodec> newTextCodecICU(const TextEncoding& encoding, const void*){ return auto_ptr<TextCodec>(new TextCodecICU(encoding));}void TextCodecICU::registerBaseEncodingNames(EncodingNameRegistrar registrar){ registrar("UTF-8", "UTF-8");}void TextCodecICU::registerBaseCodecs(TextCodecRegistrar registrar){ registrar("UTF-8", newTextCodecICU, 0);}// FIXME: Registering all the encodings we get from ucnv_getAvailableName// includes encodings we don't want or need. For example, all// the encodings with commas and version numbers.void TextCodecICU::registerExtendedEncodingNames(EncodingNameRegistrar registrar){ // We register Hebrew with logical ordering using a separate name. // Otherwise, this would share the same canonical name as the // visual ordering case, and then TextEncoding could not tell them // apart; ICU treats these names as synonyms. registrar("ISO-8859-8-I", "ISO-8859-8-I"); int32_t numEncodings = ucnv_countAvailable(); for (int32_t i = 0; i < numEncodings; ++i) { const char* name = ucnv_getAvailableName(i); UErrorCode error = U_ZERO_ERROR; // Try MIME before trying IANA to pick up commonly used names like // 'EUC-JP' instead of horrendeously long names like // 'Extended_UNIX_Code_Packed_Format_for_Japanese'. const char* standardName = ucnv_getStandardName(name, "MIME", &error); if (!U_SUCCESS(error) || !standardName) { error = U_ZERO_ERROR; // Try IANA to pick up 'windows-12xx' and other names // which are not preferred MIME names but are widely used. standardName = ucnv_getStandardName(name, "IANA", &error); if (!U_SUCCESS(error) || !standardName) continue; } // 1. Treat GB2312 encoding as GBK (its more modern superset), to match other browsers. // 2. On the Web, GB2312 is encoded as EUC-CN or HZ, while ICU provides a native encoding // for encoding GB_2312-80 and several others. So, we need to override this behavior, too. if (strcmp(standardName, "GB2312") == 0 || strcmp(standardName, "GB_2312-80") == 0) standardName = "GBK"; // Similarly, EUC-KR encodings all map to an extended version. else if (strcmp(standardName, "KSC_5601") == 0 || strcmp(standardName, "EUC-KR") == 0 || strcmp(standardName, "cp1363") == 0) standardName = "windows-949"; // And so on. else if (strcasecmp(standardName, "iso-8859-9") == 0) // This name is returned in different case by ICU 3.2 and 3.6. standardName = "windows-1254"; else if (strcmp(standardName, "TIS-620") == 0) standardName = "windows-874"; registrar(standardName, standardName); uint16_t numAliases = ucnv_countAliases(name, &error); ASSERT(U_SUCCESS(error)); if (U_SUCCESS(error)) for (uint16_t j = 0; j < numAliases; ++j) { error = U_ZERO_ERROR; const char* alias = ucnv_getAlias(name, j, &error); ASSERT(U_SUCCESS(error)); if (U_SUCCESS(error) && alias != standardName) registrar(alias, standardName); } } // Additional aliases. // These are present in modern versions of ICU, but not in ICU 3.2 (shipped with Mac OS X 10.4). registrar("macroman", "macintosh"); registrar("maccyrillic", "x-mac-cyrillic"); // Additional aliases that historically were present in the encoding // table in WebKit on Macintosh that don't seem to be present in ICU. // Perhaps we can prove these are not used on the web and remove them. // Or perhaps we can get them added to ICU. registrar("xmacroman", "macintosh"); registrar("xmacukrainian", "x-mac-cyrillic"); registrar("cnbig5", "Big5"); registrar("xxbig5", "Big5"); registrar("cngb", "GBK"); registrar("csgb231280", "GBK"); registrar("xeuccn", "GBK"); registrar("xgbk", "GBK"); registrar("csISO88598I", "ISO_8859-8-I"); registrar("koi", "KOI8-R"); registrar("logical", "ISO-8859-8-I"); registrar("unicode11utf8", "UTF-8"); registrar("unicode20utf8", "UTF-8"); registrar("xunicode20utf8", "UTF-8"); registrar("visual", "ISO-8859-8"); registrar("winarabic", "windows-1256"); registrar("winbaltic", "windows-1257"); registrar("wincyrillic", "windows-1251"); registrar("iso885911", "windows-874"); registrar("dos874", "windows-874"); registrar("wingreek", "windows-1253"); registrar("winhebrew", "windows-1255"); registrar("winlatin2", "windows-1250"); registrar("winturkish", "windows-1254"); registrar("winvietnamese", "windows-1258"); registrar("xcp1250", "windows-1250"); registrar("xcp1251", "windows-1251"); registrar("xeuc", "EUC-JP"); registrar("xwindows949", "windows-949"); registrar("xuhc", "windows-949"); // These aliases are present in modern versions of ICU, but use different codecs, and have no standard names. // They are not present in ICU 3.2. registrar("dos720", "cp864"); registrar("jis7", "ISO-2022-JP");}void TextCodecICU::registerExtendedCodecs(TextCodecRegistrar registrar){ // See comment above in registerEncodingNames. registrar("ISO-8859-8-I", newTextCodecICU, 0); int32_t numEncodings = ucnv_countAvailable(); for (int32_t i = 0; i < numEncodings; ++i) { const char* name = ucnv_getAvailableName(i); UErrorCode error = U_ZERO_ERROR; const char* standardName = ucnv_getStandardName(name, "MIME", &error); if (!U_SUCCESS(error) || !standardName) { error = U_ZERO_ERROR; standardName = ucnv_getStandardName(name, "IANA", &error); if (!U_SUCCESS(error) || !standardName) continue; } registrar(standardName, newTextCodecICU, 0); }}TextCodecICU::TextCodecICU(const TextEncoding& encoding) : m_encoding(encoding) , m_numBufferedBytes(0) , m_converterICU(0) , m_needsGBKFallbacks(false){}TextCodecICU::~TextCodecICU(){ releaseICUConverter();}void TextCodecICU::releaseICUConverter() const{ if (m_converterICU) { UConverter*& cachedConverter = cachedConverterICU(); if (cachedConverter) ucnv_close(cachedConverter); cachedConverter = m_converterICU; m_converterICU = 0; }}void TextCodecICU::createICUConverter() const{ ASSERT(!m_converterICU); const char* name = m_encoding.name(); m_needsGBKFallbacks = name[0] == 'G' && name[1] == 'B' && name[2] == 'K' && !name[3]; UErrorCode err; UConverter*& cachedConverter = cachedConverterICU(); if (cachedConverter) { err = U_ZERO_ERROR; const char* cachedName = ucnv_getName(cachedConverter, &err); if (U_SUCCESS(err) && m_encoding == cachedName) { m_converterICU = cachedConverter; cachedConverter = 0; return; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -