Botan  2.1.0
Crypto and TLS for C++11
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/hex_filt.h>
9 #include <botan/hex.h>
10 #include <botan/parsing.h>
11 #include <botan/charset.h>
12 #include <botan/exceptn.h>
13 #include <algorithm>
14 
15 namespace Botan {
16 
17 /**
18 * Size used for internal buffer in hex encoder/decoder
19 */
20 const size_t HEX_CODEC_BUFFER_SIZE = 256;
21 
22 /*
23 * Hex_Encoder Constructor
24 */
25 Hex_Encoder::Hex_Encoder(bool breaks, size_t length, Case c) :
26  m_casing(c), m_line_length(breaks ? length : 0)
27  {
28  m_in.resize(HEX_CODEC_BUFFER_SIZE);
29  m_out.resize(2*m_in.size());
30  m_counter = m_position = 0;
31  }
32 
33 /*
34 * Hex_Encoder Constructor
35 */
36 Hex_Encoder::Hex_Encoder(Case c) : m_casing(c), m_line_length(0)
37  {
38  m_in.resize(HEX_CODEC_BUFFER_SIZE);
39  m_out.resize(2*m_in.size());
40  m_counter = m_position = 0;
41  }
42 
43 /*
44 * Encode and send a block
45 */
46 void Hex_Encoder::encode_and_send(const uint8_t block[], size_t length)
47  {
48  hex_encode(reinterpret_cast<char*>(m_out.data()),
49  block, length,
50  m_casing == Uppercase);
51 
52  if(m_line_length == 0)
53  send(m_out, 2*length);
54  else
55  {
56  size_t remaining = 2*length, offset = 0;
57  while(remaining)
58  {
59  size_t sent = std::min(m_line_length - m_counter, remaining);
60  send(&m_out[offset], sent);
61  m_counter += sent;
62  remaining -= sent;
63  offset += sent;
64  if(m_counter == m_line_length)
65  {
66  send('\n');
67  m_counter = 0;
68  }
69  }
70  }
71  }
72 
73 /*
74 * Convert some data into hex format
75 */
76 void Hex_Encoder::write(const uint8_t input[], size_t length)
77  {
78  buffer_insert(m_in, m_position, input, length);
79  if(m_position + length >= m_in.size())
80  {
81  encode_and_send(m_in.data(), m_in.size());
82  input += (m_in.size() - m_position);
83  length -= (m_in.size() - m_position);
84  while(length >= m_in.size())
85  {
86  encode_and_send(input, m_in.size());
87  input += m_in.size();
88  length -= m_in.size();
89  }
90  copy_mem(m_in.data(), input, length);
91  m_position = 0;
92  }
93  m_position += length;
94  }
95 
96 /*
97 * Flush buffers
98 */
100  {
101  encode_and_send(m_in.data(), m_position);
102  if(m_counter && m_line_length)
103  send('\n');
104  m_counter = m_position = 0;
105  }
106 
107 /*
108 * Hex_Decoder Constructor
109 */
111  {
112  m_in.resize(HEX_CODEC_BUFFER_SIZE);
113  m_out.resize(m_in.size() / 2);
114  m_position = 0;
115  }
116 
117 /*
118 * Convert some data from hex format
119 */
120 void Hex_Decoder::write(const uint8_t input[], size_t length)
121  {
122  while(length)
123  {
124  size_t to_copy = std::min<size_t>(length, m_in.size() - m_position);
125  copy_mem(&m_in[m_position], input, to_copy);
126  m_position += to_copy;
127 
128  size_t consumed = 0;
129  size_t written = hex_decode(m_out.data(),
130  reinterpret_cast<const char*>(m_in.data()),
131  m_position,
132  consumed,
133  m_checking != FULL_CHECK);
134 
135  send(m_out, written);
136 
137  if(consumed != m_position)
138  {
139  copy_mem(m_in.data(), m_in.data() + consumed, m_position - consumed);
140  m_position = m_position - consumed;
141  }
142  else
143  m_position = 0;
144 
145  length -= to_copy;
146  input += to_copy;
147  }
148  }
149 
150 /*
151 * Flush buffers
152 */
154  {
155  size_t consumed = 0;
156  size_t written = hex_decode(m_out.data(),
157  reinterpret_cast<const char*>(m_in.data()),
158  m_position,
159  consumed,
160  m_checking != FULL_CHECK);
161 
162  send(m_out, written);
163 
164  const bool not_full_bytes = consumed != m_position;
165 
166  m_position = 0;
167 
168  if(not_full_bytes)
169  throw Invalid_Argument("Hex_Decoder: Input not full bytes");
170  }
171 
172 }
virtual void send(const uint8_t in[], size_t length)
Definition: filter.cpp:27
void write(const uint8_t in[], size_t length) override
Definition: hex_filt.cpp:76
const size_t HEX_CODEC_BUFFER_SIZE
Definition: hex_filt.cpp:20
void write(const uint8_t[], size_t) override
Definition: hex_filt.cpp:120
Hex_Decoder(Decoder_Checking checking=NONE)
Definition: hex_filt.cpp:110
void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:68
Definition: alg_id.cpp:13
void end_msg() override
Definition: hex_filt.cpp:153
Hex_Encoder(Case the_case)
Definition: hex_filt.cpp:36
size_t buffer_insert(std::vector< T, Alloc > &buf, size_t buf_offset, const T input[], size_t input_length)
Definition: secmem.h:133
T min(T a, T b)
Definition: ct_utils.h:180
void hex_encode(char output[], const uint8_t input[], size_t input_length, bool uppercase)
Definition: hex.cpp:14
size_t hex_decode(uint8_t output[], const char input[], size_t input_length, size_t &input_consumed, bool ignore_ws)
Definition: hex.cpp:49
void end_msg() override
Definition: hex_filt.cpp:99
Decoder_Checking
Definition: filter.h:179