use crate::{Error, Result};
#[cfg(feature = "std")]
use std::io::{Read, Write};
pub const MAGIC: [u8; 4] = *b"ZEX1";
pub const VERSION: u16 = 1;
pub const HEADER_SIZE: usize = 26;
pub const DEFAULT_BLOCK_SIZE: u32 = 256 * 1024;
macro_rules! bitflags_struct {
(
$(#[$meta:meta])*
pub struct $name:ident: $ty:ty {
$(
$(#[$fmeta:meta])*
const $flag:ident = $val:expr;
)*
}
) => {
$(#[$meta])*
pub struct $name(pub $ty);
impl $name {
$(
$(#[$fmeta])*
pub const $flag: Self = Self($val);
)*
pub const fn empty() -> Self { Self(0) }
#[allow(dead_code)]
pub const fn all() -> Self {
Self(0 $(| $val)*)
}
pub const fn bits(self) -> $ty { self.0 }
pub const fn from_bits_truncate(bits: $ty) -> Self { Self(bits) }
pub const fn contains(self, other: Self) -> bool {
self.0 & other.0 == other.0
}
pub fn insert(&mut self, other: Self) { self.0 |= other.0; }
pub fn remove(&mut self, other: Self) { self.0 &= !other.0; }
pub fn set(&mut self, other: Self, on: bool) {
if on { self.insert(other) } else { self.remove(other) }
}
}
impl Default for $name {
fn default() -> Self { Self::empty() }
}
impl ::core::ops::BitOr for $name {
type Output = Self;
fn bitor(self, rhs: Self) -> Self { Self(self.0 | rhs.0) }
}
impl ::core::ops::BitOrAssign for $name {
fn bitor_assign(&mut self, rhs: Self) { self.0 |= rhs.0; }
}
};
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[repr(u8)]
pub enum CompressionMode {
Turbo = 0,
#[default]
Balanced = 1,
Max = 2,
}
impl CompressionMode {
pub fn from_u8(v: u8) -> Result<Self> {
match v {
0 => Ok(Self::Turbo),
1 => Ok(Self::Balanced),
2 => Ok(Self::Max),
other => Err(Error::InvalidHeader(format!("unknown mode {other}"))),
}
}
pub fn default_level(self) -> u8 {
match self {
Self::Turbo => 1,
Self::Balanced => 3,
Self::Max => 12,
}
}
pub fn effective_level(self, user_level: u8) -> u8 {
let l = user_level.clamp(1, 22);
match self {
Self::Turbo => l.min(4),
Self::Balanced => l,
Self::Max => l.max(8),
}
}
pub fn as_str(self) -> &'static str {
match self {
Self::Turbo => "turbo",
Self::Balanced => "balanced",
Self::Max => "max",
}
}
}
impl ::core::fmt::Display for CompressionMode {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
f.write_str(self.as_str())
}
}
impl ::core::str::FromStr for CompressionMode {
type Err = Error;
fn from_str(s: &str) -> Result<Self> {
match s.to_ascii_lowercase().as_str() {
"turbo" | "fast" | "speed" => Ok(Self::Turbo),
"balanced" | "normal" | "default" | "hybrid" => Ok(Self::Balanced),
"max" | "high" | "ratio" | "best" => Ok(Self::Max),
other => Err(Error::InvalidParameter(format!(
"unknown profile '{other}' (use turbo|balanced|max)"
))),
}
}
}
bitflags_struct! {
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct HeaderFlags: u16 {
const CHECKSUM = 0b0000_0000_0001;
const CONTENT_SIZE = 0b0000_0000_0010;
const DICTIONARY = 0b0000_0000_0100;
const INDEPENDENT = 0b0000_0000_1000;
const MULTITHREAD = 0b0000_0001_0000;
const SECURE = 0b0000_0010_0000;
const ENCRYPTED = 0b0000_0100_0000;
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ZexHeader {
pub version: u16,
pub flags: HeaderFlags,
pub mode: CompressionMode,
pub level: u8,
pub block_size: u32,
pub content_size: u64,
pub dictionary_id: u32,
}
impl Default for ZexHeader {
fn default() -> Self {
Self {
version: VERSION,
flags: HeaderFlags::CHECKSUM | HeaderFlags::CONTENT_SIZE | HeaderFlags::INDEPENDENT,
mode: CompressionMode::Balanced,
level: 3,
block_size: DEFAULT_BLOCK_SIZE,
content_size: u64::MAX,
dictionary_id: 0,
}
}
}
impl ZexHeader {
pub fn new(content_size: u64, mode: CompressionMode, level: u8) -> Self {
let mut h = Self {
content_size,
mode,
level: level.clamp(1, 22),
..Default::default()
};
h.flags
.set(HeaderFlags::CONTENT_SIZE, content_size != u64::MAX);
h
}
pub fn to_bytes(&self) -> [u8; HEADER_SIZE] {
let mut buf = [0u8; HEADER_SIZE];
buf[0..4].copy_from_slice(&MAGIC);
buf[4..6].copy_from_slice(&self.version.to_le_bytes());
buf[6..8].copy_from_slice(&self.flags.bits().to_le_bytes());
buf[8] = self.mode as u8;
buf[9] = self.level;
buf[10..14].copy_from_slice(&self.block_size.to_le_bytes());
buf[14..22].copy_from_slice(&self.content_size.to_le_bytes());
buf[22..26].copy_from_slice(&self.dictionary_id.to_le_bytes());
buf
}
#[cfg(feature = "std")]
pub fn write_to<W: Write>(&self, out: &mut W) -> Result<()> {
out.write_all(&self.to_bytes())?;
Ok(())
}
#[cfg(feature = "std")]
pub fn read_from<R: Read>(inp: &mut R) -> Result<Self> {
let mut buf = [0u8; HEADER_SIZE];
inp.read_exact(&mut buf)?;
Self::from_bytes(&buf)
}
pub fn from_bytes(buf: &[u8]) -> Result<Self> {
if buf.len() < HEADER_SIZE {
return Err(Error::InvalidHeader(format!(
"buffer too short: {} < {HEADER_SIZE}",
buf.len()
)));
}
let magic = &buf[0..4];
if magic != MAGIC {
return Err(Error::InvalidHeader(format!(
"bad magic {:02x?} (expected ZEX1)",
magic
)));
}
let version = u16::from_le_bytes([buf[4], buf[5]]);
if version != VERSION {
return Err(Error::UnsupportedVersion {
found: version,
supported: VERSION,
});
}
let flags = HeaderFlags::from_bits_truncate(u16::from_le_bytes([buf[6], buf[7]]));
let mode = CompressionMode::from_u8(buf[8])?;
let level = buf[9];
let block_size = u32::from_le_bytes(buf[10..14].try_into().unwrap());
let content_size = u64::from_le_bytes(buf[14..22].try_into().unwrap());
let dictionary_id = u32::from_le_bytes(buf[22..26].try_into().unwrap());
if !(1024..=128 * 1024 * 1024).contains(&block_size) {
return Err(Error::InvalidHeader(format!(
"block_size {block_size} out of range"
)));
}
Ok(Self {
version,
flags,
mode,
level,
block_size,
content_size,
dictionary_id,
})
}
pub fn has_checksum(&self) -> bool {
self.flags.contains(HeaderFlags::CHECKSUM)
}
pub fn independent_blocks(&self) -> bool {
self.flags.contains(HeaderFlags::INDEPENDENT)
}
pub fn is_encrypted(&self) -> bool {
self.flags.contains(HeaderFlags::ENCRYPTED)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn header_roundtrip() {
let h = ZexHeader::new(12345, CompressionMode::Max, 9);
let bytes = h.to_bytes();
assert_eq!(&bytes[..4], b"ZEX1");
let parsed = ZexHeader::from_bytes(&bytes).unwrap();
assert_eq!(parsed, h);
}
#[test]
fn bad_magic() {
let mut bad = ZexHeader::default().to_bytes();
bad[0] = b'X';
assert!(ZexHeader::from_bytes(&bad).is_err());
}
}