2 This file is part of the KDE libraries
4 Copyright (C) 1999 Lars Knoll (knoll@mpi-hd.mpg.de)
5 Copyright (C) 2003, 2004, 2005, 2006 Apple Computer, Inc.
6 Copyright (C) 2005, 2006 Alexey Proskuryakov (ap@nypop.com)
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Library General Public
10 License as published by the Free Software Foundation; either
11 version 2 of the License, or (at your option) any later version.
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Library General Public License for more details.
18 You should have received a copy of the GNU Library General Public License
19 along with this library; see the file COPYING.LIB. If not, write to
20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA.
29 #include "DOMImplementation.h"
30 #include "DeprecatedCString.h"
31 #include "DeprecatedString.h"
32 #include "HTMLNames.h"
33 #include "StreamingTextDecoder.h"
37 using namespace HTMLNames;
41 enum Type { ASCII, JIS, EUC, SJIS, UTF16, UTF8 };
42 static enum Type judge(const char* str, int length);
43 static const int ESC = 0x1b;
44 static const unsigned char sjisMap[256];
45 static int ISkanji(int code)
49 return sjisMap[code & 0xff] & 1;
51 static int ISkana(int code)
55 return sjisMap[code & 0xff] & 2;
59 const unsigned char KanjiCode::sjisMap[256] = {
60 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
61 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
62 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
63 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
64 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
65 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
66 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
67 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
68 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
69 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
70 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
71 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
72 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
73 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
74 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
75 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0
80 * [0xa1 - 0xfe][0xa1 - 0xfe]
81 * 0x8e[0xa1 - 0xfe](SS2)
82 * 0x8f[0xa1 - 0xfe][0xa1 - 0xfe](SS3)
85 * [0x81 - 0x9f, 0xe0 - 0xef(0xfe?)][0x40 - 0x7e, 0x80 - 0xfc]
87 * Shift_Jis Hankaku Kana is
92 * KanjiCode::judge() is based on judge_jcode() from jvim
93 * http://hp.vector.co.jp/authors/VA003457/vim/
95 * Special Thanks to Kenichi Tsuchida
98 enum KanjiCode::Type KanjiCode::judge(const char* str, int size)
102 int bfr = false; /* Kana Moji */
103 int bfk = 0; /* EUC Kana */
107 const unsigned char* ptr = reinterpret_cast<const unsigned char*>(str);
113 if (ptr[i] == ESC && (size - i >= 3)) {
114 if ((ptr[i + 1] == '$' && ptr[i + 2] == 'B')
115 || (ptr[i + 1] == '(' && ptr[i + 2] == 'B')) {
118 } else if ((ptr[i + 1] == '$' && ptr[i + 2] == '@')
119 || (ptr[i + 1] == '(' && ptr[i + 2] == 'J')) {
122 } else if (ptr[i + 1] == '(' && ptr[i + 2] == 'I') {
125 } else if (ptr[i + 1] == ')' && ptr[i + 2] == 'I') {
137 /* ?? check kudokuten ?? && ?? hiragana ?? */
138 if ((i >= 2) && (ptr[i - 2] == 0x81)
139 && (0x41 <= ptr[i - 1] && ptr[i - 1] <= 0x49)) {
141 sjis += 100; /* kudokuten */
142 } else if ((i >= 2) && (ptr[i - 2] == 0xa1)
143 && (0xa2 <= ptr[i - 1] && ptr[i - 1] <= 0xaa)) {
145 euc += 100; /* kudokuten */
146 } else if ((i >= 2) && (ptr[i - 2] == 0x82) && (0xa0 <= ptr[i - 1])) {
147 sjis += 40; /* hiragana */
148 } else if ((i >= 2) && (ptr[i - 2] == 0xa4) && (0xa0 <= ptr[i - 1])) {
149 euc += 40; /* hiragana */
152 /* ?? check hiragana or katana ?? */
153 if ((size - i > 1) && (ptr[i] == 0x82) && (0xa0 <= ptr[i + 1])) {
154 sjis++; /* hiragana */
155 } else if ((size - i > 1) && (ptr[i] == 0x83)
156 && (0x40 <= ptr[i + 1] && ptr[i + 1] <= 0x9f)) {
157 sjis++; /* katakana */
158 } else if ((size - i > 1) && (ptr[i] == 0xa4) && (0xa0 <= ptr[i + 1])) {
159 euc++; /* hiragana */
160 } else if ((size - i > 1) && (ptr[i] == 0xa5) && (0xa0 <= ptr[i + 1])) {
161 euc++; /* katakana */
164 if ((i >= 1) && (0x40 <= ptr[i] && ptr[i] <= 0xa0) && ISkanji(ptr[i - 1])) {
167 } else if ((i >= 1) && (0x81 <= ptr[i - 1] && ptr[i - 1] <= 0x9f) && ((0x40 <= ptr[i] && ptr[i] < 0x7e) || (0x7e < ptr[i] && ptr[i] <= 0xfc))) {
170 } else if ((i >= 1) && (0xfd <= ptr[i] && ptr[i] <= 0xfe) && (0xa1 <= ptr[i - 1] && ptr[i - 1] <= 0xfe)) {
173 } else if ((i >= 1) && (0xfd <= ptr[i - 1] && ptr[i - 1] <= 0xfe) && (0xa1 <= ptr[i] && ptr[i] <= 0xfe)) {
176 } else if ((i >= 1) && (ptr[i] < 0xa0 || 0xdf < ptr[i]) && (0x8e == ptr[i - 1])) {
179 } else if (ptr[i] <= 0x7f) {
183 if (0xa1 <= ptr[i] && ptr[i] <= 0xa6) {
184 euc++; /* sjis hankaku kana kigo */
185 } else if (0xa1 <= ptr[i] && ptr[i] <= 0xdf) {
186 ; /* sjis hankaku kana */
187 } else if (0xa1 <= ptr[i] && ptr[i] <= 0xfe) {
189 } else if (0x8e == ptr[i]) {
191 } else if (0x20 <= ptr[i] && ptr[i] <= 0x7f) {
197 } else if (0x8e == ptr[i]) {
200 } else if (0xa1 <= ptr[i + 1] && ptr[i + 1] <= 0xdf) {
201 /* EUC KANA or SJIS KANJI */
212 } else if (0x81 <= ptr[i] && ptr[i] <= 0x9f) {
216 && ((0x40 <= ptr[i + 1] && ptr[i + 1] <= 0x7e)
217 || (0x80 <= ptr[i + 1] && ptr[i + 1] <= 0xfc))) {
220 } else if (0xfd <= ptr[i] && ptr[i] <= 0xfe) {
224 && (0xa1 <= ptr[i + 1] && ptr[i + 1] <= 0xfe)) {
227 } else if (ptr[i] <= 0x7f) {
240 } else if (sjis < euc) {
248 Decoder::ContentType Decoder::determineContentType(const String& mimeType)
250 if (equalIgnoringCase(mimeType, "text/css"))
252 if (equalIgnoringCase(mimeType, "text/html"))
254 if (DOMImplementation::isXMLMIMEType(mimeType))
259 const TextEncoding& Decoder::defaultEncoding(ContentType contentType, const TextEncoding& specifiedDefaultEncoding)
261 // Despite 8.5 "Text/xml with Omitted Charset" of RFC 3023, we assume UTF-8 instead of US-ASCII
262 // for text/xml. This matches Firefox.
263 if (contentType == XML)
264 return UTF8Encoding();
265 if (!specifiedDefaultEncoding.isValid())
266 return Latin1Encoding();
267 return specifiedDefaultEncoding;
270 Decoder::Decoder(const String& mimeType, const TextEncoding& specifiedDefaultEncoding)
271 : m_contentType(determineContentType(mimeType))
272 , m_decoder(defaultEncoding(m_contentType, specifiedDefaultEncoding))
273 , m_source(DefaultEncoding)
274 , m_checkedForBOM(false)
275 , m_checkedForCSSCharset(false)
276 , m_checkedForHeadCharset(false)
284 void Decoder::setEncoding(const TextEncoding& encoding, EncodingSource source)
286 // In case the encoding didn't exist, we keep the old one (helps some sites specifying invalid encodings).
287 if (!encoding.isValid())
290 if (source == EncodingFromMetaTag || source == EncodingFromXMLHeader || source == EncodingFromCSSCharset)
291 m_decoder.reset(encoding.closest8BitEquivalent());
293 m_decoder.reset(encoding);
298 // Returns the position of the encoding string.
299 static int findXMLEncoding(const DeprecatedCString &str, int &encodingLength)
301 int len = str.length();
303 int pos = str.find("encoding");
308 // Skip spaces and stray control characters.
309 while (str[pos] <= ' ' && pos != len)
317 // Skip spaces and stray control characters.
318 while (str[pos] <= ' ' && pos != len)
321 // Skip quotation mark.
322 char quoteMark = str[pos];
323 if (quoteMark != '"' && quoteMark != '\'')
327 // Find the trailing quotation mark.
329 while (str[end] != quoteMark)
335 encodingLength = end - pos;
339 // true if there is more to parse
340 static inline bool skipWhitespace(const char*& pos, const char* dataEnd)
342 while (pos < dataEnd && (*pos == '\t' || *pos == ' '))
344 return pos != dataEnd;
347 void Decoder::checkForBOM(const char* data, size_t len)
349 // Check for UTF-16 or UTF-8 BOM mark at the beginning, which is a sure sign of a Unicode encoding.
351 if (m_source == UserChosenEncoding) {
352 // FIXME: Maybe a BOM should override even a user-chosen encoding.
353 m_checkedForBOM = true;
357 // Check if we have enough data.
358 size_t bufferLength = m_buffer.size();
359 if (bufferLength + len < 3)
362 m_checkedForBOM = true;
364 // Extract the first three bytes.
365 // Handle the case where some of bytes are already in the buffer.
366 // The last byte is always guaranteed to not be in the buffer.
367 const unsigned char* udata = reinterpret_cast<const unsigned char*>(data);
368 unsigned char c1 = bufferLength >= 1 ? m_buffer[0] : *udata++;
369 unsigned char c2 = bufferLength >= 2 ? m_buffer[1] : *udata++;
370 ASSERT(bufferLength < 3);
371 unsigned char c3 = *udata;
373 // Check for the BOM.
374 if (c1 == 0xFE && c2 == 0xFF)
375 setEncoding(UTF16BigEndianEncoding(), AutoDetectedEncoding);
376 else if (c1 == 0xFF && c2 == 0xFE)
377 setEncoding(UTF16LittleEndianEncoding(), AutoDetectedEncoding);
378 else if (c1 == 0xEF && c2 == 0xBB && c3 == 0xBF)
379 setEncoding(UTF8Encoding(), AutoDetectedEncoding);
382 void Decoder::checkForCSSCharset(const char* data, size_t len)
384 if (m_source != DefaultEncoding) {
385 m_checkedForCSSCharset = true;
389 size_t oldSize = m_buffer.size();
390 m_buffer.resize(oldSize + len);
391 memcpy(m_buffer.data() + oldSize, data, len);
393 if (m_buffer.size() > 8) { // strlen("@charset") == 8
394 const char* dataStart = m_buffer.data();
395 const char* dataEnd = dataStart + m_buffer.size();
397 if (dataStart[0] == '@' && dataStart[1] == 'c' && dataStart[2] == 'h' && dataStart[3] == 'a' && dataStart[4] == 'r' &&
398 dataStart[5] == 's' && dataStart[6] == 'e' && dataStart[7] == 't') {
401 const char* pos = dataStart;
402 if (!skipWhitespace(pos, dataEnd))
405 if (*pos == '"' || *pos == '\'') {
406 char quotationMark = *pos;
410 while (pos < dataEnd && *pos != quotationMark)
415 DeprecatedCString encodingName(dataStart, pos - dataStart + 1);
418 if (!skipWhitespace(pos, dataEnd))
422 setEncoding(TextEncoding(encodingName), EncodingFromCSSCharset);
425 m_checkedForCSSCharset = true;
429 // Other browsers allow comments in the head section, so we need to also.
430 // It's important not to look for tags inside the comments.
431 static inline void skipComment(const char*& ptr, const char* pEnd)
434 // Allow <!-->; other browsers do.
440 // This is the real end of comment, "-->".
441 if (p[1] == '-' && p[2] == '>') {
445 // This is the incorrect end of comment that other browsers allow, "--!>".
446 if (p[1] == '-' && p[2] == '!' && p[3] == '>') {
457 bool Decoder::checkForHeadCharset(const char* data, size_t len, bool& movedDataToBuffer)
459 if (m_source != DefaultEncoding) {
460 m_checkedForHeadCharset = true;
464 // This is not completely efficient, since the function might go
465 // through the HTML head several times.
467 size_t oldSize = m_buffer.size();
468 m_buffer.resize(oldSize + len);
469 memcpy(m_buffer.data() + oldSize, data, len);
471 movedDataToBuffer = true;
473 // we still don't have an encoding, and are in the head
474 // the following tags are allowed in <head>:
475 // SCRIPT|STYLE|META|LINK|OBJECT|TITLE|BASE
477 // We stop scanning when a tag that is not permitted in <head>
478 // is seen, rather when </head> is seen, because that more closely
479 // matches behavior in other browsers; more details in
480 // <http://bugzilla.opendarwin.org/show_bug.cgi?id=3590>.
482 // Additionally, we ignore things that looks like tags in <title>; see
483 // <http://bugzilla.opendarwin.org/show_bug.cgi?id=4560>.
485 bool withinTitle = false;
487 const char* ptr = m_buffer.data();
488 const char* pEnd = ptr + m_buffer.size();
489 while (ptr != pEnd) {
495 if (ptr[0] == '!' && ptr[1] == '-' && ptr[2] == '-') {
497 skipComment(ptr, pEnd);
501 // Handle XML declaration, which can have encoding in it.
502 // This encoding is honored even for HTML documents.
503 if (ptr[0] == '?' && ptr[1] == 'x' && ptr[2] == 'm' && ptr[3] == 'l') {
504 const char* end = ptr;
505 while (*end != '>' && *end != '\0')
509 DeprecatedCString str(ptr, end - ptr);
511 int pos = findXMLEncoding(str, len);
513 setEncoding(TextEncoding(str.mid(pos, len)), EncodingFromXMLHeader);
514 // continue looking for a charset - it may be specified in an HTTP-Equiv meta
515 } else if (ptr[0] == 0 && ptr[1] == '?' && ptr[2] == 0 && ptr[3] == 'x' && ptr[4] == 0 && ptr[5] == 'm' && ptr[6] == 0 && ptr[7] == 'l') {
516 // UTF-16 without BOM
517 setEncoding(((ptr - m_buffer.data()) % 2) ? "UTF-16LE" : "UTF-16BE", AutoDetectedEncoding);
521 // the HTTP-EQUIV meta has no effect on XHTML
522 if (m_contentType == XML)
533 ((*ptr >= 'a') && (*ptr <= 'z') ||
534 (*ptr >= 'A') && (*ptr <= 'Z') ||
535 (*ptr >= '0') && (*ptr <= '9'))
538 tmp[len] = tolower(*ptr);
543 AtomicString tag(tmp);
548 if (!end && tag == metaTag) {
549 const char* end = ptr;
550 while (*end != '>' && *end != '\0')
554 DeprecatedCString str(ptr, (end-ptr)+1);
557 while (pos < (int)str.length()) {
558 if ((pos = str.find("charset", pos, false)) == -1)
562 while (pos < (int)str.length() && str[pos] <= ' ')
564 if (pos == (int)str.length())
566 if (str[pos++] != '=')
568 while (pos < (int)str.length() &&
569 (str[pos] <= ' ') || str[pos] == '=' || str[pos] == '"' || str[pos] == '\'')
573 if (pos == (int)str.length())
575 unsigned endpos = pos;
576 while (endpos < str.length() &&
577 str[endpos] != ' ' && str[endpos] != '"' && str[endpos] != '\'' &&
578 str[endpos] != ';' && str[endpos] != '>')
580 setEncoding(TextEncoding(str.mid(pos, endpos - pos)), EncodingFromMetaTag);
581 if (m_source == EncodingFromMetaTag)
584 if (endpos >= str.length() || str[endpos] == '/' || str[endpos] == '>')
589 } else if (tag != scriptTag && tag != noscriptTag && tag != styleTag &&
590 tag != linkTag && tag != metaTag && tag != objectTag &&
591 tag != titleTag && tag != baseTag &&
592 (end || tag != htmlTag) && !withinTitle &&
593 (tag != headTag) && isalpha(tmp[0])) {
594 m_checkedForHeadCharset = true;
604 void Decoder::detectJapaneseEncoding(const char* data, size_t len)
606 switch (KanjiCode::judge(data, len)) {
608 setEncoding("ISO-2022-JP", AutoDetectedEncoding);
611 setEncoding("EUC-JP", AutoDetectedEncoding);
613 case KanjiCode::SJIS:
614 setEncoding("Shift_JIS", AutoDetectedEncoding);
616 case KanjiCode::ASCII:
617 case KanjiCode::UTF16:
618 case KanjiCode::UTF8:
623 String Decoder::decode(const char* data, size_t len)
625 if (!m_checkedForBOM)
626 checkForBOM(data, len);
628 if (m_contentType == CSS && !m_checkedForCSSCharset) {
629 checkForCSSCharset(data, len);
633 bool movedDataToBuffer = false;
635 if ((m_contentType == HTML || m_contentType == XML) && !m_checkedForHeadCharset) { // HTML and XML
636 if (!checkForHeadCharset(data, len, movedDataToBuffer))
640 // Do the auto-detect if our default encoding is one of the Japanese ones.
641 // FIXME: It seems wrong to change our encoding downstream after we have already done some decoding.
642 if (m_source != UserChosenEncoding && m_source != AutoDetectedEncoding && encoding().isJapanese())
643 detectJapaneseEncoding(data, len);
645 ASSERT(encoding().isValid());
647 if (m_buffer.isEmpty())
648 return m_decoder.decode(data, len);
650 if (!movedDataToBuffer) {
651 size_t oldSize = m_buffer.size();
652 m_buffer.resize(oldSize + len);
653 memcpy(m_buffer.data() + oldSize, data, len);
656 String result = m_decoder.decode(m_buffer.data(), m_buffer.size());
661 String Decoder::flush()
663 String result = m_decoder.decode(m_buffer.data(), m_buffer.size(), true);