annotate src/libstrfunc.c @ 35:b2f247463b83 stable-0-5-6

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