Mercurial > libpst
annotate src/libstrfunc.c @ 138:4b8c2c8a5829
Added tag stable-0-6-27 for changeset d9d4ae17a2c3
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Sat, 07 Feb 2009 10:26:56 -0800 |
parents | fc11b1d1ad34 |
children | cda7c812ec01 |
rev | line source |
---|---|
16 | 1 |
2 /* Taken from LibStrfunc v7.3 */ | |
3 | |
123
ab2a11e72250
more cleanup of #include files.
Carl Byington <carl@five-ten-sg.com>
parents:
110
diff
changeset
|
4 #include "define.h" |
ab2a11e72250
more cleanup of #include files.
Carl Byington <carl@five-ten-sg.com>
parents:
110
diff
changeset
|
5 |
16 | 6 |
94
997cf1373f9e
fix base64 encoding that could create long lines
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
7 static char base64_code_chars[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=="; |
16 | 8 |
94
997cf1373f9e
fix base64 encoding that could create long lines
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
9 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
|
10 { |
997cf1373f9e
fix base64 encoding that could create long lines
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
11 if (*line_count == 76) { |
997cf1373f9e
fix base64 encoding that could create long lines
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
12 *(*ou)++ = '\n'; |
997cf1373f9e
fix base64 encoding that could create long lines
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
13 *line_count = 0; |
997cf1373f9e
fix base64 encoding that could create long lines
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
14 } |
997cf1373f9e
fix base64 encoding that could create long lines
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
15 *(*ou)++ = data; |
997cf1373f9e
fix base64 encoding that could create long lines
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
16 (*line_count)++; |
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 |
997cf1373f9e
fix base64 encoding that could create long lines
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
19 |
997cf1373f9e
fix base64 encoding that could create long lines
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
20 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
|
21 { |
997cf1373f9e
fix base64 encoding that could create long lines
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
22 int line_count = 0; |
997cf1373f9e
fix base64 encoding that could create long lines
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
23 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
|
24 } |
997cf1373f9e
fix base64 encoding that could create long lines
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
25 |
997cf1373f9e
fix base64 encoding that could create long lines
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
26 |
997cf1373f9e
fix base64 encoding that could create long lines
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
27 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
|
28 { |
43 | 29 char *output; |
94
997cf1373f9e
fix base64 encoding that could create long lines
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
30 char *ou; |
997cf1373f9e
fix base64 encoding that could create long lines
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
31 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
|
32 unsigned char *dte = p + size; |
16 | 33 |
94
997cf1373f9e
fix base64 encoding that could create long lines
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
34 if (data == NULL || size == 0) return NULL; |
36 | 35 |
110
7133b39975f7
patch from David Cuadrado to process emails with type PST_TYPE_OTHER
Carl Byington <carl@five-ten-sg.com>
parents:
94
diff
changeset
|
36 ou = output = (char *)malloc(size / 3 * 4 + (size / 57) + 6); |
94
997cf1373f9e
fix base64 encoding that could create long lines
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
37 if (!output) return NULL; |
36 | 38 |
94
997cf1373f9e
fix base64 encoding that could create long lines
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
39 while((dte-p) >= 3) { |
43 | 40 unsigned char x = p[0]; |
41 unsigned char y = p[1]; | |
42 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
|
43 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
|
44 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
|
45 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
|
46 base64_append(&ou, line_count, base64_code_chars[ z & 0x3F ]); |
43 | 47 p+=3; |
48 }; | |
94
997cf1373f9e
fix base64 encoding that could create long lines
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
49 if ((dte-p) == 2) { |
997cf1373f9e
fix base64 encoding that could create long lines
Carl Byington <carl@five-ten-sg.com>
parents:
43
diff
changeset
|
50 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
|
51 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
|
52 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
|
53 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
|
54 } 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
|
55 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
|
56 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
|
57 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
|
58 base64_append(&ou, line_count, '='); |
43 | 59 }; |
36 | 60 |
43 | 61 *ou=0; |
62 return output; | |
16 | 63 }; |
64 | |
43 | 65 |
66 void hexdump(char *hbuf, int start, int stop, int ascii) /* {{{ HexDump all or a part of some buffer */ | |
67 { | |
68 char c; | |
69 int diff,i; | |
70 | |
71 while (start < stop ) { | |
72 diff = stop - start; | |
73 if (diff > 16) diff = 16; | |
74 | |
75 fprintf(stderr, ":%08X ",start); | |
76 | |
77 for (i = 0; i < diff; i++) { | |
78 if( 8 == i ) fprintf( stderr, " " ); | |
79 fprintf(stderr, "%02X ",(unsigned char)*(hbuf+start+i)); | |
80 } | |
81 if (ascii) { | |
82 for (i = diff; i < 16; i++) fprintf(stderr, " "); | |
83 for (i = 0; i < diff; i++) { | |
84 c = *(hbuf+start+i); | |
85 fprintf(stderr, "%c", isprint(c) ? c : '.'); | |
86 } | |
87 } | |
88 fprintf(stderr, "\n"); | |
89 start += 16; | |
90 } | |
91 } |