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

Side by Side Diff: Source/wtf/text/Base64.cpp

Issue 215833002: window.atob() returns wrong value when given a string container only white spaces (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 6 years, 9 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « LayoutTests/fast/dom/Window/atob-btoa-expected.txt ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 Copyright (C) 2000-2001 Dawit Alemayehu <adawit@kde.org> 2 Copyright (C) 2000-2001 Dawit Alemayehu <adawit@kde.org>
3 Copyright (C) 2006 Alexey Proskuryakov <ap@webkit.org> 3 Copyright (C) 2006 Alexey Proskuryakov <ap@webkit.org>
4 Copyright (C) 2007, 2008 Apple Inc. All rights reserved. 4 Copyright (C) 2007, 2008 Apple Inc. All rights reserved.
5 Copyright (C) 2010 Patrick Gansterer <paroga@paroga.com> 5 Copyright (C) 2010 Patrick Gansterer <paroga@paroga.com>
6 6
7 This program is free software; you can redistribute it and/or modify 7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License (LGPL) 8 it under the terms of the GNU Lesser General Public License (LGPL)
9 version 2 as published by the Free Software Foundation. 9 version 2 as published by the Free Software Foundation.
10 10
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
142 static inline bool base64DecodeInternal(const T* data, unsigned length, Vector<c har>& out, CharacterMatchFunctionPtr shouldIgnoreCharacter, Base64DecodePolicy p olicy) 142 static inline bool base64DecodeInternal(const T* data, unsigned length, Vector<c har>& out, CharacterMatchFunctionPtr shouldIgnoreCharacter, Base64DecodePolicy p olicy)
143 { 143 {
144 out.clear(); 144 out.clear();
145 if (!length) 145 if (!length)
146 return true; 146 return true;
147 147
148 out.grow(length); 148 out.grow(length);
149 149
150 unsigned equalsSignCount = 0; 150 unsigned equalsSignCount = 0;
151 unsigned outLength = 0; 151 unsigned outLength = 0;
152 bool hadError = false;
152 for (unsigned idx = 0; idx < length; ++idx) { 153 for (unsigned idx = 0; idx < length; ++idx) {
153 unsigned ch = data[idx]; 154 unsigned ch = data[idx];
154 if (ch == '=') { 155 if (ch == '=') {
155 ++equalsSignCount; 156 ++equalsSignCount;
156 // There should never be more than 2 padding characters. 157 // There should never be more than 2 padding characters.
157 if (policy == Base64ValidatePadding && equalsSignCount > 2) 158 if (policy == Base64ValidatePadding && equalsSignCount > 2) {
158 return false; 159 hadError = true;
160 break;
161 }
159 } else if (('0' <= ch && ch <= '9') || ('A' <= ch && ch <= 'Z') || ('a' <= ch && ch <= 'z') || ch == '+' || ch == '/') { 162 } else if (('0' <= ch && ch <= '9') || ('A' <= ch && ch <= 'Z') || ('a' <= ch && ch <= 'z') || ch == '+' || ch == '/') {
160 if (equalsSignCount) 163 if (equalsSignCount) {
161 return false; 164 hadError = true;
165 break;
166 }
162 out[outLength++] = base64DecMap[ch]; 167 out[outLength++] = base64DecMap[ch];
163 } else if (!shouldIgnoreCharacter || !shouldIgnoreCharacter(ch)) { 168 } else if (!shouldIgnoreCharacter || !shouldIgnoreCharacter(ch)) {
164 return false; 169 hadError = true;
170 break;
165 } 171 }
166 } 172 }
167 173
174 if (outLength < out.size())
175 out.shrink(outLength);
176
177 if (hadError)
178 return false;
179
168 if (!outLength) 180 if (!outLength)
169 return !equalsSignCount; 181 return !equalsSignCount;
170 182
171 // There should be no padding if length is a multiple of 4. 183 // There should be no padding if length is a multiple of 4.
172 // We use (outLength + equalsSignCount) instead of length because we don't w ant to account for ignored characters. 184 // We use (outLength + equalsSignCount) instead of length because we don't w ant to account for ignored characters.
173 if (policy == Base64ValidatePadding && equalsSignCount && (outLength + equal sSignCount) % 4) 185 if (policy == Base64ValidatePadding && equalsSignCount && (outLength + equal sSignCount) % 4)
174 return false; 186 return false;
175 187
176 // Valid data is (n * 4 + [0,2,3]) characters long. 188 // Valid data is (n * 4 + [0,2,3]) characters long.
177 if ((outLength % 4) == 1) 189 if ((outLength % 4) == 1)
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
219 bool base64Decode(const String& in, Vector<char>& out, CharacterMatchFunctionPtr shouldIgnoreCharacter, Base64DecodePolicy policy) 231 bool base64Decode(const String& in, Vector<char>& out, CharacterMatchFunctionPtr shouldIgnoreCharacter, Base64DecodePolicy policy)
220 { 232 {
221 if (in.isEmpty()) 233 if (in.isEmpty())
222 return base64DecodeInternal<LChar>(0, 0, out, shouldIgnoreCharacter, pol icy); 234 return base64DecodeInternal<LChar>(0, 0, out, shouldIgnoreCharacter, pol icy);
223 if (in.is8Bit()) 235 if (in.is8Bit())
224 return base64DecodeInternal<LChar>(in.characters8(), in.length(), out, s houldIgnoreCharacter, policy); 236 return base64DecodeInternal<LChar>(in.characters8(), in.length(), out, s houldIgnoreCharacter, policy);
225 return base64DecodeInternal<UChar>(in.characters16(), in.length(), out, shou ldIgnoreCharacter, policy); 237 return base64DecodeInternal<UChar>(in.characters16(), in.length(), out, shou ldIgnoreCharacter, policy);
226 } 238 }
227 239
228 } // namespace WTF 240 } // namespace WTF
OLDNEW
« no previous file with comments | « LayoutTests/fast/dom/Window/atob-btoa-expected.txt ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698