Skip to main content

zrip_encode/
lib.rs

1#![cfg_attr(not(feature = "std"), no_std)]
2#![cfg_attr(feature = "nightly", feature(optimize_attribute))]
3#![forbid(unsafe_code)]
4
5#[cfg(feature = "alloc")]
6extern crate alloc;
7
8/// Evaluates `$body` compiled for x86-64-v3 (AVX2, BMI1/2, LZCNT) when the CPU
9/// supports it, and compiled for the build target otherwise.
10///
11/// Use it as a whole function body around a call to an `#[inline(always)]`
12/// implementation, so the implementation is duplicated into both contexts.
13/// `fearless_simd` selects the level at runtime and provides the safe
14/// target-feature entry point.
15#[cfg(all(feature = "simd", target_arch = "x86_64", not(target_feature = "avx2")))]
16macro_rules! simd_body {
17    ($body:expr) => {
18        match fearless_simd::Level::new().as_avx2() {
19            Some(avx2) => fearless_simd::Simd::vectorize(
20                avx2,
21                #[inline(always)]
22                || $body,
23            ),
24            None => $body,
25        }
26    };
27}
28
29#[cfg(not(all(feature = "simd", target_arch = "x86_64", not(target_feature = "avx2"))))]
30macro_rules! simd_body {
31    ($body:expr) => {
32        $body
33    };
34}
35
36pub(crate) mod block_encoder;
37#[cfg(feature = "std")]
38pub mod context;
39pub(crate) mod dfast;
40pub(crate) mod fast;
41#[cfg(feature = "ldm")]
42pub(crate) mod ldm;
43mod output;
44pub(crate) mod primitives;
45pub mod strategy;
46#[cfg(feature = "std")]
47pub mod streaming;
48
49#[cfg(feature = "alloc")]
50use alloc::vec;
51#[cfg(feature = "alloc")]
52use alloc::vec::Vec;
53
54use crate::output::{OutputSink, SliceSink};
55use crate::strategy::Strategy;
56use zrip_core::error::CompressError;
57use zrip_core::frame::{MAX_BLOCK_SIZE, MAX_WINDOW_SIZE, ZSTD_MAGIC};
58use zrip_core::xxhash::xxh64;
59
60pub(crate) fn write_frame_header(
61    output: &mut impl OutputSink,
62    content_size: usize,
63    dict_id: Option<u32>,
64    window_log: u32,
65) -> Result<(), CompressError> {
66    write_frame_header_inner(output, Some(content_size), dict_id, window_log, true)
67}
68
69#[cfg_attr(not(feature = "std"), allow(dead_code))]
70pub(crate) fn write_frame_header_with_checksum(
71    output: &mut impl OutputSink,
72    content_size: usize,
73    dict_id: Option<u32>,
74    window_log: u32,
75    content_checksum: bool,
76) -> Result<(), CompressError> {
77    write_frame_header_inner(
78        output,
79        Some(content_size),
80        dict_id,
81        window_log,
82        content_checksum,
83    )
84}
85
86#[cfg_attr(not(feature = "std"), allow(dead_code))]
87pub(crate) fn write_frame_header_without_content_size(
88    output: &mut impl OutputSink,
89    dict_id: Option<u32>,
90    window_log: u32,
91) -> Result<(), CompressError> {
92    write_frame_header_inner(output, None, dict_id, window_log, true)
93}
94
95fn write_frame_header_inner(
96    output: &mut impl OutputSink,
97    content_size: Option<usize>,
98    dict_id: Option<u32>,
99    window_log: u32,
100    content_checksum: bool,
101) -> Result<(), CompressError> {
102    output.extend_from_slice(&ZSTD_MAGIC.to_le_bytes())?;
103
104    let single_segment =
105        dict_id.is_none() && content_size.is_some_and(|size| size as u64 <= MAX_WINDOW_SIZE);
106    let fcs_size = content_size.map_or(0, |size| {
107        frame_content_size_field_size(size, single_segment)
108    });
109    let fcs_flag: u8 = match fcs_size {
110        0 => 0,
111        1 => 0,
112        2 => 1,
113        4 => 2,
114        _ => 3,
115    };
116
117    let dict_id_flag: u8 = match dict_id {
118        None => 0,
119        Some(id) if id <= 0xFF => 1,
120        Some(id) if id <= 0xFFFF => 2,
121        Some(_) => 3,
122    };
123
124    let descriptor = if single_segment { 0x20 } else { 0 }
125        | if content_checksum { 0x04 } else { 0 }
126        | (fcs_flag << 6)
127        | dict_id_flag;
128    output.push(descriptor)?;
129
130    if !single_segment {
131        output.push(window_descriptor_for_log(window_log))?;
132    }
133
134    match dict_id {
135        Some(id) if id <= 0xFF => output.push(id as u8)?,
136        Some(id) if id <= 0xFFFF => output.extend_from_slice(&(id as u16).to_le_bytes())?,
137        Some(id) => output.extend_from_slice(&id.to_le_bytes())?,
138        None => {}
139    }
140
141    let Some(content_size) = content_size else {
142        return Ok(());
143    };
144    match fcs_size {
145        0 => {}
146        1 => output.push(content_size as u8)?,
147        2 => {
148            let v = (content_size - 256) as u16;
149            output.extend_from_slice(&v.to_le_bytes())?;
150        }
151        4 => output.extend_from_slice(&(content_size as u32).to_le_bytes())?,
152        _ => output.extend_from_slice(&(content_size as u64).to_le_bytes())?,
153    }
154    Ok(())
155}
156
157fn frame_content_size_field_size(content_size: usize, single_segment: bool) -> usize {
158    if single_segment && content_size <= 255 {
159        1
160    } else if (256..=0xFFFF + 256).contains(&content_size) {
161        2
162    } else if content_size <= 0xFFFF_FFFF {
163        4
164    } else {
165        8
166    }
167}
168
169fn window_descriptor_for_log(window_log: u32) -> u8 {
170    let window_log = window_log.clamp(strategy::WINDOW_LOG_MIN, strategy::WINDOW_LOG_MAX);
171    ((window_log - 10) as u8) << 3
172}
173
174/// Whether to store `data`, a block of an `input_len`-byte input, raw without
175/// a match search.
176///
177/// Inputs that keep raw literals skip the sampling: it costs about as much as
178/// their match search, and a block without matches is stored raw anyway.
179pub(crate) fn skip_match_search(
180    params: &strategy::LevelParams,
181    input_len: usize,
182    data: &[u8],
183) -> bool {
184    !strategy::keeps_raw_literals(params, input_len) && block_looks_incompressible(data)
185}
186
187/// Whether `data` looks as flat as random bytes.
188///
189/// Samples eight 512-byte windows spread over the block and applies C zstd's
190/// Huffman bound: no byte value may occur much more than 1/128 of the time.
191/// A single window at the start would miss compressible data later in the
192/// block.
193fn block_looks_incompressible(data: &[u8]) -> bool {
194    const WINDOWS: usize = 8;
195    const WINDOW: usize = 512;
196    if data.len() < WINDOWS * WINDOW {
197        return false;
198    }
199    // Four interleaved tables, so runs of one byte value do not serialize on
200    // a single counter.
201    let mut counts = [[0u16; 256]; 4];
202    for w in 0..WINDOWS {
203        let start = data.len() * w / WINDOWS;
204        let (chunks, _) = data[start..start + WINDOW].as_chunks::<4>();
205        for c in chunks {
206            counts[0][c[0] as usize] += 1;
207            counts[1][c[1] as usize] += 1;
208            counts[2][c[2] as usize] += 1;
209            counts[3][c[3] as usize] += 1;
210        }
211    }
212    let largest = (0..256)
213        .map(|i| usize::from(counts[0][i] + counts[1][i] + counts[2][i] + counts[3][i]))
214        .max()
215        .unwrap();
216    largest <= ((WINDOWS * WINDOW) >> 7) + 4
217}
218
219pub(crate) fn clamp_params_to_src_size(params: &mut strategy::LevelParams, src_len: usize) {
220    params.hash_log = params
221        .hash_log
222        .clamp(strategy::HASH_LOG_MIN, strategy::HASH_LOG_MAX);
223    params.chain_log = params
224        .chain_log
225        .clamp(strategy::HASH_LOG_MIN, strategy::HASH_LOG_MAX);
226    params.window_log = params
227        .window_log
228        .clamp(strategy::WINDOW_LOG_MIN, strategy::WINDOW_LOG_MAX);
229    if src_len >= 2 {
230        let src_log = 32 - ((src_len as u32) - 1).leading_zeros();
231        params.hash_log = params.hash_log.min(src_log).max(strategy::HASH_LOG_MIN);
232        params.chain_log = params.chain_log.min(src_log).max(strategy::HASH_LOG_MIN);
233        params.window_log = params.window_log.min(src_log);
234    }
235}
236
237pub fn compress_with_params(
238    input: &[u8],
239    params: &strategy::LevelParams,
240) -> Result<Vec<u8>, CompressError> {
241    let mut params = *params;
242    clamp_params_to_src_size(&mut params, input.len());
243    compress_inner(input, &params)
244}
245
246pub fn compress(input: &[u8], level: i32) -> Result<Vec<u8>, CompressError> {
247    let params = strategy::level_params_for_size(level, input.len())
248        .ok_or(CompressError::InvalidLevel(level))?;
249    compress_inner(input, &params)
250}
251
252pub fn compress_opts(
253    input: &[u8],
254    level: i32,
255    opts: &strategy::Options,
256) -> Result<Vec<u8>, CompressError> {
257    let mut params = strategy::level_params_for_size(level, input.len())
258        .ok_or(CompressError::InvalidLevel(level))?;
259    strategy::apply_options(&mut params, opts);
260    clamp_params_to_src_size(&mut params, input.len());
261    compress_inner(input, &params)
262}
263
264#[allow(clippy::unnecessary_wraps)]
265fn compress_inner(input: &[u8], params: &strategy::LevelParams) -> Result<Vec<u8>, CompressError> {
266    let mut output = Vec::with_capacity(input.len() + 32);
267    compress_frame(input, params, &mut output)?;
268    Ok(output)
269}
270
271fn compress_frame(
272    input: &[u8],
273    params: &strategy::LevelParams,
274    output: &mut impl OutputSink,
275) -> Result<(), CompressError> {
276    write_frame_header(output, input.len(), None, params.window_log)?;
277
278    if input.is_empty() {
279        block_encoder::encode_raw_block(&[], true, output)?;
280    } else {
281        let mut rep_offsets = [1u32, 4, 8];
282        let mut offset = 0;
283        let mut sequences = Vec::with_capacity(MAX_BLOCK_SIZE / 8);
284        let mut workspace = block_encoder::BlockEncodeWorkspace::new();
285
286        #[cfg(feature = "ldm")]
287        let mut ldm_state = params.ldm_params.as_ref().map(ldm::LdmState::new);
288
289        match params.strategy {
290            Strategy::Fast => {
291                let hash_size = 1usize << params.hash_log;
292                let mut hash_table = vec![0u32; hash_size];
293                while offset < input.len() {
294                    let chunk_size = (input.len() - offset).min(MAX_BLOCK_SIZE);
295                    let block_end = offset + chunk_size;
296                    let is_last = block_end >= input.len();
297                    let block = &input[offset..block_end];
298
299                    if skip_match_search(params, input.len(), block) {
300                        block_encoder::encode_raw_block(block, is_last, output)?;
301                    } else {
302                        #[cfg(feature = "ldm")]
303                        let used_ldm = if let Some(ref mut ldm) = ldm_state {
304                            let mut empty = Vec::new();
305                            ldm.compress_block(
306                                input,
307                                offset,
308                                block_end,
309                                params,
310                                &rep_offsets,
311                                &mut hash_table,
312                                &mut empty,
313                                &mut sequences,
314                            );
315                            true
316                        } else {
317                            false
318                        };
319                        #[cfg(not(feature = "ldm"))]
320                        let used_ldm = false;
321
322                        if !used_ldm {
323                            fast::compress_fast_block(
324                                input,
325                                offset,
326                                block_end,
327                                params,
328                                &rep_offsets,
329                                &mut hash_table,
330                                &mut sequences,
331                            );
332                        }
333                        if params.force_raw_literals {
334                            block_encoder::encode_compressed_block_raw(
335                                block,
336                                &sequences,
337                                &mut rep_offsets,
338                                is_last,
339                                output,
340                                &mut workspace,
341                            )?;
342                        } else {
343                            block_encoder::encode_compressed_block(
344                                block,
345                                &sequences,
346                                &mut rep_offsets,
347                                is_last,
348                                output,
349                                &mut workspace,
350                                strategy::block_policy(params, input.len()),
351                            )?;
352                        }
353                    }
354                    offset = block_end;
355                }
356            }
357            Strategy::DFast => {
358                let short_size = 1usize << params.chain_log;
359                let long_size = 1usize << params.hash_log;
360                let mut hash_short = vec![0u32; short_size];
361                let mut hash_long = vec![0u32; long_size];
362                while offset < input.len() {
363                    let chunk_size = (input.len() - offset).min(MAX_BLOCK_SIZE);
364                    let block_end = offset + chunk_size;
365                    let is_last = block_end >= input.len();
366                    let block = &input[offset..block_end];
367
368                    if skip_match_search(params, input.len(), block) {
369                        block_encoder::encode_raw_block(block, is_last, output)?;
370                    } else {
371                        #[cfg(feature = "ldm")]
372                        let used_ldm = if let Some(ref mut ldm) = ldm_state {
373                            ldm.compress_block(
374                                input,
375                                offset,
376                                block_end,
377                                params,
378                                &rep_offsets,
379                                &mut hash_short,
380                                &mut hash_long,
381                                &mut sequences,
382                            );
383                            true
384                        } else {
385                            false
386                        };
387                        #[cfg(not(feature = "ldm"))]
388                        let used_ldm = false;
389
390                        if !used_ldm {
391                            dfast::compress_dfast_block(
392                                input,
393                                offset,
394                                block_end,
395                                params,
396                                &rep_offsets,
397                                &mut hash_short,
398                                &mut hash_long,
399                                &mut sequences,
400                            );
401                        }
402                        block_encoder::encode_compressed_block(
403                            block,
404                            &sequences,
405                            &mut rep_offsets,
406                            is_last,
407                            output,
408                            &mut workspace,
409                            strategy::block_policy(params, input.len()),
410                        )?;
411                    }
412                    offset = block_end;
413                }
414            }
415        }
416    }
417
418    let hash = xxh64(input, 0);
419    let checksum = (hash & 0xFFFF_FFFF) as u32;
420    output.extend_from_slice(&checksum.to_le_bytes())?;
421    Ok(())
422}
423
424pub fn compress_with_dict(
425    input: &[u8],
426    level: i32,
427    dict: &zrip_core::dict::Dictionary,
428) -> Result<Vec<u8>, CompressError> {
429    let total_window = dict.content().len() + input.len();
430    let params = strategy::level_params_for_size(level, total_window)
431        .ok_or(CompressError::InvalidLevel(level))?;
432
433    let mut output = Vec::with_capacity(input.len() + 32);
434    write_frame_header(&mut output, input.len(), Some(dict.id()), params.window_log)?;
435
436    if input.is_empty() {
437        block_encoder::encode_raw_block(&[], true, &mut output)?;
438    } else {
439        let prefix = dict.content();
440        let mut rep_offsets = *dict.rep_offsets();
441        let mut workspace = block_encoder::BlockEncodeWorkspace::new();
442
443        workspace.prev_ll = dict
444            .ll_table()
445            .map(|(dt, al)| block_encoder::FseEncodeTable::from_decode_table(dt, al, 35));
446        workspace.prev_of = dict
447            .of_table()
448            .map(|(dt, al)| block_encoder::FseEncodeTable::from_decode_table(dt, al, 31));
449        workspace.prev_ml = dict
450            .ml_table()
451            .map(|(dt, al)| block_encoder::FseEncodeTable::from_decode_table(dt, al, 52));
452        workspace.prev_huffman = dict.huf_table().and_then(|(dt, tl)| {
453            zrip_core::huffman::encode::HuffmanEncodeTable::from_decode_table(dt, tl)
454        });
455
456        if input.len() <= MAX_BLOCK_SIZE {
457            let sequences = match params.strategy {
458                Strategy::Fast => {
459                    fast::compress_fast_with_prefix(input, &params, &rep_offsets, prefix)
460                }
461                Strategy::DFast => {
462                    dfast::compress_dfast_with_prefix(input, &params, &rep_offsets, prefix)
463                }
464            };
465            if params.force_raw_literals {
466                block_encoder::encode_compressed_block_raw(
467                    input,
468                    &sequences,
469                    &mut rep_offsets,
470                    true,
471                    &mut output,
472                    &mut workspace,
473                )?;
474            } else {
475                block_encoder::encode_compressed_block(
476                    input,
477                    &sequences,
478                    &mut rep_offsets,
479                    true,
480                    &mut output,
481                    &mut workspace,
482                    strategy::block_policy(&params, input.len()),
483                )?;
484            }
485        } else {
486            let mut combined = Vec::with_capacity(prefix.len() + input.len());
487            combined.extend_from_slice(prefix);
488            combined.extend_from_slice(input);
489            let plen = prefix.len();
490            let hash_size = 1usize << params.hash_log;
491            let mut sequences = Vec::new();
492
493            match params.strategy {
494                Strategy::Fast => {
495                    let mut hash_table = vec![0u32; hash_size];
496                    fast::prefill_hash_table(&combined, plen, params.hash_log, &mut hash_table);
497                    let mut offset = 0;
498                    while offset < input.len() {
499                        let chunk_size = (input.len() - offset).min(MAX_BLOCK_SIZE);
500                        let is_last = offset + chunk_size >= input.len();
501                        fast::compress_fast_block(
502                            &combined,
503                            plen + offset,
504                            plen + offset + chunk_size,
505                            &params,
506                            &rep_offsets,
507                            &mut hash_table,
508                            &mut sequences,
509                        );
510                        if params.force_raw_literals {
511                            block_encoder::encode_compressed_block_raw(
512                                &input[offset..offset + chunk_size],
513                                &sequences,
514                                &mut rep_offsets,
515                                is_last,
516                                &mut output,
517                                &mut workspace,
518                            )?;
519                        } else {
520                            block_encoder::encode_compressed_block(
521                                &input[offset..offset + chunk_size],
522                                &sequences,
523                                &mut rep_offsets,
524                                is_last,
525                                &mut output,
526                                &mut workspace,
527                                strategy::block_policy(&params, input.len()),
528                            )?;
529                        }
530                        offset += chunk_size;
531                    }
532                }
533                Strategy::DFast => {
534                    let short_size = 1usize << params.chain_log;
535                    let long_size = 1usize << params.hash_log;
536                    let mut hash_short = vec![0u32; short_size];
537                    let mut hash_long = vec![0u32; long_size];
538                    dfast::prefill_hash_tables(
539                        &combined,
540                        plen,
541                        params.hash_log,
542                        params.chain_log,
543                        params.min_match,
544                        &mut hash_short,
545                        &mut hash_long,
546                    );
547                    let mut offset = 0;
548                    while offset < input.len() {
549                        let chunk_size = (input.len() - offset).min(MAX_BLOCK_SIZE);
550                        let is_last = offset + chunk_size >= input.len();
551                        dfast::compress_dfast_block(
552                            &combined,
553                            plen + offset,
554                            plen + offset + chunk_size,
555                            &params,
556                            &rep_offsets,
557                            &mut hash_short,
558                            &mut hash_long,
559                            &mut sequences,
560                        );
561                        block_encoder::encode_compressed_block(
562                            &input[offset..offset + chunk_size],
563                            &sequences,
564                            &mut rep_offsets,
565                            is_last,
566                            &mut output,
567                            &mut workspace,
568                            strategy::block_policy(&params, input.len()),
569                        )?;
570                        offset += chunk_size;
571                    }
572                }
573            }
574        }
575    }
576
577    let hash = xxh64(input, 0);
578    let checksum = (hash & 0xFFFF_FFFF) as u32;
579    output.extend_from_slice(&checksum.to_le_bytes());
580
581    Ok(output)
582}
583
584pub fn compress_into(input: &[u8], output: &mut [u8], level: i32) -> Result<usize, CompressError> {
585    let params = strategy::level_params_for_size(level, input.len())
586        .ok_or(CompressError::InvalidLevel(level))?;
587    let mut sink = SliceSink::new(output);
588    compress_frame(input, &params, &mut sink)?;
589    Ok(sink.pos())
590}
591
592#[cfg(test)]
593mod tests {
594    use super::*;
595    use zrip_core::frame::header::parse_frame_header;
596
597    #[test]
598    fn clamp_params_normalizes_public_log_values() {
599        let mut params = strategy::level_params(1).unwrap();
600        params.hash_log = 0;
601        params.chain_log = 40;
602        params.window_log = 40;
603
604        clamp_params_to_src_size(&mut params, usize::MAX);
605
606        assert_eq!(params.hash_log, strategy::HASH_LOG_MIN);
607        assert_eq!(params.chain_log, strategy::HASH_LOG_MAX);
608        assert_eq!(params.window_log, strategy::WINDOW_LOG_MAX);
609    }
610
611    #[test]
612    fn options_clamp_window_log_before_ldm_defaults() {
613        let mut params = strategy::level_params(1).unwrap();
614        let opts = strategy::Options::default().window_log(0);
615
616        strategy::apply_options(&mut params, &opts);
617
618        assert_eq!(params.window_log, strategy::WINDOW_LOG_MIN);
619        #[cfg(feature = "ldm")]
620        {
621            let mut params = strategy::level_params(1).unwrap();
622            let opts = strategy::Options::default().window_log(0).ldm(true);
623            strategy::apply_options(&mut params, &opts);
624
625            let ldm = params.ldm_params.unwrap();
626            assert!(ldm.hash_log >= ldm.bucket_size_log);
627        }
628    }
629
630    /// Bytes from a skewed alphabet: about 3.5 bits of order-0 entropy per
631    /// byte and few repeated 6-byte strings.
632    fn skewed_bytes(len: usize) -> Vec<u8> {
633        let mut x = 0x2545_F491_4F6C_DD1Du64;
634        (0..len)
635            .map(|_| {
636                x ^= x << 13;
637                x ^= x >> 7;
638                x ^= x << 17;
639                let r = (x >> 32) as u32;
640                b"eeeeeeettttaaoonnissshhrdl  "[(r % 28) as usize] + (r >> 30) as u8
641            })
642            .collect()
643    }
644
645    /// Words from a 64-word vocabulary. Repeats are short and sparse, so a
646    /// sparse match search misses most of them.
647    fn wordy_bytes(len: usize) -> Vec<u8> {
648        const WORDS: &str = "the of and to in is was for on that with as by at \
649            from his her they this have had were which their are but not one \
650            all been when there she would what so if will more no out up into \
651            could them than then some other time very about only upon over \
652            such said great before after little";
653        let words: Vec<&str> = WORDS.split_whitespace().collect();
654        let mut x = 0x9E37_79B9_7F4A_7C15u64;
655        let mut out = Vec::with_capacity(len + 16);
656        while out.len() < len {
657            x ^= x << 13;
658            x ^= x >> 7;
659            x ^= x << 17;
660            out.extend_from_slice(words[(x >> 58) as usize % words.len()].as_bytes());
661            out.push(b' ');
662        }
663        out.truncate(len);
664        out
665    }
666
667    #[test]
668    fn negative_levels_skip_huffman_on_tiny_inputs() {
669        // No usable matches: only Huffman could shrink these bytes.
670        let data = skewed_bytes(2048);
671        for level in -7..=-1 {
672            let out = compress(&data, level).unwrap();
673            assert!(out.len() > data.len(), "L{level}: {} compressed", out.len());
674        }
675        for level in 1..=4 {
676            let out = compress(&data, level).unwrap();
677            assert!(out.len() * 5 < data.len() * 4, "L{level}: {}", out.len());
678        }
679    }
680
681    #[test]
682    fn negative_levels_still_match_tiny_inputs() {
683        let data = wordy_bytes(2048);
684        let sizes: Vec<usize> = (-7..=-1)
685            .map(|level| compress(&data, level).unwrap().len())
686            .collect();
687        // L-6 to L-1 at least 10% smaller; L-7 takes only matches of eight
688        // bytes or more, which these short words seldom repeat. No smaller
689        // output at a faster level.
690        assert!(
691            sizes[1..].iter().all(|&n| n * 11 < data.len() * 10),
692            "{sizes:?}"
693        );
694        assert!(sizes.windows(2).all(|w| w[0] >= w[1]), "{sizes:?}");
695    }
696
697    #[test]
698    fn negative_levels_huffman_code_above_their_raw_limit() {
699        let data = skewed_bytes(32 * 1024);
700        for level in -6..=-1 {
701            let out = compress(&data, level).unwrap();
702            assert!(out.len() * 5 < data.len() * 4, "L{level}: {}", out.len());
703        }
704        // L-7 keeps literals raw at every size.
705        let out = compress(&data, -7).unwrap();
706        assert!(out.len() > data.len(), "L-7: {}", out.len());
707    }
708
709    #[test]
710    fn level_minus_8_runs_as_level_minus_7() {
711        let data = wordy_bytes(64 * 1024);
712        assert_eq!(compress(&data, -8).unwrap(), compress(&data, -7).unwrap());
713    }
714
715    #[test]
716    fn faster_levels_keep_raw_literals_longer() {
717        let limits: Vec<usize> = (-6..=-1)
718            .map(|level| strategy::raw_literals_limit(level).unwrap())
719            .collect();
720        assert_eq!(limits.last(), Some(&2048));
721        assert!(limits.windows(2).all(|w| w[0] > w[1]), "{limits:?}");
722        assert_eq!(strategy::raw_literals_limit(-7), None);
723        assert_eq!(strategy::raw_literals_limit(1), None);
724    }
725
726    #[test]
727    fn small_plain_frame_uses_single_segment_header() {
728        let mut output = Vec::new();
729
730        write_frame_header(&mut output, 12, None, 19).unwrap();
731        let header = parse_frame_header(&output).unwrap();
732
733        assert!(header.single_segment);
734        assert_eq!(header.frame_content_size, Some(12));
735        assert_eq!(header.window_size, 12);
736        assert_eq!(header.dict_id, None);
737        assert!(header.content_checksum);
738        assert_eq!(header.header_size, 6);
739    }
740
741    #[test]
742    fn large_plain_frame_uses_bounded_window_descriptor() {
743        let mut output = Vec::new();
744        let content_size = MAX_WINDOW_SIZE as usize + 1;
745
746        write_frame_header(&mut output, content_size, None, 19).unwrap();
747        let header = parse_frame_header(&output).unwrap();
748
749        assert!(!header.single_segment);
750        assert_eq!(header.frame_content_size, Some(content_size as u64));
751        assert_eq!(header.window_size, 1 << 19);
752        assert_eq!(header.dict_id, None);
753        assert!(header.content_checksum);
754        assert_eq!(header.header_size, 10);
755    }
756
757    #[test]
758    fn dict_frame_uses_window_descriptor_even_when_small() {
759        let mut output = Vec::new();
760
761        write_frame_header(&mut output, 12, Some(0x1234), 10).unwrap();
762        let header = parse_frame_header(&output).unwrap();
763
764        assert!(!header.single_segment);
765        assert_eq!(header.frame_content_size, Some(12));
766        assert_eq!(header.window_size, 1 << 10);
767        assert_eq!(header.dict_id, Some(0x1234));
768        assert!(header.content_checksum);
769        assert_eq!(header.header_size, 12);
770    }
771
772    #[test]
773    fn no_fcs_frame_uses_window_descriptor() {
774        let mut output = Vec::new();
775
776        write_frame_header_without_content_size(&mut output, None, 19).unwrap();
777        let header = parse_frame_header(&output).unwrap();
778
779        assert!(!header.single_segment);
780        assert_eq!(header.frame_content_size, None);
781        assert_eq!(header.window_size, 1 << 19);
782        assert_eq!(header.dict_id, None);
783        assert!(header.content_checksum);
784        assert_eq!(header.header_size, 6);
785    }
786}