This source file includes following definitions.
- bytesEqual
- bytesEqual
- bytesEqual
- bytesEqual
- find
- findTextEncoding
- determineContentType
- defaultEncoding
- m_usesEncodingDetector
- setEncoding
- findXMLEncoding
- checkForBOM
- checkForCSSCharset
- checkForXMLCharset
- checkForMetaCharset
- shouldAutoDetect
- decode
- flush
#include "config.h"
#include "core/html/parser/TextResourceDecoder.h"
#include "HTMLNames.h"
#include "core/dom/DOMImplementation.h"
#include "core/html/parser/HTMLMetaCharsetParser.h"
#include "platform/text/TextEncodingDetector.h"
#include "wtf/StringExtras.h"
#include "wtf/text/TextCodec.h"
#include "wtf/text/TextEncodingRegistry.h"
using namespace WTF;
namespace WebCore {
using namespace HTMLNames;
static inline bool bytesEqual(const char* p, char b0, char b1, char b2, char b3, char b4)
{
return p[0] == b0 && p[1] == b1 && p[2] == b2 && p[3] == b3 && p[4] == b4;
}
static inline bool bytesEqual(const char* p, char b0, char b1, char b2, char b3, char b4, char b5)
{
return p[0] == b0 && p[1] == b1 && p[2] == b2 && p[3] == b3 && p[4] == b4 && p[5] == b5;
}
static inline bool bytesEqual(const char* p, char b0, char b1, char b2, char b3, char b4, char b5, char b6, char b7)
{
return p[0] == b0 && p[1] == b1 && p[2] == b2 && p[3] == b3 && p[4] == b4 && p[5] == b5 && p[6] == b6 && p[7] == b7;
}
static inline bool bytesEqual(const char* p, char b0, char b1, char b2, char b3, char b4, char b5, char b6, char b7, char b8, char b9)
{
return p[0] == b0 && p[1] == b1 && p[2] == b2 && p[3] == b3 && p[4] == b4 && p[5] == b5 && p[6] == b6 && p[7] == b7 && p[8] == b8 && p[9] == b9;
}
static int find(const char* subject, size_t subjectLength, const char* target)
{
size_t targetLength = strlen(target);
if (targetLength > subjectLength)
return -1;
for (size_t i = 0; i <= subjectLength - targetLength; ++i) {
bool match = true;
for (size_t j = 0; j < targetLength; ++j) {
if (subject[i + j] != target[j]) {
match = false;
break;
}
}
if (match)
return i;
}
return -1;
}
static WTF::TextEncoding findTextEncoding(const char* encodingName, int length)
{
Vector<char, 64> buffer(length + 1);
memcpy(buffer.data(), encodingName, length);
buffer[length] = '\0';
return buffer.data();
}
TextResourceDecoder::ContentType TextResourceDecoder::determineContentType(const String& mimeType)
{
if (equalIgnoringCase(mimeType, "text/css"))
return CSSContent;
if (equalIgnoringCase(mimeType, "text/html"))
return HTMLContent;
if (DOMImplementation::isXMLMIMEType(mimeType))
return XMLContent;
return PlainTextContent;
}
const WTF::TextEncoding& TextResourceDecoder::defaultEncoding(ContentType contentType, const WTF::TextEncoding& specifiedDefaultEncoding)
{
if (contentType == XMLContent)
return UTF8Encoding();
if (!specifiedDefaultEncoding.isValid())
return Latin1Encoding();
return specifiedDefaultEncoding;
}
TextResourceDecoder::TextResourceDecoder(const String& mimeType, const WTF::TextEncoding& specifiedDefaultEncoding, bool usesEncodingDetector)
: m_contentType(determineContentType(mimeType))
, m_encoding(defaultEncoding(m_contentType, specifiedDefaultEncoding))
, m_source(DefaultEncoding)
, m_hintEncoding(0)
, m_checkedForBOM(false)
, m_checkedForCSSCharset(false)
, m_checkedForXMLCharset(false)
, m_checkedForMetaCharset(false)
, m_useLenientXMLDecoding(false)
, m_sawError(false)
, m_usesEncodingDetector(usesEncodingDetector)
{
}
TextResourceDecoder::~TextResourceDecoder()
{
}
void TextResourceDecoder::setEncoding(const WTF::TextEncoding& encoding, EncodingSource source)
{
if (!encoding.isValid())
return;
if (source == EncodingFromMetaTag && !strcasecmp(encoding.name(), "x-user-defined"))
m_encoding = "windows-1252";
else if (source == EncodingFromMetaTag || source == EncodingFromXMLHeader || source == EncodingFromCSSCharset)
m_encoding = encoding.closestByteBasedEquivalent();
else
m_encoding = encoding;
m_codec.clear();
m_source = source;
}
static int findXMLEncoding(const char* str, int len, int& encodingLength)
{
int pos = find(str, len, "encoding");
if (pos == -1)
return -1;
pos += 8;
while (pos < len && str[pos] <= ' ')
++pos;
if (pos >= len || str[pos] != '=')
return -1;
++pos;
while (pos < len && str[pos] <= ' ')
++pos;
if (pos >= len)
return - 1;
char quoteMark = str[pos];
if (quoteMark != '"' && quoteMark != '\'')
return -1;
++pos;
int end = pos;
while (end < len && str[end] != quoteMark)
++end;
if (end >= len)
return -1;
encodingLength = end - pos;
return pos;
}
size_t TextResourceDecoder::checkForBOM(const char* data, size_t len)
{
ASSERT(!m_checkedForBOM);
size_t lengthOfBOM = 0;
size_t bufferLength = m_buffer.size();
size_t buf1Len = bufferLength;
size_t buf2Len = len;
const unsigned char* buf1 = reinterpret_cast<const unsigned char*>(m_buffer.data());
const unsigned char* buf2 = reinterpret_cast<const unsigned char*>(data);
unsigned char c1 = buf1Len ? (--buf1Len, *buf1++) : buf2Len ? (--buf2Len, *buf2++) : 0;
unsigned char c2 = buf1Len ? (--buf1Len, *buf1++) : buf2Len ? (--buf2Len, *buf2++) : 0;
unsigned char c3 = buf1Len ? (--buf1Len, *buf1++) : buf2Len ? (--buf2Len, *buf2++) : 0;
unsigned char c4 = buf2Len ? (--buf2Len, *buf2++) : 0;
if (c1 == 0xFF && c2 == 0xFE) {
if (c3 || c4) {
setEncoding(UTF16LittleEndianEncoding(), AutoDetectedEncoding);
lengthOfBOM = 2;
} else {
setEncoding(UTF32LittleEndianEncoding(), AutoDetectedEncoding);
lengthOfBOM = 4;
}
} else if (c1 == 0xEF && c2 == 0xBB && c3 == 0xBF) {
setEncoding(UTF8Encoding(), AutoDetectedEncoding);
lengthOfBOM = 3;
} else if (c1 == 0xFE && c2 == 0xFF) {
setEncoding(UTF16BigEndianEncoding(), AutoDetectedEncoding);
lengthOfBOM = 2;
} else if (!c1 && !c2 && c3 == 0xFE && c4 == 0xFF) {
setEncoding(UTF32BigEndianEncoding(), AutoDetectedEncoding);
lengthOfBOM = 4;
}
if (lengthOfBOM || bufferLength + len >= 4)
m_checkedForBOM = true;
return lengthOfBOM;
}
bool TextResourceDecoder::checkForCSSCharset(const char* data, size_t len, bool& movedDataToBuffer)
{
if (m_source != DefaultEncoding && m_source != EncodingFromParentFrame) {
m_checkedForCSSCharset = true;
return true;
}
size_t oldSize = m_buffer.size();
m_buffer.grow(oldSize + len);
memcpy(m_buffer.data() + oldSize, data, len);
movedDataToBuffer = true;
if (m_buffer.size() <= 13)
return false;
const char* dataStart = m_buffer.data();
const char* dataEnd = dataStart + m_buffer.size();
if (bytesEqual(dataStart, '@', 'c', 'h', 'a', 'r', 's', 'e', 't', ' ', '"')) {
dataStart += 10;
const char* pos = dataStart;
while (pos < dataEnd && *pos != '"')
++pos;
if (pos == dataEnd)
return false;
int encodingNameLength = pos - dataStart;
++pos;
if (*pos == ';')
setEncoding(findTextEncoding(dataStart, encodingNameLength), EncodingFromCSSCharset);
}
m_checkedForCSSCharset = true;
return true;
}
bool TextResourceDecoder::checkForXMLCharset(const char* data, size_t len, bool& movedDataToBuffer)
{
if (m_source != DefaultEncoding && m_source != EncodingFromParentFrame) {
m_checkedForXMLCharset = true;
return true;
}
size_t oldSize = m_buffer.size();
m_buffer.grow(oldSize + len);
memcpy(m_buffer.data() + oldSize, data, len);
movedDataToBuffer = true;
const char* ptr = m_buffer.data();
const char* pEnd = ptr + m_buffer.size();
if (m_buffer.size() < 8)
return false;
if (bytesEqual(ptr, '<', '?', 'x', 'm', 'l')) {
const char* xmlDeclarationEnd = ptr;
while (xmlDeclarationEnd != pEnd && *xmlDeclarationEnd != '>')
++xmlDeclarationEnd;
if (xmlDeclarationEnd == pEnd)
return false;
int len = 0;
int pos = findXMLEncoding(ptr, xmlDeclarationEnd - ptr, len);
if (pos != -1)
setEncoding(findTextEncoding(ptr + pos, len), EncodingFromXMLHeader);
} else if (bytesEqual(ptr, '<', 0, '?', 0, 'x', 0)) {
setEncoding(UTF16LittleEndianEncoding(), AutoDetectedEncoding);
} else if (bytesEqual(ptr, 0, '<', 0, '?', 0, 'x')) {
setEncoding(UTF16BigEndianEncoding(), AutoDetectedEncoding);
} else if (bytesEqual(ptr, '<', 0, 0, 0, '?', 0, 0, 0)) {
setEncoding(UTF32LittleEndianEncoding(), AutoDetectedEncoding);
} else if (bytesEqual(ptr, 0, 0, 0, '<', 0, 0, 0, '?')) {
setEncoding(UTF32BigEndianEncoding(), AutoDetectedEncoding);
}
m_checkedForXMLCharset = true;
return true;
}
void TextResourceDecoder::checkForMetaCharset(const char* data, size_t length)
{
if (m_source == UserChosenEncoding || m_source == EncodingFromHTTPHeader || m_source == AutoDetectedEncoding) {
m_checkedForMetaCharset = true;
return;
}
if (!m_charsetParser)
m_charsetParser = HTMLMetaCharsetParser::create();
if (!m_charsetParser->checkForMetaCharset(data, length))
return;
setEncoding(m_charsetParser->encoding(), EncodingFromMetaTag);
m_charsetParser.clear();
m_checkedForMetaCharset = true;
return;
}
bool TextResourceDecoder::shouldAutoDetect() const
{
return m_usesEncodingDetector
&& (m_source == DefaultEncoding || (m_source == EncodingFromParentFrame && m_hintEncoding));
}
String TextResourceDecoder::decode(const char* data, size_t len)
{
size_t lengthOfBOM = 0;
if (!m_checkedForBOM)
lengthOfBOM = checkForBOM(data, len);
bool movedDataToBuffer = false;
if (m_contentType == CSSContent && !m_checkedForCSSCharset) {
if (!checkForCSSCharset(data, len, movedDataToBuffer))
return emptyString();
}
if ((m_contentType == HTMLContent || m_contentType == XMLContent) && !m_checkedForXMLCharset) {
if (!checkForXMLCharset(data, len, movedDataToBuffer))
return emptyString();
}
const char* dataForDecode = data + lengthOfBOM;
size_t lengthForDecode = len - lengthOfBOM;
if (!m_buffer.isEmpty()) {
if (!movedDataToBuffer) {
size_t oldSize = m_buffer.size();
m_buffer.grow(oldSize + len);
memcpy(m_buffer.data() + oldSize, data, len);
}
dataForDecode = m_buffer.data() + lengthOfBOM;
lengthForDecode = m_buffer.size() - lengthOfBOM;
}
if (m_contentType == HTMLContent && !m_checkedForMetaCharset)
checkForMetaCharset(dataForDecode, lengthForDecode);
if (shouldAutoDetect()) {
WTF::TextEncoding detectedEncoding;
if (detectTextEncoding(data, len, m_hintEncoding, &detectedEncoding))
setEncoding(detectedEncoding, EncodingFromContentSniffing);
}
ASSERT(m_encoding.isValid());
if (!m_codec)
m_codec = newTextCodec(m_encoding);
String result = m_codec->decode(dataForDecode, lengthForDecode, DoNotFlush, m_contentType == XMLContent && !m_useLenientXMLDecoding, m_sawError);
m_buffer.clear();
return result;
}
String TextResourceDecoder::flush()
{
if (m_buffer.size() && shouldAutoDetect()
&& ((!m_checkedForXMLCharset && (m_contentType == HTMLContent || m_contentType == XMLContent)) || (!m_checkedForCSSCharset && (m_contentType == CSSContent)))) {
WTF::TextEncoding detectedEncoding;
if (detectTextEncoding(m_buffer.data(), m_buffer.size(), m_hintEncoding, &detectedEncoding))
setEncoding(detectedEncoding, EncodingFromContentSniffing);
}
if (!m_codec)
m_codec = newTextCodec(m_encoding);
String result = m_codec->decode(m_buffer.data(), m_buffer.size(), FetchEOF, m_contentType == XMLContent && !m_useLenientXMLDecoding, m_sawError);
m_buffer.clear();
m_codec.clear();
m_checkedForBOM = false;
return result;
}
}