1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2013, 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 table64[]=
41 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
43 static size_t decodeQuantum(unsigned char *dest, const char *src)
47 unsigned long i, v, x = 0;
49 for(i = 0, s = src; i < 4; i++, s++) {
59 while(*p && (*p != *s)) {
72 dest[2] = curlx_ultouc(x & 0xFFUL);
76 dest[1] = curlx_ultouc(x & 0xFFUL);
79 dest[0] = curlx_ultouc(x & 0xFFUL);
85 * Curl_base64_decode()
87 * Given a base64 NUL-terminated string at src, decode it and return a
88 * pointer in *outptr to a newly allocated memory area holding decoded
89 * data. Size of decoded data is returned in variable pointed by outlen.
91 * Returns CURLE_OK on success, otherwise specific error code. Function
92 * output shall not be considered valid unless CURLE_OK is returned.
94 * When decoded data length is 0, returns NULL in *outptr.
98 CURLcode Curl_base64_decode(const char *src,
99 unsigned char **outptr, size_t *outlen)
109 unsigned char *newstr;
113 srclen = strlen(src);
115 /* Check the length of the input string is valid */
116 if(!srclen || srclen % 4)
117 return CURLE_BAD_CONTENT_ENCODING;
119 /* Find the position of any = padding characters */
120 while((src[length] != '=') && src[length])
123 /* A maximum of two = padding characters is allowed */
124 if(src[length] == '=') {
126 if(src[length + 1] == '=')
130 /* Check the = padding characters weren't part way through the input */
131 if(length + padding != srclen)
132 return CURLE_BAD_CONTENT_ENCODING;
134 /* Calculate the number of quantums */
135 numQuantums = srclen / 4;
137 /* Calculate the size of the decoded string */
138 rawlen = (numQuantums * 3) - padding;
140 /* Allocate our buffer including room for a zero terminator */
141 newstr = malloc(rawlen + 1);
143 return CURLE_OUT_OF_MEMORY;
147 /* Decode the quantums */
148 for(i = 0; i < numQuantums; i++) {
149 result = decodeQuantum(pos, src);
151 Curl_safefree(newstr);
153 return CURLE_BAD_CONTENT_ENCODING;
163 /* Return the decoded data */
171 * Curl_base64_encode()
173 * Given a pointer to an input buffer and an input size, encode it and
174 * return a pointer in *outptr to a newly allocated memory area holding
175 * encoded data. Size of encoded data is returned in variable pointed by
178 * Input length of 0 indicates input buffer holds a NUL-terminated string.
180 * Returns CURLE_OK on success, otherwise specific error code. Function
181 * output shall not be considered valid unless CURLE_OK is returned.
183 * When encoded data length is 0, returns NULL in *outptr.
187 CURLcode Curl_base64_encode(struct SessionHandle *data,
188 const char *inputbuff, size_t insize,
189 char **outptr, size_t *outlen)
192 unsigned char ibuf[3];
193 unsigned char obuf[4];
198 char *convbuf = NULL;
200 const char *indata = inputbuff;
206 insize = strlen(indata);
208 base64data = output = malloc(insize*4/3+4);
210 return CURLE_OUT_OF_MEMORY;
213 * The base64 data needs to be created using the network encoding
214 * not the host encoding. And we can't change the actual input
215 * so we copy it to a buffer, translate it, and use that instead.
217 error = Curl_convert_clone(data, indata, insize, &convbuf);
224 indata = (char *)convbuf;
227 for(i = inputparts = 0; i < 3; i++) {
230 ibuf[i] = (unsigned char) *indata;
238 obuf[0] = (unsigned char) ((ibuf[0] & 0xFC) >> 2);
239 obuf[1] = (unsigned char) (((ibuf[0] & 0x03) << 4) | \
240 ((ibuf[1] & 0xF0) >> 4));
241 obuf[2] = (unsigned char) (((ibuf[1] & 0x0F) << 2) | \
242 ((ibuf[2] & 0xC0) >> 6));
243 obuf[3] = (unsigned char) (ibuf[2] & 0x3F);
246 case 1: /* only one byte read */
247 snprintf(output, 5, "%c%c==",
251 case 2: /* two bytes read */
252 snprintf(output, 5, "%c%c%c=",
258 snprintf(output, 5, "%c%c%c%c",
268 *outptr = base64data; /* return pointer to new data, allocated memory */
273 *outlen = strlen(base64data); /* return the length of the new data */
277 /* ---- End of Base64 Encoding ---- */