oops, the decode() function got its arguments reversed in my cleanup
[platform/upstream/curl.git] / lib / base64.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2004, Daniel Stenberg, <daniel@haxx.se>, et al.
9  *
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.
13  *
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.
17  *
18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19  * KIND, either express or implied.
20  *
21  * $Id$
22  ***************************************************************************/
23
24 /* Base64 encoding/decoding
25  *
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.
31  *
32  * This code will break if int is smaller than 32 bits
33  */
34
35 #include "setup.h"
36
37 #include <stdlib.h>
38 #include <string.h>
39
40 #define _MPRINTF_REPLACE /* use our functions only */
41 #include <curl/mprintf.h>
42
43 #include "base64.h"
44
45 #ifdef CURLDEBUG
46 #include "memdebug.h"
47 #endif
48
49 static void decodeQuantum(unsigned char *dest, const char *src)
50 {
51   unsigned int x = 0;
52   int i;
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] == '+')
61       x = (x << 6) + 62;
62     else if(src[i] == '/')
63       x = (x << 6) + 63;
64     else if(src[i] == '=')
65       x = (x << 6);
66   }
67
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;
71 }
72
73 /*
74  * Curl_base64_decode()
75  *
76  * Given a base64 string at src, decode it into the memory pointed to by
77  * dest. Returns the length of the decoded data.
78  */
79 size_t Curl_base64_decode(const char *src, char *dest)
80 {
81   int length = 0;
82   int equalsTerm = 0;
83   int i;
84   int numQuantums;
85   unsigned char lastQuantum[3];
86   size_t rawlen=0;
87
88   while((src[length] != '=') && src[length])
89     length++;
90   while(src[length+equalsTerm] == '=')
91     equalsTerm++;
92
93   numQuantums = (length + equalsTerm) / 4;
94
95   rawlen = (numQuantums * 3) - equalsTerm;
96
97   for(i = 0; i < numQuantums - 1; i++) {
98     decodeQuantum((unsigned char *)dest, src);
99     dest += 3; src += 4;
100   }
101
102   decodeQuantum(lastQuantum, src);
103   for(i = 0; i < 3 - equalsTerm; i++)
104     dest[i] = lastQuantum[i];
105
106   return rawlen;
107 }
108
109 /* ---- Base64 Encoding --- */
110 static char table64[]=
111   "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
112
113 /*
114  * Curl_base64_encode()
115  *
116  * Returns the length of the newly created base64 string. The third argument
117  * is a pointer to an allocated area holding the base64 data. If something
118  * went wrong, -1 is returned.
119  *
120  */
121 size_t Curl_base64_encode(const char *inp, size_t insize, char **outptr)
122 {
123   unsigned char ibuf[3];
124   unsigned char obuf[4];
125   int i;
126   int inputparts;
127   char *output;
128   char *base64data;
129
130   char *indata = (char *)inp;
131
132   if(0 == insize)
133     insize = strlen(indata);
134
135   base64data = output = (char*)malloc(insize*4/3+4);
136   if(NULL == output)
137     return 0;
138
139   while(insize > 0) {
140     for (i = inputparts = 0; i < 3; i++) {
141       if(insize > 0) {
142         inputparts++;
143         ibuf[i] = *indata;
144         indata++;
145         insize--;
146       }
147       else
148         ibuf[i] = 0;
149     }
150
151     obuf [0] = (ibuf [0] & 0xFC) >> 2;
152     obuf [1] = ((ibuf [0] & 0x03) << 4) | ((ibuf [1] & 0xF0) >> 4);
153     obuf [2] = ((ibuf [1] & 0x0F) << 2) | ((ibuf [2] & 0xC0) >> 6);
154     obuf [3] = ibuf [2] & 0x3F;
155
156     switch(inputparts) {
157     case 1: /* only one byte read */
158       sprintf(output, "%c%c==",
159               table64[obuf[0]],
160               table64[obuf[1]]);
161       break;
162     case 2: /* two bytes read */
163       sprintf(output, "%c%c%c=",
164               table64[obuf[0]],
165               table64[obuf[1]],
166               table64[obuf[2]]);
167       break;
168     default:
169       sprintf(output, "%c%c%c%c",
170               table64[obuf[0]],
171               table64[obuf[1]],
172               table64[obuf[2]],
173               table64[obuf[3]] );
174       break;
175     }
176     output += 4;
177   }
178   *output=0;
179   *outptr = base64data; /* make it return the actual data memory */
180
181   return strlen(base64data); /* return the length of the new data */
182 }
183 /* ---- End of Base64 Encoding ---- */
184
185 /************* TEST HARNESS STUFF ****************/
186
187
188 #ifdef TEST_ENCODE
189 /* encoding test harness. Read in standard input and write out the length
190  * returned by Curl_base64_encode, followed by the base64'd data itself
191  */
192 #include <stdio.h>
193
194 #define TEST_NEED_SUCK
195 void *suck(int *);
196
197 int main(int argc, char **argv, char **envp)
198 {
199   char *base64;
200   size_t base64Len;
201   unsigned char *data;
202   int dataLen;
203
204   data = (unsigned char *)suck(&dataLen);
205   base64Len = Curl_base64_encode(data, dataLen, &base64);
206
207   fprintf(stderr, "%d\n", base64Len);
208   fprintf(stdout, "%s",   base64);
209
210   free(base64); free(data);
211   return 0;
212 }
213 #endif
214
215 #ifdef TEST_DECODE
216 /* decoding test harness. Read in a base64 string from stdin and write out the
217  * length returned by Curl_base64_decode, followed by the decoded data itself
218  *
219  * gcc -DTEST_DECODE base64.c -o base64 mprintf.o memdebug.o
220  */
221 #include <stdio.h>
222
223 #define TEST_NEED_SUCK
224 void *suck(int *);
225
226 int main(int argc, char **argv, char **envp)
227 {
228   char *base64;
229   int base64Len;
230   unsigned char *data;
231   int dataLen;
232   int i, j;
233
234   base64 = (char *)suck(&base64Len);
235   data = (unsigned char *)malloc(base64Len * 3/4 + 8);
236   dataLen = Curl_base64_decode(base64, data);
237
238   fprintf(stderr, "%d\n", dataLen);
239
240   for(i=0; i < dataLen; i+=0x10) {
241     printf("0x%02x: ", i);
242     for(j=0; j < 0x10; j++)
243       if((j+i) < dataLen)
244         printf("%02x ", data[i+j]);
245       else
246         printf("   ");
247
248     printf(" | ");
249
250     for(j=0; j < 0x10; j++)
251       if((j+i) < dataLen)
252         printf("%c", isgraph(data[i+j])?data[i+j]:'.');
253       else
254         break;
255     puts("");
256   }
257
258   free(base64); free(data);
259   return 0;
260 }
261 #endif
262
263 #ifdef TEST_NEED_SUCK
264 /* this function 'sucks' in as much as possible from stdin */
265 void *suck(int *lenptr)
266 {
267   int cursize = 8192;
268   unsigned char *buf = NULL;
269   int lastread;
270   int len = 0;
271
272   do {
273     cursize *= 2;
274     buf = (unsigned char *)realloc(buf, cursize);
275     memset(buf + len, 0, cursize - len);
276     lastread = fread(buf + len, 1, cursize - len, stdin);
277     len += lastread;
278   } while(!feof(stdin));
279
280   lenptr[0] = len;
281   return (void *)buf;
282 }
283 #endif