@@ -13,7 +13,9 @@ use std::io::Write;
1313use flate2:: read:: ZlibDecoder ;
1414
1515const DAT_FILE_FOOTER_BYTES : usize = 8 ;
16- const DAT_FILE_MIN_SIZE : usize = DAT_FILE_FOOTER_BYTES ;
16+ const DAT_FILE_MIN_SIZE : usize = DAT_FILE_FOOTER_BYTES + DATA_SECTION_TERMINATOR_LEN ;
17+
18+ const DATA_SECTION_TERMINATOR_LEN : usize = 1 ;
1719
1820const TREE_ENTRY_HEADER_SIZE : usize = 4 ;
1921const TREE_ENTRY_FOOTER_SIZE : usize = 13 ;
@@ -82,7 +84,7 @@ pub fn read_tree_entry(data: &[u8]) -> io::Result<(TreeEntry, usize)> {
8284 return Err ( err) ;
8385 }
8486
85- let filename = match str:: from_utf8 ( & data[ TREE_ENTRY_HEADER_SIZE ..filename_len] ) {
87+ let filename = match str:: from_utf8 ( & data[ TREE_ENTRY_HEADER_SIZE ..TREE_ENTRY_HEADER_SIZE + filename_len] ) {
8688 Ok ( s) => {
8789 let mut filename = PathBuf :: new ( ) ;
8890 for el in s. split ( TREE_ENTRY_PATH_SEPARATOR ) {
@@ -221,48 +223,53 @@ fn get_data_slice_for_entry<'a>(dat_data: &'a[u8], entry: &TreeEntry) -> io::Res
221223}
222224
223225fn extract_entry ( dat_data : & [ u8 ] , output_dir : & Path , entry : TreeEntry ) -> io:: Result < ( ) > {
224- let dat_data = get_data_slice_for_entry ( & dat_data, & entry) ?;
226+ let entry_data = get_data_slice_for_entry ( & dat_data, & entry) ?;
225227 let output_path = output_dir. join ( & entry. filename ) ;
226228
227229 if let Some ( parent) = output_path. parent ( ) {
228230 if !parent. exists ( ) {
229231 std:: fs:: create_dir_all ( parent) ?;
230- println ! ( "{}" , parent. to_str( ) . unwrap( ) ) ;
231232 }
232233 }
233234
234235 if output_path. exists ( ) {
235236 eprintln ! ( "{}: already exists: skipping" , output_path. to_str( ) . unwrap( ) ) ;
236237 } else {
237- write_entry ( & dat_data , & output_path, & entry) ?;
238+ write_entry ( & entry_data , & output_path, & entry) ?;
238239 }
239240
240241 Ok ( ( ) )
241242}
242243
243- fn write_entry ( dat_data : & [ u8 ] , output_path : & Path , entry : & TreeEntry ) -> io:: Result < ( ) > {
244- let mut output_file = std:: fs:: File :: create ( & output_path) ?;
245- println ! ( "{}" , output_path. to_str( ) . unwrap( ) ) ;
246-
244+ fn write_entry ( entry_data : & [ u8 ] , output_path : & Path , entry : & TreeEntry ) -> io:: Result < ( ) > {
247245 if entry. is_compressed {
248- write_compressed_entry ( & dat_data , output_file , & entry) ?;
246+ write_compressed_entry ( & entry_data , & output_path , & entry) ?;
249247 } else {
250- output_file. write ( dat_data) ?;
248+ let mut output_file = std:: fs:: File :: create ( & output_path) ?;
249+ output_file. write ( entry_data) ?;
251250 }
252251
253252 Ok ( ( ) )
254253}
255254
256- fn write_compressed_entry ( dat_data : & [ u8 ] , mut output_file : File , entry : & TreeEntry ) -> io:: Result < ( ) > {
255+ fn write_compressed_entry ( dat_data : & [ u8 ] , output_path : & Path , entry : & TreeEntry ) -> io:: Result < ( ) > {
256+ let mut output_file = std:: fs:: File :: create ( & output_path) ?;
257+
257258 if dat_data. len ( ) < 2 {
258- eprintln ! ( "{}: smaller than 2 bytes but marked as ' compressed' skipping decompression" , entry . filename . to_str( ) . unwrap( ) ) ;
259+ eprintln ! ( "{}: smaller than 2 bytes but marked as compressed: skipping decompression" , output_path . to_str( ) . unwrap( ) ) ;
259260 output_file. write ( dat_data) ?;
260261 } else if dat_data[ 0 ] != 0x78 || dat_data[ 1 ] != 0xda {
261- eprintln ! ( "{}: marked as compressed but no magic number: not decompressing " , entry . filename . to_str( ) . unwrap( ) ) ;
262+ eprintln ! ( "{}: marked as compressed but no magic number: skipping decompression " , output_path . to_str( ) . unwrap( ) ) ;
262263 output_file. write ( dat_data) ?;
263264 } else {
264265 let mut zlib_reader = ZlibDecoder :: new ( dat_data) ;
265266 std:: io:: copy ( & mut zlib_reader, & mut output_file) ?;
267+
268+ let decompressed_len = std:: fs:: metadata ( output_path) ?. len ( ) as usize ;
269+ if decompressed_len != entry. decompressed_size {
270+ eprintln ! ( "{}: decompressed size ({}) does not match expected decompressed size ({})" , output_path. to_str( ) . unwrap( ) , decompressed_len, entry. decompressed_size) ;
271+ }
266272 }
273+
267274 Ok ( ( ) )
268275}
0 commit comments