File: camel-mime-filter-windows.c

package info (click to toggle)
evolution-data-server 1.6.3-5etch3
  • links: PTS
  • area: main
  • in suites: etch
  • size: 59,384 kB
  • ctags: 43,218
  • sloc: ansic: 319,315; tcl: 30,499; xml: 19,166; sh: 18,776; perl: 11,529; cpp: 8,259; java: 7,653; makefile: 6,448; awk: 1,338; yacc: 1,103; sed: 772; cs: 505; lex: 134; asm: 14
file content (201 lines) | stat: -rw-r--r-- 5,617 bytes parent folder | download | duplicates (2)
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 -*- */
/*
 *  Authors: Jeffrey Stedfast <fejj@ximian.com>
 *
 *  Copyright 2002 Ximian, Inc. (www.ximian.com)
 *
 *  This program 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; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
 *
 */


#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <string.h>
#include <ctype.h>

#include "camel-charset-map.h"
#include "camel-mime-filter-windows.h"

#define d(x)
#define w(x)

static void camel_mime_filter_windows_class_init (CamelMimeFilterWindowsClass *klass);
static void camel_mime_filter_windows_init       (CamelObject *o);
static void camel_mime_filter_windows_finalize   (CamelObject *o);

static CamelMimeFilterClass *parent_class = NULL;

CamelType
camel_mime_filter_windows_get_type (void)
{
	static CamelType type = CAMEL_INVALID_TYPE;
	
	if (type == CAMEL_INVALID_TYPE) {
		type = camel_type_register (camel_mime_filter_get_type (),
					    "CamelMimeFilterWindows",
					    sizeof (CamelMimeFilterWindows),
					    sizeof (CamelMimeFilterWindowsClass),
					    (CamelObjectClassInitFunc) camel_mime_filter_windows_class_init,
					    NULL,
					    (CamelObjectInitFunc) camel_mime_filter_windows_init,
					    (CamelObjectFinalizeFunc) camel_mime_filter_windows_finalize);
	}
	
	return type;
}

static void
camel_mime_filter_windows_finalize (CamelObject *o)
{
	CamelMimeFilterWindows *windows = (CamelMimeFilterWindows *) o;
	
	g_free (windows->claimed_charset);
}

static void
camel_mime_filter_windows_init (CamelObject *o)
{
	CamelMimeFilterWindows *windows = (CamelMimeFilterWindows *) o;
	
	windows->is_windows = FALSE;
	windows->claimed_charset = NULL;
}

static void
filter_filter (CamelMimeFilter *filter, char *in, size_t len, size_t prespace,
	       char **out, size_t *outlen, size_t *outprespace)
{
	CamelMimeFilterWindows *windows = (CamelMimeFilterWindows *) filter;
	register unsigned char *inptr;
	unsigned char *inend;
	
	if (!windows->is_windows) {
		inptr = (unsigned char *) in;
		inend = inptr + len;
		
		while (inptr < inend) {
			register unsigned char c = *inptr++;
			
			if (c >= 128 && c <= 159) {
				w(g_warning ("Encountered Windows charset masquerading as %s",
					     windows->claimed_charset));
				windows->is_windows = TRUE;
				break;
			}
		}
	}
	
	*out = in;
	*outlen = len;
	*outprespace = prespace;
}

static void 
filter_complete (CamelMimeFilter *filter, char *in, size_t len, size_t prespace,
		 char **out, size_t *outlen, size_t *outprespace)
{
	filter_filter (filter, in, len, prespace, out, outlen, outprespace);
}

static void
filter_reset (CamelMimeFilter *filter)
{
	CamelMimeFilterWindows *windows = (CamelMimeFilterWindows *) filter;
	
	windows->is_windows = FALSE;
}

static void
camel_mime_filter_windows_class_init (CamelMimeFilterWindowsClass *klass)
{
	CamelMimeFilterClass *filter_class = (CamelMimeFilterClass *) klass;
	
	parent_class = CAMEL_MIME_FILTER_CLASS (camel_type_get_global_classfuncs (camel_mime_filter_get_type ()));
	
	filter_class->reset = filter_reset;
	filter_class->filter = filter_filter;
	filter_class->complete = filter_complete;
}


/**
 * camel_mime_filter_windows_new:
 * @claimed_charset: ISO charset name
 *
 * Create a new #CamelMimeFilterWindows object that will analyse
 * whether or not the text is really encoded in @claimed_charset.
 *
 * Returns a new #CamelMimeFilter object
 **/
CamelMimeFilter *
camel_mime_filter_windows_new (const char *claimed_charset)
{
	CamelMimeFilterWindows *new;
	
	g_return_val_if_fail (claimed_charset != NULL, NULL);
	
	new = CAMEL_MIME_FILTER_WINDOWS (camel_object_new (camel_mime_filter_windows_get_type ()));
	
	new->claimed_charset = g_strdup (claimed_charset);
	
	return CAMEL_MIME_FILTER (new);
}


/**
 * camel_mime_filter_windows_is_windows_charset:
 * @filter: a #CamelMimeFilterWindows object
 *
 * Get whether or not the textual content filtered by @filetr is
 * really in a Microsoft Windows charset rather than the claimed ISO
 * charset.
 *
 * Returns %TRUE if the text was found to be in a Microsoft Windows
 * CP125x charset or %FALSE otherwise.
 **/
gboolean
camel_mime_filter_windows_is_windows_charset (CamelMimeFilterWindows *filter)
{
	g_return_val_if_fail (CAMEL_IS_MIME_FILTER_WINDOWS (filter), FALSE);
	
	return filter->is_windows;
}


/**
 * camel_mime_filter_windows_real_charset:
 * @filter: a #CamelMimeFilterWindows object
 *
 * Get the name of the actual charset used to encode the textual
 * content filtered by @filter (it will either be the original
 * claimed_charset passed in at creation time or the Windows-CP125x
 * equivalent).
 *
 * Returns the name of the actual charset
 **/
const char *
camel_mime_filter_windows_real_charset (CamelMimeFilterWindows *filter)
{
	g_return_val_if_fail (CAMEL_IS_MIME_FILTER_WINDOWS (filter), NULL);
	
	if (filter->is_windows)
		return camel_charset_iso_to_windows (filter->claimed_charset);
	else
		return filter->claimed_charset;
}