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