summaryrefslogtreecommitdiff
path: root/camel/camel-mime-filter-charset.c
blob: 78133b19d3a299bd9d3bbcc79f1167e9ec310740 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 * This library is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors: Michael Zucchi <notzed@ximian.com>
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <errno.h>
#include <string.h>

#include "camel-charset-map.h"
#include "camel-iconv.h"
#include "camel-mime-filter-charset.h"

#define CAMEL_MIME_FILTER_CHARSET_GET_PRIVATE(obj) \
	(G_TYPE_INSTANCE_GET_PRIVATE \
	((obj), CAMEL_TYPE_MIME_FILTER_CHARSET, CamelMimeFilterCharsetPrivate))

#define d(x)
#define w(x)

struct _CamelMimeFilterCharsetPrivate {
	iconv_t ic;
	gchar *from;
	gchar *to;
};

G_DEFINE_TYPE (CamelMimeFilterCharset, camel_mime_filter_charset, CAMEL_TYPE_MIME_FILTER)

static void
mime_filter_charset_finalize (GObject *object)
{
	CamelMimeFilterCharsetPrivate *priv;

	priv = CAMEL_MIME_FILTER_CHARSET_GET_PRIVATE (object);

	g_free (priv->from);
	g_free (priv->to);

	if (priv->ic != (iconv_t) -1) {
		camel_iconv_close (priv->ic);
		priv->ic = (iconv_t) -1;
	}

	/* Chain up to parent's finalize() method. */
	G_OBJECT_CLASS (camel_mime_filter_charset_parent_class)->finalize (object);
}

static void
mime_filter_charset_complete (CamelMimeFilter *mime_filter,
                              const gchar *in,
                              gsize len,
                              gsize prespace,
                              gchar **out,
                              gsize *outlen,
                              gsize *outprespace)
{
	CamelMimeFilterCharsetPrivate *priv;
	gsize inleft, outleft, converted = 0;
	const gchar *inbuf;
	gchar *outbuf;

	priv = CAMEL_MIME_FILTER_CHARSET_GET_PRIVATE (mime_filter);

	if (priv->ic == (iconv_t) -1)
		goto noop;

	camel_mime_filter_set_size (mime_filter, len * 5 + 16, FALSE);
	outbuf = mime_filter->outbuf;
	outleft = mime_filter->outsize;

	inbuf = in;
	inleft = len;

	if (inleft > 0) {
		do {
			converted = camel_iconv (priv->ic, &inbuf, &inleft, &outbuf, &outleft);
			if (converted == (gsize) -1) {
				if (errno == E2BIG) {
					/*
					 * E2BIG   There is not sufficient room at *outbuf.
					 *
					 * We just need to grow our outbuffer and try again.
					 */

					converted = outbuf - mime_filter->outbuf;
					camel_mime_filter_set_size (mime_filter, inleft * 5 + mime_filter->outsize + 16, TRUE);
					outbuf = mime_filter->outbuf + converted;
					outleft = mime_filter->outsize - converted;
				} else if (errno == EILSEQ) {
					/*
					 * EILSEQ An invalid multibyte sequence has been  encountered
					 *        in the input.
					 *
					 * What we do here is eat the invalid bytes in the sequence and continue
					 */

					inbuf++;
					inleft--;
				} else if (errno == EINVAL) {
					/*
					 * EINVAL  An  incomplete  multibyte sequence has been encoun­
					 *         tered in the input.
					 *
					 * We assume that this can only happen if we've run out of
					 * bytes for a multibyte sequence, if not we're in trouble.
					 */

					break;
				} else
					goto noop;
			}
		} while (((gint) inleft) > 0);
	}

	/* flush the iconv conversion */
	while (camel_iconv (priv->ic, NULL, NULL, &outbuf, &outleft) == (gsize) -1) {
		if (errno != E2BIG)
			break;

		converted = outbuf - mime_filter->outbuf;
		camel_mime_filter_set_size (mime_filter, mime_filter->outsize + 16, TRUE);
		outbuf = mime_filter->outbuf + converted;
		outleft = mime_filter->outsize - converted;
	}

	*out = mime_filter->outbuf;
	*outlen = mime_filter->outsize - outleft;
	*outprespace = mime_filter->outpre;

	return;

 noop:

	*out = (gchar *) in;
	*outlen = len;
	*outprespace = prespace;
}

static void
mime_filter_charset_filter (CamelMimeFilter *mime_filter,
                            const gchar *in,
                            gsize len,
                            gsize prespace,
                            gchar **out,
                            gsize *outlen,
                            gsize *outprespace)
{
	CamelMimeFilterCharsetPrivate *priv;
	gsize inleft, outleft, converted = 0;
	const gchar *inbuf;
	gchar *outbuf;

	priv = CAMEL_MIME_FILTER_CHARSET_GET_PRIVATE (mime_filter);

	if (priv->ic == (iconv_t) -1)
		goto noop;

	camel_mime_filter_set_size (mime_filter, len * 5 + 16, FALSE);
	outbuf = mime_filter->outbuf + converted;
	outleft = mime_filter->outsize - converted;

	inbuf = in;
	inleft = len;

	do {
		converted = camel_iconv (priv->ic, &inbuf, &inleft, &outbuf, &outleft);
		if (converted == (gsize) -1) {
			if (errno == E2BIG || errno == EINVAL)
				break;

			if (errno == EILSEQ) {
				/*
				 * EILSEQ An invalid multibyte sequence has been  encountered
				 *        in the input.
				 *
				 * What we do here is eat the invalid bytes in the sequence and continue
				 */

				inbuf++;
				inleft--;
			} else {
				/* unknown error condition */
				goto noop;
			}
		}
	} while (((gint) inleft) > 0);

	if (((gint) inleft) > 0) {
		/* We've either got an E2BIG or EINVAL. Save the
		 * remainder of the buffer as we'll process this next
		 * time through */
		camel_mime_filter_backup (mime_filter, inbuf, inleft);
	}

	*out = mime_filter->outbuf;
	*outlen = outbuf - mime_filter->outbuf;
	*outprespace = mime_filter->outpre;

	return;

 noop:

	*out = (gchar *) in;
	*outlen = len;
	*outprespace = prespace;
}

static void
mime_filter_charset_reset (CamelMimeFilter *mime_filter)
{
	CamelMimeFilterCharsetPrivate *priv;
	gchar buf[16];
	gchar *buffer;
	gsize outlen = 16;

	priv = CAMEL_MIME_FILTER_CHARSET_GET_PRIVATE (mime_filter);

	/* what happens with the output bytes if this resets the state? */
	if (priv->ic != (iconv_t) -1) {
		buffer = buf;
		camel_iconv (priv->ic, NULL, NULL, &buffer, &outlen);
	}
}

static void
camel_mime_filter_charset_class_init (CamelMimeFilterCharsetClass *class)
{
	GObjectClass *object_class;
	CamelMimeFilterClass *mime_filter_class;

	g_type_class_add_private (class, sizeof (CamelMimeFilterCharsetPrivate));

	object_class = G_OBJECT_CLASS (class);
	object_class->finalize = mime_filter_charset_finalize;

	mime_filter_class = CAMEL_MIME_FILTER_CLASS (class);
	mime_filter_class->filter = mime_filter_charset_filter;
	mime_filter_class->complete = mime_filter_charset_complete;
	mime_filter_class->reset = mime_filter_charset_reset;
}

static void
camel_mime_filter_charset_init (CamelMimeFilterCharset *filter)
{
	filter->priv = CAMEL_MIME_FILTER_CHARSET_GET_PRIVATE (filter);
	filter->priv->ic = (iconv_t) -1;
}

/**
 * camel_mime_filter_charset_new:
 * @from_charset: charset to convert from
 * @to_charset: charset to convert to
 *
 * Create a new #CamelMimeFiletrCharset object to convert text from
 * @from_charset to @to_charset.
 *
 * Returns: a new #CamelMimeFilterCharset object
 **/
CamelMimeFilter *
camel_mime_filter_charset_new (const gchar *from_charset,
                               const gchar *to_charset)
{
	CamelMimeFilter *new;
	CamelMimeFilterCharsetPrivate *priv;

	new = g_object_new (CAMEL_TYPE_MIME_FILTER_CHARSET, NULL);
	priv = CAMEL_MIME_FILTER_CHARSET_GET_PRIVATE (new);

	priv->ic = camel_iconv_open (to_charset, from_charset);
	if (priv->ic == (iconv_t) -1) {
		w (g_warning (
			"Cannot create charset conversion from %s to %s: %s",
			from_charset ? from_charset : "(null)",
			to_charset ? to_charset : "(null)",
			g_strerror (errno)));
		g_object_unref (new);
		new = NULL;
	} else {
		priv->from = g_strdup (from_charset);
		priv->to = g_strdup (to_charset);
	}

	return new;
}