stdex
Additional custom or not Standard C++ covered algorithms
Loading...
Searching...
No Matches
hex.hpp
1/*
2 SPDX-License-Identifier: MIT
3 Copyright © 2016-2023 Amebis
4*/
5
6#pragma once
7
8#include "compat.hpp"
9#include <assert.h>
10#include <cstdint>
11#include <string>
12#include <vector>
13
14
15namespace stdex
16{
20 class hex_enc
21 {
22 public:
26 hex_enc() noexcept
27 {
28 }
29
30
38 template<class _Elem, class _Traits, class _Ax>
39 void encode(_Inout_ std::basic_string<_Elem, _Traits, _Ax> &out, _In_bytecount_(size) const void *data, _In_ size_t size)
40 {
41 assert(data || !size);
42
43 // Preallocate output
44 out.reserve(out.size() + enc_size(size));
45
46 // Convert data character by character.
47 for (size_t i = 0; i < size; i++) {
48 uint8_t
49 x = reinterpret_cast<const uint8_t*>(data)[i],
50 x_h = ((x & 0xf0) >> 4),
51 x_l = ((x & 0x0f) );
52
53 out += x_h < 10 ? '0' + x_h : 'A' - 10 + x_h;
54 out += x_l < 10 ? '0' + x_l : 'A' - 10 + x_l;
55 }
56 }
57
58
66 size_t enc_size(_In_ size_t size) const noexcept
67 {
68 return size*2;
69 }
70 };
71
72
76 class hex_dec
77 {
78 public:
82 hex_dec() noexcept :
83 buf(0),
84 num(0)
85 {
86 }
87
88
97 template<class _Ty, class _Ax, class _Tchr>
98 void decode(_Inout_ std::vector<_Ty, _Ax> &out, _Out_ bool &is_last, _In_z_count_(size) const _Tchr *data, _In_ size_t size)
99 {
100 is_last = false;
101
102 // Trim data size to first terminator.
103 for (size_t k = 0; k < size; k++)
104 if (!data[k]) { size = k; break; }
105
106 // Preallocate output
107 out.reserve(out.size() + dec_size(size));
108
109 for (size_t i = 0;; i++) {
110 if (num >= 2) {
111 // Buffer full.
112 out.push_back(buf);
113 num = 0;
114 is_last = true;
115 } else
116 is_last = false;
117
118 if (i >= size)
119 break;
120
121 int x = data[i];
122 if ('0' <= x && x <= '9') {
123 buf = ((buf & 0xf) << 4) | (uint8_t)(x - '0');
124 num++;
125 } else if ('A' <= x && x <= 'F') {
126 buf = ((buf & 0xf) << 4) | (uint8_t)(x - ('A' - 10));
127 num++;
128 } else if ('a' <= x && x <= 'f') {
129 buf = ((buf & 0xf) << 4) | (uint8_t)(x - ('a' - 10));
130 num++;
131 }
132 }
133 }
134
135
139 void clear() noexcept
140 {
141 num = 0;
142 }
143
144
152 size_t dec_size(_In_ size_t size) const noexcept
153 {
154 return (size + 1)/2;
155 }
156
157
158 protected:
159 uint8_t buf;
160 size_t num;
161 };
162}
Hexadecimal decoding session.
Definition hex.hpp:77
void clear() noexcept
Resets decoding session.
Definition hex.hpp:139
uint8_t buf
Internal buffer.
Definition hex.hpp:159
hex_dec() noexcept
Constructs blank decoding session.
Definition hex.hpp:82
size_t num
Number of nibbles used in buf
Definition hex.hpp:160
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:98
size_t dec_size(size_t size) const noexcept
Returns maximum decoded size.
Definition hex.hpp:152
Hexadecimal encoding session.
Definition hex.hpp:21
size_t enc_size(size_t size) const noexcept
Returns maximum encoded size.
Definition hex.hpp:66
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:39
hex_enc() noexcept
Constructs blank encoding session.
Definition hex.hpp:26