aboutsummaryrefslogtreecommitdiffstats
path: root/src/charsetConverter_icu.cpp
blob: c5a19dc6f8fd5087cbfcd1833ced300ef212ddf0 (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
//
// VMime library (http://www.vmime.org)
// Copyright (C) 2002-2013 Vincent Richard <vincent@vmime.org>
//
// 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 3 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.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// Linking this library statically or dynamically with other modules is making
// a combined work based on this library.  Thus, the terms and conditions of
// the GNU General Public License cover the whole combination.
//

#include "vmime/config.hpp"


#if VMIME_CHARSETCONV_LIB_IS_ICU


#include "vmime/charsetConverter_icu.hpp"

#include "vmime/exception.hpp"
#include "vmime/utility/inputStreamStringAdapter.hpp"
#include "vmime/utility/outputStreamStringAdapter.hpp"


extern "C"
{
#ifndef VMIME_BUILDING_DOC

	#include <unicode/ucnv.h>
	#include <unicode/ucnv_err.h>

#endif // VMIME_BUILDING_DOC
}


#include <unicode/unistr.h>


namespace vmime
{


// static
ref <charsetConverter> charsetConverter::createGenericConverter
	(const charset& source, const charset& dest,
	 const charsetConverterOptions& opts)
{
	return vmime::create <charsetConverter_icu>(source, dest, opts);
}


charsetConverter_icu::charsetConverter_icu
	(const charset& source, const charset& dest, const charsetConverterOptions& opts)
	: m_from(NULL), m_to(NULL), m_source(source), m_dest(dest), m_options(opts)
{
	UErrorCode err = U_ZERO_ERROR;
	m_from = ucnv_open(source.getName().c_str(), &err);

	if (err != U_ZERO_ERROR)
		throw exceptions::charset_conv_error("Cannot initialize ICU [from] converter.");

	m_to = ucnv_open(dest.getName().c_str(), &err);

	if (err != U_ZERO_ERROR)
		throw exceptions::charset_conv_error("Cannot initialize ICU [to] converter.");
}


charsetConverter_icu::~charsetConverter_icu()
{
	ucnv_close(m_from);
	ucnv_close(m_to);
}


void charsetConverter_icu::convert(utility::inputStream& in, utility::outputStream& out)
{
	UErrorCode err = U_ZERO_ERROR;

	// From buffers
	char cpInBuffer[16]; // stream data put here
	size_t outSize = ucnv_getMinCharSize(m_from) * sizeof(cpInBuffer) * sizeof(UChar);
	UChar* uOutBuffer = new UChar[outSize]; // Unicode chars end up here

	// Auto delete Unicode char buffer
	vmime::utility::auto_ptr<UChar> cleanup(uOutBuffer);

	// To buffers
	// converted (char) data end up here
	size_t cpOutBufferSz = ucnv_getMaxCharSize(m_to) * outSize;
	char* cpOutBuffer = new char[cpOutBufferSz];
	vmime::utility::auto_ptr<char> cleanupOut(cpOutBuffer);

	// Set replacement chars for when converting from Unicode to codepage
	icu::UnicodeString substString(m_options.invalidSequence.c_str());
	ucnv_setSubstString(m_to, substString.getTerminatedBuffer(), -1, &err);

	if (U_FAILURE(err))
		throw exceptions::charset_conv_error("[ICU] Error setting replacement char.");

	// Input data available
	while (!in.eof())
	{
		// Read input data into buffer
		size_t inLength = static_cast<size_t>(in.read(cpInBuffer, sizeof(cpInBuffer)));

		// Beginning of read data
		const char* source = &cpInBuffer[0];
		const char* sourceLimit = source + inLength; // end + 1

		UBool flush = in.eof();  // is this last run?

		UErrorCode toErr;

		// Loop until all source has been processed
		do
		{
			// Set up target pointers
			UChar* target = uOutBuffer;
			UChar* targetLimit = target + outSize;

			toErr = U_ZERO_ERROR;
			ucnv_toUnicode(m_from, &target, targetLimit,
			               &source, sourceLimit, NULL, flush, &toErr);

			if (toErr != U_BUFFER_OVERFLOW_ERROR && U_FAILURE(toErr))
				throw exceptions::charset_conv_error("[ICU] Error converting to Unicode from " + m_source.getName());

			// The Unicode source is the buffer just written and the limit
			// is where the previous conversion stopped (target is moved in the conversion)
			const UChar* uSource = uOutBuffer;
			UChar* uSourceLimit = target;
			UErrorCode fromErr;

			// Loop until converted chars are fully written
			do
			{
				char* cpTarget = &cpOutBuffer[0];
				const char* cpTargetLimit = cpOutBuffer + cpOutBufferSz;

				fromErr = U_ZERO_ERROR;

				// Write converted bytes (Unicode) to destination codepage
				ucnv_fromUnicode(m_to, &cpTarget, cpTargetLimit,
				                 &uSource, uSourceLimit, NULL, flush, &fromErr);

				if (fromErr != U_BUFFER_OVERFLOW_ERROR && U_FAILURE(fromErr))
					throw exceptions::charset_conv_error("[ICU] Error converting from Unicode to " + m_dest.getName());

				// Write to destination stream
				out.write(cpOutBuffer, (cpTarget - cpOutBuffer));

			} while (fromErr == U_BUFFER_OVERFLOW_ERROR);

		} while (toErr == U_BUFFER_OVERFLOW_ERROR);
	}
}


void charsetConverter_icu::convert(const string& in, string& out)
{
	if (m_source == m_dest)
	{
		// No conversion needed
		out = in;
		return;
	}

	out.clear();

	utility::inputStreamStringAdapter is(in);
	utility::outputStreamStringAdapter os(out);

	convert(is, os);

	os.flush();
}


ref <utility::charsetFilteredOutputStream> charsetConverter_icu::getFilteredOutputStream(utility::outputStream& os)
{
	// TODO: implement charsetFilteredOutputStream for ICU
	return NULL;
}


} // vmime


#endif // VMIME_CHARSETCONV_LIB_IS_ICU