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 /// # Errors
126 ///
127 /// Returns an allocation error when the requested buffer cannot be
128 /// allocated.
129 #[inline]
130 pub fn try_with_capacity(inner: R, capacity: usize) -> std::result::Result<Self, TryReserveError> {
131 Ok(Self {
132 input: TranscodeDecodeInput::try_with_capacity(inner, capacity.max(MIN_CODEC_BUFFER_CAPACITY))?,
133 marker: PhantomData,
134 })
135 }
136
137 /// Returns whether this reader rejects non-canonical encodings.
138 ///
139 /// # Returns
140 ///
141 /// Returns `true` when policy `P` requires canonical encodings.
142 #[must_use]
143 #[inline(always)]
144 pub const fn is_strict(&self) -> bool {
145 P::STRICT
146 }
147
148 /// Returns a shared reference to the underlying reader.
149 ///
150 /// The underlying reader may already be positioned past unread bytes held
151 /// in this wrapper's internal buffer.
152 ///
153 /// # Returns
154 ///
155 /// Returns the wrapped reader.
156 #[must_use]
157 #[inline(always)]
158 pub const fn inner(&self) -> &R {
159 self.input.inner()
160 }
161
162 /// Consumes this wrapper and preserves its unread buffered bytes.
163 ///
164 /// # Returns
165 ///
166 /// Returns the wrapped reader and the buffer whose [`Buffer::readable`]
167 /// slice contains every prefetched byte not yet consumed logically. To
168 /// continue the same logical stream, consume that slice before reading
169 /// from the returned reader.
170 #[inline(always)]
171 #[must_use = "the returned inner reader and unread buffer must be handled"]
172 pub fn into_parts(self) -> (R, Buffer<u8>) {
173 self.input.into_parts()
174 }
175}
176
177macro_rules! impl_read_value {
178 ($policy:ty, $method:ident, $ty:ty, $doc:literal) => {
179 #[doc = $doc]
180 #[doc = ""]
181 #[doc = "# Returns"]
182 #[doc = ""]
183 #[doc = concat!("Returns the decoded `", stringify!($ty), "`.")]
184 #[doc = ""]
185 #[doc = "# Errors"]
186 #[doc = ""]
187 #[doc = "Returns an input error or an invalid-data error when the \
188 payload is malformed or violates the selected policy."]
189 #[inline(always)]
190 pub fn $method(&mut self) -> Result<$ty> {
191 type Codec = Leb128Codec<$ty, $policy>;
192
193 self.input.read_decoded::<Codec>()
194 }
195 };
196}
197
198macro_rules! impl_for_policy {
199 (
200 $policy:ty,
201 $read_usize:ident,
202 $read_u64:ident,
203 $read_utf8_string_usize:ident,
204 $read_utf8_string_u64:ident,
205 {
206 $(($method:ident, $ty:ty, $doc:literal)),* $(,)?
207 }
208 $(,)?
209 ) => {
210 impl<R> BufferedLeb128Reader<R, $policy>
211 where
212 R: ByteInput,
213 {
214 $(
215 impl_read_value!($policy, $method, $ty, $doc);
216 )*
217
218 /// Reads a UTF-8 string prefixed by an unsigned LEB128 byte length.
219 ///
220 /// The length prefix is decoded as usize, so this format is
221 /// target-width dependent. Prefer a fixed-width length prefix for
222 /// persistent files and cross-platform protocols.
223 ///
224 /// # Parameters
225 ///
226 /// - max_len: Maximum accepted UTF-8 payload length in bytes.
227 ///
228 /// # Returns
229 ///
230 /// Returns the decoded UTF-8 string.
231 ///
232 /// # Errors
233 ///
234 /// Returns an input or allocation error, or an invalid-data error
235 /// for a malformed or excessive length or invalid UTF-8.
236 ///
237 /// # Stream position
238 ///
239 /// If the decoded length exceeds `max_len`, the length prefix has been
240 /// consumed logically, but the payload has not. If UTF-8 validation
241 /// fails, the prefix and complete payload have been consumed logically.
242 /// The underlying input may already have prefetched payload bytes;
243 /// its position is not the logical consumption position. These operations
244 /// do not roll back; recover only at an outer framing boundary or a
245 /// trusted seek checkpoint. See [`StringReadExt`](crate::StringReadExt)
246 /// for the shared length-prefixed read contract.
247 #[inline]
248 pub fn $read_utf8_string_usize(
249 &mut self,
250 max_len: usize,
251 ) -> Result<String> {
252 let len = self.$read_usize()?;
253 read_utf8_payload(&mut self.input, len, max_len)
254 }
255
256 /// Reads a UTF-8 string prefixed by an unsigned LEB128 u64 byte
257 /// length.
258 ///
259 /// Prefer this method over the usize variant for persistent files
260 /// and cross-platform protocols because the length field is
261 /// independent of the current Rust target's pointer width.
262 ///
263 /// # Parameters
264 ///
265 /// - max_len: Maximum accepted UTF-8 payload length in bytes.
266 ///
267 /// # Returns
268 ///
269 /// Returns the decoded UTF-8 string.
270 ///
271 /// # Errors
272 ///
273 /// Returns an input, conversion, or allocation error, or an
274 /// invalid-data error for a malformed or excessive length or
275 /// invalid UTF-8.
276 ///
277 /// # Stream position
278 ///
279 /// If the decoded length exceeds `max_len`, the length prefix has been
280 /// consumed logically, but the payload has not. If UTF-8 validation
281 /// fails, the prefix and complete payload have been consumed logically.
282 /// The underlying input may already have prefetched payload bytes;
283 /// its position is not the logical consumption position. These operations
284 /// do not roll back; recover only at an outer framing boundary or a
285 /// trusted seek checkpoint. See [`StringReadExt`](crate::StringReadExt)
286 /// for the shared length-prefixed read contract.
287 #[inline]
288 pub fn $read_utf8_string_u64(
289 &mut self,
290 max_len: usize,
291 ) -> Result<String> {
292 let len = self.$read_u64()?;
293 #[cfg(target_pointer_width = "64")]
294 let len = len as usize;
295 #[cfg(not(target_pointer_width = "64"))]
296 let len = usize_from_u64_len(len)?;
297 read_utf8_payload(&mut self.input, len, max_len)
298 }
299 }
300 };
301}
302
303impl_for_policy!(
304 NonStrict,
305 read_usize_non_strict,
306 read_u64_non_strict,
307 read_utf8_string_usize_non_strict,
308 read_utf8_string_u64_non_strict,
309 {
310 (read_u8_non_strict, u8, "Reads an unsigned LEB128 u8."),
311 (read_u16_non_strict, u16, "Reads an unsigned LEB128 u16."),
312 (read_u32_non_strict, u32, "Reads an unsigned LEB128 u32."),
313 (read_u64_non_strict, u64, "Reads an unsigned LEB128 u64."),
314 (read_u128_non_strict, u128, "Reads an unsigned LEB128 u128."),
315 (read_usize_non_strict, usize, "Reads an unsigned LEB128 usize."),
316 (read_i8_non_strict, i8, "Reads a signed LEB128 i8."),
317 (read_i16_non_strict, i16, "Reads a signed LEB128 i16."),
318 (read_i32_non_strict, i32, "Reads a signed LEB128 i32."),
319 (read_i64_non_strict, i64, "Reads a signed LEB128 i64."),
320 (read_i128_non_strict, i128, "Reads a signed LEB128 i128."),
321 (read_isize_non_strict, isize, "Reads a signed LEB128 isize."),
322 },
323);
324impl_for_policy!(
325 Strict,
326 read_usize,
327 read_u64,
328 read_utf8_string_usize,
329 read_utf8_string_u64,
330 {
331 (read_u8, u8, "Reads an unsigned LEB128 u8."),
332 (read_u16, u16, "Reads an unsigned LEB128 u16."),
333 (read_u32, u32, "Reads an unsigned LEB128 u32."),
334 (read_u64, u64, "Reads an unsigned LEB128 u64."),
335 (read_u128, u128, "Reads an unsigned LEB128 u128."),
336 (read_usize, usize, "Reads an unsigned LEB128 usize."),
337 (read_i8, i8, "Reads a signed LEB128 i8."),
338 (read_i16, i16, "Reads a signed LEB128 i16."),
339 (read_i32, i32, "Reads a signed LEB128 i32."),
340 (read_i64, i64, "Reads a signed LEB128 i64."),
341 (read_i128, i128, "Reads a signed LEB128 i128."),
342 (read_isize, isize, "Reads a signed LEB128 isize."),
343 },
344);
345impl<R, P> Input for BufferedLeb128Reader<R, P>
346where
347 R: ByteInput,
348{
349 type Item = u8;
350
351 /// Reports that this adapter buffers input.
352 ///
353 /// # Returns
354 ///
355 /// Always returns `true`.
356 #[inline(always)]
357 fn is_buffered(&self) -> bool {
358 true
359 }
360
361 /// Reads up to `count` bytes into an indexed output range.
362 ///
363 /// # Parameters
364 ///
365 /// - `output`: Destination slice.
366 /// - `index`: First destination index.
367 /// - `count`: Maximum number of bytes to read.
368 ///
369 /// # Returns
370 ///
371 /// Returns the number of bytes read.
372 ///
373 /// # Errors
374 ///
375 /// Returns an error reported while reading the buffered input.
376 ///
377 /// # Safety
378 ///
379 /// `index..index + count` must be a valid range within `output`.
380 #[inline(always)]
381 unsafe fn read_unchecked(&mut self, output: &mut [u8], index: usize, count: usize) -> Result<usize> {
382 // SAFETY: The caller upholds the indexed destination range contract.
383 unsafe { self.input.read_unchecked(output, index, count) }
384 }
385}
386
387impl<R, P> Seekable for BufferedLeb128Reader<R, P>
388where
389 R: SeekableByteInput,
390{
391 type Unit = u8;
392
393 /// Seeks relative to this reader's logical position. Relative seeks within
394 /// the buffered window preserve unread bytes; successful seeks outside the
395 /// window discard the old buffer. The returned position is logical.
396 ///
397 /// # Parameters
398 ///
399 /// - `position`: Target seek position.
400 ///
401 /// # Returns
402 ///
403 /// Returns the new logical stream position in bytes.
404 ///
405 /// # Errors
406 ///
407 /// Returns the seek error reported by the wrapped reader and preserves
408 /// buffered bytes when seeking fails, including when an in-window seek
409 /// cannot query the wrapped position.
410 #[inline(always)]
411 fn seek_to(&mut self, position: SeekFrom) -> Result<u64> {
412 self.input.seek(position)
413 }
414}