Home | History | Annotate | Download | only in text
      1 /*
      2  * Copyright (C) 2004, 2006, 2007, 2008, 2011 Apple Inc. All rights reserved.
      3  * Copyright (C) 2006 Alexey Proskuryakov <ap (at) nypop.com>
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  *
     14  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
     15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
     18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     22  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     25  */
     26 
     27 #include "config.h"
     28 #include "wtf/text/TextCodecICU.h"
     29 
     30 #include <unicode/ucnv.h>
     31 #include <unicode/ucnv_cb.h>
     32 #include "wtf/Assertions.h"
     33 #include "wtf/StringExtras.h"
     34 #include "wtf/Threading.h"
     35 #include "wtf/WTFThreadData.h"
     36 #include "wtf/text/CString.h"
     37 #include "wtf/text/StringBuilder.h"
     38 #include "wtf/unicode/CharacterNames.h"
     39 
     40 using std::min;
     41 
     42 namespace WTF {
     43 
     44 const size_t ConversionBufferSize = 16384;
     45 
     46 ICUConverterWrapper::~ICUConverterWrapper()
     47 {
     48     if (converter)
     49         ucnv_close(converter);
     50 }
     51 
     52 static UConverter*& cachedConverterICU()
     53 {
     54     return wtfThreadData().cachedConverterICU().converter;
     55 }
     56 
     57 PassOwnPtr<TextCodec> TextCodecICU::create(const TextEncoding& encoding, const void*)
     58 {
     59     return adoptPtr(new TextCodecICU(encoding));
     60 }
     61 
     62 void TextCodecICU::registerEncodingNames(EncodingNameRegistrar registrar)
     63 {
     64     // We register Hebrew with logical ordering using a separate name.
     65     // Otherwise, this would share the same canonical name as the
     66     // visual ordering case, and then TextEncoding could not tell them
     67     // apart; ICU treats these names as synonyms.
     68     registrar("ISO-8859-8-I", "ISO-8859-8-I");
     69 
     70     int32_t numEncodings = ucnv_countAvailable();
     71     for (int32_t i = 0; i < numEncodings; ++i) {
     72         const char* name = ucnv_getAvailableName(i);
     73         UErrorCode error = U_ZERO_ERROR;
     74         // Try MIME before trying IANA to pick up commonly used names like
     75         // 'EUC-JP' instead of horrendously long names like
     76         // 'Extended_UNIX_Code_Packed_Format_for_Japanese'.
     77         const char* standardName = ucnv_getStandardName(name, "MIME", &error);
     78         if (!U_SUCCESS(error) || !standardName) {
     79             error = U_ZERO_ERROR;
     80             // Try IANA to pick up 'windows-12xx' and other names
     81             // which are not preferred MIME names but are widely used.
     82             standardName = ucnv_getStandardName(name, "IANA", &error);
     83             if (!U_SUCCESS(error) || !standardName)
     84                 continue;
     85         }
     86 
     87         // A number of these aliases are handled in Chrome's copy of ICU, but
     88         // Chromium can be compiled with the system ICU.
     89 
     90         // 1. Treat GB2312 encoding as GBK (its more modern superset), to match other browsers.
     91         // 2. On the Web, GB2312 is encoded as EUC-CN or HZ, while ICU provides a native encoding
     92         //    for encoding GB_2312-80 and several others. So, we need to override this behavior, too.
     93         if (!strcmp(standardName, "GB2312") || !strcmp(standardName, "GB_2312-80"))
     94             standardName = "GBK";
     95         // Similarly, EUC-KR encodings all map to an extended version, but
     96         // per HTML5, the canonical name still should be EUC-KR.
     97         else if (!strcmp(standardName, "EUC-KR") || !strcmp(standardName, "KSC_5601") || !strcmp(standardName, "cp1363"))
     98             standardName = "EUC-KR";
     99         // And so on.
    100         else if (!strcasecmp(standardName, "iso-8859-9")) // This name is returned in different case by ICU 3.2 and 3.6.
    101             standardName = "windows-1254";
    102         else if (!strcmp(standardName, "TIS-620"))
    103             standardName = "windows-874";
    104 
    105         registrar(standardName, standardName);
    106 
    107         uint16_t numAliases = ucnv_countAliases(name, &error);
    108         ASSERT(U_SUCCESS(error));
    109         if (U_SUCCESS(error))
    110             for (uint16_t j = 0; j < numAliases; ++j) {
    111                 error = U_ZERO_ERROR;
    112                 const char* alias = ucnv_getAlias(name, j, &error);
    113                 ASSERT(U_SUCCESS(error));
    114                 if (U_SUCCESS(error) && alias != standardName)
    115                     registrar(alias, standardName);
    116             }
    117     }
    118 
    119     // Additional aliases.
    120     // These are present in modern versions of ICU, but not in ICU 3.2 (shipped with Mac OS X 10.4).
    121     registrar("macroman", "macintosh");
    122     registrar("maccyrillic", "x-mac-cyrillic");
    123 
    124     // Additional aliases that historically were present in the encoding
    125     // table in WebKit on Macintosh that don't seem to be present in ICU.
    126     // Perhaps we can prove these are not used on the web and remove them.
    127     // Or perhaps we can get them added to ICU.
    128     registrar("x-mac-roman", "macintosh");
    129     registrar("x-mac-ukrainian", "x-mac-cyrillic");
    130     registrar("cn-big5", "Big5");
    131     registrar("x-x-big5", "Big5");
    132     registrar("cn-gb", "GBK");
    133     registrar("csgb231280", "GBK");
    134     registrar("x-euc-cn", "GBK");
    135     registrar("x-gbk", "GBK");
    136     registrar("csISO88598I", "ISO-8859-8-I");
    137     registrar("koi", "KOI8-R");
    138     registrar("logical", "ISO-8859-8-I");
    139     registrar("visual", "ISO-8859-8");
    140     registrar("winarabic", "windows-1256");
    141     registrar("winbaltic", "windows-1257");
    142     registrar("wincyrillic", "windows-1251");
    143     registrar("iso-8859-11", "windows-874");
    144     registrar("iso8859-11", "windows-874");
    145     registrar("dos-874", "windows-874");
    146     registrar("wingreek", "windows-1253");
    147     registrar("winhebrew", "windows-1255");
    148     registrar("winlatin2", "windows-1250");
    149     registrar("winturkish", "windows-1254");
    150     registrar("winvietnamese", "windows-1258");
    151     registrar("x-cp1250", "windows-1250");
    152     registrar("x-cp1251", "windows-1251");
    153     registrar("x-euc", "EUC-JP");
    154     registrar("x-windows-949", "EUC-KR");
    155     registrar("KSC5601", "EUC-KR");
    156     registrar("x-uhc", "EUC-KR");
    157     registrar("shift-jis", "Shift_JIS");
    158 
    159     // These aliases are present in modern versions of ICU, but use different codecs, and have no standard names.
    160     // They are not present in ICU 3.2.
    161     registrar("dos-720", "cp864");
    162     registrar("jis7", "ISO-2022-JP");
    163 
    164     // Alternative spelling of ISO encoding names.
    165     registrar("ISO8859-1", "ISO-8859-1");
    166     registrar("ISO8859-2", "ISO-8859-2");
    167     registrar("ISO8859-3", "ISO-8859-3");
    168     registrar("ISO8859-4", "ISO-8859-4");
    169     registrar("ISO8859-5", "ISO-8859-5");
    170     registrar("ISO8859-6", "ISO-8859-6");
    171     registrar("ISO8859-7", "ISO-8859-7");
    172     registrar("ISO8859-8", "ISO-8859-8");
    173     registrar("ISO8859-8-I", "ISO-8859-8-I");
    174     registrar("ISO8859-9", "ISO-8859-9");
    175     registrar("ISO8859-10", "ISO-8859-10");
    176     registrar("ISO8859-13", "ISO-8859-13");
    177     registrar("ISO8859-14", "ISO-8859-14");
    178     registrar("ISO8859-15", "ISO-8859-15");
    179     // Not registering ISO8859-16, because Firefox (as of version 3.6.6) doesn't know this particular alias,
    180     // and because older versions of ICU don't support ISO-8859-16 encoding at all.
    181 
    182     // Additional aliases present in the WHATWG Encoding Standard (http://encoding.spec.whatwg.org/)
    183     // and Firefox (24), but not in ICU 4.6.
    184     registrar("csiso58gb231280", "GBK");
    185     registrar("csiso88596e", "ISO-8859-6");
    186     registrar("csiso88596i", "ISO-8859-6");
    187     registrar("csiso88598e", "ISO-8859-8");
    188     registrar("gb_2312", "GBK");
    189     registrar("iso88591", "windows-1252");
    190     registrar("iso88592", "ISO-8859-2");
    191     registrar("iso88593", "ISO-8859-3");
    192     registrar("iso88594", "ISO-8859-4");
    193     registrar("iso88595", "ISO-8859-5");
    194     registrar("iso88596", "ISO-8859-6");
    195     registrar("iso88597", "ISO-8859-7");
    196     registrar("iso88598", "ISO-8859-8");
    197     registrar("iso88599", "windows-1254");
    198     registrar("iso885910", "ISO-8859-10");
    199     registrar("iso885911", "windows-874");
    200     registrar("iso885913", "ISO-8859-13");
    201     registrar("iso885914", "ISO-8859-14");
    202     registrar("iso885915", "ISO-8859-15");
    203     registrar("iso_8859-1", "windows-1252");
    204     registrar("iso_8859-2", "ISO-8859-2");
    205     registrar("iso_8859-3", "ISO-8859-3");
    206     registrar("iso_8859-4", "ISO-8859-4");
    207     registrar("iso_8859-5", "ISO-8859-5");
    208     registrar("iso_8859-6", "ISO-8859-6");
    209     registrar("iso_8859-7", "ISO-8859-7");
    210     registrar("iso_8859-8", "ISO-8859-8");
    211     registrar("iso_8859-9", "windows-1254");
    212     registrar("iso_8859-15", "ISO-8859-15");
    213     registrar("koi8_r", "KOI8-R");
    214     registrar("x-cp1252", "windows-1252");
    215     registrar("x-cp1253", "windows-1253");
    216     registrar("x-cp1254", "windows-1254");
    217     registrar("x-cp1255", "windows-1255");
    218     registrar("x-cp1256", "windows-1256");
    219     registrar("x-cp1257", "windows-1257");
    220     registrar("x-cp1258", "windows-1258");
    221 }
    222 
    223 void TextCodecICU::registerCodecs(TextCodecRegistrar registrar)
    224 {
    225     // See comment above in registerEncodingNames.
    226     registrar("ISO-8859-8-I", create, 0);
    227 
    228     int32_t numEncodings = ucnv_countAvailable();
    229     for (int32_t i = 0; i < numEncodings; ++i) {
    230         const char* name = ucnv_getAvailableName(i);
    231         UErrorCode error = U_ZERO_ERROR;
    232         const char* standardName = ucnv_getStandardName(name, "MIME", &error);
    233         if (!U_SUCCESS(error) || !standardName) {
    234             error = U_ZERO_ERROR;
    235             standardName = ucnv_getStandardName(name, "IANA", &error);
    236             if (!U_SUCCESS(error) || !standardName)
    237                 continue;
    238         }
    239         registrar(standardName, create, 0);
    240     }
    241 }
    242 
    243 TextCodecICU::TextCodecICU(const TextEncoding& encoding)
    244     : m_encoding(encoding)
    245     , m_converterICU(0)
    246     , m_needsGBKFallbacks(false)
    247 {
    248 }
    249 
    250 TextCodecICU::~TextCodecICU()
    251 {
    252     releaseICUConverter();
    253 }
    254 
    255 void TextCodecICU::releaseICUConverter() const
    256 {
    257     if (m_converterICU) {
    258         UConverter*& cachedConverter = cachedConverterICU();
    259         if (cachedConverter)
    260             ucnv_close(cachedConverter);
    261         cachedConverter = m_converterICU;
    262         m_converterICU = 0;
    263     }
    264 }
    265 
    266 void TextCodecICU::createICUConverter() const
    267 {
    268     ASSERT(!m_converterICU);
    269 
    270     const char* name = m_encoding.name();
    271     m_needsGBKFallbacks = name[0] == 'G' && name[1] == 'B' && name[2] == 'K' && !name[3];
    272 
    273     UErrorCode err;
    274 
    275     UConverter*& cachedConverter = cachedConverterICU();
    276     if (cachedConverter) {
    277         err = U_ZERO_ERROR;
    278         const char* cachedName = ucnv_getName(cachedConverter, &err);
    279         if (U_SUCCESS(err) && m_encoding == cachedName) {
    280             m_converterICU = cachedConverter;
    281             cachedConverter = 0;
    282             return;
    283         }
    284     }
    285 
    286     err = U_ZERO_ERROR;
    287     m_converterICU = ucnv_open(m_encoding.name(), &err);
    288 #if !LOG_DISABLED
    289     if (err == U_AMBIGUOUS_ALIAS_WARNING)
    290         WTF_LOG_ERROR("ICU ambiguous alias warning for encoding: %s", m_encoding.name());
    291 #endif
    292     if (m_converterICU)
    293         ucnv_setFallback(m_converterICU, TRUE);
    294 }
    295 
    296 int TextCodecICU::decodeToBuffer(UChar* target, UChar* targetLimit, const char*& source, const char* sourceLimit, int32_t* offsets, bool flush, UErrorCode& err)
    297 {
    298     UChar* targetStart = target;
    299     err = U_ZERO_ERROR;
    300     ucnv_toUnicode(m_converterICU, &target, targetLimit, &source, sourceLimit, offsets, flush, &err);
    301     return target - targetStart;
    302 }
    303 
    304 class ErrorCallbackSetter {
    305 public:
    306     ErrorCallbackSetter(UConverter* converter, bool stopOnError)
    307         : m_converter(converter)
    308         , m_shouldStopOnEncodingErrors(stopOnError)
    309     {
    310         if (m_shouldStopOnEncodingErrors) {
    311             UErrorCode err = U_ZERO_ERROR;
    312             ucnv_setToUCallBack(m_converter, UCNV_TO_U_CALLBACK_SUBSTITUTE,
    313                            UCNV_SUB_STOP_ON_ILLEGAL, &m_savedAction,
    314                            &m_savedContext, &err);
    315             ASSERT(err == U_ZERO_ERROR);
    316         }
    317     }
    318     ~ErrorCallbackSetter()
    319     {
    320         if (m_shouldStopOnEncodingErrors) {
    321             UErrorCode err = U_ZERO_ERROR;
    322             const void* oldContext;
    323             UConverterToUCallback oldAction;
    324             ucnv_setToUCallBack(m_converter, m_savedAction,
    325                    m_savedContext, &oldAction,
    326                    &oldContext, &err);
    327             ASSERT(oldAction == UCNV_TO_U_CALLBACK_SUBSTITUTE);
    328             ASSERT(!strcmp(static_cast<const char*>(oldContext), UCNV_SUB_STOP_ON_ILLEGAL));
    329             ASSERT(err == U_ZERO_ERROR);
    330         }
    331     }
    332 
    333 private:
    334     UConverter* m_converter;
    335     bool m_shouldStopOnEncodingErrors;
    336     const void* m_savedContext;
    337     UConverterToUCallback m_savedAction;
    338 };
    339 
    340 String TextCodecICU::decode(const char* bytes, size_t length, bool flush, bool stopOnError, bool& sawError)
    341 {
    342     // Get a converter for the passed-in encoding.
    343     if (!m_converterICU) {
    344         createICUConverter();
    345         ASSERT(m_converterICU);
    346         if (!m_converterICU) {
    347             WTF_LOG_ERROR("error creating ICU encoder even though encoding was in table");
    348             return String();
    349         }
    350     }
    351 
    352     ErrorCallbackSetter callbackSetter(m_converterICU, stopOnError);
    353 
    354     StringBuilder result;
    355 
    356     UChar buffer[ConversionBufferSize];
    357     UChar* bufferLimit = buffer + ConversionBufferSize;
    358     const char* source = reinterpret_cast<const char*>(bytes);
    359     const char* sourceLimit = source + length;
    360     int32_t* offsets = NULL;
    361     UErrorCode err = U_ZERO_ERROR;
    362 
    363     do {
    364         int ucharsDecoded = decodeToBuffer(buffer, bufferLimit, source, sourceLimit, offsets, flush, err);
    365         result.append(buffer, ucharsDecoded);
    366     } while (err == U_BUFFER_OVERFLOW_ERROR);
    367 
    368     if (U_FAILURE(err)) {
    369         // flush the converter so it can be reused, and not be bothered by this error.
    370         do {
    371             decodeToBuffer(buffer, bufferLimit, source, sourceLimit, offsets, true, err);
    372         } while (source < sourceLimit);
    373         sawError = true;
    374     }
    375 
    376     String resultString = result.toString();
    377 
    378     // <http://bugs.webkit.org/show_bug.cgi?id=17014>
    379     // Simplified Chinese pages use the code A3A0 to mean "full-width space", but ICU decodes it as U+E5E5.
    380     if (!strcmp(m_encoding.name(), "GBK") || !strcasecmp(m_encoding.name(), "gb18030"))
    381         resultString.replace(0xE5E5, ideographicSpace);
    382 
    383     return resultString;
    384 }
    385 
    386 // We need to apply these fallbacks ourselves as they are not currently supported by ICU and
    387 // they were provided by the old TEC encoding path. Needed to fix <rdar://problem/4708689>.
    388 static UChar fallbackForGBK(UChar32 character)
    389 {
    390     switch (character) {
    391     case 0x01F9:
    392         return 0xE7C8;
    393     case 0x1E3F:
    394         return 0xE7C7;
    395     case 0x22EF:
    396         return 0x2026;
    397     case 0x301C:
    398         return 0xFF5E;
    399     }
    400     return 0;
    401 }
    402 
    403 // Invalid character handler when writing escaped entities for unrepresentable
    404 // characters. See the declaration of TextCodec::encode for more.
    405 static void urlEscapedEntityCallback(const void* context, UConverterFromUnicodeArgs* fromUArgs, const UChar* codeUnits, int32_t length,
    406     UChar32 codePoint, UConverterCallbackReason reason, UErrorCode* err)
    407 {
    408     if (reason == UCNV_UNASSIGNED) {
    409         *err = U_ZERO_ERROR;
    410 
    411         UnencodableReplacementArray entity;
    412         int entityLen = TextCodec::getUnencodableReplacement(codePoint, URLEncodedEntitiesForUnencodables, entity);
    413         ucnv_cbFromUWriteBytes(fromUArgs, entity, entityLen, 0, err);
    414     } else
    415         UCNV_FROM_U_CALLBACK_ESCAPE(context, fromUArgs, codeUnits, length, codePoint, reason, err);
    416 }
    417 
    418 // Substitutes special GBK characters, escaping all other unassigned entities.
    419 static void gbkCallbackEscape(const void* context, UConverterFromUnicodeArgs* fromUArgs, const UChar* codeUnits, int32_t length,
    420     UChar32 codePoint, UConverterCallbackReason reason, UErrorCode* err)
    421 {
    422     UChar outChar;
    423     if (reason == UCNV_UNASSIGNED && (outChar = fallbackForGBK(codePoint))) {
    424         const UChar* source = &outChar;
    425         *err = U_ZERO_ERROR;
    426         ucnv_cbFromUWriteUChars(fromUArgs, &source, source + 1, 0, err);
    427         return;
    428     }
    429     UCNV_FROM_U_CALLBACK_ESCAPE(context, fromUArgs, codeUnits, length, codePoint, reason, err);
    430 }
    431 
    432 // Combines both gbkUrlEscapedEntityCallback and GBK character substitution.
    433 static void gbkUrlEscapedEntityCallack(const void* context, UConverterFromUnicodeArgs* fromUArgs, const UChar* codeUnits, int32_t length,
    434     UChar32 codePoint, UConverterCallbackReason reason, UErrorCode* err)
    435 {
    436     if (reason == UCNV_UNASSIGNED) {
    437         if (UChar outChar = fallbackForGBK(codePoint)) {
    438             const UChar* source = &outChar;
    439             *err = U_ZERO_ERROR;
    440             ucnv_cbFromUWriteUChars(fromUArgs, &source, source + 1, 0, err);
    441             return;
    442         }
    443         urlEscapedEntityCallback(context, fromUArgs, codeUnits, length, codePoint, reason, err);
    444         return;
    445     }
    446     UCNV_FROM_U_CALLBACK_ESCAPE(context, fromUArgs, codeUnits, length, codePoint, reason, err);
    447 }
    448 
    449 static void gbkCallbackSubstitute(const void* context, UConverterFromUnicodeArgs* fromUArgs, const UChar* codeUnits, int32_t length,
    450     UChar32 codePoint, UConverterCallbackReason reason, UErrorCode* err)
    451 {
    452     UChar outChar;
    453     if (reason == UCNV_UNASSIGNED && (outChar = fallbackForGBK(codePoint))) {
    454         const UChar* source = &outChar;
    455         *err = U_ZERO_ERROR;
    456         ucnv_cbFromUWriteUChars(fromUArgs, &source, source + 1, 0, err);
    457         return;
    458     }
    459     UCNV_FROM_U_CALLBACK_SUBSTITUTE(context, fromUArgs, codeUnits, length, codePoint, reason, err);
    460 }
    461 
    462 class TextCodecInput {
    463 public:
    464     TextCodecInput(const TextEncoding& encoding, const UChar* characters, size_t length)
    465         : m_begin(characters)
    466         , m_end(characters + length)
    467     { }
    468 
    469     TextCodecInput(const TextEncoding& encoding, const LChar* characters, size_t length)
    470     {
    471         m_buffer.reserveInitialCapacity(length);
    472         for (size_t i = 0; i < length; ++i)
    473             m_buffer.append(characters[i]);
    474         m_begin = m_buffer.data();
    475         m_end = m_begin + m_buffer.size();
    476     }
    477 
    478     const UChar* begin() const { return m_begin; }
    479     const UChar* end() const { return m_end; }
    480 
    481 private:
    482     const UChar* m_begin;
    483     const UChar* m_end;
    484     Vector<UChar> m_buffer;
    485 };
    486 
    487 CString TextCodecICU::encodeInternal(const TextCodecInput& input, UnencodableHandling handling)
    488 {
    489     const UChar* source = input.begin();
    490     const UChar* end = input.end();
    491 
    492     UErrorCode err = U_ZERO_ERROR;
    493 
    494     switch (handling) {
    495         case QuestionMarksForUnencodables:
    496             ucnv_setSubstChars(m_converterICU, "?", 1, &err);
    497             ucnv_setFromUCallBack(m_converterICU, m_needsGBKFallbacks ? gbkCallbackSubstitute : UCNV_FROM_U_CALLBACK_SUBSTITUTE, 0, 0, 0, &err);
    498             break;
    499         case EntitiesForUnencodables:
    500             ucnv_setFromUCallBack(m_converterICU, m_needsGBKFallbacks ? gbkCallbackEscape : UCNV_FROM_U_CALLBACK_ESCAPE, UCNV_ESCAPE_XML_DEC, 0, 0, &err);
    501             break;
    502         case URLEncodedEntitiesForUnencodables:
    503             ucnv_setFromUCallBack(m_converterICU, m_needsGBKFallbacks ? gbkUrlEscapedEntityCallack : urlEscapedEntityCallback, 0, 0, 0, &err);
    504             break;
    505     }
    506 
    507     ASSERT(U_SUCCESS(err));
    508     if (U_FAILURE(err))
    509         return CString();
    510 
    511     Vector<char> result;
    512     size_t size = 0;
    513     do {
    514         char buffer[ConversionBufferSize];
    515         char* target = buffer;
    516         char* targetLimit = target + ConversionBufferSize;
    517         err = U_ZERO_ERROR;
    518         ucnv_fromUnicode(m_converterICU, &target, targetLimit, &source, end, 0, true, &err);
    519         size_t count = target - buffer;
    520         result.grow(size + count);
    521         memcpy(result.data() + size, buffer, count);
    522         size += count;
    523     } while (err == U_BUFFER_OVERFLOW_ERROR);
    524 
    525     return CString(result.data(), size);
    526 }
    527 
    528 template<typename CharType>
    529 CString TextCodecICU::encodeCommon(const CharType* characters, size_t length, UnencodableHandling handling)
    530 {
    531     if (!length)
    532         return "";
    533 
    534     if (!m_converterICU)
    535         createICUConverter();
    536     if (!m_converterICU)
    537         return CString();
    538 
    539     TextCodecInput input(m_encoding, characters, length);
    540     return encodeInternal(input, handling);
    541 }
    542 
    543 CString TextCodecICU::encode(const UChar* characters, size_t length, UnencodableHandling handling)
    544 {
    545     return encodeCommon(characters, length, handling);
    546 }
    547 
    548 CString TextCodecICU::encode(const LChar* characters, size_t length, UnencodableHandling handling)
    549 {
    550     return encodeCommon(characters, length, handling);
    551 }
    552 
    553 } // namespace WTF
    554