use bytes::Bytes;
use super::{BlockCodec, Codec, Decoded};
use crate::budget::{DecodeBuf, Reservation};
use crate::error::{Error, LimitKind};
use crate::limits::{ChargeError, heap_cost};
#[derive(Clone, Copy, Debug, Default)]
pub struct Lzma;
const _: () = crate::send_sync::<Lzma>();
const LZMA_FIRST_DICT_MAX: u64 = 8 << 20;
const LZMA_DICT_SIZE_MIN: u32 = 4096;
const LZ_DICT_REPEAT_ROOM: u64 = 2 * 288;
impl Lzma {
pub const DECODER_STATE: u64 = 32_784 + 256;
#[must_use]
pub const fn decoder_charge(dict: u32) -> u64 {
let dict = if dict < LZMA_DICT_SIZE_MIN {
LZMA_DICT_SIZE_MIN
} else {
dict
};
let buffer = (dict as u64).next_multiple_of(16) + LZ_DICT_REPEAT_ROOM;
heap_cost(buffer) + Self::DECODER_STATE
}
}
impl BlockCodec for Lzma {
fn decode(
&self,
input: &Bytes,
uncompressed: u32,
out: &mut DecodeBuf,
) -> Result<Decoded, Error> {
let (Some(&props), Some(dict)) = (input.first(), input.get(1..5)) else {
return Err(lzma_error(&format!(
"struct.error: unpack requires a buffer of 5 bytes (got {})",
input.len()
)));
};
let named = u32::from_le_bytes(<[u8; 4]>::try_from(dict).unwrap_or_default());
let data = input.get(5..).unwrap_or_default();
let lc = u32::from(props % 9);
let rest = u32::from(props / 9);
let (pb, lp) = (rest / 5, rest % 5);
let first =
u64::from(uncompressed).clamp(u64::from(LZMA_DICT_SIZE_MIN), LZMA_FIRST_DICT_MAX);
let mut dict = u32::try_from(first).unwrap_or(u32::MAX).min(named);
let start = out.len();
loop {
match lzma_attempt([lc, lp, pb], dict, data, uncompressed, out) {
Err(LzmaFail::Data(produced))
if dict < named
&& u64::try_from(produced).unwrap_or(u64::MAX) >= u64::from(dict) =>
{
out.truncate(start);
let wider = u64::try_from(produced)
.unwrap_or(u64::MAX)
.saturating_mul(2)
.max(u64::from(dict).saturating_mul(2));
dict = u32::try_from(wider).unwrap_or(u32::MAX).min(named);
}
Err(LzmaFail::Data(_)) => return Err(lzma_error("LZMAError: Corrupt input data")),
Err(LzmaFail::Other(e)) => return Err(e),
Ok(()) => return Ok(Decoded::Written),
}
}
}
}
fn lzma_error(detail: &str) -> Error {
Error::Decompress {
codec: Codec::Lzma,
detail: detail.into(),
}
}
enum LzmaFail {
Data(usize),
Other(Error),
}
impl From<ChargeError> for LzmaFail {
fn from(e: ChargeError) -> Self {
Self::Other(e.into())
}
}
fn failure(e: liblzma::stream::Error, produced: usize, held: u64, what: Reservation) -> LzmaFail {
let other = |detail: &str| LzmaFail::Other(lzma_error(detail));
match e {
liblzma::stream::Error::Data => LzmaFail::Data(produced),
liblzma::stream::Error::Options => other("LZMAError: Invalid or unsupported options"),
liblzma::stream::Error::Mem => {
crate::counters::allocator(what);
LzmaFail::Other(Error::Budget(crate::limits::refusal(held, what)))
}
_ => other("LZMAError: Internal error"),
}
}
const LZMA_WINDOW_MAX: usize = 1 << 20;
fn lzma_attempt(
[lc, lp, pb]: [u32; 3],
dict: u32,
data: &[u8],
declared: u32,
out: &mut DecodeBuf,
) -> Result<(), LzmaFail> {
use liblzma::stream::{Action, Filters, LzmaOptions, Status, Stream};
let other = |what: &str| LzmaFail::Other(lzma_error(what));
let held = Lzma::decoder_charge(dict);
let _decoder = out.meter().charge(held)?;
let what = out.meter().reservation();
let map = |e: liblzma::stream::Error, produced: usize| failure(e, produced, held, what);
let mut opts = LzmaOptions::new_preset(6).map_err(|e| map(e, 0))?;
opts.dict_size(dict)
.literal_context_bits(lc)
.literal_position_bits(lp)
.position_bits(pb);
let mut filters = Filters::new();
filters.lzma1(&opts);
let mut stream = Stream::new_raw_decoder(&filters).map_err(|e| map(e, 0))?;
let seg_start = out.len();
let declared_len = usize::try_from(declared).unwrap_or(usize::MAX);
let mut want = match declared_len {
0 => LZMA_WINDOW_MAX,
n => n.min(LZMA_WINDOW_MAX),
};
let input_done =
|stream: &Stream| usize::try_from(stream.total_in()).unwrap_or(usize::MAX) >= data.len();
let rest = |stream: &Stream| {
let consumed = usize::try_from(stream.total_in()).unwrap_or(usize::MAX);
data.get(consumed..).unwrap_or_default()
};
let mut full = false;
loop {
let produced = out.len() - seg_start;
if full {
let mut probe = [0u8; 1];
let before = stream.total_out();
let status = stream
.process(rest(&stream), &mut probe, Action::Run)
.map_err(|e| map(e, produced))?;
if stream.total_out() > before {
out.extend_from_slice(&probe)?;
}
match status {
Status::StreamEnd => return Ok(()),
Status::MemNeeded if !input_done(&stream) => {
return Err(other("LZMAError: Insufficient buffer space"));
}
_ => {}
}
if stream.total_out() == before && input_done(&stream) {
return Ok(());
}
full = false;
continue;
}
let left = declared_len.saturating_sub(produced);
let ask = if left > 0 { want.min(left) } else { want };
let base = out.len();
let window = match out.grow_zeroed(ask) {
Ok(w) => w,
Err(e) if ask > 1 => {
want = refused_window(&e, ask, produced);
continue;
}
Err(_) => {
full = true;
continue;
}
};
let room = window.len();
let before = stream.total_out();
let status = stream.process(rest(&stream), window, Action::Run);
let made = usize::try_from(stream.total_out() - before).unwrap_or(room);
out.truncate(base + made);
let status = status.map_err(|e| map(e, produced + made))?;
match status {
Status::StreamEnd => return Ok(()),
Status::MemNeeded if !input_done(&stream) && made < room => {
return Err(other("LZMAError: Insufficient buffer space"));
}
_ => {}
}
if made == room {
full = true;
want = want.saturating_mul(2).min(LZMA_WINDOW_MAX);
} else if input_done(&stream) {
return Ok(());
}
}
}
fn refused_window(e: &ChargeError, want: usize, produced: usize) -> usize {
let half = (want / 2).max(1);
match e {
ChargeError::Limit(l) if l.kind() == LimitKind::BlockOutput => {
let room = usize::try_from(l.max())
.unwrap_or(usize::MAX)
.saturating_sub(produced);
room.clamp(1, half)
}
_ => half,
}
}
#[cfg(test)]
mod tests {
use super::{LzmaFail, failure};
use crate::budget::{BudgetExceeded, Reservation};
use crate::error::Error;
use crate::once::CellError;
#[test]
fn liblzmas_memory_error_is_a_refusal_of_memory_not_a_decode_failure() {
for what in [Reservation::Input, Reservation::Provider] {
match failure(liblzma::stream::Error::Mem, 3, 8 << 20, what) {
LzmaFail::Other(e @ Error::Budget(_)) => {
assert!(e.is_transient());
let Error::Budget(b) = e else { unreachable!() };
assert_eq!(
b,
BudgetExceeded {
requested: 8 << 20,
what
}
);
}
LzmaFail::Other(e) => panic!("{what:?}: {e:?}"),
LzmaFail::Data(n) => panic!("{what:?}: a data error after {n} bytes"),
}
}
assert!(matches!(
failure(liblzma::stream::Error::Data, 3, 4096, Reservation::Input),
LzmaFail::Data(3)
));
}
#[test]
fn liblzmas_memory_error_on_a_dependency_counts_as_a_pool_refusal() {
let before = crate::limits::pool_refusals();
let fail = failure(
liblzma::stream::Error::Mem,
3,
8 << 20,
Reservation::Dependency,
);
assert!(
matches!(&fail, LzmaFail::Other(Error::Budget(b)) if b.what == Reservation::Dependency)
);
assert_eq!(crate::limits::pool_refusals() - before, 1);
}
}