1/*
2 Copyright (C) 2000-2001 Dawit Alemayehu <[email protected]>
3 Copyright (C) 2006 Alexey Proskuryakov <[email protected]>
4 Copyright (C) 2007, 2008, 2013, 2016 Apple Inc. All rights reserved.
5 Copyright (C) 2010 Patrick Gansterer <[email protected]>
6
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)
9 version 2 as published by the Free Software Foundation.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU Library General Public
17 License along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
20 This code is based on the java implementation in HTTPClient
21 package by Ronald Tschalär Copyright (C) 1996-1999.
22*/
23
24#include "config.h"
25#include <wtf/text/Base64.h>
26
27#include <limits.h>
28#include <wtf/text/WTFString.h>
29
30namespace WTF {
31
32const char nonAlphabet = -1;
33
34static const char base64EncMap[64] = {
35 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
36 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50,
37 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
38 0x59, 0x5A, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
39 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E,
40 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76,
41 0x77, 0x78, 0x79, 0x7A, 0x30, 0x31, 0x32, 0x33,
42 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x2B, 0x2F
43};
44
45static const char base64DecMap[128] = {
46 nonAlphabet, nonAlphabet, nonAlphabet, nonAlphabet, nonAlphabet, nonAlphabet, nonAlphabet, nonAlphabet,
47 nonAlphabet, nonAlphabet, nonAlphabet, nonAlphabet, nonAlphabet, nonAlphabet, nonAlphabet, nonAlphabet,
48 nonAlphabet, nonAlphabet, nonAlphabet, nonAlphabet, nonAlphabet, nonAlphabet, nonAlphabet, nonAlphabet,
49 nonAlphabet, nonAlphabet, nonAlphabet, nonAlphabet, nonAlphabet, nonAlphabet, nonAlphabet, nonAlphabet,
50 nonAlphabet, nonAlphabet, nonAlphabet, nonAlphabet, nonAlphabet, nonAlphabet, nonAlphabet, nonAlphabet,
51 nonAlphabet, nonAlphabet, nonAlphabet, 0x3E, nonAlphabet, nonAlphabet, nonAlphabet, 0x3F,
52 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B,
53 0x3C, 0x3D, nonAlphabet, nonAlphabet, nonAlphabet, nonAlphabet, nonAlphabet, nonAlphabet,
54 nonAlphabet, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
55 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E,
56 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
57 0x17, 0x18, 0x19, nonAlphabet, nonAlphabet, nonAlphabet, nonAlphabet, nonAlphabet,
58 nonAlphabet, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20,
59 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
60 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30,
61 0x31, 0x32, 0x33, nonAlphabet, nonAlphabet, nonAlphabet, nonAlphabet, nonAlphabet
62};
63
64static const char base64URLEncMap[64] = {
65 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
66 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50,
67 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
68 0x59, 0x5A, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
69 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E,
70 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76,
71 0x77, 0x78, 0x79, 0x7A, 0x30, 0x31, 0x32, 0x33,
72 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x2D, 0x5F
73};
74
75static const char base64URLDecMap[128] = {
76 nonAlphabet, nonAlphabet, nonAlphabet, nonAlphabet, nonAlphabet, nonAlphabet, nonAlphabet, nonAlphabet,
77 nonAlphabet, nonAlphabet, nonAlphabet, nonAlphabet, nonAlphabet, nonAlphabet, nonAlphabet, nonAlphabet,
78 nonAlphabet, nonAlphabet, nonAlphabet, nonAlphabet, nonAlphabet, nonAlphabet, nonAlphabet, nonAlphabet,
79 nonAlphabet, nonAlphabet, nonAlphabet, nonAlphabet, nonAlphabet, nonAlphabet, nonAlphabet, nonAlphabet,
80 nonAlphabet, nonAlphabet, nonAlphabet, nonAlphabet, nonAlphabet, nonAlphabet, nonAlphabet, nonAlphabet,
81 nonAlphabet, nonAlphabet, nonAlphabet, nonAlphabet, nonAlphabet, 0x3E, nonAlphabet, nonAlphabet,
82 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B,
83 0x3C, 0x3D, nonAlphabet, nonAlphabet, nonAlphabet, nonAlphabet, nonAlphabet, nonAlphabet,
84 nonAlphabet, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
85 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E,
86 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
87 0x17, 0x18, 0x19, nonAlphabet, nonAlphabet, nonAlphabet, nonAlphabet, 0x3F,
88 nonAlphabet, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20,
89 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
90 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30,
91 0x31, 0x32, 0x33, nonAlphabet, nonAlphabet, nonAlphabet, nonAlphabet, nonAlphabet
92};
93
94static inline void base64EncodeInternal(const unsigned char* data, unsigned len, Vector<char>& out, Base64EncodePolicy policy, const char (&encodeMap)[64])
95{
96 out.clear();
97 if (!len)
98 return;
99
100 // If the input string is pathologically large, just return nothing.
101 // Note: Keep this in sync with the "outLength" computation below.
102 // Rather than being perfectly precise, this is a bit conservative.
103 const unsigned maxInputBufferSize = UINT_MAX / 77 * 76 / 4 * 3 - 2;
104 if (len > maxInputBufferSize)
105 return;
106
107 unsigned sidx = 0;
108 unsigned didx = 0;
109
110 unsigned outLength = ((len + 2) / 3) * 4;
111
112 // Deal with the 76 character per line limit specified in RFC 2045.
113 bool insertLFs = (policy == Base64InsertLFs && outLength > 76);
114 if (insertLFs)
115 outLength += ((outLength - 1) / 76);
116
117 int count = 0;
118 out.grow(outLength);
119
120 // 3-byte to 4-byte conversion + 0-63 to ascii printable conversion
121 if (len > 1) {
122 while (sidx < len - 2) {
123 if (insertLFs) {
124 if (count && !(count % 76))
125 out[didx++] = '\n';
126 count += 4;
127 }
128 out[didx++] = encodeMap[(data[sidx] >> 2) & 077];
129 out[didx++] = encodeMap[((data[sidx + 1] >> 4) & 017) | ((data[sidx] << 4) & 077)];
130 out[didx++] = encodeMap[((data[sidx + 2] >> 6) & 003) | ((data[sidx + 1] << 2) & 077)];
131 out[didx++] = encodeMap[data[sidx + 2] & 077];
132 sidx += 3;
133 }
134 }
135
136 if (sidx < len) {
137 if (insertLFs && (count > 0) && !(count % 76))
138 out[didx++] = '\n';
139
140 out[didx++] = encodeMap[(data[sidx] >> 2) & 077];
141 if (sidx < len - 1) {
142 out[didx++] = encodeMap[((data[sidx + 1] >> 4) & 017) | ((data[sidx] << 4) & 077)];
143 out[didx++] = encodeMap[(data[sidx + 1] << 2) & 077];
144 } else
145 out[didx++] = encodeMap[(data[sidx] << 4) & 077];
146 }
147
148 // Add padding
149 if (policy == Base64URLPolicy)
150 out.resize(didx);
151 else {
152 while (didx < out.size()) {
153 out[didx] = '=';
154 ++didx;
155 }
156 }
157}
158
159String base64Encode(const void* data, unsigned length, Base64EncodePolicy policy)
160{
161 Vector<char> result;
162 base64EncodeInternal(static_cast<const unsigned char*>(data), length, result, policy, base64EncMap);
163 return String(result.data(), result.size());
164}
165
166void base64Encode(const void* data, unsigned len, Vector<char>& out, Base64EncodePolicy policy)
167{
168 base64EncodeInternal(static_cast<const unsigned char*>(data), len, out, policy, base64EncMap);
169}
170
171String base64URLEncode(const void* data, unsigned length)
172{
173 Vector<char> result;
174 base64EncodeInternal(static_cast<const unsigned char*>(data), length, result, Base64URLPolicy, base64URLEncMap);
175 return String(result.data(), result.size());
176}
177
178void base64URLEncode(const void* data, unsigned len, Vector<char>& out)
179{
180 base64EncodeInternal(static_cast<const unsigned char*>(data), len, out, Base64URLPolicy, base64URLEncMap);
181}
182
183template<typename T>
184static inline bool base64DecodeInternal(const T* data, unsigned length, SignedOrUnsignedCharVectorAdapter& out, unsigned options, const char (&decodeMap)[128])
185{
186 out.clear();
187 if (!length)
188 return true;
189
190 out.grow(length);
191
192 unsigned equalsSignCount = 0;
193 unsigned outLength = 0;
194 bool hadError = false;
195 for (unsigned idx = 0; idx < length; ++idx) {
196 unsigned ch = data[idx];
197 if (ch == '=') {
198 ++equalsSignCount;
199 // There should never be more than 2 padding characters.
200 if (options & Base64ValidatePadding && equalsSignCount > 2) {
201 hadError = true;
202 break;
203 }
204 } else {
205 char decodedCharacter = ch < WTF_ARRAY_LENGTH(decodeMap) ? decodeMap[ch] : nonAlphabet;
206 if (decodedCharacter != nonAlphabet) {
207 if (equalsSignCount) {
208 hadError = true;
209 break;
210 }
211 out[outLength++] = decodedCharacter;
212 } else if (!(options & Base64IgnoreSpacesAndNewLines) || !isSpaceOrNewline(ch)) {
213 hadError = true;
214 break;
215 }
216 }
217 }
218
219 // Make sure we shrink back the Vector before returning. outLength may be shorter than expected
220 // in case of error or in case of ignored spaces.
221 if (outLength < out.size())
222 out.shrink(outLength);
223
224 if (hadError)
225 return false;
226
227 if (!outLength)
228 return !equalsSignCount;
229
230 // The should be no padding if length is a multiple of 4.
231 // We use (outLength + equalsSignCount) instead of length because we don't want to account for ignored characters (i.e. spaces).
232 if (options & Base64ValidatePadding && equalsSignCount && (outLength + equalsSignCount) % 4)
233 return false;
234
235 // Valid data is (n * 4 + [0,2,3]) characters long.
236 if ((outLength % 4) == 1)
237 return false;
238
239 // 4-byte to 3-byte conversion
240 outLength -= (outLength + 3) / 4;
241 if (!outLength)
242 return false;
243
244 unsigned sidx = 0;
245 unsigned didx = 0;
246 if (outLength > 1) {
247 while (didx < outLength - 2) {
248 out[didx] = (((out[sidx] << 2) & 255) | ((out[sidx + 1] >> 4) & 003));
249 out[didx + 1] = (((out[sidx + 1] << 4) & 255) | ((out[sidx + 2] >> 2) & 017));
250 out[didx + 2] = (((out[sidx + 2] << 6) & 255) | (out[sidx + 3] & 077));
251 sidx += 4;
252 didx += 3;
253 }
254 }
255
256 if (didx < outLength)
257 out[didx] = (((out[sidx] << 2) & 255) | ((out[sidx + 1] >> 4) & 003));
258
259 if (++didx < outLength)
260 out[didx] = (((out[sidx + 1] << 4) & 255) | ((out[sidx + 2] >> 2) & 017));
261
262 if (outLength < out.size())
263 out.shrink(outLength);
264
265 return true;
266}
267
268bool base64Decode(const String& in, SignedOrUnsignedCharVectorAdapter out, unsigned options)
269{
270 unsigned length = in.length();
271 if (!length || in.is8Bit())
272 return base64DecodeInternal(in.characters8(), length, out, options, base64DecMap);
273 return base64DecodeInternal(in.characters16(), length, out, options, base64DecMap);
274}
275
276bool base64Decode(StringView in, SignedOrUnsignedCharVectorAdapter out, unsigned options)
277{
278 unsigned length = in.length();
279 if (!length || in.is8Bit())
280 return base64DecodeInternal(in.characters8(), length, out, options, base64DecMap);
281 return base64DecodeInternal(in.characters16(), length, out, options, base64DecMap);
282}
283
284bool base64Decode(const Vector<char>& in, SignedOrUnsignedCharVectorAdapter out, unsigned options)
285{
286 out.clear();
287
288 // If the input string is pathologically large, just return nothing.
289 if (in.size() > UINT_MAX)
290 return false;
291
292 return base64DecodeInternal(reinterpret_cast<const LChar*>(in.data()), in.size(), out, options, base64DecMap);
293}
294
295bool base64Decode(const char* data, unsigned len, SignedOrUnsignedCharVectorAdapter out, unsigned options)
296{
297 return base64DecodeInternal(reinterpret_cast<const LChar*>(data), len, out, options, base64DecMap);
298}
299
300bool base64URLDecode(const String& in, SignedOrUnsignedCharVectorAdapter out)
301{
302 unsigned length = in.length();
303 if (!length || in.is8Bit())
304 return base64DecodeInternal(in.characters8(), length, out, Base64Default, base64URLDecMap);
305 return base64DecodeInternal(in.characters16(), length, out, Base64Default, base64URLDecMap);
306}
307
308bool base64URLDecode(StringView in, SignedOrUnsignedCharVectorAdapter out)
309{
310 unsigned length = in.length();
311 if (!length || in.is8Bit())
312 return base64DecodeInternal(in.characters8(), length, out, Base64Default, base64URLDecMap);
313 return base64DecodeInternal(in.characters16(), length, out, Base64Default, base64URLDecMap);
314}
315
316bool base64URLDecode(const Vector<char>& in, SignedOrUnsignedCharVectorAdapter out)
317{
318 out.clear();
319
320 // If the input string is pathologically large, just return nothing.
321 if (in.size() > UINT_MAX)
322 return false;
323
324 return base64DecodeInternal(reinterpret_cast<const LChar*>(in.data()), in.size(), out, Base64Default, base64URLDecMap);
325}
326
327bool base64URLDecode(const char* data, unsigned len, SignedOrUnsignedCharVectorAdapter out)
328{
329 return base64DecodeInternal(reinterpret_cast<const LChar*>(data), len, out, Base64Default, base64URLDecMap);
330}
331
332} // namespace WTF
333