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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « Source/wtf/text/Base64.h ('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 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
121 out[didx++] = base64EncMap[(data[sidx] << 4) & 077]; 121 out[didx++] = base64EncMap[(data[sidx] << 4) & 077];
122 } 122 }
123 123
124 // Add padding 124 // Add padding
125 while (didx < out.size()) { 125 while (didx < out.size()) {
126 out[didx] = '='; 126 out[didx] = '=';
127 ++didx; 127 ++didx;
128 } 128 }
129 } 129 }
130 130
131 bool base64Decode(const Vector<char>& in, Vector<char>& out, Base64InvalidCharac tersPolicy charactersPolicy, Base64PaddingValidationPolicy paddingPolicy) 131 bool base64Decode(const Vector<char>& in, Vector<char>& out, Base64DecodePolicy policy)
132 { 132 {
133 out.clear(); 133 out.clear();
134 134
135 // If the input string is pathologically large, just return nothing. 135 // If the input string is pathologically large, just return nothing.
136 if (in.size() > UINT_MAX) 136 if (in.size() > UINT_MAX)
137 return false; 137 return false;
138 138
139 return base64Decode(in.data(), in.size(), out, charactersPolicy, paddingPoli cy); 139 return base64Decode(in.data(), in.size(), out, policy);
140 } 140 }
141 141
142 template<typename T> 142 template<typename T>
143 static inline bool base64DecodeInternal(const T* data, unsigned length, Vector<c har>& out, Base64InvalidCharactersPolicy charactersPolicy, Base64PaddingValidati onPolicy paddingPolicy) 143 static inline bool base64DecodeInternal(const T* data, unsigned length, Vector<c har>& out, Base64DecodePolicy policy)
144 { 144 {
145 out.clear(); 145 out.clear();
146 if (!length) 146 if (!length)
147 return true; 147 return true;
148 148
149 unsigned dataLength = length;
150 if (paddingPolicy == Base64StrictPaddingValidation) {
151 if (!(dataLength % 4)) {
152 // There may be 2 = padding max.
153 while (data[dataLength - 1] == '=' && dataLength >= (length - 2))
154 --dataLength;
155 }
156 if (dataLength % 4 == 1)
157 return false;
158 }
159
160 out.grow(length); 149 out.grow(length);
161 150
162 bool sawEqualsSign = false; 151 unsigned equalsSignCount = 0;
163 unsigned outLength = 0; 152 unsigned outLength = 0;
164 for (unsigned idx = 0; idx < length; ++idx) { 153 for (unsigned idx = 0; idx < length; ++idx) {
165 unsigned ch = data[idx]; 154 unsigned ch = data[idx];
166 if (ch == '=') { 155 if (ch == '=') {
167 sawEqualsSign = true; 156 ++equalsSignCount;
168 if (paddingPolicy == Base64StrictPaddingValidation && idx < dataLeng th) 157 // There should be no padding if length is a multiple of 4, and ther e
158 // should never be more than 2 padding characters.
159 if (policy == Base64FailOnInvalidCharacterOrExcessPadding && (length % 4 || equalsSignCount > 2))
169 return false; 160 return false;
170 } else if (('0' <= ch && ch <= '9') || ('A' <= ch && ch <= 'Z') || ('a' <= ch && ch <= 'z') || ch == '+' || ch == '/') { 161 } else if (('0' <= ch && ch <= '9') || ('A' <= ch && ch <= 'Z') || ('a' <= ch && ch <= 'z') || ch == '+' || ch == '/') {
171 if (sawEqualsSign) 162 if (equalsSignCount)
172 return false; 163 return false;
173 out[outLength] = base64DecMap[ch]; 164 out[outLength] = base64DecMap[ch];
174 ++outLength; 165 ++outLength;
175 } else if (charactersPolicy == Base64FailOnInvalidCharacter || (characte rsPolicy == Base64IgnoreWhitespace && !isSpaceOrNewline(ch))) 166 } else if (policy == Base64FailOnInvalidCharacterOrExcessPadding || poli cy == Base64FailOnInvalidCharacter || (policy == Base64IgnoreWhitespace && !isSp aceOrNewline(ch))) {
176 return false; 167 return false;
168 }
177 } 169 }
178 170
179 if (!outLength) 171 if (!outLength)
180 return !sawEqualsSign; 172 return !equalsSignCount;
181 173
182 // Valid data is (n * 4 + [0,2,3]) characters long. 174 // Valid data is (n * 4 + [0,2,3]) characters long.
183 if ((outLength % 4) == 1) 175 if ((outLength % 4) == 1)
184 return false; 176 return false;
185 177
186 // 4-byte to 3-byte conversion 178 // 4-byte to 3-byte conversion
187 outLength -= (outLength + 3) / 4; 179 outLength -= (outLength + 3) / 4;
188 if (!outLength) 180 if (!outLength)
189 return false; 181 return false;
190 182
(...skipping 14 matching lines...) Expand all
205 197
206 if (++didx < outLength) 198 if (++didx < outLength)
207 out[didx] = (((out[sidx + 1] << 4) & 255) | ((out[sidx + 2] >> 2) & 017) ); 199 out[didx] = (((out[sidx + 1] << 4) & 255) | ((out[sidx + 2] >> 2) & 017) );
208 200
209 if (outLength < out.size()) 201 if (outLength < out.size())
210 out.shrink(outLength); 202 out.shrink(outLength);
211 203
212 return true; 204 return true;
213 } 205 }
214 206
215 bool base64Decode(const char* data, unsigned length, Vector<char>& out, Base64In validCharactersPolicy charactersPolicy, Base64PaddingValidationPolicy paddingPol icy) 207 bool base64Decode(const char* data, unsigned length, Vector<char>& out, Base64De codePolicy policy)
216 { 208 {
217 return base64DecodeInternal<LChar>(reinterpret_cast<const LChar*>(data), len gth, out, charactersPolicy, paddingPolicy); 209 return base64DecodeInternal<LChar>(reinterpret_cast<const LChar*>(data), len gth, out, policy);
218 } 210 }
219 211
220 bool base64Decode(const String& in, Vector<char>& out, Base64InvalidCharactersPo licy charactersPolicy, Base64PaddingValidationPolicy paddingPolicy) 212 bool base64Decode(const String& in, Vector<char>& out, Base64DecodePolicy policy )
221 { 213 {
222 if (in.isEmpty()) 214 if (in.isEmpty())
223 return base64DecodeInternal<LChar>(0, 0, out, charactersPolicy, paddingP olicy); 215 return base64DecodeInternal<LChar>(0, 0, out, policy);
224 if (in.is8Bit()) 216 if (in.is8Bit())
225 return base64DecodeInternal<LChar>(in.characters8(), in.length(), out, c haractersPolicy, paddingPolicy); 217 return base64DecodeInternal<LChar>(in.characters8(), in.length(), out, p olicy);
226 return base64DecodeInternal<UChar>(in.characters16(), in.length(), out, char actersPolicy, paddingPolicy); 218 return base64DecodeInternal<UChar>(in.characters16(), in.length(), out, poli cy);
227 } 219 }
228 220
229 } // namespace WTF 221 } // namespace WTF
OLDNEW
« 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