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
193
194
195
196
197
198
199
200
201
use crate::{
frame::{FitError, Tag},
varint::VarInt,
};
use core::{convert::TryFrom, mem::size_of};
use s2n_codec::{
decoder_parameterized_value, DecoderBuffer, DecoderBufferMut, Encoder, EncoderValue,
};
macro_rules! crypto_tag {
() => {
0x06u8
};
}
#[derive(Debug, PartialEq, Eq)]
pub struct Crypto<Data> {
pub offset: VarInt,
pub data: Data,
}
impl<Data> Crypto<Data> {
pub const fn tag(&self) -> u8 {
crypto_tag!()
}
pub fn map_data<F: FnOnce(Data) -> Out, Out>(self, map: F) -> Crypto<Out> {
Crypto {
offset: self.offset,
data: map(self.data),
}
}
}
impl<Data: EncoderValue> Crypto<Data> {
pub fn try_fit(&self, capacity: usize) -> Result<usize, FitError> {
let mut fixed_len = 0;
fixed_len += size_of::<Tag>();
fixed_len += self.offset.encoding_size();
let remaining_capacity = capacity.checked_sub(fixed_len).ok_or(FitError)?;
let data_len = self.data.encoding_size();
let max_data_len = remaining_capacity.min(data_len);
let len_prefix_size = VarInt::try_from(max_data_len)
.map_err(|_| FitError)?
.encoding_size();
let prefixed_data_len = remaining_capacity
.checked_sub(len_prefix_size)
.ok_or(FitError)?;
let data_len = prefixed_data_len.min(data_len);
Ok(data_len)
}
}
pub type CryptoRef<'a> = Crypto<&'a [u8]>;
pub type CryptoMut<'a> = Crypto<&'a mut [u8]>;
decoder_parameterized_value!(
impl<'a, Data> Crypto<Data> {
fn decode(_tag: Tag, buffer: Buffer) -> Result<Self> {
let (offset, buffer) = buffer.decode()?;
let (data, buffer) = buffer.decode_with_len_prefix::<VarInt, Data>()?;
let frame = Crypto { offset, data };
Ok((frame, buffer))
}
}
);
impl<Data: EncoderValue> EncoderValue for Crypto<Data> {
fn encode<E: Encoder>(&self, buffer: &mut E) {
buffer.encode(&self.tag());
buffer.encode(&self.offset);
buffer.encode_with_len_prefix::<VarInt, _>(&self.data);
}
}
impl<'a> From<Crypto<DecoderBuffer<'a>>> for CryptoRef<'a> {
fn from(s: Crypto<DecoderBuffer<'a>>) -> Self {
s.map_data(|data| data.into_less_safe_slice())
}
}
impl<'a> From<Crypto<DecoderBufferMut<'a>>> for CryptoRef<'a> {
fn from(s: Crypto<DecoderBufferMut<'a>>) -> Self {
s.map_data(|data| &*data.into_less_safe_slice())
}
}
impl<'a> From<Crypto<DecoderBufferMut<'a>>> for CryptoMut<'a> {
fn from(s: Crypto<DecoderBufferMut<'a>>) -> Self {
s.map_data(|data| data.into_less_safe_slice())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::frame::Padding;
use bolero::check;
use core::convert::TryInto;
fn model(offset: VarInt, length: VarInt, capacity: usize) {
let length = if let Ok(length) = VarInt::try_into(length) {
length
} else {
return;
};
let mut frame = Crypto {
offset,
data: Padding { length },
};
if let Ok(new_length) = frame.try_fit(capacity) {
frame.data = Padding { length: new_length };
assert!(
frame.encoding_size() <= capacity,
"the encoding_size should not exceed capacity {:#?}",
frame
);
if new_length < length {
let tolerance = VarInt::try_from(new_length).unwrap().encoding_size();
assert!(
capacity - frame.encoding_size() <= tolerance,
"should fit capacity tolerance: expected {}, got {}; {:#?}",
tolerance,
capacity - frame.encoding_size(),
frame,
);
}
} else {
assert!(
frame.encoding_size() > capacity,
"rejection should only occur when encoding size > capacity {:#?}",
frame
);
}
}
#[test]
fn try_fit_test() {
check!()
.with_type()
.cloned()
.for_each(|(offset, length, capacity)| {
model(offset, length, capacity);
});
}
}