1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2016, 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.haxx.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"
26 #include "urldata.h" /* for the Curl_easy definition */
28 #include "curl_base64.h"
29 #include "non-ascii.h"
31 /* The last 3 #include files should be in this order */
32 #include "curl_printf.h"
33 #include "curl_memory.h"
36 /* ---- Base64 Encoding/Decoding Table --- */
37 static const char base64[]=
38 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
40 /* The Base 64 encoding with an URL and filename safe alphabet, RFC 4648
42 static const char base64url[]=
43 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
45 static size_t decodeQuantum(unsigned char *dest, const char *src)
49 unsigned long i, x = 0;
51 for(i = 0, s = src; i < 4; i++, s++) {
61 while(*p && (*p != *s)) {
74 dest[2] = curlx_ultouc(x & 0xFFUL);
78 dest[1] = curlx_ultouc(x & 0xFFUL);
81 dest[0] = curlx_ultouc(x & 0xFFUL);
87 * Curl_base64_decode()
89 * Given a base64 NUL-terminated string at src, decode it and return a
90 * pointer in *outptr to a newly allocated memory area holding decoded
91 * data. Size of decoded data is returned in variable pointed by outlen.
93 * Returns CURLE_OK on success, otherwise specific error code. Function
94 * output shall not be considered valid unless CURLE_OK is returned.
96 * When decoded data length is 0, returns NULL in *outptr.
100 CURLcode Curl_base64_decode(const char *src,
101 unsigned char **outptr, size_t *outlen)
110 unsigned char *newstr;
114 srclen = strlen(src);
116 /* Check the length of the input string is valid */
117 if(!srclen || srclen % 4)
118 return CURLE_BAD_CONTENT_ENCODING;
120 /* Find the position of any = padding characters */
121 while((src[length] != '=') && src[length])
124 /* A maximum of two = padding characters is allowed */
125 if(src[length] == '=') {
127 if(src[length + 1] == '=')
131 /* Check the = padding characters weren't part way through the input */
132 if(length + padding != srclen)
133 return CURLE_BAD_CONTENT_ENCODING;
135 /* Calculate the number of quantums */
136 numQuantums = srclen / 4;
138 /* Calculate the size of the decoded string */
139 rawlen = (numQuantums * 3) - padding;
141 /* Allocate our buffer including room for a zero terminator */
142 newstr = malloc(rawlen + 1);
144 return CURLE_OUT_OF_MEMORY;
148 /* Decode the quantums */
149 for(i = 0; i < numQuantums; i++) {
150 size_t result = decodeQuantum(pos, src);
154 return CURLE_BAD_CONTENT_ENCODING;
164 /* Return the decoded data */
171 static CURLcode base64_encode(const char *table64,
172 struct Curl_easy *data,
173 const char *inputbuff, size_t insize,
174 char **outptr, size_t *outlen)
177 unsigned char ibuf[3];
178 unsigned char obuf[4];
183 char *convbuf = NULL;
185 const char *indata = inputbuff;
191 insize = strlen(indata);
193 #if SIZEOF_SIZE_T == 4
194 if(insize > UINT_MAX/4)
195 return CURLE_OUT_OF_MEMORY;
198 base64data = output = malloc(insize * 4 / 3 + 4);
200 return CURLE_OUT_OF_MEMORY;
203 * The base64 data needs to be created using the network encoding
204 * not the host encoding. And we can't change the actual input
205 * so we copy it to a buffer, translate it, and use that instead.
207 result = Curl_convert_clone(data, indata, insize, &convbuf);
214 indata = (char *)convbuf;
217 for(i = inputparts = 0; i < 3; i++) {
220 ibuf[i] = (unsigned char) *indata;
228 obuf[0] = (unsigned char) ((ibuf[0] & 0xFC) >> 2);
229 obuf[1] = (unsigned char) (((ibuf[0] & 0x03) << 4) | \
230 ((ibuf[1] & 0xF0) >> 4));
231 obuf[2] = (unsigned char) (((ibuf[1] & 0x0F) << 2) | \
232 ((ibuf[2] & 0xC0) >> 6));
233 obuf[3] = (unsigned char) (ibuf[2] & 0x3F);
236 case 1: /* only one byte read */
237 snprintf(output, 5, "%c%c==",
242 case 2: /* two bytes read */
243 snprintf(output, 5, "%c%c%c=",
250 snprintf(output, 5, "%c%c%c%c",
263 /* Return the pointer to the new data (allocated memory) */
264 *outptr = base64data;
268 /* Return the length of the new data */
269 *outlen = strlen(base64data);
275 * Curl_base64_encode()
277 * Given a pointer to an input buffer and an input size, encode it and
278 * return a pointer in *outptr to a newly allocated memory area holding
279 * encoded data. Size of encoded data is returned in variable pointed by
282 * Input length of 0 indicates input buffer holds a NUL-terminated string.
284 * Returns CURLE_OK on success, otherwise specific error code. Function
285 * output shall not be considered valid unless CURLE_OK is returned.
287 * When encoded data length is 0, returns NULL in *outptr.
291 CURLcode Curl_base64_encode(struct Curl_easy *data,
292 const char *inputbuff, size_t insize,
293 char **outptr, size_t *outlen)
295 return base64_encode(base64, data, inputbuff, insize, outptr, outlen);
299 * Curl_base64url_encode()
301 * Given a pointer to an input buffer and an input size, encode it and
302 * return a pointer in *outptr to a newly allocated memory area holding
303 * encoded data. Size of encoded data is returned in variable pointed by
306 * Input length of 0 indicates input buffer holds a NUL-terminated string.
308 * Returns CURLE_OK on success, otherwise specific error code. Function
309 * output shall not be considered valid unless CURLE_OK is returned.
311 * When encoded data length is 0, returns NULL in *outptr.
315 CURLcode Curl_base64url_encode(struct Curl_easy *data,
316 const char *inputbuff, size_t insize,
317 char **outptr, size_t *outlen)
319 return base64_encode(base64url, data, inputbuff, insize, outptr, outlen);