Reviewed by Darin.
authorap <ap@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sat, 27 Oct 2007 08:40:23 +0000 (08:40 +0000)
committerap <ap@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sat, 27 Oct 2007 08:40:23 +0000 (08:40 +0000)
        http://bugs.webkit.org/show_bug.cgi?id=15555
        XMLHttpRequest does not support charset "x-user-defined", which can
        facilitate loading of binary data

        Test: http/tests/xmlhttprequest/binary-x-user-defined.html

        * WebCore.pro:
        * WebCore.vcproj/WebCore.vcproj:
        * WebCore.xcodeproj/project.pbxproj:
        * platform/TextCodecUserDefined.cpp: Added.
        (WebCore::TextCodecUserDefined::registerEncodingNames):
        (WebCore::newStreamingTextDecoderUserDefined):
        (WebCore::TextCodecUserDefined::registerCodecs):
        (WebCore::TextCodecUserDefined::decode):
        (WebCore::encodeComplexUserDefined):
        (WebCore::TextCodecUserDefined::encode):
        * platform/TextCodecUserDefined.h: Added.
        * platform/TextEncodingRegistry.cpp:
        (WebCore::buildBaseTextCodecMaps):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@27145 268f45cc-cd09-0410-ab3c-d52691b4dbfc

LayoutTests/ChangeLog
LayoutTests/http/tests/xmlhttprequest/binary-x-user-defined-expected.txt [new file with mode: 0644]
LayoutTests/http/tests/xmlhttprequest/binary-x-user-defined.html [new file with mode: 0644]
LayoutTests/http/tests/xmlhttprequest/resources/256.bin [new file with mode: 0644]
WebCore/ChangeLog
WebCore/WebCore.pro
WebCore/WebCore.vcproj/WebCore.vcproj
WebCore/WebCore.xcodeproj/project.pbxproj
WebCore/platform/TextCodecUserDefined.cpp [new file with mode: 0644]
WebCore/platform/TextCodecUserDefined.h [new file with mode: 0644]
WebCore/platform/TextEncodingRegistry.cpp

index a290f680655c52ebff62383e589618e7ccbce66c..2288890b372b36089a2401877541903d1cda1d54 100644 (file)
@@ -1,3 +1,15 @@
+2007-10-27  Alexey Proskuryakov  <ap@webkit.org>
+
+        Reviewed by Darin.
+
+        http://bugs.webkit.org/show_bug.cgi?id=15555
+        XMLHttpRequest does not support charset "x-user-defined", which can
+        facilitate loading of binary data
+
+        * http/tests/xmlhttprequest/binary-x-user-defined-expected.txt: Added.
+        * http/tests/xmlhttprequest/binary-x-user-defined.html: Added.
+        * http/tests/xmlhttprequest/resources/256.bin: Added.
+
 2007-10-27  Alexey Proskuryakov  <ap@webkit.org>
 
         Reviewed by Darin.
diff --git a/LayoutTests/http/tests/xmlhttprequest/binary-x-user-defined-expected.txt b/LayoutTests/http/tests/xmlhttprequest/binary-x-user-defined-expected.txt
new file mode 100644 (file)
index 0000000..a34734d
--- /dev/null
@@ -0,0 +1,3 @@
+Test for bug 15555: XMLHttpRequest does not support charset "x-user-defined", which can facilitate loading of binary data.
+
+SUCCESS
diff --git a/LayoutTests/http/tests/xmlhttprequest/binary-x-user-defined.html b/LayoutTests/http/tests/xmlhttprequest/binary-x-user-defined.html
new file mode 100644 (file)
index 0000000..358912a
--- /dev/null
@@ -0,0 +1,29 @@
+<p>Test for <a href="http://bugs.webkit.org/show_bug.cgi?id=15555">bug 15555</a>:
+XMLHttpRequest does not support charset "x-user-defined", which can facilitate loading of binary data.</p>
+<script>
+if (window.layoutTestController)
+  layoutTestController.dumpAsText();
+
+try {
+  req = new XMLHttpRequest;
+  req.overrideMimeType("text/plain; charset=x-user-defined");
+  req.open("GET", "resources/256.bin", false);
+  req.send();
+  for (var i = 0; i < 128; i++) {
+    var byt = req.responseText.charCodeAt(i);
+    if (byt != i)
+      throw "incorrect byte: " + byt + " (should be " + i + ")";
+  }
+
+  for (var i = 128; i < 256; i++) {
+    var byt = req.responseText.charCodeAt(i);
+    if (byt != i + 0xf700)
+      throw "incorrect byte: " + byt + " (should be " + (i + 0xf700) + ")";
+  }
+
+  document.write("<div>SUCCESS</div>");
+
+} catch (ex) {
+  document.write("<div>" + ex + "</div>");
+}
+</script>
diff --git a/LayoutTests/http/tests/xmlhttprequest/resources/256.bin b/LayoutTests/http/tests/xmlhttprequest/resources/256.bin
new file mode 100644 (file)
index 0000000..c866266
Binary files /dev/null and b/LayoutTests/http/tests/xmlhttprequest/resources/256.bin differ
index bc4d9c01dd56e04241087169f6a53c7b9ba72b66..a3ed15652a3b66448b3c20bb24d2dcc51ffa1b1c 100644 (file)
@@ -1,3 +1,27 @@
+2007-10-27  Alexey Proskuryakov  <ap@webkit.org>
+
+        Reviewed by Darin.
+
+        http://bugs.webkit.org/show_bug.cgi?id=15555
+        XMLHttpRequest does not support charset "x-user-defined", which can
+        facilitate loading of binary data
+
+        Test: http/tests/xmlhttprequest/binary-x-user-defined.html
+
+        * WebCore.pro:
+        * WebCore.vcproj/WebCore.vcproj:
+        * WebCore.xcodeproj/project.pbxproj:
+        * platform/TextCodecUserDefined.cpp: Added.
+        (WebCore::TextCodecUserDefined::registerEncodingNames):
+        (WebCore::newStreamingTextDecoderUserDefined):
+        (WebCore::TextCodecUserDefined::registerCodecs):
+        (WebCore::TextCodecUserDefined::decode):
+        (WebCore::encodeComplexUserDefined):
+        (WebCore::TextCodecUserDefined::encode):
+        * platform/TextCodecUserDefined.h: Added.
+        * platform/TextEncodingRegistry.cpp:
+        (WebCore::buildBaseTextCodecMaps):
+
 2007-10-27  Alexey Proskuryakov  <ap@webkit.org>
 
         Reviewed by Darin.
index 342ddfa4b4d7c0cb3e37f00ee8075d096249976e..510c88f8bbae670168b1806c788937d4516146ef 100644 (file)
@@ -671,6 +671,7 @@ SOURCES += \
     platform/StringImpl.cpp \
     platform/TextCodec.cpp \
     platform/TextCodecLatin1.cpp \
+    platform/TextCodecUserDefined.cpp \
     platform/TextCodecUTF16.cpp \
     platform/TextDecoder.cpp \
     platform/TextEncoding.cpp \
index 72920d681f14f759c735e2f7eb9e3f83025d1674..9bbd125f3233c9b0886265a40f4ae874d9e5f8bf 100644 (file)
                                RelativePath="..\platform\TextCodecLatin1.h"\r
                                >\r
                        </File>\r
+                       <File\r
+                               RelativePath="..\platform\TextCodecUserDefined.cpp"\r
+                               >\r
+                       </File>\r
+                       <File\r
+                               RelativePath="..\platform\TextCodecUserDefined.h"\r
+                               >\r
+                       </File>\r
                        <File\r
                                RelativePath="..\platform\TextCodecUTF16.cpp"\r
                                >\r
index 646d15ffda61e90ff9a648060a9d9c7fc4bb6e62..5c9cd80376c1e5e1b9bc807d61d811c72df8dcab 100644 (file)
                E14842FF0A674A31007E4D39 /* TextCodecICU.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E14842FE0A674A31007E4D39 /* TextCodecICU.cpp */; };
                E148432F0A674FC2007E4D39 /* TextCodecMac.h in Headers */ = {isa = PBXBuildFile; fileRef = E148432E0A674FC2007E4D39 /* TextCodecMac.h */; };
                E14843D60A6754A6007E4D39 /* TextCodecMac.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E14843910A6752BF007E4D39 /* TextCodecMac.cpp */; };
+               E15863990CD10B730066E2C9 /* TextCodecUserDefined.h in Headers */ = {isa = PBXBuildFile; fileRef = E15863970CD10B730066E2C9 /* TextCodecUserDefined.h */; };
+               E158639A0CD10B730066E2C9 /* TextCodecUserDefined.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E15863980CD10B730066E2C9 /* TextCodecUserDefined.cpp */; };
                E1D8E3160B29E39C00F4BAF6 /* HTTPParsers.h in Headers */ = {isa = PBXBuildFile; fileRef = E1D8E3150B29E39C00F4BAF6 /* HTTPParsers.h */; };
                E1D8E31A0B29E3B600F4BAF6 /* HTTPParsers.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E1D8E3190B29E3B600F4BAF6 /* HTTPParsers.cpp */; };
                E1E6EEA40B628DA8005F2F70 /* JSHTMLSelectElement.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E1E6EEA30B628DA8005F2F70 /* JSHTMLSelectElement.cpp */; };
                E14842FE0A674A31007E4D39 /* TextCodecICU.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = TextCodecICU.cpp; sourceTree = "<group>"; };
                E148432E0A674FC2007E4D39 /* TextCodecMac.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; lineEnding = 0; name = TextCodecMac.h; path = mac/TextCodecMac.h; sourceTree = "<group>"; };
                E14843910A6752BF007E4D39 /* TextCodecMac.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; lineEnding = 0; name = TextCodecMac.cpp; path = mac/TextCodecMac.cpp; sourceTree = "<group>"; };
+               E15863970CD10B730066E2C9 /* TextCodecUserDefined.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TextCodecUserDefined.h; sourceTree = "<group>"; };
+               E15863980CD10B730066E2C9 /* TextCodecUserDefined.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = TextCodecUserDefined.cpp; sourceTree = "<group>"; };
                E1D8E3150B29E39C00F4BAF6 /* HTTPParsers.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HTTPParsers.h; sourceTree = "<group>"; };
                E1D8E3190B29E3B600F4BAF6 /* HTTPParsers.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = HTTPParsers.cpp; sourceTree = "<group>"; };
                E1E6EEA30B628DA8005F2F70 /* JSHTMLSelectElement.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = JSHTMLSelectElement.cpp; sourceTree = "<group>"; };
                                932CA7C00AAA20C100AD1FAD /* TextCodecLatin1.h */,
                                E14843910A6752BF007E4D39 /* TextCodecMac.cpp */,
                                E148432E0A674FC2007E4D39 /* TextCodecMac.h */,
+                               E15863970CD10B730066E2C9 /* TextCodecUserDefined.h */,
+                               E15863980CD10B730066E2C9 /* TextCodecUserDefined.cpp */,
                                932CA8470AAA66CB00AD1FAD /* TextCodecUTF16.cpp */,
                                932CA83A0AAA667F00AD1FAD /* TextCodecUTF16.h */,
                                932CA7640AAA1DF500AD1FAD /* TextDecoder.cpp */,
                                A80F3A750CCDA318002DD990 /* DOMSVGPathSegCurvetoQuadraticAbs.h in Headers */,
                                A80F3A7D0CCDA324002DD990 /* DOMSVGAngle.h in Headers */,
                                A80F43520CCDE604002DD990 /* DOMSVGAnimateElementInternal.h in Headers */,
+                               E15863990CD10B730066E2C9 /* TextCodecUserDefined.h in Headers */,
                        );
                        runOnlyForDeploymentPostprocessing = 0;
                };
                0867D690FE84028FC02AAC07 /* Project object */ = {
                        isa = PBXProject;
                        buildConfigurationList = 149C284308902B11008A9EFC /* Build configuration list for PBXProject "WebCore" */;
-                       compatibilityVersion = "Xcode 2.4";
                        hasScannedForEncodings = 1;
                        knownRegions = (
                                English,
                                A83B79560CCB0125000B0825 /* DOMSVGFontFaceElement.mm in Sources */,
                                A80F393E0CCD971A002DD990 /* JSSVGDefinitionSrcElement.cpp in Sources */,
                                A80F39590CCD9796002DD990 /* DOMSVGDefinitionSrcElement.mm in Sources */,
+                               E158639A0CD10B730066E2C9 /* TextCodecUserDefined.cpp in Sources */,
                        );
                        runOnlyForDeploymentPostprocessing = 0;
                };
diff --git a/WebCore/platform/TextCodecUserDefined.cpp b/WebCore/platform/TextCodecUserDefined.cpp
new file mode 100644 (file)
index 0000000..2ac56b8
--- /dev/null
@@ -0,0 +1,114 @@
+/*
+ * Copyright (C) 2007 Apple, Inc.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
+ */
+
+#include "config.h"
+#include "TextCodecUserDefined.h"
+
+#include "CString.h"
+#include "PlatformString.h"
+
+using std::auto_ptr;
+
+namespace WebCore {
+
+void TextCodecUserDefined::registerEncodingNames(EncodingNameRegistrar registrar)
+{
+    registrar("x-user-defined", "x-user-defined");
+}
+
+static auto_ptr<TextCodec> newStreamingTextDecoderUserDefined(const TextEncoding&, const void*)
+{
+    return auto_ptr<TextCodec>(new TextCodecUserDefined);
+}
+
+void TextCodecUserDefined::registerCodecs(TextCodecRegistrar registrar)
+{
+    registrar("x-user-defined", newStreamingTextDecoderUserDefined, 0);
+}
+
+String TextCodecUserDefined::decode(const char* bytes, size_t length, bool)
+{
+    UChar* characters;
+    String string = String::newUninitialized(length, characters);
+
+    for (size_t i = 0; i < length; ++i) {
+        signed char c = bytes[i];
+        characters[i] = c & 0xf7ff;
+    }
+
+    return string;
+}
+
+static CString encodeComplexUserDefined(const UChar* characters, size_t length, bool allowEntities)
+{
+    Vector<char> result(length);
+    char* bytes = result.data();
+
+    size_t resultLength = 0;
+    for (size_t i = 0; i < length; ) {
+        UChar32 c;
+        U16_NEXT(characters, i, length, c);
+        signed char signedByte = c;
+        if ((signedByte & 0xf7ff) == c)
+            bytes[resultLength++] = signedByte;
+        else {
+            // No way to encode this character with x-user-defined.
+            if (allowEntities) {
+                char entityBuffer[16];
+                sprintf(entityBuffer, "&#%u;", c);
+                size_t entityLength = strlen(entityBuffer);
+                result.resize(resultLength + entityLength + length - i);
+                bytes = result.data();
+                memcpy(bytes + resultLength, entityBuffer, entityLength);
+                resultLength += entityLength;
+            } else
+                bytes[resultLength++] = '?';
+        }
+    }
+
+    return CString(bytes, resultLength);
+}
+
+CString TextCodecUserDefined::encode(const UChar* characters, size_t length, bool allowEntities)
+{
+    char* bytes;
+    CString string = CString::newUninitialized(length, bytes);
+
+    // Convert the string a fast way and simultaneously do an efficient check to see if it's all ASCII.
+    UChar ored = 0;
+    for (size_t i = 0; i < length; ++i) {
+        UChar c = characters[i];
+        bytes[i] = c;
+        ored |= c;
+    }
+
+    if (!(ored & 0xFF80))
+        return string;
+
+    // If it wasn't all ASCII, call the function that handles more-complex cases.
+    return encodeComplexUserDefined(characters, length, allowEntities);
+}
+
+} // namespace WebCore
diff --git a/WebCore/platform/TextCodecUserDefined.h b/WebCore/platform/TextCodecUserDefined.h
new file mode 100644 (file)
index 0000000..4fba907
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * Copyright (C) 2007 Apple, Inc.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
+ */
+
+#ifndef TextCodecUserDefined_h
+#define TextCodecUserDefined_h
+
+#include "TextCodec.h"
+
+namespace WebCore {
+
+    class TextCodecUserDefined : public TextCodec {
+    public:
+        static void registerEncodingNames(EncodingNameRegistrar);
+        static void registerCodecs(TextCodecRegistrar);
+
+        virtual String decode(const char*, size_t length, bool flush = false);
+        virtual CString encode(const UChar*, size_t length, bool allowEntities = false);
+    };
+
+} // namespace WebCore
+
+#endif // TextCodecUserDefined_h
index 4d247bb96a6e836420a7f093710aa749558bb400..14914454169db336915c98943517cc8413cb1f53 100644 (file)
@@ -28,6 +28,7 @@
 
 #include "PlatformString.h"
 #include "TextCodecLatin1.h"
+#include "TextCodecUserDefined.h"
 #include "TextCodecUTF16.h"
 #include <wtf/ASCIICType.h>
 #include <wtf/Assertions.h>
@@ -167,6 +168,9 @@ static void buildBaseTextCodecMaps()
     TextCodecUTF16::registerEncodingNames(addToTextEncodingNameMap);
     TextCodecUTF16::registerCodecs(addToTextCodecMap);
 
+    TextCodecUserDefined::registerEncodingNames(addToTextEncodingNameMap);
+    TextCodecUserDefined::registerCodecs(addToTextCodecMap);
+
 #if USE(ICU_UNICODE)
     TextCodecICU::registerBaseEncodingNames(addToTextEncodingNameMap);
     TextCodecICU::registerBaseCodecs(addToTextCodecMap);