1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*  GMime
3  *  Copyright (C) 2000-2014 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 * inbuf,size_t inlen,size_t prespace,char ** outbuf,size_t * outlen,size_t * outprespace)121 filter_filter (GMimeFilter *filter, char *inbuf, size_t inlen, size_t prespace,
122 	       char **outbuf, size_t *outlen, size_t *outprespace)
123 {
124 	GMimeFilterBest *best = (GMimeFilterBest *) filter;
125 	register unsigned char *inptr, *inend;
126 	register unsigned char c;
127 	size_t left;
128 
129 	if (best->flags & GMIME_FILTER_BEST_CHARSET)
130 		g_mime_charset_step (&best->charset, inbuf, inlen);
131 
132 	if (best->flags & GMIME_FILTER_BEST_ENCODING) {
133 		best->total += inlen;
134 
135 		inptr = (unsigned char *) inbuf;
136 		inend = inptr + inlen;
137 
138 		while (inptr < inend) {
139 			c = 0;
140 
141 			if (best->midline) {
142 				while (inptr < inend && (c = *inptr++) != '\n') {
143 					if (c == 0)
144 						best->count0++;
145 					else if (c & 0x80)
146 						best->count8++;
147 
148 					if (best->fromlen > 0 && best->fromlen < 5)
149 						best->frombuf[best->fromlen++] = c & 0xff;
150 
151 					best->linelen++;
152 				}
153 
154 				if (c == '\n') {
155 					best->maxline = MAX (best->maxline, best->linelen);
156 					best->startline = TRUE;
157 					best->midline = FALSE;
158 					best->linelen = 0;
159 				}
160 			}
161 
162 			/* check our from-save buffer for "From " */
163 			if (best->fromlen == 5 && !strcmp ((char *) best->frombuf, "From "))
164 				best->hadfrom = TRUE;
165 
166 			best->fromlen = 0;
167 
168 			left = inend - inptr;
169 
170 			/* if we have not yet found a from-line, check for one */
171 			if (best->startline && !best->hadfrom && left > 0) {
172 				if (left < 5) {
173 					if (!strncmp ((char *) inptr, "From ", left)) {
174 						memcpy (best->frombuf, inptr, left);
175 						best->frombuf[left] = '\0';
176 						best->fromlen = left;
177 						break;
178 					}
179 				} else {
180 					if (!strncmp ((char *) inptr, "From ", 5)) {
181 						best->hadfrom = TRUE;
182 						inptr += 5;
183 					}
184 				}
185 			}
186 
187 			best->startline = FALSE;
188 			best->midline = TRUE;
189 		}
190 	}
191 
192 	*outprespace = prespace;
193 	*outlen = inlen;
194 	*outbuf = inbuf;
195 }
196 
197 static void
filter_complete(GMimeFilter * filter,char * in,size_t len,size_t prespace,char ** out,size_t * outlen,size_t * outprespace)198 filter_complete (GMimeFilter *filter, char *in, size_t len, size_t prespace,
199 		 char **out, size_t *outlen, size_t *outprespace)
200 {
201 	GMimeFilterBest *best = (GMimeFilterBest *) filter;
202 
203 	filter_filter (filter, in, len, prespace, out, outlen, outprespace);
204 
205 	best->maxline = MAX (best->maxline, best->linelen);
206 }
207 
208 static void
filter_reset(GMimeFilter * filter)209 filter_reset (GMimeFilter *filter)
210 {
211 	GMimeFilterBest *best = (GMimeFilterBest *) filter;
212 
213 	g_mime_charset_init (&best->charset);
214 	best->count0 = 0;
215 	best->count8 = 0;
216 	best->total = 0;
217 	best->maxline = 0;
218 	best->linelen = 0;
219 	best->fromlen = 0;
220 	best->hadfrom = FALSE;
221 	best->startline = TRUE;
222 	best->midline = FALSE;
223 }
224 
225 
226 /**
227  * g_mime_filter_best_new:
228  * @flags: filter flags
229  *
230  * Creates a new GMimeFilterBest filter. @flags are used to determine
231  * which information to keep statistics of. If the
232  * #GMIME_FILTER_BEST_CHARSET bit is set, the filter will be able to
233  * compute the best charset for encoding the stream of data
234  * filtered. If the #GMIME_FILTER_BEST_ENCODING bit is set, the filter
235  * will be able to compute the best Content-Transfer-Encoding for use
236  * with the stream being filtered.
237  *
238  * Note: In order for the g_mime_filter_best_charset() function to
239  * work, the stream being filtered MUST already be encoded in UTF-8.
240  *
241  * Returns: a new best filter with flags @flags.
242  **/
243 GMimeFilter *
g_mime_filter_best_new(GMimeFilterBestFlags flags)244 g_mime_filter_best_new (GMimeFilterBestFlags flags)
245 {
246 	GMimeFilterBest *new;
247 
248 	new = g_object_newv (GMIME_TYPE_FILTER_BEST, 0, NULL);
249 	new->flags = flags;
250 	filter_reset ((GMimeFilter *) new);
251 
252 	return (GMimeFilter *) new;
253 }
254 
255 
256 /**
257  * g_mime_filter_best_charset:
258  * @best: best filter
259  *
260  * Calculates the best charset for encoding the stream filtered
261  * through the @best filter.
262  *
263  * Returns: a pointer to a string containing the name of the charset
264  * best suited for the text filtered through @best.
265  **/
266 const char *
g_mime_filter_best_charset(GMimeFilterBest * best)267 g_mime_filter_best_charset (GMimeFilterBest *best)
268 {
269 	const char *charset;
270 
271 	g_return_val_if_fail (GMIME_IS_FILTER_BEST (best), NULL);
272 
273 	if (!(best->flags & GMIME_FILTER_BEST_CHARSET))
274 		return NULL;
275 
276 	charset = g_mime_charset_best_name (&best->charset);
277 
278 	return charset ? charset : "us-ascii";
279 }
280 
281 
282 /**
283  * g_mime_filter_best_encoding:
284  * @best: a #GMimeFilterBest
285  * @constraint: a #GMimeEncodingConstraint
286  *
287  * Calculates the most efficient Content-Transfer-Encoding for the
288  * stream filtered through @best that fits within the encoding
289  * @constraint.
290  *
291  * Returns: the best encoding for the stream filtered by @best.
292  **/
293 GMimeContentEncoding
g_mime_filter_best_encoding(GMimeFilterBest * best,GMimeEncodingConstraint constraint)294 g_mime_filter_best_encoding (GMimeFilterBest *best, GMimeEncodingConstraint constraint)
295 {
296 	GMimeContentEncoding encoding = GMIME_CONTENT_ENCODING_DEFAULT;
297 
298 	g_return_val_if_fail (GMIME_IS_FILTER_BEST (best), GMIME_CONTENT_ENCODING_DEFAULT);
299 
300 	if (!(best->flags & GMIME_FILTER_BEST_ENCODING))
301 		return GMIME_CONTENT_ENCODING_DEFAULT;
302 
303 	switch (constraint) {
304 	case GMIME_ENCODING_CONSTRAINT_7BIT:
305 		if (best->count0 > 0) {
306 			encoding = GMIME_CONTENT_ENCODING_BASE64;
307 		} else if (best->count8 > 0) {
308 			if (best->count8 >= (unsigned int) (best->total * (17.0 / 100.0)))
309 				encoding = GMIME_CONTENT_ENCODING_BASE64;
310 			else
311 				encoding = GMIME_CONTENT_ENCODING_QUOTEDPRINTABLE;
312 		} else if (best->maxline > 998) {
313 			encoding = GMIME_CONTENT_ENCODING_QUOTEDPRINTABLE;
314 		}
315 		break;
316 	case GMIME_ENCODING_CONSTRAINT_8BIT:
317 		if (best->count0 > 0) {
318 			encoding = GMIME_CONTENT_ENCODING_BASE64;
319 		} else if (best->maxline > 998) {
320 			encoding = GMIME_CONTENT_ENCODING_QUOTEDPRINTABLE;
321 		}
322 		break;
323 	case GMIME_ENCODING_CONSTRAINT_BINARY:
324 		if (best->count0 + best->count8 > 0)
325 			encoding = GMIME_CONTENT_ENCODING_BINARY;
326 		break;
327 	}
328 
329 	if (encoding == GMIME_CONTENT_ENCODING_DEFAULT && best->hadfrom)
330 		encoding = GMIME_CONTENT_ENCODING_QUOTEDPRINTABLE;
331 
332 	return encoding;
333 }
334