Skip to main content

compression/
compression_encode.rs

1use crate::{ffi, Algorithm, CompressionError, Encoder, Result};
2
3fn encode_len(dst: &mut [u8], src: &[u8], algorithm: Algorithm) -> usize {
4    unsafe {
5        ffi::compression_encode::compression_rs_compression_encode_buffer(
6            dst.as_mut_ptr(),
7            dst.len(),
8            src.as_ptr(),
9            src.len(),
10            algorithm.as_raw(),
11        )
12    }
13}
14
15pub fn compression_encode_scratch_buffer_size(algorithm: Algorithm) -> usize {
16    unsafe {
17        ffi::compression_encode::compression_rs_compression_encode_scratch_buffer_size(
18            algorithm.as_raw(),
19        )
20    }
21}
22
23pub fn compression_encode_buffer(
24    dst: &mut [u8],
25    src: &[u8],
26    algorithm: Algorithm,
27) -> Result<usize> {
28    let written = encode_len(dst, src, algorithm);
29    if src.is_empty() || written > 0 {
30        Ok(written)
31    } else {
32        Err(CompressionError::BufferOperationFailed {
33            operation: "compression_encode_buffer",
34            algorithm,
35            input_len: src.len(),
36            output_capacity: dst.len(),
37        })
38    }
39}
40
41pub fn compress(input: &[u8], algorithm: Algorithm) -> Result<Vec<u8>> {
42    if input.is_empty() {
43        return Ok(Vec::new());
44    }
45
46    if algorithm.supports_streams() {
47        let mut encoder = Encoder::new(algorithm)?;
48        let mut output = encoder.process(input)?;
49        output.extend(encoder.finish()?);
50        return Ok(output);
51    }
52
53    let max_capacity = input
54        .len()
55        .saturating_mul(8)
56        .saturating_add(1 << 20)
57        .max(64);
58    let mut capacity = input
59        .len()
60        .saturating_add(input.len() / 8)
61        .saturating_add(64)
62        .max(64);
63
64    loop {
65        let mut output = vec![0_u8; capacity];
66        let written = encode_len(&mut output, input, algorithm);
67        if written > 0 {
68            output.truncate(written);
69            return Ok(output);
70        }
71        if capacity >= max_capacity {
72            return Err(CompressionError::BufferOperationFailed {
73                operation: "compression_encode_buffer",
74                algorithm,
75                input_len: input.len(),
76                output_capacity: capacity,
77            });
78        }
79        capacity = capacity.saturating_mul(2).min(max_capacity);
80    }
81}