annotate libstrfunc.c @ 0:6b1b602514db libpst_0_5

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