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