Skip to main content

tidecoin_consensus_encoding/decode/
decoders.rs

1// SPDX-License-Identifier: CC0-1.0
2
3//! Primitive decoders.
4
5#[cfg(feature = "alloc")]
6use alloc::vec::Vec;
7use core::{fmt, mem};
8
9#[cfg(feature = "alloc")]
10use super::Decodable;
11use super::Decoder;
12#[cfg(feature = "alloc")]
13use crate::compact_size::CompactSizeDecoder;
14#[cfg(feature = "alloc")]
15use crate::error::{
16    ByteVecDecoderError, ByteVecDecoderErrorInner, VecDecoderError, VecDecoderErrorInner,
17};
18use crate::{Decoder2Error, Decoder3Error, Decoder4Error, Decoder6Error, UnexpectedEofError};
19
20/// Maximum amount of memory (in bytes) to allocate at once when deserializing vectors.
21#[cfg(feature = "alloc")]
22const MAX_VECTOR_ALLOCATE: usize = 1_000_000;
23
24/// A decoder that decodes a byte vector.
25///
26/// The encoding is expected to start with the number of encoded bytes (length prefix).
27#[cfg(feature = "alloc")]
28#[derive(Debug, Clone)]
29pub struct ByteVecDecoder {
30    prefix_decoder: Option<CompactSizeDecoder>,
31    buffer: Vec<u8>,
32    bytes_expected: usize,
33    bytes_written: usize,
34}
35
36#[cfg(feature = "alloc")]
37impl ByteVecDecoder {
38    /// Constructs a new byte decoder.
39    pub const fn new() -> Self {
40        Self {
41            prefix_decoder: Some(CompactSizeDecoder::new()),
42            buffer: Vec::new(),
43            bytes_expected: 0,
44            bytes_written: 0,
45        }
46    }
47
48    /// Reserves capacity for byte vectors in batches.
49    ///
50    /// Reserves up to `MAX_VECTOR_ALLOCATE` bytes when the buffer has no remaining capacity.
51    ///
52    /// For `DoS` prevention, do not blindly allocate as much as the stream claims to contain.
53    /// Instead, allocate in batches, so that an attacker actually needs to provide X MB of
54    /// data to make us allocate roughly X MB of memory.
55    fn reserve(&mut self) {
56        if self.buffer.len() == self.buffer.capacity() {
57            let bytes_remaining = self.bytes_expected - self.bytes_written;
58            let batch_size = bytes_remaining.min(MAX_VECTOR_ALLOCATE);
59            self.buffer.reserve_exact(batch_size);
60        }
61    }
62}
63
64#[cfg(feature = "alloc")]
65impl Default for ByteVecDecoder {
66    fn default() -> Self {
67        Self::new()
68    }
69}
70
71#[cfg(feature = "alloc")]
72impl Decoder for ByteVecDecoder {
73    type Output = Vec<u8>;
74    type Error = ByteVecDecoderError;
75
76    fn push_bytes(&mut self, bytes: &mut &[u8]) -> Result<bool, Self::Error> {
77        use ByteVecDecoderError as E;
78        use ByteVecDecoderErrorInner as Inner;
79
80        if let Some(mut decoder) = self.prefix_decoder.take() {
81            if decoder.push_bytes(bytes).map_err(|e| E(Inner::LengthPrefixDecode(e)))? {
82                self.prefix_decoder = Some(decoder);
83                return Ok(true);
84            }
85            self.bytes_expected = decoder.end().map_err(|e| E(Inner::LengthPrefixDecode(e)))?;
86            self.prefix_decoder = None;
87
88            // For DoS prevention, let's not allocate all memory upfront.
89        }
90
91        self.reserve();
92
93        let remaining = self.bytes_expected - self.bytes_written;
94        let available_capacity = self.buffer.capacity() - self.buffer.len();
95        let copy_len = bytes.len().min(remaining).min(available_capacity);
96
97        self.buffer.extend_from_slice(&bytes[..copy_len]);
98        self.bytes_written += copy_len;
99        *bytes = &bytes[copy_len..];
100
101        // Return true if we still need more data.
102        Ok(self.bytes_written < self.bytes_expected)
103    }
104
105    fn end(self) -> Result<Self::Output, Self::Error> {
106        use ByteVecDecoderError as E;
107        use ByteVecDecoderErrorInner as Inner;
108
109        if let Some(ref prefix_decoder) = self.prefix_decoder {
110            return Err(E(Inner::UnexpectedEof(UnexpectedEofError {
111                missing: prefix_decoder.read_limit(),
112            })));
113        }
114
115        if self.bytes_written == self.bytes_expected {
116            Ok(self.buffer)
117        } else {
118            Err(E(Inner::UnexpectedEof(UnexpectedEofError {
119                missing: self.bytes_expected - self.bytes_written,
120            })))
121        }
122    }
123
124    fn read_limit(&self) -> usize {
125        if let Some(prefix_decoder) = &self.prefix_decoder {
126            prefix_decoder.read_limit()
127        } else {
128            self.bytes_expected - self.bytes_written
129        }
130    }
131}
132
133/// A decoder that decodes a vector of `T`s.
134///
135/// The decoding is expected to start with expected number of items in the vector.
136#[cfg(feature = "alloc")]
137pub struct VecDecoder<T: Decodable> {
138    prefix_decoder: Option<CompactSizeDecoder>,
139    length: usize,
140    buffer: Vec<T>,
141    decoder: Option<<T as Decodable>::Decoder>,
142}
143
144#[cfg(feature = "alloc")]
145impl<T: Decodable> fmt::Debug for VecDecoder<T>
146where
147    T::Decoder: fmt::Debug,
148{
149    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
150        f.debug_struct("VecDecoder")
151            .field("prefix_decoder", &self.prefix_decoder)
152            .field("length", &self.length)
153            // Print the count rather than contents to avoid requiring `T: Debug`.
154            .field("buffer_len", &self.buffer.len())
155            .field("decoder", &self.decoder)
156            .finish()
157    }
158}
159
160#[cfg(feature = "alloc")]
161impl<T: Decodable> Clone for VecDecoder<T>
162where
163    T: Clone,
164    T::Decoder: Clone,
165{
166    fn clone(&self) -> Self {
167        Self {
168            prefix_decoder: self.prefix_decoder.clone(),
169            length: self.length,
170            buffer: self.buffer.clone(),
171            decoder: self.decoder.clone(),
172        }
173    }
174}
175
176#[cfg(feature = "alloc")]
177impl<T: Decodable> VecDecoder<T> {
178    /// Constructs a new byte decoder.
179    pub const fn new() -> Self {
180        Self {
181            prefix_decoder: Some(CompactSizeDecoder::new()),
182            length: 0,
183            buffer: Vec::new(),
184            decoder: None,
185        }
186    }
187
188    /// Reserves capacity for typed vectors in batches.
189    ///
190    /// Calculates how many elements of type `T` fit within `MAX_VECTOR_ALLOCATE` bytes and reserves
191    /// up to that amount when the buffer reaches capacity.
192    ///
193    /// For `DoS` prevention, do not blindly allocate as much as the stream claims to contain.
194    /// Instead, allocate in batches, so that an attacker actually needs to provide X MB of
195    /// data to make us allocate roughly X MB of memory.
196    fn reserve(&mut self) {
197        if self.buffer.len() == self.buffer.capacity() {
198            let elements_remaining = self.length - self.buffer.len();
199            let element_size = mem::size_of::<T>().max(1);
200            let batch_elements = MAX_VECTOR_ALLOCATE / element_size;
201            let elements_to_reserve = elements_remaining.min(batch_elements);
202            self.buffer.reserve_exact(elements_to_reserve);
203        }
204    }
205}
206
207#[cfg(feature = "alloc")]
208impl<T: Decodable> Default for VecDecoder<T> {
209    fn default() -> Self {
210        Self::new()
211    }
212}
213
214#[cfg(feature = "alloc")]
215impl<T: Decodable> Decoder for VecDecoder<T> {
216    type Output = Vec<T>;
217    type Error = VecDecoderError<<<T as Decodable>::Decoder as Decoder>::Error>;
218
219    fn push_bytes(&mut self, bytes: &mut &[u8]) -> Result<bool, Self::Error> {
220        use VecDecoderError as E;
221        use VecDecoderErrorInner as Inner;
222
223        if let Some(mut decoder) = self.prefix_decoder.take() {
224            if decoder.push_bytes(bytes).map_err(|e| E(Inner::LengthPrefixDecode(e)))? {
225                self.prefix_decoder = Some(decoder);
226                return Ok(true);
227            }
228            self.length = decoder.end().map_err(|e| E(Inner::LengthPrefixDecode(e)))?;
229            if self.length == 0 {
230                return Ok(false);
231            }
232
233            self.prefix_decoder = None;
234
235            // For DoS prevention, let's not allocate all memory upfront.
236        }
237
238        while !bytes.is_empty() {
239            self.reserve();
240
241            let mut decoder = self.decoder.take().unwrap_or_else(T::decoder);
242
243            if decoder.push_bytes(bytes).map_err(|e| E(Inner::Item(e)))? {
244                self.decoder = Some(decoder);
245                return Ok(true);
246            }
247            let item = decoder.end().map_err(|e| E(Inner::Item(e)))?;
248            self.buffer.push(item);
249
250            if self.buffer.len() == self.length {
251                return Ok(false);
252            }
253        }
254
255        if self.buffer.len() == self.length {
256            Ok(false)
257        } else {
258            Ok(true)
259        }
260    }
261
262    fn end(self) -> Result<Self::Output, Self::Error> {
263        use VecDecoderErrorInner as E;
264
265        if let Some(ref prefix_decoder) = self.prefix_decoder {
266            return Err(VecDecoderError(E::UnexpectedEof(UnexpectedEofError {
267                missing: prefix_decoder.read_limit(),
268            })));
269        }
270
271        if self.buffer.len() == self.length {
272            Ok(self.buffer)
273        } else {
274            Err(VecDecoderError(E::UnexpectedEof(UnexpectedEofError {
275                missing: self.length - self.buffer.len(),
276            })))
277        }
278    }
279
280    fn read_limit(&self) -> usize {
281        if let Some(prefix_decoder) = &self.prefix_decoder {
282            prefix_decoder.read_limit()
283        } else if let Some(decoder) = &self.decoder {
284            decoder.read_limit()
285        } else if self.buffer.len() == self.length {
286            // Totally done.
287            0
288        } else {
289            let items_left_to_decode = self.length - self.buffer.len();
290            let decoder = T::decoder();
291            // This could be inaccurate (eg 1 for a `ByteVecDecoder`) but its the best we can do.
292            let limit_per_decoder = decoder.read_limit();
293            items_left_to_decode * limit_per_decoder
294        }
295    }
296}
297
298/// A decoder that expects exactly N bytes and returns them as an array.
299#[derive(Debug, Clone)]
300pub struct ArrayDecoder<const N: usize> {
301    buffer: [u8; N],
302    bytes_written: usize,
303}
304
305impl<const N: usize> ArrayDecoder<N> {
306    /// Constructs a new array decoder that expects exactly N bytes.
307    pub const fn new() -> Self {
308        Self { buffer: [0; N], bytes_written: 0 }
309    }
310}
311
312impl<const N: usize> Default for ArrayDecoder<N> {
313    fn default() -> Self {
314        Self::new()
315    }
316}
317
318impl<const N: usize> Decoder for ArrayDecoder<N> {
319    type Output = [u8; N];
320    type Error = UnexpectedEofError;
321
322    fn push_bytes(&mut self, bytes: &mut &[u8]) -> Result<bool, Self::Error> {
323        let remaining_space = N - self.bytes_written;
324        let copy_len = bytes.len().min(remaining_space);
325
326        if copy_len > 0 {
327            self.buffer[self.bytes_written..self.bytes_written + copy_len]
328                .copy_from_slice(&bytes[..copy_len]);
329            self.bytes_written += copy_len;
330            // Advance the slice reference to consume the bytes.
331            *bytes = &bytes[copy_len..];
332        }
333
334        // Return true if we still need more data.
335        Ok(self.bytes_written < N)
336    }
337
338    #[inline]
339    fn end(self) -> Result<Self::Output, Self::Error> {
340        if self.bytes_written == N {
341            Ok(self.buffer)
342        } else {
343            Err(UnexpectedEofError { missing: N - self.bytes_written })
344        }
345    }
346
347    #[inline]
348    fn read_limit(&self) -> usize {
349        N - self.bytes_written
350    }
351}
352
353/// A decoder which wraps two inner decoders and returns the output of both.
354pub struct Decoder2<A, B>
355where
356    A: Decoder,
357    B: Decoder,
358{
359    state: Decoder2State<A, B>,
360}
361
362enum Decoder2State<A: Decoder, B: Decoder> {
363    /// Decoding the first decoder, with second decoder waiting.
364    First(A, B),
365    /// Decoding the second decoder, with the first result stored.
366    Second(A::Output, B),
367    /// Decoder has failed and cannot be used again.
368    Errored,
369}
370
371impl<A, B> Decoder2<A, B>
372where
373    A: Decoder,
374    B: Decoder,
375{
376    /// Constructs a new composite decoder.
377    pub const fn new(first: A, second: B) -> Self {
378        Self { state: Decoder2State::First(first, second) }
379    }
380}
381
382impl<A, B> fmt::Debug for Decoder2<A, B>
383where
384    A: Decoder + fmt::Debug,
385    B: Decoder + fmt::Debug,
386    A::Output: fmt::Debug,
387{
388    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
389        match &self.state {
390            Decoder2State::First(a, b) => f.debug_tuple("First").field(a).field(b).finish(),
391            Decoder2State::Second(out, b) => f.debug_tuple("Second").field(out).field(b).finish(),
392            Decoder2State::Errored => write!(f, "Errored"),
393        }
394    }
395}
396
397impl<A, B> Decoder for Decoder2<A, B>
398where
399    A: Decoder,
400    B: Decoder,
401{
402    type Output = (A::Output, B::Output);
403    type Error = Decoder2Error<A::Error, B::Error>;
404
405    fn push_bytes(&mut self, bytes: &mut &[u8]) -> Result<bool, Self::Error> {
406        loop {
407            match &mut self.state {
408                Decoder2State::First(first_decoder, _) => {
409                    if first_decoder.push_bytes(bytes).map_err(Decoder2Error::First)? {
410                        // First decoder wants more data.
411                        return Ok(true);
412                    }
413
414                    // First decoder is complete, transition to second.
415                    // If the first decoder fails, the composite decoder
416                    // remains in an Errored state.
417                    match mem::replace(&mut self.state, Decoder2State::Errored) {
418                        Decoder2State::First(first, second) => {
419                            let first_result = first.end().map_err(Decoder2Error::First)?;
420                            self.state = Decoder2State::Second(first_result, second);
421                        }
422                        _ => unreachable!("we know we're in First state"),
423                    }
424                }
425                Decoder2State::Second(_, second_decoder) => {
426                    return second_decoder.push_bytes(bytes).map_err(|error| {
427                        self.state = Decoder2State::Errored;
428                        Decoder2Error::Second(error)
429                    });
430                }
431                Decoder2State::Errored => {
432                    panic!("use of failed decoder");
433                }
434            }
435        }
436    }
437
438    #[inline]
439    fn end(self) -> Result<Self::Output, Self::Error> {
440        match self.state {
441            Decoder2State::First(first_decoder, second_decoder) => {
442                // This branch is most likely an error since the decoder
443                // never got to the second one. But letting the error bubble
444                // up naturally from the child decoders.
445                let first_result = first_decoder.end().map_err(Decoder2Error::First)?;
446                let second_result = second_decoder.end().map_err(Decoder2Error::Second)?;
447                Ok((first_result, second_result))
448            }
449            Decoder2State::Second(first_result, second_decoder) => {
450                let second_result = second_decoder.end().map_err(Decoder2Error::Second)?;
451                Ok((first_result, second_result))
452            }
453            Decoder2State::Errored => {
454                panic!("use of failed decoder");
455            }
456        }
457    }
458
459    #[inline]
460    fn read_limit(&self) -> usize {
461        match &self.state {
462            Decoder2State::First(first_decoder, second_decoder) => {
463                first_decoder.read_limit() + second_decoder.read_limit()
464            }
465            Decoder2State::Second(_, second_decoder) => second_decoder.read_limit(),
466            Decoder2State::Errored => 0,
467        }
468    }
469}
470
471/// A decoder which decodes three objects, one after the other.
472pub struct Decoder3<A, B, C>
473where
474    A: Decoder,
475    B: Decoder,
476    C: Decoder,
477{
478    inner: Decoder2<Decoder2<A, B>, C>,
479}
480
481impl<A, B, C> Decoder3<A, B, C>
482where
483    A: Decoder,
484    B: Decoder,
485    C: Decoder,
486{
487    /// Constructs a new composite decoder.
488    pub const fn new(dec_1: A, dec_2: B, dec_3: C) -> Self {
489        Self { inner: Decoder2::new(Decoder2::new(dec_1, dec_2), dec_3) }
490    }
491}
492
493impl<A, B, C> fmt::Debug for Decoder3<A, B, C>
494where
495    A: Decoder + fmt::Debug,
496    B: Decoder + fmt::Debug,
497    C: Decoder + fmt::Debug,
498    A::Output: fmt::Debug,
499    B::Output: fmt::Debug,
500{
501    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
502        self.inner.fmt(f)
503    }
504}
505
506impl<A, B, C> Decoder for Decoder3<A, B, C>
507where
508    A: Decoder,
509    B: Decoder,
510    C: Decoder,
511{
512    type Output = (A::Output, B::Output, C::Output);
513    type Error = Decoder3Error<A::Error, B::Error, C::Error>;
514
515    #[inline]
516    fn push_bytes(&mut self, bytes: &mut &[u8]) -> Result<bool, Self::Error> {
517        self.inner.push_bytes(bytes).map_err(|error| match error {
518            Decoder2Error::First(Decoder2Error::First(a)) => Decoder3Error::First(a),
519            Decoder2Error::First(Decoder2Error::Second(b)) => Decoder3Error::Second(b),
520            Decoder2Error::Second(c) => Decoder3Error::Third(c),
521        })
522    }
523
524    #[inline]
525    fn end(self) -> Result<Self::Output, Self::Error> {
526        let result = self.inner.end().map_err(|error| match error {
527            Decoder2Error::First(Decoder2Error::First(a)) => Decoder3Error::First(a),
528            Decoder2Error::First(Decoder2Error::Second(b)) => Decoder3Error::Second(b),
529            Decoder2Error::Second(c) => Decoder3Error::Third(c),
530        })?;
531
532        let ((first, second), third) = result;
533        Ok((first, second, third))
534    }
535
536    #[inline]
537    fn read_limit(&self) -> usize {
538        self.inner.read_limit()
539    }
540}
541
542/// A decoder which decodes four objects, one after the other.
543pub struct Decoder4<A, B, C, D>
544where
545    A: Decoder,
546    B: Decoder,
547    C: Decoder,
548    D: Decoder,
549{
550    inner: Decoder2<Decoder2<A, B>, Decoder2<C, D>>,
551}
552
553impl<A, B, C, D> Decoder4<A, B, C, D>
554where
555    A: Decoder,
556    B: Decoder,
557    C: Decoder,
558    D: Decoder,
559{
560    /// Constructs a new composite decoder.
561    pub const fn new(dec_1: A, dec_2: B, dec_3: C, dec_4: D) -> Self {
562        Self { inner: Decoder2::new(Decoder2::new(dec_1, dec_2), Decoder2::new(dec_3, dec_4)) }
563    }
564}
565
566impl<A, B, C, D> fmt::Debug for Decoder4<A, B, C, D>
567where
568    A: Decoder + fmt::Debug,
569    B: Decoder + fmt::Debug,
570    C: Decoder + fmt::Debug,
571    D: Decoder + fmt::Debug,
572    A::Output: fmt::Debug,
573    B::Output: fmt::Debug,
574    C::Output: fmt::Debug,
575{
576    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
577        self.inner.fmt(f)
578    }
579}
580
581impl<A, B, C, D> Decoder for Decoder4<A, B, C, D>
582where
583    A: Decoder,
584    B: Decoder,
585    C: Decoder,
586    D: Decoder,
587{
588    type Output = (A::Output, B::Output, C::Output, D::Output);
589    type Error = Decoder4Error<A::Error, B::Error, C::Error, D::Error>;
590
591    #[inline]
592    fn push_bytes(&mut self, bytes: &mut &[u8]) -> Result<bool, Self::Error> {
593        self.inner.push_bytes(bytes).map_err(|error| match error {
594            Decoder2Error::First(Decoder2Error::First(a)) => Decoder4Error::First(a),
595            Decoder2Error::First(Decoder2Error::Second(b)) => Decoder4Error::Second(b),
596            Decoder2Error::Second(Decoder2Error::First(c)) => Decoder4Error::Third(c),
597            Decoder2Error::Second(Decoder2Error::Second(d)) => Decoder4Error::Fourth(d),
598        })
599    }
600
601    #[inline]
602    fn end(self) -> Result<Self::Output, Self::Error> {
603        let result = self.inner.end().map_err(|error| match error {
604            Decoder2Error::First(Decoder2Error::First(a)) => Decoder4Error::First(a),
605            Decoder2Error::First(Decoder2Error::Second(b)) => Decoder4Error::Second(b),
606            Decoder2Error::Second(Decoder2Error::First(c)) => Decoder4Error::Third(c),
607            Decoder2Error::Second(Decoder2Error::Second(d)) => Decoder4Error::Fourth(d),
608        })?;
609
610        let ((first, second), (third, fourth)) = result;
611        Ok((first, second, third, fourth))
612    }
613
614    #[inline]
615    fn read_limit(&self) -> usize {
616        self.inner.read_limit()
617    }
618}
619
620/// A decoder which decodes six objects, one after the other.
621#[allow(clippy::type_complexity)] // Nested composition is easier than flattened alternatives.
622pub struct Decoder6<A, B, C, D, E, F>
623where
624    A: Decoder,
625    B: Decoder,
626    C: Decoder,
627    D: Decoder,
628    E: Decoder,
629    F: Decoder,
630{
631    inner: Decoder2<Decoder3<A, B, C>, Decoder3<D, E, F>>,
632}
633
634impl<A, B, C, D, E, F> Decoder6<A, B, C, D, E, F>
635where
636    A: Decoder,
637    B: Decoder,
638    C: Decoder,
639    D: Decoder,
640    E: Decoder,
641    F: Decoder,
642{
643    /// Constructs a new composite decoder.
644    pub const fn new(dec_1: A, dec_2: B, dec_3: C, dec_4: D, dec_5: E, dec_6: F) -> Self {
645        Self {
646            inner: Decoder2::new(
647                Decoder3::new(dec_1, dec_2, dec_3),
648                Decoder3::new(dec_4, dec_5, dec_6),
649            ),
650        }
651    }
652}
653
654impl<A, B, C, D, E, F> Decoder for Decoder6<A, B, C, D, E, F>
655where
656    A: Decoder,
657    B: Decoder,
658    C: Decoder,
659    D: Decoder,
660    E: Decoder,
661    F: Decoder,
662{
663    type Output = (A::Output, B::Output, C::Output, D::Output, E::Output, F::Output);
664    type Error = Decoder6Error<A::Error, B::Error, C::Error, D::Error, E::Error, F::Error>;
665
666    #[inline]
667    fn push_bytes(&mut self, bytes: &mut &[u8]) -> Result<bool, Self::Error> {
668        self.inner.push_bytes(bytes).map_err(|error| match error {
669            Decoder2Error::First(Decoder3Error::First(a)) => Decoder6Error::First(a),
670            Decoder2Error::First(Decoder3Error::Second(b)) => Decoder6Error::Second(b),
671            Decoder2Error::First(Decoder3Error::Third(c)) => Decoder6Error::Third(c),
672            Decoder2Error::Second(Decoder3Error::First(d)) => Decoder6Error::Fourth(d),
673            Decoder2Error::Second(Decoder3Error::Second(e)) => Decoder6Error::Fifth(e),
674            Decoder2Error::Second(Decoder3Error::Third(f)) => Decoder6Error::Sixth(f),
675        })
676    }
677
678    #[inline]
679    fn end(self) -> Result<Self::Output, Self::Error> {
680        let result = self.inner.end().map_err(|error| match error {
681            Decoder2Error::First(Decoder3Error::First(a)) => Decoder6Error::First(a),
682            Decoder2Error::First(Decoder3Error::Second(b)) => Decoder6Error::Second(b),
683            Decoder2Error::First(Decoder3Error::Third(c)) => Decoder6Error::Third(c),
684            Decoder2Error::Second(Decoder3Error::First(d)) => Decoder6Error::Fourth(d),
685            Decoder2Error::Second(Decoder3Error::Second(e)) => Decoder6Error::Fifth(e),
686            Decoder2Error::Second(Decoder3Error::Third(f)) => Decoder6Error::Sixth(f),
687        })?;
688
689        let ((first, second, third), (fourth, fifth, sixth)) = result;
690        Ok((first, second, third, fourth, fifth, sixth))
691    }
692
693    #[inline]
694    fn read_limit(&self) -> usize {
695        self.inner.read_limit()
696    }
697}
698
699#[cfg(test)]
700mod tests {
701    #[cfg(feature = "alloc")]
702    use alloc::vec;
703    #[cfg(feature = "alloc")]
704    use alloc::vec::Vec;
705
706    #[cfg(feature = "alloc")]
707    use super::*;
708
709    #[test]
710    #[cfg(feature = "alloc")]
711    fn byte_vec_decoder_decode_empty_slice() {
712        let mut decoder = ByteVecDecoder::new();
713        let data = [];
714        let _ = decoder.push_bytes(&mut data.as_slice());
715        let err = decoder.end().unwrap_err();
716
717        if let ByteVecDecoderErrorInner::UnexpectedEof(e) = err.0 {
718            assert_eq!(e.missing, 1);
719        } else {
720            panic!("Expected UnexpectedEof error");
721        }
722    }
723
724    #[test]
725    #[cfg(feature = "alloc")]
726    fn byte_vec_decoder_incomplete_0xfd_prefix() {
727        let mut decoder = ByteVecDecoder::new();
728        let data = [0xFD];
729        let _ = decoder.push_bytes(&mut data.as_slice());
730        let err = decoder.end().unwrap_err();
731
732        if let ByteVecDecoderErrorInner::UnexpectedEof(e) = err.0 {
733            assert_eq!(e.missing, 2);
734        } else {
735            panic!("Expected UnexpectedEof error");
736        }
737    }
738
739    #[test]
740    #[cfg(feature = "alloc")]
741    fn byte_vec_decoder_incomplete_0xfe_prefix() {
742        let mut decoder = ByteVecDecoder::new();
743        let data = [0xFE];
744        let _ = decoder.push_bytes(&mut data.as_slice());
745        let err = decoder.end().unwrap_err();
746
747        if let ByteVecDecoderErrorInner::UnexpectedEof(e) = err.0 {
748            assert_eq!(e.missing, 4);
749        } else {
750            panic!("Expected UnexpectedEof error");
751        }
752    }
753
754    #[test]
755    #[cfg(feature = "alloc")]
756    fn byte_vec_decoder_incomplete_0xff_prefix() {
757        let mut decoder = ByteVecDecoder::new();
758        let data = [0xFF];
759        let _ = decoder.push_bytes(&mut data.as_slice());
760        let err = decoder.end().unwrap_err();
761
762        if let ByteVecDecoderErrorInner::UnexpectedEof(e) = err.0 {
763            assert_eq!(e.missing, 8);
764        } else {
765            panic!("Expected UnexpectedEof error");
766        }
767    }
768
769    #[test]
770    #[cfg(feature = "alloc")]
771    fn byte_vec_decoder_reserves_in_batches() {
772        // A small number of extra bytes so we extend exactly by the remainder
773        // instead of another full batch.
774        let tail_length: usize = 11;
775
776        let total_len = MAX_VECTOR_ALLOCATE + tail_length;
777        let total_len_le = u32::try_from(total_len).expect("total_len fits u32").to_le_bytes();
778        let mut decoder = ByteVecDecoder::new();
779
780        let mut prefix = vec![0xFE]; // total_len_le is a compact size of four bytes.
781        prefix.extend_from_slice(&total_len_le);
782        prefix.push(0xAA);
783        let mut prefix_slice = prefix.as_slice();
784        decoder.push_bytes(&mut prefix_slice).expect("length plus first element");
785        assert!(prefix_slice.is_empty());
786
787        assert_eq!(decoder.buffer.capacity(), MAX_VECTOR_ALLOCATE);
788        assert_eq!(decoder.buffer.len(), 1);
789        assert_eq!(decoder.buffer[0], 0xAA);
790
791        let fill = vec![0xBB; MAX_VECTOR_ALLOCATE - 1];
792        let mut fill_slice = fill.as_slice();
793        decoder.push_bytes(&mut fill_slice).expect("fills to batch boundary, full capacity");
794        assert!(fill_slice.is_empty());
795
796        assert_eq!(decoder.buffer.capacity(), MAX_VECTOR_ALLOCATE);
797        assert_eq!(decoder.buffer.len(), MAX_VECTOR_ALLOCATE);
798        assert_eq!(decoder.buffer[MAX_VECTOR_ALLOCATE - 1], 0xBB);
799
800        let mut tail = vec![0xCC];
801        tail.extend([0xDD].repeat(tail_length - 1));
802        let mut tail_slice = tail.as_slice();
803        decoder.push_bytes(&mut tail_slice).expect("fills the remaining bytes");
804        assert!(tail_slice.is_empty());
805
806        assert_eq!(decoder.buffer.capacity(), MAX_VECTOR_ALLOCATE + tail_length);
807        assert_eq!(decoder.buffer.len(), total_len);
808        assert_eq!(decoder.buffer[MAX_VECTOR_ALLOCATE], 0xCC);
809
810        let result = decoder.end().unwrap();
811        assert_eq!(result.len(), total_len);
812        assert_eq!(result[total_len - 1], 0xDD);
813    }
814
815    #[cfg(feature = "alloc")]
816    #[derive(Clone, Debug, PartialEq, Eq)]
817    pub struct Inner(u32);
818
819    /// The decoder for the [`Inner`] type.
820    #[cfg(feature = "alloc")]
821    #[derive(Clone)]
822    pub struct InnerDecoder(ArrayDecoder<4>);
823
824    #[cfg(feature = "alloc")]
825    impl Decoder for InnerDecoder {
826        type Output = Inner;
827        type Error = UnexpectedEofError;
828
829        fn push_bytes(&mut self, bytes: &mut &[u8]) -> Result<bool, Self::Error> {
830            self.0.push_bytes(bytes)
831        }
832
833        fn end(self) -> Result<Self::Output, Self::Error> {
834            let n = u32::from_le_bytes(self.0.end()?);
835            Ok(Inner(n))
836        }
837
838        fn read_limit(&self) -> usize {
839            self.0.read_limit()
840        }
841    }
842
843    #[cfg(feature = "alloc")]
844    impl Decodable for Inner {
845        type Decoder = InnerDecoder;
846        fn decoder() -> Self::Decoder {
847            InnerDecoder(ArrayDecoder::<4>::new())
848        }
849    }
850
851    #[cfg(feature = "alloc")]
852    #[derive(Clone, Debug, PartialEq, Eq)]
853    pub struct Test(Vec<Inner>);
854
855    /// The decoder for the [`Test`] type.
856    #[cfg(feature = "alloc")]
857    #[derive(Clone, Default)]
858    pub struct TestDecoder(VecDecoder<Inner>);
859
860    #[cfg(feature = "alloc")]
861    impl Decoder for TestDecoder {
862        type Output = Test;
863        type Error = VecDecoderError<UnexpectedEofError>;
864
865        fn push_bytes(&mut self, bytes: &mut &[u8]) -> Result<bool, Self::Error> {
866            self.0.push_bytes(bytes)
867        }
868
869        fn end(self) -> Result<Self::Output, Self::Error> {
870            let v = self.0.end()?;
871            Ok(Test(v))
872        }
873
874        fn read_limit(&self) -> usize {
875            self.0.read_limit()
876        }
877    }
878
879    #[cfg(feature = "alloc")]
880    impl Decodable for Test {
881        type Decoder = TestDecoder;
882        fn decoder() -> Self::Decoder {
883            TestDecoder(VecDecoder::new())
884        }
885    }
886
887    #[test]
888    #[cfg(feature = "alloc")]
889    fn vec_decoder_empty() {
890        // Empty with a couple of arbitrary extra bytes.
891        let encoded = vec![0x00, 0xFF, 0xFF];
892
893        let mut slice = encoded.as_slice();
894        let mut decoder = Test::decoder();
895        assert!(!decoder.push_bytes(&mut slice).unwrap());
896
897        let got = decoder.end().unwrap();
898        let want = Test(vec![]);
899
900        assert_eq!(got, want);
901    }
902
903    #[test]
904    #[cfg(feature = "alloc")]
905    fn vec_decoder_empty_no_bytes() {
906        // Empty slice. Note the lack of any length prefix compact size.
907        let encoded = &[];
908
909        let mut slice = encoded.as_slice();
910        let mut decoder = Test::decoder();
911        // Should want more bytes since we've provided nothing
912        assert!(decoder.push_bytes(&mut slice).unwrap());
913
914        assert!(matches!(
915            decoder.end().unwrap_err(),
916            VecDecoderError(VecDecoderErrorInner::UnexpectedEof(_))
917        ));
918    }
919
920    #[test]
921    #[cfg(feature = "alloc")]
922    fn vec_decoder_one_item() {
923        let encoded = vec![0x01, 0xEF, 0xBE, 0xAD, 0xDE];
924
925        let mut slice = encoded.as_slice();
926        let mut decoder = Test::decoder();
927        decoder.push_bytes(&mut slice).unwrap();
928
929        let got = decoder.end().unwrap();
930        let want = Test(vec![Inner(0xDEAD_BEEF)]);
931
932        assert_eq!(got, want);
933    }
934
935    #[test]
936    #[cfg(feature = "alloc")]
937    fn vec_decoder_two_items() {
938        let encoded = vec![0x02, 0xEF, 0xBE, 0xAD, 0xDE, 0xBE, 0xBA, 0xFE, 0xCA];
939
940        let mut slice = encoded.as_slice();
941        let mut decoder = Test::decoder();
942        decoder.push_bytes(&mut slice).unwrap();
943
944        let got = decoder.end().unwrap();
945        let want = Test(vec![Inner(0xDEAD_BEEF), Inner(0xCAFE_BABE)]);
946
947        assert_eq!(got, want);
948    }
949
950    #[test]
951    #[cfg(feature = "alloc")]
952    fn vec_decoder_reserves_in_batches() {
953        // A small number of extra elements so we extend exactly by the remainder
954        // instead of another full batch.
955        let tail_length: usize = 11;
956
957        let element_size = core::mem::size_of::<Inner>();
958        let batch_length = MAX_VECTOR_ALLOCATE / element_size;
959        assert!(batch_length > 1);
960        let total_len = batch_length + tail_length;
961        let total_len_le = u32::try_from(total_len).expect("total_len fits u32").to_le_bytes();
962        let mut decoder = Test::decoder();
963
964        let mut prefix = vec![0xFE]; // total_len_le is a compact size of four bytes.
965        prefix.extend_from_slice(&total_len_le);
966        prefix.extend_from_slice(&0xAA_u32.to_le_bytes());
967        let mut prefix_slice = prefix.as_slice();
968        decoder.push_bytes(&mut prefix_slice).expect("length plus first element");
969        assert!(prefix_slice.is_empty());
970
971        assert_eq!(decoder.0.buffer.capacity(), batch_length);
972        assert_eq!(decoder.0.buffer.len(), 1);
973        assert_eq!(decoder.0.buffer[0], Inner(0xAA));
974
975        let fill = 0xBB_u32.to_le_bytes().repeat(batch_length - 1);
976        let mut fill_slice = fill.as_slice();
977        decoder.push_bytes(&mut fill_slice).expect("fills to batch boundary, full capacity");
978        assert!(fill_slice.is_empty());
979
980        assert_eq!(decoder.0.buffer.capacity(), batch_length);
981        assert_eq!(decoder.0.buffer.len(), batch_length);
982        assert_eq!(decoder.0.buffer[batch_length - 1], Inner(0xBB));
983
984        let mut tail = 0xCC_u32.to_le_bytes().to_vec();
985        tail.extend(0xDD_u32.to_le_bytes().repeat(tail_length - 1));
986        let mut tail_slice = tail.as_slice();
987        decoder.push_bytes(&mut tail_slice).expect("fills the remaining bytes");
988        assert!(tail_slice.is_empty());
989
990        assert_eq!(decoder.0.buffer.capacity(), batch_length + tail_length);
991        assert_eq!(decoder.0.buffer.len(), total_len);
992        assert_eq!(decoder.0.buffer[batch_length], Inner(0xCC));
993
994        let Test(result) = decoder.end().unwrap();
995        assert_eq!(result.len(), total_len);
996        assert_eq!(result[total_len - 1], Inner(0xDD));
997    }
998}