author | zautrix <zautrix> | 2004-11-09 11:57:44 (UTC) |
---|---|---|
committer | zautrix <zautrix> | 2004-11-09 11:57:44 (UTC) |
commit | 21c8ec57e0258815f3d1474c1194d1572f78029e (patch) (unidiff) | |
tree | a9088fc093487b09ed2448b548276db1395bd449 /kmicromail | |
parent | 318f9bf1f4bc84af280bd8e090989677ba21d241 (diff) | |
download | kdepimpi-21c8ec57e0258815f3d1474c1194d1572f78029e.zip kdepimpi-21c8ec57e0258815f3d1474c1194d1572f78029e.tar.gz kdepimpi-21c8ec57e0258815f3d1474c1194d1572f78029e.tar.bz2 |
aaded files
-rw-r--r-- | kmicromail/libetpan/tools/charconv.c | 2 |
1 files changed, 1 insertions, 1 deletions
diff --git a/kmicromail/libetpan/tools/charconv.c b/kmicromail/libetpan/tools/charconv.c index bf3de51..f7a3c89 100644 --- a/kmicromail/libetpan/tools/charconv.c +++ b/kmicromail/libetpan/tools/charconv.c | |||
@@ -1,251 +1,251 @@ | |||
1 | /* | 1 | /* |
2 | * libEtPan! -- a mail stuff library | 2 | * libEtPan! -- a mail stuff library |
3 | * | 3 | * |
4 | * Copyright (C) 2001, 2002 - DINH Viet Hoa | 4 | * Copyright (C) 2001, 2002 - DINH Viet Hoa |
5 | * All rights reserved. | 5 | * All rights reserved. |
6 | * | 6 | * |
7 | * Redistribution and use in source and binary forms, with or without | 7 | * Redistribution and use in source and binary forms, with or without |
8 | * modification, are permitted provided that the following conditions | 8 | * modification, are permitted provided that the following conditions |
9 | * are met: | 9 | * are met: |
10 | * 1. Redistributions of source code must retain the above copyright | 10 | * 1. Redistributions of source code must retain the above copyright |
11 | * notice, this list of conditions and the following disclaimer. | 11 | * notice, this list of conditions and the following disclaimer. |
12 | * 2. Redistributions in binary form must reproduce the above copyright | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
13 | * notice, this list of conditions and the following disclaimer in the | 13 | * notice, this list of conditions and the following disclaimer in the |
14 | * documentation and/or other materials provided with the distribution. | 14 | * documentation and/or other materials provided with the distribution. |
15 | * 3. Neither the name of the libEtPan! project nor the names of its | 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 | 16 | * contributors may be used to endorse or promote products derived |
17 | * from this software without specific prior written permission. | 17 | * from this software without specific prior written permission. |
18 | * | 18 | * |
19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | 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 | 20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | 24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | 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 | 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 | 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 | 28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
29 | * SUCH DAMAGE. | 29 | * SUCH DAMAGE. |
30 | */ | 30 | */ |
31 | 31 | ||
32 | /* | 32 | /* |
33 | * $Id$ | 33 | * $Id$ |
34 | */ | 34 | */ |
35 | 35 | ||
36 | #include "charconv.h" | 36 | #include "charconv.h" |
37 | 37 | ||
38 | #include "config.h" | 38 | #include "config.h" |
39 | #ifdef HAVE_ICONV | 39 | #ifdef HAVE_ICONV |
40 | #include <iconv.h> | 40 | #include <iconv.h> |
41 | #endif | 41 | #endif |
42 | #include <stdlib.h> | 42 | #include <stdlib.h> |
43 | #include <string.h> | 43 | #include <string.h> |
44 | #include <stdio.h> | 44 | #include <stdio.h> |
45 | #include <errno.h> | 45 | #include <errno.h> |
46 | 46 | ||
47 | #include "mmapstring.h" | 47 | #include "mmapstring.h" |
48 | |||
49 | #ifdef HAVE_ICONV | 48 | #ifdef HAVE_ICONV |
49 | |||
50 | static size_t mail_iconv (iconv_t cd, const char **inbuf, size_t *inbytesleft, | 50 | static size_t mail_iconv (iconv_t cd, const char **inbuf, size_t *inbytesleft, |
51 | char **outbuf, size_t *outbytesleft, | 51 | char **outbuf, size_t *outbytesleft, |
52 | char **inrepls, char *outrepl) | 52 | char **inrepls, char *outrepl) |
53 | { | 53 | { |
54 | /* | 54 | /* |
55 | XXX - force conversion of (* inbuf) to (char *) | 55 | XXX - force conversion of (* inbuf) to (char *) |
56 | because prototype of iconv() is the following : | 56 | because prototype of iconv() is the following : |
57 | 57 | ||
58 | size_t iconv(iconv_t cd, char **restrict inbuf, | 58 | size_t iconv(iconv_t cd, char **restrict inbuf, |
59 | size_t *restrict inbytesleft, char **restrict outbuf, | 59 | size_t *restrict inbytesleft, char **restrict outbuf, |
60 | size_t *restrict outbytesleft); | 60 | size_t *restrict outbytesleft); |
61 | */ | 61 | */ |
62 | 62 | ||
63 | size_t ret = 0, ret1; | 63 | size_t ret = 0, ret1; |
64 | char *ib = (char *) *inbuf; | 64 | char *ib = (char *) *inbuf; |
65 | size_t ibl = *inbytesleft; | 65 | size_t ibl = *inbytesleft; |
66 | char *ob = *outbuf; | 66 | char *ob = *outbuf; |
67 | size_t obl = *outbytesleft; | 67 | size_t obl = *outbytesleft; |
68 | 68 | ||
69 | for (;;) | 69 | for (;;) |
70 | { | 70 | { |
71 | ret1 = iconv (cd, &ib, &ibl, &ob, &obl); | 71 | ret1 = iconv (cd, &ib, &ibl, &ob, &obl); |
72 | if (ret1 != (size_t)-1) | 72 | if (ret1 != (size_t)-1) |
73 | ret += ret1; | 73 | ret += ret1; |
74 | if (ibl && obl && errno == EILSEQ) | 74 | if (ibl && obl && errno == EILSEQ) |
75 | { | 75 | { |
76 | if (inrepls) | 76 | if (inrepls) |
77 | { | 77 | { |
78 | /* Try replacing the input */ | 78 | /* Try replacing the input */ |
79 | char **t; | 79 | char **t; |
80 | for (t = inrepls; *t; t++) | 80 | for (t = inrepls; *t; t++) |
81 | { | 81 | { |
82 | char *ib1 = *t; | 82 | char *ib1 = *t; |
83 | size_t ibl1 = strlen (*t); | 83 | size_t ibl1 = strlen (*t); |
84 | char *ob1 = ob; | 84 | char *ob1 = ob; |
85 | size_t obl1 = obl; | 85 | size_t obl1 = obl; |
86 | iconv (cd, &ib1, &ibl1, &ob1, &obl1); | 86 | iconv (cd, &ib1, &ibl1, &ob1, &obl1); |
87 | if (!ibl1) | 87 | if (!ibl1) |
88 | { | 88 | { |
89 | ++ib, --ibl; | 89 | ++ib, --ibl; |
90 | ob = ob1, obl = obl1; | 90 | ob = ob1, obl = obl1; |
91 | ++ret; | 91 | ++ret; |
92 | break; | 92 | break; |
93 | } | 93 | } |
94 | } | 94 | } |
95 | if (*t) | 95 | if (*t) |
96 | continue; | 96 | continue; |
97 | } | 97 | } |
98 | if (outrepl) | 98 | if (outrepl) |
99 | { | 99 | { |
100 | /* Try replacing the output */ | 100 | /* Try replacing the output */ |
101 | size_t n = strlen (outrepl); | 101 | size_t n = strlen (outrepl); |
102 | if (n <= obl) | 102 | if (n <= obl) |
103 | { | 103 | { |
104 | memcpy (ob, outrepl, n); | 104 | memcpy (ob, outrepl, n); |
105 | ++ib, --ibl; | 105 | ++ib, --ibl; |
106 | ob += n, obl -= n; | 106 | ob += n, obl -= n; |
107 | ++ret; | 107 | ++ret; |
108 | continue; | 108 | continue; |
109 | } | 109 | } |
110 | } | 110 | } |
111 | } | 111 | } |
112 | *inbuf = ib, *inbytesleft = ibl; | 112 | *inbuf = ib, *inbytesleft = ibl; |
113 | *outbuf = ob, *outbytesleft = obl; | 113 | *outbuf = ob, *outbytesleft = obl; |
114 | return ret; | 114 | return ret; |
115 | } | 115 | } |
116 | } | 116 | } |
117 | #endif | 117 | #endif |
118 | 118 | ||
119 | int charconv(const char * tocode, const char * fromcode, | 119 | int charconv(const char * tocode, const char * fromcode, |
120 | const char * str, size_t length, | 120 | const char * str, size_t length, |
121 | char ** result) | 121 | char ** result) |
122 | { | 122 | { |
123 | #ifndef HAVE_ICONV | 123 | #ifndef HAVE_ICONV |
124 | return MAIL_CHARCONV_ERROR_UNKNOWN_CHARSET; | 124 | return MAIL_CHARCONV_ERROR_UNKNOWN_CHARSET; |
125 | #else | 125 | #else |
126 | iconv_t conv; | 126 | iconv_t conv; |
127 | size_t r; | 127 | size_t r; |
128 | char * out; | 128 | char * out; |
129 | char * pout; | 129 | char * pout; |
130 | size_t out_size; | 130 | size_t out_size; |
131 | size_t old_out_size; | 131 | size_t old_out_size; |
132 | size_t count; | 132 | size_t count; |
133 | int res; | 133 | int res; |
134 | 134 | ||
135 | conv = iconv_open(tocode, fromcode); | 135 | conv = iconv_open(tocode, fromcode); |
136 | if (conv == (iconv_t) -1) { | 136 | if (conv == (iconv_t) -1) { |
137 | res = MAIL_CHARCONV_ERROR_UNKNOWN_CHARSET; | 137 | res = MAIL_CHARCONV_ERROR_UNKNOWN_CHARSET; |
138 | goto err; | 138 | goto err; |
139 | } | 139 | } |
140 | 140 | ||
141 | out_size = 4 * length; | 141 | out_size = 4 * length; |
142 | 142 | ||
143 | out = malloc(out_size + 1); | 143 | out = malloc(out_size + 1); |
144 | if (out == NULL) { | 144 | if (out == NULL) { |
145 | res = MAIL_CHARCONV_ERROR_MEMORY; | 145 | res = MAIL_CHARCONV_ERROR_MEMORY; |
146 | goto close_iconv; | 146 | goto close_iconv; |
147 | } | 147 | } |
148 | 148 | ||
149 | pout = out; | 149 | pout = out; |
150 | old_out_size = out_size; | 150 | old_out_size = out_size; |
151 | 151 | ||
152 | r = mail_iconv(conv, &str, &length, &pout, &out_size, NULL, "?"); | 152 | r = mail_iconv(conv, &str, &length, &pout, &out_size, NULL, "?"); |
153 | 153 | ||
154 | if (r == (size_t) -1) { | 154 | if (r == (size_t) -1) { |
155 | res = MAIL_CHARCONV_ERROR_CONV; | 155 | res = MAIL_CHARCONV_ERROR_CONV; |
156 | goto free; | 156 | goto free; |
157 | } | 157 | } |
158 | 158 | ||
159 | iconv_close(conv); | 159 | iconv_close(conv); |
160 | 160 | ||
161 | * pout = '\0'; | 161 | * pout = '\0'; |
162 | count = old_out_size - out_size; | 162 | count = old_out_size - out_size; |
163 | pout = realloc(out, count + 1); | 163 | pout = realloc(out, count + 1); |
164 | if (pout != NULL) | 164 | if (pout != NULL) |
165 | out = pout; | 165 | out = pout; |
166 | 166 | ||
167 | * result = out; | 167 | * result = out; |
168 | 168 | ||
169 | return MAIL_CHARCONV_NO_ERROR; | 169 | return MAIL_CHARCONV_NO_ERROR; |
170 | 170 | ||
171 | free: | 171 | free: |
172 | free(out); | 172 | free(out); |
173 | close_iconv: | 173 | close_iconv: |
174 | iconv_close(conv); | 174 | iconv_close(conv); |
175 | err: | 175 | err: |
176 | return res; | 176 | return res; |
177 | #endif | 177 | #endif |
178 | }; | 178 | }; |
179 | 179 | ||
180 | int charconv_buffer(const char * tocode, const char * fromcode, | 180 | int charconv_buffer(const char * tocode, const char * fromcode, |
181 | const char * str, size_t length, | 181 | const char * str, size_t length, |
182 | char ** result, size_t * result_len) | 182 | char ** result, size_t * result_len) |
183 | { | 183 | { |
184 | #ifndef HAVE_ICONV | 184 | #ifndef HAVE_ICONV |
185 | return MAIL_CHARCONV_ERROR_UNKNOWN_CHARSET; | 185 | return MAIL_CHARCONV_ERROR_UNKNOWN_CHARSET; |
186 | #else | 186 | #else |
187 | iconv_t conv; | 187 | iconv_t conv; |
188 | size_t iconv_r; | 188 | size_t iconv_r; |
189 | int r; | 189 | int r; |
190 | char * out; | 190 | char * out; |
191 | char * pout; | 191 | char * pout; |
192 | size_t out_size; | 192 | size_t out_size; |
193 | size_t old_out_size; | 193 | size_t old_out_size; |
194 | size_t count; | 194 | size_t count; |
195 | MMAPString * mmapstr; | 195 | MMAPString * mmapstr; |
196 | int res; | 196 | int res; |
197 | 197 | ||
198 | conv = iconv_open(tocode, fromcode); | 198 | conv = iconv_open(tocode, fromcode); |
199 | if (conv == (iconv_t) -1) { | 199 | if (conv == (iconv_t) -1) { |
200 | res = MAIL_CHARCONV_ERROR_UNKNOWN_CHARSET; | 200 | res = MAIL_CHARCONV_ERROR_UNKNOWN_CHARSET; |
201 | goto err; | 201 | goto err; |
202 | } | 202 | } |
203 | 203 | ||
204 | out_size = 4 * length; | 204 | out_size = 4 * length; |
205 | 205 | ||
206 | mmapstr = mmap_string_sized_new(out_size + 1); | 206 | mmapstr = mmap_string_sized_new(out_size + 1); |
207 | if (mmapstr == NULL) { | 207 | if (mmapstr == NULL) { |
208 | res = MAIL_CHARCONV_ERROR_MEMORY; | 208 | res = MAIL_CHARCONV_ERROR_MEMORY; |
209 | goto err; | 209 | goto err; |
210 | } | 210 | } |
211 | 211 | ||
212 | out = mmapstr->str; | 212 | out = mmapstr->str; |
213 | 213 | ||
214 | pout = out; | 214 | pout = out; |
215 | old_out_size = out_size; | 215 | old_out_size = out_size; |
216 | 216 | ||
217 | iconv_r = mail_iconv(conv, &str, &length, &pout, &out_size, NULL, "?"); | 217 | iconv_r = mail_iconv(conv, &str, &length, &pout, &out_size, NULL, "?"); |
218 | 218 | ||
219 | if (iconv_r == (size_t) -1) { | 219 | if (iconv_r == (size_t) -1) { |
220 | res = MAIL_CHARCONV_ERROR_CONV; | 220 | res = MAIL_CHARCONV_ERROR_CONV; |
221 | goto free; | 221 | goto free; |
222 | } | 222 | } |
223 | 223 | ||
224 | iconv_close(conv); | 224 | iconv_close(conv); |
225 | 225 | ||
226 | * pout = '\0'; | 226 | * pout = '\0'; |
227 | 227 | ||
228 | count = old_out_size - out_size; | 228 | count = old_out_size - out_size; |
229 | 229 | ||
230 | r = mmap_string_ref(mmapstr); | 230 | r = mmap_string_ref(mmapstr); |
231 | if (r < 0) { | 231 | if (r < 0) { |
232 | res = MAIL_CHARCONV_ERROR_MEMORY; | 232 | res = MAIL_CHARCONV_ERROR_MEMORY; |
233 | goto free; | 233 | goto free; |
234 | } | 234 | } |
235 | 235 | ||
236 | * result = out; | 236 | * result = out; |
237 | * result_len = count; | 237 | * result_len = count; |
238 | 238 | ||
239 | return MAIL_CHARCONV_NO_ERROR; | 239 | return MAIL_CHARCONV_NO_ERROR; |
240 | 240 | ||
241 | free: | 241 | free: |
242 | mmap_string_free(mmapstr); | 242 | mmap_string_free(mmapstr); |
243 | err: | 243 | err: |
244 | return -1; | 244 | return -1; |
245 | #endif | 245 | #endif |
246 | }; | 246 | }; |
247 | 247 | ||
248 | void charconv_buffer_free(char * str) | 248 | void charconv_buffer_free(char * str) |
249 | { | 249 | { |
250 | mmap_string_unref(str); | 250 | mmap_string_unref(str); |
251 | } | 251 | } |