use std::io::{self, Read};
use crate::{
FIXED_HEADER_SIZE, Header, MAGIC, SLAB_TABLE_ENTRY_SIZE, SlabBytes, SlabPlaceholder,
SlabPrimitive, SlabTableEntry, SlabType, VERSION,
};
use thiserror::Error;
#[derive(Debug, Error)]
pub enum ParseError {
#[error("file too short: need {FIXED_HEADER_SIZE} bytes, got {0}")]
TooShort(usize),
#[error("bad magic bytes")]
BadMagic,
#[error("unsupported version {0}")]
UnsupportedVersion(u32),
#[error("file has no slabs")]
NoSlabs,
#[error("slab table overruns buffer")]
SlabTableOverrun,
#[error("root slab index {index} out of range (slab count: {slab_count})")]
RootIndexOutOfRange {
index: usize,
slab_count: usize,
},
#[error("slab {index} has unknown type {type_byte:#x}")]
UnknownSlabType {
index: usize,
type_byte: u32,
},
#[error("slab {index} byte length {byte_length} not a multiple of element size {element_size}")]
UnalignedSlabLength {
index: usize,
byte_length: u32,
element_size: usize,
},
#[error("slab {index} data overruns buffer")]
SlabDataOverrun {
index: usize,
},
#[error("root slab is not TYPE_JSON")]
RootNotJson,
}
impl Header {
pub fn parse(bytes: &[u8]) -> Result<Self, ParseError> {
if bytes.len() < FIXED_HEADER_SIZE {
return Err(ParseError::TooShort(bytes.len()));
}
if bytes[0..8] != MAGIC {
return Err(ParseError::BadMagic);
}
let version = u32::from_le_bytes(bytes[8..12].try_into().unwrap());
if version != VERSION {
return Err(ParseError::UnsupportedVersion(version));
}
let slab_count = u32::from_le_bytes(bytes[12..16].try_into().unwrap()) as usize;
let root_json_slab_index = u32::from_le_bytes(bytes[16..20].try_into().unwrap()) as usize;
if slab_count == 0 {
return Err(ParseError::NoSlabs);
}
if root_json_slab_index >= slab_count {
return Err(ParseError::RootIndexOutOfRange {
index: root_json_slab_index,
slab_count,
});
}
Ok(Header {
slab_count,
root_json_slab_index,
})
}
}
impl SlabTableEntry {
pub fn parse(bytes: &[u8], index: usize) -> Result<Self, ParseError> {
let type_byte = u32::from_le_bytes(bytes[0..4].try_into().unwrap());
let start_offset = u32::from_le_bytes(bytes[4..8].try_into().unwrap()) as u64;
let byte_length = u32::from_le_bytes(bytes[8..12].try_into().unwrap()) as u64;
let slab_type = SlabType::try_from(type_byte)
.map_err(|type_byte| ParseError::UnknownSlabType { index, type_byte })?;
let element_size = slab_type.element_size() as u64;
if !byte_length.is_multiple_of(element_size) {
return Err(ParseError::UnalignedSlabLength {
index,
byte_length: byte_length as u32,
element_size: element_size as usize,
});
}
Ok(SlabTableEntry {
slab_type,
start_offset,
byte_length,
})
}
}
#[derive(Debug, Clone)]
pub struct SlabDirectory {
pub header: Header,
pub entries: Vec<SlabTableEntry>,
}
impl SlabDirectory {
pub fn parse(bytes: &[u8]) -> Result<Self, ParseError> {
let header = Header::parse(bytes)?;
let table_end = FIXED_HEADER_SIZE + header.slab_count * SLAB_TABLE_ENTRY_SIZE;
if table_end > bytes.len() {
return Err(ParseError::SlabTableOverrun);
}
let mut entries = Vec::with_capacity(header.slab_count);
for i in 0..header.slab_count {
let pos = FIXED_HEADER_SIZE + i * SLAB_TABLE_ENTRY_SIZE;
entries.push(SlabTableEntry::parse(
&bytes[pos..pos + SLAB_TABLE_ENTRY_SIZE],
i,
)?);
}
Ok(Self { header, entries })
}
pub fn read<R: Read>(r: &mut R) -> io::Result<Self> {
let mut header_buf = [0u8; FIXED_HEADER_SIZE];
r.read_exact(&mut header_buf)?;
let header = Header::parse(&header_buf)
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
let table_len = header
.slab_count
.checked_mul(SLAB_TABLE_ENTRY_SIZE)
.ok_or_else(|| {
io::Error::new(io::ErrorKind::InvalidData, "slab table size overflow")
})?;
let mut table = vec![0u8; table_len];
r.read_exact(&mut table)?;
let mut entries = Vec::with_capacity(header.slab_count);
for i in 0..header.slab_count {
let pos = i * SLAB_TABLE_ENTRY_SIZE;
let entry = SlabTableEntry::parse(&table[pos..pos + SLAB_TABLE_ENTRY_SIZE], i)
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
entries.push(entry);
}
Ok(Self { header, entries })
}
pub fn validate_extents(&self, file_len: u64) -> Result<(), ParseError> {
for (index, entry) in self.entries.iter().enumerate() {
entry
.start_offset
.checked_add(entry.byte_length)
.filter(|&e| e <= file_len)
.ok_or(ParseError::SlabDataOverrun { index })?;
}
Ok(())
}
pub fn root_json_index(&self) -> usize {
self.header.root_json_slab_index
}
pub fn root_entry(&self) -> &SlabTableEntry {
&self.entries[self.root_json_index()]
}
pub fn header_and_table_size(&self) -> u64 {
(FIXED_HEADER_SIZE + self.entries.len() * SLAB_TABLE_ENTRY_SIZE) as u64
}
}
#[derive(Debug)]
pub struct ParsedFile<'a> {
root_json_slab_index: usize,
slabs: Vec<SlabBytes<'a>>,
}
impl<'a> ParsedFile<'a> {
pub fn parse(data: &'a [u8]) -> Result<ParsedFile<'a>, ParseError> {
let dir = SlabDirectory::parse(data)?;
dir.validate_extents(data.len() as u64)?;
let slabs = dir
.entries
.iter()
.map(|entry| SlabBytes {
slab_type: entry.slab_type,
bytes: &data[entry.start_offset as usize
..(entry.start_offset + entry.byte_length) as usize],
})
.collect::<Vec<_>>();
let root_json_slab_index = dir.header.root_json_slab_index;
if slabs[root_json_slab_index].slab_type != SlabType::Json {
return Err(ParseError::RootNotJson);
}
Ok(ParsedFile {
root_json_slab_index,
slabs,
})
}
pub fn slabs(&self) -> &[SlabBytes<'a>] {
&self.slabs
}
pub fn root_json_slab_index(&self) -> usize {
self.root_json_slab_index
}
pub fn root_json_bytes(&self) -> &'a [u8] {
self.slabs[self.root_json_slab_index].bytes
}
pub fn slab_at(&self, p: SlabPlaceholder) -> Result<&SlabBytes<'a>, DecodeError> {
self.slabs.get(p.0).ok_or(DecodeError::SlabIndexOutOfRange {
index: p.0,
slab_count: self.slabs.len(),
})
}
pub fn read<T: SlabPrimitive>(&self, p: SlabPlaceholder) -> Result<Vec<T>, DecodeError> {
let slab = self.slab_at(p)?;
let expected = T::SLAB_TYPE;
if slab.slab_type != expected {
return Err(DecodeError::SlabTypeMismatch {
index: p.0,
expected,
found: slab.slab_type,
});
}
let elem_size = expected.element_size();
Ok(slab
.bytes
.chunks_exact(elem_size)
.map(T::from_le_bytes)
.collect())
}
pub fn read_subjson_bytes(&self, p: SlabPlaceholder) -> Result<&'a [u8], DecodeError> {
let slab = self.slab_at(p)?;
if slab.slab_type != SlabType::Json {
return Err(DecodeError::SlabTypeMismatch {
index: p.0,
expected: SlabType::Json,
found: slab.slab_type,
});
}
Ok(slab.bytes)
}
pub fn read_raw(&self, p: SlabPlaceholder) -> Result<&'a [u8], DecodeError> {
Ok(self.slab_at(p)?.bytes)
}
}
#[derive(Debug, Error)]
pub enum DecodeError {
#[error("JSLB parse error: {0}")]
Parse(#[from] ParseError),
#[error("slab index {index} out of range (slab count: {slab_count})")]
SlabIndexOutOfRange {
index: usize,
slab_count: usize,
},
#[error("slab {index} has type {found} but caller expected {expected}")]
SlabTypeMismatch {
index: usize,
expected: SlabType,
found: SlabType,
},
}
#[derive(Debug)]
pub struct RootJsonReader<R: Read> {
inner: R,
remaining: u64,
}
impl<R: Read> RootJsonReader<R> {
pub fn new(mut inner: R) -> io::Result<Self> {
let dir = SlabDirectory::read(&mut inner)?;
let consumed = dir.header_and_table_size();
let root = *dir.root_entry();
if root.slab_type != SlabType::Json {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"JSLB root slab is not TYPE_JSON",
));
}
if root.start_offset < consumed {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"JSLB root slab overlaps the slab table",
));
}
let to_skip = root.start_offset - consumed;
let skipped = io::copy(&mut (&mut inner).take(to_skip), &mut io::sink())?;
if skipped < to_skip {
return Err(io::Error::new(
io::ErrorKind::UnexpectedEof,
"JSLB stream ended before the root JSON slab",
));
}
Ok(Self {
inner,
remaining: root.byte_length,
})
}
}
impl<R: Read> Read for RootJsonReader<R> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
if self.remaining == 0 {
return Ok(0);
}
let take = (buf.len() as u64).min(self.remaining) as usize;
let n = self.inner.read(&mut buf[..take])?;
self.remaining -= n as u64;
Ok(n)
}
}