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