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
202
203
204
205
206
#[cfg(feature = "noise_sv2")]
use binary_sv2::Deserialize;
#[cfg(feature = "noise_sv2")]
use binary_sv2::GetSize;
use binary_sv2::Serialize;
use core::marker::PhantomData;
use framing_sv2::framing2::{EitherFrame, Frame as F_, Sv2Frame};
#[cfg(feature = "noise_sv2")]
use framing_sv2::framing2::{HandShakeFrame, NoiseFrame};
use framing_sv2::header::Header;
#[cfg(feature = "noise_sv2")]
use framing_sv2::header::NoiseHeader;
use crate::buffer::{Buffer, SlowAndCorrect};
use crate::error::{Error, Result};
#[cfg(feature = "noise_sv2")]
use crate::{State, TransportMode};
#[cfg(feature = "noise_sv2")]
pub type StandardNoiseDecoder<T> = WithNoise<SlowAndCorrect, T>;
pub type StandardEitherFrame<T> = EitherFrame<T, <SlowAndCorrect as Buffer>::Slice>;
pub type StandardSv2Frame<T> = Sv2Frame<T, <SlowAndCorrect as Buffer>::Slice>;
pub type StandardDecoder<T> = WithoutNoise<SlowAndCorrect, T>;
#[cfg(feature = "noise_sv2")]
pub struct WithNoise<B: Buffer, T: Serialize + binary_sv2::GetSize> {
frame: PhantomData<T>,
missing_noise_b: usize,
noise_buffer: B,
sv2_buffer: B,
sv2_frame_size: usize,
}
#[cfg(feature = "noise_sv2")]
impl<'a, T: Serialize + GetSize + Deserialize<'a>, B: Buffer> WithNoise<B, T> {
#[inline]
pub fn next_frame(&mut self, state: &mut State) -> Result<EitherFrame<T, B::Slice>> {
let len = self.noise_buffer.len();
let src = self.noise_buffer.get_data_by_ref(len);
let hint = NoiseFrame::size_hint(src) as usize;
match hint {
0 => {
self.missing_noise_b = NoiseHeader::SIZE;
self.decode_noise_frame(state)
}
_ => {
self.missing_noise_b = hint;
Err(Error::MissingBytes(hint))
}
}
}
#[inline]
fn decode_noise_frame(&mut self, state: &mut State) -> Result<EitherFrame<T, B::Slice>> {
match state {
State::Transport(transport_mode) => {
let src = self.noise_buffer.get_data_owned().as_mut().to_vec();
let mut noise_frame = NoiseFrame::from_bytes_unchecked(src);
let src = noise_frame.payload();
let len = TransportMode::size_hint_decrypt(src.len());
let mut decrypted = self.sv2_buffer.get_writable(len);
transport_mode.read(src, &mut decrypted).map_err(|_| ())?;
if self.sv2_frame_size > 0 {
return Ok(self.handle_fragmented().ok_or(())?);
};
let len = self.sv2_buffer.len();
let src = self.sv2_buffer.get_data_by_ref(len);
let hint = Sv2Frame::<T, B::Slice>::size_hint(src);
if hint == 0 {
let src = self.sv2_buffer.get_data_owned();
let frame = Sv2Frame::<T, B::Slice>::from_bytes_unchecked(src);
return Ok(frame.into());
}
self.handle_fragmented().ok_or(())?;
Err(Error::MissingBytes(self.missing_noise_b))
}
State::HandShake(_) => Ok(self.while_handshaking()),
State::NotInitialized => Ok(self.while_handshaking()),
}
}
#[inline(never)]
fn handle_fragmented(&mut self) -> Option<EitherFrame<T, B::Slice>> {
let len = self.sv2_buffer.len();
let src = self.sv2_buffer.get_data_by_ref(len);
let hint = Sv2Frame::<T, B::Slice>::size_hint(src);
if self.sv2_frame_size != 0 {
if hint == 0 {
let src = self.sv2_buffer.get_data_owned();
let frame = Sv2Frame::<T, B::Slice>::from_bytes_unchecked(src);
Some(frame.into())
} else {
self.missing_noise_b = NoiseHeader::SIZE;
None
}
} else {
self.sv2_frame_size = hint as usize;
self.missing_noise_b = NoiseHeader::SIZE;
None
}
}
fn while_handshaking(&mut self) -> EitherFrame<T, B::Slice> {
let src = self.noise_buffer.get_data_owned().as_mut().to_vec();
let frame = HandShakeFrame::from_bytes_unchecked(src);
frame.into()
}
#[inline]
pub fn writable(&mut self) -> &mut [u8] {
self.noise_buffer.get_writable(self.missing_noise_b)
}
}
#[cfg(feature = "noise_sv2")]
impl<T: Serialize + binary_sv2::GetSize> WithNoise<SlowAndCorrect, T> {
pub fn new() -> Self {
Self {
frame: PhantomData,
missing_noise_b: 0,
noise_buffer: SlowAndCorrect::new(),
sv2_buffer: SlowAndCorrect::new(),
sv2_frame_size: 0,
}
}
}
#[cfg(feature = "noise_sv2")]
impl<T: Serialize + binary_sv2::GetSize> Default for WithNoise<SlowAndCorrect, T> {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug)]
pub struct WithoutNoise<B: Buffer, T: Serialize + binary_sv2::GetSize> {
frame: PhantomData<T>,
missing_b: usize,
buffer: B,
}
impl<T: Serialize + binary_sv2::GetSize, B: Buffer> WithoutNoise<B, T> {
#[inline]
pub fn next_frame(&mut self) -> Result<Sv2Frame<T, B::Slice>> {
let len = self.buffer.len();
let src = self.buffer.get_data_by_ref(len);
let hint = Sv2Frame::<T, B::Slice>::size_hint(src) as usize;
match hint {
0 => {
self.missing_b = Header::SIZE;
let src = self.buffer.get_data_owned();
let frame = Sv2Frame::<T, B::Slice>::from_bytes_unchecked(src);
Ok(frame)
}
_ => {
self.missing_b = hint;
Err(Error::MissingBytes(self.missing_b))
}
}
}
pub fn writable(&mut self) -> &mut [u8] {
self.buffer.get_writable(self.missing_b)
}
}
impl<T: Serialize + binary_sv2::GetSize> WithoutNoise<SlowAndCorrect, T> {
pub fn new() -> Self {
Self {
frame: PhantomData,
missing_b: Header::SIZE,
buffer: SlowAndCorrect::new(),
}
}
}
impl<T: Serialize + binary_sv2::GetSize> Default for WithoutNoise<SlowAndCorrect, T> {
fn default() -> Self {
Self::new()
}
}