use crate::bytes::{get_u32, get_u64};
use crate::compress::Algorithm;
pub(crate) const HEADER_LEN: usize = 16;
pub(crate) const MAGIC: &[u8; 4] = b"fpmc";
pub(crate) const CHUNK: u64 = 64 * 1024;
pub(crate) const MAX_INLINE: u64 = 3_802;
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub(crate) struct Header {
pub(crate) method: u32,
pub(crate) size: u64,
}
impl Header {
pub(crate) fn read_from(bytes: &[u8]) -> Option<Self> {
(bytes.len() >= HEADER_LEN && &bytes[..4] == MAGIC).then(|| Self {
method: get_u32(bytes, 4),
size: get_u64(bytes, 8),
})
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub(crate) enum Layout {
Inline,
Offsets,
Resource,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub(crate) enum Encoding {
Compressed(Algorithm),
Stored,
}
impl Encoding {
pub(crate) const fn available(self) -> bool {
match self {
Encoding::Compressed(algorithm) => algorithm.available(),
Encoding::Stored => true,
}
}
pub(crate) const fn algorithm(self) -> Option<Algorithm> {
match self {
Encoding::Compressed(algorithm) => Some(algorithm),
Encoding::Stored => None,
}
}
}
pub(crate) const fn layout(method: u32) -> Option<(Layout, Encoding)> {
use Encoding::{Compressed, Stored};
match method {
3 => Some((Layout::Inline, Compressed(Algorithm::Zlib))),
4 => Some((Layout::Resource, Compressed(Algorithm::Zlib))),
7 => Some((Layout::Inline, Compressed(Algorithm::Lzvn))),
8 => Some((Layout::Offsets, Compressed(Algorithm::Lzvn))),
9 => Some((Layout::Inline, Stored)),
10 => Some((Layout::Offsets, Stored)),
11 => Some((Layout::Inline, Compressed(Algorithm::Lzfse))),
12 => Some((Layout::Offsets, Compressed(Algorithm::Lzfse))),
13 => Some((Layout::Inline, Compressed(Algorithm::Lzbitmap))),
14 => Some((Layout::Offsets, Compressed(Algorithm::Lzbitmap))),
_ => None,
}
}
pub(crate) const STORED: u8 = 0xCC;
const RAW_ZLIB: u8 = 0xFF;
const RAW_LZVN: u8 = 0x06;
#[derive(Clone, Copy, PartialEq, Eq, Debug, thiserror::Error)]
pub(crate) enum ChunkError {
#[error(transparent)]
Decode(#[from] crate::compress::Error),
#[error("a stored chunk opens with {}, where its marker is {STORED:#04x}", opening(*.0))]
Unmarked(Option<u8>),
#[error(
"a chunk kept as its content holds {held} bytes, past the {expected} its place gives it"
)]
Overrun {
held: usize,
expected: usize,
},
}
fn opening(byte: Option<u8>) -> String {
byte.map_or_else(|| "nothing".into(), |b| format!("{b:#04x}"))
}
pub(crate) fn decode_chunk(
encoding: Encoding,
stored: &[u8],
out: &mut [u8],
) -> Result<usize, ChunkError> {
match (encoding, stored.split_first()) {
(Encoding::Stored, Some((&STORED, content))) => kept(content, out),
(Encoding::Stored, opening) => Err(ChunkError::Unmarked(opening.map(|(b, _)| *b))),
(Encoding::Compressed(Algorithm::Zlib), Some((&RAW_ZLIB, content))) => kept(content, out),
(Encoding::Compressed(Algorithm::Lzvn), Some((&RAW_LZVN, content))) => kept(content, out),
(Encoding::Compressed(algorithm), _) => {
Ok(crate::compress::decompress(algorithm, stored, out)?)
}
}
}
pub(crate) fn kept_inline(encoding: Encoding, stored: &[u8]) -> Option<&[u8]> {
match (encoding, stored.split_first()) {
(Encoding::Stored, Some((&STORED, content)))
| (Encoding::Compressed(Algorithm::Lzvn), Some((&RAW_LZVN, content))) => Some(content),
_ => None,
}
}
fn kept(content: &[u8], out: &mut [u8]) -> Result<usize, ChunkError> {
let room = out.len();
let to = out.get_mut(..content.len()).ok_or(ChunkError::Overrun {
held: content.len(),
expected: room,
})?;
to.copy_from_slice(content);
Ok(content.len())
}
pub(crate) const fn chunk_count(size: u64) -> u64 {
size.div_ceil(CHUNK)
}
pub(crate) const fn chunk_len(size: u64, index: u64) -> u64 {
let start = index * CHUNK;
if start >= size {
0
} else if size - start < CHUNK {
size - start
} else {
CHUNK
}
}
pub(crate) fn word(entries: &[u8], index: usize) -> u64 {
u64::from(get_u32(entries, 4 * index))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_header_is_its_magic_type_and_size() {
let mut bytes = b"fpmc".to_vec();
bytes.extend_from_slice(&8u32.to_le_bytes());
bytes.extend_from_slice(&204_800u64.to_le_bytes());
assert_eq!(
Header::read_from(&bytes),
Some(Header {
method: 8,
size: 204_800
})
);
bytes[0] = b'x';
assert_eq!(Header::read_from(&bytes), None);
assert_eq!(Header::read_from(b"fpmc"), None);
}
#[test]
fn content_is_cut_into_whole_chunks_and_a_short_last_one() {
assert_eq!(chunk_count(204_800), 4);
assert_eq!(chunk_len(204_800, 0), CHUNK);
assert_eq!(chunk_len(204_800, 3), 8_192);
assert_eq!(chunk_len(204_800, 4), 0);
assert_eq!(chunk_count(131_072), 2);
assert_eq!(chunk_len(131_072, 1), CHUNK);
assert_eq!(chunk_count(0), 0);
}
const ZLIB: Encoding = Encoding::Compressed(Algorithm::Zlib);
#[test]
fn a_zlib_chunk_opening_with_its_marker_is_its_content_raw() {
let mut out = [0u8; 4];
assert_eq!(decode_chunk(ZLIB, &[0xFF, b'e'], &mut out), Ok(1));
assert_eq!(out[0], b'e');
assert_eq!(
decode_chunk(ZLIB, &[0xFF; 6], &mut out),
Err(ChunkError::Overrun {
held: 5,
expected: 4
})
);
let lzvn = Encoding::Compressed(Algorithm::Lzvn);
assert!(decode_chunk(lzvn, &[0xFF, b'e'], &mut out).is_err());
}
#[test]
fn an_lzvn_chunk_opening_with_the_end_of_a_stream_is_its_content_raw() {
let lzvn = Encoding::Compressed(Algorithm::Lzvn);
let mut out = [0u8; 4];
assert_eq!(decode_chunk(lzvn, &[0x06, 1, 2, 3, 4], &mut out), Ok(4));
assert_eq!(out, [1, 2, 3, 4]);
assert_eq!(decode_chunk(lzvn, &[0x06, 9], &mut out), Ok(1));
assert_eq!(
decode_chunk(lzvn, &[0x06; 6], &mut out),
Err(ChunkError::Overrun {
held: 5,
expected: 4
})
);
assert!(decode_chunk(ZLIB, &[0x06, 1, 2, 3, 4], &mut out).is_err());
}
#[test]
fn a_stored_chunk_is_its_content_after_its_marker_and_nothing_else() {
let mut out = [0u8; 4];
assert_eq!(decode_chunk(Encoding::Stored, b"\xccabc", &mut out), Ok(3));
assert_eq!(&out[..3], b"abc");
assert_eq!(
decode_chunk(Encoding::Stored, b"\xcc\xccab", &mut out),
Ok(3)
);
assert_eq!(&out[..3], b"\xccab");
assert_eq!(decode_chunk(Encoding::Stored, b"\xcc", &mut out), Ok(0));
for other in (0..=u8::MAX).filter(|&b| b != STORED) {
assert_eq!(
decode_chunk(Encoding::Stored, &[other, b'a'], &mut out),
Err(ChunkError::Unmarked(Some(other)))
);
}
let none = decode_chunk(Encoding::Stored, b"", &mut out);
assert_eq!(none, Err(ChunkError::Unmarked(None)));
assert!(none.unwrap_err().to_string().contains("opens with nothing"));
assert_eq!(
decode_chunk(Encoding::Stored, b"\xccabcde", &mut out),
Err(ChunkError::Overrun {
held: 5,
expected: 4
})
);
assert!(decode_chunk(ZLIB, b"\xccabc", &mut out).is_err());
}
#[test]
fn only_the_observed_types_are_read() {
use Encoding::{Compressed, Stored};
assert_eq!(
layout(3),
Some((Layout::Inline, Compressed(Algorithm::Zlib)))
);
assert_eq!(
layout(4),
Some((Layout::Resource, Compressed(Algorithm::Zlib)))
);
assert_eq!(
layout(7),
Some((Layout::Inline, Compressed(Algorithm::Lzvn)))
);
assert_eq!(
layout(8),
Some((Layout::Offsets, Compressed(Algorithm::Lzvn)))
);
assert_eq!(layout(9), Some((Layout::Inline, Stored)));
assert_eq!(layout(10), Some((Layout::Offsets, Stored)));
assert_eq!(
layout(11),
Some((Layout::Inline, Compressed(Algorithm::Lzfse)))
);
assert_eq!(
layout(12),
Some((Layout::Offsets, Compressed(Algorithm::Lzfse)))
);
assert_eq!(
layout(13),
Some((Layout::Inline, Compressed(Algorithm::Lzbitmap)))
);
assert_eq!(
layout(14),
Some((Layout::Offsets, Compressed(Algorithm::Lzbitmap)))
);
for unobserved in [0, 1, 2, 5, 6, 15, 16] {
assert_eq!(layout(unobserved), None, "type {unobserved}");
}
assert!(Stored.available());
}
}