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 |
11 This program is distributed in the hope that it will be useful, | 11 This program is distributed in the hope that it will be useful, |
12 but WITHOUT ANY WARRANTY; without even the implied warranty of | 12 but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 GNU General Public License for more details. | 14 GNU General Public License for more details. |
15 | 15 |
16 You should have received a copy of the GNU Library General Public | 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 | 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, US
A. | 18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, US
A. |
19 | 19 |
20 This code is based on the java implementation in HTTPClient | 20 This code is based on the java implementation in HTTPClient |
21 package by Ronald Tschalaer Copyright (C) 1996-1999. | 21 package by Ronald Tschalaer Copyright (C) 1996-1999. |
22 */ | 22 */ |
23 | 23 |
24 #include "config.h" | 24 #include "config.h" |
25 #include "Base64.h" | 25 #include "Base64.h" |
26 | 26 |
| 27 #include "wtf/StringExtras.h" |
27 #include <limits.h> | 28 #include <limits.h> |
28 #include "wtf/StringExtras.h" | |
29 | 29 |
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, |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 out[didx++] = base64EncMap[(data[sidx] >> 2) & 077]; | 103 out[didx++] = base64EncMap[(data[sidx] >> 2) & 077]; |
104 out[didx++] = base64EncMap[((data[sidx + 1] >> 4) & 017) | ((data[si
dx] << 4) & 077)]; | 104 out[didx++] = base64EncMap[((data[sidx + 1] >> 4) & 017) | ((data[si
dx] << 4) & 077)]; |
105 out[didx++] = base64EncMap[((data[sidx + 2] >> 6) & 003) | ((data[si
dx + 1] << 2) & 077)]; | 105 out[didx++] = base64EncMap[((data[sidx + 2] >> 6) & 003) | ((data[si
dx + 1] << 2) & 077)]; |
106 out[didx++] = base64EncMap[data[sidx + 2] & 077]; | 106 out[didx++] = base64EncMap[data[sidx + 2] & 077]; |
107 sidx += 3; | 107 sidx += 3; |
108 } | 108 } |
109 } | 109 } |
110 | 110 |
111 if (sidx < len) { | 111 if (sidx < len) { |
112 if (insertLFs && (count > 0) && !(count % 76)) | 112 if (insertLFs && (count > 0) && !(count % 76)) |
113 out[didx++] = '\n'; | 113 out[didx++] = '\n'; |
114 | 114 |
115 out[didx++] = base64EncMap[(data[sidx] >> 2) & 077]; | 115 out[didx++] = base64EncMap[(data[sidx] >> 2) & 077]; |
116 if (sidx < len - 1) { | 116 if (sidx < len - 1) { |
117 out[didx++] = base64EncMap[((data[sidx + 1] >> 4) & 017) | ((data[si
dx] << 4) & 077)]; | 117 out[didx++] = base64EncMap[((data[sidx + 1] >> 4) & 017) | ((data[si
dx] << 4) & 077)]; |
118 out[didx++] = base64EncMap[(data[sidx + 1] << 2) & 077]; | 118 out[didx++] = base64EncMap[(data[sidx + 1] << 2) & 077]; |
119 } else | 119 } else { |
120 out[didx++] = base64EncMap[(data[sidx] << 4) & 077]; | 120 out[didx++] = base64EncMap[(data[sidx] << 4) & 077]; |
| 121 } |
121 } | 122 } |
122 | 123 |
123 // Add padding | 124 // Add padding |
124 while (didx < out.size()) { | 125 while (didx < out.size()) { |
125 out[didx] = '='; | 126 out[didx] = '='; |
126 ++didx; | 127 ++didx; |
127 } | 128 } |
128 } | 129 } |
129 | 130 |
130 bool base64Decode(const Vector<char>& in, Vector<char>& out, CharacterMatchFunct
ionPtr shouldIgnoreCharacter, Base64DecodePolicy policy) | 131 bool base64Decode(const Vector<char>& in, Vector<char>& out, CharacterMatchFunct
ionPtr shouldIgnoreCharacter, Base64DecodePolicy policy) |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
241 { | 242 { |
242 return base64Encode(data, length, policy).replace('+', '-').replace('/', '_'
); | 243 return base64Encode(data, length, policy).replace('+', '-').replace('/', '_'
); |
243 } | 244 } |
244 | 245 |
245 String normalizeToBase64(const String& encoding) | 246 String normalizeToBase64(const String& encoding) |
246 { | 247 { |
247 return String(encoding).replace('-', '+').replace('_', '/'); | 248 return String(encoding).replace('-', '+').replace('_', '/'); |
248 } | 249 } |
249 | 250 |
250 } // namespace WTF | 251 } // namespace WTF |
OLD | NEW |