/src/curl/lib/curlx/base64.c
Line | Count | Source |
1 | | /*************************************************************************** |
2 | | * _ _ ____ _ |
3 | | * Project ___| | | | _ \| | |
4 | | * / __| | | | |_) | | |
5 | | * | (__| |_| | _ <| |___ |
6 | | * \___|\___/|_| \_\_____| |
7 | | * |
8 | | * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. |
9 | | * |
10 | | * This software is licensed as described in the file COPYING, which |
11 | | * you should have received as part of this distribution. The terms |
12 | | * are also available at https://curl.se/docs/copyright.html. |
13 | | * |
14 | | * You may opt to use, copy, modify, merge, publish, distribute and/or sell |
15 | | * copies of the Software, and permit persons to whom the Software is |
16 | | * furnished to do so, under the terms of the COPYING file. |
17 | | * |
18 | | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
19 | | * KIND, either express or implied. |
20 | | * |
21 | | * SPDX-License-Identifier: curl |
22 | | * |
23 | | ***************************************************************************/ |
24 | | |
25 | | /* Base64 encoding/decoding */ |
26 | | |
27 | | #include "../curl_setup.h" |
28 | | |
29 | | #include <curl/curl.h> |
30 | | #include "warnless.h" |
31 | | #include "base64.h" |
32 | | |
33 | | /* ---- Base64 Encoding/Decoding Table --- */ |
34 | | const char Curl_base64encdec[] = |
35 | | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
36 | | |
37 | | /* The Base 64 encoding with a URL and filename safe alphabet, RFC 4648 |
38 | | section 5 */ |
39 | | static const char base64url[] = |
40 | | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; |
41 | | |
42 | | static const unsigned char decodetable[] = |
43 | | { 62, 255, 255, 255, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255, 255, |
44 | | 255, 255, 255, 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, |
45 | | 17, 18, 19, 20, 21, 22, 23, 24, 25, 255, 255, 255, 255, 255, 255, 26, 27, 28, |
46 | | 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, |
47 | | 48, 49, 50, 51 }; |
48 | | /* |
49 | | * curlx_base64_decode() |
50 | | * |
51 | | * Given a base64 null-terminated string at src, decode it and return a |
52 | | * pointer in *outptr to a newly allocated memory area holding decoded data. |
53 | | * Size of decoded data is returned in variable pointed by outlen. |
54 | | * |
55 | | * Returns CURLE_OK on success, otherwise specific error code. Function |
56 | | * output shall not be considered valid unless CURLE_OK is returned. |
57 | | * |
58 | | * When decoded data length is 0, returns NULL in *outptr. |
59 | | * |
60 | | * @unittest: 1302 |
61 | | */ |
62 | | CURLcode curlx_base64_decode(const char *src, |
63 | | uint8_t **outptr, size_t *outlen) |
64 | 4.42k | { |
65 | 4.42k | size_t srclen = 0; |
66 | 4.42k | size_t padding = 0; |
67 | 4.42k | size_t i; |
68 | 4.42k | size_t numQuantums; |
69 | 4.42k | size_t fullQuantums; |
70 | 4.42k | size_t rawlen = 0; |
71 | 4.42k | unsigned char *pos; |
72 | 4.42k | unsigned char *newstr; |
73 | 4.42k | unsigned char lookup[256]; |
74 | | |
75 | 4.42k | *outptr = NULL; |
76 | 4.42k | *outlen = 0; |
77 | 4.42k | srclen = strlen(src); |
78 | | |
79 | | /* Check the length of the input string is valid */ |
80 | 4.42k | if(!srclen || srclen % 4) |
81 | 2.05k | return CURLE_BAD_CONTENT_ENCODING; |
82 | | |
83 | | /* srclen is at least 4 here */ |
84 | 4.04k | while(src[srclen - 1 - padding] == '=') { |
85 | | /* count padding characters */ |
86 | 1.70k | padding++; |
87 | | /* A maximum of two = padding characters is allowed */ |
88 | 1.70k | if(padding > 2) |
89 | 36 | return CURLE_BAD_CONTENT_ENCODING; |
90 | 1.70k | } |
91 | | |
92 | | /* Calculate the number of quantums */ |
93 | 2.33k | numQuantums = srclen / 4; |
94 | 2.33k | fullQuantums = numQuantums - (padding ? 1 : 0); |
95 | | |
96 | | /* Calculate the size of the decoded string */ |
97 | 2.33k | rawlen = (numQuantums * 3) - padding; |
98 | | |
99 | | /* Allocate our buffer including room for a null-terminator */ |
100 | 2.33k | newstr = curlx_malloc(rawlen + 1); |
101 | 2.33k | if(!newstr) |
102 | 0 | return CURLE_OUT_OF_MEMORY; |
103 | | |
104 | 2.33k | pos = newstr; |
105 | | |
106 | 2.33k | memset(lookup, 0xff, sizeof(lookup)); |
107 | 2.33k | memcpy(&lookup['+'], decodetable, sizeof(decodetable)); |
108 | | |
109 | | /* Decode the complete quantums first */ |
110 | 6.79k | for(i = 0; i < fullQuantums; i++) { |
111 | 5.66k | unsigned char val; |
112 | 5.66k | unsigned int x = 0; |
113 | 5.66k | int j; |
114 | | |
115 | 24.5k | for(j = 0; j < 4; j++) { |
116 | 20.1k | val = lookup[(unsigned char)*src++]; |
117 | 20.1k | if(val == 0xff) /* bad symbol */ |
118 | 1.20k | goto bad; |
119 | 18.8k | x = (x << 6) | val; |
120 | 18.8k | } |
121 | 4.45k | pos[2] = x & 0xff; |
122 | 4.45k | pos[1] = (x >> 8) & 0xff; |
123 | 4.45k | pos[0] = (x >> 16) & 0xff; |
124 | 4.45k | pos += 3; |
125 | 4.45k | } |
126 | 1.12k | if(padding) { |
127 | | /* this means either 8 or 16 bits output */ |
128 | 875 | unsigned char val; |
129 | 875 | unsigned int x = 0; |
130 | 875 | int j; |
131 | 875 | size_t padc = 0; |
132 | 2.29k | for(j = 0; j < 4; j++) { |
133 | 2.20k | if(*src == '=') { |
134 | 404 | x <<= 6; |
135 | 404 | src++; |
136 | 404 | if(++padc > padding) |
137 | | /* this is a badly placed '=' symbol! */ |
138 | 122 | goto bad; |
139 | 404 | } |
140 | 1.79k | else { |
141 | 1.79k | val = lookup[(unsigned char)*src++]; |
142 | 1.79k | if(val == 0xff) /* bad symbol */ |
143 | 661 | goto bad; |
144 | 1.13k | x = (x << 6) | val; |
145 | 1.13k | } |
146 | 2.20k | } |
147 | 92 | if(padding == 1) |
148 | 68 | pos[1] = (x >> 8) & 0xff; |
149 | 92 | pos[0] = (x >> 16) & 0xff; |
150 | 92 | pos += 3 - padding; |
151 | 92 | } |
152 | | |
153 | | /* Zero terminate */ |
154 | 344 | *pos = '\0'; |
155 | | |
156 | | /* Return the decoded data */ |
157 | 344 | *outptr = newstr; |
158 | 344 | *outlen = rawlen; |
159 | | |
160 | 344 | return CURLE_OK; |
161 | 1.99k | bad: |
162 | 1.99k | curlx_free(newstr); |
163 | 1.99k | return CURLE_BAD_CONTENT_ENCODING; |
164 | 1.12k | } |
165 | | |
166 | | static CURLcode base64_encode(const char *table64, |
167 | | uint8_t padbyte, |
168 | | const uint8_t *inputbuff, size_t insize, |
169 | | char **outptr, size_t *outlen) |
170 | 26.2k | { |
171 | 26.2k | char *output; |
172 | 26.2k | char *base64data; |
173 | 26.2k | const unsigned char *in = (const unsigned char *)inputbuff; |
174 | | |
175 | 26.2k | *outptr = NULL; |
176 | 26.2k | *outlen = 0; |
177 | | |
178 | 26.2k | if(!insize) |
179 | 0 | return CURLE_OK; |
180 | | |
181 | | /* safety precaution */ |
182 | 26.2k | DEBUGASSERT(insize <= CURL_MAX_BASE64_INPUT); |
183 | 26.2k | if(insize > CURL_MAX_BASE64_INPUT) |
184 | 0 | return CURLE_TOO_LARGE; |
185 | | |
186 | 26.2k | base64data = output = curlx_malloc((insize + 2) / 3 * 4 + 1); |
187 | 26.2k | if(!output) |
188 | 0 | return CURLE_OUT_OF_MEMORY; |
189 | | |
190 | 6.72M | while(insize >= 3) { |
191 | 6.69M | *output++ = table64[in[0] >> 2]; |
192 | 6.69M | *output++ = table64[((in[0] & 0x03) << 4) | (in[1] >> 4)]; |
193 | 6.69M | *output++ = table64[((in[1] & 0x0F) << 2) | ((in[2] & 0xC0) >> 6)]; |
194 | 6.69M | *output++ = table64[in[2] & 0x3F]; |
195 | 6.69M | insize -= 3; |
196 | 6.69M | in += 3; |
197 | 6.69M | } |
198 | 26.2k | if(insize) { |
199 | | /* this is only one or two bytes now */ |
200 | 20.6k | *output++ = table64[in[0] >> 2]; |
201 | 20.6k | if(insize == 1) { |
202 | 7.77k | *output++ = table64[((in[0] & 0x03) << 4)]; |
203 | 7.77k | if(padbyte) { |
204 | 7.77k | *output++ = padbyte; |
205 | 7.77k | *output++ = padbyte; |
206 | 7.77k | } |
207 | 7.77k | } |
208 | 12.8k | else { |
209 | | /* insize == 2 */ |
210 | 12.8k | *output++ = table64[((in[0] & 0x03) << 4) | ((in[1] & 0xF0) >> 4)]; |
211 | 12.8k | *output++ = table64[((in[1] & 0x0F) << 2)]; |
212 | 12.8k | if(padbyte) |
213 | 12.8k | *output++ = padbyte; |
214 | 12.8k | } |
215 | 20.6k | } |
216 | | |
217 | | /* Zero terminate */ |
218 | 26.2k | *output = '\0'; |
219 | | |
220 | | /* Return the pointer to the new data (allocated memory) */ |
221 | 26.2k | *outptr = base64data; |
222 | | |
223 | | /* Return the length of the new data */ |
224 | 26.2k | *outlen = (size_t)(output - base64data); |
225 | | |
226 | 26.2k | return CURLE_OK; |
227 | 26.2k | } |
228 | | |
229 | | /* |
230 | | * curlx_base64_encode() |
231 | | * |
232 | | * Given a pointer to an input buffer and an input size, encode it and |
233 | | * return a pointer in *outptr to a newly allocated memory area holding |
234 | | * encoded data. Size of encoded data is returned in variable pointed by |
235 | | * outlen. |
236 | | * |
237 | | * Returns CURLE_OK on success, otherwise specific error code. Function |
238 | | * output shall not be considered valid unless CURLE_OK is returned. |
239 | | * |
240 | | * @unittest: 1302 |
241 | | */ |
242 | | CURLcode curlx_base64_encode(const uint8_t *inputbuff, size_t insize, |
243 | | char **outptr, size_t *outlen) |
244 | 25.8k | { |
245 | 25.8k | return base64_encode(Curl_base64encdec, '=', |
246 | 25.8k | inputbuff, insize, outptr, outlen); |
247 | 25.8k | } |
248 | | |
249 | | /* |
250 | | * curlx_base64url_encode() |
251 | | * |
252 | | * Given a pointer to an input buffer and an input size, encode it and |
253 | | * return a pointer in *outptr to a newly allocated memory area holding |
254 | | * encoded data. Size of encoded data is returned in variable pointed by |
255 | | * outlen. |
256 | | * |
257 | | * Input length of 0 indicates input buffer holds a null-terminated string. |
258 | | * |
259 | | * Returns CURLE_OK on success, otherwise specific error code. Function |
260 | | * output shall not be considered valid unless CURLE_OK is returned. |
261 | | * |
262 | | * @unittest: 1302 |
263 | | */ |
264 | | CURLcode curlx_base64url_encode(const uint8_t *inputbuff, size_t insize, |
265 | | char **outptr, size_t *outlen) |
266 | 415 | { |
267 | 415 | return base64_encode(base64url, 0, inputbuff, insize, outptr, outlen); |
268 | 415 | } |