1use crate::*;
2use anyhow::{bail, Result};
3use core::mem::size_of;
4use std::fs::File;
5use std::io::{Read, Seek, SeekFrom};
6use std::path::Path;
7use std::sync::{Arc, OnceLock};
8use sync_file::{RandomAccessFile, ReadAt};
9use tracing::{debug, debug_span, info, info_span, trace, trace_span};
10use zerocopy::IntoBytes;
11
12pub struct Msfz<F = RandomAccessFile> {
14 file: F,
15 fragments: Vec<Fragment>,
21
22 stream_fragments: Vec<u32>,
30
31 chunk_table: Box<[ChunkEntry]>,
32 chunk_cache: Vec<OnceLock<Arc<[u8]>>>,
33}
34
35#[derive(Clone)]
37struct Fragment {
38 size: u32,
39 location: FragmentLocation,
40}
41
42impl std::fmt::Debug for Fragment {
43 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
44 write!(f, "size 0x{:05x} at {:?}", self.size, self.location)
45 }
46}
47
48impl std::fmt::Debug for FragmentLocation {
49 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
50 if self.is_nil() {
51 f.write_str("nil")
52 } else if self.is_compressed() {
53 write!(
54 f,
55 "uncompressed at 0x{:06x}",
56 self.uncompressed_file_offset()
57 )
58 } else {
59 write!(
60 f,
61 "chunk {} : 0x{:04x}",
62 self.compressed_first_chunk(),
63 self.compressed_offset_within_chunk()
64 )
65 }
66 }
67}
68
69const FRAGMENT_LOCATION_32BIT_IS_COMPRESSED_MASK: u32 = 1u32 << 31;
70
71#[derive(Copy, Clone)]
73struct FragmentLocation {
74 lo: u32,
76 hi: u32,
78}
79
80impl FragmentLocation {
81 const NIL: Self = Self {
84 lo: u32::MAX,
85 hi: u32::MAX,
86 };
87
88 fn is_nil(&self) -> bool {
89 self.lo == u32::MAX && self.hi == u32::MAX
90 }
91
92 fn is_compressed(&self) -> bool {
93 (self.hi & FRAGMENT_LOCATION_32BIT_IS_COMPRESSED_MASK) != 0
94 }
95
96 fn compressed_first_chunk(&self) -> u32 {
97 debug_assert!(!self.is_nil());
98 debug_assert!(self.is_compressed());
99 self.hi & !FRAGMENT_LOCATION_32BIT_IS_COMPRESSED_MASK
100 }
101
102 fn compressed_offset_within_chunk(&self) -> u32 {
103 debug_assert!(!self.is_nil());
104 debug_assert!(self.is_compressed());
105 self.lo
106 }
107
108 fn uncompressed_file_offset(&self) -> u64 {
109 debug_assert!(!self.is_nil());
110 debug_assert!(!self.is_compressed());
111 ((self.hi as u64) << 32) | (self.lo as u64)
112 }
113}
114
115impl Msfz<RandomAccessFile> {
116 pub fn open<P: AsRef<Path>>(path: P) -> Result<Self> {
118 let f = File::open(path)?;
119 let raf = RandomAccessFile::from(f);
120 Self::from_file(raf)
121 }
122}
123
124impl<F: ReadAt> Msfz<F> {
125 pub fn from_file(file: F) -> Result<Self> {
127 let _span = info_span!("Msfz::from_file").entered();
128
129 let mut header: MsfzFileHeader = MsfzFileHeader::new_zeroed();
130 file.read_exact_at(header.as_mut_bytes(), 0)?;
131
132 if header.signature != MSFZ_FILE_SIGNATURE {
133 bail!("This file does not have a PDZ file signature.");
134 }
135
136 if header.version.get() != MSFZ_FILE_VERSION_V0 {
137 bail!("This PDZ file uses a version number that is not supported.");
138 }
139
140 let num_streams = header.num_streams.get();
142 if num_streams == 0 {
143 bail!("The stream directory is invalid; it is empty.");
144 }
145
146 let stream_dir_size_uncompressed = header.stream_dir_size_uncompressed.get() as usize;
147 let stream_dir_size_compressed = header.stream_dir_size_compressed.get() as usize;
148 let stream_dir_file_offset = header.stream_dir_offset.get();
149 let stream_dir_compression = header.stream_dir_compression.get();
150 info!(
151 num_streams,
152 stream_dir_size_uncompressed,
153 stream_dir_size_compressed,
154 stream_dir_compression,
155 stream_dir_file_offset,
156 "reading stream directory"
157 );
158
159 let mut stream_dir_bytes: Vec<u8> =
160 map_alloc_error(FromZeros::new_vec_zeroed(stream_dir_size_uncompressed))?;
161 if let Some(compression) = Compression::try_from_code_opt(stream_dir_compression)? {
162 let mut compressed_stream_dir: Vec<u8> =
163 map_alloc_error(FromZeros::new_vec_zeroed(stream_dir_size_compressed))?;
164 file.read_exact_at(
165 compressed_stream_dir.as_mut_bytes(),
166 header.stream_dir_offset.get(),
167 )?;
168
169 debug!("decompressing stream directory");
170
171 crate::compress_utils::decompress_to_slice(
172 compression,
173 &compressed_stream_dir,
174 &mut stream_dir_bytes,
175 )?;
176 } else {
177 file.read_exact_at(stream_dir_bytes.as_mut_bytes(), stream_dir_file_offset)?;
178 }
179
180 let num_chunks = header.num_chunks.get() as usize;
182 let chunk_index_size = header.chunk_table_size.get() as usize;
183 if chunk_index_size != num_chunks * size_of::<ChunkEntry>() {
184 bail!("This PDZ file is invalid. num_chunks and chunk_index_size are not consistent.");
185 }
186
187 let chunk_table_offset = header.chunk_table_offset.get();
188 let mut chunk_table: Box<[ChunkEntry]> =
189 map_alloc_error(FromZeros::new_box_zeroed_with_elems(num_chunks))?;
190 if num_chunks != 0 {
191 info!(
192 num_chunks,
193 chunk_table_offset, "reading compressed chunk table"
194 );
195 file.read_exact_at(chunk_table.as_mut_bytes(), chunk_table_offset)?;
196 } else {
197 }
199
200 let mut chunk_cache = Vec::with_capacity(num_chunks);
201 chunk_cache.resize_with(num_chunks, Default::default);
202
203 let stream_dir = decode_stream_dir(&stream_dir_bytes, num_streams, &chunk_table)?;
206
207 Ok(Self {
208 file,
209 fragments: stream_dir.fragments,
210 stream_fragments: stream_dir.stream_fragments,
211 chunk_table,
212 chunk_cache,
213 })
214 }
215
216 pub fn num_streams(&self) -> u32 {
218 (self.stream_fragments.len() - 1) as u32
219 }
220
221 fn stream_fragments_result(&self, stream: u32) -> Result<&[Fragment]> {
222 self.stream_fragments(stream)
223 .ok_or_else(|| anyhow::anyhow!("Stream index is out of range"))
224 }
225
226 fn stream_fragments(&self, stream: u32) -> Option<&[Fragment]> {
230 let i = stream as usize;
231 if i < self.stream_fragments.len() - 1 {
232 let start = self.stream_fragments[i] as usize;
233 let end = self.stream_fragments[i + 1] as usize;
234 let fragments = &self.fragments[start..end];
235 match fragments {
236 [f, ..] if f.location.is_nil() => Some(&[]),
237 _ => Some(fragments),
238 }
239 } else {
240 None
241 }
242 }
243
244 pub fn stream_size(&self, stream: u32) -> Result<u64> {
250 let fragments = self.stream_fragments_result(stream)?;
251 Ok(fragments.iter().map(|f| f.size as u64).sum())
252 }
253
254 pub fn is_stream_valid(&self, stream: u32) -> bool {
261 assert!(!self.stream_fragments.is_empty());
262
263 if stream == 0 {
264 return false;
265 }
266
267 let i = stream as usize;
268 if i < self.stream_fragments.len() - 1 {
269 let start = self.stream_fragments[i] as usize;
270 let end = self.stream_fragments[i + 1] as usize;
271 let fragments = &self.fragments[start..end];
272 match fragments {
273 [f, ..] if f.location.is_nil() => false,
274 _ => true,
275 }
276 } else {
277 false
278 }
279 }
280
281 fn get_chunk_slice(&self, chunk: u32, offset: u32, size: u32) -> std::io::Result<&[u8]> {
284 let chunk_data = self.get_chunk_data(chunk)?;
285 if let Some(slice) = chunk_data.get(offset as usize..offset as usize + size as usize) {
286 Ok(slice)
287 } else {
288 Err(std::io::Error::new(
289 std::io::ErrorKind::InvalidData,
290 "PDZ file contains invalid byte ranges within a chunk",
291 ))
292 }
293 }
294
295 fn get_chunk_data(&self, chunk_index: u32) -> std::io::Result<&Arc<[u8]>> {
296 let _span = trace_span!("get_chunk_data").entered();
297 trace!(chunk_index);
298
299 debug_assert_eq!(self.chunk_cache.len(), self.chunk_table.len());
300
301 let Some(slot) = self.chunk_cache.get(chunk_index as usize) else {
302 return Err(std::io::Error::new(
303 std::io::ErrorKind::InvalidInput,
304 "Chunk index is out of range.",
305 ));
306 };
307
308 if let Some(arc) = slot.get() {
309 trace!(chunk_index, "found chunk in cache");
310 return Ok(arc);
311 }
312
313 let arc = self.load_chunk_data(chunk_index)?;
314 Ok(slot.get_or_init(move || arc))
315 }
316
317 #[inline(never)]
320 fn load_chunk_data(&self, chunk_index: u32) -> std::io::Result<Arc<[u8]>> {
321 assert_eq!(self.chunk_cache.len(), self.chunk_table.len());
322
323 let _span = debug_span!("load_chunk_data").entered();
324
325 let entry = &self.chunk_table[chunk_index as usize];
331
332 let compression_opt =
333 Compression::try_from_code_opt(entry.compression.get()).map_err(|_| {
334 std::io::Error::new(
335 std::io::ErrorKind::Unsupported,
336 "Chunk uses an unrecognized compression algorithm",
337 )
338 })?;
339
340 let mut compressed_data: Box<[u8]> =
342 FromZeros::new_box_zeroed_with_elems(entry.compressed_size.get() as usize)
343 .map_err(|_| std::io::Error::from(std::io::ErrorKind::OutOfMemory))?;
344 self.file
345 .read_exact_at(&mut compressed_data, entry.file_offset.get())?;
346
347 let uncompressed_data: Box<[u8]> = if let Some(compression) = compression_opt {
348 let mut uncompressed_data: Box<[u8]> =
349 FromZeros::new_box_zeroed_with_elems(entry.uncompressed_size.get() as usize)
350 .map_err(|_| std::io::Error::from(std::io::ErrorKind::OutOfMemory))?;
351
352 self::compress_utils::decompress_to_slice(
353 compression,
354 &compressed_data,
355 &mut uncompressed_data,
356 )?;
357 uncompressed_data
358 } else {
359 compressed_data
361 };
362
363 Ok(Arc::from(uncompressed_data))
367 }
368
369 pub fn read_stream(&self, stream: u32) -> anyhow::Result<StreamData> {
374 let _span = trace_span!("read_stream_to_cow").entered();
375 trace!(stream);
376
377 let mut fragments = self.stream_fragments_result(stream)?;
378
379 match fragments.first() {
380 Some(f) if f.location.is_nil() => fragments = &[],
381 _ => {}
382 }
383
384 if fragments.is_empty() {
386 return Ok(StreamData::empty());
387 }
388
389 if fragments.len() == 1 && fragments[0].location.is_compressed() {
393 let chunk_index = fragments[0].location.compressed_first_chunk();
394 let offset_within_chunk = fragments[0].location.compressed_offset_within_chunk();
395
396 let chunk_data = self.get_chunk_data(chunk_index)?;
397 let fragment_range = offset_within_chunk as usize
398 ..offset_within_chunk as usize + fragments[0].size as usize;
399
400 if chunk_data.get(fragment_range.clone()).is_none() {
402 bail!("PDZ data is invalid. Stream fragment byte range is out of range.");
403 }
404
405 return Ok(StreamData::ArcSlice(Arc::clone(chunk_data), fragment_range));
406 }
407
408 let stream_size: u32 = fragments.iter().map(|f| f.size).sum();
409 let stream_usize = stream_size as usize;
410
411 let mut output_buffer: Box<[u8]> = FromZeros::new_box_zeroed_with_elems(stream_usize)
413 .map_err(|_| std::io::Error::from(std::io::ErrorKind::OutOfMemory))?;
414 let mut output_slice: &mut [u8] = &mut output_buffer;
415
416 for fragment in fragments.iter() {
417 let stream_offset = stream_usize - output_slice.len();
418
419 let (fragment_output_slice, rest) = output_slice.split_at_mut(fragment.size as usize);
422 output_slice = rest;
423
424 if fragment.location.is_compressed() {
425 let chunk_index = fragment.location.compressed_first_chunk();
426 let offset_within_chunk = fragment.location.compressed_offset_within_chunk();
427
428 let chunk_data = self.get_chunk_data(chunk_index)?;
429 if let Some(chunk_slice) = chunk_data.get(
430 offset_within_chunk as usize
431 ..offset_within_chunk as usize + fragment.size as usize,
432 ) {
433 fragment_output_slice.copy_from_slice(chunk_slice);
434 } else {
435 bail!("PDZ data is invalid. Stream fragment byte range is out of range.");
436 }
437 } else {
438 let file_offset = fragment.location.uncompressed_file_offset();
439 trace!(
441 file_offset,
442 stream_offset,
443 fragment_len = fragment_output_slice.len(),
444 "reading uncompressed fragment"
445 );
446 self.file
447 .read_exact_at(fragment_output_slice, file_offset)?;
448 }
449 }
450
451 assert!(output_slice.is_empty());
452
453 Ok(StreamData::Box(output_buffer))
454 }
455
456 pub fn get_stream_reader(&self, stream: u32) -> Result<StreamReader<'_, F>> {
464 let fragments = self.stream_fragments_result(stream)?;
465 Ok(StreamReader {
466 msfz: self,
467 size: fragments.iter().map(|f| f.size).sum(),
468 fragments,
469 pos: 0,
470 })
471 }
472
473 pub fn num_fragments(&self) -> usize {
475 self.fragments.len()
476 }
477
478 pub fn num_chunks(&self) -> usize {
480 self.chunk_table.len()
481 }
482}
483
484pub struct StreamReader<'a, F> {
486 msfz: &'a Msfz<F>,
487 size: u32,
488 fragments: &'a [Fragment],
489 pos: u64,
490}
491
492impl<'a, F> StreamReader<'a, F> {
493 pub fn is_empty(&self) -> bool {
495 self.stream_size() == 0
496 }
497
498 pub fn stream_size(&self) -> u32 {
502 self.size
503 }
504}
505
506impl<'a, F: ReadAt> ReadAt for StreamReader<'a, F> {
507 fn read_at(&self, mut buf: &mut [u8], offset: u64) -> std::io::Result<usize> {
508 if buf.is_empty() {
509 return Ok(0);
510 }
511
512 let original_buf_len = buf.len();
513 let mut current_offset: u64 = offset;
514
515 for fragment in self.fragments.iter() {
516 debug_assert!(!buf.is_empty());
517
518 if current_offset >= fragment.size as u64 {
519 current_offset -= fragment.size as u64;
520 continue;
521 }
522
523 let fragment_bytes_available = fragment.size - current_offset as u32;
525
526 let num_bytes_xfer = buf.len().min(fragment_bytes_available as usize);
527 let (buf_xfer, buf_rest) = buf.split_at_mut(num_bytes_xfer);
528 buf = buf_rest;
529
530 if fragment.location.is_compressed() {
531 let chunk_index = fragment.location.compressed_first_chunk();
532 let offset_within_chunk = fragment.location.compressed_offset_within_chunk();
533
534 let chunk_slice = self.msfz.get_chunk_slice(
535 chunk_index,
536 offset_within_chunk + current_offset as u32,
537 num_bytes_xfer as u32,
538 )?;
539 buf_xfer.copy_from_slice(chunk_slice);
540 } else {
541 let file_offset = fragment.location.uncompressed_file_offset();
543 self.msfz
544 .file
545 .read_exact_at(buf_xfer, file_offset + current_offset)?;
546 }
547
548 if buf.is_empty() {
549 break;
550 }
551
552 if current_offset >= num_bytes_xfer as u64 {
553 current_offset -= num_bytes_xfer as u64;
554 } else {
555 current_offset = 0;
556 }
557 }
558
559 Ok(original_buf_len - buf.len())
560 }
561}
562
563impl<'a, F: ReadAt> Read for StreamReader<'a, F> {
564 fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
565 let n = self.read_at(buf, self.pos)?;
566 self.pos += n as u64;
567 Ok(n)
568 }
569}
570
571impl<'a, F> Seek for StreamReader<'a, F> {
572 fn seek(&mut self, pos: SeekFrom) -> std::io::Result<u64> {
573 match pos {
574 SeekFrom::Start(p) => self.pos = p,
575 SeekFrom::End(offset) => {
576 let new_pos = self.stream_size() as i64 + offset;
577 if new_pos < 0 {
578 return Err(std::io::ErrorKind::InvalidInput.into());
579 }
580 self.pos = new_pos as u64;
581 }
582 SeekFrom::Current(offset) => {
583 let new_pos = self.pos as i64 + offset;
584 if new_pos < 0 {
585 return Err(std::io::ErrorKind::InvalidInput.into());
586 }
587 self.pos = new_pos as u64;
588 }
589 }
590 Ok(self.pos)
591 }
592}
593
594struct DecodedStreamDir {
595 fragments: Vec<Fragment>,
596 stream_fragments: Vec<u32>,
597}
598
599fn decode_stream_dir(
600 stream_dir_bytes: &[u8],
601 num_streams: u32,
602 chunk_table: &[ChunkEntry],
603) -> anyhow::Result<DecodedStreamDir> {
604 let mut dec = Decoder {
605 bytes: stream_dir_bytes,
606 };
607
608 let mut fragments: Vec<Fragment> = Vec::new();
609 let mut stream_fragments: Vec<u32> = Vec::with_capacity(num_streams as usize + 1);
610
611 for _ in 0..num_streams {
612 stream_fragments.push(fragments.len() as u32);
613
614 let mut fragment_size = dec.u32()?;
615
616 if fragment_size == NIL_STREAM_SIZE {
617 fragments.push(Fragment {
621 size: 0,
622 location: FragmentLocation::NIL,
623 });
624 continue;
625 }
626
627 while fragment_size != 0 {
628 debug_assert_ne!(fragment_size, NIL_STREAM_SIZE);
629
630 let location_lo = dec.u32()?;
631 let location_hi = dec.u32()?;
632
633 if location_lo == u32::MAX && location_hi == u32::MAX {
634 bail!("The Stream Directory contains an invalid fragment record.");
635 }
636
637 let location = FragmentLocation {
638 lo: location_lo,
639 hi: location_hi,
640 };
641
642 if location.is_compressed() {
643 let first_chunk = location.compressed_first_chunk();
644 let offset_within_chunk = location.compressed_offset_within_chunk();
645
646 let Some(chunk) = chunk_table.get(first_chunk as usize) else {
647 bail!("The Stream Directory contains an invalid fragment record. Chunk index {first_chunk} exceeds the size of the chunk table.");
648 };
649
650 let uncompressed_chunk_size = chunk.uncompressed_size.get();
651
652 if offset_within_chunk >= uncompressed_chunk_size {
656 bail!("The Stream Directory contains an invalid fragment record. offset_within_chunk {offset_within_chunk} exceeds the size of the chunk.");
657 };
658
659 } else {
662 }
668
669 fragments.push(Fragment {
670 size: fragment_size,
671 location,
672 });
673
674 fragment_size = dec.u32()?;
677 if fragment_size == NIL_STREAM_SIZE {
678 bail!("Stream directory is malformed. It contains a non-initial fragment with size = NIL_STREAM_SIZE.");
679 }
680 }
682 }
683
684 stream_fragments.push(fragments.len() as u32);
685
686 fragments.shrink_to_fit();
687
688 Ok(DecodedStreamDir {
689 fragments,
690 stream_fragments,
691 })
692}
693
694struct Decoder<'a> {
695 bytes: &'a [u8],
696}
697
698impl<'a> Decoder<'a> {
699 fn next_n<const N: usize>(&mut self) -> anyhow::Result<&'a [u8; N]> {
700 if self.bytes.len() < N {
701 bail!("Buffer ran out of bytes");
702 }
703
704 let (lo, hi) = self.bytes.split_at(N);
705 self.bytes = hi;
706 Ok(<&[u8; N]>::try_from(lo).unwrap())
709 }
710
711 fn u32(&mut self) -> anyhow::Result<u32> {
712 Ok(u32::from_le_bytes(*self.next_n()?))
713 }
714}
715
716fn map_alloc_error<T>(result: Result<T, zerocopy::AllocError>) -> anyhow::Result<T> {
717 match result {
718 Ok(value) => Ok(value),
719 Err(zerocopy::AllocError) => {
720 Err(std::io::Error::from(std::io::ErrorKind::OutOfMemory).into())
721 }
722 }
723}