Skip to main content

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