1 /*
2 * Copyright (C) 2006 Lars Knoll <lars@trolltech.com>
3 * Copyright (C) 2008 Holger Hans Peter Freyther
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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.
25 */
26
27 #include "config.h"
28 #include "TextCodecQt.h"
29 #include "PlatformString.h"
30 #include "CString.h"
31 #include <qset.h>
32 // #include <QDebug>
33
34 namespace WebCore {
35
36 static QSet<QByteArray> *unique_names = 0;
37
getAtomicName(const QByteArray & name)38 static const char *getAtomicName(const QByteArray &name)
39 {
40 if (!unique_names)
41 unique_names = new QSet<QByteArray>;
42
43 unique_names->insert(name);
44 return unique_names->find(name)->constData();
45 }
46
registerEncodingNames(EncodingNameRegistrar registrar)47 void TextCodecQt::registerEncodingNames(EncodingNameRegistrar registrar)
48 {
49 QList<int> mibs = QTextCodec::availableMibs();
50 // qDebug() << ">>>>>>>>> registerEncodingNames";
51
52 for (int i = 0; i < mibs.size(); ++i) {
53 QTextCodec *c = QTextCodec::codecForMib(mibs.at(i));
54 const char *name = getAtomicName(c->name());
55 registrar(name, name);
56 // qDebug() << " " << name << name;
57 QList<QByteArray> aliases = c->aliases();
58 for (int i = 0; i < aliases.size(); ++i) {
59 const char *a = getAtomicName(aliases.at(i));
60 // qDebug() << " (a) " << a << name;
61 registrar(a, name);
62 }
63 }
64 }
65
newTextCodecQt(const TextEncoding & encoding,const void *)66 static PassOwnPtr<TextCodec> newTextCodecQt(const TextEncoding& encoding, const void*)
67 {
68 return new TextCodecQt(encoding);
69 }
70
registerCodecs(TextCodecRegistrar registrar)71 void TextCodecQt::registerCodecs(TextCodecRegistrar registrar)
72 {
73 QList<int> mibs = QTextCodec::availableMibs();
74 // qDebug() << ">>>>>>>>> registerCodecs";
75
76 for (int i = 0; i < mibs.size(); ++i) {
77 QTextCodec *c = QTextCodec::codecForMib(mibs.at(i));
78 const char *name = getAtomicName(c->name());
79 // qDebug() << " " << name;
80 registrar(name, newTextCodecQt, 0);
81 }
82 }
83
TextCodecQt(const TextEncoding & encoding)84 TextCodecQt::TextCodecQt(const TextEncoding& encoding)
85 : m_encoding(encoding)
86 {
87 m_codec = QTextCodec::codecForName(m_encoding.name());
88 }
89
~TextCodecQt()90 TextCodecQt::~TextCodecQt()
91 {
92 }
93
94
decode(const char * bytes,size_t length,bool flush,bool,bool & sawError)95 String TextCodecQt::decode(const char* bytes, size_t length, bool flush, bool /*stopOnError*/, bool& sawError)
96 {
97 // We chop input buffer to smaller buffers to avoid excessive memory consumption
98 // when the input buffer is big. This helps reduce peak memory consumption in
99 // mobile devices where system RAM is limited.
100 #if OS(SYMBIAN)
101 static const int MaxInputChunkSize = 32 * 1024;
102 #else
103 static const int MaxInputChunkSize = 1024 * 1024;
104 #endif
105 const char* buf = bytes;
106 const char* end = buf + length;
107 String unicode(""); // a non-null string is expected
108
109 while (buf < end) {
110 int size = end - buf;
111 size = qMin(size, MaxInputChunkSize);
112 QString decoded = m_codec->toUnicode(buf, size, &m_state);
113 unicode.append(decoded);
114 buf += size;
115 }
116
117 sawError = m_state.invalidChars != 0;
118
119 if (flush) {
120 m_state.flags = QTextCodec::DefaultConversion;
121 m_state.remainingChars = 0;
122 m_state.invalidChars = 0;
123 }
124
125 return unicode;
126 }
127
encode(const UChar * characters,size_t length,UnencodableHandling)128 CString TextCodecQt::encode(const UChar* characters, size_t length, UnencodableHandling)
129 {
130 if (!length)
131 return "";
132
133 // FIXME: do something sensible with UnencodableHandling
134
135 QByteArray ba = m_codec->fromUnicode(reinterpret_cast<const QChar*>(characters), length, 0);
136 return CString(ba.constData(), ba.length());
137 }
138
139
140 } // namespace WebCore
141