10#include <botan/exceptn.h>
11#include <botan/mem_ops.h>
12#include <botan/internal/charset.h>
13#include <botan/internal/ct_utils.h>
14#include <botan/internal/fmt.h>
20char hex_encode_nibble(uint8_t n,
bool uppercase) {
25 const char c_09 = n +
'0';
26 const char c_af = n + (uppercase ?
'A' :
'a') - 10;
28 return in_09.select(c_09, c_af);
33void hex_encode(
char output[],
const uint8_t input[],
size_t input_length,
bool uppercase) {
34 for(
size_t i = 0; i != input_length; ++i) {
35 const uint8_t n0 = (input[i] >> 4) & 0xF;
36 const uint8_t n1 = (input[i]) & 0xF;
38 output[2 * i] = hex_encode_nibble(n0, uppercase);
39 output[2 * i + 1] = hex_encode_nibble(n1, uppercase);
43std::string
hex_encode(
const uint8_t input[],
size_t input_length,
bool uppercase) {
44 std::string output(2 * input_length, 0);
47 hex_encode(&output.front(), input, input_length, uppercase);
55uint8_t hex_char_to_bin(
char input) {
56 const uint8_t c =
static_cast<uint8_t
>(input);
62 const auto is_whitespace =
65 const uint8_t c_upper = c - uint8_t(
'A') + 10;
66 const uint8_t c_lower = c - uint8_t(
'a') + 10;
67 const uint8_t c_decim = c - uint8_t(
'0');
71 ret = is_alpha_upper.select(c_upper, ret);
72 ret = is_alpha_lower.select(c_lower, ret);
73 ret = is_decimal.select(c_decim, ret);
74 ret = is_whitespace.select(0x80, ret);
81size_t hex_decode(uint8_t output[],
const char input[],
size_t input_length,
size_t& input_consumed,
bool ignore_ws) {
82 uint8_t* out_ptr = output;
83 bool top_nibble =
true;
87 for(
size_t i = 0; i != input_length; ++i) {
88 const uint8_t bin = hex_char_to_bin(input[i]);
91 if(bin == 0x80 && ignore_ws) {
104 top_nibble = !top_nibble;
110 input_consumed = input_length;
111 size_t written = (out_ptr - output);
125size_t hex_decode(uint8_t output[],
const char input[],
size_t input_length,
bool ignore_ws) {
127 size_t written =
hex_decode(output, input, input_length, consumed, ignore_ws);
129 if(consumed != input_length) {
136size_t hex_decode(uint8_t output[], std::string_view input,
bool ignore_ws) {
137 return hex_decode(output, input.data(), input.length(), ignore_ws);
140size_t hex_decode(std::span<uint8_t> output, std::string_view input,
bool ignore_ws) {
141 return hex_decode(output.data(), input.data(), input.length(), ignore_ws);
147 size_t written =
hex_decode(bin.data(), input, input_length, ignore_ws);
157std::vector<uint8_t>
hex_decode(
const char input[],
size_t input_length,
bool ignore_ws) {
158 std::vector<uint8_t> bin(1 + input_length / 2);
160 size_t written =
hex_decode(bin.data(), input, input_length, ignore_ws);
166std::vector<uint8_t>
hex_decode(std::string_view input,
bool ignore_ws) {
167 return hex_decode(input.data(), input.size(), ignore_ws);
#define BOTAN_DEBUG_ASSERT(expr)
static constexpr Mask< T > is_within_range(T v, T l, T u)
static constexpr Mask< T > is_lt(T x, T y)
static constexpr Mask< T > is_any_of(T v, std::initializer_list< T > accepted)
std::string format_char_for_display(char c)
std::string fmt(std::string_view format, const T &... args)
secure_vector< uint8_t > hex_decode_locked(const char input[], size_t input_length, bool ignore_ws)
void hex_encode(char output[], const uint8_t input[], size_t input_length, bool uppercase)
size_t hex_decode(uint8_t output[], const char input[], size_t input_length, size_t &input_consumed, bool ignore_ws)
std::vector< T, secure_allocator< T > > secure_vector
constexpr void clear_mem(T *ptr, size_t n)