/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 "base64.h" |
30 | | |
31 | | /* ---- Base64 Encoding/Decoding Table --- */ |
32 | | const char curlx_base64encdec[] = |
33 | | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
34 | | |
35 | | /* The Base 64 encoding with a URL and filename safe alphabet, RFC 4648 |
36 | | section 5 */ |
37 | | static const char base64url[] = |
38 | | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; |
39 | | |
40 | | static const unsigned char decodetable[] = { |
41 | | 62, 255, 255, 255, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, |
42 | | 255, 255, 255, 255, 255, 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, |
43 | | 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, |
44 | | 255, 255, 255, 255, 255, 255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, |
45 | | 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51 |
46 | | }; |
47 | | /* |
48 | | * curlx_base64_decode() |
49 | | * |
50 | | * Given a base64 null-terminated string at src, decode it and return a |
51 | | * pointer in *outptr to a newly allocated memory area holding decoded data. |
52 | | * Size of decoded data is returned in variable pointed by outlen. |
53 | | * |
54 | | * Returns CURLE_OK on success, otherwise specific error code. Function |
55 | | * output shall not be considered valid unless CURLE_OK is returned. |
56 | | * |
57 | | * When decoded data length is 0, returns NULL in *outptr. |
58 | | * |
59 | | * @unittest: 1302 |
60 | | */ |
61 | | CURLcode curlx_base64_decode(const char *src, |
62 | | uint8_t **outptr, size_t *outlen) |
63 | 0 | { |
64 | 0 | size_t srclen = 0; |
65 | 0 | size_t padding = 0; |
66 | 0 | size_t i; |
67 | 0 | size_t numQuantums; |
68 | 0 | size_t fullQuantums; |
69 | 0 | size_t rawlen = 0; |
70 | 0 | unsigned char *pos; |
71 | 0 | unsigned char *newstr; |
72 | 0 | unsigned char lookup[256]; |
73 | |
|
74 | 0 | *outptr = NULL; |
75 | 0 | *outlen = 0; |
76 | 0 | srclen = strlen(src); |
77 | | |
78 | | /* Check the length of the input string is valid */ |
79 | 0 | if(!srclen || srclen % 4) |
80 | 0 | return CURLE_BAD_CONTENT_ENCODING; |
81 | | |
82 | | /* srclen is at least 4 here */ |
83 | 0 | while(src[srclen - 1 - padding] == '=') { |
84 | | /* count padding characters */ |
85 | 0 | padding++; |
86 | | /* A maximum of two = padding characters is allowed */ |
87 | 0 | if(padding > 2) |
88 | 0 | return CURLE_BAD_CONTENT_ENCODING; |
89 | 0 | } |
90 | | |
91 | | /* Calculate the number of quantums */ |
92 | 0 | numQuantums = srclen / 4; |
93 | 0 | fullQuantums = numQuantums - (padding ? 1 : 0); |
94 | | |
95 | | /* Calculate the size of the decoded string */ |
96 | 0 | rawlen = (numQuantums * 3) - padding; |
97 | | |
98 | | /* Allocate our buffer including room for a null-terminator */ |
99 | 0 | newstr = curlx_malloc(rawlen + 1); |
100 | 0 | if(!newstr) |
101 | 0 | return CURLE_OUT_OF_MEMORY; |
102 | | |
103 | 0 | pos = newstr; |
104 | |
|
105 | 0 | memset(lookup, 0xff, sizeof(lookup)); |
106 | 0 | memcpy(&lookup['+'], decodetable, sizeof(decodetable)); |
107 | | |
108 | | /* Decode the complete quantums first */ |
109 | 0 | for(i = 0; i < fullQuantums; i++) { |
110 | 0 | unsigned char val; |
111 | 0 | unsigned int x = 0; |
112 | 0 | int j; |
113 | |
|
114 | 0 | for(j = 0; j < 4; j++) { |
115 | 0 | val = lookup[(unsigned char)*src++]; |
116 | 0 | if(val == 0xff) /* bad symbol */ |
117 | 0 | goto bad; |
118 | 0 | x = (x << 6) | val; |
119 | 0 | } |
120 | 0 | pos[2] = x & 0xff; |
121 | 0 | pos[1] = (x >> 8) & 0xff; |
122 | 0 | pos[0] = (x >> 16) & 0xff; |
123 | 0 | pos += 3; |
124 | 0 | } |
125 | 0 | if(padding) { |
126 | | /* this means either 8 or 16 bits output */ |
127 | 0 | unsigned char val; |
128 | 0 | unsigned int x = 0; |
129 | 0 | int j; |
130 | 0 | size_t padc = 0; |
131 | 0 | for(j = 0; j < 4; j++) { |
132 | 0 | if(*src == '=') { |
133 | 0 | x <<= 6; |
134 | 0 | src++; |
135 | 0 | if(++padc > padding) |
136 | | /* this is a badly placed '=' symbol! */ |
137 | 0 | goto bad; |
138 | 0 | } |
139 | 0 | else { |
140 | 0 | val = lookup[(unsigned char)*src++]; |
141 | 0 | if(val == 0xff) /* bad symbol */ |
142 | 0 | goto bad; |
143 | 0 | x = (x << 6) | val; |
144 | 0 | } |
145 | 0 | } |
146 | 0 | if(padding == 1) |
147 | 0 | pos[1] = (x >> 8) & 0xff; |
148 | 0 | pos[0] = (x >> 16) & 0xff; |
149 | 0 | pos += 3 - padding; |
150 | 0 | } |
151 | | |
152 | | /* Zero terminate */ |
153 | 0 | *pos = '\0'; |
154 | | |
155 | | /* Return the decoded data */ |
156 | 0 | *outptr = newstr; |
157 | 0 | *outlen = rawlen; |
158 | |
|
159 | 0 | return CURLE_OK; |
160 | 0 | bad: |
161 | 0 | curlx_free(newstr); |
162 | 0 | return CURLE_BAD_CONTENT_ENCODING; |
163 | 0 | } |
164 | | |
165 | | static CURLcode base64_encode(const char *table64, |
166 | | uint8_t padbyte, |
167 | | const uint8_t *inputbuff, size_t insize, |
168 | | char **outptr, size_t *outlen) |
169 | 0 | { |
170 | 0 | char *output; |
171 | 0 | char *base64data; |
172 | 0 | const unsigned char *in = (const unsigned char *)inputbuff; |
173 | |
|
174 | 0 | *outptr = NULL; |
175 | 0 | *outlen = 0; |
176 | |
|
177 | 0 | if(!insize) |
178 | 0 | return CURLE_OK; |
179 | | |
180 | | /* safety precaution */ |
181 | 0 | DEBUGASSERT(insize <= CURL_MAX_BASE64_INPUT); |
182 | 0 | if(insize > CURL_MAX_BASE64_INPUT) |
183 | 0 | return CURLE_TOO_LARGE; |
184 | | |
185 | 0 | base64data = output = curlx_malloc((insize + 2) / 3 * 4 + 1); |
186 | 0 | if(!output) |
187 | 0 | return CURLE_OUT_OF_MEMORY; |
188 | | |
189 | 0 | while(insize >= 3) { |
190 | 0 | *output++ = table64[in[0] >> 2]; |
191 | 0 | *output++ = table64[((in[0] & 0x03) << 4) | (in[1] >> 4)]; |
192 | 0 | *output++ = table64[((in[1] & 0x0F) << 2) | ((in[2] & 0xC0) >> 6)]; |
193 | 0 | *output++ = table64[in[2] & 0x3F]; |
194 | 0 | insize -= 3; |
195 | 0 | in += 3; |
196 | 0 | } |
197 | 0 | if(insize) { |
198 | | /* this is only one or two bytes now */ |
199 | 0 | *output++ = table64[in[0] >> 2]; |
200 | 0 | if(insize == 1) { |
201 | 0 | *output++ = table64[((in[0] & 0x03) << 4)]; |
202 | 0 | if(padbyte) { |
203 | 0 | *output++ = padbyte; |
204 | 0 | *output++ = padbyte; |
205 | 0 | } |
206 | 0 | } |
207 | 0 | else { |
208 | | /* insize == 2 */ |
209 | 0 | *output++ = table64[((in[0] & 0x03) << 4) | ((in[1] & 0xF0) >> 4)]; |
210 | 0 | *output++ = table64[((in[1] & 0x0F) << 2)]; |
211 | 0 | if(padbyte) |
212 | 0 | *output++ = padbyte; |
213 | 0 | } |
214 | 0 | } |
215 | | |
216 | | /* Zero terminate */ |
217 | 0 | *output = '\0'; |
218 | | |
219 | | /* Return the pointer to the new data (allocated memory) */ |
220 | 0 | *outptr = base64data; |
221 | | |
222 | | /* Return the length of the new data */ |
223 | 0 | *outlen = (size_t)(output - base64data); |
224 | |
|
225 | 0 | return CURLE_OK; |
226 | 0 | } |
227 | | |
228 | | /* |
229 | | * curlx_base64_encode() |
230 | | * |
231 | | * Given a pointer to an input buffer and an input size, encode it and |
232 | | * return a pointer in *outptr to a newly allocated memory area holding |
233 | | * encoded data. Size of encoded data is returned in variable pointed by |
234 | | * outlen. |
235 | | * |
236 | | * Returns CURLE_OK on success, otherwise specific error code. Function |
237 | | * output shall not be considered valid unless CURLE_OK is returned. |
238 | | * |
239 | | * @unittest: 1302 |
240 | | */ |
241 | | CURLcode curlx_base64_encode(const uint8_t *inputbuff, size_t insize, |
242 | | char **outptr, size_t *outlen) |
243 | 0 | { |
244 | 0 | return base64_encode(curlx_base64encdec, '=', |
245 | 0 | inputbuff, insize, outptr, outlen); |
246 | 0 | } |
247 | | |
248 | | /* |
249 | | * curlx_base64url_encode() |
250 | | * |
251 | | * Given a pointer to an input buffer and an input size, encode it and |
252 | | * return a pointer in *outptr to a newly allocated memory area holding |
253 | | * encoded data. Size of encoded data is returned in variable pointed by |
254 | | * outlen. |
255 | | * |
256 | | * Input length of 0 indicates input buffer holds a null-terminated string. |
257 | | * |
258 | | * Returns CURLE_OK on success, otherwise specific error code. Function |
259 | | * output shall not be considered valid unless CURLE_OK is returned. |
260 | | * |
261 | | * @unittest: 1302 |
262 | | */ |
263 | | CURLcode curlx_base64url_encode(const uint8_t *inputbuff, size_t insize, |
264 | | char **outptr, size_t *outlen) |
265 | 0 | { |
266 | 0 | return base64_encode(base64url, 0, inputbuff, insize, outptr, outlen); |
267 | 0 | } |