Index: Source/wtf/text/Base64.cpp |
diff --git a/Source/wtf/text/Base64.cpp b/Source/wtf/text/Base64.cpp |
index 21552ffbe0cf1bec0131a1f4e9db0d6cdf6e89df..8aa34e257e81a433d8761f9843eba761db90fd63 100644 |
--- a/Source/wtf/text/Base64.cpp |
+++ b/Source/wtf/text/Base64.cpp |
@@ -128,7 +128,7 @@ void base64Encode(const char* data, unsigned len, Vector<char>& out, Base64Encod |
} |
} |
-bool base64Decode(const Vector<char>& in, Vector<char>& out, Base64DecodePolicy policy) |
+bool base64Decode(const Vector<char>& in, Vector<char>& out, CharacterMatchFunctionPtr shouldIgnoreCharacter, Base64DecodePolicy policy) |
{ |
out.clear(); |
@@ -136,11 +136,11 @@ bool base64Decode(const Vector<char>& in, Vector<char>& out, Base64DecodePolicy |
if (in.size() > UINT_MAX) |
return false; |
- return base64Decode(in.data(), in.size(), out, policy); |
+ return base64Decode(in.data(), in.size(), out, shouldIgnoreCharacter, policy); |
} |
template<typename T> |
-static inline bool base64DecodeInternal(const T* data, unsigned length, Vector<char>& out, Base64DecodePolicy policy) |
+static inline bool base64DecodeInternal(const T* data, unsigned length, Vector<char>& out, CharacterMatchFunctionPtr shouldIgnoreCharacter, Base64DecodePolicy policy) |
{ |
out.clear(); |
if (!length) |
@@ -154,16 +154,14 @@ static inline bool base64DecodeInternal(const T* data, unsigned length, Vector<c |
unsigned ch = data[idx]; |
if (ch == '=') { |
++equalsSignCount; |
- // There should be no padding if length is a multiple of 4, and there |
- // should never be more than 2 padding characters. |
- if (policy == Base64FailOnInvalidCharacterOrExcessPadding && (length % 4 || equalsSignCount > 2)) |
+ // There should never be more than 2 padding characters. |
+ if (policy == Base64ValidatePadding && equalsSignCount > 2) |
return false; |
} else if (('0' <= ch && ch <= '9') || ('A' <= ch && ch <= 'Z') || ('a' <= ch && ch <= 'z') || ch == '+' || ch == '/') { |
if (equalsSignCount) |
return false; |
- out[outLength] = base64DecMap[ch]; |
- ++outLength; |
- } else if (policy == Base64FailOnInvalidCharacterOrExcessPadding || policy == Base64FailOnInvalidCharacter || (policy == Base64IgnoreWhitespace && !isSpaceOrNewline(ch))) { |
+ out[outLength++] = base64DecMap[ch]; |
+ } else if (!shouldIgnoreCharacter || !shouldIgnoreCharacter(ch)) { |
return false; |
} |
} |
@@ -171,6 +169,11 @@ static inline bool base64DecodeInternal(const T* data, unsigned length, Vector<c |
if (!outLength) |
return !equalsSignCount; |
+ // There should be no padding if length is a multiple of 4. |
+ // We use (outLength + equalsSignCount) instead of length because we don't want to account for ignored characters. |
+ if (policy == Base64ValidatePadding && equalsSignCount && (outLength + equalsSignCount) % 4) |
+ return false; |
+ |
// Valid data is (n * 4 + [0,2,3]) characters long. |
if ((outLength % 4) == 1) |
return false; |
@@ -204,23 +207,23 @@ static inline bool base64DecodeInternal(const T* data, unsigned length, Vector<c |
return true; |
} |
-bool base64Decode(const char* data, unsigned length, Vector<char>& out, Base64DecodePolicy policy) |
+bool base64Decode(const char* data, unsigned length, Vector<char>& out, CharacterMatchFunctionPtr shouldIgnoreCharacter, Base64DecodePolicy policy) |
{ |
- return base64DecodeInternal<LChar>(reinterpret_cast<const LChar*>(data), length, out, policy); |
+ return base64DecodeInternal<LChar>(reinterpret_cast<const LChar*>(data), length, out, shouldIgnoreCharacter, policy); |
} |
-bool base64Decode(const UChar* data, unsigned length, Vector<char>& out, Base64DecodePolicy policy) |
+bool base64Decode(const UChar* data, unsigned length, Vector<char>& out, CharacterMatchFunctionPtr shouldIgnoreCharacter, Base64DecodePolicy policy) |
{ |
- return base64DecodeInternal<UChar>(data, length, out, policy); |
+ return base64DecodeInternal<UChar>(data, length, out, shouldIgnoreCharacter, policy); |
} |
-bool base64Decode(const String& in, Vector<char>& out, Base64DecodePolicy policy) |
+bool base64Decode(const String& in, Vector<char>& out, CharacterMatchFunctionPtr shouldIgnoreCharacter, Base64DecodePolicy policy) |
{ |
if (in.isEmpty()) |
- return base64DecodeInternal<LChar>(0, 0, out, policy); |
+ return base64DecodeInternal<LChar>(0, 0, out, shouldIgnoreCharacter, policy); |
if (in.is8Bit()) |
- return base64DecodeInternal<LChar>(in.characters8(), in.length(), out, policy); |
- return base64DecodeInternal<UChar>(in.characters16(), in.length(), out, policy); |
+ return base64DecodeInternal<LChar>(in.characters8(), in.length(), out, shouldIgnoreCharacter, policy); |
+ return base64DecodeInternal<UChar>(in.characters16(), in.length(), out, shouldIgnoreCharacter, policy); |
} |
} // namespace WTF |