use std::io::{Read, Write};
use flate2::Compression;
use flate2::read::ZlibDecoder;
use flate2::write::ZlibEncoder;
use crate::le::{u32_at, u64_at};
use crate::segment::{ErrorKind, SectionHandle, Segment, SegmentError, SegmentWriter};
pub(crate) const INDEX_ENTRY_LEN: usize = 17;
const MAX_UNPACKED: u32 = 256 << 20;
const MIN_SAVING_PERCENT: usize = 10;
const RAW: u8 = 0;
const ZLIB: u8 = 1;
#[derive(Debug, Default)]
pub(crate) struct PackedBuilder {
data: Vec<u8>,
index: Vec<u8>,
}
impl PackedBuilder {
pub(crate) fn with_capacity(units: usize) -> Self {
Self {
data: Vec::new(),
index: Vec::with_capacity(units * INDEX_ENTRY_LEN),
}
}
pub(crate) fn push(&mut self, bytes: &[u8]) {
let (codec, stored) = pack(bytes);
self.index
.extend_from_slice(&(self.data.len() as u64).to_le_bytes());
self.index.extend_from_slice(
&u32::try_from(stored.len())
.expect("a unit is under 4 GiB")
.to_le_bytes(),
);
self.index.extend_from_slice(
&u32::try_from(bytes.len())
.expect("a unit is under 4 GiB")
.to_le_bytes(),
);
self.index.push(codec);
self.data.extend_from_slice(&stored);
}
pub(crate) fn finish(self) -> (Vec<u8>, Vec<u8>) {
(self.data, self.index)
}
}
#[derive(Debug)]
pub(crate) struct StoredUnit {
codec: u8,
unpacked: u32,
bytes: Vec<u8>,
}
impl StoredUnit {
pub(crate) const EMPTY: Self = Self {
codec: RAW,
unpacked: 0,
bytes: Vec::new(),
};
}
#[derive(Debug, Default)]
pub(crate) struct PackedCopy {
len: u64,
index: Vec<u8>,
}
impl PackedCopy {
pub(crate) fn with_capacity(units: usize) -> Self {
Self {
len: 0,
index: Vec::with_capacity(units * INDEX_ENTRY_LEN),
}
}
pub(crate) fn push(
&mut self,
writer: &mut SegmentWriter,
unit: &StoredUnit,
) -> Result<(), SegmentError> {
self.index.extend_from_slice(&self.len.to_le_bytes());
self.index.extend_from_slice(
&u32::try_from(unit.bytes.len())
.expect("a unit is under 4 GiB")
.to_le_bytes(),
);
self.index.extend_from_slice(&unit.unpacked.to_le_bytes());
self.index.push(unit.codec);
writer.write(&unit.bytes)?;
self.len += unit.bytes.len() as u64;
Ok(())
}
pub(crate) fn finish(self) -> Vec<u8> {
self.index
}
}
fn pack(bytes: &[u8]) -> (u8, Vec<u8>) {
if bytes.is_empty() {
return (RAW, Vec::new());
}
let mut encoder = ZlibEncoder::new(Vec::with_capacity(bytes.len() / 3), Compression::fast());
encoder
.write_all(bytes)
.expect("writing into a Vec cannot fail");
let packed = encoder.finish().expect("writing into a Vec cannot fail");
if packed.len() * 100 <= bytes.len() * (100 - MIN_SAVING_PERCENT) {
(ZLIB, packed)
} else {
(RAW, bytes.to_vec())
}
}
pub(crate) fn read(
segment: &Segment,
data: SectionHandle,
index: SectionHandle,
ordinal: u32,
) -> Result<Vec<u8>, SegmentError> {
let malformed = |reason| SegmentError::new(segment.path(), ErrorKind::Malformed(reason));
let StoredUnit {
codec,
unpacked,
bytes,
} = read_stored(segment, data, index, ordinal)?;
match codec {
RAW => Ok(bytes),
_ => {
let mut out = Vec::with_capacity((unpacked as usize).min(1 << 20));
ZlibDecoder::new(bytes.as_slice())
.take(u64::from(unpacked) + 1)
.read_to_end(&mut out)
.map_err(|_| malformed("a packed unit does not unpack"))?;
if out.len() == unpacked as usize {
Ok(out)
} else {
Err(malformed("a packed unit unpacks to the wrong length"))
}
}
}
}
pub(crate) fn read_stored(
segment: &Segment,
data: SectionHandle,
index: SectionHandle,
ordinal: u32,
) -> Result<StoredUnit, SegmentError> {
let malformed = |reason| SegmentError::new(segment.path(), ErrorKind::Malformed(reason));
let mut entry = [0; INDEX_ENTRY_LEN];
segment.read(
index,
u64::from(ordinal) * INDEX_ENTRY_LEN as u64,
&mut entry,
)?;
let offset = u64_at(&entry, 0);
let stored = u32_at(&entry, 8);
let unpacked = u32_at(&entry, 12);
let codec = entry[16];
match codec {
RAW if stored == unpacked => {}
ZLIB if unpacked <= MAX_UNPACKED => {}
_ => return Err(malformed("unknown packed unit codec")),
}
let end = offset
.checked_add(u64::from(stored))
.ok_or_else(|| malformed("a packed unit's range overflows"))?;
let bytes = segment.read_range(data, offset..end)?;
Ok(StoredUnit {
codec,
unpacked,
bytes,
})
}