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
|
43
|
12 char *base64_encode(void *data, size_t size) {
|
|
13 char *output;
|
|
14 register char *ou;
|
|
15 register unsigned char *p=(unsigned char *)data;
|
|
16 register void * dte = (void*)((char*)data + size);
|
|
17 register int nc=0;
|
16
|
18
|
43
|
19 if ( data == NULL || size == 0 ) return NULL;
|
36
|
20
|
43
|
21 ou = output = (char *)malloc(size / 3 * 4 + (size / 57) + 5);
|
|
22 if(!output) return NULL;
|
36
|
23
|
43
|
24 while((char *)dte - (char *)p >= 3) {
|
|
25 unsigned char x = p[0];
|
|
26 unsigned char y = p[1];
|
|
27 unsigned char z = p[2];
|
|
28 ou[0] = _sf_uc_ib[ x >> 2 ];
|
|
29 ou[1] = _sf_uc_ib[ ((x & 0x03) << 4) | (y >> 4) ];
|
|
30 ou[2] = _sf_uc_ib[ ((y & 0x0F) << 2) | (z >> 6) ];
|
|
31 ou[3] = _sf_uc_ib[ z & 0x3F ];
|
|
32 p+=3;
|
|
33 ou+=4;
|
|
34 nc+=4;
|
|
35 if(!(nc % 76)) *ou++='\n';
|
|
36 };
|
|
37 if ((char *)dte - (char *)p == 2) {
|
|
38 *ou++ = _sf_uc_ib[ *p >> 2 ];
|
|
39 *ou++ = _sf_uc_ib[ ((*p & 0x03) << 4) | (p[1] >> 4) ];
|
|
40 *ou++ = _sf_uc_ib[ ((p[1] & 0x0F) << 2) ];
|
|
41 *ou++ = '=';
|
|
42 } else if((char *)dte - (char *)p == 1) {
|
|
43 *ou++ = _sf_uc_ib[ *p >> 2 ];
|
|
44 *ou++ = _sf_uc_ib[ ((*p & 0x03) << 4) ];
|
|
45 *ou++ = '=';
|
|
46 *ou++ = '=';
|
|
47 };
|
36
|
48
|
43
|
49 *ou=0;
|
36
|
50
|
43
|
51 return output;
|
16
|
52 };
|
|
53
|
43
|
54
|
|
55 void hexdump(char *hbuf, int start, int stop, int ascii) /* {{{ HexDump all or a part of some buffer */
|
|
56 {
|
|
57 char c;
|
|
58 int diff,i;
|
|
59
|
|
60 while (start < stop ) {
|
|
61 diff = stop - start;
|
|
62 if (diff > 16) diff = 16;
|
|
63
|
|
64 fprintf(stderr, ":%08X ",start);
|
|
65
|
|
66 for (i = 0; i < diff; i++) {
|
|
67 if( 8 == i ) fprintf( stderr, " " );
|
|
68 fprintf(stderr, "%02X ",(unsigned char)*(hbuf+start+i));
|
|
69 }
|
|
70 if (ascii) {
|
|
71 for (i = diff; i < 16; i++) fprintf(stderr, " ");
|
|
72 for (i = 0; i < diff; i++) {
|
|
73 c = *(hbuf+start+i);
|
|
74 fprintf(stderr, "%c", isprint(c) ? c : '.');
|
|
75 }
|
|
76 }
|
|
77 fprintf(stderr, "\n");
|
|
78 start += 16;
|
|
79 }
|
|
80 }
|