an efficient binary archive format
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

cleanup padding calculations

zach f426319f 8b3db366

+35 -16
+6 -3
build.rs
··· 7 7 // Instead of using .generate(), use the Builder for more control 8 8 let config = cbindgen::Config::from_file("cbindgen.toml").unwrap_or_default(); 9 9 10 - cbindgen::Builder::new() 10 + if let Ok(b) = cbindgen::Builder::new() 11 11 .with_crate(&crate_dir) 12 12 .with_config(config) 13 13 .generate() 14 - .expect("Unable to generate bindings") 15 - .write_to_file(PathBuf::from(crate_dir).join("include/bindle.h")); 14 + { 15 + b.write_to_file(PathBuf::from(crate_dir).join("include/bindle.h")); 16 + } else { 17 + eprintln!("WARNING: bindle.h not updated"); 18 + } 16 19 }
+29 -13
src/lib.rs
··· 13 13 const BNDL_ALIGN: usize = 8; 14 14 const ENTRY_SIZE: usize = std::mem::size_of::<Entry>(); 15 15 const FOOTER_SIZE: usize = std::mem::size_of::<Footer>(); 16 - const HEADER_SIZE: u64 = 8; 16 + const HEADER_SIZE: usize = 8; 17 + 18 + fn pad< 19 + const SIZE: usize, 20 + T: Copy + TryFrom<usize> + std::ops::Sub<T, Output = T> + std::ops::Rem<T, Output = T>, 21 + >( 22 + n: T, 23 + ) -> T 24 + where 25 + <T as std::ops::Sub>::Output: std::ops::Rem<T>, 26 + { 27 + if let Ok(size) = T::try_from(SIZE) { 28 + return (size - (n % size)) % size; 29 + } 30 + 31 + unreachable!() 32 + } 17 33 18 34 #[repr(C)] 19 35 #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord)] ··· 26 42 #[repr(C, packed)] 27 43 #[derive(FromBytes, Unaligned, IntoBytes, Immutable, Clone, Copy, Debug, Default)] 28 44 pub struct Entry { 29 - pub offset: [u8; 8], // Use [u8; 8] for disk stability 30 - pub compressed_size: [u8; 8], 31 - pub uncompressed_size: [u8; 8], 32 - pub crc32: [u8; 4], 33 - pub name_len: [u8; 2], 45 + pub offset: [u8; std::mem::size_of::<u64>()], // Use [u8; 8] for disk stability 46 + pub compressed_size: [u8; std::mem::size_of::<u64>()], 47 + pub uncompressed_size: [u8; std::mem::size_of::<u64>()], 48 + pub crc32: [u8; std::mem::size_of::<u32>()], 49 + pub name_len: [u8; std::mem::size_of::<u16>()], 34 50 pub compression_type: u8, 35 51 pub _reserved: u8, 36 52 } ··· 120 136 file, 121 137 mmap: None, 122 138 index: BTreeMap::new(), 123 - data_end: HEADER_SIZE, 139 + data_end: HEADER_SIZE as u64, 124 140 }); 125 141 } 126 142 ··· 170 186 171 187 let offset = self.data_end; 172 188 let c_size = processed.len() as u64; 173 - let pad = (8 - (c_size % 8)) % 8; 189 + let pad = pad::<8, u64>(c_size); 174 190 if pad > 0 { 175 191 self.file.write_all(&vec![0u8; pad as usize])?; 176 192 } ··· 198 214 for (name, entry) in &self.index { 199 215 self.file.write_all(entry.as_bytes())?; 200 216 self.file.write_all(name.as_bytes())?; 201 - let pad = (BNDL_ALIGN - ((ENTRY_SIZE + name.len()) % BNDL_ALIGN)) % BNDL_ALIGN; 217 + let pad = pad::<BNDL_ALIGN, usize>(ENTRY_SIZE + name.len()); // (BNDL_ALIGN - ((ENTRY_SIZE + name.len()) % BNDL_ALIGN)) % BNDL_ALIGN; 202 218 if pad > 0 { 203 219 self.file.write_all(&vec![0u8; pad])?; 204 220 } ··· 227 243 .open(&tmp_path)?; 228 244 229 245 new_file.write_all(BNDL_MAGIC)?; 230 - let mut current_offset = HEADER_SIZE; 246 + let mut current_offset = HEADER_SIZE as u64; 231 247 232 248 // Copy only live entries to the new file 233 249 for entry in self.index.values_mut() { ··· 235 251 self.file.seek(SeekFrom::Start(entry.offset()))?; 236 252 self.file.read_exact(&mut buf)?; 237 253 238 - new_file.seek(SeekFrom::Start(current_offset))?; 254 + new_file.seek(SeekFrom::Start(current_offset as u64))?; 239 255 new_file.write_all(&buf)?; 240 256 241 257 entry.offset = current_offset.to_le_bytes(); 242 - let pad = (8 - (entry.compressed_size() % 8)) % 8; 258 + let pad = pad::<8, u64>(entry.compressed_size()); 243 259 if pad > 0 { 244 260 new_file.write_all(&vec![0u8; pad as usize])?; 245 261 } ··· 251 267 for (name, entry) in &self.index { 252 268 new_file.write_all(entry.as_bytes())?; 253 269 new_file.write_all(name.as_bytes())?; 254 - let pad = (BNDL_ALIGN - ((ENTRY_SIZE + name.len()) % BNDL_ALIGN)) % BNDL_ALIGN; 270 + let pad = pad::<BNDL_ALIGN, usize>(ENTRY_SIZE + name.len()); 255 271 if pad > 0 { 256 272 new_file.write_all(&vec![0u8; pad])?; 257 273 }