1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*  GMime
3  *  Copyright (C) 2000-2009 Jeffrey Stedfast
4  *
5  *  This library is free software; you can redistribute it and/or
6  *  modify it under the terms of the GNU Lesser General Public License
7  *  as published by the Free Software Foundation; either version 2.1
8  *  of the License, or (at your option) any later version.
9  *
10  *  This library is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  *  Lesser General Public License for more details.
14  *
15  *  You should have received a copy of the GNU Lesser General Public
16  *  License along with this library; if not, write to the Free
17  *  Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
18  *  02110-1301, USA.
19  */
20 
21 
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25 
26 #include <string.h>
27 
28 #include "gmime-filter-best.h"
29 
30 
31 /**
32  * SECTION: gmime-filter-best
33  * @title: GMimeFilterBest
34  * @short_description: Determine the best charset/encoding to use for a stream
35  * @see_also: #GMimeFilter
36  *
37  * A #GMimeFilter which is meant to determine the best charset and/or
38  * transfer encoding suitable for the stream which is filtered through
39  * it.
40  **/
41 
42 
43 static void g_mime_filter_best_class_init (GMimeFilterBestClass *klass);
44 static void g_mime_filter_best_init (GMimeFilterBest *filter, GMimeFilterBestClass *klass);
45 static void g_mime_filter_best_finalize (GObject *object);
46 
47 static GMimeFilter *filter_copy (GMimeFilter *filter);
48 static void filter_filter (GMimeFilter *filter, char *in, size_t len, size_t prespace,
49 			   char **out, size_t *outlen, size_t *outprespace);
50 static void filter_complete (GMimeFilter *filter, char *in, size_t len, size_t prespace,
51 			     char **out, size_t *outlen, size_t *outprespace);
52 static void filter_reset (GMimeFilter *filter);
53 
54 
55 static GMimeFilterClass *parent_class = NULL;
56 
57 
58 GType
g_mime_filter_best_get_type(void)59 g_mime_filter_best_get_type (void)
60 {
61 	static GType type = 0;
62 
63 	if (!type) {
64 		static const GTypeInfo info = {
65 			sizeof (GMimeFilterBestClass),
66 			NULL, /* base_class_init */
67 			NULL, /* base_class_finalize */
68 			(GClassInitFunc) g_mime_filter_best_class_init,
69 			NULL, /* class_finalize */
70 			NULL, /* class_data */
71 			sizeof (GMimeFilterBest),
72 			0,    /* n_preallocs */
73 			(GInstanceInitFunc) g_mime_filter_best_init,
74 		};
75 
76 		type = g_type_register_static (GMIME_TYPE_FILTER, "GMimeFilterBest", &info, 0);
77 	}
78 
79 	return type;
80 }
81 
82 
83 static void
g_mime_filter_best_class_init(GMimeFilterBestClass * klass)84 g_mime_filter_best_class_init (GMimeFilterBestClass *klass)
85 {
86 	GObjectClass *object_class = G_OBJECT_CLASS (klass);
87 	GMimeFilterClass *filter_class = GMIME_FILTER_CLASS (klass);
88 
89 	parent_class = g_type_class_ref (GMIME_TYPE_FILTER);
90 
91 	object_class->finalize = g_mime_filter_best_finalize;
92 
93 	filter_class->copy = filter_copy;
94 	filter_class->filter = filter_filter;
95 	filter_class->complete = filter_complete;
96 	filter_class->reset = filter_reset;
97 }
98 
99 static void
g_mime_filter_best_init(GMimeFilterBest * filter,GMimeFilterBestClass * klass)100 g_mime_filter_best_init (GMimeFilterBest *filter, GMimeFilterBestClass *klass)
101 {
102 	filter->frombuf[5] = '\0';
103 }
104 
105 static void
g_mime_filter_best_finalize(GObject * object)106 g_mime_filter_best_finalize (GObject *object)
107 {
108 	G_OBJECT_CLASS (parent_class)->finalize (object);
109 }
110 
111 
112 static GMimeFilter *
filter_copy(GMimeFilter * filter)113 filter_copy (GMimeFilter *filter)
114 {
115 	GMimeFilterBest *best = (GMimeFilterBest *) filter;
116 
117 	return g_mime_filter_best_new (best->flags);
118 }
119 
120 static void
filter_filter(GMimeFilter * filter,char * in,size_t len,size_t prespace,char ** out,size_t * outlen,size_t * outprespace)121 filter_filter (GMimeFilter *filter, char *in, size_t len, size_t prespace,
122 	       char **out, size_t *outlen, size_t *outprespace)
123 {
124 	GMimeFilterBest *best = (GMimeFilterBest *) filter;
125 	register unsigned char *inptr, *inend;
126 	size_t left;
127 
128 	if (best->flags & GMIME_FILTER_BEST_CHARSET)
129 		g_mime_charset_step (&best->charset, in, len);
130 
131 	if (best->flags & GMIME_FILTER_BEST_ENCODING) {
132 		best->total += len;
133 
134 		inptr = (unsigned char *) in;
135 		inend = inptr + len;
136 
137 		while (inptr < inend) {
138 			register unsigned char c = '\0';
139 
140 			if (best->midline) {
141 				while (inptr < inend && (c = *inptr++) != '\n') {
142 					if (c == 0)
143 						best->count0++;
144 					else if (c & 0x80)
145 						best->count8++;
146 
147 					if (best->fromlen > 0 && best->fromlen < 5)
148 						best->frombuf[best->fromlen++] = c & 0xff;
149 
150 					best->linelen++;
151 				}
152 
153 				if (c == '\n') {
154 					best->startline = TRUE;
155 					best->midline = FALSE;
156 					best->maxline = MAX (best->maxline, best->linelen);
157 				}
158 			}
159 
160 			/* check our from-save buffer for "From " */
161 			if (best->fromlen == 5 && !strcmp ((char *) best->frombuf, "From "))
162 				best->hadfrom = TRUE;
163 
164 			best->fromlen = 0;
165 
166 			left = inend - inptr;
167 
168 			/* if we have not yet found a from-line, check for one */
169 			if (best->startline && !best->hadfrom && left > 0) {
170 				if (left < 5) {
171 					if (!strncmp ((char *) inptr, "From ", left)) {
172 						memcpy (best->frombuf, inptr, left);
173 						best->frombuf[left] = '\0';
174 						best->fromlen = left;
175 						break;
176 					}
177 				} else {
178 					if (!strncmp ((char *) inptr, "From ", 5)) {
179 						best->hadfrom = TRUE;
180 
181 						inptr += 5;
182 					}
183 				}
184 			}
185 
186 			best->startline = FALSE;
187 			best->midline = TRUE;
188 		}
189 	}
190 
191 	*out = in;
192 	*outlen = len;
193 	*outprespace = prespace;
194 }
195 
196 static void
filter_complete(GMimeFilter * filter,char * in,size_t len,size_t prespace,char ** out,size_t * outlen,size_t * outprespace)197 filter_complete (GMimeFilter *filter, char *in, size_t len, size_t prespace,
198 		 char **out, size_t *outlen, size_t *outprespace)
199 {
200 	GMimeFilterBest *best = (GMimeFilterBest *) filter;
201 
202 	filter_filter (filter, in, len, prespace, out, outlen, outprespace);
203 
204 	best->maxline = MAX (best->maxline, best->linelen);
205 }
206 
207 static void
filter_reset(GMimeFilter * filter)208 filter_reset (GMimeFilter *filter)
209 {
210 	GMimeFilterBest *best = (GMimeFilterBest *) filter;
211 
212 	g_mime_charset_init (&best->charset);
213 	best->count0 = 0;
214 	best->count8 = 0;
215 	best->total = 0;
216 	best->maxline = 0;
217 	best->linelen = 0;
218 	best->fromlen = 0;
219 	best->hadfrom = FALSE;
220 	best->startline = TRUE;
221 	best->midline = FALSE;
222 }
223 
224 
225 /**
226  * g_mime_filter_best_new:
227  * @flags: filter flags
228  *
229  * Creates a new GMimeFilterBest filter. @flags are used to determine
230  * which information to keep statistics of. If the
231  * #GMIME_FILTER_BEST_CHARSET bit is set, the filter will be able to
232  * compute the best charset for encoding the stream of data
233  * filtered. If the #GMIME_FILTER_BEST_ENCODING bit is set, the filter
234  * will be able to compute the best Content-Transfer-Encoding for use
235  * with the stream being filtered.
236  *
237  * Note: In order for the g_mime_filter_best_charset() function to
238  * work, the stream being filtered MUST already be encoded in UTF-8.
239  *
240  * Returns a new best filter with flags @flags.
241  **/
242 GMimeFilter *
g_mime_filter_best_new(unsigned int flags)243 g_mime_filter_best_new (unsigned int flags)
244 {
245 	GMimeFilterBest *new;
246 
247 	new = g_object_new (GMIME_TYPE_FILTER_BEST, NULL);
248 	new->flags = flags;
249 	filter_reset ((GMimeFilter *) new);
250 
251 	return (GMimeFilter *) new;
252 }
253 
254 
255 /**
256  * g_mime_filter_best_charset:
257  * @best: best filter
258  *
259  * Calculates the best charset for encoding the stream filtered
260  * through the @best filter.
261  *
262  * Returns a pointer to a string containing the name of the charset
263  * best suited for the text filtered through @best.
264  **/
265 const char *
g_mime_filter_best_charset(GMimeFilterBest * best)266 g_mime_filter_best_charset (GMimeFilterBest *best)
267 {
268 	const char *charset;
269 
270 	g_return_val_if_fail (GMIME_IS_FILTER_BEST (best), NULL);
271 
272 	if (!(best->flags & GMIME_FILTER_BEST_CHARSET))
273 		return NULL;
274 
275 	charset = g_mime_charset_best_name (&best->charset);
276 
277 	return charset ? charset : "us-ascii";
278 }
279 
280 
281 /**
282  * g_mime_filter_best_encoding:
283  * @best: best filter
284  * @required: encoding that all data must fit within
285  *
286  * Calculates the best Content-Transfer-Encoding for the stream
287  * filtered through @best that fits within the @required encoding.
288  *
289  * Returns the best encoding for the stream filtered by @best.
290  **/
291 GMimePartEncodingType
g_mime_filter_best_encoding(GMimeFilterBest * best,GMimeBestEncoding required)292 g_mime_filter_best_encoding (GMimeFilterBest *best, GMimeBestEncoding required)
293 {
294 	GMimePartEncodingType encoding = GMIME_PART_ENCODING_DEFAULT;
295 
296 	g_return_val_if_fail (GMIME_IS_FILTER_BEST (best), GMIME_PART_ENCODING_DEFAULT);
297 
298 	if (!(best->flags & GMIME_FILTER_BEST_ENCODING))
299 		return GMIME_PART_ENCODING_DEFAULT;
300 
301 	switch (required) {
302 	case GMIME_BEST_ENCODING_7BIT:
303 		if (best->count0 > 0) {
304 			encoding = GMIME_PART_ENCODING_BASE64;
305 		} else if (best->count8 > 0) {
306 			if (best->count8 >= (best->total * 17 / 100))
307 				encoding = GMIME_PART_ENCODING_BASE64;
308 			else
309 				encoding = GMIME_PART_ENCODING_QUOTEDPRINTABLE;
310 		} else if (best->maxline > 998) {
311 			encoding = GMIME_PART_ENCODING_QUOTEDPRINTABLE;
312 		}
313 		break;
314 	case GMIME_BEST_ENCODING_8BIT:
315 		if (best->count0 > 0) {
316 			encoding = GMIME_PART_ENCODING_BASE64;
317 		} else if (best->maxline > 998) {
318 			encoding = GMIME_PART_ENCODING_QUOTEDPRINTABLE;
319 		}
320 		break;
321 	case GMIME_BEST_ENCODING_BINARY:
322 		if (best->count0 + best->count8 > 0)
323 			encoding = GMIME_PART_ENCODING_BINARY;
324 		break;
325 	}
326 
327 	if (encoding == GMIME_PART_ENCODING_DEFAULT && best->hadfrom)
328 		encoding = GMIME_PART_ENCODING_QUOTEDPRINTABLE;
329 
330 	return encoding;
331 }
332 
333