1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 /* Base64 encoding/decoding */
25 #include "curl_setup.h"
27 #if !defined(CURL_DISABLE_HTTP_AUTH) || defined(USE_SSH) || \
28 !defined(CURL_DISABLE_LDAP) || \
29 !defined(CURL_DISABLE_SMTP) || \
30 !defined(CURL_DISABLE_POP3) || \
31 !defined(CURL_DISABLE_IMAP) || \
32 !defined(CURL_DISABLE_DOH) || defined(USE_SSL)
34 #include "urldata.h" /* for the Curl_easy definition */
36 #include "curl_base64.h"
38 /* The last 3 #include files should be in this order */
39 #include "curl_printf.h"
40 #include "curl_memory.h"
43 /* ---- Base64 Encoding/Decoding Table --- */
44 static const char base64[]=
45 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
47 /* The Base 64 encoding with an URL and filename safe alphabet, RFC 4648
49 static const char base64url[]=
50 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
52 static size_t decodeQuantum(unsigned char *dest, const char *src)
56 unsigned long i, x = 0;
58 for(i = 0, s = src; i < 4; i++, s++) {
67 while(*p && (*p != *s)) {
80 dest[2] = curlx_ultouc(x & 0xFFUL);
84 dest[1] = curlx_ultouc(x & 0xFFUL);
87 dest[0] = curlx_ultouc(x & 0xFFUL);
93 * Curl_base64_decode()
95 * Given a base64 NUL-terminated string at src, decode it and return a
96 * pointer in *outptr to a newly allocated memory area holding decoded
97 * data. Size of decoded data is returned in variable pointed by outlen.
99 * Returns CURLE_OK on success, otherwise specific error code. Function
100 * output shall not be considered valid unless CURLE_OK is returned.
102 * When decoded data length is 0, returns NULL in *outptr.
106 CURLcode Curl_base64_decode(const char *src,
107 unsigned char **outptr, size_t *outlen)
116 unsigned char *newstr;
120 srclen = strlen(src);
122 /* Check the length of the input string is valid */
123 if(!srclen || srclen % 4)
124 return CURLE_BAD_CONTENT_ENCODING;
126 /* Find the position of any = padding characters */
127 while((src[length] != '=') && src[length])
130 /* A maximum of two = padding characters is allowed */
131 if(src[length] == '=') {
133 if(src[length + 1] == '=')
137 /* Check the = padding characters weren't part way through the input */
138 if(length + padding != srclen)
139 return CURLE_BAD_CONTENT_ENCODING;
141 /* Calculate the number of quantums */
142 numQuantums = srclen / 4;
144 /* Calculate the size of the decoded string */
145 rawlen = (numQuantums * 3) - padding;
147 /* Allocate our buffer including room for a zero terminator */
148 newstr = malloc(rawlen + 1);
150 return CURLE_OUT_OF_MEMORY;
154 /* Decode the quantums */
155 for(i = 0; i < numQuantums; i++) {
156 size_t result = decodeQuantum(pos, src);
160 return CURLE_BAD_CONTENT_ENCODING;
170 /* Return the decoded data */
177 static CURLcode base64_encode(const char *table64,
178 const char *inputbuff, size_t insize,
179 char **outptr, size_t *outlen)
181 unsigned char ibuf[3];
182 unsigned char obuf[4];
187 const char *indata = inputbuff;
193 insize = strlen(indata);
195 #if SIZEOF_SIZE_T == 4
196 if(insize > UINT_MAX/4)
197 return CURLE_OUT_OF_MEMORY;
200 base64data = output = malloc(insize * 4 / 3 + 4);
202 return CURLE_OUT_OF_MEMORY;
205 for(i = inputparts = 0; i < 3; i++) {
208 ibuf[i] = (unsigned char) *indata;
216 obuf[0] = (unsigned char) ((ibuf[0] & 0xFC) >> 2);
217 obuf[1] = (unsigned char) (((ibuf[0] & 0x03) << 4) | \
218 ((ibuf[1] & 0xF0) >> 4));
219 obuf[2] = (unsigned char) (((ibuf[1] & 0x0F) << 2) | \
220 ((ibuf[2] & 0xC0) >> 6));
221 obuf[3] = (unsigned char) (ibuf[2] & 0x3F);
224 case 1: /* only one byte read */
225 msnprintf(output, 5, "%c%c==",
230 case 2: /* two bytes read */
231 msnprintf(output, 5, "%c%c%c=",
238 msnprintf(output, 5, "%c%c%c%c",
251 /* Return the pointer to the new data (allocated memory) */
252 *outptr = base64data;
254 /* Return the length of the new data */
255 *outlen = output - base64data;
261 * Curl_base64_encode()
263 * Given a pointer to an input buffer and an input size, encode it and
264 * return a pointer in *outptr to a newly allocated memory area holding
265 * encoded data. Size of encoded data is returned in variable pointed by
268 * Input length of 0 indicates input buffer holds a NUL-terminated string.
270 * Returns CURLE_OK on success, otherwise specific error code. Function
271 * output shall not be considered valid unless CURLE_OK is returned.
273 * When encoded data length is 0, returns NULL in *outptr.
277 CURLcode Curl_base64_encode(const char *inputbuff, size_t insize,
278 char **outptr, size_t *outlen)
280 return base64_encode(base64, inputbuff, insize, outptr, outlen);
284 * Curl_base64url_encode()
286 * Given a pointer to an input buffer and an input size, encode it and
287 * return a pointer in *outptr to a newly allocated memory area holding
288 * encoded data. Size of encoded data is returned in variable pointed by
291 * Input length of 0 indicates input buffer holds a NUL-terminated string.
293 * Returns CURLE_OK on success, otherwise specific error code. Function
294 * output shall not be considered valid unless CURLE_OK is returned.
296 * When encoded data length is 0, returns NULL in *outptr.
300 CURLcode Curl_base64url_encode(const char *inputbuff, size_t insize,
301 char **outptr, size_t *outlen)
303 return base64_encode(base64url, inputbuff, insize, outptr, outlen);
306 #endif /* no users so disabled */