1/* 2 * Copyright (C) 2004, 2006, 2007, 2008, 2011 Apple Inc. All rights reserved. 3 * Copyright (C) 2006 Alexey Proskuryakov <ap@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 40using std::min; 41 42namespace WTF { 43 44const size_t ConversionBufferSize = 16384; 45 46ICUConverterWrapper::~ICUConverterWrapper() 47{ 48 if (converter) 49 ucnv_close(converter); 50} 51 52static UConverter*& cachedConverterICU() 53{ 54 return wtfThreadData().cachedConverterICU().converter; 55} 56 57PassOwnPtr<TextCodec> TextCodecICU::create(const TextEncoding& encoding, const void*) 58{ 59 return adoptPtr(new TextCodecICU(encoding)); 60} 61 62void 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 alias for MacCyrillic not present in ICU. 120 registrar("maccyrillic", "x-mac-cyrillic"); 121 122 // Additional aliases that historically were present in the encoding 123 // table in WebKit on Macintosh that don't seem to be present in ICU. 124 // Perhaps we can prove these are not used on the web and remove them. 125 // Or perhaps we can get them added to ICU. 126 registrar("x-mac-roman", "macintosh"); 127 registrar("x-mac-ukrainian", "x-mac-cyrillic"); 128 registrar("cn-big5", "Big5"); 129 registrar("x-x-big5", "Big5"); 130 registrar("cn-gb", "GBK"); 131 registrar("csgb231280", "GBK"); 132 registrar("x-euc-cn", "GBK"); 133 registrar("x-gbk", "GBK"); 134 registrar("csISO88598I", "ISO-8859-8-I"); 135 registrar("koi", "KOI8-R"); 136 registrar("logical", "ISO-8859-8-I"); 137 registrar("visual", "ISO-8859-8"); 138 registrar("winarabic", "windows-1256"); 139 registrar("winbaltic", "windows-1257"); 140 registrar("wincyrillic", "windows-1251"); 141 registrar("iso-8859-11", "windows-874"); 142 registrar("iso8859-11", "windows-874"); 143 registrar("dos-874", "windows-874"); 144 registrar("wingreek", "windows-1253"); 145 registrar("winhebrew", "windows-1255"); 146 registrar("winlatin2", "windows-1250"); 147 registrar("winturkish", "windows-1254"); 148 registrar("winvietnamese", "windows-1258"); 149 registrar("x-cp1250", "windows-1250"); 150 registrar("x-cp1251", "windows-1251"); 151 registrar("x-euc", "EUC-JP"); 152 registrar("x-windows-949", "EUC-KR"); 153 registrar("KSC5601", "EUC-KR"); 154 registrar("x-uhc", "EUC-KR"); 155 registrar("shift-jis", "Shift_JIS"); 156 157 // Alternative spelling of ISO encoding names. 158 registrar("ISO8859-1", "ISO-8859-1"); 159 registrar("ISO8859-2", "ISO-8859-2"); 160 registrar("ISO8859-3", "ISO-8859-3"); 161 registrar("ISO8859-4", "ISO-8859-4"); 162 registrar("ISO8859-5", "ISO-8859-5"); 163 registrar("ISO8859-6", "ISO-8859-6"); 164 registrar("ISO8859-7", "ISO-8859-7"); 165 registrar("ISO8859-8", "ISO-8859-8"); 166 registrar("ISO8859-8-I", "ISO-8859-8-I"); 167 registrar("ISO8859-9", "ISO-8859-9"); 168 registrar("ISO8859-10", "ISO-8859-10"); 169 registrar("ISO8859-13", "ISO-8859-13"); 170 registrar("ISO8859-14", "ISO-8859-14"); 171 registrar("ISO8859-15", "ISO-8859-15"); 172 // No need to have an entry for ISO8859-16. ISO-8859-16 has just one label 173 // listed in WHATWG Encoding Living Standard (http://encoding.spec.whatwg.org/ ). 174 175 // Additional aliases present in the WHATWG Encoding Standard 176 // and Firefox (24), but not in ICU 4.6. 177 registrar("csiso58gb231280", "GBK"); 178 registrar("csiso88596e", "ISO-8859-6"); 179 registrar("csiso88596i", "ISO-8859-6"); 180 registrar("csiso88598e", "ISO-8859-8"); 181 registrar("gb_2312", "GBK"); 182 registrar("iso88591", "windows-1252"); 183 registrar("iso88592", "ISO-8859-2"); 184 registrar("iso88593", "ISO-8859-3"); 185 registrar("iso88594", "ISO-8859-4"); 186 registrar("iso88595", "ISO-8859-5"); 187 registrar("iso88596", "ISO-8859-6"); 188 registrar("iso88597", "ISO-8859-7"); 189 registrar("iso88598", "ISO-8859-8"); 190 registrar("iso88599", "windows-1254"); 191 registrar("iso885910", "ISO-8859-10"); 192 registrar("iso885911", "windows-874"); 193 registrar("iso885913", "ISO-8859-13"); 194 registrar("iso885914", "ISO-8859-14"); 195 registrar("iso885915", "ISO-8859-15"); 196 registrar("iso_8859-1", "windows-1252"); 197 registrar("iso_8859-2", "ISO-8859-2"); 198 registrar("iso_8859-3", "ISO-8859-3"); 199 registrar("iso_8859-4", "ISO-8859-4"); 200 registrar("iso_8859-5", "ISO-8859-5"); 201 registrar("iso_8859-6", "ISO-8859-6"); 202 registrar("iso_8859-7", "ISO-8859-7"); 203 registrar("iso_8859-8", "ISO-8859-8"); 204 registrar("iso_8859-9", "windows-1254"); 205 registrar("iso_8859-15", "ISO-8859-15"); 206 registrar("koi8_r", "KOI8-R"); 207 registrar("x-cp1252", "windows-1252"); 208 registrar("x-cp1253", "windows-1253"); 209 registrar("x-cp1254", "windows-1254"); 210 registrar("x-cp1255", "windows-1255"); 211 registrar("x-cp1256", "windows-1256"); 212 registrar("x-cp1257", "windows-1257"); 213 registrar("x-cp1258", "windows-1258"); 214} 215 216void TextCodecICU::registerCodecs(TextCodecRegistrar registrar) 217{ 218 // See comment above in registerEncodingNames. 219 registrar("ISO-8859-8-I", create, 0); 220 221 int32_t numEncodings = ucnv_countAvailable(); 222 for (int32_t i = 0; i < numEncodings; ++i) { 223 const char* name = ucnv_getAvailableName(i); 224 UErrorCode error = U_ZERO_ERROR; 225 const char* standardName = ucnv_getStandardName(name, "MIME", &error); 226 if (!U_SUCCESS(error) || !standardName) { 227 error = U_ZERO_ERROR; 228 standardName = ucnv_getStandardName(name, "IANA", &error); 229 if (!U_SUCCESS(error) || !standardName) 230 continue; 231 } 232 registrar(standardName, create, 0); 233 } 234} 235 236TextCodecICU::TextCodecICU(const TextEncoding& encoding) 237 : m_encoding(encoding) 238 , m_converterICU(0) 239 , m_needsGBKFallbacks(false) 240{ 241} 242 243TextCodecICU::~TextCodecICU() 244{ 245 releaseICUConverter(); 246} 247 248void TextCodecICU::releaseICUConverter() const 249{ 250 if (m_converterICU) { 251 UConverter*& cachedConverter = cachedConverterICU(); 252 if (cachedConverter) 253 ucnv_close(cachedConverter); 254 cachedConverter = m_converterICU; 255 m_converterICU = 0; 256 } 257} 258 259void TextCodecICU::createICUConverter() const 260{ 261 ASSERT(!m_converterICU); 262 263 const char* name = m_encoding.name(); 264 m_needsGBKFallbacks = name[0] == 'G' && name[1] == 'B' && name[2] == 'K' && !name[3]; 265 266 UErrorCode err; 267 268 UConverter*& cachedConverter = cachedConverterICU(); 269 if (cachedConverter) { 270 err = U_ZERO_ERROR; 271 const char* cachedName = ucnv_getName(cachedConverter, &err); 272 if (U_SUCCESS(err) && m_encoding == cachedName) { 273 m_converterICU = cachedConverter; 274 cachedConverter = 0; 275 return; 276 } 277 } 278 279 err = U_ZERO_ERROR; 280 m_converterICU = ucnv_open(m_encoding.name(), &err); 281#if !LOG_DISABLED 282 if (err == U_AMBIGUOUS_ALIAS_WARNING) 283 WTF_LOG_ERROR("ICU ambiguous alias warning for encoding: %s", m_encoding.name()); 284#endif 285 if (m_converterICU) 286 ucnv_setFallback(m_converterICU, TRUE); 287} 288 289int TextCodecICU::decodeToBuffer(UChar* target, UChar* targetLimit, const char*& source, const char* sourceLimit, int32_t* offsets, bool flush, UErrorCode& err) 290{ 291 UChar* targetStart = target; 292 err = U_ZERO_ERROR; 293 ucnv_toUnicode(m_converterICU, &target, targetLimit, &source, sourceLimit, offsets, flush, &err); 294 return target - targetStart; 295} 296 297class ErrorCallbackSetter { 298public: 299 ErrorCallbackSetter(UConverter* converter, bool stopOnError) 300 : m_converter(converter) 301 , m_shouldStopOnEncodingErrors(stopOnError) 302 { 303 if (m_shouldStopOnEncodingErrors) { 304 UErrorCode err = U_ZERO_ERROR; 305 ucnv_setToUCallBack(m_converter, UCNV_TO_U_CALLBACK_SUBSTITUTE, 306 UCNV_SUB_STOP_ON_ILLEGAL, &m_savedAction, 307 &m_savedContext, &err); 308 ASSERT(err == U_ZERO_ERROR); 309 } 310 } 311 ~ErrorCallbackSetter() 312 { 313 if (m_shouldStopOnEncodingErrors) { 314 UErrorCode err = U_ZERO_ERROR; 315 const void* oldContext; 316 UConverterToUCallback oldAction; 317 ucnv_setToUCallBack(m_converter, m_savedAction, 318 m_savedContext, &oldAction, 319 &oldContext, &err); 320 ASSERT(oldAction == UCNV_TO_U_CALLBACK_SUBSTITUTE); 321 ASSERT(!strcmp(static_cast<const char*>(oldContext), UCNV_SUB_STOP_ON_ILLEGAL)); 322 ASSERT(err == U_ZERO_ERROR); 323 } 324 } 325 326private: 327 UConverter* m_converter; 328 bool m_shouldStopOnEncodingErrors; 329 const void* m_savedContext; 330 UConverterToUCallback m_savedAction; 331}; 332 333String TextCodecICU::decode(const char* bytes, size_t length, FlushBehavior flush, bool stopOnError, bool& sawError) 334{ 335 // Get a converter for the passed-in encoding. 336 if (!m_converterICU) { 337 createICUConverter(); 338 ASSERT(m_converterICU); 339 if (!m_converterICU) { 340 WTF_LOG_ERROR("error creating ICU encoder even though encoding was in table"); 341 return String(); 342 } 343 } 344 345 ErrorCallbackSetter callbackSetter(m_converterICU, stopOnError); 346 347 StringBuilder result; 348 349 UChar buffer[ConversionBufferSize]; 350 UChar* bufferLimit = buffer + ConversionBufferSize; 351 const char* source = reinterpret_cast<const char*>(bytes); 352 const char* sourceLimit = source + length; 353 int32_t* offsets = NULL; 354 UErrorCode err = U_ZERO_ERROR; 355 356 do { 357 int ucharsDecoded = decodeToBuffer(buffer, bufferLimit, source, sourceLimit, offsets, flush != DoNotFlush, err); 358 result.append(buffer, ucharsDecoded); 359 } while (err == U_BUFFER_OVERFLOW_ERROR); 360 361 if (U_FAILURE(err)) { 362 // flush the converter so it can be reused, and not be bothered by this error. 363 do { 364 decodeToBuffer(buffer, bufferLimit, source, sourceLimit, offsets, true, err); 365 } while (source < sourceLimit); 366 sawError = true; 367 } 368 369 String resultString = result.toString(); 370 371 // <http://bugs.webkit.org/show_bug.cgi?id=17014> 372 // Simplified Chinese pages use the code A3A0 to mean "full-width space", but ICU decodes it as U+E5E5. 373 if (!strcmp(m_encoding.name(), "GBK") || !strcasecmp(m_encoding.name(), "gb18030")) 374 resultString.replace(0xE5E5, ideographicSpace); 375 376 return resultString; 377} 378 379// We need to apply these fallbacks ourselves as they are not currently supported by ICU and 380// they were provided by the old TEC encoding path. Needed to fix <rdar://problem/4708689>. 381static UChar fallbackForGBK(UChar32 character) 382{ 383 switch (character) { 384 case 0x01F9: 385 return 0xE7C8; 386 case 0x1E3F: 387 return 0xE7C7; 388 case 0x22EF: 389 return 0x2026; 390 case 0x301C: 391 return 0xFF5E; 392 } 393 return 0; 394} 395 396// Invalid character handler when writing escaped entities for unrepresentable 397// characters. See the declaration of TextCodec::encode for more. 398static void urlEscapedEntityCallback(const void* context, UConverterFromUnicodeArgs* fromUArgs, const UChar* codeUnits, int32_t length, 399 UChar32 codePoint, UConverterCallbackReason reason, UErrorCode* err) 400{ 401 if (reason == UCNV_UNASSIGNED) { 402 *err = U_ZERO_ERROR; 403 404 UnencodableReplacementArray entity; 405 int entityLen = TextCodec::getUnencodableReplacement(codePoint, URLEncodedEntitiesForUnencodables, entity); 406 ucnv_cbFromUWriteBytes(fromUArgs, entity, entityLen, 0, err); 407 } else 408 UCNV_FROM_U_CALLBACK_ESCAPE(context, fromUArgs, codeUnits, length, codePoint, reason, err); 409} 410 411// Substitutes special GBK characters, escaping all other unassigned entities. 412static void gbkCallbackEscape(const void* context, UConverterFromUnicodeArgs* fromUArgs, const UChar* codeUnits, int32_t length, 413 UChar32 codePoint, UConverterCallbackReason reason, UErrorCode* err) 414{ 415 UChar outChar; 416 if (reason == UCNV_UNASSIGNED && (outChar = fallbackForGBK(codePoint))) { 417 const UChar* source = &outChar; 418 *err = U_ZERO_ERROR; 419 ucnv_cbFromUWriteUChars(fromUArgs, &source, source + 1, 0, err); 420 return; 421 } 422 UCNV_FROM_U_CALLBACK_ESCAPE(context, fromUArgs, codeUnits, length, codePoint, reason, err); 423} 424 425// Combines both gbkUrlEscapedEntityCallback and GBK character substitution. 426static void gbkUrlEscapedEntityCallack(const void* context, UConverterFromUnicodeArgs* fromUArgs, const UChar* codeUnits, int32_t length, 427 UChar32 codePoint, UConverterCallbackReason reason, UErrorCode* err) 428{ 429 if (reason == UCNV_UNASSIGNED) { 430 if (UChar outChar = fallbackForGBK(codePoint)) { 431 const UChar* source = &outChar; 432 *err = U_ZERO_ERROR; 433 ucnv_cbFromUWriteUChars(fromUArgs, &source, source + 1, 0, err); 434 return; 435 } 436 urlEscapedEntityCallback(context, fromUArgs, codeUnits, length, codePoint, reason, err); 437 return; 438 } 439 UCNV_FROM_U_CALLBACK_ESCAPE(context, fromUArgs, codeUnits, length, codePoint, reason, err); 440} 441 442static void gbkCallbackSubstitute(const void* context, UConverterFromUnicodeArgs* fromUArgs, const UChar* codeUnits, int32_t length, 443 UChar32 codePoint, UConverterCallbackReason reason, UErrorCode* err) 444{ 445 UChar outChar; 446 if (reason == UCNV_UNASSIGNED && (outChar = fallbackForGBK(codePoint))) { 447 const UChar* source = &outChar; 448 *err = U_ZERO_ERROR; 449 ucnv_cbFromUWriteUChars(fromUArgs, &source, source + 1, 0, err); 450 return; 451 } 452 UCNV_FROM_U_CALLBACK_SUBSTITUTE(context, fromUArgs, codeUnits, length, codePoint, reason, err); 453} 454 455class TextCodecInput { 456public: 457 TextCodecInput(const TextEncoding& encoding, const UChar* characters, size_t length) 458 : m_begin(characters) 459 , m_end(characters + length) 460 { } 461 462 TextCodecInput(const TextEncoding& encoding, const LChar* characters, size_t length) 463 { 464 m_buffer.reserveInitialCapacity(length); 465 for (size_t i = 0; i < length; ++i) 466 m_buffer.append(characters[i]); 467 m_begin = m_buffer.data(); 468 m_end = m_begin + m_buffer.size(); 469 } 470 471 const UChar* begin() const { return m_begin; } 472 const UChar* end() const { return m_end; } 473 474private: 475 const UChar* m_begin; 476 const UChar* m_end; 477 Vector<UChar> m_buffer; 478}; 479 480CString TextCodecICU::encodeInternal(const TextCodecInput& input, UnencodableHandling handling) 481{ 482 const UChar* source = input.begin(); 483 const UChar* end = input.end(); 484 485 UErrorCode err = U_ZERO_ERROR; 486 487 switch (handling) { 488 case QuestionMarksForUnencodables: 489 ucnv_setSubstChars(m_converterICU, "?", 1, &err); 490 ucnv_setFromUCallBack(m_converterICU, m_needsGBKFallbacks ? gbkCallbackSubstitute : UCNV_FROM_U_CALLBACK_SUBSTITUTE, 0, 0, 0, &err); 491 break; 492 case EntitiesForUnencodables: 493 ucnv_setFromUCallBack(m_converterICU, m_needsGBKFallbacks ? gbkCallbackEscape : UCNV_FROM_U_CALLBACK_ESCAPE, UCNV_ESCAPE_XML_DEC, 0, 0, &err); 494 break; 495 case URLEncodedEntitiesForUnencodables: 496 ucnv_setFromUCallBack(m_converterICU, m_needsGBKFallbacks ? gbkUrlEscapedEntityCallack : urlEscapedEntityCallback, 0, 0, 0, &err); 497 break; 498 } 499 500 ASSERT(U_SUCCESS(err)); 501 if (U_FAILURE(err)) 502 return CString(); 503 504 Vector<char> result; 505 size_t size = 0; 506 do { 507 char buffer[ConversionBufferSize]; 508 char* target = buffer; 509 char* targetLimit = target + ConversionBufferSize; 510 err = U_ZERO_ERROR; 511 ucnv_fromUnicode(m_converterICU, &target, targetLimit, &source, end, 0, true, &err); 512 size_t count = target - buffer; 513 result.grow(size + count); 514 memcpy(result.data() + size, buffer, count); 515 size += count; 516 } while (err == U_BUFFER_OVERFLOW_ERROR); 517 518 return CString(result.data(), size); 519} 520 521template<typename CharType> 522CString TextCodecICU::encodeCommon(const CharType* characters, size_t length, UnencodableHandling handling) 523{ 524 if (!length) 525 return ""; 526 527 if (!m_converterICU) 528 createICUConverter(); 529 if (!m_converterICU) 530 return CString(); 531 532 TextCodecInput input(m_encoding, characters, length); 533 return encodeInternal(input, handling); 534} 535 536CString TextCodecICU::encode(const UChar* characters, size_t length, UnencodableHandling handling) 537{ 538 return encodeCommon(characters, length, handling); 539} 540 541CString TextCodecICU::encode(const LChar* characters, size_t length, UnencodableHandling handling) 542{ 543 return encodeCommon(characters, length, handling); 544} 545 546} // namespace WTF 547