Botan 1.10.17

b64_filt.cpp
Go to the documentation of this file.
1 /*
2 * Base64 Encoder/Decoder
3 * (C) 1999-2010 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7
8 #include <botan/b64_filt.h>
9 #include <botan/base64.h>
10 #include <botan/charset.h>
11 #include <botan/exceptn.h>
12 #include <algorithm>
13
14 namespace Botan {
15
16 /*
17 * Base64_Encoder Constructor
18 */
19 Base64_Encoder::Base64_Encoder(bool breaks, size_t length, bool t_n) :
20 line_length(breaks ? length : 0),
21 trailing_newline(t_n && breaks),
22 in(48),
23 out(64),
24 position(0),
25 out_position(0)
26 {
27 }
28
29 /*
30 * Encode and send a block
31 */
32 void Base64_Encoder::encode_and_send(const byte input[], size_t length,
33 bool final_inputs)
34 {
35 while(length)
36 {
37 const size_t proc = std::min(length, in.size());
38
39 size_t consumed = 0;
40 size_t produced = base64_encode(reinterpret_cast<char*>(&out[0]), input,
41 proc, consumed, final_inputs);
42
43 do_output(&out[0], produced);
44
45 // FIXME: s/proc/consumed/?
46 input += proc;
47 length -= proc;
48 }
49 }
50
51 /*
52 * Handle the output
53 */
54 void Base64_Encoder::do_output(const byte input[], size_t length)
55 {
56 if(line_length == 0)
57 send(input, length);
58 else
59 {
60 size_t remaining = length, offset = 0;
61 while(remaining)
62 {
63 size_t sent = std::min(line_length - out_position, remaining);
64 send(input + offset, sent);
65 out_position += sent;
66 remaining -= sent;
67 offset += sent;
68 if(out_position == line_length)
69 {
70 send('\n');
71 out_position = 0;
72 }
73 }
74 }
75 }
76
77 /*
78 * Convert some data into Base64
79 */
80 void Base64_Encoder::write(const byte input[], size_t length)
81 {
82 in.copy(position, input, length);
83 if(position + length >= in.size())
84 {
85 encode_and_send(&in[0], in.size());
86 input += (in.size() - position);
87 length -= (in.size() - position);
88 while(length >= in.size())
89 {
90 encode_and_send(input, in.size());
91 input += in.size();
92 length -= in.size();
93 }
94 in.copy(input, length);
95 position = 0;
96 }
97 position += length;
98 }
99
100 /*
101 * Flush buffers
102 */
103 void Base64_Encoder::end_msg()
104 {
105 encode_and_send(&in[0], position, true);
106
107 if(trailing_newline || (out_position && line_length))
108 send('\n');
109
110 out_position = position = 0;
111 }
112
113 /*
114 * Base64_Decoder Constructor
115 */
116 Base64_Decoder::Base64_Decoder(Decoder_Checking c) :
117 checking(c), in(64), out(48), position(0)
118 {
119 }
120
121 /*
122 * Convert some data from Base64
123 */
124 void Base64_Decoder::write(const byte input[], size_t length)
125 {
126 while(length)
127 {
128 size_t to_copy = std::min<size_t>(length, in.size() - position);
129 if(to_copy == 0)
130 {
131 in.resize(in.size()*2);
132 out.resize(out.size()*2);
133 }
134 copy_mem(&in[position], input, to_copy);
135 position += to_copy;
136
137 size_t consumed = 0;
138 size_t written = base64_decode(&out[0],
139 reinterpret_cast<const char*>(&in[0]),
140 position,
141 consumed,
142 false,
143 checking != FULL_CHECK);
144
145 send(out, written);
146
147 if(consumed != position)
148 {
149 copy_mem(&in[0], &in[consumed], position - consumed);
150 position = position - consumed;
151 }
152 else
153 position = 0;
154
155 length -= to_copy;
156 input += to_copy;
157 }
158 }
159
160 /*
161 * Flush buffers
162 */
163 void Base64_Decoder::end_msg()
164 {
165 size_t consumed = 0;
166 size_t written = base64_decode(&out[0],
167 reinterpret_cast<const char*>(&in[0]),
168 position,
169 consumed,
170 true,
171 checking != FULL_CHECK);
172
173 send(out, written);
174
175 const bool not_full_bytes = consumed != position;
176
177 position = 0;
178
179 if(not_full_bytes)
180 throw std::invalid_argument("Base64_Decoder: Input not full bytes");
181 }
182
183 }
Botan::MemoryRegion::resize
void resize(size_t n)
Definition: secmem.h:217
Botan::Base64_Encoder::Base64_Encoder
Base64_Encoder(bool breaks=false, size_t length=72, bool t_n=false)
Definition: b64_filt.cpp:19
Botan::base64_decode
size_t base64_decode(byte output[], const char input[], size_t input_length, size_t &input_consumed, bool final_inputs, bool ignore_ws)
Definition: base64.cpp:100
Botan::Base64_Decoder::Base64_Decoder
Base64_Decoder(Decoder_Checking checking=NONE)
Definition: b64_filt.cpp:116
Botan::Base64_Decoder::end_msg
void end_msg()
Definition: b64_filt.cpp:163
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::Base64_Decoder::write
void write(const byte input[], size_t length)
Definition: b64_filt.cpp:124
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::Base64_Encoder::end_msg
void end_msg()
Definition: b64_filt.cpp:103
Botan::MemoryRegion::size
size_t size() const
Definition: secmem.h:29
Botan::Base64_Encoder::write
void write(const byte input[], size_t length)
Definition: b64_filt.cpp:80
Botan::CT::min
T min(T a, T b)
Definition: ct_utils.h:127
Botan::base64_encode
size_t base64_encode(char out[], const byte in[], size_t input_length, size_t &input_consumed, bool final_inputs)
Definition: base64.cpp:36
Botan::Decoder_Checking
Decoder_Checking
Definition: filter.h:155