euv_docs/codec/impl.rs
1use super::*;
2
3/// Human-readable diagnostic formatting for `DocsCodecError`.
4impl fmt::Display for DocsCodecError {
5 /// Formats a descriptive error string for build logs and boot diagnostics.
6 ///
7 /// # Arguments
8 ///
9 /// - `&Self` - Error variant reference.
10 /// - `&mut fmt::Formatter<'_>` - Output formatter.
11 ///
12 /// # Returns
13 ///
14 /// - `fmt::Result` - Formatting result.
15 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
16 match self {
17 Self::BadMagic => write!(f, "{CODEC_ERR_BAD_MAGIC}"),
18 Self::UnsupportedVersion(version) => write!(f, "{CODEC_ERR_VERSION}: {version}"),
19 Self::Truncated => write!(f, "{CODEC_ERR_TRUNCATED}"),
20 Self::InvalidCodeLengths => write!(f, "{CODEC_ERR_CODE_LENGTHS}"),
21 Self::InvalidSymbol => write!(f, "{CODEC_ERR_SYMBOL}"),
22 Self::InvalidDistance => write!(f, "{CODEC_ERR_DISTANCE}"),
23 Self::LengthMismatch => write!(f, "{CODEC_ERR_LENGTH}"),
24 Self::PageCountMismatch => write!(f, "{CODEC_ERR_PAGE_COUNT}"),
25 Self::InvalidPayload(reason) => write!(f, "{CODEC_ERR_PAYLOAD}: {reason}"),
26 }
27 }
28}
29
30/// Bit-level streaming helpers for `CodecBitWriter`.
31impl CodecBitWriter {
32 /// Creates an empty bit writer preallocated for approximately `capacity` output bytes.
33 ///
34 /// # Arguments
35 ///
36 /// - `usize` - Estimated output byte capacity.
37 ///
38 /// # Returns
39 ///
40 /// - `Self` - Initialized bit writer.
41 pub(crate) fn with_capacity(capacity: usize) -> Self {
42 Self {
43 bytes: Vec::with_capacity(capacity),
44 bit_buf: 0,
45 bit_count: 0,
46 }
47 }
48
49 /// Appends the lowest `count` bits of `bits` in LSB-first order.
50 ///
51 /// # Arguments
52 ///
53 /// - `&Self` - Mutable bit writer reference.
54 /// - `u64` - Bit payload (only lowest `count` bits are used).
55 /// - `u32` - Number of bits to append (`0..=32`).
56 pub(crate) fn write_bits(&mut self, bits: u64, count: u32) {
57 if count == 0 {
58 return;
59 }
60 let next_buf: u64 = self.get_bit_buf() | (bits << self.get_bit_count());
61 let mut next_count: u32 = self.get_bit_count() + count;
62 let mut cur_buf: u64 = next_buf;
63 while next_count >= 8 {
64 self.get_mut_bytes().push((cur_buf & 0xFF) as u8);
65 cur_buf >>= 8;
66 next_count -= 8;
67 }
68 self.set_bit_buf(cur_buf);
69 self.set_bit_count(next_count);
70 }
71
72 /// Flushes any trailing partial byte padded with zero bits and returns the encoded byte vector.
73 ///
74 /// # Arguments
75 ///
76 /// - `Self` - Consumed bit writer.
77 ///
78 /// # Returns
79 ///
80 /// - `Vec<u8>` - Complete encoded byte stream.
81 pub(crate) fn finish(mut self) -> Vec<u8> {
82 if self.get_bit_count() > 0 {
83 let tail: u8 = (self.get_bit_buf() & 0xFF) as u8;
84 self.get_mut_bytes().push(tail);
85 self.set_bit_buf(0);
86 self.set_bit_count(0);
87 }
88 take(self.get_mut_bytes())
89 }
90}
91
92/// Bit-level streaming helpers for `CodecBitReader`.
93impl<'a> CodecBitReader<'a> {
94 /// Creates a new bit reader positioned at byte offset `0` of `data`.
95 ///
96 /// # Arguments
97 ///
98 /// - `&'a [u8]` - Compressed byte slice positioned after the stream header.
99 ///
100 /// # Returns
101 ///
102 /// - `Self` - Initialized bit reader.
103 pub(crate) fn from_slice(data: &'a [u8]) -> Self {
104 Self {
105 data,
106 cursor: 0,
107 bit_buf: 0,
108 bit_count: 0,
109 }
110 }
111
112 /// Refills the bit buffer from `data` until at least `CODEC_REFILL_THRESHOLD` bits are buffered or input ends.
113 ///
114 /// # Arguments
115 ///
116 /// - `&Self` - Mutable bit reader reference.
117 pub(crate) fn refill(&mut self) {
118 let slice: &[u8] = self.get_data();
119 let mut pos: usize = self.get_cursor();
120 let mut buf: u64 = self.get_bit_buf();
121 let mut count: u32 = self.get_bit_count();
122 while count <= CODEC_REFILL_THRESHOLD && pos < slice.len() {
123 buf |= (slice[pos] as u64) << count;
124 pos += 1;
125 count += 8;
126 }
127 self.set_cursor(pos);
128 self.set_bit_buf(buf);
129 self.set_bit_count(count);
130 }
131
132 /// Reads exactly `count` bits in LSB-first order, failing with `DocsCodecError::Truncated` if unavailable.
133 ///
134 /// # Arguments
135 ///
136 /// - `&Self` - Mutable bit reader reference.
137 /// - `u32` - Number of bits to read (`0..=32`).
138 ///
139 /// # Returns
140 ///
141 /// - `Result<u32, DocsCodecError>` - Extracted bit value or truncation error.
142 pub(crate) fn read_bits(&mut self, count: u32) -> Result<u32, DocsCodecError> {
143 if count == 0 {
144 return Ok(0);
145 }
146 if self.get_bit_count() < count {
147 self.refill();
148 if self.get_bit_count() < count {
149 return Err(DocsCodecError::Truncated);
150 }
151 }
152 let mask: u64 = (1u64 << count) - 1;
153 let value: u32 = (self.get_bit_buf() & mask) as u32;
154 self.set_bit_buf(self.get_bit_buf() >> count);
155 self.set_bit_count(self.get_bit_count() - count);
156 Ok(value)
157 }
158
159 /// Consumes `count` already-peeked bits from the buffer.
160 ///
161 /// # Arguments
162 ///
163 /// - `&Self` - Mutable bit reader reference.
164 /// - `u32` - Number of bits to drop.
165 pub(crate) fn consume(&mut self, count: u32) {
166 self.set_bit_buf(self.get_bit_buf() >> count);
167 self.set_bit_count(self.get_bit_count() - count);
168 }
169}