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