| OLD | NEW |
| 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 19 matching lines...) Expand all Loading... |
| 30 namespace WTF { | 30 namespace WTF { |
| 31 | 31 |
| 32 static const char base64EncMap[64] = { | 32 static const char base64EncMap[64] = { |
| 33 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, | 33 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, |
| 34 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, | 34 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, |
| 35 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, | 35 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, |
| 36 0x59, 0x5A, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, | 36 0x59, 0x5A, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, |
| 37 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, | 37 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, |
| 38 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, | 38 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, |
| 39 0x77, 0x78, 0x79, 0x7A, 0x30, 0x31, 0x32, 0x33, | 39 0x77, 0x78, 0x79, 0x7A, 0x30, 0x31, 0x32, 0x33, |
| 40 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x2B, 0x2F | 40 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x2B, 0x2F}; |
| 41 }; | |
| 42 | 41 |
| 43 static const char base64DecMap[128] = { | 42 static const char base64DecMap[128] = { |
| 44 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | 43 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 45 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | 44 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 46 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | 45 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 47 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | 46 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 48 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | 47 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 49 0x00, 0x00, 0x00, 0x3E, 0x00, 0x00, 0x00, 0x3F, | 48 0x00, 0x00, 0x00, 0x3E, 0x00, 0x00, 0x00, 0x3F, |
| 50 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, | 49 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, |
| 51 0x3C, 0x3D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | 50 0x3C, 0x3D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 52 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, | 51 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, |
| 53 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, | 52 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, |
| 54 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, | 53 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, |
| 55 0x17, 0x18, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00, | 54 0x17, 0x18, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 56 0x00, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, | 55 0x00, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, |
| 57 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, | 56 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, |
| 58 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, | 57 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, |
| 59 0x31, 0x32, 0x33, 0x00, 0x00, 0x00, 0x00, 0x00 | 58 0x31, 0x32, 0x33, 0x00, 0x00, 0x00, 0x00, 0x00}; |
| 60 }; | |
| 61 | 59 |
| 62 String base64Encode(const char* data, unsigned length, Base64EncodePolicy policy
) | 60 String base64Encode(const char* data, unsigned length, Base64EncodePolicy policy
) { |
| 63 { | 61 Vector<char> result; |
| 64 Vector<char> result; | 62 base64Encode(data, length, result, policy); |
| 65 base64Encode(data, length, result, policy); | 63 return String(result.data(), result.size()); |
| 66 return String(result.data(), result.size()); | |
| 67 } | 64 } |
| 68 | 65 |
| 69 void base64Encode(const char* data, unsigned len, Vector<char>& out, Base64Encod
ePolicy policy) | 66 void base64Encode(const char* data, unsigned len, Vector<char>& out, Base64Encod
ePolicy policy) { |
| 70 { | 67 out.clear(); |
| 71 out.clear(); | 68 if (!len) |
| 72 if (!len) | 69 return; |
| 73 return; | |
| 74 | 70 |
| 75 // If the input string is pathologically large, just return nothing. | 71 // If the input string is pathologically large, just return nothing. |
| 76 // Note: Keep this in sync with the "outLength" computation below. | 72 // Note: Keep this in sync with the "outLength" computation below. |
| 77 // Rather than being perfectly precise, this is a bit conservative. | 73 // Rather than being perfectly precise, this is a bit conservative. |
| 78 const unsigned maxInputBufferSize = UINT_MAX / 77 * 76 / 4 * 3 - 2; | 74 const unsigned maxInputBufferSize = UINT_MAX / 77 * 76 / 4 * 3 - 2; |
| 79 if (len > maxInputBufferSize) | 75 if (len > maxInputBufferSize) |
| 80 return; | 76 return; |
| 81 | 77 |
| 82 unsigned sidx = 0; | 78 unsigned sidx = 0; |
| 83 unsigned didx = 0; | 79 unsigned didx = 0; |
| 84 | 80 |
| 85 unsigned outLength = ((len + 2) / 3) * 4; | 81 unsigned outLength = ((len + 2) / 3) * 4; |
| 86 | 82 |
| 87 // Deal with the 76 character per line limit specified in RFC 2045. | 83 // Deal with the 76 character per line limit specified in RFC 2045. |
| 88 bool insertLFs = (policy == Base64InsertLFs && outLength > 76); | 84 bool insertLFs = (policy == Base64InsertLFs && outLength > 76); |
| 89 if (insertLFs) | 85 if (insertLFs) |
| 90 outLength += ((outLength - 1) / 76); | 86 outLength += ((outLength - 1) / 76); |
| 91 | 87 |
| 92 int count = 0; | 88 int count = 0; |
| 93 out.grow(outLength); | 89 out.grow(outLength); |
| 94 | 90 |
| 95 // 3-byte to 4-byte conversion + 0-63 to ascii printable conversion | 91 // 3-byte to 4-byte conversion + 0-63 to ascii printable conversion |
| 96 if (len > 1) { | 92 if (len > 1) { |
| 97 while (sidx < len - 2) { | 93 while (sidx < len - 2) { |
| 98 if (insertLFs) { | 94 if (insertLFs) { |
| 99 if (count && !(count % 76)) | 95 if (count && !(count % 76)) |
| 100 out[didx++] = '\n'; | 96 out[didx++] = '\n'; |
| 101 count += 4; | 97 count += 4; |
| 102 } | 98 } |
| 103 out[didx++] = base64EncMap[(data[sidx] >> 2) & 077]; | 99 out[didx++] = base64EncMap[(data[sidx] >> 2) & 077]; |
| 104 out[didx++] = base64EncMap[((data[sidx + 1] >> 4) & 017) | ((data[si
dx] << 4) & 077)]; | 100 out[didx++] = base64EncMap[((data[sidx + 1] >> 4) & 017) | ((data[sidx] <<
4) & 077)]; |
| 105 out[didx++] = base64EncMap[((data[sidx + 2] >> 6) & 003) | ((data[si
dx + 1] << 2) & 077)]; | 101 out[didx++] = base64EncMap[((data[sidx + 2] >> 6) & 003) | ((data[sidx + 1
] << 2) & 077)]; |
| 106 out[didx++] = base64EncMap[data[sidx + 2] & 077]; | 102 out[didx++] = base64EncMap[data[sidx + 2] & 077]; |
| 107 sidx += 3; | 103 sidx += 3; |
| 108 } | |
| 109 } | 104 } |
| 105 } |
| 110 | 106 |
| 111 if (sidx < len) { | 107 if (sidx < len) { |
| 112 if (insertLFs && (count > 0) && !(count % 76)) | 108 if (insertLFs && (count > 0) && !(count % 76)) |
| 113 out[didx++] = '\n'; | 109 out[didx++] = '\n'; |
| 114 | 110 |
| 115 out[didx++] = base64EncMap[(data[sidx] >> 2) & 077]; | 111 out[didx++] = base64EncMap[(data[sidx] >> 2) & 077]; |
| 116 if (sidx < len - 1) { | 112 if (sidx < len - 1) { |
| 117 out[didx++] = base64EncMap[((data[sidx + 1] >> 4) & 017) | ((data[si
dx] << 4) & 077)]; | 113 out[didx++] = base64EncMap[((data[sidx + 1] >> 4) & 017) | ((data[sidx] <<
4) & 077)]; |
| 118 out[didx++] = base64EncMap[(data[sidx + 1] << 2) & 077]; | 114 out[didx++] = base64EncMap[(data[sidx + 1] << 2) & 077]; |
| 119 } else { | 115 } else { |
| 120 out[didx++] = base64EncMap[(data[sidx] << 4) & 077]; | 116 out[didx++] = base64EncMap[(data[sidx] << 4) & 077]; |
| 121 } | |
| 122 } | 117 } |
| 118 } |
| 123 | 119 |
| 124 // Add padding | 120 // Add padding |
| 125 while (didx < out.size()) { | 121 while (didx < out.size()) { |
| 126 out[didx] = '='; | 122 out[didx] = '='; |
| 127 ++didx; | 123 ++didx; |
| 128 } | 124 } |
| 129 } | 125 } |
| 130 | 126 |
| 131 bool base64Decode(const Vector<char>& in, Vector<char>& out, CharacterMatchFunct
ionPtr shouldIgnoreCharacter, Base64DecodePolicy policy) | 127 bool base64Decode(const Vector<char>& in, Vector<char>& out, CharacterMatchFunct
ionPtr shouldIgnoreCharacter, Base64DecodePolicy policy) { |
| 132 { | 128 out.clear(); |
| 133 out.clear(); | |
| 134 | 129 |
| 135 // If the input string is pathologically large, just return nothing. | 130 // If the input string is pathologically large, just return nothing. |
| 136 if (in.size() > UINT_MAX) | 131 if (in.size() > UINT_MAX) |
| 137 return false; | 132 return false; |
| 138 | 133 |
| 139 return base64Decode(in.data(), in.size(), out, shouldIgnoreCharacter, policy
); | 134 return base64Decode(in.data(), in.size(), out, shouldIgnoreCharacter, policy); |
| 140 } | 135 } |
| 141 | 136 |
| 142 template<typename T> | 137 template <typename T> |
| 143 static inline bool base64DecodeInternal(const T* data, unsigned length, Vector<c
har>& out, CharacterMatchFunctionPtr shouldIgnoreCharacter, Base64DecodePolicy p
olicy) | 138 static inline bool base64DecodeInternal(const T* data, unsigned length, Vector<c
har>& out, CharacterMatchFunctionPtr shouldIgnoreCharacter, Base64DecodePolicy p
olicy) { |
| 144 { | 139 out.clear(); |
| 145 out.clear(); | 140 if (!length) |
| 146 if (!length) | 141 return true; |
| 147 return true; | |
| 148 | 142 |
| 149 out.grow(length); | 143 out.grow(length); |
| 150 | 144 |
| 151 unsigned equalsSignCount = 0; | 145 unsigned equalsSignCount = 0; |
| 152 unsigned outLength = 0; | 146 unsigned outLength = 0; |
| 153 bool hadError = false; | 147 bool hadError = false; |
| 154 for (unsigned idx = 0; idx < length; ++idx) { | 148 for (unsigned idx = 0; idx < length; ++idx) { |
| 155 UChar ch = data[idx]; | 149 UChar ch = data[idx]; |
| 156 if (ch == '=') { | 150 if (ch == '=') { |
| 157 ++equalsSignCount; | 151 ++equalsSignCount; |
| 158 // There should never be more than 2 padding characters. | 152 // There should never be more than 2 padding characters. |
| 159 if (policy == Base64ValidatePadding && equalsSignCount > 2) { | 153 if (policy == Base64ValidatePadding && equalsSignCount > 2) { |
| 160 hadError = true; | 154 hadError = true; |
| 161 break; | 155 break; |
| 162 } | 156 } |
| 163 } else if (('0' <= ch && ch <= '9') || ('A' <= ch && ch <= 'Z') || ('a'
<= ch && ch <= 'z') || ch == '+' || ch == '/') { | 157 } else if (('0' <= ch && ch <= '9') || ('A' <= ch && ch <= 'Z') || ('a' <= c
h && ch <= 'z') || ch == '+' || ch == '/') { |
| 164 if (equalsSignCount) { | 158 if (equalsSignCount) { |
| 165 hadError = true; | 159 hadError = true; |
| 166 break; | 160 break; |
| 167 } | 161 } |
| 168 out[outLength++] = base64DecMap[ch]; | 162 out[outLength++] = base64DecMap[ch]; |
| 169 } else if (!shouldIgnoreCharacter || !shouldIgnoreCharacter(ch)) { | 163 } else if (!shouldIgnoreCharacter || !shouldIgnoreCharacter(ch)) { |
| 170 hadError = true; | 164 hadError = true; |
| 171 break; | 165 break; |
| 172 } | |
| 173 } | 166 } |
| 167 } |
| 174 | 168 |
| 175 if (outLength < out.size()) | 169 if (outLength < out.size()) |
| 176 out.shrink(outLength); | 170 out.shrink(outLength); |
| 177 | 171 |
| 178 if (hadError) | 172 if (hadError) |
| 179 return false; | 173 return false; |
| 180 | 174 |
| 181 if (!outLength) | 175 if (!outLength) |
| 182 return !equalsSignCount; | 176 return !equalsSignCount; |
| 183 | 177 |
| 184 // There should be no padding if length is a multiple of 4. | 178 // There should be no padding if length is a multiple of 4. |
| 185 // We use (outLength + equalsSignCount) instead of length because we don't w
ant to account for ignored characters. | 179 // We use (outLength + equalsSignCount) instead of length because we don't wan
t to account for ignored characters. |
| 186 if (policy == Base64ValidatePadding && equalsSignCount && (outLength + equal
sSignCount) % 4) | 180 if (policy == Base64ValidatePadding && equalsSignCount && (outLength + equalsS
ignCount) % 4) |
| 187 return false; | 181 return false; |
| 188 | 182 |
| 189 // Valid data is (n * 4 + [0,2,3]) characters long. | 183 // Valid data is (n * 4 + [0,2,3]) characters long. |
| 190 if ((outLength % 4) == 1) | 184 if ((outLength % 4) == 1) |
| 191 return false; | 185 return false; |
| 192 | 186 |
| 193 // 4-byte to 3-byte conversion | 187 // 4-byte to 3-byte conversion |
| 194 outLength -= (outLength + 3) / 4; | 188 outLength -= (outLength + 3) / 4; |
| 195 if (!outLength) | 189 if (!outLength) |
| 196 return false; | 190 return false; |
| 197 | 191 |
| 198 unsigned sidx = 0; | 192 unsigned sidx = 0; |
| 199 unsigned didx = 0; | 193 unsigned didx = 0; |
| 200 if (outLength > 1) { | 194 if (outLength > 1) { |
| 201 while (didx < outLength - 2) { | 195 while (didx < outLength - 2) { |
| 202 out[didx] = (((out[sidx] << 2) & 255) | ((out[sidx + 1] >> 4) & 003)
); | 196 out[didx] = (((out[sidx] << 2) & 255) | ((out[sidx + 1] >> 4) & 003)); |
| 203 out[didx + 1] = (((out[sidx + 1] << 4) & 255) | ((out[sidx + 2] >> 2
) & 017)); | 197 out[didx + 1] = (((out[sidx + 1] << 4) & 255) | ((out[sidx + 2] >> 2) & 01
7)); |
| 204 out[didx + 2] = (((out[sidx + 2] << 6) & 255) | (out[sidx + 3] & 077
)); | 198 out[didx + 2] = (((out[sidx + 2] << 6) & 255) | (out[sidx + 3] & 077)); |
| 205 sidx += 4; | 199 sidx += 4; |
| 206 didx += 3; | 200 didx += 3; |
| 207 } | |
| 208 } | 201 } |
| 202 } |
| 209 | 203 |
| 210 if (didx < outLength) | 204 if (didx < outLength) |
| 211 out[didx] = (((out[sidx] << 2) & 255) | ((out[sidx + 1] >> 4) & 003)); | 205 out[didx] = (((out[sidx] << 2) & 255) | ((out[sidx + 1] >> 4) & 003)); |
| 212 | 206 |
| 213 if (++didx < outLength) | 207 if (++didx < outLength) |
| 214 out[didx] = (((out[sidx + 1] << 4) & 255) | ((out[sidx + 2] >> 2) & 017)
); | 208 out[didx] = (((out[sidx + 1] << 4) & 255) | ((out[sidx + 2] >> 2) & 017)); |
| 215 | 209 |
| 216 if (outLength < out.size()) | 210 if (outLength < out.size()) |
| 217 out.shrink(outLength); | 211 out.shrink(outLength); |
| 218 | 212 |
| 219 return true; | 213 return true; |
| 220 } | 214 } |
| 221 | 215 |
| 222 bool base64Decode(const char* data, unsigned length, Vector<char>& out, Characte
rMatchFunctionPtr shouldIgnoreCharacter, Base64DecodePolicy policy) | 216 bool base64Decode(const char* data, unsigned length, Vector<char>& out, Characte
rMatchFunctionPtr shouldIgnoreCharacter, Base64DecodePolicy policy) { |
| 223 { | 217 return base64DecodeInternal<LChar>(reinterpret_cast<const LChar*>(data), lengt
h, out, shouldIgnoreCharacter, policy); |
| 224 return base64DecodeInternal<LChar>(reinterpret_cast<const LChar*>(data), len
gth, out, shouldIgnoreCharacter, policy); | |
| 225 } | 218 } |
| 226 | 219 |
| 227 bool base64Decode(const UChar* data, unsigned length, Vector<char>& out, Charact
erMatchFunctionPtr shouldIgnoreCharacter, Base64DecodePolicy policy) | 220 bool base64Decode(const UChar* data, unsigned length, Vector<char>& out, Charact
erMatchFunctionPtr shouldIgnoreCharacter, Base64DecodePolicy policy) { |
| 228 { | 221 return base64DecodeInternal<UChar>(data, length, out, shouldIgnoreCharacter, p
olicy); |
| 229 return base64DecodeInternal<UChar>(data, length, out, shouldIgnoreCharacter,
policy); | |
| 230 } | 222 } |
| 231 | 223 |
| 232 bool base64Decode(const String& in, Vector<char>& out, CharacterMatchFunctionPtr
shouldIgnoreCharacter, Base64DecodePolicy policy) | 224 bool base64Decode(const String& in, Vector<char>& out, CharacterMatchFunctionPtr
shouldIgnoreCharacter, Base64DecodePolicy policy) { |
| 233 { | 225 if (in.isEmpty()) |
| 234 if (in.isEmpty()) | 226 return base64DecodeInternal<LChar>(0, 0, out, shouldIgnoreCharacter, policy)
; |
| 235 return base64DecodeInternal<LChar>(0, 0, out, shouldIgnoreCharacter, pol
icy); | 227 if (in.is8Bit()) |
| 236 if (in.is8Bit()) | 228 return base64DecodeInternal<LChar>(in.characters8(), in.length(), out, shoul
dIgnoreCharacter, policy); |
| 237 return base64DecodeInternal<LChar>(in.characters8(), in.length(), out, s
houldIgnoreCharacter, policy); | 229 return base64DecodeInternal<UChar>(in.characters16(), in.length(), out, should
IgnoreCharacter, policy); |
| 238 return base64DecodeInternal<UChar>(in.characters16(), in.length(), out, shou
ldIgnoreCharacter, policy); | |
| 239 } | 230 } |
| 240 | 231 |
| 241 String base64URLEncode(const char* data, unsigned length, Base64EncodePolicy pol
icy) | 232 String base64URLEncode(const char* data, unsigned length, Base64EncodePolicy pol
icy) { |
| 242 { | 233 return base64Encode(data, length, policy).replace('+', '-').replace('/', '_'); |
| 243 return base64Encode(data, length, policy).replace('+', '-').replace('/', '_'
); | |
| 244 } | 234 } |
| 245 | 235 |
| 246 String normalizeToBase64(const String& encoding) | 236 String normalizeToBase64(const String& encoding) { |
| 247 { | 237 return String(encoding).replace('-', '+').replace('_', '/'); |
| 248 return String(encoding).replace('-', '+').replace('_', '/'); | |
| 249 } | 238 } |
| 250 | 239 |
| 251 } // namespace WTF | 240 } // namespace WTF |
| OLD | NEW |