an efficient binary archive format
0
fork

Configure Feed

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

add footer magic for additional check when opening file

zach e42fb1fb af348272

+24 -13
+2 -1
SPEC.md
··· 51 51 | Field | Size | Type | Description | 52 52 | :--- | :--- | :--- | :--- | 53 53 | `index_offset` | 8 bytes | u64 | Absolute offset to the start of the index | 54 - | `entry_count` | 8 bytes | u64 | Total number of unique entries in the index | 54 + | `entry_count` | 4 bytes | u32 | Total number of unique entries in the index | 55 + | `magic` | 4 bytes | u32 | Magic sentinel number 55 56 56 57 --- 57 58
+2 -2
include/bindle.h
··· 108 108 109 109 bool bindle_writer_write(struct BindleWriter *stream, const uint8_t *data, size_t len); 110 110 111 - bool bindle_writer_finish(struct BindleWriter *stream); 111 + bool bindle_writer_close(struct BindleWriter *stream); 112 112 113 113 struct BindleReader *bindle_reader_new(const struct Bindle *ctx, const char *name); 114 114 115 115 ptrdiff_t bindle_reader_read(struct BindleReader *reader, uint8_t *buffer, size_t buffer_len); 116 116 117 - void bindle_reader_free(struct BindleReader *reader); 117 + void bindle_reader_close(struct BindleReader *reader); 118 118 119 119 #endif /* BINDLE_H */
+3 -4
src/ffi.rs
··· 338 338 } 339 339 340 340 #[unsafe(no_mangle)] 341 - pub unsafe extern "C" fn bindle_writer_finish(stream: *mut Writer) -> bool { 342 - // Reclaim memory from the Box and call finish() 341 + pub unsafe extern "C" fn bindle_writer_close(stream: *mut Writer) -> bool { 343 342 let s = unsafe { Box::from_raw(stream) }; 344 - s.finish().is_ok() 343 + s.close().is_ok() 345 344 } 346 345 347 346 #[unsafe(no_mangle)] ··· 382 381 } 383 382 384 383 #[unsafe(no_mangle)] 385 - pub unsafe extern "C" fn bindle_reader_free(reader: *mut Reader) { 384 + pub unsafe extern "C" fn bindle_reader_close(reader: *mut Reader) { 386 385 if !reader.is_null() { 387 386 unsafe { 388 387 drop(Box::from_raw(reader));
+17 -6
src/lib.rs
··· 15 15 const FOOTER_SIZE: usize = std::mem::size_of::<Footer>(); 16 16 const HEADER_SIZE: usize = 8; 17 17 const AUTO_COMPRESS_THRESHOLD: usize = 2048; 18 + const FOOTER_MAGIC: u32 = 0x98989898; 18 19 19 20 fn pad< 20 21 const SIZE: usize, ··· 84 85 #[derive(FromBytes, Unaligned, IntoBytes, Immutable, Debug)] 85 86 struct Footer { 86 87 pub index_offset: u64, 87 - pub entry_count: u64, 88 + pub entry_count: u32, 89 + pub magic: u32, 88 90 } 89 91 90 92 pub struct Bindle { ··· 159 161 Ok(()) 160 162 } 161 163 162 - pub fn finish(self) -> io::Result<()> { 164 + pub fn close(self) -> io::Result<()> { 163 165 let compression_type = if let Some(encoder) = self.encoder { 164 166 encoder.finish()?; 165 167 1 ··· 262 264 let footer_pos = m.len() - FOOTER_SIZE; 263 265 let footer = Footer::read_from_bytes(&m[footer_pos..]).unwrap(); 264 266 267 + if footer.magic != FOOTER_MAGIC { 268 + return Err(io::Error::new( 269 + io::ErrorKind::InvalidData, 270 + "Invalid footer, the file may be corrupt", 271 + )); 272 + } 273 + 265 274 let data_end = footer.index_offset; 266 275 let count = footer.entry_count; 267 276 let mut index = BTreeMap::new(); ··· 305 314 pub fn add(&mut self, name: &str, data: &[u8], compress: Compress) -> io::Result<()> { 306 315 let mut stream = self.writer(name, compress)?; 307 316 stream.write_all(data)?; 308 - stream.finish()?; 317 + stream.close()?; 309 318 Ok(()) 310 319 } 311 320 ··· 337 346 338 347 let footer = Footer { 339 348 index_offset: index_start, 340 - entry_count: self.index.len() as u64, 349 + entry_count: self.index.len() as u32, 350 + magic: FOOTER_MAGIC, 341 351 }; 342 352 self.file.write_all(footer.as_bytes())?; 343 353 self.file.flush()?; ··· 390 400 391 401 let footer = Footer { 392 402 index_offset: index_start, 393 - entry_count: self.index.len() as u64, 403 + entry_count: self.index.len() as u32, 404 + magic: FOOTER_MAGIC, 394 405 }; 395 406 new_file.write_all(footer.as_bytes())?; 396 407 new_file.sync_all()?; ··· 793 804 s.write_chunk(chunk2).unwrap(); 794 805 s.write_chunk(chunk3).unwrap(); 795 806 796 - s.finish().expect("Failed to finish stream"); 807 + s.close().expect("Failed to finish stream"); 797 808 b.save().expect("Failed to save"); 798 809 } 799 810