2011-01-23 Patrick Gansterer <paroga@webkit.org>
authorparoga@webkit.org <paroga@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sun, 23 Jan 2011 23:17:26 +0000 (23:17 +0000)
committerparoga@webkit.org <paroga@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sun, 23 Jan 2011 23:17:26 +0000 (23:17 +0000)
        Reviewed by David Kilzer.

        Add an overload to base64Encode with String output
        https://bugs.webkit.org/show_bug.cgi?id=50122

        This change removes duplicated code.

        * inspector/InspectorResourceAgent.cpp:
        (WebCore::InspectorResourceAgent::resourceContentBase64):
        * page/DOMWindow.cpp:
        (WebCore::DOMWindow::btoa):
        * platform/graphics/skia/FontCustomPlatformData.cpp:
        (WebCore::createUniqueFontName):
        * platform/graphics/win/FontCustomPlatformData.cpp:
        (WebCore::createUniqueFontName):
        * platform/graphics/wince/FontCustomPlatformData.cpp:
        (WebCore::createUniqueFontName):
        * platform/network/cf/ResourceHandleCFNet.cpp:
        (WebCore::encodeBasicAuthorization):
        * platform/network/mac/ResourceHandleMac.mm:
        (WebCore::encodeBasicAuthorization):
        * platform/text/Base64.cpp:
        (WebCore::base64Encode):
        * platform/text/Base64.h:
        (WebCore::base64Encode):
        * platform/wince/KeygenWinCE.cpp:
        (WebCore::WebCore::signedPublicKeyAndChallengeString):

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

Source/WebCore/ChangeLog
Source/WebCore/inspector/InspectorResourceAgent.cpp
Source/WebCore/page/DOMWindow.cpp
Source/WebCore/platform/graphics/skia/FontCustomPlatformData.cpp
Source/WebCore/platform/graphics/win/FontCustomPlatformData.cpp
Source/WebCore/platform/graphics/wince/FontCustomPlatformData.cpp
Source/WebCore/platform/network/cf/ResourceHandleCFNet.cpp
Source/WebCore/platform/network/mac/ResourceHandleMac.mm
Source/WebCore/platform/text/Base64.cpp
Source/WebCore/platform/text/Base64.h
Source/WebCore/platform/wince/KeygenWinCE.cpp

index 9d9d26d..322d7ec 100644 (file)
@@ -2,6 +2,36 @@
 
         Reviewed by David Kilzer.
 
+        Add an overload to base64Encode with String output
+        https://bugs.webkit.org/show_bug.cgi?id=50122
+
+        This change removes duplicated code.
+
+        * inspector/InspectorResourceAgent.cpp:
+        (WebCore::InspectorResourceAgent::resourceContentBase64):
+        * page/DOMWindow.cpp:
+        (WebCore::DOMWindow::btoa):
+        * platform/graphics/skia/FontCustomPlatformData.cpp:
+        (WebCore::createUniqueFontName):
+        * platform/graphics/win/FontCustomPlatformData.cpp:
+        (WebCore::createUniqueFontName):
+        * platform/graphics/wince/FontCustomPlatformData.cpp:
+        (WebCore::createUniqueFontName):
+        * platform/network/cf/ResourceHandleCFNet.cpp:
+        (WebCore::encodeBasicAuthorization):
+        * platform/network/mac/ResourceHandleMac.mm:
+        (WebCore::encodeBasicAuthorization):
+        * platform/text/Base64.cpp:
+        (WebCore::base64Encode):
+        * platform/text/Base64.h:
+        (WebCore::base64Encode):
+        * platform/wince/KeygenWinCE.cpp:
+        (WebCore::WebCore::signedPublicKeyAndChallengeString):
+
+2011-01-23  Patrick Gansterer  <paroga@webkit.org>
+
+        Reviewed by David Kilzer.
+
         Add String::containsOnlyLatin1()
         https://bugs.webkit.org/show_bug.cgi?id=52979
 
index 029c79d..16d2b66 100644 (file)
@@ -95,7 +95,6 @@ bool InspectorResourceAgent::resourceContent(Frame* frame, const KURL& url, Stri
 
 bool InspectorResourceAgent::resourceContentBase64(Frame* frame, const KURL& url, String* result)
 {
-    Vector<char> out;
     String textEncodingName;
     RefPtr<SharedBuffer> data = InspectorResourceAgent::resourceData(frame, url, &textEncodingName);
     if (!data) {
@@ -103,8 +102,7 @@ bool InspectorResourceAgent::resourceContentBase64(Frame* frame, const KURL& url
         return false;
     }
 
-    base64Encode(data->buffer(), out);
-    *result = String(out.data(), out.size());
+    *result = base64Encode(data->buffer());
     return true;
 }
 
index 5492386..572e559 100644 (file)
@@ -974,13 +974,7 @@ String DOMWindow::btoa(const String& stringToEncode, ExceptionCode& ec)
         return String();
     }
 
-    Vector<char> in;
-    in.append(stringToEncode.characters(), stringToEncode.length());
-    Vector<char> out;
-
-    base64Encode(in, out);
-
-    return String(out.data(), out.size());
+    return base64Encode(stringToEncode.latin1());
 }
 
 String DOMWindow::atob(const String& encodedString, ExceptionCode& ec)
index 0b31dfa..b6427b0 100644 (file)
@@ -114,13 +114,12 @@ FontPlatformData FontCustomPlatformData::fontPlatformData(int size, bool bold, b
 // not allow access from CSS.
 static String createUniqueFontName()
 {
-    Vector<char> fontUuid(sizeof(GUID));
-    CoCreateGuid(reinterpret_cast<GUID*>(fontUuid.data()));
+    GUID fontUuid;
+    CoCreateGuid(&fontUuid);
 
-    Vector<char> fontNameVector;
-    base64Encode(fontUuid, fontNameVector);
-    ASSERT(fontNameVector.size() < LF_FACESIZE);
-    return String(fontNameVector.data(), fontNameVector.size());
+    String fontName = base64Encode(reinterpret_cast<char*>(&fontUuid), sizeof(fontUuid));
+    ASSERT(fontName.length() < LF_FACESIZE);
+    return fontName;
 }
 #endif
 
index 9cae99b..1bb2050 100644 (file)
@@ -154,13 +154,12 @@ static unsigned long WINAPIV readEmbedProc(void* stream, void* buffer, unsigned
 // not allow access from CSS.
 static String createUniqueFontName()
 {
-    Vector<char> fontUuid(sizeof(GUID));
-    CoCreateGuid(reinterpret_cast<GUID*>(fontUuid.data()));
+    GUID fontUuid;
+    CoCreateGuid(&fontUuid);
 
-    Vector<char> fontNameVector;
-    base64Encode(fontUuid, fontNameVector);
-    ASSERT(fontNameVector.size() < LF_FACESIZE);
-    return String(fontNameVector.data(), fontNameVector.size());
+    String fontName = base64Encode(reinterpret_cast<char*>(&fontUuid), sizeof(fontUuid));
+    ASSERT(fontName.length() < LF_FACESIZE);
+    return fontName;
 }
 
 FontCustomPlatformData* createFontCustomPlatformData(SharedBuffer* buffer)
index f61ae8e..424b64a 100644 (file)
@@ -59,16 +59,14 @@ FontPlatformData FontCustomPlatformData::fontPlatformData(int size, bool bold, b
 // not allow access from CSS.
 static String createUniqueFontName()
 {
-    Vector<char> fontUuid(sizeof(GUID));
+    GUID fontUuid;
 
-    unsigned int* ptr = reinterpret_cast<unsigned int*>(fontUuid.data());
+    unsigned int* ptr = reinterpret_cast<unsigned int*>(&fontUuid);
     for (int i = 0; i < sizeof(GUID) / sizeof(int) ; ++i)
         *(ptr + i) = static_cast<unsigned int>(randomNumber() * (std::numeric_limits<unsigned>::max() + 1.0));
 
-    Vector<char> fontNameVector;
-    base64Encode(fontUuid, fontNameVector);
-    ASSERT(fontNameVector.size() < LF_FACESIZE);
-    String fontName(fontNameVector.data(), fontNameVector.size());
+    String fontName = base64Encode(reinterpret_cast<char*>(&fontUuid), sizeof(fontUuid));
+    ASSERT(fontName.length() < LF_FACESIZE);
     return fontName.replace('/', '_');
 }
 
index 66ae5a0..52b100f 100644 (file)
@@ -126,12 +126,7 @@ static void setDefaultMIMEType(CFURLResponseRef response)
 
 static String encodeBasicAuthorization(const String& user, const String& password)
 {
-    CString unencodedString = (user + ":" + password).utf8();
-    Vector<char> unencoded(unencodedString.length());
-    std::copy(unencodedString.data(), unencodedString.data() + unencodedString.length(), unencoded.begin());
-    Vector<char> encoded;
-    base64Encode(unencoded, encoded);
-    return String(encoded.data(), encoded.size());
+    return base64Encode((user + ":" + password).utf8());
 }
 
 CFURLRequestRef willSendRequest(CFURLConnectionRef conn, CFURLRequestRef cfRequest, CFURLResponseRef cfRedirectResponse, const void* clientInfo)
index 84b656c..2d687c0 100644 (file)
@@ -164,12 +164,7 @@ public:
 #ifndef BUILDING_ON_TIGER
 static String encodeBasicAuthorization(const String& user, const String& password)
 {
-    CString unencodedString = (user + ":" + password).utf8();
-    Vector<char> unencoded(unencodedString.length());
-    std::copy(unencodedString.data(), unencodedString.data() + unencodedString.length(), unencoded.begin());
-    Vector<char> encoded;
-    base64Encode(unencoded, encoded);
-    return String(encoded.data(), encoded.size());
+    return base64Encode((user + ":" + password).utf8());
 }
 #endif
 
index 98b537a..bf706f6 100644 (file)
@@ -60,9 +60,11 @@ static const char base64DecMap[128] = {
     0x31, 0x32, 0x33, 0x00, 0x00, 0x00, 0x00, 0x00
 };
 
-void base64Encode(const Vector<char>& in, Vector<char>& out, bool insertLFs)
+String base64Encode(const char* data, unsigned length, bool insertLFs)
 {
-    base64Encode(in.data(), in.size(), out, insertLFs);
+    Vector<char> result;
+    base64Encode(data, length, result, insertLFs);
+    return String(result.data(), result.size());
 }
 
 void base64Encode(const char* data, unsigned len, Vector<char>& out, bool insertLFs)
index 211bd3c..70855de 100644 (file)
 #ifndef Base64_h
 #define Base64_h
 
-#include <wtf/Forward.h>
 #include <wtf/Vector.h>
+#include <wtf/text/CString.h>
+#include <wtf/text/WTFString.h>
 
 namespace WebCore {
 
 enum Base64DecodePolicy { FailOnInvalidCharacter, IgnoreWhitespace, IgnoreInvalidCharacters };
 
-void base64Encode(const Vector<char>&, Vector<char>&, bool insertLFs = false);
 void base64Encode(const char*, unsigned, Vector<char>&, bool insertLFs = false);
+void base64Encode(const Vector<char>&, Vector<char>&, bool insertLFs = false);
+void base64Encode(const CString&, Vector<char>&, bool insertLFs = false);
+String base64Encode(const char*, unsigned, bool insertLFs = false);
+String base64Encode(const Vector<char>&, bool insertLFs = false);
+String base64Encode(const CString&, bool insertLFs = false);
 
 bool base64Decode(const String&, Vector<char>&, Base64DecodePolicy = FailOnInvalidCharacter);
 bool base64Decode(const Vector<char>&, Vector<char>&, Base64DecodePolicy = FailOnInvalidCharacter);
 bool base64Decode(const char*, unsigned, Vector<char>&, Base64DecodePolicy = FailOnInvalidCharacter);
 
+inline void base64Encode(const Vector<char>& in, Vector<char>& out, bool insertLFs)
+{
+    base64Encode(in.data(), in.size(), out, insertLFs);
 }
 
+inline void base64Encode(const CString& in, Vector<char>& out, bool insertLFs)
+{
+    base64Encode(in.data(), in.length(), out, insertLFs);
+}
+
+inline String base64Encode(const Vector<char>& in, bool insertLFs)
+{
+    return base64Encode(in.data(), in.size(), insertLFs);
+}
+
+inline String base64Encode(const CString& in, bool insertLFs)
+{
+    return base64Encode(in.data(), in.length(), insertLFs);
+}
+
+} // namespace WebCore
+
 #endif // Base64_h
index 0c1b3c6..8537f44 100644 (file)
@@ -80,10 +80,7 @@ String WebCore::signedPublicKeyAndChallengeString(unsigned index, const String&
         if (!CryptSignAndEncodeCertificate(hContext, AT_KEYEXCHANGE, X509_ASN_ENCODING, X509_KEYGEN_REQUEST_TO_BE_SIGNED, &requestInfo, &signAlgo, 0, reinterpret_cast<LPBYTE>(binary.data()), &dwEncodedLength))
             break;
 
-        Vector<char> base64;
-        base64Encode(binary, base64);
-        keyString = String(base64.data(), base64.size());
-
+        keyString = base64Encode(binary);
     } while(0);
 
     if (pPubInfo)