1use std::io::{self, Read, Write};
42
43pub const FOOTER_SIZE: usize = 76;
44pub const MAGIC: [u8; 8] = *b"ONELF\x00\x01\x00";
45pub const END_MAGIC: [u8; 8] = *b"FLENONE\x00";
46
47bitflags! {
48 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
49 pub struct Flags: u16 {
50 const HAS_DICT = 1 << 0;
51 const MEMFD_HINT = 1 << 1;
52 const SHARUN_COMPAT = 1 << 2;
53 const STORED = 1 << 3;
57 }
58}
59
60#[derive(Debug, Clone)]
61pub struct Footer {
62 pub format_version: u16,
64 pub flags: Flags,
66 pub manifest_offset: u64,
68 pub manifest_compressed: u64,
70 pub manifest_original: u64,
72 pub payload_offset: u64,
74 pub payload_size: u64,
76 pub dict_offset: u64,
78 pub dict_size: u32,
80 pub manifest_checksum: [u8; 4],
83}
84
85impl Footer {
86 pub fn is_stored(&self) -> bool {
89 self.flags.contains(Flags::STORED)
90 }
91
92 pub fn write_to<W: Write>(&self, w: &mut W) -> io::Result<()> {
93 w.write_all(&MAGIC)?; w.write_all(&self.format_version.to_le_bytes())?; w.write_all(&self.flags.bits().to_le_bytes())?; w.write_all(&self.manifest_offset.to_le_bytes())?; w.write_all(&self.manifest_compressed.to_le_bytes())?; w.write_all(&self.manifest_original.to_le_bytes())?; w.write_all(&self.payload_offset.to_le_bytes())?; w.write_all(&self.payload_size.to_le_bytes())?; w.write_all(&self.dict_offset.to_le_bytes())?; w.write_all(&self.dict_size.to_le_bytes())?; w.write_all(&self.manifest_checksum)?; w.write_all(&END_MAGIC)?; Ok(()) }
107
108 pub fn read_from<R: Read>(r: &mut R) -> io::Result<Self> {
109 let mut buf = [0u8; FOOTER_SIZE];
110 r.read_exact(&mut buf)?;
111 Self::from_bytes(&buf)
112 }
113
114 pub fn from_bytes(buf: &[u8; FOOTER_SIZE]) -> io::Result<Self> {
115 if &buf[0..8] != &MAGIC {
116 return Err(io::Error::new(
117 io::ErrorKind::InvalidData,
118 "invalid onelf magic",
119 ));
120 }
121 if &buf[68..76] != &END_MAGIC {
122 return Err(io::Error::new(
123 io::ErrorKind::InvalidData,
124 "invalid onelf end magic",
125 ));
126 }
127
128 let format_version = u16::from_le_bytes(buf[8..10].try_into().unwrap());
129 if format_version != 1 {
130 return Err(io::Error::new(
131 io::ErrorKind::InvalidData,
132 format!("unsupported format version: {}", format_version),
133 ));
134 }
135
136 let flags_raw = u16::from_le_bytes(buf[10..12].try_into().unwrap());
137 let flags = Flags::from_bits_retain(flags_raw);
138
139 Ok(Footer {
140 format_version,
141 flags,
142 manifest_offset: u64::from_le_bytes(buf[12..20].try_into().unwrap()),
143 manifest_compressed: u64::from_le_bytes(buf[20..28].try_into().unwrap()),
144 manifest_original: u64::from_le_bytes(buf[28..36].try_into().unwrap()),
145 payload_offset: u64::from_le_bytes(buf[36..44].try_into().unwrap()),
146 payload_size: u64::from_le_bytes(buf[44..52].try_into().unwrap()),
147 dict_offset: u64::from_le_bytes(buf[52..60].try_into().unwrap()),
148 dict_size: u32::from_le_bytes(buf[60..64].try_into().unwrap()),
149 manifest_checksum: buf[64..68].try_into().unwrap(),
150 })
151 }
152}
153
154#[cfg(test)]
155mod tests {
156 use super::*;
157
158 fn sample() -> Footer {
159 Footer {
160 format_version: 1,
161 flags: Flags::HAS_DICT | Flags::STORED,
162 manifest_offset: 0x1122,
163 manifest_compressed: 0x33,
164 manifest_original: 0x44,
165 payload_offset: 0x55,
166 payload_size: 0x66,
167 dict_offset: 0x77,
168 dict_size: 0x88,
169 manifest_checksum: [1, 2, 3, 4],
170 }
171 }
172
173 #[test]
174 fn footer_roundtrips() {
175 let f = sample();
176 let mut buf = Vec::new();
177 f.write_to(&mut buf).unwrap();
178 assert_eq!(buf.len(), FOOTER_SIZE);
179 let back = Footer::from_bytes(&buf.try_into().unwrap()).unwrap();
180 assert_eq!(back.format_version, f.format_version);
181 assert_eq!(back.flags, f.flags);
182 assert_eq!(back.manifest_offset, f.manifest_offset);
183 assert_eq!(back.dict_size, f.dict_size);
184 assert_eq!(back.manifest_checksum, f.manifest_checksum);
185 }
186
187 #[test]
188 fn malformed_footers_error_without_panic() {
189 let mut buf = Vec::new();
190 sample().write_to(&mut buf).unwrap();
191
192 let mut bad = buf.clone();
194 bad[0] ^= 0xff;
195 assert!(Footer::from_bytes(&bad.clone().try_into().unwrap()).is_err());
196
197 let mut bad = buf.clone();
199 bad[68] ^= 0xff;
200 assert!(Footer::from_bytes(&bad.clone().try_into().unwrap()).is_err());
201
202 let mut bad = buf.clone();
204 bad[8..10].copy_from_slice(&2u16.to_le_bytes());
205 assert!(Footer::from_bytes(&bad.try_into().unwrap()).is_err());
206 }
207}