#![allow(dead_code)]
pub(crate) mod bitwriter;
#[cfg(all(target_arch = "x86_64", not(feature = "portable")))]
pub(crate) mod block_asm;
pub(crate) mod cdict;
pub(crate) mod compress;
pub(crate) mod dfast;
pub(crate) mod fast;
pub(crate) mod fse_enc;
pub(crate) mod hist;
pub(crate) mod huf_enc;
pub(crate) mod lazy;
pub(crate) mod ldm;
pub(crate) mod literals;
#[cfg(feature = "std")]
pub(crate) mod mt;
pub(crate) mod opt;
pub(crate) mod params;
pub(crate) mod presplit;
pub(crate) mod row;
#[cfg(all(target_arch = "x86_64", not(feature = "portable")))]
pub(crate) mod seq_asm;
pub(crate) mod seq_store;
pub(crate) mod sequences;
pub(crate) mod split;
pub(crate) mod stream;
pub(crate) mod table;
pub use cdict::EncoderDictionary;
use compress::FrameParams;
pub use compress::compress_bound;
pub use stream::{CompressProgress, EndOp, StreamEncoder};
pub fn compress(src: &[u8], level: i32) -> crate::error::Result<alloc::vec::Vec<u8>> {
with_cached(|fc| compress::compress_with(fc, src, level, true, FrameParams::default()))
}
pub fn compress_to_slice(src: &[u8], dst: &mut [u8], level: i32) -> crate::error::Result<usize> {
with_cached(|fc| {
compress::compress_to_slice_with(fc, src, dst, level, true, FrameParams::default())
})
}
pub fn compress_with_dictionary(
src: &[u8],
dict: &[u8],
level: i32,
) -> crate::error::Result<alloc::vec::Vec<u8>> {
let mut slot = cdict::DictSlot::local(dict)?;
with_cached(|fc| {
compress::compress_dict_with(fc, &mut slot, src, level, true, FrameParams::default())
})
}
type Cached = Option<alloc::boxed::Box<compress::FrameCompressor>>;
#[cfg(feature = "std")]
fn with_cached<T>(f: impl FnOnce(&mut Cached) -> T) -> T {
use core::cell::Cell;
std::thread_local! {
static CACHED: Cell<Cached> = const { Cell::new(None) };
}
let mut c = CACHED.with(Cell::take);
let r = f(&mut c);
CACHED.with(|cell| cell.set(c));
r
}
#[cfg(not(feature = "std"))]
fn with_cached<T>(f: impl FnOnce(&mut Cached) -> T) -> T {
f(&mut None)
}
pub struct Compressor {
level: i32,
inner: Cached,
dict: cdict::DictSlot,
fparams: FrameParams,
#[cfg(feature = "std")]
mt_params: mt::MtParams,
#[cfg(feature = "std")]
mt: Option<alloc::boxed::Box<mt::MtCtx>>,
}
impl core::fmt::Debug for Compressor {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let mut d = f.debug_struct("Compressor");
d.field("level", &self.level)
.field("dict", &self.dict)
.field("checksum", &self.fparams.checksum);
#[cfg(feature = "std")]
d.field("workers", &self.mt_params.workers);
d.finish_non_exhaustive()
}
}
impl Compressor {
pub fn new(level: i32) -> Self {
Compressor {
level,
inner: None,
dict: cdict::DictSlot::None,
fparams: FrameParams::default(),
#[cfg(feature = "std")]
mt_params: mt::MtParams::default(),
#[cfg(feature = "std")]
mt: None,
}
}
pub fn set_checksum(&mut self, on: bool) {
self.fparams.checksum = on;
}
pub fn set_content_size_flag(&mut self, on: bool) {
self.fparams.content_size = on;
}
pub fn set_dict_id_flag(&mut self, on: bool) {
self.fparams.dict_id = on;
}
#[cfg(feature = "std")]
pub fn set_workers(&mut self, n: u32) {
self.mt_params.set_workers(n);
}
#[cfg(feature = "std")]
pub fn workers(&self) -> u32 {
self.mt_params.workers
}
#[cfg(feature = "std")]
#[doc(hidden)]
pub fn set_job_size(&mut self, size: usize) {
self.mt_params.set_job_size(size);
}
#[cfg(feature = "std")]
#[doc(hidden)]
pub fn set_overlap_log(&mut self, log: u32) {
self.mt_params.set_overlap_log(log);
}
#[cfg(feature = "std")]
fn compress_mt(
&mut self,
src: &[u8],
dst: Option<&mut [u8]>,
) -> Option<crate::error::Result<(alloc::vec::Vec<u8>, usize)>> {
let pledged = Some(src.len() as u64);
if !self.mt_params.applies(pledged) {
return None;
}
Some((|| {
let (cd, level) = self.dict.for_frame(self.level)?;
let cp = cdict::frame_cparams(level, cd.as_deref(), pledged, 0, false);
let ldm = params::ldm_enabled(&cp);
let m = self
.mt
.get_or_insert_with(|| alloc::boxed::Box::new(mt::MtCtx::new()));
m.init_frame(&self.mt_params, cp, ldm, cd, pledged, self.fparams)?;
match dst {
Some(d) => {
let p = m.compress(src, d, EndOp::End)?;
if p.remaining != 0 {
m.abandon();
return Err(crate::Error::OutputTooSmall);
}
Ok((alloc::vec::Vec::new(), p.written))
}
None => {
let mut v = alloc::vec::Vec::with_capacity(compress_bound(src.len()));
m.compress_end_to_vec(src, &mut v)?;
let n = v.len();
Ok((v, n))
}
}
})())
}
pub fn with_dictionary(level: i32, dict: &[u8]) -> crate::error::Result<Self> {
let mut c = Self::new(level);
c.set_dictionary(dict)?;
Ok(c)
}
pub fn with_prepared_dictionary(dict: &EncoderDictionary) -> Self {
let mut c = Self::new(dict.level());
c.set_prepared_dictionary(dict);
c
}
pub fn set_level(&mut self, level: i32) {
self.level = level;
}
pub fn set_dictionary(&mut self, dict: &[u8]) -> crate::error::Result<()> {
self.dict = cdict::DictSlot::None;
self.dict = cdict::DictSlot::local(dict)?;
Ok(())
}
pub fn set_prepared_dictionary(&mut self, dict: &EncoderDictionary) {
self.dict = cdict::DictSlot::Prepared(dict.inner.clone());
}
pub fn clear_dictionary(&mut self) {
self.dict = cdict::DictSlot::None;
}
pub fn compress(&mut self, src: &[u8]) -> crate::error::Result<alloc::vec::Vec<u8>> {
#[cfg(feature = "std")]
if let Some(r) = self.compress_mt(src, None) {
return r.map(|(v, _)| v);
}
if !matches!(self.dict, cdict::DictSlot::None) {
return compress::compress_dict_with(
&mut self.inner,
&mut self.dict,
src,
self.level,
false,
self.fparams,
);
}
compress::compress_with(&mut self.inner, src, self.level, false, self.fparams)
}
pub fn compress_to_slice(&mut self, src: &[u8], dst: &mut [u8]) -> crate::error::Result<usize> {
#[cfg(feature = "std")]
if let Some(r) = self.compress_mt(src, Some(&mut *dst)) {
return r.map(|(_, n)| n);
}
if !matches!(self.dict, cdict::DictSlot::None) {
return compress::compress_dict_to_slice_with(
&mut self.inner,
&mut self.dict,
src,
dst,
self.level,
false,
self.fparams,
);
}
compress::compress_to_slice_with(&mut self.inner, src, dst, self.level, false, self.fparams)
}
}