1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2003, 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.
22 ***************************************************************************/
24 /* Base64 encoding/decoding
26 * Test harnesses down the bottom - compile with -DTEST_ENCODE for
27 * a program that will read in raw data from stdin and write out
28 * a base64-encoded version to stdout, and the length returned by the
29 * encoding function to stderr. Compile with -DTEST_DECODE for a program that
30 * will go the other way.
32 * This code will break if int is smaller than 32 bits
40 #define _MPRINTF_REPLACE /* use our functions only */
41 #include <curl/mprintf.h>
49 static void decodeQuantum(unsigned char *dest, char *src)
53 for(i = 0; i < 4; i++) {
54 if(src[i] >= 'A' && src[i] <= 'Z')
55 x = (x << 6) + (unsigned int)(src[i] - 'A' + 0);
56 else if(src[i] >= 'a' && src[i] <= 'z')
57 x = (x << 6) + (unsigned int)(src[i] - 'a' + 26);
58 else if(src[i] >= '0' && src[i] <= '9')
59 x = (x << 6) + (unsigned int)(src[i] - '0' + 52);
60 else if(src[i] == '+')
62 else if(src[i] == '/')
64 else if(src[i] == '=')
68 dest[2] = (unsigned char)(x & 255); x >>= 8;
69 dest[1] = (unsigned char)(x & 255); x >>= 8;
70 dest[0] = (unsigned char)(x & 255); x >>= 8;
74 * Given a base64 string at src, decode it into the memory pointed
75 * to by dest. If rawLength points to a valid address (ie not NULL),
76 * store the length of the decoded data to it.
78 static void base64Decode(unsigned char *dest, char *src, int *rawLength)
84 unsigned char lastQuantum[3];
86 while((src[length] != '=') && src[length])
88 while(src[length+equalsTerm] == '=')
91 numQuantums = (length + equalsTerm) / 4;
93 *rawLength = (numQuantums * 3) - equalsTerm;
95 for(i = 0; i < numQuantums - 1; i++) {
96 decodeQuantum(dest, src);
100 decodeQuantum(lastQuantum, src);
101 for(i = 0; i < 3 - equalsTerm; i++)
102 dest[i] = lastQuantum[i];
106 /* ---- Base64 Encoding --- */
107 static char table64[]=
108 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
111 * Curl_base64_encode()
113 * Returns the length of the newly created base64 string. The third argument
114 * is a pointer to an allocated area holding the base64 data. If something
115 * went wrong, -1 is returned.
118 int Curl_base64_encode(const void *inp, int insize, char **outptr)
120 unsigned char ibuf[3];
121 unsigned char obuf[4];
127 char *indata = (char *)inp;
130 insize = strlen(indata);
132 base64data = output = (char*)malloc(insize*4/3+4);
137 for (i = inputparts = 0; i < 3; i++) {
148 obuf [0] = (ibuf [0] & 0xFC) >> 2;
149 obuf [1] = ((ibuf [0] & 0x03) << 4) | ((ibuf [1] & 0xF0) >> 4);
150 obuf [2] = ((ibuf [1] & 0x0F) << 2) | ((ibuf [2] & 0xC0) >> 6);
151 obuf [3] = ibuf [2] & 0x3F;
154 case 1: /* only one byte read */
155 sprintf(output, "%c%c==",
159 case 2: /* two bytes read */
160 sprintf(output, "%c%c%c=",
166 sprintf(output, "%c%c%c%c",
176 *outptr = base64data; /* make it return the actual data memory */
178 return strlen(base64data); /* return the length of the new data */
180 /* ---- End of Base64 Encoding ---- */
182 int Curl_base64_decode(const char *str, void *data)
186 base64Decode((unsigned char *)data, (char *)str, &ret);
190 /************* TEST HARNESS STUFF ****************/
194 /* encoding test harness. Read in standard input and write out the length
195 * returned by Curl_base64_encode, followed by the base64'd data itself
199 #define TEST_NEED_SUCK
202 int main(int argc, char **argv, char **envp)
209 data = (unsigned char *)suck(&dataLen);
210 base64Len = Curl_base64_encode(data, dataLen, &base64);
212 fprintf(stderr, "%d\n", base64Len);
213 fprintf(stdout, "%s", base64);
215 free(base64); free(data);
221 /* decoding test harness. Read in a base64 string from stdin and write out the
222 * length returned by Curl_base64_decode, followed by the decoded data itself
226 #define TEST_NEED_SUCK
229 int main(int argc, char **argv, char **envp)
236 base64 = (char *)suck(&base64Len);
237 data = (unsigned char *)malloc(base64Len * 3/4 + 8);
238 dataLen = Curl_base64_decode(base64, data);
240 fprintf(stderr, "%d\n", dataLen);
241 fwrite(data,1,dataLen,stdout);
243 free(base64); free(data);
248 #ifdef TEST_NEED_SUCK
249 /* this function 'sucks' in as much as possible from stdin */
250 void *suck(int *lenptr)
253 unsigned char *buf = NULL;
259 buf = (unsigned char *)realloc(buf, cursize);
260 memset(buf + len, 0, cursize - len);
261 lastread = fread(buf + len, 1, cursize - len, stdin);
263 } while(!feof(stdin));
272 * eval: (load-file "../curl-mode.el")
275 * vim: et sw=2 ts=2 sts=2 tw=78