/src/httpd/srclib/apr/encoding/apr_base64.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Licensed to the Apache Software Foundation (ASF) under one or more |
2 | | * contributor license agreements. See the NOTICE file distributed with |
3 | | * this work for additional information regarding copyright ownership. |
4 | | * The ASF licenses this file to You under the Apache License, Version 2.0 |
5 | | * (the "License"); you may not use this file except in compliance with |
6 | | * the License. You may obtain a copy of the License at |
7 | | * |
8 | | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | | * |
10 | | * Unless required by applicable law or agreed to in writing, software |
11 | | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | | * See the License for the specific language governing permissions and |
14 | | * limitations under the License. |
15 | | */ |
16 | | |
17 | | /* base64 encoder/decoder. Originally part of main/util.c |
18 | | * but moved here so that support/ab and apr_sha1.c could |
19 | | * use it. This meant removing the apr_palloc()s and adding |
20 | | * ugly 'len' functions, which is quite a nasty cost. |
21 | | */ |
22 | | |
23 | | #undef NDEBUG /* always abort() on assert()ion failure */ |
24 | | #include <assert.h> |
25 | | |
26 | | #include "apr_base64.h" |
27 | | #if APR_CHARSET_EBCDIC |
28 | | #include "apr_xlate.h" |
29 | | #endif /* APR_CHARSET_EBCDIC */ |
30 | | |
31 | | /* Above APR_BASE64_ENCODE_MAX length the encoding can't fit in an int >= 0 */ |
32 | | #define APR_BASE64_ENCODE_MAX 1610612733 |
33 | | |
34 | | /* Above APR_BASE64_DECODE_MAX length the decoding can't fit in an int >= 0 */ |
35 | | #define APR_BASE64_DECODE_MAX 2863311524u |
36 | | |
37 | | /* aaaack but it's fast and const should make it shared text page. */ |
38 | | static const unsigned char pr2six[256] = |
39 | | { |
40 | | #if !APR_CHARSET_EBCDIC |
41 | | /* ASCII table */ |
42 | | 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, |
43 | | 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, |
44 | | 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63, |
45 | | 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64, |
46 | | 64, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, |
47 | | 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64, |
48 | | 64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, |
49 | | 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64, |
50 | | 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, |
51 | | 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, |
52 | | 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, |
53 | | 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, |
54 | | 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, |
55 | | 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, |
56 | | 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, |
57 | | 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 |
58 | | #else /*APR_CHARSET_EBCDIC*/ |
59 | | /* EBCDIC table */ |
60 | | 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, |
61 | | 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, |
62 | | 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, |
63 | | 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, |
64 | | 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, |
65 | | 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, |
66 | | 64, 63, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, |
67 | | 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, |
68 | | 64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 64, 64, 64, 64, 64, 64, |
69 | | 64, 35, 36, 37, 38, 39, 40, 41, 42, 43, 64, 64, 64, 64, 64, 64, |
70 | | 64, 64, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64, 64, |
71 | | 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, |
72 | | 64, 0, 1, 2, 3, 4, 5, 6, 7, 8, 64, 64, 64, 64, 64, 64, |
73 | | 64, 9, 10, 11, 12, 13, 14, 15, 16, 17, 64, 64, 64, 64, 64, 64, |
74 | | 64, 64, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64, 64, |
75 | | 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64 |
76 | | #endif /*APR_CHARSET_EBCDIC*/ |
77 | | }; |
78 | | |
79 | | #if APR_CHARSET_EBCDIC |
80 | | static apr_xlate_t *xlate_to_ebcdic; |
81 | | static unsigned char os_toascii[256]; |
82 | | |
83 | | APR_DECLARE(apr_status_t) apr_base64init_ebcdic(apr_xlate_t *to_ascii, |
84 | | apr_xlate_t *to_ebcdic) |
85 | | { |
86 | | int i; |
87 | | apr_size_t inbytes_left, outbytes_left; |
88 | | apr_status_t rv; |
89 | | int onoff; |
90 | | |
91 | | /* Only single-byte conversion is supported. |
92 | | */ |
93 | | rv = apr_xlate_sb_get(to_ascii, &onoff); |
94 | | if (rv) { |
95 | | return rv; |
96 | | } |
97 | | if (!onoff) { /* If conversion is not single-byte-only */ |
98 | | return APR_EINVAL; |
99 | | } |
100 | | rv = apr_xlate_sb_get(to_ebcdic, &onoff); |
101 | | if (rv) { |
102 | | return rv; |
103 | | } |
104 | | if (!onoff) { /* If conversion is not single-byte-only */ |
105 | | return APR_EINVAL; |
106 | | } |
107 | | xlate_to_ebcdic = to_ebcdic; |
108 | | for (i = 0; i < sizeof(os_toascii); i++) { |
109 | | os_toascii[i] = i; |
110 | | } |
111 | | inbytes_left = outbytes_left = sizeof(os_toascii); |
112 | | apr_xlate_conv_buffer(to_ascii, os_toascii, &inbytes_left, |
113 | | os_toascii, &outbytes_left); |
114 | | |
115 | | return APR_SUCCESS; |
116 | | } |
117 | | #endif /*APR_CHARSET_EBCDIC*/ |
118 | | |
119 | | APR_DECLARE(int) apr_base64_decode_len(const char *bufcoded) |
120 | 1.29k | { |
121 | 1.29k | register const unsigned char *bufin; |
122 | 1.29k | register apr_size_t nprbytes; |
123 | | |
124 | 1.29k | bufin = (const unsigned char *) bufcoded; |
125 | 7.94k | while (pr2six[*(bufin++)] <= 63); |
126 | 1.29k | nprbytes = (bufin - (const unsigned char *) bufcoded) - 1; |
127 | 1.29k | assert(nprbytes <= APR_BASE64_DECODE_MAX); |
128 | | |
129 | 1.29k | return (int)(((nprbytes + 3u) / 4u) * 3u + 1u); |
130 | 1.29k | } |
131 | | |
132 | | APR_DECLARE(int) apr_base64_decode(char *bufplain, const char *bufcoded) |
133 | 1.29k | { |
134 | | #if APR_CHARSET_EBCDIC |
135 | | apr_size_t inbytes_left, outbytes_left; |
136 | | #endif /* APR_CHARSET_EBCDIC */ |
137 | 1.29k | int len; |
138 | | |
139 | 1.29k | len = apr_base64_decode_binary((unsigned char *) bufplain, bufcoded); |
140 | | #if APR_CHARSET_EBCDIC |
141 | | inbytes_left = outbytes_left = len; |
142 | | apr_xlate_conv_buffer(xlate_to_ebcdic, bufplain, &inbytes_left, |
143 | | bufplain, &outbytes_left); |
144 | | #endif /* APR_CHARSET_EBCDIC */ |
145 | 1.29k | bufplain[len] = '\0'; |
146 | 1.29k | return len; |
147 | 1.29k | } |
148 | | |
149 | | /* This is the same as apr_base64_decode() except: |
150 | | * - no \0 is appended |
151 | | * - on EBCDIC machines, the conversion of the output to ebcdic is left out |
152 | | */ |
153 | | APR_DECLARE(int) apr_base64_decode_binary(unsigned char *bufplain, |
154 | | const char *bufcoded) |
155 | 1.29k | { |
156 | 1.29k | int nbytesdecoded; |
157 | 1.29k | register const unsigned char *bufin; |
158 | 1.29k | register unsigned char *bufout; |
159 | 1.29k | register apr_size_t nprbytes; |
160 | | |
161 | 1.29k | bufin = (const unsigned char *) bufcoded; |
162 | 7.94k | while (pr2six[*(bufin++)] <= 63); |
163 | 1.29k | nprbytes = (bufin - (const unsigned char *) bufcoded) - 1; |
164 | 1.29k | assert(nprbytes <= APR_BASE64_DECODE_MAX); |
165 | 1.29k | nbytesdecoded = (int)(((nprbytes + 3u) / 4u) * 3u); |
166 | | |
167 | 1.29k | bufout = (unsigned char *) bufplain; |
168 | 1.29k | bufin = (const unsigned char *) bufcoded; |
169 | | |
170 | 2.78k | while (nprbytes >= 4) { |
171 | 1.48k | *(bufout++) = |
172 | 1.48k | (unsigned char) (pr2six[*bufin] << 2 | pr2six[bufin[1]] >> 4); |
173 | 1.48k | *(bufout++) = |
174 | 1.48k | (unsigned char) (pr2six[bufin[1]] << 4 | pr2six[bufin[2]] >> 2); |
175 | 1.48k | *(bufout++) = |
176 | 1.48k | (unsigned char) (pr2six[bufin[2]] << 6 | pr2six[bufin[3]]); |
177 | 1.48k | bufin += 4; |
178 | 1.48k | nprbytes -= 4; |
179 | 1.48k | } |
180 | | |
181 | | /* Note: (nprbytes == 1) would be an error, so just ignore that case */ |
182 | 1.29k | if (nprbytes > 1) { |
183 | 228 | *(bufout++) = |
184 | 228 | (unsigned char) (pr2six[*bufin] << 2 | pr2six[bufin[1]] >> 4); |
185 | 228 | } |
186 | 1.29k | if (nprbytes > 2) { |
187 | 128 | *(bufout++) = |
188 | 128 | (unsigned char) (pr2six[bufin[1]] << 4 | pr2six[bufin[2]] >> 2); |
189 | 128 | } |
190 | | |
191 | 1.29k | return nbytesdecoded - (int)((4u - nprbytes) & 3u); |
192 | 1.29k | } |
193 | | |
194 | | APR_DECLARE(char *) apr_pbase64_decode(apr_pool_t *p, const char *bufcoded) |
195 | 0 | { |
196 | 0 | char *decoded; |
197 | |
|
198 | 0 | decoded = (char *) apr_palloc(p, apr_base64_decode_len(bufcoded)); |
199 | 0 | apr_base64_decode(decoded, bufcoded); |
200 | |
|
201 | 0 | return decoded; |
202 | 0 | } |
203 | | |
204 | | static const char basis_64[] = |
205 | | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
206 | | |
207 | | APR_DECLARE(int) apr_base64_encode_len(int len) |
208 | 1.26k | { |
209 | 1.26k | assert(len >= 0 && len <= APR_BASE64_ENCODE_MAX); |
210 | | |
211 | 1.26k | return ((len + 2) / 3 * 4) + 1; |
212 | 1.26k | } |
213 | | |
214 | | APR_DECLARE(int) apr_base64_encode(char *encoded, const char *string, int len) |
215 | 1.26k | { |
216 | 1.26k | #if !APR_CHARSET_EBCDIC |
217 | 1.26k | return apr_base64_encode_binary(encoded, (const unsigned char *) string, len); |
218 | | #else /* APR_CHARSET_EBCDIC */ |
219 | | int i; |
220 | | char *p; |
221 | | |
222 | | assert(len >= 0 && len <= APR_BASE64_ENCODE_MAX); |
223 | | |
224 | | p = encoded; |
225 | | for (i = 0; i < len - 2; i += 3) { |
226 | | *p++ = basis_64[(os_toascii[string[i]] >> 2) & 0x3F]; |
227 | | *p++ = basis_64[((os_toascii[string[i]] & 0x3) << 4) | |
228 | | ((int) (os_toascii[string[i + 1]] & 0xF0) >> 4)]; |
229 | | *p++ = basis_64[((os_toascii[string[i + 1]] & 0xF) << 2) | |
230 | | ((int) (os_toascii[string[i + 2]] & 0xC0) >> 6)]; |
231 | | *p++ = basis_64[os_toascii[string[i + 2]] & 0x3F]; |
232 | | } |
233 | | if (i < len) { |
234 | | *p++ = basis_64[(os_toascii[string[i]] >> 2) & 0x3F]; |
235 | | if (i == (len - 1)) { |
236 | | *p++ = basis_64[((os_toascii[string[i]] & 0x3) << 4)]; |
237 | | *p++ = '='; |
238 | | } |
239 | | else { |
240 | | *p++ = basis_64[((os_toascii[string[i]] & 0x3) << 4) | |
241 | | ((int) (os_toascii[string[i + 1]] & 0xF0) >> 4)]; |
242 | | *p++ = basis_64[((os_toascii[string[i + 1]] & 0xF) << 2)]; |
243 | | } |
244 | | *p++ = '='; |
245 | | } |
246 | | |
247 | | *p++ = '\0'; |
248 | | return (unsigned int)(p - encoded); |
249 | | #endif /* APR_CHARSET_EBCDIC */ |
250 | 1.26k | } |
251 | | |
252 | | /* This is the same as apr_base64_encode() except on EBCDIC machines, where |
253 | | * the conversion of the input to ascii is left out. |
254 | | */ |
255 | | APR_DECLARE(int) apr_base64_encode_binary(char *encoded, |
256 | | const unsigned char *string, int len) |
257 | 1.26k | { |
258 | 1.26k | int i; |
259 | 1.26k | char *p; |
260 | | |
261 | 1.26k | assert(len >= 0 && len <= APR_BASE64_ENCODE_MAX); |
262 | | |
263 | 1.26k | p = encoded; |
264 | 19.2k | for (i = 0; i < len - 2; i += 3) { |
265 | 18.0k | *p++ = basis_64[(string[i] >> 2) & 0x3F]; |
266 | 18.0k | *p++ = basis_64[((string[i] & 0x3) << 4) | |
267 | 18.0k | ((int) (string[i + 1] & 0xF0) >> 4)]; |
268 | 18.0k | *p++ = basis_64[((string[i + 1] & 0xF) << 2) | |
269 | 18.0k | ((int) (string[i + 2] & 0xC0) >> 6)]; |
270 | 18.0k | *p++ = basis_64[string[i + 2] & 0x3F]; |
271 | 18.0k | } |
272 | 1.26k | if (i < len) { |
273 | 608 | *p++ = basis_64[(string[i] >> 2) & 0x3F]; |
274 | 608 | if (i == (len - 1)) { |
275 | 340 | *p++ = basis_64[((string[i] & 0x3) << 4)]; |
276 | 340 | *p++ = '='; |
277 | 340 | } |
278 | 268 | else { |
279 | 268 | *p++ = basis_64[((string[i] & 0x3) << 4) | |
280 | 268 | ((int) (string[i + 1] & 0xF0) >> 4)]; |
281 | 268 | *p++ = basis_64[((string[i + 1] & 0xF) << 2)]; |
282 | 268 | } |
283 | 608 | *p++ = '='; |
284 | 608 | } |
285 | | |
286 | 1.26k | *p++ = '\0'; |
287 | 1.26k | return (unsigned int)(p - encoded); |
288 | 1.26k | } |
289 | | |
290 | | APR_DECLARE(char *) apr_pbase64_encode(apr_pool_t *p, const char *string) |
291 | 0 | { |
292 | 0 | char *encoded; |
293 | 0 | apr_size_t len = strlen(string); |
294 | |
|
295 | 0 | assert(len <= (apr_size_t)APR_BASE64_ENCODE_MAX); |
296 | 0 | encoded = (char *) apr_palloc(p, apr_base64_encode_len((int)len)); |
297 | 0 | apr_base64_encode(encoded, string, (int)len); |
298 | |
|
299 | 0 | return encoded; |
300 | 0 | } |