1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2000-2012 Jeffrey Stedfast
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.
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.
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
28 #include "gmime-filter-best.h"
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
37 * A #GMimeFilter which is meant to determine the best charset and/or
38 * transfer encoding suitable for the stream which is filtered through
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);
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);
55 static GMimeFilterClass *parent_class = NULL;
59 g_mime_filter_best_get_type (void)
61 static GType type = 0;
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),
73 (GInstanceInitFunc) g_mime_filter_best_init,
76 type = g_type_register_static (GMIME_TYPE_FILTER, "GMimeFilterBest", &info, 0);
84 g_mime_filter_best_class_init (GMimeFilterBestClass *klass)
86 GObjectClass *object_class = G_OBJECT_CLASS (klass);
87 GMimeFilterClass *filter_class = GMIME_FILTER_CLASS (klass);
89 parent_class = g_type_class_ref (GMIME_TYPE_FILTER);
91 object_class->finalize = g_mime_filter_best_finalize;
93 filter_class->copy = filter_copy;
94 filter_class->filter = filter_filter;
95 filter_class->complete = filter_complete;
96 filter_class->reset = filter_reset;
100 g_mime_filter_best_init (GMimeFilterBest *filter, GMimeFilterBestClass *klass)
102 filter->frombuf[5] = '\0';
106 g_mime_filter_best_finalize (GObject *object)
108 G_OBJECT_CLASS (parent_class)->finalize (object);
113 filter_copy (GMimeFilter *filter)
115 GMimeFilterBest *best = (GMimeFilterBest *) filter;
117 return g_mime_filter_best_new (best->flags);
121 filter_filter (GMimeFilter *filter, char *inbuf, size_t inlen, size_t prespace,
122 char **outbuf, size_t *outlen, size_t *outprespace)
124 GMimeFilterBest *best = (GMimeFilterBest *) filter;
125 register unsigned char *inptr, *inend;
126 register unsigned char c;
129 if (best->flags & GMIME_FILTER_BEST_CHARSET)
130 g_mime_charset_step (&best->charset, inbuf, inlen);
132 if (best->flags & GMIME_FILTER_BEST_ENCODING) {
133 best->total += inlen;
135 inptr = (unsigned char *) inbuf;
136 inend = inptr + inlen;
138 while (inptr < inend) {
142 while (inptr < inend && (c = *inptr++) != '\n') {
148 if (best->fromlen > 0 && best->fromlen < 5)
149 best->frombuf[best->fromlen++] = c & 0xff;
155 best->maxline = MAX (best->maxline, best->linelen);
156 best->startline = TRUE;
157 best->midline = FALSE;
161 /* check our from-save buffer for "From " */
162 if (best->fromlen == 5 && !strcmp ((char *) best->frombuf, "From "))
163 best->hadfrom = TRUE;
167 left = inend - inptr;
169 /* if we have not yet found a from-line, check for one */
170 if (best->startline && !best->hadfrom && left > 0) {
172 if (!strncmp ((char *) inptr, "From ", left)) {
173 memcpy (best->frombuf, inptr, left);
174 best->frombuf[left] = '\0';
175 best->fromlen = left;
179 if (!strncmp ((char *) inptr, "From ", 5)) {
180 best->hadfrom = TRUE;
186 best->startline = FALSE;
187 best->midline = TRUE;
191 *outprespace = prespace;
197 filter_complete (GMimeFilter *filter, char *in, size_t len, size_t prespace,
198 char **out, size_t *outlen, size_t *outprespace)
200 GMimeFilterBest *best = (GMimeFilterBest *) filter;
202 filter_filter (filter, in, len, prespace, out, outlen, outprespace);
204 best->maxline = MAX (best->maxline, best->linelen);
208 filter_reset (GMimeFilter *filter)
210 GMimeFilterBest *best = (GMimeFilterBest *) filter;
212 g_mime_charset_init (&best->charset);
219 best->hadfrom = FALSE;
220 best->startline = TRUE;
221 best->midline = FALSE;
226 * g_mime_filter_best_new:
227 * @flags: filter flags
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.
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.
240 * Returns: a new best filter with flags @flags.
243 g_mime_filter_best_new (GMimeFilterBestFlags flags)
245 GMimeFilterBest *new;
247 new = g_object_newv (GMIME_TYPE_FILTER_BEST, 0, NULL);
249 filter_reset ((GMimeFilter *) new);
251 return (GMimeFilter *) new;
256 * g_mime_filter_best_charset:
259 * Calculates the best charset for encoding the stream filtered
260 * through the @best filter.
262 * Returns: a pointer to a string containing the name of the charset
263 * best suited for the text filtered through @best.
266 g_mime_filter_best_charset (GMimeFilterBest *best)
270 g_return_val_if_fail (GMIME_IS_FILTER_BEST (best), NULL);
272 if (!(best->flags & GMIME_FILTER_BEST_CHARSET))
275 charset = g_mime_charset_best_name (&best->charset);
277 return charset ? charset : "us-ascii";
282 * g_mime_filter_best_encoding:
283 * @best: a #GMimeFilterBest
284 * @constraint: a #GMimeEncodingConstraint
286 * Calculates the most efficient Content-Transfer-Encoding for the
287 * stream filtered through @best that fits within the encoding
290 * Returns: the best encoding for the stream filtered by @best.
293 g_mime_filter_best_encoding (GMimeFilterBest *best, GMimeEncodingConstraint constraint)
295 GMimeContentEncoding encoding = GMIME_CONTENT_ENCODING_DEFAULT;
297 g_return_val_if_fail (GMIME_IS_FILTER_BEST (best), GMIME_CONTENT_ENCODING_DEFAULT);
299 if (!(best->flags & GMIME_FILTER_BEST_ENCODING))
300 return GMIME_CONTENT_ENCODING_DEFAULT;
302 switch (constraint) {
303 case GMIME_ENCODING_CONSTRAINT_7BIT:
304 if (best->count0 > 0) {
305 encoding = GMIME_CONTENT_ENCODING_BASE64;
306 } else if (best->count8 > 0) {
307 if (best->count8 >= (unsigned int) (best->total * (17.0 / 100.0)))
308 encoding = GMIME_CONTENT_ENCODING_BASE64;
310 encoding = GMIME_CONTENT_ENCODING_QUOTEDPRINTABLE;
311 } else if (best->maxline > 998) {
312 encoding = GMIME_CONTENT_ENCODING_QUOTEDPRINTABLE;
315 case GMIME_ENCODING_CONSTRAINT_8BIT:
316 if (best->count0 > 0) {
317 encoding = GMIME_CONTENT_ENCODING_BASE64;
318 } else if (best->maxline > 998) {
319 encoding = GMIME_CONTENT_ENCODING_QUOTEDPRINTABLE;
322 case GMIME_ENCODING_CONSTRAINT_BINARY:
323 if (best->count0 + best->count8 > 0)
324 encoding = GMIME_CONTENT_ENCODING_BINARY;
328 if (encoding == GMIME_CONTENT_ENCODING_DEFAULT && best->hadfrom)
329 encoding = GMIME_CONTENT_ENCODING_QUOTEDPRINTABLE;