Initialize the gmime for upstream
[platform/upstream/gmime.git] / gmime / gmime-filter-best.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*  GMime
3  *  Copyright (C) 2000-2012 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
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
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
100 g_mime_filter_best_init (GMimeFilterBest *filter, GMimeFilterBestClass *klass)
101 {
102         filter->frombuf[5] = '\0';
103 }
104
105 static void
106 g_mime_filter_best_finalize (GObject *object)
107 {
108         G_OBJECT_CLASS (parent_class)->finalize (object);
109 }
110
111
112 static GMimeFilter *
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
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                                 }
159                         }
160                         
161                         /* check our from-save buffer for "From " */
162                         if (best->fromlen == 5 && !strcmp ((char *) best->frombuf, "From "))
163                                 best->hadfrom = TRUE;
164                         
165                         best->fromlen = 0;
166                         
167                         left = inend - inptr;
168                         
169                         /* if we have not yet found a from-line, check for one */
170                         if (best->startline && !best->hadfrom && left > 0) {
171                                 if (left < 5) {
172                                         if (!strncmp ((char *) inptr, "From ", left)) {
173                                                 memcpy (best->frombuf, inptr, left);
174                                                 best->frombuf[left] = '\0';
175                                                 best->fromlen = left;
176                                                 break;
177                                         }
178                                 } else {
179                                         if (!strncmp ((char *) inptr, "From ", 5)) {
180                                                 best->hadfrom = TRUE;
181                                                 inptr += 5;
182                                         }
183                                 }
184                         }
185                         
186                         best->startline = FALSE;
187                         best->midline = TRUE;
188                 }
189         }
190         
191         *outprespace = prespace;
192         *outlen = inlen;
193         *outbuf = inbuf;
194 }
195
196 static void 
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
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 *
243 g_mime_filter_best_new (GMimeFilterBestFlags flags)
244 {
245         GMimeFilterBest *new;
246         
247         new = g_object_newv (GMIME_TYPE_FILTER_BEST, 0, 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 *
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: a #GMimeFilterBest
284  * @constraint: a #GMimeEncodingConstraint
285  *
286  * Calculates the most efficient Content-Transfer-Encoding for the
287  * stream filtered through @best that fits within the encoding
288  * @constraint.
289  *
290  * Returns: the best encoding for the stream filtered by @best.
291  **/
292 GMimeContentEncoding
293 g_mime_filter_best_encoding (GMimeFilterBest *best, GMimeEncodingConstraint constraint)
294 {
295         GMimeContentEncoding encoding = GMIME_CONTENT_ENCODING_DEFAULT;
296         
297         g_return_val_if_fail (GMIME_IS_FILTER_BEST (best), GMIME_CONTENT_ENCODING_DEFAULT);
298         
299         if (!(best->flags & GMIME_FILTER_BEST_ENCODING))
300                 return GMIME_CONTENT_ENCODING_DEFAULT;
301         
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;
309                         else
310                                 encoding = GMIME_CONTENT_ENCODING_QUOTEDPRINTABLE;
311                 } else if (best->maxline > 998) {
312                         encoding = GMIME_CONTENT_ENCODING_QUOTEDPRINTABLE;
313                 }
314                 break;
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;
320                 }
321                 break;
322         case GMIME_ENCODING_CONSTRAINT_BINARY:
323                 if (best->count0 + best->count8 > 0)
324                         encoding = GMIME_CONTENT_ENCODING_BINARY;
325                 break;
326         }
327         
328         if (encoding == GMIME_CONTENT_ENCODING_DEFAULT && best->hadfrom)
329                 encoding = GMIME_CONTENT_ENCODING_QUOTEDPRINTABLE;
330         
331         return encoding;
332 }