wxExtend
Additional templates and function helpers for wxWidgets
Loading...
Searching...
No Matches
hex.h
1/*
2 ​​​SPDX-License-Identifier: GPL-3.0-or-later
3 Copyright © 2015-2022 Amebis
4 Copyright © 2016 GÉANT
5*/
6
7#pragma once
8
9#include "common.h"
10
11#include <codeanalysis\warnings.h>
12#pragma warning(push)
13#pragma warning(disable: WXWIDGETS_CODE_ANALYSIS_WARNINGS)
14#include <wx/string.h>
15#include <wx/buffer.h>
16#pragma warning(pop)
17
18
19// ----------------------------------------------------------------------------
20// Encoding Functions
21// ----------------------------------------------------------------------------
22
25
34inline constexpr size_t wxHexEncodedSize(size_t len) noexcept
35{
36 return 2*len;
37}
38
39
54size_t WXEXTEND_API wxHexEncode(char *dst, size_t dstLen, const void *src, size_t srcLen);
55
56
66inline wxString wxHexEncode(const void *src, size_t srcLen)
67{
68 const size_t dstLen = wxHexEncodedSize(srcLen);
69 wxCharBuffer dst(dstLen);
70 wxHexEncode(dst.data(), dstLen, src, srcLen);
71
72 return dst;
73}
74
75
83inline wxString wxHexEncode(const wxMemoryBuffer& buf)
84{
85 return wxHexEncode(buf.GetData(), buf.GetDataLen());
86}
87
88
89// ----------------------------------------------------------------------------
90// Decoding Functions
91// ----------------------------------------------------------------------------
92
98{
99 Strict,
100 SkipWS,
101 Relaxed,
102};
103
104
113inline constexpr size_t wxHexDecodedSize(size_t len) noexcept
114{
115 return (len + 1)/2;
116}
117
139size_t WXEXTEND_API wxHexDecode(void *dst, size_t dstLen, const char *src, size_t srcLen = wxNO_LEN, wxHexDecodeMode mode = wxHexDecodeMode::Strict, size_t *posErr = NULL);
140
141
160inline size_t wxHexDecode(void *dst, size_t dstLen, const wxString& src, wxHexDecodeMode mode = wxHexDecodeMode::Strict, size_t *posErr = NULL)
161{
162 // don't use str.length() here as the ASCII buffer is shorter than it is for
163 // strings with embedded NULs
164 return wxHexDecode(dst, dstLen, src.ToAscii(), wxNO_LEN, mode, posErr);
165}
166
167
183wxMemoryBuffer WXEXTEND_API wxHexDecode(const char *src, size_t srcLen = wxNO_LEN, wxHexDecodeMode mode = wxHexDecodeMode::Strict, size_t *posErr = NULL);
184
185
199inline wxMemoryBuffer wxHexDecode(const wxString& src, wxHexDecodeMode mode = wxHexDecodeMode::Strict, size_t *posErr = NULL)
200{
201 // don't use str.length() here as the ASCII buffer is shorter than it for
202 // strings with embedded NULs
203 return wxHexDecode(src.ToAscii(), wxNO_LEN, mode, posErr);
204}
205
size_t WXEXTEND_API wxHexDecode(void *dst, size_t dstLen, const char *src, size_t srcLen=wxNO_LEN, wxHexDecodeMode mode=wxHexDecodeMode::Strict, size_t *posErr=NULL)
Raw decoding function which decodes the contents of the string of specified length (or zero terminate...
Definition hex.cpp:36
#define WXEXTEND_API
Public function calling convention.
Definition common.h:56
constexpr size_t wxHexDecodedSize(size_t len) noexcept
Return the buffer size necessary for decoding a hex string of the given length.
Definition hex.h:113
size_t WXEXTEND_API wxHexEncode(char *dst, size_t dstLen, const void *src, size_t srcLen)
Raw hex encoding function which encodes the contents of a buffer of the specified length into the buf...
Definition hex.cpp:10
wxHexDecodeMode
Elements of this enum specify the possible behaviours of wxHexDecode() when an invalid character is e...
Definition hex.h:98
constexpr size_t wxHexEncodedSize(size_t len) noexcept
Return the size needed for the buffer containing the encoded representation of a buffer of given leng...
Definition hex.h:34
@ SkipWS
Skip whitespace characters.
@ Strict
Normal behaviour: stop at any invalid characters.
@ Relaxed
The most lenient behaviour: simply ignore all invalid characters.