weezl/decode.rs
1//! A module for all decoding needs.
2#[cfg(feature = "std")]
3use crate::error::StreamResult;
4use crate::error::{BufferResult, LzwError, LzwStatus, VectorResult};
5use crate::{BitOrder, Code, StreamBuf, MAX_CODESIZE, MAX_ENTRIES, STREAM_BUF_SIZE};
6
7use crate::alloc::{boxed::Box, vec, vec::Vec};
8#[cfg(feature = "std")]
9use std::io::{self, BufRead, Write};
10
11/// The state for decoding data with an LZW algorithm.
12///
13/// The same structure can be utilized with streams as well as your own buffers and driver logic.
14/// It may even be possible to mix them if you are sufficiently careful not to lose or skip any
15/// already decode data in the process.
16///
17/// This is a sans-IO implementation, meaning that it only contains the state of the decoder and
18/// the caller will provide buffers for input and output data when calling the basic
19/// [`decode_bytes`] method. Nevertheless, a number of _adapters_ are provided in the `into_*`
20/// methods for decoding with a particular style of common IO.
21///
22/// * [`decode`] for decoding once without any IO-loop.
23/// * [`into_async`] for decoding with the `futures` traits for asynchronous IO.
24/// * [`into_stream`] for decoding with the standard `io` traits.
25/// * [`into_vec`] for in-memory decoding.
26///
27/// [`decode_bytes`]: #method.decode_bytes
28/// [`decode`]: #method.decode
29/// [`into_async`]: #method.into_async
30/// [`into_stream`]: #method.into_stream
31/// [`into_vec`]: #method.into_vec
32pub struct Decoder {
33 state: Box<dyn Stateful + Send + 'static>,
34}
35
36/// A decoding stream sink.
37///
38/// See [`Decoder::into_stream`] on how to create this type.
39///
40/// [`Decoder::into_stream`]: struct.Decoder.html#method.into_stream
41#[cfg_attr(
42 not(feature = "std"),
43 deprecated = "This type is only useful with the `std` feature."
44)]
45#[cfg_attr(not(feature = "std"), allow(dead_code))]
46pub struct IntoStream<'d, W> {
47 decoder: &'d mut Decoder,
48 writer: W,
49 buffer: Option<StreamBuf<'d>>,
50 default_size: usize,
51}
52
53/// An async decoding sink.
54///
55/// See [`Decoder::into_async`] on how to create this type.
56///
57/// [`Decoder::into_async`]: struct.Decoder.html#method.into_async
58#[cfg(feature = "async")]
59pub struct IntoAsync<'d, W> {
60 decoder: &'d mut Decoder,
61 writer: W,
62 buffer: Option<StreamBuf<'d>>,
63 default_size: usize,
64}
65
66/// A decoding sink into a vector.
67///
68/// See [`Decoder::into_vec`] on how to create this type.
69///
70/// [`Decoder::into_vec`]: struct.Decoder.html#method.into_vec
71pub struct IntoVec<'d> {
72 decoder: &'d mut Decoder,
73 vector: &'d mut Vec<u8>,
74}
75
76trait Stateful {
77 fn advance(&mut self, inp: &[u8], out: &mut [u8]) -> BufferResult;
78 fn has_ended(&self) -> bool;
79 /// Ignore an end code and continue decoding (no implied reset).
80 fn restart(&mut self);
81 /// Reset the decoder to the beginning, dropping all buffers etc.
82 fn reset(&mut self);
83}
84
85#[derive(Clone)]
86struct Link {
87 prev: Code,
88 byte: u8,
89 first: u8,
90}
91
92#[derive(Clone)]
93struct DerivationBase {
94 code: Code,
95 first: u8,
96}
97
98#[derive(Default)]
99struct MsbBuffer {
100 /// A buffer of individual bits. The oldest code is kept in the high-order bits.
101 bit_buffer: u64,
102 /// A precomputed mask for this code.
103 code_mask: u16,
104 /// The current code size.
105 code_size: u8,
106 /// The number of bits in the buffer.
107 bits: u8,
108}
109
110#[derive(Default)]
111struct LsbBuffer {
112 /// A buffer of individual bits. The oldest code is kept in the high-order bits.
113 bit_buffer: u64,
114 /// A precomputed mask for this code.
115 code_mask: u16,
116 /// The current code size.
117 code_size: u8,
118 /// The number of bits in the buffer.
119 bits: u8,
120}
121
122trait CodeBuffer {
123 fn new(min_size: u8) -> Self;
124 fn reset(&mut self, min_size: u8);
125 fn bump_code_size(&mut self);
126
127 /// Retrieve the next symbol, refilling if necessary.
128 fn next_symbol(&mut self, inp: &mut &[u8]) -> Option<Code>;
129 /// Refill the internal buffer.
130 fn refill_bits(&mut self, inp: &mut &[u8]);
131
132 fn peek_bits(&self, code: &mut [Code; BURST]) -> usize;
133 fn consume_bits(&mut self, code_cnt: u8);
134
135 fn max_code(&self) -> Code;
136 fn code_size(&self) -> u8;
137}
138
139trait CodegenConstants {
140 const YIELD_ON_FULL: bool;
141}
142
143struct DecodeState<CodeBuffer, Constants: CodegenConstants> {
144 /// The original minimum code size.
145 min_size: u8,
146 /// The table of decoded codes.
147 table: Table,
148 /// The buffer of decoded data.
149 buffer: Buffer,
150 /// The link which we are still decoding and its original code.
151 last: Option<DerivationBase>,
152 /// The next code entry.
153 next_code: Code,
154 /// Code to reset all tables.
155 clear_code: Code,
156 /// Code to signal the end of the stream.
157 end_code: Code,
158 /// A stored flag if the end code has already appeared.
159 has_ended: bool,
160 /// If tiff then bumps are a single code sooner.
161 is_tiff: bool,
162 /// Do we allow stream to start without an explicit reset code?
163 implicit_reset: bool,
164 /// The buffer for decoded words.
165 code_buffer: CodeBuffer,
166 #[allow(dead_code)]
167 constants: core::marker::PhantomData<Constants>,
168}
169
170// We have a buffer of 64 bits. So at max size at most 5 units can be read at once without
171// refilling the buffer. At smaller code sizes there are more. We tune for 6 here, by slight
172// experimentation. This may be an architecture dependent constant.
173const BURST: usize = 6;
174
175struct Buffer {
176 bytes: Box<[u8]>,
177 read_mark: usize,
178 write_mark: usize,
179}
180
181struct Table {
182 inner: Vec<Link>,
183 depths: Vec<u16>,
184}
185
186/// Describes the static parameters for creating a decoder.
187#[derive(Clone, Debug)]
188pub struct Configuration {
189 order: BitOrder,
190 size: u8,
191 tiff: bool,
192 yield_on_full: bool,
193}
194
195impl Configuration {
196 /// Create a configuration to decode with the specified bit order and symbol size.
197 pub fn new(order: BitOrder, size: u8) -> Self {
198 super::assert_decode_size(size);
199 Configuration {
200 order,
201 size,
202 tiff: false,
203 yield_on_full: false,
204 }
205 }
206
207 /// Create a configuration for a TIFF compatible decoder.
208 pub fn with_tiff_size_switch(order: BitOrder, size: u8) -> Self {
209 super::assert_decode_size(size);
210 Configuration {
211 order,
212 size,
213 tiff: true,
214 yield_on_full: false,
215 }
216 }
217
218 /// Immediately yield to the caller when the decoder buffer is full.
219 ///
220 /// This can be used for `libtiff` compatibility. It will use a "relaxed" stream interpretation
221 /// that need not contain an explicit EOF. Instead, the decoder is expected to stop fetching
222 /// symbols when some out-of-band specified length of the decoded text has been reached. The
223 /// caller indicates this maximum length through the available output buffer space.
224 ///
225 /// Symbols afterwards must not be expected to be valid. On filling the output buffer space
226 /// completely, the decoder will return immediately to the caller instead of potentially
227 /// interpreting the following bit-stream (and returning an error on doing so).
228 ///
229 /// Default: `false`.
230 pub fn with_yield_on_full_buffer(self, do_yield: bool) -> Self {
231 Configuration {
232 yield_on_full: do_yield,
233 ..self
234 }
235 }
236
237 /// Create a new decoder with the define configuration.
238 pub fn build(self) -> Decoder {
239 Decoder {
240 state: Decoder::from_configuration(&self),
241 }
242 }
243}
244
245impl Decoder {
246 /// Create a new decoder with the specified bit order and symbol size.
247 ///
248 /// The algorithm for dynamically increasing the code symbol bit width is compatible with the
249 /// original specification. In particular you will need to specify an `Lsb` bit oder to decode
250 /// the data portion of a compressed `gif` image.
251 ///
252 /// # Panics
253 ///
254 /// The `size` needs to be in the interval `0..=12`.
255 pub fn new(order: BitOrder, size: u8) -> Self {
256 Configuration::new(order, size).build()
257 }
258
259 /// Create a TIFF compatible decoder with the specified bit order and symbol size.
260 ///
261 /// The algorithm for dynamically increasing the code symbol bit width is compatible with the
262 /// TIFF specification, which is a misinterpretation of the original algorithm for increasing
263 /// the code size. It switches one symbol sooner.
264 ///
265 /// # Panics
266 ///
267 /// The `size` needs to be in the interval `0..=12`.
268 pub fn with_tiff_size_switch(order: BitOrder, size: u8) -> Self {
269 Configuration::with_tiff_size_switch(order, size).build()
270 }
271
272 fn from_configuration(configuration: &Configuration) -> Box<dyn Stateful + Send + 'static> {
273 struct NoYield;
274 struct YieldOnFull;
275
276 impl CodegenConstants for NoYield {
277 const YIELD_ON_FULL: bool = false;
278 }
279
280 impl CodegenConstants for YieldOnFull {
281 const YIELD_ON_FULL: bool = true;
282 }
283
284 type Boxed = Box<dyn Stateful + Send + 'static>;
285 match (configuration.order, configuration.yield_on_full) {
286 (BitOrder::Lsb, false) => {
287 let mut state =
288 Box::new(DecodeState::<LsbBuffer, NoYield>::new(configuration.size));
289 state.is_tiff = configuration.tiff;
290 state as Boxed
291 }
292 (BitOrder::Lsb, true) => {
293 let mut state = Box::new(DecodeState::<LsbBuffer, YieldOnFull>::new(
294 configuration.size,
295 ));
296 state.is_tiff = configuration.tiff;
297 state as Boxed
298 }
299 (BitOrder::Msb, false) => {
300 let mut state =
301 Box::new(DecodeState::<MsbBuffer, NoYield>::new(configuration.size));
302 state.is_tiff = configuration.tiff;
303 state as Boxed
304 }
305 (BitOrder::Msb, true) => {
306 let mut state = Box::new(DecodeState::<MsbBuffer, YieldOnFull>::new(
307 configuration.size,
308 ));
309 state.is_tiff = configuration.tiff;
310 state as Boxed
311 }
312 }
313 }
314
315 /// Decode some bytes from `inp` and write result to `out`.
316 ///
317 /// This will consume a prefix of the input buffer and write decoded output into a prefix of
318 /// the output buffer. See the respective fields of the return value for the count of consumed
319 /// and written bytes. For the next call You should have adjusted the inputs accordingly.
320 ///
321 /// The call will try to decode and write as many bytes of output as available. It will be
322 /// much more optimized (and avoid intermediate buffering) if it is allowed to write a large
323 /// contiguous chunk at once.
324 ///
325 /// See [`into_stream`] for high-level functions (that are only available with the `std`
326 /// feature).
327 ///
328 /// [`into_stream`]: #method.into_stream
329 pub fn decode_bytes(&mut self, inp: &[u8], out: &mut [u8]) -> BufferResult {
330 self.state.advance(inp, out)
331 }
332
333 /// Decode a single chunk of lzw encoded data.
334 ///
335 /// This method requires the data to contain an end marker, and returns an error otherwise.
336 ///
337 /// This is a convenience wrapper around [`into_vec`]. Use the `into_vec` adapter to customize
338 /// buffer size, to supply an existing vector, to control whether an end marker is required, or
339 /// to preserve partial data in the case of a decoding error.
340 ///
341 /// [`into_vec`]: #into_vec
342 ///
343 /// # Example
344 ///
345 /// ```
346 /// use weezl::{BitOrder, decode::Decoder};
347 ///
348 /// // Encoded that was created with an encoder.
349 /// let data = b"\x80\x04\x81\x94l\x1b\x06\xf0\xb0 \x1d\xc6\xf1\xc8l\x19 \x10";
350 /// let decoded = Decoder::new(BitOrder::Msb, 9)
351 /// .decode(data)
352 /// .unwrap();
353 /// assert_eq!(decoded, b"Hello, world");
354 /// ```
355 pub fn decode(&mut self, data: &[u8]) -> Result<Vec<u8>, LzwError> {
356 let mut output = vec![];
357 self.into_vec(&mut output).decode_all(data).status?;
358 Ok(output)
359 }
360
361 /// Construct a decoder into a writer.
362 #[cfg(feature = "std")]
363 pub fn into_stream<W: Write>(&mut self, writer: W) -> IntoStream<'_, W> {
364 IntoStream {
365 decoder: self,
366 writer,
367 buffer: None,
368 default_size: STREAM_BUF_SIZE,
369 }
370 }
371
372 /// Construct a decoder into an async writer.
373 #[cfg(feature = "async")]
374 pub fn into_async<W: futures::io::AsyncWrite>(&mut self, writer: W) -> IntoAsync<'_, W> {
375 IntoAsync {
376 decoder: self,
377 writer,
378 buffer: None,
379 default_size: STREAM_BUF_SIZE,
380 }
381 }
382
383 /// Construct a decoder into a vector.
384 ///
385 /// All decoded data is appended and the vector is __not__ cleared.
386 ///
387 /// Compared to `into_stream` this interface allows a high-level access to decoding without
388 /// requires the `std`-feature. Also, it can make full use of the extra buffer control that the
389 /// special target exposes.
390 pub fn into_vec<'lt>(&'lt mut self, vec: &'lt mut Vec<u8>) -> IntoVec<'lt> {
391 IntoVec {
392 decoder: self,
393 vector: vec,
394 }
395 }
396
397 /// Check if the decoding has finished.
398 ///
399 /// No more output is produced beyond the end code that marked the finish of the stream. The
400 /// decoder may have read additional bytes, including padding bits beyond the last code word
401 /// but also excess bytes provided.
402 pub fn has_ended(&self) -> bool {
403 self.state.has_ended()
404 }
405
406 /// Ignore an end code and continue.
407 ///
408 /// This will _not_ reset any of the inner code tables and not have the effect of a clear code.
409 /// It will instead continue as if the end code had not been present. If no end code has
410 /// occurred then this is a no-op.
411 ///
412 /// You can test if an end code has occurred with [`has_ended`](#method.has_ended).
413 /// FIXME: clarify how this interacts with padding introduced after end code.
414 #[allow(dead_code)]
415 pub(crate) fn restart(&mut self) {
416 self.state.restart();
417 }
418
419 /// Reset all internal state.
420 ///
421 /// This produce a decoder as if just constructed with `new` but taking slightly less work. In
422 /// particular it will not deallocate any internal allocations. It will also avoid some
423 /// duplicate setup work.
424 pub fn reset(&mut self) {
425 self.state.reset();
426 }
427}
428
429#[cfg(feature = "std")]
430impl<'d, W: Write> IntoStream<'d, W> {
431 /// Decode data from a reader.
432 ///
433 /// This will read data until the stream is empty or an end marker is reached.
434 pub fn decode(&mut self, read: impl BufRead) -> StreamResult {
435 self.decode_part(read, false)
436 }
437
438 /// Decode data from a reader, requiring an end marker.
439 pub fn decode_all(mut self, read: impl BufRead) -> StreamResult {
440 self.decode_part(read, true)
441 }
442
443 /// Set the size of the intermediate decode buffer.
444 ///
445 /// A buffer of this size is allocated to hold one part of the decoded stream when no buffer is
446 /// available and any decoding method is called. No buffer is allocated if `set_buffer` has
447 /// been called. The buffer is reused.
448 ///
449 /// # Panics
450 /// This method panics if `size` is `0`.
451 pub fn set_buffer_size(&mut self, size: usize) {
452 assert_ne!(size, 0, "Attempted to set empty buffer");
453 self.default_size = size;
454 }
455
456 /// Use a particular buffer as an intermediate decode buffer.
457 ///
458 /// Calling this sets or replaces the buffer. When a buffer has been set then it is used
459 /// instead of dynamically allocating a buffer. Note that the size of the buffer is critical
460 /// for efficient decoding. Some optimization techniques require the buffer to hold one or more
461 /// previous decoded words. There is also additional overhead from `write` calls each time the
462 /// buffer has been filled.
463 ///
464 /// # Panics
465 /// This method panics if the `buffer` is empty.
466 pub fn set_buffer(&mut self, buffer: &'d mut [u8]) {
467 assert_ne!(buffer.len(), 0, "Attempted to set empty buffer");
468 self.buffer = Some(StreamBuf::Borrowed(buffer));
469 }
470
471 fn decode_part(&mut self, mut read: impl BufRead, must_finish: bool) -> StreamResult {
472 let IntoStream {
473 decoder,
474 writer,
475 buffer,
476 default_size,
477 } = self;
478
479 enum Progress {
480 Ok,
481 Done,
482 }
483
484 let mut bytes_read = 0;
485 let mut bytes_written = 0;
486
487 // Converting to mutable refs to move into the `once` closure.
488 let read_bytes = &mut bytes_read;
489 let write_bytes = &mut bytes_written;
490
491 let outbuf: &mut [u8] =
492 match { buffer.get_or_insert_with(|| StreamBuf::Owned(vec![0u8; *default_size])) } {
493 StreamBuf::Borrowed(slice) => &mut *slice,
494 StreamBuf::Owned(vec) => &mut *vec,
495 };
496 assert!(!outbuf.is_empty());
497
498 let once = move || {
499 // Try to grab one buffer of input data.
500 let data = read.fill_buf()?;
501
502 // Decode as much of the buffer as fits.
503 let result = decoder.decode_bytes(data, &mut outbuf[..]);
504 // Do the bookkeeping and consume the buffer.
505 *read_bytes += result.consumed_in;
506 *write_bytes += result.consumed_out;
507 read.consume(result.consumed_in);
508
509 // Handle the status in the result.
510 let done = result.status.map_err(|err| {
511 io::Error::new(io::ErrorKind::InvalidData, &*format!("{:?}", err))
512 })?;
513
514 // Check if we had any new data at all.
515 if let LzwStatus::NoProgress = done {
516 debug_assert_eq!(
517 result.consumed_out, 0,
518 "No progress means we have not decoded any data"
519 );
520 // In particular we did not finish decoding.
521 if must_finish {
522 return Err(io::Error::new(
523 io::ErrorKind::UnexpectedEof,
524 "No more data but no end marker detected",
525 ));
526 } else {
527 return Ok(Progress::Done);
528 }
529 }
530
531 // And finish by writing our result.
532 // TODO: we may lose data on error (also on status error above) which we might want to
533 // deterministically handle so that we don't need to restart everything from scratch as
534 // the only recovery strategy. Any changes welcome.
535 writer.write_all(&outbuf[..result.consumed_out])?;
536
537 Ok(if let LzwStatus::Done = done {
538 Progress::Done
539 } else {
540 Progress::Ok
541 })
542 };
543
544 // Decode chunks of input data until we're done.
545 let status = core::iter::repeat_with(once)
546 // scan+fuse can be replaced with map_while
547 .scan((), |(), result| match result {
548 Ok(Progress::Ok) => Some(Ok(())),
549 Err(err) => Some(Err(err)),
550 Ok(Progress::Done) => None,
551 })
552 .fuse()
553 .collect();
554
555 StreamResult {
556 bytes_read,
557 bytes_written,
558 status,
559 }
560 }
561}
562
563impl IntoVec<'_> {
564 /// Decode data from a slice.
565 ///
566 /// This will read data until the slice is empty or an end marker is reached.
567 pub fn decode(&mut self, read: &[u8]) -> VectorResult {
568 self.decode_part(read, false)
569 }
570
571 /// Decode data from a slice, requiring an end marker.
572 pub fn decode_all(mut self, read: &[u8]) -> VectorResult {
573 self.decode_part(read, true)
574 }
575
576 fn grab_buffer(&mut self) -> (&mut [u8], &mut Decoder) {
577 const CHUNK_SIZE: usize = 1 << 12;
578 let decoder = &mut self.decoder;
579 let length = self.vector.len();
580
581 // Use the vector to do overflow checks and w/e.
582 self.vector.reserve(CHUNK_SIZE);
583 // FIXME: decoding into uninit buffer?
584 self.vector.resize(length + CHUNK_SIZE, 0u8);
585
586 (&mut self.vector[length..], decoder)
587 }
588
589 fn decode_part(&mut self, part: &[u8], must_finish: bool) -> VectorResult {
590 let mut result = VectorResult {
591 consumed_in: 0,
592 consumed_out: 0,
593 status: Ok(LzwStatus::Ok),
594 };
595
596 enum Progress {
597 Ok,
598 Done,
599 }
600
601 // Converting to mutable refs to move into the `once` closure.
602 let read_bytes = &mut result.consumed_in;
603 let write_bytes = &mut result.consumed_out;
604 let mut data = part;
605
606 // A 64 MB buffer is quite large but should get alloc_zeroed.
607 // Note that the decoded size can be up to quadratic in code block.
608 let once = move || {
609 // Grab a new output buffer.
610 let (outbuf, decoder) = self.grab_buffer();
611
612 // Decode as much of the buffer as fits.
613 let result = decoder.decode_bytes(data, &mut outbuf[..]);
614 // Do the bookkeeping and consume the buffer.
615 *read_bytes += result.consumed_in;
616 *write_bytes += result.consumed_out;
617 data = &data[result.consumed_in..];
618
619 let unfilled = outbuf.len() - result.consumed_out;
620 let filled = self.vector.len() - unfilled;
621 self.vector.truncate(filled);
622
623 // Handle the status in the result.
624 match result.status {
625 Err(err) => Err(err),
626 Ok(LzwStatus::NoProgress) if must_finish => Err(LzwError::InvalidCode),
627 Ok(LzwStatus::NoProgress) | Ok(LzwStatus::Done) => Ok(Progress::Done),
628 Ok(LzwStatus::Ok) => Ok(Progress::Ok),
629 }
630 };
631
632 // Decode chunks of input data until we're done.
633 let status: Result<(), _> = core::iter::repeat_with(once)
634 // scan+fuse can be replaced with map_while
635 .scan((), |(), result| match result {
636 Ok(Progress::Ok) => Some(Ok(())),
637 Err(err) => Some(Err(err)),
638 Ok(Progress::Done) => None,
639 })
640 .fuse()
641 .collect();
642
643 if let Err(err) = status {
644 result.status = Err(err);
645 }
646
647 result
648 }
649}
650
651// This is implemented in a separate file, so that 1.34.2 does not parse it. Otherwise, it would
652// trip over the usage of await, which is a reserved keyword in that edition/version. It only
653// contains an impl block.
654#[cfg(feature = "async")]
655#[path = "decode_into_async.rs"]
656mod impl_decode_into_async;
657
658impl<C: CodeBuffer, CgC: CodegenConstants> DecodeState<C, CgC> {
659 fn new(min_size: u8) -> Self {
660 DecodeState {
661 min_size,
662 table: Table::new(),
663 buffer: Buffer::new(),
664 last: None,
665 clear_code: 1 << min_size,
666 end_code: (1 << min_size) + 1,
667 next_code: (1 << min_size) + 2,
668 has_ended: false,
669 is_tiff: false,
670 implicit_reset: true,
671 code_buffer: CodeBuffer::new(min_size),
672 constants: core::marker::PhantomData,
673 }
674 }
675
676 fn init_tables(&mut self) {
677 self.code_buffer.reset(self.min_size);
678 self.next_code = (1 << self.min_size) + 2;
679 self.table.init(self.min_size);
680 }
681
682 fn reset_tables(&mut self) {
683 self.code_buffer.reset(self.min_size);
684 self.next_code = (1 << self.min_size) + 2;
685 self.table.clear(self.min_size);
686 }
687}
688
689impl<C: CodeBuffer, CgC: CodegenConstants> Stateful for DecodeState<C, CgC> {
690 fn has_ended(&self) -> bool {
691 self.has_ended
692 }
693
694 fn restart(&mut self) {
695 self.has_ended = false;
696 }
697
698 fn reset(&mut self) {
699 self.table.init(self.min_size);
700 self.next_code = (1 << self.min_size) + 2;
701 self.buffer.read_mark = 0;
702 self.buffer.write_mark = 0;
703 self.last = None;
704 self.restart();
705 self.code_buffer = CodeBuffer::new(self.min_size);
706 }
707
708 fn advance(&mut self, mut inp: &[u8], mut out: &mut [u8]) -> BufferResult {
709 // Skip everything if there is nothing to do.
710 if self.has_ended {
711 return BufferResult {
712 consumed_in: 0,
713 consumed_out: 0,
714 status: Ok(LzwStatus::Done),
715 };
716 }
717
718 // Rough description:
719 // We will fill the output slice as much as possible until either there is no more symbols
720 // to decode or an end code has been reached. This requires an internal buffer to hold a
721 // potential tail of the word corresponding to the last symbol. This tail will then be
722 // decoded first before continuing with the regular decoding. The same buffer is required
723 // to persist some symbol state across calls.
724 //
725 // We store the words corresponding to code symbols in an index chain, bytewise, where we
726 // push each decoded symbol. (TODO: wuffs shows some success with 8-byte units). This chain
727 // is traversed for each symbol when it is decoded and bytes are placed directly into the
728 // output slice. In the special case (new_code == next_code) we use an existing decoded
729 // version that is present in either the out bytes of this call or in buffer to copy the
730 // repeated prefix slice.
731 // TODO: I played with a 'decoding cache' to remember the position of long symbols and
732 // avoid traversing the chain, doing a copy of memory instead. It did however not lead to
733 // a serious improvement. It's just unlikely to both have a long symbol and have that
734 // repeated twice in the same output buffer.
735 //
736 // You will also find the (to my knowledge novel) concept of a _decoding burst_ which
737 // gained some >~10% speedup in tests. This is motivated by wanting to use out-of-order
738 // execution as much as possible and for this reason have the least possible stress on
739 // branch prediction. Our decoding table already gives us a lookahead on symbol lengths but
740 // only for re-used codes, not novel ones. This lookahead also makes the loop termination
741 // when restoring each byte of the code word perfectly predictable! So a burst is a chunk
742 // of code words which are all independent of each other, have known lengths _and_ are
743 // guaranteed to fit into the out slice without requiring a buffer. One burst can be
744 // decoded in an extremely tight loop.
745 //
746 // TODO: since words can be at most (1 << MAX_CODESIZE) = 4096 bytes long we could avoid
747 // that intermediate buffer at the expense of not always filling the output buffer
748 // completely. Alternatively we might follow its chain of precursor states twice. This may
749 // be even cheaper if we store more than one byte per link so it really should be
750 // evaluated.
751 // TODO: if the caller was required to provide the previous last word we could also avoid
752 // the buffer for cases where we need it to restore the next code! This could be built
753 // backwards compatible by only doing it after an opt-in call that enables the behaviour.
754
755 // Record initial lengths for the result that is returned.
756 let o_in = inp.len();
757 let o_out = out.len();
758
759 // The code_link is the previously decoded symbol.
760 // It's used to link the new code back to its predecessor.
761 let mut code_link = None;
762 // The status, which is written to on an invalid code.
763 let mut status = Ok(LzwStatus::Ok);
764
765 match self.last.take() {
766 // No last state? This is the first code after a reset?
767 None => {
768 match self.next_symbol(&mut inp) {
769 // Plainly invalid code.
770 Some(code) if code > self.next_code => status = Err(LzwError::InvalidCode),
771 // next_code would require an actual predecessor.
772 Some(code) if code == self.next_code => status = Err(LzwError::InvalidCode),
773 // No more symbols available and nothing decoded yet.
774 // Assume that we didn't make progress, this may get reset to Done if we read
775 // some bytes from the input.
776 None => status = Ok(LzwStatus::NoProgress),
777 // Handle a valid code.
778 Some(init_code) => {
779 if init_code == self.clear_code {
780 self.init_tables();
781 } else if init_code == self.end_code {
782 self.has_ended = true;
783 status = Ok(LzwStatus::Done);
784 } else if self.table.is_empty() {
785 if self.implicit_reset {
786 self.init_tables();
787
788 self.buffer.fill_reconstruct(&self.table, init_code);
789 let link = self.table.at(init_code).clone();
790 code_link = Some(DerivationBase {
791 code: init_code,
792 first: link.first,
793 });
794 } else {
795 // We require an explicit reset.
796 status = Err(LzwError::InvalidCode);
797 }
798 } else {
799 // Reconstruct the first code in the buffer.
800 self.buffer.fill_reconstruct(&self.table, init_code);
801 let link = self.table.at(init_code).clone();
802 code_link = Some(DerivationBase {
803 code: init_code,
804 first: link.first,
805 });
806 }
807 }
808 }
809 }
810 // Move the tracking state to the stack.
811 Some(tup) => code_link = Some(tup),
812 };
813
814 // Track an empty `burst` (see below) means we made no progress.
815 let mut have_yet_to_decode_data = false;
816
817 // Restore the previous state, if any.
818 if code_link.is_some() {
819 let remain = self.buffer.buffer();
820 // Check if we can fully finish the buffer.
821 if remain.len() > out.len() {
822 if out.is_empty() {
823 // This also implies the buffer is _not_ empty and we will not enter any
824 // decoding loop.
825 status = Ok(LzwStatus::NoProgress);
826 } else {
827 out.copy_from_slice(&remain[..out.len()]);
828 self.buffer.consume(out.len());
829 out = &mut [];
830 }
831 } else if remain.is_empty() {
832 status = Ok(LzwStatus::NoProgress);
833 have_yet_to_decode_data = true;
834 } else {
835 let consumed = remain.len();
836 out[..consumed].copy_from_slice(remain);
837 self.buffer.consume(consumed);
838 out = &mut out[consumed..];
839 have_yet_to_decode_data = false;
840 }
841 }
842
843 // A special reference to out slice which holds the last decoded symbol.
844 let mut last_decoded: Option<&[u8]> = None;
845
846 if self.buffer.buffer().is_empty() {
847 // Hot loop that writes data to the output as long as we can do so directly from the
848 // input stream. As an invariant of this block we did not need to use the buffer to
849 // store a decoded code word. Testing the condition ahead of time avoids a test in the
850 // loop body since every code path where the buffer is filled already breaks.
851 //
852 // In a previous iteration of the code we trusted compiler optimization to work this
853 // out but it seems that it does not. Another edit hidden behind some performance work
854 // then edited out the check, inadvertently changing the behavior for callers that
855 // relied on being able to provide an empty output buffer and still receiving a useful
856 // signal about the state of the stream.
857
858 // A burst is a sequence of code words that are independently decoded, i.e. they do not
859 // change the state of the decoder in ways that would influence the interpretation of
860 // each other. That is: they are not special symbols, they do not make us increase the
861 // code size, they are each codes already in the tree before the burst.
862 //
863 // The tracking state for a burst. These are actually initialized later but compiler
864 // wasn't smart enough to fully optimize out the init code so that appears outside the
865 // loop.
866 let mut burst = [0; BURST];
867 let mut burst_byte_len = [0u16; BURST];
868 let mut burst_byte = [0u8; BURST];
869 let mut target: [&mut [u8]; BURST] = Default::default();
870
871 loop {
872 // In particular, we *also* break if the output buffer is still empty. Especially
873 // when the output parameter was an empty slice, we must try to fetch at least one
874 // code but with YIELD_ON_FULL we do not.
875 if CgC::YIELD_ON_FULL && out.is_empty() {
876 break;
877 }
878
879 let mut deriv = match code_link.take() {
880 Some(link) => link,
881 None => {
882 // TODO: we do not need to break here. This does not indicate that the buffer
883 // has been filled, rather it indicates we have reset the state. The next code
884 // should be part of the initial alphabet. However the first code is special in
885 // the sense of not creating a new code itself. This is handled correctly in
886 // the initialization prior to the loop; and in particular that handling as
887 // written currently relies on putting it into the buffer; so handling it we
888 // would need to ensure that either the buffer is fully cleared after its use,
889 // or use another implementation of handling that first code.
890 break;
891 }
892 };
893
894 // Ensure the code buffer is full, we're about to request some codes.
895 // Note that this also ensures at least one code is in the buffer if any input is left.
896 self.refill_bits(&mut inp);
897 let cnt = self.code_buffer.peek_bits(&mut burst);
898
899 // No code left in the buffer, and no more bytes to refill the buffer.
900 if cnt == 0 {
901 if have_yet_to_decode_data {
902 status = Ok(LzwStatus::NoProgress);
903 }
904
905 code_link = Some(deriv);
906 break;
907 }
908
909 debug_assert!(
910 // When the table is full, we have a max code one above the mask.
911 self.table.is_full()
912 // When the code size is 2 we have a bit code: (0, 1, CLS, EOF). Then the
913 // computed next_code is 4 which already exceeds the bit width from the start.
914 // Then we will immediately switch code size after this code.
915 //
916 // TODO: this is the reason for some saturating and non-sharp comparisons in
917 // the code below. Maybe it makes sense to revisit turning this into a compile
918 // time choice?
919 || (self.code_buffer.code_size() == 2 && self.next_code == 4)
920 || self.code_buffer.max_code() - Code::from(self.is_tiff) >= self.next_code,
921 );
922
923 let mut burst_size = 0;
924 let left_before_size_switch = (self.code_buffer.max_code()
925 - Code::from(self.is_tiff))
926 .saturating_sub(self.next_code);
927
928 // A burst is a sequence of decodes that are completely independent of each other. This
929 // is the case if neither is an end code, a clear code, or a next code, i.e. we have
930 // all of them in the decoding table and thus known their depths, and additionally if
931 // we can decode them directly into the output buffer.
932 for b in &burst[..cnt] {
933 // We can commit the previous burst code, and will take a slice from the output
934 // buffer. This also avoids the bounds check in the tight loop later.
935 if burst_size > 0 {
936 let len = burst_byte_len[burst_size - 1];
937 let (into, tail) = out.split_at_mut(usize::from(len));
938 target[burst_size - 1] = into;
939 out = tail;
940 }
941
942 // Check that we don't overflow the code size with all codes we burst decode.
943 burst_size += 1;
944
945 if burst_size > usize::from(left_before_size_switch) {
946 break;
947 }
948
949 let read_code = *b;
950
951 // A burst code can't be special.
952 if read_code == self.clear_code
953 || read_code == self.end_code
954 || read_code >= self.next_code
955 {
956 break;
957 }
958
959 // Read the code length and check that we can decode directly into the out slice.
960 let len = self.table.depths[usize::from(read_code)];
961
962 if out.len() < usize::from(len) {
963 break;
964 }
965
966 // We do exactly one more code (the one being inspected in the current iteration)
967 // after the 'burst'. When we want to break decoding precisely on the supplied
968 // buffer, we check if this is the last code to be decoded into it.
969 if CgC::YIELD_ON_FULL {
970 if out.len() == usize::from(len) {
971 break;
972 }
973 }
974
975 burst_byte_len[burst_size - 1] = len;
976 }
977
978 self.code_buffer.consume_bits(burst_size as u8);
979 have_yet_to_decode_data = false;
980
981 // Note that the very last code in the burst buffer doesn't actually belong to the
982 // burst itself. TODO: sometimes it could, we just don't differentiate between the
983 // breaks and a loop end condition above. That may be a speed advantage?
984 let (&new_code, burst) = burst[..burst_size].split_last().unwrap();
985
986 // The very tight loop for restoring the actual burst. These can be reconstructed in
987 // parallel since none of them depend on a prior constructed. Only the derivation of
988 // new codes is not parallel. There are no size changes here either.
989 let burst_targets = &mut target[..burst_size - 1];
990
991 if !self.table.is_full() {
992 self.next_code += burst_targets.len() as u16;
993 }
994
995 for ((&burst, target), byte) in
996 burst.iter().zip(&mut *burst_targets).zip(&mut burst_byte)
997 {
998 *byte = self.table.reconstruct(burst, target);
999 }
1000
1001 self.table.derive_burst(&mut deriv, burst, &burst_byte[..]);
1002
1003 // Now handle the special codes.
1004 if new_code == self.clear_code {
1005 self.reset_tables();
1006 last_decoded = None;
1007 // Restarts in the next call to the entry point.
1008 break;
1009 }
1010
1011 if new_code == self.end_code {
1012 self.has_ended = true;
1013 status = Ok(LzwStatus::Done);
1014 last_decoded = None;
1015 break;
1016 }
1017
1018 if new_code > self.next_code {
1019 status = Err(LzwError::InvalidCode);
1020 last_decoded = None;
1021 break;
1022 }
1023
1024 let required_len = if new_code == self.next_code {
1025 self.table.depths[usize::from(deriv.code)] + 1
1026 } else {
1027 self.table.depths[usize::from(new_code)]
1028 };
1029
1030 // We need the decoded data of the new code if it is the `next_code`. This is the
1031 // special case of LZW decoding that is demonstrated by `banana` (or form cScSc). In
1032 // all other cases we only need the first character of the decoded data.
1033 let have_next_code = new_code == self.next_code;
1034
1035 // Update the slice holding the last decoded word.
1036 if have_next_code {
1037 // If we did not have any burst code, we still hold that slice in the buffer.
1038 if let Some(new_last) = target[..burst_size - 1].last_mut() {
1039 let slice = core::mem::replace(new_last, &mut []);
1040 last_decoded = Some(&*slice);
1041 }
1042 }
1043
1044 let cha;
1045 let is_in_buffer = usize::from(required_len) > out.len();
1046 // Check if we will need to store our current state into the buffer.
1047 if is_in_buffer {
1048 if have_next_code {
1049 // last_decoded will be Some if we have restored any code into the out slice.
1050 // Otherwise it will still be present in the buffer.
1051 if let Some(last) = last_decoded.take() {
1052 self.buffer.bytes[..last.len()].copy_from_slice(last);
1053 self.buffer.write_mark = last.len();
1054 self.buffer.read_mark = last.len();
1055 }
1056
1057 cha = self.buffer.fill_cscsc();
1058 } else {
1059 // Restore the decoded word into the buffer.
1060 last_decoded = None;
1061 cha = self.buffer.fill_reconstruct(&self.table, new_code);
1062 }
1063 } else {
1064 let (target, tail) = out.split_at_mut(usize::from(required_len));
1065 out = tail;
1066
1067 if have_next_code {
1068 // Reconstruct high.
1069 let source = match last_decoded.take() {
1070 Some(last) => last,
1071 None => &self.buffer.bytes[..self.buffer.write_mark],
1072 };
1073
1074 // We don't *actually* expect the unwrap to happen. Each source is at least 1
1075 // byte long. But llvm doesn't know this (too much indirect loads and cases).
1076 cha = source.get(0).map(|x| *x).unwrap_or(0);
1077 target[..source.len()].copy_from_slice(source);
1078 target[source.len()..][0] = cha;
1079 } else {
1080 cha = self.table.reconstruct(new_code, target);
1081 }
1082
1083 // A new decoded word.
1084 last_decoded = Some(target);
1085 }
1086
1087 // Each newly read code creates one new code/link based on the preceding code if we
1088 // have enough space to put it there.
1089 if !self.table.is_full() {
1090 self.table.derive(&deriv, cha);
1091
1092 if self.next_code >= self.code_buffer.max_code() - Code::from(self.is_tiff)
1093 && self.code_buffer.code_size() < MAX_CODESIZE
1094 {
1095 self.bump_code_size();
1096 }
1097
1098 self.next_code += 1;
1099 }
1100
1101 // store the information on the decoded word.
1102 code_link = Some(DerivationBase {
1103 code: new_code,
1104 first: cha,
1105 });
1106
1107 // Can't make any more progress with decoding.
1108 //
1109 // We have more data buffered but not enough space to put it? We want fetch a next
1110 // symbol if possible as in the case of it being a new symbol we can refer to the
1111 // buffered output as the source for that symbol's meaning and do a memcpy.
1112 //
1113 // Since this test is after decoding at least one code, we can now check for an
1114 // empty buffer and still guarantee progress when one was passed as a parameter.
1115 if is_in_buffer || out.is_empty() {
1116 break;
1117 }
1118 }
1119 }
1120
1121 // We need to store the last word into the buffer in case the first code in the next
1122 // iteration is the next_code.
1123 if let Some(tail) = last_decoded {
1124 self.buffer.bytes[..tail.len()].copy_from_slice(tail);
1125 self.buffer.write_mark = tail.len();
1126 // Mark the full buffer as having been consumed.
1127 self.buffer.read_mark = tail.len();
1128 }
1129
1130 // Ensure we don't indicate that no progress was made if we read some bytes from the input
1131 // (which is progress).
1132 if o_in > inp.len() {
1133 if let Ok(LzwStatus::NoProgress) = status {
1134 status = Ok(LzwStatus::Ok);
1135 }
1136 }
1137
1138 // Store the code/link state.
1139 self.last = code_link;
1140
1141 BufferResult {
1142 consumed_in: o_in.wrapping_sub(inp.len()),
1143 consumed_out: o_out.wrapping_sub(out.len()),
1144 status,
1145 }
1146 }
1147}
1148
1149impl<C: CodeBuffer, CgC: CodegenConstants> DecodeState<C, CgC> {
1150 fn next_symbol(&mut self, inp: &mut &[u8]) -> Option<Code> {
1151 self.code_buffer.next_symbol(inp)
1152 }
1153
1154 fn bump_code_size(&mut self) {
1155 self.code_buffer.bump_code_size()
1156 }
1157
1158 fn refill_bits(&mut self, inp: &mut &[u8]) {
1159 self.code_buffer.refill_bits(inp)
1160 }
1161}
1162
1163impl CodeBuffer for MsbBuffer {
1164 fn new(min_size: u8) -> Self {
1165 MsbBuffer {
1166 code_size: min_size + 1,
1167 code_mask: (1u16 << (min_size + 1)) - 1,
1168 bit_buffer: 0,
1169 bits: 0,
1170 }
1171 }
1172
1173 fn reset(&mut self, min_size: u8) {
1174 self.code_size = min_size + 1;
1175 self.code_mask = (1 << self.code_size) - 1;
1176 }
1177
1178 fn next_symbol(&mut self, inp: &mut &[u8]) -> Option<Code> {
1179 if self.bits < self.code_size {
1180 self.refill_bits(inp);
1181 }
1182
1183 if self.bits < self.code_size {
1184 return None;
1185 }
1186
1187 let mask = u64::from(self.code_mask);
1188 let rotbuf = self.bit_buffer.rotate_left(self.code_size.into());
1189 self.bit_buffer = rotbuf & !mask;
1190 self.bits -= self.code_size;
1191 Some((rotbuf & mask) as u16)
1192 }
1193
1194 fn bump_code_size(&mut self) {
1195 self.code_size += 1;
1196 self.code_mask = (self.code_mask << 1) | 1;
1197 }
1198
1199 fn refill_bits(&mut self, inp: &mut &[u8]) {
1200 let wish_count = (64 - self.bits) / 8;
1201 let mut buffer = [0u8; 8];
1202 let new_bits = match inp.get(..usize::from(wish_count)) {
1203 Some(bytes) => {
1204 buffer[..usize::from(wish_count)].copy_from_slice(bytes);
1205 *inp = &inp[usize::from(wish_count)..];
1206 wish_count * 8
1207 }
1208 None => {
1209 let new_bits = inp.len() * 8;
1210 buffer[..inp.len()].copy_from_slice(inp);
1211 *inp = &[];
1212 new_bits as u8
1213 }
1214 };
1215 self.bit_buffer |= u64::from_be_bytes(buffer) >> self.bits;
1216 self.bits += new_bits;
1217 }
1218
1219 fn peek_bits(&self, code: &mut [Code; BURST]) -> usize {
1220 let mut bit_buffer = self.bit_buffer;
1221 let mask = u64::from(self.code_mask);
1222 let mut consumed = 0;
1223 let mut cnt = 0;
1224
1225 for b in code {
1226 let consumed_after = consumed + self.code_size;
1227 if consumed_after > self.bits {
1228 break;
1229 }
1230
1231 cnt += 1;
1232 consumed = consumed_after;
1233
1234 let rotbuf = bit_buffer.rotate_left(self.code_size.into());
1235 *b = (rotbuf & mask) as u16;
1236 // The read bits are 'appended' but we never interpret those appended bits.
1237 bit_buffer = rotbuf;
1238 }
1239
1240 cnt
1241 }
1242
1243 fn consume_bits(&mut self, code_cnt: u8) {
1244 let bits = self.code_size * code_cnt;
1245 debug_assert!(bits <= self.bits);
1246
1247 if bits >= self.bits {
1248 self.bit_buffer = 0;
1249 } else {
1250 // bits < self.bits so this must be smaller than the number size.
1251 self.bit_buffer = self.bit_buffer << bits;
1252 }
1253
1254 self.bits = self.bits.wrapping_sub(bits);
1255 }
1256
1257 fn max_code(&self) -> Code {
1258 self.code_mask
1259 }
1260
1261 fn code_size(&self) -> u8 {
1262 self.code_size
1263 }
1264}
1265
1266impl CodeBuffer for LsbBuffer {
1267 fn new(min_size: u8) -> Self {
1268 LsbBuffer {
1269 code_size: min_size + 1,
1270 code_mask: (1u16 << (min_size + 1)) - 1,
1271 bit_buffer: 0,
1272 bits: 0,
1273 }
1274 }
1275
1276 fn reset(&mut self, min_size: u8) {
1277 self.code_size = min_size + 1;
1278 self.code_mask = (1 << self.code_size) - 1;
1279 }
1280
1281 fn next_symbol(&mut self, inp: &mut &[u8]) -> Option<Code> {
1282 if self.bits < self.code_size {
1283 self.refill_bits(inp);
1284 }
1285
1286 if self.bits < self.code_size {
1287 return None;
1288 }
1289
1290 let mask = u64::from(self.code_mask);
1291 let code = self.bit_buffer & mask;
1292 self.bit_buffer >>= self.code_size;
1293 self.bits -= self.code_size;
1294 Some(code as u16)
1295 }
1296
1297 fn bump_code_size(&mut self) {
1298 self.code_size += 1;
1299 self.code_mask = (self.code_mask << 1) | 1;
1300 }
1301
1302 fn refill_bits(&mut self, inp: &mut &[u8]) {
1303 let wish_count = (64 - self.bits) / 8;
1304 let mut buffer = [0u8; 8];
1305 let new_bits = match inp.get(..usize::from(wish_count)) {
1306 Some(bytes) => {
1307 buffer[..usize::from(wish_count)].copy_from_slice(bytes);
1308 *inp = &inp[usize::from(wish_count)..];
1309 wish_count * 8
1310 }
1311 None => {
1312 let new_bits = inp.len() * 8;
1313 buffer[..inp.len()].copy_from_slice(inp);
1314 *inp = &[];
1315 new_bits as u8
1316 }
1317 };
1318 self.bit_buffer |= u64::from_be_bytes(buffer).swap_bytes() << self.bits;
1319 self.bits += new_bits;
1320 }
1321
1322 fn peek_bits(&self, code: &mut [Code; BURST]) -> usize {
1323 let mut bit_buffer = self.bit_buffer;
1324 let mask = u64::from(self.code_mask);
1325 let mut consumed = 0;
1326 let mut cnt = 0;
1327
1328 for b in code {
1329 let consumed_after = consumed + self.code_size;
1330 if consumed_after > self.bits {
1331 break;
1332 }
1333
1334 cnt += 1;
1335 consumed = consumed_after;
1336
1337 *b = (bit_buffer & mask) as u16;
1338 bit_buffer = bit_buffer >> self.code_size;
1339 }
1340
1341 cnt
1342 }
1343
1344 fn consume_bits(&mut self, code_cnt: u8) {
1345 let bits = self.code_size * code_cnt;
1346 debug_assert!(bits <= self.bits);
1347
1348 if bits >= self.bits {
1349 self.bit_buffer = 0;
1350 } else {
1351 // bits < self.bits so this must be smaller than the number size.
1352 self.bit_buffer = self.bit_buffer >> bits;
1353 }
1354
1355 self.bits = self.bits.wrapping_sub(bits);
1356 }
1357
1358 fn max_code(&self) -> Code {
1359 self.code_mask
1360 }
1361
1362 fn code_size(&self) -> u8 {
1363 self.code_size
1364 }
1365}
1366
1367impl Buffer {
1368 fn new() -> Self {
1369 Buffer {
1370 bytes: vec![0; MAX_ENTRIES].into_boxed_slice(),
1371 read_mark: 0,
1372 write_mark: 0,
1373 }
1374 }
1375
1376 /// When encoding a sequence `cScSc` where `c` is any character and `S` is any string
1377 /// this results in two codes `AB`, `A` encoding `cS` and `B` encoding `cSc`. Supposing
1378 /// the buffer is already filled with the reconstruction of `A`, we can easily fill it
1379 /// with the reconstruction of `B`.
1380 fn fill_cscsc(&mut self) -> u8 {
1381 self.bytes[self.write_mark] = self.bytes[0];
1382 self.write_mark += 1;
1383 self.read_mark = 0;
1384 self.bytes[0]
1385 }
1386
1387 // Fill the buffer by decoding from the table
1388 fn fill_reconstruct(&mut self, table: &Table, code: Code) -> u8 {
1389 self.write_mark = 0;
1390 self.read_mark = 0;
1391 let depth = table.depths[usize::from(code)];
1392 let mut memory = core::mem::replace(&mut self.bytes, Box::default());
1393
1394 let out = &mut memory[..usize::from(depth)];
1395 let last = table.reconstruct(code, out);
1396
1397 self.bytes = memory;
1398 self.write_mark = usize::from(depth);
1399 last
1400 }
1401
1402 fn buffer(&self) -> &[u8] {
1403 &self.bytes[self.read_mark..self.write_mark]
1404 }
1405
1406 fn consume(&mut self, amt: usize) {
1407 self.read_mark += amt;
1408 }
1409}
1410
1411impl Table {
1412 fn new() -> Self {
1413 Table {
1414 inner: Vec::with_capacity(MAX_ENTRIES),
1415 depths: Vec::with_capacity(MAX_ENTRIES),
1416 }
1417 }
1418
1419 fn clear(&mut self, min_size: u8) {
1420 let static_count = usize::from(1u16 << u16::from(min_size)) + 2;
1421 self.inner.truncate(static_count);
1422 self.depths.truncate(static_count);
1423 }
1424
1425 fn init(&mut self, min_size: u8) {
1426 self.inner.clear();
1427 self.depths.clear();
1428 for i in 0..(1u16 << u16::from(min_size)) {
1429 self.inner.push(Link::base(i as u8));
1430 self.depths.push(1);
1431 }
1432 // Clear code.
1433 self.inner.push(Link::base(0));
1434 self.depths.push(0);
1435 // End code.
1436 self.inner.push(Link::base(0));
1437 self.depths.push(0);
1438 }
1439
1440 fn at(&self, code: Code) -> &Link {
1441 &self.inner[usize::from(code)]
1442 }
1443
1444 fn is_empty(&self) -> bool {
1445 self.inner.is_empty()
1446 }
1447
1448 fn is_full(&self) -> bool {
1449 self.inner.len() >= MAX_ENTRIES
1450 }
1451
1452 fn derive(&mut self, from: &DerivationBase, byte: u8) {
1453 let link = from.derive(byte);
1454 let depth = self.depths[usize::from(from.code)] + 1;
1455 self.inner.push(link);
1456 self.depths.push(depth);
1457 }
1458
1459 // Derive multiple codes in a row, where each base is guaranteed to already exist.
1460 fn derive_burst(&mut self, from: &mut DerivationBase, burst: &[Code], first: &[u8]) {
1461 let mut depth_of = from.code;
1462 // Note that false data dependency we want to get rid of!
1463 // TODO: this pushes into a Vec, maybe we can make this cleaner.
1464 for &code in burst {
1465 let depth = self.depths[usize::from(depth_of)] + 1;
1466 self.depths.push(depth);
1467 depth_of = code;
1468 }
1469
1470 // Llvm tends to be flaky with code layout for the case of requiring an allocation. It's
1471 // not clear if that can occur in practice but it relies on iterator size hint..
1472 let extensions = burst.iter().zip(first);
1473 self.inner.extend(extensions.map(|(&code, &first)| {
1474 let link = from.derive(first);
1475 from.code = code;
1476 from.first = first;
1477 link
1478 }));
1479 }
1480
1481 fn reconstruct(&self, code: Code, out: &mut [u8]) -> u8 {
1482 let mut code_iter = code;
1483 let table = &self.inner[..=usize::from(code)];
1484 let first = table[usize::from(code)].first;
1485
1486 let len = code_iter;
1487 for ch in out.iter_mut().rev() {
1488 //(code, cha) = self.table[k as usize];
1489 // Note: This could possibly be replaced with an unchecked array access if
1490 // - value is asserted to be < self.next_code() in push
1491 // - min_size is asserted to be < MAX_CODESIZE
1492 let entry = &table[usize::from(code_iter)];
1493 code_iter = core::cmp::min(len, entry.prev);
1494 *ch = entry.byte;
1495 }
1496
1497 first
1498 }
1499}
1500
1501impl Link {
1502 fn base(byte: u8) -> Self {
1503 Link {
1504 prev: 0,
1505 byte,
1506 first: byte,
1507 }
1508 }
1509}
1510
1511impl DerivationBase {
1512 // TODO: this has self type to make it clear we might depend on the old in a future
1513 // optimization. However, that has no practical purpose right now.
1514 fn derive(&self, byte: u8) -> Link {
1515 Link {
1516 prev: self.code,
1517 byte,
1518 first: self.first,
1519 }
1520 }
1521}
1522
1523#[cfg(test)]
1524mod tests {
1525 use crate::alloc::vec::Vec;
1526 #[cfg(feature = "std")]
1527 use crate::StreamBuf;
1528 use crate::{decode::Decoder, BitOrder};
1529
1530 #[test]
1531 fn invalid_code_size_low() {
1532 let _ = Decoder::new(BitOrder::Msb, 0);
1533 let _ = Decoder::new(BitOrder::Msb, 1);
1534 }
1535
1536 #[test]
1537 #[should_panic]
1538 fn invalid_code_size_high() {
1539 let _ = Decoder::new(BitOrder::Msb, 14);
1540 }
1541
1542 fn make_encoded() -> Vec<u8> {
1543 const FILE: &'static [u8] = include_bytes!(concat!(
1544 env!("CARGO_MANIFEST_DIR"),
1545 "/benches/binary-8-msb.lzw"
1546 ));
1547 return Vec::from(FILE);
1548 }
1549
1550 #[test]
1551 #[cfg(feature = "std")]
1552 fn into_stream_buffer_no_alloc() {
1553 let encoded = make_encoded();
1554 let mut decoder = Decoder::new(BitOrder::Msb, 8);
1555
1556 let mut output = vec![];
1557 let mut buffer = [0; 512];
1558 let mut istream = decoder.into_stream(&mut output);
1559 istream.set_buffer(&mut buffer[..]);
1560 istream.decode(&encoded[..]).status.unwrap();
1561
1562 match istream.buffer {
1563 Some(StreamBuf::Borrowed(_)) => {}
1564 None => panic!("Decoded without buffer??"),
1565 Some(StreamBuf::Owned(_)) => panic!("Unexpected buffer allocation"),
1566 }
1567 }
1568
1569 #[test]
1570 #[cfg(feature = "std")]
1571 fn into_stream_buffer_small_alloc() {
1572 struct WriteTap<W: std::io::Write>(W);
1573 const BUF_SIZE: usize = 512;
1574
1575 impl<W: std::io::Write> std::io::Write for WriteTap<W> {
1576 fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
1577 assert!(buf.len() <= BUF_SIZE);
1578 self.0.write(buf)
1579 }
1580 fn flush(&mut self) -> std::io::Result<()> {
1581 self.0.flush()
1582 }
1583 }
1584
1585 let encoded = make_encoded();
1586 let mut decoder = Decoder::new(BitOrder::Msb, 8);
1587
1588 let mut output = vec![];
1589 let mut istream = decoder.into_stream(WriteTap(&mut output));
1590 istream.set_buffer_size(512);
1591 istream.decode(&encoded[..]).status.unwrap();
1592
1593 match istream.buffer {
1594 Some(StreamBuf::Owned(vec)) => assert!(vec.len() <= BUF_SIZE),
1595 Some(StreamBuf::Borrowed(_)) => panic!("Unexpected borrowed buffer, where from?"),
1596 None => panic!("Decoded without buffer??"),
1597 }
1598 }
1599
1600 #[test]
1601 #[cfg(feature = "std")]
1602 fn reset() {
1603 let encoded = make_encoded();
1604 let mut decoder = Decoder::new(BitOrder::Msb, 8);
1605 let mut reference = None;
1606
1607 for _ in 0..2 {
1608 let mut output = vec![];
1609 let mut buffer = [0; 512];
1610 let mut istream = decoder.into_stream(&mut output);
1611 istream.set_buffer(&mut buffer[..]);
1612 istream.decode_all(&encoded[..]).status.unwrap();
1613
1614 decoder.reset();
1615 if let Some(reference) = &reference {
1616 assert_eq!(output, *reference);
1617 } else {
1618 reference = Some(output);
1619 }
1620 }
1621 }
1622}