2
* Copyright (C) 2006, 2007 Apple 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 "TextEncodingRegistry.h"
29
#include "PlatformString.h"
30
#include "TextCodecLatin1.h"
31
#include "TextCodecUTF16.h"
33
#include <wtf/Assertions.h>
34
#include <wtf/HashMap.h>
37
#include "TextCodecICU.h"
40
#include "TextCodecMac.h"
43
#include "qt/TextCodecQt.h"
48
const size_t maxEncodingNameLength = 63;
50
// Hash for all-ASCII strings that does case folding and skips any characters
51
// that are not alphanumeric. If passed any non-ASCII characters, depends on
52
// the behavior of isalnum -- if that returns false as it does on OS X, then
53
// it will properly skip those characters too.
54
struct TextEncodingNameHash {
56
// Golden ratio - arbitrary start value to avoid mapping all 0's to all 0's
57
// or anything like that.
58
static const unsigned PHI = 0x9e3779b9U;
60
static bool equal(const char* s1, const char* s2)
67
while (c1 && !isalnum(c1));
70
while (c2 && !isalnum(c2));
71
if (tolower(c1) != tolower(c2))
77
// This algorithm is the one-at-a-time hash from:
78
// http://burtleburtle.net/bob/hash/hashfaq.html
79
// http://burtleburtle.net/bob/hash/doobs.html
80
static unsigned hash(const char* s)
93
} while (!isalnum(c));
102
struct TextCodecFactory {
103
NewTextCodecFunction function;
104
const void* additionalData;
105
TextCodecFactory(NewTextCodecFunction f = 0, const void* d = 0) : function(f), additionalData(d) { }
108
typedef HashMap<const char*, const char*, TextEncodingNameHash> TextEncodingNameMap;
109
typedef HashMap<const char*, TextCodecFactory> TextCodecMap;
111
static TextEncodingNameMap* textEncodingNameMap;
112
static TextCodecMap* textCodecMap;
113
static bool didExtendTextCodecMaps;
117
static inline void checkExistingName(const char*, const char*) { }
121
static void checkExistingName(const char* alias, const char* atomicName)
123
const char* oldAtomicName = textEncodingNameMap->get(alias);
126
if (oldAtomicName == atomicName)
128
// Keep the warning silent about one case where we know this will happen.
129
if (strcmp(alias, "ISO-8859-8-I") == 0
130
&& strcmp(oldAtomicName, "ISO-8859-8-I") == 0
131
&& strcmp(atomicName, "ISO_8859-8:1988") == 0)
133
LOG_ERROR("alias %s maps to %s already, but someone is trying to make it map to %s",
134
alias, oldAtomicName, atomicName);
139
static void addToTextEncodingNameMap(const char* alias, const char* name)
141
ASSERT(strlen(alias) <= maxEncodingNameLength);
142
const char* atomicName = textEncodingNameMap->get(name);
143
ASSERT(strcmp(alias, name) == 0 || atomicName);
146
checkExistingName(alias, atomicName);
147
textEncodingNameMap->add(alias, atomicName);
150
static void addToTextCodecMap(const char* name, NewTextCodecFunction function, const void* additionalData)
152
TextEncoding encoding(name);
153
ASSERT(encoding.isValid());
154
textCodecMap->add(encoding.name(), TextCodecFactory(function, additionalData));
157
static void buildBaseTextCodecMaps()
159
textCodecMap = new TextCodecMap;
160
textEncodingNameMap = new TextEncodingNameMap;
162
TextCodecLatin1::registerEncodingNames(addToTextEncodingNameMap);
163
TextCodecLatin1::registerCodecs(addToTextCodecMap);
165
TextCodecUTF16::registerEncodingNames(addToTextEncodingNameMap);
166
TextCodecUTF16::registerCodecs(addToTextCodecMap);
169
TextCodecICU::registerBaseEncodingNames(addToTextEncodingNameMap);
170
TextCodecICU::registerBaseCodecs(addToTextCodecMap);
174
static void extendTextCodecMaps()
177
TextCodecICU::registerExtendedEncodingNames(addToTextEncodingNameMap);
178
TextCodecICU::registerExtendedCodecs(addToTextCodecMap);
182
TextCodecQt::registerEncodingNames(addToTextEncodingNameMap);
183
TextCodecQt::registerCodecs(addToTextCodecMap);
187
TextCodecMac::registerEncodingNames(addToTextEncodingNameMap);
188
TextCodecMac::registerCodecs(addToTextCodecMap);
192
std::auto_ptr<TextCodec> newTextCodec(const TextEncoding& encoding)
194
ASSERT(textCodecMap);
195
TextCodecFactory factory = textCodecMap->get(encoding.name());
196
ASSERT(factory.function);
197
return factory.function(encoding, factory.additionalData);
200
const char* atomicCanonicalTextEncodingName(const char* name)
202
if (!name || !name[0])
204
if (!textEncodingNameMap)
205
buildBaseTextCodecMaps();
206
if (const char* atomicName = textEncodingNameMap->get(name))
208
if (didExtendTextCodecMaps)
210
extendTextCodecMaps();
211
didExtendTextCodecMaps = true;
212
return textEncodingNameMap->get(name);
215
const char* atomicCanonicalTextEncodingName(const UChar* characters, size_t length)
217
char buffer[maxEncodingNameLength + 1];
219
for (size_t i = 0; i < length; ++i) {
220
UChar c = characters[i];
222
if (j == maxEncodingNameLength)
228
return atomicCanonicalTextEncodingName(buffer);
231
bool noExtendedTextEncodingNameUsed()
233
return !didExtendTextCodecMaps;
236
} // namespace WebCore