Index: Source/wtf/text/Base64.cpp |
diff --git a/Source/wtf/text/Base64.cpp b/Source/wtf/text/Base64.cpp |
index 9a31527a2251f696a6846510cacbe9cfed9e844a..876e22565bfa547ae6c5d36384511b7d11af5eac 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, Base64InvalidCharactersPolicy charactersPolicy, Base64PaddingValidationPolicy paddingPolicy) |
+bool base64Decode(const Vector<char>& in, Vector<char>& out, Base64DecodePolicy policy) |
{ |
out.clear(); |
@@ -136,48 +136,40 @@ bool base64Decode(const Vector<char>& in, Vector<char>& out, Base64InvalidCharac |
if (in.size() > UINT_MAX) |
return false; |
- return base64Decode(in.data(), in.size(), out, charactersPolicy, paddingPolicy); |
+ return base64Decode(in.data(), in.size(), out, policy); |
} |
template<typename T> |
-static inline bool base64DecodeInternal(const T* data, unsigned length, Vector<char>& out, Base64InvalidCharactersPolicy charactersPolicy, Base64PaddingValidationPolicy paddingPolicy) |
+static inline bool base64DecodeInternal(const T* data, unsigned length, Vector<char>& out, Base64DecodePolicy policy) |
{ |
out.clear(); |
if (!length) |
return true; |
- unsigned dataLength = length; |
- if (paddingPolicy == Base64StrictPaddingValidation) { |
- if (!(dataLength % 4)) { |
- // There may be 2 = padding max. |
- while (data[dataLength - 1] == '=' && dataLength >= (length - 2)) |
- --dataLength; |
- } |
- if (dataLength % 4 == 1) |
- return false; |
- } |
- |
out.grow(length); |
- bool sawEqualsSign = false; |
+ unsigned equalsSignCount = 0; |
unsigned outLength = 0; |
for (unsigned idx = 0; idx < length; ++idx) { |
unsigned ch = data[idx]; |
if (ch == '=') { |
- sawEqualsSign = true; |
- if (paddingPolicy == Base64StrictPaddingValidation && idx < dataLength) |
+ ++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)) |
return false; |
} else if (('0' <= ch && ch <= '9') || ('A' <= ch && ch <= 'Z') || ('a' <= ch && ch <= 'z') || ch == '+' || ch == '/') { |
- if (sawEqualsSign) |
+ if (equalsSignCount) |
return false; |
out[outLength] = base64DecMap[ch]; |
++outLength; |
- } else if (charactersPolicy == Base64FailOnInvalidCharacter || (charactersPolicy == Base64IgnoreWhitespace && !isSpaceOrNewline(ch))) |
+ } else if (policy == Base64FailOnInvalidCharacterOrExcessPadding || policy == Base64FailOnInvalidCharacter || (policy == Base64IgnoreWhitespace && !isSpaceOrNewline(ch))) { |
return false; |
+ } |
} |
if (!outLength) |
- return !sawEqualsSign; |
+ return !equalsSignCount; |
// Valid data is (n * 4 + [0,2,3]) characters long. |
if ((outLength % 4) == 1) |
@@ -212,18 +204,18 @@ static inline bool base64DecodeInternal(const T* data, unsigned length, Vector<c |
return true; |
} |
-bool base64Decode(const char* data, unsigned length, Vector<char>& out, Base64InvalidCharactersPolicy charactersPolicy, Base64PaddingValidationPolicy paddingPolicy) |
+bool base64Decode(const char* data, unsigned length, Vector<char>& out, Base64DecodePolicy policy) |
{ |
- return base64DecodeInternal<LChar>(reinterpret_cast<const LChar*>(data), length, out, charactersPolicy, paddingPolicy); |
+ return base64DecodeInternal<LChar>(reinterpret_cast<const LChar*>(data), length, out, policy); |
} |
-bool base64Decode(const String& in, Vector<char>& out, Base64InvalidCharactersPolicy charactersPolicy, Base64PaddingValidationPolicy paddingPolicy) |
+bool base64Decode(const String& in, Vector<char>& out, Base64DecodePolicy policy) |
{ |
if (in.isEmpty()) |
- return base64DecodeInternal<LChar>(0, 0, out, charactersPolicy, paddingPolicy); |
+ return base64DecodeInternal<LChar>(0, 0, out, policy); |
if (in.is8Bit()) |
- return base64DecodeInternal<LChar>(in.characters8(), in.length(), out, charactersPolicy, paddingPolicy); |
- return base64DecodeInternal<UChar>(in.characters16(), in.length(), out, charactersPolicy, paddingPolicy); |
+ return base64DecodeInternal<LChar>(in.characters8(), in.length(), out, policy); |
+ return base64DecodeInternal<UChar>(in.characters16(), in.length(), out, policy); |
} |
} // namespace WTF |