2014-07-21 05:22:06 +02:00
|
|
|
// OpenVPN -- An application to securely tunnel IP networks
|
|
|
|
// over a single port, with support for SSL/TLS-based
|
|
|
|
// session authentication and key exchange,
|
|
|
|
// packet encryption, packet authentication, and
|
|
|
|
// packet compression.
|
2012-08-24 23:13:42 +02:00
|
|
|
//
|
2017-12-21 22:15:36 +01:00
|
|
|
// Copyright (C) 2012-2017 OpenVPN Inc.
|
2012-08-24 23:13:42 +02:00
|
|
|
//
|
2014-07-21 05:22:06 +02:00
|
|
|
// This program is free software: you can redistribute it and/or modify
|
2017-12-21 21:42:20 +01:00
|
|
|
// it under the terms of the GNU Affero General Public License Version 3
|
2014-07-21 05:22:06 +02:00
|
|
|
// as published by the Free Software Foundation.
|
2012-08-24 23:13:42 +02:00
|
|
|
//
|
2014-07-21 05:22:06 +02:00
|
|
|
// 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
|
2017-12-21 21:42:20 +01:00
|
|
|
// GNU Affero General Public License for more details.
|
2014-07-21 05:22:06 +02:00
|
|
|
//
|
2017-12-21 21:42:20 +01:00
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
2014-07-21 05:22:06 +02:00
|
|
|
// along with this program in the COPYING file.
|
|
|
|
// If not, see <http://www.gnu.org/licenses/>.
|
2012-08-24 23:13:42 +02:00
|
|
|
|
2012-11-23 07:18:43 +01:00
|
|
|
// A collection of functions for rendering and parsing hexadecimal strings
|
|
|
|
|
2011-09-30 00:42:37 +02:00
|
|
|
#ifndef OPENVPN_COMMON_HEXSTR_H
|
|
|
|
#define OPENVPN_COMMON_HEXSTR_H
|
|
|
|
|
|
|
|
#include <string>
|
2014-10-26 05:25:11 +01:00
|
|
|
#include <iomanip>
|
|
|
|
#include <sstream>
|
2011-09-30 17:25:38 +02:00
|
|
|
|
|
|
|
#include <openvpn/common/exception.hpp>
|
2015-10-30 23:39:38 +01:00
|
|
|
#include <openvpn/common/string.hpp>
|
2011-09-30 00:42:37 +02:00
|
|
|
|
|
|
|
namespace openvpn {
|
|
|
|
|
2015-04-21 08:32:03 +02:00
|
|
|
inline char render_hex_char(const int c, const bool caps=false)
|
2011-09-30 00:42:37 +02:00
|
|
|
{
|
|
|
|
if (c < 10)
|
|
|
|
return '0' + c;
|
|
|
|
else if (c < 16)
|
2015-04-21 08:32:03 +02:00
|
|
|
return (caps ? 'A' : 'a') - 10 + c;
|
2011-09-30 00:42:37 +02:00
|
|
|
else
|
|
|
|
return '?';
|
|
|
|
}
|
|
|
|
|
2015-04-21 08:32:03 +02:00
|
|
|
inline int parse_hex_char(const char c)
|
2011-09-30 17:25:38 +02:00
|
|
|
{
|
|
|
|
if (c >= '0' && c <= '9')
|
|
|
|
return c - '0';
|
|
|
|
else if (c >= 'a' && c <= 'f')
|
|
|
|
return c - 'a' + 10;
|
|
|
|
else if (c >= 'A' && c <= 'F')
|
|
|
|
return c - 'A' + 10;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-12-14 18:15:15 +01:00
|
|
|
class RenderHexByte
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
RenderHexByte(const unsigned char byte, const bool caps=false)
|
|
|
|
{
|
|
|
|
c[0] = render_hex_char(byte >> 4, caps);
|
|
|
|
c[1] = render_hex_char(byte & 0x0F, caps);
|
|
|
|
}
|
|
|
|
|
|
|
|
char char1() const { return c[0]; }
|
|
|
|
char char2() const { return c[1]; }
|
|
|
|
|
|
|
|
const char *str2() const { return c; } // Note: length=2, NOT null terminated
|
|
|
|
|
|
|
|
private:
|
|
|
|
char c[2];
|
|
|
|
};
|
|
|
|
|
2015-04-21 08:32:03 +02:00
|
|
|
inline std::string render_hex(const unsigned char *data, size_t size, const bool caps=false)
|
2011-09-30 00:42:37 +02:00
|
|
|
{
|
2016-07-13 07:00:35 +02:00
|
|
|
if (!data)
|
|
|
|
return "NULL";
|
2011-09-30 00:42:37 +02:00
|
|
|
std::string ret;
|
|
|
|
ret.reserve(size*2+1);
|
|
|
|
while (size--)
|
|
|
|
{
|
2016-12-14 18:15:15 +01:00
|
|
|
const RenderHexByte b(*data++, caps);
|
|
|
|
ret += b.char1();
|
|
|
|
ret += b.char2();
|
2011-09-30 17:25:38 +02:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-08-19 08:02:52 +02:00
|
|
|
inline std::string render_hex(const void *data, const size_t size, const bool caps=false)
|
|
|
|
{
|
|
|
|
return render_hex((const unsigned char *)data, size, caps);
|
|
|
|
}
|
|
|
|
|
2015-05-01 21:41:25 +02:00
|
|
|
inline std::string render_hex_sep(const unsigned char *data, size_t size, const char sep, const bool caps=false)
|
|
|
|
{
|
2016-07-13 07:00:35 +02:00
|
|
|
if (!data)
|
|
|
|
return "NULL";
|
2015-05-01 21:41:25 +02:00
|
|
|
std::string ret;
|
|
|
|
ret.reserve(size*3);
|
|
|
|
bool prsep = false;
|
|
|
|
while (size--)
|
|
|
|
{
|
|
|
|
if (prsep)
|
|
|
|
ret += sep;
|
2016-12-14 18:15:15 +01:00
|
|
|
const RenderHexByte b(*data++, caps);
|
|
|
|
ret += b.char1();
|
|
|
|
ret += b.char2();
|
2015-05-01 21:41:25 +02:00
|
|
|
prsep = true;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-08-19 08:02:52 +02:00
|
|
|
inline std::string render_hex_sep(const void *data, const size_t size, const char sep, const bool caps=false)
|
|
|
|
{
|
|
|
|
return render_hex_sep((const unsigned char *)data, size, sep, caps);
|
|
|
|
}
|
|
|
|
|
2011-10-02 20:55:55 +02:00
|
|
|
template <typename V>
|
2015-04-21 08:32:03 +02:00
|
|
|
inline std::string render_hex_generic(const V& data, const bool caps=false)
|
2011-09-30 17:25:38 +02:00
|
|
|
{
|
|
|
|
std::string ret;
|
|
|
|
ret.reserve(data.size()*2+1);
|
2011-10-02 20:55:55 +02:00
|
|
|
for (size_t i = 0; i < data.size(); ++i)
|
2011-09-30 17:25:38 +02:00
|
|
|
{
|
2016-12-14 18:15:15 +01:00
|
|
|
const RenderHexByte b(data[i], caps);
|
|
|
|
ret += b.char1();
|
|
|
|
ret += b.char2();
|
2011-09-30 00:42:37 +02:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-01-13 07:16:51 +01:00
|
|
|
inline std::string dump_hex(const unsigned char *data, size_t size)
|
2014-10-26 05:25:11 +01:00
|
|
|
{
|
2016-07-13 07:00:35 +02:00
|
|
|
if (!data)
|
|
|
|
return "NULL\n";
|
2015-10-30 23:39:38 +01:00
|
|
|
const unsigned int mask = 0x0F; // N bytes per line - 1
|
2014-10-26 05:25:11 +01:00
|
|
|
std::ostringstream os;
|
|
|
|
os << std::hex;
|
2015-10-30 23:39:38 +01:00
|
|
|
std::string chars;
|
|
|
|
size_t i;
|
|
|
|
for (i = 0; i < size; ++i)
|
2014-10-26 05:25:11 +01:00
|
|
|
{
|
2015-10-30 23:39:38 +01:00
|
|
|
if (!(i & mask))
|
2014-10-26 05:25:11 +01:00
|
|
|
{
|
|
|
|
if (i)
|
2015-10-30 23:39:38 +01:00
|
|
|
{
|
|
|
|
os << " " << chars << std::endl;
|
|
|
|
chars.clear();
|
|
|
|
}
|
2014-10-26 05:25:11 +01:00
|
|
|
os << std::setfill(' ') << std::setw(8) << i << ":";
|
|
|
|
}
|
2015-10-30 23:39:38 +01:00
|
|
|
const unsigned char c = data[i];
|
|
|
|
os << ' ' << std::setfill('0') << std::setw(2) << (unsigned int)c;
|
|
|
|
if (string::is_printable(c))
|
|
|
|
chars += c;
|
|
|
|
else
|
|
|
|
chars += '.';
|
2014-10-26 05:25:11 +01:00
|
|
|
}
|
2015-10-30 23:39:38 +01:00
|
|
|
if (i)
|
|
|
|
os << string::spaces(2 + (((i-1) & mask) ^ mask) * 3) << chars << std::endl;
|
2014-10-26 05:25:11 +01:00
|
|
|
return os.str();
|
|
|
|
}
|
|
|
|
|
2016-02-22 22:22:14 +01:00
|
|
|
inline std::string dump_hex(const std::string& str)
|
|
|
|
{
|
|
|
|
return dump_hex((const unsigned char *)str.c_str(), str.length());
|
|
|
|
}
|
|
|
|
|
2015-01-13 07:16:51 +01:00
|
|
|
template <typename V>
|
|
|
|
inline std::string dump_hex(const V& data)
|
|
|
|
{
|
|
|
|
return dump_hex(data.c_data(), data.size());
|
|
|
|
}
|
|
|
|
|
2011-09-30 17:25:38 +02:00
|
|
|
OPENVPN_SIMPLE_EXCEPTION(parse_hex_error);
|
|
|
|
|
2011-10-02 20:55:55 +02:00
|
|
|
template <typename V>
|
2011-12-16 18:20:46 +01:00
|
|
|
inline void parse_hex(V& dest, const std::string& str)
|
2011-09-30 17:25:38 +02:00
|
|
|
{
|
|
|
|
const int len = int(str.length());
|
|
|
|
int i;
|
|
|
|
for (i = 0; i <= len - 2; i += 2)
|
|
|
|
{
|
|
|
|
const int high = parse_hex_char(str[i]);
|
|
|
|
const int low = parse_hex_char(str[i+1]);
|
|
|
|
if (high == -1 || low == -1)
|
|
|
|
throw parse_hex_error();
|
|
|
|
dest.push_back((high<<4) + low);
|
|
|
|
}
|
|
|
|
if (i != len)
|
|
|
|
throw parse_hex_error(); // straggler char
|
|
|
|
}
|
|
|
|
|
2012-10-31 15:46:40 +01:00
|
|
|
// note -- currently doesn't detect overflow
|
|
|
|
template <typename T>
|
2016-05-12 21:45:14 +02:00
|
|
|
inline bool parse_hex_number(const char *str, T& retval)
|
2012-10-31 15:46:40 +01:00
|
|
|
{
|
|
|
|
if (!str[0])
|
2016-05-12 21:45:14 +02:00
|
|
|
return false; // empty string
|
2012-10-31 15:46:40 +01:00
|
|
|
size_t i = 0;
|
|
|
|
T ret = T(0);
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
const char c = str[i++];
|
|
|
|
const int hd = parse_hex_char(c);
|
|
|
|
if (hd >= 0)
|
|
|
|
{
|
|
|
|
ret *= T(16);
|
|
|
|
ret += T(hd);
|
|
|
|
}
|
|
|
|
else if (!c)
|
2016-05-12 21:45:14 +02:00
|
|
|
{
|
|
|
|
retval = ret;
|
|
|
|
return true;
|
|
|
|
}
|
2012-10-31 15:46:40 +01:00
|
|
|
else
|
2016-05-12 21:45:14 +02:00
|
|
|
return false; // non-hex-digit
|
2012-10-31 15:46:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-12 21:45:14 +02:00
|
|
|
template <typename T>
|
|
|
|
inline bool parse_hex_number(const std::string& str, T& retval)
|
|
|
|
{
|
|
|
|
return parse_hex_number(str.c_str(), retval);
|
|
|
|
}
|
|
|
|
|
2015-08-25 06:07:24 +02:00
|
|
|
template <typename T>
|
|
|
|
inline T parse_hex_number(const std::string& str)
|
|
|
|
{
|
2016-05-12 21:45:14 +02:00
|
|
|
T ret;
|
|
|
|
if (!parse_hex_number<T>(str.c_str(), ret))
|
|
|
|
throw parse_hex_error();
|
|
|
|
return ret;
|
2015-08-25 06:07:24 +02:00
|
|
|
}
|
|
|
|
|
2015-01-08 23:16:11 +01:00
|
|
|
template <typename T>
|
2015-04-21 08:32:03 +02:00
|
|
|
std::string render_hex_number(T value, const bool caps=false)
|
2015-01-08 23:16:11 +01:00
|
|
|
{
|
|
|
|
unsigned char buf[sizeof(T)];
|
|
|
|
for (size_t i = sizeof(T); i --> 0 ;)
|
|
|
|
{
|
|
|
|
buf[i] = value & 0xFF;
|
|
|
|
value >>= 8;
|
|
|
|
}
|
2015-04-21 08:32:03 +02:00
|
|
|
return render_hex(buf, sizeof(T), caps);
|
2015-01-08 23:16:11 +01:00
|
|
|
}
|
|
|
|
|
2016-03-30 08:08:51 +02:00
|
|
|
std::string render_hex_number(unsigned char uc, const bool caps=false)
|
|
|
|
{
|
2016-12-14 18:15:15 +01:00
|
|
|
RenderHexByte b(uc, caps);
|
|
|
|
return std::string(b.str2(), 2);
|
2016-03-30 08:08:51 +02:00
|
|
|
}
|
|
|
|
|
2011-09-30 00:42:37 +02:00
|
|
|
} // namespace openvpn
|
|
|
|
|
|
|
|
#endif // OPENVPN_COMMON_HEXSTR_H
|