2014-11-29 23:59:37 +01:00
|
|
|
/*
|
2017-01-03 20:48:59 +01:00
|
|
|
* Copyright 2003-2017 The Music Player Daemon Project
|
2014-11-29 23:59:37 +01:00
|
|
|
* 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 "config.h"
|
|
|
|
#include "Converter.hxx"
|
2014-11-30 00:59:43 +01:00
|
|
|
#include "util/Macros.hxx"
|
2015-06-25 22:43:55 +02:00
|
|
|
#include "util/AllocatedString.hxx"
|
2016-04-12 21:32:02 +02:00
|
|
|
#include "util/AllocatedArray.hxx"
|
2014-11-30 00:59:43 +01:00
|
|
|
#include "util/ConstBuffer.hxx"
|
2016-04-12 22:18:36 +02:00
|
|
|
#include "util/FormatString.hxx"
|
|
|
|
|
|
|
|
#include <stdexcept>
|
2014-11-29 23:59:37 +01:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2014-11-30 00:59:43 +01:00
|
|
|
#ifdef HAVE_ICU
|
|
|
|
#include "Util.hxx"
|
|
|
|
#include <unicode/ucnv.h>
|
2015-06-26 16:43:27 +02:00
|
|
|
#elif defined(HAVE_ICONV)
|
2016-04-12 22:18:36 +02:00
|
|
|
#include "system/Error.hxx"
|
2014-11-29 23:59:37 +01:00
|
|
|
#endif
|
|
|
|
|
2014-11-30 00:59:43 +01:00
|
|
|
#ifdef HAVE_ICU
|
|
|
|
|
|
|
|
IcuConverter::~IcuConverter()
|
|
|
|
{
|
|
|
|
ucnv_close(converter);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2014-11-29 23:59:37 +01:00
|
|
|
#ifdef HAVE_ICU_CONVERTER
|
|
|
|
|
|
|
|
IcuConverter *
|
2016-04-12 22:18:36 +02:00
|
|
|
IcuConverter::Create(const char *charset)
|
2014-11-29 23:59:37 +01:00
|
|
|
{
|
2014-11-30 00:59:43 +01:00
|
|
|
#ifdef HAVE_ICU
|
|
|
|
UErrorCode code = U_ZERO_ERROR;
|
|
|
|
UConverter *converter = ucnv_open(charset, &code);
|
2016-04-12 22:18:36 +02:00
|
|
|
if (converter == nullptr)
|
|
|
|
throw std::runtime_error(FormatString("Failed to initialize charset '%s': %s",
|
|
|
|
charset, u_errorName(code)).c_str());
|
2014-11-30 00:59:43 +01:00
|
|
|
|
|
|
|
return new IcuConverter(converter);
|
2015-06-26 16:43:27 +02:00
|
|
|
#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) {
|
2016-04-12 22:18:36 +02:00
|
|
|
int e = errno;
|
2015-06-26 16:43:27 +02:00
|
|
|
if (to != (iconv_t)-1)
|
|
|
|
iconv_close(to);
|
|
|
|
if (from != (iconv_t)-1)
|
|
|
|
iconv_close(from);
|
2016-04-12 22:18:36 +02:00
|
|
|
throw FormatErrno(e, "Failed to initialize charset '%s'",
|
|
|
|
charset);
|
2015-06-26 16:43:27 +02:00
|
|
|
}
|
|
|
|
|
2014-11-29 23:59:37 +01:00
|
|
|
return new IcuConverter(to, from);
|
2014-11-30 00:59:43 +01:00
|
|
|
#endif
|
2014-11-29 23:59:37 +01:00
|
|
|
}
|
|
|
|
|
2014-11-30 00:59:43 +01:00
|
|
|
#ifdef HAVE_ICU
|
2015-06-26 16:43:27 +02:00
|
|
|
#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);
|
|
|
|
|
2016-04-13 10:39:29 +02:00
|
|
|
if (n == static_cast<size_t>(-1))
|
|
|
|
throw MakeErrno("Charset conversion failed");
|
|
|
|
|
|
|
|
if (in_left > 0)
|
|
|
|
throw std::runtime_error("Charset conversion failed");
|
2015-06-26 16:43:27 +02:00
|
|
|
|
|
|
|
return AllocatedString<>::Duplicate(buffer, sizeof(buffer) - out_left);
|
|
|
|
}
|
|
|
|
|
2014-11-30 00:59:43 +01:00
|
|
|
#endif
|
|
|
|
|
2015-06-25 22:43:55 +02:00
|
|
|
AllocatedString<char>
|
2014-11-29 23:59:37 +01:00
|
|
|
IcuConverter::ToUTF8(const char *s) const
|
2016-04-13 10:39:29 +02:00
|
|
|
{
|
2014-11-30 00:59:43 +01:00
|
|
|
#ifdef HAVE_ICU
|
2017-01-03 07:11:57 +01:00
|
|
|
const std::lock_guard<Mutex> protect(mutex);
|
2014-11-30 00:59:43 +01:00
|
|
|
|
|
|
|
ucnv_resetToUnicode(converter);
|
|
|
|
|
|
|
|
// TODO: dynamic buffer?
|
|
|
|
UChar buffer[4096], *target = buffer;
|
|
|
|
const char *source = s;
|
|
|
|
|
|
|
|
UErrorCode code = U_ZERO_ERROR;
|
|
|
|
|
|
|
|
ucnv_toUnicode(converter, &target, buffer + ARRAY_SIZE(buffer),
|
|
|
|
&source, source + strlen(source),
|
|
|
|
nullptr, true, &code);
|
|
|
|
if (code != U_ZERO_ERROR)
|
2016-04-13 10:39:29 +02:00
|
|
|
throw std::runtime_error(FormatString("Failed to convert to Unicode: %s",
|
|
|
|
u_errorName(code)).c_str());
|
2014-11-30 00:59:43 +01:00
|
|
|
|
|
|
|
const size_t target_length = target - buffer;
|
2015-06-25 22:43:55 +02:00
|
|
|
return UCharToUTF8({buffer, target_length});
|
2015-06-26 16:51:18 +02:00
|
|
|
#elif defined(HAVE_ICONV)
|
2014-11-29 23:59:37 +01:00
|
|
|
return DoConvert(to_utf8, s);
|
2014-11-30 00:59:43 +01:00
|
|
|
#endif
|
2014-11-29 23:59:37 +01:00
|
|
|
}
|
|
|
|
|
2015-06-25 22:43:55 +02:00
|
|
|
AllocatedString<char>
|
2014-11-29 23:59:37 +01:00
|
|
|
IcuConverter::FromUTF8(const char *s) const
|
2016-04-13 10:39:29 +02:00
|
|
|
{
|
2014-11-30 00:59:43 +01:00
|
|
|
#ifdef HAVE_ICU
|
2017-01-03 07:11:57 +01:00
|
|
|
const std::lock_guard<Mutex> protect(mutex);
|
2014-11-30 00:59:43 +01:00
|
|
|
|
|
|
|
const auto u = UCharFromUTF8(s);
|
|
|
|
|
|
|
|
ucnv_resetFromUnicode(converter);
|
|
|
|
|
|
|
|
// TODO: dynamic buffer?
|
|
|
|
char buffer[4096], *target = buffer;
|
2016-04-12 21:32:02 +02:00
|
|
|
const UChar *source = u.begin();
|
2014-11-30 00:59:43 +01:00
|
|
|
UErrorCode code = U_ZERO_ERROR;
|
|
|
|
|
|
|
|
ucnv_fromUnicode(converter, &target, buffer + ARRAY_SIZE(buffer),
|
|
|
|
&source, u.end(),
|
|
|
|
nullptr, true, &code);
|
|
|
|
|
|
|
|
if (code != U_ZERO_ERROR)
|
2016-04-13 10:39:29 +02:00
|
|
|
throw std::runtime_error(FormatString("Failed to convert from Unicode: %s",
|
|
|
|
u_errorName(code)).c_str());
|
2014-11-30 00:59:43 +01:00
|
|
|
|
2015-06-25 22:43:55 +02:00
|
|
|
return AllocatedString<>::Duplicate(buffer, target);
|
2014-11-30 00:59:43 +01:00
|
|
|
|
2015-06-26 16:51:18 +02:00
|
|
|
#elif defined(HAVE_ICONV)
|
2014-11-29 23:59:37 +01:00
|
|
|
return DoConvert(from_utf8, s);
|
2014-11-30 00:59:43 +01:00
|
|
|
#endif
|
2014-11-29 23:59:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|