2012-03-20 04:53:07 +01:00
|
|
|
/*
|
|
|
|
|
2018-04-09 09:04:33 +02:00
|
|
|
Copyright (c) 2012-2018, 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"
|
|
|
|
|
2015-07-31 02:23:35 +02:00
|
|
|
#include <iterator>
|
|
|
|
#include "libtorrent/utf8.hpp"
|
2016-09-25 04:16:10 +02:00
|
|
|
#include "libtorrent/assert.hpp"
|
|
|
|
#include "libtorrent/error_code.hpp"
|
2015-07-31 02:23:35 +02:00
|
|
|
#include "libtorrent/ConvertUTF.h"
|
2017-01-29 21:37:42 +01:00
|
|
|
#include "libtorrent/aux_/throw.hpp"
|
2017-02-07 06:22:30 +01:00
|
|
|
#include "libtorrent/aux_/numeric_cast.hpp"
|
2015-07-31 02:23:35 +02:00
|
|
|
|
2015-09-02 07:30:40 +02:00
|
|
|
|
|
|
|
#ifdef __clang__
|
|
|
|
#pragma clang diagnostic push
|
|
|
|
#pragma clang diagnostic ignored "-Wunused-member-function"
|
|
|
|
#endif
|
|
|
|
|
2017-04-12 19:00:57 +02:00
|
|
|
namespace libtorrent {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2015-05-09 15:58:32 +02:00
|
|
|
// ==== 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
|
|
|
{
|
2016-09-25 04:16:10 +02:00
|
|
|
static utf8_errors::error_code_enum convert(UTF8 const** src_start
|
2015-05-09 15:58:32 +02:00
|
|
|
, 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);
|
2016-09-25 04:16:10 +02:00
|
|
|
return utf8_errors::error_code_enum::source_illegal;
|
2015-05-09 15:58:32 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// ==== utf-8 -> utf-32 ===
|
|
|
|
template<>
|
|
|
|
struct convert_to_wide<4>
|
|
|
|
{
|
2016-09-25 04:16:10 +02:00
|
|
|
static utf8_errors::error_code_enum convert(char const** src_start
|
2015-05-09 15:58:32 +02:00
|
|
|
, 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();
|
2016-06-18 20:01:38 +02:00
|
|
|
std::copy(reinterpret_cast<std::uint8_t const*>(*src_start)
|
|
|
|
,reinterpret_cast<std::uint8_t const*>(src_end)
|
2015-07-27 17:59:38 +02:00
|
|
|
, std::back_inserter(wide));
|
2016-09-25 04:16:10 +02:00
|
|
|
return static_cast<utf8_errors::error_code_enum>(ret);
|
2015-07-27 17:59:38 +02:00
|
|
|
}
|
2017-02-07 06:22:30 +01:00
|
|
|
wide.resize(aux::numeric_cast<std::size_t>(dst_start - wide.c_str()));
|
2016-09-25 04:16:10 +02:00
|
|
|
return static_cast<utf8_errors::error_code_enum>(ret);
|
2015-05-09 15:58:32 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// ==== utf-8 -> utf-16 ===
|
|
|
|
template<>
|
|
|
|
struct convert_to_wide<2>
|
|
|
|
{
|
2016-09-25 04:16:10 +02:00
|
|
|
static utf8_errors::error_code_enum convert(char const** src_start
|
2015-05-09 15:58:32 +02:00
|
|
|
, 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();
|
2016-06-18 20:01:38 +02:00
|
|
|
std::copy(reinterpret_cast<std::uint8_t const*>(*src_start)
|
|
|
|
, reinterpret_cast<std::uint8_t const*>(src_end)
|
2015-07-27 17:59:38 +02:00
|
|
|
, std::back_inserter(wide));
|
2016-09-25 04:16:10 +02:00
|
|
|
return static_cast<utf8_errors::error_code_enum>(ret);
|
2015-07-27 17:59:38 +02:00
|
|
|
}
|
2017-02-07 06:22:30 +01:00
|
|
|
wide.resize(aux::numeric_cast<std::size_t>(dst_start - wide.c_str()));
|
2016-09-25 04:16:10 +02:00
|
|
|
return static_cast<utf8_errors::error_code_enum>(ret);
|
2015-05-09 15:58:32 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// ==== wide -> utf-8 ===
|
|
|
|
template<int width>
|
|
|
|
struct convert_from_wide
|
|
|
|
{
|
2016-09-25 04:16:10 +02:00
|
|
|
static utf8_errors::error_code_enum convert(wchar_t const** src_start
|
2015-05-09 15:58:32 +02:00
|
|
|
, 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);
|
2016-09-25 04:16:10 +02:00
|
|
|
return utf8_errors::error_code_enum::source_illegal;
|
2015-05-09 15:58:32 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// ==== utf-32 -> utf-8 ===
|
|
|
|
template<>
|
|
|
|
struct convert_from_wide<4>
|
|
|
|
{
|
2016-09-25 04:16:10 +02:00
|
|
|
static utf8_errors::error_code_enum convert(wchar_t const** src_start
|
2015-05-09 15:58:32 +02:00
|
|
|
, 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);
|
2017-02-07 06:22:30 +01:00
|
|
|
utf8.resize(aux::numeric_cast<std::size_t>(dst_start - &utf8[0]));
|
2016-09-25 04:16:10 +02:00
|
|
|
return static_cast<utf8_errors::error_code_enum>(ret);
|
2015-05-09 15:58:32 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// ==== utf-16 -> utf-8 ===
|
|
|
|
template<>
|
|
|
|
struct convert_from_wide<2>
|
|
|
|
{
|
2016-09-25 04:16:10 +02:00
|
|
|
static utf8_errors::error_code_enum convert(wchar_t const** src_start
|
2015-05-09 15:58:32 +02:00
|
|
|
, 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);
|
2017-02-07 06:22:30 +01:00
|
|
|
utf8.resize(aux::numeric_cast<std::size_t>(dst_start - &utf8[0]));
|
2016-09-25 04:16:10 +02:00
|
|
|
return static_cast<utf8_errors::error_code_enum>(ret);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-01-19 22:47:48 +01:00
|
|
|
struct utf8_error_category final : boost::system::error_category
|
2016-09-25 04:16:10 +02:00
|
|
|
{
|
|
|
|
const char* name() const BOOST_SYSTEM_NOEXCEPT override
|
|
|
|
{
|
|
|
|
return "UTF error";
|
|
|
|
}
|
|
|
|
|
2017-11-10 00:46:03 +01:00
|
|
|
std::string message(int ev) const override
|
2016-09-25 04:16:10 +02:00
|
|
|
{
|
2017-01-22 16:33:43 +01:00
|
|
|
static char const* error_messages[] = {
|
2016-09-25 04:16:10 +02:00
|
|
|
"ok",
|
|
|
|
"source exhausted",
|
|
|
|
"target exhausted",
|
|
|
|
"source illegal"
|
|
|
|
};
|
|
|
|
|
|
|
|
TORRENT_ASSERT(ev >= 0);
|
2016-11-21 07:49:56 +01:00
|
|
|
TORRENT_ASSERT(ev < int(sizeof(error_messages)/sizeof(error_messages[0])));
|
2016-09-25 04:16:10 +02:00
|
|
|
return error_messages[ev];
|
|
|
|
}
|
|
|
|
|
|
|
|
boost::system::error_condition default_error_condition(
|
|
|
|
int ev) const BOOST_SYSTEM_NOEXCEPT override
|
2018-01-11 01:35:15 +01:00
|
|
|
{ return {ev, *this}; }
|
2015-05-09 15:58:32 +02:00
|
|
|
};
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2016-09-25 04:16:10 +02:00
|
|
|
namespace utf8_errors
|
|
|
|
{
|
|
|
|
boost::system::error_code make_error_code(utf8_errors::error_code_enum e)
|
|
|
|
{
|
2018-01-11 01:35:15 +01:00
|
|
|
return {e, utf8_category()};
|
2016-09-25 04:16:10 +02:00
|
|
|
}
|
|
|
|
} // utf_errors namespace
|
|
|
|
|
|
|
|
boost::system::error_category const& utf8_category()
|
|
|
|
{
|
|
|
|
static utf8_error_category cat;
|
|
|
|
return cat;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::wstring utf8_wchar(string_view utf8, error_code& ec)
|
2012-03-20 04:53:07 +01:00
|
|
|
{
|
|
|
|
// allocate space for worst-case
|
2016-09-25 04:16:10 +02:00
|
|
|
std::wstring wide;
|
2012-03-20 04:53:07 +01:00
|
|
|
wide.resize(utf8.size());
|
2016-09-25 04:16:10 +02:00
|
|
|
char const* src_start = utf8.data();
|
|
|
|
utf8_errors::error_code_enum const ret = convert_to_wide<sizeof(wchar_t)>::convert(
|
2015-05-09 15:58:32 +02:00
|
|
|
&src_start, src_start + utf8.size(), wide);
|
2016-09-25 04:16:10 +02:00
|
|
|
if (ret != utf8_errors::error_code_enum::conversion_ok)
|
|
|
|
ec = make_error_code(ret);
|
|
|
|
return wide;
|
2012-03-20 04:53:07 +01:00
|
|
|
}
|
|
|
|
|
2016-09-25 04:16:10 +02:00
|
|
|
std::wstring utf8_wchar(string_view wide)
|
|
|
|
{
|
|
|
|
error_code ec;
|
|
|
|
std::wstring ret = utf8_wchar(wide, ec);
|
2017-01-29 21:37:42 +01:00
|
|
|
if (ec) aux::throw_ex<system_error>(ec);
|
2016-09-25 04:16:10 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string wchar_utf8(wstring_view wide, error_code& ec)
|
2012-03-20 04:53:07 +01:00
|
|
|
{
|
|
|
|
// allocate space for worst-case
|
2016-09-25 04:16:10 +02:00
|
|
|
std::string utf8;
|
2012-03-20 04:53:07 +01:00
|
|
|
utf8.resize(wide.size() * 6);
|
2016-09-25 04:16:10 +02:00
|
|
|
if (wide.empty()) return {};
|
|
|
|
|
|
|
|
wchar_t const* src_start = wide.data();
|
|
|
|
utf8_errors::error_code_enum const ret = convert_from_wide<sizeof(wchar_t)>::convert(
|
2015-05-09 15:58:32 +02:00
|
|
|
&src_start, src_start + wide.size(), utf8);
|
2016-09-25 04:16:10 +02:00
|
|
|
if (ret != utf8_errors::error_code_enum::conversion_ok)
|
|
|
|
ec = make_error_code(ret);
|
|
|
|
return utf8;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string wchar_utf8(wstring_view wide)
|
|
|
|
{
|
|
|
|
error_code ec;
|
|
|
|
std::string ret = wchar_utf8(wide, ec);
|
2017-01-29 21:37:42 +01:00
|
|
|
if (ec) aux::throw_ex<system_error>(ec);
|
2016-09-25 04:16:10 +02:00
|
|
|
return ret;
|
2012-03-20 04:53:07 +01:00
|
|
|
}
|
2017-05-15 02:58:51 +02:00
|
|
|
|
|
|
|
// returns the unicode codepoint and the number of bytes of the utf8 sequence
|
|
|
|
// that was parsed. The codepoint is -1 if it's invalid
|
2017-09-02 00:27:47 +02:00
|
|
|
std::pair<std::int32_t, int> parse_utf8_codepoint(string_view str)
|
2017-05-15 02:58:51 +02:00
|
|
|
{
|
2017-09-02 00:27:47 +02:00
|
|
|
int const sequence_len = trailingBytesForUTF8[static_cast<std::uint8_t>(str[0])] + 1;
|
|
|
|
if (sequence_len > int(str.size())) return std::make_pair(-1, static_cast<int>(str.size()));
|
2017-05-15 02:58:51 +02:00
|
|
|
|
|
|
|
if (sequence_len > 4)
|
|
|
|
{
|
|
|
|
return std::make_pair(-1, sequence_len);
|
|
|
|
}
|
|
|
|
|
2017-09-02 00:27:47 +02:00
|
|
|
if (!isLegalUTF8(reinterpret_cast<UTF8 const*>(str.data()), sequence_len))
|
2017-05-15 02:58:51 +02:00
|
|
|
{
|
|
|
|
return std::make_pair(-1, sequence_len);
|
|
|
|
}
|
|
|
|
|
2017-05-15 15:08:57 +02:00
|
|
|
std::uint32_t ch = 0;
|
2017-05-15 02:58:51 +02:00
|
|
|
for (int i = 0; i < sequence_len; ++i)
|
|
|
|
{
|
|
|
|
ch <<= 6;
|
2017-09-02 00:27:47 +02:00
|
|
|
ch += static_cast<std::uint8_t>(str[static_cast<std::size_t>(i)]);
|
2017-05-15 02:58:51 +02:00
|
|
|
}
|
|
|
|
ch -= offsetsFromUTF8[sequence_len-1];
|
|
|
|
|
|
|
|
if (ch > 0x7fffffff)
|
|
|
|
{
|
|
|
|
return std::make_pair(-1, sequence_len);
|
|
|
|
}
|
|
|
|
|
2017-05-15 15:08:57 +02:00
|
|
|
return std::make_pair(static_cast<std::int32_t>(ch), sequence_len);
|
2017-05-15 02:58:51 +02:00
|
|
|
}
|
2012-03-20 04:53:07 +01:00
|
|
|
}
|
|
|
|
|
2015-09-02 07:30:40 +02:00
|
|
|
#ifdef __clang__
|
|
|
|
#pragma clang diagnostic pop
|
|
|
|
#endif
|
|
|
|
|