1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2014, 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 http://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"
27 #define _MPRINTF_REPLACE /* use our functions only */
28 #include <curl/mprintf.h>
30 #include "urldata.h" /* for the SessionHandle definition */
32 #include "curl_base64.h"
33 #include "curl_memory.h"
34 #include "non-ascii.h"
36 /* include memdebug.h last */
39 /* ---- Base64 Encoding/Decoding Table --- */
40 static const char base64[]=
41 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
43 /* The Base 64 encoding with an URL and filename safe alphabet, RFC 4648
45 static const char base64url[]=
46 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
48 static size_t decodeQuantum(unsigned char *dest, const char *src)
52 unsigned long i, x = 0;
54 for(i = 0, s = src; i < 4; i++, s++) {
64 while(*p && (*p != *s)) {
77 dest[2] = curlx_ultouc(x & 0xFFUL);
81 dest[1] = curlx_ultouc(x & 0xFFUL);
84 dest[0] = curlx_ultouc(x & 0xFFUL);
90 * Curl_base64_decode()
92 * Given a base64 NUL-terminated string at src, decode it and return a
93 * pointer in *outptr to a newly allocated memory area holding decoded
94 * data. Size of decoded data is returned in variable pointed by outlen.
96 * Returns CURLE_OK on success, otherwise specific error code. Function
97 * output shall not be considered valid unless CURLE_OK is returned.
99 * When decoded data length is 0, returns NULL in *outptr.
103 CURLcode Curl_base64_decode(const char *src,
104 unsigned char **outptr, size_t *outlen)
113 unsigned char *newstr;
117 srclen = strlen(src);
119 /* Check the length of the input string is valid */
120 if(!srclen || srclen % 4)
121 return CURLE_BAD_CONTENT_ENCODING;
123 /* Find the position of any = padding characters */
124 while((src[length] != '=') && src[length])
127 /* A maximum of two = padding characters is allowed */
128 if(src[length] == '=') {
130 if(src[length + 1] == '=')
134 /* Check the = padding characters weren't part way through the input */
135 if(length + padding != srclen)
136 return CURLE_BAD_CONTENT_ENCODING;
138 /* Calculate the number of quantums */
139 numQuantums = srclen / 4;
141 /* Calculate the size of the decoded string */
142 rawlen = (numQuantums * 3) - padding;
144 /* Allocate our buffer including room for a zero terminator */
145 newstr = malloc(rawlen + 1);
147 return CURLE_OUT_OF_MEMORY;
151 /* Decode the quantums */
152 for(i = 0; i < numQuantums; i++) {
153 size_t result = decodeQuantum(pos, src);
155 Curl_safefree(newstr);
157 return CURLE_BAD_CONTENT_ENCODING;
167 /* Return the decoded data */
174 static CURLcode base64_encode(const char *table64,
175 struct SessionHandle *data,
176 const char *inputbuff, size_t insize,
177 char **outptr, size_t *outlen)
180 unsigned char ibuf[3];
181 unsigned char obuf[4];
186 char *convbuf = NULL;
188 const char *indata = inputbuff;
194 insize = strlen(indata);
196 base64data = output = malloc(insize*4/3+4);
198 return CURLE_OUT_OF_MEMORY;
201 * The base64 data needs to be created using the network encoding
202 * not the host encoding. And we can't change the actual input
203 * so we copy it to a buffer, translate it, and use that instead.
205 error = Curl_convert_clone(data, indata, insize, &convbuf);
212 indata = (char *)convbuf;
215 for(i = inputparts = 0; i < 3; i++) {
218 ibuf[i] = (unsigned char) *indata;
226 obuf[0] = (unsigned char) ((ibuf[0] & 0xFC) >> 2);
227 obuf[1] = (unsigned char) (((ibuf[0] & 0x03) << 4) | \
228 ((ibuf[1] & 0xF0) >> 4));
229 obuf[2] = (unsigned char) (((ibuf[1] & 0x0F) << 2) | \
230 ((ibuf[2] & 0xC0) >> 6));
231 obuf[3] = (unsigned char) (ibuf[2] & 0x3F);
234 case 1: /* only one byte read */
235 snprintf(output, 5, "%c%c==",
239 case 2: /* two bytes read */
240 snprintf(output, 5, "%c%c%c=",
246 snprintf(output, 5, "%c%c%c%c",
256 *outptr = base64data; /* return pointer to new data, allocated memory */
261 *outlen = strlen(base64data); /* return the length of the new data */
267 * Curl_base64_encode()
269 * Given a pointer to an input buffer and an input size, encode it and
270 * return a pointer in *outptr to a newly allocated memory area holding
271 * encoded data. Size of encoded data is returned in variable pointed by
274 * Input length of 0 indicates input buffer holds a NUL-terminated string.
276 * Returns CURLE_OK on success, otherwise specific error code. Function
277 * output shall not be considered valid unless CURLE_OK is returned.
279 * When encoded data length is 0, returns NULL in *outptr.
283 CURLcode Curl_base64_encode(struct SessionHandle *data,
284 const char *inputbuff, size_t insize,
285 char **outptr, size_t *outlen)
287 return base64_encode(base64, data, inputbuff, insize, outptr, outlen);
291 * Curl_base64url_encode()
293 * Given a pointer to an input buffer and an input size, encode it and
294 * return a pointer in *outptr to a newly allocated memory area holding
295 * encoded data. Size of encoded data is returned in variable pointed by
298 * Input length of 0 indicates input buffer holds a NUL-terminated string.
300 * Returns CURLE_OK on success, otherwise specific error code. Function
301 * output shall not be considered valid unless CURLE_OK is returned.
303 * When encoded data length is 0, returns NULL in *outptr.
307 CURLcode Curl_base64url_encode(struct SessionHandle *data,
308 const char *inputbuff, size_t insize,
309 char **outptr, size_t *outlen)
311 return base64_encode(base64url, data, inputbuff, insize, outptr, outlen);
313 /* ---- End of Base64 Encoding ---- */