-
Notifications
You must be signed in to change notification settings - Fork 75
/
encoding.rs
394 lines (324 loc) · 12.1 KB
/
encoding.rs
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
use integer_encoding::VarInt;
/// This is the max required bytes to encode a u64 using the varint encoding scheme.
/// It is size 10=ceil(64/7)
pub const MAX_ENCODED_SIZE: usize = 10;
/// Encode a message, returning the bytes that must be sent before the message.
/// A buffer is used to avoid heap allocation.
pub fn encode_size<'a>(message: &[u8], buf: &'a mut [u8; MAX_ENCODED_SIZE]) -> &'a [u8] {
let varint_size = message.len().encode_var(buf);
&buf[..varint_size]
}
/// Decodes an encoded value in a buffer.
/// The function returns the message size and the consumed bytes or none if the buffer is too small.
pub fn decode_size(data: &[u8]) -> Option<(usize, usize)> {
usize::decode_var(data)
}
/// Used to decoded messages from several/partial data chunks
pub struct Decoder {
stored: Vec<u8>,
}
impl Default for Decoder {
/// Creates a new decoder.
/// It will only reserve memory in cases where decoding needs to keep data among messages.
fn default() -> Decoder {
Decoder { stored: Vec::new() }
}
}
impl Decoder {
fn try_decode(&mut self, data: &[u8], mut decoded_callback: impl FnMut(&[u8])) {
let mut next_data = data;
loop {
if let Some((expected_size, used_bytes)) = decode_size(next_data) {
let remaining = &next_data[used_bytes..];
if remaining.len() >= expected_size {
let (decoded, not_decoded) = remaining.split_at(expected_size);
decoded_callback(decoded);
if !not_decoded.is_empty() {
next_data = not_decoded;
continue
}
else {
break
}
}
}
self.stored.extend_from_slice(next_data);
break
}
}
fn store_and_decoded_data<'a>(&mut self, data: &'a [u8]) -> Option<(&[u8], &'a [u8])> {
// Process frame header
let ((expected_size, used_bytes), data) = match decode_size(&self.stored) {
Some(size_info) => (size_info, data),
None => {
// we append at most the potential data needed to decode the size
let max_remaining = (MAX_ENCODED_SIZE - self.stored.len()).min(data.len());
self.stored.extend_from_slice(&data[..max_remaining]);
if let Some(x) = decode_size(&self.stored) {
// Now we know the size
(x, &data[max_remaining..])
}
else {
// We still don't know the size (data was too small)
return None
}
}
};
// At this point we know at least the expected size of the frame.
let remaining = expected_size - (self.stored.len() - used_bytes);
if data.len() < remaining {
// We need more data to decoder
self.stored.extend_from_slice(data);
None
}
else {
// We can complete a message here
let (to_store, remaining) = data.split_at(remaining);
self.stored.extend_from_slice(to_store);
Some((&self.stored[used_bytes..], remaining))
}
}
/// Tries to decode data without reserve any memory, direcly from `data`.
/// `decoded_callback` will be called for each decoded message.
/// If `data` is not enough to decoding a message, the data will be stored
/// until more data is decoded (more successives calls to this function).
pub fn decode(&mut self, data: &[u8], mut decoded_callback: impl FnMut(&[u8])) {
if self.stored.is_empty() {
self.try_decode(data, decoded_callback);
}
else {
//There was already data in the Decoder
if let Some((decoded_data, remaining)) = self.store_and_decoded_data(data) {
decoded_callback(decoded_data);
self.stored.clear();
self.try_decode(remaining, decoded_callback);
}
}
}
/// Returns the bytes len stored in this decoder.
/// It can include both, the padding bytes and the data message bytes.
/// After decoding a message, its bytes are removed from the decoder.
pub fn stored_size(&self) -> usize {
self.stored.len()
}
}
#[cfg(test)]
mod tests {
use super::*;
const MESSAGE_SIZE: usize = 20; // only works if (X + PADDING ) % 6 == 0
const ENCODED_MESSAGE_SIZE: usize = 1 + MESSAGE_SIZE; // 1 = log_2(20)/7
const MESSAGE: [u8; MESSAGE_SIZE] = [42; MESSAGE_SIZE];
const MESSAGE_A: [u8; MESSAGE_SIZE] = ['A' as u8; MESSAGE_SIZE];
const MESSAGE_B: [u8; MESSAGE_SIZE] = ['B' as u8; MESSAGE_SIZE];
const MESSAGE_C: [u8; MESSAGE_SIZE] = ['C' as u8; MESSAGE_SIZE];
fn encode_message(buffer: &mut Vec<u8>, message: &[u8]) {
let mut buf = [0; MAX_ENCODED_SIZE];
buffer.extend_from_slice(&*encode_size(message, &mut buf));
buffer.extend_from_slice(message);
}
#[test]
fn encode_one_message() {
let mut buffer = Vec::new();
encode_message(&mut buffer, &MESSAGE);
assert_eq!(ENCODED_MESSAGE_SIZE, buffer.len());
let (expected_size, used_bytes) = decode_size(&buffer).unwrap();
assert_eq!(MESSAGE_SIZE, expected_size);
assert_eq!(used_bytes, 1);
assert_eq!(&MESSAGE, &buffer[used_bytes..]);
}
#[test]
fn encode_one_big_message() {
let mut buffer = Vec::new();
encode_message(&mut buffer, &vec![0; 1000]);
assert_eq!(1002, buffer.len());
let (expected_size, used_bytes) = decode_size(&buffer).unwrap();
assert_eq!(1000, expected_size);
assert_eq!(used_bytes, 2);
assert_eq!(&vec![0; 1000], &buffer[used_bytes..]);
}
#[test]
// [ data ]
// [message]
fn decode_one_message() {
let mut buffer = Vec::new();
encode_message(&mut buffer, &MESSAGE);
let mut decoder = Decoder::default();
let mut times_called = 0;
decoder.decode(&buffer, |decoded| {
times_called += 1;
assert_eq!(MESSAGE, decoded);
});
assert_eq!(1, times_called);
assert_eq!(0, decoder.stored.len());
}
#[test]
// [ 4B ]
// [ message ]
fn decode_message_no_size() {
let mut buffer = Vec::new();
encode_message(&mut buffer, &[]);
let mut decoder = Decoder::default();
let mut times_called = 0;
decoder.decode(&buffer, |_decoded| {
// Should not be called
times_called += 1;
});
assert_eq!(1, times_called);
assert_eq!(0, decoder.stored.len());
}
#[test]
// [ 5B ]
// [ message ]
fn decode_message_one_byte() {
let mut buffer = Vec::new();
encode_message(&mut buffer, &[0xFF]);
let mut decoder = Decoder::default();
let mut times_called = 0;
decoder.decode(&buffer, |decoded| {
times_called += 1;
assert_eq!([0xFF], decoded);
});
assert_eq!(1, times_called);
assert_eq!(0, decoder.stored.len());
}
#[test]
// [ data ]
// [message][message][message]
fn decode_multiple_messages_exact() {
let mut buffer = Vec::new();
let messages = [&MESSAGE_A, &MESSAGE_B, &MESSAGE_C];
encode_message(&mut buffer, messages[0]);
encode_message(&mut buffer, messages[1]);
encode_message(&mut buffer, messages[2]);
let mut decoder = Decoder::default();
let mut times_called = 0;
decoder.decode(&buffer, |decoded| {
assert_eq!(messages[times_called], decoded);
times_called += 1;
});
assert_eq!(3, times_called);
assert_eq!(0, decoder.stored.len());
}
#[test]
// [ data ][ data ]
// [ message ]
fn decode_one_message_in_two_parts() {
let mut buffer = Vec::new();
encode_message(&mut buffer, &MESSAGE);
const SPLIT: usize = ENCODED_MESSAGE_SIZE / 2;
let (first, second) = buffer.split_at(SPLIT);
let mut decoder = Decoder::default();
let mut times_called = 0;
decoder.decode(&first, |_decoded| {
// Should not be called
times_called += 1;
});
assert_eq!(0, times_called);
assert_eq!(SPLIT, decoder.stored.len());
decoder.decode(&second, |decoded| {
times_called += 1;
assert_eq!(MESSAGE, decoded);
});
assert_eq!(1, times_called);
assert_eq!(0, decoder.stored.len());
}
#[test]
// [ data ][ data ]
// [ message ][ message ]
fn decode_two_messages_in_two_parts() {
let mut buffer = Vec::new();
encode_message(&mut buffer, &MESSAGE);
encode_message(&mut buffer, &MESSAGE);
const SPLIT: usize = ENCODED_MESSAGE_SIZE * 2 / 3;
let (first, second) = buffer.split_at(SPLIT);
let mut decoder = Decoder::default();
let mut times_called = 0;
decoder.decode(&first, |_decoded| {
// Should not be called
times_called += 1;
});
assert_eq!(0, times_called);
assert_eq!(SPLIT, decoder.stored.len());
decoder.decode(&second, |decoded| {
times_called += 1;
assert_eq!(MESSAGE, decoded);
});
assert_eq!(2, times_called);
assert_eq!(0, decoder.stored.len());
}
#[test]
// [ 1B ][ 1B ][...][ 1B ]
// [ message ]
fn decode_byte_per_byte() {
let mut buffer = Vec::new();
encode_message(&mut buffer, &MESSAGE);
let mut decoder = Decoder::default();
let mut times_called = 0;
for i in 0..buffer.len() {
decoder.decode(&buffer[i..i + 1], |decoded| {
assert_eq!(buffer.len() - 1, i);
times_called += 1;
assert_eq!(MESSAGE, decoded);
});
if i < buffer.len() - 1 {
assert_eq!(i + 1, decoder.stored.len());
}
}
assert_eq!(0, decoder.stored.len());
assert_eq!(1, times_called);
}
#[test]
// [ 1B ][ remaining ]
// [ message ]
fn decode_message_after_non_enough_padding() {
let msg = [0; 1000];
let mut buffer = Vec::new();
encode_message(&mut buffer, &msg);
let (start_1b, remaining) = buffer.split_at(2);
let mut decoder = Decoder::default();
let mut times_called = 0;
decoder.decode(&start_1b, |_decoded| {
// Should not be called
times_called += 1;
});
assert_eq!(0, times_called);
assert_eq!(2, decoder.stored.len());
decoder.decode(&remaining, |decoded| {
times_called += 1;
assert_eq!(msg, decoded);
});
assert_eq!(1, times_called);
assert_eq!(0, decoder.stored.len());
}
#[test]
// [ 1B ][ 1B ][ remaining ]
// [ message ]
fn decode_message_var_size_in_two_data() {
let msg = [0; 1000];
let mut buffer = Vec::new();
encode_message(&mut buffer, &msg);
let (start_1b, remaining) = buffer.split_at(1);
let mut decoder = Decoder::default();
let mut times_called = 0;
decoder.decode(&start_1b, |_decoded| {
// Should not be called
times_called += 1;
});
assert_eq!(0, times_called);
assert_eq!(1, decoder.stored.len());
let (next_1b, remaining) = remaining.split_at(1);
let mut times_called = 0;
decoder.decode(&next_1b, |_decoded| {
// Should not be called
times_called += 1;
});
assert_eq!(0, times_called);
assert_eq!(2, decoder.stored.len());
decoder.decode(&remaining, |decoded| {
times_called += 1;
assert_eq!(msg, decoded);
});
assert_eq!(1, times_called);
assert_eq!(0, decoder.stored.len());
}
}