author | zautrix <zautrix> | 2004-07-03 16:33:12 (UTC) |
---|---|---|
committer | zautrix <zautrix> | 2004-07-03 16:33:12 (UTC) |
commit | e3b89230f065c48c84b48c88edb6eb088374c487 (patch) (unidiff) | |
tree | 162ea2ef909a6f82ccfcedf45d80d6c821174912 /kmicromail/libetpan/tools | |
parent | 2dd6ac0b2d24c91d35ce674a6c26351352df2b15 (diff) | |
download | kdepimpi-e3b89230f065c48c84b48c88edb6eb088374c487.zip kdepimpi-e3b89230f065c48c84b48c88edb6eb088374c487.tar.gz kdepimpi-e3b89230f065c48c84b48c88edb6eb088374c487.tar.bz2 |
Initial revision
40 files changed, 6598 insertions, 0 deletions
diff --git a/kmicromail/libetpan/tools/.libs/libtools.a b/kmicromail/libetpan/tools/.libs/libtools.a new file mode 100644 index 0000000..43424ef --- a/dev/null +++ b/kmicromail/libetpan/tools/.libs/libtools.a | |||
Binary files differ | |||
diff --git a/kmicromail/libetpan/tools/base64.c b/kmicromail/libetpan/tools/base64.c new file mode 100644 index 0000000..1532bac --- a/dev/null +++ b/kmicromail/libetpan/tools/base64.c | |||
@@ -0,0 +1,143 @@ | |||
1 | /* | ||
2 | * libEtPan! -- a mail stuff library | ||
3 | * | ||
4 | * Copyright (C) 2001, 2002 - Juergen Graf | ||
5 | * All rights reserved. | ||
6 | * | ||
7 | * Redistribution and use in source and binary forms, with or without | ||
8 | * modification, are permitted provided that the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. Neither the name of the libEtPan! project nor the names of its | ||
16 | * contributors may be used to endorse or promote products derived | ||
17 | * from this software without specific prior written permission. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
29 | * SUCH DAMAGE. | ||
30 | */ | ||
31 | |||
32 | /* | ||
33 | * $Id$ | ||
34 | */ | ||
35 | |||
36 | #include "base64.h" | ||
37 | |||
38 | #include <stdlib.h> | ||
39 | |||
40 | #define OUTPUT_SIZE 513 | ||
41 | #define CHAR64(c) (((c) < 0 || (c) > 127) ? -1 : index_64[(c)]) | ||
42 | |||
43 | static char index_64[128] = { | ||
44 | -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, | ||
45 | -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, | ||
46 | -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63, | ||
47 | 52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1,-1,-1,-1, | ||
48 | -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14, | ||
49 | 15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1, | ||
50 | -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40, | ||
51 | 41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1 | ||
52 | }; | ||
53 | |||
54 | static char basis_64[] = | ||
55 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; | ||
56 | |||
57 | char * encode_base64(const char * in, int len) | ||
58 | { | ||
59 | char * output, * tmp; | ||
60 | unsigned char oval; | ||
61 | int out_len; | ||
62 | |||
63 | out_len = ((len + 2) / 3 * 4) + 1; | ||
64 | |||
65 | if ((len > 0) && (in == NULL)) | ||
66 | return NULL; | ||
67 | |||
68 | output = malloc(out_len); | ||
69 | if (!output) | ||
70 | return NULL; | ||
71 | |||
72 | tmp = output; | ||
73 | while (len >= 3) { | ||
74 | *tmp++ = basis_64[in[0] >> 2]; | ||
75 | *tmp++ = basis_64[((in[0] << 4) & 0x30) | (in[1] >> 4)]; | ||
76 | *tmp++ = basis_64[((in[1] << 2) & 0x3c) | (in[2] >> 6)]; | ||
77 | *tmp++ = basis_64[in[2] & 0x3f]; | ||
78 | in += 3; | ||
79 | len -= 3; | ||
80 | } | ||
81 | if (len > 0) { | ||
82 | *tmp++ = basis_64[in[0] >> 2]; | ||
83 | oval = (in[0] << 4) & 0x30; | ||
84 | if (len > 1) oval |= in[1] >> 4; | ||
85 | *tmp++ = basis_64[oval]; | ||
86 | *tmp++ = (len < 2) ? '=' : basis_64[(in[1] << 2) & 0x3c]; | ||
87 | *tmp++ = '='; | ||
88 | } | ||
89 | |||
90 | *tmp = '\0'; | ||
91 | |||
92 | return output; | ||
93 | } | ||
94 | |||
95 | char * decode_base64(const char * in, int len) | ||
96 | { | ||
97 | char * output, * out; | ||
98 | int i, c1, c2, c3, c4, out_len; | ||
99 | |||
100 | out_len = 0; | ||
101 | |||
102 | output = malloc(OUTPUT_SIZE); | ||
103 | if (output == NULL) | ||
104 | return NULL; | ||
105 | out = output; | ||
106 | |||
107 | if (in[0] == '+' && in[1] == ' ') | ||
108 | in += 2; | ||
109 | |||
110 | for (i = 0; i < (len / 4); i++) { | ||
111 | c1 = in[0]; | ||
112 | c2 = in[1]; | ||
113 | c3 = in[2]; | ||
114 | c4 = in[3]; | ||
115 | if (CHAR64(c1) == -1 || CHAR64(c2) == -1 || | ||
116 | (c3 != '=' && CHAR64(c3) == -1) || | ||
117 | (c4 != '=' && CHAR64(c4) == -1)) | ||
118 | return NULL; | ||
119 | |||
120 | in += 4; | ||
121 | *output++ = (CHAR64(c1) << 2) | (CHAR64(c2) >> 4); | ||
122 | if (++out_len >= OUTPUT_SIZE) | ||
123 | return NULL; | ||
124 | |||
125 | if (c3 != '=') { | ||
126 | *output++ = ((CHAR64(c2) << 4) & 0xf0) | (CHAR64(c3) >> 2); | ||
127 | if (++out_len >= OUTPUT_SIZE) | ||
128 | return NULL; | ||
129 | |||
130 | if (c4 != '=') { | ||
131 | *output++ = ((CHAR64(c3) << 6) & 0xc0) | CHAR64(c4); | ||
132 | if (++out_len >= OUTPUT_SIZE) | ||
133 | return NULL; | ||
134 | } | ||
135 | } | ||
136 | } | ||
137 | |||
138 | *output = 0; | ||
139 | |||
140 | return out; | ||
141 | } | ||
142 | |||
143 | |||
diff --git a/kmicromail/libetpan/tools/base64.h b/kmicromail/libetpan/tools/base64.h new file mode 100644 index 0000000..aa5f3e5 --- a/dev/null +++ b/kmicromail/libetpan/tools/base64.h | |||
@@ -0,0 +1,59 @@ | |||
1 | /* | ||
2 | * libEtPan! -- a mail stuff library | ||
3 | * | ||
4 | * Copyright (C) 2001, 2002 - Juergen Graf | ||
5 | * All rights reserved. | ||
6 | * | ||
7 | * Redistribution and use in source and binary forms, with or without | ||
8 | * modification, are permitted provided that the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. Neither the name of the libEtPan! project nor the names of its | ||
16 | * contributors may be used to endorse or promote products derived | ||
17 | * from this software without specific prior written permission. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
29 | * SUCH DAMAGE. | ||
30 | */ | ||
31 | |||
32 | /* | ||
33 | * $Id$ | ||
34 | */ | ||
35 | |||
36 | #ifndef BASE64_H | ||
37 | #define BASE64_H | ||
38 | |||
39 | #ifdef __cplusplus | ||
40 | extern "C" { | ||
41 | #endif | ||
42 | |||
43 | /** | ||
44 | * creates (malloc) a new base64 encoded string from a standard 8bit string | ||
45 | * don't forget to free it when time comes ;) | ||
46 | */ | ||
47 | char * encode_base64(const char * in, int len); | ||
48 | |||
49 | /** | ||
50 | * creates (malloc) a new standard 8bit string from an base64 encoded string | ||
51 | * don't forget to free it when time comes ;) | ||
52 | */ | ||
53 | char * decode_base64(const char * in, int len); | ||
54 | |||
55 | #ifdef __cplusplus | ||
56 | } | ||
57 | #endif | ||
58 | |||
59 | #endif | ||
diff --git a/kmicromail/libetpan/tools/carray.c b/kmicromail/libetpan/tools/carray.c new file mode 100644 index 0000000..a8e78c9 --- a/dev/null +++ b/kmicromail/libetpan/tools/carray.c | |||
@@ -0,0 +1,143 @@ | |||
1 | |||
2 | /* | ||
3 | * libEtPan! -- a mail stuff library | ||
4 | * | ||
5 | * carray - Implements simple dynamic pointer arrays | ||
6 | * | ||
7 | * Copyright (c) 1999-2000, Gaël Roualland <gael.roualland@iname.com> | ||
8 | * interface changes - 2002 - DINH Viet Hoa | ||
9 | * All rights reserved. | ||
10 | * | ||
11 | * Redistribution and use in source and binary forms, with or without | ||
12 | * modification, are permitted provided that the following conditions | ||
13 | * are met: | ||
14 | * 1. Redistributions of source code must retain the above copyright | ||
15 | * notice, this list of conditions and the following disclaimer. | ||
16 | * 2. Redistributions in binary form must reproduce the above copyright | ||
17 | * notice, this list of conditions and the following disclaimer in the | ||
18 | * documentation and/or other materials provided with the distribution. | ||
19 | * 3. Neither the name of the libEtPan! project nor the names of its | ||
20 | * contributors may be used to endorse or promote products derived | ||
21 | * from this software without specific prior written permission. | ||
22 | * | ||
23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
33 | * SUCH DAMAGE. | ||
34 | */ | ||
35 | |||
36 | /* | ||
37 | * $Id$ | ||
38 | */ | ||
39 | |||
40 | #include <stdlib.h> | ||
41 | #include <string.h> | ||
42 | #include "carray.h" | ||
43 | |||
44 | carray * carray_new(unsigned int initsize) { | ||
45 | carray * array; | ||
46 | |||
47 | array = (carray *) malloc(sizeof(carray)); | ||
48 | if (!array) return NULL; | ||
49 | |||
50 | array->len = 0; | ||
51 | array->max = initsize; | ||
52 | array->array = (void **) malloc(sizeof(void *) * initsize); | ||
53 | if (!array->array) { | ||
54 | free(array); | ||
55 | return NULL; | ||
56 | } | ||
57 | return array; | ||
58 | } | ||
59 | |||
60 | int carray_add(carray * array, void * data, unsigned int * index) { | ||
61 | int r; | ||
62 | |||
63 | r = carray_set_size(array, array->len + 1); | ||
64 | if (r < 0) | ||
65 | return r; | ||
66 | |||
67 | array->array[array->len - 1] = data; | ||
68 | if (index != NULL) | ||
69 | * index = array->len - 1; | ||
70 | |||
71 | return 0; | ||
72 | } | ||
73 | |||
74 | int carray_set_size(carray * array, unsigned int new_size) | ||
75 | { | ||
76 | if (new_size > array->max) { | ||
77 | unsigned int n = array->max * 2; | ||
78 | void * new; | ||
79 | |||
80 | while (n <= new_size) | ||
81 | n *= 2; | ||
82 | |||
83 | new = (void **) realloc(array->array, sizeof(void *) * n); | ||
84 | if (!new) | ||
85 | return -1; | ||
86 | array->array = new; | ||
87 | array->max = n; | ||
88 | } | ||
89 | array->len = new_size; | ||
90 | |||
91 | return 0; | ||
92 | } | ||
93 | |||
94 | int carray_delete_fast(carray * array, unsigned int indx) { | ||
95 | if (indx >= array->len) | ||
96 | return -1; | ||
97 | |||
98 | array->array[indx] = NULL; | ||
99 | |||
100 | return 0; | ||
101 | } | ||
102 | |||
103 | int carray_delete(carray * array, unsigned int indx) { | ||
104 | if (indx >= array->len) | ||
105 | return -1; | ||
106 | |||
107 | if (indx != --array->len) | ||
108 | array->array[indx] = array->array[array->len]; | ||
109 | return 0; | ||
110 | } | ||
111 | |||
112 | int carray_delete_slow(carray * array, unsigned int indx) { | ||
113 | if (indx >= array->len) | ||
114 | return -1; | ||
115 | |||
116 | if (indx != --array->len) | ||
117 | memmove(array->array + indx, array->array + indx + 1, | ||
118 | (array->len - indx) * sizeof(void *)); | ||
119 | return 0; | ||
120 | } | ||
121 | |||
122 | #ifdef NO_MACROS | ||
123 | void ** carray_data(carray * array) { | ||
124 | return array->array; | ||
125 | } | ||
126 | |||
127 | unsigned int carray_count(carray * array) { | ||
128 | return array->len; | ||
129 | } | ||
130 | |||
131 | void * carray_get(carray * array, unsigned int indx) { | ||
132 | return array->array[indx]; | ||
133 | } | ||
134 | |||
135 | void carray_set(carray * array, unsigned int indx, void * value) { | ||
136 | array->array[indx] = value; | ||
137 | } | ||
138 | #endif | ||
139 | |||
140 | void carray_free(carray * array) { | ||
141 | free(array->array); | ||
142 | free(array); | ||
143 | } | ||
diff --git a/kmicromail/libetpan/tools/carray.h b/kmicromail/libetpan/tools/carray.h new file mode 100644 index 0000000..06003aa --- a/dev/null +++ b/kmicromail/libetpan/tools/carray.h | |||
@@ -0,0 +1,124 @@ | |||
1 | |||
2 | /* | ||
3 | * libEtPan! -- a mail stuff library | ||
4 | * | ||
5 | * carray - Implements simple dynamic pointer arrays | ||
6 | * | ||
7 | * Copyright (c) 1999-2000, Gaël Roualland <gael.roualland@iname.com> | ||
8 | * interface changes - 2002 - DINH Viet Hoa | ||
9 | * All rights reserved. | ||
10 | * | ||
11 | * Redistribution and use in source and binary forms, with or without | ||
12 | * modification, are permitted provided that the following conditions | ||
13 | * are met: | ||
14 | * 1. Redistributions of source code must retain the above copyright | ||
15 | * notice, this list of conditions and the following disclaimer. | ||
16 | * 2. Redistributions in binary form must reproduce the above copyright | ||
17 | * notice, this list of conditions and the following disclaimer in the | ||
18 | * documentation and/or other materials provided with the distribution. | ||
19 | * 3. Neither the name of the libEtPan! project nor the names of its | ||
20 | * contributors may be used to endorse or promote products derived | ||
21 | * from this software without specific prior written permission. | ||
22 | * | ||
23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
33 | * SUCH DAMAGE. | ||
34 | */ | ||
35 | |||
36 | /* | ||
37 | * $Id$ | ||
38 | */ | ||
39 | |||
40 | #ifndef CARRAY_H | ||
41 | #define CARRAY_H | ||
42 | |||
43 | #ifdef __cplusplus | ||
44 | extern "C" { | ||
45 | #endif | ||
46 | |||
47 | struct carray_s { | ||
48 | void ** array; | ||
49 | unsigned int len; | ||
50 | unsigned int max; | ||
51 | }; | ||
52 | |||
53 | typedef struct carray_s carray; | ||
54 | |||
55 | /* Creates a new array of pointers, with initsize preallocated cells */ | ||
56 | carray * carray_new(unsigned int initsize); | ||
57 | |||
58 | /* Adds the pointer to data in the array. | ||
59 | Returns the index of the pointer in the array or -1 on error */ | ||
60 | int carray_add(carray * array, void * data, unsigned int * index); | ||
61 | |||
62 | int carray_set_size(carray * array, unsigned int new_size); | ||
63 | |||
64 | /* Removes the cell at this index position. Returns TRUE on success. | ||
65 | Order of elements in the array IS changed. */ | ||
66 | int carray_delete(carray * array, unsigned int indx); | ||
67 | |||
68 | /* Removes the cell at this index position. Returns TRUE on success. | ||
69 | Order of elements in the array IS not changed. */ | ||
70 | int carray_delete_slow(carray * array, unsigned int indx); | ||
71 | |||
72 | /* remove without decreasing the size of the array */ | ||
73 | int carray_delete_fast(carray * array, unsigned int indx); | ||
74 | |||
75 | /* Some of the following routines can be implemented as macros to | ||
76 | be faster. If you don't want it, define NO_MACROS */ | ||
77 | #ifdef NO_MACROS | ||
78 | |||
79 | /* Returns the array itself */ | ||
80 | void ** carray_data(carray *); | ||
81 | |||
82 | /* Returns the number of elements in the array */ | ||
83 | int carray_count(carray *); | ||
84 | |||
85 | /* Returns the contents of one cell */ | ||
86 | void * carray_get(carray * array, unsigned int indx); | ||
87 | |||
88 | /* Sets the contents of one cell */ | ||
89 | void carray_set(carray * array, unsigned int indx, void * value); | ||
90 | |||
91 | #else | ||
92 | |||
93 | #if 0 | ||
94 | #define carray_data(a) (a->array) | ||
95 | #define carray_count(a) (a->len) | ||
96 | #define carray_get(a, indx) (a->array[indx]) | ||
97 | #define carray_set(a, indx, v) do { a->array[indx]=v; } while(0) | ||
98 | #endif | ||
99 | |||
100 | static inline void ** carray_data(carray * array) { | ||
101 | return array->array; | ||
102 | } | ||
103 | |||
104 | static inline unsigned int carray_count(carray * array) { | ||
105 | return array->len; | ||
106 | } | ||
107 | |||
108 | static inline void * carray_get(carray * array, unsigned int indx) { | ||
109 | return array->array[indx]; | ||
110 | } | ||
111 | |||
112 | static inline void carray_set(carray * array, | ||
113 | unsigned int indx, void * value) { | ||
114 | array->array[indx] = value; | ||
115 | } | ||
116 | #endif | ||
117 | |||
118 | void carray_free(carray * array); | ||
119 | |||
120 | #ifdef __cplusplus | ||
121 | } | ||
122 | #endif | ||
123 | |||
124 | #endif | ||
diff --git a/kmicromail/libetpan/tools/charconv.c b/kmicromail/libetpan/tools/charconv.c new file mode 100644 index 0000000..bf3de51 --- a/dev/null +++ b/kmicromail/libetpan/tools/charconv.c | |||
@@ -0,0 +1,251 @@ | |||
1 | /* | ||
2 | * libEtPan! -- a mail stuff library | ||
3 | * | ||
4 | * Copyright (C) 2001, 2002 - DINH Viet Hoa | ||
5 | * All rights reserved. | ||
6 | * | ||
7 | * Redistribution and use in source and binary forms, with or without | ||
8 | * modification, are permitted provided that the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. Neither the name of the libEtPan! project nor the names of its | ||
16 | * contributors may be used to endorse or promote products derived | ||
17 | * from this software without specific prior written permission. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
29 | * SUCH DAMAGE. | ||
30 | */ | ||
31 | |||
32 | /* | ||
33 | * $Id$ | ||
34 | */ | ||
35 | |||
36 | #include "charconv.h" | ||
37 | |||
38 | #include "config.h" | ||
39 | #ifdef HAVE_ICONV | ||
40 | #include <iconv.h> | ||
41 | #endif | ||
42 | #include <stdlib.h> | ||
43 | #include <string.h> | ||
44 | #include <stdio.h> | ||
45 | #include <errno.h> | ||
46 | |||
47 | #include "mmapstring.h" | ||
48 | |||
49 | #ifdef HAVE_ICONV | ||
50 | static size_t mail_iconv (iconv_t cd, const char **inbuf, size_t *inbytesleft, | ||
51 | char **outbuf, size_t *outbytesleft, | ||
52 | char **inrepls, char *outrepl) | ||
53 | { | ||
54 | /* | ||
55 | XXX - force conversion of (* inbuf) to (char *) | ||
56 | because prototype of iconv() is the following : | ||
57 | |||
58 | size_t iconv(iconv_t cd, char **restrict inbuf, | ||
59 | size_t *restrict inbytesleft, char **restrict outbuf, | ||
60 | size_t *restrict outbytesleft); | ||
61 | */ | ||
62 | |||
63 | size_t ret = 0, ret1; | ||
64 | char *ib = (char *) *inbuf; | ||
65 | size_t ibl = *inbytesleft; | ||
66 | char *ob = *outbuf; | ||
67 | size_t obl = *outbytesleft; | ||
68 | |||
69 | for (;;) | ||
70 | { | ||
71 | ret1 = iconv (cd, &ib, &ibl, &ob, &obl); | ||
72 | if (ret1 != (size_t)-1) | ||
73 | ret += ret1; | ||
74 | if (ibl && obl && errno == EILSEQ) | ||
75 | { | ||
76 | if (inrepls) | ||
77 | { | ||
78 | /* Try replacing the input */ | ||
79 | char **t; | ||
80 | for (t = inrepls; *t; t++) | ||
81 | { | ||
82 | char *ib1 = *t; | ||
83 | size_t ibl1 = strlen (*t); | ||
84 | char *ob1 = ob; | ||
85 | size_t obl1 = obl; | ||
86 | iconv (cd, &ib1, &ibl1, &ob1, &obl1); | ||
87 | if (!ibl1) | ||
88 | { | ||
89 | ++ib, --ibl; | ||
90 | ob = ob1, obl = obl1; | ||
91 | ++ret; | ||
92 | break; | ||
93 | } | ||
94 | } | ||
95 | if (*t) | ||
96 | continue; | ||
97 | } | ||
98 | if (outrepl) | ||
99 | { | ||
100 | /* Try replacing the output */ | ||
101 | size_t n = strlen (outrepl); | ||
102 | if (n <= obl) | ||
103 | { | ||
104 | memcpy (ob, outrepl, n); | ||
105 | ++ib, --ibl; | ||
106 | ob += n, obl -= n; | ||
107 | ++ret; | ||
108 | continue; | ||
109 | } | ||
110 | } | ||
111 | } | ||
112 | *inbuf = ib, *inbytesleft = ibl; | ||
113 | *outbuf = ob, *outbytesleft = obl; | ||
114 | return ret; | ||
115 | } | ||
116 | } | ||
117 | #endif | ||
118 | |||
119 | int charconv(const char * tocode, const char * fromcode, | ||
120 | const char * str, size_t length, | ||
121 | char ** result) | ||
122 | { | ||
123 | #ifndef HAVE_ICONV | ||
124 | return MAIL_CHARCONV_ERROR_UNKNOWN_CHARSET; | ||
125 | #else | ||
126 | iconv_t conv; | ||
127 | size_t r; | ||
128 | char * out; | ||
129 | char * pout; | ||
130 | size_t out_size; | ||
131 | size_t old_out_size; | ||
132 | size_t count; | ||
133 | int res; | ||
134 | |||
135 | conv = iconv_open(tocode, fromcode); | ||
136 | if (conv == (iconv_t) -1) { | ||
137 | res = MAIL_CHARCONV_ERROR_UNKNOWN_CHARSET; | ||
138 | goto err; | ||
139 | } | ||
140 | |||
141 | out_size = 4 * length; | ||
142 | |||
143 | out = malloc(out_size + 1); | ||
144 | if (out == NULL) { | ||
145 | res = MAIL_CHARCONV_ERROR_MEMORY; | ||
146 | goto close_iconv; | ||
147 | } | ||
148 | |||
149 | pout = out; | ||
150 | old_out_size = out_size; | ||
151 | |||
152 | r = mail_iconv(conv, &str, &length, &pout, &out_size, NULL, "?"); | ||
153 | |||
154 | if (r == (size_t) -1) { | ||
155 | res = MAIL_CHARCONV_ERROR_CONV; | ||
156 | goto free; | ||
157 | } | ||
158 | |||
159 | iconv_close(conv); | ||
160 | |||
161 | * pout = '\0'; | ||
162 | count = old_out_size - out_size; | ||
163 | pout = realloc(out, count + 1); | ||
164 | if (pout != NULL) | ||
165 | out = pout; | ||
166 | |||
167 | * result = out; | ||
168 | |||
169 | return MAIL_CHARCONV_NO_ERROR; | ||
170 | |||
171 | free: | ||
172 | free(out); | ||
173 | close_iconv: | ||
174 | iconv_close(conv); | ||
175 | err: | ||
176 | return res; | ||
177 | #endif | ||
178 | }; | ||
179 | |||
180 | int charconv_buffer(const char * tocode, const char * fromcode, | ||
181 | const char * str, size_t length, | ||
182 | char ** result, size_t * result_len) | ||
183 | { | ||
184 | #ifndef HAVE_ICONV | ||
185 | return MAIL_CHARCONV_ERROR_UNKNOWN_CHARSET; | ||
186 | #else | ||
187 | iconv_t conv; | ||
188 | size_t iconv_r; | ||
189 | int r; | ||
190 | char * out; | ||
191 | char * pout; | ||
192 | size_t out_size; | ||
193 | size_t old_out_size; | ||
194 | size_t count; | ||
195 | MMAPString * mmapstr; | ||
196 | int res; | ||
197 | |||
198 | conv = iconv_open(tocode, fromcode); | ||
199 | if (conv == (iconv_t) -1) { | ||
200 | res = MAIL_CHARCONV_ERROR_UNKNOWN_CHARSET; | ||
201 | goto err; | ||
202 | } | ||
203 | |||
204 | out_size = 4 * length; | ||
205 | |||
206 | mmapstr = mmap_string_sized_new(out_size + 1); | ||
207 | if (mmapstr == NULL) { | ||
208 | res = MAIL_CHARCONV_ERROR_MEMORY; | ||
209 | goto err; | ||
210 | } | ||
211 | |||
212 | out = mmapstr->str; | ||
213 | |||
214 | pout = out; | ||
215 | old_out_size = out_size; | ||
216 | |||
217 | iconv_r = mail_iconv(conv, &str, &length, &pout, &out_size, NULL, "?"); | ||
218 | |||
219 | if (iconv_r == (size_t) -1) { | ||
220 | res = MAIL_CHARCONV_ERROR_CONV; | ||
221 | goto free; | ||
222 | } | ||
223 | |||
224 | iconv_close(conv); | ||
225 | |||
226 | * pout = '\0'; | ||
227 | |||
228 | count = old_out_size - out_size; | ||
229 | |||
230 | r = mmap_string_ref(mmapstr); | ||
231 | if (r < 0) { | ||
232 | res = MAIL_CHARCONV_ERROR_MEMORY; | ||
233 | goto free; | ||
234 | } | ||
235 | |||
236 | * result = out; | ||
237 | * result_len = count; | ||
238 | |||
239 | return MAIL_CHARCONV_NO_ERROR; | ||
240 | |||
241 | free: | ||
242 | mmap_string_free(mmapstr); | ||
243 | err: | ||
244 | return -1; | ||
245 | #endif | ||
246 | }; | ||
247 | |||
248 | void charconv_buffer_free(char * str) | ||
249 | { | ||
250 | mmap_string_unref(str); | ||
251 | } | ||
diff --git a/kmicromail/libetpan/tools/charconv.h b/kmicromail/libetpan/tools/charconv.h new file mode 100644 index 0000000..5a435ff --- a/dev/null +++ b/kmicromail/libetpan/tools/charconv.h | |||
@@ -0,0 +1,67 @@ | |||
1 | /* | ||
2 | * libEtPan! -- a mail stuff library | ||
3 | * | ||
4 | * Copyright (C) 2001, 2002 - DINH Viet Hoa | ||
5 | * All rights reserved. | ||
6 | * | ||
7 | * Redistribution and use in source and binary forms, with or without | ||
8 | * modification, are permitted provided that the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. Neither the name of the libEtPan! project nor the names of its | ||
16 | * contributors may be used to endorse or promote products derived | ||
17 | * from this software without specific prior written permission. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
29 | * SUCH DAMAGE. | ||
30 | */ | ||
31 | |||
32 | /* | ||
33 | * $Id$ | ||
34 | */ | ||
35 | |||
36 | #ifndef CHARCONV_H | ||
37 | |||
38 | #define CHARCONV_H | ||
39 | |||
40 | #ifdef __cplusplus | ||
41 | extern "C" { | ||
42 | #endif | ||
43 | |||
44 | #include <sys/types.h> | ||
45 | |||
46 | enum { | ||
47 | MAIL_CHARCONV_NO_ERROR = 0, | ||
48 | MAIL_CHARCONV_ERROR_UNKNOWN_CHARSET, | ||
49 | MAIL_CHARCONV_ERROR_MEMORY, | ||
50 | MAIL_CHARCONV_ERROR_CONV, | ||
51 | }; | ||
52 | |||
53 | int charconv(const char * tocode, const char * fromcode, | ||
54 | const char * str, size_t length, | ||
55 | char ** result); | ||
56 | |||
57 | int charconv_buffer(const char * tocode, const char * fromcode, | ||
58 | const char * str, size_t length, | ||
59 | char ** result, size_t * result_len); | ||
60 | |||
61 | void charconv_buffer_free(char * str); | ||
62 | |||
63 | #ifdef __cplusplus | ||
64 | } | ||
65 | #endif | ||
66 | |||
67 | #endif | ||
diff --git a/kmicromail/libetpan/tools/chash.c b/kmicromail/libetpan/tools/chash.c new file mode 100644 index 0000000..2055221 --- a/dev/null +++ b/kmicromail/libetpan/tools/chash.c | |||
@@ -0,0 +1,395 @@ | |||
1 | |||
2 | /* | ||
3 | * libEtPan! -- a mail stuff library | ||
4 | * | ||
5 | * chash - Implements generic hash tables. | ||
6 | * | ||
7 | * Copyright (c) 1999-2000, Gaël Roualland <gael.roualland@iname.com> | ||
8 | * interface changes - 2002 - DINH Viet Hoa | ||
9 | * All rights reserved. | ||
10 | * | ||
11 | * Redistribution and use in source and binary forms, with or without | ||
12 | * modification, are permitted provided that the following conditions | ||
13 | * are met: | ||
14 | * 1. Redistributions of source code must retain the above copyright | ||
15 | * notice, this list of conditions and the following disclaimer. | ||
16 | * 2. Redistributions in binary form must reproduce the above copyright | ||
17 | * notice, this list of conditions and the following disclaimer in the | ||
18 | * documentation and/or other materials provided with the distribution. | ||
19 | * 3. Neither the name of the libEtPan! project nor the names of its | ||
20 | * contributors may be used to endorse or promote products derived | ||
21 | * from this software without specific prior written permission. | ||
22 | * | ||
23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
33 | * SUCH DAMAGE. | ||
34 | */ | ||
35 | |||
36 | /* | ||
37 | * $Id$ | ||
38 | */ | ||
39 | |||
40 | #include <stdlib.h> | ||
41 | #include <string.h> | ||
42 | |||
43 | #include "chash.h" | ||
44 | |||
45 | /* This defines the maximum (average) number of entries per bucket. | ||
46 | The hash is resized everytime inserting an entry makes the | ||
47 | average go over that value. */ | ||
48 | #define CHASH_MAXDEPTH 3 | ||
49 | |||
50 | static inline unsigned int chash_func(const char * key, unsigned int len) { | ||
51 | #if 0 | ||
52 | register unsigned int c = 0, t; | ||
53 | register const char * k = key; | ||
54 | |||
55 | while (len--) { | ||
56 | c += (c << 4) + *k++; | ||
57 | if ((t = c & 0xF0000000)) { | ||
58 | c ^= t >> 24; | ||
59 | c ^= t; | ||
60 | } | ||
61 | } | ||
62 | return c; | ||
63 | #endif | ||
64 | register unsigned int c = 5381; | ||
65 | register const char * k = key; | ||
66 | |||
67 | while (len--) { | ||
68 | c = ((c << 5) + c) + *k++; | ||
69 | } | ||
70 | |||
71 | return c; | ||
72 | } | ||
73 | |||
74 | static inline char * chash_dup(const void * data, unsigned int len) | ||
75 | { | ||
76 | void * r; | ||
77 | |||
78 | r = (char *) malloc(len); | ||
79 | if (!r) | ||
80 | return NULL; | ||
81 | memcpy(r, data, len); | ||
82 | return r; | ||
83 | } | ||
84 | |||
85 | chash * chash_new(unsigned int size, int flags) | ||
86 | { | ||
87 | chash * h; | ||
88 | |||
89 | h = (chash *) malloc(sizeof(chash)); | ||
90 | if (h == NULL) | ||
91 | return NULL; | ||
92 | |||
93 | h->count = 0; | ||
94 | h->cells = (struct chashcell **) calloc(size, sizeof(struct chashcell *)); | ||
95 | if (h->cells == NULL) { | ||
96 | free(h); | ||
97 | return NULL; | ||
98 | } | ||
99 | h->size = size; | ||
100 | h->copykey = flags & CHASH_COPYKEY; | ||
101 | h->copyvalue = flags & CHASH_COPYVALUE; | ||
102 | |||
103 | return h; | ||
104 | } | ||
105 | |||
106 | int chash_get(chash * hash, | ||
107 | chashdatum * key, chashdatum * result) | ||
108 | { | ||
109 | unsigned int func; | ||
110 | chashiter * iter; | ||
111 | |||
112 | func = chash_func(key->data, key->len); | ||
113 | |||
114 | /* look for the key in existing cells */ | ||
115 | iter = hash->cells[func % hash->size]; | ||
116 | while (iter) { | ||
117 | if (iter->key.len == key->len && iter->func == func | ||
118 | && !memcmp(iter->key.data, key->data, key->len)) { | ||
119 | * result = iter->value; /* found */ | ||
120 | |||
121 | return 0; | ||
122 | } | ||
123 | iter = iter->next; | ||
124 | } | ||
125 | |||
126 | return -1; | ||
127 | } | ||
128 | |||
129 | int chash_set(chash * hash, | ||
130 | chashdatum * key, | ||
131 | chashdatum * value, | ||
132 | chashdatum * oldvalue) | ||
133 | { | ||
134 | unsigned int func, indx; | ||
135 | chashiter * iter, * cell; | ||
136 | int r; | ||
137 | |||
138 | if (hash->count > hash->size * CHASH_MAXDEPTH) { | ||
139 | r = chash_resize(hash, (hash->count / CHASH_MAXDEPTH) * 2 + 1); | ||
140 | if (r < 0) | ||
141 | goto err; | ||
142 | } | ||
143 | |||
144 | func = chash_func(key->data, key->len); | ||
145 | indx = func % hash->size; | ||
146 | |||
147 | /* look for the key in existing cells */ | ||
148 | iter = hash->cells[indx]; | ||
149 | while (iter) { | ||
150 | if (iter->key.len == key->len && iter->func == func | ||
151 | && !memcmp(iter->key.data, key->data, key->len)) { | ||
152 | /* found, replacing entry */ | ||
153 | if (hash->copyvalue) { | ||
154 | char * data; | ||
155 | |||
156 | data = chash_dup(value->data, value->len); | ||
157 | if (data == NULL) | ||
158 | goto err; | ||
159 | |||
160 | free(iter->value.data); | ||
161 | iter->value.data = data; | ||
162 | iter->value.len = value->len; | ||
163 | } else { | ||
164 | if (oldvalue != NULL) { | ||
165 | oldvalue->data = iter->value.data; | ||
166 | oldvalue->len = iter->value.len; | ||
167 | } | ||
168 | iter->value.data = value->data; | ||
169 | iter->value.len = value->len; | ||
170 | } | ||
171 | if (!hash->copykey) | ||
172 | iter->key.data = key->data; | ||
173 | |||
174 | if (oldvalue != NULL) { | ||
175 | oldvalue->data = value->data; | ||
176 | oldvalue->len = value->len; | ||
177 | } | ||
178 | |||
179 | return 0; | ||
180 | } | ||
181 | iter = iter->next; | ||
182 | } | ||
183 | |||
184 | if (oldvalue != NULL) { | ||
185 | oldvalue->data = NULL; | ||
186 | oldvalue->len = 0; | ||
187 | } | ||
188 | |||
189 | /* not found, adding entry */ | ||
190 | cell = (struct chashcell *) malloc(sizeof(struct chashcell)); | ||
191 | if (cell == NULL) | ||
192 | goto err; | ||
193 | |||
194 | if (hash->copykey) { | ||
195 | cell->key.data = chash_dup(key->data, key->len); | ||
196 | if (cell->key.data == NULL) | ||
197 | goto free; | ||
198 | } | ||
199 | else | ||
200 | cell->key.data = key->data; | ||
201 | |||
202 | cell->key.len = key->len; | ||
203 | if (hash->copyvalue) { | ||
204 | cell->value.data = chash_dup(value->data, value->len); | ||
205 | if (cell->value.data == NULL) | ||
206 | goto free_key_data; | ||
207 | } | ||
208 | else | ||
209 | cell->value.data = value->data; | ||
210 | |||
211 | cell->value.len = value->len; | ||
212 | cell->func = func; | ||
213 | cell->next = hash->cells[indx]; | ||
214 | hash->cells[indx] = cell; | ||
215 | hash->count++; | ||
216 | |||
217 | return 0; | ||
218 | |||
219 | free_key_data: | ||
220 | if (hash->copykey) | ||
221 | free(cell->key.data); | ||
222 | free: | ||
223 | free(cell); | ||
224 | err: | ||
225 | return -1; | ||
226 | } | ||
227 | |||
228 | int chash_delete(chash * hash, chashdatum * key, chashdatum * oldvalue) | ||
229 | { | ||
230 | /* chashdatum result = { NULL, TRUE }; */ | ||
231 | unsigned int func, indx; | ||
232 | chashiter * iter, * old; | ||
233 | |||
234 | /* | ||
235 | if (!keylen) | ||
236 | keylen = strlen(key) + 1; | ||
237 | */ | ||
238 | |||
239 | func = chash_func(key->data, key->len); | ||
240 | indx = func % hash->size; | ||
241 | |||
242 | /* look for the key in existing cells */ | ||
243 | old = NULL; | ||
244 | iter = hash->cells[indx]; | ||
245 | while (iter) { | ||
246 | if (iter->key.len == key->len && iter->func == func | ||
247 | && !memcmp(iter->key.data, key->data, key->len)) { | ||
248 | /* found, deleting */ | ||
249 | if (old) | ||
250 | old->next = iter->next; | ||
251 | else | ||
252 | hash->cells[indx] = iter->next; | ||
253 | if (hash->copykey) | ||
254 | free(iter->key.data); | ||
255 | if (hash->copyvalue) | ||
256 | free(iter->value.data); | ||
257 | else { | ||
258 | if (oldvalue != NULL) { | ||
259 | oldvalue->data = iter->value.data; | ||
260 | oldvalue->len = iter->value.len; | ||
261 | } | ||
262 | } | ||
263 | free(iter); | ||
264 | hash->count--; | ||
265 | return 0; | ||
266 | } | ||
267 | old = iter; | ||
268 | iter = iter->next; | ||
269 | } | ||
270 | |||
271 | return -1; /* not found */ | ||
272 | } | ||
273 | |||
274 | void chash_free(chash * hash) { | ||
275 | unsigned int indx; | ||
276 | chashiter * iter, * next; | ||
277 | |||
278 | /* browse the hash table */ | ||
279 | for(indx = 0; indx < hash->size; indx++) { | ||
280 | iter = hash->cells[indx]; | ||
281 | while (iter) { | ||
282 | next = iter->next; | ||
283 | if (hash->copykey) | ||
284 | free(iter->key.data); | ||
285 | if (hash->copyvalue) | ||
286 | free(iter->value.data); | ||
287 | free(iter); | ||
288 | iter = next; | ||
289 | } | ||
290 | } | ||
291 | free(hash->cells); | ||
292 | free(hash); | ||
293 | } | ||
294 | |||
295 | void chash_clear(chash * hash) { | ||
296 | unsigned int indx; | ||
297 | chashiter * iter, * next; | ||
298 | |||
299 | /* browse the hash table */ | ||
300 | for(indx = 0; indx < hash->size; indx++) { | ||
301 | iter = hash->cells[indx]; | ||
302 | while (iter) { | ||
303 | next = iter->next; | ||
304 | if (hash->copykey) | ||
305 | free(iter->key.data); | ||
306 | if (hash->copyvalue) | ||
307 | free(iter->value.data); | ||
308 | free(iter); | ||
309 | iter = next; | ||
310 | } | ||
311 | } | ||
312 | memset(hash->cells, 0, hash->size * sizeof(* hash->cells)); | ||
313 | hash->count = 0; | ||
314 | } | ||
315 | |||
316 | chashiter * chash_begin(chash * hash) { | ||
317 | chashiter * iter; | ||
318 | unsigned int indx = 0; | ||
319 | |||
320 | iter = hash->cells[0]; | ||
321 | while(!iter) { | ||
322 | indx++; | ||
323 | if (indx >= hash->size) | ||
324 | return NULL; | ||
325 | iter = hash->cells[indx]; | ||
326 | } | ||
327 | return iter; | ||
328 | } | ||
329 | |||
330 | chashiter * chash_next(chash * hash, chashiter * iter) { | ||
331 | unsigned int indx; | ||
332 | |||
333 | if (!iter) | ||
334 | return NULL; | ||
335 | |||
336 | indx = iter->func % hash->size; | ||
337 | iter = iter->next; | ||
338 | |||
339 | while(!iter) { | ||
340 | indx++; | ||
341 | if (indx >= hash->size) | ||
342 | return NULL; | ||
343 | iter = hash->cells[indx]; | ||
344 | } | ||
345 | return iter; | ||
346 | } | ||
347 | |||
348 | int chash_resize(chash * hash, unsigned int size) | ||
349 | { | ||
350 | struct chashcell ** cells; | ||
351 | unsigned int indx, nindx; | ||
352 | chashiter * iter, * next; | ||
353 | |||
354 | if (hash->size == size) | ||
355 | return 0; | ||
356 | |||
357 | cells = (struct chashcell **) calloc(size, sizeof(struct chashcell *)); | ||
358 | if (!cells) | ||
359 | return -1; | ||
360 | |||
361 | /* browse initial hash and copy items in second hash */ | ||
362 | for(indx = 0; indx < hash->size; indx++) { | ||
363 | iter = hash->cells[indx]; | ||
364 | while (iter) { | ||
365 | next = iter->next; | ||
366 | nindx = iter->func % size; | ||
367 | iter->next = cells[nindx]; | ||
368 | cells[nindx] = iter; | ||
369 | iter = next; | ||
370 | } | ||
371 | } | ||
372 | free(hash->cells); | ||
373 | hash->size = size; | ||
374 | hash->cells = cells; | ||
375 | |||
376 | return 0; | ||
377 | } | ||
378 | |||
379 | #ifdef NO_MACROS | ||
380 | int chash_count(chash * hash) { | ||
381 | return hash->count; | ||
382 | } | ||
383 | |||
384 | int chash_size(chash * hash) { | ||
385 | return hash->size; | ||
386 | } | ||
387 | |||
388 | void chash_value(chashiter * iter, chashdatum * result) { | ||
389 | * result = iter->value; | ||
390 | } | ||
391 | |||
392 | void chash_key(chashiter * iter, chashdatum * result) { | ||
393 | * result = iter->key; | ||
394 | } | ||
395 | #endif | ||
diff --git a/kmicromail/libetpan/tools/chash.h b/kmicromail/libetpan/tools/chash.h new file mode 100644 index 0000000..3b2b7d3 --- a/dev/null +++ b/kmicromail/libetpan/tools/chash.h | |||
@@ -0,0 +1,166 @@ | |||
1 | |||
2 | /* | ||
3 | * libEtPan! -- a mail stuff library | ||
4 | * | ||
5 | * chash - Implements generic hash tables. | ||
6 | * | ||
7 | * Copyright (c) 1999-2000, Gaël Roualland <gael.roualland@iname.com> | ||
8 | * interface changes - 2002 - DINH Viet Hoa | ||
9 | * All rights reserved. | ||
10 | * | ||
11 | * Redistribution and use in source and binary forms, with or without | ||
12 | * modification, are permitted provided that the following conditions | ||
13 | * are met: | ||
14 | * 1. Redistributions of source code must retain the above copyright | ||
15 | * notice, this list of conditions and the following disclaimer. | ||
16 | * 2. Redistributions in binary form must reproduce the above copyright | ||
17 | * notice, this list of conditions and the following disclaimer in the | ||
18 | * documentation and/or other materials provided with the distribution. | ||
19 | * 3. Neither the name of the libEtPan! project nor the names of its | ||
20 | * contributors may be used to endorse or promote products derived | ||
21 | * from this software without specific prior written permission. | ||
22 | * | ||
23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
33 | * SUCH DAMAGE. | ||
34 | */ | ||
35 | |||
36 | /* | ||
37 | * $Id$ | ||
38 | */ | ||
39 | |||
40 | #ifndef CHASH_H | ||
41 | #define CHASH_H | ||
42 | |||
43 | #ifdef __cplusplus | ||
44 | extern "C" { | ||
45 | #endif | ||
46 | |||
47 | typedef struct { | ||
48 | void * data; | ||
49 | unsigned int len; | ||
50 | } chashdatum; | ||
51 | |||
52 | struct chash { | ||
53 | unsigned int size; | ||
54 | unsigned int count; | ||
55 | int copyvalue; | ||
56 | int copykey; | ||
57 | struct chashcell ** cells; | ||
58 | }; | ||
59 | |||
60 | typedef struct chash chash; | ||
61 | |||
62 | struct chashcell { | ||
63 | unsigned int func; | ||
64 | chashdatum key; | ||
65 | chashdatum value; | ||
66 | struct chashcell * next; | ||
67 | }; | ||
68 | |||
69 | typedef struct chashcell chashiter; | ||
70 | |||
71 | #define CHASH_COPYNONE 0 | ||
72 | #define CHASH_COPYKEY 1 | ||
73 | #define CHASH_COPYVALUE 2 | ||
74 | #define CHASH_COPYALL (CHASH_COPYKEY | CHASH_COPYVALUE) | ||
75 | |||
76 | #define CHASH_DEFAULTSIZE 13 | ||
77 | |||
78 | /* Allocates a new (empty) hash using this initial size and the given flags, | ||
79 | specifying which data should be copied in the hash. | ||
80 | CHASH_COPYNONE : Keys/Values are not copied. | ||
81 | CHASH_COPYKEY : Keys are dupped and freed as needed in the hash. | ||
82 | CHASH_COPYVALUE : Values are dupped and freed as needed in the hash. | ||
83 | CHASH_COPYALL : Both keys and values are dupped in the hash. | ||
84 | */ | ||
85 | chash * chash_new(unsigned int size, int flags); | ||
86 | |||
87 | /* Frees a hash */ | ||
88 | void chash_free(chash * hash); | ||
89 | |||
90 | /* Removes all elements from a hash */ | ||
91 | void chash_clear(chash * hash); | ||
92 | |||
93 | /* Adds an entry in the hash table. | ||
94 | Length can be 0 if key/value are strings. | ||
95 | If an entry already exists for this key, it is replaced, and its value | ||
96 | is returned. Otherwise, the data pointer will be NULL and the length | ||
97 | field be set to TRUE or FALSe to indicate success or failure. */ | ||
98 | int chash_set(chash * hash, | ||
99 | chashdatum * key, | ||
100 | chashdatum * value, | ||
101 | chashdatum * oldvalue); | ||
102 | |||
103 | /* Retrieves the data associated to the key if it is found in the hash table. | ||
104 | The data pointer and the length will be NULL if not found*/ | ||
105 | int chash_get(chash * hash, | ||
106 | chashdatum * key, chashdatum * result); | ||
107 | |||
108 | /* Removes the entry associated to this key if it is found in the hash table, | ||
109 | and returns its contents if not dupped (otherwise, pointer will be NULL | ||
110 | and len TRUE). If entry is not found both pointer and len will be NULL. */ | ||
111 | int chash_delete(chash * hash, | ||
112 | chashdatum * key, | ||
113 | chashdatum * oldvalue); | ||
114 | |||
115 | /* Resizes the hash table to the passed size. */ | ||
116 | int chash_resize(chash * hash, unsigned int size); | ||
117 | |||
118 | /* Returns an iterator to the first non-empty entry of the hash table */ | ||
119 | chashiter * chash_begin(chash * hash); | ||
120 | |||
121 | /* Returns the next non-empty entry of the hash table */ | ||
122 | chashiter * chash_next(chash * hash, chashiter * iter); | ||
123 | |||
124 | /* Some of the following routines can be implemented as macros to | ||
125 | be faster. If you don't want it, define NO_MACROS */ | ||
126 | #ifdef NO_MACROS | ||
127 | /* Returns the size of the hash table */ | ||
128 | unsigned int chash_size(chash * hash); | ||
129 | |||
130 | /* Returns the number of entries in the hash table */ | ||
131 | unsigned int chash_count(chash * hash); | ||
132 | |||
133 | /* Returns the key part of the entry pointed by the iterator */ | ||
134 | void chash_key(chashiter * iter, chashdatum * result); | ||
135 | |||
136 | /* Returns the value part of the entry pointed by the iterator */ | ||
137 | void chash_value(chashiter * iter, chashdatum * result); | ||
138 | |||
139 | #else | ||
140 | static inline unsigned int chash_size(chash * hash) | ||
141 | { | ||
142 | return hash->size; | ||
143 | } | ||
144 | |||
145 | static inline unsigned int chash_count(chash * hash) | ||
146 | { | ||
147 | return hash->count; | ||
148 | } | ||
149 | |||
150 | static inline void chash_key(chashiter * iter, chashdatum * result) | ||
151 | { | ||
152 | * result = iter->key; | ||
153 | } | ||
154 | |||
155 | static inline void chash_value(chashiter * iter, chashdatum * result) | ||
156 | { | ||
157 | * result = iter->value; | ||
158 | } | ||
159 | |||
160 | #endif | ||
161 | |||
162 | #ifdef __cplusplus | ||
163 | } | ||
164 | #endif | ||
165 | |||
166 | #endif | ||
diff --git a/kmicromail/libetpan/tools/cinthash.c b/kmicromail/libetpan/tools/cinthash.c new file mode 100644 index 0000000..02ee727 --- a/dev/null +++ b/kmicromail/libetpan/tools/cinthash.c | |||
@@ -0,0 +1,248 @@ | |||
1 | /* | ||
2 | * libEtPan! -- a mail stuff library | ||
3 | * | ||
4 | * Copyright (C) 2001, 2002 - DINH Viet Hoa | ||
5 | * All rights reserved. | ||
6 | * | ||
7 | * Redistribution and use in source and binary forms, with or without | ||
8 | * modification, are permitted provided that the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. Neither the name of the libEtPan! project nor the names of its | ||
16 | * contributors may be used to endorse or promote products derived | ||
17 | * from this software without specific prior written permission. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
29 | * SUCH DAMAGE. | ||
30 | */ | ||
31 | |||
32 | /* | ||
33 | * $Id$ | ||
34 | */ | ||
35 | |||
36 | #include <stdlib.h> | ||
37 | #include "cinthash.h" | ||
38 | |||
39 | struct cinthash_list { | ||
40 | unsigned long hash; | ||
41 | void * data; | ||
42 | struct cinthash_list * next; | ||
43 | }; | ||
44 | |||
45 | static struct cinthash_list HASH_LISTHEAD_NEW = { 0, NULL, NULL }; | ||
46 | |||
47 | static inline int hash_list_add(cinthash_t * table, | ||
48 | unsigned long hash, void * data) | ||
49 | { | ||
50 | struct cinthash_list * ht; | ||
51 | int index; | ||
52 | |||
53 | index = hash % table->hashtable_size; | ||
54 | |||
55 | ht = malloc(sizeof(struct cinthash_list)); | ||
56 | if (ht == NULL) | ||
57 | return -1; | ||
58 | |||
59 | ht->hash = hash; | ||
60 | ht->data = data; | ||
61 | ht->next = table->table[index].next; | ||
62 | |||
63 | table->table[index].next = ht; | ||
64 | |||
65 | return 0; | ||
66 | } | ||
67 | |||
68 | static inline void hash_list_free(struct cinthash_list * list) | ||
69 | { | ||
70 | struct cinthash_list * cur; | ||
71 | struct cinthash_list * next; | ||
72 | |||
73 | next = list; | ||
74 | while (next != NULL) { | ||
75 | cur = next; | ||
76 | next = cur->next; | ||
77 | free(cur); | ||
78 | } | ||
79 | } | ||
80 | |||
81 | static inline int hash_list_remove(cinthash_t * table, unsigned long hash) | ||
82 | { | ||
83 | struct cinthash_list * cur; | ||
84 | int index; | ||
85 | |||
86 | index = hash % table->hashtable_size; | ||
87 | |||
88 | for(cur = &table->table[index] ; cur->next != NULL ; cur = cur->next) { | ||
89 | if (cur->next->hash == hash) { | ||
90 | struct cinthash_list * hash_data; | ||
91 | |||
92 | hash_data = cur->next; | ||
93 | cur->next = cur->next->next; | ||
94 | |||
95 | free(hash_data); | ||
96 | |||
97 | return 0; | ||
98 | } | ||
99 | } | ||
100 | |||
101 | return -1; | ||
102 | } | ||
103 | |||
104 | static inline void * hash_list_find(cinthash_t * table, unsigned long hash) | ||
105 | { | ||
106 | struct cinthash_list * cur; | ||
107 | int index; | ||
108 | |||
109 | index = hash % table->hashtable_size; | ||
110 | |||
111 | for(cur = table->table[index].next ; cur != NULL ; cur = cur->next) { | ||
112 | if (cur->hash == hash) | ||
113 | return cur->data; | ||
114 | } | ||
115 | |||
116 | return NULL; | ||
117 | } | ||
118 | |||
119 | cinthash_t * cinthash_new(unsigned long hashtable_size) | ||
120 | { | ||
121 | cinthash_t * ht; | ||
122 | unsigned long i; | ||
123 | |||
124 | ht = malloc(sizeof(cinthash_t)); | ||
125 | if (ht == NULL) | ||
126 | return NULL; | ||
127 | |||
128 | ht->table = malloc(sizeof(struct cinthash_list) * hashtable_size); | ||
129 | if (ht->table == NULL) | ||
130 | return NULL; | ||
131 | |||
132 | ht->hashtable_size = hashtable_size; | ||
133 | ht->count = 0; | ||
134 | |||
135 | for(i = 0 ; i < hashtable_size ; i++) | ||
136 | ht->table[i] = HASH_LISTHEAD_NEW; | ||
137 | |||
138 | return ht; | ||
139 | } | ||
140 | |||
141 | void cinthash_free(cinthash_t * table) | ||
142 | { | ||
143 | unsigned long i; | ||
144 | |||
145 | for(i = 0 ; i < table->hashtable_size ; i++) | ||
146 | hash_list_free(table->table[i].next); | ||
147 | |||
148 | free(table->table); | ||
149 | |||
150 | free(table); | ||
151 | } | ||
152 | |||
153 | int cinthash_add(cinthash_t * table, unsigned long hash, void * data) | ||
154 | { | ||
155 | int index; | ||
156 | |||
157 | index = hash % table->hashtable_size; | ||
158 | |||
159 | if (table->table[index].data == NULL) { | ||
160 | table->table[index].hash = hash; | ||
161 | table->table[index].data = data; | ||
162 | table->table[index].next = NULL; | ||
163 | |||
164 | table->count ++; | ||
165 | |||
166 | return 0; | ||
167 | } | ||
168 | else { | ||
169 | int r; | ||
170 | |||
171 | r = hash_list_add(table, hash, data); | ||
172 | if (r == -1) | ||
173 | return -1; | ||
174 | |||
175 | table->count ++; | ||
176 | |||
177 | return 0; | ||
178 | } | ||
179 | } | ||
180 | |||
181 | int cinthash_remove(cinthash_t * table, unsigned long hash) | ||
182 | { | ||
183 | int index; | ||
184 | |||
185 | index = hash % table->hashtable_size; | ||
186 | |||
187 | if (table->table[index].hash == hash) { | ||
188 | table->table[index].hash = 0; | ||
189 | table->table[index].data = NULL; | ||
190 | |||
191 | table->count --; | ||
192 | |||
193 | return 0; | ||
194 | } | ||
195 | else { | ||
196 | int r; | ||
197 | |||
198 | r = hash_list_remove(table, hash); | ||
199 | |||
200 | table->count --; | ||
201 | |||
202 | return 0; | ||
203 | } | ||
204 | } | ||
205 | |||
206 | void * cinthash_find(cinthash_t * table, unsigned long hash) | ||
207 | { | ||
208 | int index; | ||
209 | |||
210 | index = hash % table->hashtable_size; | ||
211 | |||
212 | if (table->table[index].hash == hash) | ||
213 | return table->table[index].data; | ||
214 | |||
215 | return hash_list_find(table, hash); | ||
216 | } | ||
217 | |||
218 | void cinthash_foreach_key(cinthash_t * table, | ||
219 | void (* func)(unsigned long, void *), | ||
220 | void * data) | ||
221 | { | ||
222 | unsigned long index; | ||
223 | struct cinthash_list * cur; | ||
224 | |||
225 | for(index = 0 ; index < table->hashtable_size ; index ++) { | ||
226 | if (table->table[index].data != NULL) { | ||
227 | func(table->table[index].hash, data); | ||
228 | for(cur = table->table[index].next ; cur != NULL ; cur = cur->next) | ||
229 | func(cur->hash, data); | ||
230 | } | ||
231 | } | ||
232 | } | ||
233 | |||
234 | void cinthash_foreach_data(cinthash_t * table, | ||
235 | void (* func)(void *, void *), | ||
236 | void * data) | ||
237 | { | ||
238 | unsigned long index; | ||
239 | struct cinthash_list * cur; | ||
240 | |||
241 | for(index = 0 ; index < table->hashtable_size ; index ++) { | ||
242 | if (table->table[index].data != NULL) { | ||
243 | func(table->table[index].data, data); | ||
244 | for(cur = table->table[index].next ; cur != NULL ; cur = cur->next) | ||
245 | func(cur->data, data); | ||
246 | } | ||
247 | } | ||
248 | } | ||
diff --git a/kmicromail/libetpan/tools/cinthash.h b/kmicromail/libetpan/tools/cinthash.h new file mode 100644 index 0000000..7e59dff --- a/dev/null +++ b/kmicromail/libetpan/tools/cinthash.h | |||
@@ -0,0 +1,69 @@ | |||
1 | /* | ||
2 | * libEtPan! -- a mail stuff library | ||
3 | * | ||
4 | * Copyright (C) 2001, 2002 - DINH Viet Hoa | ||
5 | * All rights reserved. | ||
6 | * | ||
7 | * Redistribution and use in source and binary forms, with or without | ||
8 | * modification, are permitted provided that the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. Neither the name of the libEtPan! project nor the names of its | ||
16 | * contributors may be used to endorse or promote products derived | ||
17 | * from this software without specific prior written permission. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
29 | * SUCH DAMAGE. | ||
30 | */ | ||
31 | |||
32 | /* | ||
33 | * $Id$ | ||
34 | */ | ||
35 | |||
36 | #ifndef CINTHASH_H | ||
37 | |||
38 | #define CINTHASH_H | ||
39 | |||
40 | #ifdef __cplusplus | ||
41 | extern "C" { | ||
42 | #endif | ||
43 | |||
44 | typedef struct cinthash_t { | ||
45 | struct cinthash_list * table; | ||
46 | unsigned long hashtable_size ; | ||
47 | unsigned long count; | ||
48 | } cinthash_t; | ||
49 | |||
50 | cinthash_t * cinthash_new(unsigned long hashtable_size); | ||
51 | void cinthash_free(cinthash_t * table); | ||
52 | |||
53 | int cinthash_add(cinthash_t * table, unsigned long hash, void * data); | ||
54 | int cinthash_remove(cinthash_t * table, unsigned long hash); | ||
55 | void * cinthash_find(cinthash_t * table, unsigned long hash); | ||
56 | |||
57 | void cinthash_foreach_key(cinthash_t * table, | ||
58 | void (* func)(unsigned long, void *), | ||
59 | void * data); | ||
60 | |||
61 | void cinthash_foreach_data(cinthash_t * table, | ||
62 | void (* fun)(void *, void *), | ||
63 | void * data); | ||
64 | |||
65 | #ifdef __cplusplus | ||
66 | } | ||
67 | #endif | ||
68 | |||
69 | #endif | ||
diff --git a/kmicromail/libetpan/tools/clist.c b/kmicromail/libetpan/tools/clist.c new file mode 100644 index 0000000..e5c680d --- a/dev/null +++ b/kmicromail/libetpan/tools/clist.c | |||
@@ -0,0 +1,266 @@ | |||
1 | |||
2 | /* | ||
3 | * libEtPan! -- a mail stuff library | ||
4 | * | ||
5 | * clist - Implements simple generic double-linked pointer lists | ||
6 | * | ||
7 | * Copyright (c) 1999-2000, Gaël Roualland <gael.roualland@iname.com> | ||
8 | * interface changes - 2002 - DINH Viet Hoa | ||
9 | * All rights reserved. | ||
10 | * | ||
11 | * Redistribution and use in source and binary forms, with or without | ||
12 | * modification, are permitted provided that the following conditions | ||
13 | * are met: | ||
14 | * 1. Redistributions of source code must retain the above copyright | ||
15 | * notice, this list of conditions and the following disclaimer. | ||
16 | * 2. Redistributions in binary form must reproduce the above copyright | ||
17 | * notice, this list of conditions and the following disclaimer in the | ||
18 | * documentation and/or other materials provided with the distribution. | ||
19 | * 3. Neither the name of the libEtPan! project nor the names of its | ||
20 | * contributors may be used to endorse or promote products derived | ||
21 | * from this software without specific prior written permission. | ||
22 | * | ||
23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
33 | * SUCH DAMAGE. | ||
34 | */ | ||
35 | |||
36 | /* | ||
37 | * $Id$ | ||
38 | */ | ||
39 | |||
40 | #include <stdlib.h> | ||
41 | #include "clist.h" | ||
42 | |||
43 | clist * clist_new() { | ||
44 | clist * lst; | ||
45 | |||
46 | lst = (clist *) malloc(sizeof(clist)); | ||
47 | if (!lst) return NULL; | ||
48 | |||
49 | lst->first = lst->last = NULL; | ||
50 | lst->count = 0; | ||
51 | |||
52 | return lst; | ||
53 | } | ||
54 | |||
55 | void clist_free(clist * lst) { | ||
56 | clistcell * l1, * l2; | ||
57 | |||
58 | l1 = lst->first; | ||
59 | while (l1) { | ||
60 | l2 = l1->next; | ||
61 | free(l1); | ||
62 | l1 = l2; | ||
63 | } | ||
64 | |||
65 | free(lst); | ||
66 | } | ||
67 | |||
68 | #ifdef NO_MACROS | ||
69 | int clist_isempty(clist * lst) { | ||
70 | return ((lst->first==lst->last) && (lst->last==NULL)); | ||
71 | } | ||
72 | |||
73 | clistiter * clist_begin(clist * lst) { | ||
74 | return lst->first; | ||
75 | } | ||
76 | |||
77 | clistiter * clist_end(clist * lst) { | ||
78 | return lst->last; | ||
79 | } | ||
80 | |||
81 | clistiter * clist_next(clistiter * iter) { | ||
82 | if (iter) | ||
83 | return iter->next; | ||
84 | else | ||
85 | return NULL; | ||
86 | } | ||
87 | |||
88 | clistiter * clist_previous(clistiter * iter) { | ||
89 | if (iter) | ||
90 | return iter->previous; | ||
91 | else | ||
92 | return NULL; | ||
93 | } | ||
94 | |||
95 | void * clist_content(clistiter * iter) { | ||
96 | if (iter) | ||
97 | return iter->data; | ||
98 | else | ||
99 | return NULL; | ||
100 | } | ||
101 | |||
102 | int clist_count(clist * lst) { | ||
103 | return lst->count; | ||
104 | } | ||
105 | |||
106 | int clist_prepend(clist * lst, void * data) { | ||
107 | return clist_insert_before(lst, lst->first, data); | ||
108 | } | ||
109 | |||
110 | int clist_append(clist * lst, void * data) { | ||
111 | return clist_insert_after(lst, lst->last, data); | ||
112 | } | ||
113 | #endif | ||
114 | |||
115 | int clist_insert_before(clist * lst, clistiter * iter, void * data) { | ||
116 | clistcell * c; | ||
117 | |||
118 | c = (clistcell *) malloc(sizeof(clistcell)); | ||
119 | if (!c) return -1; | ||
120 | |||
121 | c->data = data; | ||
122 | lst->count++; | ||
123 | |||
124 | if (clist_isempty(lst)) { | ||
125 | c->previous = c->next = NULL; | ||
126 | lst->first = lst->last = c; | ||
127 | return 0; | ||
128 | } | ||
129 | |||
130 | if (!iter) { | ||
131 | c->previous = lst->last; | ||
132 | c->previous->next = c; | ||
133 | c->next = NULL; | ||
134 | lst->last = c; | ||
135 | return 0; | ||
136 | } | ||
137 | |||
138 | c->previous = iter->previous; | ||
139 | c->next = iter; | ||
140 | c->next->previous = c; | ||
141 | if (c->previous) | ||
142 | c->previous->next = c; | ||
143 | else | ||
144 | lst->first = c; | ||
145 | |||
146 | return 0; | ||
147 | } | ||
148 | |||
149 | int clist_insert_after(clist * lst, clistiter * iter, void * data) { | ||
150 | clistcell * c; | ||
151 | |||
152 | c = (clistcell *) malloc(sizeof(clistcell)); | ||
153 | if (!c) return -1; | ||
154 | |||
155 | c->data = data; | ||
156 | lst->count++; | ||
157 | |||
158 | if (clist_isempty(lst)) { | ||
159 | c->previous = c->next = NULL; | ||
160 | lst->first = lst->last = c; | ||
161 | return 0; | ||
162 | } | ||
163 | |||
164 | if (!iter) { | ||
165 | c->previous = lst->last; | ||
166 | c->previous->next = c; | ||
167 | c->next = NULL; | ||
168 | lst->last = c; | ||
169 | return 0; | ||
170 | } | ||
171 | |||
172 | c->previous = iter; | ||
173 | c->next = iter->next; | ||
174 | if (c->next) | ||
175 | c->next->previous = c; | ||
176 | else | ||
177 | lst->last = c; | ||
178 | c->previous->next = c; | ||
179 | |||
180 | return 0; | ||
181 | } | ||
182 | |||
183 | clistiter * clist_delete(clist * lst, clistiter * iter) { | ||
184 | clistiter * ret; | ||
185 | |||
186 | if (!iter) return NULL; | ||
187 | |||
188 | if (iter->previous) | ||
189 | iter->previous->next = iter->next; | ||
190 | else | ||
191 | lst->first = iter->next; | ||
192 | |||
193 | if (iter->next) { | ||
194 | iter->next->previous = iter->previous; | ||
195 | ret = iter->next; | ||
196 | } else { | ||
197 | lst->last = iter->previous; | ||
198 | ret = NULL; | ||
199 | } | ||
200 | |||
201 | free(iter); | ||
202 | lst->count--; | ||
203 | |||
204 | return ret; | ||
205 | } | ||
206 | |||
207 | |||
208 | |||
209 | void clist_foreach(clist * lst, clist_func func, void * data) | ||
210 | { | ||
211 | clistiter * cur; | ||
212 | |||
213 | for(cur = clist_begin(lst) ; cur != NULL ; cur = cur->next) | ||
214 | func(cur->data, data); | ||
215 | } | ||
216 | |||
217 | void clist_concat(clist * dest, clist * src) | ||
218 | { | ||
219 | if (src->first == NULL) { | ||
220 | /* do nothing */ | ||
221 | } | ||
222 | else if (dest->last == NULL) { | ||
223 | dest->first = src->first; | ||
224 | dest->last = src->last; | ||
225 | } | ||
226 | else { | ||
227 | dest->last->next = src->first; | ||
228 | src->first->previous = dest->last; | ||
229 | dest->last = src->last; | ||
230 | } | ||
231 | |||
232 | dest->count += src->count; | ||
233 | src->last = src->first = NULL; | ||
234 | } | ||
235 | |||
236 | static inline clistiter * internal_clist_nth(clist * lst, int index) | ||
237 | { | ||
238 | clistiter * cur; | ||
239 | |||
240 | cur = clist_begin(lst); | ||
241 | while ((index > 0) && (cur != NULL)) { | ||
242 | cur = cur->next; | ||
243 | index --; | ||
244 | } | ||
245 | |||
246 | if (cur == NULL) | ||
247 | return NULL; | ||
248 | |||
249 | return cur; | ||
250 | } | ||
251 | |||
252 | void * clist_nth_data(clist * lst, int index) | ||
253 | { | ||
254 | clistiter * cur; | ||
255 | |||
256 | cur = internal_clist_nth(lst, index); | ||
257 | if (cur == NULL) | ||
258 | return NULL; | ||
259 | |||
260 | return cur->data; | ||
261 | } | ||
262 | |||
263 | clistiter * clist_nth(clist * lst, int index) | ||
264 | { | ||
265 | return internal_clist_nth(lst, index); | ||
266 | } | ||
diff --git a/kmicromail/libetpan/tools/clist.h b/kmicromail/libetpan/tools/clist.h new file mode 100644 index 0000000..bd97f59 --- a/dev/null +++ b/kmicromail/libetpan/tools/clist.h | |||
@@ -0,0 +1,134 @@ | |||
1 | |||
2 | /* | ||
3 | * libEtPan! -- a mail stuff library | ||
4 | * | ||
5 | * clist - Implements simple generic double-linked pointer lists | ||
6 | * | ||
7 | * Copyright (c) 1999-2000, Gaël Roualland <gael.roualland@iname.com> | ||
8 | * interface changes - 2002 - DINH Viet Hoa | ||
9 | * All rights reserved. | ||
10 | * | ||
11 | * Redistribution and use in source and binary forms, with or without | ||
12 | * modification, are permitted provided that the following conditions | ||
13 | * are met: | ||
14 | * 1. Redistributions of source code must retain the above copyright | ||
15 | * notice, this list of conditions and the following disclaimer. | ||
16 | * 2. Redistributions in binary form must reproduce the above copyright | ||
17 | * notice, this list of conditions and the following disclaimer in the | ||
18 | * documentation and/or other materials provided with the distribution. | ||
19 | * 3. Neither the name of the libEtPan! project nor the names of its | ||
20 | * contributors may be used to endorse or promote products derived | ||
21 | * from this software without specific prior written permission. | ||
22 | * | ||
23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
33 | * SUCH DAMAGE. | ||
34 | */ | ||
35 | |||
36 | /* | ||
37 | * $Id$ | ||
38 | */ | ||
39 | |||
40 | #ifndef CLIST_H | ||
41 | #define CLIST_H | ||
42 | |||
43 | #ifdef __cplusplus | ||
44 | extern "C" { | ||
45 | #endif | ||
46 | |||
47 | typedef struct clistcell_s { | ||
48 | void * data; | ||
49 | struct clistcell_s * previous; | ||
50 | struct clistcell_s * next; | ||
51 | } clistcell; | ||
52 | |||
53 | struct clist_s { | ||
54 | clistcell * first; | ||
55 | clistcell * last; | ||
56 | int count; | ||
57 | }; | ||
58 | |||
59 | typedef struct clist_s clist; | ||
60 | typedef clistcell clistiter; | ||
61 | |||
62 | /* Allocate a new pointer list */ | ||
63 | clist * clist_new(); | ||
64 | |||
65 | /* Destroys a list. Data pointed by data pointers is NOT freed. */ | ||
66 | void clist_free(clist *); | ||
67 | |||
68 | /* Some of the following routines can be implemented as macros to | ||
69 | be faster. If you don't want it, define NO_MACROS */ | ||
70 | #ifdef NO_MACROS | ||
71 | |||
72 | /* Returns TRUE if list is empty */ | ||
73 | int clist_isempty(clist *); | ||
74 | |||
75 | /* Returns the number of elements in the list */ | ||
76 | int clist_count(clist *); | ||
77 | |||
78 | /* Returns an iterator to the first element of the list */ | ||
79 | clistiter * clist_begin(clist *); | ||
80 | |||
81 | /* Returns an iterator to the last element of the list */ | ||
82 | clistiter * clist_end(clist *); | ||
83 | |||
84 | /* Returns an iterator to the next element of the list */ | ||
85 | clistiter * clist_next(clistiter *); | ||
86 | |||
87 | /* Returns an iterator to the previous element of the list */ | ||
88 | clistiter * clist_previous(clistiter *); | ||
89 | |||
90 | /* Returns the data pointer of this element of the list */ | ||
91 | void* clist_content(clistiter *); | ||
92 | |||
93 | /* Inserts this data pointer at the beginning of the list */ | ||
94 | int clist_prepend(clist *, void *); | ||
95 | |||
96 | /* Inserts this data pointer at the end of the list */ | ||
97 | int clist_append(clist *, void *); | ||
98 | #else | ||
99 | #define clist_isempty(lst) ((lst->first==lst->last) && (lst->last==NULL)) | ||
100 | #define clist_count(lst) (lst->count) | ||
101 | #define clist_begin(lst) (lst->first) | ||
102 | #define clist_end(lst) (lst->last) | ||
103 | #define clist_next(iter) (iter ? iter->next : NULL) | ||
104 | #define clist_previous(iter) (iter ? iter->previous : NULL) | ||
105 | #define clist_content(iter) (iter ? iter->data : NULL) | ||
106 | #define clist_prepend(lst, data) (clist_insert_before(lst, lst->first, data)) | ||
107 | #define clist_append(lst, data) (clist_insert_after(lst, lst->last, data)) | ||
108 | #endif | ||
109 | |||
110 | /* Inserts this data pointer before the element pointed by the iterator */ | ||
111 | int clist_insert_before(clist *, clistiter *, void *); | ||
112 | |||
113 | /* Inserts this data pointer after the element pointed by the iterator */ | ||
114 | int clist_insert_after(clist *, clistiter *, void *); | ||
115 | |||
116 | /* Deletes the element pointed by the iterator. | ||
117 | Returns an iterator to the next element. */ | ||
118 | clistiter * clist_delete(clist *, clistiter *); | ||
119 | |||
120 | typedef void (* clist_func)(void *, void *); | ||
121 | |||
122 | void clist_foreach(clist * lst, clist_func func, void * data); | ||
123 | |||
124 | void clist_concat(clist * dest, clist * src); | ||
125 | |||
126 | void * clist_nth_data(clist * lst, int index); | ||
127 | |||
128 | clistiter * clist_nth(clist * lst, int index); | ||
129 | |||
130 | #ifdef __cplusplus | ||
131 | } | ||
132 | #endif | ||
133 | |||
134 | #endif | ||
diff --git a/kmicromail/libetpan/tools/connect.c b/kmicromail/libetpan/tools/connect.c new file mode 100644 index 0000000..c67c18c --- a/dev/null +++ b/kmicromail/libetpan/tools/connect.c | |||
@@ -0,0 +1,86 @@ | |||
1 | /* | ||
2 | * libEtPan! -- a mail stuff library | ||
3 | * | ||
4 | * Copyright (C) 2001, 2002 - DINH Viet Hoa | ||
5 | * All rights reserved. | ||
6 | * | ||
7 | * Redistribution and use in source and binary forms, with or without | ||
8 | * modification, are permitted provided that the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. Neither the name of the libEtPan! project nor the names of its | ||
16 | * contributors may be used to endorse or promote products derived | ||
17 | * from this software without specific prior written permission. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
29 | * SUCH DAMAGE. | ||
30 | */ | ||
31 | |||
32 | /* | ||
33 | * $Id$ | ||
34 | */ | ||
35 | |||
36 | #include "connect.h" | ||
37 | |||
38 | #include <sys/types.h> | ||
39 | #include <string.h> | ||
40 | #include <netdb.h> | ||
41 | #include <netinet/in.h> | ||
42 | #include <sys/socket.h> | ||
43 | #include <unistd.h> | ||
44 | |||
45 | uint16_t mail_get_service_port(const char * name, char * protocol) | ||
46 | { | ||
47 | struct servent * service; | ||
48 | |||
49 | service = getservbyname(name, protocol); | ||
50 | |||
51 | if (service == NULL) | ||
52 | return 0; | ||
53 | |||
54 | return service->s_port; | ||
55 | } | ||
56 | |||
57 | int mail_tcp_connect(const char * server, uint16_t port) | ||
58 | { | ||
59 | struct hostent * remotehost; | ||
60 | struct sockaddr_in sa; | ||
61 | int s; | ||
62 | int r; | ||
63 | |||
64 | s = socket(PF_INET, SOCK_STREAM, 0); | ||
65 | if (s == -1) | ||
66 | goto err; | ||
67 | |||
68 | remotehost = gethostbyname(server); | ||
69 | if (remotehost == NULL) | ||
70 | goto close_socket; | ||
71 | |||
72 | sa.sin_family = AF_INET; | ||
73 | sa.sin_port = htons(port); | ||
74 | memcpy(&sa.sin_addr, remotehost->h_addr, remotehost->h_length); | ||
75 | |||
76 | r = connect(s, (struct sockaddr *) &sa, sizeof(struct sockaddr_in)); | ||
77 | if (r == -1) | ||
78 | goto close_socket; | ||
79 | |||
80 | return s; | ||
81 | |||
82 | close_socket: | ||
83 | close(s); | ||
84 | err: | ||
85 | return -1; | ||
86 | } | ||
diff --git a/kmicromail/libetpan/tools/connect.h b/kmicromail/libetpan/tools/connect.h new file mode 100644 index 0000000..9e44501 --- a/dev/null +++ b/kmicromail/libetpan/tools/connect.h | |||
@@ -0,0 +1,54 @@ | |||
1 | /* | ||
2 | * libEtPan! -- a mail stuff library | ||
3 | * | ||
4 | * Copyright (C) 2001, 2002 - DINH Viet Hoa | ||
5 | * All rights reserved. | ||
6 | * | ||
7 | * Redistribution and use in source and binary forms, with or without | ||
8 | * modification, are permitted provided that the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. Neither the name of the libEtPan! project nor the names of its | ||
16 | * contributors may be used to endorse or promote products derived | ||
17 | * from this software without specific prior written permission. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
29 | * SUCH DAMAGE. | ||
30 | */ | ||
31 | |||
32 | /* | ||
33 | * $Id$ | ||
34 | */ | ||
35 | |||
36 | #ifndef CONNECT_H | ||
37 | |||
38 | #define CONNECT_H | ||
39 | |||
40 | #include <inttypes.h> | ||
41 | |||
42 | #ifdef __cplusplus | ||
43 | extern "C" { | ||
44 | #endif | ||
45 | |||
46 | uint16_t mail_get_service_port(const char * name, char * protocol); | ||
47 | int mail_tcp_connect(const char * server, uint16_t port); | ||
48 | |||
49 | #ifdef __cplusplus | ||
50 | } | ||
51 | #endif | ||
52 | |||
53 | #endif | ||
54 | |||
diff --git a/kmicromail/libetpan/tools/hmac-md5.h b/kmicromail/libetpan/tools/hmac-md5.h new file mode 100644 index 0000000..2ae6098 --- a/dev/null +++ b/kmicromail/libetpan/tools/hmac-md5.h | |||
@@ -0,0 +1,94 @@ | |||
1 | /* | ||
2 | * libEtPan! -- a mail stuff library | ||
3 | * | ||
4 | * Copyright (C) 2001, 2002 - DINH Viet Hoa | ||
5 | * All rights reserved. | ||
6 | * | ||
7 | * Redistribution and use in source and binary forms, with or without | ||
8 | * modification, are permitted provided that the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. Neither the name of the libEtPan! project nor the names of its | ||
16 | * contributors may be used to endorse or promote products derived | ||
17 | * from this software without specific prior written permission. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
29 | * SUCH DAMAGE. | ||
30 | */ | ||
31 | |||
32 | /* hmac-md5.h -- HMAC_MD5 functions | ||
33 | */ | ||
34 | |||
35 | /* | ||
36 | * $Id$ | ||
37 | */ | ||
38 | |||
39 | #ifndef HMAC_MD5_H | ||
40 | #define HMAC_MD5_H 1 | ||
41 | |||
42 | #ifdef __cplusplus | ||
43 | extern "C" { | ||
44 | #endif | ||
45 | |||
46 | #define HMAC_MD5_SIZE 16 | ||
47 | |||
48 | /* intermediate MD5 context */ | ||
49 | typedef struct HMAC_MD5_CTX_s { | ||
50 | MD5_CTX ictx, octx; | ||
51 | } HMAC_MD5_CTX; | ||
52 | |||
53 | /* intermediate HMAC state | ||
54 | * values stored in network byte order (Big Endian) | ||
55 | */ | ||
56 | typedef struct HMAC_MD5_STATE_s { | ||
57 | UINT4 istate[4]; | ||
58 | UINT4 ostate[4]; | ||
59 | } HMAC_MD5_STATE; | ||
60 | |||
61 | /* One step hmac computation | ||
62 | * | ||
63 | * digest may be same as text or key | ||
64 | */ | ||
65 | void hmac_md5(const unsigned char *text, int text_len, | ||
66 | const unsigned char *key, int key_len, | ||
67 | unsigned char digest[HMAC_MD5_SIZE]); | ||
68 | |||
69 | /* create context from key | ||
70 | */ | ||
71 | void hmac_md5_init(HMAC_MD5_CTX *hmac, | ||
72 | const unsigned char *key, int key_len); | ||
73 | |||
74 | /* precalculate intermediate state from key | ||
75 | */ | ||
76 | void hmac_md5_precalc(HMAC_MD5_STATE *hmac, | ||
77 | const unsigned char *key, int key_len); | ||
78 | |||
79 | /* initialize context from intermediate state | ||
80 | */ | ||
81 | void hmac_md5_import(HMAC_MD5_CTX *hmac, HMAC_MD5_STATE *state); | ||
82 | |||
83 | #define hmac_md5_update(hmac, text, text_len) MD5Update(&(hmac)->ictx, (text), (text_len)) | ||
84 | |||
85 | /* finish hmac from intermediate result. Intermediate result is zeroed. | ||
86 | */ | ||
87 | void hmac_md5_final(unsigned char digest[HMAC_MD5_SIZE], | ||
88 | HMAC_MD5_CTX *hmac); | ||
89 | |||
90 | #ifdef __cplusplus | ||
91 | } | ||
92 | #endif | ||
93 | |||
94 | #endif /* HMAC_MD5_H */ | ||
diff --git a/kmicromail/libetpan/tools/mail.h b/kmicromail/libetpan/tools/mail.h new file mode 100644 index 0000000..d4c63c4 --- a/dev/null +++ b/kmicromail/libetpan/tools/mail.h | |||
@@ -0,0 +1,56 @@ | |||
1 | /* | ||
2 | * libEtPan! -- a mail stuff library | ||
3 | * | ||
4 | * Copyright (C) 2001, 2002 - DINH Viet Hoa | ||
5 | * All rights reserved. | ||
6 | * | ||
7 | * Redistribution and use in source and binary forms, with or without | ||
8 | * modification, are permitted provided that the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. Neither the name of the libEtPan! project nor the names of its | ||
16 | * contributors may be used to endorse or promote products derived | ||
17 | * from this software without specific prior written permission. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
29 | * SUCH DAMAGE. | ||
30 | */ | ||
31 | |||
32 | /* | ||
33 | * $Id$ | ||
34 | */ | ||
35 | |||
36 | #ifndef MAIL_H | ||
37 | |||
38 | #define MAIL_H | ||
39 | |||
40 | #ifdef __cplusplus | ||
41 | extern "C" { | ||
42 | #endif | ||
43 | |||
44 | #ifndef TRUE | ||
45 | #define TRUE 1 | ||
46 | #endif | ||
47 | |||
48 | #ifndef FALSE | ||
49 | #define FALSE 0 | ||
50 | #endif | ||
51 | |||
52 | #ifdef __cplusplus | ||
53 | } | ||
54 | #endif | ||
55 | |||
56 | #endif | ||
diff --git a/kmicromail/libetpan/tools/mail_cache_db.c b/kmicromail/libetpan/tools/mail_cache_db.c new file mode 100644 index 0000000..5b6e9c9 --- a/dev/null +++ b/kmicromail/libetpan/tools/mail_cache_db.c | |||
@@ -0,0 +1,364 @@ | |||
1 | /* | ||
2 | * libEtPan! -- a mail stuff library | ||
3 | * | ||
4 | * Copyright (C) 2001, 2002 - DINH Viet Hoa | ||
5 | * All rights reserved. | ||
6 | * | ||
7 | * Redistribution and use in source and binary forms, with or without | ||
8 | * modification, are permitted provided that the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. Neither the name of the libEtPan! project nor the names of its | ||
16 | * contributors may be used to endorse or promote products derived | ||
17 | * from this software without specific prior written permission. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
29 | * SUCH DAMAGE. | ||
30 | */ | ||
31 | |||
32 | /* | ||
33 | * $Id$ | ||
34 | */ | ||
35 | |||
36 | #include "mail_cache_db.h" | ||
37 | |||
38 | #include <sys/types.h> | ||
39 | #include <sys/stat.h> | ||
40 | #include <fcntl.h> | ||
41 | #include <stdlib.h> | ||
42 | #include <string.h> | ||
43 | |||
44 | #ifndef CONFIG_H | ||
45 | #define CONFIG_H | ||
46 | #include "config.h" | ||
47 | #endif | ||
48 | |||
49 | #include "libetpan-config.h" | ||
50 | |||
51 | #include "maillock.h" | ||
52 | |||
53 | #if DBVERS >= 1 | ||
54 | #include <db.h> | ||
55 | #endif | ||
56 | |||
57 | #if DBVERS >= 1 | ||
58 | static struct mail_cache_db * mail_cache_db_new(DB * db) | ||
59 | { | ||
60 | struct mail_cache_db * cache_db; | ||
61 | |||
62 | cache_db = malloc(sizeof(* cache_db)); | ||
63 | if (cache_db == NULL) | ||
64 | return NULL; | ||
65 | cache_db->internal_database = db; | ||
66 | |||
67 | return cache_db; | ||
68 | } | ||
69 | |||
70 | static void mail_cache_db_free(struct mail_cache_db * cache_db) | ||
71 | { | ||
72 | free(cache_db); | ||
73 | } | ||
74 | #endif | ||
75 | |||
76 | int mail_cache_db_open(const char * filename, | ||
77 | struct mail_cache_db ** pcache_db) | ||
78 | { | ||
79 | #if DBVERS >= 1 | ||
80 | DB * dbp; | ||
81 | int r; | ||
82 | struct mail_cache_db * cache_db; | ||
83 | |||
84 | #if DB_VERSION_MAJOR >= 3 | ||
85 | r = db_create(&dbp, NULL, 0); | ||
86 | if (r != 0) | ||
87 | goto err; | ||
88 | |||
89 | #if (DB_VERSION_MAJOR >= 4) && ((DB_VERSION_MAJOR > 4) || (DB_VERSION_MINOR >= 1)) | ||
90 | r = dbp->open(dbp, NULL, filename, NULL, DB_BTREE, DB_CREATE, | ||
91 | S_IRUSR | S_IWUSR); | ||
92 | #else | ||
93 | r = dbp->open(dbp, filename, NULL, DB_BTREE, DB_CREATE, S_IRUSR | S_IWUSR); | ||
94 | #endif | ||
95 | if (r != 0) | ||
96 | goto close_db; | ||
97 | #else | ||
98 | #if DBVERS > 1 | ||
99 | r = db_open(filename, DB_BTREE, DB_CREATE, S_IRUSR | S_IWUSR, | ||
100 | NULL, NULL, &dbp); | ||
101 | if (r != 0) | ||
102 | goto err; | ||
103 | #elif DBVERS == 1 | ||
104 | dbp = dbopen(filename, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, DB_BTREE, NULL); | ||
105 | if (dbp == NULL) | ||
106 | goto err; | ||
107 | #else | ||
108 | goto err; | ||
109 | #endif | ||
110 | #endif | ||
111 | |||
112 | cache_db = mail_cache_db_new(dbp); | ||
113 | if (cache_db == NULL) | ||
114 | goto close_db; | ||
115 | |||
116 | * pcache_db = cache_db; | ||
117 | |||
118 | return 0; | ||
119 | |||
120 | close_db: | ||
121 | #if DBVERS > 1 | ||
122 | dbp->close(cache_db->internal_database, 0); | ||
123 | #elif DBVERS == 1 | ||
124 | dbp->close(cache_db->internal_database); | ||
125 | #endif | ||
126 | err: | ||
127 | return -1; | ||
128 | #else | ||
129 | return -1; | ||
130 | #endif | ||
131 | } | ||
132 | |||
133 | void mail_cache_db_close(struct mail_cache_db * cache_db) | ||
134 | { | ||
135 | #if DBVERS >= 1 | ||
136 | DB * dbp; | ||
137 | |||
138 | dbp = cache_db->internal_database; | ||
139 | |||
140 | #if DBVERS > 1 | ||
141 | dbp->close(cache_db->internal_database, 0); | ||
142 | #elif DBVERS == 1 | ||
143 | dbp->close(cache_db->internal_database); | ||
144 | #endif | ||
145 | |||
146 | mail_cache_db_free(cache_db); | ||
147 | #endif | ||
148 | } | ||
149 | |||
150 | int mail_cache_db_open_lock(const char * filename, | ||
151 | struct mail_cache_db ** pcache_db) | ||
152 | { | ||
153 | int r; | ||
154 | struct mail_cache_db * cache_db; | ||
155 | |||
156 | r = maillock_write_lock(filename, -1); | ||
157 | if (r < 0) | ||
158 | goto err; | ||
159 | |||
160 | r = mail_cache_db_open(filename, &cache_db); | ||
161 | if (r < 0) | ||
162 | goto unlock; | ||
163 | |||
164 | * pcache_db = cache_db; | ||
165 | |||
166 | return 0; | ||
167 | |||
168 | unlock: | ||
169 | maillock_write_unlock(filename, -1); | ||
170 | err: | ||
171 | return -1; | ||
172 | } | ||
173 | |||
174 | void mail_cache_db_close_unlock(const char * filename, | ||
175 | struct mail_cache_db * cache_db) | ||
176 | { | ||
177 | maillock_write_unlock(filename, -1); | ||
178 | mail_cache_db_close(cache_db); | ||
179 | } | ||
180 | |||
181 | |||
182 | int mail_cache_db_put(struct mail_cache_db * cache_db, | ||
183 | const void * key, size_t key_len, const void * value, size_t value_len) | ||
184 | { | ||
185 | #if DBVERS >= 1 | ||
186 | int r; | ||
187 | DBT db_key; | ||
188 | DBT db_data; | ||
189 | DB * dbp; | ||
190 | |||
191 | dbp = cache_db->internal_database; | ||
192 | |||
193 | memset(&db_key, 0, sizeof(db_key)); | ||
194 | memset(&db_data, 0, sizeof(db_data)); | ||
195 | db_key.data = (void *) key; | ||
196 | db_key.size = key_len; | ||
197 | db_data.data = (void *) value; | ||
198 | db_data.size = value_len; | ||
199 | |||
200 | #if DBVERS > 1 | ||
201 | r = dbp->put(dbp, NULL, &db_key, &db_data, 0); | ||
202 | #elif DBVERS == 1 | ||
203 | r = dbp->put(dbp, &db_key, &db_data, 0); | ||
204 | #else | ||
205 | r = -1; | ||
206 | #endif | ||
207 | |||
208 | return r; | ||
209 | #else | ||
210 | return -1; | ||
211 | #endif | ||
212 | } | ||
213 | |||
214 | int mail_cache_db_get(struct mail_cache_db * cache_db, | ||
215 | const void * key, size_t key_len, void ** pvalue, size_t * pvalue_len) | ||
216 | { | ||
217 | #if DBVERS >= 1 | ||
218 | int r; | ||
219 | DBT db_key; | ||
220 | DBT db_data; | ||
221 | DB * dbp; | ||
222 | |||
223 | dbp = cache_db->internal_database; | ||
224 | |||
225 | memset(&db_key, 0, sizeof(db_key)); | ||
226 | memset(&db_data, 0, sizeof(db_data)); | ||
227 | db_key.data = (void *) key; | ||
228 | db_key.size = key_len; | ||
229 | |||
230 | #if DBVERS > 1 | ||
231 | r = dbp->get(dbp, NULL, &db_key, &db_data, 0); | ||
232 | #elif DBVERS == 1 | ||
233 | r = dbp->get(dbp, &db_key, &db_data, 0); | ||
234 | #else | ||
235 | r = -1; | ||
236 | #endif | ||
237 | |||
238 | if (r != 0) | ||
239 | return r; | ||
240 | |||
241 | * pvalue = db_data.data; | ||
242 | * pvalue_len = db_data.size; | ||
243 | |||
244 | return 0; | ||
245 | #else | ||
246 | return -1; | ||
247 | #endif | ||
248 | } | ||
249 | |||
250 | int mail_cache_db_del(struct mail_cache_db * cache_db, | ||
251 | const void * key, size_t key_len) | ||
252 | { | ||
253 | #if DBVERS >= 1 | ||
254 | int r; | ||
255 | DBT db_key; | ||
256 | DB * dbp; | ||
257 | |||
258 | dbp = cache_db->internal_database; | ||
259 | |||
260 | memset(&db_key, 0, sizeof(db_key)); | ||
261 | db_key.data = (void *) key; | ||
262 | db_key.size = key_len; | ||
263 | |||
264 | #if DBVERS > 1 | ||
265 | r = dbp->del(dbp, NULL, &db_key, 0); | ||
266 | #elif DBVERS == 1 | ||
267 | r = dbp->del(dbp, &db_key, 0); | ||
268 | #else | ||
269 | r = -1; | ||
270 | #endif | ||
271 | |||
272 | return r; | ||
273 | #else | ||
274 | return -1; | ||
275 | #endif | ||
276 | } | ||
277 | |||
278 | #if DBVERS > 1 | ||
279 | int mail_cache_db_clean_up(struct mail_cache_db * cache_db, | ||
280 | chash * exist) | ||
281 | { | ||
282 | DB * dbp; | ||
283 | int r; | ||
284 | DBC * dbcp; | ||
285 | DBT db_key; | ||
286 | DBT db_data; | ||
287 | |||
288 | dbp = cache_db->internal_database; | ||
289 | |||
290 | r = dbp->cursor(dbp, NULL, &dbcp, 0); | ||
291 | if (r != 0) | ||
292 | return r; | ||
293 | |||
294 | memset(&db_key, 0, sizeof(db_key)); | ||
295 | memset(&db_data, 0, sizeof(db_data)); | ||
296 | |||
297 | while (1) { | ||
298 | chashdatum hash_key; | ||
299 | chashdatum hash_data; | ||
300 | |||
301 | r = dbcp->c_get(dbcp, &db_key, &db_data, DB_NEXT); | ||
302 | if (r != 0) | ||
303 | break; | ||
304 | |||
305 | hash_key.data = db_key.data; | ||
306 | hash_key.len = db_key.size; | ||
307 | |||
308 | r = chash_get(exist, &hash_key, &hash_data); | ||
309 | if (r < 0) { | ||
310 | r = dbcp->c_del(dbcp, 0); | ||
311 | if (r != 0) | ||
312 | return r; | ||
313 | } | ||
314 | } | ||
315 | |||
316 | r = dbcp->c_close(dbcp); | ||
317 | if (r != 0) | ||
318 | return r; | ||
319 | |||
320 | return 0; | ||
321 | } | ||
322 | #elif DBVERS == 1 | ||
323 | int mail_cache_db_clean_up(struct mail_cache_db * cache_db, | ||
324 | chash * exist) | ||
325 | { | ||
326 | DB * dbp; | ||
327 | int r; | ||
328 | DBT db_key; | ||
329 | DBT db_data; | ||
330 | |||
331 | dbp = cache_db->internal_database; | ||
332 | |||
333 | r = dbp->seq(dbp, &db_key, &db_data, R_FIRST); | ||
334 | if (r == -1) | ||
335 | return r; | ||
336 | |||
337 | while (r == 0) { | ||
338 | chashdatum hash_key; | ||
339 | chashdatum hash_data; | ||
340 | |||
341 | hash_key.data = db_key.data; | ||
342 | hash_key.len = db_key.size; | ||
343 | |||
344 | r = chash_get(exist, &hash_key, &hash_data); | ||
345 | if (r < 0) { | ||
346 | r = dbp->del(dbp, &db_key, 0); | ||
347 | if (r != 0) | ||
348 | return r; | ||
349 | } | ||
350 | |||
351 | r = dbp->seq(dbp, &db_key, &db_data, R_NEXT); | ||
352 | if (r == -1) | ||
353 | return r; | ||
354 | } | ||
355 | |||
356 | return 0; | ||
357 | } | ||
358 | #else | ||
359 | int mail_cache_db_clean_up(struct mail_cache_db * cache_db, | ||
360 | chash * exist) | ||
361 | { | ||
362 | return -1; | ||
363 | } | ||
364 | #endif | ||
diff --git a/kmicromail/libetpan/tools/mail_cache_db.h b/kmicromail/libetpan/tools/mail_cache_db.h new file mode 100644 index 0000000..0ca17d9 --- a/dev/null +++ b/kmicromail/libetpan/tools/mail_cache_db.h | |||
@@ -0,0 +1,138 @@ | |||
1 | /* | ||
2 | * libEtPan! -- a mail stuff library | ||
3 | * | ||
4 | * Copyright (C) 2001, 2002 - DINH Viet Hoa | ||
5 | * All rights reserved. | ||
6 | * | ||
7 | * Redistribution and use in source and binary forms, with or without | ||
8 | * modification, are permitted provided that the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. Neither the name of the libEtPan! project nor the names of its | ||
16 | * contributors may be used to endorse or promote products derived | ||
17 | * from this software without specific prior written permission. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
29 | * SUCH DAMAGE. | ||
30 | */ | ||
31 | |||
32 | /* | ||
33 | * $Id$ | ||
34 | */ | ||
35 | |||
36 | #ifndef MAIL_CACHE_DB_H | ||
37 | |||
38 | #define MAIL_CACHE_DB_H | ||
39 | |||
40 | #include <sys/types.h> | ||
41 | #include "mail_cache_db_types.h" | ||
42 | #include "chash.h" | ||
43 | |||
44 | #ifdef __cplusplus | ||
45 | extern "C" { | ||
46 | #endif | ||
47 | |||
48 | /* | ||
49 | this module will handle a database "f(key) -> value" in a file | ||
50 | |||
51 | berkeley DB or other can be used for implementation of low-level file. | ||
52 | */ | ||
53 | |||
54 | /* | ||
55 | mail_cache_db_open() | ||
56 | |||
57 | This function opens the file "filename". | ||
58 | The pointer return in pcache_db should be used for further references | ||
59 | to the database. | ||
60 | */ | ||
61 | |||
62 | int mail_cache_db_open(const char * filename, | ||
63 | struct mail_cache_db ** pcache_db); | ||
64 | |||
65 | /* | ||
66 | mail_cache_db_close() | ||
67 | |||
68 | This function closes the opened database. | ||
69 | The pointer cannot be used later. | ||
70 | */ | ||
71 | |||
72 | void mail_cache_db_close(struct mail_cache_db * cache_db); | ||
73 | |||
74 | /* | ||
75 | mail_cache_db_open_lock() | ||
76 | |||
77 | This function opens and locks the file "filename". | ||
78 | The pointer return in pcache_db should be used for further references | ||
79 | to the database. | ||
80 | */ | ||
81 | |||
82 | int mail_cache_db_open_lock(const char * filename, | ||
83 | struct mail_cache_db ** pcache_db); | ||
84 | |||
85 | /* | ||
86 | mail_cache_db_open_unlock() | ||
87 | |||
88 | This function closes and unlocks the opened database. | ||
89 | The pointer cannot be used later. | ||
90 | */ | ||
91 | |||
92 | void mail_cache_db_close_unlock(const char * filename, | ||
93 | struct mail_cache_db * cache_db); | ||
94 | |||
95 | /* | ||
96 | mail_cache_db_put() | ||
97 | |||
98 | This function will store a given key and value in the database. | ||
99 | */ | ||
100 | |||
101 | int mail_cache_db_put(struct mail_cache_db * cache_db, | ||
102 | const void * key, size_t key_len, const void * value, size_t value_len); | ||
103 | |||
104 | /* | ||
105 | mail_cache_db_get() | ||
106 | |||
107 | This function will retrieve the value corresponding to a given key | ||
108 | from the database. | ||
109 | */ | ||
110 | |||
111 | int mail_cache_db_get(struct mail_cache_db * cache_db, | ||
112 | const void * key, size_t key_len, void ** pvalue, size_t * pvalue_len); | ||
113 | |||
114 | /* | ||
115 | mail_cache_db_del() | ||
116 | |||
117 | This function will delete the given key and the corresponding value | ||
118 | from the database. | ||
119 | */ | ||
120 | |||
121 | int mail_cache_db_del(struct mail_cache_db * cache_db, | ||
122 | const void * key, size_t key_len); | ||
123 | |||
124 | /* | ||
125 | mail_cache_clean_up() | ||
126 | |||
127 | This function will delete the key all the key/value pairs of the | ||
128 | database file which key does not exist in the given hash. | ||
129 | */ | ||
130 | |||
131 | int mail_cache_db_clean_up(struct mail_cache_db * cache_db, | ||
132 | chash * exist); | ||
133 | |||
134 | #ifdef __cplusplus | ||
135 | } | ||
136 | #endif | ||
137 | |||
138 | #endif | ||
diff --git a/kmicromail/libetpan/tools/mail_cache_db_types.h b/kmicromail/libetpan/tools/mail_cache_db_types.h new file mode 100644 index 0000000..f346c5f --- a/dev/null +++ b/kmicromail/libetpan/tools/mail_cache_db_types.h | |||
@@ -0,0 +1,52 @@ | |||
1 | /* | ||
2 | * libEtPan! -- a mail stuff library | ||
3 | * | ||
4 | * Copyright (C) 2001, 2002 - DINH Viet Hoa | ||
5 | * All rights reserved. | ||
6 | * | ||
7 | * Redistribution and use in source and binary forms, with or without | ||
8 | * modification, are permitted provided that the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. Neither the name of the libEtPan! project nor the names of its | ||
16 | * contributors may be used to endorse or promote products derived | ||
17 | * from this software without specific prior written permission. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
29 | * SUCH DAMAGE. | ||
30 | */ | ||
31 | |||
32 | /* | ||
33 | * $Id$ | ||
34 | */ | ||
35 | |||
36 | #ifndef MAIL_CACHE_DB_TYPES_H | ||
37 | |||
38 | #define MAIL_CACHE_DB_TYPES_H | ||
39 | |||
40 | #ifdef __cplusplus | ||
41 | extern "C" { | ||
42 | #endif | ||
43 | |||
44 | struct mail_cache_db { | ||
45 | void * internal_database; | ||
46 | }; | ||
47 | |||
48 | #ifdef __cplusplus | ||
49 | } | ||
50 | #endif | ||
51 | |||
52 | #endif | ||
diff --git a/kmicromail/libetpan/tools/maillock.c b/kmicromail/libetpan/tools/maillock.c new file mode 100644 index 0000000..6128f34 --- a/dev/null +++ b/kmicromail/libetpan/tools/maillock.c | |||
@@ -0,0 +1,286 @@ | |||
1 | /* | ||
2 | * libEtPan! -- a mail stuff library | ||
3 | * | ||
4 | * Copyright (C) 2001, 2002 - DINH Viet Hoa | ||
5 | * All rights reserved. | ||
6 | * | ||
7 | * Redistribution and use in source and binary forms, with or without | ||
8 | * modification, are permitted provided that the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. Neither the name of the libEtPan! project nor the names of its | ||
16 | * contributors may be used to endorse or promote products derived | ||
17 | * from this software without specific prior written permission. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
29 | * SUCH DAMAGE. | ||
30 | */ | ||
31 | |||
32 | /* | ||
33 | * $Id $ | ||
34 | */ | ||
35 | |||
36 | #include "maillock.h" | ||
37 | |||
38 | #include "libetpan-config.h" | ||
39 | |||
40 | #include <sys/types.h> | ||
41 | #include <sys/stat.h> | ||
42 | #include <fcntl.h> | ||
43 | #include <unistd.h> | ||
44 | #include <stdio.h> | ||
45 | #include <time.h> | ||
46 | #include <string.h> | ||
47 | |||
48 | /* ********************************************************************** */ | ||
49 | |||
50 | /* lock primitives */ | ||
51 | |||
52 | /* the lock code is modified from the dot lock file code from mail.local.c */ | ||
53 | |||
54 | /* | ||
55 | SENDMAIL LICENSE | ||
56 | |||
57 | The following license terms and conditions apply, unless a different | ||
58 | license is obtained from Sendmail, Inc., 6425 Christie Ave, Fourth Floor, | ||
59 | Emeryville, CA 94608, or by electronic mail at license@sendmail.com. | ||
60 | |||
61 | License Terms: | ||
62 | |||
63 | Use, Modification and Redistribution (including distribution of any | ||
64 | modified or derived work) in source and binary forms is permitted only if | ||
65 | each of the following conditions is met: | ||
66 | |||
67 | 1. Redistributions qualify as "freeware" or "Open Source Software" under | ||
68 | one of the following terms: | ||
69 | |||
70 | (a) Redistributions are made at no charge beyond the reasonable cost of | ||
71 | materials and delivery. | ||
72 | |||
73 | (b) Redistributions are accompanied by a copy of the Source Code or by an | ||
74 | irrevocable offer to provide a copy of the Source Code for up to three | ||
75 | years at the cost of materials and delivery. Such redistributions | ||
76 | must allow further use, modification, and redistribution of the Source | ||
77 | Code under substantially the same terms as this license. For the | ||
78 | purposes of redistribution "Source Code" means the complete compilable | ||
79 | and linkable source code of sendmail including all modifications. | ||
80 | |||
81 | 2. Redistributions of source code must retain the copyright notices as they | ||
82 | appear in each source code file, these license terms, and the | ||
83 | disclaimer/limitation of liability set forth as paragraph 6 below. | ||
84 | |||
85 | 3. Redistributions in binary form must reproduce the Copyright Notice, | ||
86 | these license terms, and the disclaimer/limitation of liability set | ||
87 | forth as paragraph 6 below, in the documentation and/or other materials | ||
88 | provided with the distribution. For the purposes of binary distribution | ||
89 | the "Copyright Notice" refers to the following language: | ||
90 | "Copyright (c) 1998-2002 Sendmail, Inc. All rights reserved." | ||
91 | |||
92 | 4. Neither the name of Sendmail, Inc. nor the University of California nor | ||
93 | the names of their contributors may be used to endorse or promote | ||
94 | products derived from this software without specific prior written | ||
95 | permission. The name "sendmail" is a trademark of Sendmail, Inc. | ||
96 | |||
97 | 5. All redistributions must comply with the conditions imposed by the | ||
98 | University of California on certain embedded code, whose copyright | ||
99 | notice and conditions for redistribution are as follows: | ||
100 | |||
101 | (a) Copyright (c) 1988, 1993 The Regents of the University of | ||
102 | California. All rights reserved. | ||
103 | |||
104 | (b) Redistribution and use in source and binary forms, with or without | ||
105 | modification, are permitted provided that the following conditions | ||
106 | are met: | ||
107 | |||
108 | (i) Redistributions of source code must retain the above copyright | ||
109 | notice, this list of conditions and the following disclaimer. | ||
110 | |||
111 | (ii) Redistributions in binary form must reproduce the above | ||
112 | copyright notice, this list of conditions and the following | ||
113 | disclaimer in the documentation and/or other materials provided | ||
114 | with the distribution. | ||
115 | |||
116 | (iii) Neither the name of the University nor the names of its | ||
117 | contributors may be used to endorse or promote products derived | ||
118 | from this software without specific prior written permission. | ||
119 | |||
120 | 6. Disclaimer/Limitation of Liability: THIS SOFTWARE IS PROVIDED BY | ||
121 | SENDMAIL, INC. AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED | ||
122 | WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | ||
123 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN | ||
124 | NO EVENT SHALL SENDMAIL, INC., THE REGENTS OF THE UNIVERSITY OF | ||
125 | CALIFORNIA OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
126 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
127 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF | ||
128 | USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON | ||
129 | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
130 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | ||
131 | THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. | ||
132 | */ | ||
133 | |||
134 | /* | ||
135 | TODO : lock, prefer fcntl() over flock() | ||
136 | AND use dotlock code above | ||
137 | */ | ||
138 | |||
139 | #define LOCKTO_RM 300/* timeout for stale lockfile removal */ | ||
140 | #define LOCKTO_GLOB 400/* global timeout for lockfile creation */ | ||
141 | |||
142 | static int lock_common(const char * filename, int fd, short locktype) | ||
143 | { | ||
144 | char lockfilename[PATH_MAX]; | ||
145 | struct flock lock; | ||
146 | /* dot lock file */ | ||
147 | int statfailed = 0; | ||
148 | time_t start; | ||
149 | int r; | ||
150 | int res; | ||
151 | |||
152 | lock.l_start = 0; | ||
153 | lock.l_len = 0; | ||
154 | lock.l_pid = getpid(); | ||
155 | lock.l_type = locktype; | ||
156 | lock.l_whence = SEEK_SET; | ||
157 | |||
158 | r = fcntl(fd, F_SETLKW, &lock); | ||
159 | if (r < 0) { | ||
160 | /* WARNING POSIX lock could not be applied */ | ||
161 | } | ||
162 | |||
163 | /* dot lock file */ | ||
164 | |||
165 | if (strlen(filename) + 6 > PATH_MAX) { | ||
166 | res = -1; | ||
167 | goto unlock; | ||
168 | } | ||
169 | |||
170 | snprintf(lockfilename, PATH_MAX, "%s.lock", filename); | ||
171 | |||
172 | time(&start); | ||
173 | while (1) { | ||
174 | int fd; | ||
175 | struct stat st; | ||
176 | time_t now; | ||
177 | |||
178 | /* global timeout */ | ||
179 | time(&now); | ||
180 | if (now > start + LOCKTO_GLOB) { | ||
181 | res = -1; | ||
182 | goto unlock; | ||
183 | } | ||
184 | |||
185 | fd = open(lockfilename, O_WRONLY|O_EXCL|O_CREAT, 0); | ||
186 | if (fd >= 0) { | ||
187 | /* defeat lock checking programs which test pid */ | ||
188 | write(fd, "0", 2); | ||
189 | close(fd); | ||
190 | break; | ||
191 | } | ||
192 | |||
193 | /* libEtPan! - adds a delay of 5 seconds between each tries */ | ||
194 | sleep(5); | ||
195 | |||
196 | if (stat(lockfilename, &st) < 0) { | ||
197 | if (statfailed++ > 5) { | ||
198 | res = -1; | ||
199 | goto unlock; | ||
200 | } | ||
201 | continue; | ||
202 | } | ||
203 | statfailed = 0; | ||
204 | time(&now); | ||
205 | |||
206 | if (now < st.st_ctime + LOCKTO_RM) | ||
207 | continue; | ||
208 | |||
209 | /* try to remove stale lockfile */ | ||
210 | if (unlink(lockfilename) < 0) { | ||
211 | res = -1; | ||
212 | goto unlock; | ||
213 | } | ||
214 | |||
215 | /* | ||
216 | libEtPan! - removes this delay of 5 seconds, | ||
217 | maybe it was misplaced ? | ||
218 | */ | ||
219 | #if 0 | ||
220 | sleep(5); | ||
221 | #endif | ||
222 | } | ||
223 | |||
224 | return 0; | ||
225 | |||
226 | unlock: | ||
227 | lock.l_start = 0; | ||
228 | lock.l_len = 0; | ||
229 | lock.l_pid = getpid(); | ||
230 | lock.l_type = F_UNLCK; | ||
231 | lock.l_whence = SEEK_SET; | ||
232 | |||
233 | r = fcntl(fd, F_SETLK, &lock); | ||
234 | if (r < 0) { | ||
235 | /* WARNING POSIX lock could not be applied */ | ||
236 | } | ||
237 | err: | ||
238 | return res; | ||
239 | } | ||
240 | |||
241 | static int unlock_common(const char * filename, int fd) | ||
242 | { | ||
243 | char lockfilename[PATH_MAX]; | ||
244 | struct flock lock; | ||
245 | int r; | ||
246 | |||
247 | if (strlen(filename) + 6 > PATH_MAX) | ||
248 | return -1; | ||
249 | |||
250 | snprintf(lockfilename, PATH_MAX, "%s.lock", filename); | ||
251 | |||
252 | unlink(lockfilename); | ||
253 | |||
254 | lock.l_start = 0; | ||
255 | lock.l_len = 0; | ||
256 | lock.l_pid = getpid(); | ||
257 | lock.l_type = F_UNLCK; | ||
258 | lock.l_whence = SEEK_SET; | ||
259 | |||
260 | r = fcntl(fd, F_SETLK, &lock); | ||
261 | if (r < 0) { | ||
262 | /* WARNING POSIX lock could not be applied */ | ||
263 | } | ||
264 | |||
265 | return 0; | ||
266 | } | ||
267 | |||
268 | int maillock_read_lock(const char * filename, int fd) | ||
269 | { | ||
270 | return lock_common(filename, fd, F_RDLCK); | ||
271 | } | ||
272 | |||
273 | int maillock_read_unlock(const char * filename, int fd) | ||
274 | { | ||
275 | return unlock_common(filename, fd); | ||
276 | } | ||
277 | |||
278 | int maillock_write_lock(const char * filename, int fd) | ||
279 | { | ||
280 | return lock_common(filename, fd, F_WRLCK); | ||
281 | } | ||
282 | |||
283 | int maillock_write_unlock(const char * filename, int fd) | ||
284 | { | ||
285 | return unlock_common(filename, fd); | ||
286 | } | ||
diff --git a/kmicromail/libetpan/tools/maillock.h b/kmicromail/libetpan/tools/maillock.h new file mode 100644 index 0000000..2f64e35 --- a/dev/null +++ b/kmicromail/libetpan/tools/maillock.h | |||
@@ -0,0 +1,53 @@ | |||
1 | /* | ||
2 | * libEtPan! -- a mail stuff library | ||
3 | * | ||
4 | * Copyright (C) 2001, 2002 - DINH Viet Hoa | ||
5 | * All rights reserved. | ||
6 | * | ||
7 | * Redistribution and use in source and binary forms, with or without | ||
8 | * modification, are permitted provided that the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. Neither the name of the libEtPan! project nor the names of its | ||
16 | * contributors may be used to endorse or promote products derived | ||
17 | * from this software without specific prior written permission. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
29 | * SUCH DAMAGE. | ||
30 | */ | ||
31 | |||
32 | /* | ||
33 | * $Id$ | ||
34 | */ | ||
35 | |||
36 | #ifndef MAILLOCK_H | ||
37 | |||
38 | #define MAILLOCK_H | ||
39 | |||
40 | #ifdef __cplusplus | ||
41 | extern "C" { | ||
42 | #endif | ||
43 | |||
44 | int maillock_read_lock(const char * filename, int fd); | ||
45 | int maillock_read_unlock(const char * filename, int fd); | ||
46 | int maillock_write_lock(const char * filename, int fd); | ||
47 | int maillock_write_unlock(const char * filename, int fd); | ||
48 | |||
49 | #ifdef __cplusplus | ||
50 | } | ||
51 | #endif | ||
52 | |||
53 | #endif | ||
diff --git a/kmicromail/libetpan/tools/mailstream.c b/kmicromail/libetpan/tools/mailstream.c new file mode 100644 index 0000000..0f55e67 --- a/dev/null +++ b/kmicromail/libetpan/tools/mailstream.c | |||
@@ -0,0 +1,394 @@ | |||
1 | /* | ||
2 | * libEtPan! -- a mail stuff library | ||
3 | * | ||
4 | * Copyright (C) 2001, 2002 - DINH Viet Hoa | ||
5 | * All rights reserved. | ||
6 | * | ||
7 | * Redistribution and use in source and binary forms, with or without | ||
8 | * modification, are permitted provided that the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. Neither the name of the libEtPan! project nor the names of its | ||
16 | * contributors may be used to endorse or promote products derived | ||
17 | * from this software without specific prior written permission. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
29 | * SUCH DAMAGE. | ||
30 | */ | ||
31 | |||
32 | /* | ||
33 | * $Id$ | ||
34 | */ | ||
35 | |||
36 | #include "mailstream.h" | ||
37 | #include "maillock.h" | ||
38 | #include <string.h> | ||
39 | #include <stdlib.h> | ||
40 | #include <sys/types.h> | ||
41 | #include <sys/stat.h> | ||
42 | |||
43 | #define DEFAULT_NETWORK_TIMEOUT 300 | ||
44 | |||
45 | #ifdef LIBETPAN_MAILSTREAM_DEBUG | ||
46 | |||
47 | #define STREAM_DEBUG | ||
48 | |||
49 | #include <stdio.h> | ||
50 | |||
51 | #define LOG_FILE "libetpan-stream-debug.log" | ||
52 | |||
53 | int mailstream_debug = 0; | ||
54 | |||
55 | #define STREAM_LOG_BUF(buf, size) \ | ||
56 | if (mailstream_debug) { \ | ||
57 | FILE * f; \ | ||
58 | mode_t old_mask; \ | ||
59 | \ | ||
60 | old_mask = umask(0077); \ | ||
61 | f = fopen(LOG_FILE, "a"); \ | ||
62 | umask(old_mask); \ | ||
63 | if (f != NULL) { \ | ||
64 | maillock_write_lock(LOG_FILE, fileno(f)); \ | ||
65 | fwrite((buf), 1, (size), f); \ | ||
66 | maillock_write_unlock(LOG_FILE, fileno(f)); \ | ||
67 | fclose(f); \ | ||
68 | } \ | ||
69 | } | ||
70 | |||
71 | #define STREAM_LOG(str) \ | ||
72 | if (mailstream_debug) { \ | ||
73 | FILE * f; \ | ||
74 | mode_t old_mask; \ | ||
75 | \ | ||
76 | old_mask = umask(0077); \ | ||
77 | f = fopen(LOG_FILE, "a"); \ | ||
78 | umask(old_mask); \ | ||
79 | if (f != NULL) { \ | ||
80 | maillock_write_lock(LOG_FILE, fileno(f)); \ | ||
81 | fputs((str), f); \ | ||
82 | maillock_write_unlock(LOG_FILE, fileno(f)); \ | ||
83 | fclose(f); \ | ||
84 | } \ | ||
85 | } | ||
86 | |||
87 | #else | ||
88 | |||
89 | #define STREAM_LOG_BUF(buf, size) do { } while (0) | ||
90 | #define STREAM_LOG(buf) do { } while (0) | ||
91 | |||
92 | #endif | ||
93 | |||
94 | |||
95 | mailstream * mailstream_new(mailstream_low * low, size_t buffer_size) | ||
96 | { | ||
97 | mailstream * s; | ||
98 | |||
99 | s = malloc(sizeof(* s)); | ||
100 | if (s == NULL) | ||
101 | goto err; | ||
102 | |||
103 | s->read_buffer = malloc(buffer_size); | ||
104 | if (s->read_buffer == NULL) | ||
105 | goto free_s; | ||
106 | s->read_buffer_len = 0; | ||
107 | |||
108 | s->write_buffer = malloc(buffer_size); | ||
109 | if (s->write_buffer == NULL) | ||
110 | goto free_read_buffer; | ||
111 | s->write_buffer_len = 0; | ||
112 | |||
113 | s->buffer_max_size = buffer_size; | ||
114 | s->low = low; | ||
115 | |||
116 | return s; | ||
117 | |||
118 | free_read_buffer: | ||
119 | free(s->read_buffer); | ||
120 | free_s: | ||
121 | free(s); | ||
122 | err: | ||
123 | return NULL; | ||
124 | } | ||
125 | |||
126 | static size_t write_to_internal_buffer(mailstream * s, | ||
127 | const void * buf, size_t count) | ||
128 | { | ||
129 | memcpy(s->write_buffer + s->write_buffer_len, buf, count); | ||
130 | s->write_buffer_len += count; | ||
131 | |||
132 | return count; | ||
133 | } | ||
134 | |||
135 | static size_t write_direct(mailstream * s, const void * buf, size_t count) | ||
136 | { | ||
137 | size_t left; | ||
138 | const char * cur_buf; | ||
139 | ssize_t written; | ||
140 | |||
141 | cur_buf = buf; | ||
142 | left = count; | ||
143 | while (left > 0) { | ||
144 | written = mailstream_low_write(s->low, cur_buf, left); | ||
145 | |||
146 | if (written == -1) { | ||
147 | if (count == left) | ||
148 | return -1; | ||
149 | else | ||
150 | return count - left; | ||
151 | } | ||
152 | |||
153 | cur_buf += written; | ||
154 | left -= written; | ||
155 | } | ||
156 | |||
157 | return count; | ||
158 | } | ||
159 | |||
160 | ssize_t mailstream_write(mailstream * s, const void * buf, size_t count) | ||
161 | { | ||
162 | int r; | ||
163 | |||
164 | if (s == NULL) | ||
165 | return -1; | ||
166 | |||
167 | if (count + s->write_buffer_len > s->buffer_max_size) { | ||
168 | r = mailstream_flush(s); | ||
169 | if (r == -1) | ||
170 | return -1; | ||
171 | |||
172 | if (count > s->buffer_max_size) | ||
173 | return write_direct(s, buf, count); | ||
174 | } | ||
175 | |||
176 | #ifdef STREAM_DEBUG | ||
177 | STREAM_LOG(">>>>>>> send >>>>>>\n"); | ||
178 | STREAM_LOG_BUF(buf, count); | ||
179 | STREAM_LOG("\n"); | ||
180 | STREAM_LOG(">>>>>>> end send >>>>>>\n"); | ||
181 | #endif | ||
182 | |||
183 | return write_to_internal_buffer(s, buf, count); | ||
184 | } | ||
185 | |||
186 | int mailstream_flush(mailstream * s) | ||
187 | { | ||
188 | char * cur_buf; | ||
189 | size_t left; | ||
190 | ssize_t written; | ||
191 | |||
192 | if (s == NULL) | ||
193 | return -1; | ||
194 | |||
195 | cur_buf = s->write_buffer; | ||
196 | left = s->write_buffer_len; | ||
197 | while (left > 0) { | ||
198 | written = mailstream_low_write(s->low, cur_buf, left); | ||
199 | |||
200 | if (written == -1) | ||
201 | goto move_buffer; | ||
202 | cur_buf += written; | ||
203 | left -= written; | ||
204 | } | ||
205 | |||
206 | s->write_buffer_len = 0; | ||
207 | |||
208 | return 0; | ||
209 | |||
210 | move_buffer: | ||
211 | memmove(s->write_buffer, cur_buf, left); | ||
212 | s->write_buffer_len = left; | ||
213 | return -1; | ||
214 | } | ||
215 | |||
216 | static ssize_t read_from_internal_buffer(mailstream * s, | ||
217 | void * buf, size_t count) | ||
218 | { | ||
219 | if (count >= s->read_buffer_len) | ||
220 | count = s->read_buffer_len; | ||
221 | if (count != 0) | ||
222 | memcpy(buf, s->read_buffer, count); | ||
223 | |||
224 | s->read_buffer_len -= count; | ||
225 | if (s->read_buffer_len != 0) | ||
226 | memmove(s->read_buffer, s->read_buffer + count, | ||
227 | s->read_buffer_len); | ||
228 | |||
229 | return count; | ||
230 | } | ||
231 | |||
232 | static ssize_t read_through_buffer(mailstream * s, void * buf, size_t count) | ||
233 | { | ||
234 | size_t left; | ||
235 | char * cur_buf; | ||
236 | ssize_t bytes_read; | ||
237 | |||
238 | cur_buf = buf; | ||
239 | left = count; | ||
240 | |||
241 | while (left > 0) { | ||
242 | bytes_read = mailstream_low_read(s->low, cur_buf, left); | ||
243 | |||
244 | if (bytes_read == -1) { | ||
245 | if (count == left) | ||
246 | return -1; | ||
247 | else | ||
248 | return count - left; | ||
249 | } | ||
250 | else if (bytes_read == 0) | ||
251 | return count - left; | ||
252 | |||
253 | cur_buf += bytes_read; | ||
254 | left -= bytes_read; | ||
255 | } | ||
256 | |||
257 | return count; | ||
258 | } | ||
259 | |||
260 | ssize_t mailstream_read(mailstream * s, void * buf, size_t count) | ||
261 | { | ||
262 | ssize_t read_bytes; | ||
263 | char * cur_buf; | ||
264 | size_t left; | ||
265 | |||
266 | if (s == NULL) | ||
267 | return -1; | ||
268 | |||
269 | left = count; | ||
270 | cur_buf = buf; | ||
271 | read_bytes = read_from_internal_buffer(s, cur_buf, left); | ||
272 | cur_buf += read_bytes; | ||
273 | left -= read_bytes; | ||
274 | |||
275 | if (left == 0) { | ||
276 | #ifdef STREAM_DEBUG | ||
277 | STREAM_LOG("<<<<<<< read <<<<<<\n"); | ||
278 | STREAM_LOG_BUF(buf, read_bytes); | ||
279 | STREAM_LOG("\n"); | ||
280 | STREAM_LOG("<<<<<<< end read <<<<<<\n"); | ||
281 | #endif | ||
282 | |||
283 | return read_bytes; | ||
284 | } | ||
285 | |||
286 | if (left > s->buffer_max_size) { | ||
287 | read_bytes = read_through_buffer(s, cur_buf, left); | ||
288 | if (read_bytes == -1) { | ||
289 | if (count == left) | ||
290 | return -1; | ||
291 | else { | ||
292 | |||
293 | #ifdef STREAM_DEBUG | ||
294 | STREAM_LOG("<<<<<<< read <<<<<<\n"); | ||
295 | STREAM_LOG_BUF(buf, count - left); | ||
296 | STREAM_LOG("\n"); | ||
297 | STREAM_LOG("<<<<<<< end read <<<<<<\n"); | ||
298 | #endif | ||
299 | |||
300 | return count - left; | ||
301 | } | ||
302 | } | ||
303 | |||
304 | cur_buf += read_bytes; | ||
305 | left -= read_bytes; | ||
306 | |||
307 | #ifdef STREAM_DEBUG | ||
308 | STREAM_LOG("<<<<<<< read <<<<<<\n"); | ||
309 | STREAM_LOG_BUF(buf, count - left); | ||
310 | STREAM_LOG("\n"); | ||
311 | STREAM_LOG("<<<<<<< end read <<<<<<\n"); | ||
312 | #endif | ||
313 | |||
314 | return count - left; | ||
315 | } | ||
316 | |||
317 | read_bytes = mailstream_low_read(s->low, s->read_buffer, s->buffer_max_size); | ||
318 | if (read_bytes == -1) { | ||
319 | if (left == count) | ||
320 | return -1; | ||
321 | else { | ||
322 | #ifdef STREAM_DEBUG | ||
323 | STREAM_LOG("<<<<<<< read <<<<<<\n"); | ||
324 | STREAM_LOG_BUF(buf, count - left); | ||
325 | STREAM_LOG("\n"); | ||
326 | STREAM_LOG("<<<<<<< end read <<<<<<\n"); | ||
327 | #endif | ||
328 | |||
329 | return count - left; | ||
330 | } | ||
331 | } | ||
332 | else | ||
333 | s->read_buffer_len += read_bytes; | ||
334 | |||
335 | read_bytes = read_from_internal_buffer(s, cur_buf, left); | ||
336 | cur_buf += read_bytes; | ||
337 | left -= read_bytes; | ||
338 | |||
339 | #ifdef STREAM_DEBUG | ||
340 | STREAM_LOG("<<<<<<< read <<<<<<\n"); | ||
341 | STREAM_LOG_BUF(buf, count - left); | ||
342 | STREAM_LOG("\n"); | ||
343 | STREAM_LOG("<<<<<<< end read <<<<<<\n"); | ||
344 | #endif | ||
345 | |||
346 | return count - left; | ||
347 | } | ||
348 | |||
349 | mailstream_low * mailstream_get_low(mailstream * s) | ||
350 | { | ||
351 | return s->low; | ||
352 | } | ||
353 | |||
354 | void mailstream_set_low(mailstream * s, mailstream_low * low) | ||
355 | { | ||
356 | s->low = low; | ||
357 | } | ||
358 | |||
359 | int mailstream_close(mailstream * s) | ||
360 | { | ||
361 | mailstream_low_close(s->low); | ||
362 | mailstream_low_free(s->low); | ||
363 | |||
364 | free(s->read_buffer); | ||
365 | free(s->write_buffer); | ||
366 | |||
367 | free(s); | ||
368 | |||
369 | return 0; | ||
370 | } | ||
371 | |||
372 | |||
373 | |||
374 | ssize_t mailstream_feed_read_buffer(mailstream * s) | ||
375 | { | ||
376 | ssize_t read_bytes; | ||
377 | |||
378 | if (s == NULL) | ||
379 | return -1; | ||
380 | |||
381 | if (s->read_buffer_len == 0) { | ||
382 | read_bytes = mailstream_low_read(s->low, s->read_buffer, | ||
383 | s->buffer_max_size); | ||
384 | if (read_bytes == -1) | ||
385 | return -1; | ||
386 | s->read_buffer_len += read_bytes; | ||
387 | } | ||
388 | |||
389 | return s->read_buffer_len; | ||
390 | } | ||
391 | |||
392 | struct timeval mailstream_network_delay = | ||
393 | { .tv_sec = DEFAULT_NETWORK_TIMEOUT, .tv_usec = 0 }; | ||
394 | |||
diff --git a/kmicromail/libetpan/tools/mailstream.h b/kmicromail/libetpan/tools/mailstream.h new file mode 100644 index 0000000..a4e35cd --- a/dev/null +++ b/kmicromail/libetpan/tools/mailstream.h | |||
@@ -0,0 +1,73 @@ | |||
1 | /* | ||
2 | * libEtPan! -- a mail stuff library | ||
3 | * | ||
4 | * Copyright (C) 2001, 2002 - DINH Viet Hoa | ||
5 | * All rights reserved. | ||
6 | * | ||
7 | * Redistribution and use in source and binary forms, with or without | ||
8 | * modification, are permitted provided that the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. Neither the name of the libEtPan! project nor the names of its | ||
16 | * contributors may be used to endorse or promote products derived | ||
17 | * from this software without specific prior written permission. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
29 | * SUCH DAMAGE. | ||
30 | */ | ||
31 | |||
32 | /* | ||
33 | * $Id$ | ||
34 | */ | ||
35 | |||
36 | #ifndef MAILSTREAM_H | ||
37 | |||
38 | #define MAILSTREAM_H | ||
39 | |||
40 | #include <sys/time.h> | ||
41 | |||
42 | #include <libetpan/mailstream_low.h> | ||
43 | #include <libetpan/mailstream_helper.h> | ||
44 | #include <libetpan/mailstream_socket.h> | ||
45 | #include <libetpan/mailstream_ssl.h> | ||
46 | #include <libetpan/mailstream_types.h> | ||
47 | |||
48 | #ifdef __cplusplus | ||
49 | extern "C" { | ||
50 | #endif | ||
51 | |||
52 | mailstream * mailstream_new(mailstream_low * low, size_t buffer_size); | ||
53 | ssize_t mailstream_write(mailstream * s, const void * buf, size_t count); | ||
54 | ssize_t mailstream_read(mailstream * s, void * buf, size_t count); | ||
55 | int mailstream_close(mailstream * s); | ||
56 | int mailstream_flush(mailstream * s); | ||
57 | ssize_t mailstream_feed_read_buffer(mailstream * s); | ||
58 | mailstream_low * mailstream_get_low(mailstream * s); | ||
59 | void mailstream_set_low(mailstream * s, mailstream_low * low); | ||
60 | |||
61 | #ifdef LIBETPAN_MAILSTREAM_DEBUG | ||
62 | extern int mailstream_debug; | ||
63 | #endif | ||
64 | |||
65 | #define LIBETPAN_MAILSTREAM_NETWORK_DELAY | ||
66 | extern struct timeval mailstream_network_delay; | ||
67 | |||
68 | #ifdef __cplusplus | ||
69 | } | ||
70 | #endif | ||
71 | |||
72 | #endif | ||
73 | |||
diff --git a/kmicromail/libetpan/tools/mailstream_helper.c b/kmicromail/libetpan/tools/mailstream_helper.c new file mode 100644 index 0000000..146f955 --- a/dev/null +++ b/kmicromail/libetpan/tools/mailstream_helper.c | |||
@@ -0,0 +1,383 @@ | |||
1 | /* | ||
2 | * libEtPan! -- a mail stuff library | ||
3 | * | ||
4 | * Copyright (C) 2001, 2002 - DINH Viet Hoa | ||
5 | * All rights reserved. | ||
6 | * | ||
7 | * Redistribution and use in source and binary forms, with or without | ||
8 | * modification, are permitted provided that the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. Neither the name of the libEtPan! project nor the names of its | ||
16 | * contributors may be used to endorse or promote products derived | ||
17 | * from this software without specific prior written permission. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
29 | * SUCH DAMAGE. | ||
30 | */ | ||
31 | |||
32 | /* | ||
33 | * $Id$ | ||
34 | */ | ||
35 | |||
36 | #include "mailstream_helper.h" | ||
37 | #include <string.h> | ||
38 | #include <stdio.h> | ||
39 | #include "mail.h" | ||
40 | |||
41 | static void remove_trailing_eol(MMAPString * mmapstr) | ||
42 | { | ||
43 | if (mmapstr->str[mmapstr->len - 1] == '\n') { | ||
44 | mmapstr->len --; | ||
45 | mmapstr->str[mmapstr->len] = '\0'; | ||
46 | } | ||
47 | if (mmapstr->str[mmapstr->len - 1] == '\r') { | ||
48 | mmapstr->len --; | ||
49 | mmapstr->str[mmapstr->len] = '\0'; | ||
50 | } | ||
51 | } | ||
52 | |||
53 | char * mailstream_read_line(mailstream * stream, MMAPString * line) | ||
54 | { | ||
55 | if (mmap_string_assign(line, "") == NULL) | ||
56 | return NULL; | ||
57 | |||
58 | return mailstream_read_line_append(stream, line); | ||
59 | } | ||
60 | |||
61 | static char * mailstream_read_len_append(mailstream * stream, | ||
62 | MMAPString * line, | ||
63 | size_t i) | ||
64 | { | ||
65 | size_t cur_size; | ||
66 | |||
67 | cur_size = line->len; | ||
68 | if (mmap_string_set_size(line, line->len + i) == NULL) | ||
69 | return NULL; | ||
70 | if (mailstream_read(stream, line->str + cur_size, i) < 0) | ||
71 | return NULL; | ||
72 | return line->str; | ||
73 | } | ||
74 | |||
75 | char * mailstream_read_line_append(mailstream * stream, MMAPString * line) | ||
76 | { | ||
77 | if (stream == NULL) | ||
78 | return NULL; | ||
79 | |||
80 | do { | ||
81 | if (stream->read_buffer_len > 0) { | ||
82 | size_t i; | ||
83 | |||
84 | i = 0; | ||
85 | while (i < stream->read_buffer_len) { | ||
86 | if (stream->read_buffer[i] == '\n') | ||
87 | return mailstream_read_len_append(stream, line, i + 1); | ||
88 | i++; | ||
89 | } | ||
90 | if (mailstream_read_len_append(stream, line, | ||
91 | stream->read_buffer_len) == NULL) | ||
92 | return NULL; | ||
93 | } | ||
94 | else { | ||
95 | ssize_t r; | ||
96 | |||
97 | r = mailstream_feed_read_buffer(stream); | ||
98 | if (r == -1) | ||
99 | return NULL; | ||
100 | |||
101 | if (r == 0) | ||
102 | break; | ||
103 | } | ||
104 | } | ||
105 | while (1); | ||
106 | |||
107 | return line->str; | ||
108 | } | ||
109 | |||
110 | char * mailstream_read_line_remove_eol(mailstream * stream, MMAPString * line) | ||
111 | { | ||
112 | if (!mailstream_read_line(stream, line)) | ||
113 | return NULL; | ||
114 | |||
115 | remove_trailing_eol(line); | ||
116 | |||
117 | return line->str; | ||
118 | } | ||
119 | |||
120 | int mailstream_is_end_multiline(const char * line) | ||
121 | { | ||
122 | if (line[0] != '.') | ||
123 | return FALSE; | ||
124 | if (line[1] != 0) | ||
125 | return FALSE; | ||
126 | return TRUE; | ||
127 | } | ||
128 | |||
129 | #if 1 | ||
130 | char * mailstream_read_multiline(mailstream * s, size_t size, | ||
131 | MMAPString * stream_buffer, | ||
132 | MMAPString * multiline_buffer, | ||
133 | size_t progr_rate, | ||
134 | progress_function * progr_fun) | ||
135 | { | ||
136 | size_t count; | ||
137 | char * line; | ||
138 | size_t last; | ||
139 | |||
140 | if (mmap_string_assign(multiline_buffer, "") == NULL) | ||
141 | return NULL; | ||
142 | |||
143 | count = 0; | ||
144 | last = 0; | ||
145 | |||
146 | while ((line = mailstream_read_line_remove_eol(s, stream_buffer)) != NULL) { | ||
147 | if (mailstream_is_end_multiline(line)) | ||
148 | return multiline_buffer->str; | ||
149 | |||
150 | if (line[0] == '.') { | ||
151 | if (mmap_string_append(multiline_buffer, line + 1) == NULL) | ||
152 | return NULL; | ||
153 | } | ||
154 | else { | ||
155 | if (mmap_string_append(multiline_buffer, line) == NULL) | ||
156 | return NULL; | ||
157 | } | ||
158 | if (mmap_string_append(multiline_buffer, "\r\n") == NULL) | ||
159 | return NULL; | ||
160 | |||
161 | count += strlen(line); | ||
162 | if ((size != 0) && (progr_rate != 0) && (progr_fun != NULL)) | ||
163 | if (count - last >= progr_rate) { | ||
164 | (* progr_fun)(count, size); | ||
165 | last = count; | ||
166 | } | ||
167 | } | ||
168 | |||
169 | return NULL; | ||
170 | } | ||
171 | |||
172 | #else | ||
173 | |||
174 | /* | ||
175 | high speed but don't replace the line break with '\n' and neither | ||
176 | remove the '.' | ||
177 | */ | ||
178 | |||
179 | static gboolean end_of_multiline(const char * str, gint len) | ||
180 | { | ||
181 | gint index; | ||
182 | |||
183 | index = len - 1; | ||
184 | |||
185 | if (str[index] != '\n') | ||
186 | return FALSE; | ||
187 | if (index == 0) | ||
188 | return FALSE; | ||
189 | |||
190 | index --; | ||
191 | |||
192 | if (str[index] == '\r') { | ||
193 | index --; | ||
194 | if (index == 0) | ||
195 | return FALSE; | ||
196 | } | ||
197 | |||
198 | if (str[index] != '.') | ||
199 | return FALSE; | ||
200 | if (index == 0) | ||
201 | return FALSE; | ||
202 | |||
203 | index--; | ||
204 | |||
205 | if (str[index] != '\n') | ||
206 | return FALSE; | ||
207 | |||
208 | return TRUE; | ||
209 | } | ||
210 | |||
211 | char * mailstream_read_multiline(mailstream * stream, size_t size, | ||
212 | MMAPString * stream_buffer, | ||
213 | MMAPString * line, | ||
214 | size_t progr_rate, | ||
215 | progress_function * progr_fun) | ||
216 | { | ||
217 | if (stream == NULL) | ||
218 | return NULL; | ||
219 | |||
220 | mmap_string_assign(line, ""); | ||
221 | |||
222 | do { | ||
223 | if (stream->read_buffer_len > 0) { | ||
224 | size_t i; | ||
225 | |||
226 | i = 0; | ||
227 | while (i < stream->read_buffer_len) { | ||
228 | if (end_of_multiline(stream->read_buffer, i + 1)) | ||
229 | return mailstream_read_len_append(stream, line, i + 1); | ||
230 | i++; | ||
231 | } | ||
232 | if (mailstream_read_len_append(stream, line, | ||
233 | stream->read_buffer_len) == NULL) | ||
234 | return NULL; | ||
235 | if (end_of_multiline(line->str, line->len)) | ||
236 | return line->str; | ||
237 | } | ||
238 | else | ||
239 | if (mailstream_feed_read_buffer(stream) == -1) | ||
240 | return NULL; | ||
241 | } | ||
242 | while (1); | ||
243 | |||
244 | return line->str; | ||
245 | } | ||
246 | #endif | ||
247 | |||
248 | |||
249 | |||
250 | static ssize_t send_data_line(mailstream * s, const char * line, size_t length) | ||
251 | { | ||
252 | int fix_eol; | ||
253 | const char * start; | ||
254 | size_t count; | ||
255 | |||
256 | start = line; | ||
257 | |||
258 | fix_eol = 0; | ||
259 | count = 0; | ||
260 | |||
261 | while (1) { | ||
262 | if (length == 0) | ||
263 | break; | ||
264 | |||
265 | if (* line == '\r') { | ||
266 | line ++; | ||
267 | |||
268 | count ++; | ||
269 | length --; | ||
270 | |||
271 | if (* line == '\n') { | ||
272 | line ++; | ||
273 | |||
274 | count ++; | ||
275 | length --; | ||
276 | |||
277 | break; | ||
278 | } | ||
279 | } | ||
280 | |||
281 | if (* line == '\n') { | ||
282 | line ++; | ||
283 | |||
284 | count ++; | ||
285 | length --; | ||
286 | |||
287 | fix_eol = 1; | ||
288 | break; | ||
289 | } | ||
290 | |||
291 | line ++; | ||
292 | length --; | ||
293 | count ++; | ||
294 | } | ||
295 | |||
296 | if (start[0] == '.') | ||
297 | if (mailstream_write(s, ".", 1) == -1) | ||
298 | goto err; | ||
299 | |||
300 | if (fix_eol) { | ||
301 | if (mailstream_write(s, start, count - 1) == -1) | ||
302 | goto err; | ||
303 | if (mailstream_write(s, "\r\n", 2) == -1) | ||
304 | goto err; | ||
305 | } | ||
306 | else { | ||
307 | if (mailstream_write(s, start, count) == -1) | ||
308 | goto err; | ||
309 | } | ||
310 | |||
311 | |||
312 | #if 0 | ||
313 | while (* line != '\n') { | ||
314 | if (* line == '\r') | ||
315 | pos = line; | ||
316 | if (* line == '\0') | ||
317 | return line; | ||
318 | if (mailstream_write(s, line, 1) == -1) | ||
319 | goto err; | ||
320 | line ++; | ||
321 | } | ||
322 | if (pos + 1 == line) { | ||
323 | if (mailstream_write(s, line, 1) == -1) | ||
324 | goto err; | ||
325 | } | ||
326 | else { | ||
327 | if (mailstream_write(s, "\r\n", 2) == -1) | ||
328 | goto err; | ||
329 | } | ||
330 | line ++; | ||
331 | #endif | ||
332 | |||
333 | return count; | ||
334 | |||
335 | err: | ||
336 | return -1; | ||
337 | } | ||
338 | |||
339 | int mailstream_send_data(mailstream * s, const char * message, | ||
340 | size_t size, | ||
341 | size_t progr_rate, | ||
342 | progress_function * progr_fun) | ||
343 | { | ||
344 | const char * current; | ||
345 | size_t count; | ||
346 | size_t last; | ||
347 | size_t remaining; | ||
348 | |||
349 | count = 0; | ||
350 | last = 0; | ||
351 | |||
352 | current = message; | ||
353 | remaining = size; | ||
354 | |||
355 | while (remaining > 0) { | ||
356 | ssize_t length; | ||
357 | |||
358 | length = send_data_line(s, current, remaining); | ||
359 | if (length < 0) | ||
360 | goto err; | ||
361 | |||
362 | current += length; | ||
363 | |||
364 | count += length; | ||
365 | if ((progr_rate != 0) && (progr_fun != NULL)) | ||
366 | if (count - last >= progr_rate) { | ||
367 | (* progr_fun)(count, size); | ||
368 | last = count; | ||
369 | } | ||
370 | |||
371 | remaining -= length; | ||
372 | } | ||
373 | if (mailstream_write(s, "\r\n.\r\n", 5) == -1) | ||
374 | goto err; | ||
375 | |||
376 | if (mailstream_flush(s) == -1) | ||
377 | goto err; | ||
378 | |||
379 | return 0; | ||
380 | |||
381 | err: | ||
382 | return -1; | ||
383 | } | ||
diff --git a/kmicromail/libetpan/tools/mailstream_helper.h b/kmicromail/libetpan/tools/mailstream_helper.h new file mode 100644 index 0000000..d030b1d --- a/dev/null +++ b/kmicromail/libetpan/tools/mailstream_helper.h | |||
@@ -0,0 +1,70 @@ | |||
1 | /* | ||
2 | * libEtPan! -- a mail stuff library | ||
3 | * | ||
4 | * Copyright (C) 2001, 2002 - DINH Viet Hoa | ||
5 | * All rights reserved. | ||
6 | * | ||
7 | * Redistribution and use in source and binary forms, with or without | ||
8 | * modification, are permitted provided that the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. Neither the name of the libEtPan! project nor the names of its | ||
16 | * contributors may be used to endorse or promote products derived | ||
17 | * from this software without specific prior written permission. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
29 | * SUCH DAMAGE. | ||
30 | */ | ||
31 | |||
32 | /* | ||
33 | * $Id$ | ||
34 | */ | ||
35 | |||
36 | #ifndef MAILSTREAM_HELPER_H | ||
37 | |||
38 | #define MAILSTREAM_HELPER_H | ||
39 | |||
40 | #include <libetpan/mmapstring.h> | ||
41 | #include <libetpan/mailstream.h> | ||
42 | |||
43 | #ifdef __cplusplus | ||
44 | extern "C" { | ||
45 | #endif | ||
46 | |||
47 | char * mailstream_read_line(mailstream * stream, MMAPString * line); | ||
48 | |||
49 | char * mailstream_read_line_append(mailstream * stream, MMAPString * line); | ||
50 | |||
51 | char * mailstream_read_line_remove_eol(mailstream * stream, MMAPString * line); | ||
52 | |||
53 | char * mailstream_read_multiline(mailstream * s, size_t size, | ||
54 | MMAPString * stream_buffer, | ||
55 | MMAPString * multiline_buffer, | ||
56 | size_t progr_rate, | ||
57 | progress_function * progr_fun); | ||
58 | |||
59 | int mailstream_is_end_multiline(const char * line); | ||
60 | |||
61 | int mailstream_send_data(mailstream * s, const char * message, | ||
62 | size_t size, | ||
63 | size_t progr_rate, | ||
64 | progress_function * progr_fun); | ||
65 | |||
66 | #ifdef __cplusplus | ||
67 | } | ||
68 | #endif | ||
69 | |||
70 | #endif | ||
diff --git a/kmicromail/libetpan/tools/mailstream_low.c b/kmicromail/libetpan/tools/mailstream_low.c new file mode 100644 index 0000000..34c96f1 --- a/dev/null +++ b/kmicromail/libetpan/tools/mailstream_low.c | |||
@@ -0,0 +1,90 @@ | |||
1 | /* | ||
2 | * libEtPan! -- a mail stuff library | ||
3 | * | ||
4 | * Copyright (C) 2001, 2002 - DINH Viet Hoa | ||
5 | * All rights reserved. | ||
6 | * | ||
7 | * Redistribution and use in source and binary forms, with or without | ||
8 | * modification, are permitted provided that the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. Neither the name of the libEtPan! project nor the names of its | ||
16 | * contributors may be used to endorse or promote products derived | ||
17 | * from this software without specific prior written permission. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
29 | * SUCH DAMAGE. | ||
30 | */ | ||
31 | |||
32 | /* | ||
33 | * $Id$ | ||
34 | */ | ||
35 | |||
36 | #include "mailstream_low.h" | ||
37 | #include <stdlib.h> | ||
38 | |||
39 | /* general functions */ | ||
40 | |||
41 | mailstream_low * mailstream_low_new(void * data, | ||
42 | mailstream_low_driver * driver) | ||
43 | { | ||
44 | mailstream_low * s; | ||
45 | |||
46 | s = malloc(sizeof(* s)); | ||
47 | if (s == NULL) | ||
48 | return NULL; | ||
49 | |||
50 | s->data = data; | ||
51 | s->driver = driver; | ||
52 | |||
53 | return s; | ||
54 | } | ||
55 | |||
56 | int mailstream_low_close(mailstream_low * s) | ||
57 | { | ||
58 | if (s == NULL) | ||
59 | return -1; | ||
60 | s->driver->mailstream_close(s); | ||
61 | |||
62 | return 0; | ||
63 | } | ||
64 | |||
65 | int mailstream_low_get_fd(mailstream_low * s) | ||
66 | { | ||
67 | if (s == NULL) | ||
68 | return -1; | ||
69 | return s->driver->mailstream_get_fd(s); | ||
70 | } | ||
71 | |||
72 | void mailstream_low_free(mailstream_low * s) | ||
73 | { | ||
74 | s->driver->mailstream_free(s); | ||
75 | } | ||
76 | |||
77 | ssize_t mailstream_low_read(mailstream_low * s, void * buf, size_t count) | ||
78 | { | ||
79 | if (s == NULL) | ||
80 | return -1; | ||
81 | return s->driver->mailstream_read(s, buf, count); | ||
82 | } | ||
83 | |||
84 | ssize_t mailstream_low_write(mailstream_low * s, | ||
85 | const void * buf, size_t count) | ||
86 | { | ||
87 | if (s == NULL) | ||
88 | return -1; | ||
89 | return s->driver->mailstream_write(s, buf, count); | ||
90 | } | ||
diff --git a/kmicromail/libetpan/tools/mailstream_low.h b/kmicromail/libetpan/tools/mailstream_low.h new file mode 100644 index 0000000..fb8914c --- a/dev/null +++ b/kmicromail/libetpan/tools/mailstream_low.h | |||
@@ -0,0 +1,62 @@ | |||
1 | /* | ||
2 | * libEtPan! -- a mail stuff library | ||
3 | * | ||
4 | * Copyright (C) 2001, 2002 - DINH Viet Hoa | ||
5 | * All rights reserved. | ||
6 | * | ||
7 | * Redistribution and use in source and binary forms, with or without | ||
8 | * modification, are permitted provided that the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. Neither the name of the libEtPan! project nor the names of its | ||
16 | * contributors may be used to endorse or promote products derived | ||
17 | * from this software without specific prior written permission. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
29 | * SUCH DAMAGE. | ||
30 | */ | ||
31 | |||
32 | /* | ||
33 | * $Id$ | ||
34 | */ | ||
35 | |||
36 | #ifndef MAILSTREAM_LOW_H | ||
37 | |||
38 | #define MAILSTREAM_LOW_H | ||
39 | |||
40 | #include <sys/types.h> | ||
41 | #include <libetpan/mailstream_types.h> | ||
42 | |||
43 | #ifdef __cplusplus | ||
44 | extern "C" { | ||
45 | #endif | ||
46 | |||
47 | /* general functions */ | ||
48 | |||
49 | mailstream_low * mailstream_low_new(void * data, | ||
50 | mailstream_low_driver * driver); | ||
51 | ssize_t mailstream_low_write(mailstream_low * s, | ||
52 | const void * buf, size_t count); | ||
53 | ssize_t mailstream_low_read(mailstream_low * s, void * buf, size_t count); | ||
54 | int mailstream_low_close(mailstream_low * s); | ||
55 | int mailstream_low_get_fd(mailstream_low * s); | ||
56 | void mailstream_low_free(mailstream_low * s); | ||
57 | |||
58 | #ifdef __cplusplus | ||
59 | } | ||
60 | #endif | ||
61 | |||
62 | #endif | ||
diff --git a/kmicromail/libetpan/tools/mailstream_socket.c b/kmicromail/libetpan/tools/mailstream_socket.c new file mode 100644 index 0000000..29e50e1 --- a/dev/null +++ b/kmicromail/libetpan/tools/mailstream_socket.c | |||
@@ -0,0 +1,239 @@ | |||
1 | /* | ||
2 | * libEtPan! -- a mail stuff library | ||
3 | * | ||
4 | * Copyright (C) 2001, 2002 - DINH Viet Hoa | ||
5 | * All rights reserved. | ||
6 | * | ||
7 | * Redistribution and use in source and binary forms, with or without | ||
8 | * modification, are permitted provided that the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. Neither the name of the libEtPan! project nor the names of its | ||
16 | * contributors may be used to endorse or promote products derived | ||
17 | * from this software without specific prior written permission. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
29 | * SUCH DAMAGE. | ||
30 | */ | ||
31 | |||
32 | /* | ||
33 | * $Id$ | ||
34 | */ | ||
35 | |||
36 | #include "mailstream_socket.h" | ||
37 | #include <unistd.h> | ||
38 | #include <stdlib.h> | ||
39 | #include <fcntl.h> | ||
40 | |||
41 | /* | ||
42 | these 3 headers MUST be included before <sys/select.h> | ||
43 | to insure compatibility with Mac OS X (this is true for 10.2) | ||
44 | */ | ||
45 | #include <sys/time.h> | ||
46 | #include <sys/socket.h> | ||
47 | #include <sys/types.h> | ||
48 | #include <unistd.h> | ||
49 | #include <sys/select.h> | ||
50 | |||
51 | /* mailstream_low, socket */ | ||
52 | |||
53 | static int mailstream_low_socket_close(mailstream_low * s); | ||
54 | static ssize_t mailstream_low_socket_read(mailstream_low * s, | ||
55 | void * buf, size_t count); | ||
56 | static ssize_t mailstream_low_socket_write(mailstream_low * s, | ||
57 | const void * buf, size_t count); | ||
58 | static void mailstream_low_socket_free(mailstream_low * s); | ||
59 | static int mailstream_low_socket_get_fd(mailstream_low * s); | ||
60 | |||
61 | static mailstream_low_driver local_mailstream_socket_driver = { | ||
62 | mailstream_read: mailstream_low_socket_read, | ||
63 | mailstream_write: mailstream_low_socket_write, | ||
64 | mailstream_close: mailstream_low_socket_close, | ||
65 | mailstream_free: mailstream_low_socket_free, | ||
66 | mailstream_get_fd: mailstream_low_socket_get_fd, | ||
67 | }; | ||
68 | |||
69 | mailstream_low_driver * mailstream_socket_driver = | ||
70 | &local_mailstream_socket_driver; | ||
71 | |||
72 | /* file descriptor must be given in (default) blocking-mode */ | ||
73 | |||
74 | static struct mailstream_socket_data * socket_data_new(int fd) | ||
75 | { | ||
76 | struct mailstream_socket_data * socket_data; | ||
77 | |||
78 | socket_data = malloc(sizeof(* socket_data)); | ||
79 | if (socket_data == NULL) | ||
80 | goto err; | ||
81 | |||
82 | socket_data->fd = fd; | ||
83 | |||
84 | return socket_data; | ||
85 | |||
86 | err: | ||
87 | return NULL; | ||
88 | } | ||
89 | |||
90 | static void socket_data_free(struct mailstream_socket_data * socket_data) | ||
91 | { | ||
92 | free(socket_data); | ||
93 | } | ||
94 | |||
95 | static void socket_data_close(struct mailstream_socket_data * socket_data) | ||
96 | { | ||
97 | close(socket_data->fd); | ||
98 | socket_data->fd = -1; | ||
99 | } | ||
100 | |||
101 | mailstream_low * mailstream_low_socket_open(int fd) | ||
102 | { | ||
103 | mailstream_low * s; | ||
104 | struct mailstream_socket_data * socket_data; | ||
105 | |||
106 | socket_data = socket_data_new(fd); | ||
107 | if (socket_data == NULL) | ||
108 | goto err; | ||
109 | |||
110 | s = mailstream_low_new(socket_data, mailstream_socket_driver); | ||
111 | if (s == NULL) | ||
112 | goto free_socket_data; | ||
113 | |||
114 | return s; | ||
115 | |||
116 | free_socket_data: | ||
117 | socket_data_free(socket_data); | ||
118 | err: | ||
119 | return NULL; | ||
120 | } | ||
121 | |||
122 | static int mailstream_low_socket_close(mailstream_low * s) | ||
123 | { | ||
124 | struct mailstream_socket_data * socket_data; | ||
125 | |||
126 | socket_data = (struct mailstream_socket_data *) s->data; | ||
127 | socket_data_close(socket_data); | ||
128 | |||
129 | return 0; | ||
130 | } | ||
131 | |||
132 | static void mailstream_low_socket_free(mailstream_low * s) | ||
133 | { | ||
134 | struct mailstream_socket_data * socket_data; | ||
135 | |||
136 | socket_data = (struct mailstream_socket_data *) s->data; | ||
137 | socket_data_free(socket_data); | ||
138 | s->data = NULL; | ||
139 | |||
140 | free(s); | ||
141 | } | ||
142 | |||
143 | static int mailstream_low_socket_get_fd(mailstream_low * s) | ||
144 | { | ||
145 | struct mailstream_socket_data * socket_data; | ||
146 | |||
147 | socket_data = (struct mailstream_socket_data *) s->data; | ||
148 | return socket_data->fd; | ||
149 | } | ||
150 | |||
151 | |||
152 | static ssize_t mailstream_low_socket_read(mailstream_low * s, | ||
153 | void * buf, size_t count) | ||
154 | { | ||
155 | struct mailstream_socket_data * socket_data; | ||
156 | |||
157 | socket_data = (struct mailstream_socket_data *) s->data; | ||
158 | |||
159 | /* timeout */ | ||
160 | { | ||
161 | fd_set fds_read; | ||
162 | fd_set fds_excp; | ||
163 | struct timeval timeout; | ||
164 | int r; | ||
165 | |||
166 | timeout = mailstream_network_delay; | ||
167 | |||
168 | FD_ZERO(&fds_read); | ||
169 | FD_SET(socket_data->fd, &fds_read); | ||
170 | FD_ZERO(&fds_excp); | ||
171 | FD_SET(socket_data->fd, &fds_excp); | ||
172 | r = select(socket_data->fd + 1, &fds_read, NULL, &fds_excp, &timeout); | ||
173 | if (r == 0) | ||
174 | return -1; | ||
175 | if (FD_ISSET(socket_data->fd, &fds_excp)) | ||
176 | return -1; | ||
177 | if (!FD_ISSET(socket_data->fd, &fds_read)) | ||
178 | return 0; | ||
179 | } | ||
180 | return recv(socket_data->fd,buf,count,MSG_NOSIGNAL); | ||
181 | //return read(socket_data->fd, buf, count); | ||
182 | } | ||
183 | |||
184 | static ssize_t mailstream_low_socket_write(mailstream_low * s, | ||
185 | const void * buf, size_t count) | ||
186 | { | ||
187 | struct mailstream_socket_data * socket_data; | ||
188 | |||
189 | socket_data = (struct mailstream_socket_data *) s->data; | ||
190 | /* timeout */ | ||
191 | { | ||
192 | fd_set fds_write; | ||
193 | fd_set fds_excp; | ||
194 | struct timeval timeout; | ||
195 | int r; | ||
196 | |||
197 | timeout = mailstream_network_delay; | ||
198 | |||
199 | FD_ZERO(&fds_write); | ||
200 | FD_SET(socket_data->fd, &fds_write); | ||
201 | FD_ZERO(&fds_excp); | ||
202 | FD_SET(socket_data->fd, &fds_excp); | ||
203 | r = select(socket_data->fd + 1, NULL, &fds_write, &fds_excp, &timeout); | ||
204 | if (r == 0) | ||
205 | return -1; | ||
206 | if (FD_ISSET(socket_data->fd, &fds_excp)) | ||
207 | return -1; | ||
208 | if (!FD_ISSET(socket_data->fd, &fds_write)) | ||
209 | return 0; | ||
210 | } | ||
211 | |||
212 | return send(socket_data->fd,buf,count,MSG_NOSIGNAL); | ||
213 | //return write(socket_data->fd, buf, count); | ||
214 | } | ||
215 | |||
216 | |||
217 | /* mailstream */ | ||
218 | |||
219 | mailstream * mailstream_socket_open(int fd) | ||
220 | { | ||
221 | mailstream_low * low; | ||
222 | mailstream * s; | ||
223 | |||
224 | low = mailstream_low_socket_open(fd); | ||
225 | if (low == NULL) | ||
226 | goto err; | ||
227 | |||
228 | s = mailstream_new(low, 8192); | ||
229 | if (s == NULL) | ||
230 | goto free_low; | ||
231 | |||
232 | return s; | ||
233 | |||
234 | free_low: | ||
235 | mailstream_low_close(low); | ||
236 | err: | ||
237 | return NULL; | ||
238 | } | ||
239 | |||
diff --git a/kmicromail/libetpan/tools/mailstream_socket.h b/kmicromail/libetpan/tools/mailstream_socket.h new file mode 100644 index 0000000..6a26e33 --- a/dev/null +++ b/kmicromail/libetpan/tools/mailstream_socket.h | |||
@@ -0,0 +1,61 @@ | |||
1 | /* | ||
2 | * libEtPan! -- a mail stuff library | ||
3 | * | ||
4 | * Copyright (C) 2001, 2002 - DINH Viet Hoa | ||
5 | * All rights reserved. | ||
6 | * | ||
7 | * Redistribution and use in source and binary forms, with or without | ||
8 | * modification, are permitted provided that the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. Neither the name of the libEtPan! project nor the names of its | ||
16 | * contributors may be used to endorse or promote products derived | ||
17 | * from this software without specific prior written permission. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
29 | * SUCH DAMAGE. | ||
30 | */ | ||
31 | |||
32 | /* | ||
33 | * $Id$ | ||
34 | */ | ||
35 | |||
36 | #ifndef MAILSTREAM_SOCKET_H | ||
37 | |||
38 | #define MAILSTREAM_SOCKET_H | ||
39 | |||
40 | #include <libetpan/mailstream.h> | ||
41 | |||
42 | #ifdef __cplusplus | ||
43 | extern "C" { | ||
44 | #endif | ||
45 | |||
46 | /* socket */ | ||
47 | |||
48 | extern mailstream_low_driver * mailstream_socket_driver; | ||
49 | |||
50 | mailstream_low * mailstream_low_socket_open(int fd); | ||
51 | mailstream * mailstream_socket_open(int fd); | ||
52 | |||
53 | struct mailstream_socket_data { | ||
54 | int fd; | ||
55 | }; | ||
56 | |||
57 | #ifdef __cplusplus | ||
58 | } | ||
59 | #endif | ||
60 | |||
61 | #endif | ||
diff --git a/kmicromail/libetpan/tools/mailstream_ssl.c b/kmicromail/libetpan/tools/mailstream_ssl.c new file mode 100644 index 0000000..9f5008d --- a/dev/null +++ b/kmicromail/libetpan/tools/mailstream_ssl.c | |||
@@ -0,0 +1,312 @@ | |||
1 | /* | ||
2 | * libEtPan! -- a mail stuff library | ||
3 | * | ||
4 | * Copyright (C) 2001, 2002 - DINH Viet Hoa | ||
5 | * All rights reserved. | ||
6 | * | ||
7 | * Redistribution and use in source and binary forms, with or without | ||
8 | * modification, are permitted provided that the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. Neither the name of the libEtPan! project nor the names of its | ||
16 | * contributors may be used to endorse or promote products derived | ||
17 | * from this software without specific prior written permission. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
29 | * SUCH DAMAGE. | ||
30 | */ | ||
31 | |||
32 | /* | ||
33 | * $Id$ | ||
34 | */ | ||
35 | |||
36 | /* | ||
37 | NOTE : | ||
38 | |||
39 | The user has to call himself SSL_library_init() if he wants to | ||
40 | use SSL. | ||
41 | */ | ||
42 | |||
43 | #include "mailstream_ssl.h" | ||
44 | #include <unistd.h> | ||
45 | #include <fcntl.h> | ||
46 | |||
47 | #ifndef CONFIG_H | ||
48 | #define CONFIG_H | ||
49 | #include "config.h" | ||
50 | #endif | ||
51 | |||
52 | /* | ||
53 | these 3 headers MUST be included before <sys/select.h> | ||
54 | to insure compatibility with Mac OS X (this is true for 10.2) | ||
55 | */ | ||
56 | #include <sys/time.h> | ||
57 | #include <sys/types.h> | ||
58 | #include <unistd.h> | ||
59 | #include <sys/select.h> | ||
60 | |||
61 | /* mailstream_low, ssl */ | ||
62 | |||
63 | #ifdef USE_SSL | ||
64 | #include <openssl/ssl.h> | ||
65 | #include <pthread.h> | ||
66 | #endif | ||
67 | |||
68 | #ifdef USE_SSL | ||
69 | struct mailstream_ssl_data { | ||
70 | int fd; | ||
71 | SSL * ssl_conn; | ||
72 | SSL_CTX * ssl_ctx; | ||
73 | }; | ||
74 | #endif | ||
75 | |||
76 | #ifdef USE_SSL | ||
77 | static pthread_mutex_t ssl_lock = PTHREAD_MUTEX_INITIALIZER; | ||
78 | static int ssl_init_done = 0; | ||
79 | #endif | ||
80 | |||
81 | #ifdef USE_SSL | ||
82 | static int mailstream_low_ssl_close(mailstream_low * s); | ||
83 | static ssize_t mailstream_low_ssl_read(mailstream_low * s, | ||
84 | void * buf, size_t count); | ||
85 | static ssize_t mailstream_low_ssl_write(mailstream_low * s, | ||
86 | const void * buf, size_t count); | ||
87 | static void mailstream_low_ssl_free(mailstream_low * s); | ||
88 | static int mailstream_low_ssl_get_fd(mailstream_low * s); | ||
89 | |||
90 | static mailstream_low_driver local_mailstream_ssl_driver = { | ||
91 | mailstream_read: mailstream_low_ssl_read, | ||
92 | mailstream_write: mailstream_low_ssl_write, | ||
93 | mailstream_close: mailstream_low_ssl_close, | ||
94 | mailstream_free: mailstream_low_ssl_free, | ||
95 | mailstream_get_fd: mailstream_low_ssl_get_fd, | ||
96 | }; | ||
97 | |||
98 | mailstream_low_driver * mailstream_ssl_driver = &local_mailstream_ssl_driver; | ||
99 | #endif | ||
100 | |||
101 | /* file descriptor must be given in (default) blocking-mode */ | ||
102 | |||
103 | #ifdef USE_SSL | ||
104 | static struct mailstream_ssl_data * ssl_data_new(int fd) | ||
105 | { | ||
106 | struct mailstream_ssl_data * ssl_data; | ||
107 | SSL * ssl_conn; | ||
108 | int r; | ||
109 | SSL_CTX * tmp_ctx; | ||
110 | int fd_flags; | ||
111 | int old_fd_flags; | ||
112 | |||
113 | pthread_mutex_lock(&ssl_lock); | ||
114 | if (!ssl_init_done) { | ||
115 | SSL_library_init(); | ||
116 | ssl_init_done = 1; | ||
117 | } | ||
118 | pthread_mutex_unlock(&ssl_lock); | ||
119 | |||
120 | tmp_ctx = SSL_CTX_new(TLSv1_client_method()); | ||
121 | if (tmp_ctx == NULL) | ||
122 | goto err; | ||
123 | |||
124 | ssl_conn = (SSL *) SSL_new(tmp_ctx); | ||
125 | if (ssl_conn == NULL) | ||
126 | goto free_ctx; | ||
127 | |||
128 | if (SSL_set_fd(ssl_conn, fd) == 0) | ||
129 | goto free_ssl_conn; | ||
130 | |||
131 | SSL_set_read_ahead(ssl_conn, 1); | ||
132 | |||
133 | r = SSL_connect(ssl_conn); | ||
134 | if (r <= 0) | ||
135 | goto free_ssl_conn; | ||
136 | |||
137 | fd_flags = fcntl(fd, F_GETFL, 0); | ||
138 | old_fd_flags = fd_flags; | ||
139 | fd_flags |= O_NDELAY; | ||
140 | r = fcntl(fd, F_SETFL, fd_flags); | ||
141 | if (r < 0) | ||
142 | goto free_ssl_conn; | ||
143 | |||
144 | ssl_data = malloc(sizeof(* ssl_data)); | ||
145 | if (ssl_data == NULL) | ||
146 | goto reset_fd_flags; | ||
147 | |||
148 | ssl_data->fd = fd; | ||
149 | ssl_data->ssl_conn = ssl_conn; | ||
150 | ssl_data->ssl_ctx = tmp_ctx; | ||
151 | |||
152 | return ssl_data; | ||
153 | |||
154 | reset_fd_flags: | ||
155 | fcntl(fd, F_SETFL, old_fd_flags); | ||
156 | free_ctx: | ||
157 | SSL_CTX_free(tmp_ctx); | ||
158 | free_ssl_conn: | ||
159 | SSL_free(ssl_conn); | ||
160 | err: | ||
161 | return NULL; | ||
162 | } | ||
163 | |||
164 | static void ssl_data_free(struct mailstream_ssl_data * ssl_data) | ||
165 | { | ||
166 | free(ssl_data); | ||
167 | } | ||
168 | |||
169 | static void ssl_data_close(struct mailstream_ssl_data * ssl_data) | ||
170 | { | ||
171 | SSL_free(ssl_data->ssl_conn); | ||
172 | ssl_data->ssl_conn = NULL; | ||
173 | SSL_CTX_free(ssl_data->ssl_ctx); | ||
174 | ssl_data->ssl_ctx = NULL; | ||
175 | close(ssl_data->fd); | ||
176 | ssl_data->fd = -1; | ||
177 | } | ||
178 | #endif | ||
179 | |||
180 | mailstream_low * mailstream_low_ssl_open(int fd) | ||
181 | { | ||
182 | #ifdef USE_SSL | ||
183 | mailstream_low * s; | ||
184 | struct mailstream_ssl_data * ssl_data; | ||
185 | |||
186 | ssl_data = ssl_data_new(fd); | ||
187 | if (ssl_data == NULL) | ||
188 | goto err; | ||
189 | |||
190 | s = mailstream_low_new(ssl_data, mailstream_ssl_driver); | ||
191 | if (s == NULL) | ||
192 | goto free_ssl_data; | ||
193 | |||
194 | return s; | ||
195 | |||
196 | free_ssl_data: | ||
197 | ssl_data_free(ssl_data); | ||
198 | err: | ||
199 | return NULL; | ||
200 | #else | ||
201 | return NULL; | ||
202 | #endif | ||
203 | } | ||
204 | |||
205 | #ifdef USE_SSL | ||
206 | static int mailstream_low_ssl_close(mailstream_low * s) | ||
207 | { | ||
208 | struct mailstream_ssl_data * ssl_data; | ||
209 | |||
210 | ssl_data = (struct mailstream_ssl_data *) s->data; | ||
211 | ssl_data_close(ssl_data); | ||
212 | |||
213 | return 0; | ||
214 | } | ||
215 | |||
216 | static void mailstream_low_ssl_free(mailstream_low * s) | ||
217 | { | ||
218 | struct mailstream_ssl_data * ssl_data; | ||
219 | |||
220 | ssl_data = (struct mailstream_ssl_data *) s->data; | ||
221 | ssl_data_free(ssl_data); | ||
222 | s->data = NULL; | ||
223 | |||
224 | free(s); | ||
225 | } | ||
226 | |||
227 | static int mailstream_low_ssl_get_fd(mailstream_low * s) | ||
228 | { | ||
229 | struct mailstream_ssl_data * ssl_data; | ||
230 | |||
231 | ssl_data = (struct mailstream_ssl_data *) s->data; | ||
232 | return ssl_data->fd; | ||
233 | } | ||
234 | |||
235 | static ssize_t mailstream_low_ssl_read(mailstream_low * s, | ||
236 | void * buf, size_t count) | ||
237 | { | ||
238 | struct mailstream_ssl_data * ssl_data; | ||
239 | int r; | ||
240 | |||
241 | ssl_data = (struct mailstream_ssl_data *) s->data; | ||
242 | |||
243 | while (1) { | ||
244 | int ssl_r; | ||
245 | fd_set fds_read; | ||
246 | struct timeval timeout; | ||
247 | |||
248 | r = SSL_read(ssl_data->ssl_conn, buf, count); | ||
249 | if (r > 0) | ||
250 | return r; | ||
251 | |||
252 | ssl_r = SSL_get_error(ssl_data->ssl_conn, r); | ||
253 | switch (ssl_r) { | ||
254 | case SSL_ERROR_NONE: | ||
255 | return r; | ||
256 | |||
257 | case SSL_ERROR_ZERO_RETURN: | ||
258 | return r; | ||
259 | |||
260 | case SSL_ERROR_WANT_READ: | ||
261 | timeout = mailstream_network_delay; | ||
262 | |||
263 | FD_ZERO(&fds_read); | ||
264 | FD_SET(ssl_data->fd, &fds_read); | ||
265 | r = select(ssl_data->fd + 1, &fds_read, NULL, NULL, &timeout); | ||
266 | if (r == 0) | ||
267 | return -1; | ||
268 | break; | ||
269 | |||
270 | default: | ||
271 | return r; | ||
272 | } | ||
273 | } | ||
274 | } | ||
275 | |||
276 | static ssize_t mailstream_low_ssl_write(mailstream_low * s, | ||
277 | const void * buf, size_t count) | ||
278 | { | ||
279 | struct mailstream_ssl_data * ssl_data; | ||
280 | |||
281 | ssl_data = (struct mailstream_ssl_data *) s->data; | ||
282 | return SSL_write(ssl_data->ssl_conn, buf, count); | ||
283 | } | ||
284 | #endif | ||
285 | |||
286 | /* mailstream */ | ||
287 | |||
288 | mailstream * mailstream_ssl_open(int fd) | ||
289 | { | ||
290 | #ifdef USE_SSL | ||
291 | mailstream_low * low; | ||
292 | mailstream * s; | ||
293 | |||
294 | low = mailstream_low_ssl_open(fd); | ||
295 | if (low == NULL) | ||
296 | goto err; | ||
297 | |||
298 | s = mailstream_new(low, 8192); | ||
299 | if (s == NULL) | ||
300 | goto free_low; | ||
301 | |||
302 | return s; | ||
303 | |||
304 | free_low: | ||
305 | mailstream_low_close(low); | ||
306 | err: | ||
307 | return NULL; | ||
308 | #else | ||
309 | return NULL; | ||
310 | #endif | ||
311 | } | ||
312 | |||
diff --git a/kmicromail/libetpan/tools/mailstream_ssl.h b/kmicromail/libetpan/tools/mailstream_ssl.h new file mode 100644 index 0000000..a37b3d4 --- a/dev/null +++ b/kmicromail/libetpan/tools/mailstream_ssl.h | |||
@@ -0,0 +1,59 @@ | |||
1 | /* | ||
2 | * libEtPan! -- a mail stuff library | ||
3 | * | ||
4 | * Copyright (C) 2001, 2002 - DINH Viet Hoa | ||
5 | * All rights reserved. | ||
6 | * | ||
7 | * Redistribution and use in source and binary forms, with or without | ||
8 | * modification, are permitted provided that the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. Neither the name of the libEtPan! project nor the names of its | ||
16 | * contributors may be used to endorse or promote products derived | ||
17 | * from this software without specific prior written permission. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
29 | * SUCH DAMAGE. | ||
30 | */ | ||
31 | |||
32 | /* | ||
33 | * $Id$ | ||
34 | */ | ||
35 | |||
36 | #ifndef MAILSTREAM_SSL_H | ||
37 | |||
38 | #define MAILSTREAM_SSL_H | ||
39 | |||
40 | #include <libetpan/mailstream.h> | ||
41 | |||
42 | #ifdef __cplusplus | ||
43 | extern "C" { | ||
44 | #endif | ||
45 | |||
46 | /* socket */ | ||
47 | |||
48 | #ifdef USE_SSL | ||
49 | extern mailstream_low_driver * mailstream_ssl_driver; | ||
50 | #endif | ||
51 | |||
52 | mailstream_low * mailstream_low_ssl_open(int fd); | ||
53 | mailstream * mailstream_ssl_open(int fd); | ||
54 | |||
55 | #ifdef __cplusplus | ||
56 | } | ||
57 | #endif | ||
58 | |||
59 | #endif | ||
diff --git a/kmicromail/libetpan/tools/mailstream_types.h b/kmicromail/libetpan/tools/mailstream_types.h new file mode 100644 index 0000000..2165149 --- a/dev/null +++ b/kmicromail/libetpan/tools/mailstream_types.h | |||
@@ -0,0 +1,87 @@ | |||
1 | /* | ||
2 | * libEtPan! -- a mail stuff library | ||
3 | * | ||
4 | * Copyright (C) 2001, 2002 - DINH Viet Hoa | ||
5 | * All rights reserved. | ||
6 | * | ||
7 | * Redistribution and use in source and binary forms, with or without | ||
8 | * modification, are permitted provided that the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. Neither the name of the libEtPan! project nor the names of its | ||
16 | * contributors may be used to endorse or promote products derived | ||
17 | * from this software without specific prior written permission. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
29 | * SUCH DAMAGE. | ||
30 | */ | ||
31 | |||
32 | /* | ||
33 | * $Id$ | ||
34 | */ | ||
35 | |||
36 | #ifndef MAILSTREAM_TYPES_H | ||
37 | |||
38 | #define MAILSTREAM_TYPES_H | ||
39 | |||
40 | #ifdef __cplusplus | ||
41 | extern "C" { | ||
42 | #endif | ||
43 | |||
44 | #define LIBETPAN_MAILSTREAM_DEBUG | ||
45 | |||
46 | struct _mailstream; | ||
47 | |||
48 | typedef struct _mailstream mailstream; | ||
49 | |||
50 | struct _mailstream_low; | ||
51 | |||
52 | typedef struct _mailstream_low mailstream_low; | ||
53 | |||
54 | struct _mailstream { | ||
55 | size_t buffer_max_size; | ||
56 | |||
57 | char * write_buffer; | ||
58 | size_t write_buffer_len; | ||
59 | |||
60 | char * read_buffer; | ||
61 | size_t read_buffer_len; | ||
62 | |||
63 | mailstream_low * low; | ||
64 | }; | ||
65 | |||
66 | struct mailstream_low_driver { | ||
67 | ssize_t (* mailstream_read)(mailstream_low *, void *, size_t); | ||
68 | ssize_t (* mailstream_write)(mailstream_low *, const void *, size_t); | ||
69 | int (* mailstream_close)(mailstream_low *); | ||
70 | int (* mailstream_get_fd)(mailstream_low *); | ||
71 | void (* mailstream_free)(mailstream_low *); | ||
72 | }; | ||
73 | |||
74 | typedef struct mailstream_low_driver mailstream_low_driver; | ||
75 | |||
76 | struct _mailstream_low { | ||
77 | void * data; | ||
78 | mailstream_low_driver * driver; | ||
79 | }; | ||
80 | |||
81 | typedef void progress_function(size_t current, size_t maximum); | ||
82 | |||
83 | #ifdef __cplusplus | ||
84 | } | ||
85 | #endif | ||
86 | |||
87 | #endif | ||
diff --git a/kmicromail/libetpan/tools/mapping.c b/kmicromail/libetpan/tools/mapping.c new file mode 100644 index 0000000..426a03c --- a/dev/null +++ b/kmicromail/libetpan/tools/mapping.c | |||
@@ -0,0 +1,67 @@ | |||
1 | /* | ||
2 | * libEtPan! -- a mail stuff library | ||
3 | * | ||
4 | * Copyright (C) 2001, 2002 - DINH Viet Hoa | ||
5 | * All rights reserved. | ||
6 | * | ||
7 | * Redistribution and use in source and binary forms, with or without | ||
8 | * modification, are permitted provided that the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. Neither the name of the libEtPan! project nor the names of its | ||
16 | * contributors may be used to endorse or promote products derived | ||
17 | * from this software without specific prior written permission. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
29 | * SUCH DAMAGE. | ||
30 | */ | ||
31 | |||
32 | /* | ||
33 | * $Id$ | ||
34 | */ | ||
35 | |||
36 | #include "mapping.h" | ||
37 | |||
38 | #include <unistd.h> | ||
39 | #include <sys/mman.h> | ||
40 | |||
41 | int get_mapping(size_t length, int prot, int flags, | ||
42 | int fd, off_t offset, | ||
43 | void ** presult, void ** pmapping, size_t * pmapping_size) | ||
44 | { | ||
45 | void * mapping; | ||
46 | size_t mapping_size; | ||
47 | void * result; | ||
48 | size_t page_size; | ||
49 | off_t delta; | ||
50 | |||
51 | page_size = getpagesize(); | ||
52 | delta = offset % page_size; | ||
53 | |||
54 | mapping = mmap(NULL, length + offset, prot, flags, fd, offset - delta); | ||
55 | if (mapping == MAP_FAILED) | ||
56 | return -1; | ||
57 | |||
58 | result = ((char *) mapping) + delta; | ||
59 | |||
60 | mapping_size = length + offset; | ||
61 | |||
62 | * pmapping = mapping; | ||
63 | * pmapping_size = mapping_size; | ||
64 | * presult = result; | ||
65 | |||
66 | return 0; | ||
67 | } | ||
diff --git a/kmicromail/libetpan/tools/mapping.h b/kmicromail/libetpan/tools/mapping.h new file mode 100644 index 0000000..8e72f98 --- a/dev/null +++ b/kmicromail/libetpan/tools/mapping.h | |||
@@ -0,0 +1,54 @@ | |||
1 | /* | ||
2 | * libEtPan! -- a mail stuff library | ||
3 | * | ||
4 | * Copyright (C) 2001, 2002 - DINH Viet Hoa | ||
5 | * All rights reserved. | ||
6 | * | ||
7 | * Redistribution and use in source and binary forms, with or without | ||
8 | * modification, are permitted provided that the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. Neither the name of the libEtPan! project nor the names of its | ||
16 | * contributors may be used to endorse or promote products derived | ||
17 | * from this software without specific prior written permission. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
29 | * SUCH DAMAGE. | ||
30 | */ | ||
31 | |||
32 | /* | ||
33 | * $Id$ | ||
34 | */ | ||
35 | |||
36 | #ifndef MAPPING_H | ||
37 | |||
38 | #define MAPPING_H | ||
39 | |||
40 | #include <sys/types.h> | ||
41 | |||
42 | #ifdef __cplusplus | ||
43 | extern "C" { | ||
44 | #endif | ||
45 | |||
46 | int get_mapping(size_t length, int prot, int flags, | ||
47 | int fd, off_t offset, | ||
48 | void ** presult, void ** pmapping, size_t * pmapping_size); | ||
49 | |||
50 | #ifdef __cplusplus | ||
51 | } | ||
52 | #endif | ||
53 | |||
54 | #endif | ||
diff --git a/kmicromail/libetpan/tools/md5.c b/kmicromail/libetpan/tools/md5.c new file mode 100644 index 0000000..50307e0 --- a/dev/null +++ b/kmicromail/libetpan/tools/md5.c | |||
@@ -0,0 +1,570 @@ | |||
1 | /* | ||
2 | * libEtPan! -- a mail stuff library | ||
3 | * | ||
4 | * Copyright (C) 2001, 2002 - DINH Viet Hoa | ||
5 | * All rights reserved. | ||
6 | * | ||
7 | * Redistribution and use in source and binary forms, with or without | ||
8 | * modification, are permitted provided that the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. Neither the name of the libEtPan! project nor the names of its | ||
16 | * contributors may be used to endorse or promote products derived | ||
17 | * from this software without specific prior written permission. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
29 | * SUCH DAMAGE. | ||
30 | */ | ||
31 | |||
32 | /* | ||
33 | * $Id$ | ||
34 | */ | ||
35 | |||
36 | /* MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm | ||
37 | */ | ||
38 | |||
39 | /* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All | ||
40 | rights reserved. | ||
41 | |||
42 | License to copy and use this software is granted provided that it | ||
43 | is identified as the "RSA Data Security, Inc. MD5 Message-Digest | ||
44 | Algorithm" in all material mentioning or referencing this software | ||
45 | or this function. | ||
46 | |||
47 | License is also granted to make and use derivative works provided | ||
48 | that such works are identified as "derived from the RSA Data | ||
49 | Security, Inc. MD5 Message-Digest Algorithm" in all material | ||
50 | mentioning or referencing the derived work. | ||
51 | |||
52 | RSA Data Security, Inc. makes no representations concerning either | ||
53 | the merchantability of this software or the suitability of this | ||
54 | software for any particular purpose. It is provided "as is" | ||
55 | without express or implied warranty of any kind. | ||
56 | |||
57 | These notices must be retained in any copies of any part of this | ||
58 | documentation and/or software. | ||
59 | */ | ||
60 | |||
61 | /* do i need all of this just for htonl()? damn. */ | ||
62 | #include <sys/types.h> | ||
63 | #include <sys/param.h> | ||
64 | #include <sys/socket.h> | ||
65 | #include <netinet/in.h> | ||
66 | |||
67 | #include "md5global.h" | ||
68 | #include "md5.h" | ||
69 | #include "hmac-md5.h" | ||
70 | |||
71 | /* Constants for MD5Transform routine. | ||
72 | */ | ||
73 | |||
74 | #define S11 7 | ||
75 | #define S12 12 | ||
76 | #define S13 17 | ||
77 | #define S14 22 | ||
78 | #define S21 5 | ||
79 | #define S22 9 | ||
80 | #define S23 14 | ||
81 | #define S24 20 | ||
82 | #define S31 4 | ||
83 | #define S32 11 | ||
84 | #define S33 16 | ||
85 | #define S34 23 | ||
86 | #define S41 6 | ||
87 | #define S42 10 | ||
88 | #define S43 15 | ||
89 | #define S44 21 | ||
90 | |||
91 | static void MD5Transform PROTO_LIST ((UINT4 [4], unsigned char [64])); | ||
92 | static void Encode PROTO_LIST | ||
93 | ((unsigned char *, UINT4 *, unsigned int)); | ||
94 | static void Decode PROTO_LIST | ||
95 | ((UINT4 *, unsigned char *, unsigned int)); | ||
96 | static void MD5_memcpy PROTO_LIST ((POINTER, POINTER, unsigned int)); | ||
97 | static void MD5_memset PROTO_LIST ((POINTER, int, unsigned int)); | ||
98 | |||
99 | static unsigned char PADDING[64] = { | ||
100 | 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
101 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 | ||
102 | }; | ||
103 | |||
104 | /* F, G, H and I are basic MD5 functions. | ||
105 | |||
106 | */ | ||
107 | #ifdef I | ||
108 | /* This might be defined via NANA */ | ||
109 | #undef I | ||
110 | #endif | ||
111 | |||
112 | #define F(x, y, z) (((x) & (y)) | ((~x) & (z))) | ||
113 | #define G(x, y, z) (((x) & (z)) | ((y) & (~z))) | ||
114 | #define H(x, y, z) ((x) ^ (y) ^ (z)) | ||
115 | #define I(x, y, z) ((y) ^ ((x) | (~z))) | ||
116 | |||
117 | /* ROTATE_LEFT rotates x left n bits. | ||
118 | |||
119 | */ | ||
120 | |||
121 | #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n)))) | ||
122 | |||
123 | /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4. | ||
124 | Rotation is separate from addition to prevent recomputation. | ||
125 | */ | ||
126 | |||
127 | #define FF(a, b, c, d, x, s, ac) { (a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); (a) = ROTATE_LEFT ((a), (s)); (a) += (b); } | ||
128 | #define GG(a, b, c, d, x, s, ac) { (a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); (a) = ROTATE_LEFT ((a), (s)); (a) += (b); } | ||
129 | #define HH(a, b, c, d, x, s, ac) { (a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); (a) = ROTATE_LEFT ((a), (s)); (a) += (b); } | ||
130 | #define II(a, b, c, d, x, s, ac) { (a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); (a) = ROTATE_LEFT ((a), (s)); (a) += (b); } | ||
131 | |||
132 | /* MD5 initialization. Begins an MD5 operation, writing a new context. | ||
133 | */ | ||
134 | |||
135 | void MD5Init (context) | ||
136 | MD5_CTX *context; /* context */ | ||
137 | { | ||
138 | context->count[0] = context->count[1] = 0; | ||
139 | |||
140 | /* Load magic initialization constants. | ||
141 | |||
142 | */ | ||
143 | context->state[0] = 0x67452301; | ||
144 | context->state[1] = 0xefcdab89; | ||
145 | context->state[2] = 0x98badcfe; | ||
146 | context->state[3] = 0x10325476; | ||
147 | } | ||
148 | |||
149 | /* MD5 block update operation. Continues an MD5 message-digest | ||
150 | operation, processing another message block, and updating the context. | ||
151 | */ | ||
152 | |||
153 | void MD5Update (context, input, inputLen) | ||
154 | MD5_CTX *context; /* context */ | ||
155 | unsigned char *input; /* input block */ | ||
156 | unsigned int inputLen; /* length of input block */ | ||
157 | { | ||
158 | unsigned int i, index, partLen; | ||
159 | |||
160 | /* Compute number of bytes mod 64 */ | ||
161 | index = (unsigned int)((context->count[0] >> 3) & 0x3F); | ||
162 | |||
163 | /* Update number of bits */ | ||
164 | if ((context->count[0] += ((UINT4)inputLen << 3)) | ||
165 | < ((UINT4)inputLen << 3)) | ||
166 | context->count[1]++; | ||
167 | context->count[1] += ((UINT4)inputLen >> 29); | ||
168 | |||
169 | partLen = 64 - index; | ||
170 | |||
171 | /* Transform as many times as possible. | ||
172 | |||
173 | */ | ||
174 | if (inputLen >= partLen) { | ||
175 | MD5_memcpy | ||
176 | ((POINTER)&context->buffer[index], (POINTER)input, partLen); MD5Transform | ||
177 | (context->state, context->buffer); | ||
178 | |||
179 | for (i = partLen; i + 63 < inputLen; i += 64) | ||
180 | MD5Transform (context->state, &input[i]); | ||
181 | |||
182 | index = 0; | ||
183 | } | ||
184 | else | ||
185 | i = 0; | ||
186 | |||
187 | /* Buffer remaining input */ | ||
188 | MD5_memcpy | ||
189 | ((POINTER)&context->buffer[index], (POINTER)&input[i], | ||
190 | inputLen-i); | ||
191 | |||
192 | } | ||
193 | |||
194 | /* MD5 finalization. Ends an MD5 message-digest operation, writing the | ||
195 | the message digest and zeroizing the context. | ||
196 | |||
197 | */ | ||
198 | |||
199 | void MD5Final (digest, context) | ||
200 | unsigned char digest[16]; /* message digest */ | ||
201 | MD5_CTX *context; /* context */ | ||
202 | { | ||
203 | unsigned char bits[8]; | ||
204 | unsigned int index, padLen; | ||
205 | |||
206 | /* Save number of bits */ | ||
207 | Encode (bits, context->count, 8); | ||
208 | |||
209 | /* Pad out to 56 mod 64. | ||
210 | |||
211 | */ | ||
212 | index = (unsigned int)((context->count[0] >> 3) & 0x3f); | ||
213 | padLen = (index < 56) ? (56 - index) : (120 - index); | ||
214 | MD5Update (context, PADDING, padLen); | ||
215 | |||
216 | /* Append length (before padding) */ | ||
217 | MD5Update (context, bits, 8); | ||
218 | |||
219 | /* Store state in digest */ | ||
220 | Encode (digest, context->state, 16); | ||
221 | |||
222 | /* Zeroize sensitive information. | ||
223 | |||
224 | */ | ||
225 | MD5_memset ((POINTER)context, 0, sizeof (*context)); | ||
226 | } | ||
227 | |||
228 | /* MD5 basic transformation. Transforms state based on block. | ||
229 | |||
230 | */ | ||
231 | |||
232 | static void MD5Transform (state, block) | ||
233 | UINT4 state[4]; | ||
234 | unsigned char block[64]; | ||
235 | { | ||
236 | UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16]; | ||
237 | |||
238 | Decode (x, block, 64); | ||
239 | |||
240 | /* Round 1 */ | ||
241 | FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */ | ||
242 | FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */ | ||
243 | FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */ | ||
244 | FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */ | ||
245 | FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */ | ||
246 | FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */ | ||
247 | FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */ | ||
248 | FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */ | ||
249 | FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */ | ||
250 | FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */ | ||
251 | FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */ | ||
252 | FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */ | ||
253 | FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */ | ||
254 | FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */ | ||
255 | FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */ | ||
256 | FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */ | ||
257 | |||
258 | /* Round 2 */ | ||
259 | GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */ | ||
260 | GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */ | ||
261 | GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */ | ||
262 | GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */ | ||
263 | GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */ | ||
264 | GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */ | ||
265 | GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */ | ||
266 | GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */ | ||
267 | GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */ | ||
268 | GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */ | ||
269 | GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */ | ||
270 | GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */ | ||
271 | GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */ | ||
272 | GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */ | ||
273 | GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */ | ||
274 | GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */ | ||
275 | |||
276 | /* Round 3 */ | ||
277 | HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */ | ||
278 | HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */ | ||
279 | HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */ | ||
280 | HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */ | ||
281 | HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */ | ||
282 | HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */ | ||
283 | HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */ | ||
284 | HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */ | ||
285 | HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */ | ||
286 | HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */ | ||
287 | HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */ | ||
288 | HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */ | ||
289 | HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */ | ||
290 | HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */ | ||
291 | HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */ | ||
292 | HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */ | ||
293 | |||
294 | /* Round 4 */ | ||
295 | II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */ | ||
296 | II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */ | ||
297 | II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */ | ||
298 | II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */ | ||
299 | II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */ | ||
300 | II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */ | ||
301 | II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */ | ||
302 | II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */ | ||
303 | II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */ | ||
304 | II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */ | ||
305 | II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */ | ||
306 | II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */ | ||
307 | II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */ | ||
308 | II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */ | ||
309 | II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */ | ||
310 | II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */ | ||
311 | |||
312 | state[0] += a; | ||
313 | state[1] += b; | ||
314 | state[2] += c; | ||
315 | state[3] += d; | ||
316 | |||
317 | /* Zeroize sensitive information. | ||
318 | */ | ||
319 | MD5_memset ((POINTER)x, 0, sizeof (x)); | ||
320 | } | ||
321 | |||
322 | /* Encodes input (UINT4) into output (unsigned char). Assumes len is | ||
323 | a multiple of 4. | ||
324 | |||
325 | */ | ||
326 | |||
327 | static void Encode (output, input, len) | ||
328 | unsigned char *output; | ||
329 | UINT4 *input; | ||
330 | unsigned int len; | ||
331 | { | ||
332 | unsigned int i, j; | ||
333 | |||
334 | for (i = 0, j = 0; j < len; i++, j += 4) { | ||
335 | output[j] = (unsigned char)(input[i] & 0xff); | ||
336 | output[j+1] = (unsigned char)((input[i] >> 8) & 0xff); | ||
337 | output[j+2] = (unsigned char)((input[i] >> 16) & 0xff); | ||
338 | output[j+3] = (unsigned char)((input[i] >> 24) & 0xff); | ||
339 | } | ||
340 | } | ||
341 | |||
342 | /* Decodes input (unsigned char) into output (UINT4). Assumes len is | ||
343 | a multiple of 4. | ||
344 | |||
345 | */ | ||
346 | |||
347 | static void Decode (output, input, len) | ||
348 | UINT4 *output; | ||
349 | unsigned char *input; | ||
350 | unsigned int len; | ||
351 | { | ||
352 | unsigned int i, j; | ||
353 | |||
354 | for (i = 0, j = 0; j < len; i++, j += 4) | ||
355 | output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) | (((UINT4)input[j+2]) << 16) | ||
356 | | (((UINT4)input[j+3]) << 24); | ||
357 | } | ||
358 | |||
359 | /* Note: Replace "for loop" with standard memcpy if possible. | ||
360 | |||
361 | */ | ||
362 | |||
363 | static void MD5_memcpy (output, input, len) | ||
364 | POINTER output; | ||
365 | POINTER input; | ||
366 | unsigned int len; | ||
367 | { | ||
368 | unsigned int i; | ||
369 | |||
370 | for (i = 0; i < len; i++) | ||
371 | output[i] = input[i]; | ||
372 | } | ||
373 | |||
374 | /* Note: Replace "for loop" with standard memset if possible. | ||
375 | */ | ||
376 | |||
377 | static void MD5_memset (output, value, len) | ||
378 | POINTER output; | ||
379 | int value; | ||
380 | unsigned int len; | ||
381 | { | ||
382 | unsigned int i; | ||
383 | |||
384 | for (i = 0; i < len; i++) | ||
385 | ((char *)output)[i] = (char)value; | ||
386 | } | ||
387 | |||
388 | void hmac_md5_init(HMAC_MD5_CTX *hmac, | ||
389 | const unsigned char *key, | ||
390 | int key_len) | ||
391 | { | ||
392 | unsigned char k_ipad[65]; /* inner padding - | ||
393 | * key XORd with ipad | ||
394 | */ | ||
395 | unsigned char k_opad[65]; /* outer padding - | ||
396 | * key XORd with opad | ||
397 | */ | ||
398 | unsigned char tk[16]; | ||
399 | int i; | ||
400 | /* if key is longer than 64 bytes reset it to key=MD5(key) */ | ||
401 | if (key_len > 64) { | ||
402 | |||
403 | MD5_CTX tctx; | ||
404 | |||
405 | MD5Init(&tctx); | ||
406 | MD5Update(&tctx, key, key_len); | ||
407 | MD5Final(tk, &tctx); | ||
408 | |||
409 | key = tk; | ||
410 | key_len = 16; | ||
411 | } | ||
412 | |||
413 | /* | ||
414 | * the HMAC_MD5 transform looks like: | ||
415 | * | ||
416 | * MD5(K XOR opad, MD5(K XOR ipad, text)) | ||
417 | * | ||
418 | * where K is an n byte key | ||
419 | * ipad is the byte 0x36 repeated 64 times | ||
420 | * opad is the byte 0x5c repeated 64 times | ||
421 | * and text is the data being protected | ||
422 | */ | ||
423 | |||
424 | /* start out by storing key in pads */ | ||
425 | MD5_memset(k_ipad, '\0', sizeof k_ipad); | ||
426 | MD5_memset(k_opad, '\0', sizeof k_opad); | ||
427 | MD5_memcpy( k_ipad, key, key_len); | ||
428 | MD5_memcpy( k_opad, key, key_len); | ||
429 | |||
430 | /* XOR key with ipad and opad values */ | ||
431 | for (i=0; i<64; i++) { | ||
432 | k_ipad[i] ^= 0x36; | ||
433 | k_opad[i] ^= 0x5c; | ||
434 | } | ||
435 | |||
436 | MD5Init(&hmac->ictx); /* init inner context */ | ||
437 | MD5Update(&hmac->ictx, k_ipad, 64); /* apply inner pad */ | ||
438 | |||
439 | MD5Init(&hmac->octx); /* init outer context */ | ||
440 | MD5Update(&hmac->octx, k_opad, 64); /* apply outer pad */ | ||
441 | |||
442 | /* scrub the pads and key context (if used) */ | ||
443 | MD5_memset(&k_ipad, 0, sizeof(k_ipad)); | ||
444 | MD5_memset(&k_opad, 0, sizeof(k_opad)); | ||
445 | MD5_memset(&tk, 0, sizeof(tk)); | ||
446 | |||
447 | /* and we're done. */ | ||
448 | } | ||
449 | |||
450 | /* The precalc and import routines here rely on the fact that we pad | ||
451 | * the key out to 64 bytes and use that to initialize the md5 | ||
452 | * contexts, and that updating an md5 context with 64 bytes of data | ||
453 | * leaves nothing left over; all of the interesting state is contained | ||
454 | * in the state field, and none of it is left over in the count and | ||
455 | * buffer fields. So all we have to do is save the state field; we | ||
456 | * can zero the others when we reload it. Which is why the decision | ||
457 | * was made to pad the key out to 64 bytes in the first place. */ | ||
458 | void hmac_md5_precalc(HMAC_MD5_STATE *state, | ||
459 | const unsigned char *key, | ||
460 | int key_len) | ||
461 | { | ||
462 | HMAC_MD5_CTX hmac; | ||
463 | unsigned lupe; | ||
464 | |||
465 | hmac_md5_init(&hmac, key, key_len); | ||
466 | for (lupe = 0; lupe < 4; lupe++) { | ||
467 | state->istate[lupe] = htonl(hmac.ictx.state[lupe]); | ||
468 | state->ostate[lupe] = htonl(hmac.octx.state[lupe]); | ||
469 | } | ||
470 | MD5_memset(&hmac, 0, sizeof(hmac)); | ||
471 | } | ||
472 | |||
473 | |||
474 | void hmac_md5_import(HMAC_MD5_CTX *hmac, | ||
475 | HMAC_MD5_STATE *state) | ||
476 | { | ||
477 | unsigned lupe; | ||
478 | MD5_memset(hmac, 0, sizeof(HMAC_MD5_CTX)); | ||
479 | for (lupe = 0; lupe < 4; lupe++) { | ||
480 | hmac->ictx.state[lupe] = ntohl(state->istate[lupe]); | ||
481 | hmac->octx.state[lupe] = ntohl(state->ostate[lupe]); | ||
482 | } | ||
483 | /* Init the counts to account for our having applied | ||
484 | * 64 bytes of key; this works out to 0x200 (64 << 3; see | ||
485 | * MD5Update above...) */ | ||
486 | hmac->ictx.count[0] = hmac->octx.count[0] = 0x200; | ||
487 | } | ||
488 | |||
489 | void hmac_md5_final(unsigned char digest[HMAC_MD5_SIZE], | ||
490 | HMAC_MD5_CTX *hmac) | ||
491 | { | ||
492 | MD5Final(digest, &hmac->ictx); /* Finalize inner md5 */ | ||
493 | MD5Update(&hmac->octx, digest, 16); /* Update outer ctx */ | ||
494 | MD5Final(digest, &hmac->octx); /* Finalize outer md5 */ | ||
495 | } | ||
496 | |||
497 | |||
498 | void hmac_md5(text, text_len, key, key_len, digest) | ||
499 | const unsigned char* text; /* pointer to data stream */ | ||
500 | int text_len; /* length of data stream */ | ||
501 | const unsigned char* key; /* pointer to authentication key */ | ||
502 | int key_len; /* length of authentication key */ | ||
503 | unsigned char *digest; /* caller digest to be filled in */ | ||
504 | { | ||
505 | MD5_CTX context; | ||
506 | |||
507 | unsigned char k_ipad[65]; /* inner padding - | ||
508 | * key XORd with ipad | ||
509 | */ | ||
510 | unsigned char k_opad[65]; /* outer padding - | ||
511 | * key XORd with opad | ||
512 | */ | ||
513 | unsigned char tk[16]; | ||
514 | int i; | ||
515 | /* if key is longer than 64 bytes reset it to key=MD5(key) */ | ||
516 | if (key_len > 64) { | ||
517 | |||
518 | MD5_CTX tctx; | ||
519 | |||
520 | MD5Init(&tctx); | ||
521 | MD5Update(&tctx, key, key_len); | ||
522 | MD5Final(tk, &tctx); | ||
523 | |||
524 | key = tk; | ||
525 | key_len = 16; | ||
526 | } | ||
527 | |||
528 | /* | ||
529 | * the HMAC_MD5 transform looks like: | ||
530 | * | ||
531 | * MD5(K XOR opad, MD5(K XOR ipad, text)) | ||
532 | * | ||
533 | * where K is an n byte key | ||
534 | * ipad is the byte 0x36 repeated 64 times | ||
535 | * opad is the byte 0x5c repeated 64 times | ||
536 | * and text is the data being protected | ||
537 | */ | ||
538 | |||
539 | /* start out by storing key in pads */ | ||
540 | MD5_memset(k_ipad, '\0', sizeof k_ipad); | ||
541 | MD5_memset(k_opad, '\0', sizeof k_opad); | ||
542 | MD5_memcpy( k_ipad, key, key_len); | ||
543 | MD5_memcpy( k_opad, key, key_len); | ||
544 | |||
545 | /* XOR key with ipad and opad values */ | ||
546 | for (i=0; i<64; i++) { | ||
547 | k_ipad[i] ^= 0x36; | ||
548 | k_opad[i] ^= 0x5c; | ||
549 | } | ||
550 | /* | ||
551 | * perform inner MD5 | ||
552 | */ | ||
553 | |||
554 | MD5Init(&context); /* init context for 1st | ||
555 | * pass */ | ||
556 | MD5Update(&context, k_ipad, 64); /* start with inner pad */ | ||
557 | MD5Update(&context, text, text_len); /* then text of datagram */ | ||
558 | MD5Final(digest, &context); /* finish up 1st pass */ | ||
559 | |||
560 | /* | ||
561 | * perform outer MD5 | ||
562 | */ | ||
563 | MD5Init(&context); /* init context for 2nd | ||
564 | * pass */ | ||
565 | MD5Update(&context, k_opad, 64); /* start with outer pad */ | ||
566 | MD5Update(&context, digest, 16); /* then results of 1st | ||
567 | * hash */ | ||
568 | MD5Final(digest, &context); /* finish up 2nd pass */ | ||
569 | |||
570 | } | ||
diff --git a/kmicromail/libetpan/tools/md5.h b/kmicromail/libetpan/tools/md5.h new file mode 100644 index 0000000..e62f157 --- a/dev/null +++ b/kmicromail/libetpan/tools/md5.h | |||
@@ -0,0 +1,88 @@ | |||
1 | /* | ||
2 | * libEtPan! -- a mail stuff library | ||
3 | * | ||
4 | * Copyright (C) 2001, 2002 - DINH Viet Hoa | ||
5 | * All rights reserved. | ||
6 | * | ||
7 | * Redistribution and use in source and binary forms, with or without | ||
8 | * modification, are permitted provided that the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. Neither the name of the libEtPan! project nor the names of its | ||
16 | * contributors may be used to endorse or promote products derived | ||
17 | * from this software without specific prior written permission. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
29 | * SUCH DAMAGE. | ||
30 | */ | ||
31 | |||
32 | /* | ||
33 | * $Id$ | ||
34 | */ | ||
35 | |||
36 | /* MD5.H - header file for MD5C.C | ||
37 | */ | ||
38 | |||
39 | /* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All | ||
40 | rights reserved. | ||
41 | |||
42 | License to copy and use this software is granted provided that it | ||
43 | is identified as the "RSA Data Security, Inc. MD5 Message-Digest | ||
44 | Algorithm" in all material mentioning or referencing this software | ||
45 | or this function. | ||
46 | |||
47 | License is also granted to make and use derivative works provided | ||
48 | that such works are identified as "derived from the RSA Data | ||
49 | Security, Inc. MD5 Message-Digest Algorithm" in all material | ||
50 | mentioning or referencing the derived work. | ||
51 | |||
52 | RSA Data Security, Inc. makes no representations concerning either | ||
53 | the merchantability of this software or the suitability of this | ||
54 | software for any particular purpose. It is provided "as is" | ||
55 | without express or implied warranty of any kind. | ||
56 | These notices must be retained in any copies of any part of this | ||
57 | documentation and/or software. | ||
58 | */ | ||
59 | |||
60 | #include "md5global.h" | ||
61 | |||
62 | #ifndef MD5_H | ||
63 | |||
64 | #define MD5_H | ||
65 | |||
66 | #ifdef __cplusplus | ||
67 | extern "C" { | ||
68 | #endif | ||
69 | |||
70 | /* MD5 context. */ | ||
71 | typedef struct { | ||
72 | UINT4 state[4]; /* state (ABCD) */ | ||
73 | UINT4 count[2]; /* number of bits, modulo 2^64 (lsb first) */ | ||
74 | unsigned char buffer[64]; /* input buffer */ | ||
75 | } MD5_CTX; | ||
76 | |||
77 | void MD5Init PROTO_LIST ((MD5_CTX *)); | ||
78 | void MD5Update PROTO_LIST | ||
79 | ((MD5_CTX *, unsigned char *, unsigned int)); | ||
80 | void MD5Final PROTO_LIST ((unsigned char [16], MD5_CTX *)); | ||
81 | |||
82 | void hmac_md5 PROTO_LIST ((unsigned char *, int, unsigned char *, int, caddr_t)); | ||
83 | |||
84 | #ifdef __cplusplus | ||
85 | } | ||
86 | #endif | ||
87 | |||
88 | #endif | ||
diff --git a/kmicromail/libetpan/tools/md5global.h b/kmicromail/libetpan/tools/md5global.h new file mode 100644 index 0000000..9089c9a --- a/dev/null +++ b/kmicromail/libetpan/tools/md5global.h | |||
@@ -0,0 +1,79 @@ | |||
1 | /* | ||
2 | * libEtPan! -- a mail stuff library | ||
3 | * | ||
4 | * Copyright (C) 2001, 2002 - DINH Viet Hoa | ||
5 | * All rights reserved. | ||
6 | * | ||
7 | * Redistribution and use in source and binary forms, with or without | ||
8 | * modification, are permitted provided that the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. Neither the name of the libEtPan! project nor the names of its | ||
16 | * contributors may be used to endorse or promote products derived | ||
17 | * from this software without specific prior written permission. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
29 | * SUCH DAMAGE. | ||
30 | */ | ||
31 | |||
32 | /* | ||
33 | * $Id$ | ||
34 | */ | ||
35 | |||
36 | /* GLOBAL.H - RSAREF types and constants | ||
37 | */ | ||
38 | |||
39 | #ifndef MD5GLOBAL_H | ||
40 | |||
41 | #define MD5GLOBAL_H | ||
42 | |||
43 | #ifdef __cplusplus | ||
44 | extern "C" { | ||
45 | #endif | ||
46 | |||
47 | /* PROTOTYPES should be set to one if and only if the compiler supports | ||
48 | function argument prototyping. | ||
49 | The following makes PROTOTYPES default to 0 if it has not already | ||
50 | been defined with C compiler flags. | ||
51 | */ | ||
52 | #ifndef PROTOTYPES | ||
53 | #define PROTOTYPES 0 | ||
54 | #endif | ||
55 | |||
56 | /* POINTER defines a generic pointer type */ | ||
57 | typedef unsigned char *POINTER; | ||
58 | |||
59 | /* UINT2 defines a two byte word */ | ||
60 | typedef unsigned short int UINT2; | ||
61 | |||
62 | /* UINT4 defines a four byte word */ | ||
63 | typedef unsigned long int UINT4; | ||
64 | |||
65 | /* PROTO_LIST is defined depending on how PROTOTYPES is defined above. | ||
66 | If using PROTOTYPES, then PROTO_LIST returns the list, otherwise it | ||
67 | returns an empty list. | ||
68 | */ | ||
69 | #if PROTOTYPES | ||
70 | #define PROTO_LIST(list) list | ||
71 | #else | ||
72 | #define PROTO_LIST(list) () | ||
73 | #endif | ||
74 | |||
75 | #ifdef __cplusplus | ||
76 | } | ||
77 | #endif | ||
78 | |||
79 | #endif | ||
diff --git a/kmicromail/libetpan/tools/mmapstring.c b/kmicromail/libetpan/tools/mmapstring.c new file mode 100644 index 0000000..8c44842 --- a/dev/null +++ b/kmicromail/libetpan/tools/mmapstring.c | |||
@@ -0,0 +1,526 @@ | |||
1 | /* | ||
2 | * libEtPan! -- a mail stuff library | ||
3 | * | ||
4 | * Copyright (C) 2001, 2002 - DINH Viet Hoa | ||
5 | * All rights reserved. | ||
6 | * | ||
7 | * Redistribution and use in source and binary forms, with or without | ||
8 | * modification, are permitted provided that the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. Neither the name of the libEtPan! project nor the names of its | ||
16 | * contributors may be used to endorse or promote products derived | ||
17 | * from this software without specific prior written permission. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
29 | * SUCH DAMAGE. | ||
30 | */ | ||
31 | |||
32 | /* | ||
33 | * $Id$ | ||
34 | */ | ||
35 | |||
36 | #include "mmapstring.h" | ||
37 | |||
38 | #include "chash.h" | ||
39 | |||
40 | #include <stdlib.h> | ||
41 | #include <unistd.h> | ||
42 | #include <sys/mman.h> | ||
43 | #include <string.h> | ||
44 | #include <pthread.h> | ||
45 | |||
46 | #include "libetpan-config.h" | ||
47 | |||
48 | #define MAX(a, b) ((a) > (b) ? (a) : (b)) | ||
49 | #define MIN(a, b) ((a) < (b) ? (a) : (b)) | ||
50 | |||
51 | #define MMAP_STRING_DEFAULT_CEIL (8 * 1024 * 1024) | ||
52 | |||
53 | #define DEFAULT_TMP_PATH "/tmp" | ||
54 | |||
55 | static char tmpdir[PATH_MAX] = DEFAULT_TMP_PATH; | ||
56 | |||
57 | static size_t mmap_string_ceil = MMAP_STRING_DEFAULT_CEIL; | ||
58 | |||
59 | /* MMAPString references */ | ||
60 | |||
61 | static pthread_mutex_t mmapstring_lock = PTHREAD_MUTEX_INITIALIZER; | ||
62 | static chash * mmapstring_hashtable = NULL; | ||
63 | |||
64 | static void mmapstring_hashtable_init() | ||
65 | { | ||
66 | mmapstring_hashtable = chash_new(CHASH_DEFAULTSIZE, CHASH_COPYKEY); | ||
67 | } | ||
68 | |||
69 | void mmap_string_set_tmpdir(char * directory) | ||
70 | { | ||
71 | strncpy(tmpdir, directory, PATH_MAX); | ||
72 | tmpdir[PATH_MAX - 1] = 0; | ||
73 | } | ||
74 | |||
75 | |||
76 | int mmap_string_ref(MMAPString * string) | ||
77 | { | ||
78 | chash * ht; | ||
79 | int r; | ||
80 | chashdatum key; | ||
81 | chashdatum data; | ||
82 | |||
83 | pthread_mutex_lock(&mmapstring_lock); | ||
84 | if (mmapstring_hashtable == NULL) { | ||
85 | mmapstring_hashtable_init(); | ||
86 | } | ||
87 | ht = mmapstring_hashtable; | ||
88 | |||
89 | if (ht == NULL) { | ||
90 | pthread_mutex_unlock(&mmapstring_lock); | ||
91 | return -1; | ||
92 | } | ||
93 | |||
94 | key.data = &string->str; | ||
95 | key.len = sizeof(string->str); | ||
96 | data.data = string; | ||
97 | data.len = 0; | ||
98 | |||
99 | r = chash_set(mmapstring_hashtable, &key, &data, NULL); | ||
100 | pthread_mutex_unlock(&mmapstring_lock); | ||
101 | |||
102 | if (r < 0) | ||
103 | return r; | ||
104 | |||
105 | return 0; | ||
106 | } | ||
107 | |||
108 | int mmap_string_unref(char * str) | ||
109 | { | ||
110 | MMAPString * string; | ||
111 | chash * ht; | ||
112 | chashdatum key; | ||
113 | chashdatum data; | ||
114 | int r; | ||
115 | |||
116 | pthread_mutex_lock(&mmapstring_lock); | ||
117 | ht = mmapstring_hashtable; | ||
118 | |||
119 | if (ht == NULL) { | ||
120 | pthread_mutex_unlock(&mmapstring_lock); | ||
121 | return -1; | ||
122 | } | ||
123 | |||
124 | key.data = &str; | ||
125 | key.len = sizeof(str); | ||
126 | |||
127 | r = chash_get(ht, &key, &data); | ||
128 | if (r < 0) | ||
129 | string = NULL; | ||
130 | else | ||
131 | string = data.data; | ||
132 | |||
133 | if (string != NULL) { | ||
134 | chash_delete(ht, &key, NULL); | ||
135 | if (chash_count(ht) == 0) { | ||
136 | chash_free(ht); | ||
137 | mmapstring_hashtable = NULL; | ||
138 | } | ||
139 | } | ||
140 | |||
141 | pthread_mutex_unlock(&mmapstring_lock); | ||
142 | |||
143 | if (string != NULL) { | ||
144 | mmap_string_free(string); | ||
145 | return 0; | ||
146 | } | ||
147 | else | ||
148 | return -1; | ||
149 | } | ||
150 | |||
151 | |||
152 | |||
153 | /* MMAPString */ | ||
154 | |||
155 | #define MY_MAXSIZE ((size_t) -1) | ||
156 | |||
157 | static inline size_t | ||
158 | nearest_power (size_t base, size_t num) | ||
159 | { | ||
160 | if (num > MY_MAXSIZE / 2) { | ||
161 | return MY_MAXSIZE; | ||
162 | } | ||
163 | else { | ||
164 | size_t n = base; | ||
165 | |||
166 | while (n < num) | ||
167 | n <<= 1; | ||
168 | |||
169 | return n; | ||
170 | } | ||
171 | } | ||
172 | |||
173 | void mmap_string_set_ceil(size_t ceil) | ||
174 | { | ||
175 | mmap_string_ceil = ceil; | ||
176 | } | ||
177 | |||
178 | /* Strings. | ||
179 | */ | ||
180 | |||
181 | static MMAPString * mmap_string_realloc_file(MMAPString * string) | ||
182 | { | ||
183 | char * data; | ||
184 | |||
185 | if (string->fd == -1) { | ||
186 | char tmpfilename[PATH_MAX]; | ||
187 | int fd; | ||
188 | |||
189 | * tmpfilename = 0; | ||
190 | strcat(tmpfilename, tmpdir); | ||
191 | strcat(tmpfilename, "/libetpan-mmapstring-XXXXXX"); | ||
192 | |||
193 | fd = mkstemp(tmpfilename); | ||
194 | if (fd == -1) | ||
195 | return NULL; | ||
196 | |||
197 | if (unlink(tmpfilename) == -1) { | ||
198 | close(fd); | ||
199 | return NULL; | ||
200 | } | ||
201 | |||
202 | if (ftruncate(fd, string->allocated_len) == -1) { | ||
203 | close(fd); | ||
204 | return NULL; | ||
205 | } | ||
206 | |||
207 | data = mmap(NULL, string->allocated_len, PROT_WRITE | PROT_READ, | ||
208 | MAP_SHARED, fd, 0); | ||
209 | |||
210 | if (data == MAP_FAILED) { | ||
211 | close(fd); | ||
212 | return NULL; | ||
213 | } | ||
214 | |||
215 | if (string->str != NULL) | ||
216 | memcpy(data, string->str, string->len + 1); | ||
217 | |||
218 | string->fd = fd; | ||
219 | string->mmapped_size = string->allocated_len; | ||
220 | free(string->str); | ||
221 | string->str = data; | ||
222 | } | ||
223 | else { | ||
224 | if (munmap(string->str, string->mmapped_size) == -1) | ||
225 | return NULL; | ||
226 | |||
227 | if (ftruncate(string->fd, string->allocated_len) == -1) | ||
228 | return NULL; | ||
229 | |||
230 | data = mmap(NULL, string->allocated_len, PROT_WRITE | PROT_READ, | ||
231 | MAP_SHARED, string->fd, 0); | ||
232 | |||
233 | if (data == MAP_FAILED) | ||
234 | return NULL; | ||
235 | |||
236 | string->mmapped_size = string->allocated_len; | ||
237 | string->str = data; | ||
238 | } | ||
239 | |||
240 | return string; | ||
241 | } | ||
242 | |||
243 | static MMAPString * mmap_string_realloc_memory(MMAPString * string) | ||
244 | { | ||
245 | char * tmp; | ||
246 | |||
247 | tmp = realloc (string->str, string->allocated_len); | ||
248 | |||
249 | if (tmp == NULL) | ||
250 | string = NULL; | ||
251 | else | ||
252 | string->str = tmp; | ||
253 | |||
254 | return string; | ||
255 | } | ||
256 | |||
257 | static MMAPString * | ||
258 | mmap_string_maybe_expand (MMAPString* string, | ||
259 | size_t len) | ||
260 | { | ||
261 | if (string->len + len >= string->allocated_len) | ||
262 | { | ||
263 | size_t old_size; | ||
264 | MMAPString * newstring; | ||
265 | |||
266 | old_size = string->allocated_len; | ||
267 | |||
268 | string->allocated_len = nearest_power (1, string->len + len + 1); | ||
269 | |||
270 | #ifndef MMAP_UNAVAILABLE | ||
271 | if (string->allocated_len > mmap_string_ceil) | ||
272 | newstring = mmap_string_realloc_file(string); | ||
273 | else { | ||
274 | #endif | ||
275 | newstring = mmap_string_realloc_memory(string); | ||
276 | #ifndef MMAP_UNAVAILABLE | ||
277 | if (newstring == NULL) | ||
278 | newstring = mmap_string_realloc_file(string); | ||
279 | } | ||
280 | #endif | ||
281 | |||
282 | if (newstring == NULL) | ||
283 | string->allocated_len = old_size; | ||
284 | } | ||
285 | |||
286 | return string; | ||
287 | } | ||
288 | |||
289 | MMAPString* | ||
290 | mmap_string_sized_new (size_t dfl_size) | ||
291 | { | ||
292 | MMAPString *string; | ||
293 | |||
294 | string = malloc(sizeof(* string)); | ||
295 | if (string == NULL) | ||
296 | return NULL; | ||
297 | |||
298 | string->allocated_len = 0; | ||
299 | string->len = 0; | ||
300 | string->str = NULL; | ||
301 | string->fd = -1; | ||
302 | string->mmapped_size = 0; | ||
303 | |||
304 | if (mmap_string_maybe_expand (string, MAX (dfl_size, 2)) == NULL) | ||
305 | return NULL; | ||
306 | |||
307 | string->str[0] = 0; | ||
308 | |||
309 | return string; | ||
310 | } | ||
311 | |||
312 | MMAPString* | ||
313 | mmap_string_new (const char *init) | ||
314 | { | ||
315 | MMAPString *string; | ||
316 | |||
317 | string = mmap_string_sized_new (init ? strlen (init) + 2 : 2); | ||
318 | if (string == NULL) | ||
319 | return NULL; | ||
320 | |||
321 | if (init) | ||
322 | mmap_string_append (string, init); | ||
323 | |||
324 | return string; | ||
325 | } | ||
326 | |||
327 | MMAPString* | ||
328 | mmap_string_new_len (const char *init, | ||
329 | size_t len) | ||
330 | { | ||
331 | MMAPString *string; | ||
332 | |||
333 | if (len <= 0) | ||
334 | return mmap_string_new (init); | ||
335 | else | ||
336 | { | ||
337 | string = mmap_string_sized_new (len); | ||
338 | |||
339 | if (init) | ||
340 | mmap_string_append_len (string, init, len); | ||
341 | |||
342 | return string; | ||
343 | } | ||
344 | } | ||
345 | |||
346 | void | ||
347 | mmap_string_free (MMAPString *string) | ||
348 | { | ||
349 | if (string == NULL) | ||
350 | return; | ||
351 | |||
352 | if (string->fd != -1) { | ||
353 | munmap(string->str, string->mmapped_size); | ||
354 | close(string->fd); | ||
355 | } | ||
356 | else { | ||
357 | free (string->str); | ||
358 | } | ||
359 | free(string); | ||
360 | } | ||
361 | |||
362 | MMAPString* | ||
363 | mmap_string_assign (MMAPString *string, | ||
364 | const char *rval) | ||
365 | { | ||
366 | mmap_string_truncate (string, 0); | ||
367 | if (mmap_string_append (string, rval) == NULL) | ||
368 | return NULL; | ||
369 | |||
370 | return string; | ||
371 | } | ||
372 | |||
373 | MMAPString* | ||
374 | mmap_string_truncate (MMAPString *string, | ||
375 | size_t len) | ||
376 | { | ||
377 | string->len = MIN (len, string->len); | ||
378 | string->str[string->len] = 0; | ||
379 | |||
380 | return string; | ||
381 | } | ||
382 | |||
383 | /** | ||
384 | * mmap_string_set_size: | ||
385 | * @string: a #MMAPString | ||
386 | * @len: the new length | ||
387 | * | ||
388 | * Sets the length of a #MMAPString. If the length is less than | ||
389 | * the current length, the string will be truncated. If the | ||
390 | * length is greater than the current length, the contents | ||
391 | * of the newly added area are undefined. (However, as | ||
392 | * always, string->str[string->len] will be a nul byte.) | ||
393 | * | ||
394 | * Return value: @string | ||
395 | **/ | ||
396 | MMAPString* | ||
397 | mmap_string_set_size (MMAPString *string, | ||
398 | size_t len) | ||
399 | { | ||
400 | if (len >= string->allocated_len) | ||
401 | if (mmap_string_maybe_expand (string, len - string->len) == NULL) | ||
402 | return NULL; | ||
403 | |||
404 | string->len = len; | ||
405 | string->str[len] = 0; | ||
406 | |||
407 | return string; | ||
408 | } | ||
409 | |||
410 | /* | ||
411 | static int in_mapped_zone(MMAPString * string, char * val) | ||
412 | { | ||
413 | return (val >= string->str) && (val < string->str + string->mmapped_size); | ||
414 | } | ||
415 | */ | ||
416 | |||
417 | MMAPString* | ||
418 | mmap_string_insert_len (MMAPString *string, | ||
419 | size_t pos, | ||
420 | const char *val, | ||
421 | size_t len) | ||
422 | { | ||
423 | if (mmap_string_maybe_expand (string, len) == NULL) | ||
424 | return NULL; | ||
425 | |||
426 | if (pos < string->len) | ||
427 | memmove (string->str + pos + len, string->str + pos, string->len - pos); | ||
428 | |||
429 | /* insert the new string */ | ||
430 | memmove (string->str + pos, val, len); | ||
431 | |||
432 | string->len += len; | ||
433 | |||
434 | string->str[string->len] = 0; | ||
435 | |||
436 | return string; | ||
437 | } | ||
438 | |||
439 | MMAPString* | ||
440 | mmap_string_append (MMAPString *string, | ||
441 | const char *val) | ||
442 | { | ||
443 | return mmap_string_insert_len (string, string->len, val, strlen(val)); | ||
444 | } | ||
445 | |||
446 | MMAPString* | ||
447 | mmap_string_append_len (MMAPString *string, | ||
448 | const char *val, | ||
449 | size_t len) | ||
450 | { | ||
451 | return mmap_string_insert_len (string, string->len, val, len); | ||
452 | } | ||
453 | |||
454 | MMAPString* | ||
455 | mmap_string_append_c (MMAPString *string, | ||
456 | char c) | ||
457 | { | ||
458 | return mmap_string_insert_c (string, string->len, c); | ||
459 | } | ||
460 | |||
461 | MMAPString* | ||
462 | mmap_string_prepend (MMAPString *string, | ||
463 | const char *val) | ||
464 | { | ||
465 | return mmap_string_insert_len (string, 0, val, strlen(val)); | ||
466 | } | ||
467 | |||
468 | MMAPString* | ||
469 | mmap_string_prepend_len (MMAPString *string, | ||
470 | const char *val, | ||
471 | size_t len) | ||
472 | { | ||
473 | return mmap_string_insert_len (string, 0, val, len); | ||
474 | } | ||
475 | |||
476 | MMAPString* | ||
477 | mmap_string_prepend_c (MMAPString *string, | ||
478 | char c) | ||
479 | { | ||
480 | return mmap_string_insert_c (string, 0, c); | ||
481 | } | ||
482 | |||
483 | MMAPString* | ||
484 | mmap_string_insert (MMAPString *string, | ||
485 | size_t pos, | ||
486 | const char *val) | ||
487 | { | ||
488 | return mmap_string_insert_len (string, pos, val, strlen(val)); | ||
489 | } | ||
490 | |||
491 | MMAPString* | ||
492 | mmap_string_insert_c (MMAPString *string, | ||
493 | size_t pos, | ||
494 | char c) | ||
495 | { | ||
496 | if (mmap_string_maybe_expand (string, 1) == NULL) | ||
497 | return NULL; | ||
498 | |||
499 | /* If not just an append, move the old stuff */ | ||
500 | if (pos < string->len) | ||
501 | memmove (string->str + pos + 1, string->str + pos, string->len - pos); | ||
502 | |||
503 | string->str[pos] = c; | ||
504 | |||
505 | string->len += 1; | ||
506 | |||
507 | string->str[string->len] = 0; | ||
508 | |||
509 | return string; | ||
510 | } | ||
511 | |||
512 | MMAPString* | ||
513 | mmap_string_erase (MMAPString *string, | ||
514 | size_t pos, | ||
515 | size_t len) | ||
516 | { | ||
517 | if ((pos + len) < string->len) | ||
518 | memmove (string->str + pos, string->str + pos + len, | ||
519 | string->len - (pos + len)); | ||
520 | |||
521 | string->len -= len; | ||
522 | |||
523 | string->str[string->len] = 0; | ||
524 | |||
525 | return string; | ||
526 | } | ||
diff --git a/kmicromail/libetpan/tools/mmapstring.h b/kmicromail/libetpan/tools/mmapstring.h new file mode 100644 index 0000000..6d7227d --- a/dev/null +++ b/kmicromail/libetpan/tools/mmapstring.h | |||
@@ -0,0 +1,136 @@ | |||
1 | /* | ||
2 | * libEtPan! -- a mail stuff library | ||
3 | * | ||
4 | * Copyright (C) 2001, 2002 - DINH Viet Hoa | ||
5 | * All rights reserved. | ||
6 | * | ||
7 | * Redistribution and use in source and binary forms, with or without | ||
8 | * modification, are permitted provided that the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. Neither the name of the libEtPan! project nor the names of its | ||
16 | * contributors may be used to endorse or promote products derived | ||
17 | * from this software without specific prior written permission. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
29 | * SUCH DAMAGE. | ||
30 | */ | ||
31 | |||
32 | /* | ||
33 | * $Id$ | ||
34 | */ | ||
35 | |||
36 | #ifndef __MMAP_STRING_H__ | ||
37 | |||
38 | #define __MMAP_STRING_H__ | ||
39 | |||
40 | #include <sys/types.h> | ||
41 | |||
42 | #ifdef __cplusplus | ||
43 | extern "C" { | ||
44 | #endif | ||
45 | |||
46 | /* | ||
47 | #define TMPDIR "/tmp" | ||
48 | */ | ||
49 | |||
50 | typedef struct _MMAPString MMAPString; | ||
51 | |||
52 | struct _MMAPString | ||
53 | { | ||
54 | char * str; | ||
55 | size_t len; | ||
56 | size_t allocated_len; | ||
57 | int fd; | ||
58 | size_t mmapped_size; | ||
59 | /* | ||
60 | char * old_non_mmapped_str; | ||
61 | */ | ||
62 | }; | ||
63 | |||
64 | /* configure location of mmaped files */ | ||
65 | |||
66 | void mmap_string_set_tmpdir(char * directory); | ||
67 | |||
68 | /* Strings | ||
69 | */ | ||
70 | |||
71 | MMAPString * mmap_string_new (const char * init); | ||
72 | |||
73 | MMAPString * mmap_string_new_len (const char * init, | ||
74 | size_t len); | ||
75 | |||
76 | MMAPString * mmap_string_sized_new (size_t dfl_size); | ||
77 | |||
78 | void mmap_string_free (MMAPString * string); | ||
79 | |||
80 | MMAPString * mmap_string_assign (MMAPString * string, | ||
81 | const char * rval); | ||
82 | |||
83 | MMAPString * mmap_string_truncate (MMAPString *string, | ||
84 | size_t len); | ||
85 | |||
86 | MMAPString * mmap_string_set_size (MMAPString * string, | ||
87 | size_t len); | ||
88 | |||
89 | MMAPString * mmap_string_insert_len (MMAPString * string, | ||
90 | size_t pos, | ||
91 | const char * val, | ||
92 | size_t len); | ||
93 | |||
94 | MMAPString * mmap_string_append (MMAPString * string, | ||
95 | const char * val); | ||
96 | |||
97 | MMAPString * mmap_string_append_len (MMAPString * string, | ||
98 | const char * val, | ||
99 | size_t len); | ||
100 | |||
101 | MMAPString * mmap_string_append_c (MMAPString * string, | ||
102 | char c); | ||
103 | |||
104 | MMAPString * mmap_string_prepend (MMAPString * string, | ||
105 | const char * val); | ||
106 | |||
107 | MMAPString * mmap_string_prepend_c (MMAPString * string, | ||
108 | char c); | ||
109 | |||
110 | MMAPString * mmap_string_prepend_len (MMAPString * string, | ||
111 | const char * val, | ||
112 | size_t len); | ||
113 | |||
114 | MMAPString * mmap_string_insert (MMAPString * string, | ||
115 | size_t pos, | ||
116 | const char * val); | ||
117 | |||
118 | MMAPString * mmap_string_insert_c (MMAPString *string, | ||
119 | size_t pos, | ||
120 | char c); | ||
121 | |||
122 | MMAPString * mmap_string_erase(MMAPString * string, | ||
123 | size_t pos, | ||
124 | size_t len); | ||
125 | |||
126 | void mmap_string_set_ceil(size_t ceil); | ||
127 | |||
128 | int mmap_string_ref(MMAPString * string); | ||
129 | int mmap_string_unref(char * str); | ||
130 | |||
131 | #ifdef __cplusplus | ||
132 | } | ||
133 | #endif | ||
134 | |||
135 | |||
136 | #endif /* __MMAP_STRING_H__ */ | ||