#![forbid(unsafe_code)]
use crate::core::candidate::{Candidate, CandidateContext, Encoder, ObjectRecord};
use crate::core::cost::ByteSplit;
use crate::core::representation::{RansCodec, Representation};
use crate::rans::metadata;
use crate::rans::model::{RansModel, normalize_histogram};
use crate::rans::residual::encode_stream;
pub const MIN_MATCH: usize = 4;
pub const SRC_LOCAL: u8 = 0x00;
pub const SRC_DICT: u8 = 0x01;
pub const SRC_SHARED: u8 = 0x02;
pub const MAX_DICT: usize = 65536;
const DICT_CHAIN_DEPTH: usize = 8;
pub const MAX_COPY: usize = 131;
pub const MAX_LIT_RUN: usize = 128;
pub const MAX_DIST: usize = 65535;
const CHAIN_DEPTH: usize = 16;
const SCALE_BITS: u8 = 14;
const CODEC: RansCodec = RansCodec::Interleaved2;
const SLOT_RANS: u8 = 0x00;
const SLOT_RAW: u8 = 0x01;
const SLOT_EMPTY: u8 = 0x02;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SequenceStreams {
pub commands: Vec<u8>,
pub literals: Vec<u8>,
pub offsets: Vec<u8>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EncodedSequence {
pub model_obj: Vec<u8>,
pub enc_obj: Vec<u8>,
pub seq_len: u32,
pub lit_len: u32,
pub off_len: u32,
pub cmds: u32,
pub lit_out: u32,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EncodedStreams {
pub model_obj: Vec<u8>,
pub enc_obj: Vec<u8>,
pub lens: Vec<u32>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SequenceError {
TooLarge {
len: u64,
max: u64,
},
Truncated,
UnknownKind(u8),
Malformed,
TrailingBytes,
Rans(String),
Stream(String),
NoCommands,
}
impl std::fmt::Display for SequenceError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{self:?}")
}
}
impl std::error::Error for SequenceError {}
pub fn encode_sequence(input: &[u8]) -> SequenceStreams {
let n = input.len();
let mut commands = Vec::new();
let mut literals = Vec::new();
let mut offsets = Vec::new();
if n == 0 {
return SequenceStreams {
commands,
literals,
offsets,
};
}
let hsize = 1usize << 16;
let mut head = vec![u32::MAX; hsize];
let mut chain = vec![u32::MAX; n];
let mut pos = 0usize;
while pos < n {
if pos + MIN_MATCH <= n {
if let Some((dist, len)) = find_match(input, pos, &head, &chain) {
let mut len = len;
let rem = len % MAX_COPY;
if rem > 0 && rem < MIN_MATCH {
len -= rem;
}
let mut remaining = len;
while remaining > 0 {
let take = remaining.min(MAX_COPY);
debug_assert!((MIN_MATCH..=MAX_COPY).contains(&take));
commands.push((0x80 + take - MIN_MATCH) as u8);
offsets.extend_from_slice(&(dist as u16).to_le_bytes());
remaining -= take;
}
let end = pos + len;
while pos < end {
if pos + MIN_MATCH <= n {
let h = hash_at(input, pos);
chain[pos] = head[h];
head[h] = pos as u32;
}
pos += 1;
}
continue;
}
}
let start = pos;
let mut run = 0usize;
while pos < n && run < MAX_LIT_RUN {
let has_match = pos + MIN_MATCH <= n && find_match(input, pos, &head, &chain).is_some();
if has_match {
break;
}
if pos + MIN_MATCH <= n {
let h = hash_at(input, pos);
chain[pos] = head[h];
head[h] = pos as u32;
}
pos += 1;
run += 1;
}
if run > 0 {
commands.push((run - 1) as u8);
literals.extend_from_slice(&input[start..pos]);
}
}
SequenceStreams {
commands,
literals,
offsets,
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DictStreams {
pub commands: Vec<u8>,
pub literals: Vec<u8>,
pub offsets: Vec<u8>,
pub sources: Vec<u8>,
}
pub fn encode_sequence_dict(input: &[u8], dict: &[u8]) -> Option<DictStreams> {
let n = input.len();
if n == 0 || dict.is_empty() || dict.len() > MAX_DICT {
return None;
}
let mut commands = Vec::new();
let mut literals = Vec::new();
let mut offsets = Vec::new();
let mut sources = Vec::new();
let hsize = 1usize << 16;
let mut head = vec![u32::MAX; hsize];
let mut chain = vec![u32::MAX; n];
let mut d_head = vec![u32::MAX; hsize];
let mut d_chain = vec![u32::MAX; dict.len()];
let dict_limit = dict.len().saturating_sub(MIN_MATCH - 1);
for (p, slot) in d_chain.iter_mut().enumerate().take(dict_limit) {
let h = hash_at(dict, p);
*slot = d_head[h];
d_head[h] = p as u32;
}
let mut pos = 0usize;
while pos < n {
if pos + MIN_MATCH <= n {
if let Some((dist, len, source)) =
best_match(input, pos, dict, &head, &chain, &d_head, &d_chain)
{
let mut len = len;
let rem = len % MAX_COPY;
if rem > 0 && rem < MIN_MATCH {
len -= rem;
}
let mut remaining = len;
let mut cur_off = dist;
while remaining > 0 {
let take = remaining.min(MAX_COPY);
debug_assert!((MIN_MATCH..=MAX_COPY).contains(&take));
commands.push((0x80 + take - MIN_MATCH) as u8);
offsets.extend_from_slice(&(cur_off as u16).to_le_bytes());
sources.push(source);
if source == SRC_DICT {
cur_off = cur_off.saturating_add(take);
}
remaining -= take;
}
let end = pos + len;
while pos < end {
if pos + MIN_MATCH <= n {
let h = hash_at(input, pos);
chain[pos] = head[h];
head[h] = pos as u32;
}
pos += 1;
}
continue;
}
}
let start = pos;
let mut run = 0usize;
while pos < n && run < MAX_LIT_RUN {
let has_match = pos + MIN_MATCH <= n
&& best_match(input, pos, dict, &head, &chain, &d_head, &d_chain).is_some();
if has_match {
break;
}
if pos + MIN_MATCH <= n {
let h = hash_at(input, pos);
chain[pos] = head[h];
head[h] = pos as u32;
}
pos += 1;
run += 1;
}
if run > 0 {
commands.push((run - 1) as u8);
literals.extend_from_slice(&input[start..pos]);
}
}
Some(DictStreams {
commands,
literals,
offsets,
sources,
})
}
pub fn encode_sequence_shared(
input: &[u8],
file_dict: &[u8],
shared: &[u8],
) -> Option<DictStreams> {
let n = input.len();
if n == 0 || shared.is_empty() || shared.len() > MAX_DICT || file_dict.len() > MAX_DICT {
return None;
}
let mut commands = Vec::new();
let mut literals = Vec::new();
let mut offsets = Vec::new();
let mut sources = Vec::new();
let hsize = 1usize << 16;
let mut head = vec![u32::MAX; hsize];
let mut chain = vec![u32::MAX; n];
let mut f_head = vec![u32::MAX; hsize];
let mut f_chain = vec![u32::MAX; file_dict.len()];
let f_limit = file_dict.len().saturating_sub(MIN_MATCH - 1);
for (p, slot) in f_chain.iter_mut().enumerate().take(f_limit) {
let h = hash_at(file_dict, p);
*slot = f_head[h];
f_head[h] = p as u32;
}
let mut s_head = vec![u32::MAX; hsize];
let mut s_chain = vec![u32::MAX; shared.len()];
let s_limit = shared.len().saturating_sub(MIN_MATCH - 1);
for (p, slot) in s_chain.iter_mut().enumerate().take(s_limit) {
let h = hash_at(shared, p);
*slot = s_head[h];
s_head[h] = p as u32;
}
let mut pos = 0usize;
while pos < n {
if pos + MIN_MATCH <= n {
if let Some((dist, len, source)) = best_match_shared(
input, pos, file_dict, shared, &head, &chain, &f_head, &f_chain, &s_head, &s_chain,
) {
let mut len = len;
let rem = len % MAX_COPY;
if rem > 0 && rem < MIN_MATCH {
len -= rem;
}
let mut remaining = len;
let mut cur_off = dist;
while remaining > 0 {
let take = remaining.min(MAX_COPY);
debug_assert!((MIN_MATCH..=MAX_COPY).contains(&take));
commands.push((0x80 + take - MIN_MATCH) as u8);
offsets.extend_from_slice(&(cur_off as u16).to_le_bytes());
sources.push(source);
if source == SRC_DICT || source == SRC_SHARED {
cur_off = cur_off.saturating_add(take);
}
remaining -= take;
}
let end = pos + len;
while pos < end {
if pos + MIN_MATCH <= n {
let h = hash_at(input, pos);
chain[pos] = head[h];
head[h] = pos as u32;
}
pos += 1;
}
continue;
}
}
let start = pos;
let mut run = 0usize;
while pos < n && run < MAX_LIT_RUN {
let has_match = pos + MIN_MATCH <= n
&& best_match_shared(
input, pos, file_dict, shared, &head, &chain, &f_head, &f_chain, &s_head,
&s_chain,
)
.is_some();
if has_match {
break;
}
if pos + MIN_MATCH <= n {
let h = hash_at(input, pos);
chain[pos] = head[h];
head[h] = pos as u32;
}
pos += 1;
run += 1;
}
if run > 0 {
commands.push((run - 1) as u8);
literals.extend_from_slice(&input[start..pos]);
}
}
Some(DictStreams {
commands,
literals,
offsets,
sources,
})
}
#[allow(clippy::too_many_arguments)]
fn best_match_shared(
input: &[u8],
pos: usize,
file_dict: &[u8],
shared: &[u8],
head: &[u32],
chain: &[u32],
f_head: &[u32],
f_chain: &[u32],
s_head: &[u32],
s_chain: &[u32],
) -> Option<(usize, usize, u8)> {
let local = find_match(input, pos, head, chain);
let f = if file_dict.is_empty() {
None
} else {
find_dict_match(input, pos, file_dict, f_head, f_chain)
};
let s = find_dict_match(input, pos, shared, s_head, s_chain);
let mut best: Option<(usize, usize, u8)> = local.map(|(d, l)| (d, l, SRC_LOCAL));
if let Some((od, ol)) = f {
let better = match best {
Some((_, bl, _)) => ol > bl,
None => true,
};
if better {
best = Some((od, ol, SRC_DICT));
}
}
if let Some((od, ol)) = s {
let better = match best {
Some((_, bl, _)) => ol > bl,
None => true,
};
if better {
best = Some((od, ol, SRC_SHARED));
}
}
best
}
fn best_match(
input: &[u8],
pos: usize,
dict: &[u8],
head: &[u32],
chain: &[u32],
d_head: &[u32],
d_chain: &[u32],
) -> Option<(usize, usize, u8)> {
let local = find_match(input, pos, head, chain);
let dm = find_dict_match(input, pos, dict, d_head, d_chain);
match (local, dm) {
(Some((ld, ll)), Some((dd, dl))) => {
if dl > ll {
Some((dd, dl, SRC_DICT))
} else {
Some((ld, ll, SRC_LOCAL))
}
}
(Some(m), None) => Some((m.0, m.1, SRC_LOCAL)),
(None, Some(m)) => Some((m.0, m.1, SRC_DICT)),
(None, None) => None,
}
}
fn find_dict_match(
input: &[u8],
pos: usize,
dict: &[u8],
d_head: &[u32],
d_chain: &[u32],
) -> Option<(usize, usize)> {
let n = input.len();
let h = hash_at(input, pos);
let max_len = n - pos;
let mut c = d_head[h];
let mut best_len = 0usize;
let mut best_off = 0usize;
let mut depth = 0usize;
while c != u32::MAX && depth < DICT_CHAIN_DEPTH {
let cpos = c as usize;
let avail = dict.len() - cpos;
let limit = max_len.min(avail);
let mut l = 0usize;
while l < limit && dict[cpos + l] == input[pos + l] {
l += 1;
}
if l >= MIN_MATCH && l > best_len {
best_len = l;
best_off = cpos;
if l == max_len {
break; }
}
c = d_chain[cpos];
depth += 1;
}
if best_len >= MIN_MATCH {
Some((best_off, best_len))
} else {
None
}
}
fn find_match(input: &[u8], pos: usize, head: &[u32], chain: &[u32]) -> Option<(usize, usize)> {
let n = input.len();
let h = hash_at(input, pos);
let mut c = head[h];
let max_len = n - pos;
let mut best_len = 0usize;
let mut best_dist = 0usize;
let mut depth = 0usize;
while c != u32::MAX && depth < CHAIN_DEPTH {
let cpos = c as usize;
let dist = pos - cpos;
if dist <= MAX_DIST {
let mut l = 0usize;
while l < max_len && input[cpos + l] == input[pos + l] {
l += 1;
}
if l >= MIN_MATCH && l > best_len {
best_len = l;
best_dist = dist;
if l == max_len {
break;
}
}
}
c = chain[cpos];
depth += 1;
}
if best_len >= MIN_MATCH {
Some((best_dist, best_len))
} else {
None
}
}
pub(crate) fn hash_at(input: &[u8], pos: usize) -> usize {
let h = u32::from_le_bytes(input[pos..pos + 4].try_into().expect("4-byte slice"));
(h.wrapping_mul(0x9E37_79B1) >> 16) as usize
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum StreamSlot {
Rans(Vec<u8>),
Raw,
Empty,
}
pub fn encode_streams(streams: &SequenceStreams) -> Option<EncodedSequence> {
if streams.commands.is_empty() {
return None;
}
let enc = encode_streams_n(&[
streams.commands.clone(),
streams.literals.clone(),
streams.offsets.clone(),
])?;
Some(EncodedSequence {
model_obj: enc.model_obj,
enc_obj: enc.enc_obj,
seq_len: enc.lens[0],
lit_len: enc.lens[1],
off_len: enc.lens[2],
cmds: streams.commands.len() as u32,
lit_out: streams.literals.len() as u32,
})
}
pub fn encode_streams_n(streams: &[Vec<u8>]) -> Option<EncodedStreams> {
if streams.is_empty() {
return None;
}
let mut model_obj = Vec::with_capacity(streams.len() * 3 + streams.len() * 512);
let mut enc_obj = Vec::new();
let mut lens = Vec::with_capacity(streams.len());
for stream in streams {
let (slot, payload) = encode_one_stream(stream)?;
match &slot {
StreamSlot::Rans(model_bytes) => {
model_obj.push(SLOT_RANS);
model_obj.extend_from_slice(&(model_bytes.len() as u16).to_le_bytes());
model_obj.extend_from_slice(model_bytes);
}
StreamSlot::Raw => {
model_obj.push(SLOT_RAW);
model_obj.extend_from_slice(&0u16.to_le_bytes());
}
StreamSlot::Empty => {
model_obj.push(SLOT_EMPTY);
model_obj.extend_from_slice(&0u16.to_le_bytes());
}
}
lens.push(payload.len() as u32);
enc_obj.extend_from_slice(&payload);
}
Some(EncodedStreams {
model_obj,
enc_obj,
lens,
})
}
fn encode_one_stream(stream: &[u8]) -> Option<(StreamSlot, Vec<u8>)> {
let mut hist = [0u32; 256];
for &b in stream {
hist[b as usize] += 1;
}
let distinct = hist.iter().filter(|&&h| h > 0).count();
match distinct {
0 => Some((StreamSlot::Empty, Vec::new())),
1 => Some((StreamSlot::Raw, stream.to_vec())),
_ => {
let model: RansModel = normalize_histogram(&hist, SCALE_BITS, CODEC)?;
match encode_stream(stream, &model) {
Ok(enc) if enc.len() < stream.len() => {
Some((StreamSlot::Rans(metadata::encode_model(&model)), enc))
}
_ => Some((StreamSlot::Raw, stream.to_vec())),
}
}
}
}
pub fn parse_model_object_slots(
bytes: &[u8],
max_bytes: u64,
slots: usize,
) -> Result<Vec<StreamSlot>, SequenceError> {
if slots == 0 || slots > 4 {
return Err(SequenceError::Malformed);
}
if bytes.len() as u64 > max_bytes {
return Err(SequenceError::TooLarge {
len: bytes.len() as u64,
max: max_bytes,
});
}
let mut out = Vec::with_capacity(slots);
let mut pos = 0usize;
for _ in 0..slots {
let kind = *bytes.get(pos).ok_or(SequenceError::Truncated)?;
pos += 1;
let len = u16::from_le_bytes(
bytes
.get(pos..pos + 2)
.ok_or(SequenceError::Truncated)?
.try_into()
.expect("2-byte slice"),
) as usize;
pos += 2;
let slot = match kind {
SLOT_RANS => {
let b = bytes.get(pos..pos + len).ok_or(SequenceError::Truncated)?;
StreamSlot::Rans(b.to_vec())
}
SLOT_RAW => {
if len != 0 {
return Err(SequenceError::Malformed);
}
StreamSlot::Raw
}
SLOT_EMPTY => {
if len != 0 {
return Err(SequenceError::Malformed);
}
StreamSlot::Empty
}
other => return Err(SequenceError::UnknownKind(other)),
};
out.push(slot);
pos += len;
}
if pos != bytes.len() {
return Err(SequenceError::TrailingBytes);
}
Ok(out)
}
pub fn parse_model_object(bytes: &[u8], max_bytes: u64) -> Result<[StreamSlot; 3], SequenceError> {
let v = parse_model_object_slots(bytes, max_bytes, 3)?;
Ok([v[0].clone(), v[1].clone(), v[2].clone()])
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ThreeStreams {
pub seq_len: u32,
pub lit_len: u32,
pub off_len: u32,
pub cmds: u32,
pub lit_out: u32,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DecodedStreams {
pub commands: Vec<u8>,
pub literals: Vec<u8>,
pub offsets: Vec<u8>,
}
#[derive(Debug, Clone, Copy)]
pub struct StreamRefs {
pub model: crate::core::extent::ChunkId,
pub enc_obj: crate::core::extent::ChunkId,
pub scale_bits: u8,
pub codec: RansCodec,
}
#[allow(clippy::too_many_arguments)] pub fn decode_streams_n(
ctx: &dyn crate::core::materialize::DecoderContext,
limits: &crate::core::limits::Limits,
refs: StreamRefs,
encoded_lens: &[u32],
commands_expected: u64,
literals_expected: u64,
copies_override: Option<u64>,
off_per_copy: u32,
) -> Result<Vec<Vec<u8>>, crate::core::materialize::MaterializeError> {
use crate::core::materialize::MaterializeError;
let n = encoded_lens.len();
if !(3..=4).contains(&n) {
return Err(MaterializeError::InvalidDescriptor(
"stream count must be 3 or 4".into(),
));
}
let StreamRefs {
model,
enc_obj,
scale_bits,
codec,
} = refs;
let enc_total: u64 = encoded_lens.iter().map(|&l| l as u64).sum();
if enc_total > limits.max_alloc_bytes {
return Err(MaterializeError::AllocTooLarge {
requested: enc_total,
max: limits.max_alloc_bytes,
});
}
let model_bytes = ctx.fetch_object(&model)?;
let slots = parse_model_object_slots(
&model_bytes,
max_model_object_bytes_n(limits.max_model_bytes, n),
n,
)
.map_err(|e| MaterializeError::Sequence(e.to_string()))?;
let enc = ctx.fetch_object(&enc_obj)?;
if enc.len() as u64 != enc_total {
return Err(MaterializeError::InvalidDescriptor(
"enc object length mismatch".into(),
));
}
let mut slices: Vec<&[u8]> = Vec::with_capacity(n);
let mut p = 0usize;
for &l in encoded_lens {
slices.push(&enc[p..p + l as usize]);
p += l as usize;
}
let commands: Vec<u8> = match &slots[0] {
StreamSlot::Rans(m) => {
ctx.decode_rans(m, slices[0], scale_bits, codec, commands_expected)?
}
StreamSlot::Raw => {
if slices[0].len() as u64 != commands_expected {
return Err(MaterializeError::InvalidDescriptor(
"raw command stream length mismatch".into(),
));
}
slices[0].to_vec()
}
StreamSlot::Empty => {
return Err(MaterializeError::InvalidDescriptor(
"empty command stream".into(),
));
}
};
if commands.len() as u64 != commands_expected {
return Err(MaterializeError::InvalidDescriptor(
"command stream decoded length mismatch".into(),
));
}
let copies = match copies_override {
Some(c) => c,
None => commands.iter().filter(|&&b| b >= 0x80).count() as u64,
};
let off_out = copies.checked_mul(off_per_copy as u64).ok_or_else(|| {
MaterializeError::InvalidDescriptor("offset stream length overflow".into())
})?;
if off_out > limits.max_alloc_bytes {
return Err(MaterializeError::AllocTooLarge {
requested: off_out,
max: limits.max_alloc_bytes,
});
}
let literals: Vec<u8> = match &slots[1] {
StreamSlot::Rans(m) => {
ctx.decode_rans(m, slices[1], scale_bits, codec, literals_expected)?
}
StreamSlot::Raw => {
if slices[1].len() as u64 != literals_expected {
return Err(MaterializeError::InvalidDescriptor(
"raw literal stream length mismatch".into(),
));
}
slices[1].to_vec()
}
StreamSlot::Empty => {
if literals_expected != 0 || slices[1].len() as u64 != 0 {
return Err(MaterializeError::InvalidDescriptor(
"non-empty literal stream without a model".into(),
));
}
Vec::new()
}
};
if literals.len() as u64 != literals_expected {
return Err(MaterializeError::InvalidDescriptor(
"literal stream decoded length mismatch".into(),
));
}
let offsets: Vec<u8> = match &slots[2] {
StreamSlot::Rans(m) => ctx.decode_rans(m, slices[2], scale_bits, codec, off_out)?,
StreamSlot::Raw => {
if slices[2].len() as u64 != off_out {
return Err(MaterializeError::InvalidDescriptor(
"raw offset stream length mismatch".into(),
));
}
slices[2].to_vec()
}
StreamSlot::Empty => {
if off_out != 0 {
return Err(MaterializeError::InvalidDescriptor(
"non-empty offset stream without a model".into(),
));
}
Vec::new()
}
};
if offsets.len() as u64 != off_out {
return Err(MaterializeError::InvalidDescriptor(
"offset stream decoded length mismatch".into(),
));
}
let mut out = vec![commands, literals, offsets];
if n == 4 {
let sources: Vec<u8> = match &slots[3] {
StreamSlot::Rans(m) => ctx.decode_rans(m, slices[3], scale_bits, codec, copies)?,
StreamSlot::Raw => {
if slices[3].len() as u64 != copies {
return Err(MaterializeError::InvalidDescriptor(
"raw source stream length mismatch".into(),
));
}
slices[3].to_vec()
}
StreamSlot::Empty => {
if copies != 0 {
return Err(MaterializeError::InvalidDescriptor(
"non-empty source stream without a model".into(),
));
}
Vec::new()
}
};
if sources.len() as u64 != copies {
return Err(MaterializeError::InvalidDescriptor(
"source stream decoded length mismatch".into(),
));
}
out.push(sources);
}
Ok(out)
}
pub fn decode_three_streams(
ctx: &dyn crate::core::materialize::DecoderContext,
limits: &crate::core::limits::Limits,
refs: StreamRefs,
lens: ThreeStreams,
units: Option<u32>,
off_per_copy: u32,
) -> Result<DecodedStreams, crate::core::materialize::MaterializeError> {
let v = decode_streams_n(
ctx,
limits,
refs,
&[lens.seq_len, lens.lit_len, lens.off_len],
lens.cmds as u64,
lens.lit_out as u64,
units.map(|u| u as u64),
off_per_copy,
)?;
Ok(DecodedStreams {
commands: v[0].clone(),
literals: v[1].clone(),
offsets: v[2].clone(),
})
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FourStreams {
pub seq_len: u32,
pub lit_len: u32,
pub off_len: u32,
pub src_len: u32,
pub cmds: u32,
pub lit_out: u32,
}
pub fn decode_four_streams(
ctx: &dyn crate::core::materialize::DecoderContext,
limits: &crate::core::limits::Limits,
refs: StreamRefs,
lens: FourStreams,
) -> Result<DictStreams, crate::core::materialize::MaterializeError> {
let v = decode_streams_n(
ctx,
limits,
refs,
&[lens.seq_len, lens.lit_len, lens.off_len, lens.src_len],
lens.cmds as u64,
lens.lit_out as u64,
None,
2,
)?;
Ok(DictStreams {
commands: v[0].clone(),
literals: v[1].clone(),
offsets: v[2].clone(),
sources: v[3].clone(),
})
}
pub const fn max_model_object_bytes_n(per_model: u64, slots: usize) -> u64 {
per_model.saturating_mul(slots as u64).saturating_add(64)
}
pub const fn max_model_object_bytes(per_model: u64) -> u64 {
max_model_object_bytes_n(per_model, 3)
}
#[derive(Debug, Default)]
pub struct SequenceEncoder;
impl Encoder for SequenceEncoder {
fn name(&self) -> &'static str {
"SEQUENCE_RANS"
}
fn encode(&self, input: &[u8], ctx: &CandidateContext<'_>) -> Vec<Candidate> {
if input.is_empty() || input.len() as u64 > ctx.limits.max_chunk_size {
return Vec::new();
}
if input.len() < 128 {
return Vec::new();
}
let streams = encode_sequence(input);
let enc = match encode_streams(&streams) {
Some(e) => e,
None => return Vec::new(),
};
let model_obj = ObjectRecord::model(enc.model_obj);
let enc_obj = ObjectRecord::data(enc.enc_obj);
let rep = Representation::SequenceRans {
model: model_obj.id,
enc_obj: enc_obj.id,
scale_bits: SCALE_BITS,
codec: CODEC,
seq_len: enc.seq_len,
lit_len: enc.lit_len,
off_len: enc.off_len,
cmds: enc.cmds,
lit_out: enc.lit_out,
len: input.len() as u64,
};
let total = rep
.encoded_size()
.saturating_add(model_obj.payload.len() as u64)
.saturating_add(enc_obj.payload.len() as u64);
if total >= input.len() as u64 {
return Vec::new();
}
let split = ByteSplit {
reference: 64, ..Default::default()
};
let cost = crate::core::candidate::account_objects(
crate::core::cost::estimate(&rep, &split, model_obj.payload.len() as u64),
&[enc_obj.clone(), model_obj.clone()],
);
vec![Candidate {
representation: rep,
objects: vec![enc_obj, model_obj],
cost,
content_id: ctx.content_id,
}]
}
}
#[derive(Debug, Clone)]
pub struct SequenceDictEncoder {
pub dictionary: crate::core::extent::ChunkId,
pub dict_bytes: Vec<u8>,
pub dict_depth: u8,
}
impl Encoder for SequenceDictEncoder {
fn name(&self) -> &'static str {
"SEQUENCE_DICT"
}
fn encode(&self, input: &[u8], ctx: &CandidateContext<'_>) -> Vec<Candidate> {
if input.is_empty() || input.len() as u64 > ctx.limits.max_chunk_size {
return Vec::new();
}
if self.dict_depth.saturating_add(1) > ctx.limits.max_reference_depth {
return Vec::new();
}
if input.len() < 128 {
return Vec::new();
}
if self.dict_bytes.is_empty() || self.dict_bytes.len() > MAX_DICT {
return Vec::new();
}
let streams = match encode_sequence_dict(input, &self.dict_bytes) {
Some(s) => s,
None => return Vec::new(),
};
if !streams.sources.contains(&SRC_DICT) {
return Vec::new();
}
let cmds = streams.commands.len() as u32;
let lit_out = streams.literals.len() as u32;
let enc = match encode_streams_n(&[
streams.commands,
streams.literals,
streams.offsets,
streams.sources,
]) {
Some(e) => e,
None => return Vec::new(),
};
let model_obj = ObjectRecord::model(enc.model_obj);
let enc_obj = ObjectRecord::data(enc.enc_obj);
let rep = Representation::SequenceDict {
dictionary: self.dictionary,
dictionary_len: self.dict_bytes.len() as u32,
model: model_obj.id,
enc_obj: enc_obj.id,
scale_bits: SCALE_BITS,
codec: CODEC,
seq_len: enc.lens[0],
lit_len: enc.lens[1],
off_len: enc.lens[2],
src_len: enc.lens[3],
cmds,
lit_out,
len: input.len() as u64,
};
let total = rep
.encoded_size()
.saturating_add(model_obj.payload.len() as u64)
.saturating_add(enc_obj.payload.len() as u64);
if total >= input.len() as u64 {
return Vec::new();
}
let split = ByteSplit {
reference: 96,
..Default::default()
};
let mut cost = crate::core::candidate::account_objects(
crate::core::cost::estimate(&rep, &split, model_obj.payload.len() as u64),
&[enc_obj.clone(), model_obj.clone()],
);
cost.depth = cost.depth.saturating_add(self.dict_depth);
vec![Candidate {
representation: rep,
objects: vec![enc_obj, model_obj],
cost,
content_id: ctx.content_id,
}]
}
}
#[derive(Debug, Clone)]
pub struct SequenceSharedDictEncoder {
pub dictionary: crate::core::extent::ChunkId,
pub dict_bytes: Vec<u8>,
pub dict_depth: u8,
pub shared: crate::core::extent::ChunkId,
pub shared_bytes: Vec<u8>,
pub shared_depth: u8,
}
impl Encoder for SequenceSharedDictEncoder {
fn name(&self) -> &'static str {
"SEQUENCE_SHARED_DICT"
}
fn encode(&self, input: &[u8], ctx: &CandidateContext<'_>) -> Vec<Candidate> {
if input.is_empty() || input.len() as u64 > ctx.limits.max_chunk_size {
return Vec::new();
}
let dict_depth = self.dict_depth.max(self.shared_depth);
if dict_depth.saturating_add(1) > ctx.limits.max_reference_depth {
return Vec::new();
}
if input.len() < 128 {
return Vec::new();
}
if self.shared_bytes.is_empty()
|| self.shared_bytes.len() > MAX_DICT
|| self.dict_bytes.len() > MAX_DICT
{
return Vec::new();
}
let streams = match encode_sequence_shared(input, &self.dict_bytes, &self.shared_bytes) {
Some(s) => s,
None => return Vec::new(),
};
if !streams.sources.contains(&SRC_SHARED) {
return Vec::new();
}
let cmds = streams.commands.len() as u32;
let lit_out = streams.literals.len() as u32;
let enc = match encode_streams_n(&[
streams.commands,
streams.literals,
streams.offsets,
streams.sources,
]) {
Some(e) => e,
None => return Vec::new(),
};
let model_obj = ObjectRecord::model(enc.model_obj);
let enc_obj = ObjectRecord::data(enc.enc_obj);
let rep = Representation::SequenceSharedDict {
dictionary: self.dictionary,
dictionary_len: self.dict_bytes.len() as u32,
shared: self.shared,
shared_len: self.shared_bytes.len() as u32,
model: model_obj.id,
enc_obj: enc_obj.id,
scale_bits: SCALE_BITS,
codec: CODEC,
seq_len: enc.lens[0],
lit_len: enc.lens[1],
off_len: enc.lens[2],
src_len: enc.lens[3],
cmds,
lit_out,
len: input.len() as u64,
};
let total = rep
.encoded_size()
.saturating_add(model_obj.payload.len() as u64)
.saturating_add(enc_obj.payload.len() as u64);
if total >= input.len() as u64 {
return Vec::new();
}
let split = ByteSplit {
reference: 128,
..Default::default()
};
let mut cost = crate::core::candidate::account_objects(
crate::core::cost::estimate(&rep, &split, model_obj.payload.len() as u64),
&[enc_obj.clone(), model_obj.clone()],
);
cost.depth = cost.depth.saturating_add(dict_depth);
vec![Candidate {
representation: rep,
objects: vec![enc_obj, model_obj],
cost,
content_id: ctx.content_id,
}]
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::core::candidate::{CandidateContext, validate_candidate};
use crate::core::cost::Policy;
use crate::core::limits::Limits;
use crate::tests::helpers::MemResolver;
fn ctx_for<'a>(
input: &'a [u8],
limits: &'a Limits,
policy: &'a Policy,
) -> CandidateContext<'a> {
CandidateContext {
limits,
policy,
content_id: crate::core::extent::ChunkId::of(input),
bases: &[],
dedup: None,
}
}
fn text_chunk() -> Vec<u8> {
let sentence =
b"the quick brown fox jumps over the lazy dog and then walks back to the riverbed ";
let mut out = Vec::new();
for i in 0..40 {
out.extend_from_slice(sentence);
out.extend_from_slice(format!("sentence number {i} has a unique tail ").as_bytes());
}
out
}
fn noise(n: usize) -> Vec<u8> {
let mut state: u64 = 0x243F_6A88_85A3_08D3;
let mut out = Vec::with_capacity(n);
while out.len() < n {
state = state.wrapping_add(0x9E37_79B9_7F4A_7C15);
let mut z = state;
z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9);
z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB);
z ^= z >> 31;
let b = z.to_le_bytes();
let take = (n - out.len()).min(8);
out.extend_from_slice(&b[..take]);
}
out
}
#[test]
fn rle_is_all_copies_after_prefix() {
let input = vec![b'a'; 4096];
let streams = encode_sequence(&input);
assert_eq!(streams.literals, b"a");
assert_eq!(streams.commands[0], 0x00);
assert!(streams.commands[1..].iter().all(|&b| b >= 0x80));
assert_eq!(streams.offsets.len() / 2, streams.commands.len() - 1);
assert!(
streams
.offsets
.chunks_exact(2)
.all(|o| u16::from_le_bytes([o[0], o[1]]) == 1)
);
}
#[test]
fn literal_only_input() {
let input = noise(4096);
let streams = encode_sequence(&input);
assert!(streams.offsets.is_empty());
assert!(streams.commands.iter().all(|&b| b < 0x80));
assert_eq!(streams.literals.len(), input.len());
}
#[test]
fn long_match_continues_at_same_distance() {
let mut input = Vec::new();
let pattern: Vec<u8> = (0..200u32).map(|i| i as u8).collect();
for _ in 0..3 {
input.extend_from_slice(&pattern);
}
let streams = encode_sequence(&input);
let copies: Vec<u16> = streams
.offsets
.chunks_exact(2)
.map(|o| u16::from_le_bytes([o[0], o[1]]))
.collect();
assert!(copies.len() >= 2);
assert!(copies.iter().all(|&d| d == 200));
let copy_len: usize = streams
.commands
.iter()
.filter(|&&b| b >= 0x80)
.map(|&b| b as usize - 0x80 + MIN_MATCH)
.sum();
assert_eq!(copy_len, 400);
assert_eq!(copy_len + streams.literals.len(), input.len());
}
#[test]
fn model_object_roundtrip() {
let streams = encode_sequence(&text_chunk());
let enc = encode_streams(&streams).unwrap();
let parsed = parse_model_object(&enc.model_obj, 4096).unwrap();
assert_eq!(parsed.len(), 3);
assert!(!matches!(parsed[0], StreamSlot::Empty));
}
#[test]
fn sequence_encoder_wins_on_text() {
let limits = Limits::default();
let policy = Policy::default();
let input = text_chunk();
let ctx = ctx_for(&input, &limits, &policy);
let cands = SequenceEncoder.encode(&input, &ctx);
assert_eq!(cands.len(), 1);
let cand = &cands[0];
let resolver = MemResolver::from_map(
cand.objects
.iter()
.map(|o| (o.id, o.payload.clone()))
.collect(),
);
validate_candidate(cand, &input, &resolver, &limits).unwrap();
assert!(
cand.cost.persisted_bytes() < input.len() as u64,
"sequence rans persisted {} >= raw {}",
cand.cost.persisted_bytes(),
input.len()
);
let rans = crate::rans::residual::RansEncoder.encode(&input, &ctx);
let best_rans = rans
.iter()
.min_by_key(|c| c.total(&policy))
.map(|c| c.cost.persisted_bytes())
.unwrap_or(input.len() as u64);
assert!(
cand.cost.persisted_bytes() < best_rans,
"sequence {} not better than plain rans {}",
cand.cost.persisted_bytes(),
best_rans
);
}
#[test]
fn sequence_skips_urandom() {
let limits = Limits::default();
let policy = Policy::default();
let input = noise(65536);
let cands = SequenceEncoder.encode(&input, &ctx_for(&input, &limits, &policy));
assert!(
cands.is_empty(),
"urandom must not produce a sequence candidate"
);
let raw = crate::core::candidate::raw_candidate(
&input,
crate::core::extent::ChunkId::of(&input),
&limits,
)
.unwrap();
assert_eq!(raw.cost.persisted_bytes(), input.len() as u64 + 41);
}
#[test]
fn versioned_class2_mutated_roundtrips() {
let corpus = crate::evidence::corpus::versioned(1, 4);
let chunk = &corpus.versions[2][2 * 65536..3 * 65536];
let streams = encode_sequence(chunk);
let mut lits = 0usize;
let mut offs = 0usize;
let mut out = Vec::with_capacity(chunk.len());
for (cmd_no, &cmd) in streams.commands.iter().enumerate() {
if cmd < 0x80 {
let run = cmd as usize + 1;
if lits + run > streams.literals.len() || out.len() + run > chunk.len() {
panic!(
"literal overflow at command {}: out {} run {} lits {} total {}",
cmd_no,
out.len(),
run,
lits,
streams.literals.len()
);
}
out.extend_from_slice(&streams.literals[lits..lits + run]);
lits += run;
} else {
let clen = cmd as usize - 0x80 + 4;
if offs + 2 > streams.offsets.len() {
panic!("offset exhausted at command {cmd_no}");
}
let d =
u16::from_le_bytes([streams.offsets[offs], streams.offsets[offs + 1]]) as usize;
offs += 2;
if d == 0 || d > out.len() {
panic!("bad dist {d} at command {cmd_no} (out {})", out.len());
}
for _ in 0..clen {
let b = out[out.len() - d];
out.push(b);
}
}
}
assert_eq!(out, chunk);
}
#[test]
fn mulshift_data_roundtrips_exactly() {
let data: Vec<u8> = (0..65536u32)
.map(|i| (i.wrapping_mul(2654435761) >> 8) as u8)
.collect();
let streams = encode_sequence(&data);
let mut lits = 0usize;
let mut offs = 0usize;
let mut out = Vec::with_capacity(data.len());
for &cmd in &streams.commands {
if cmd < 0x80 {
let run = cmd as usize + 1;
out.extend_from_slice(&streams.literals[lits..lits + run]);
lits += run;
} else {
let clen = cmd as usize - 0x80 + 4;
let d =
u16::from_le_bytes([streams.offsets[offs], streams.offsets[offs + 1]]) as usize;
offs += 2;
assert!(d > 0 && d <= out.len());
for _ in 0..clen {
let b = out[out.len() - d];
out.push(b);
}
}
}
assert_eq!(out, data);
let cands = SequenceEncoder.encode(
&data,
&ctx_for(&data, &Limits::default(), &Policy::default()),
);
assert_eq!(cands.len(), 1);
let cand = &cands[0];
assert!(
cand.cost.persisted_bytes() < data.len() as u64 / 10,
"persisted {}",
cand.cost.persisted_bytes()
);
}
#[test]
fn degenerate_streams_fall_back_to_raw_slots() {
let input = noise(4096);
let streams = encode_sequence(&input);
assert!(streams.offsets.is_empty());
let enc = encode_streams(&streams).unwrap();
let parsed = parse_model_object(&enc.model_obj, 4096).unwrap();
assert_eq!(parsed[0], StreamSlot::Raw);
assert_eq!(parsed[2], StreamSlot::Empty);
let cands = SequenceEncoder.encode(
&input,
&ctx_for(&input, &Limits::default(), &Policy::default()),
);
assert!(cands.is_empty());
}
#[test]
fn rle_slot_layout() {
let input = vec![b'a'; 65536];
let streams = encode_sequence(&input);
assert_eq!(streams.literals, b"a");
let enc = encode_streams(&streams).unwrap();
let parsed = parse_model_object(&enc.model_obj, 4096).unwrap();
assert!(matches!(parsed[0], StreamSlot::Rans(_)));
assert_eq!(parsed[1], StreamSlot::Raw);
assert!(matches!(parsed[2], StreamSlot::Rans(_)));
let back = reassemble_for_test(&enc, &parsed);
assert_eq!(back.commands, streams.commands);
assert_eq!(back.literals, streams.literals);
assert_eq!(back.offsets, streams.offsets);
}
fn reassemble_for_test(enc: &EncodedSequence, slots: &[StreamSlot; 3]) -> SequenceStreams {
use crate::rans::metadata::decode_model;
use crate::rans::residual::decode_stream;
let seq_start = 0usize;
let seq_end = enc.seq_len as usize;
let lit_end = seq_end + enc.lit_len as usize;
let commands: Vec<u8> = match &slots[0] {
StreamSlot::Rans(m) => {
let model = decode_model(m, 4096).unwrap();
decode_stream(&model, &enc.enc_obj[seq_start..seq_end], enc.cmds as u64).unwrap()
}
StreamSlot::Raw => enc.enc_obj[seq_start..seq_end].to_vec(),
StreamSlot::Empty => Vec::new(),
};
let copies = commands.iter().filter(|&&b| b >= 0x80).count();
let off_out = 2 * copies;
let literals: Vec<u8> = match &slots[1] {
StreamSlot::Rans(m) => {
let model = decode_model(m, 4096).unwrap();
decode_stream(&model, &enc.enc_obj[seq_end..lit_end], enc.lit_out as u64).unwrap()
}
StreamSlot::Raw => enc.enc_obj[seq_end..lit_end].to_vec(),
StreamSlot::Empty => Vec::new(),
};
let offsets: Vec<u8> = match &slots[2] {
StreamSlot::Rans(m) => {
let model = decode_model(m, 4096).unwrap();
decode_stream(&model, &enc.enc_obj[lit_end..], off_out as u64).unwrap()
}
StreamSlot::Raw => enc.enc_obj[lit_end..].to_vec(),
StreamSlot::Empty => Vec::new(),
};
SequenceStreams {
commands,
literals,
offsets,
}
}
fn dict_chunk() -> Vec<u8> {
let mut out = Vec::with_capacity(65536);
let pattern: Vec<u8> = (0..7u32).map(|i| (i * 37 % 251) as u8).collect();
while out.len() < 65536 {
let take = (65536 - out.len()).min(pattern.len());
out.extend_from_slice(&pattern[..take]);
}
assert_eq!(out.len(), MAX_DICT);
out
}
#[test]
fn dict_parse_uses_dictionary_and_roundtrips_exactly() {
let dict = dict_chunk();
let mut input = dict.clone();
input[100] ^= 0x5A;
input[65535] ^= 0x01;
let streams = encode_sequence_dict(&input, &dict).unwrap();
assert!(
streams.sources.contains(&SRC_DICT),
"expected DICT copies on a dictionary-correlated input"
);
let mut lits = 0usize;
let mut offs = 0usize;
let mut srcs = 0usize;
let mut out = Vec::with_capacity(input.len());
for (i, &cmd) in streams.commands.iter().enumerate() {
if cmd < 0x80 {
let run = cmd as usize + 1;
assert!(lits + run <= streams.literals.len(), "lit overflow at {i}");
out.extend_from_slice(&streams.literals[lits..lits + run]);
lits += run;
} else {
let clen = cmd as usize - 0x80 + 4;
assert!(srcs < streams.sources.len(), "src exhausted at {i}");
let source = streams.sources[srcs];
srcs += 1;
assert!(offs + 2 <= streams.offsets.len(), "off exhausted at {i}");
let v =
u16::from_le_bytes([streams.offsets[offs], streams.offsets[offs + 1]]) as usize;
offs += 2;
match source {
SRC_LOCAL => {
assert!(v > 0 && v <= out.len(), "bad dist {v} at {i}");
for _ in 0..clen {
let b = out[out.len() - v];
out.push(b);
}
}
SRC_DICT => {
assert!(v + clen <= dict.len(), "dict copy out of bounds at {i}");
out.extend_from_slice(&dict[v..v + clen]);
}
other => panic!("unknown source {other} at {i}"),
}
}
}
assert_eq!(srcs, streams.sources.len());
assert_eq!(out, input);
}
#[test]
fn dict_long_match_continuation_advances_offset() {
let seq: Vec<u8> = (0..25536u32)
.map(|i| (i.wrapping_mul(2654435761) >> 16) as u8)
.collect();
let mut dict = seq.clone();
dict.extend_from_slice(&seq);
dict.extend_from_slice(&seq[..65536 - 2 * seq.len()]);
assert_eq!(dict.len(), MAX_DICT);
assert_eq!(&dict[25536..], &dict[..65536 - 25536]);
let input: Vec<u8> = dict[25536..].to_vec(); let streams = encode_sequence_dict(&input, &dict).unwrap();
assert!(streams.sources.contains(&SRC_DICT));
let mut lits = 0usize;
let mut offs = 0usize;
let mut srcs = 0usize;
let mut out = Vec::with_capacity(input.len());
let mut dict_copies = 0usize;
for &cmd in &streams.commands {
if cmd < 0x80 {
let run = cmd as usize + 1;
out.extend_from_slice(&streams.literals[lits..lits + run]);
lits += run;
} else {
let clen = cmd as usize - 0x80 + 4;
let source = streams.sources[srcs];
srcs += 1;
let v =
u16::from_le_bytes([streams.offsets[offs], streams.offsets[offs + 1]]) as usize;
offs += 2;
match source {
SRC_LOCAL => {
for _ in 0..clen {
let b = out[out.len() - v];
out.push(b);
}
}
SRC_DICT => {
out.extend_from_slice(&dict[v..v + clen]);
dict_copies += 1;
}
other => panic!("unknown source {other}"),
}
}
}
assert!(
dict_copies >= 2,
"expected a continuation chain (got {dict_copies} DICT copies)"
);
assert_eq!(out, input, "long DICT continuation must advance offsets");
}
#[test]
fn dict_encoder_wins_and_validates() {
let limits = Limits::default();
let policy = Policy::default();
let dict = dict_chunk();
let mut input = dict.clone();
for i in (0..65536).step_by(17) {
input[i] ^= 0x03;
}
let dict_id = crate::core::extent::ChunkId::of(&dict);
let enc = SequenceDictEncoder {
dictionary: dict_id,
dict_bytes: dict.clone(),
dict_depth: 0,
};
let ctx = ctx_for(&input, &limits, &policy);
let cands = enc.encode(&input, &ctx);
assert_eq!(cands.len(), 1);
let cand = &cands[0];
assert!(matches!(
cand.representation,
Representation::SequenceDict { .. }
));
assert_eq!(cand.cost.depth, 1);
let mut resolver = MemResolver::from_map(
cand.objects
.iter()
.map(|o| (o.id, o.payload.clone()))
.collect(),
);
resolver.put_chunk(
dict_id,
Representation::Raw {
obj: dict_id,
len: dict.len() as u64,
},
);
resolver.put_object(dict_id, dict);
validate_candidate(cand, &input, &resolver, &limits).unwrap();
assert!(
cand.cost.persisted_bytes() < input.len() as u64 / 4,
"persisted {} >= raw/4",
cand.cost.persisted_bytes()
);
}
#[test]
fn dict_skips_unrelated_dictionary() {
let limits = Limits::default();
let policy = Policy::default();
let dict = vec![0xFFu8; 65536];
let mut input = text_chunk();
input.resize(65536, b' ');
let enc = SequenceDictEncoder {
dictionary: crate::core::extent::ChunkId::of(&dict),
dict_bytes: dict,
dict_depth: 0,
};
let cands = enc.encode(&input, &ctx_for(&input, &limits, &policy));
assert!(cands.is_empty());
}
#[test]
fn dict_depth_cap_refuses_candidate() {
let limits = Limits::default();
let policy = Policy::default();
let dict = dict_chunk();
let input = text_chunk();
let enc = SequenceDictEncoder {
dictionary: crate::core::extent::ChunkId::of(&dict),
dict_bytes: dict,
dict_depth: limits.max_reference_depth, };
let cands = enc.encode(&input, &ctx_for(&input, &limits, &policy));
assert!(cands.is_empty());
}
#[test]
fn dict_urandom_has_no_fake_density() {
let limits = Limits::default();
let policy = Policy::default();
let dict = noise(65536);
let mut input = noise(65536);
for b in &mut input {
*b ^= 0xAA;
}
let enc = SequenceDictEncoder {
dictionary: crate::core::extent::ChunkId::of(&dict),
dict_bytes: dict,
dict_depth: 0,
};
assert!(
enc.encode(&input, &ctx_for(&input, &limits, &policy))
.is_empty()
);
}
#[test]
fn dict_dictionary_must_be_bounded() {
let limits = Limits::default();
let policy = Policy::default();
let dict = vec![0u8; MAX_DICT + 1];
let enc = SequenceDictEncoder {
dictionary: crate::core::extent::ChunkId::of(&dict),
dict_bytes: dict,
dict_depth: 0,
};
let input = text_chunk();
assert!(
enc.encode(&input, &ctx_for(&input, &limits, &policy))
.is_empty()
);
}
#[test]
fn shared_parse_uses_shared_dictionary_and_roundtrips_exactly() {
let shared = dict_chunk();
let mut input = shared.clone();
input[100] ^= 0x5A;
input[65535] ^= 0x01;
let streams = encode_sequence_shared(&input, &[], &shared).unwrap();
assert!(
streams.sources.contains(&SRC_SHARED),
"expected SHARED copies on a shared-dict-correlated input"
);
let mut lits = 0usize;
let mut offs = 0usize;
let mut srcs = 0usize;
let mut out = Vec::with_capacity(input.len());
for (i, &cmd) in streams.commands.iter().enumerate() {
if cmd < 0x80 {
let run = cmd as usize + 1;
out.extend_from_slice(&streams.literals[lits..lits + run]);
lits += run;
} else {
let clen = cmd as usize - 0x80 + 4;
let source = streams.sources[srcs];
srcs += 1;
let v =
u16::from_le_bytes([streams.offsets[offs], streams.offsets[offs + 1]]) as usize;
offs += 2;
match source {
SRC_LOCAL => {
assert!(v > 0 && v <= out.len(), "bad dist {v} at {i}");
for _ in 0..clen {
let b = out[out.len() - v];
out.push(b);
}
}
SRC_SHARED => {
assert!(v + clen <= shared.len(), "shared copy out of bounds at {i}");
out.extend_from_slice(&shared[v..v + clen]);
}
other => panic!("unknown source {other} at {i}"),
}
}
}
assert_eq!(out, input);
}
#[test]
fn shared_long_match_continuation_advances_offset() {
let seq: Vec<u8> = (0..25536u32)
.map(|i| (i.wrapping_mul(2654435761) >> 16) as u8)
.collect();
let mut shared = seq.clone();
shared.extend_from_slice(&seq);
shared.extend_from_slice(&seq[..65536 - 2 * seq.len()]);
assert_eq!(shared.len(), MAX_DICT);
let input: Vec<u8> = shared[25536..].to_vec(); let streams = encode_sequence_shared(&input, &[], &shared).unwrap();
assert!(streams.sources.contains(&SRC_SHARED));
let mut lits = 0usize;
let mut offs = 0usize;
let mut srcs = 0usize;
let mut out = Vec::with_capacity(input.len());
let mut shared_copies = 0usize;
for &cmd in &streams.commands {
if cmd < 0x80 {
let run = cmd as usize + 1;
out.extend_from_slice(&streams.literals[lits..lits + run]);
lits += run;
} else {
let clen = cmd as usize - 0x80 + 4;
let source = streams.sources[srcs];
srcs += 1;
let v =
u16::from_le_bytes([streams.offsets[offs], streams.offsets[offs + 1]]) as usize;
offs += 2;
match source {
SRC_LOCAL => {
for _ in 0..clen {
let b = out[out.len() - v];
out.push(b);
}
}
SRC_SHARED => {
out.extend_from_slice(&shared[v..v + clen]);
shared_copies += 1;
}
other => panic!("unknown source {other}"),
}
}
}
assert!(
shared_copies >= 2,
"expected a continuation chain (got {shared_copies} SHARED copies)"
);
assert_eq!(out, input, "long SHARED continuation must advance offsets");
}
#[test]
fn shared_three_way_parse_uses_all_sources() {
let shared = dict_chunk();
let mut input = Vec::new();
input.extend_from_slice(&shared[..20000]);
let pattern: Vec<u8> = (0..500u32).map(|i| i as u8).collect();
for _ in 0..20 {
input.extend_from_slice(&pattern);
}
input.extend_from_slice(&shared[20000..]);
let streams = encode_sequence_shared(&input, &[], &shared).unwrap();
assert!(streams.sources.contains(&SRC_SHARED));
assert!(streams.sources.contains(&SRC_LOCAL));
let mut lits = 0usize;
let mut offs = 0usize;
let mut srcs = 0usize;
let mut out = Vec::with_capacity(input.len());
for &cmd in &streams.commands {
if cmd < 0x80 {
let run = cmd as usize + 1;
out.extend_from_slice(&streams.literals[lits..lits + run]);
lits += run;
} else {
let clen = cmd as usize - 0x80 + 4;
let source = streams.sources[srcs];
srcs += 1;
let v =
u16::from_le_bytes([streams.offsets[offs], streams.offsets[offs + 1]]) as usize;
offs += 2;
match source {
SRC_LOCAL => {
assert!(v > 0 && v <= out.len());
for _ in 0..clen {
let b = out[out.len() - v];
out.push(b);
}
}
SRC_SHARED => {
assert!(v + clen <= shared.len());
out.extend_from_slice(&shared[v..v + clen]);
}
other => panic!("unknown source {other}"),
}
}
}
assert_eq!(out, input);
}
#[test]
fn shared_encoder_wins_and_validates() {
let limits = Limits::default();
let policy = Policy::default();
let shared = dict_chunk();
let mut input = shared.clone();
for i in (0..65536).step_by(17) {
input[i] ^= 0x03;
}
let shared_id = crate::core::extent::ChunkId::of(&shared);
let enc = SequenceSharedDictEncoder {
dictionary: crate::core::extent::ChunkId::ZERO,
dict_bytes: Vec::new(),
dict_depth: 0,
shared: shared_id,
shared_bytes: shared.clone(),
shared_depth: 0,
};
let ctx = ctx_for(&input, &limits, &policy);
let cands = enc.encode(&input, &ctx);
assert_eq!(cands.len(), 1);
let cand = &cands[0];
assert!(matches!(
cand.representation,
Representation::SequenceSharedDict { .. }
));
assert_eq!(cand.cost.depth, 1);
let mut resolver = MemResolver::from_map(
cand.objects
.iter()
.map(|o| (o.id, o.payload.clone()))
.collect(),
);
resolver.put_chunk(
shared_id,
Representation::Raw {
obj: shared_id,
len: shared.len() as u64,
},
);
resolver.put_object(shared_id, shared);
validate_candidate(cand, &input, &resolver, &limits).unwrap();
assert!(
cand.cost.persisted_bytes() < input.len() as u64 / 4,
"persisted {} >= raw/4",
cand.cost.persisted_bytes()
);
}
#[test]
fn shared_skips_unrelated_dictionary() {
let limits = Limits::default();
let policy = Policy::default();
let shared = vec![0xFFu8; 65536];
let mut input = text_chunk();
input.resize(65536, b' ');
let enc = SequenceSharedDictEncoder {
dictionary: crate::core::extent::ChunkId::ZERO,
dict_bytes: Vec::new(),
dict_depth: 0,
shared: crate::core::extent::ChunkId::of(&shared),
shared_bytes: shared,
shared_depth: 0,
};
let cands = enc.encode(&input, &ctx_for(&input, &limits, &policy));
assert!(cands.is_empty());
}
#[test]
fn shared_depth_cap_refuses_candidate() {
let limits = Limits::default();
let policy = Policy::default();
let shared = dict_chunk();
let input = text_chunk();
let enc = SequenceSharedDictEncoder {
dictionary: crate::core::extent::ChunkId::ZERO,
dict_bytes: Vec::new(),
dict_depth: 0,
shared: crate::core::extent::ChunkId::of(&shared),
shared_bytes: shared,
shared_depth: limits.max_reference_depth, };
let cands = enc.encode(&input, &ctx_for(&input, &limits, &policy));
assert!(cands.is_empty());
}
#[test]
fn shared_urandom_has_no_fake_density() {
let limits = Limits::default();
let policy = Policy::default();
let shared = noise(65536);
let mut input = noise(65536);
for b in &mut input {
*b ^= 0xAA;
}
let enc = SequenceSharedDictEncoder {
dictionary: crate::core::extent::ChunkId::ZERO,
dict_bytes: Vec::new(),
dict_depth: 0,
shared: crate::core::extent::ChunkId::of(&shared),
shared_bytes: shared,
shared_depth: 0,
};
assert!(
enc.encode(&input, &ctx_for(&input, &limits, &policy))
.is_empty()
);
}
#[test]
fn shared_dictionary_must_be_bounded() {
let limits = Limits::default();
let policy = Policy::default();
let shared = vec![0u8; MAX_DICT + 1];
let enc = SequenceSharedDictEncoder {
dictionary: crate::core::extent::ChunkId::ZERO,
dict_bytes: Vec::new(),
dict_depth: 0,
shared: crate::core::extent::ChunkId::of(&shared),
shared_bytes: shared,
shared_depth: 0,
};
let input = text_chunk();
assert!(
enc.encode(&input, &ctx_for(&input, &limits, &policy))
.is_empty()
);
}
}