Botan 3.13.0
Crypto and TLS for C&
hex_filt.cpp
Go to the documentation of this file.
1/*
2* Hex Encoder/Decoder
3* (C) 1999-2007 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#include <botan/filters.h>
9
10#include <botan/exceptn.h>
11#include <botan/hex.h>
12#include <botan/mem_ops.h>
13#include <algorithm>
14
15namespace Botan {
16
17/**
18* Size used for internal buffer in hex encoder/decoder
19*/
20const size_t HEX_CODEC_BUFFER_SIZE = 256;
21
22/*
23* Hex_Encoder Constructor
24*/
25Hex_Encoder::Hex_Encoder(bool breaks, size_t length, Case c) : m_casing(c), m_line_length(breaks ? length : 0) {
26 m_in.resize(HEX_CODEC_BUFFER_SIZE);
27 m_out.resize(2 * m_in.size());
28}
29
30/*
31* Hex_Encoder Constructor
32*/
33Hex_Encoder::Hex_Encoder(Case c) : m_casing(c), m_line_length(0) {
34 m_in.resize(HEX_CODEC_BUFFER_SIZE);
35 m_out.resize(2 * m_in.size());
36}
37
38/*
39* Encode and send a block
40*/
41void Hex_Encoder::encode_and_send(const uint8_t block[], size_t length) {
42 hex_encode(cast_uint8_ptr_to_char(m_out.data()), block, length, m_casing == Uppercase);
43
44 if(m_line_length == 0) {
45 send(m_out, 2 * length);
46 } else {
47 size_t remaining = 2 * length;
48 size_t offset = 0;
49 while(remaining > 0) {
50 const size_t sent = std::min(m_line_length - m_counter, remaining);
51 send(&m_out[offset], sent);
52 m_counter += sent;
53 remaining -= sent;
54 offset += sent;
55 if(m_counter == m_line_length) {
56 send('\n');
57 m_counter = 0;
58 }
59 }
60 }
61}
62
63/*
64* Convert some data into hex format
65*/
66void Hex_Encoder::write(const uint8_t input[], size_t length) {
67 const size_t initial_fill = std::min(m_in.size() - m_position, length);
68 copy_mem(&m_in[m_position], input, initial_fill);
69
70 if(m_position + length >= m_in.size()) {
71 encode_and_send(m_in.data(), m_in.size());
72 input += (m_in.size() - m_position);
73 length -= (m_in.size() - m_position);
74 while(length >= m_in.size()) {
75 encode_and_send(input, m_in.size());
76 input += m_in.size();
77 length -= m_in.size();
78 }
79 copy_mem(m_in.data(), input, length);
80 m_position = 0;
81 }
82 m_position += length;
83}
84
85/*
86* Flush buffers
87*/
89 encode_and_send(m_in.data(), m_position);
90 if(m_counter > 0 && m_line_length > 0) {
91 send('\n');
92 }
93 m_counter = m_position = 0;
94}
95
96/*
97* Hex_Decoder Constructor
98*/
100 m_in.resize(HEX_CODEC_BUFFER_SIZE);
101 m_out.resize(m_in.size() / 2);
102}
103
104namespace {
105
106// Whitespace ignored by hex_char_to_bin() in hex.cpp. Dropping it before
107// buffering keeps the decoder buffer bounded (see Hex_Decoder::write).
108bool hex_ignorable_whitespace(uint8_t c) {
109 return c == ' ' || c == '\t' || c == '\n' || c == '\r';
110}
111
112} // namespace
113
114/*
115* Convert some data from hex format
116*/
117void Hex_Decoder::write(const uint8_t input[], size_t length) {
118 const bool ignore_ws = (m_checking != FULL_CHECK);
119
120 size_t pos = 0;
121 while(pos < length) {
122 // Drop ignored whitespace before buffering so a partial trailing nibble
123 // stays next to its pair and the fixed buffer can't fill with whitespace.
124 while(pos < length && m_position < m_in.size()) {
125 const uint8_t c = input[pos++];
126 if(ignore_ws && hex_ignorable_whitespace(c)) {
127 continue;
128 }
129 m_in[m_position++] = c;
130 }
131
132 size_t consumed = 0;
133 const size_t written =
134 hex_decode(m_out.data(), cast_uint8_ptr_to_char(m_in.data()), m_position, consumed, ignore_ws);
135
136 send(m_out, written);
137
138 if(consumed != m_position) {
139 copy_mem(m_in.data(), m_in.data() + consumed, m_position - consumed);
140 m_position = m_position - consumed;
141 } else {
142 m_position = 0;
143 }
144 }
145}
146
147/*
148* Flush buffers
149*/
151 size_t consumed = 0;
152 const size_t written =
153 hex_decode(m_out.data(), cast_uint8_ptr_to_char(m_in.data()), m_position, consumed, m_checking != FULL_CHECK);
154
155 send(m_out, written);
156
157 const bool not_full_bytes = consumed != m_position;
158
159 m_position = 0;
160
161 if(not_full_bytes) {
162 throw Invalid_Argument("Hex_Decoder: Input not full bytes");
163 }
164}
165
166} // namespace Botan
virtual void send(const uint8_t in[], size_t length)
Definition filter.cpp:30
void end_msg() override
Definition hex_filt.cpp:150
Hex_Decoder(Decoder_Checking checking=NONE)
Definition hex_filt.cpp:99
void write(const uint8_t input[], size_t length) override
Definition hex_filt.cpp:117
void write(const uint8_t in[], size_t length) override
Definition hex_filt.cpp:66
void end_msg() override
Definition hex_filt.cpp:88
Hex_Encoder(Case the_case)
Definition hex_filt.cpp:33
Decoder_Checking
Definition filter.h:193
@ FULL_CHECK
Definition filter.h:193
constexpr void copy_mem(T *out, const T *in, size_t n)
Definition mem_ops.h:144
const size_t HEX_CODEC_BUFFER_SIZE
Definition hex_filt.cpp:20
void hex_encode(char output[], const uint8_t input[], size_t input_length, bool uppercase)
Definition hex.cpp:34
size_t hex_decode(uint8_t output[], const char input[], size_t input_length, size_t &input_consumed, bool ignore_ws)
Definition hex.cpp:75
const char * cast_uint8_ptr_to_char(const uint8_t *b)
Definition mem_ops.h:323