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_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 debug!(
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 debug!(
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 #[allow(clippy::match_like_matches_macro)]
261 pub fn is_stream_valid(&self, stream: u32) -> bool {
262 assert!(!self.stream_fragments.is_empty());
263
264 if stream == 0 {
265 return false;
266 }
267
268 let i = stream as usize;
269 if i < self.stream_fragments.len() - 1 {
270 let start = self.stream_fragments[i] as usize;
271 let end = self.stream_fragments[i + 1] as usize;
272 let fragments = &self.fragments[start..end];
273 match fragments {
274 [f, ..] if f.location.is_nil() => false,
275 _ => true,
276 }
277 } else {
278 false
279 }
280 }
281
282 fn get_chunk_slice(&self, chunk: u32, offset: u32, size: u32) -> std::io::Result<&[u8]> {
285 let chunk_data = self.get_chunk_data(chunk)?;
286 if let Some(slice) = chunk_data.get(offset as usize..offset as usize + size as usize) {
287 Ok(slice)
288 } else {
289 Err(std::io::Error::new(
290 std::io::ErrorKind::InvalidData,
291 "PDZ file contains invalid byte ranges within a chunk",
292 ))
293 }
294 }
295
296 fn get_chunk_data(&self, chunk_index: u32) -> std::io::Result<&Arc<[u8]>> {
297 let _span = trace_span!("get_chunk_data").entered();
298 trace!(chunk_index);
299
300 debug_assert_eq!(self.chunk_cache.len(), self.chunk_table.len());
301
302 let Some(slot) = self.chunk_cache.get(chunk_index as usize) else {
303 return Err(std::io::Error::new(
304 std::io::ErrorKind::InvalidInput,
305 "Chunk index is out of range.",
306 ));
307 };
308
309 if let Some(arc) = slot.get() {
310 trace!(chunk_index, "found chunk in cache");
311 return Ok(arc);
312 }
313
314 let arc = self.load_chunk_data(chunk_index)?;
315 Ok(slot.get_or_init(move || arc))
316 }
317
318 #[inline(never)]
321 fn load_chunk_data(&self, chunk_index: u32) -> std::io::Result<Arc<[u8]>> {
322 assert_eq!(self.chunk_cache.len(), self.chunk_table.len());
323
324 let _span = debug_span!("load_chunk_data").entered();
325
326 let entry = &self.chunk_table[chunk_index as usize];
332
333 let compression_opt =
334 Compression::try_from_code_opt(entry.compression.get()).map_err(|_| {
335 std::io::Error::new(
336 std::io::ErrorKind::Unsupported,
337 "Chunk uses an unrecognized compression algorithm",
338 )
339 })?;
340
341 let mut compressed_data: Box<[u8]> =
343 FromZeros::new_box_zeroed_with_elems(entry.compressed_size.get() as usize)
344 .map_err(|_| std::io::Error::from(std::io::ErrorKind::OutOfMemory))?;
345 self.file
346 .read_exact_at(&mut compressed_data, entry.file_offset.get())?;
347
348 let uncompressed_data: Box<[u8]> = if let Some(compression) = compression_opt {
349 let mut uncompressed_data: Box<[u8]> =
350 FromZeros::new_box_zeroed_with_elems(entry.uncompressed_size.get() as usize)
351 .map_err(|_| std::io::Error::from(std::io::ErrorKind::OutOfMemory))?;
352
353 self::compress_utils::decompress_to_slice(
354 compression,
355 &compressed_data,
356 &mut uncompressed_data,
357 )?;
358 uncompressed_data
359 } else {
360 compressed_data
362 };
363
364 Ok(Arc::from(uncompressed_data))
368 }
369
370 pub fn read_stream(&self, stream: u32) -> anyhow::Result<StreamData> {
375 let _span = trace_span!("read_stream_to_cow").entered();
376 trace!(stream);
377
378 let mut fragments = self.stream_fragments_result(stream)?;
379
380 match fragments.first() {
381 Some(f) if f.location.is_nil() => fragments = &[],
382 _ => {}
383 }
384
385 if fragments.is_empty() {
387 return Ok(StreamData::empty());
388 }
389
390 if fragments.len() == 1 && fragments[0].location.is_compressed() {
394 let chunk_index = fragments[0].location.compressed_first_chunk();
395 let offset_within_chunk = fragments[0].location.compressed_offset_within_chunk();
396
397 let chunk_data = self.get_chunk_data(chunk_index)?;
398 let fragment_range = offset_within_chunk as usize
399 ..offset_within_chunk as usize + fragments[0].size as usize;
400
401 if chunk_data.get(fragment_range.clone()).is_none() {
403 bail!("PDZ data is invalid. Stream fragment byte range is out of range.");
404 }
405
406 return Ok(StreamData::ArcSlice(Arc::clone(chunk_data), fragment_range));
407 }
408
409 let stream_size: u32 = fragments.iter().map(|f| f.size).sum();
410 let stream_usize = stream_size as usize;
411
412 let mut output_buffer: Box<[u8]> = FromZeros::new_box_zeroed_with_elems(stream_usize)
414 .map_err(|_| std::io::Error::from(std::io::ErrorKind::OutOfMemory))?;
415 let mut output_slice: &mut [u8] = &mut output_buffer;
416
417 for fragment in fragments.iter() {
418 let stream_offset = stream_usize - output_slice.len();
419
420 let (fragment_output_slice, rest) = output_slice.split_at_mut(fragment.size as usize);
423 output_slice = rest;
424
425 if fragment.location.is_compressed() {
426 let chunk_index = fragment.location.compressed_first_chunk();
427 let offset_within_chunk = fragment.location.compressed_offset_within_chunk();
428
429 let chunk_data = self.get_chunk_data(chunk_index)?;
430 if let Some(chunk_slice) = chunk_data.get(
431 offset_within_chunk as usize
432 ..offset_within_chunk as usize + fragment.size as usize,
433 ) {
434 fragment_output_slice.copy_from_slice(chunk_slice);
435 } else {
436 bail!("PDZ data is invalid. Stream fragment byte range is out of range.");
437 }
438 } else {
439 let file_offset = fragment.location.uncompressed_file_offset();
440 trace!(
442 file_offset,
443 stream_offset,
444 fragment_len = fragment_output_slice.len(),
445 "reading uncompressed fragment"
446 );
447 self.file
448 .read_exact_at(fragment_output_slice, file_offset)?;
449 }
450 }
451
452 assert!(output_slice.is_empty());
453
454 Ok(StreamData::Box(output_buffer))
455 }
456
457 pub fn get_stream_reader(&self, stream: u32) -> Result<StreamReader<'_, F>> {
465 let fragments = self.stream_fragments_result(stream)?;
466 Ok(StreamReader {
467 msfz: self,
468 size: fragments.iter().map(|f| f.size).sum(),
469 fragments,
470 pos: 0,
471 })
472 }
473
474 pub fn num_fragments(&self) -> usize {
476 self.fragments.len()
477 }
478
479 pub fn num_chunks(&self) -> usize {
481 self.chunk_table.len()
482 }
483}
484
485pub struct StreamReader<'a, F> {
487 msfz: &'a Msfz<F>,
488 size: u32,
489 fragments: &'a [Fragment],
490 pos: u64,
491}
492
493impl<'a, F> StreamReader<'a, F> {
494 pub fn is_empty(&self) -> bool {
496 self.stream_size() == 0
497 }
498
499 pub fn stream_size(&self) -> u32 {
503 self.size
504 }
505}
506
507impl<'a, F: ReadAt> ReadAt for StreamReader<'a, F> {
508 fn read_at(&self, mut buf: &mut [u8], offset: u64) -> std::io::Result<usize> {
509 if buf.is_empty() {
510 return Ok(0);
511 }
512
513 let original_buf_len = buf.len();
514 let mut current_offset: u64 = offset;
515
516 for fragment in self.fragments.iter() {
517 debug_assert!(!buf.is_empty());
518
519 if current_offset >= fragment.size as u64 {
520 current_offset -= fragment.size as u64;
521 continue;
522 }
523
524 let fragment_bytes_available = fragment.size - current_offset as u32;
526
527 let num_bytes_xfer = buf.len().min(fragment_bytes_available as usize);
528 let (buf_xfer, buf_rest) = buf.split_at_mut(num_bytes_xfer);
529 buf = buf_rest;
530
531 if fragment.location.is_compressed() {
532 let chunk_index = fragment.location.compressed_first_chunk();
533 let offset_within_chunk = fragment.location.compressed_offset_within_chunk();
534
535 let chunk_slice = self.msfz.get_chunk_slice(
536 chunk_index,
537 offset_within_chunk + current_offset as u32,
538 num_bytes_xfer as u32,
539 )?;
540 buf_xfer.copy_from_slice(chunk_slice);
541 } else {
542 let file_offset = fragment.location.uncompressed_file_offset();
544 self.msfz
545 .file
546 .read_exact_at(buf_xfer, file_offset + current_offset)?;
547 }
548
549 if buf.is_empty() {
550 break;
551 }
552
553 if current_offset >= num_bytes_xfer as u64 {
554 current_offset -= num_bytes_xfer as u64;
555 } else {
556 current_offset = 0;
557 }
558 }
559
560 Ok(original_buf_len - buf.len())
561 }
562}
563
564impl<'a, F: ReadAt> Read for StreamReader<'a, F> {
565 fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
566 let n = self.read_at(buf, self.pos)?;
567 self.pos += n as u64;
568 Ok(n)
569 }
570}
571
572impl<'a, F> Seek for StreamReader<'a, F> {
573 fn seek(&mut self, pos: SeekFrom) -> std::io::Result<u64> {
574 match pos {
575 SeekFrom::Start(p) => self.pos = p,
576 SeekFrom::End(offset) => {
577 let new_pos = self.stream_size() as i64 + offset;
578 if new_pos < 0 {
579 return Err(std::io::ErrorKind::InvalidInput.into());
580 }
581 self.pos = new_pos as u64;
582 }
583 SeekFrom::Current(offset) => {
584 let new_pos = self.pos as i64 + offset;
585 if new_pos < 0 {
586 return Err(std::io::ErrorKind::InvalidInput.into());
587 }
588 self.pos = new_pos as u64;
589 }
590 }
591 Ok(self.pos)
592 }
593}
594
595struct DecodedStreamDir {
596 fragments: Vec<Fragment>,
597 stream_fragments: Vec<u32>,
598}
599
600fn decode_stream_dir(
601 stream_dir_bytes: &[u8],
602 num_streams: u32,
603 chunk_table: &[ChunkEntry],
604) -> anyhow::Result<DecodedStreamDir> {
605 let mut dec = Decoder {
606 bytes: stream_dir_bytes,
607 };
608
609 let mut fragments: Vec<Fragment> = Vec::new();
610 let mut stream_fragments: Vec<u32> = Vec::with_capacity(num_streams as usize + 1);
611
612 for _ in 0..num_streams {
613 stream_fragments.push(fragments.len() as u32);
614
615 let mut fragment_size = dec.u32()?;
616
617 if fragment_size == NIL_STREAM_SIZE {
618 fragments.push(Fragment {
622 size: 0,
623 location: FragmentLocation::NIL,
624 });
625 continue;
626 }
627
628 while fragment_size != 0 {
629 debug_assert_ne!(fragment_size, NIL_STREAM_SIZE);
630
631 let location_lo = dec.u32()?;
632 let location_hi = dec.u32()?;
633
634 if location_lo == u32::MAX && location_hi == u32::MAX {
635 bail!("The Stream Directory contains an invalid fragment record.");
636 }
637
638 let location = FragmentLocation {
639 lo: location_lo,
640 hi: location_hi,
641 };
642
643 if location.is_compressed() {
644 let first_chunk = location.compressed_first_chunk();
645 let offset_within_chunk = location.compressed_offset_within_chunk();
646
647 let Some(chunk) = chunk_table.get(first_chunk as usize) else {
648 bail!("The Stream Directory contains an invalid fragment record. Chunk index {first_chunk} exceeds the size of the chunk table.");
649 };
650
651 let uncompressed_chunk_size = chunk.uncompressed_size.get();
652
653 if offset_within_chunk >= uncompressed_chunk_size {
657 bail!("The Stream Directory contains an invalid fragment record. offset_within_chunk {offset_within_chunk} exceeds the size of the chunk.");
658 };
659
660 } else {
663 }
669
670 fragments.push(Fragment {
671 size: fragment_size,
672 location,
673 });
674
675 fragment_size = dec.u32()?;
678 if fragment_size == NIL_STREAM_SIZE {
679 bail!("Stream directory is malformed. It contains a non-initial fragment with size = NIL_STREAM_SIZE.");
680 }
681 }
683 }
684
685 stream_fragments.push(fragments.len() as u32);
686
687 fragments.shrink_to_fit();
688
689 Ok(DecodedStreamDir {
690 fragments,
691 stream_fragments,
692 })
693}
694
695struct Decoder<'a> {
696 bytes: &'a [u8],
697}
698
699impl<'a> Decoder<'a> {
700 fn next_n<const N: usize>(&mut self) -> anyhow::Result<&'a [u8; N]> {
701 if self.bytes.len() < N {
702 bail!("Buffer ran out of bytes");
703 }
704
705 let (lo, hi) = self.bytes.split_at(N);
706 self.bytes = hi;
707 Ok(<&[u8; N]>::try_from(lo).unwrap())
710 }
711
712 fn u32(&mut self) -> anyhow::Result<u32> {
713 Ok(u32::from_le_bytes(*self.next_n()?))
714 }
715}
716
717fn map_alloc_error<T>(result: Result<T, zerocopy::AllocError>) -> anyhow::Result<T> {
718 match result {
719 Ok(value) => Ok(value),
720 Err(zerocopy::AllocError) => {
721 Err(std::io::Error::from(std::io::ErrorKind::OutOfMemory).into())
722 }
723 }
724}