ms_pdb_msfz/
reader.rs

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
12/// Reads MSFZ files.
13pub struct Msfz<F = RandomAccessFile> {
14    file: F,
15    /// The list of all fragments in all streams.
16    ///
17    /// `fragments` is sorted by stream index, then by the order of the fragments in each stream.
18    /// Each stream has zero or more fragments associated with it. The set of fragments for a stream `s` is
19    /// `&fragments[stream_fragments[s] .. stream_fragments[s + 1]]`.
20    fragments: Vec<Fragment>,
21
22    /// Contains the index of the first entry in `fragments` for a given stream.
23    ///
24    /// The last entry in this list does not point to a stream. It simply points to the end of
25    /// the `fragments` list.
26    ///
27    /// Invariant: `stream_fragments.len() > 0`
28    /// Invariant: `stream_fragments.len() == num_streams() + 1`.
29    stream_fragments: Vec<u32>,
30
31    chunk_table: Box<[ChunkEntry]>,
32    chunk_cache: Vec<OnceLock<Arc<[u8]>>>,
33}
34
35// Describes a region within a stream.
36#[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/// Represents the location of a fragment, either compressed or uncompressed.
72#[derive(Copy, Clone)]
73struct FragmentLocation {
74    /// bits 0-31
75    lo: u32,
76    /// bits 32-63
77    hi: u32,
78}
79
80impl FragmentLocation {
81    /// This is a sentinel value for `FragmentLocation` that means "this stream is a nil stream".
82    /// It is not an actual fragment.
83    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    /// Opens an MSFZ file and validates its header.
117    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    /// Opens an MSFZ file using an implementation of the [`ReadAt`] trait.
126    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        // Load the stream directory.
141        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            if stream_dir_size_uncompressed != stream_dir_size_compressed {
178                bail!("This PDZ file is invalid. The Stream Directory is not compressed, but has inconsistent compressed vs. uncompressed sizes.");
179            }
180            file.read_exact_at(stream_dir_bytes.as_mut_bytes(), stream_dir_file_offset)?;
181        }
182
183        // Load the chunk table.
184        let num_chunks = header.num_chunks.get() as usize;
185        let chunk_index_size = header.chunk_table_size.get() as usize;
186        if chunk_index_size != num_chunks * size_of::<ChunkEntry>() {
187            bail!("This PDZ file is invalid. num_chunks and chunk_index_size are not consistent.");
188        }
189
190        let chunk_table_offset = header.chunk_table_offset.get();
191        let mut chunk_table: Box<[ChunkEntry]> =
192            map_alloc_error(FromZeros::new_box_zeroed_with_elems(num_chunks))?;
193        if num_chunks != 0 {
194            debug!(
195                num_chunks,
196                chunk_table_offset, "reading compressed chunk table"
197            );
198            file.read_exact_at(chunk_table.as_mut_bytes(), chunk_table_offset)?;
199        } else {
200            // Don't issue a read. The writer code may not have actually extended the file.
201        }
202
203        let mut chunk_cache = Vec::with_capacity(num_chunks);
204        chunk_cache.resize_with(num_chunks, Default::default);
205
206        // Decode the Stream Directory. We do this after loading the chunk table so that we can
207        // validate fragment records within the Stream Directory now.
208        let stream_dir = decode_stream_dir(&stream_dir_bytes, num_streams, &chunk_table)?;
209
210        Ok(Self {
211            file,
212            fragments: stream_dir.fragments,
213            stream_fragments: stream_dir.stream_fragments,
214            chunk_table,
215            chunk_cache,
216        })
217    }
218
219    /// The total number of streams in this MSFZ file. This count includes nil streams.
220    pub fn num_streams(&self) -> u32 {
221        (self.stream_fragments.len() - 1) as u32
222    }
223
224    fn stream_fragments_result(&self, stream: u32) -> Result<&[Fragment]> {
225        self.stream_fragments(stream)
226            .ok_or_else(|| anyhow::anyhow!("Stream index is out of range"))
227    }
228
229    /// Gets the fragments for a given stream.
230    ///
231    /// If `stream` is out of range, returns `None`.
232    fn stream_fragments(&self, stream: u32) -> Option<&[Fragment]> {
233        let i = stream as usize;
234        if i < self.stream_fragments.len() - 1 {
235            let start = self.stream_fragments[i] as usize;
236            let end = self.stream_fragments[i + 1] as usize;
237            let fragments = &self.fragments[start..end];
238            match fragments {
239                [f, ..] if f.location.is_nil() => Some(&[]),
240                _ => Some(fragments),
241            }
242        } else {
243            None
244        }
245    }
246
247    /// Gets the size of a given stream, in bytes.
248    ///
249    /// The `stream` value must be in a valid range of `0..num_streams()`.
250    ///
251    /// If `stream` is a NIL stream, this function returns 0.
252    pub fn stream_size(&self, stream: u32) -> Result<u64> {
253        let fragments = self.stream_fragments_result(stream)?;
254        Ok(fragments.iter().map(|f| f.size as u64).sum())
255    }
256
257    /// Returns `true` if `stream` is a valid stream index and the stream is non-nil.
258    ///
259    /// * If `stream` is 0, returns `false`.
260    /// * if `stream` is greater than `num_streams()`, returns false.
261    /// * If `stream` is a nil stream, this returns `false`.
262    /// * Else returns `true`.
263    #[allow(clippy::match_like_matches_macro)]
264    pub fn is_stream_valid(&self, stream: u32) -> bool {
265        assert!(!self.stream_fragments.is_empty());
266
267        if stream == 0 {
268            return false;
269        }
270
271        let i = stream as usize;
272        if i < self.stream_fragments.len() - 1 {
273            let start = self.stream_fragments[i] as usize;
274            let end = self.stream_fragments[i + 1] as usize;
275            let fragments = &self.fragments[start..end];
276            match fragments {
277                [f, ..] if f.location.is_nil() => false,
278                _ => true,
279            }
280        } else {
281            false
282        }
283    }
284
285    /// Gets a slice of a chunk. `offset` is the offset within the chunk and `size` is the
286    /// length in bytes of the slice. The chunk is loaded and decompressed, if necessary.
287    fn get_chunk_slice(&self, chunk: u32, offset: u32, size: u32) -> std::io::Result<&[u8]> {
288        let chunk_data = self.get_chunk_data(chunk)?;
289        if let Some(slice) = chunk_data.get(offset as usize..offset as usize + size as usize) {
290            Ok(slice)
291        } else {
292            Err(std::io::Error::new(
293                std::io::ErrorKind::InvalidData,
294                "PDZ file contains invalid byte ranges within a chunk",
295            ))
296        }
297    }
298
299    fn get_chunk_data(&self, chunk_index: u32) -> std::io::Result<&Arc<[u8]>> {
300        let _span = trace_span!("get_chunk_data").entered();
301        trace!(chunk_index);
302
303        debug_assert_eq!(self.chunk_cache.len(), self.chunk_table.len());
304
305        let Some(slot) = self.chunk_cache.get(chunk_index as usize) else {
306            return Err(std::io::Error::new(
307                std::io::ErrorKind::InvalidInput,
308                "Chunk index is out of range.",
309            ));
310        };
311
312        if let Some(arc) = slot.get() {
313            trace!(chunk_index, "found chunk in cache");
314            return Ok(arc);
315        }
316
317        let arc = self.load_chunk_data(chunk_index)?;
318        Ok(slot.get_or_init(move || arc))
319    }
320
321    /// This is the slow path for `get_chunk_data`, which loads the chunk data from disk and
322    /// decompresses it.
323    #[inline(never)]
324    fn load_chunk_data(&self, chunk_index: u32) -> std::io::Result<Arc<[u8]>> {
325        assert_eq!(self.chunk_cache.len(), self.chunk_table.len());
326
327        let _span = debug_span!("load_chunk_data").entered();
328
329        // We may race with another read that is loading the same entry.
330        // For now, that's OK, but in the future we should be smarter about de-duping
331        // cache fill requests.
332
333        // We have already implicitly validated the chunk index.
334        let entry = &self.chunk_table[chunk_index as usize];
335
336        let compression_opt =
337            Compression::try_from_code_opt(entry.compression.get()).map_err(|_| {
338                std::io::Error::new(
339                    std::io::ErrorKind::Unsupported,
340                    "Chunk uses an unrecognized compression algorithm",
341                )
342            })?;
343
344        // Read the data from disk.
345        let mut compressed_data: Box<[u8]> =
346            FromZeros::new_box_zeroed_with_elems(entry.compressed_size.get() as usize)
347                .map_err(|_| std::io::Error::from(std::io::ErrorKind::OutOfMemory))?;
348        self.file
349            .read_exact_at(&mut compressed_data, entry.file_offset.get())?;
350
351        let uncompressed_data: Box<[u8]> = if let Some(compression) = compression_opt {
352            let mut uncompressed_data: Box<[u8]> =
353                FromZeros::new_box_zeroed_with_elems(entry.uncompressed_size.get() as usize)
354                    .map_err(|_| std::io::Error::from(std::io::ErrorKind::OutOfMemory))?;
355
356            self::compress_utils::decompress_to_slice(
357                compression,
358                &compressed_data,
359                &mut uncompressed_data,
360            )?;
361            uncompressed_data
362        } else {
363            // This chunk is not compressed.
364            compressed_data
365        };
366
367        // This conversion should not need to allocate memory for the buffer.  The conversion from
368        // Box to Arc should allocate a new Arc object, but the backing allocation for the buffer
369        // should simply be transferred.
370        Ok(Arc::from(uncompressed_data))
371    }
372
373    /// Reads an entire stream to a vector.
374    ///
375    /// If the stream data fits entirely within a single decompressed chunk, then this function
376    /// returns a slice to the data, without copying it.
377    pub fn read_stream(&self, stream: u32) -> anyhow::Result<StreamData> {
378        let _span = trace_span!("read_stream_to_cow").entered();
379        trace!(stream);
380
381        let mut fragments = self.stream_fragments_result(stream)?;
382
383        match fragments.first() {
384            Some(f) if f.location.is_nil() => fragments = &[],
385            _ => {}
386        }
387
388        // If the stream is zero-length, then things are really simple.
389        if fragments.is_empty() {
390            return Ok(StreamData::empty());
391        }
392
393        // If this stream fits in a single fragment and the fragment is compressed, then we can
394        // return a single borrowed reference to it. This is common, and is one of the most
395        // important optimizations.
396        if fragments.len() == 1 && fragments[0].location.is_compressed() {
397            let chunk_index = fragments[0].location.compressed_first_chunk();
398            let offset_within_chunk = fragments[0].location.compressed_offset_within_chunk();
399
400            let chunk_data = self.get_chunk_data(chunk_index)?;
401            let fragment_range = offset_within_chunk as usize
402                ..offset_within_chunk as usize + fragments[0].size as usize;
403
404            // Validate the fragment range.
405            if chunk_data.get(fragment_range.clone()).is_none() {
406                bail!("PDZ data is invalid. Stream fragment byte range is out of range.");
407            }
408
409            return Ok(StreamData::ArcSlice(Arc::clone(chunk_data), fragment_range));
410        }
411
412        let stream_size: u32 = fragments.iter().map(|f| f.size).sum();
413        let stream_usize = stream_size as usize;
414
415        // Allocate a buffer and copy data from each chunk.
416        let mut output_buffer: Box<[u8]> = FromZeros::new_box_zeroed_with_elems(stream_usize)
417            .map_err(|_| std::io::Error::from(std::io::ErrorKind::OutOfMemory))?;
418        let mut output_slice: &mut [u8] = &mut output_buffer;
419
420        for fragment in fragments.iter() {
421            let stream_offset = stream_usize - output_slice.len();
422
423            // Because we computed stream_usize by summing the fragment sizes, this
424            // split_at_mut() call should not fail.
425            let (fragment_output_slice, rest) = output_slice.split_at_mut(fragment.size as usize);
426            output_slice = rest;
427
428            if fragment.location.is_compressed() {
429                let chunk_index = fragment.location.compressed_first_chunk();
430                let offset_within_chunk = fragment.location.compressed_offset_within_chunk();
431
432                let chunk_data = self.get_chunk_data(chunk_index)?;
433                if let Some(chunk_slice) = chunk_data.get(
434                    offset_within_chunk as usize
435                        ..offset_within_chunk as usize + fragment.size as usize,
436                ) {
437                    fragment_output_slice.copy_from_slice(chunk_slice);
438                } else {
439                    bail!("PDZ data is invalid. Stream fragment byte range is out of range.");
440                }
441            } else {
442                let file_offset = fragment.location.uncompressed_file_offset();
443                // Read an uncompressed fragment.
444                trace!(
445                    file_offset,
446                    stream_offset,
447                    fragment_len = fragment_output_slice.len(),
448                    "reading uncompressed fragment"
449                );
450                self.file
451                    .read_exact_at(fragment_output_slice, file_offset)?;
452            }
453        }
454
455        assert!(output_slice.is_empty());
456
457        Ok(StreamData::Box(output_buffer))
458    }
459
460    /// Returns an object which can read from a given stream.  The returned object implements
461    /// the [`Read`], [`Seek`], and [`ReadAt`] traits.
462    ///
463    /// If `stream` is out of range (greater than or equal to `num_streams()`) then this function
464    /// returns an error.
465    ///
466    /// If `stream` is a nil stream then this function returns a `StreamReader` whose size is 0.
467    pub fn get_stream_reader(&self, stream: u32) -> Result<StreamReader<'_, F>> {
468        let fragments = self.stream_fragments_result(stream)?;
469        Ok(StreamReader {
470            msfz: self,
471            size: fragments.iter().map(|f| f.size).sum(),
472            fragments,
473            pos: 0,
474        })
475    }
476
477    /// The total number of fragments in the MSFZ file.
478    pub fn num_fragments(&self) -> usize {
479        self.fragments.len()
480    }
481
482    /// The total number of compressed chunks.
483    pub fn num_chunks(&self) -> usize {
484        self.chunk_table.len()
485    }
486}
487
488/// Allows reading a stream using the [`Read`], [`Seek`], and [`ReadAt`] traits.
489pub struct StreamReader<'a, F> {
490    msfz: &'a Msfz<F>,
491    size: u32,
492    fragments: &'a [Fragment],
493    pos: u64,
494}
495
496impl<'a, F> StreamReader<'a, F> {
497    /// Returns `true` if this is a zero-length stream or a nil stream.
498    pub fn is_empty(&self) -> bool {
499        self.stream_size() == 0
500    }
501
502    /// Size in bytes of the stream.
503    ///
504    /// This returns zero for nil streams.
505    pub fn stream_size(&self) -> u32 {
506        self.size
507    }
508}
509
510impl<'a, F: ReadAt> ReadAt for StreamReader<'a, F> {
511    fn read_at(&self, mut buf: &mut [u8], offset: u64) -> std::io::Result<usize> {
512        if buf.is_empty() {
513            return Ok(0);
514        }
515
516        let original_buf_len = buf.len();
517        let mut current_offset: u64 = offset;
518
519        for fragment in self.fragments.iter() {
520            debug_assert!(!buf.is_empty());
521
522            if current_offset >= fragment.size as u64 {
523                current_offset -= fragment.size as u64;
524                continue;
525            }
526
527            // Because of the range check above, we know that this cannot overflow.
528            let fragment_bytes_available = fragment.size - current_offset as u32;
529
530            let num_bytes_xfer = buf.len().min(fragment_bytes_available as usize);
531            let (buf_xfer, buf_rest) = buf.split_at_mut(num_bytes_xfer);
532            buf = buf_rest;
533
534            if fragment.location.is_compressed() {
535                let chunk_index = fragment.location.compressed_first_chunk();
536                let offset_within_chunk = fragment.location.compressed_offset_within_chunk();
537
538                let chunk_slice = self.msfz.get_chunk_slice(
539                    chunk_index,
540                    offset_within_chunk + current_offset as u32,
541                    num_bytes_xfer as u32,
542                )?;
543                buf_xfer.copy_from_slice(chunk_slice);
544            } else {
545                // Read the stream data directly from disk.
546                let file_offset = fragment.location.uncompressed_file_offset();
547                self.msfz
548                    .file
549                    .read_exact_at(buf_xfer, file_offset + current_offset)?;
550            }
551
552            if buf.is_empty() {
553                break;
554            }
555
556            if current_offset >= num_bytes_xfer as u64 {
557                current_offset -= num_bytes_xfer as u64;
558            } else {
559                current_offset = 0;
560            }
561        }
562
563        Ok(original_buf_len - buf.len())
564    }
565}
566
567impl<'a, F: ReadAt> Read for StreamReader<'a, F> {
568    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
569        let n = self.read_at(buf, self.pos)?;
570        self.pos += n as u64;
571        Ok(n)
572    }
573}
574
575impl<'a, F> Seek for StreamReader<'a, F> {
576    fn seek(&mut self, pos: SeekFrom) -> std::io::Result<u64> {
577        match pos {
578            SeekFrom::Start(p) => self.pos = p,
579            SeekFrom::End(offset) => {
580                let new_pos = self.stream_size() as i64 + offset;
581                if new_pos < 0 {
582                    return Err(std::io::ErrorKind::InvalidInput.into());
583                }
584                self.pos = new_pos as u64;
585            }
586            SeekFrom::Current(offset) => {
587                let new_pos = self.pos as i64 + offset;
588                if new_pos < 0 {
589                    return Err(std::io::ErrorKind::InvalidInput.into());
590                }
591                self.pos = new_pos as u64;
592            }
593        }
594        Ok(self.pos)
595    }
596}
597
598struct DecodedStreamDir {
599    fragments: Vec<Fragment>,
600    stream_fragments: Vec<u32>,
601}
602
603fn decode_stream_dir(
604    stream_dir_bytes: &[u8],
605    num_streams: u32,
606    chunk_table: &[ChunkEntry],
607) -> anyhow::Result<DecodedStreamDir> {
608    let mut dec = Decoder {
609        bytes: stream_dir_bytes,
610    };
611
612    let mut fragments: Vec<Fragment> = Vec::new();
613    let mut stream_fragments: Vec<u32> = Vec::with_capacity(num_streams as usize + 1);
614
615    for _ in 0..num_streams {
616        stream_fragments.push(fragments.len() as u32);
617
618        let mut fragment_size = dec.u32()?;
619
620        if fragment_size == NIL_STREAM_SIZE {
621            // Nil stream. We synthesize a fake fragment record so that we can distinguish
622            // nil streams and non-nil streams, and yet optimize for the case where nearly all
623            // streams are non-nil.
624            fragments.push(Fragment {
625                size: 0,
626                location: FragmentLocation::NIL,
627            });
628            continue;
629        }
630
631        while fragment_size != 0 {
632            debug_assert_ne!(fragment_size, NIL_STREAM_SIZE);
633
634            let location_lo = dec.u32()?;
635            let location_hi = dec.u32()?;
636
637            if location_lo == u32::MAX && location_hi == u32::MAX {
638                bail!("The Stream Directory contains an invalid fragment record.");
639            }
640
641            let location = FragmentLocation {
642                lo: location_lo,
643                hi: location_hi,
644            };
645
646            if location.is_compressed() {
647                let first_chunk = location.compressed_first_chunk();
648                let offset_within_chunk = location.compressed_offset_within_chunk();
649
650                let Some(chunk) = chunk_table.get(first_chunk as usize) else {
651                    bail!("The Stream Directory contains an invalid fragment record. Chunk index {first_chunk} exceeds the size of the chunk table.");
652                };
653
654                let uncompressed_chunk_size = chunk.uncompressed_size.get();
655
656                // Testing for greater-than-or-equal instead of greater-than is correct. Fragments
657                // always have a size that is non-zero, so at least one byte must come from the
658                // first chunk identified by a compressed fragment.
659                if offset_within_chunk >= uncompressed_chunk_size {
660                    bail!("The Stream Directory contains an invalid fragment record. offset_within_chunk {offset_within_chunk} exceeds the size of the chunk.");
661                };
662
663                // We could go further and validate that the current fragment extends beyond a
664                // valid number of chunks. The stream reader code handles that, though.
665            } else {
666                // We could validate that the uncompressed fragment lies entirely within the MSFZ
667                // file, if we knew the length of the file. Unfortunately, ReadAt does not provide
668                // the length of the file, so we will not validate the fragment here. If the
669                // fragment is invalid it will cause a read failure within the StreamReader,
670                // which will be propagated to the application.
671            }
672
673            fragments.push(Fragment {
674                size: fragment_size,
675                location,
676            });
677
678            // Read the fragment size for the next fragment. A value of zero terminates the list,
679            // which is handled at the start of the while loop.
680            fragment_size = dec.u32()?;
681            if fragment_size == NIL_STREAM_SIZE {
682                bail!("Stream directory is malformed. It contains a non-initial fragment with size = NIL_STREAM_SIZE.");
683            }
684            // continue for more
685        }
686    }
687
688    stream_fragments.push(fragments.len() as u32);
689
690    fragments.shrink_to_fit();
691
692    Ok(DecodedStreamDir {
693        fragments,
694        stream_fragments,
695    })
696}
697
698struct Decoder<'a> {
699    bytes: &'a [u8],
700}
701
702impl<'a> Decoder<'a> {
703    fn next_n<const N: usize>(&mut self) -> anyhow::Result<&'a [u8; N]> {
704        if self.bytes.len() < N {
705            bail!("Buffer ran out of bytes");
706        }
707
708        let (lo, hi) = self.bytes.split_at(N);
709        self.bytes = hi;
710        // This unwrap() should never fail because we just tested the length, above.
711        // The optimizer should eliminate the unwrap() call.
712        Ok(<&[u8; N]>::try_from(lo).unwrap())
713    }
714
715    fn u32(&mut self) -> anyhow::Result<u32> {
716        Ok(u32::from_le_bytes(*self.next_n()?))
717    }
718}
719
720fn map_alloc_error<T>(result: Result<T, zerocopy::AllocError>) -> anyhow::Result<T> {
721    match result {
722        Ok(value) => Ok(value),
723        Err(zerocopy::AllocError) => {
724            Err(std::io::Error::from(std::io::ErrorKind::OutOfMemory).into())
725        }
726    }
727}