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 "compat.hpp"
9#include <cstdint>
10#include <string>
11#include <vector>
12
13namespace stdex
14{
18 class hex_enc
19 {
20 public:
24 hex_enc() noexcept
25 {}
26
34 template<class T, class TR, class AX>
35 void encode(_Inout_ std::basic_string<T, TR, AX> &out, _In_bytecount_(size) const void *data, _In_ size_t size)
36 {
37 _Assume_(data || !size);
38
39 // Preallocate output
40 out.reserve(out.size() + enc_size(size));
41
42 // Convert data character by character.
43 for (size_t i = 0; i < size; i++) {
44 uint8_t
45 x = reinterpret_cast<const uint8_t*>(data)[i],
46 x_h = ((x & 0xf0) >> 4),
47 x_l = ((x & 0x0f) );
48
49 out += x_h < 10 ? '0' + x_h : 'A' - 10 + x_h;
50 out += x_l < 10 ? '0' + x_l : 'A' - 10 + x_l;
51 }
52 }
53
61 size_t enc_size(_In_ size_t size) const noexcept
62 {
63 return size*2;
64 }
65 };
66
70 class hex_dec
71 {
72 public:
76 hex_dec() noexcept :
77 buf(0),
78 num(0)
79 {}
80
89 template<class T_to, class AX, class T_from>
90 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)
91 {
92 is_last = false;
93
94 // Trim data size to first terminator.
95 for (size_t k = 0; k < size; k++)
96 if (!data[k]) { size = k; break; }
97
98 // Preallocate output
99 out.reserve(out.size() + dec_size(size));
100
101 for (size_t i = 0;; i++) {
102 if (num >= 2) {
103 // Buffer full.
104 out.push_back(buf);
105 num = 0;
106 is_last = true;
107 } else
108 is_last = false;
109
110 if (i >= size)
111 break;
112
113 auto x = data[i];
114 if ('0' <= x && x <= '9') {
115 buf = ((buf & 0xf) << 4) | static_cast<uint8_t>(x - '0');
116 num++;
117 } else if ('A' <= x && x <= 'F') {
118 buf = ((buf & 0xf) << 4) | static_cast<uint8_t>(x - ('A' - 10));
119 num++;
120 } else if ('a' <= x && x <= 'f') {
121 buf = ((buf & 0xf) << 4) | static_cast<uint8_t>(x - ('a' - 10));
122 num++;
123 }
124 }
125 }
126
130 void clear() noexcept
131 {
132 num = 0;
133 }
134
142 size_t dec_size(_In_ size_t size) const noexcept
143 {
144 return (size + 1)/2;
145 }
146
147 protected:
148 uint8_t buf;
149 size_t num;
150 };
151}
Hexadecimal decoding session.
Definition hex.hpp:71
void clear() noexcept
Resets decoding session.
Definition hex.hpp:130
uint8_t buf
Internal buffer.
Definition hex.hpp:148
hex_dec() noexcept
Constructs blank decoding session.
Definition hex.hpp:76
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:90
size_t num
Number of nibbles used in buf
Definition hex.hpp:149
size_t dec_size(size_t size) const noexcept
Returns maximum decoded size.
Definition hex.hpp:142
Hexadecimal encoding session.
Definition hex.hpp:19
size_t enc_size(size_t size) const noexcept
Returns maximum encoded size.
Definition hex.hpp:61
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:35
hex_enc() noexcept
Constructs blank encoding session.
Definition hex.hpp:24