Skip to main content

onelf_format/
footer.rs

1//! Footer structure and serialization
2//!
3//! The footer is located at the end of every ONELF package and contains:
4//! - Magic bytes for identification
5//! - Format version
6//! - Offsets to manifest, payload, and optional dictionary
7//! - Checksums for integrity verification
8//!
9//! # Structure
10//!
11//! The footer is exactly 76 bytes and is organized as follows:
12//!
13//! ```text
14//! Offset  Size    Field
15//! ------  -------  -------------------
16//! 0      8        Magic: "ONELF\0\x01\x00"
17//! 8      2        Format version (u16)
18//! 10     2        Flags (u16)
19//! 12     8        Manifest offset (u64)
20//! 20     8        Manifest compressed size (u64)
21//! 28     8        Manifest original size (u64)
22//! 36     8        Payload offset (u64)
23//! 44     8        Payload total size (u64)
24//! 52     8        Dictionary offset (u64)
25//! 60     4        Dictionary size (u32)
26//! 64     4        Manifest checksum (xxh32)
27//! 68     8        End magic: "FLENONE\x00"
28//!//!
29//! # Example
30//!
31//! no_run
32//! use onelf_format::Footer;
33//!
34//! let footer = Footer {
35//!     format_version: 1,
36//!     // ... other fields
37//! };
38//!
39
40use std::io::{self, Read, Write};
41
42pub const FOOTER_SIZE: usize = 76;
43pub const MAGIC: [u8; 8] = *b"ONELF\x00\x01\x00";
44pub const END_MAGIC: [u8; 8] = *b"FLENONE\x00";
45
46bitflags! {
47    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
48    pub struct Flags: u16 {
49        const HAS_DICT       = 1 << 0;
50        const MEMFD_HINT     = 1 << 1;
51        const SHARUN_COMPAT  = 1 << 2;
52    }
53}
54
55#[derive(Debug, Clone)]
56pub struct Footer {
57    /// Format version number (currently 1).
58    pub format_version: u16,
59    /// Feature flags describing optional sections and capabilities.
60    pub flags: Flags,
61    /// Byte offset where the compressed manifest begins.
62    pub manifest_offset: u64,
63    /// Size of the manifest after compression.
64    pub manifest_compressed: u64,
65    /// Size of the manifest before compression.
66    pub manifest_original: u64,
67    /// Byte offset where the payload section begins.
68    pub payload_offset: u64,
69    /// Total size of the payload section in bytes.
70    pub payload_size: u64,
71    /// Byte offset of the zstd dictionary, or 0 if absent.
72    pub dict_offset: u64,
73    /// Size of the zstd dictionary in bytes, or 0 if absent.
74    pub dict_size: u32,
75    /// xxHash32 checksum of the compressed manifest for integrity verification.
76    pub manifest_checksum: [u8; 4],
77}
78
79impl Footer {
80    pub fn write_to<W: Write>(&self, w: &mut W) -> io::Result<()> {
81        w.write_all(&MAGIC)?; // 8
82        w.write_all(&self.format_version.to_le_bytes())?; // 2
83        w.write_all(&self.flags.bits().to_le_bytes())?; // 2
84        w.write_all(&self.manifest_offset.to_le_bytes())?; // 8
85        w.write_all(&self.manifest_compressed.to_le_bytes())?; // 8
86        w.write_all(&self.manifest_original.to_le_bytes())?; // 8
87        w.write_all(&self.payload_offset.to_le_bytes())?; // 8
88        w.write_all(&self.payload_size.to_le_bytes())?; // 8
89        w.write_all(&self.dict_offset.to_le_bytes())?; // 8
90        w.write_all(&self.dict_size.to_le_bytes())?; // 4
91        w.write_all(&self.manifest_checksum)?; // 4
92        w.write_all(&END_MAGIC)?; // 8
93        Ok(()) // = 76
94    }
95
96    pub fn read_from<R: Read>(r: &mut R) -> io::Result<Self> {
97        let mut buf = [0u8; FOOTER_SIZE];
98        r.read_exact(&mut buf)?;
99        Self::from_bytes(&buf)
100    }
101
102    pub fn from_bytes(buf: &[u8; FOOTER_SIZE]) -> io::Result<Self> {
103        if &buf[0..8] != &MAGIC {
104            return Err(io::Error::new(
105                io::ErrorKind::InvalidData,
106                "invalid onelf magic",
107            ));
108        }
109        if &buf[68..76] != &END_MAGIC {
110            return Err(io::Error::new(
111                io::ErrorKind::InvalidData,
112                "invalid onelf end magic",
113            ));
114        }
115
116        let format_version = u16::from_le_bytes(buf[8..10].try_into().unwrap());
117        if format_version != 1 {
118            return Err(io::Error::new(
119                io::ErrorKind::InvalidData,
120                format!("unsupported format version: {}", format_version),
121            ));
122        }
123
124        let flags_raw = u16::from_le_bytes(buf[10..12].try_into().unwrap());
125        let flags = Flags::from_bits_truncate(flags_raw);
126
127        Ok(Footer {
128            format_version,
129            flags,
130            manifest_offset: u64::from_le_bytes(buf[12..20].try_into().unwrap()),
131            manifest_compressed: u64::from_le_bytes(buf[20..28].try_into().unwrap()),
132            manifest_original: u64::from_le_bytes(buf[28..36].try_into().unwrap()),
133            payload_offset: u64::from_le_bytes(buf[36..44].try_into().unwrap()),
134            payload_size: u64::from_le_bytes(buf[44..52].try_into().unwrap()),
135            dict_offset: u64::from_le_bytes(buf[52..60].try_into().unwrap()),
136            dict_size: u32::from_le_bytes(buf[60..64].try_into().unwrap()),
137            manifest_checksum: buf[64..68].try_into().unwrap(),
138        })
139    }
140}