use alloc::vec::Vec;
use crate::utils::crc::crc32;
use crate::{Error, Result};
#[derive(Debug, Clone)]
pub struct Dictionary {
pub id: u32,
pub data: Vec<u8>,
}
impl Dictionary {
pub fn from_bytes(data: Vec<u8>) -> Result<Self> {
if data.is_empty() {
return Err(Error::Dictionary("empty dictionary".into()));
}
if data.len() > 32 * 1024 * 1024 {
return Err(Error::Dictionary("dictionary too large (>32MiB)".into()));
}
let mut id = crc32(&data);
if id == 0 {
id = 1;
}
Ok(Self { id, data })
}
pub fn with_id(id: u32, data: Vec<u8>) -> Result<Self> {
if data.is_empty() {
return Err(Error::Dictionary("empty dictionary".into()));
}
Ok(Self { id, data })
}
#[cfg(feature = "std")]
pub fn train_cover(samples: &[&[u8]], max_size: usize) -> Result<Self> {
crate::core::cover::train_cover(
samples,
crate::core::cover::CoverParams {
dict_size: max_size,
..Default::default()
},
)
}
pub fn train_simple(samples: &[&[u8]], max_size: usize) -> Result<Self> {
if samples.is_empty() {
return Err(Error::Dictionary("no samples".into()));
}
let max_size = max_size.clamp(256, 8 * 1024 * 1024);
let mut data = Vec::with_capacity(max_size);
for s in samples.iter().rev() {
if data.len() >= max_size {
break;
}
let take = (max_size - data.len()).min(s.len());
let start = s.len() - take;
data.extend_from_slice(&s[start..]);
}
if data.is_empty() {
return Err(Error::Dictionary("samples empty".into()));
}
Self::from_bytes(data)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn simple_train() {
let samples: Vec<&[u8]> = vec![
b"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n",
b"HTTP/1.1 404 Not Found\r\nContent-Type: text/html\r\n",
b"HTTP/1.1 500 Error\r\nContent-Type: text/plain\r\n",
];
let d = Dictionary::train_simple(&samples, 1024).unwrap();
assert!(!d.data.is_empty());
assert_ne!(d.id, 0);
}
}