forked from datrs/compact-encoding
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
1278 lines (1168 loc) · 40.4 KB
/
lib.rs
File metadata and controls
1278 lines (1168 loc) · 40.4 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
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#![forbid(unsafe_code, missing_docs)]
#![cfg_attr(test, deny(warnings))]
//! # Series of compact encoding schemes for building small and fast parsers and serializers
//!
//! Binary compatible with the
//! [original JavaScript compact-encoding library](https://github.com/compact-encoding/compact-encoding/).
//!
//! ## Usage
//!
//! The simplest way to encoded and decode a some data is using the [`to_encoded_bytes`] and
//! [`map_decode`] macros:
//! ```
//! use compact_encoding::{map_decode, to_encoded_bytes};
//!
//! let number = 41_u32;
//! let word = "hi";
//!
//! let encoded_buffer = to_encoded_bytes!(number, word);
//! let ((number_dec, word_dec), remaining_buffer) = map_decode!(&encoded_buffer, [u32, String]);
//!
//! assert!(remaining_buffer.is_empty());
//! assert_eq!(number_dec, number);
//! assert_eq!(word_dec, word);
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```
//! ### Manual encoding and decoding
//!
//! When more fine-grained control of encoding and decoding is needed you manually do each step of
//! encoding and decoding like in the following example, where we want to use a fixed width
//! encoding for `number` (instead of the default variable width encoding). It shows how to
//! manually calculate the needed buffer size, create the buffer, encode data, and decode it.
//! ```
//! use compact_encoding::{CompactEncoding, FixedWidthEncoding, FixedWidthU32};
//!
//! let number = 41_u32;
//! let word = "hi";
//!
//! // Use `encoded_size` to figure out how big a buffer should be
//! let size = number.as_fixed_width().encoded_size()? + word.encoded_size()?;
//!
//! // Create a buffer with the calculated size
//! let mut buffer = vec![0; size];
//! assert_eq!(buffer.len(), 4 + 1 + 2);
//!
//! // Then actually encode the values
//! let mut remaining_buffer = number.as_fixed_width().encode(&mut buffer)?;
//! remaining_buffer = word.encode(remaining_buffer)?;
//! assert!(remaining_buffer.is_empty());
//! assert_eq!(buffer.to_vec(), vec![41_u8, 0, 0, 0, 2_u8, b'h', b'i']);
//!
//! // `buffer` now contains all the encoded data, and we can decode from it
//! let (number_dec, remaining_buffer) = FixedWidthU32::decode(&buffer)?;
//! let (word_dec, remaining_buffer) = String::decode(remaining_buffer)?;
//! assert!(remaining_buffer.is_empty());
//! assert_eq!(number_dec, number);
//! assert_eq!(word_dec, word);
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```
//!
//! ### Implementing CompactEncoding for custom types
//!
//! Here we demonstrate how to implement [`CompactEncoding`] for a custom struct.
//! ```
//! use compact_encoding::{
//! map_decode, map_encode, sum_encoded_size, to_encoded_bytes, CompactEncoding, EncodingError,
//! };
//!
//! #[derive(Debug, PartialEq)]
//! struct MyStruct {
//! some_flag: bool,
//! values: Option<Vec<[u8; 32]>>,
//! other: String,
//! stuff: u64,
//! }
//!
//! impl CompactEncoding for MyStruct {
//! fn encoded_size(&self) -> Result<usize, EncodingError> {
//! Ok(1 /* flags */ + {
//! /* handle option values */
//! if let Some(values) = &self.values {
//! values.encoded_size()?
//! } else {
//! 0
//! }
//! } + sum_encoded_size!(&self.other, &self.stuff))
//! }
//!
//! fn encode<'a>(&self, buffer: &'a mut [u8]) -> Result<&'a mut [u8], EncodingError> {
//! let mut flags: u8 = 0;
//! if self.some_flag {
//! flags |= 1;
//! }
//! if self.values.is_some() {
//! flags |= 2;
//! }
//! let mut rest = flags.encode(buffer)?;
//! if let Some(values) = &self.values {
//! rest = values.encode(rest)?;
//! }
//! Ok(map_encode!(rest, self.other, self.stuff))
//! }
//!
//! fn decode(buffer: &[u8]) -> Result<(Self, &[u8]), EncodingError> {
//! let (flags, rest) = u8::decode(buffer)?;
//! let some_flag: bool = flags & 1 != 0;
//! let (values, rest) = if flags & 2 != 0 {
//! let (vec, rest) = <Vec<[u8; 32]>>::decode(rest)?;
//! (Some(vec), rest)
//! } else {
//! (None, rest)
//! };
//! let ((other, stuff), rest) = map_decode!(rest, [String, u64]);
//! Ok((
//! Self {
//! some_flag,
//! values,
//! other,
//! stuff,
//! },
//! rest,
//! ))
//! }
//! }
//!
//! // Test values
//! let foo = MyStruct {
//! some_flag: false,
//! values: None,
//! other: "hi".to_string(),
//! stuff: 42,
//! };
//!
//! let bar = MyStruct {
//! some_flag: true,
//! values: Some(vec![[1; 32], [2; 32]]),
//! other: "yo".to_string(),
//! stuff: 0,
//! };
//!
//! // Encode `foo` and `bar` to a buffer
//! let buffer = to_encoded_bytes!(&foo, &bar);
//!
//! // With the above use of a flags byte, the empty value encodes to only one byte
//! assert_eq!(
//! buffer.len(),
//! // flags + string + u64
//! (1 + 3 + 1) +
//! // "" + (values.len().encoded_size() + (values.len() * <[u8;32]>::encoded_size()) + ""
//! (1 + (1 + (2 * 32)) + 3 + 1)
//! );
//!
//! // And decode directly to your own struct
//! let (foo_dec, rest) = MyStruct::decode(&buffer)?;
//! let (bar_dec, rest) = MyStruct::decode(&rest)?;
//! // Ensure all bytes were used
//! assert!(rest.is_empty());
//! assert_eq!(foo_dec, foo);
//! assert_eq!(bar_dec, bar);
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```
mod error;
mod fixedwidth;
pub use fixedwidth::{FixedWidthEncoding, FixedWidthU32, FixedWidthU64, FixedWidthUint};
use std::{
any::type_name,
net::{Ipv4Addr, Ipv6Addr, SocketAddrV4, SocketAddrV6},
};
pub use crate::error::{EncodingError, EncodingErrorKind};
/// indicates a variable width unsigned integer fits in u16
pub const U16_SIGNIFIER: u8 = 0xfd;
/// indicates a variable width unsigned integer fits in u32
pub const U32_SIGNIFIER: u8 = 0xfe;
/// indicates a variable width unsigned integer fits in u64
pub const U64_SIGNIFIER: u8 = 0xff;
const U16_SIZE: usize = 2;
const U32_SIZE: usize = 4;
const U64_SIZE: usize = 8;
/// Encoded size of a network port
pub const PORT_ENCODED_SIZE: usize = 2;
/// Encoded size of an ipv4 address
pub const IPV4_ADDR_ENCODED_SIZE: usize = U32_SIZE;
/// Encoded size of an ipv6 address
pub const IPV6_ADDR_ENCODED_SIZE: usize = 16;
/// Encoded size for a [`SocketAddrV4`], an ipv4 address plus port.
pub const SOCKET_ADDR_V4_ENCODED_SIZE: usize = IPV4_ADDR_ENCODED_SIZE + PORT_ENCODED_SIZE;
/// Encoded size for a [`SocketAddrV6`], an ipv6 address plus port.
pub const SOCKET_ADDR_V6_ENCODED_SIZE: usize = IPV6_ADDR_ENCODED_SIZE + PORT_ENCODED_SIZE;
/// A trait for building small and fast parsers and serializers.
pub trait CompactEncoding<Decode: ?Sized = Self> {
/// The size in bytes required to encode `self`.
fn encoded_size(&self) -> Result<usize, EncodingError>;
/// Encode `self` into `buffer` returning the remainder of `buffer`.
fn encode<'a>(&self, buffer: &'a mut [u8]) -> Result<&'a mut [u8], EncodingError>;
/// Decode a value from the given `buffer` of the type specified by the `Decode` type parameter
/// (which defaults to `Self`). Returns the decoded value and remaining undecoded bytes.
fn decode(buffer: &[u8]) -> Result<(Decode, &[u8]), EncodingError>
where
Decode: Sized;
/// Encode `self` into a `Vec<u8>`. This is just a helper method for creating a buffer and
/// encoding to it in one step.
/// ```
/// # use std::net::Ipv4Addr;
/// # use compact_encoding::CompactEncoding;
/// let foo: Ipv4Addr = "0.0.0.0".parse()?;
/// let mut buff = vec![0; foo.encoded_size()?];
/// foo.encode(&mut buff)?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
fn to_encoded_bytes(&self) -> Result<Box<[u8]>, EncodingError> {
let mut buff = self.create_buffer()?;
self.encode(&mut buff)?;
Ok(buff)
}
/// Create an empty buffer of the correct size for encoding `self` to. This is just a helper
/// method for: encoding to it in one step.
/// ```
/// # use std::net::Ipv4Addr;
/// # use compact_encoding::CompactEncoding;
/// let foo: Ipv4Addr = "0.0.0.0".parse()?;
/// vec![0; foo.encoded_size()?];
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
fn create_buffer(&self) -> Result<Box<[u8]>, EncodingError> {
Ok(vec![0; self.encoded_size()?].into_boxed_slice())
}
/// Like [`CompactEncoding::encode`] but also return the number of bytes encoded.
fn encode_with_len<'a>(
&self,
buffer: &'a mut [u8],
) -> Result<(&'a mut [u8], usize), EncodingError> {
let before_len = buffer.len();
let rest = self.encode(buffer)?;
let num_encoded_bytes = before_len - rest.len();
Ok((rest, num_encoded_bytes))
}
/// Like [`CompactEncoding::decode`] but also return the number of bytes decoded.
fn decode_with_len(buffer: &[u8]) -> Result<(Decode, &[u8], usize), EncodingError>
where
Decode: Sized,
{
let (out, rest) = Self::decode(buffer)?;
Ok((out, rest, buffer.len() - rest.len()))
}
}
/// Implement this for type `T` to have `CompactEncoding` implemented for `Vec<T>`
pub trait VecEncodable: CompactEncoding {
/// Calculate the resulting size in bytes of `vec`
fn vec_encoded_size(vec: &[Self]) -> Result<usize, EncodingError>
where
Self: Sized;
/// Encode `vec` to `buffer`
fn vec_encode<'a>(vec: &[Self], buffer: &'a mut [u8]) -> Result<&'a mut [u8], EncodingError>
where
Self: Sized,
{
encode_vec(vec, buffer)
}
/// Decode [`Vec<Self>`] from buffer
fn vec_decode(buffer: &[u8]) -> Result<(Vec<Self>, &[u8]), EncodingError>
where
Self: Sized,
{
decode_vec(buffer)
}
}
// NB: we DO want &Box<..> because we want the trait to work for boxed things, hence clippy::allow
#[allow(clippy::borrowed_box)]
/// Define this trait for `T` to get `impl Box<[T]> for CompactEncoding`
pub trait BoxedSliceEncodable: CompactEncoding {
/// The encoded size in bytes
fn boxed_slice_encoded_size(boxed: &Box<[Self]>) -> Result<usize, EncodingError>
where
Self: Sized;
/// Encode `Box<[Self]>` to the buffer and return the remainder of the buffer
fn boxed_slice_encode<'a>(
vec: &Box<[Self]>,
buffer: &'a mut [u8],
) -> Result<&'a mut [u8], EncodingError>
where
Self: Sized,
{
encode_vec(vec, buffer)
}
/// Decode [`Box<[Self]>`] from buffer
fn boxed_slice_decode(buffer: &[u8]) -> Result<(Box<[Self]>, &[u8]), EncodingError>
where
Self: Sized,
{
let (result, rest) = decode_vec(buffer)?;
Ok((result.into_boxed_slice(), rest))
}
}
#[macro_export]
/// Given a list of [`CompactEncoding`] things, sum the result of calling
/// [`CompactEncoding::encoded_size`] on all of them.
/// Note this is macro is useful when your arguments have differing types.
/// ```
/// # use crate::compact_encoding::{sum_encoded_size, CompactEncoding};
/// # use std::net::Ipv4Addr;
/// let foo: Ipv4Addr = "0.0.0.0".parse()?;
/// let bar = 42u64;
/// let qux = "hello?";
/// let result = sum_encoded_size!(foo, bar, qux);
/// assert_eq!(result, 12);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
/// If you want to use this within a non-result context you can do
/// ```
/// # use crate::compact_encoding::{sum_encoded_size, CompactEncoding, EncodingError};
/// let bar = 42u64;
/// let result = (|| Ok::<usize, EncodingError>(sum_encoded_size!(bar)))().unwrap();
/// assert_eq!(result, 1);
/// ```
macro_rules! sum_encoded_size {
($($val:expr),+) => {{
0
$(
+ $val.encoded_size()?
)*
}}
}
#[macro_export]
/// Given a list of [`CompactEncoding`] things, create a zeroed buffer of the correct size for encoding.
/// Note this is macro is useful when your arguments have differing types.
/// ```
/// # use crate::compact_encoding::{create_buffer, CompactEncoding};
/// # use std::net::Ipv4Addr;
/// let foo: Ipv4Addr = "0.0.0.0".parse()?;
/// let bar = 42u64;
/// let qux = "hello?";
/// let buff = create_buffer!(foo, bar, qux);
/// assert_eq!(buff.len(), 12);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
macro_rules! create_buffer {
($($val:expr),+) => {{
let len = (
0
$(
+ $val.encoded_size()?
)*
);
vec![0; len].into_boxed_slice()
}}
}
#[macro_export]
/// Given a buffer and a list of [`CompactEncoding`] things, encode the arguments to the buffer.
/// Note this is macro is useful when your arguments have differing types.
/// ```
/// # use crate::compact_encoding::{create_buffer, map_encode, CompactEncoding};
/// let num = 42u64;
/// let word = "yo";
/// let mut buff = create_buffer!(num, word);
/// let result = map_encode!(&mut buff, num, word);
/// assert!(result.is_empty());
/// assert_eq!(&*buff, &[42, 2, b'y', b'o']);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
macro_rules! map_encode {
($buffer:expr$(,)*) => {
$buffer
};
// Base case: single field
($buffer:expr, $field:expr) => {
$field.encode($buffer)?
};
// Recursive case: first field + rest
($buffer:expr, $first:expr, $($rest:expr),+) => {{
let rest = $first.encode($buffer)?;
map_encode!(rest, $($rest),+)
}};
}
#[macro_export]
/// Given a list of [`CompactEncoding`] things, encode the arguments to the buffer.
/// Note this is macro is useful when your arguments have differing types.
/// ```
/// # use crate::compact_encoding::to_encoded_bytes;
/// let result = to_encoded_bytes!(42u64, "yo");
/// assert_eq!(&*result, &[42, 2, 121, 111]);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
macro_rules! to_encoded_bytes {
($($val:expr),*) => {{
use $crate::{map_encode, create_buffer, CompactEncoding};
let mut buffer = create_buffer!($($val),*);
map_encode!(&mut buffer, $($val),*);
buffer
}}
}
#[macro_export]
/// Decode a buffer to the list of types provided, returning the remaining buffer.
/// It takes as arguments: `(&buffer, [type1, type2, type3, ...])`
/// And returns: `((decoded_type1, decoded_type2, ...), remaining_buffer)`
/// ```
/// # use crate::compact_encoding::{to_encoded_bytes, map_decode};
/// let buffer = to_encoded_bytes!(42u64, "yo");
/// let ((number, word), remaining_buffer) = map_decode!(&buffer, [u64, String]);
/// assert!(remaining_buffer.is_empty());
/// assert_eq!(number, 42u64);
/// assert_eq!(word, "yo");
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
macro_rules! map_decode {
($buffer:expr, [
$($field_type:ty),* $(,)?
]) => {{
use $crate::CompactEncoding;
let mut current_buffer: &[u8] = $buffer;
// Decode each type into `result_tuple`
let result_tuple = (
$(
match <$field_type>::decode(¤t_buffer)? {
(value, new_buf) => {
current_buffer = new_buf;
value
}
},
)*
);
(result_tuple, current_buffer)
}};
}
#[macro_export]
/// Helper for mapping the first element of a two eleent tuple.
/// This is useful for cleanly handling the result of CompactEncoding::decode.
macro_rules! map_first {
($res:expr, $f:expr) => {{
let (one, two) = $res;
let mapped = $f(one);
(mapped, two)
}};
}
#[macro_export]
/// like [`map_first`] but the mapping should return a result.
macro_rules! map_first_result {
($res:expr, $f:expr) => {{
let (one, two) = $res;
let mapped = $f(one)?;
(mapped, two)
}};
}
/// Split a slice in two at `mid`. Returns encoding error when `mid` is out of bounds.
pub fn get_slices_checked(buffer: &[u8], mid: usize) -> Result<(&[u8], &[u8]), EncodingError> {
buffer.split_at_checked(mid).ok_or_else(|| {
EncodingError::out_of_bounds(&format!(
"Could not split slice at [{mid}] slice.len() = [{}]",
buffer.len()
))
})
}
/// Split a mutable slice into two mutable slices around `mid`.
/// Returns encoding error when `mid` is out of bounds.
pub fn get_slices_mut_checked(
buffer: &mut [u8],
mid: usize,
) -> Result<(&mut [u8], &mut [u8]), EncodingError> {
let len = buffer.len();
buffer.split_at_mut_checked(mid).ok_or_else(|| {
EncodingError::out_of_bounds(&format!(
"Could not split mut slice at [{mid}] slice.len() = [{len}]"
))
})
}
/// Get a slice as an array of size `N`. Errors when `slice.len() != N`.
pub fn as_array<const N: usize>(buffer: &[u8]) -> Result<&[u8; N], EncodingError> {
let blen = buffer.len();
if blen != N {
return Err(EncodingError::out_of_bounds(&format!(
"Could get a [{N}] byte array from a slice of length [{blen}]"
)));
}
Ok(buffer.split_first_chunk::<N>().expect("checked above").0)
}
/// Get a slice as a mutable array of size `N`. Errors when `slice.len() != N`.
pub fn as_array_mut<const N: usize>(buffer: &mut [u8]) -> Result<&mut [u8; N], EncodingError> {
let blen = buffer.len();
if blen != N {
return Err(EncodingError::out_of_bounds(&format!(
"Could get a [{N}] byte array from a slice of length [{blen}]"
)));
}
Ok(buffer
.split_first_chunk_mut::<N>()
.expect("checked above")
.0)
}
/// Write `source` to `buffer` and return the remainder of `buffer`.
/// Errors when `N < buffer.len()`
pub fn write_array<'a, const N: usize>(
source: &[u8; N],
buffer: &'a mut [u8],
) -> std::result::Result<&'a mut [u8], EncodingError> {
let blen = buffer.len();
let Some((dest, rest)) = buffer.split_first_chunk_mut::<N>() else {
return Err(EncodingError::out_of_bounds(&format!(
"Could not write [{}] bytes to buffer of length [{}]",
N, blen
)));
};
dest.copy_from_slice(source);
Ok(rest)
}
/// split the first `N` bytes of `buffer` off and return them
pub fn take_array<const N: usize>(
buffer: &[u8],
) -> std::result::Result<([u8; N], &[u8]), EncodingError> {
let Some((out, rest)) = buffer.split_first_chunk::<N>() else {
return Err(EncodingError::out_of_bounds(&format!(
"Could not take [{}] bytes from buffer of length [{}]",
N,
buffer.len()
)));
};
Ok((*out, rest))
}
/// split the first `N` bytes of `buffer` off and return them
pub fn take_array_mut<const N: usize>(
buffer: &mut [u8],
) -> std::result::Result<(&mut [u8; N], &mut [u8]), EncodingError> {
let blen = buffer.len();
let Some((out, rest)) = buffer.split_first_chunk_mut::<N>() else {
return Err(EncodingError::out_of_bounds(&format!(
"Could not write [{}] bytes to buffer of length [{blen}]",
N,
)));
};
Ok((out, rest))
}
/// write `source` to `buffer` and return remaining buffer
pub fn write_slice<'a>(source: &[u8], buffer: &'a mut [u8]) -> Result<&'a mut [u8], EncodingError> {
let mid = source.len();
let (dest, rest) = get_slices_mut_checked(buffer, mid)?;
dest.copy_from_slice(source);
Ok(rest)
}
/// Helper to convert a vec to an array, and fail with an encoding error when needed
pub fn bytes_fixed_from_vec<const N: usize>(value: &[u8]) -> Result<[u8; N], EncodingError> {
<[u8; N]>::try_from(value).map_err(|e| {
EncodingError::invalid_data(&format!(
"Could not covert slice with length [{}] to array of length [{}]. Error: [{e}]",
value.len(),
N
))
})
}
fn encoded_size_str(value: &str) -> Result<usize, EncodingError> {
Ok(encoded_size_usize(value.len()) + value.len())
}
/// The number of bytes required to encode this number. Note this is always variable width.
pub fn encoded_size_usize(val: usize) -> usize {
if val < U16_SIGNIFIER.into() {
1
} else if val <= 0xffff {
3
} else if val <= 0xffffffff {
5
} else {
9
}
}
/// The number of bytes required to encode this number.
/// We only need this for u64 because all other uints can be converted to usize reliably.
pub fn encoded_size_var_u64(val: u64) -> usize {
if val < U16_SIGNIFIER.into() {
1
} else if val <= 0xffff {
3
} else if val <= 0xffffffff {
5
} else {
9
}
}
/// Write `uint` to the start of `buffer` and return the remaining part of `buffer`.
pub fn encode_var_u64(uint: u64, buffer: &mut [u8]) -> Result<&mut [u8], EncodingError> {
if uint < U16_SIGNIFIER.into() {
encode_u8(uint as u8, buffer)
} else if uint <= 0xffff {
let rest = write_array(&[U16_SIGNIFIER], buffer)?;
encode_u16(uint as u16, rest)
} else if uint <= 0xffffffff {
let rest = write_array(&[U32_SIGNIFIER], buffer)?;
encode_u32(uint as u32, rest)
} else {
let rest = write_array(&[U64_SIGNIFIER], buffer)?;
encode_u64(uint, rest)
}
}
/// Decode a `usize` from `buffer` and return the remaining bytes.
/// This will fail, when we are decoding a `usize` on a usize = u32 machine for data that was originally encoded on a `usize = u64` machine whenever the value is over `u32::MAX`.
pub fn decode_usize(buffer: &[u8]) -> Result<(usize, &[u8]), EncodingError> {
let ([first], rest) = take_array::<1>(buffer)?;
Ok(match first {
x if x < U16_SIGNIFIER => (x.into(), rest),
U16_SIGNIFIER => map_first!(decode_u16(rest)?, |x: u16| x.into()),
U32_SIGNIFIER => {
map_first_result!(decode_u32(rest)?, |val| usize::try_from(val)
.map_err(|_| EncodingError::overflow("Could not convert u32 to usize")))
}
_ => {
map_first_result!(decode_u64(rest)?, |val| usize::try_from(val)
.map_err(|_| EncodingError::overflow("Could not convert u64 to usize")))
}
})
}
/// Encoded a fixed sized array to a buffer, return the remainder of the buffer.
/// Errors when `value.len() > buffer.len()`;
/// ```
/// # use compact_encoding::encode_bytes_fixed;
/// let mut buffer = vec![0; 3];
/// let rest = encode_bytes_fixed(&[4, 2], &mut buffer)?;
/// assert_eq!(rest, &[0]);
/// assert_eq!(buffer, &[4, 2, 0]);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn encode_bytes_fixed<'a, const N: usize>(
value: &[u8; N],
buffer: &'a mut [u8],
) -> Result<&'a mut [u8], EncodingError> {
write_array(value, buffer)
}
/// Decode a fixed sized array from a buffer. Return the array and the remainder of the buffer.
/// Errors when `buffer.len() < N`;
/// ```
/// # use compact_encoding::decode_bytes_fixed;
/// let mut buffer = vec![1, 2, 3];
/// let (arr, rest) = decode_bytes_fixed::<2>(&mut buffer)?;
/// assert_eq!(arr, [1, 2]);
/// assert_eq!(rest, &[3]);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub fn decode_bytes_fixed<const N: usize>(
buffer: &[u8],
) -> Result<([u8; N], &[u8]), EncodingError> {
take_array(buffer)
//write_array(value, buffer)
}
fn decode_u16(buffer: &[u8]) -> Result<(u16, &[u8]), EncodingError> {
let (data, rest) = take_array::<2>(buffer)?;
Ok((u16::from_le_bytes(data), rest))
}
fn decode_u32(buffer: &[u8]) -> Result<(u32, &[u8]), EncodingError> {
let (data, rest) = take_array::<4>(buffer)?;
Ok((u32::from_le_bytes(data), rest))
}
fn decode_u64(buffer: &[u8]) -> Result<(u64, &[u8]), EncodingError> {
let (data, rest) = take_array::<8>(buffer)?;
Ok((u64::from_le_bytes(data), rest))
}
fn decode_u32_var(buffer: &[u8]) -> Result<(u32, &[u8]), EncodingError> {
let ([first], rest) = take_array::<1>(buffer)?;
Ok(match first {
x if x < U16_SIGNIFIER => (x.into(), rest),
U16_SIGNIFIER => {
let (val, rest) = decode_u16(rest)?;
(val.into(), rest)
}
_ => decode_u32(rest)?,
})
}
fn decode_u64_var(buffer: &[u8]) -> Result<(u64, &[u8]), EncodingError> {
let ([first], rest) = take_array::<1>(buffer)?;
Ok(match first {
x if x < U16_SIGNIFIER => (x.into(), rest),
U16_SIGNIFIER => map_first!(decode_u16(rest)?, |x: u16| x.into()),
U32_SIGNIFIER => map_first!(decode_u32(rest)?, |x: u32| x.into()),
_ => decode_u64(rest)?,
})
}
fn decode_buffer_vec(buffer: &[u8]) -> Result<(Vec<u8>, &[u8]), EncodingError> {
let (n_bytes, rest) = decode_usize(buffer)?;
let (out, rest) = get_slices_checked(rest, n_bytes)?;
Ok((out.to_vec(), rest))
}
fn decode_string(buffer: &[u8]) -> Result<(String, &[u8]), EncodingError> {
let (len, rest) = decode_usize(buffer)?;
let (str_buff, rest) = get_slices_checked(rest, len)?;
let out = String::from_utf8(str_buff.to_vec())
.map_err(|e| EncodingError::invalid_data(&format!("String is invalid UTF-8, {e}")))?;
Ok((out, rest))
}
fn encode_u8(val: u8, buffer: &mut [u8]) -> Result<&mut [u8], EncodingError> {
write_array(&val.to_le_bytes(), buffer)
}
fn encode_u16(val: u16, buffer: &mut [u8]) -> Result<&mut [u8], EncodingError> {
write_array(&val.to_le_bytes(), buffer)
}
fn encode_u32(val: u32, buffer: &mut [u8]) -> Result<&mut [u8], EncodingError> {
write_array(&val.to_le_bytes(), buffer)
}
fn encode_u64(val: u64, buffer: &mut [u8]) -> Result<&mut [u8], EncodingError> {
write_array(&val.to_le_bytes(), buffer)
}
/// Encode a `usize` in a variable width way
pub fn encode_usize_var<'a>(
value: &usize,
buffer: &'a mut [u8],
) -> Result<&'a mut [u8], EncodingError> {
if *value < U16_SIGNIFIER.into() {
encode_u8(*value as u8, buffer)
} else if *value <= 0xffff {
encode_u16(*value as u16, write_array(&[U16_SIGNIFIER], buffer)?)
} else if *value <= 0xffffffff {
let value = u32::try_from(*value).map_err(|e| {
EncodingError::overflow(&format!(
"count not covert usize [{value}] to u32. Error: [{e}]"
))
})?;
encode_u32(value, write_array(&[U32_SIGNIFIER], buffer)?)
} else {
let value = u64::try_from(*value).map_err(|e| {
EncodingError::overflow(&format!(
"count not covert usize [{value}] to u64. Error: [{e}]"
))
})?;
encode_u64(value, write_array(&[U64_SIGNIFIER], buffer)?)
}
}
fn encode_str<'a>(value: &str, buffer: &'a mut [u8]) -> Result<&'a mut [u8], EncodingError> {
let rest = encode_usize_var(&value.len(), buffer)?;
write_slice(value.as_bytes(), rest)
}
fn encode_buffer<'a>(value: &[u8], buffer: &'a mut [u8]) -> Result<&'a mut [u8], EncodingError> {
let rest = encode_usize_var(&value.len(), buffer)?;
write_slice(value, rest)
}
impl<const N: usize> CompactEncoding for [u8; N] {
fn encoded_size(&self) -> Result<usize, EncodingError> {
Ok(N)
}
fn encode<'a>(&self, buffer: &'a mut [u8]) -> Result<&'a mut [u8], EncodingError> {
write_array(self, buffer)
}
fn decode(buffer: &[u8]) -> Result<(Self, &[u8]), EncodingError>
where
Self: Sized,
{
take_array(buffer)
}
}
impl CompactEncoding for u8 {
fn encoded_size(&self) -> Result<usize, EncodingError> {
Ok(1)
}
fn encode<'a>(&self, buffer: &'a mut [u8]) -> Result<&'a mut [u8], EncodingError> {
write_array(&[*self], buffer)
}
fn decode(buffer: &[u8]) -> Result<(Self, &[u8]), EncodingError>
where
Self: Sized,
{
let ([out], rest) = take_array::<1>(buffer)?;
Ok((out, rest))
}
}
impl CompactEncoding for u16 {
fn encoded_size(&self) -> Result<usize, EncodingError> {
Ok(U16_SIZE)
}
fn encode<'a>(&self, buffer: &'a mut [u8]) -> Result<&'a mut [u8], EncodingError> {
encode_u16(*self, buffer)
}
fn decode(buffer: &[u8]) -> Result<(Self, &[u8]), EncodingError>
where
Self: Sized,
{
decode_u16(buffer)
}
}
// NB: we want u32 encoded and decoded as variable sized uint
impl CompactEncoding for u32 {
fn encoded_size(&self) -> Result<usize, EncodingError> {
Ok(encoded_size_usize(*self as usize))
}
fn encode<'a>(&self, buffer: &'a mut [u8]) -> Result<&'a mut [u8], EncodingError> {
encode_usize_var(&(*self as usize), buffer)
}
fn decode(buffer: &[u8]) -> Result<(Self, &[u8]), EncodingError>
where
Self: Sized,
{
decode_u32_var(buffer)
}
}
impl CompactEncoding for u64 {
fn encoded_size(&self) -> Result<usize, EncodingError> {
Ok(encoded_size_var_u64(*self))
}
fn encode<'a>(&self, buffer: &'a mut [u8]) -> Result<&'a mut [u8], EncodingError> {
encode_var_u64(*self, buffer)
}
fn decode(buffer: &[u8]) -> Result<(Self, &[u8]), EncodingError>
where
Self: Sized,
{
decode_u64_var(buffer)
}
}
impl CompactEncoding for usize {
fn encoded_size(&self) -> Result<usize, EncodingError> {
Ok(encoded_size_usize(*self))
}
fn encode<'a>(&self, buffer: &'a mut [u8]) -> Result<&'a mut [u8], EncodingError> {
encode_usize_var(self, buffer)
}
fn decode(buffer: &[u8]) -> Result<(Self, &[u8]), EncodingError>
where
Self: Sized,
{
decode_usize(buffer)
}
}
impl CompactEncoding for String {
fn encoded_size(&self) -> Result<usize, EncodingError> {
encoded_size_str(self)
}
fn encode<'a>(&self, buffer: &'a mut [u8]) -> Result<&'a mut [u8], EncodingError> {
encode_str(self, buffer)
}
fn decode(buffer: &[u8]) -> Result<(Self, &[u8]), EncodingError>
where
Self: Sized,
{
decode_string(buffer)
}
}
impl CompactEncoding<String> for str {
fn encoded_size(&self) -> Result<usize, EncodingError> {
encoded_size_str(self)
}
fn encode<'a>(&self, buffer: &'a mut [u8]) -> Result<&'a mut [u8], EncodingError> {
encode_str(self, buffer)
}
fn decode(buffer: &[u8]) -> Result<(String, &[u8]), EncodingError> {
decode_string(buffer)
}
}
impl CompactEncoding for Vec<String> {
fn encoded_size(&self) -> Result<usize, EncodingError> {
let mut out = encoded_size_usize(self.len());
for s in self {
out += s.encoded_size()?;
}
Ok(out)
}
fn encode<'a>(&self, buffer: &'a mut [u8]) -> Result<&'a mut [u8], EncodingError> {
let mut rest = encode_usize_var(&self.len(), buffer)?;
for s in self {
rest = s.encode(rest)?;
}
Ok(rest)
}
fn decode(buffer: &[u8]) -> Result<(Self, &[u8]), EncodingError>
where
Self: Sized,
{
let (len, mut rest) = decode_usize(buffer)?;
let mut out = Vec::with_capacity(len);
for _ in 0..len {
let result = String::decode(rest)?;
out.push(result.0);
rest = result.1;
}
Ok((out, rest))
}
}
impl CompactEncoding for Vec<u8> {
fn encoded_size(&self) -> Result<usize, EncodingError> {
Ok(encoded_size_usize(self.len()) + self.len())
}
fn encode<'a>(&self, buffer: &'a mut [u8]) -> Result<&'a mut [u8], EncodingError> {
encode_buffer(self, buffer)
}
fn decode(buffer: &[u8]) -> Result<(Self, &[u8]), EncodingError>
where
Self: Sized,
{
decode_buffer_vec(buffer)
}
}
impl CompactEncoding for Ipv4Addr {
fn encoded_size(&self) -> std::result::Result<usize, EncodingError> {
Ok(U32_SIZE)
}
fn encode<'a>(&self, buffer: &'a mut [u8]) -> std::result::Result<&'a mut [u8], EncodingError> {
let Some((dest, rest)) = buffer.split_first_chunk_mut::<4>() else {
return Err(EncodingError::out_of_bounds(&format!(
"Colud not encode {}, not enough room in buffer",
type_name::<Self>()
)));
};
dest.copy_from_slice(&self.octets());
Ok(rest)
}
fn decode(buffer: &[u8]) -> std::result::Result<(Self, &[u8]), EncodingError>
where
Self: Sized,
{
let Some((dest, rest)) = buffer.split_first_chunk::<4>() else {
return Err(EncodingError::out_of_bounds(&format!(
"Colud not decode {}, buffer not big enough",
type_name::<Self>()
)));
};
Ok((Ipv4Addr::from(*dest), rest))
}
}
impl CompactEncoding for Ipv6Addr {
fn encoded_size(&self) -> std::result::Result<usize, EncodingError> {
Ok(IPV6_ADDR_ENCODED_SIZE)
}
/// ```
/// # use std::net::Ipv6Addr;
/// # use compact_encoding::CompactEncoding;
/// let addr: Ipv6Addr = "1:2:3::1".parse()?;
/// let buff = addr.to_encoded_bytes()?.to_vec();
/// assert_eq!(buff, vec![0, 1, 0, 2, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]);
/// # Ok::<(), Box<dyn std::error::Error>>(())