ifc_lite_cache/
format.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4
5//! Format constants and the 64-byte header, mirroring
6//! `packages/cache/src/types.ts` and `sections/header.ts`.
7//!
8//! Every value here is read by the TS reader, so each one names the TS
9//! constant it must equal. The cross-language conformance gate
10//! (`scripts/cache-native-conformance.mjs`) reads the native bytes back with
11//! that reader, so a drifted constant fails there, not in the browser.
12
13use crate::buf::Buf;
14
15/// `MAGIC`: "IFCL" little-endian.
16pub(crate) const MAGIC: u32 = 0x4C43_4649;
17/// `FORMAT_VERSION`.
18pub const FORMAT_VERSION: u16 = 28;
19/// `HEADER_SIZE`.
20pub(crate) const HEADER_SIZE: usize = 64;
21/// `SECTION_ENTRY_SIZE`.
22pub(crate) const SECTION_ENTRY_SIZE: usize = 16;
23/// Bytes the header fields occupy before the zero padding (`header.ts`).
24const HEADER_FIELDS: usize = 31;
25
26/// `GEOMETRY_CHUNK_CELL_SIZE`.
27pub(crate) const GEOMETRY_CHUNK_CELL_SIZE: f64 = 32.0;
28/// `GEOMETRY_CHUNK_SOFT_BYTES`.
29pub(crate) const GEOMETRY_CHUNK_SOFT_BYTES: usize = 8 * 1024 * 1024;
30/// `GEOMETRY_CHUNK_COMPRESS_MIN_BYTES`.
31pub(crate) const GEOMETRY_CHUNK_COMPRESS_MIN_BYTES: usize = 64 * 1024;
32/// `GeometryChunkFlags.DeflateRaw`.
33pub(crate) const CHUNK_FLAG_DEFLATE_RAW: u32 = 1;
34
35/// The bounds a build is held to: [`Limits::FORMAT`] on every shipped path.
36/// Tests shrink `max_bytes` to exercise the u32 refusal without writing
37/// 4 GiB, and inject a mid-geometry failure.
38#[derive(Clone, Copy, Debug)]
39pub(crate) struct Limits {
40    /// The largest file the u32 section and chunk offsets and lengths can
41    /// address.
42    pub(crate) max_bytes: u64,
43    /// Fail on appending this (0-based) chunk, as a write error would.
44    pub(crate) fail_at_chunk: Option<usize>,
45}
46
47impl Limits {
48    pub(crate) const FORMAT: Limits = Limits { max_bytes: u32::MAX as u64, fail_at_chunk: None };
49}
50
51/// `SectionType`.
52#[derive(Clone, Copy, Debug, PartialEq, Eq)]
53#[repr(u16)]
54pub(crate) enum SectionType {
55    Strings = 1,
56    Entities = 2,
57    Properties = 3,
58    Quantities = 4,
59    Relationships = 5,
60    Geometry = 6,
61    EntityIndex = 9,
62}
63
64/// `HeaderFlags`. `HasSpatial` (bit 2) is never set, exactly as the TS
65/// writer: no Spatial section exists to back it.
66pub(crate) mod header_flags {
67    /// `HeaderFlags.HasGeometry`.
68    pub(crate) const HAS_GEOMETRY: u16 = 1 << 1;
69    /// `HeaderFlags.SourceHashUnset`: bit 3. The fork prototype had it at bit
70    /// 4, so the TS reader took a deliberately zeroed hash for a real one and
71    /// `read({ sourceBuffer })` refused a valid cache.
72    pub(crate) const SOURCE_HASH_UNSET: u16 = 1 << 3;
73}
74
75/// `SchemaVersion`: IFC2X3 = 0, IFC4 = 1, IFC4X3 = 2. Anything else (IFC5)
76/// takes the IFC2X3 slot, the documented fallback of `toCacheDataStore`.
77pub(crate) fn schema_byte(schema: &str) -> u8 {
78    match schema {
79        "IFC4" => 1,
80        "IFC4X3" => 2,
81        _ => 0,
82    }
83}
84
85pub(crate) struct Header {
86    pub(crate) flags: u16,
87    pub(crate) source_hash: u64,
88    pub(crate) schema: u8,
89    pub(crate) entity_count: u32,
90    pub(crate) total_vertices: u32,
91    pub(crate) total_triangles: u32,
92}
93
94pub(crate) struct SectionEntry {
95    pub(crate) section_type: SectionType,
96    pub(crate) offset: u32,
97    pub(crate) size: u32,
98}
99
100/// `writeHeader`: the header, zero padding to 64 bytes, then the section
101/// table (flags and compressedSize are always 0: no section is compressed).
102pub(crate) fn write_header(buf: &mut Buf, header: &Header, sections: &[SectionEntry]) {
103    buf.u32(MAGIC);
104    buf.u16(FORMAT_VERSION);
105    buf.u16(header.flags);
106    buf.u64(header.source_hash);
107    buf.u8(header.schema);
108    buf.u32(header.entity_count);
109    buf.u32(header.total_vertices);
110    buf.u32(header.total_triangles);
111    buf.u16(sections.len() as u16);
112    for _ in HEADER_FIELDS..HEADER_SIZE {
113        buf.u8(0);
114    }
115    for section in sections {
116        buf.u16(section.section_type as u16);
117        buf.u16(0);
118        buf.u32(section.offset);
119        buf.u32(section.size);
120        buf.u32(0);
121    }
122}