1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2000 Ximian, Inc.
5 * Authors: Dan Winship <danw@ximian.com>
6 * Jeffrey Stedfast <fejj@ximian.com>
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.
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.
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.
23 #include "camel-mime-filter-crlf.h"
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,
30 static void reset (CamelMimeFilter *f);
34 camel_mime_filter_crlf_class_init (CamelMimeFilterCRLFClass *klass)
36 CamelMimeFilterClass *mime_filter_class =
37 (CamelMimeFilterClass *) klass;
39 mime_filter_class->filter = filter;
40 mime_filter_class->complete = complete;
41 mime_filter_class->reset = reset;
45 camel_mime_filter_crlf_get_type (void)
47 static CamelType type = CAMEL_INVALID_TYPE;
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,
63 filter (CamelMimeFilter *f, char *in, size_t len, size_t prespace,
64 char **out, size_t *outlen, size_t *outprespace)
66 CamelMimeFilterCRLF *crlf = (CamelMimeFilterCRLF *)f;
67 register const char *inptr;
72 do_dots = crlf->mode == CAMEL_MIME_FILTER_CRLF_MODE_CRLF_DOTS;
77 if (crlf->direction == CAMEL_MIME_FILTER_CRLF_ENCODE) {
78 camel_mime_filter_set_size (f, 3 * len, FALSE);
81 while (inptr < inend) {
84 } else if (*inptr == '\n') {
90 if (do_dots && *inptr == '.' && crlf->saw_lf)
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
104 camel_mime_filter_set_size (f, len + 1, FALSE);
107 while (inptr < inend) {
108 if (*inptr == '\r') {
112 crlf->saw_cr = FALSE;
114 if (*inptr == '\n') {
116 *outptr++ = *inptr++;
125 if (do_dots && *inptr == '.') {
127 crlf->saw_dot = TRUE;
128 crlf->saw_lf = FALSE;
130 } else if (crlf->saw_dot) {
131 crlf->saw_dot = FALSE;
135 crlf->saw_lf = FALSE;
142 *outlen = outptr - f->outbuf;
143 *outprespace = f->outpre;
147 complete (CamelMimeFilter *f, char *in, size_t len, size_t prespace,
148 char **out, size_t *outlen, size_t *outprespace)
151 filter (f, in, len, prespace, out, outlen, outprespace);
155 reset (CamelMimeFilter *f)
157 CamelMimeFilterCRLF *crlf = (CamelMimeFilterCRLF *)f;
159 crlf->saw_cr = FALSE;
161 crlf->saw_dot = FALSE;
166 * camel_mime_filter_crlf_new:
167 * @direction: encode vs decode
168 * @mode: whether or not to perform SMTP dot-escaping
170 * Create a new #CamelMimeFiletrCRLF object.
172 * Returns a new #CamelMimeFilterCRLF object
175 camel_mime_filter_crlf_new (CamelMimeFilterCRLFDirection direction, CamelMimeFilterCRLFMode mode)
177 CamelMimeFilterCRLF *crlf = CAMEL_MIME_FILTER_CRLF(camel_object_new (CAMEL_MIME_FILTER_CRLF_TYPE));
179 crlf->direction = direction;
181 crlf->saw_cr = FALSE;
183 crlf->saw_dot = FALSE;
185 return (CamelMimeFilter *)crlf;