forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB30d372eTahinli3 months ago42 commits

bits.rs112 lines, 3.6 KiBHistoryRawDownload
1//! LSB-first bit buffer sized for table-driven Huffman decoding.
2//!
3//! DEFLATE reads its bits low-end first and its Huffman codes high-bit first,
4//! so decoding wants to *look* at the next fifteen bits before knowing how many
5//! of them belong to the code. [`ec_core::BitReaderLsb`] has no peek and refills
6//! one bit at a time, so this is its own accumulator: up to 64 bits in a `u64`,
7//! refilled a byte at a time, peeked without consuming.
8//!
9//! Past the end of the input the buffer reads as zeros; a code that would need
10//! those zeros fails at [`Bits::consume`] with [`Error::Truncated`]. Nothing
11//! here ever indexes out of bounds or panics on short input.
12
13use crate::Error;
14
15pub(crate) struct Bits<'a> {
16 src: &'a [u8],
17 /// Index of the next byte to pull into `buf`.
18 pos: usize,
19 /// The lowest `count` bits are real; everything above is zero padding.
20 buf: u64,
21 count: u32,
22}
23
24impl<'a> Bits<'a> {
25 pub(crate) fn new(src: &'a [u8]) -> Bits<'a> {
26 Bits {
27 src,
28 pos: 0,
29 buf: 0,
30 count: 0,
31 }
32 }
33
34 /// Top the accumulator up to at least 57 bits, input permitting.
35 ///
36 /// Away from the end of the input that is one unaligned 64-bit load: the
37 /// eight bytes are OR'd in above what is already there and the cursor moves
38 /// by however many of them fit, which is the whole cost of a symbol for
39 /// most symbols.
40 #[inline]
41 pub(crate) fn refill(&mut self) {
42 if let Some(next) = self.src.get(self.pos..self.pos + 8) {
43 let bytes = u64::from_le_bytes(next.try_into().expect("an eight byte slice"));
44 self.buf |= bytes << self.count;
45 self.pos += ((63 - self.count) >> 3) as usize;
46 self.count |= 56;
47 return;
48 }
49 while self.count <= 56 && self.pos < self.src.len() {
50 self.buf |= (self.src[self.pos] as u64) << self.count;
51 self.pos += 1;
52 self.count += 8;
53 }
54 }
55
56 /// The next `n` bits (`n <= 32`), zero-padded past end of input.
57 #[inline]
58 pub(crate) fn peek(&self, n: u32) -> u32 {
59 self.peek_at(0, n)
60 }
61
62 /// The `n` bits sitting `skip` bits ahead (`skip + n <= 32`).
63 #[inline]
64 pub(crate) fn peek_at(&self, skip: u32, n: u32) -> u32 {
65 ((self.buf >> skip) & ((1u64 << n) - 1)) as u32
66 }
67
68 /// Drop `n` bits already accounted for by a peek.
69 #[inline]
70 pub(crate) fn consume(&mut self, n: u32) -> Result<(), Error> {
71 if n > self.count {
72 return Err(Error::Truncated);
73 }
74 self.buf >>= n;
75 self.count -= n;
76 Ok(())
77 }
78
79 /// Read `n` bits (`n <= 32`) as a value.
80 #[inline]
81 pub(crate) fn take(&mut self, n: u32) -> Result<u32, Error> {
82 if n == 0 {
83 return Ok(0);
84 }
85 self.refill();
86 let value = self.peek(n);
87 self.consume(n)?;
88 Ok(value)
89 }
90
91 /// Drop the partial byte and hand back the byte offset now current.
92 ///
93 /// The accumulator is emptied, so the caller can read whole bytes straight
94 /// out of the source slice (stored blocks, the zlib trailer).
95 pub(crate) fn align(&mut self) -> usize {
96 let whole = (self.count / 8) as usize;
97 self.buf = 0;
98 self.count = 0;
99 self.pos -= whole;
100 self.pos
101 }
102
103 /// Move the byte cursor after a direct read from the source slice.
104 pub(crate) fn seek(&mut self, pos: usize) {
105 debug_assert_eq!(self.count, 0, "seek on an unaligned reader");
106 self.pos = pos;
107 }
108
109 pub(crate) fn src(&self) -> &'a [u8] {
110 self.src
111 }
112}