annotate libstrfunc.c @ 1:43e8802f08c5 debian libpst_0_5_1

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