diff --git a/include/stdex/hex b/include/stdex/hex new file mode 100644 index 000000000..bcc25edb0 --- /dev/null +++ b/include/stdex/hex @@ -0,0 +1,159 @@ +/* + SPDX-License-Identifier: GPL-3.0-or-later + Copyright © 2016-2022 Amebis +*/ + +#pragma once + +#include +#include + + +namespace stdex +{ + /// + /// Hexadecimal encoding session + /// + class hex_enc + { + public: + /// + /// Constructs blank encoding session + /// + hex_enc() noexcept + { + } + + + /// + /// Encodes one block of information, and _appends_ it to the output + /// + /// \param[out] out Output + /// \param[in ] data Data to encode + /// \param[in ] size Length of `data` in bytes + /// + template + void encode(_Inout_ std::basic_string<_Elem, _Traits, _Ax> &out, _In_bytecount_(size) const void *data, _In_ size_t size) + { + assert(data || !size); + + // Preallocate output + out.reserve(out.size() + enc_size(size)); + + // Convert data character by character. + for (size_t i = 0; i < size; i++) { + unsigned char + x = reinterpret_cast(data)[i], + x_h = ((x & 0xf0) >> 4), + x_l = ((x & 0x0f) ); + + out += x_h < 10 ? '0' + x_h : 'A' - 10 + x_h; + out += x_l < 10 ? '0' + x_l : 'A' - 10 + x_l; + } + } + + + /// + /// Returns maximum encoded size + /// + /// \param size Number of bytes to encode + /// + /// \returns Maximum number of bytes for the encoded data of `size` length + /// + size_t enc_size(size_t size) const noexcept + { + return size*2; + } + }; + + + /// + /// Hexadecimal decoding session + /// + class hex_dec + { + public: + /// + /// Constructs blank decoding session + /// + hex_dec() noexcept : + buf(0), + num(0) + { + } + + + /// + /// Decodes one block of information, and _appends_ it to the output + /// + /// \param[inout] out Output + /// \param[out ] is_last Was this the last block of data? Actually, is this block of data complete? + /// \param[in ] data Data to decode + /// \param[in ] size Length of `data` in bytes + /// + template + void decode(_Inout_ std::vector<_Ty, _Ax> &out, _Out_ bool &is_last, _In_z_count_(size) const _Tchr *data, _In_ size_t size) + { + is_last = false; + + // Trim data size to first terminator. + for (size_t k = 0; k < size; k++) + if (!data[k]) { size = k; break; } + + // Preallocate output + out.reserve(out.size() + dec_size(size)); + + for (size_t i = 0;; i++) { + if (num >= 2) { + // Buffer full. + out.push_back(buf); + num = 0; + is_last = true; + } else + is_last = false; + + if (i >= size) + break; + + int x = data[i]; + if ('0' <= x && x <= '9') { + buf = ((buf & 0xf) << 4) | (unsigned char)(x - '0'); + num++; + } else if ('A' <= x && x <= 'F') { + buf = ((buf & 0xf) << 4) | (unsigned char)(x - ('A' - 10)); + num++; + } else if ('a' <= x && x <= 'f') { + buf = ((buf & 0xf) << 4) | (unsigned char)(x - ('a' - 10)); + num++; + } + } + } + + + /// + /// Resets decoding session + /// + void clear() noexcept + { + num = 0; + } + + + /// + /// Returns maximum decoded size + /// + /// \param size Number of bytes to decode + /// + /// \returns Maximum number of bytes for the decoded data of `size` length + /// + size_t dec_size(size_t size) const noexcept + { + return (size + 1)/2; + } + + + protected: + unsigned char buf; ///< Internal buffer + size_t num; ///< Number of nibbles used in `buf` + }; +}