1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
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 #define CAMEL_MIME_FILTER_CRLF_GET_PRIVATE(obj) \
26 (G_TYPE_INSTANCE_GET_PRIVATE \
27 ((obj), CAMEL_TYPE_MIME_FILTER_CRLF, CamelMimeFilterCRLFPrivate))
29 struct _CamelMimeFilterCRLFPrivate {
30 CamelMimeFilterCRLFDirection direction;
31 CamelMimeFilterCRLFMode mode;
37 G_DEFINE_TYPE (CamelMimeFilterCRLF, camel_mime_filter_crlf, CAMEL_TYPE_MIME_FILTER)
40 mime_filter_crlf_filter (CamelMimeFilter *mime_filter,
48 CamelMimeFilterCRLFPrivate *priv;
49 register const gchar *inptr;
54 priv = CAMEL_MIME_FILTER_CRLF_GET_PRIVATE (mime_filter);
56 do_dots = priv->mode == CAMEL_MIME_FILTER_CRLF_MODE_CRLF_DOTS;
61 if (priv->direction == CAMEL_MIME_FILTER_CRLF_ENCODE) {
62 camel_mime_filter_set_size (mime_filter, 3 * len, FALSE);
64 outptr = mime_filter->outbuf;
65 while (inptr < inend) {
68 } else if (*inptr == '\n') {
74 if (do_dots && *inptr == '.' && priv->saw_lf)
84 /* Output can "grow" by one byte if priv->saw_cr was set as
85 * a carry-over from the previous invocation. This will happen
86 * in practice, as the input is processed in arbitrarily-sized
88 camel_mime_filter_set_size (mime_filter, len + 1, FALSE);
90 outptr = mime_filter->outbuf;
91 while (inptr < inend) {
100 *outptr++ = *inptr++;
109 if (do_dots && *inptr == '.') {
111 priv->saw_dot = TRUE;
112 priv->saw_lf = FALSE;
114 } else if (priv->saw_dot) {
115 priv->saw_dot = FALSE;
119 priv->saw_lf = FALSE;
125 *out = mime_filter->outbuf;
126 *outlen = outptr - mime_filter->outbuf;
127 *outprespace = mime_filter->outpre;
131 mime_filter_crlf_complete (CamelMimeFilter *mime_filter,
140 mime_filter_crlf_filter (
141 mime_filter, in, len, prespace,
142 out, outlen, outprespace);
146 mime_filter_crlf_reset (CamelMimeFilter *mime_filter)
148 CamelMimeFilterCRLFPrivate *priv;
150 priv = CAMEL_MIME_FILTER_CRLF_GET_PRIVATE (mime_filter);
152 priv->saw_cr = FALSE;
154 priv->saw_dot = FALSE;
158 camel_mime_filter_crlf_class_init (CamelMimeFilterCRLFClass *class)
160 CamelMimeFilterClass *mime_filter_class;
162 g_type_class_add_private (class, sizeof (CamelMimeFilterCRLFPrivate));
164 mime_filter_class = CAMEL_MIME_FILTER_CLASS (class);
165 mime_filter_class->filter = mime_filter_crlf_filter;
166 mime_filter_class->complete = mime_filter_crlf_complete;
167 mime_filter_class->reset = mime_filter_crlf_reset;
171 camel_mime_filter_crlf_init (CamelMimeFilterCRLF *filter)
173 filter->priv = CAMEL_MIME_FILTER_CRLF_GET_PRIVATE (filter);
175 filter->priv->saw_cr = FALSE;
176 filter->priv->saw_lf = TRUE;
177 filter->priv->saw_dot = FALSE;
181 * camel_mime_filter_crlf_new:
182 * @direction: encode vs decode
183 * @mode: whether or not to perform SMTP dot-escaping
185 * Create a new #CamelMimeFiletrCRLF object.
187 * Returns: a new #CamelMimeFilterCRLF object
190 camel_mime_filter_crlf_new (CamelMimeFilterCRLFDirection direction,
191 CamelMimeFilterCRLFMode mode)
193 CamelMimeFilter *filter;
194 CamelMimeFilterCRLFPrivate *priv;
196 filter = g_object_new (CAMEL_TYPE_MIME_FILTER_CRLF, NULL);
197 priv = CAMEL_MIME_FILTER_CRLF_GET_PRIVATE (filter);
199 priv->direction = direction;