1 //! Deflate format (RFC 1951) support.
3 //! This module provides functionality for decompressing raw deflated streams via [`Inflate`] and gzip files (RFC 1952) via [`gzip_decode`] and compressing raw or zlib streams via [`Deflate`].
5 //! [`Deflate`]: ./struct.Deflate.html
6 //! [`Inflate`]: ./struct.Inflate.html
7 //! [`gzip_decode`]: ./fn.gzip_decode.html
11 //! Decompressing full input buffer into sufficiently large output buffer:
13 //! # use nihav_core::compr::DecompressError;
14 //! use nihav_core::compr::deflate::Inflate;
16 //! # fn decompress(input: &[u8]) -> Result<(), DecompressError> {
17 //! # let mut output_buffer = [0u8; 16];
18 //! let output_length = Inflate::uncompress(input, &mut output_buffer)?;
23 //! Decompressing input chunks into portions of output:
25 //! use nihav_core::compr::DecompressError;
26 //! use nihav_core::compr::deflate::Inflate;
28 //! # fn decompress(input_data: &[u8]) -> Result<(), DecompressError> {
29 //! let mut inflate = Inflate::new();
30 //! let mut dst_buf: Vec<u8> = Vec::new();
31 //! let mut output_chunk = [0u8; 1024];
32 //! for src in input_data.chunks(512) {
33 //! let mut repeat = false;
35 //! let ret = inflate.decompress_data(src, &mut output_chunk, repeat);
37 //! Ok(len) => { // we got a buffer decoded successfully to the end
38 //! dst_buf.extend_from_slice(&output_chunk[..len]);
41 //! Err(DecompressError::ShortData) => { // this block of data was fully read
44 //! Err(DecompressError::OutputFull) => {
45 //! // the output buffer is full, flush it and continue decoding the same block
47 //! dst_buf.extend_from_slice(&output_chunk);
59 //! Compressing input buffer into zlib stream:
61 //! use nihav_core::compr::deflate::{Deflate, DeflateMode, DeflateWriter};
63 //! # fn compress(input: &[u8]) {
64 //! let output = Vec::with_capacity(input.len() * 65540 / 65535 + 6);
65 //! let mut writer = DeflateWriter::new(output);
66 //! let mut compr = Deflate::new(DeflateMode::Fast);
67 //! compr.write_zlib_header(&mut writer);
68 //! compr.compress(input, &mut writer);
69 //! compr.compress_end(&mut writer);
70 //! let output = writer.end();
74 use crate::options::NAOptionDefinitionType;
75 use crate::io::byteio::*;
76 use crate::io::bitreader::*;
77 use crate::io::codebook::*;
80 const NUM_LITERALS: usize = 287;
81 const NUM_DISTS: usize = 32;
83 struct FixedLenCodeReader {}
85 impl CodebookDescReader<u16> for FixedLenCodeReader {
86 fn bits(&mut self, idx: usize) -> u8 {
88 else if idx < 256 { 9 }
89 else if idx < 280 { 7 }
92 #[allow(clippy::identity_op)]
93 fn code(&mut self, idx: usize) -> u32 {
94 let base = idx as u32;
95 let bits = self.bits(idx);
96 if idx < 144 { reverse_bits(base + 0x30, bits) }
97 else if idx < 256 { reverse_bits(base + 0x190 - 144, bits) }
98 else if idx < 280 { reverse_bits(base + 0x000 - 256, bits) }
99 else { reverse_bits(base + 0xC0 - 280, bits) }
101 fn sym (&mut self, idx: usize) -> u16 { idx as u16 }
102 fn len(&mut self) -> usize { NUM_LITERALS + 1 }
105 #[derive(Clone,Copy,Default)]
106 struct BitReaderState {
112 struct CurrentSource<'a> {
117 impl<'a> CurrentSource<'a> {
118 fn new(src: &'a [u8], br: BitReaderState) -> Self {
119 let mut newsrc = Self { src, br };
124 fn reinit(src: &'a [u8], br: BitReaderState) -> Self {
125 let mut newsrc = Self { src, br };
129 fn refill(&mut self) {
130 while (self.br.bits <= 24) && (self.br.pos < self.src.len()) {
131 self.br.bitbuf |= u32::from(self.src[self.br.pos]) << self.br.bits;
136 fn skip_cache(&mut self, nbits: u8) {
137 self.br.bitbuf >>= nbits;
138 self.br.bits -= nbits;
140 fn read(&mut self, nbits: u8) -> BitReaderResult<u32> {
141 if nbits == 0 { return Ok(0); }
142 if nbits > 16 { return Err(BitReaderError::TooManyBitsRequested); }
143 if self.br.bits < nbits {
145 if self.br.bits < nbits { return Err(BitReaderError::BitstreamEnd); }
147 let ret = self.br.bitbuf & ((1 << nbits) - 1);
148 self.skip_cache(nbits);
151 fn read_bool(&mut self) -> BitReaderResult<bool> {
152 if self.br.bits == 0 {
154 if self.br.bits == 0 { return Err(BitReaderError::BitstreamEnd); }
156 let ret = (self.br.bitbuf & 1) != 0;
160 fn peek(&mut self, nbits: u8) -> u32 {
161 if nbits == 0 || nbits > 16 { return 0; }
162 if self.br.bits < nbits {
165 self.br.bitbuf & ((1 << nbits) - 1)
167 fn skip(&mut self, nbits: u32) -> BitReaderResult<()> {
168 if u32::from(self.br.bits) >= nbits {
169 self.skip_cache(nbits as u8);
175 fn skip_bytes(&mut self, nbytes: usize) -> BitReaderResult<()> {
177 let cached = usize::from(self.br.bits / 8);
178 if nbytes <= cached {
179 self.skip((nbytes as u32) * 8)?;
181 self.skip((cached as u32) * 8)?;
184 self.br.pos += nbytes - cached;
185 if self.br.pos > self.src.len() {
186 return Err(BitReaderError::BitstreamEnd);
192 fn align(&mut self) {
193 let b = self.br.bits & 7;
198 fn left(&self) -> isize {
199 ((self.src.len() as isize) - (self.br.pos as isize)) * 8 + (self.br.bits as isize)
201 fn tell(&self) -> usize {
202 self.br.pos - usize::from(self.br.bits / 8)
206 impl<'a, S: Copy> CodebookReader<S> for CurrentSource<'a> {
207 fn read_cb(&mut self, cb: &Codebook<S>) -> CodebookResult<S> {
210 let mut lut_bits = cb.lut_bits;
211 let orig_br = self.br;
213 let lut_idx = (self.peek(lut_bits) as usize) + idx;
214 if cb.table[lut_idx] == TABLE_FILL_VALUE { return Err(CodebookError::InvalidCode); }
215 let bits = cb.table[lut_idx] & 0x7F;
216 esc = (cb.table[lut_idx] & 0x80) != 0;
217 idx = (cb.table[lut_idx] >> 8) as usize;
218 let skip_bits = if esc { u32::from(lut_bits) } else { bits };
219 if (skip_bits as isize) > self.left() {
222 return Err(CodebookError::MemoryError);
224 self.skip(skip_bits).unwrap();
225 lut_bits = bits as u8;
236 StaticBlockInvLen(u32),
237 StaticBlockCopy(usize),
239 FixedBlockLengthExt(usize, u8),
240 FixedBlockDist(usize),
241 FixedBlockDistExt(usize, usize, u8),
242 FixedBlockCopy(usize, usize),
243 FixedBlockLiteral(u8),
249 DynCodeLengthsAdd(usize),
251 DynBlockLengthExt(usize, u8),
253 DynBlockDistExt(usize, usize, u8),
254 DynCopy(usize, usize),
259 /// The decompressor for deflated streams (RFC 1951).
262 fix_len_cb: Codebook<u16>,
273 dyn_len_cb: Option<Codebook<u32>>,
274 dyn_lit_cb: Option<Codebook<u32>>,
275 dyn_dist_cb: Option<Codebook<u32>>,
276 len_lengths: [u8; 19],
277 all_lengths: [u8; NUM_LITERALS + NUM_DISTS],
281 const LENGTH_ADD_BITS: [u8; 29] = [
282 0, 0, 0, 0, 0, 0, 0, 0, 1, 1,
283 1, 1, 2, 2, 2, 2, 3, 3, 3, 3,
284 4, 4, 4, 4, 5, 5, 5, 5, 0
286 const LENGTH_BASE: [u16; 29] = [
287 3, 4, 5, 6, 7, 8, 9, 10, 11, 13,
288 15, 17, 19, 23, 27, 31, 35, 43, 51, 59,
289 67, 83, 99, 115, 131, 163, 195, 227, 258
291 const DIST_ADD_BITS: [u8; 30] = [
292 0, 0, 0, 0, 1, 1, 2, 2, 3, 3,
293 4, 4, 5, 5, 6, 6, 7, 7, 8, 8,
294 9, 9, 10, 10, 11, 11, 12, 12, 13, 13
296 const DIST_BASE: [u16; 30] = [
297 1, 2, 3, 4, 5, 7, 9, 13, 17, 25,
298 33, 49, 65, 97, 129, 193, 257, 385, 513, 769,
299 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577
301 const LEN_RECODE: [usize; 19] = [
302 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
304 const REPEAT_BITS: [u8; 3] = [ 2, 3, 7 ];
305 const REPEAT_BASE: [u8; 3] = [ 3, 3, 11 ];
307 macro_rules! read_bits {
308 ($self: expr, $csrc: expr, $bits: expr) => ({
309 if $csrc.left() < $bits as isize {
311 return Err(DecompressError::ShortData);
313 $csrc.read($bits).unwrap()
317 macro_rules! read_cb {
318 ($self: expr, $csrc: expr, $cb: expr) => ({
319 let ret = $csrc.read_cb($cb);
320 if let Err(CodebookError::MemoryError) = ret {
322 return Err(DecompressError::ShortData);
327 $self.state = InflateState::End;
328 return Err(DecompressError::InvalidData);
335 /// Creates a new instance of `Inflate` struct.
336 pub fn new() -> Self {
337 let mut cr = FixedLenCodeReader {};
338 let fix_len_cb = Codebook::new(&mut cr, CodebookMode::LSB).unwrap();
340 br: BitReaderState::default(),
348 state: InflateState::Start,
355 len_lengths: [0; 19],
356 all_lengths: [0; NUM_LITERALS + NUM_DISTS],
360 fn put_literal(&mut self, val: u8) {
361 self.buf[self.bpos] = val;
362 self.bpos = (self.bpos + 1) & (self.buf.len() - 1);
365 fn lz_copy(&mut self, offset: usize, len: usize, dst: &mut [u8]) -> DecompressResult<()> {
366 let mask = self.buf.len() - 1;
367 if offset > self.full_pos {
368 return Err(DecompressError::InvalidData);
370 let cstart = (self.bpos.wrapping_sub(offset)) & mask;
371 for (i, out) in dst[..len].iter_mut().enumerate() {
372 self.buf[(self.bpos + i) & mask] = self.buf[(cstart + i) & mask];
373 *out = self.buf[(cstart + i) & mask];
375 self.bpos = (self.bpos + len) & mask;
376 self.full_pos += len;
379 /// Sets custom history for decoding an update for already decoded data.
380 pub fn set_dict(&mut self, dict: &[u8]) {
381 let len = dict.len().min(self.buf.len());
382 let start = dict.len() - len;
383 self.buf[..len].copy_from_slice(&dict[start..]);
387 /// Reports whether decoder has finished decoding the input.
388 pub fn is_finished(&self) -> bool {
389 matches!(self.state, InflateState::End)
391 /// Reports the current amount of bytes output into the destination buffer after the last run.
392 pub fn get_current_output_size(&self) -> usize { self.output_idx }
393 /// Reports the total amount of bytes decoded so far.
394 pub fn get_total_output_size(&self) -> usize { self.bpos }
395 /// Tries to decompress input data and write it to the output buffer.
397 /// Since the decompressor can work with arbitrary input and output chunks its return value may have several meanings:
398 /// * `Ok(len)` means the stream has been fully decoded and then number of bytes output into the destination buffer is returned.
399 /// * [`DecompressError::ShortData`] means the input stream has been fully read but more data is needed.
400 /// * [`DecompressError::OutputFull`] means the output buffer is full and should be flushed. Then decoding should continue on the same input block with `continue_block` parameter set to `true`.
402 /// [`DecompressError::ShortData`]: ../enum.DecompressError.html#variant.ShortData
403 /// [`DecompressError::OutputFull`]: ../enum.DecompressError.html#variant.OutputFull
404 pub fn decompress_data(&mut self, src: &[u8], dst: &mut [u8], continue_block: bool) -> DecompressResult<usize> {
405 self.decompress_data_internal(src, dst, continue_block, false)
407 /// Tries to decompress whole input chunk to the output buffer.
408 pub fn decompress_block(&mut self, src: &[u8], dst: &mut [u8]) -> DecompressResult<usize> {
409 self.decompress_data_internal(src, dst, false, true)
411 #[allow(clippy::comparison_chain)]
412 fn decompress_data_internal(&mut self, src: &[u8], dst: &mut [u8], continue_block: bool, do_one_block: bool) -> DecompressResult<usize> {
413 if src.is_empty() || dst.is_empty() {
414 return Err(DecompressError::InvalidArgument);
416 let mut csrc = if !continue_block {
417 CurrentSource::new(src, self.br)
420 CurrentSource::reinit(src, self.br)
425 // check for zlib stream header
426 if let (&InflateState::Start, true) = (&self.state, src.len() > 2) {
427 let cm = src[0] & 0xF;
428 let cinfo = src[0] >> 4;
429 let hdr = (u16::from(src[0]) << 8) | u16::from(src[1]);
430 if cm == 8 && cinfo <= 7 && (hdr % 31) == 0 {
431 csrc.skip(16).unwrap();
436 InflateState::Start | InflateState::BlockStart => {
437 if csrc.left() == 0 {
439 return Ok(self.output_idx);
442 return Err(DecompressError::ShortData);
444 self.final_block = csrc.read_bool().unwrap();
445 self.state = InflateState::BlockMode;
447 InflateState::BlockMode => {
448 let bmode = read_bits!(self, csrc, 2);
452 self.state = InflateState::StaticBlockLen;
454 1 => { self.state = InflateState::FixedBlock; },
455 2 => { self.state = InflateState::DynBlockHlit; },
457 self.state = InflateState::End;
458 return Err(DecompressError::InvalidHeader);
462 InflateState::StaticBlockLen => {
463 let len = read_bits!(self, csrc, 16);
464 self.state = InflateState::StaticBlockInvLen(len);
466 InflateState::StaticBlockInvLen(len) => {
467 let inv_len = read_bits!(self, csrc, 16);
468 if (len ^ inv_len) != 0xFFFF {
469 self.state = InflateState::End;
470 return Err(DecompressError::InvalidHeader);
472 self.state = InflateState::StaticBlockCopy(len as usize);
474 InflateState::StaticBlockCopy(len) => {
478 self.state = InflateState::StaticBlockCopy(len - i);
479 return Err(DecompressError::ShortData);
481 let val = csrc.read(8).unwrap() as u8;
482 self.put_literal(val);
484 self.state = InflateState::BlockStart;
486 InflateState::FixedBlock => {
487 let val = read_cb!(self, csrc, &self.fix_len_cb);
489 if self.output_idx >= dst.len() {
491 self.state = InflateState::FixedBlockLiteral(val as u8);
492 return Err(DecompressError::OutputFull);
494 self.put_literal(val as u8);
495 dst[self.output_idx] = val as u8;
496 self.output_idx += 1;
497 } else if val == 256 {
498 if self.final_block {
499 self.state = InflateState::End;
500 return Ok(self.output_idx);
502 self.state = InflateState::BlockStart;
505 let len_idx = (val - 257) as usize;
506 if len_idx >= LENGTH_BASE.len() {
507 self.state = InflateState::End;
508 return Err(DecompressError::InvalidData);
510 let len_bits = LENGTH_ADD_BITS[len_idx];
511 let add_base = LENGTH_BASE[len_idx] as usize;
513 self.state = InflateState::FixedBlockLengthExt(add_base, len_bits);
515 self.state = InflateState::FixedBlockDist(add_base);
519 InflateState::FixedBlockLiteral(sym) => {
520 if self.output_idx >= dst.len() {
522 return Err(DecompressError::OutputFull);
524 self.put_literal(sym);
525 dst[self.output_idx] = sym;
526 self.output_idx += 1;
527 self.state = InflateState::FixedBlock;
529 InflateState::FixedBlockLengthExt(base, bits) => {
530 let add = read_bits!(self, csrc, bits) as usize;
531 self.state = InflateState::FixedBlockDist(base + add);
533 InflateState::FixedBlockDist(length) => {
534 let dist_idx = reverse_bits(read_bits!(self, csrc, 5), 5) as usize;
535 if dist_idx >= DIST_BASE.len() {
536 self.state = InflateState::End;
537 return Err(DecompressError::InvalidData);
539 let dist_bits = DIST_ADD_BITS[dist_idx];
540 let dist_base = DIST_BASE[dist_idx] as usize;
542 self.state = InflateState::FixedBlockCopy(length, dist_base);
544 self.state = InflateState::FixedBlockDistExt(length, dist_base, dist_bits);
547 InflateState::FixedBlockDistExt(length, base, bits) => {
548 let add = read_bits!(self, csrc, bits) as usize;
549 self.state = InflateState::FixedBlockCopy(length, base + add);
551 InflateState::FixedBlockCopy(length, dist) => {
552 if self.output_idx + length > dst.len() {
553 let copy_size = dst.len() - self.output_idx;
554 let ret = self.lz_copy(dist, copy_size, &mut dst[self.output_idx..]);
556 self.state = InflateState::End;
557 return Err(DecompressError::InvalidData);
559 self.output_idx += copy_size;
561 self.state = InflateState::FixedBlockCopy(length - copy_size, dist);
562 return Err(DecompressError::OutputFull);
564 let ret = self.lz_copy(dist, length, &mut dst[self.output_idx..]);
566 self.state = InflateState::End;
567 return Err(DecompressError::InvalidData);
569 self.output_idx += length;
570 self.state = InflateState::FixedBlock;
572 InflateState::DynBlockHlit => {
573 self.hlit = (read_bits!(self, csrc, 5) as usize) + 257;
574 if self.hlit >= 287 {
575 self.state = InflateState::End;
576 return Err(DecompressError::InvalidHeader);
578 self.state = InflateState::DynBlockHdist;
580 InflateState::DynBlockHdist => {
581 self.hdist = (read_bits!(self, csrc, 5) as usize) + 1;
582 self.state = InflateState::DynBlockHclen;
584 InflateState::DynBlockHclen => {
585 let hclen = (read_bits!(self, csrc, 4) as usize) + 4;
586 self.cur_len_idx = 0;
587 self.len_lengths = [0; 19];
588 self.all_lengths = [0; NUM_LITERALS + NUM_DISTS];
589 self.state = InflateState::DynLengths(hclen);
591 InflateState::DynLengths(len) => {
595 self.state = InflateState::DynLengths(len - i);
596 return Err(DecompressError::ShortData);
598 self.len_lengths[LEN_RECODE[self.cur_len_idx]] = csrc.read(3).unwrap() as u8;
599 self.cur_len_idx += 1;
601 let mut len_codes = [ShortCodebookDesc { code: 0, bits: 0 }; 19];
602 lengths_to_codes(&self.len_lengths, &mut len_codes)?;
603 let mut cr = ShortCodebookDescReader::new(&len_codes);
604 let ret = Codebook::new(&mut cr, CodebookMode::LSB);
606 self.state = InflateState::End;
607 return Err(DecompressError::InvalidHeader);
609 self.dyn_len_cb = Some(ret.unwrap());
610 self.cur_len_idx = 0;
611 self.state = InflateState::DynCodeLengths;
613 InflateState::DynCodeLengths => {
614 if let Some(ref len_cb) = self.dyn_len_cb {
615 while self.cur_len_idx < self.hlit + self.hdist {
616 let ret = csrc.read_cb(len_cb);
617 let val = match ret {
619 Err(CodebookError::MemoryError) => {
621 return Err(DecompressError::ShortData);
624 self.state = InflateState::End;
625 return Err(DecompressError::InvalidHeader);
629 self.all_lengths[self.cur_len_idx] = val as u8;
630 self.cur_len_idx += 1;
632 let idx = (val as usize) - 16;
634 self.state = InflateState::End;
635 return Err(DecompressError::InvalidHeader);
637 self.state = InflateState::DynCodeLengthsAdd(idx);
641 let (lit_lengths, dist_lengths) = self.all_lengths.split_at(self.hlit);
643 let mut lit_codes = [ShortCodebookDesc { code: 0, bits: 0 }; NUM_LITERALS];
644 lengths_to_codes(lit_lengths, &mut lit_codes)?;
645 let mut cr = ShortCodebookDescReader::new(&lit_codes);
646 let ret = Codebook::new(&mut cr, CodebookMode::LSB);
647 if ret.is_err() { return Err(DecompressError::InvalidHeader); }
648 self.dyn_lit_cb = Some(ret.unwrap());
650 let mut dist_codes = [ShortCodebookDesc { code: 0, bits: 0 }; NUM_DISTS];
651 lengths_to_codes(&dist_lengths[..self.hdist], &mut dist_codes)?;
652 let mut cr = ShortCodebookDescReader::new(&dist_codes);
653 let ret = Codebook::new(&mut cr, CodebookMode::LSB);
654 if ret.is_err() { return Err(DecompressError::InvalidHeader); }
655 self.dyn_dist_cb = Some(ret.unwrap());
657 self.state = InflateState::DynBlock;
662 InflateState::DynCodeLengthsAdd(mode) => {
663 let base = REPEAT_BASE[mode] as usize;
664 let bits = REPEAT_BITS[mode];
665 let len = base + read_bits!(self, csrc, bits) as usize;
666 if self.cur_len_idx + len > self.hlit + self.hdist {
667 self.state = InflateState::End;
668 return Err(DecompressError::InvalidHeader);
670 let rpt = if mode == 0 {
671 if self.cur_len_idx == 0 {
672 self.state = InflateState::End;
673 return Err(DecompressError::InvalidHeader);
675 self.all_lengths[self.cur_len_idx - 1]
680 self.all_lengths[self.cur_len_idx] = rpt;
681 self.cur_len_idx += 1;
683 self.state = InflateState::DynCodeLengths;
685 InflateState::DynBlock => {
686 if let Some(ref lit_cb) = self.dyn_lit_cb {
687 let val = read_cb!(self, csrc, lit_cb);
689 if self.output_idx >= dst.len() {
691 self.state = InflateState::DynBlockLiteral(val as u8);
692 return Err(DecompressError::OutputFull);
694 self.put_literal(val as u8);
695 dst[self.output_idx] = val as u8;
696 self.output_idx += 1;
697 } else if val == 256 {
698 if self.final_block {
699 self.state = InflateState::End;
700 return Ok(self.output_idx);
702 self.state = InflateState::BlockStart;
705 let len_idx = (val - 257) as usize;
706 if len_idx >= LENGTH_BASE.len() {
707 self.state = InflateState::End;
708 return Err(DecompressError::InvalidData);
710 let len_bits = LENGTH_ADD_BITS[len_idx];
711 let add_base = LENGTH_BASE[len_idx] as usize;
713 self.state = InflateState::DynBlockLengthExt(add_base, len_bits);
715 self.state = InflateState::DynBlockDist(add_base);
722 InflateState::DynBlockLiteral(sym) => {
723 if self.output_idx >= dst.len() {
725 return Err(DecompressError::OutputFull);
727 self.put_literal(sym);
728 dst[self.output_idx] = sym;
729 self.output_idx += 1;
730 self.state = InflateState::DynBlock;
732 InflateState::DynBlockLengthExt(base, bits) => {
733 let add = read_bits!(self, csrc, bits) as usize;
734 self.state = InflateState::DynBlockDist(base + add);
736 InflateState::DynBlockDist(length) => {
737 if let Some(ref dist_cb) = self.dyn_dist_cb {
738 let dist_idx = read_cb!(self, csrc, dist_cb) as usize;
739 if dist_idx >= DIST_BASE.len() {
740 self.state = InflateState::End;
741 return Err(DecompressError::InvalidData);
743 let dist_bits = DIST_ADD_BITS[dist_idx];
744 let dist_base = DIST_BASE[dist_idx] as usize;
746 self.state = InflateState::DynCopy(length, dist_base);
748 self.state = InflateState::DynBlockDistExt(length, dist_base, dist_bits);
754 InflateState::DynBlockDistExt(length, base, bits) => {
755 let add = read_bits!(self, csrc, bits) as usize;
756 self.state = InflateState::DynCopy(length, base + add);
758 InflateState::DynCopy(length, dist) => {
759 if self.output_idx + length > dst.len() {
760 let copy_size = dst.len() - self.output_idx;
761 let ret = self.lz_copy(dist, copy_size, &mut dst[self.output_idx..]);
763 self.state = InflateState::End;
764 return Err(DecompressError::InvalidData);
766 self.output_idx += copy_size;
768 self.state = InflateState::DynCopy(length - copy_size, dist);
769 return Err(DecompressError::OutputFull);
771 let ret = self.lz_copy(dist, length, &mut dst[self.output_idx..]);
773 self.state = InflateState::End;
774 return Err(DecompressError::InvalidData);
776 self.output_idx += length;
777 self.state = InflateState::DynBlock;
779 InflateState::End => {
785 /// Resets decoder state.
786 pub fn reset(&mut self) {
790 self.state = InflateState::Start;
793 #[allow(clippy::comparison_chain)]
794 /// Decompresses input data into output returning the uncompressed data length.
795 pub fn uncompress(src: &[u8], dst: &mut [u8]) -> DecompressResult<usize> {
796 let mut csrc = CurrentSource::new(src, BitReaderState::default());
798 let cm = src[0] & 0xF;
799 let cinfo = src[0] >> 4;
800 let hdr = (u16::from(src[0]) << 8) | u16::from(src[1]);
801 if cm == 8 && cinfo <= 7 && (hdr % 31) == 0 {
802 csrc.skip(16).unwrap();
806 let mut fix_len_cb = None;
809 let mut final_block = false;
811 final_block = csrc.read_bool()?;
813 let bmode = csrc.read(2)?;
817 let len = csrc.read(16)? as usize;
818 let inv_len = csrc.read(16)? as usize;
819 if (len ^ inv_len) != 0xFFFF {
820 return Err(DecompressError::InvalidHeader);
822 let src_pos = csrc.tell();
823 if src_pos + len > src.len() {
824 return Err(DecompressError::ShortData);
826 if dst_idx + len > dst.len() {
827 return Err(DecompressError::OutputFull);
829 dst[dst_idx..][..len].copy_from_slice(&src[src_pos..][..len]);
831 csrc.skip_bytes(len)?;
834 if fix_len_cb.is_none() {
835 let mut cr = FixedLenCodeReader {};
836 fix_len_cb = Some(Codebook::new(&mut cr, CodebookMode::LSB).unwrap());
838 if let Some(ref len_cb) = &fix_len_cb {
840 let val = csrc.read_cb(len_cb)?;
842 if dst_idx >= dst.len() {
843 return Err(DecompressError::OutputFull);
845 dst[dst_idx] = val as u8;
847 } else if val == 256 {
850 let len_idx = (val - 257) as usize;
851 if len_idx >= LENGTH_BASE.len() {
852 return Err(DecompressError::InvalidData);
854 let len_bits = LENGTH_ADD_BITS[len_idx];
855 let mut length = LENGTH_BASE[len_idx] as usize;
857 length += csrc.read(len_bits)? as usize;
859 let dist_idx = reverse_bits(csrc.read(5)?, 5) as usize;
860 if dist_idx >= DIST_BASE.len() {
861 return Err(DecompressError::InvalidData);
863 let dist_bits = DIST_ADD_BITS[dist_idx];
864 let mut dist = DIST_BASE[dist_idx] as usize;
866 dist += csrc.read(dist_bits)? as usize;
869 if dst_idx + length > dst.len() {
870 return Err(DecompressError::OutputFull);
873 return Err(DecompressError::InvalidData);
875 lz_copy(dst, dst_idx, dist, length);
884 let hlit = csrc.read(5)? as usize + 257;
886 return Err(DecompressError::InvalidHeader);
888 let hdist = csrc.read(5)? as usize + 1;
889 let hclen = csrc.read(4)? as usize + 4;
890 let mut len_lengths = [0; 19];
891 let mut all_lengths = [0; NUM_LITERALS + NUM_DISTS];
893 for cur_len_idx in 0..hclen {
894 len_lengths[LEN_RECODE[cur_len_idx]] = csrc.read(3)? as u8;
896 let mut len_codes = [ShortCodebookDesc { code: 0, bits: 0 }; 19];
897 lengths_to_codes(&len_lengths, &mut len_codes)?;
898 let mut cr = ShortCodebookDescReader::new(&len_codes);
899 let ret = Codebook::new(&mut cr, CodebookMode::LSB);
901 return Err(DecompressError::InvalidHeader);
903 let dyn_len_cb = ret.unwrap();
905 let mut cur_len_idx = 0;
906 while cur_len_idx < hlit + hdist {
907 let val = csrc.read_cb(&dyn_len_cb)?;
909 all_lengths[cur_len_idx] = val as u8;
912 let mode = (val as usize) - 16;
914 return Err(DecompressError::InvalidHeader);
916 let base = REPEAT_BASE[mode] as usize;
917 let bits = REPEAT_BITS[mode];
918 let len = base + (csrc.read(bits)? as usize);
919 if cur_len_idx + len > hlit + hdist {
920 return Err(DecompressError::InvalidHeader);
922 let rpt = if mode == 0 {
923 if cur_len_idx == 0 {
924 return Err(DecompressError::InvalidHeader);
926 all_lengths[cur_len_idx - 1]
931 all_lengths[cur_len_idx] = rpt;
936 let (lit_lengths, dist_lengths) = all_lengths.split_at(hlit);
938 let mut lit_codes = [ShortCodebookDesc { code: 0, bits: 0 }; NUM_LITERALS];
939 lengths_to_codes(lit_lengths, &mut lit_codes)?;
940 let mut cr = ShortCodebookDescReader::new(&lit_codes);
941 let ret = Codebook::new(&mut cr, CodebookMode::LSB);
942 if ret.is_err() { return Err(DecompressError::InvalidHeader); }
943 let dyn_lit_cb = ret.unwrap();
945 let mut dist_codes = [ShortCodebookDesc { code: 0, bits: 0 }; NUM_DISTS];
946 lengths_to_codes(&dist_lengths[..hdist], &mut dist_codes)?;
947 let mut cr = ShortCodebookDescReader::new(&dist_codes);
948 let ret = Codebook::new(&mut cr, CodebookMode::LSB);
949 if ret.is_err() { return Err(DecompressError::InvalidHeader); }
950 let dyn_dist_cb = ret.unwrap();
953 let val = csrc.read_cb(&dyn_lit_cb)?;
955 if dst_idx >= dst.len() {
956 return Err(DecompressError::OutputFull);
958 dst[dst_idx] = val as u8;
960 } else if val == 256 {
963 let len_idx = (val - 257) as usize;
964 if len_idx >= LENGTH_BASE.len() {
965 return Err(DecompressError::InvalidData);
967 let len_bits = LENGTH_ADD_BITS[len_idx];
968 let mut length = LENGTH_BASE[len_idx] as usize;
970 length += csrc.read(len_bits)? as usize;
973 let dist_idx = csrc.read_cb(&dyn_dist_cb)? as usize;
974 if dist_idx >= DIST_BASE.len() {
975 return Err(DecompressError::InvalidData);
977 let dist_bits = DIST_ADD_BITS[dist_idx];
978 let mut dist = DIST_BASE[dist_idx] as usize;
980 dist += csrc.read(dist_bits)? as usize;
983 if dst_idx + length > dst.len() {
984 return Err(DecompressError::OutputFull);
987 return Err(DecompressError::InvalidData);
989 lz_copy(dst, dst_idx, dist, length);
994 _ => return Err(DecompressError::InvalidHeader),
1001 impl Default for Inflate {
1002 fn default() -> Self {
1007 fn lengths_to_codes(lens: &[u8], codes: &mut [ShortCodebookDesc]) -> DecompressResult<()> {
1008 let mut bits = [0u32; 32];
1009 let mut pfx = [0u32; 33];
1010 for len in lens.iter() {
1011 let len = *len as usize;
1012 if len >= bits.len() {
1013 return Err(DecompressError::InvalidHeader);
1019 for i in 0..bits.len() {
1020 code = (code + bits[i]) << 1;
1024 for (len, codes) in lens.iter().zip(codes.iter_mut()) {
1025 let len = *len as usize;
1027 let bits = len as u8;
1028 *codes = ShortCodebookDesc { code: reverse_bits(pfx[len], bits), bits };
1031 *codes = ShortCodebookDesc { code: 0, bits: 0 };
1045 let mut tab = [0u32; 256];
1046 for (i, el) in tab.iter_mut().enumerate() {
1047 let mut c = i as u32;
1050 c = 0xEDB88320 ^ (c >> 1);
1057 Self { tab, crc: 0 }
1059 fn update_crc(&mut self, src: &[u8]) {
1060 let mut c = !self.crc;
1061 for el in src.iter() {
1062 c = self.tab[((c ^ u32::from(*el)) & 0xFF) as usize] ^ (c >> 8);
1068 /// Decodes input data in gzip file format (RFC 1952) returning a vector containing decoded data.
1069 pub fn gzip_decode(br: &mut ByteReader, skip_crc: bool) -> DecompressResult<Vec<u8>> {
1070 const FLAG_HCRC: u8 = 0x02;
1071 const FLAG_EXTRA: u8 = 0x04;
1072 const FLAG_NAME: u8 = 0x08;
1073 const FLAG_COMMENT: u8 = 0x10;
1075 let id1 = br.read_byte()?;
1076 let id2 = br.read_byte()?;
1077 let cm = br.read_byte()?;
1078 let flg = br.read_byte()?;
1079 let _mtime = br.read_u32le()?;
1080 let _xfl = br.read_byte()?;
1081 let _os = br.read_byte()?;
1082 if id1 != 0x1F || id2 != 0x8B || cm != 8 {
1083 return Err(DecompressError::InvalidHeader);
1086 if (flg & FLAG_EXTRA) != 0 {
1087 let xlen = br.read_u16le()? as usize;
1088 br.read_skip(xlen)?;
1090 if (flg & FLAG_NAME) != 0 {
1092 let b = br.read_byte()?;
1098 if (flg & FLAG_COMMENT) != 0 {
1100 let b = br.read_byte()?;
1106 let _hcrc = if (flg & FLAG_HCRC) != 0 {
1111 if (flg & 0xE0) != 0 {
1112 return Err(DecompressError::Unsupported);
1115 let mut output: Vec<u8> = Vec::new();
1116 let mut tail = [0u8; 8];
1117 let mut inblk = [0u8; 1024];
1118 let mut oblk = [0u8; 4096];
1119 let mut inflate = Inflate::new();
1120 let mut checker = GzipCRC32::new();
1123 let ret = br.read_buf_some(&mut inblk);
1124 if let Err(ByteIOError::EOF) = ret {
1127 let inlen = match ret {
1129 Err(_) => return Err(DecompressError::IOError),
1131 let mut repeat = false;
1133 let ret = inflate.decompress_data(&inblk[..inlen], &mut oblk, repeat);
1136 checker.update_crc(&oblk[..outlen]);
1137 output.extend_from_slice(&oblk[..outlen]);
1140 Err(DecompressError::ShortData) => {
1143 Err(DecompressError::OutputFull) => {
1145 checker.update_crc(&oblk);
1146 output.extend_from_slice(&oblk);
1153 // Save last 8 bytes for CRC and size.
1155 tail.copy_from_slice(&inblk[inlen - 8..][..8]);
1157 let shift_len = 8 - inlen;
1158 for i in 0..shift_len {
1159 tail[i] = tail[i + inlen];
1161 tail[shift_len..8].copy_from_slice(&inblk[..8 - shift_len]);
1165 if !inflate.is_finished() { println!("???"); }
1166 let crc = read_u32le(&tail[0..4])?;
1167 let size = read_u32le(&tail[4..8])?;
1168 if size != (output.len() as u32) {
1169 return Err(DecompressError::CRCError);
1171 if crc != checker.crc {
1172 return Err(DecompressError::CRCError);
1179 #[derive(Clone,Copy,Default)]
1187 const TOKEN_EOB: Token = Token { sym: 256, distsym: 0, len: 0, dist: 0 };
1190 fn from_literal(sym: u8) -> Self {
1192 sym: u16::from(sym),
1198 fn from_match(dist: u16, len: u16) -> Self {
1199 let sym = match len {
1200 3..= 10 => 257 + len - 3,
1201 11..= 18 => 265 + (len - 11) / 2,
1202 19..= 34 => 269 + (len - 19) / 4,
1203 35..= 66 => 273 + (len - 35) / 8,
1204 67..=130 => 277 + (len - 67) / 16,
1205 131..=257 => 281 + (len - 131) / 32,
1208 let distsym = if dist <= 4 {
1211 let bits = 16 - (dist - 1).leading_zeros();
1212 (bits as u8) * 2 - 2 + if ((dist - 1) & (1 << (bits - 2))) != 0 { 1 } else { 0 }
1215 sym, distsym, len, dist
1220 fn add_codes(lens: &[u8], stats: &mut [u32], toks: &mut Vec<(u8, u8)>) {
1224 for &len in lens.iter() {
1230 let run = lcount.min(138);
1232 toks.push((18, run - 11));
1237 toks.push((17, lcount - 3));
1240 for _ in 0..lcount {
1246 let run = lcount.min(6);
1248 toks.push((16, run - 3));
1251 for _ in 0..lcount {
1252 stats[last as usize] += 1;
1253 toks.push((last, 0));
1256 stats[len as usize] += 1;
1257 toks.push((len, 0));
1265 let run = lcount.min(138);
1267 toks.push((18, run - 11));
1272 toks.push((17, lcount - 3));
1275 for _ in 0..lcount {
1281 let run = lcount.min(6);
1283 toks.push((16, run - 3));
1286 for _ in 0..lcount {
1287 stats[last as usize] += 1;
1288 toks.push((last, 0));
1294 /// Deflate stream writer.
1295 pub struct DeflateWriter {
1301 impl DeflateWriter {
1302 /// Creates a new instance of `DeflateWriter` for a provided output.
1303 pub fn new(dst: Vec<u8>) -> Self {
1310 fn align(&mut self) {
1311 if (self.bits & 7) != 0 {
1312 self.bits += 8 - (self.bits & 7);
1315 fn flush(&mut self) {
1316 while self.bits >= 8 {
1317 self.dst.push(self.bbuf as u8);
1322 fn write(&mut self, val: u16, len: u8) {
1324 self.bbuf |= u32::from(val) << self.bits;
1327 /// Finishes writing the stream and returns the output vector.
1328 pub fn end(mut self) -> Vec<u8> {
1331 self.dst.push(self.bbuf as u8);
1336 fn write_codes(&mut self, codes: &CodeHuff, dists: &DistHuff) {
1337 let mut stats = [0u32; 19];
1338 let mut toks = Vec::with_capacity(NUM_LITERALS + NUM_DISTS);
1339 let mut cw = [0u16; 19];
1340 let mut cl = [0u8; 19];
1343 add_codes(&codes.lens[..codes.num_codes], &mut stats, &mut toks);
1344 add_codes(&dists.lens[..dists.num_codes], &mut stats, &mut toks);
1346 gen_tree(&mut cw, &mut cl, &mut nc, &mut stats, 7);
1349 for &idx in LEN_RECODE.iter().rev() {
1359 self.write((nc - 4) as u16, 4);
1360 for &idx in LEN_RECODE.iter().take(nc) {
1361 self.write(u16::from(cl[idx]), 3);
1363 for &(sym, add) in toks.iter() {
1364 self.write(cw[sym as usize], cl[sym as usize]);
1366 16 => self.write(u16::from(add), 2),
1367 17 => self.write(u16::from(add), 3),
1368 18 => self.write(u16::from(add), 7),
1373 fn write_tokens(&mut self, src: &[Token], codes: &CodeHuff, dists: &DistHuff) {
1374 for &tok in src.iter() {
1375 self.write(codes.codes[tok.sym as usize], codes.lens[tok.sym as usize]);
1377 self.write_len_bits(tok.len);
1378 self.write(dists.codes[tok.distsym as usize], dists.lens[tok.distsym as usize]);
1379 self.write_dist_bits(tok.dist);
1383 fn write_len_bits(&mut self, len: u16) {
1385 if (8..255).contains(&llen) {
1386 let bits = (16 - llen.leading_zeros() - 3) as u8;
1387 self.write(llen & ((1 << bits) - 1), bits);
1390 fn write_dist_bits(&mut self, dist: u16) {
1391 let ddist = dist - 1;
1393 let bits = (16 - ddist.leading_zeros() - 2) as u8;
1394 self.write(ddist & ((1 << bits) - 1), bits);
1401 stats: [u32; NUM_LITERALS],
1402 codes: [u16; NUM_LITERALS],
1403 lens: [u8; NUM_LITERALS],
1408 fn new(is_fixed: bool) -> Self {
1411 stats: [0; NUM_LITERALS],
1412 codes: [0; NUM_LITERALS],
1413 lens: [0; NUM_LITERALS],
1414 num_codes: NUM_LITERALS,
1417 fn make_codes(&mut self, src: &[Token]) {
1420 self.codes[i] = reverse_bits((i + 0x30) as u32, 8) as u16;
1423 for i in 144..=255 {
1424 self.codes[i] = reverse_bits((i + 0x100) as u32, 9) as u16;
1427 for i in 256..=279 {
1428 self.codes[i] = reverse_bits((i & 0x1F) as u32, 7) as u16;
1431 for i in 280..NUM_LITERALS {
1432 self.codes[i] = reverse_bits((i - 280 + 0xC0) as u32, 8) as u16;
1436 for &tok in src.iter() {
1437 self.stats[tok.sym as usize] += 1;
1439 gen_tree(&mut self.codes, &mut self.lens, &mut self.num_codes, &mut self.stats, 15);
1440 if self.num_codes < 257 {
1441 self.num_codes = 257;
1449 stats: [u32; NUM_DISTS],
1450 codes: [u16; NUM_DISTS],
1451 lens: [u8; NUM_DISTS],
1456 fn new(is_fixed: bool) -> Self {
1459 stats: [0; NUM_DISTS],
1460 codes: [0; NUM_DISTS],
1461 lens: [0; NUM_DISTS],
1462 num_codes: NUM_DISTS,
1465 fn make_codes(&mut self, src: &[Token]) {
1467 for i in 0..NUM_DISTS {
1468 self.codes[i] = reverse_bits(i as u32, 5) as u16;
1472 for &tok in src.iter() {
1474 self.stats[tok.distsym as usize] += 1;
1477 gen_tree(&mut self.codes, &mut self.lens, &mut self.num_codes, &mut self.stats, 15);
1478 if self.num_codes < 1 {
1485 #[derive(Clone,Copy,Default)]
1493 const NODE_SYM: u16 = 65500;
1496 nodes: [Node; NUM_LITERALS * 2],
1503 nodes: [Node::default(); NUM_LITERALS * 2],
1507 fn insert(&mut self, val: Node) {
1508 let mut idx = self.nnodes;
1509 for (i, nn) in self.nodes[..self.nnodes].iter().enumerate() {
1515 if idx < self.nnodes {
1516 for i in (idx..self.nnodes).rev() {
1517 self.nodes[i + 1] = self.nodes[i];
1520 self.nodes[idx] = val;
1523 fn trim(&mut self) {
1524 let mut start = self.nnodes;
1525 for (i, n) in self.nodes[..self.nnodes].iter().enumerate() {
1532 for i in 0..self.nnodes - start {
1533 self.nodes[i] = self.nodes[i + start];
1535 self.nnodes -= start;
1538 fn build(&mut self) {
1539 if self.nnodes == 1 {
1540 self.nodes[0].w = 1;
1544 while start + 1 < self.nnodes {
1545 let nn1 = self.nodes[start];
1546 let nn2 = self.nodes[start + 1];
1551 idx1: (start + 1) as u16,
1553 self.nodes[start].w = 0;
1554 self.nodes[start + 1].w = 0;
1558 if self.nnodes > 1 {
1559 self.assign_len(self.nnodes - 1, 0);
1562 fn assign_len(&mut self, idx: usize, len: u16) {
1563 if self.nodes[idx].sym == NODE_SYM {
1564 self.assign_len(self.nodes[idx].idx0 as usize, len + 1);
1565 self.assign_len(self.nodes[idx].idx1 as usize, len + 1);
1567 self.nodes[idx].w = len;
1572 fn gen_tree(codes: &mut [u16], lens: &mut [u8], num_codes: &mut usize, stats: &mut [u32], max_bits: u8) {
1574 for &w in stats.iter() {
1584 let mut tree = Tree::new();
1585 for (sym, &w) in stats.iter().enumerate() {
1586 tree.insert(Node{ sym: sym as u16, w: w as u16, idx0: 64000, idx1: 64000 });
1591 for n in tree.nodes[..tree.nnodes].iter() {
1592 if n.sym != NODE_SYM {
1593 lens[n.sym as usize] = n.w as u8;
1596 if !lens.iter().any(|&x| x > max_bits) {
1599 for w in stats.iter_mut() {
1604 lengths_to_codes16(lens, codes);
1605 let mut sz = codes.len();
1606 for &len in lens.iter().rev() {
1615 fn lengths_to_codes16(lens: &[u8], codes: &mut [u16]) {
1616 let mut bits = [0u32; 32];
1617 let mut pfx = [0u32; 33];
1618 for len in lens.iter() {
1619 let len = *len as usize;
1624 for i in 0..bits.len() {
1625 code = (code + bits[i]) << 1;
1629 for (len, codes) in lens.iter().zip(codes.iter_mut()) {
1630 let len = *len as usize;
1632 let bits = len as u8;
1633 *codes = reverse_bits(pfx[len], bits) as u16;
1642 fn parse(&mut self, src: &[u8], dst: &mut Vec<Token>);
1646 impl LZParse for NoParser {
1647 fn parse(&mut self, src: &[u8], dst: &mut Vec<Token>) {
1648 dst.reserve(src.len());
1649 for &b in src.iter() {
1650 dst.push(Token::from_literal(b));
1652 dst.push(TOKEN_EOB);
1656 fn check_match(src1: &[u8], src2: &[u8]) -> u16 {
1658 for (&a, &b) in src1.iter().zip(src2.iter()) {
1667 const HASH_SIZE: usize = 4096;
1668 const MAX_MATCH_LEN: usize = 258;
1669 const WINDOW_SIZE: usize = 32768 - MAX_MATCH_LEN;
1670 const WINDOW_MASK: usize = 32767;
1672 struct MatchFinder<'a> {
1675 hstart: [usize; HASH_SIZE],
1676 hnext: [usize; WINDOW_MASK + 1],
1679 impl<'a> MatchFinder<'a> {
1680 fn new(src: &'a [u8]) -> Self {
1684 hstart: [src.len(); HASH_SIZE],
1685 hnext: [src.len(); WINDOW_MASK + 1],
1688 fn hash(src: &[u8]) -> usize {
1690 (((u16::from(src[0]) << 10) ^ (u16::from(src[1]) << 5) ^ u16::from(src[2])) & ((HASH_SIZE as u16) - 1)) as usize
1692 fn add_hash(&mut self, hash: usize) {
1693 self.hnext[self.pos & WINDOW_MASK] = self.hstart[hash];
1694 self.hstart[hash] = self.pos;
1695 self.hnext[(self.pos + 1) & WINDOW_MASK] = self.src.len();
1697 fn find_match(&mut self) -> (u16, u16) {
1698 if self.pos == 0 || self.pos + 3 > self.src.len() {
1701 let key = Self::hash(&self.src[self.pos..]);
1703 let mut best_pos = 0;
1704 let mut best_len = 0;
1705 let mut idx = self.hstart[key];
1706 let search_end = self.pos.saturating_sub(WINDOW_SIZE);
1708 while idx >= search_end && idx < self.pos {
1709 let cur_len = check_match(&self.src[self.pos..], &self.src[idx..]);
1710 if cur_len > best_len {
1712 best_pos = self.pos - idx;
1713 if best_len >= (MAX_MATCH_LEN as u16) {
1714 return (best_pos as u16, MAX_MATCH_LEN as u16);
1721 idx = self.hnext[idx & WINDOW_MASK];
1724 (best_pos as u16, best_len)
1726 fn find_all_matches(&mut self, dists: &mut [u16; MAX_MATCH_LEN + 1]) {
1727 if self.pos == 0 || self.pos + 3 > self.src.len() {
1730 let key = Self::hash(&self.src[self.pos..]);
1731 let mut idx = self.hstart[key];
1732 let search_end = self.pos.saturating_sub(WINDOW_SIZE);
1734 while idx >= search_end && idx < self.pos {
1735 let cur_len = (check_match(&self.src[self.pos..], &self.src[idx..]) as usize).min(MAX_MATCH_LEN);
1736 if cur_len > 0 && dists[cur_len] == 0 {
1737 dists[cur_len] = (self.pos - idx) as u16;
1739 idx = self.hnext[idx & WINDOW_MASK];
1747 fn advance(&mut self, num: usize) {
1751 if self.pos + 3 <= self.src.len() {
1752 let key = Self::hash(&self.src[self.pos..]);
1759 fn get_sym(&self) -> u8 { self.src[self.pos] }
1760 fn is_end(&self) -> bool { self.pos >= self.src.len() }
1763 struct GreedyParser {}
1764 impl LZParse for GreedyParser {
1765 fn parse(&mut self, src: &[u8], dst: &mut Vec<Token>) {
1766 dst.reserve(src.len());
1768 let mut matcher = MatchFinder::new(src);
1769 while !matcher.is_end() {
1770 let (best_pos, best_len) = matcher.find_match();
1773 dst.push(Token::from_match(best_pos, best_len));
1774 matcher.advance(best_len as usize);
1776 dst.push(Token::from_literal(matcher.get_sym()));
1780 dst.push(TOKEN_EOB);
1784 struct LazyParser {}
1785 impl LZParse for LazyParser {
1786 fn parse(&mut self, src: &[u8], dst: &mut Vec<Token>) {
1787 dst.reserve(src.len());
1789 let mut matcher = MatchFinder::new(src);
1790 while !matcher.is_end() {
1791 let (best_pos, best_len) = matcher.find_match();
1793 let last_sym = matcher.get_sym();
1795 if !matcher.is_end() {
1796 let (best_pos1, best_len1) = matcher.find_match();
1797 if best_len1 > best_len + 1 {
1798 dst.push(Token::from_literal(last_sym));
1799 dst.push(Token::from_match(best_pos1, best_len1));
1800 matcher.advance(best_len1 as usize);
1804 dst.push(Token::from_match(best_pos, best_len));
1805 matcher.advance((best_len - 1) as usize);
1807 dst.push(Token::from_literal(matcher.get_sym()));
1811 dst.push(TOKEN_EOB);
1815 #[derive(Clone,Copy)]
1822 impl Default for TNode {
1823 fn default() -> Self {
1832 struct OptimalParser {
1833 trellis: Vec<TNode>,
1835 impl OptimalParser {
1836 fn new() -> Self { Self::default() }
1837 fn sym_price(_sym: u8) -> u32 { 9 }
1838 fn match_price(dist: u16, _len: u16) -> u32 {
1842 let bits = 16 - (dist - 1).leading_zeros();
1847 impl Default for OptimalParser {
1848 fn default() -> Self {
1850 trellis: Vec::with_capacity(WINDOW_SIZE),
1854 impl LZParse for OptimalParser {
1855 fn parse(&mut self, src: &[u8], dst: &mut Vec<Token>) {
1857 dst.push(TOKEN_EOB);
1860 dst.reserve(src.len());
1862 self.trellis.clear();
1863 self.trellis.reserve(src.len() + 1);
1864 for _ in 0..=src.len() {
1865 self.trellis.push(TNode::default());
1867 self.trellis[0].price = 0;
1869 let mut matcher = MatchFinder::new(src);
1870 for i in 0..self.trellis.len() - 1 {
1871 let mut dists = [0; MAX_MATCH_LEN + 1];
1872 matcher.find_all_matches(&mut dists);
1874 let sym = matcher.get_sym();
1875 let lprice = Self::sym_price(sym) + self.trellis[i].price;
1876 if self.trellis[i + 1].price > lprice {
1877 self.trellis[i + 1].price = lprice;
1878 self.trellis[i + 1].link = i;
1880 for (len, &dist) in dists.iter().enumerate() {
1882 let mprice = Self::match_price(dist, len as u16) + self.trellis[i].price;
1883 if self.trellis[i + len].price > mprice {
1884 self.trellis[i + len].price = mprice;
1885 self.trellis[i + len].link = i;
1886 self.trellis[i].dist = dist;
1893 let mut idx = self.trellis.len() - 1;
1894 let mut nidx = self.trellis[idx].link;
1898 nidx = self.trellis[idx].link;
1899 self.trellis[idx].link = oidx;
1903 while idx < self.trellis.len() - 1 {
1904 let len = self.trellis[idx].link - idx;
1906 dst.push(Token::from_literal(src[idx]));
1908 dst.push(Token::from_match(self.trellis[idx].dist, len as u16));
1910 idx = self.trellis[idx].link;
1913 dst.push(TOKEN_EOB);
1917 /// Deflate compression mode.
1918 #[derive(Clone,Copy,Debug,PartialEq,Default)]
1919 pub enum DeflateMode {
1922 /// Fast compression.
1924 /// Still fast but better compression.
1927 /// Slow but the best compression.
1931 pub const DEFLATE_MODE_DESCRIPTION: &str = "Deflate compression level.";
1932 /// Deflate option for no compression.
1933 pub const DEFLATE_MODE_NONE: &str = "none";
1934 /// Deflate option for fast compression.
1935 pub const DEFLATE_MODE_FAST: &str = "fast";
1936 /// Deflate option for better compression.
1937 pub const DEFLATE_MODE_BETTER: &str = "better";
1938 /// Deflate option for best compression.
1939 pub const DEFLATE_MODE_BEST: &str = "best";
1941 /// All possible option values for deflate compression.
1942 pub const DEFLATE_OPTION_VALUES: NAOptionDefinitionType = NAOptionDefinitionType::String(Some(&[DEFLATE_MODE_NONE, DEFLATE_MODE_FAST, DEFLATE_MODE_BETTER, DEFLATE_MODE_BEST]));
1944 impl std::str::FromStr for DeflateMode {
1947 fn from_str(s: &str) -> Result<Self, Self::Err> {
1949 DEFLATE_MODE_NONE => Ok(DeflateMode::NoCompr),
1950 DEFLATE_MODE_FAST => Ok(DeflateMode::Fast),
1951 DEFLATE_MODE_BETTER => Ok(DeflateMode::Better),
1952 DEFLATE_MODE_BEST => Ok(DeflateMode::Best),
1958 impl ToString for DeflateMode {
1959 fn to_string(&self) -> String {
1961 DeflateMode::NoCompr => DEFLATE_MODE_NONE.to_string(),
1962 DeflateMode::Fast => DEFLATE_MODE_FAST.to_string(),
1963 DeflateMode::Better => DEFLATE_MODE_BETTER.to_string(),
1964 DeflateMode::Best => DEFLATE_MODE_BEST.to_string(),
1969 #[derive(Clone,Copy,Debug,PartialEq)]
1976 const MAX_BLOCK_SIZE: usize = 65535;
1978 /// Deflate stream compressor.
1979 pub struct Deflate {
1982 srcbuf: [u8; MAX_BLOCK_SIZE],
1987 parser: Box<dyn LZParse + Send>,
1991 /// Creates a new instance of `Deflate`.
1992 pub fn new(mode: DeflateMode) -> Self {
1993 let (mode, parser) = match mode {
1994 DeflateMode::NoCompr => (Mode::Copy, Box::new(NoParser{}) as Box<dyn LZParse + Send>),
1995 DeflateMode::Fast => (Mode::Fixed, Box::new(GreedyParser{}) as Box<dyn LZParse + Send>),
1996 DeflateMode::Better => (Mode::Dynamic, Box::new(LazyParser{}) as Box<dyn LZParse + Send>),
1997 DeflateMode::Best => (Mode::Dynamic, Box::new(OptimalParser::new()) as Box<dyn LZParse + Send>),
2001 tokens: Vec::with_capacity(MAX_BLOCK_SIZE),
2002 srcbuf: [0; MAX_BLOCK_SIZE],
2009 /// Writes zlib stream header.
2010 pub fn write_zlib_header(&mut self, wr: &mut DeflateWriter) {
2013 let level = match self.mode {
2015 Mode::Fixed => 0x5E,
2016 Mode::Dynamic => 0x9C,
2017 // 0xDA for the strongest one
2020 self.zlib_mode = true;
2022 fn write_zlib_footer(&self, wr: &mut DeflateWriter) {
2024 wr.write((self.sum2 >> 8) as u16, 8);
2025 wr.write((self.sum2 & 0xFF) as u16, 8);
2026 wr.write((self.sum1 >> 8) as u16, 8);
2027 wr.write((self.sum1 & 0xFF) as u16, 8);
2029 /// Queues data for compression.
2031 /// The data might be not actually compressed until [`compress_end`] is called.
2033 /// [`compress_end`]: ./struct.Deflate.html#method.compress_end
2034 pub fn compress(&mut self, src: &[u8], wr: &mut DeflateWriter) {
2036 while !src.is_empty() {
2037 let clen = src.len().min(MAX_BLOCK_SIZE - self.ssize);
2038 let (head, tail) = src.split_at(clen);
2040 self.srcbuf[self.ssize..][..clen].copy_from_slice(head);
2042 if self.ssize == MAX_BLOCK_SIZE {
2043 self.do_block(wr, false);
2047 /// Tells the encoder to finish data compression.
2049 /// Complete data will be output after this call.
2050 pub fn compress_end(&mut self, wr: &mut DeflateWriter) {
2052 self.do_block(wr, true);
2056 wr.write(0, 7); //static EOF sym
2059 self.write_zlib_footer(wr);
2062 /// Tells the encoder to compress the data it received and flush it.
2063 pub fn compress_flush(&mut self, wr: &mut DeflateWriter) {
2065 self.do_block(wr, false);
2067 if (wr.bits & 7) != 0 {
2068 // write zero-length copy block for byte-alignment
2073 wr.write(0xFFFF, 16);
2076 fn do_block(&mut self, wr: &mut DeflateWriter, final_block: bool) {
2077 const CRC_BASE: u32 = 65521;
2078 for &b in self.srcbuf[..self.ssize].iter() {
2079 self.sum1 += u32::from(b);
2080 if self.sum1 >= CRC_BASE {
2081 self.sum1 -= CRC_BASE;
2083 self.sum2 += self.sum1;
2084 if self.sum2 >= CRC_BASE {
2085 self.sum2 -= CRC_BASE;
2090 wr.write(final_block as u16, 1);
2093 wr.write(self.ssize as u16, 16);
2094 wr.write(!self.ssize as u16, 16);
2095 for &b in self.srcbuf[..self.ssize].iter() {
2096 wr.write(u16::from(b), 8);
2100 wr.write(final_block as u16, 1);
2102 self.tokens.clear();
2103 self.parser.parse(&self.srcbuf[..self.ssize], &mut self.tokens);
2104 let mut codes = CodeHuff::new(true);
2105 codes.make_codes(&self.tokens);
2106 let mut dists = DistHuff::new(true);
2107 dists.make_codes(&self.tokens);
2108 wr.write_tokens(&self.tokens, &codes, &dists);
2111 wr.write(final_block as u16, 1);
2113 self.tokens.clear();
2114 self.parser.parse(&self.srcbuf[..self.ssize], &mut self.tokens);
2115 let mut codes = CodeHuff::new(false);
2116 codes.make_codes(&self.tokens);
2117 let mut dists = DistHuff::new(false);
2118 dists.make_codes(&self.tokens);
2119 wr.write((codes.num_codes - 257) as u16, 5);
2120 wr.write((dists.num_codes - 1) as u16, 5);
2121 wr.write_codes(&codes, &dists);
2122 wr.write_tokens(&self.tokens, &codes, &dists);
2134 fn test_inflate1() {
2135 const TEST_DATA: &[u8] = &[
2136 0xF3, 0x48, 0xCD, 0xC9, 0xC9, 0xD7, 0x51, 0x28,
2137 0xCF, 0x2F, 0xCA, 0x49, 0x51, 0x04, 0x00 ];
2138 const TEST_REF: &[u8] = b"Hello, world!";
2139 let mut dst_buf = [0u8; 13];
2140 let len = Inflate::uncompress(TEST_DATA, &mut dst_buf).unwrap();
2141 assert_eq!(len, 13);
2143 assert_eq!(dst_buf[i], TEST_REF[i]);
2147 fn test_inflate2() {
2148 const TEST_DATA3: &[u8] = &[ 0x4B, 0x4C, 0x44, 0x80, 0x24, 0x54, 0x80, 0x2C, 0x06, 0x00 ];
2149 const TEST_REF3: &[u8] = b"aaaaaaaaaaaabbbbbbbbbbbbbbbaaaaabbbbbbb";
2150 let mut dst_buf = [0u8; 39];
2152 let mut inflate = Inflate::new();
2153 let mut output_chunk = [0u8; 7];
2154 let mut output_pos = 0;
2155 for input in TEST_DATA3.chunks(3) {
2156 let mut repeat = false;
2158 let ret = inflate.decompress_data(input, &mut output_chunk, repeat);
2162 dst_buf[output_pos + i] = output_chunk[i];
2167 Err(DecompressError::ShortData) => {
2170 Err(DecompressError::OutputFull) => {
2172 for i in 0..output_chunk.len() {
2173 dst_buf[output_pos + i] = output_chunk[i];
2175 output_pos += output_chunk.len();
2178 panic!("decompress error {:?}", ret.err().unwrap());
2184 assert_eq!(output_pos, dst_buf.len());
2185 for i in 0..output_pos {
2186 assert_eq!(dst_buf[i], TEST_REF3[i]);
2190 fn test_inflate3() {
2191 const TEST_DATA: &[u8] = &[
2192 0x1F, 0x8B, 0x08, 0x08, 0xF6, 0x7B, 0x90, 0x5E, 0x02, 0x03, 0x31, 0x2E, 0x74, 0x78, 0x74, 0x00,
2193 0xE5, 0x95, 0x4B, 0x4E, 0xC3, 0x30, 0x10, 0x40, 0xF7, 0x39, 0xC5, 0x1C, 0x00, 0x16, 0x70, 0x83,
2194 0x0A, 0xB5, 0x3B, 0xE8, 0x82, 0x5E, 0x60, 0x1A, 0x4F, 0xE2, 0x11, 0xFE, 0x44, 0x1E, 0xA7, 0x69,
2195 0x6E, 0xCF, 0x38, 0xDD, 0xB0, 0x40, 0xA2, 0x46, 0x2D, 0x20, 0x2A, 0xE5, 0xAB, 0xCC, 0xE7, 0xBD,
2196 0x49, 0xAC, 0x6C, 0x03, 0x64, 0x4B, 0xD0, 0x71, 0x92, 0x0C, 0x06, 0x67, 0x88, 0x1D, 0x3C, 0xD9,
2197 0xC4, 0x92, 0x3D, 0x4A, 0xF3, 0x3C, 0x43, 0x4E, 0x23, 0x81, 0x8B, 0x07, 0x82, 0x1E, 0xF5, 0x90,
2198 0x23, 0x78, 0x6A, 0x56, 0x30, 0x60, 0xCA, 0x89, 0x4D, 0x4F, 0xC0, 0x01, 0x10, 0x06, 0xC2, 0xA4,
2199 0xA1, 0x44, 0xCD, 0xF6, 0x54, 0x50, 0xA8, 0x8D, 0xC1, 0x9C, 0x5F, 0x71, 0x37, 0x45, 0xC8, 0x63,
2200 0xCA, 0x8E, 0xC0, 0xE8, 0x23, 0x69, 0x56, 0x9A, 0x8D, 0x5F, 0xB6, 0xC9, 0x96, 0x53, 0x4D, 0x17,
2201 0xAB, 0xB9, 0xB0, 0x49, 0x14, 0x5A, 0x0B, 0x96, 0x82, 0x7C, 0xB7, 0x6F, 0x17, 0x35, 0xC7, 0x9E,
2202 0xDF, 0x78, 0xA3, 0xF1, 0xD0, 0xA2, 0x73, 0x1C, 0x7A, 0xD8, 0x2B, 0xB3, 0x5C, 0x90, 0x85, 0xBB,
2203 0x2A, 0x14, 0x2E, 0xF7, 0xD1, 0x19, 0x48, 0x0A, 0x23, 0x57, 0x45, 0x13, 0x3E, 0xD6, 0xA0, 0xBD,
2204 0xF2, 0x11, 0x7A, 0x22, 0x21, 0xAD, 0xE5, 0x70, 0x56, 0xA0, 0x9F, 0xA5, 0xA5, 0x03, 0x85, 0x2A,
2205 0xDE, 0x92, 0x00, 0x32, 0x61, 0x10, 0xAD, 0x27, 0x13, 0x7B, 0x5F, 0x98, 0x7F, 0x59, 0x83, 0xB8,
2206 0xB7, 0x35, 0x16, 0xEB, 0x12, 0x0F, 0x1E, 0xD9, 0x14, 0x0B, 0xCF, 0xEE, 0x6D, 0x91, 0xF8, 0x93,
2207 0x6E, 0x81, 0x3F, 0x7F, 0x41, 0xA4, 0x22, 0x1F, 0xB7, 0xE6, 0x85, 0x83, 0x9A, 0xA2, 0x61, 0x12,
2208 0x0D, 0x0F, 0x6D, 0x01, 0xBD, 0xB0, 0xE8, 0x1D, 0xEC, 0xD1, 0xA0, 0xBF, 0x1F, 0x4E, 0xFB, 0x55,
2209 0xBD, 0x73, 0xDD, 0x87, 0xB9, 0x53, 0x23, 0x17, 0xD3, 0xE2, 0xE9, 0x08, 0x87, 0x42, 0xFF, 0xCF,
2210 0x26, 0x42, 0xAE, 0x76, 0xB5, 0xAE, 0x97, 0x0C, 0x18, 0x78, 0xA0, 0x24, 0xE5, 0x54, 0x0C, 0x6E,
2211 0x60, 0x52, 0x79, 0x22, 0x57, 0xF5, 0x87, 0x78, 0x78, 0x04, 0x93, 0x46, 0xEF, 0xCB, 0x98, 0x96,
2212 0x8B, 0x65, 0x00, 0xB7, 0x36, 0xBD, 0x77, 0xA8, 0xBD, 0x5A, 0xAA, 0x1A, 0x09, 0x00, 0x00
2215 let mut mr = MemoryReader::new_read(TEST_DATA);
2216 let mut br = ByteReader::new(&mut mr);
2217 let _dst_buf = gzip_decode(&mut br, false).unwrap();
2219 // println!("{}", String::from_utf8_lossy(_dst_buf.as_slice()));
2222 fn test_deflate_crc() {
2223 let output = Vec::with_capacity(20);
2224 let mut writer = DeflateWriter::new(output);
2225 let mut compr = Deflate::new(DeflateMode::NoCompr);
2226 compr.write_zlib_header(&mut writer);
2227 compr.compress(b"Hello, world!", &mut writer);
2228 compr.compress_end(&mut writer);
2229 let output = writer.end();
2230 assert_eq!(output.as_slice(), b"\x78\x01\x01\x0D\x00\xF2\xFFHello, world!\x20\x5E\x04\x8A");
2232 fn deflate_test(mode: DeflateMode) {
2234 b"The first day of Christmas,
2235 My true love sent to me
2236 A partridge in a pear tree.
2238 The second day of Christmas,
2239 My true love sent to me
2240 Two turtle doves, and
2241 A partridge in a pear tree.
2243 The third day of Christmas,
2244 My true love sent to me
2246 Two turtle doves, and
2247 A partridge in a pear tree.
2249 The fourth day of Christmas,
2250 My true love sent to me
2253 Two turtle doves, and
2254 A partridge in a pear tree.
2256 The fifth day of Christmas,
2257 My true love sent to me
2261 Two turtle doves, and
2262 A partridge in a pear tree.";
2263 let output = Vec::with_capacity(SRC.len() + 16);
2264 let mut writer = DeflateWriter::new(output);
2265 let mut compr = Deflate::new(mode);
2266 compr.write_zlib_header(&mut writer);
2267 compr.compress(SRC, &mut writer);
2268 compr.compress_end(&mut writer);
2269 let output = writer.end();
2270 let mut uncompr = vec![0u8; SRC.len()];
2271 Inflate::uncompress(&output, &mut uncompr).unwrap();
2272 assert_eq!(SRC, uncompr.as_slice());
2275 fn test_deflate_fast() {
2276 deflate_test(DeflateMode::Fast);
2279 fn test_deflate_better() {
2280 deflate_test(DeflateMode::Better);
2283 fn test_deflate_best() {
2284 deflate_test(DeflateMode::Best);