Fix FSF address (Tobias Mueller, #470445)
[platform/upstream/evolution-data-server.git] / camel / camel-mime-filter-charset.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  *  Copyright (C) 2000 Ximian Inc.
4  *
5  *  Authors: Michael Zucchi <notzed@ximian.com>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of version 2 of the GNU Lesser General Public
9  * License as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this program; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <errno.h>
28 #include <string.h>
29
30 #include <libedataserver/e-iconv.h>
31
32 #include "camel-charset-map.h"
33 #include "camel-mime-filter-charset.h"
34
35 #define d(x)
36 #define w(x)
37
38 static void camel_mime_filter_charset_class_init (CamelMimeFilterCharsetClass *klass);
39 static void camel_mime_filter_charset_init       (CamelMimeFilterCharset *obj);
40 static void camel_mime_filter_charset_finalize   (CamelObject *o);
41
42 static CamelMimeFilterClass *camel_mime_filter_charset_parent;
43
44 CamelType
45 camel_mime_filter_charset_get_type (void)
46 {
47         static CamelType type = CAMEL_INVALID_TYPE;
48         
49         if (type == CAMEL_INVALID_TYPE) {
50                 type = camel_type_register (camel_mime_filter_get_type (), "CamelMimeFilterCharset",
51                                             sizeof (CamelMimeFilterCharset),
52                                             sizeof (CamelMimeFilterCharsetClass),
53                                             (CamelObjectClassInitFunc) camel_mime_filter_charset_class_init,
54                                             NULL,
55                                             (CamelObjectInitFunc) camel_mime_filter_charset_init,
56                                             (CamelObjectFinalizeFunc) camel_mime_filter_charset_finalize);
57         }
58         
59         return type;
60 }
61
62 static void
63 camel_mime_filter_charset_finalize(CamelObject *o)
64 {
65         CamelMimeFilterCharset *f = (CamelMimeFilterCharset *)o;
66
67         g_free(f->from);
68         g_free(f->to);
69         if (f->ic != (iconv_t) -1) {
70                 e_iconv_close (f->ic);
71                 f->ic = (iconv_t) -1;
72         }
73 }
74
75 static void
76 reset(CamelMimeFilter *mf)
77 {
78         CamelMimeFilterCharset *f = (CamelMimeFilterCharset *)mf;
79         char buf[16];
80         char *buffer;
81         size_t outlen = 16;
82         
83         /* what happens with the output bytes if this resets the state? */
84         if (f->ic != (iconv_t) -1) {
85                 buffer = buf;
86                 e_iconv (f->ic, NULL, 0, &buffer, &outlen);
87         }
88 }
89
90 static void
91 complete(CamelMimeFilter *mf, char *in, size_t len, size_t prespace, char **out, size_t *outlen, size_t *outprespace)
92 {
93         CamelMimeFilterCharset *charset = (CamelMimeFilterCharset *)mf;
94         size_t inleft, outleft, converted = 0;
95         const char *inbuf;
96         char *outbuf;
97         
98         if (charset->ic == (iconv_t) -1)
99                 goto noop;
100         
101         camel_mime_filter_set_size (mf, len * 5 + 16, FALSE);
102         outbuf = mf->outbuf;
103         outleft = mf->outsize;
104         
105         inbuf = in;
106         inleft = len;
107         
108         if (inleft > 0) {
109                 do {
110                         converted = e_iconv (charset->ic, &inbuf, &inleft, &outbuf, &outleft);
111                         if (converted == (size_t) -1) {
112                                 if (errno == E2BIG) {
113                                         /*
114                                          * E2BIG   There is not sufficient room at *outbuf.
115                                          *
116                                          * We just need to grow our outbuffer and try again.
117                                          */
118                                         
119                                         converted = outbuf - mf->outbuf;
120                                         camel_mime_filter_set_size (mf, inleft * 5 + mf->outsize + 16, TRUE);
121                                         outbuf = mf->outbuf + converted;
122                                         outleft = mf->outsize - converted;
123                                 } else if (errno == EILSEQ) {
124                                         /*
125                                          * EILSEQ An invalid multibyte sequence has been  encountered
126                                          *        in the input.
127                                          *
128                                          * What we do here is eat the invalid bytes in the sequence and continue
129                                          */
130                                         
131                                         inbuf++;
132                                         inleft--;
133                                 } else if (errno == EINVAL) {
134                                         /*
135                                          * EINVAL  An  incomplete  multibyte sequence has been encounĀ­
136                                          *         tered in the input.
137                                          *
138                                          * We assume that this can only happen if we've run out of
139                                          * bytes for a multibyte sequence, if not we're in trouble.
140                                          */
141                                         
142                                         break;
143                                 } else
144                                         goto noop;
145                         }
146                 } while (((int) inleft) > 0);
147         }
148         
149         /* flush the iconv conversion */
150         e_iconv (charset->ic, NULL, NULL, &outbuf, &outleft);
151         
152         *out = mf->outbuf;
153         *outlen = mf->outsize - outleft;
154         *outprespace = mf->outpre;
155         
156         return;
157         
158  noop:
159         
160         *out = in;
161         *outlen = len;
162         *outprespace = prespace;
163 }
164
165 static void
166 filter(CamelMimeFilter *mf, char *in, size_t len, size_t prespace, char **out, size_t *outlen, size_t *outprespace)
167 {
168         CamelMimeFilterCharset *charset = (CamelMimeFilterCharset *)mf;
169         size_t inleft, outleft, converted = 0;
170         const char *inbuf;
171         char *outbuf;
172         
173         if (charset->ic == (iconv_t) -1)
174                 goto noop;
175         
176         camel_mime_filter_set_size (mf, len * 5 + 16, FALSE);
177         outbuf = mf->outbuf + converted;
178         outleft = mf->outsize - converted;
179         
180         inbuf = in;
181         inleft = len;
182         
183         do {
184                 converted = e_iconv (charset->ic, &inbuf, &inleft, &outbuf, &outleft);
185                 if (converted == (size_t) -1) {
186                         if (errno == E2BIG || errno == EINVAL)
187                                 break;
188                         
189                         if (errno == EILSEQ) {
190                                 /*
191                                  * EILSEQ An invalid multibyte sequence has been  encountered
192                                  *        in the input.
193                                  *
194                                  * What we do here is eat the invalid bytes in the sequence and continue
195                                  */
196                                 
197                                 inbuf++;
198                                 inleft--;
199                         } else {
200                                 /* unknown error condition */
201                                 goto noop;
202                         }
203                 }
204         } while (((int) inleft) > 0);
205         
206         if (((int) inleft) > 0) {
207                 /* We've either got an E2BIG or EINVAL. Save the
208                    remainder of the buffer as we'll process this next
209                    time through */
210                 camel_mime_filter_backup (mf, inbuf, inleft);
211         }
212         
213         *out = mf->outbuf;
214         *outlen = outbuf - mf->outbuf;
215         *outprespace = mf->outpre;
216         
217         return;
218         
219  noop:
220         
221         *out = in;
222         *outlen = len;
223         *outprespace = prespace;
224 }
225
226 static void
227 camel_mime_filter_charset_class_init (CamelMimeFilterCharsetClass *klass)
228 {
229         CamelMimeFilterClass *filter_class = (CamelMimeFilterClass *) klass;
230         
231         camel_mime_filter_charset_parent = CAMEL_MIME_FILTER_CLASS (camel_type_get_global_classfuncs (camel_mime_filter_get_type ()));
232
233         filter_class->reset = reset;
234         filter_class->filter = filter;
235         filter_class->complete = complete;
236 }
237
238 static void
239 camel_mime_filter_charset_init (CamelMimeFilterCharset *obj)
240 {
241         obj->ic = (iconv_t)-1;
242 }
243
244
245 /**
246  * camel_mime_filter_charset_new:
247  *
248  * Create a new #CamelMimeFilterCharset object.
249  * 
250  * Returns a new #CamelMimeFilterCharset object
251  **/
252 CamelMimeFilterCharset *
253 camel_mime_filter_charset_new (void)
254 {
255         return CAMEL_MIME_FILTER_CHARSET (camel_object_new (camel_mime_filter_charset_get_type ()));
256 }
257
258
259 /**
260  * camel_mime_filter_charset_new_convert:
261  * @from_charset: charset to convert from
262  * @to_charset: charset to convert to
263  *
264  * Create a new #CamelMimeFiletrCharset object to convert text from
265  * @from_charset to @to_charset.
266  *
267  * Returns a new #CamelMimeFilterCharset object
268  **/
269 CamelMimeFilterCharset *
270 camel_mime_filter_charset_new_convert (const char *from_charset, const char *to_charset)
271 {
272         CamelMimeFilterCharset *new;
273         
274         new = CAMEL_MIME_FILTER_CHARSET (camel_object_new (camel_mime_filter_charset_get_type ()));
275         
276         new->ic = e_iconv_open (to_charset, from_charset);
277         if (new->ic == (iconv_t) -1) {
278                 w(g_warning ("Cannot create charset conversion from %s to %s: %s",
279                              from_charset ? from_charset : "(null)",
280                              to_charset ? to_charset : "(null)",
281                              g_strerror (errno)));
282                 camel_object_unref (new);
283                 new = NULL;
284         } else {
285                 new->from = g_strdup (from_charset);
286                 new->to = g_strdup (to_charset);
287         }
288         
289         return new;
290 }