2 * Copyright (C) 2004, 2006 Apple Computer, Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "TextDecoder.h"
29 #include "TextEncodingRegistry.h"
31 // FIXME: Would be nice to also handle BOM for UTF-7 and UTF-32.
35 TextDecoder::TextDecoder(const TextEncoding& encoding)
36 : m_encoding(encoding)
37 , m_checkedForBOM(false)
38 , m_numBufferedBytes(0)
42 void TextDecoder::reset(const TextEncoding& encoding)
44 m_encoding = encoding;
46 m_checkedForBOM = false;
47 m_numBufferedBytes = 0;
50 String TextDecoder::checkForBOM(const char* data, size_t length, bool flush, bool stopOnError, bool& sawError)
52 // Check to see if we found a BOM.
53 size_t numBufferedBytes = m_numBufferedBytes;
54 size_t buf1Len = numBufferedBytes;
55 size_t buf2Len = length;
56 const unsigned char* buf1 = m_bufferedBytes;
57 const unsigned char* buf2 = reinterpret_cast<const unsigned char*>(data);
58 unsigned char c1 = buf1Len ? (--buf1Len, *buf1++) : buf2Len ? (--buf2Len, *buf2++) : 0;
59 unsigned char c2 = buf1Len ? (--buf1Len, *buf1++) : buf2Len ? (--buf2Len, *buf2++) : 0;
60 unsigned char c3 = buf1Len ? (--buf1Len, *buf1++) : buf2Len ? (--buf2Len, *buf2++) : 0;
61 unsigned char c4 = buf2Len ? (--buf2Len, *buf2++) : 0;
63 const TextEncoding* encodingConsideringBOM = &m_encoding;
65 if (c1 == 0xFF && c2 == 0xFE) {
66 if (c3 != 0 || c4 != 0)
67 encodingConsideringBOM = &UTF16LittleEndianEncoding();
68 else if (numBufferedBytes + length > sizeof(m_bufferedBytes))
69 encodingConsideringBOM = &UTF32LittleEndianEncoding();
73 else if (c1 == 0xEF && c2 == 0xBB && c3 == 0xBF)
74 encodingConsideringBOM = &UTF8Encoding();
75 else if (c1 == 0xFE && c2 == 0xFF)
76 encodingConsideringBOM = &UTF16BigEndianEncoding();
77 else if (c1 == 0 && c2 == 0 && c3 == 0xFE && c4 == 0xFF)
78 encodingConsideringBOM = &UTF32BigEndianEncoding();
81 if (!foundBOM && numBufferedBytes + length <= sizeof(m_bufferedBytes) && !flush) {
82 // Continue to look for the BOM.
83 memcpy(&m_bufferedBytes[numBufferedBytes], data, length);
84 m_numBufferedBytes += length;
88 // Done checking for BOM.
89 m_codec.set(newTextCodec(*encodingConsideringBOM).release());
92 m_checkedForBOM = true;
94 // Handle case where we have some buffered bytes to deal with.
95 if (numBufferedBytes) {
96 char bufferedBytes[sizeof(m_bufferedBytes)];
97 memcpy(bufferedBytes, m_bufferedBytes, numBufferedBytes);
98 m_numBufferedBytes = 0;
100 String bufferedResult = m_codec->decode(bufferedBytes, numBufferedBytes, false, stopOnError, sawError);
101 if (stopOnError && sawError)
102 return bufferedResult;
103 return bufferedResult + m_codec->decode(data, length, flush, stopOnError, sawError);
106 return m_codec->decode(data, length, flush, stopOnError, sawError);
109 } // namespace WebCore