Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1071)

Unified Diff: Source/wtf/text/Base64.cpp

Issue 22796004: Simplify WTF::base64Decode() after r154538 (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Restore the enum's original name Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « Source/wtf/text/Base64.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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
« no previous file with comments | « Source/wtf/text/Base64.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698