~jconti/ubuntu/oneiric/webkit/fix_doc_path

« back to all changes in this revision

Viewing changes to WebCore/platform/TextEncodingRegistry.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Mike Hommey
  • Date: 2008-09-27 08:57:48 UTC
  • mfrom: (3.1.6 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080927085748-yhzld00w0rekp961
Tags: 1.0.1-4
WebCore/dom/Document.*, WebCore/loader/DocLoader.*: Avoid DoS via
crafted CSS import statements. Fixes: CVE-2008-3632. Closes: #499771.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
3
 
 *
4
 
 * Redistribution and use in source and binary forms, with or without
5
 
 * modification, are permitted provided that the following conditions
6
 
 * are met:
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.
12
 
 *
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. 
24
 
 */
25
 
 
26
 
#include "config.h"
27
 
#include "TextEncodingRegistry.h"
28
 
 
29
 
#include "PlatformString.h"
30
 
#include "TextCodecLatin1.h"
31
 
#include "TextCodecUTF16.h"
32
 
#include <ctype.h>
33
 
#include <wtf/Assertions.h>
34
 
#include <wtf/HashMap.h>
35
 
 
36
 
#if USE(ICU_UNICODE)
37
 
#include "TextCodecICU.h"
38
 
#endif
39
 
#if PLATFORM(MAC)
40
 
#include "TextCodecMac.h"
41
 
#endif
42
 
#if PLATFORM(QT)
43
 
#include "qt/TextCodecQt.h"
44
 
#endif
45
 
 
46
 
namespace WebCore {
47
 
 
48
 
const size_t maxEncodingNameLength = 63;
49
 
 
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 {
55
 
 
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;
59
 
 
60
 
    static bool equal(const char* s1, const char* s2)
61
 
    {
62
 
        char c1;
63
 
        char c2;
64
 
        do {
65
 
            do
66
 
                c1 = *s1++;
67
 
            while (c1 && !isalnum(c1));
68
 
            do
69
 
                c2 = *s2++;
70
 
            while (c2 && !isalnum(c2));
71
 
            if (tolower(c1) != tolower(c2))
72
 
                return false;
73
 
        } while (c1 && c2);
74
 
        return !c1 && !c2;
75
 
    }
76
 
 
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)
81
 
    {
82
 
        unsigned h = PHI;
83
 
        for (;;) {
84
 
            char c;
85
 
            do {
86
 
                c = *s++;
87
 
                if (!c) {
88
 
                    h += (h << 3);
89
 
                    h ^= (h >> 11);
90
 
                    h += (h << 15);
91
 
                    return h;
92
 
                }
93
 
            } while (!isalnum(c));
94
 
            h += tolower(c);
95
 
            h += (h << 10); 
96
 
            h ^= (h >> 6); 
97
 
        }
98
 
    }
99
 
 
100
 
};
101
 
 
102
 
struct TextCodecFactory {
103
 
    NewTextCodecFunction function;
104
 
    const void* additionalData;
105
 
    TextCodecFactory(NewTextCodecFunction f = 0, const void* d = 0) : function(f), additionalData(d) { }
106
 
};
107
 
 
108
 
typedef HashMap<const char*, const char*, TextEncodingNameHash> TextEncodingNameMap;
109
 
typedef HashMap<const char*, TextCodecFactory> TextCodecMap;
110
 
 
111
 
static TextEncodingNameMap* textEncodingNameMap;
112
 
static TextCodecMap* textCodecMap;
113
 
static bool didExtendTextCodecMaps;
114
 
 
115
 
#if ERROR_DISABLED
116
 
 
117
 
static inline void checkExistingName(const char*, const char*) { }
118
 
 
119
 
#else
120
 
 
121
 
static void checkExistingName(const char* alias, const char* atomicName)
122
 
{
123
 
    const char* oldAtomicName = textEncodingNameMap->get(alias);
124
 
    if (!oldAtomicName)
125
 
        return;
126
 
    if (oldAtomicName == atomicName)
127
 
        return;
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)
132
 
        return;
133
 
    LOG_ERROR("alias %s maps to %s already, but someone is trying to make it map to %s",
134
 
        alias, oldAtomicName, atomicName);
135
 
}
136
 
 
137
 
#endif
138
 
 
139
 
static void addToTextEncodingNameMap(const char* alias, const char* name)
140
 
{
141
 
    ASSERT(strlen(alias) <= maxEncodingNameLength);
142
 
    const char* atomicName = textEncodingNameMap->get(name);
143
 
    ASSERT(strcmp(alias, name) == 0 || atomicName);
144
 
    if (!atomicName)
145
 
        atomicName = name;
146
 
    checkExistingName(alias, atomicName);
147
 
    textEncodingNameMap->add(alias, atomicName);
148
 
}
149
 
 
150
 
static void addToTextCodecMap(const char* name, NewTextCodecFunction function, const void* additionalData)
151
 
{
152
 
    TextEncoding encoding(name);
153
 
    ASSERT(encoding.isValid());
154
 
    textCodecMap->add(encoding.name(), TextCodecFactory(function, additionalData));
155
 
}
156
 
 
157
 
static void buildBaseTextCodecMaps()
158
 
{
159
 
    textCodecMap = new TextCodecMap;
160
 
    textEncodingNameMap = new TextEncodingNameMap;
161
 
 
162
 
    TextCodecLatin1::registerEncodingNames(addToTextEncodingNameMap);
163
 
    TextCodecLatin1::registerCodecs(addToTextCodecMap);
164
 
 
165
 
    TextCodecUTF16::registerEncodingNames(addToTextEncodingNameMap);
166
 
    TextCodecUTF16::registerCodecs(addToTextCodecMap);
167
 
 
168
 
#if USE(ICU_UNICODE)
169
 
    TextCodecICU::registerBaseEncodingNames(addToTextEncodingNameMap);
170
 
    TextCodecICU::registerBaseCodecs(addToTextCodecMap);
171
 
#endif
172
 
}
173
 
 
174
 
static void extendTextCodecMaps()
175
 
{
176
 
#if USE(ICU_UNICODE)
177
 
    TextCodecICU::registerExtendedEncodingNames(addToTextEncodingNameMap);
178
 
    TextCodecICU::registerExtendedCodecs(addToTextCodecMap);
179
 
#endif
180
 
 
181
 
#if USE(QT4_UNICODE)
182
 
    TextCodecQt::registerEncodingNames(addToTextEncodingNameMap);
183
 
    TextCodecQt::registerCodecs(addToTextCodecMap);
184
 
#endif
185
 
 
186
 
#if PLATFORM(MAC)
187
 
    TextCodecMac::registerEncodingNames(addToTextEncodingNameMap);
188
 
    TextCodecMac::registerCodecs(addToTextCodecMap);
189
 
#endif
190
 
}
191
 
 
192
 
std::auto_ptr<TextCodec> newTextCodec(const TextEncoding& encoding)
193
 
{
194
 
    ASSERT(textCodecMap);
195
 
    TextCodecFactory factory = textCodecMap->get(encoding.name());
196
 
    ASSERT(factory.function);
197
 
    return factory.function(encoding, factory.additionalData);
198
 
}
199
 
 
200
 
const char* atomicCanonicalTextEncodingName(const char* name)
201
 
{
202
 
    if (!name || !name[0])
203
 
        return 0;
204
 
    if (!textEncodingNameMap)
205
 
        buildBaseTextCodecMaps();
206
 
    if (const char* atomicName = textEncodingNameMap->get(name))
207
 
        return atomicName;
208
 
    if (didExtendTextCodecMaps)
209
 
        return 0;
210
 
    extendTextCodecMaps();
211
 
    didExtendTextCodecMaps = true;
212
 
    return textEncodingNameMap->get(name);
213
 
}
214
 
 
215
 
const char* atomicCanonicalTextEncodingName(const UChar* characters, size_t length)
216
 
{
217
 
    char buffer[maxEncodingNameLength + 1];
218
 
    size_t j = 0;
219
 
    for (size_t i = 0; i < length; ++i) {
220
 
        UChar c = characters[i];
221
 
        if (isalnum(c)) {
222
 
            if (j == maxEncodingNameLength)
223
 
                return 0;
224
 
            buffer[j++] = c;
225
 
        }
226
 
    }
227
 
    buffer[j] = 0;
228
 
    return atomicCanonicalTextEncodingName(buffer);
229
 
}
230
 
 
231
 
bool noExtendedTextEncodingNameUsed()
232
 
{
233
 
    return !didExtendTextCodecMaps;
234
 
}
235
 
 
236
 
} // namespace WebCore