aboutsummaryrefslogtreecommitdiffstats
path: root/src/charset.cpp
blob: 96b854ed23e21b5f67c250d26d9b9db73ee5bd0f (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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
//
// VMime library (http://vmime.sourceforge.net)
// Copyright (C) 2002-2005 Vincent Richard <[email protected]>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU 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
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//

#include "vmime/charset.hpp"
#include "vmime/exception.hpp"
#include "vmime/platformDependant.hpp"

#include "vmime/utility/stringUtils.hpp"


extern "C"
{
#ifndef VMIME_BUILDING_DOC

	#include <iconv.h>

	// HACK: prototypes may differ depending on the compiler and/or system (the
	// second parameter may or may not be 'const'). This redeclaration is a hack
	// to have a common prototype "iconv_cast".
	class ICONV_HACK
	{
	public:

		ICONV_HACK(const char** ptr) : m_ptr(ptr) { }

		operator const char**() { return m_ptr; }
		operator char**() { return const_cast <char**>(m_ptr); }

	private:

		const char** m_ptr;
	};

#endif // VMIME_BUILDING_DOC
}


namespace vmime
{


charset::charset()
	: m_name(charsets::US_ASCII)
{
}


charset::charset(const string& name)
	: m_name(name)
{
}


void charset::parse(const string& buffer, const string::size_type position,
	const string::size_type end, string::size_type* newPosition)
{
	m_name = string(buffer.begin() + position, buffer.begin() + end);

	setParsedBounds(position, end);

	if (newPosition)
		*newPosition = end;
}


void charset::generate(utility::outputStream& os, const string::size_type /* maxLineLength */,
	const string::size_type curLinePos, string::size_type* newLinePos) const
{
	os << m_name;

	if (newLinePos)
		*newLinePos = curLinePos + m_name.length();
}


struct X
{
	X(const char**);

	operator const char**() { return x; }
	operator char**() { return const_cast <char**>(x); }

	const char** x;
};

void charset::convert(utility::inputStream& in, utility::outputStream& out,
	const charset& source, const charset& dest)
{
	// Get an iconv descriptor
	const iconv_t cd = iconv_open(dest.getName().c_str(), source.getName().c_str());

	if (cd != reinterpret_cast <iconv_t>(-1))
	{
		char inBuffer[5];
		char outBuffer[32768];
		size_t inPos = 0;

		bool prevIsInvalid = false;

		while (true)
		{
			// Fullfill the buffer
			size_t inLength = static_cast <size_t>(in.read(inBuffer + inPos, sizeof(inBuffer) - inPos) + inPos);
			size_t outLength = sizeof(outBuffer);

			const char* inPtr = inBuffer;
			char* outPtr = outBuffer;

			// Convert input bytes
			if (iconv(cd, ICONV_HACK(&inPtr), &inLength,
			              &outPtr, &outLength) == static_cast <size_t>(-1))
			{
				// Illegal input sequence or input sequence has no equivalent
				// sequence in the destination charset.
				if (prevIsInvalid)
				{
					// Write successfully converted bytes
					out.write(outBuffer, sizeof(outBuffer) - outLength);

					// Output a special character to indicate we don't known how to
					// convert the sequence at this position
					out.write("?", 1);

					// Skip a byte and leave unconverted bytes in the input buffer
					std::copy(const_cast <char*>(inPtr + 1), inBuffer + sizeof(inBuffer), inBuffer);
					inPos = inLength - 1;
				}
				else
				{
					// Write successfully converted bytes
					out.write(outBuffer, sizeof(outBuffer) - outLength);

					// Leave unconverted bytes in the input buffer
					std::copy(const_cast <char*>(inPtr), inBuffer + sizeof(inBuffer), inBuffer);
					inPos = inLength;

					prevIsInvalid = true;
				}
			}
			else
			{
				// Write successfully converted bytes
				out.write(outBuffer, sizeof(outBuffer) - outLength);

				inPos = 0;
				prevIsInvalid = false;
			}

			// Check for end of data
			if (in.eof() && inPos == 0)
				break;
		}

		// Close iconv handle
		iconv_close(cd);
	}
	else
	{
		throw exceptions::charset_conv_error();
	}
}


template <class STRINGF, class STRINGT>
void charset::iconvert(const STRINGF& in, STRINGT& out, const charset& from, const charset& to)
{
	// Get an iconv descriptor
	const iconv_t cd = iconv_open(to.getName().c_str(), from.getName().c_str());

	typedef typename STRINGF::value_type ivt;
	typedef typename STRINGT::value_type ovt;

	if (cd != reinterpret_cast <iconv_t>(-1))
	{
		out.clear();

		char buffer[65536];

		const char* inBuffer = static_cast <const char*>(in.data());
		size_t inBytesLeft = in.length();

		for ( ; inBytesLeft > 0 ; )
		{
			size_t outBytesLeft = sizeof(buffer);
			char* outBuffer = buffer;

			if (iconv(cd, ICONV_HACK(&inBuffer), &inBytesLeft,
			              &outBuffer, &outBytesLeft) == static_cast <size_t>(-1))
			{
				out += STRINGT(static_cast <ovt*>(buffer), sizeof(buffer) - outBytesLeft);

				// Ignore this "blocking" character and continue
				out += '?';
				++inBuffer;
				--inBytesLeft;
			}
			else
			{
				out += STRINGT(static_cast <ovt*>(buffer), sizeof(buffer) - outBytesLeft);
			}
		}

		// Close iconv handle
		iconv_close(cd);
	}
	else
	{
		throw exceptions::charset_conv_error();
	}
}


#if VMIME_WIDE_CHAR_SUPPORT

void charset::decode(const string& in, wstring& out, const charset& ch)
{
	iconvert(in, out, ch, charset("WCHAR_T"));
}


void charset::encode(const wstring& in, string& out, const charset& ch)
{
	iconvert(in, out, charset("WCHAR_T"), ch);
}

#endif


void charset::convert(const string& in, string& out, const charset& source, const charset& dest)
{
	iconvert(in, out, source, dest);
}


const charset charset::getLocaleCharset()
{
	return (platformDependant::getHandler()->getLocaleCharset());
}


charset& charset::operator=(const charset& other)
{
	copyFrom(other);
	return (*this);
}


charset& charset::operator=(const string& name)
{
	parse(name);
	return (*this);
}


const bool charset::operator==(const charset& value) const
{
	return (utility::stringUtils::isStringEqualNoCase(m_name, value.m_name));
}


const bool charset::operator!=(const charset& value) const
{
	return !(*this == value);
}


charset* charset::clone() const
{
	return new charset(m_name);
}


const string& charset::getName() const
{
	return (m_name);
}


void charset::copyFrom(const component& other)
{
	m_name = dynamic_cast <const charset&>(other).m_name;
}


const std::vector <const component*> charset::getChildComponents() const
{
	return std::vector <const component*>();
}


} // vmime