1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2011, 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 */
30 #define _MPRINTF_REPLACE /* use our functions only */
31 #include <curl/mprintf.h>
33 #include "urldata.h" /* for the SessionHandle definition */
35 #include "curl_base64.h"
36 #include "curl_memory.h"
37 #include "non-ascii.h"
39 /* include memdebug.h last */
42 /* ---- Base64 Encoding/Decoding Table --- */
43 static const char table64[]=
44 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
46 static void decodeQuantum(unsigned char *dest, const char *src)
49 unsigned long i, v, x = 0;
51 for(i = 0, s = src; i < 4; i++, s++) {
54 while(*p && (*p != *s)) {
64 dest[2] = curlx_ultouc(x);
66 dest[1] = curlx_ultouc(x);
68 dest[0] = curlx_ultouc(x);
72 * Curl_base64_decode()
74 * Given a base64 string at src, decode it and return an allocated memory in
75 * the *outptr. Returns the length of the decoded data.
77 size_t Curl_base64_decode(const char *src, unsigned char **outptr)
80 size_t equalsTerm = 0;
83 unsigned char lastQuantum[3];
85 unsigned char *newstr;
89 while((src[length] != '=') && src[length])
91 /* A maximum of two = padding characters is allowed */
92 if(src[length] == '=') {
94 if(src[length+equalsTerm] == '=')
97 numQuantums = (length + equalsTerm) / 4;
99 /* Don't allocate a buffer if the decoded length is 0 */
103 rawlen = (numQuantums * 3) - equalsTerm;
105 /* The buffer must be large enough to make room for the last quantum
106 (which may be partially thrown out) and the zero terminator. */
107 newstr = malloc(rawlen+4);
113 /* Decode all but the last quantum (which may not decode to a
114 multiple of 3 bytes) */
115 for(i = 0; i < numQuantums - 1; i++) {
116 decodeQuantum(newstr, src);
117 newstr += 3; src += 4;
120 /* This final decode may actually read slightly past the end of the buffer
121 if the input string is missing pad bytes. This will almost always be
123 decodeQuantum(lastQuantum, src);
124 for(i = 0; i < 3 - equalsTerm; i++)
125 newstr[i] = lastQuantum[i];
127 newstr[i] = '\0'; /* zero terminate */
132 * Curl_base64_encode()
134 * Returns the length of the newly created base64 string. The third argument
135 * is a pointer to an allocated area holding the base64 data. If something
136 * went wrong, 0 is returned.
139 size_t Curl_base64_encode(struct SessionHandle *data,
140 const char *inputbuff, size_t insize,
144 unsigned char ibuf[3];
145 unsigned char obuf[4];
150 char *convbuf = NULL;
152 const char *indata = inputbuff;
154 *outptr = NULL; /* set to NULL in case of failure before we reach the end */
157 insize = strlen(indata);
159 base64data = output = malloc(insize*4/3+4);
164 * The base64 data needs to be created using the network encoding
165 * not the host encoding. And we can't change the actual input
166 * so we copy it to a buffer, translate it, and use that instead.
168 res = Curl_convert_clone(data, indata, insize, &convbuf);
175 indata = (char *)convbuf;
178 for(i = inputparts = 0; i < 3; i++) {
181 ibuf[i] = (unsigned char) *indata;
189 obuf[0] = (unsigned char) ((ibuf[0] & 0xFC) >> 2);
190 obuf[1] = (unsigned char) (((ibuf[0] & 0x03) << 4) | \
191 ((ibuf[1] & 0xF0) >> 4));
192 obuf[2] = (unsigned char) (((ibuf[1] & 0x0F) << 2) | \
193 ((ibuf[2] & 0xC0) >> 6));
194 obuf[3] = (unsigned char) (ibuf[2] & 0x3F);
197 case 1: /* only one byte read */
198 snprintf(output, 5, "%c%c==",
202 case 2: /* two bytes read */
203 snprintf(output, 5, "%c%c%c=",
209 snprintf(output, 5, "%c%c%c%c",
219 *outptr = base64data; /* make it return the actual data memory */
224 return strlen(base64data); /* return the length of the new data */
226 /* ---- End of Base64 Encoding ---- */