2012-03-20 04:53:07 +01:00
|
|
|
/*
|
|
|
|
|
2016-01-18 00:57:46 +01:00
|
|
|
Copyright (c) 2012-2016, Arvid Norberg
|
2012-03-20 04:53:07 +01:00
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in
|
|
|
|
the documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the author nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libtorrent/config.hpp"
|
|
|
|
|
|
|
|
// on windows we need these functions for
|
|
|
|
// convert_to_native and convert_from_native
|
|
|
|
#if TORRENT_USE_WSTRING || defined TORRENT_WINDOWS
|
|
|
|
|
2015-07-31 02:23:35 +02:00
|
|
|
#include <iterator>
|
|
|
|
#include "libtorrent/utf8.hpp"
|
|
|
|
#include "libtorrent/ConvertUTF.h"
|
|
|
|
|
2015-09-02 07:30:40 +02:00
|
|
|
|
|
|
|
#ifdef __clang__
|
|
|
|
#pragma clang diagnostic push
|
|
|
|
#pragma clang diagnostic ignored "-Wunused-member-function"
|
|
|
|
#endif
|
|
|
|
|
2012-03-20 04:53:07 +01:00
|
|
|
namespace libtorrent
|
|
|
|
{
|
2015-05-09 15:58:32 +02:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
// ==== utf-8 -> wide ===
|
|
|
|
template<int width>
|
2015-08-16 18:17:23 +02:00
|
|
|
struct convert_to_wide
|
2015-05-09 15:58:32 +02:00
|
|
|
{
|
|
|
|
static utf8_conv_result_t convert(UTF8 const** src_start
|
|
|
|
, UTF8 const* src_end
|
|
|
|
, std::wstring& wide)
|
|
|
|
{
|
2015-05-16 20:35:47 +02:00
|
|
|
TORRENT_UNUSED(src_start);
|
|
|
|
TORRENT_UNUSED(src_end);
|
|
|
|
TORRENT_UNUSED(wide);
|
2015-05-09 15:58:32 +02:00
|
|
|
return source_illegal;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// ==== utf-8 -> utf-32 ===
|
|
|
|
template<>
|
|
|
|
struct convert_to_wide<4>
|
|
|
|
{
|
|
|
|
static utf8_conv_result_t convert(char const** src_start
|
|
|
|
, char const* src_end
|
|
|
|
, std::wstring& wide)
|
|
|
|
{
|
2015-05-16 20:35:47 +02:00
|
|
|
wchar_t* dst_start = &wide[0];
|
|
|
|
int ret = ConvertUTF8toUTF32(
|
|
|
|
reinterpret_cast<UTF8 const**>(src_start)
|
|
|
|
, reinterpret_cast<UTF8 const*>(src_end)
|
|
|
|
, reinterpret_cast<UTF32**>(&dst_start)
|
|
|
|
, reinterpret_cast<UTF32*>(dst_start + wide.size())
|
2015-05-09 15:58:32 +02:00
|
|
|
, lenientConversion);
|
2015-07-27 17:59:38 +02:00
|
|
|
if (ret == sourceIllegal)
|
|
|
|
{
|
|
|
|
// assume Latin-1
|
|
|
|
wide.clear();
|
2015-08-16 18:17:23 +02:00
|
|
|
std::copy(reinterpret_cast<boost::uint8_t const*>(*src_start)
|
|
|
|
,reinterpret_cast<boost::uint8_t const*>(src_end)
|
2015-07-27 17:59:38 +02:00
|
|
|
, std::back_inserter(wide));
|
|
|
|
return static_cast<utf8_conv_result_t>(ret);
|
|
|
|
}
|
2015-05-09 15:58:32 +02:00
|
|
|
wide.resize(dst_start - wide.c_str());
|
2015-05-16 20:35:47 +02:00
|
|
|
return static_cast<utf8_conv_result_t>(ret);
|
2015-05-09 15:58:32 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// ==== utf-8 -> utf-16 ===
|
|
|
|
template<>
|
|
|
|
struct convert_to_wide<2>
|
|
|
|
{
|
|
|
|
static utf8_conv_result_t convert(char const** src_start
|
|
|
|
, char const* src_end
|
|
|
|
, std::wstring& wide)
|
|
|
|
{
|
2015-05-16 20:35:47 +02:00
|
|
|
wchar_t* dst_start = &wide[0];
|
|
|
|
int ret = ConvertUTF8toUTF16(
|
|
|
|
reinterpret_cast<UTF8 const**>(src_start)
|
|
|
|
, reinterpret_cast<UTF8 const*>(src_end)
|
|
|
|
, reinterpret_cast<UTF16**>(&dst_start)
|
|
|
|
, reinterpret_cast<UTF16*>(dst_start + wide.size())
|
2015-05-09 15:58:32 +02:00
|
|
|
, lenientConversion);
|
2015-07-27 17:59:38 +02:00
|
|
|
if (ret == sourceIllegal)
|
|
|
|
{
|
|
|
|
// assume Latin-1
|
|
|
|
wide.clear();
|
2015-08-16 18:17:23 +02:00
|
|
|
std::copy(reinterpret_cast<boost::uint8_t const*>(*src_start)
|
|
|
|
, reinterpret_cast<boost::uint8_t const*>(src_end)
|
2015-07-27 17:59:38 +02:00
|
|
|
, std::back_inserter(wide));
|
|
|
|
return static_cast<utf8_conv_result_t>(ret);
|
|
|
|
}
|
2015-05-09 15:58:32 +02:00
|
|
|
wide.resize(dst_start - wide.c_str());
|
2015-05-16 20:35:47 +02:00
|
|
|
return static_cast<utf8_conv_result_t>(ret);
|
2015-05-09 15:58:32 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// ==== wide -> utf-8 ===
|
|
|
|
template<int width>
|
|
|
|
struct convert_from_wide
|
|
|
|
{
|
|
|
|
static utf8_conv_result_t convert(wchar_t const** src_start
|
|
|
|
, wchar_t const* src_end
|
|
|
|
, std::string& utf8)
|
|
|
|
{
|
2015-05-16 20:35:47 +02:00
|
|
|
TORRENT_UNUSED(src_start);
|
|
|
|
TORRENT_UNUSED(src_end);
|
|
|
|
TORRENT_UNUSED(utf8);
|
2015-05-09 15:58:32 +02:00
|
|
|
return source_illegal;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// ==== utf-32 -> utf-8 ===
|
|
|
|
template<>
|
|
|
|
struct convert_from_wide<4>
|
|
|
|
{
|
|
|
|
static utf8_conv_result_t convert(wchar_t const** src_start
|
|
|
|
, wchar_t const* src_end
|
|
|
|
, std::string& utf8)
|
|
|
|
{
|
2015-05-16 20:35:47 +02:00
|
|
|
char* dst_start = &utf8[0];
|
|
|
|
int ret = ConvertUTF32toUTF8(
|
|
|
|
reinterpret_cast<UTF32 const**>(src_start)
|
|
|
|
, reinterpret_cast<UTF32 const*>(src_end)
|
|
|
|
, reinterpret_cast<UTF8**>(&dst_start)
|
|
|
|
, reinterpret_cast<UTF8*>(dst_start + utf8.size())
|
2015-05-09 15:58:32 +02:00
|
|
|
, lenientConversion);
|
|
|
|
utf8.resize(dst_start - &utf8[0]);
|
2015-05-16 20:35:47 +02:00
|
|
|
return static_cast<utf8_conv_result_t>(ret);
|
2015-05-09 15:58:32 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// ==== utf-16 -> utf-8 ===
|
|
|
|
template<>
|
|
|
|
struct convert_from_wide<2>
|
|
|
|
{
|
|
|
|
static utf8_conv_result_t convert(wchar_t const** src_start
|
|
|
|
, wchar_t const* src_end
|
|
|
|
, std::string& utf8)
|
|
|
|
{
|
2015-05-16 20:35:47 +02:00
|
|
|
char* dst_start = &utf8[0];
|
|
|
|
int ret = ConvertUTF16toUTF8(
|
|
|
|
reinterpret_cast<UTF16 const**>(src_start)
|
|
|
|
, reinterpret_cast<UTF16 const*>(src_end)
|
|
|
|
, reinterpret_cast<UTF8**>(&dst_start)
|
|
|
|
, reinterpret_cast<UTF8*>(dst_start + utf8.size())
|
2015-05-09 15:58:32 +02:00
|
|
|
, lenientConversion);
|
|
|
|
utf8.resize(dst_start - &utf8[0]);
|
2015-05-16 20:35:47 +02:00
|
|
|
return static_cast<utf8_conv_result_t>(ret);
|
2015-05-09 15:58:32 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
utf8_conv_result_t utf8_wchar(std::string const& utf8, std::wstring &wide)
|
2012-03-20 04:53:07 +01:00
|
|
|
{
|
|
|
|
// allocate space for worst-case
|
|
|
|
wide.resize(utf8.size());
|
|
|
|
char const* src_start = utf8.c_str();
|
2015-05-09 15:58:32 +02:00
|
|
|
return convert_to_wide<sizeof(wchar_t)>::convert(
|
|
|
|
&src_start, src_start + utf8.size(), wide);
|
2012-03-20 04:53:07 +01:00
|
|
|
}
|
|
|
|
|
2015-05-09 15:58:32 +02:00
|
|
|
utf8_conv_result_t wchar_utf8(std::wstring const& wide, std::string &utf8)
|
2012-03-20 04:53:07 +01:00
|
|
|
{
|
|
|
|
// allocate space for worst-case
|
|
|
|
utf8.resize(wide.size() * 6);
|
2013-11-28 09:41:35 +01:00
|
|
|
if (wide.empty()) return conversion_ok;
|
2012-03-20 04:53:07 +01:00
|
|
|
wchar_t const* src_start = wide.c_str();
|
2015-05-09 15:58:32 +02:00
|
|
|
return convert_from_wide<sizeof(wchar_t)>::convert(
|
|
|
|
&src_start, src_start + wide.size(), utf8);
|
2012-03-20 04:53:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-02 07:30:40 +02:00
|
|
|
#ifdef __clang__
|
|
|
|
#pragma clang diagnostic pop
|
|
|
|
#endif
|
|
|
|
|
2012-03-22 16:51:56 +01:00
|
|
|
#endif
|
|
|
|
|