stdex Additional custom or not Standard C++ covered algorithms

Loading...
Searching...
No Matches
hex.hpp
1/*
2 SPDX-License-Identifier: MIT
3 Copyright © 2016-2024 Amebis
4*/
5
6#pragma once
7
8#include "assert.hpp"
9#include "compat.hpp"
10#include <cstdint>
11#include <string>
12#include <vector>
13
14 namespace stdex
15{
19 class hex_enc
20 {
21 public:
25 hex_enc() noexcept
26 {}
27
35 template<class T, class TR, class AX>
36 void encode(_Inout_ std::basic_string<T, TR, AX> &out, _In_bytecount_(size) const void *data, _In_ size_t size)
37 {
38 stdex_assert(data || !size);
39
40 // Preallocate output
41 out.reserve(out.size() + enc_size(size));
42
43 // Convert data character by character.
44 for (size_t i = 0; i < size; i++) {
45 uint8_t
46 x = reinterpret_cast<const uint8_t*>(data)[i],
47 x_h = ((x & 0xf0) >> 4),
48 x_l = ((x & 0x0f) );
49
50 out += x_h < 10 ? '0' + x_h : 'A' - 10 + x_h;
51 out += x_l < 10 ? '0' + x_l : 'A' - 10 + x_l;
52 }
53 }
54
62 size_t enc_size(_In_ size_t size) const noexcept
63 {
64 return size*2;
65 }
66 };
67
71 class hex_dec
72 {
73 public:
77 hex_dec() noexcept :
78 buf(0),
79 num(0)
80 {}
81
90 template<class T_to, class AX, class T_from>
91 void decode(_Inout_ std::vector<T_to, AX> &out, _Out_ bool &is_last, _In_z_count_(size) const T_from *data, _In_ size_t size)
92 {
93 is_last = false;
94
95 // Trim data size to first terminator.
96 for (size_t k = 0; k < size; k++)
97 if (!data[k]) { size = k; break; }
98
99 // Preallocate output
100 out.reserve(out.size() + dec_size(size));
101
102 for (size_t i = 0;; i++) {
103 if (num >= 2) {
104 // Buffer full.
105 out.push_back(buf);
106 num = 0;
107 is_last = true;
108 } else
109 is_last = false;
110
111 if (i >= size)
112 break;
113
114 auto x = data[i];
115 if ('0' <= x && x <= '9') {
116 buf = ((buf & 0xf) << 4) | static_cast<uint8_t>(x - '0');
117 num++;
118 } else if ('A' <= x && x <= 'F') {
119 buf = ((buf & 0xf) << 4) | static_cast<uint8_t>(x - ('A' - 10));
120 num++;
121 } else if ('a' <= x && x <= 'f') {
122 buf = ((buf & 0xf) << 4) | static_cast<uint8_t>(x - ('a' - 10));
123 num++;
124 }
125 }
126 }
127
131 void clear() noexcept
132 {
133 num = 0;
134 }
135
143 size_t dec_size(_In_ size_t size) const noexcept
144 {
145 return (size + 1)/2;
146 }
147
148 protected:
149 uint8_t buf;
150 size_t num;
151 };
152}
stdex::hex_dec
Hexadecimal decoding session.
Definition hex.hpp:72
stdex::hex_dec::clear
void clear() noexcept
Resets decoding session.
Definition hex.hpp:131
stdex::hex_dec::buf
uint8_t buf
Internal buffer.
Definition hex.hpp:149
stdex::hex_dec::hex_dec
hex_dec() noexcept
Constructs blank decoding session.
Definition hex.hpp:77
stdex::hex_dec::decode
void decode(std::vector< T_to, AX > &out, bool &is_last, const T_from *data, size_t size)
Decodes one block of information, and appends it to the output.
Definition hex.hpp:91
stdex::hex_dec::num
size_t num
Number of nibbles used in buf
Definition hex.hpp:150
stdex::hex_dec::dec_size
size_t dec_size(size_t size) const noexcept
Returns maximum decoded size.
Definition hex.hpp:143
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:62
stdex::hex_enc::encode
void encode(std::basic_string< T, TR, AX > &out, const void *data, size_t size)
Encodes one block of information, and appends it to the output.
Definition hex.hpp:36
stdex::hex_enc::hex_enc
hex_enc() noexcept
Constructs blank encoding session.
Definition hex.hpp:25