Fix FSF address (Tobias Mueller, #470445)
[platform/upstream/evolution-data-server.git] / camel / camel-mime-filter-bestenc.c
1 /*
2  *  Copyright (C) 2000 Ximian Inc.
3  *
4  *  Authors: Michael Zucchi <notzed@ximian.com>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of version 2 of the GNU Lesser General Public
8  * License as published by the Free Software Foundation.
9  *
10  * This program 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  * 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 program; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <string.h>
28
29 #include "camel-mime-filter-bestenc.h"
30
31 static void camel_mime_filter_bestenc_class_init (CamelMimeFilterBestencClass *klass);
32 static void camel_mime_filter_bestenc_init       (CamelMimeFilter *obj);
33
34 static CamelMimeFilterClass *camel_mime_filter_bestenc_parent;
35
36 CamelType
37 camel_mime_filter_bestenc_get_type (void)
38 {
39         static CamelType type = CAMEL_INVALID_TYPE;
40         
41         if (type == CAMEL_INVALID_TYPE) {
42                 type = camel_type_register (camel_mime_filter_get_type (), "CamelMimeFilterBestenc",
43                                             sizeof (CamelMimeFilterBestenc),
44                                             sizeof (CamelMimeFilterBestencClass),
45                                             (CamelObjectClassInitFunc) camel_mime_filter_bestenc_class_init,
46                                             NULL,
47                                             (CamelObjectInitFunc) camel_mime_filter_bestenc_init,
48                                             NULL);
49         }
50         
51         return type;
52 }
53
54 static void
55 reset(CamelMimeFilter *mf)
56 {
57         CamelMimeFilterBestenc *f = (CamelMimeFilterBestenc *)mf;
58
59         f->count0 = 0;
60         f->count8 = 0;
61         f->countline = 0;
62         f->total = 0;
63         f->lastc = ~0;
64         f->crlfnoorder = FALSE;
65         f->fromcount = 0;
66         f->hadfrom = FALSE;
67         f->startofline = TRUE;
68
69         camel_charset_init(&f->charset);
70 }
71
72 static void
73 filter(CamelMimeFilter *mf, char *in, size_t len, size_t prespace, char **out, size_t *outlen, size_t *outprespace)
74 {
75         CamelMimeFilterBestenc *f = (CamelMimeFilterBestenc *)mf;
76         register unsigned char *p, *pend;
77
78         if (len == 0)
79                 goto donothing;
80
81         if (f->flags & CAMEL_BESTENC_GET_ENCODING) {
82                 register unsigned int /* hopefully reg's are assinged in the order they appear? */
83                         c,
84                         lastc=f->lastc, 
85                         countline=f->countline,
86                         count0=f->count0,
87                         count8 = f->count8;
88
89                 /* Check ^From  lines first call, or have the start of a new line waiting? */
90                 if ((f->flags & CAMEL_BESTENC_NO_FROM) && !f->hadfrom
91                     && (f->fromcount > 0 || f->startofline)) {
92                         if (f->fromcount + len >=5) {
93                                 memcpy(&f->fromsave[f->fromcount], in, 5-f->fromcount);
94                                 f->hadfrom = strncmp(f->fromsave, "From ", 5) == 0;
95                                 f->fromcount = 0;
96                         } else {
97                                 memcpy(&f->fromsave[f->fromcount], in, len);
98                                 f->fromcount += len;
99                         }
100                 }
101
102                 f->startofline = FALSE;
103
104                 /* See rfc2045 section 2 for definitions of 7bit/8bit/binary */
105                 p = (unsigned char *) in;
106                 pend = p + len;
107                 while (p<pend) {
108                         c = *p++;
109                         /* check for 8 bit characters */
110                         if (c & 0x80)
111                                 count8++;
112
113                         /* check for nul's */
114                         if (c == 0)
115                                 count0++;
116
117                         /* check for wild '\r's in a unix format stream */
118                         if (c == '\r' && (f->flags & CAMEL_BESTENC_LF_IS_CRLF)) {
119                                 f->crlfnoorder = TRUE;
120                         }
121
122                         /* check for end of line */
123                         if (c == '\n') {
124                                 /* check for wild '\n's in canonical format stream */
125                                 if (lastc == '\r' || (f->flags & CAMEL_BESTENC_LF_IS_CRLF)) {
126                                         if (countline > f->maxline)
127                                                 f->maxline = countline;
128                                         countline = 0;
129
130                                         /* Check for "^From " lines */
131                                         if ((f->flags & CAMEL_BESTENC_NO_FROM) && !f->hadfrom) {
132                                                 if (pend-p >= 5) {
133                                                         f->hadfrom = strncmp((char *) p, (char *) "From ", 5) == 0;
134                                                 } else if (pend-p == 0) {
135                                                         f->startofline = TRUE;
136                                                 } else {
137                                                         f->fromcount = pend-p;
138                                                         memcpy(f->fromsave, p, pend-p);
139                                                 }
140                                         }
141                                 } else {
142                                         f->crlfnoorder = TRUE;
143                                 }
144                         } else {
145                                 countline++;
146                         }
147                         lastc = c;
148                 }
149                 f->count8 = count8;
150                 f->count0 = count0;
151                 f->countline = countline;
152                 f->lastc = lastc;
153         }
154
155         f->total += len;
156
157         if (f->flags & CAMEL_BESTENC_GET_CHARSET)
158                 camel_charset_step(&f->charset, in, len);
159
160 donothing:
161         *out = in;
162         *outlen = len;
163         *outprespace = prespace;
164 }
165
166 static void
167 complete(CamelMimeFilter *mf, char *in, size_t len, size_t prespace, char **out, size_t *outlen, size_t *outprespace)
168 {
169         CamelMimeFilterBestenc *f = (CamelMimeFilterBestenc *)mf;
170
171         filter(mf, in, len, prespace, out, outlen, outprespace);
172
173         if (f->countline > f->maxline)
174                 f->maxline = f->countline;
175         f->countline = 0;
176 }
177
178 static void
179 camel_mime_filter_bestenc_class_init (CamelMimeFilterBestencClass *klass)
180 {
181         CamelMimeFilterClass *filter_class = (CamelMimeFilterClass *) klass;
182
183         camel_mime_filter_bestenc_parent = (CamelMimeFilterClass *)(camel_type_get_global_classfuncs (camel_mime_filter_get_type ()));
184
185         filter_class->reset = reset;
186         filter_class->filter = filter;
187         filter_class->complete = complete;
188 }
189
190 static void
191 camel_mime_filter_bestenc_init (CamelMimeFilter *f)
192 {
193         reset(f);
194 }
195
196 /**
197  * camel_mime_filter_bestenc_new:
198  * @flags: a bitmask of data required.
199  *
200  * Create a new #CamelMimeFilterBestenc object. 
201  * 
202  * Returns a new #CamelMimeFilterBestenc object
203  **/
204 CamelMimeFilterBestenc *
205 camel_mime_filter_bestenc_new (unsigned int flags)
206 {
207         CamelMimeFilterBestenc *new = (CamelMimeFilterBestenc *)camel_object_new(camel_mime_filter_bestenc_get_type());
208         new->flags = flags;
209         return new;
210 }
211
212
213 /**
214  * camel_mime_filter_bestenc_get_best_encoding:
215  * @filter: a #CamelMimeFilterBestenc object
216  * @required: maximum level of output encoding allowed.
217  * 
218  * Get the best encoding, given specific constraints, that can be used to
219  * encode a stream of bytes.
220  * 
221  * Returns the best encoding to use
222  **/
223 CamelTransferEncoding
224 camel_mime_filter_bestenc_get_best_encoding(CamelMimeFilterBestenc *filter, CamelBestencEncoding required)
225 {
226         CamelTransferEncoding bestenc;
227         int istext;
228         
229         istext = (required & CAMEL_BESTENC_TEXT) ? 1 : 0;
230         required = required & ~CAMEL_BESTENC_TEXT;
231         
232 #if 0
233         printf("count0 = %d, count8 = %d, total = %d\n", filter->count0, filter->count8, filter->total);
234         printf("maxline = %d, crlfnoorder = %s\n", filter->maxline, filter->crlfnoorder?"TRUE":"FALSE");
235         printf(" %d%% require encoding?\n", (filter->count0+filter->count8)*100 / filter->total);
236 #endif
237
238         /* if we're not allowed to have From lines and we had one, use an encoding
239            that will never let it show.  Unfortunately only base64 can at present,
240            although qp could be modified to allow it too */
241         if ((filter->flags & CAMEL_BESTENC_NO_FROM) && filter->hadfrom)
242                 return CAMEL_TRANSFER_ENCODING_BASE64;
243
244         /* if we need to encode, see how we do it */
245         if (required == CAMEL_BESTENC_BINARY)
246                 bestenc = CAMEL_TRANSFER_ENCODING_BINARY;
247         else if (istext && (filter->count0 == 0 && filter->count8 < (filter->total * 17 / 100)))
248                 bestenc = CAMEL_TRANSFER_ENCODING_QUOTEDPRINTABLE;
249         else
250                 bestenc = CAMEL_TRANSFER_ENCODING_BASE64;
251         
252         /* if we have nocrlf order, or long lines, we need to encode always */
253         if (filter->crlfnoorder || filter->maxline >= 998)
254                 return bestenc;
255
256         /* if we have no 8 bit chars or nul's, we can just use 7 bit */
257         if (filter->count8 + filter->count0 == 0)
258                 return CAMEL_TRANSFER_ENCODING_7BIT;
259
260         /* otherwise, we see if we can use 8 bit, or not */
261         switch(required) {
262         case CAMEL_BESTENC_7BIT:
263                 return bestenc;
264         case CAMEL_BESTENC_8BIT:
265         case CAMEL_BESTENC_BINARY:
266         default:
267                 if (filter->count0 == 0)
268                         return CAMEL_TRANSFER_ENCODING_8BIT;
269                 else
270                         return bestenc;
271         }
272
273 }
274
275
276 /**
277  * camel_mime_filter_bestenc_get_best_charset:
278  * @filter: a #CamelMimeFilterBestenc object
279  * 
280  * Gets the best charset that can be used to contain this content.
281  * 
282  * Returns the name of the best charset to use to encode the input
283  * text filtered by @filter
284  **/
285 const char *
286 camel_mime_filter_bestenc_get_best_charset (CamelMimeFilterBestenc *filter)
287 {
288         return camel_charset_best_name(&filter->charset);
289 }
290
291
292 /**
293  * camel_mime_filter_bestenc_set_flags:
294  * @filter: a #CamelMimeFilterBestenc object
295  * @flags: bestenc filter flags
296  * 
297  * Set the flags for subsequent operations.
298  **/
299 void
300 camel_mime_filter_bestenc_set_flags(CamelMimeFilterBestenc *filter, unsigned int flags)
301 {
302         filter->flags = flags;
303 }