2 * Copyright (C) 2004, 2006, 2007, 2008 Apple Inc. All rights reserved.
3 * Copyright (C) 2006 Alexey Proskuryakov <ap@nypop.com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
28 #include "TextCodecICU.h"
30 #include "CharacterNames.h"
32 #include "PlatformString.h"
33 #include <unicode/ucnv.h>
34 #include <unicode/ucnv_cb.h>
35 #include <wtf/Assertions.h>
42 const size_t ConversionBufferSize = 16384;
44 static UConverter* cachedConverterICU;
46 static auto_ptr<TextCodec> newTextCodecICU(const TextEncoding& encoding, const void*)
48 return auto_ptr<TextCodec>(new TextCodecICU(encoding));
51 void TextCodecICU::registerBaseEncodingNames(EncodingNameRegistrar registrar)
53 registrar("UTF-8", "UTF-8");
56 void TextCodecICU::registerBaseCodecs(TextCodecRegistrar registrar)
58 registrar("UTF-8", newTextCodecICU, 0);
61 // FIXME: Registering all the encodings we get from ucnv_getAvailableName
62 // includes encodings we don't want or need. For example: UTF16_PlatformEndian,
63 // UTF16_OppositeEndian, UTF32_PlatformEndian, UTF32_OppositeEndian, and all
64 // the encodings with commas and version numbers.
66 void TextCodecICU::registerExtendedEncodingNames(EncodingNameRegistrar registrar)
68 // We register Hebrew with logical ordering using a separate name.
69 // Otherwise, this would share the same canonical name as the
70 // visual ordering case, and then TextEncoding could not tell them
71 // apart; ICU works with either name.
72 registrar("ISO-8859-8-I", "ISO-8859-8-I");
74 int32_t numEncodings = ucnv_countAvailable();
75 for (int32_t i = 0; i < numEncodings; ++i) {
76 const char* name = ucnv_getAvailableName(i);
77 UErrorCode error = U_ZERO_ERROR;
78 // FIXME: Should we use the "MIME" standard instead of "IANA"?
79 const char* standardName = ucnv_getStandardName(name, "IANA", &error);
80 if (!U_SUCCESS(error) || !standardName)
83 // 1. Treat GB2312 encoding as GBK (its more modern superset), to match other browsers.
84 // 2. On the Web, GB2312 is encoded as EUC-CN or HZ, while ICU provides a native encoding
85 // for encoding GB_2312-80 and several others. So, we need to override this behavior, too.
86 if (strcmp(standardName, "GB2312") == 0 || strcmp(standardName, "GB_2312-80") == 0)
88 // Similarly, EUC-KR encodings all map to an extended version.
89 else if (strcmp(standardName, "KS_C_5601-1987") == 0 || strcmp(standardName, "EUC-KR") == 0)
90 standardName = "windows-949-2000";
92 else if (strcmp(standardName, "ISO_8859-9:1989") == 0)
93 standardName = "windows-1254";
95 registrar(standardName, standardName);
97 uint16_t numAliases = ucnv_countAliases(name, &error);
98 ASSERT(U_SUCCESS(error));
100 for (uint16_t j = 0; j < numAliases; ++j) {
101 error = U_ZERO_ERROR;
102 const char* alias = ucnv_getAlias(name, j, &error);
103 ASSERT(U_SUCCESS(error));
104 if (U_SUCCESS(error) && alias != standardName)
105 registrar(alias, standardName);
109 // Additional aliases.
110 // Perhaps we can get these added to ICU.
111 registrar("macroman", "macintosh");
112 registrar("xmacroman", "macintosh");
114 // Additional aliases that historically were present in the encoding
115 // table in WebKit on Macintosh that don't seem to be present in ICU.
116 // Perhaps we can prove these are not used on the web and remove them.
117 // Or perhaps we can get them added to ICU.
118 registrar("cnbig5", "Big5");
119 registrar("cngb", "EUC-CN");
120 registrar("csISO88598I", "ISO_8859-8-I");
121 registrar("csgb231280", "EUC-CN");
122 registrar("dos720", "cp864");
123 registrar("dos874", "cp874");
124 registrar("jis7", "ISO-2022-JP");
125 registrar("koi", "KOI8-R");
126 registrar("logical", "ISO-8859-8-I");
127 registrar("unicode11utf8", "UTF-8");
128 registrar("unicode20utf8", "UTF-8");
129 registrar("visual", "ISO-8859-8");
130 registrar("winarabic", "windows-1256");
131 registrar("winbaltic", "windows-1257");
132 registrar("wincyrillic", "windows-1251");
133 registrar("windows874", "cp874");
134 registrar("wingreek", "windows-1253");
135 registrar("winhebrew", "windows-1255");
136 registrar("winlatin2", "windows-1250");
137 registrar("winturkish", "windows-1254");
138 registrar("winvietnamese", "windows-1258");
139 registrar("xcp1250", "windows-1250");
140 registrar("xcp1251", "windows-1251");
141 registrar("xeuc", "EUC-JP");
142 registrar("xeuccn", "EUC-CN");
143 registrar("xgbk", "EUC-CN");
144 registrar("xunicode20utf8", "UTF-8");
145 registrar("xxbig5", "Big5");
148 void TextCodecICU::registerExtendedCodecs(TextCodecRegistrar registrar)
150 // See comment above in registerEncodingNames.
151 registrar("ISO-8859-8-I", newTextCodecICU, 0);
153 int32_t numEncodings = ucnv_countAvailable();
154 for (int32_t i = 0; i < numEncodings; ++i) {
155 const char* name = ucnv_getAvailableName(i);
156 UErrorCode error = U_ZERO_ERROR;
157 // FIXME: Should we use the "MIME" standard instead of "IANA"?
158 const char* standardName = ucnv_getStandardName(name, "IANA", &error);
159 if (!U_SUCCESS(error) || !standardName)
161 registrar(standardName, newTextCodecICU, 0);
165 TextCodecICU::TextCodecICU(const TextEncoding& encoding)
166 : m_encoding(encoding)
167 , m_numBufferedBytes(0)
169 , m_needsGBKFallbacks(false)
173 TextCodecICU::~TextCodecICU()
175 releaseICUConverter();
178 void TextCodecICU::releaseICUConverter() const
180 if (m_converterICU) {
181 if (cachedConverterICU)
182 ucnv_close(cachedConverterICU);
183 cachedConverterICU = m_converterICU;
188 void TextCodecICU::createICUConverter() const
190 ASSERT(!m_converterICU);
192 const char* name = m_encoding.name();
193 m_needsGBKFallbacks = name[0] == 'G' && name[1] == 'B' && name[2] == 'K' && !name[3];
197 if (cachedConverterICU) {
199 const char* cachedName = ucnv_getName(cachedConverterICU, &err);
200 if (U_SUCCESS(err) && m_encoding == cachedName) {
201 m_converterICU = cachedConverterICU;
202 cachedConverterICU = 0;
208 m_converterICU = ucnv_open(m_encoding.name(), &err);
210 if (err == U_AMBIGUOUS_ALIAS_WARNING)
211 LOG_ERROR("ICU ambiguous alias warning for encoding: %s", m_encoding.name());
214 ucnv_setFallback(m_converterICU, TRUE);
217 int TextCodecICU::decodeToBuffer(UChar* target, UChar* targetLimit, const char*& source, const char* sourceLimit, int32_t* offsets, bool flush, UErrorCode& err)
219 UChar* targetStart = target;
221 ucnv_toUnicode(m_converterICU, &target, targetLimit, &source, sourceLimit, offsets, flush, &err);
222 return target - targetStart;
225 class ErrorCallbackSetter {
227 ErrorCallbackSetter(UConverter* converter, bool stopOnError)
228 : m_converter(converter)
229 , m_shouldStopOnEncodingErrors(stopOnError)
231 if (m_shouldStopOnEncodingErrors) {
232 UErrorCode err = U_ZERO_ERROR;
233 ucnv_setToUCallBack(m_converter, UCNV_TO_U_CALLBACK_SUBSTITUTE,
234 UCNV_SUB_STOP_ON_ILLEGAL, &m_savedAction,
235 &m_savedContext, &err);
236 ASSERT(err == U_ZERO_ERROR);
239 ~ErrorCallbackSetter()
241 if (m_shouldStopOnEncodingErrors) {
242 UErrorCode err = U_ZERO_ERROR;
243 const void* oldContext;
244 UConverterToUCallback oldAction;
245 ucnv_setToUCallBack(m_converter, m_savedAction,
246 m_savedContext, &oldAction,
248 ASSERT(oldAction == UCNV_TO_U_CALLBACK_SUBSTITUTE);
249 ASSERT(!strcmp(static_cast<const char*>(oldContext), UCNV_SUB_STOP_ON_ILLEGAL));
250 ASSERT(err == U_ZERO_ERROR);
254 UConverter* m_converter;
255 bool m_shouldStopOnEncodingErrors;
256 const void* m_savedContext;
257 UConverterToUCallback m_savedAction;
260 String TextCodecICU::decode(const char* bytes, size_t length, bool flush, bool stopOnError, bool& sawError)
262 // Get a converter for the passed-in encoding.
263 if (!m_converterICU) {
264 createICUConverter();
265 ASSERT(m_converterICU);
266 if (!m_converterICU) {
267 LOG_ERROR("error creating ICU encoder even though encoding was in table");
272 ErrorCallbackSetter callbackSetter(m_converterICU, stopOnError);
274 Vector<UChar> result;
276 UChar buffer[ConversionBufferSize];
277 UChar* bufferLimit = buffer + ConversionBufferSize;
278 const char* source = reinterpret_cast<const char*>(bytes);
279 const char* sourceLimit = source + length;
280 int32_t* offsets = NULL;
281 UErrorCode err = U_ZERO_ERROR;
284 int ucharsDecoded = decodeToBuffer(buffer, bufferLimit, source, sourceLimit, offsets, flush, err);
285 result.append(buffer, ucharsDecoded);
286 } while (err == U_BUFFER_OVERFLOW_ERROR);
288 if (U_FAILURE(err)) {
289 // flush the converter so it can be reused, and not be bothered by this error.
291 decodeToBuffer(buffer, bufferLimit, source, sourceLimit, offsets, true, err);
292 } while (source < sourceLimit);
296 String resultString = String::adopt(result);
298 // <http://bugs.webkit.org/show_bug.cgi?id=17014>
299 // Simplified Chinese pages use the code A3A0 to mean "full-width space", but ICU decodes it as U+E5E5.
300 if (m_encoding == "GBK" || m_encoding == "gb18030")
301 resultString.replace(0xE5E5, ideographicSpace);
306 // We need to apply these fallbacks ourselves as they are not currently supported by ICU and
307 // they were provided by the old TEC encoding path
308 // Needed to fix <rdar://problem/4708689>
309 static UChar getGbkEscape(UChar32 codePoint)
325 // Invalid character handler when writing escaped entities for unrepresentable
326 // characters. See the declaration of TextCodec::encode for more.
327 static void urlEscapedEntityCallback(const void* context, UConverterFromUnicodeArgs* fromUArgs, const UChar* codeUnits, int32_t length,
328 UChar32 codePoint, UConverterCallbackReason reason, UErrorCode* err)
330 if (reason == UCNV_UNASSIGNED) {
333 UnencodableReplacementArray entity;
334 int entityLen = TextCodec::getUnencodableReplacement(codePoint, URLEncodedEntitiesForUnencodables, entity);
335 ucnv_cbFromUWriteBytes(fromUArgs, entity, entityLen, 0, err);
337 UCNV_FROM_U_CALLBACK_ESCAPE(context, fromUArgs, codeUnits, length, codePoint, reason, err);
340 // Substitutes special GBK characters, escaping all other unassigned entities.
341 static void gbkCallbackEscape(const void* context, UConverterFromUnicodeArgs* fromUArgs, const UChar* codeUnits, int32_t length,
342 UChar32 codePoint, UConverterCallbackReason reason, UErrorCode* err)
345 if (reason == UCNV_UNASSIGNED && (outChar = getGbkEscape(codePoint))) {
346 const UChar* source = &outChar;
348 ucnv_cbFromUWriteUChars(fromUArgs, &source, source + 1, 0, err);
351 UCNV_FROM_U_CALLBACK_ESCAPE(context, fromUArgs, codeUnits, length, codePoint, reason, err);
354 // Combines both gbkUrlEscapedEntityCallback and GBK character substitution.
355 static void gbkUrlEscapedEntityCallack(const void* context, UConverterFromUnicodeArgs* fromUArgs, const UChar* codeUnits, int32_t length,
356 UChar32 codePoint, UConverterCallbackReason reason, UErrorCode* err)
358 if (reason == UCNV_UNASSIGNED) {
359 if (UChar outChar = getGbkEscape(codePoint)) {
360 const UChar* source = &outChar;
362 ucnv_cbFromUWriteUChars(fromUArgs, &source, source + 1, 0, err);
365 urlEscapedEntityCallback(context, fromUArgs, codeUnits, length, codePoint, reason, err);
368 UCNV_FROM_U_CALLBACK_ESCAPE(context, fromUArgs, codeUnits, length, codePoint, reason, err);
371 static void gbkCallbackSubstitute(const void* context, UConverterFromUnicodeArgs* fromUArgs, const UChar* codeUnits, int32_t length,
372 UChar32 codePoint, UConverterCallbackReason reason, UErrorCode* err)
375 if (reason == UCNV_UNASSIGNED && (outChar = getGbkEscape(codePoint))) {
376 const UChar* source = &outChar;
378 ucnv_cbFromUWriteUChars(fromUArgs, &source, source + 1, 0, err);
381 UCNV_FROM_U_CALLBACK_SUBSTITUTE(context, fromUArgs, codeUnits, length, codePoint, reason, err);
384 CString TextCodecICU::encode(const UChar* characters, size_t length, UnencodableHandling handling)
390 createICUConverter();
394 // FIXME: We should see if there is "force ASCII range" mode in ICU;
395 // until then, we change the backslash into a yen sign.
396 // Encoding will change the yen sign back into a backslash.
397 String copy(characters, length);
398 copy.replace('\\', m_encoding.backslashAsCurrencySymbol());
400 const UChar* source = copy.characters();
401 const UChar* sourceLimit = source + copy.length();
403 UErrorCode err = U_ZERO_ERROR;
406 case QuestionMarksForUnencodables:
407 ucnv_setSubstChars(m_converterICU, "?", 1, &err);
408 ucnv_setFromUCallBack(m_converterICU, m_needsGBKFallbacks ? gbkCallbackSubstitute : UCNV_FROM_U_CALLBACK_SUBSTITUTE, 0, 0, 0, &err);
410 case EntitiesForUnencodables:
411 ucnv_setFromUCallBack(m_converterICU, m_needsGBKFallbacks ? gbkCallbackEscape : UCNV_FROM_U_CALLBACK_ESCAPE, UCNV_ESCAPE_XML_DEC, 0, 0, &err);
413 case URLEncodedEntitiesForUnencodables:
414 ucnv_setFromUCallBack(m_converterICU, m_needsGBKFallbacks ? gbkUrlEscapedEntityCallack : urlEscapedEntityCallback, 0, 0, 0, &err);
418 ASSERT(U_SUCCESS(err));
425 char buffer[ConversionBufferSize];
426 char* target = buffer;
427 char* targetLimit = target + ConversionBufferSize;
429 ucnv_fromUnicode(m_converterICU, &target, targetLimit, &source, sourceLimit, 0, true, &err);
430 size_t count = target - buffer;
431 result.grow(size + count);
432 memcpy(result.data() + size, buffer, count);
434 } while (err == U_BUFFER_OVERFLOW_ERROR);
436 return CString(result.data(), size);
440 } // namespace WebCore