stdex
Additional custom or not Standard C++ covered algorithms
hex.h
1/*
2 SPDX-License-Identifier: MIT
3 Copyright © 2016-2022 Amebis
4*/
5
6#pragma once
7
8#include <string>
9#include <vector>
10
11
12namespace stdex
13{
17 class hex_enc
18 {
19 public:
23 hex_enc() noexcept
24 {
25 }
26
27
35 template<class _Elem, class _Traits, class _Ax>
36 void encode(_Inout_ std::basic_string<_Elem, _Traits, _Ax> &out, _In_bytecount_(size) const void *data, _In_ size_t size)
37 {
38 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 unsigned char
46 x = reinterpret_cast<const unsigned char*>(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
55
63 size_t enc_size(size_t size) const noexcept
64 {
65 return size*2;
66 }
67 };
68
69
73 class hex_dec
74 {
75 public:
79 hex_dec() noexcept :
80 buf(0),
81 num(0)
82 {
83 }
84
85
94 template<class _Ty, class _Ax, class _Tchr>
95 void decode(_Inout_ std::vector<_Ty, _Ax> &out, _Out_ bool &is_last, _In_z_count_(size) const _Tchr *data, _In_ size_t size)
96 {
97 is_last = false;
98
99 // Trim data size to first terminator.
100 for (size_t k = 0; k < size; k++)
101 if (!data[k]) { size = k; break; }
102
103 // Preallocate output
104 out.reserve(out.size() + dec_size(size));
105
106 for (size_t i = 0;; i++) {
107 if (num >= 2) {
108 // Buffer full.
109 out.push_back(buf);
110 num = 0;
111 is_last = true;
112 } else
113 is_last = false;
114
115 if (i >= size)
116 break;
117
118 int x = data[i];
119 if ('0' <= x && x <= '9') {
120 buf = ((buf & 0xf) << 4) | (unsigned char)(x - '0');
121 num++;
122 } else if ('A' <= x && x <= 'F') {
123 buf = ((buf & 0xf) << 4) | (unsigned char)(x - ('A' - 10));
124 num++;
125 } else if ('a' <= x && x <= 'f') {
126 buf = ((buf & 0xf) << 4) | (unsigned char)(x - ('a' - 10));
127 num++;
128 }
129 }
130 }
131
132
136 void clear() noexcept
137 {
138 num = 0;
139 }
140
141
149 size_t dec_size(size_t size) const noexcept
150 {
151 return (size + 1)/2;
152 }
153
154
155 protected:
156 unsigned char buf;
157 size_t num;
158 };
159}
Hexadecimal decoding session.
Definition: hex.h:74
unsigned char buf
Internal buffer.
Definition: hex.h:156
void clear() noexcept
Resets decoding session.
Definition: hex.h:136
hex_dec() noexcept
Constructs blank decoding session.
Definition: hex.h:79
size_t num
Number of nibbles used in buf
Definition: hex.h:157
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.h:95
size_t dec_size(size_t size) const noexcept
Returns maximum decoded size.
Definition: hex.h:149
Hexadecimal encoding session.
Definition: hex.h:18
size_t enc_size(size_t size) const noexcept
Returns maximum encoded size.
Definition: hex.h:63
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.h:36
hex_enc() noexcept
Constructs blank encoding session.
Definition: hex.h:23