Skip to main content

ionic/ion/decoder/decode/
mod.rs

1use std::sync::Arc;
2
3#[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"))))]
4use crate::ion::decoder::utilities::byte_source::FileSource;
5use crate::{
6    accessions::{
7        FLOAT_16BIT, FLOAT_32BIT, FLOAT_64BIT, INT_16BIT, INT_32BIT, INT_64BIT, format_accession,
8    },
9    ion::{
10        ByteRange, IonError, IonResult, Range,
11        attr_meta::{
12            ACC_ATTR_DEFAULT_INSTRUMENT_CONFIGURATION_REF, ACC_ATTR_DEFAULT_SOURCE_FILE_REF,
13            ACC_ATTR_ID, ACC_ATTR_INSTRUMENT_CONFIGURATION_REF, ACC_ATTR_REF, ACC_ATTR_SAMPLE_REF,
14            ACC_ATTR_START_TIME_STAMP, AccessionTail, parse_accession_tail,
15        },
16        decoder::utilities::{
17            byte_source::{BytesSource, ReadBytes, SourceBytes},
18            common::decompress_zstd,
19        },
20        encoder::encode::{
21            CHROM_SUMMARY_SIZE, FILE_DTYPE_F16, FILE_DTYPE_F32, FILE_DTYPE_F64, FILE_DTYPE_I16,
22            FILE_DTYPE_I32, FILE_DTYPE_I64, SPEC_SUMMARY_SIZE,
23        },
24        filter_summary::{ChromatogramSummary, SpectrumSummary},
25        format::{CODEC_NONE, CODEC_ZSTD, FILE_TRAILER},
26        meta_groups::meta_directory_range,
27        packing::PackingId,
28        utilities::{
29            Header, MetaGroupReader,
30            block_reader::{
31                BlockReader, ContainerAccess, DefaultBlockProcessor, container_directory_range,
32            },
33            check_section_layout,
34            children_lookup::{ChildrenLookup, DefaultMetadataPolicy, OwnerRows},
35            common::get_attr_text,
36            decompression_limit::DecompressionLimit,
37            meta_column_layout::MetaColumnLayout,
38            parse_chromatogram, parse_chromatogram_list, parse_chromatogram_list_header,
39            parse_cv_and_user_params, parse_cv_list, parse_data_processing_list,
40            parse_file_description, parse_global_metadata::parse_global_metadata,
41            parse_instrument_list, parse_referenceable_param_group_list, parse_sample_list,
42            parse_scan_settings_list, parse_software_list, parse_spectrum, parse_spectrum_list,
43            parse_spectrum_list_header,
44            spectrum_source::{ScanSummary, TimeUnit, f16_bits_to_f64},
45            window_directory::WindowDirectory,
46        },
47    },
48    mzml::{schema::TagId, structs::*},
49};
50
51#[cfg(test)]
52pub(crate) const ACC_MZ: u32 = 1_000_514;
53pub(crate) const ACC_INT: u32 = 1_000_515;
54pub(crate) const INDEX_ENTRY_BYTES: usize = 16;
55pub(crate) const ARRAY_ADDRESS_BYTES: usize = 32;
56const DEFAULT_MAX_CACHED_BYTES: usize = 256 * 1024 * 1024;
57pub(crate) const INLINE_ARRAY_ADDRESS_CAP: usize = 8;
58
59fn check_crc(bytes: &[u8], expected: u32, name: &str) -> IonResult<()> {
60    let computed = crc32fast::hash(bytes);
61    if computed != expected {
62        return Err(IonError::from(format!("{name}: crc mismatch")));
63    }
64    Ok(())
65}
66
67fn max_address_index(entries_buf: &[u8]) -> u64 {
68    let mut total = 0u64;
69    for entry in entries_buf.as_chunks::<INDEX_ENTRY_BYTES>().0 {
70        let first = u64::from_le_bytes(entry[0..8].try_into().unwrap());
71        let count = u64::from_le_bytes(entry[8..16].try_into().unwrap());
72        total = total.max(first.saturating_add(count));
73    }
74    total
75}
76
77fn check_address_table(entries_buf: &[u8], table: &[u8], name: &str) -> IonResult<()> {
78    let expected = max_address_index(entries_buf)
79        .checked_mul(ARRAY_ADDRESS_BYTES as u64)
80        .ok_or_else(|| IonError::from(format!("{name}: address count overflows the table size")))?;
81    if table.len() as u64 != expected {
82        return Err(IonError::from(format!(
83            "{name}: record size is not {ARRAY_ADDRESS_BYTES} bytes (old format); re-encode the file from its mzML source with `ionic convert`"
84        )));
85    }
86    Ok(())
87}
88
89fn check_array_dtypes(address_bytes: &[u8], name: &'static str) -> IonResult<()> {
90    for record in address_bytes.as_chunks::<ARRAY_ADDRESS_BYTES>().0 {
91        crate::ion::packing::Dtype::from_byte(record[24]).map_err(|_| IonError::BadDtype {
92            dtype: record[24],
93            kind: name,
94        })?;
95    }
96    Ok(())
97}
98
99fn decompress_fixed_section(
100    stored: &[u8],
101    plain_len: usize,
102    budget: DecompressionLimit,
103    name: &str,
104) -> IonResult<Arc<[u8]>> {
105    let plain = decompress_zstd(stored, plain_len, budget)
106        .map_err(|e| IonError::from(format!("{name}: decompression failed: {e}")))?;
107    Ok(Arc::from(plain))
108}
109
110type FixedSectionBuffers = (Arc<[u8]>, Arc<[u8]>, Arc<[u8]>, Arc<[u8]>, Arc<[u8]>, Arc<[u8]>);
111
112struct FixedSections {
113    spec_summary: Arc<[u8]>,
114    spec_entries: Arc<[u8]>,
115    spec_array_addresses: Arc<[u8]>,
116    chrom_summary: Arc<[u8]>,
117    chrom_entries: Arc<[u8]>,
118    chrom_array_addresses: Arc<[u8]>,
119}
120
121fn check_integrity(header: &Header, source: &Arc<dyn ReadBytes>) -> IonResult<()> {
122    let layout_failures = check_section_layout(header);
123    if !layout_failures.is_empty() {
124        return Err(IonError::from(format!(
125            "header: file integrity validation failed ({} check(s)):\n{}",
126            layout_failures.len(),
127            layout_failures.join("\n")
128        )));
129    }
130    let trailer_length = FILE_TRAILER.len() as u64;
131    let trailer = source.read(ByteRange {
132        offset: header.total_file_size.saturating_sub(trailer_length),
133        length: trailer_length,
134    })?;
135    if trailer[..] != FILE_TRAILER {
136        return Err(IonError::from("header: missing or invalid file trailer"));
137    }
138    if let Some(actual_len) = source.total_len()
139        && actual_len != header.total_file_size
140    {
141        return Err(IonError::from(format!(
142            "header: total_file_size ({}) does not match source length ({actual_len})",
143            header.total_file_size
144        )));
145    }
146    Ok(())
147}
148
149fn read_fixed_sections(
150    header: &Header,
151    source: &Arc<dyn ReadBytes>,
152    options: &ReadOptions,
153) -> IonResult<FixedSections> {
154    let spec_summary_buf = source.read(ByteRange {
155        offset: header.off_spec_summary,
156        length: header.len_spec_summary,
157    })?;
158    let chrom_summary_buf = source.read(ByteRange {
159        offset: header.off_chrom_summary,
160        length: header.len_chrom_summary,
161    })?;
162    let spec_entries_buf = source.read(ByteRange {
163        offset: header.off_spec_entries,
164        length: header.len_spec_entries,
165    })?;
166    let spec_array_addresses = source.read(ByteRange {
167        offset: header.off_spec_array_addresses,
168        length: header.len_spec_array_addresses,
169    })?;
170    let chrom_entries_buf = source.read(ByteRange {
171        offset: header.off_chrom_entries,
172        length: header.len_chrom_entries,
173    })?;
174    let chrom_array_addresses = source.read(ByteRange {
175        offset: header.off_chrom_array_addresses,
176        length: header.len_chrom_array_addresses,
177    })?;
178
179    if options.verify_checksums {
180        check_crc(&spec_summary_buf, header.spec_summary_crc32, "spec_summary")?;
181        check_crc(&spec_entries_buf, header.spec_entries_crc32, "spec_entries")?;
182        check_crc(
183            &spec_array_addresses,
184            header.spec_array_addresses_crc32,
185            "spec_array_addresses",
186        )?;
187        check_crc(
188            &chrom_summary_buf,
189            header.chrom_summary_crc32,
190            "chrom_summary",
191        )?;
192        check_crc(
193            &chrom_entries_buf,
194            header.chrom_entries_crc32,
195            "chrom_entries",
196        )?;
197        check_crc(
198            &chrom_array_addresses,
199            header.chrom_array_addresses_crc32,
200            "chrom_array_addresses",
201        )?;
202    }
203
204    let (
205        spec_summary_plain,
206        spec_entries_plain,
207        spec_array_addresses_plain,
208        chrom_summary_plain,
209        chrom_entries_plain,
210        chrom_array_addresses_plain,
211    ): FixedSectionBuffers = if header.compression_codec == CODEC_ZSTD {
212        let spec_summary_plain = decompress_fixed_section(
213            &spec_summary_buf,
214            header.spectrum_count as usize * SPEC_SUMMARY_SIZE,
215            options.decompression_limit,
216            "spec_summary",
217        )?;
218        let spec_entries_plain = decompress_fixed_section(
219            &spec_entries_buf,
220            header.spectrum_count as usize * INDEX_ENTRY_BYTES,
221            options.decompression_limit,
222            "spec_entries",
223        )?;
224        let spec_array_addresses_plain = decompress_fixed_section(
225            &spec_array_addresses,
226            max_address_index(&spec_entries_plain) as usize * ARRAY_ADDRESS_BYTES,
227            options.decompression_limit,
228            "spec_array_addresses",
229        )?;
230        let chrom_summary_plain = decompress_fixed_section(
231            &chrom_summary_buf,
232            header.chrom_count as usize * CHROM_SUMMARY_SIZE,
233            options.decompression_limit,
234            "chrom_summary",
235        )?;
236        let chrom_entries_plain = decompress_fixed_section(
237            &chrom_entries_buf,
238            header.chrom_count as usize * INDEX_ENTRY_BYTES,
239            options.decompression_limit,
240            "chrom_entries",
241        )?;
242        let chrom_array_addresses_plain = decompress_fixed_section(
243            &chrom_array_addresses,
244            max_address_index(&chrom_entries_plain) as usize * ARRAY_ADDRESS_BYTES,
245            options.decompression_limit,
246            "chrom_array_addresses",
247        )?;
248        (
249            spec_summary_plain,
250            spec_entries_plain,
251            spec_array_addresses_plain,
252            chrom_summary_plain,
253            chrom_entries_plain,
254            chrom_array_addresses_plain,
255        )
256    } else {
257        (
258            spec_summary_buf.into_arc(),
259            spec_entries_buf.into_arc(),
260            spec_array_addresses.into_arc(),
261            chrom_summary_buf.into_arc(),
262            chrom_entries_buf.into_arc(),
263            chrom_array_addresses.into_arc(),
264        )
265    };
266
267    check_address_table(
268        &spec_entries_plain,
269        &spec_array_addresses_plain,
270        "spec_array_addresses",
271    )?;
272    check_address_table(
273        &chrom_entries_plain,
274        &chrom_array_addresses_plain,
275        "chrom_array_addresses",
276    )?;
277
278    check_array_dtypes(&spec_array_addresses_plain, "spec array dtype")?;
279    check_array_dtypes(&chrom_array_addresses_plain, "chrom array dtype")?;
280
281    Ok(FixedSections {
282        spec_summary: spec_summary_plain,
283        spec_entries: spec_entries_plain,
284        spec_array_addresses: spec_array_addresses_plain,
285        chrom_summary: chrom_summary_plain,
286        chrom_entries: chrom_entries_plain,
287        chrom_array_addresses: chrom_array_addresses_plain,
288    })
289}
290
291mod arrays;
292mod spectra;
293mod to_mzml;
294mod windows;
295
296pub(crate) use arrays::ArrayAddress;
297pub(crate) use arrays::{
298    ArrayGroup, address_read_params, dtype_stride, group_arrays, parse_array_address,
299    read_array_addresses_from_buffers, unfilter_array_bytes,
300};
301pub use to_mzml::{Metadatum, MetadatumValue};
302pub use windows::{DataXY, ScanQuery, Select, Window};
303#[cfg(test)]
304pub(crate) use windows::ItemKind;
305
306#[derive(Debug, Clone)]
307pub struct ReadOptions {
308    pub max_cached_bytes: usize,
309    pub verify_checksums: bool,
310    pub parallel: bool,
311    pub decompression_limit: DecompressionLimit,
312}
313
314impl Default for ReadOptions {
315    fn default() -> Self {
316        Self {
317            max_cached_bytes: DEFAULT_MAX_CACHED_BYTES,
318            verify_checksums: true,
319            parallel: true,
320            decompression_limit: DecompressionLimit::default(),
321        }
322    }
323}
324
325pub(crate) enum WindowDirectoryCache {
326    Unloaded,
327    Missing,
328    BadChecksum,
329    Malformed(String),
330    Loaded(WindowDirectory),
331}
332
333pub struct IonReader {
334    pub(crate) header: Header,
335    pub(crate) source: Arc<dyn ReadBytes>,
336    pub(crate) spec_window_directory: WindowDirectoryCache,
337    #[allow(dead_code)]
338    pub(crate) chrom_window_directory: WindowDirectoryCache,
339    pub(crate) spec_summary_buf: Arc<[u8]>,
340    pub(crate) chrom_summary_buf: Arc<[u8]>,
341    pub(crate) spec_entries_buf: Arc<[u8]>,
342    pub(crate) spec_array_addresses: Arc<[u8]>,
343    pub(crate) chrom_entries_buf: Arc<[u8]>,
344    pub(crate) chrom_array_addresses: Arc<[u8]>,
345    pub(crate) global_meta: std::cell::OnceCell<SourceBytes>,
346    pub(crate) spec_container: BlockReader<DefaultBlockProcessor>,
347    pub(crate) chrom_container: Option<BlockReader<DefaultBlockProcessor>>,
348    pub(crate) spec_meta: MetaGroupReader,
349    pub(crate) chrom_meta: MetaGroupReader,
350    pub(crate) options: ReadOptions,
351    pub(crate) spec_rt_finite_ascending: std::cell::Cell<Option<bool>>,
352}
353
354impl IonReader {
355    pub fn from_bytes(bytes: &[u8], options: &ReadOptions) -> IonResult<Self> {
356        let source = Arc::new(BytesSource::new(Arc::from(bytes))) as Arc<dyn ReadBytes>;
357        Self::new(source, options)
358    }
359
360    #[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"))))]
361    pub fn open(path: impl AsRef<std::path::Path>, options: &ReadOptions) -> IonResult<Self> {
362        let path = path.as_ref();
363        let file = std::fs::File::open(path).map_err(|e| IonError::from(e.to_string()))?;
364        let map = unsafe { memmap2::Mmap::map(&file).map_err(|e| IonError::from(e.to_string()))? };
365        let source = Arc::new(FileSource::new(map)) as Arc<dyn ReadBytes>;
366        Self::new(source, options)
367    }
368
369    pub fn new(source: Arc<dyn ReadBytes>, options: &ReadOptions) -> IonResult<Self> {
370        let header_buf = source.read(ByteRange {
371            offset: 0,
372            length: 1024,
373        })?;
374        let header = Header::parse(&header_buf)?;
375        let block_packing_id = PackingId::from_byte(header.default_array_filter)?;
376
377        if options.verify_checksums {
378            check_integrity(&header, &source)?;
379        }
380
381        let fixed = read_fixed_sections(&header, &source, options)?;
382        let meta_column_layout = MetaColumnLayout::new();
383        let spec_meta = MetaGroupReader::open(
384            source.clone(),
385            header.spec_meta_section(),
386            header.compression_codec,
387            options,
388            meta_column_layout,
389        )?;
390        let chrom_meta = MetaGroupReader::open(
391            source.clone(),
392            header.chrom_meta_section(),
393            header.compression_codec,
394            options,
395            meta_column_layout,
396        )?;
397
398        let spec_container = BlockReader::new(
399            source.clone(),
400            header.off_spec_container,
401            header.len_spec_container,
402            header.spec_block_count,
403            header.spec_directory_crc32,
404            header.compression_codec,
405            block_packing_id,
406            options.verify_checksums,
407            "spec",
408            DefaultBlockProcessor,
409            options.max_cached_bytes,
410            options.decompression_limit,
411        )?;
412
413        let chrom_container = if header.chrom_block_count > 0 && header.len_chrom_container > 0 {
414            Some(BlockReader::new(
415                source.clone(),
416                header.off_chrom_container,
417                header.len_chrom_container,
418                header.chrom_block_count,
419                header.chrom_directory_crc32,
420                header.compression_codec,
421                block_packing_id,
422                options.verify_checksums,
423                "chrom",
424                DefaultBlockProcessor,
425                options.max_cached_bytes,
426                options.decompression_limit,
427            )?)
428        } else {
429            None
430        };
431
432        Ok(Self {
433            header,
434            source,
435            spec_window_directory: WindowDirectoryCache::Unloaded,
436            chrom_window_directory: WindowDirectoryCache::Unloaded,
437            spec_summary_buf: fixed.spec_summary,
438            chrom_summary_buf: fixed.chrom_summary,
439            spec_entries_buf: fixed.spec_entries,
440            spec_array_addresses: fixed.spec_array_addresses,
441            chrom_entries_buf: fixed.chrom_entries,
442            chrom_array_addresses: fixed.chrom_array_addresses,
443            global_meta: std::cell::OnceCell::new(),
444            spec_container,
445            chrom_container,
446            spec_meta,
447            chrom_meta,
448            options: options.clone(),
449            spec_rt_finite_ascending: std::cell::Cell::new(None),
450        })
451    }
452
453    #[inline]
454    pub fn format_version(&self) -> u16 {
455        self.header.format_version
456    }
457
458    #[inline]
459    pub fn spectrum_count(&self) -> u64 {
460        self.header.spectrum_count
461    }
462
463    #[inline]
464    pub fn chromatogram_count(&self) -> u64 {
465        self.header.chrom_count
466    }
467
468    pub(crate) fn ensure_spec_window_directory(&mut self) -> IonResult<()> {
469        if matches!(self.spec_window_directory, WindowDirectoryCache::Unloaded) {
470            self.spec_window_directory = match self.load_spec_window_directory() {
471                Ok(index) => WindowDirectoryCache::Loaded(index),
472                Err(IonError::MissingSpectrumBounds) => WindowDirectoryCache::Missing,
473                Err(IonError::BadSpectrumBoundsChecksum) => WindowDirectoryCache::BadChecksum,
474                Err(IonError::MalformedSpectrumBounds(reason)) => {
475                    WindowDirectoryCache::Malformed(reason)
476                }
477                Err(other) => WindowDirectoryCache::Malformed(other.to_string()),
478            };
479        }
480        match &self.spec_window_directory {
481            WindowDirectoryCache::Loaded(_) => Ok(()),
482            WindowDirectoryCache::Missing => Err(IonError::MissingSpectrumBounds),
483            WindowDirectoryCache::BadChecksum => Err(IonError::BadSpectrumBoundsChecksum),
484            WindowDirectoryCache::Malformed(reason) => {
485                Err(IonError::MalformedSpectrumBounds(reason.clone()))
486            }
487            WindowDirectoryCache::Unloaded => Err(IonError::MissingSpectrumBounds),
488        }
489    }
490
491    #[cfg(test)]
492    pub(crate) fn ensure_chrom_window_directory(&mut self) {
493        if !matches!(self.chrom_window_directory, WindowDirectoryCache::Unloaded) {
494            return;
495        }
496        self.chrom_window_directory = match self.load_chrom_window_directory() {
497            Ok(index) => WindowDirectoryCache::Loaded(index),
498            Err(IonError::MissingChromatogramBounds) => WindowDirectoryCache::Missing,
499            Err(IonError::BadChromatogramBoundsChecksum) => WindowDirectoryCache::BadChecksum,
500            Err(IonError::MalformedChromatogramBounds(reason)) => {
501                WindowDirectoryCache::Malformed(reason)
502            }
503            Err(other) => WindowDirectoryCache::Malformed(other.to_string()),
504        };
505    }
506
507    fn load_spec_window_directory(&self) -> IonResult<WindowDirectory> {
508        if self.header.len_spec_window_directory == 0 {
509            return Err(IonError::MissingSpectrumBounds);
510        }
511        let spec_array_address_count =
512            self.spec_array_addresses.len() as u64 / ARRAY_ADDRESS_BYTES as u64;
513        let bytes = self
514            .source
515            .read(ByteRange {
516                offset: self.header.off_spec_window_directory,
517                length: self.header.len_spec_window_directory,
518            })
519            .map_err(|error| IonError::MalformedSpectrumBounds(format!("read failed: {error}")))?;
520        if crc32fast::hash(&bytes) != self.header.spec_window_directory_crc32 {
521            return Err(IonError::BadSpectrumBoundsChecksum);
522        }
523        let decompressed = self
524            .decompress_window_directory(
525                &bytes,
526                self.header.plain_len_spec_window_directory as usize,
527            )
528            .map_err(|error| {
529                IonError::MalformedSpectrumBounds(format!("decompression failed: {error}"))
530            })?;
531        WindowDirectory::from_bytes(
532            &decompressed,
533            spec_array_address_count,
534            self.header.spectrum_count,
535        )
536        .map_err(|error| IonError::MalformedSpectrumBounds(error.to_string()))
537    }
538
539    #[cfg(test)]
540    fn load_chrom_window_directory(&self) -> IonResult<WindowDirectory> {
541        if self.header.len_chrom_window_directory == 0 {
542            return Err(IonError::MissingChromatogramBounds);
543        }
544        let chrom_array_address_count =
545            self.chrom_array_addresses.len() as u64 / ARRAY_ADDRESS_BYTES as u64;
546        let bytes = self
547            .source
548            .read(ByteRange {
549                offset: self.header.off_chrom_window_directory,
550                length: self.header.len_chrom_window_directory,
551            })
552            .map_err(|error| {
553                IonError::MalformedChromatogramBounds(format!("read failed: {error}"))
554            })?;
555        if crc32fast::hash(&bytes) != self.header.chrom_window_directory_crc32 {
556            return Err(IonError::BadChromatogramBoundsChecksum);
557        }
558        let decompressed = self
559            .decompress_window_directory(
560                &bytes,
561                self.header.plain_len_chrom_window_directory as usize,
562            )
563            .map_err(|error| {
564                IonError::MalformedChromatogramBounds(format!("decompression failed: {error}"))
565            })?;
566        WindowDirectory::from_bytes(
567            &decompressed,
568            chrom_array_address_count,
569            self.header.chrom_count,
570        )
571        .map_err(|error| IonError::MalformedChromatogramBounds(error.to_string()))
572    }
573
574    fn decompress_window_directory(&self, bytes: &[u8], plain_len: usize) -> IonResult<Vec<u8>> {
575        match self.header.compression_codec {
576            CODEC_NONE => {
577                if bytes.len() != plain_len {
578                    return Err("window bounds: uncompressed length mismatch".into());
579                }
580                Ok(bytes.to_vec())
581            }
582            CODEC_ZSTD => decompress_zstd(bytes, plain_len, self.options.decompression_limit),
583            _ => Err("window bounds: unsupported codec".into()),
584        }
585    }
586}
587
588pub fn merge_ranges(ranges: &mut Vec<ByteRange>, gap: u64) {
589    ranges.retain(|range| range.length > 0);
590    ranges.sort_unstable_by_key(|range| (range.offset, range.length));
591    let mut kept = 0;
592    for index in 0..ranges.len() {
593        let range = ranges[index];
594        if kept == 0 {
595            ranges[0] = range;
596            kept = 1;
597            continue;
598        }
599        let last = ranges[kept - 1];
600        let last_end = last.offset.saturating_add(last.length);
601        let range_end = range.offset.saturating_add(range.length);
602        if range.offset <= last_end.saturating_add(gap) {
603            ranges[kept - 1].length = range_end.max(last_end) - last.offset;
604            continue;
605        }
606        ranges[kept] = range;
607        kept += 1;
608    }
609    ranges.truncate(kept);
610}
611
612pub fn header_ranges(header_bytes: &[u8]) -> IonResult<Vec<ByteRange>> {
613    if header_bytes.len() < 1024 {
614        return Err("header_ranges: needs at least 1024 header bytes".into());
615    }
616    let header = Header::parse(&header_bytes[..1024])?;
617    open_byte_ranges(&header)
618}
619
620pub(crate) fn open_byte_ranges(header: &Header) -> IonResult<Vec<ByteRange>> {
621    let mut ranges = vec![
622        ByteRange {
623            offset: 0,
624            length: 1024,
625        },
626        ByteRange {
627            offset: header.off_spec_summary,
628            length: header.len_spec_summary,
629        },
630        ByteRange {
631            offset: header.off_chrom_summary,
632            length: header.len_chrom_summary,
633        },
634        ByteRange {
635            offset: header.off_spec_entries,
636            length: header.len_spec_entries,
637        },
638        ByteRange {
639            offset: header.off_spec_array_addresses,
640            length: header.len_spec_array_addresses,
641        },
642        ByteRange {
643            offset: header.off_chrom_entries,
644            length: header.len_chrom_entries,
645        },
646        ByteRange {
647            offset: header.off_chrom_array_addresses,
648            length: header.len_chrom_array_addresses,
649        },
650        meta_directory_range(
651            ByteRange {
652                offset: header.off_spec_meta,
653                length: header.len_spec_meta,
654            },
655            header.spec_meta_group_count,
656        )?,
657        meta_directory_range(
658            ByteRange {
659                offset: header.off_chrom_meta,
660                length: header.len_chrom_meta,
661            },
662            header.chrom_meta_group_count,
663        )?,
664    ];
665
666    if header.len_spec_window_directory > 0 {
667        ranges.push(ByteRange {
668            offset: header.off_spec_window_directory,
669            length: header.len_spec_window_directory,
670        });
671    }
672    if header.len_chrom_window_directory > 0 {
673        ranges.push(ByteRange {
674            offset: header.off_chrom_window_directory,
675            length: header.len_chrom_window_directory,
676        });
677    }
678
679    let spec_blocks = usize::try_from(header.spec_block_count)
680        .map_err(|_| IonError::from("spec: block count too large for this platform"))?;
681    ranges.push(container_directory_range(
682        header.off_spec_container,
683        header.len_spec_container,
684        spec_blocks,
685        "spec",
686    )?);
687
688    if header.chrom_block_count > 0 && header.len_chrom_container > 0 {
689        let chrom_blocks = usize::try_from(header.chrom_block_count)
690            .map_err(|_| IonError::from("chrom: block count too large for this platform"))?;
691        ranges.push(container_directory_range(
692            header.off_chrom_container,
693            header.len_chrom_container,
694            chrom_blocks,
695            "chrom",
696        )?);
697    }
698
699    let trailer_length = FILE_TRAILER.len() as u64;
700    if header.total_file_size > trailer_length {
701        ranges.push(ByteRange {
702            offset: header.total_file_size - trailer_length,
703            length: trailer_length,
704        });
705    }
706
707    Ok(ranges)
708}
709
710#[cfg(test)]
711mod tests;