summaryrefslogtreecommitdiff
path: root/camel/camel-mime-filter-crlf.c
blob: 4d3d20b27e8ce27b01bb7d2e1c95f0448fcd714d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 * This library is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors: Dan Winship <danw@ximian.com>
 *          Jeffrey Stedfast <fejj@ximian.com>
 */

#include "camel-mime-filter-crlf.h"

#define CAMEL_MIME_FILTER_CRLF_GET_PRIVATE(obj) \
	(G_TYPE_INSTANCE_GET_PRIVATE \
	((obj), CAMEL_TYPE_MIME_FILTER_CRLF, CamelMimeFilterCRLFPrivate))

struct _CamelMimeFilterCRLFPrivate {
	CamelMimeFilterCRLFDirection direction;
	CamelMimeFilterCRLFMode mode;
	gboolean saw_cr;
	gboolean saw_lf;
	gboolean saw_dot;
};

G_DEFINE_TYPE (CamelMimeFilterCRLF, camel_mime_filter_crlf, CAMEL_TYPE_MIME_FILTER)

static void
mime_filter_crlf_filter (CamelMimeFilter *mime_filter,
                         const gchar *in,
                         gsize len,
                         gsize prespace,
                         gchar **out,
                         gsize *outlen,
                         gsize *outprespace)
{
	CamelMimeFilterCRLFPrivate *priv;
	register const gchar *inptr;
	const gchar *inend;
	gboolean do_dots;
	gchar *outptr;

	priv = CAMEL_MIME_FILTER_CRLF_GET_PRIVATE (mime_filter);

	do_dots = priv->mode == CAMEL_MIME_FILTER_CRLF_MODE_CRLF_DOTS;

	inptr = in;
	inend = in + len;

	if (priv->direction == CAMEL_MIME_FILTER_CRLF_ENCODE) {
		camel_mime_filter_set_size (mime_filter, 3 * len, FALSE);

		outptr = mime_filter->outbuf;
		while (inptr < inend) {
			if (*inptr == '\r') {
				priv->saw_cr = TRUE;
			} else if (*inptr == '\n') {
				priv->saw_lf = TRUE;
				if (!priv->saw_cr)
					*outptr++ = '\r';
				priv->saw_cr = FALSE;
			} else {
				if (do_dots && *inptr == '.' && priv->saw_lf)
					*outptr++ = '.';

				priv->saw_cr = FALSE;
				priv->saw_lf = FALSE;
			}

			*outptr++ = *inptr++;
		}
	} else {
		/* Output can "grow" by one byte if priv->saw_cr was set as
		 * a carry-over from the previous invocation. This will happen
		 * in practice, as the input is processed in arbitrarily-sized
		 * blocks. */
		camel_mime_filter_set_size (mime_filter, len + 1, FALSE);

		outptr = mime_filter->outbuf;
		while (inptr < inend) {
			if (*inptr == '\r') {
				priv->saw_cr = TRUE;
			} else {
				if (priv->saw_cr) {
					priv->saw_cr = FALSE;

					if (*inptr == '\n') {
						priv->saw_lf = TRUE;
						*outptr++ = *inptr++;
						continue;
					} else
						*outptr++ = '\r';
				}

				*outptr++ = *inptr;
			}

			if (do_dots && *inptr == '.') {
				if (priv->saw_lf) {
					priv->saw_dot = TRUE;
					priv->saw_lf = FALSE;
					inptr++;
				} else if (priv->saw_dot) {
					priv->saw_dot = FALSE;
				}
			}

			priv->saw_lf = FALSE;

			inptr++;
		}
	}

	*out = mime_filter->outbuf;
	*outlen = outptr - mime_filter->outbuf;
	*outprespace = mime_filter->outpre;
}

static void
mime_filter_crlf_complete (CamelMimeFilter *mime_filter,
                           const gchar *in,
                           gsize len,
                           gsize prespace,
                           gchar **out,
                           gsize *outlen,
                           gsize *outprespace)
{
	if (len)
		mime_filter_crlf_filter (
			mime_filter, in, len, prespace,
			out, outlen, outprespace);
}

static void
mime_filter_crlf_reset (CamelMimeFilter *mime_filter)
{
	CamelMimeFilterCRLFPrivate *priv;

	priv = CAMEL_MIME_FILTER_CRLF_GET_PRIVATE (mime_filter);

	priv->saw_cr = FALSE;
	priv->saw_lf = TRUE;
	priv->saw_dot = FALSE;
}

static void
camel_mime_filter_crlf_class_init (CamelMimeFilterCRLFClass *class)
{
	CamelMimeFilterClass *mime_filter_class;

	g_type_class_add_private (class, sizeof (CamelMimeFilterCRLFPrivate));

	mime_filter_class = CAMEL_MIME_FILTER_CLASS (class);
	mime_filter_class->filter = mime_filter_crlf_filter;
	mime_filter_class->complete = mime_filter_crlf_complete;
	mime_filter_class->reset = mime_filter_crlf_reset;
}

static void
camel_mime_filter_crlf_init (CamelMimeFilterCRLF *filter)
{
	filter->priv = CAMEL_MIME_FILTER_CRLF_GET_PRIVATE (filter);

	filter->priv->saw_cr = FALSE;
	filter->priv->saw_lf = TRUE;
	filter->priv->saw_dot = FALSE;
}

/**
 * camel_mime_filter_crlf_new:
 * @direction: encode vs decode
 * @mode: whether or not to perform SMTP dot-escaping
 *
 * Create a new #CamelMimeFiletrCRLF object.
 *
 * Returns: a new #CamelMimeFilterCRLF object
 **/
CamelMimeFilter *
camel_mime_filter_crlf_new (CamelMimeFilterCRLFDirection direction,
                            CamelMimeFilterCRLFMode mode)
{
	CamelMimeFilter *filter;
	CamelMimeFilterCRLFPrivate *priv;

	filter = g_object_new (CAMEL_TYPE_MIME_FILTER_CRLF, NULL);
	priv = CAMEL_MIME_FILTER_CRLF_GET_PRIVATE (filter);

	priv->direction = direction;
	priv->mode = mode;

	return filter;
}