Mercurial > libpst
annotate src/libstrfunc.c @ 96:2e3ec9511166
update the release notes
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Sun, 14 Sep 2008 11:56:19 -0700 |
parents | 997cf1373f9e |
children | 7133b39975f7 |
rev | line source |
---|---|
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 | |
94
997cf1373f9e
fix base64 encoding that could create long lines
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
10 static char base64_code_chars[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=="; |
16 | 11 |
94
997cf1373f9e
fix base64 encoding that could create long lines
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
12 void base64_append(char **ou, int *line_count, char data) |
997cf1373f9e
fix base64 encoding that could create long lines
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
13 { |
997cf1373f9e
fix base64 encoding that could create long lines
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
14 if (*line_count == 76) { |
997cf1373f9e
fix base64 encoding that could create long lines
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
15 *(*ou)++ = '\n'; |
997cf1373f9e
fix base64 encoding that could create long lines
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
16 *line_count = 0; |
997cf1373f9e
fix base64 encoding that could create long lines
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
17 } |
997cf1373f9e
fix base64 encoding that could create long lines
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
18 *(*ou)++ = data; |
997cf1373f9e
fix base64 encoding that could create long lines
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
19 (*line_count)++; |
997cf1373f9e
fix base64 encoding that could create long lines
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
20 } |
997cf1373f9e
fix base64 encoding that could create long lines
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
21 |
997cf1373f9e
fix base64 encoding that could create long lines
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
22 |
997cf1373f9e
fix base64 encoding that could create long lines
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
23 char *base64_encode(void *data, size_t size) |
997cf1373f9e
fix base64 encoding that could create long lines
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
24 { |
997cf1373f9e
fix base64 encoding that could create long lines
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
25 int line_count = 0; |
997cf1373f9e
fix base64 encoding that could create long lines
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
26 return base64_encode_multiple(data, size, &line_count); |
997cf1373f9e
fix base64 encoding that could create long lines
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
27 } |
997cf1373f9e
fix base64 encoding that could create long lines
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
28 |
997cf1373f9e
fix base64 encoding that could create long lines
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
29 |
997cf1373f9e
fix base64 encoding that could create long lines
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
30 char *base64_encode_multiple(void *data, size_t size, int *line_count) |
997cf1373f9e
fix base64 encoding that could create long lines
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
31 { |
43 | 32 char *output; |
94
997cf1373f9e
fix base64 encoding that could create long lines
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
33 char *ou; |
997cf1373f9e
fix base64 encoding that could create long lines
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
34 unsigned char *p = (unsigned char *)data; |
997cf1373f9e
fix base64 encoding that could create long lines
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
35 unsigned char *dte = p + size; |
16 | 36 |
94
997cf1373f9e
fix base64 encoding that could create long lines
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
37 if (data == NULL || size == 0) return NULL; |
36 | 38 |
43 | 39 ou = output = (char *)malloc(size / 3 * 4 + (size / 57) + 5); |
94
997cf1373f9e
fix base64 encoding that could create long lines
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
40 if (!output) return NULL; |
36 | 41 |
94
997cf1373f9e
fix base64 encoding that could create long lines
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
42 while((dte-p) >= 3) { |
43 | 43 unsigned char x = p[0]; |
44 unsigned char y = p[1]; | |
45 unsigned char z = p[2]; | |
94
997cf1373f9e
fix base64 encoding that could create long lines
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
46 base64_append(&ou, line_count, base64_code_chars[ x >> 2 ]); |
997cf1373f9e
fix base64 encoding that could create long lines
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
47 base64_append(&ou, line_count, base64_code_chars[ ((x & 0x03) << 4) | (y >> 4) ]); |
997cf1373f9e
fix base64 encoding that could create long lines
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
48 base64_append(&ou, line_count, base64_code_chars[ ((y & 0x0F) << 2) | (z >> 6) ]); |
997cf1373f9e
fix base64 encoding that could create long lines
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
49 base64_append(&ou, line_count, base64_code_chars[ z & 0x3F ]); |
43 | 50 p+=3; |
51 }; | |
94
997cf1373f9e
fix base64 encoding that could create long lines
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
52 if ((dte-p) == 2) { |
997cf1373f9e
fix base64 encoding that could create long lines
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
53 base64_append(&ou, line_count, base64_code_chars[ *p >> 2 ]); |
997cf1373f9e
fix base64 encoding that could create long lines
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
54 base64_append(&ou, line_count, base64_code_chars[ ((*p & 0x03) << 4) | (p[1] >> 4) ]); |
997cf1373f9e
fix base64 encoding that could create long lines
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
55 base64_append(&ou, line_count, base64_code_chars[ ((p[1] & 0x0F) << 2) ]); |
997cf1373f9e
fix base64 encoding that could create long lines
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
56 base64_append(&ou, line_count, '='); |
997cf1373f9e
fix base64 encoding that could create long lines
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
57 } else if ((dte-p) == 1) { |
997cf1373f9e
fix base64 encoding that could create long lines
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
58 base64_append(&ou, line_count, base64_code_chars[ *p >> 2 ]); |
997cf1373f9e
fix base64 encoding that could create long lines
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
59 base64_append(&ou, line_count, base64_code_chars[ ((*p & 0x03) << 4) ]); |
997cf1373f9e
fix base64 encoding that could create long lines
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
60 base64_append(&ou, line_count, '='); |
997cf1373f9e
fix base64 encoding that could create long lines
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
61 base64_append(&ou, line_count, '='); |
43 | 62 }; |
36 | 63 |
43 | 64 *ou=0; |
65 return output; | |
16 | 66 }; |
67 | |
43 | 68 |
69 void hexdump(char *hbuf, int start, int stop, int ascii) /* {{{ HexDump all or a part of some buffer */ | |
70 { | |
71 char c; | |
72 int diff,i; | |
73 | |
74 while (start < stop ) { | |
75 diff = stop - start; | |
76 if (diff > 16) diff = 16; | |
77 | |
78 fprintf(stderr, ":%08X ",start); | |
79 | |
80 for (i = 0; i < diff; i++) { | |
81 if( 8 == i ) fprintf( stderr, " " ); | |
82 fprintf(stderr, "%02X ",(unsigned char)*(hbuf+start+i)); | |
83 } | |
84 if (ascii) { | |
85 for (i = diff; i < 16; i++) fprintf(stderr, " "); | |
86 for (i = 0; i < diff; i++) { | |
87 c = *(hbuf+start+i); | |
88 fprintf(stderr, "%c", isprint(c) ? c : '.'); | |
89 } | |
90 } | |
91 fprintf(stderr, "\n"); | |
92 start += 16; | |
93 } | |
94 } |