18e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project/*
28f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian * Copyright (C) 2004, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
38e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * Copyright (C) 2006 Alexey Proskuryakov <ap@nypop.com>
40bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch * Copyright (C) 2007-2009 Torch Mobile, Inc.
58e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project *
68e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * Redistribution and use in source and binary forms, with or without
78e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * modification, are permitted provided that the following conditions
88e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * are met:
98e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * 1. Redistributions of source code must retain the above copyright
108e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project *    notice, this list of conditions and the following disclaimer.
118e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * 2. Redistributions in binary form must reproduce the above copyright
128e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project *    notice, this list of conditions and the following disclaimer in the
138e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project *    documentation and/or other materials provided with the distribution.
148e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project *
158e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
168e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
178e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
188e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
198e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
208e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
218e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
228e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
238e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
248e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
258e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
268e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project */
278e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
288e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project#include "config.h"
298e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project#include "TextEncoding.h"
308e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
318e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project#include "CString.h"
328e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project#include "PlatformString.h"
338e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project#include "TextCodec.h"
348e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project#include "TextEncodingRegistry.h"
35643ca7872b450ea4efacab6188849e5aac2ba161Steve Block#if USE(ICU_UNICODE)
368e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project#include <unicode/unorm.h>
378e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project#elif USE(QT4_UNICODE)
388e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project#include <QString>
39643ca7872b450ea4efacab6188849e5aac2ba161Steve Block#elif USE(GLIB_UNICODE)
40643ca7872b450ea4efacab6188849e5aac2ba161Steve Block#include <glib.h>
41643ca7872b450ea4efacab6188849e5aac2ba161Steve Block#include <wtf/gtk/GOwnPtr.h>
428e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project#endif
438e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project#include <wtf/HashSet.h>
448e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project#include <wtf/OwnPtr.h>
45635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project#include <wtf/StdLibExtras.h>
468e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
478e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Projectnamespace WebCore {
488e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
498e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Projectstatic void addEncodingName(HashSet<const char*>& set, const char* name)
508e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project{
518e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    const char* atomicName = atomicCanonicalTextEncodingName(name);
528e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    if (atomicName)
538e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        set.add(atomicName);
548e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project}
558e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
56635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Projectstatic const TextEncoding& UTF7Encoding()
57635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project{
58635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project    static TextEncoding globalUTF7Encoding("UTF-7");
59635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project    return globalUTF7Encoding;
60635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project}
61635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project
628e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source ProjectTextEncoding::TextEncoding(const char* name)
638e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    : m_name(atomicCanonicalTextEncodingName(name))
64635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project    , m_backslashAsCurrencySymbol(backslashAsCurrencySymbol())
658e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project{
668e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project}
678e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
688e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source ProjectTextEncoding::TextEncoding(const String& name)
698e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    : m_name(atomicCanonicalTextEncodingName(name.characters(), name.length()))
70635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project    , m_backslashAsCurrencySymbol(backslashAsCurrencySymbol())
718e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project{
728e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project}
738e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
748e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source ProjectString TextEncoding::decode(const char* data, size_t length, bool stopOnError, bool& sawError) const
758e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project{
768e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    if (!m_name)
778e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        return String();
788e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
798f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian    return newTextCodec(*this)->decode(data, length, true, stopOnError, sawError);
808e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project}
818e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
828e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source ProjectCString TextEncoding::encode(const UChar* characters, size_t length, UnencodableHandling handling) const
838e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project{
848e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    if (!m_name)
858e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        return CString();
868e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
878e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    if (!length)
888e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        return "";
898e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
90643ca7872b450ea4efacab6188849e5aac2ba161Steve Block#if USE(ICU_UNICODE)
918e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    // FIXME: What's the right place to do normalization?
928e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    // It's a little strange to do it inside the encode function.
938e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    // Perhaps normalization should be an explicit step done before calling encode.
948e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
958e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    const UChar* source = characters;
968e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    size_t sourceLength = length;
978e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
988e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    Vector<UChar> normalizedCharacters;
998e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
1008e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    UErrorCode err = U_ZERO_ERROR;
1018e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    if (unorm_quickCheck(source, sourceLength, UNORM_NFC, &err) != UNORM_YES) {
1028e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        // First try using the length of the original string, since normalization to NFC rarely increases length.
1038e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        normalizedCharacters.grow(sourceLength);
1048e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        int32_t normalizedLength = unorm_normalize(source, length, UNORM_NFC, 0, normalizedCharacters.data(), length, &err);
1058e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        if (err == U_BUFFER_OVERFLOW_ERROR) {
1068e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project            err = U_ZERO_ERROR;
1078e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project            normalizedCharacters.resize(normalizedLength);
1088e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project            normalizedLength = unorm_normalize(source, length, UNORM_NFC, 0, normalizedCharacters.data(), normalizedLength, &err);
1098e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        }
1108e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        ASSERT(U_SUCCESS(err));
1118e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
1128e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        source = normalizedCharacters.data();
1138e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        sourceLength = normalizedLength;
1148e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    }
1158e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    return newTextCodec(*this)->encode(source, sourceLength, handling);
1168e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project#elif USE(QT4_UNICODE)
1178e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    QString str(reinterpret_cast<const QChar*>(characters), length);
1188e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    str = str.normalized(QString::NormalizationForm_C);
1198e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    return newTextCodec(*this)->encode(reinterpret_cast<const UChar *>(str.utf16()), str.length(), handling);
120643ca7872b450ea4efacab6188849e5aac2ba161Steve Block#elif USE(GLIB_UNICODE)
121643ca7872b450ea4efacab6188849e5aac2ba161Steve Block    GOwnPtr<char> UTF8Source;
122643ca7872b450ea4efacab6188849e5aac2ba161Steve Block    UTF8Source.set(g_utf16_to_utf8(characters, length, 0, 0, 0));
123643ca7872b450ea4efacab6188849e5aac2ba161Steve Block
124643ca7872b450ea4efacab6188849e5aac2ba161Steve Block    GOwnPtr<char> UTF8Normalized;
125643ca7872b450ea4efacab6188849e5aac2ba161Steve Block    UTF8Normalized.set(g_utf8_normalize(UTF8Source.get(), -1, G_NORMALIZE_NFC));
126643ca7872b450ea4efacab6188849e5aac2ba161Steve Block
127643ca7872b450ea4efacab6188849e5aac2ba161Steve Block    long UTF16Length;
128643ca7872b450ea4efacab6188849e5aac2ba161Steve Block    GOwnPtr<UChar> UTF16Normalized;
129643ca7872b450ea4efacab6188849e5aac2ba161Steve Block    UTF16Normalized.set(g_utf8_to_utf16(UTF8Normalized.get(), -1, 0, &UTF16Length, 0));
130643ca7872b450ea4efacab6188849e5aac2ba161Steve Block
131643ca7872b450ea4efacab6188849e5aac2ba161Steve Block    return newTextCodec(*this)->encode(UTF16Normalized.get(), UTF16Length, handling);
132d0825bca7fe65beaee391d30da42e937db621564Steve Block#elif OS(WINCE)
1330bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch    // normalization will be done by Windows CE API
1340bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch    OwnPtr<TextCodec> textCodec = newTextCodec(*this);
1350bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch    return textCodec.get() ? textCodec->encode(characters, length, handling) : CString();
1368e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project#endif
1378e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project}
1388e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
1395f1ab04193ad0130ca8204aadaceae083aca9881Feng Qianconst char* TextEncoding::domName() const
1405f1ab04193ad0130ca8204aadaceae083aca9881Feng Qian{
1415f1ab04193ad0130ca8204aadaceae083aca9881Feng Qian    if (noExtendedTextEncodingNameUsed())
1425f1ab04193ad0130ca8204aadaceae083aca9881Feng Qian        return m_name;
1435f1ab04193ad0130ca8204aadaceae083aca9881Feng Qian
1445f1ab04193ad0130ca8204aadaceae083aca9881Feng Qian    // We treat EUC-KR as windows-949 (its superset), but need to expose
1455f1ab04193ad0130ca8204aadaceae083aca9881Feng Qian    // the name 'EUC-KR' because the name 'windows-949' is not recognized by
1465f1ab04193ad0130ca8204aadaceae083aca9881Feng Qian    // most Korean web servers even though they do use the encoding
1475f1ab04193ad0130ca8204aadaceae083aca9881Feng Qian    // 'windows-949' with the name 'EUC-KR'.
1485f1ab04193ad0130ca8204aadaceae083aca9881Feng Qian    // FIXME: This is not thread-safe. At the moment, this function is
1495f1ab04193ad0130ca8204aadaceae083aca9881Feng Qian    // only accessed in a single thread, but eventually has to be made
1505f1ab04193ad0130ca8204aadaceae083aca9881Feng Qian    // thread-safe along with usesVisualOrdering().
1515f1ab04193ad0130ca8204aadaceae083aca9881Feng Qian    static const char* const a = atomicCanonicalTextEncodingName("windows-949");
1525f1ab04193ad0130ca8204aadaceae083aca9881Feng Qian    if (m_name == a)
1535f1ab04193ad0130ca8204aadaceae083aca9881Feng Qian        return "EUC-KR";
1545f1ab04193ad0130ca8204aadaceae083aca9881Feng Qian    return m_name;
1555f1ab04193ad0130ca8204aadaceae083aca9881Feng Qian}
1565f1ab04193ad0130ca8204aadaceae083aca9881Feng Qian
1578e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Projectbool TextEncoding::usesVisualOrdering() const
1588e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project{
1598e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    if (noExtendedTextEncodingNameUsed())
1608e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        return false;
1618e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
1628e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    static const char* const a = atomicCanonicalTextEncodingName("ISO-8859-8");
1638e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    return m_name == a;
1648e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project}
1658e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
1668e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Projectbool TextEncoding::isJapanese() const
1678e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project{
1688e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    if (noExtendedTextEncodingNameUsed())
1698e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        return false;
1708e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
171635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project    DEFINE_STATIC_LOCAL(HashSet<const char*>, set, ());
1728e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    if (set.isEmpty()) {
1738e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        addEncodingName(set, "x-mac-japanese");
1748e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        addEncodingName(set, "cp932");
1758e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        addEncodingName(set, "JIS_X0201");
1768e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        addEncodingName(set, "JIS_X0208-1983");
1778e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        addEncodingName(set, "JIS_X0208-1990");
1788e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        addEncodingName(set, "JIS_X0212-1990");
1798e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        addEncodingName(set, "JIS_C6226-1978");
1808e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        addEncodingName(set, "Shift_JIS_X0213-2000");
1818e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        addEncodingName(set, "ISO-2022-JP");
1828e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        addEncodingName(set, "ISO-2022-JP-2");
1838e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        addEncodingName(set, "ISO-2022-JP-1");
1848e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        addEncodingName(set, "ISO-2022-JP-3");
1858e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        addEncodingName(set, "EUC-JP");
1868e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        addEncodingName(set, "Shift_JIS");
1878e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    }
1888e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    return m_name && set.contains(m_name);
1898e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project}
1908e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
1918e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source ProjectUChar TextEncoding::backslashAsCurrencySymbol() const
1928e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project{
1938e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    if (noExtendedTextEncodingNameUsed())
1948e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        return '\\';
1958e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
1968e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    // The text encodings below treat backslash as a currency symbol.
1978e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    // See http://blogs.msdn.com/michkap/archive/2005/09/17/469941.aspx for more information.
1988e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    static const char* const a = atomicCanonicalTextEncodingName("Shift_JIS_X0213-2000");
1998e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    static const char* const b = atomicCanonicalTextEncodingName("EUC-JP");
2008e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    return (m_name == a || m_name == b) ? 0x00A5 : '\\';
2018e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project}
2028e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
203635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Projectbool TextEncoding::isNonByteBasedEncoding() const
204635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project{
2058f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian    if (noExtendedTextEncodingNameUsed()) {
2068f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian        return *this == UTF16LittleEndianEncoding()
2078f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian            || *this == UTF16BigEndianEncoding();
2088f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian    }
2098f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian
210635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project    return *this == UTF16LittleEndianEncoding()
2118f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian        || *this == UTF16BigEndianEncoding()
2128f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian        || *this == UTF32BigEndianEncoding()
2138f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian        || *this == UTF32LittleEndianEncoding();
2148f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian}
2158f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian
2168f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qianbool TextEncoding::isUTF7Encoding() const
2178f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian{
2188f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian    if (noExtendedTextEncodingNameUsed())
2198f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian        return false;
2208f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian
2218f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian    return *this == UTF7Encoding();
222635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project}
223635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project
224635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Projectconst TextEncoding& TextEncoding::closestByteBasedEquivalent() const
2258e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project{
226635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project    if (isNonByteBasedEncoding())
227635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project        return UTF8Encoding();
228635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project    return *this;
229635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project}
230635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project
231635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project// HTML5 specifies that UTF-8 be used in form submission when a form is
232635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project// is a part of a document in UTF-16 probably because UTF-16 is not a
233635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project// byte-based encoding and can contain 0x00. By extension, the same
234635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project// should be done for UTF-32. In case of UTF-7, it is a byte-based encoding,
235635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project// but it's fraught with problems and we'd rather steer clear of it.
236635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Projectconst TextEncoding& TextEncoding::encodingForFormSubmission() const
237635860845790a19bf50bbc51ba8fb66a96dde068The Android Open Source Project{
2388f72e70a9fd78eec56623b3a62e68f16b7b27e28Feng Qian    if (isNonByteBasedEncoding() || isUTF7Encoding())
2398e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project        return UTF8Encoding();
2408e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    return *this;
2418e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project}
2428e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
2438e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Projectconst TextEncoding& ASCIIEncoding()
2448e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project{
2458e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    static TextEncoding globalASCIIEncoding("ASCII");
2468e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    return globalASCIIEncoding;
2478e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project}
2488e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
2498e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Projectconst TextEncoding& Latin1Encoding()
2508e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project{
2518e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    static TextEncoding globalLatin1Encoding("Latin-1");
2528e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    return globalLatin1Encoding;
2538e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project}
2548e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
2558e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Projectconst TextEncoding& UTF16BigEndianEncoding()
2568e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project{
2578e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    static TextEncoding globalUTF16BigEndianEncoding("UTF-16BE");
2588e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    return globalUTF16BigEndianEncoding;
2598e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project}
2608e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
2618e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Projectconst TextEncoding& UTF16LittleEndianEncoding()
2628e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project{
2638e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    static TextEncoding globalUTF16LittleEndianEncoding("UTF-16LE");
2648e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    return globalUTF16LittleEndianEncoding;
2658e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project}
2668e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
2678e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Projectconst TextEncoding& UTF32BigEndianEncoding()
2688e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project{
2698e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    static TextEncoding globalUTF32BigEndianEncoding("UTF-32BE");
2708e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    return globalUTF32BigEndianEncoding;
2718e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project}
2728e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
2738e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Projectconst TextEncoding& UTF32LittleEndianEncoding()
2748e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project{
2758e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    static TextEncoding globalUTF32LittleEndianEncoding("UTF-32LE");
2768e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    return globalUTF32LittleEndianEncoding;
2778e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project}
2788e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
2798e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Projectconst TextEncoding& UTF8Encoding()
2808e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project{
2818e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    static TextEncoding globalUTF8Encoding("UTF-8");
282231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block    ASSERT(globalUTF8Encoding.isValid());
2838e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    return globalUTF8Encoding;
2848e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project}
2858e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
2868e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Projectconst TextEncoding& WindowsLatin1Encoding()
2878e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project{
2888e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    static TextEncoding globalWindowsLatin1Encoding("WinLatin-1");
2898e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project    return globalWindowsLatin1Encoding;
2908e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project}
2918e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project
2928e35f3cfc7fba1d1c829dc557ebad6409cbe16a2The Android Open Source Project} // namespace WebCore
293