Converter.cxx 3.97 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2019 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * http://www.musicpd.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 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.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "Converter.hxx"
21
#include "util/AllocatedString.hxx"
22
#include "util/AllocatedArray.hxx"
23
#include "util/FormatString.hxx"
24
#include "config.h"
25

26
#include <iterator>
27
#include <stdexcept>
28 29 30

#include <string.h>

31 32 33
#ifdef HAVE_ICU
#include "Util.hxx"
#include <unicode/ucnv.h>
34
#elif defined(HAVE_ICONV)
35
#include "system/Error.hxx"
36 37
#endif

38 39 40 41 42 43 44 45 46
#ifdef HAVE_ICU

IcuConverter::~IcuConverter()
{
	ucnv_close(converter);
}

#endif

47 48 49
#ifdef HAVE_ICU_CONVERTER

IcuConverter *
50
IcuConverter::Create(const char *charset)
51
{
52 53 54
#ifdef HAVE_ICU
	UErrorCode code = U_ZERO_ERROR;
	UConverter *converter = ucnv_open(charset, &code);
55 56 57
	if (converter == nullptr)
		throw std::runtime_error(FormatString("Failed to initialize charset '%s': %s",
						      charset, u_errorName(code)).c_str());
58 59

	return new IcuConverter(converter);
60 61 62 63
#elif defined(HAVE_ICONV)
	iconv_t to = iconv_open("utf-8", charset);
	iconv_t from = iconv_open(charset, "utf-8");
	if (to == (iconv_t)-1 || from == (iconv_t)-1) {
64
		int e = errno;
65 66 67 68
		if (to != (iconv_t)-1)
			iconv_close(to);
		if (from != (iconv_t)-1)
			iconv_close(from);
69 70
		throw FormatErrno(e, "Failed to initialize charset '%s'",
				  charset);
71 72
	}

73
	return new IcuConverter(to, from);
74
#endif
75 76
}

77
#ifdef HAVE_ICU
78 79 80 81 82 83 84 85 86 87 88 89 90 91
#elif defined(HAVE_ICONV)

static AllocatedString<char>
DoConvert(iconv_t conv, const char *src)
{
	// TODO: dynamic buffer?
	char buffer[4096];
	char *in = const_cast<char *>(src);
	char *out = buffer;
	size_t in_left = strlen(src);
	size_t out_left = sizeof(buffer);

	size_t n = iconv(conv, &in, &in_left, &out, &out_left);

92 93 94 95 96
	if (n == static_cast<size_t>(-1))
		throw MakeErrno("Charset conversion failed");

	if (in_left > 0)
		throw std::runtime_error("Charset conversion failed");
97 98 99 100

	return AllocatedString<>::Duplicate(buffer, sizeof(buffer) - out_left);
}

101 102
#endif

103
AllocatedString<char>
104
IcuConverter::ToUTF8(const char *s) const
105
{
106
#ifdef HAVE_ICU
107
	const std::lock_guard<Mutex> protect(mutex);
108 109 110 111 112 113 114 115 116

	ucnv_resetToUnicode(converter);

	// TODO: dynamic buffer?
	UChar buffer[4096], *target = buffer;
	const char *source = s;

	UErrorCode code = U_ZERO_ERROR;

117
	ucnv_toUnicode(converter, &target, buffer + std::size(buffer),
118 119 120
		       &source, source + strlen(source),
		       nullptr, true, &code);
	if (code != U_ZERO_ERROR)
121 122
		throw std::runtime_error(FormatString("Failed to convert to Unicode: %s",
						      u_errorName(code)).c_str());
123 124

	const size_t target_length = target - buffer;
125
	return UCharToUTF8({buffer, target_length});
126
#elif defined(HAVE_ICONV)
127
	return DoConvert(to_utf8, s);
128
#endif
129 130
}

131
AllocatedString<char>
132
IcuConverter::FromUTF8(const char *s) const
133
{
134
#ifdef HAVE_ICU
135
	const std::lock_guard<Mutex> protect(mutex);
136 137 138 139 140 141 142

	const auto u = UCharFromUTF8(s);

	ucnv_resetFromUnicode(converter);

	// TODO: dynamic buffer?
	char buffer[4096], *target = buffer;
143
	const UChar *source = u.begin();
144 145
	UErrorCode code = U_ZERO_ERROR;

146
	ucnv_fromUnicode(converter, &target, buffer + std::size(buffer),
147 148 149 150
			 &source, u.end(),
			 nullptr, true, &code);

	if (code != U_ZERO_ERROR)
151 152
		throw std::runtime_error(FormatString("Failed to convert from Unicode: %s",
						      u_errorName(code)).c_str());
153

154
	return AllocatedString<>::Duplicate(buffer, target);
155

156
#elif defined(HAVE_ICONV)
157
	return DoConvert(from_utf8, s);
158
#endif
159 160 161
}

#endif