1#[cfg(feature = "alloc")]
2use alloc::borrow::Cow;
3#[cfg(feature = "alloc")]
4use alloc::vec;
5#[cfg(feature = "alloc")]
6use alloc::vec::Vec;
7
8use crate::block_encoder::{self, BlockEncodeWorkspace};
9use crate::output::{OutputSink, SliceSink};
10use crate::strategy::{self, LevelParams, Strategy};
11use crate::{dfast, fast, skip_match_search, write_frame_header_with_checksum};
12use zrip_core::Sequence;
13use zrip_core::dict::Dictionary;
14use zrip_core::error::CompressError;
15use zrip_core::frame::MAX_BLOCK_SIZE;
16use zrip_core::huffman::encode::HuffmanEncodeTable;
17use zrip_core::xxhash::{Xxh64State, xxh64};
18
19const ATTACH_THRESHOLD: usize = 16384;
25
26pub(crate) struct PreparedDict {
27 combined: Vec<u8>,
28 hash_snapshot: Vec<u32>,
29 hash_long_snapshot: Vec<u32>,
30 hash_log: u32,
31 prefix_len: usize,
32 rep_offsets: [u32; 3],
33 dict_id: u32,
34 huf_table: Option<HuffmanEncodeTable>,
35 ll_table: Option<block_encoder::FseEncodeTable>,
36 of_table: Option<block_encoder::FseEncodeTable>,
37 ml_table: Option<block_encoder::FseEncodeTable>,
38}
39
40impl PreparedDict {
41 pub fn new(dict: &Dictionary, params: &LevelParams) -> Self {
42 let prefix = dict.content();
43 let prefix_len = prefix.len();
44
45 let mut combined = Vec::with_capacity(prefix_len + MAX_BLOCK_SIZE);
46 combined.extend_from_slice(prefix);
47
48 let (hash_snapshot, hash_long_snapshot) = match params.strategy {
49 Strategy::Fast => {
50 let hash_size = 1usize << params.hash_log;
51 let mut hash_table = vec![0u32; hash_size];
52 fast::prefill_hash_table(&combined, prefix_len, params.hash_log, &mut hash_table);
53 (hash_table, Vec::new())
54 }
55 Strategy::DFast => {
56 let short_size = 1usize << params.chain_log;
57 let long_size = 1usize << params.hash_log;
58 let mut hash_short = vec![0u32; short_size];
59 let mut hash_long = vec![0u32; long_size];
60 dfast::prefill_hash_tables(
61 &combined,
62 prefix_len,
63 params.hash_log,
64 params.chain_log,
65 params.min_match,
66 &mut hash_short,
67 &mut hash_long,
68 );
69 (hash_short, hash_long)
70 }
71 };
72
73 let huf_table = dict
74 .huf_table()
75 .and_then(|(dt, tl)| HuffmanEncodeTable::from_decode_table(dt, tl));
76
77 let ll_table = dict
78 .ll_table()
79 .map(|(dt, al)| block_encoder::FseEncodeTable::from_decode_table(dt, al, 35));
80 let of_table = dict
81 .of_table()
82 .map(|(dt, al)| block_encoder::FseEncodeTable::from_decode_table(dt, al, 31));
83 let ml_table = dict
84 .ml_table()
85 .map(|(dt, al)| block_encoder::FseEncodeTable::from_decode_table(dt, al, 52));
86
87 Self {
88 combined,
89 hash_snapshot,
90 hash_long_snapshot,
91 hash_log: params.hash_log,
92 prefix_len,
93 rep_offsets: *dict.rep_offsets(),
94 dict_id: dict.id(),
95 huf_table,
96 ll_table,
97 of_table,
98 ml_table,
99 }
100 }
101}
102
103pub struct CompressContext {
117 level: i32,
118 content_checksum: bool,
119 prepared: Option<PreparedDict>,
120 hash_table: Vec<u32>,
121 hash_long: Vec<u32>,
122 dict_hash: Vec<u32>,
123 small_hash: Vec<u32>,
124 sequences: Vec<Sequence>,
125 output: Vec<u8>,
126 workspace: BlockEncodeWorkspace,
127 combined: Vec<u8>,
128}
129
130impl CompressContext {
131 pub fn new(level: i32) -> Result<Self, CompressError> {
133 let params = strategy::level_params(level).ok_or(CompressError::InvalidLevel(level))?;
134 let max_log = strategy::max_hash_log(level).expect("level validated above");
135 let alloc_size = 1usize << max_log;
136 let (hash_table, hash_long) = match params.strategy {
137 Strategy::Fast => (vec![0u32; alloc_size], Vec::new()),
138 Strategy::DFast => (vec![0u32; alloc_size], vec![0u32; alloc_size]),
139 };
140 Ok(Self {
141 level,
142 content_checksum: true,
143 prepared: None,
144 hash_table,
145 hash_long,
146 dict_hash: Vec::new(),
147 small_hash: Vec::new(),
148 sequences: Vec::new(),
149 output: Vec::new(),
150 workspace: BlockEncodeWorkspace::new(),
151 combined: Vec::new(),
152 })
153 }
154
155 pub fn with_dict(level: i32, dict: Dictionary) -> Result<Self, CompressError> {
165 Self::with_dict_for_size(level, dict, usize::MAX)
166 }
167
168 pub fn with_dict_for_size(
175 level: i32,
176 dict: Dictionary,
177 expected_size: usize,
178 ) -> Result<Self, CompressError> {
179 let total_window = dict.content().len().saturating_add(expected_size);
180 let params = strategy::level_params_for_size(level, total_window)
181 .ok_or(CompressError::InvalidLevel(level))?;
182 let prepared = PreparedDict::new(&dict, ¶ms);
183 let hash_table = vec![0u32; prepared.hash_snapshot.len()];
184 let hash_long = vec![0u32; prepared.hash_long_snapshot.len()];
185 Ok(Self {
186 level,
187 content_checksum: true,
188 prepared: Some(prepared),
189 hash_table,
190 hash_long,
191 dict_hash: Vec::new(),
192 small_hash: Vec::new(),
193 sequences: Vec::new(),
194 output: Vec::new(),
195 workspace: BlockEncodeWorkspace::new(),
196 combined: Vec::new(),
197 })
198 }
199
200 pub const fn set_content_checksum(&mut self, enabled: bool) {
205 self.content_checksum = enabled;
206 }
207
208 pub const fn content_checksum(&self) -> bool {
210 self.content_checksum
211 }
212
213 pub fn compress(&mut self, input: &[u8]) -> Result<Cow<'_, [u8]>, CompressError> {
215 let mut output = core::mem::take(&mut self.output);
216 output.clear();
217 let result = self.compress_to(input, &mut output);
218 self.output = output;
219 result?;
220 Ok(self.take_or_borrow_output())
221 }
222
223 pub fn compress_into(
232 &mut self,
233 input: &[u8],
234 output: &mut [u8],
235 ) -> Result<usize, CompressError> {
236 let mut sink = SliceSink::new(output);
237 self.compress_to(input, &mut sink)?;
238 Ok(sink.pos())
239 }
240
241 fn compress_to<S: OutputSink>(
243 &mut self,
244 input: &[u8],
245 output: &mut S,
246 ) -> Result<(), CompressError> {
247 if self.prepared.is_some() {
248 return self.compress_with_prepared(input, output);
249 }
250 let params = strategy::level_params_for_size(self.level, input.len())
251 .expect("level validated at construction");
252 compress_core(
253 input,
254 params,
255 None,
256 &[],
257 [1u32, 4, 8],
258 &mut self.hash_table,
259 &mut self.hash_long,
260 &mut self.dict_hash,
261 &mut self.sequences,
262 output,
263 &mut self.workspace,
264 &mut self.combined,
265 self.content_checksum,
266 )
267 }
268
269 pub fn compress_with_dict(
271 &mut self,
272 input: &[u8],
273 dict: &Dictionary,
274 ) -> Result<Cow<'_, [u8]>, CompressError> {
275 let total_window = dict.content().len().saturating_add(input.len());
276 let params = strategy::level_params_for_size(self.level, total_window)
277 .expect("level validated at construction");
278 self.workspace.prev_ll = dict
279 .ll_table()
280 .map(|(dt, al)| block_encoder::FseEncodeTable::from_decode_table(dt, al, 35));
281 self.workspace.prev_of = dict
282 .of_table()
283 .map(|(dt, al)| block_encoder::FseEncodeTable::from_decode_table(dt, al, 31));
284 self.workspace.prev_ml = dict
285 .ml_table()
286 .map(|(dt, al)| block_encoder::FseEncodeTable::from_decode_table(dt, al, 52));
287 self.workspace.prev_huffman = dict
288 .huf_table()
289 .and_then(|(dt, tl)| HuffmanEncodeTable::from_decode_table(dt, tl));
290 self.output.clear();
291 compress_core(
292 input,
293 params,
294 Some(dict.id()),
295 dict.content(),
296 *dict.rep_offsets(),
297 &mut self.hash_table,
298 &mut self.hash_long,
299 &mut self.dict_hash,
300 &mut self.sequences,
301 &mut self.output,
302 &mut self.workspace,
303 &mut self.combined,
304 self.content_checksum,
305 )?;
306 Ok(self.take_or_borrow_output())
307 }
308
309 fn compress_with_prepared<S: OutputSink>(
310 &mut self,
311 input: &[u8],
312 output: &mut S,
313 ) -> Result<(), CompressError> {
314 let prep = self.prepared.as_ref().unwrap();
315 let total_window = prep.prefix_len + input.len();
316 let params = strategy::level_params_for_size(self.level, total_window)
317 .expect("level validated at construction");
318
319 let use_attached = !input.is_empty()
320 && input.len() <= ATTACH_THRESHOLD
321 && params.strategy == Strategy::Fast;
322
323 let dict_id = prep.dict_id;
324 let prefix_len = prep.prefix_len;
325 let dict_hash_log = prep.hash_log;
326
327 if !use_attached {
328 let snapshot_matches = match params.strategy {
329 Strategy::Fast => (1usize << params.hash_log) == prep.hash_snapshot.len(),
330 Strategy::DFast => {
331 (1usize << params.chain_log) == prep.hash_snapshot.len()
332 && (1usize << params.hash_log) == prep.hash_long_snapshot.len()
333 }
334 };
335 if !snapshot_matches {
336 return self.compress_with_dict_fallback(input, dict_id, prefix_len, output);
337 }
338 }
339
340 {
341 let prep = self.prepared.as_mut().unwrap();
342 if !use_attached {
343 self.hash_table.clear();
346 self.hash_table.extend_from_slice(&prep.hash_snapshot);
347 if !prep.hash_long_snapshot.is_empty() {
348 self.hash_long.clear();
349 self.hash_long.extend_from_slice(&prep.hash_long_snapshot);
350 }
351 }
352 prep.combined.truncate(prep.prefix_len);
353 prep.combined.extend_from_slice(input);
354 }
355
356 let prep = self.prepared.as_ref().unwrap();
357
358 if use_attached {
359 self.workspace.prev_huffman = if params.force_raw_literals {
360 None
361 } else {
362 prep.huf_table.clone()
363 };
364 } else if let Some(ref huf) = prep.huf_table {
365 self.workspace.prev_huffman = Some(huf.clone());
366 } else {
367 self.workspace.prev_huffman = None;
368 }
369 self.workspace.prev_ll = prep.ll_table.clone();
370 self.workspace.prev_of = prep.of_table.clone();
371 self.workspace.prev_ml = prep.ml_table.clone();
372
373 output.reserve(input.len() + 32);
374 write_frame_header_with_checksum(
375 output,
376 input.len(),
377 Some(dict_id),
378 params.window_log,
379 self.content_checksum,
380 )?;
381
382 if input.is_empty() {
383 block_encoder::encode_raw_block(&[], true, output)?;
384 } else if use_attached {
385 let input_hash_log = if input.len() >= 2 {
386 let src_log = 32 - ((input.len() as u32) - 1).leading_zeros();
387 params.hash_log.min(src_log).max(strategy::HASH_LOG_MIN)
388 } else {
389 strategy::HASH_LOG_MIN
390 };
391 let input_hash_size = 1usize << input_hash_log;
392 self.small_hash.resize(input_hash_size, 0);
393 self.small_hash.fill(0);
394
395 let mut rep_offsets = prep.rep_offsets;
396
397 fast::compress_fast_attached(
398 &prep.combined,
399 prefix_len,
400 prefix_len + input.len(),
401 ¶ms,
402 &prep.rep_offsets,
403 &prep.hash_snapshot,
404 dict_hash_log,
405 &mut self.small_hash,
406 input_hash_log,
407 &mut self.sequences,
408 );
409
410 if params.force_raw_literals {
411 block_encoder::encode_compressed_block_raw(
412 input,
413 &self.sequences,
414 &mut rep_offsets,
415 true,
416 output,
417 &mut self.workspace,
418 )?;
419 } else {
420 block_encoder::encode_compressed_block(
421 input,
422 &self.sequences,
423 &mut rep_offsets,
424 true,
425 output,
426 &mut self.workspace,
427 strategy::block_policy(¶ms, input.len()),
428 )?;
429 }
430 } else {
431 let combined = &prep.combined;
432 let mut rep_offsets = prep.rep_offsets;
433
434 if input.len() <= MAX_BLOCK_SIZE {
435 match params.strategy {
436 Strategy::Fast => {
437 fast::compress_fast_block(
438 combined,
439 prefix_len,
440 prefix_len + input.len(),
441 ¶ms,
442 &rep_offsets,
443 &mut self.hash_table,
444 &mut self.sequences,
445 );
446 }
447 Strategy::DFast => {
448 dfast::compress_dfast_block(
449 combined,
450 prefix_len,
451 prefix_len + input.len(),
452 ¶ms,
453 &rep_offsets,
454 &mut self.hash_table,
455 &mut self.hash_long,
456 &mut self.sequences,
457 );
458 }
459 }
460 if params.force_raw_literals {
461 block_encoder::encode_compressed_block_raw(
462 input,
463 &self.sequences,
464 &mut rep_offsets,
465 true,
466 output,
467 &mut self.workspace,
468 )?;
469 } else {
470 block_encoder::encode_compressed_block(
471 input,
472 &self.sequences,
473 &mut rep_offsets,
474 true,
475 output,
476 &mut self.workspace,
477 strategy::block_policy(¶ms, input.len()),
478 )?;
479 }
480 } else {
481 let mut offset = 0;
482 while offset < input.len() {
483 let chunk_size = (input.len() - offset).min(MAX_BLOCK_SIZE);
484 let is_last = offset + chunk_size >= input.len();
485 match params.strategy {
486 Strategy::Fast => {
487 fast::compress_fast_block(
488 combined,
489 prefix_len + offset,
490 prefix_len + offset + chunk_size,
491 ¶ms,
492 &rep_offsets,
493 &mut self.hash_table,
494 &mut self.sequences,
495 );
496 }
497 Strategy::DFast => {
498 dfast::compress_dfast_block(
499 combined,
500 prefix_len + offset,
501 prefix_len + offset + chunk_size,
502 ¶ms,
503 &rep_offsets,
504 &mut self.hash_table,
505 &mut self.hash_long,
506 &mut self.sequences,
507 );
508 }
509 }
510 if params.force_raw_literals {
511 block_encoder::encode_compressed_block_raw(
512 &input[offset..offset + chunk_size],
513 &self.sequences,
514 &mut rep_offsets,
515 is_last,
516 output,
517 &mut self.workspace,
518 )?;
519 } else {
520 block_encoder::encode_compressed_block(
521 &input[offset..offset + chunk_size],
522 &self.sequences,
523 &mut rep_offsets,
524 is_last,
525 output,
526 &mut self.workspace,
527 strategy::block_policy(¶ms, input.len()),
528 )?;
529 }
530 offset += chunk_size;
531 }
532 }
533 }
534
535 if self.content_checksum {
536 let hash = xxh64(input, 0);
537 let checksum = (hash & 0xFFFF_FFFF) as u32;
538 output.extend_from_slice(&checksum.to_le_bytes())?;
539 }
540
541 Ok(())
542 }
543
544 fn compress_with_dict_fallback<S: OutputSink>(
545 &mut self,
546 input: &[u8],
547 dict_id: u32,
548 prefix_len: usize,
549 output: &mut S,
550 ) -> Result<(), CompressError> {
551 let prep = self.prepared.as_ref().unwrap();
552 let rep_offsets = prep.rep_offsets;
553 let prefix = &prep.combined[..prefix_len];
554
555 let total_window = prefix_len.saturating_add(input.len());
556 let params = strategy::level_params_for_size(self.level, total_window)
557 .expect("level validated at construction");
558 self.workspace.prev_huffman = prep.huf_table.clone();
559 self.workspace.prev_ll = prep.ll_table.clone();
560 self.workspace.prev_of = prep.of_table.clone();
561 self.workspace.prev_ml = prep.ml_table.clone();
562 compress_core(
563 input,
564 params,
565 Some(dict_id),
566 prefix,
567 rep_offsets,
568 &mut self.hash_table,
569 &mut self.hash_long,
570 &mut self.dict_hash,
571 &mut self.sequences,
572 output,
573 &mut self.workspace,
574 &mut self.combined,
575 self.content_checksum,
576 )
577 }
578
579 fn take_or_borrow_output(&mut self) -> Cow<'_, [u8]> {
580 if self.output.len() >= zrip_core::LARGE_OUTPUT_THRESHOLD {
581 Cow::Owned(core::mem::take(&mut self.output))
582 } else {
583 Cow::Borrowed(&self.output)
584 }
585 }
586}
587
588#[allow(clippy::too_many_arguments, clippy::unnecessary_wraps)]
589fn compress_core<S: OutputSink>(
590 input: &[u8],
591 params: LevelParams,
592 dict_id: Option<u32>,
593 prefix: &[u8],
594 init_rep_offsets: [u32; 3],
595 hash_table: &mut Vec<u32>,
596 hash_long: &mut Vec<u32>,
597 dict_hash: &mut Vec<u32>,
598 sequences: &mut Vec<Sequence>,
599 output: &mut S,
600 workspace: &mut BlockEncodeWorkspace,
601 combined: &mut Vec<u8>,
602 content_checksum: bool,
603) -> Result<(), CompressError> {
604 let hash_size = match params.strategy {
605 Strategy::Fast => 1usize << params.hash_log,
606 Strategy::DFast => 1usize << params.chain_log,
607 };
608 let long_size = 1usize << params.hash_log;
609
610 if prefix.is_empty() {
611 workspace.prev_huffman = None;
612 workspace.prev_ll = None;
613 workspace.prev_of = None;
614 workspace.prev_ml = None;
615 }
616
617 output.reserve(input.len() + 32);
618 write_frame_header_with_checksum(
619 output,
620 input.len(),
621 dict_id,
622 params.window_log,
623 content_checksum,
624 )?;
625
626 let mut checksum = content_checksum.then(|| Xxh64State::new(0));
629 let mut hashed_blocks = false;
630
631 if input.is_empty() {
632 block_encoder::encode_raw_block(&[], true, output)?;
633 } else {
634 let has_prefix = !prefix.is_empty();
635 let mut rep_offsets = init_rep_offsets;
636 let mut offset = 0;
637
638 if hash_table.len() != hash_size {
639 hash_table.resize(hash_size, 0);
640 }
641
642 match params.strategy {
643 Strategy::Fast => {
644 if has_prefix && input.len() <= MAX_BLOCK_SIZE {
645 if dict_hash.len() != hash_size {
646 dict_hash.resize(hash_size, 0);
647 }
648 fast::compress_fast_with_prefix_reuse(
649 input,
650 ¶ms,
651 &rep_offsets,
652 prefix,
653 dict_hash,
654 hash_table,
655 sequences,
656 combined,
657 );
658 if params.force_raw_literals {
659 block_encoder::encode_compressed_block_raw(
660 input,
661 sequences,
662 &mut rep_offsets,
663 true,
664 output,
665 workspace,
666 )?;
667 } else {
668 block_encoder::encode_compressed_block(
669 input,
670 sequences,
671 &mut rep_offsets,
672 true,
673 output,
674 workspace,
675 strategy::block_policy(¶ms, input.len()),
676 )?;
677 }
678 } else if has_prefix {
679 combined.clear();
680 combined.reserve(prefix.len() + input.len());
681 combined.extend_from_slice(prefix);
682 combined.extend_from_slice(input);
683 let plen = prefix.len();
684 fast::prefill_hash_table(combined, plen, params.hash_log, hash_table);
685
686 while offset < input.len() {
687 let chunk_size = (input.len() - offset).min(MAX_BLOCK_SIZE);
688 let is_last = offset + chunk_size >= input.len();
689 fast::compress_fast_block(
690 combined,
691 plen + offset,
692 plen + offset + chunk_size,
693 ¶ms,
694 &rep_offsets,
695 hash_table,
696 sequences,
697 );
698 if params.force_raw_literals {
699 block_encoder::encode_compressed_block_raw(
700 &input[offset..offset + chunk_size],
701 sequences,
702 &mut rep_offsets,
703 is_last,
704 output,
705 workspace,
706 )?;
707 } else {
708 block_encoder::encode_compressed_block(
709 &input[offset..offset + chunk_size],
710 sequences,
711 &mut rep_offsets,
712 is_last,
713 output,
714 workspace,
715 strategy::block_policy(¶ms, input.len()),
716 )?;
717 }
718 offset += chunk_size;
719 }
720 } else {
721 hash_table.fill(0);
722 while offset < input.len() {
723 let chunk_size = (input.len() - offset).min(MAX_BLOCK_SIZE);
724 let block_end = offset + chunk_size;
725 let is_last = block_end >= input.len();
726 let block = &input[offset..block_end];
727
728 if skip_match_search(¶ms, input.len(), block) {
729 block_encoder::encode_raw_block(block, is_last, output)?;
730 } else {
731 fast::compress_fast_block(
732 input,
733 offset,
734 block_end,
735 ¶ms,
736 &rep_offsets,
737 hash_table,
738 sequences,
739 );
740 if params.force_raw_literals {
741 block_encoder::encode_compressed_block_raw(
742 block,
743 sequences,
744 &mut rep_offsets,
745 is_last,
746 output,
747 workspace,
748 )?;
749 } else {
750 block_encoder::encode_compressed_block(
751 block,
752 sequences,
753 &mut rep_offsets,
754 is_last,
755 output,
756 workspace,
757 strategy::block_policy(¶ms, input.len()),
758 )?;
759 }
760 }
761 if let Some(c) = checksum.as_mut() {
762 c.update(block);
763 }
764 offset = block_end;
765 }
766 hashed_blocks = true;
767 }
768 }
769 Strategy::DFast => {
770 if hash_long.len() != long_size {
771 hash_long.resize(long_size, 0);
772 }
773 if has_prefix && input.len() <= MAX_BLOCK_SIZE {
774 dfast::compress_dfast_with_prefix_reuse(
775 input,
776 ¶ms,
777 &rep_offsets,
778 prefix,
779 hash_table,
780 hash_long,
781 sequences,
782 combined,
783 );
784 block_encoder::encode_compressed_block(
785 input,
786 sequences,
787 &mut rep_offsets,
788 true,
789 output,
790 workspace,
791 strategy::block_policy(¶ms, input.len()),
792 )?;
793 } else if has_prefix {
794 combined.clear();
795 combined.reserve(prefix.len() + input.len());
796 combined.extend_from_slice(prefix);
797 combined.extend_from_slice(input);
798 let plen = prefix.len();
799 dfast::prefill_hash_tables(
800 combined,
801 plen,
802 params.hash_log,
803 params.chain_log,
804 params.min_match,
805 hash_table,
806 hash_long,
807 );
808
809 while offset < input.len() {
810 let chunk_size = (input.len() - offset).min(MAX_BLOCK_SIZE);
811 let is_last = offset + chunk_size >= input.len();
812 dfast::compress_dfast_block(
813 combined,
814 plen + offset,
815 plen + offset + chunk_size,
816 ¶ms,
817 &rep_offsets,
818 hash_table,
819 hash_long,
820 sequences,
821 );
822 block_encoder::encode_compressed_block(
823 &input[offset..offset + chunk_size],
824 sequences,
825 &mut rep_offsets,
826 is_last,
827 output,
828 workspace,
829 strategy::block_policy(¶ms, input.len()),
830 )?;
831 offset += chunk_size;
832 }
833 } else {
834 hash_table.fill(0);
835 hash_long.fill(0);
836 while offset < input.len() {
837 let chunk_size = (input.len() - offset).min(MAX_BLOCK_SIZE);
838 let block_end = offset + chunk_size;
839 let is_last = block_end >= input.len();
840 let block = &input[offset..block_end];
841
842 if skip_match_search(¶ms, input.len(), block) {
843 block_encoder::encode_raw_block(block, is_last, output)?;
844 } else {
845 dfast::compress_dfast_block(
846 input,
847 offset,
848 block_end,
849 ¶ms,
850 &rep_offsets,
851 hash_table,
852 hash_long,
853 sequences,
854 );
855 block_encoder::encode_compressed_block(
856 block,
857 sequences,
858 &mut rep_offsets,
859 is_last,
860 output,
861 workspace,
862 strategy::block_policy(¶ms, input.len()),
863 )?;
864 }
865 if let Some(c) = checksum.as_mut() {
866 c.update(block);
867 }
868 offset = block_end;
869 }
870 hashed_blocks = true;
871 }
872 }
873 }
874 }
875
876 if let Some(c) = checksum {
877 let hash = if hashed_blocks {
878 c.finish()
879 } else {
880 xxh64(input, 0)
881 };
882 output.extend_from_slice(&((hash & 0xFFFF_FFFF) as u32).to_le_bytes())?;
883 }
884
885 Ok(())
886}