annotate src/libstrfunc.c @ 87:3ec5ad97e926

Use inttypes.h for portable printing of 64 bit items.
author Carl Byington <carl@five-ten-sg.com>
date Sun, 03 Aug 2008 15:42:37 -0700
parents f6db1f060a95
children 997cf1373f9e
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
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
10 static unsigned char _sf_uc_ib[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/==";
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
11
43
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents: 36
diff changeset
12 char *base64_encode(void *data, size_t size) {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents: 36
diff changeset
13 char *output;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents: 36
diff changeset
14 register char *ou;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents: 36
diff changeset
15 register unsigned char *p=(unsigned char *)data;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents: 36
diff changeset
16 register void * dte = (void*)((char*)data + size);
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents: 36
diff changeset
17 register int nc=0;
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
18
43
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents: 36
diff changeset
19 if ( data == NULL || size == 0 ) return NULL;
36
6fe121a971c9 valgrind fixes
carl
parents: 31
diff changeset
20
43
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents: 36
diff changeset
21 ou = output = (char *)malloc(size / 3 * 4 + (size / 57) + 5);
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents: 36
diff changeset
22 if(!output) return NULL;
36
6fe121a971c9 valgrind fixes
carl
parents: 31
diff changeset
23
43
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents: 36
diff changeset
24 while((char *)dte - (char *)p >= 3) {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents: 36
diff changeset
25 unsigned char x = p[0];
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents: 36
diff changeset
26 unsigned char y = p[1];
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents: 36
diff changeset
27 unsigned char z = p[2];
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents: 36
diff changeset
28 ou[0] = _sf_uc_ib[ x >> 2 ];
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents: 36
diff changeset
29 ou[1] = _sf_uc_ib[ ((x & 0x03) << 4) | (y >> 4) ];
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents: 36
diff changeset
30 ou[2] = _sf_uc_ib[ ((y & 0x0F) << 2) | (z >> 6) ];
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents: 36
diff changeset
31 ou[3] = _sf_uc_ib[ z & 0x3F ];
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents: 36
diff changeset
32 p+=3;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents: 36
diff changeset
33 ou+=4;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents: 36
diff changeset
34 nc+=4;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents: 36
diff changeset
35 if(!(nc % 76)) *ou++='\n';
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents: 36
diff changeset
36 };
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents: 36
diff changeset
37 if ((char *)dte - (char *)p == 2) {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents: 36
diff changeset
38 *ou++ = _sf_uc_ib[ *p >> 2 ];
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents: 36
diff changeset
39 *ou++ = _sf_uc_ib[ ((*p & 0x03) << 4) | (p[1] >> 4) ];
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents: 36
diff changeset
40 *ou++ = _sf_uc_ib[ ((p[1] & 0x0F) << 2) ];
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents: 36
diff changeset
41 *ou++ = '=';
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents: 36
diff changeset
42 } else if((char *)dte - (char *)p == 1) {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents: 36
diff changeset
43 *ou++ = _sf_uc_ib[ *p >> 2 ];
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents: 36
diff changeset
44 *ou++ = _sf_uc_ib[ ((*p & 0x03) << 4) ];
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents: 36
diff changeset
45 *ou++ = '=';
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents: 36
diff changeset
46 *ou++ = '=';
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents: 36
diff changeset
47 };
36
6fe121a971c9 valgrind fixes
carl
parents: 31
diff changeset
48
43
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents: 36
diff changeset
49 *ou=0;
36
6fe121a971c9 valgrind fixes
carl
parents: 31
diff changeset
50
43
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents: 36
diff changeset
51 return output;
16
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
52 };
c508ee15dfca switch to automake/autoconf
carl
parents:
diff changeset
53
43
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents: 36
diff changeset
54
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents: 36
diff changeset
55 void hexdump(char *hbuf, int start, int stop, int ascii) /* {{{ HexDump all or a part of some buffer */
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents: 36
diff changeset
56 {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents: 36
diff changeset
57 char c;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents: 36
diff changeset
58 int diff,i;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents: 36
diff changeset
59
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents: 36
diff changeset
60 while (start < stop ) {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents: 36
diff changeset
61 diff = stop - start;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents: 36
diff changeset
62 if (diff > 16) diff = 16;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents: 36
diff changeset
63
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents: 36
diff changeset
64 fprintf(stderr, ":%08X ",start);
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents: 36
diff changeset
65
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents: 36
diff changeset
66 for (i = 0; i < diff; i++) {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents: 36
diff changeset
67 if( 8 == i ) fprintf( stderr, " " );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents: 36
diff changeset
68 fprintf(stderr, "%02X ",(unsigned char)*(hbuf+start+i));
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents: 36
diff changeset
69 }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents: 36
diff changeset
70 if (ascii) {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents: 36
diff changeset
71 for (i = diff; i < 16; i++) fprintf(stderr, " ");
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents: 36
diff changeset
72 for (i = 0; i < diff; i++) {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents: 36
diff changeset
73 c = *(hbuf+start+i);
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents: 36
diff changeset
74 fprintf(stderr, "%c", isprint(c) ? c : '.');
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents: 36
diff changeset
75 }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents: 36
diff changeset
76 }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents: 36
diff changeset
77 fprintf(stderr, "\n");
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents: 36
diff changeset
78 start += 16;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents: 36
diff changeset
79 }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents: 36
diff changeset
80 }