8b89180e712a7b4a204089afdbf34ff37c1b3f6f
[platform/upstream/evolution-data-server.git] / camel / camel-mime-filter-crlf.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  *  Copyright (C) 2000 Ximian, Inc.
4  *
5  *  Authors: Dan Winship <danw@ximian.com>
6  *           Jeffrey Stedfast <fejj@ximian.com>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of version 2 of the GNU Lesser General Public
10  * License as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this program; if not, write to the
19  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22
23 #include "camel-mime-filter-crlf.h"
24
25 static void filter (CamelMimeFilter *f, char *in, size_t len, size_t prespace,
26                     char **out, size_t *outlen, size_t *outprespace);
27 static void complete (CamelMimeFilter *f, char *in, size_t len,
28                       size_t prespace, char **out, size_t *outlen,
29                       size_t *outprespace);
30 static void reset (CamelMimeFilter *f);
31
32
33 static void
34 camel_mime_filter_crlf_class_init (CamelMimeFilterCRLFClass *klass)
35 {
36         CamelMimeFilterClass *mime_filter_class =
37                 (CamelMimeFilterClass *) klass;
38         
39         mime_filter_class->filter = filter;
40         mime_filter_class->complete = complete;
41         mime_filter_class->reset = reset;
42 }
43
44 CamelType
45 camel_mime_filter_crlf_get_type (void)
46 {
47         static CamelType type = CAMEL_INVALID_TYPE;
48         
49         if (type == CAMEL_INVALID_TYPE) {
50                 type = camel_type_register (camel_mime_filter_get_type(), "CamelMimeFilterCRLF",
51                                             sizeof (CamelMimeFilterCRLF),
52                                             sizeof (CamelMimeFilterCRLFClass),
53                                             (CamelObjectClassInitFunc) camel_mime_filter_crlf_class_init,
54                                             NULL,
55                                             NULL,
56                                             NULL);
57         }
58
59         return type;
60 }
61
62 static void
63 filter (CamelMimeFilter *f, char *in, size_t len, size_t prespace,
64         char **out, size_t *outlen, size_t *outprespace)
65 {
66         CamelMimeFilterCRLF *crlf = (CamelMimeFilterCRLF *)f;
67         register const char *inptr;
68         const char *inend;
69         gboolean do_dots;
70         char *outptr;
71         
72         do_dots = crlf->mode == CAMEL_MIME_FILTER_CRLF_MODE_CRLF_DOTS;
73         
74         inptr = in;
75         inend = in + len;
76         
77         if (crlf->direction == CAMEL_MIME_FILTER_CRLF_ENCODE) {
78                 camel_mime_filter_set_size (f, 3 * len, FALSE);
79                 
80                 outptr = f->outbuf;
81                 while (inptr < inend) {
82                         if (*inptr == '\r') {
83                                 crlf->saw_cr = TRUE;
84                         } else if (*inptr == '\n') {
85                                 crlf->saw_lf = TRUE;
86                                 if (!crlf->saw_cr)
87                                         *outptr++ = '\r';
88                                 crlf->saw_cr = FALSE;
89                         } else {
90                                 if (do_dots && *inptr == '.' && crlf->saw_lf)
91                                         *outptr++ = '.';
92                                 
93                                 crlf->saw_cr = FALSE;
94                                 crlf->saw_lf = FALSE;
95                         }
96                         
97                         *outptr++ = *inptr++;
98                 }
99         } else {
100                 /* Output can "grow" by one byte if crlf->saw_cr was set as
101                  * a carry-over from the previous invocation. This will happen
102                  * in practice, as the input is processed in arbitrarily-sized
103                  * blocks. */
104                 camel_mime_filter_set_size (f, len + 1, FALSE);
105                 
106                 outptr = f->outbuf;
107                 while (inptr < inend) {
108                         if (*inptr == '\r') {
109                                 crlf->saw_cr = TRUE;
110                         } else {
111                                 if (crlf->saw_cr) {
112                                         crlf->saw_cr = FALSE;
113                                         
114                                         if (*inptr == '\n') {
115                                                 crlf->saw_lf = TRUE;
116                                                 *outptr++ = *inptr++;
117                                                 continue;
118                                         } else
119                                                 *outptr++ = '\r';
120                                 }
121                                 
122                                 *outptr++ = *inptr;
123                         }
124                         
125                         if (do_dots && *inptr == '.') {
126                                 if (crlf->saw_lf) {
127                                         crlf->saw_dot = TRUE;
128                                         crlf->saw_lf = FALSE;
129                                         inptr++;
130                                 } else if (crlf->saw_dot) {
131                                         crlf->saw_dot = FALSE;
132                                 }
133                         }
134                         
135                         crlf->saw_lf = FALSE;
136                         
137                         inptr++;
138                 }
139         }
140         
141         *out = f->outbuf;
142         *outlen = outptr - f->outbuf;
143         *outprespace = f->outpre;
144 }
145
146 static void 
147 complete (CamelMimeFilter *f, char *in, size_t len, size_t prespace,
148           char **out, size_t *outlen, size_t *outprespace)
149 {
150         if (len)
151                 filter (f, in, len, prespace, out, outlen, outprespace);
152 }
153
154 static void
155 reset (CamelMimeFilter *f)
156 {
157         CamelMimeFilterCRLF *crlf = (CamelMimeFilterCRLF *)f;
158         
159         crlf->saw_cr = FALSE;
160         crlf->saw_lf = TRUE;
161         crlf->saw_dot = FALSE;
162 }
163
164
165 /**
166  * camel_mime_filter_crlf_new:
167  * @direction: encode vs decode
168  * @mode: whether or not to perform SMTP dot-escaping
169  *
170  * Create a new #CamelMimeFiletrCRLF object.
171  *
172  * Returns a new #CamelMimeFilterCRLF object
173  **/
174 CamelMimeFilter *
175 camel_mime_filter_crlf_new (CamelMimeFilterCRLFDirection direction, CamelMimeFilterCRLFMode mode)
176 {
177         CamelMimeFilterCRLF *crlf = CAMEL_MIME_FILTER_CRLF(camel_object_new (CAMEL_MIME_FILTER_CRLF_TYPE));
178         
179         crlf->direction = direction;
180         crlf->mode = mode;
181         crlf->saw_cr = FALSE;
182         crlf->saw_lf = TRUE;
183         crlf->saw_dot = FALSE;
184         
185         return (CamelMimeFilter *)crlf;
186 }