Git init
[external/curl.git] / lib / base64.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2009, 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  ***************************************************************************/
22
23 /* Base64 encoding/decoding
24  *
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.
30  *
31  * This code will break if int is smaller than 32 bits
32  */
33
34 #include "setup.h"
35
36 #include <stdlib.h>
37 #include <string.h>
38
39 #define _MPRINTF_REPLACE /* use our functions only */
40 #include <curl/mprintf.h>
41
42 #include "urldata.h" /* for the SessionHandle definition */
43 #include "easyif.h"  /* for Curl_convert_... prototypes */
44 #include "warnless.h"
45 #include "curl_base64.h"
46 #include "curl_memory.h"
47
48 /* include memdebug.h last */
49 #include "memdebug.h"
50
51 /* ---- Base64 Encoding/Decoding Table --- */
52 static const char table64[]=
53   "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
54
55 static void decodeQuantum(unsigned char *dest, const char *src)
56 {
57   const char *s, *p;
58   unsigned long i, v, x = 0;
59
60   for(i = 0, s = src; i < 4; i++, s++) {
61     v = 0;
62     p = table64;
63     while(*p && (*p != *s)) {
64       v++;
65       p++;
66     }
67     if(*p == *s)
68       x = (x << 6) + v;
69     else if(*s == '=')
70       x = (x << 6);
71   }
72
73   dest[2] = curlx_ultouc(x);
74   x >>= 8;
75   dest[1] = curlx_ultouc(x);
76   x >>= 8;
77   dest[0] = curlx_ultouc(x);
78 }
79
80 /*
81  * Curl_base64_decode()
82  *
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.
85  */
86 size_t Curl_base64_decode(const char *src, unsigned char **outptr)
87 {
88   size_t length = 0;
89   size_t equalsTerm = 0;
90   size_t i;
91   size_t numQuantums;
92   unsigned char lastQuantum[3];
93   size_t rawlen = 0;
94   unsigned char *newstr;
95
96   *outptr = NULL;
97
98   while((src[length] != '=') && src[length])
99     length++;
100   /* A maximum of two = padding characters is allowed */
101   if(src[length] == '=') {
102     equalsTerm++;
103     if(src[length+equalsTerm] == '=')
104       equalsTerm++;
105   }
106   numQuantums = (length + equalsTerm) / 4;
107
108   /* Don't allocate a buffer if the decoded length is 0 */
109   if(numQuantums == 0)
110     return 0;
111
112   rawlen = (numQuantums * 3) - equalsTerm;
113
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);
117   if(!newstr)
118     return 0;
119
120   *outptr = newstr;
121
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;
127   }
128
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
131   harmless. */
132   decodeQuantum(lastQuantum, src);
133   for(i = 0; i < 3 - equalsTerm; i++)
134     newstr[i] = lastQuantum[i];
135
136   newstr[i] = '\0'; /* zero terminate */
137   return rawlen;
138 }
139
140 /*
141  * Curl_base64_encode()
142  *
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.
146  *
147  */
148 size_t Curl_base64_encode(struct SessionHandle *data,
149                           const char *inputbuff, size_t insize,
150                           char **outptr)
151 {
152   unsigned char ibuf[3];
153   unsigned char obuf[4];
154   int i;
155   int inputparts;
156   char *output;
157   char *base64data;
158 #ifdef CURL_DOES_CONVERSIONS
159   char *convbuf = NULL;
160 #endif
161
162   const char *indata = inputbuff;
163
164   *outptr = NULL; /* set to NULL in case of failure before we reach the end */
165
166   if(0 == insize)
167     insize = strlen(indata);
168
169   base64data = output = malloc(insize*4/3+4);
170   if(NULL == output)
171     return 0;
172
173 #ifdef CURL_DOES_CONVERSIONS
174   /*
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.
178    */
179   if(data) {
180     convbuf = malloc(insize);
181     if(!convbuf) {
182       free(output);
183       return 0;
184     }
185     memcpy(convbuf, indata, insize);
186     if(CURLE_OK != Curl_convert_to_network(data, convbuf, insize)) {
187       free(convbuf);
188       free(output);
189       return 0;
190     }
191     indata = convbuf; /* switch to the converted buffer */
192   }
193 #else
194   (void)data;
195 #endif
196
197   while(insize > 0) {
198     for (i = inputparts = 0; i < 3; i++) {
199       if(insize > 0) {
200         inputparts++;
201         ibuf[i] = (unsigned char) *indata;
202         indata++;
203         insize--;
204       }
205       else
206         ibuf[i] = 0;
207     }
208
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);
215
216     switch(inputparts) {
217     case 1: /* only one byte read */
218       snprintf(output, 5, "%c%c==",
219                table64[obuf[0]],
220                table64[obuf[1]]);
221       break;
222     case 2: /* two bytes read */
223       snprintf(output, 5, "%c%c%c=",
224                table64[obuf[0]],
225                table64[obuf[1]],
226                table64[obuf[2]]);
227       break;
228     default:
229       snprintf(output, 5, "%c%c%c%c",
230                table64[obuf[0]],
231                table64[obuf[1]],
232                table64[obuf[2]],
233                table64[obuf[3]] );
234       break;
235     }
236     output += 4;
237   }
238   *output=0;
239   *outptr = base64data; /* make it return the actual data memory */
240
241 #ifdef CURL_DOES_CONVERSIONS
242   if(data)
243     free(convbuf);
244 #endif
245   return strlen(base64data); /* return the length of the new data */
246 }
247 /* ---- End of Base64 Encoding ---- */