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