Mercurial > libpst
comparison src/libstrfunc.c @ 94:997cf1373f9e
fix base64 encoding that could create long lines
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Tue, 09 Sep 2008 11:11:56 -0700 |
parents | f6db1f060a95 |
children | 7133b39975f7 |
comparison
equal
deleted
inserted
replaced
93:cb14583c119a | 94:997cf1373f9e |
---|---|
5 #include <ctype.h> | 5 #include <ctype.h> |
6 #include <stdlib.h> | 6 #include <stdlib.h> |
7 #include "libstrfunc.h" | 7 #include "libstrfunc.h" |
8 | 8 |
9 | 9 |
10 static unsigned char _sf_uc_ib[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=="; | 10 static char base64_code_chars[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=="; |
11 | 11 |
12 char *base64_encode(void *data, size_t size) { | 12 void base64_append(char **ou, int *line_count, char data) |
13 { | |
14 if (*line_count == 76) { | |
15 *(*ou)++ = '\n'; | |
16 *line_count = 0; | |
17 } | |
18 *(*ou)++ = data; | |
19 (*line_count)++; | |
20 } | |
21 | |
22 | |
23 char *base64_encode(void *data, size_t size) | |
24 { | |
25 int line_count = 0; | |
26 return base64_encode_multiple(data, size, &line_count); | |
27 } | |
28 | |
29 | |
30 char *base64_encode_multiple(void *data, size_t size, int *line_count) | |
31 { | |
13 char *output; | 32 char *output; |
14 register char *ou; | 33 char *ou; |
15 register unsigned char *p=(unsigned char *)data; | 34 unsigned char *p = (unsigned char *)data; |
16 register void * dte = (void*)((char*)data + size); | 35 unsigned char *dte = p + size; |
17 register int nc=0; | |
18 | 36 |
19 if ( data == NULL || size == 0 ) return NULL; | 37 if (data == NULL || size == 0) return NULL; |
20 | 38 |
21 ou = output = (char *)malloc(size / 3 * 4 + (size / 57) + 5); | 39 ou = output = (char *)malloc(size / 3 * 4 + (size / 57) + 5); |
22 if(!output) return NULL; | 40 if (!output) return NULL; |
23 | 41 |
24 while((char *)dte - (char *)p >= 3) { | 42 while((dte-p) >= 3) { |
25 unsigned char x = p[0]; | 43 unsigned char x = p[0]; |
26 unsigned char y = p[1]; | 44 unsigned char y = p[1]; |
27 unsigned char z = p[2]; | 45 unsigned char z = p[2]; |
28 ou[0] = _sf_uc_ib[ x >> 2 ]; | 46 base64_append(&ou, line_count, base64_code_chars[ x >> 2 ]); |
29 ou[1] = _sf_uc_ib[ ((x & 0x03) << 4) | (y >> 4) ]; | 47 base64_append(&ou, line_count, base64_code_chars[ ((x & 0x03) << 4) | (y >> 4) ]); |
30 ou[2] = _sf_uc_ib[ ((y & 0x0F) << 2) | (z >> 6) ]; | 48 base64_append(&ou, line_count, base64_code_chars[ ((y & 0x0F) << 2) | (z >> 6) ]); |
31 ou[3] = _sf_uc_ib[ z & 0x3F ]; | 49 base64_append(&ou, line_count, base64_code_chars[ z & 0x3F ]); |
32 p+=3; | 50 p+=3; |
33 ou+=4; | |
34 nc+=4; | |
35 if(!(nc % 76)) *ou++='\n'; | |
36 }; | 51 }; |
37 if ((char *)dte - (char *)p == 2) { | 52 if ((dte-p) == 2) { |
38 *ou++ = _sf_uc_ib[ *p >> 2 ]; | 53 base64_append(&ou, line_count, base64_code_chars[ *p >> 2 ]); |
39 *ou++ = _sf_uc_ib[ ((*p & 0x03) << 4) | (p[1] >> 4) ]; | 54 base64_append(&ou, line_count, base64_code_chars[ ((*p & 0x03) << 4) | (p[1] >> 4) ]); |
40 *ou++ = _sf_uc_ib[ ((p[1] & 0x0F) << 2) ]; | 55 base64_append(&ou, line_count, base64_code_chars[ ((p[1] & 0x0F) << 2) ]); |
41 *ou++ = '='; | 56 base64_append(&ou, line_count, '='); |
42 } else if((char *)dte - (char *)p == 1) { | 57 } else if ((dte-p) == 1) { |
43 *ou++ = _sf_uc_ib[ *p >> 2 ]; | 58 base64_append(&ou, line_count, base64_code_chars[ *p >> 2 ]); |
44 *ou++ = _sf_uc_ib[ ((*p & 0x03) << 4) ]; | 59 base64_append(&ou, line_count, base64_code_chars[ ((*p & 0x03) << 4) ]); |
45 *ou++ = '='; | 60 base64_append(&ou, line_count, '='); |
46 *ou++ = '='; | 61 base64_append(&ou, line_count, '='); |
47 }; | 62 }; |
48 | 63 |
49 *ou=0; | 64 *ou=0; |
50 | |
51 return output; | 65 return output; |
52 }; | 66 }; |
53 | 67 |
54 | 68 |
55 void hexdump(char *hbuf, int start, int stop, int ascii) /* {{{ HexDump all or a part of some buffer */ | 69 void hexdump(char *hbuf, int start, int stop, int ascii) /* {{{ HexDump all or a part of some buffer */ |