1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2009, 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 * Test harnesses down the bottom - compile with -DTEST_ENCODE for
26 * a program that will read in raw data from stdin and write out
27 * a base64-encoded version to stdout, and the length returned by the
28 * encoding function to stderr. Compile with -DTEST_DECODE for a program that
29 * will go the other way.
31 * This code will break if int is smaller than 32 bits
39 #define _MPRINTF_REPLACE /* use our functions only */
40 #include <curl/mprintf.h>
42 #include "urldata.h" /* for the SessionHandle definition */
43 #include "easyif.h" /* for Curl_convert_... prototypes */
45 #include "curl_base64.h"
46 #include "curl_memory.h"
48 /* include memdebug.h last */
51 /* ---- Base64 Encoding/Decoding Table --- */
52 static const char table64[]=
53 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
55 static void decodeQuantum(unsigned char *dest, const char *src)
58 unsigned long i, v, x = 0;
60 for(i = 0, s = src; i < 4; i++, s++) {
63 while(*p && (*p != *s)) {
73 dest[2] = curlx_ultouc(x);
75 dest[1] = curlx_ultouc(x);
77 dest[0] = curlx_ultouc(x);
81 * Curl_base64_decode()
83 * Given a base64 string at src, decode it and return an allocated memory in
84 * the *outptr. Returns the length of the decoded data.
86 size_t Curl_base64_decode(const char *src, unsigned char **outptr)
89 size_t equalsTerm = 0;
92 unsigned char lastQuantum[3];
94 unsigned char *newstr;
98 while((src[length] != '=') && src[length])
100 /* A maximum of two = padding characters is allowed */
101 if(src[length] == '=') {
103 if(src[length+equalsTerm] == '=')
106 numQuantums = (length + equalsTerm) / 4;
108 /* Don't allocate a buffer if the decoded length is 0 */
112 rawlen = (numQuantums * 3) - equalsTerm;
114 /* The buffer must be large enough to make room for the last quantum
115 (which may be partially thrown out) and the zero terminator. */
116 newstr = malloc(rawlen+4);
122 /* Decode all but the last quantum (which may not decode to a
123 multiple of 3 bytes) */
124 for(i = 0; i < numQuantums - 1; i++) {
125 decodeQuantum(newstr, src);
126 newstr += 3; src += 4;
129 /* This final decode may actually read slightly past the end of the buffer
130 if the input string is missing pad bytes. This will almost always be
132 decodeQuantum(lastQuantum, src);
133 for(i = 0; i < 3 - equalsTerm; i++)
134 newstr[i] = lastQuantum[i];
136 newstr[i] = '\0'; /* zero terminate */
141 * Curl_base64_encode()
143 * Returns the length of the newly created base64 string. The third argument
144 * is a pointer to an allocated area holding the base64 data. If something
145 * went wrong, 0 is returned.
148 size_t Curl_base64_encode(struct SessionHandle *data,
149 const char *inputbuff, size_t insize,
152 unsigned char ibuf[3];
153 unsigned char obuf[4];
158 #ifdef CURL_DOES_CONVERSIONS
159 char *convbuf = NULL;
162 const char *indata = inputbuff;
164 *outptr = NULL; /* set to NULL in case of failure before we reach the end */
167 insize = strlen(indata);
169 base64data = output = malloc(insize*4/3+4);
173 #ifdef CURL_DOES_CONVERSIONS
175 * The base64 data needs to be created using the network encoding
176 * not the host encoding. And we can't change the actual input
177 * so we copy it to a buffer, translate it, and use that instead.
180 convbuf = malloc(insize);
185 memcpy(convbuf, indata, insize);
186 if(CURLE_OK != Curl_convert_to_network(data, convbuf, insize)) {
191 indata = convbuf; /* switch to the converted buffer */
198 for (i = inputparts = 0; i < 3; i++) {
201 ibuf[i] = (unsigned char) *indata;
209 obuf[0] = (unsigned char) ((ibuf[0] & 0xFC) >> 2);
210 obuf[1] = (unsigned char) (((ibuf[0] & 0x03) << 4) | \
211 ((ibuf[1] & 0xF0) >> 4));
212 obuf[2] = (unsigned char) (((ibuf[1] & 0x0F) << 2) | \
213 ((ibuf[2] & 0xC0) >> 6));
214 obuf[3] = (unsigned char) (ibuf[2] & 0x3F);
217 case 1: /* only one byte read */
218 snprintf(output, 5, "%c%c==",
222 case 2: /* two bytes read */
223 snprintf(output, 5, "%c%c%c=",
229 snprintf(output, 5, "%c%c%c%c",
239 *outptr = base64data; /* make it return the actual data memory */
241 #ifdef CURL_DOES_CONVERSIONS
245 return strlen(base64data); /* return the length of the new data */
247 /* ---- End of Base64 Encoding ---- */