1use std::io::{self, Read};
4
5use crate::{
6 FIXED_HEADER_SIZE, Header, MAGIC, SLAB_TABLE_ENTRY_SIZE, SlabBytes, SlabPlaceholder,
7 SlabPrimitive, SlabTableEntry, SlabType, VERSION,
8};
9use thiserror::Error;
10
11#[derive(Debug, Error)]
15pub enum ParseError {
16 #[error("file too short: need {FIXED_HEADER_SIZE} bytes, got {0}")]
19 TooShort(usize),
20 #[error("bad magic bytes")]
22 BadMagic,
23 #[error("unsupported version {0}")]
25 UnsupportedVersion(u32),
26 #[error("file has no slabs")]
29 NoSlabs,
30 #[error("slab table overruns buffer")]
32 SlabTableOverrun,
33 #[error("root slab index {index} out of range (slab count: {slab_count})")]
35 RootIndexOutOfRange {
36 index: usize,
38 slab_count: usize,
40 },
41 #[error("slab {index} has unknown type {type_byte:#x}")]
44 UnknownSlabType {
45 index: usize,
47 type_byte: u32,
49 },
50 #[error("slab {index} byte length {byte_length} not a multiple of element size {element_size}")]
52 UnalignedSlabLength {
53 index: usize,
55 byte_length: u32,
57 element_size: usize,
59 },
60 #[error("slab {index} data overruns buffer")]
63 SlabDataOverrun {
64 index: usize,
66 },
67 #[error("root slab is not TYPE_JSON")]
70 RootNotJson,
71}
72
73impl Header {
74 pub fn parse(bytes: &[u8]) -> Result<Self, ParseError> {
78 if bytes.len() < FIXED_HEADER_SIZE {
79 return Err(ParseError::TooShort(bytes.len()));
80 }
81 if bytes[0..8] != MAGIC {
82 return Err(ParseError::BadMagic);
83 }
84 let version = u32::from_le_bytes(bytes[8..12].try_into().unwrap());
85 if version != VERSION {
86 return Err(ParseError::UnsupportedVersion(version));
87 }
88 let slab_count = u32::from_le_bytes(bytes[12..16].try_into().unwrap()) as usize;
89 let root_json_slab_index = u32::from_le_bytes(bytes[16..20].try_into().unwrap()) as usize;
90
91 if slab_count == 0 {
92 return Err(ParseError::NoSlabs);
93 }
94 if root_json_slab_index >= slab_count {
95 return Err(ParseError::RootIndexOutOfRange {
96 index: root_json_slab_index,
97 slab_count,
98 });
99 }
100
101 Ok(Header {
102 slab_count,
103 root_json_slab_index,
104 })
105 }
106}
107
108impl SlabTableEntry {
109 pub fn parse(bytes: &[u8], index: usize) -> Result<Self, ParseError> {
117 let type_byte = u32::from_le_bytes(bytes[0..4].try_into().unwrap());
118 let start_offset = u32::from_le_bytes(bytes[4..8].try_into().unwrap()) as u64;
119 let byte_length = u32::from_le_bytes(bytes[8..12].try_into().unwrap()) as u64;
120
121 let slab_type = SlabType::try_from(type_byte)
122 .map_err(|type_byte| ParseError::UnknownSlabType { index, type_byte })?;
123
124 let element_size = slab_type.element_size() as u64;
125 if !byte_length.is_multiple_of(element_size) {
126 return Err(ParseError::UnalignedSlabLength {
127 index,
128 byte_length: byte_length as u32,
129 element_size: element_size as usize,
130 });
131 }
132
133 Ok(SlabTableEntry {
134 slab_type,
135 start_offset,
136 byte_length,
137 })
138 }
139}
140
141#[derive(Debug, Clone)]
144pub struct SlabDirectory {
145 pub header: Header,
147 pub entries: Vec<SlabTableEntry>,
149}
150
151impl SlabDirectory {
152 pub fn parse(bytes: &[u8]) -> Result<Self, ParseError> {
157 let header = Header::parse(bytes)?;
158
159 let table_end = FIXED_HEADER_SIZE + header.slab_count * SLAB_TABLE_ENTRY_SIZE;
160 if table_end > bytes.len() {
161 return Err(ParseError::SlabTableOverrun);
162 }
163
164 let mut entries = Vec::with_capacity(header.slab_count);
165 for i in 0..header.slab_count {
166 let pos = FIXED_HEADER_SIZE + i * SLAB_TABLE_ENTRY_SIZE;
167 entries.push(SlabTableEntry::parse(
168 &bytes[pos..pos + SLAB_TABLE_ENTRY_SIZE],
169 i,
170 )?);
171 }
172
173 Ok(Self { header, entries })
174 }
175
176 pub fn read<R: Read>(r: &mut R) -> io::Result<Self> {
185 let mut header_buf = [0u8; FIXED_HEADER_SIZE];
186 r.read_exact(&mut header_buf)?;
187 let header = Header::parse(&header_buf)
188 .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
189
190 let table_len = header
191 .slab_count
192 .checked_mul(SLAB_TABLE_ENTRY_SIZE)
193 .ok_or_else(|| {
194 io::Error::new(io::ErrorKind::InvalidData, "slab table size overflow")
195 })?;
196 let mut table = vec![0u8; table_len];
197 r.read_exact(&mut table)?;
198
199 let mut entries = Vec::with_capacity(header.slab_count);
200 for i in 0..header.slab_count {
201 let pos = i * SLAB_TABLE_ENTRY_SIZE;
202 let entry = SlabTableEntry::parse(&table[pos..pos + SLAB_TABLE_ENTRY_SIZE], i)
203 .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
204 entries.push(entry);
205 }
206
207 Ok(Self { header, entries })
208 }
209
210 pub fn validate_extents(&self, file_len: u64) -> Result<(), ParseError> {
213 for (index, entry) in self.entries.iter().enumerate() {
214 entry
215 .start_offset
216 .checked_add(entry.byte_length)
217 .filter(|&e| e <= file_len)
218 .ok_or(ParseError::SlabDataOverrun { index })?;
219 }
220 Ok(())
221 }
222
223 pub fn root_json_index(&self) -> usize {
225 self.header.root_json_slab_index
226 }
227
228 pub fn root_entry(&self) -> &SlabTableEntry {
230 &self.entries[self.root_json_index()]
231 }
232
233 pub fn header_and_table_size(&self) -> u64 {
236 (FIXED_HEADER_SIZE + self.entries.len() * SLAB_TABLE_ENTRY_SIZE) as u64
237 }
238}
239
240#[derive(Debug)]
245pub struct ParsedFile<'a> {
246 root_json_slab_index: usize,
247 slabs: Vec<SlabBytes<'a>>,
248}
249
250impl<'a> ParsedFile<'a> {
251 pub fn parse(data: &'a [u8]) -> Result<ParsedFile<'a>, ParseError> {
256 let dir = SlabDirectory::parse(data)?;
257 dir.validate_extents(data.len() as u64)?;
258
259 let slabs = dir
260 .entries
261 .iter()
262 .map(|entry| SlabBytes {
263 slab_type: entry.slab_type,
264 bytes: &data[entry.start_offset as usize
265 ..(entry.start_offset + entry.byte_length) as usize],
266 })
267 .collect::<Vec<_>>();
268
269 let root_json_slab_index = dir.header.root_json_slab_index;
270 if slabs[root_json_slab_index].slab_type != SlabType::Json {
271 return Err(ParseError::RootNotJson);
272 }
273
274 Ok(ParsedFile {
275 root_json_slab_index,
276 slabs,
277 })
278 }
279
280 pub fn slabs(&self) -> &[SlabBytes<'a>] {
282 &self.slabs
283 }
284
285 pub fn root_json_slab_index(&self) -> usize {
287 self.root_json_slab_index
288 }
289
290 pub fn root_json_bytes(&self) -> &'a [u8] {
292 self.slabs[self.root_json_slab_index].bytes
293 }
294
295 pub fn slab_at(&self, p: SlabPlaceholder) -> Result<&SlabBytes<'a>, DecodeError> {
298 self.slabs.get(p.0).ok_or(DecodeError::SlabIndexOutOfRange {
299 index: p.0,
300 slab_count: self.slabs.len(),
301 })
302 }
303
304 pub fn read<T: SlabPrimitive>(&self, p: SlabPlaceholder) -> Result<Vec<T>, DecodeError> {
307 let slab = self.slab_at(p)?;
308 let expected = T::SLAB_TYPE;
309 if slab.slab_type != expected {
310 return Err(DecodeError::SlabTypeMismatch {
311 index: p.0,
312 expected,
313 found: slab.slab_type,
314 });
315 }
316 let elem_size = expected.element_size();
317 Ok(slab
318 .bytes
319 .chunks_exact(elem_size)
320 .map(T::from_le_bytes)
321 .collect())
322 }
323
324 pub fn read_subjson_bytes(&self, p: SlabPlaceholder) -> Result<&'a [u8], DecodeError> {
328 let slab = self.slab_at(p)?;
329 if slab.slab_type != SlabType::Json {
330 return Err(DecodeError::SlabTypeMismatch {
331 index: p.0,
332 expected: SlabType::Json,
333 found: slab.slab_type,
334 });
335 }
336 Ok(slab.bytes)
337 }
338
339 pub fn read_raw(&self, p: SlabPlaceholder) -> Result<&'a [u8], DecodeError> {
344 Ok(self.slab_at(p)?.bytes)
345 }
346}
347
348#[derive(Debug, Error)]
350pub enum DecodeError {
351 #[error("JSLB parse error: {0}")]
353 Parse(#[from] ParseError),
354 #[error("slab index {index} out of range (slab count: {slab_count})")]
356 SlabIndexOutOfRange {
357 index: usize,
359 slab_count: usize,
361 },
362 #[error("slab {index} has type {found} but caller expected {expected}")]
364 SlabTypeMismatch {
365 index: usize,
367 expected: SlabType,
369 found: SlabType,
371 },
372}
373
374#[derive(Debug)]
383pub struct RootJsonReader<R: Read> {
384 inner: R,
385 remaining: u64,
386}
387
388impl<R: Read> RootJsonReader<R> {
389 pub fn new(mut inner: R) -> io::Result<Self> {
398 let dir = SlabDirectory::read(&mut inner)?;
399 let consumed = dir.header_and_table_size();
400 let root = *dir.root_entry();
401
402 if root.slab_type != SlabType::Json {
403 return Err(io::Error::new(
404 io::ErrorKind::InvalidData,
405 "JSLB root slab is not TYPE_JSON",
406 ));
407 }
408 if root.start_offset < consumed {
409 return Err(io::Error::new(
410 io::ErrorKind::InvalidData,
411 "JSLB root slab overlaps the slab table",
412 ));
413 }
414
415 let to_skip = root.start_offset - consumed;
416 let skipped = io::copy(&mut (&mut inner).take(to_skip), &mut io::sink())?;
417 if skipped < to_skip {
418 return Err(io::Error::new(
419 io::ErrorKind::UnexpectedEof,
420 "JSLB stream ended before the root JSON slab",
421 ));
422 }
423
424 Ok(Self {
425 inner,
426 remaining: root.byte_length,
427 })
428 }
429}
430
431impl<R: Read> Read for RootJsonReader<R> {
432 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
433 if self.remaining == 0 {
434 return Ok(0);
435 }
436 let take = (buf.len() as u64).min(self.remaining) as usize;
437 let n = self.inner.read(&mut buf[..take])?;
438 self.remaining -= n as u64;
439 Ok(n)
440 }
441}