Skip to main content

qubit_io_binary/stream/
buffered_leb128_reader.rs

1// =============================================================================
2//    Copyright (c) 2026 Haixing Hu.
3//
4//    SPDX-License-Identifier: Apache-2.0
5//
6//    Licensed under the Apache License, Version 2.0.
7// =============================================================================
8
9//! Buffered reader for LEB128-encoded values.
10
11use core::marker::PhantomData;
12use std::collections::TryReserveError;
13use std::io::Result;
14use std::io::SeekFrom;
15
16use qubit_codec::TranscodeDecodeInput;
17use qubit_codec_binary::Leb128Codec;
18use qubit_codec_binary::Leb128DecodePolicy;
19use qubit_codec_binary::NonStrict;
20use qubit_codec_binary::Strict;
21use qubit_io::Buffer;
22use qubit_io::ByteInput;
23use qubit_io::Input;
24use qubit_io::Seekable;
25use qubit_io::SeekableByteInput;
26
27use super::internal::TranscodeDecodeInputExt;
28use crate::util::MIN_CODEC_BUFFER_CAPACITY;
29use crate::util::read_utf8_payload;
30#[cfg(not(target_pointer_width = "64"))]
31use crate::util::usize_from_u64_len;
32
33/// Buffered reader for LEB128 integers.
34///
35/// Values are decoded directly from the internal input buffer while the codec
36/// scans for the LEB128 terminating byte.
37///
38/// # Buffered state
39///
40/// This reader may prefetch bytes from the wrapped reader. As a result,
41/// [`Self::inner`] can observe an underlying stream position ahead of the
42/// logical position exposed by this wrapper.
43///
44/// # Target-width integers
45///
46/// `usize` and `isize` methods use the current Rust target's pointer width.
47/// Prefer fixed-width integer methods such as `read_u64` or `read_i64` for
48/// persistent files and cross-platform protocols.
49///
50/// # Type Parameters
51///
52/// - `R`: Underlying byte input.
53/// - `P`: LEB128 canonicality policy.
54///
55/// # Examples
56///
57/// ```
58/// use qubit_codec_binary::NonStrict;
59/// use qubit_io_binary::BufferedLeb128Reader;
60///
61/// let mut reader = BufferedLeb128Reader::<_, NonStrict>::new(
62///     std::io::Cursor::new([0xAC, 0x02]),
63/// );
64/// assert_eq!(reader.read_u32_non_strict()?, 300);
65/// # Ok::<(), std::io::Error>(())
66/// ```
67pub struct BufferedLeb128Reader<R, P>
68where
69    R: ByteInput,
70{
71    /// Buffered codec input and wrapped reader.
72    input: TranscodeDecodeInput<R>,
73    /// Associates the selected decoding policy without storing a value.
74    marker: PhantomData<fn() -> P>,
75}
76
77impl_input_methods!(BufferedLeb128Reader<R, P> where R: ByteInput);
78impl_seekable_methods!(BufferedLeb128Reader<R, P> where R: SeekableByteInput);
79
80impl<R, P> BufferedLeb128Reader<R, P>
81where
82    R: ByteInput,
83    P: Leb128DecodePolicy,
84{
85    /// Creates a buffered LEB128 reader with the default buffer capacity.
86    ///
87    /// # Parameters
88    ///
89    /// - `inner`: Underlying byte input.
90    ///
91    /// # Returns
92    ///
93    /// Returns a buffered reader using policy `P`.
94    #[must_use]
95    #[inline]
96    pub fn new(inner: R) -> Self {
97        Self {
98            input: TranscodeDecodeInput::new(inner),
99            marker: PhantomData,
100        }
101    }
102
103    /// Creates a buffered LEB128 reader with at least `capacity` bytes.
104    ///
105    /// # Parameters
106    ///
107    /// - `inner`: Underlying byte input.
108    /// - `capacity`: Requested internal buffer capacity in bytes.
109    ///
110    /// # Returns
111    ///
112    /// Returns a buffered reader whose capacity also satisfies the largest
113    /// supported codec payload.
114    #[must_use]
115    #[inline]
116    pub fn with_capacity(inner: R, capacity: usize) -> Self {
117        Self {
118            input: TranscodeDecodeInput::with_capacity(inner, capacity.max(MIN_CODEC_BUFFER_CAPACITY)),
119            marker: PhantomData,
120        }
121    }
122
123    /// Tries to create a buffered LEB128 reader with at least `capacity` bytes.
124    ///
125    /// # Parameters
126    ///
127    /// - `inner`: Underlying byte input to wrap.
128    /// - `capacity`: Requested internal buffer capacity in bytes. The request
129    ///   is raised when necessary to fit the largest supported codec payload.
130    ///
131    /// # Returns
132    ///
133    /// Returns a buffered reader on successful allocation. Its capacity
134    /// satisfies both the request and the minimum required by the supported
135    /// codecs.
136    ///
137    /// # Errors
138    ///
139    /// Returns a [`TryReserveError`] if the buffer capacity overflows or its
140    /// allocation fails.
141    #[inline]
142    pub fn try_with_capacity(inner: R, capacity: usize) -> std::result::Result<Self, TryReserveError> {
143        Ok(Self {
144            input: TranscodeDecodeInput::try_with_capacity(inner, capacity.max(MIN_CODEC_BUFFER_CAPACITY))?,
145            marker: PhantomData,
146        })
147    }
148
149    /// Returns whether this reader rejects non-canonical encodings.
150    ///
151    /// # Returns
152    ///
153    /// Returns `true` when policy `P` requires canonical encodings.
154    #[must_use]
155    #[inline(always)]
156    pub const fn is_strict(&self) -> bool {
157        P::STRICT
158    }
159
160    /// Returns a shared reference to the underlying reader.
161    ///
162    /// The underlying reader may already be positioned past unread bytes held
163    /// in this wrapper's internal buffer.
164    ///
165    /// # Returns
166    ///
167    /// Returns the wrapped reader.
168    #[must_use]
169    #[inline(always)]
170    pub const fn inner(&self) -> &R {
171        self.input.inner()
172    }
173
174    /// Consumes this wrapper and preserves its unread buffered bytes.
175    ///
176    /// # Returns
177    ///
178    /// Returns the wrapped reader and the buffer whose [`Buffer::readable`]
179    /// slice contains every prefetched byte not yet consumed logically. To
180    /// continue the same logical stream, consume that slice before reading
181    /// from the returned reader.
182    #[inline(always)]
183    #[must_use = "the returned inner reader and unread buffer must be handled"]
184    pub fn into_parts(self) -> (R, Buffer<u8>) {
185        self.input.into_parts()
186    }
187}
188
189macro_rules! impl_read_value {
190    ($policy:ty, $method:ident, $ty:ty, $doc:literal) => {
191        #[doc = $doc]
192        #[doc = ""]
193        #[doc = "# Returns"]
194        #[doc = ""]
195        #[doc = concat!("Returns the decoded `", stringify!($ty), "`.")]
196        #[doc = ""]
197        #[doc = "# Errors"]
198        #[doc = ""]
199        #[doc = "Returns an input error or an invalid-data error when the \
200                 payload is malformed or violates the selected policy."]
201        #[inline(always)]
202        pub fn $method(&mut self) -> Result<$ty> {
203            type Codec = Leb128Codec<$ty, $policy>;
204
205            self.input.read_decoded::<Codec>()
206        }
207    };
208}
209
210macro_rules! impl_for_policy {
211    (
212        $policy:ty,
213        $read_usize:ident,
214        $read_u64:ident,
215        $read_utf8_string_usize:ident,
216        $read_utf8_string_u64:ident,
217        {
218            $(($method:ident, $ty:ty, $doc:literal)),* $(,)?
219        }
220        $(,)?
221    ) => {
222        impl<R> BufferedLeb128Reader<R, $policy>
223        where
224            R: ByteInput,
225        {
226            $(
227                impl_read_value!($policy, $method, $ty, $doc);
228            )*
229
230            /// Reads a UTF-8 string prefixed by an unsigned LEB128 byte length.
231            ///
232            /// The length prefix is decoded as usize, so this format is
233            /// target-width dependent. Prefer a fixed-width length prefix for
234            /// persistent files and cross-platform protocols.
235            ///
236            /// # Parameters
237            ///
238            /// - max_len: Maximum accepted UTF-8 payload length in bytes.
239            ///
240            /// # Returns
241            ///
242            /// Returns the decoded UTF-8 string.
243            ///
244            /// # Errors
245            ///
246            /// Returns an input or allocation error, or an invalid-data error
247            /// for a malformed or excessive length or invalid UTF-8.
248            ///
249            /// # Stream position
250            ///
251            /// If the decoded length exceeds `max_len`, the length prefix has been
252            /// consumed logically, but the payload has not. If UTF-8 validation
253            /// fails, the prefix and complete payload have been consumed logically.
254            /// The underlying input may already have prefetched payload bytes;
255            /// its position is not the logical consumption position. These operations
256            /// do not roll back; recover only at an outer framing boundary or a
257            /// trusted seek checkpoint. See [`StringReadExt`](crate::StringReadExt)
258            /// for the shared length-prefixed read contract.
259            #[inline]
260            pub fn $read_utf8_string_usize(
261                &mut self,
262                max_len: usize,
263            ) -> Result<String> {
264                let len = self.$read_usize()?;
265                read_utf8_payload(&mut self.input, len, max_len)
266            }
267
268            /// Reads a UTF-8 string prefixed by an unsigned LEB128 u64 byte
269            /// length.
270            ///
271            /// Prefer this method over the usize variant for persistent files
272            /// and cross-platform protocols because the length field is
273            /// independent of the current Rust target's pointer width.
274            ///
275            /// # Parameters
276            ///
277            /// - max_len: Maximum accepted UTF-8 payload length in bytes.
278            ///
279            /// # Returns
280            ///
281            /// Returns the decoded UTF-8 string.
282            ///
283            /// # Errors
284            ///
285            /// Returns an input, conversion, or allocation error, or an
286            /// invalid-data error for a malformed or excessive length or
287            /// invalid UTF-8.
288            ///
289            /// # Stream position
290            ///
291            /// If the decoded length exceeds `max_len`, the length prefix has been
292            /// consumed logically, but the payload has not. If UTF-8 validation
293            /// fails, the prefix and complete payload have been consumed logically.
294            /// The underlying input may already have prefetched payload bytes;
295            /// its position is not the logical consumption position. These operations
296            /// do not roll back; recover only at an outer framing boundary or a
297            /// trusted seek checkpoint. See [`StringReadExt`](crate::StringReadExt)
298            /// for the shared length-prefixed read contract.
299            #[inline]
300            pub fn $read_utf8_string_u64(
301                &mut self,
302                max_len: usize,
303            ) -> Result<String> {
304                let len = self.$read_u64()?;
305                #[cfg(target_pointer_width = "64")]
306                let len = len as usize;
307                #[cfg(not(target_pointer_width = "64"))]
308                let len = usize_from_u64_len(len)?;
309                read_utf8_payload(&mut self.input, len, max_len)
310            }
311        }
312    };
313}
314
315impl_for_policy!(
316    NonStrict,
317    read_usize_non_strict,
318    read_u64_non_strict,
319    read_utf8_string_usize_non_strict,
320    read_utf8_string_u64_non_strict,
321    {
322        (read_u8_non_strict, u8, "Reads an unsigned LEB128 u8."),
323        (read_u16_non_strict, u16, "Reads an unsigned LEB128 u16."),
324        (read_u32_non_strict, u32, "Reads an unsigned LEB128 u32."),
325        (read_u64_non_strict, u64, "Reads an unsigned LEB128 u64."),
326        (read_u128_non_strict, u128, "Reads an unsigned LEB128 u128."),
327        (read_usize_non_strict, usize, "Reads an unsigned LEB128 usize."),
328        (read_i8_non_strict, i8, "Reads a signed LEB128 i8."),
329        (read_i16_non_strict, i16, "Reads a signed LEB128 i16."),
330        (read_i32_non_strict, i32, "Reads a signed LEB128 i32."),
331        (read_i64_non_strict, i64, "Reads a signed LEB128 i64."),
332        (read_i128_non_strict, i128, "Reads a signed LEB128 i128."),
333        (read_isize_non_strict, isize, "Reads a signed LEB128 isize."),
334    },
335);
336impl_for_policy!(
337    Strict,
338    read_usize,
339    read_u64,
340    read_utf8_string_usize,
341    read_utf8_string_u64,
342    {
343        (read_u8, u8, "Reads an unsigned LEB128 u8."),
344        (read_u16, u16, "Reads an unsigned LEB128 u16."),
345        (read_u32, u32, "Reads an unsigned LEB128 u32."),
346        (read_u64, u64, "Reads an unsigned LEB128 u64."),
347        (read_u128, u128, "Reads an unsigned LEB128 u128."),
348        (read_usize, usize, "Reads an unsigned LEB128 usize."),
349        (read_i8, i8, "Reads a signed LEB128 i8."),
350        (read_i16, i16, "Reads a signed LEB128 i16."),
351        (read_i32, i32, "Reads a signed LEB128 i32."),
352        (read_i64, i64, "Reads a signed LEB128 i64."),
353        (read_i128, i128, "Reads a signed LEB128 i128."),
354        (read_isize, isize, "Reads a signed LEB128 isize."),
355    },
356);
357impl<R, P> Input for BufferedLeb128Reader<R, P>
358where
359    R: ByteInput,
360{
361    /// Byte type yielded by the buffered input adapter.
362    type Item = u8;
363
364    /// Reports that this adapter buffers input.
365    ///
366    /// # Returns
367    ///
368    /// Always returns `true`.
369    #[inline(always)]
370    fn is_buffered(&self) -> bool {
371        true
372    }
373
374    /// Reads up to `count` bytes into an indexed output range.
375    ///
376    /// # Parameters
377    ///
378    /// - `output`: Destination slice.
379    /// - `index`: First destination index.
380    /// - `count`: Maximum number of bytes to read.
381    ///
382    /// # Returns
383    ///
384    /// Returns the number of bytes read.
385    ///
386    /// # Errors
387    ///
388    /// Returns an error reported while reading the buffered input.
389    ///
390    /// # Safety
391    ///
392    /// `index..index + count` must be a valid range within `output`.
393    #[inline(always)]
394    unsafe fn read_unchecked(&mut self, output: &mut [u8], index: usize, count: usize) -> Result<usize> {
395        // SAFETY: The caller upholds the indexed destination range contract.
396        unsafe { self.input.read_unchecked(output, index, count) }
397    }
398}
399
400impl<R, P> Seekable for BufferedLeb128Reader<R, P>
401where
402    R: SeekableByteInput,
403{
404    /// Unit type used for seeking in the byte stream.
405    type Unit = u8;
406
407    /// Seeks relative to this reader's logical position. Relative seeks within
408    /// the buffered window preserve unread bytes; successful seeks outside the
409    /// window discard the old buffer. The returned position is logical.
410    ///
411    /// # Parameters
412    ///
413    /// - `position`: Target seek position.
414    ///
415    /// # Returns
416    ///
417    /// Returns the new logical stream position in bytes.
418    ///
419    /// # Errors
420    ///
421    /// Returns the seek error reported by the wrapped reader and preserves
422    /// buffered bytes when seeking fails, including when an in-window seek
423    /// cannot query the wrapped position.
424    #[inline(always)]
425    fn seek_to(&mut self, position: SeekFrom) -> Result<u64> {
426        self.input.seek(position)
427    }
428}