File: rfc2047decode.C

package info (click to toggle)
cone 0.75-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 31,040 kB
  • ctags: 13,930
  • sloc: ansic: 90,648; cpp: 79,781; sh: 18,355; perl: 3,218; makefile: 1,611; yacc: 289; sed: 16
file content (205 lines) | stat: -rw-r--r-- 3,742 bytes parent folder | download
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
202
203
204
205
/* $Id: rfc2047decode.C,v 1.3 2008/05/24 17:57:42 mrsam Exp $
**
** Copyright 2002-2008, Double Precision Inc.
**
** See COPYING for distribution information.
*/
#include "libmail_config.h"
#include "rfc2047decode.H"
#include "rfc822/rfc2047.h"
#include "mail.H"
#include <cstring>

using namespace std;

mail::rfc2047::decoder::decoder()
{
}

mail::rfc2047::decoder::~decoder()
{
}

int mail::rfc2047::decoder::rfc2047_decode_callback(const char *text,
						    int text_len,
						    const char *charset,
						    const char *language,
						    void *voidarg)
{
	return ((mail::rfc2047::decoder *)voidarg)
		->rfc2047_callback(text, text_len, charset, language);
}

int mail::rfc2047::decoder::rfc2047_callback(const char *text, int text_len,
					     const char *charset,
					     const char *language)
{
	if (charset == NULL)
	{
		string s;

		s.insert(s.end(), text, text+text_len);

		unicode_char *u= (*unicode_ISO8859_1.c2u)(&unicode_ISO8859_1,
							  s.c_str(), NULL);

		if (!u)
			LIBMAIL_THROW("Out of memory.");

		size_t n;

		for (n=0; u[n]; n++)
			;
		try {
			unicodes.insert(unicodes.end(), u, u+n);
			free(u);
		} catch (...) {
			free(u);
			LIBMAIL_THROW();
		}
		return (0);
	}

	const struct unicode_info *uc=unicode_find(charset);

	if (strcasecmp(charset, nativeCharset->chset) == 0)
		uc= nativeCharset;

	if (strcasecmp(charset, nativeCharset->chset)
	    && !(nativeCharset->flags & UNICODE_UTF))
		// No need to insert [] for UTF charset
	{
		string s=string(" [") + charset + "]";
		rfc2047_callback(&*s.begin(), s.size(), NULL, NULL);
		// HACK - reuse code above.
	}

	if (uc == NULL)
		uc= &unicode_ISO8859_1;

	string s;

	s.insert(s.end(), text, text+text_len);

	unicode_char *u= (*uc->c2u)(uc, s.c_str(), NULL);

	if (!u)
		LIBMAIL_THROW("Out of memory.");

	size_t n;

	for (n=0; u[n]; n++)
		;
	try {
		unicodes.insert(unicodes.end(), u, u+n);
		free(u);
	} catch (...) {
		free(u);
		LIBMAIL_THROW();
	}
	return (0);
}

const unicode_char *mail::rfc2047::decoder::decode(string text)
{
	nativeCharset= &unicode_ISO8859_1;
	unicodes.clear();

	rfc2047_decode(text.c_str(),
		       &mail::rfc2047::decoder::rfc2047_decode_callback,
		       this);
	unicodes.push_back(0);

	return &*unicodes.begin();
}

string mail::rfc2047::decoder::decode(string text, string charset)
{
	const struct unicode_info *uc=unicode_find(charset.c_str());

	if (!uc)
		uc=&unicode_ISO8859_1;

	return decode(text, *uc);
}

string mail::rfc2047::decoder::decode(string text,
				      const struct unicode_info &uc)
{
	nativeCharset=&uc;
	unicodes.clear();

	rfc2047_decode(text.c_str(),
		       &mail::rfc2047::decoder::rfc2047_decode_callback,
		       this);

	unicodes.push_back(0);


	string s;

	char *p= (uc.u2c)(&uc, &*unicodes.begin(), NULL);

	if (!p)
		LIBMAIL_THROW("Out of memory.");

	try {
		s=p;
		free(p);
	} catch (...) {
		free(p);
		LIBMAIL_THROW();
	}
	return s;
}

void mail::rfc2047::decoder::decode(vector<mail::address> &addr_cpy,
				    const struct unicode_info &unicodeInfo)
{
	vector<mail::address>::iterator b=addr_cpy.begin(), e=addr_cpy.end();

	while (b != e)
	{
		b->setName(decode(b->getName(), unicodeInfo));
		b++;
	}
}

string mail::rfc2047::decoder::decodeSimple(string str)
{
	char *p=rfc2047_decode_simple(str.c_str());

	string s="";

	if (p)
		try {
			s=p;
			free(p);
		} catch (...) {
			free(p);
			LIBMAIL_THROW();
		}

	return s;
}

string mail::rfc2047::decoder::decodeEnhanced(string str,
					      const struct unicode_info
					      &myCharset)
{
	char *p=rfc2047_decode_unicode(str.c_str(), &myCharset,
				       RFC2047_DECODE_NOTAG);

	string s="";

	if (p)
		try {
			s=p;
			free(p);
		} catch (...) {
			free(p);
			LIBMAIL_THROW();
		}

	return s;
}