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