Loading...
Searching...
No Matches
hex.hpp
/*
SPDX-License-Identifier: MIT
Copyright © 2016-2023 Amebis
*/
#pragma once
#include "compat.hpp"
#include <cstdint>
#include <string>
#include <vector>
namespace stdex
{
class hex_enc
{
public:
hex_enc() noexcept
{
}
template<class _Elem, class _Traits, class _Ax>
void encode(_Inout_ std::basic_string<_Elem, _Traits, _Ax> &out, _In_bytecount_(size) const void *data, _In_ size_t size)
{
_Assume_(data || !size);
// Preallocate output
out.reserve(out.size() + enc_size(size));
// Convert data character by character.
for (size_t i = 0; i < size; i++) {
uint8_t
x = reinterpret_cast<const uint8_t*>(data)[i],
x_h = ((x & 0xf0) >> 4),
x_l = ((x & 0x0f) );
out += x_h < 10 ? '0' + x_h : 'A' - 10 + x_h;
out += x_l < 10 ? '0' + x_l : 'A' - 10 + x_l;
}
}
size_t enc_size(_In_ size_t size) const noexcept
{
return size*2;
}
};
class hex_dec
{
public:
hex_dec() noexcept :
buf(0),
num(0)
{
}
template<class _Ty, class _Ax, class _Tchr>
void decode(_Inout_ std::vector<_Ty, _Ax> &out, _Out_ bool &is_last, _In_z_count_(size) const _Tchr *data, _In_ size_t size)
{
is_last = false;
// Trim data size to first terminator.
for (size_t k = 0; k < size; k++)
if (!data[k]) { size = k; break; }
// Preallocate output
out.reserve(out.size() + dec_size(size));
for (size_t i = 0;; i++) {
if (num >= 2) {
// Buffer full.
out.push_back(buf);
num = 0;
is_last = true;
} else
is_last = false;
if (i >= size)
break;
int x = data[i];
if ('0' <= x && x <= '9') {
buf = ((buf & 0xf) << 4) | (uint8_t)(x - '0');
num++;
} else if ('A' <= x && x <= 'F') {
buf = ((buf & 0xf) << 4) | (uint8_t)(x - ('A' - 10));
num++;
} else if ('a' <= x && x <= 'f') {
buf = ((buf & 0xf) << 4) | (uint8_t)(x - ('a' - 10));
num++;
}
}
}
void clear() noexcept
{
num = 0;
}
size_t dec_size(_In_ size_t size) const noexcept
{
return (size + 1)/2;
}
protected:
uint8_t buf;
size_t num;
};
}
stdex::hex_dec
Hexadecimal decoding session.
Definition hex.hpp:76
stdex::hex_dec::clear
void clear() noexcept
Resets decoding session.
Definition hex.hpp:138
stdex::hex_dec::buf
uint8_t buf
Internal buffer.
Definition hex.hpp:158
stdex::hex_dec::hex_dec
hex_dec() noexcept
Constructs blank decoding session.
Definition hex.hpp:81
stdex::hex_dec::num
size_t num
Number of nibbles used in buf
Definition hex.hpp:159
stdex::hex_dec::decode
void decode(std::vector< _Ty, _Ax > &out, bool &is_last, const _Tchr *data, size_t size)
Decodes one block of information, and appends it to the output.
Definition hex.hpp:97
stdex::hex_dec::dec_size
size_t dec_size(size_t size) const noexcept
Returns maximum decoded size.
Definition hex.hpp:151
stdex::hex_enc
Hexadecimal encoding session.
Definition hex.hpp:20
stdex::hex_enc::enc_size
size_t enc_size(size_t size) const noexcept
Returns maximum encoded size.
Definition hex.hpp:65
stdex::hex_enc::encode
void encode(std::basic_string< _Elem, _Traits, _Ax > &out, const void *data, size_t size)
Encodes one block of information, and appends it to the output.
Definition hex.hpp:38
stdex::hex_enc::hex_enc
hex_enc() noexcept
Constructs blank encoding session.
Definition hex.hpp:25