1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2000-2012 Jeffrey Stedfast
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public License
7 * as published by the Free Software Foundation; either version 2.1
8 * of the License, or (at your option) any later version.
10 * This library 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 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free
17 * Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
26 #include "gmime-filter-crlf.h"
30 * SECTION: gmime-filter-crlf
31 * @title: GMimeFilterCRLF
32 * @short_description: Convert line-endings from LF to CRLF or vise versa
34 * A #GMimeFilter for converting between DOS and UNIX line-endings.
38 static void g_mime_filter_crlf_class_init (GMimeFilterCRLFClass *klass);
39 static void g_mime_filter_crlf_init (GMimeFilterCRLF *filter, GMimeFilterCRLFClass *klass);
40 static void g_mime_filter_crlf_finalize (GObject *object);
42 static GMimeFilter *filter_copy (GMimeFilter *filter);
43 static void filter_filter (GMimeFilter *filter, char *in, size_t len, size_t prespace,
44 char **out, size_t *outlen, size_t *outprespace);
45 static void filter_complete (GMimeFilter *filter, char *in, size_t len, size_t prespace,
46 char **out, size_t *outlen, size_t *outprespace);
47 static void filter_reset (GMimeFilter *filter);
50 static GMimeFilterClass *parent_class = NULL;
54 g_mime_filter_crlf_get_type (void)
56 static GType type = 0;
59 static const GTypeInfo info = {
60 sizeof (GMimeFilterCRLFClass),
61 NULL, /* base_class_init */
62 NULL, /* base_class_finalize */
63 (GClassInitFunc) g_mime_filter_crlf_class_init,
64 NULL, /* class_finalize */
65 NULL, /* class_data */
66 sizeof (GMimeFilterCRLF),
68 (GInstanceInitFunc) g_mime_filter_crlf_init,
71 type = g_type_register_static (GMIME_TYPE_FILTER, "GMimeFilterCRLF", &info, 0);
79 g_mime_filter_crlf_class_init (GMimeFilterCRLFClass *klass)
81 GObjectClass *object_class = G_OBJECT_CLASS (klass);
82 GMimeFilterClass *filter_class = GMIME_FILTER_CLASS (klass);
84 parent_class = g_type_class_ref (GMIME_TYPE_FILTER);
86 object_class->finalize = g_mime_filter_crlf_finalize;
88 filter_class->copy = filter_copy;
89 filter_class->filter = filter_filter;
90 filter_class->complete = filter_complete;
91 filter_class->reset = filter_reset;
95 g_mime_filter_crlf_init (GMimeFilterCRLF *filter, GMimeFilterCRLFClass *klass)
97 filter->saw_cr = FALSE;
98 filter->saw_lf = FALSE;
99 filter->saw_dot = FALSE;
103 g_mime_filter_crlf_finalize (GObject *object)
105 G_OBJECT_CLASS (parent_class)->finalize (object);
110 filter_copy (GMimeFilter *filter)
112 GMimeFilterCRLF *crlf = (GMimeFilterCRLF *) filter;
114 return g_mime_filter_crlf_new (crlf->encode, crlf->dots);
118 filter_filter (GMimeFilter *filter, char *inbuf, size_t inlen, size_t prespace,
119 char **outbuf, size_t *outlen, size_t *outprespace)
121 GMimeFilterCRLF *crlf = (GMimeFilterCRLF *) filter;
122 register const char *inptr = inbuf;
123 const char *inend = inbuf + inlen;
127 g_mime_filter_set_size (filter, 3 * inlen, FALSE);
129 outptr = filter->outbuf;
130 while (inptr < inend) {
131 if (*inptr == '\r') {
133 } else if (*inptr == '\n') {
137 crlf->saw_cr = FALSE;
139 if (crlf->dots && *inptr == '.' && crlf->saw_lf)
142 crlf->saw_cr = FALSE;
143 crlf->saw_lf = FALSE;
146 *outptr++ = *inptr++;
149 g_mime_filter_set_size (filter, inlen + 1, FALSE);
151 outptr = filter->outbuf;
152 while (inptr < inend) {
153 if (*inptr == '\r') {
154 crlf->saw_dot = FALSE;
158 crlf->saw_cr = FALSE;
160 if (*inptr == '\n') {
162 *outptr++ = *inptr++;
168 if (!(crlf->dots && crlf->saw_dot && *inptr == '.'))
172 if (crlf->dots && *inptr == '.') {
174 crlf->saw_dot = TRUE;
175 } else if (crlf->saw_dot) {
176 crlf->saw_dot = FALSE;
180 crlf->saw_lf = FALSE;
186 *outlen = outptr - filter->outbuf;
187 *outprespace = filter->outpre;
188 *outbuf = filter->outbuf;
192 filter_complete (GMimeFilter *filter, char *inbuf, size_t inlen, size_t prespace,
193 char **outbuf, size_t *outlen, size_t *outprespace)
196 filter_filter (filter, inbuf, inlen, prespace, outbuf, outlen, outprespace);
200 filter_reset (GMimeFilter *filter)
202 GMimeFilterCRLF *crlf = (GMimeFilterCRLF *) filter;
204 crlf->saw_cr = FALSE;
206 crlf->saw_dot = FALSE;
211 * g_mime_filter_crlf_new:
212 * @encode: %TRUE if the filter should encode or %FALSE otherwise
213 * @dots: encode/decode dots (as for SMTP)
215 * Creates a new #GMimeFilterCRLF filter.
217 * If @encode is %TRUE, then lone line-feeds ('\n') will be 'encoded'
218 * into the canonical CRLF end-of-line sequence ("\r\n") otherwise
219 * CRLF sequences will be 'decoded' into the UNIX line-ending form
222 * The @dots parameter tells the filter whether or not it should
223 * encode or decode lines beginning with a dot ('.'). If both @encode
224 * and @dots are %TRUE, then a '.' at the beginning of a line will be
225 * 'encoded' into "..". If @encode is %FALSE, then ".." at the
226 * beginning of a line will be decoded into a single '.'.
228 * Returns: a new #GMimeFilterCRLF filter.
231 g_mime_filter_crlf_new (gboolean encode, gboolean dots)
233 GMimeFilterCRLF *new;
235 new = g_object_newv (GMIME_TYPE_FILTER_CRLF, 0, NULL);
236 new->encode = encode;
239 return (GMimeFilter *) new;