Debian Codegraph @

src/block.rs

raw symbols SCIP
1 use crate::{Bitstream, DecodeFailed, DecoderState, Tree};
2
3 // if position_slot < 4 {
4 // 0
5 // } else if position_slot >= 36 {
6 // 17
7 // } else {
8 // (position_slot - 2) / 2
9 // }
10 const FOOTER_BITS: [u8; 289] = [
11 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13,
12 13, 14, 14, 15, 15, 16, 16, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
13 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
14 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
15 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
16 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
17 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
18 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
19 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
20 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
21 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
22 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
23 ];
24
25 // if position_slot == 0 {
26 // 0
27 // } else {
28 // BASE_POSITION[position_slot - 1] + (1 << FOOTER_BITS[position_slot - 1])
29 // }
30 const BASE_POSITION: [u32; 290] = [
31 0, 1, 2, 3, 4, 6, 8, 12, 16, 24, 32, 48, 64, 96, 128, 192, 256, 384, 512, 768, 1024, 1536,
32 2048, 3072, 4096, 6144, 8192, 12288, 16384, 24576, 32768, 49152, 65536, 98304, 131072, 196608,
33 262144, 393216, 524288, 655360, 786432, 917504, 1048576, 1179648, 1310720, 1441792, 1572864,
34 1703936, 1835008, 1966080, 2097152, 2228224, 2359296, 2490368, 2621440, 2752512, 2883584,
35 3014656, 3145728, 3276800, 3407872, 3538944, 3670016, 3801088, 3932160, 4063232, 4194304,
36 4325376, 4456448, 4587520, 4718592, 4849664, 4980736, 5111808, 5242880, 5373952, 5505024,
37 5636096, 5767168, 5898240, 6029312, 6160384, 6291456, 6422528, 6553600, 6684672, 6815744,
38 6946816, 7077888, 7208960, 7340032, 7471104, 7602176, 7733248, 7864320, 7995392, 8126464,
39 8257536, 8388608, 8519680, 8650752, 8781824, 8912896, 9043968, 9175040, 9306112, 9437184,
40 9568256, 9699328, 9830400, 9961472, 10092544, 10223616, 10354688, 10485760, 10616832, 10747904,
41 10878976, 11010048, 11141120, 11272192, 11403264, 11534336, 11665408, 11796480, 11927552,
42 12058624, 12189696, 12320768, 12451840, 12582912, 12713984, 12845056, 12976128, 13107200,
43 13238272, 13369344, 13500416, 13631488, 13762560, 13893632, 14024704, 14155776, 14286848,
44 14417920, 14548992, 14680064, 14811136, 14942208, 15073280, 15204352, 15335424, 15466496,
45 15597568, 15728640, 15859712, 15990784, 16121856, 16252928, 16384000, 16515072, 16646144,
46 16777216, 16908288, 17039360, 17170432, 17301504, 17432576, 17563648, 17694720, 17825792,
47 17956864, 18087936, 18219008, 18350080, 18481152, 18612224, 18743296, 18874368, 19005440,
48 19136512, 19267584, 19398656, 19529728, 19660800, 19791872, 19922944, 20054016, 20185088,
49 20316160, 20447232, 20578304, 20709376, 20840448, 20971520, 21102592, 21233664, 21364736,
50 21495808, 21626880, 21757952, 21889024, 22020096, 22151168, 22282240, 22413312, 22544384,
51 22675456, 22806528, 22937600, 23068672, 23199744, 23330816, 23461888, 23592960, 23724032,
52 23855104, 23986176, 24117248, 24248320, 24379392, 24510464, 24641536, 24772608, 24903680,
53 25034752, 25165824, 25296896, 25427968, 25559040, 25690112, 25821184, 25952256, 26083328,
54 26214400, 26345472, 26476544, 26607616, 26738688, 26869760, 27000832, 27131904, 27262976,
55 27394048, 27525120, 27656192, 27787264, 27918336, 28049408, 28180480, 28311552, 28442624,
56 28573696, 28704768, 28835840, 28966912, 29097984, 29229056, 29360128, 29491200, 29622272,
57 29753344, 29884416, 30015488, 30146560, 30277632, 30408704, 30539776, 30670848, 30801920,
58 30932992, 31064064, 31195136, 31326208, 31457280, 31588352, 31719424, 31850496, 31981568,
59 32112640, 32243712, 32374784, 32505856, 32636928, 32768000, 32899072, 33030144, 33161216,
60 33292288, 33423360,
61 ];
62
63 struct DecodeInfo<'a> {
64 aligned_offset_tree: Option<&'a Tree>,
65 main_tree: &'a Tree,
66 length_tree: Option<&'a Tree>,
67 }
68
69 #[derive(Debug)]
70 pub enum Decoded {
71 Single(u8),
72 Match { offset: usize, length: usize },
73 Read(usize),
74 }
75
76 #[derive(Debug)]
77 pub enum Kind {
78 Verbatim {
79 main_tree: Tree,
80 length_tree: Option<Tree>,
81 },
82 AlignedOffset {
83 aligned_offset_tree: Tree,
84 main_tree: Tree,
85 length_tree: Option<Tree>,
86 },
87 Uncompressed {
88 r: [u32; 3],
89 },
90 }
91
92 /// Note that this is not the block header, but the head of the block's body, which includes
93 /// everything except the tail of the block data (either uncompressed data or token sequence).
94 pub struct Block {
95 /// Only 24 bits may be used.
96 pub remaining: u32,
97 pub size: u32,
98 pub kind: Kind,
99 }
100
101 /// Read the pretrees for the main and length tree, and with those also read the trees
102 /// themselves, using the path lengths from a previous tree if any.
103 ///
104 /// This is used when reading a verbatim or aligned block.
105 fn read_main_and_length_trees(
106 bitstream: &mut Bitstream,
107 state: &mut DecoderState,
108 ) -> Result<(), DecodeFailed> {
109 // Verbatim block
110 // Entry Comments
111 // Pretree for first 256 elements of main tree 20 elements, 4 bits each
112 // Path lengths of first 256 elements of main tree Encoded using pretree
113 // Pretree for remainder of main tree 20 elements, 4 bits each
114 // Path lengths of remaining elements of main tree Encoded using pretree
115 // Pretree for length tree 20 elements, 4 bits each
116 // Path lengths of elements in length tree Encoded using pretree
117 // Token sequence (matches and literals) Specified in section 2.6
118
119 state
120 .main_tree
121 .update_range_with_pretree(bitstream, 0..256)?;
122
123 state
124 .main_tree
125 .update_range_with_pretree(bitstream, 256..256 + 8 * state.window_size.position_slots())?;
126
127 state
128 .length_tree
129 .update_range_with_pretree(bitstream, 0..249)?;
130
131 Ok(())
132 }
133
134 fn decode_element(
135 bitstream: &mut Bitstream,
136 r: &mut [u32; 3],
137 DecodeInfo {
138 aligned_offset_tree,
139 main_tree,
140 length_tree,
141 }: DecodeInfo,
142 ) -> Result<Decoded, DecodeFailed> {
143 // Decoding Matches and Literals (Aligned and Verbatim Blocks)
144 let main_element = main_tree.decode_element(bitstream)?;
145
146 // Check if it is a literal character.
147 Ok(if main_element < 256 {
148 // It is a literal, so copy the literal to output.
149 Decoded::Single(main_element as u8)
150 } else {
151 // Decode the match. For a match, there are two components, offset and length.
152 let length_header = (main_element - 256) & 7;
153
154 let match_length = if length_header == 7 {
155 // Length of the footer.
156 length_tree
157 .ok_or(DecodeFailed::EmptyTree)?
158 .decode_element(bitstream)?
159 + 7
160 + 2
161 } else {
162 length_header + 2 // no length footer
163 // Decoding a match length (if a match length < 257).
164 };
165 assert_ne!(match_length, 0);
166
167 let position_slot = (main_element - 256) >> 3;
168
169 // Check for repeated offsets (positions 0, 1, 2).
170 let match_offset;
171 if position_slot == 0 {
172 match_offset = r[0];
173 } else if position_slot == 1 {
174 match_offset = r[1];
175 r.swap(0, 1);
176 } else if position_slot == 2 {
177 match_offset = r[2];
178 r.swap(0, 2);
179 } else {
180 // Not a repeated offset.
181 let offset_bits = FOOTER_BITS[position_slot as usize];
182
183 let formatted_offset = if let Some(aligned_offset_tree) = aligned_offset_tree.as_ref() {
184 let verbatim_bits;
185 let aligned_bits;
186
187 // This means there are some aligned bits.
188 if offset_bits >= 3 {
189 verbatim_bits = bitstream.read_bits(offset_bits - 3)? << 3;
190 aligned_bits = aligned_offset_tree.decode_element(bitstream)?;
191 } else {
192 // 0, 1, or 2 verbatim bits
193 verbatim_bits = bitstream.read_bits(offset_bits)?;
194 aligned_bits = 0;
195 }
196
197 BASE_POSITION[position_slot as usize] + verbatim_bits + aligned_bits as u32
198 } else {
199 // Block_type is a verbatim_block.
200 let verbatim_bits = bitstream.read_bits(offset_bits)?;
201 BASE_POSITION[position_slot as usize] + verbatim_bits
202 };
203
204 // Decoding a match offset.
205 match_offset = formatted_offset - 2;
206
207 // Update repeated offset least recently used queue.
208 r[2] = r[1];
209 r[1] = r[0];
210 r[0] = match_offset;
211 }
212
213 // Check for extra length.
214 // > If the match length is 257 or larger, the encoded match length token
215 // > (or match length, as specified in section 2.6) value is 257, and an
216 // > encoded Extra Length field follows the other match encoding components,
217 // > as specified in section 2.6.7, in the bitstream.
218
219 // TODO for some reason, if we do this, parsing .xnb files with window size
220 // 64KB, it breaks and stops decompressing correctly, but no idea why.
221 /*
222 let match_length = if match_length == 257 {
223 // Decode the extra length.
224 let extra_len = if bitstream.read_bit() != 0 {
225 if bitstream.read_bit() != 0 {
226 if bitstream.read_bit() != 0 {
227 // > Prefix 0b111; Number of bits to decode 15;
228 bitstream.read_bits(15)
229 } else {
230 // > Prefix 0b110; Number of bits to decode 12;
231 bitstream.read_bits(12) + 1024 + 256
232 }
233 } else {
234 // > Prefix 0b10; Number of bits to decode 10;
235 bitstream.read_bits(10) + 256
236 }
237 } else {
238 // > Prefix 0b0; Number of bits to decode 8;
239 bitstream.read_bits(8)
240 };
241
242 // Get the match length (if match length >= 257).
243 // In all cases,
244 // > Base value to add to decoded value 257 + …
245 257 + extra_len
246 } else {
247 match_length as u16
248 };
249 */
250
251 // Get match length and offset. Perform copy and paste work.
252 Decoded::Match {
253 offset: match_offset as usize,
254 length: match_length as usize,
255 }
256 })
257 }
258
259 impl Block {
260 pub(crate) fn read(
261 bitstream: &mut Bitstream,
262 state: &mut DecoderState,
263 ) -> Result<Self, DecodeFailed> {
264 // > Each block of compressed data begins with a 3-bit Block Type field.
265 // > Of the eight possible values, only three are valid values for the Block Type
266 // > field.
267 let kind = bitstream.read_bits(3)? as u8;
268 let size = bitstream.read_u24_be()?;
269 if size == 0 {
270 return Err(DecodeFailed::InvalidBlockSize(size));
271 }
272
273 let kind = match kind {
274 0b001 => {
275 read_main_and_length_trees(bitstream, state)?;
276
277 Kind::Verbatim {
278 main_tree: state.main_tree.create_instance()?,
279 length_tree: state.length_tree.create_instance_allow_empty()?,
280 }
281 }
282 0b010 => {
283 // > encoding only the delta path lengths between the current and previous trees
284 //
285 // This means we don't need to worry about deltas on this tree.
286 let aligned_offset_tree = {
287 let mut path_lengths = Vec::with_capacity(8);
288 for _ in 0..8 {
289 path_lengths.push(bitstream.read_bits(3)? as u8);
290 }
291
292 Tree::from_path_lengths(path_lengths)?
293 };
294
295 // > An aligned offset block is identical to the verbatim block except for the
296 // > presence of the aligned offset tree preceding the other trees.
297 read_main_and_length_trees(bitstream, state)?;
298
299 Kind::AlignedOffset {
300 aligned_offset_tree,
301 main_tree: state.main_tree.create_instance()?,
302 length_tree: state.length_tree.create_instance_allow_empty()?,
303 }
304 }
305 0b011 => {
306 bitstream.align()?;
307 Kind::Uncompressed {
308 r: [
309 bitstream.read_u32_le()?,
310 bitstream.read_u32_le()?,
311 bitstream.read_u32_le()?,
312 ],
313 }
314 }
315 _ => return Err(DecodeFailed::InvalidBlock(kind)),
316 };
317
318 Ok(Block {
319 remaining: size,
320 size,
321 kind,
322 })
323 }
324
325 pub(crate) fn decode_element(
326 &self,
327 bitstream: &mut Bitstream,
328 r: &mut [u32; 3],
329 ) -> Result<Decoded, DecodeFailed> {
330 match &self.kind {
331 Kind::Verbatim {
332 main_tree,
333 length_tree,
334 } => decode_element(
335 bitstream,
336 r,
337 DecodeInfo {
338 aligned_offset_tree: None,
339 main_tree,
340 length_tree: length_tree.as_ref(),
341 },
342 ),
343 Kind::AlignedOffset {
344 aligned_offset_tree,
345 main_tree,
346 length_tree,
347 } => decode_element(
348 bitstream,
349 r,
350 DecodeInfo {
351 aligned_offset_tree: Some(aligned_offset_tree),
352 main_tree,
353 length_tree: length_tree.as_ref(),
354 },
355 ),
356 Kind::Uncompressed { r: new_r } => {
357 r.copy_from_slice(new_r);
358 Ok(Decoded::Read(self.remaining as usize))
359 }
360 }
361 }
362 }