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"
|
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"
|
|
|
|
|
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
|
|
|
{
|
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
|
|
|
}
|
2015-05-09 15:58:32 +02:00
|
|
|
wide.resize(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
|
|
|
}
|
2015-05-09 15:58:32 +02:00
|
|
|
wide.resize(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);
|
|
|
|
utf8.resize(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);
|
|
|
|
utf8.resize(dst_start - &utf8[0]);
|
2016-09-25 04:16:10 +02:00
|
|
|
return static_cast<utf8_errors::error_code_enum>(ret);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct utf8_error_category : boost::system::error_category
|
|
|
|
{
|
|
|
|
const char* name() const BOOST_SYSTEM_NOEXCEPT override
|
|
|
|
{
|
|
|
|
return "UTF error";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string message(int ev) const BOOST_SYSTEM_NOEXCEPT override
|
|
|
|
{
|
|
|
|
char const* error_messages[] = {
|
|
|
|
"ok",
|
|
|
|
"source exhausted",
|
|
|
|
"target exhausted",
|
|
|
|
"source illegal"
|
|
|
|
};
|
|
|
|
|
|
|
|
TORRENT_ASSERT(ev >= 0);
|
|
|
|
TORRENT_ASSERT(ev < sizeof(error_messages)/sizeof(error_messages[0]));
|
|
|
|
return error_messages[ev];
|
|
|
|
}
|
|
|
|
|
|
|
|
boost::system::error_condition default_error_condition(
|
|
|
|
int ev) const BOOST_SYSTEM_NOEXCEPT override
|
|
|
|
{
|
|
|
|
return boost::system::error_condition(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)
|
|
|
|
{
|
|
|
|
return error_code(e, utf8_category());
|
|
|
|
}
|
|
|
|
} // 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);
|
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
|
|
|
if (ec) throw system_error(ec);
|
|
|
|
#else
|
|
|
|
if (ec) std::terminate();
|
|
|
|
#endif
|
|
|
|
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);
|
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
|
|
|
if (ec) throw system_error(ec);
|
|
|
|
#else
|
|
|
|
if (ec) std::terminate();
|
|
|
|
#endif
|
|
|
|
return ret;
|
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
|
|
|
|
|