16
|
1
|
|
2 /* Taken from LibStrfunc v7.3 */
|
|
3
|
|
4 #include <stdio.h>
|
|
5 #include <ctype.h>
|
|
6 #include <stdlib.h>
|
|
7 #include "libstrfunc.h"
|
|
8
|
|
9 char *_sf_b64_buf=NULL;
|
|
10 size_t _sf_b64_len=0;
|
|
11
|
|
12
|
|
13 static unsigned char _sf_uc_ib[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/==";
|
|
14
|
|
15 char *
|
|
16 base64_encode(void *data, size_t size) {
|
|
17 char *output;
|
|
18 register char *ou;
|
|
19 register unsigned char *p=(unsigned char *)data;
|
|
20 #ifdef __LINUX__
|
|
21 register void * dte = ((char*)data + size);
|
|
22 #endif
|
|
23
|
|
24 #ifndef __LINUX__
|
|
25 register void * dte = (void*)((char*)data + size);
|
|
26 #endif
|
|
27 //register void *dte=data + size;
|
|
28 register int nc=0;
|
|
29
|
31
|
30 if ( data == NULL || size == 0 )
|
16
|
31 return NULL;
|
|
32
|
|
33 ou=output=(char *)malloc(size / 3 * 4 + (size / 50) + 5);
|
|
34 if(!output)
|
|
35 return NULL;
|
|
36
|
|
37 while((char *)dte - (char *)p >= 3) {
|
|
38 *ou = _sf_uc_ib[ *p >> 2 ];
|
|
39 ou[1] = _sf_uc_ib[ ((*p & 0x03) << 4) | (p[1] >> 4) ];
|
|
40 ou[2] = _sf_uc_ib[ ((p[1] & 0x0F) << 2) | (p[2] >> 6) ];
|
|
41 ou[3] = _sf_uc_ib[ p[2] & 0x3F ];
|
|
42
|
|
43 p+=3;
|
|
44 ou+=4;
|
|
45
|
|
46 nc+=4;
|
|
47 if(!(nc % 76)) *ou++='\n';
|
|
48 };
|
|
49 if((char *)dte - (char *)p == 2) {
|
|
50 *ou++ = _sf_uc_ib[ *p >> 2 ];
|
|
51 *ou++ = _sf_uc_ib[ ((*p & 0x03) << 4) | (p[1] >> 4) ];
|
|
52 *ou++ = _sf_uc_ib[ ((p[1] & 0x0F) << 2) ];
|
|
53 *ou++ = '=';
|
|
54 } else if((char *)dte - (char *)p == 1) {
|
|
55 *ou++ = _sf_uc_ib[ *p >> 2 ];
|
|
56 *ou++ = _sf_uc_ib[ ((*p & 0x03) << 4) ];
|
|
57 *ou++ = '=';
|
|
58 *ou++ = '=';
|
|
59 };
|
|
60
|
|
61 *ou=0;
|
|
62
|
|
63 _sf_b64_len = (ou - output);
|
|
64
|
|
65 if(_sf_b64_buf)
|
|
66 free(_sf_b64_buf);
|
|
67 return _sf_b64_buf=output;
|
|
68 };
|
|
69
|