Botan 1.10.17

hex_filt.cpp
Go to the documentation of this file.
1 /*
2 * Hex Encoder/Decoder
3 * (C) 1999-2007 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
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 casing(c), line_length(breaks ? length : 0)
27 {
28 in.resize(HEX_CODEC_BUFFER_SIZE);
29 out.resize(2*in.size());
30 counter = position = 0;
31 }
32
33 /*
34 * Hex_Encoder Constructor
35 */
36 Hex_Encoder::Hex_Encoder(Case c) : casing(c), line_length(0)
37 {
38 in.resize(HEX_CODEC_BUFFER_SIZE);
39 out.resize(2*in.size());
40 counter = position = 0;
41 }
42
43 /*
44 * Encode and send a block
45 */
46 void Hex_Encoder::encode_and_send(const byte block[], size_t length)
47 {
48 hex_encode(reinterpret_cast<char*>(&out[0]),
49 block, length,
50 casing == Uppercase);
51
52 if(line_length == 0)
53 send(out, 2*length);
54 else
55 {
56 size_t remaining = 2*length, offset = 0;
57 while(remaining)
58 {
59 size_t sent = std::min(line_length - counter, remaining);
60 send(&out[offset], sent);
61 counter += sent;
62 remaining -= sent;
63 offset += sent;
64 if(counter == line_length)
65 {
66 send('\n');
67 counter = 0;
68 }
69 }
70 }
71 }
72
73 /*
74 * Convert some data into hex format
75 */
76 void Hex_Encoder::write(const byte input[], size_t length)
77 {
78 in.copy(position, input, length);
79 if(position + length >= in.size())
80 {
81 encode_and_send(&in[0], in.size());
82 input += (in.size() - position);
83 length -= (in.size() - position);
84 while(length >= in.size())
85 {
86 encode_and_send(input, in.size());
87 input += in.size();
88 length -= in.size();
89 }
90 in.copy(input, length);
91 position = 0;
92 }
93 position += length;
94 }
95
96 /*
97 * Flush buffers
98 */
99 void Hex_Encoder::end_msg()
100 {
101 encode_and_send(&in[0], position);
102 if(counter && line_length)
103 send('\n');
104 counter = position = 0;
105 }
106
107 /*
108 * Hex_Decoder Constructor
109 */
110 Hex_Decoder::Hex_Decoder(Decoder_Checking c) : checking(c)
111 {
112 in.resize(HEX_CODEC_BUFFER_SIZE);
113 out.resize(in.size() / 2);
114 position = 0;
115 }
116
117 /*
118 * Convert some data from hex format
119 */
120 void Hex_Decoder::write(const byte input[], size_t length)
121 {
122 while(length)
123 {
124 size_t to_copy = std::min<size_t>(length, in.size() - position);
125 copy_mem(&in[position], input, to_copy);
126 position += to_copy;
127
128 size_t consumed = 0;
129 size_t written = hex_decode(&out[0],
130 reinterpret_cast<const char*>(&in[0]),
131 position,
132 consumed,
133 checking != FULL_CHECK);
134
135 send(out, written);
136
137 if(consumed != position)
138 {
139 copy_mem(&in[0], &in[consumed], position - consumed);
140 position = position - consumed;
141 }
142 else
143 position = 0;
144
145 length -= to_copy;
146 input += to_copy;
147 }
148 }
149
150 /*
151 * Flush buffers
152 */
153 void Hex_Decoder::end_msg()
154 {
155 size_t consumed = 0;
156 size_t written = hex_decode(&out[0],
157 reinterpret_cast<const char*>(&in[0]),
158 position,
159 consumed,
160 checking != FULL_CHECK);
161
162 send(out, written);
163
164 const bool not_full_bytes = consumed != position;
165
166 position = 0;
167
168 if(not_full_bytes)
169 throw std::invalid_argument("Hex_Decoder: Input not full bytes");
170 }
171
172 }
Botan::MemoryRegion::resize
void resize(size_t n)
Definition: secmem.h:217
Botan::Hex_Encoder::Uppercase
Definition: hex_filt.h:25
Botan::Hex_Encoder::Case
Case
Definition: hex_filt.h:25
Botan::hex_decode
size_t hex_decode(byte output[], const char input[], size_t input_length, size_t &input_consumed, bool ignore_ws)
Definition: hex.cpp:55
Botan::Hex_Encoder::end_msg
void end_msg()
Definition: hex_filt.cpp:99
Botan::HEX_CODEC_BUFFER_SIZE
const size_t HEX_CODEC_BUFFER_SIZE
Definition: hex_filt.cpp:20
Botan::Hex_Decoder::Hex_Decoder
Hex_Decoder(Decoder_Checking checking=NONE)
Definition: hex_filt.cpp:110
Botan::Hex_Decoder::end_msg
void end_msg()
Definition: hex_filt.cpp:153
Botan::MemoryRegion::copy
void copy(const T in[], size_t n)
Definition: secmem.h:126
Botan::FULL_CHECK
Definition: filter.h:155
Botan::byte
unsigned char byte
Definition: types.h:22
Botan::Filter::send
void send(const byte in[], size_t length)
Definition: filter.cpp:28
Botan::copy_mem
void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:22
Botan
Definition: algo_base.h:14
Botan::Hex_Encoder::Hex_Encoder
Hex_Encoder(Case the_case)
Definition: hex_filt.cpp:36
Botan::MemoryRegion::size
size_t size() const
Definition: secmem.h:29
Botan::CT::min
T min(T a, T b)
Definition: ct_utils.h:127
Botan::Hex_Decoder::write
void write(const byte[], size_t)
Definition: hex_filt.cpp:120
Botan::Hex_Encoder::write
void write(const byte in[], size_t length)
Definition: hex_filt.cpp:76
Botan::Decoder_Checking
Decoder_Checking
Definition: filter.h:155
Botan::hex_encode
void hex_encode(char output[], const byte input[], size_t input_length, bool uppercase)
Definition: hex.cpp:14