blob: 35ac76c5a889124c5bb128b20324f5eefb448bd1 [file] [log] [blame]
Brian Silverman41cdd3e2019-01-19 19:48:58 -08001/* ====================================================================
2 * Copyright (c) 1995-1999 The Apache Group. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
14 * distribution.
15 *
16 * 3. All advertising materials mentioning features or use of this
17 * software must display the following acknowledgment:
18 * "This product includes software developed by the Apache Group
19 * for use in the Apache HTTP server project (http://www.apache.org/)."
20 *
21 * 4. The names "Apache Server" and "Apache Group" must not be used to
22 * endorse or promote products derived from this software without
23 * prior written permission. For written permission, please contact
24 * apache@apache.org.
25 *
26 * 5. Products derived from this software may not be called "Apache"
27 * nor may "Apache" appear in their names without prior written
28 * permission of the Apache Group.
29 *
30 * 6. Redistributions of any form whatsoever must retain the following
31 * acknowledgment:
32 * "This product includes software developed by the Apache Group
33 * for use in the Apache HTTP server project (http://www.apache.org/)."
34 *
35 * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
36 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE GROUP OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46 * OF THE POSSIBILITY OF SUCH DAMAGE.
47 * ====================================================================
48 *
49 * This software consists of voluntary contributions made by many
50 * individuals on behalf of the Apache Group and was originally based
51 * on public domain software written at the National Center for
52 * Supercomputing Applications, University of Illinois, Urbana-Champaign.
53 * For more information on the Apache Group and the Apache HTTP server
54 * project, please see <http://www.apache.org/>.
55 *
56 */
57
58#include "wpi/Base64.h"
59
60#include "wpi/SmallVector.h"
61#include "wpi/raw_ostream.h"
62
63namespace wpi {
64
65// aaaack but it's fast and const should make it shared text page.
66static const unsigned char pr2six[256] = {
67 // ASCII table
68 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
69 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
70 64, 64, 64, 64, 64, 62, 64, 64, 64, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60,
71 61, 64, 64, 64, 64, 64, 64, 64, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
72 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64,
73 64, 64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42,
74 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
75 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
76 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
77 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
78 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
79 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
80 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
81 64, 64, 64, 64, 64, 64, 64, 64, 64};
82
83size_t Base64Decode(raw_ostream& os, StringRef encoded) {
84 const unsigned char* end = encoded.bytes_begin();
85 while (pr2six[*end] <= 63 && end != encoded.bytes_end()) ++end;
86 size_t nprbytes = end - encoded.bytes_begin();
87 if (nprbytes == 0) return 0;
88
89 const unsigned char* cur = encoded.bytes_begin();
90
91 while (nprbytes > 4) {
92 os << static_cast<unsigned char>(pr2six[cur[0]] << 2 | pr2six[cur[1]] >> 4);
93 os << static_cast<unsigned char>(pr2six[cur[1]] << 4 | pr2six[cur[2]] >> 2);
94 os << static_cast<unsigned char>(pr2six[cur[2]] << 6 | pr2six[cur[3]]);
95 cur += 4;
96 nprbytes -= 4;
97 }
98
99 // Note: (nprbytes == 1) would be an error, so just ignore that case
100 if (nprbytes > 1)
101 os << static_cast<unsigned char>(pr2six[cur[0]] << 2 | pr2six[cur[1]] >> 4);
102 if (nprbytes > 2)
103 os << static_cast<unsigned char>(pr2six[cur[1]] << 4 | pr2six[cur[2]] >> 2);
104 if (nprbytes > 3)
105 os << static_cast<unsigned char>(pr2six[cur[2]] << 6 | pr2six[cur[3]]);
106
107 return (end - encoded.bytes_begin()) + ((4 - nprbytes) & 3);
108}
109
110size_t Base64Decode(StringRef encoded, std::string* plain) {
111 plain->resize(0);
112 raw_string_ostream os(*plain);
113 size_t rv = Base64Decode(os, encoded);
114 os.flush();
115 return rv;
116}
117
118StringRef Base64Decode(StringRef encoded, size_t* num_read,
119 SmallVectorImpl<char>& buf) {
120 buf.clear();
121 raw_svector_ostream os(buf);
122 *num_read = Base64Decode(os, encoded);
123 return os.str();
124}
125
126static const char basis_64[] =
127 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
128
129void Base64Encode(raw_ostream& os, StringRef plain) {
130 if (plain.empty()) return;
131 size_t len = plain.size();
132
133 size_t i;
134 for (i = 0; (i + 2) < len; i += 3) {
135 os << basis_64[(plain[i] >> 2) & 0x3F];
136 os << basis_64[((plain[i] & 0x3) << 4) |
137 (static_cast<int>(plain[i + 1] & 0xF0) >> 4)];
138 os << basis_64[((plain[i + 1] & 0xF) << 2) |
139 (static_cast<int>(plain[i + 2] & 0xC0) >> 6)];
140 os << basis_64[plain[i + 2] & 0x3F];
141 }
142 if (i < len) {
143 os << basis_64[(plain[i] >> 2) & 0x3F];
144 if (i == (len - 1)) {
145 os << basis_64[((plain[i] & 0x3) << 4)];
146 os << '=';
147 } else {
148 os << basis_64[((plain[i] & 0x3) << 4) |
149 (static_cast<int>(plain[i + 1] & 0xF0) >> 4)];
150 os << basis_64[((plain[i + 1] & 0xF) << 2)];
151 }
152 os << '=';
153 }
154}
155
156void Base64Encode(StringRef plain, std::string* encoded) {
157 encoded->resize(0);
158 raw_string_ostream os(*encoded);
159 Base64Encode(os, plain);
160 os.flush();
161}
162
163StringRef Base64Encode(StringRef plain, SmallVectorImpl<char>& buf) {
164 buf.clear();
165 raw_svector_ostream os(buf);
166 Base64Encode(os, plain);
167 return os.str();
168}
169
170} // namespace wpi