2015-04-21 02:23:00 +02:00
|
|
|
/*
|
|
|
|
|
2016-01-18 00:57:46 +01:00
|
|
|
Copyright (c) 2003-2016, Arvid Norberg
|
2015-04-21 02:23:00 +02: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/hex.hpp"
|
|
|
|
|
2017-04-12 19:00:57 +02:00
|
|
|
namespace libtorrent {
|
2015-04-21 02:23:00 +02:00
|
|
|
|
2016-06-04 16:01:43 +02:00
|
|
|
namespace aux {
|
2015-04-21 02:23:00 +02:00
|
|
|
|
2016-06-04 16:01:43 +02:00
|
|
|
int hex_to_int(char in)
|
2015-04-21 02:23:00 +02:00
|
|
|
{
|
|
|
|
if (in >= '0' && in <= '9') return int(in) - '0';
|
|
|
|
if (in >= 'A' && in <= 'F') return int(in) - 'A' + 10;
|
|
|
|
if (in >= 'a' && in <= 'f') return int(in) - 'a' + 10;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-07-29 08:36:15 +02:00
|
|
|
bool is_hex(span<char const> in)
|
2015-04-21 02:23:00 +02:00
|
|
|
{
|
2016-07-29 08:36:15 +02:00
|
|
|
for (char const c : in)
|
2015-04-21 02:23:00 +02:00
|
|
|
{
|
2016-07-29 08:36:15 +02:00
|
|
|
int const t = hex_to_int(c);
|
2015-04-21 02:23:00 +02:00
|
|
|
if (t == -1) return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-07-29 08:36:15 +02:00
|
|
|
bool from_hex(span<char const> in, char* out)
|
2015-04-21 02:23:00 +02:00
|
|
|
{
|
2016-07-29 08:36:15 +02:00
|
|
|
for (auto i = in.begin(), end = in.end(); i != end; ++i, ++out)
|
2015-04-21 02:23:00 +02:00
|
|
|
{
|
2016-07-29 08:36:15 +02:00
|
|
|
int const t1 = aux::hex_to_int(*i);
|
|
|
|
if (t1 == -1) return false;
|
2016-11-25 17:17:25 +01:00
|
|
|
*out = char(t1 << 4);
|
2016-07-29 08:36:15 +02:00
|
|
|
++i;
|
|
|
|
int const t2 = aux::hex_to_int(*i);
|
|
|
|
if (t2 == -1) return false;
|
|
|
|
*out |= t2 & 15;
|
2015-04-21 02:23:00 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-06-04 16:01:43 +02:00
|
|
|
extern char const hex_chars[];
|
2015-04-21 02:23:00 +02:00
|
|
|
|
2016-06-04 16:01:43 +02:00
|
|
|
char const hex_chars[] = "0123456789abcdef";
|
2016-12-22 16:44:36 +01:00
|
|
|
void to_hex(char const* in, size_t const len, char* out)
|
|
|
|
{
|
|
|
|
int idx = 0;
|
|
|
|
for (size_t i=0; i < len; ++i)
|
|
|
|
{
|
|
|
|
out[idx++] = hex_chars[std::uint8_t(in[i]) >> 4];
|
|
|
|
out[idx++] = hex_chars[std::uint8_t(in[i]) & 0xf];
|
|
|
|
}
|
|
|
|
}
|
2015-04-21 02:23:00 +02:00
|
|
|
|
2016-12-22 16:44:36 +01:00
|
|
|
std::string to_hex(span<char const> in)
|
2015-04-21 02:23:00 +02:00
|
|
|
{
|
|
|
|
std::string ret;
|
2016-12-22 16:44:36 +01:00
|
|
|
if (!in.empty())
|
2015-04-21 02:23:00 +02:00
|
|
|
{
|
2016-12-22 16:44:36 +01:00
|
|
|
ret.resize(in.size() * 2);
|
|
|
|
to_hex(in.data(), in.size(), &ret[0]);
|
2015-04-21 02:23:00 +02:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-07-29 08:36:15 +02:00
|
|
|
void to_hex(span<char const> in, char* out)
|
2015-04-21 02:23:00 +02:00
|
|
|
{
|
2016-12-22 16:44:36 +01:00
|
|
|
to_hex(in.data(), in.size(), out);
|
|
|
|
out[in.size() * 2] = '\0';
|
2015-04-21 02:23:00 +02:00
|
|
|
}
|
|
|
|
|
2016-06-04 16:01:43 +02:00
|
|
|
} // aux namespace
|
|
|
|
|
2015-04-21 02:23:00 +02:00
|
|
|
}
|