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