5
5
* | (__| |_| | _ <| |___
6
6
* \___|\___/|_| \_\_____|
8
* Copyright (C) 1998 - 2007, Daniel Stenberg, <daniel@haxx.se>, et al.
8
* Copyright (C) 1998 - 2008, Daniel Stenberg, <daniel@haxx.se>, et al.
10
10
* This software is licensed as described in the file COPYING, which
11
11
* you should have received as part of this distribution. The terms
18
18
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
19
* KIND, either express or implied.
21
* $Id: base64.c,v 1.45 2007-11-05 09:45:09 bagder Exp $
21
* $Id: base64.c,v 1.51 2008-11-14 16:22:18 bagder Exp $
22
22
***************************************************************************/
24
24
/* Base64 encoding/decoding
239
239
return strlen(base64data); /* return the length of the new data */
241
241
/* ---- End of Base64 Encoding ---- */
243
/************* TEST HARNESS STUFF ****************/
247
/* encoding test harness. Read in standard input and write out the length
248
* returned by Curl_base64_encode, followed by the base64'd data itself
252
#define TEST_NEED_SUCK
255
int main(int argc, argv_item_t argv[], char **envp)
261
struct SessionHandle *handle = NULL;
263
#ifdef CURL_DOES_CONVERSIONS
264
/* get a Curl handle so Curl_base64_encode can translate properly */
265
handle = curl_easy_init();
267
fprintf(stderr, "Error: curl_easy_init failed\n");
271
data = (unsigned char *)suck(&dataLen);
272
base64Len = Curl_base64_encode(handle, data, dataLen, &base64);
274
fprintf(stderr, "%d\n", base64Len);
275
fprintf(stdout, "%s\n", base64);
277
free(base64); free(data);
278
#ifdef CURL_DOES_CONVERSIONS
279
curl_easy_cleanup(handle);
286
/* decoding test harness. Read in a base64 string from stdin and write out the
287
* length returned by Curl_base64_decode, followed by the decoded data itself
289
* gcc -DTEST_DECODE base64.c -o base64 mprintf.o memdebug.o
293
#define TEST_NEED_SUCK
296
int main(int argc, argv_item_t argv[], char **envp)
303
#ifdef CURL_DOES_CONVERSIONS
304
/* get a Curl handle so main can translate properly */
305
struct SessionHandle *handle = curl_easy_init();
307
fprintf(stderr, "Error: curl_easy_init failed\n");
312
base64 = (char *)suck(&base64Len);
313
dataLen = Curl_base64_decode(base64, &data);
315
fprintf(stderr, "%d\n", dataLen);
317
for(i=0; i < dataLen; i+=0x10) {
318
printf("0x%02x: ", i);
319
for(j=0; j < 0x10; j++)
321
printf("%02x ", data[i+j]);
327
for(j=0; j < 0x10; j++)
328
if((j+i) < dataLen) {
329
#ifdef CURL_DOES_CONVERSIONS
331
Curl_convert_from_network(handle, &data[i+j], (size_t)1))
333
#endif /* CURL_DOES_CONVERSIONS */
334
printf("%c", ISGRAPH(data[i+j])?data[i+j]:'.');
340
#ifdef CURL_DOES_CONVERSIONS
341
curl_easy_cleanup(handle);
343
free(base64); free(data);
348
#ifdef TEST_NEED_SUCK
349
/* this function 'sucks' in as much as possible from stdin */
350
void *suck(int *lenptr)
353
unsigned char *buf = NULL;
359
buf = (unsigned char *)realloc(buf, cursize);
360
memset(buf + len, 0, cursize - len);
361
lastread = fread(buf + len, 1, cursize - len, stdin);
363
} while(!feof(stdin));