-
Notifications
You must be signed in to change notification settings - Fork 175
Expand file tree
/
Copy pathencoder.rs
More file actions
192 lines (160 loc) · 5.65 KB
/
encoder.rs
File metadata and controls
192 lines (160 loc) · 5.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
//! Buffered Base64 encoder.
use crate::{
variant::Variant,
Encoding,
Error::{self, InvalidLength},
};
use core::{cmp, marker::PhantomData, str};
/// Stateful Base64 encoder with support for buffered, incremental encoding.
///
/// The `E` type parameter can be any type which impls [`Encoding`] such as
/// [`Base64`] or [`Base64Unpadded`].
///
/// Internally it uses a sealed `Variant` trait which is an implementation
/// detail of this crate, and leverages a [blanket impl] of [`Encoding`].
///
/// [blanket impl]: ./trait.Encoding.html#impl-Encoding
pub struct Encoder<'o, E: Variant> {
/// Output buffer.
output: &'o mut [u8],
/// Cursor within the output buffer.
position: usize,
/// Block buffer used for non-block-aligned data.
block_buffer: BlockBuffer,
/// Phantom parameter for the Base64 encoding in use.
encoding: PhantomData<E>,
}
impl<'o, E: Variant> Encoder<'o, E> {
/// Create a new decoder for a byte slice containing contiguous
/// (non-newline-delimited) Base64-encoded data.
pub fn new(output: &'o mut [u8]) -> Result<Self, Error> {
if output.is_empty() {
return Err(InvalidLength);
}
Ok(Self {
output,
position: 0,
block_buffer: BlockBuffer::default(),
encoding: PhantomData,
})
}
/// Encode the provided buffer as Base64, writing it to the output buffer.
///
/// # Returns
/// - `Ok(bytes)` if the expected amount of data was read
/// - `Err(Error::InvalidLength)` if there is insufficient space in the output buffer
pub fn encode(&mut self, mut input: &[u8]) -> Result<(), Error> {
while !input.is_empty() {
// If there's data in the block buffer, fill it
if !self.block_buffer.is_empty() {
self.fill_block_buffer(&mut input)?;
}
// Attempt to decode a stride of block-aligned data
let in_blocks = input.len() / 3;
let out_blocks = self.remaining().len() / 4;
let blocks = cmp::min(in_blocks, out_blocks);
if blocks > 0 {
let (in_aligned, in_rem) = input.split_at(blocks * 3);
input = in_rem;
self.perform_encode(in_aligned)?;
}
// If there's remaining non-aligned data, fill the block buffer
if !input.is_empty() {
self.fill_block_buffer(&mut input)?;
}
}
Ok(())
}
/// Finish encoding data, returning the resulting Base64 as a `str`.
pub fn finish(mut self) -> Result<&'o str, Error> {
if !self.block_buffer.is_empty() {
let buffer_len = self.block_buffer.position;
let block = self.block_buffer.bytes;
self.perform_encode(&block[..buffer_len])?;
}
Ok(str::from_utf8(&self.output[..self.position])?)
}
/// Borrow the remaining data in the buffer.
fn remaining(&mut self) -> &mut [u8] {
&mut self.output[self.position..]
}
/// Fill the block buffer with data, consuming and encoding it when the
/// buffer is full.
fn fill_block_buffer(&mut self, input: &mut &[u8]) -> Result<(), Error> {
self.block_buffer.fill(input);
if self.block_buffer.is_full() {
let block = self.block_buffer.take();
self.perform_encode(&block)?;
}
Ok(())
}
/// Perform Base64 encoding operation.
fn perform_encode(&mut self, input: &[u8]) -> Result<usize, Error> {
let len = E::encode(input, self.remaining())?.as_bytes().len();
self.position += len;
Ok(len)
}
}
/// Base64 encode buffer for a 1-block output.
///
/// This handles a partial block of data, i.e. data which hasn't been
#[derive(Clone, Default, Debug)]
struct BlockBuffer {
/// 3 decoded bytes to be encoded to a 4-byte Base64-encoded input.
bytes: [u8; Self::SIZE],
/// Position within the buffer.
position: usize,
}
impl BlockBuffer {
/// Size of the buffer in bytes: 3-bytes of unencoded input which
/// Base64 encode to 4-bytes of output.
const SIZE: usize = 3;
/// Fill the remaining space in the buffer with the input data.
fn fill(&mut self, input: &mut &[u8]) {
let len = cmp::min(Self::SIZE - self.position, input.len());
self.bytes[self.position..][..len].copy_from_slice(&input[..len]);
self.position += len;
*input = &input[len..];
}
/// Take the output buffer, resetting the position to 0.
fn take(&mut self) -> [u8; Self::SIZE] {
debug_assert!(self.is_full());
let result = self.bytes;
*self = Default::default();
result
}
/// Is the buffer empty?
fn is_empty(&self) -> bool {
self.position == 0
}
/// Is the buffer full?
fn is_full(&self) -> bool {
self.position == Self::SIZE
}
}
#[cfg(test)]
mod tests {
use crate::{test_vectors::*, variant::Variant, Base64, Base64Unpadded, Encoder};
#[test]
fn encode_padded() {
encode_test::<Base64>(PADDED_BIN, PADDED_BASE64);
}
#[test]
fn encode_unpadded() {
encode_test::<Base64Unpadded>(UNPADDED_BIN, UNPADDED_BASE64);
}
/// Core functionality of an encoding test.
fn encode_test<V>(input: &[u8], expected: &str)
where
V: Variant,
{
for chunk_size in 1..input.len() {
let mut buffer = [0u8; 1024];
let mut encoder = Encoder::<V>::new(&mut buffer).unwrap();
for chunk in input.chunks(chunk_size) {
encoder.encode(chunk).unwrap();
}
assert_eq!(expected, encoder.finish().unwrap());
}
}
}