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