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

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

Issue 19623002: Make atob() throw an InvalidCharacterError on excess padding characters (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 7 years, 5 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
Index: Source/wtf/text/Base64.cpp
diff --git a/Source/wtf/text/Base64.cpp b/Source/wtf/text/Base64.cpp
index 4d1c88ea022160899c9deea84a1415715aa1972c..7c2fec4d51c8b28734e5105bf8522ed9c3072c13 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, Base64InvalidCharactersPolicy charactersPolicy, Base64PaddingValidationPolicy paddingPolicy)
{
out.clear();
@@ -136,30 +136,44 @@ 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, charactersPolicy, paddingPolicy);
}
template<typename T>
-static inline bool base64DecodeInternal(const T* data, unsigned len, Vector<char>& out, Base64DecodePolicy policy)
+static inline bool base64DecodeInternal(const T* data, unsigned length, Vector<char>& out, Base64InvalidCharactersPolicy charactersPolicy, Base64PaddingValidationPolicy paddingPolicy)
{
out.clear();
- if (!len)
+ if (!length)
return true;
- out.grow(len);
+ 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 outLength = 0;
- for (unsigned idx = 0; idx < len; ++idx) {
+ for (unsigned idx = 0; idx < length; ++idx) {
unsigned ch = data[idx];
- if (ch == '=')
+ if (ch == '=') {
sawEqualsSign = true;
+ if (paddingPolicy == Base64StrictPaddingValidation && idx < dataLength)
+ return false;
+ }
arv (Not doing code reviews) 2013/07/17 17:49:06 } else if (...) {
else if (('0' <= ch && ch <= '9') || ('A' <= ch && ch <= 'Z') || ('a' <= ch && ch <= 'z') || ch == '+' || ch == '/') {
if (sawEqualsSign)
return false;
out[outLength] = base64DecMap[ch];
++outLength;
- } else if (policy == Base64FailOnInvalidCharacter || (policy == Base64IgnoreWhitespace && !isSpaceOrNewline(ch)))
+ } else if (charactersPolicy == Base64FailOnInvalidCharacter || (charactersPolicy == Base64IgnoreWhitespace && !isSpaceOrNewline(ch)))
return false;
}
@@ -199,18 +213,18 @@ static inline bool base64DecodeInternal(const T* data, unsigned len, Vector<char
return true;
}
-bool base64Decode(const char* data, unsigned length, Vector<char>& out, Base64DecodePolicy policy)
+bool base64Decode(const char* data, unsigned length, Vector<char>& out, Base64InvalidCharactersPolicy charactersPolicy, Base64PaddingValidationPolicy paddingPolicy)
{
- return base64DecodeInternal<LChar>(reinterpret_cast<const LChar*>(data), length, out, policy);
+ return base64DecodeInternal<LChar>(reinterpret_cast<const LChar*>(data), length, out, charactersPolicy, paddingPolicy);
}
-bool base64Decode(const String& in, Vector<char>& out, Base64DecodePolicy policy)
+bool base64Decode(const String& in, Vector<char>& out, Base64InvalidCharactersPolicy charactersPolicy, Base64PaddingValidationPolicy paddingPolicy)
{
if (in.isEmpty())
- return base64DecodeInternal<LChar>(0, 0, out, policy);
+ return base64DecodeInternal<LChar>(0, 0, out, charactersPolicy, paddingPolicy);
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, charactersPolicy, paddingPolicy);
+ return base64DecodeInternal<UChar>(in.characters16(), in.length(), out, charactersPolicy, paddingPolicy);
}
} // namespace WTF
« LayoutTests/fast/dom/Window/atob-btoa-expected.txt ('K') | « Source/wtf/text/Base64.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698