Skip to content

Commit

Permalink
zip: Correctly look for and decode both zip32/64 EOCD record
Browse files Browse the repository at this point in the history
There will always be zip(32) EOCD but optinally a zip64 EOCD

Related to #586
  • Loading branch information
wader committed Feb 22, 2023
1 parent 9f7d50b commit 5228fdd
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 10 deletions.
Binary file added format/zip/testdata/zip64-offset-not-ffffffff.zip
Binary file not shown.
118 changes: 118 additions & 0 deletions format/zip/testdata/zip64-offset-not-ffffffff.zip.fqtest
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# from https://github.com/nih-at/libzip/blob/main/regress/zip64.zip
$ fq dv zip64-offset-not-ffffffff.zip
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|.{}: zip64-offset-not-ffffffff.zip (zip) 0x0-0xc5.7 (198)
| | | local_files[0:1]: 0x0-0x34.7 (53)
| | | [0]{}: local_file 0x0-0x34.7 (53)
0x00|50 4b 03 04 |PK.. | signature: raw bits (valid) 0x0-0x3.7 (4)
0x00| 2d 00 | -. | version_needed: 45 0x4-0x5.7 (2)
| | | flags{}: 0x6-0x7.7 (2)
0x00| 00 | . | unused0: 0 0x6-0x6 (0.1)
0x00| 00 | . | strong_encryption: false 0x6.1-0x6.1 (0.1)
0x00| 00 | . | compressed_patched_data: false 0x6.2-0x6.2 (0.1)
0x00| 00 | . | enhanced_deflation: false 0x6.3-0x6.3 (0.1)
0x00| 00 | . | data_descriptor: false 0x6.4-0x6.4 (0.1)
0x00| 00 | . | compression0: false 0x6.5-0x6.5 (0.1)
0x00| 00 | . | compression1: false 0x6.6-0x6.6 (0.1)
0x00| 00 | . | encrypted: false 0x6.7-0x6.7 (0.1)
0x00| 00 | . | reserved0: 0 0x7-0x7.1 (0.2)
0x00| 00 | . | mask_header_values: false 0x7.2-0x7.2 (0.1)
0x00| 00 | . | reserved1: false 0x7.3-0x7.3 (0.1)
0x00| 00 | . | language_encoding: false 0x7.4-0x7.4 (0.1)
0x00| 00 | . | unused1: 0 0x7.5-0x7.7 (0.3)
0x00| 00 00 | .. | compression_method: "none" (0) 0x8-0x9.7 (2)
| | | last_modification_date{}: 0xa-0xb.7 (2)
0x00| 4f | O | hours: 9 0xa-0xa.4 (0.5)
0x00| 4f 72 | Or | minutes: 59 0xa.5-0xb.2 (0.6)
0x00| 72 | r | seconds: 18 0xb.3-0xb.7 (0.5)
| | | last_modification_time{}: 0xc-0xd.7 (2)
0x00| 5b | [ | year: 45 0xc-0xc.6 (0.7)
0x00| 5b 40 | [@ | month: 10 0xc.7-0xd.2 (0.4)
0x00| 40 | @ | day: 0 0xd.3-0xd.7 (0.5)
0x00| 07 a1| ..| crc32_uncompressed: 0xddeaa107 0xe-0x11.7 (4)
0x10|ea dd |.. |
0x10| ff ff ff ff | .... | compressed_size: 4294967295 0x12-0x15.7 (4)
0x10| ff ff ff ff | .... | uncompressed_size: 4294967295 0x16-0x19.7 (4)
0x10| 01 00 | .. | file_name_length: 1 0x1a-0x1b.7 (2)
0x10| 14 00 | .. | extra_field_length: 20 0x1c-0x1d.7 (2)
0x10| 2d | - | file_name: "-" 0x1e-0x1e.7 (1)
| | | extra_fields[0:1]: 0x1f-0x32.7 (20)
| | | [0]{}: extra_field 0x1f-0x32.7 (20)
0x10| 01| .| header_id: 0x1 (ZIP64 extended information extra field) 0x1f-0x20.7 (2)
0x20|00 |. |
0x20| 10 00 | .. | data_size: 16 0x21-0x22.7 (2)
0x20| 02 00 00 00 00 00 00 00 | ........ | uncompressed_size: 2 0x23-0x2a.7 (8)
0x20| 02 00 00 00 00| .....| compressed_size: 2 0x2b-0x32.7 (8)
0x30|00 00 00 |... |
0x30| 61 0a | a. | uncompressed: raw bits 0x33-0x34.7 (2)
| | | central_directories[0:1]: 0x35-0x63.7 (47)
| | | [0]{}: central_directory 0x35-0x63.7 (47)
0x30| 50 4b 01 02 | PK.. | signature: raw bits (valid) 0x35-0x38.7 (4)
0x30| 1e 03 | .. | version_made_by: 798 0x39-0x3a.7 (2)
0x30| 2d 00 | -. | version_needed: 45 0x3b-0x3c.7 (2)
| | | flags{}: 0x3d-0x3e.7 (2)
0x30| 00 | . | unused0: 0 0x3d-0x3d (0.1)
0x30| 00 | . | strong_encryption: false 0x3d.1-0x3d.1 (0.1)
0x30| 00 | . | compressed_patched_data: false 0x3d.2-0x3d.2 (0.1)
0x30| 00 | . | enhanced_deflation: false 0x3d.3-0x3d.3 (0.1)
0x30| 00 | . | data_descriptor: false 0x3d.4-0x3d.4 (0.1)
0x30| 00 | . | compression0: false 0x3d.5-0x3d.5 (0.1)
0x30| 00 | . | compression1: false 0x3d.6-0x3d.6 (0.1)
0x30| 00 | . | encrypted: false 0x3d.7-0x3d.7 (0.1)
0x30| 00 | . | reserved0: 0 0x3e-0x3e.1 (0.2)
0x30| 00 | . | mask_header_values: false 0x3e.2-0x3e.2 (0.1)
0x30| 00 | . | reserved1: false 0x3e.3-0x3e.3 (0.1)
0x30| 00 | . | language_encoding: false 0x3e.4-0x3e.4 (0.1)
0x30| 00 | . | unused1: 0 0x3e.5-0x3e.7 (0.3)
0x30| 00| .| compression_method: "none" (0) 0x3f-0x40.7 (2)
0x40|00 |. |
| | | last_modification_date{}: 0x41-0x42.7 (2)
0x40| 4f | O | hours: 9 0x41-0x41.4 (0.5)
0x40| 4f 72 | Or | minutes: 59 0x41.5-0x42.2 (0.6)
0x40| 72 | r | seconds: 18 0x42.3-0x42.7 (0.5)
| | | last_modification_time{}: 0x43-0x44.7 (2)
0x40| 5b | [ | year: 45 0x43-0x43.6 (0.7)
0x40| 5b 40 | [@ | month: 10 0x43.7-0x44.2 (0.4)
0x40| 40 | @ | day: 0 0x44.3-0x44.7 (0.5)
0x40| 07 a1 ea dd | .... | crc32_uncompressed: 0xddeaa107 0x45-0x48.7 (4)
0x40| 02 00 00 00 | .... | compressed_size: 2 0x49-0x4c.7 (4)
0x40| 02 00 00| ...| uncompressed_size: 2 0x4d-0x50.7 (4)
0x50|00 |. |
0x50| 01 00 | .. | file_name_length: 1 0x51-0x52.7 (2)
0x50| 00 00 | .. | extra_field_length: 0 0x53-0x54.7 (2)
0x50| 00 00 | .. | file_comment_length: 0 0x55-0x56.7 (2)
0x50| 00 00 | .. | disk_number_where_file_starts: 0 0x57-0x58.7 (2)
0x50| 01 00 | .. | internal_file_attributes: 1 0x59-0x5a.7 (2)
0x50| 00 00 80 11 | .... | external_file_attributes: 293601280 0x5b-0x5e.7 (4)
0x50| 00| .| relative_offset_of_local_file_header: 0 0x5f-0x62.7 (4)
0x60|00 00 00 |... |
0x60| 2d | - | file_name: "-" 0x63-0x63.7 (1)
| | | extra_fields[0:0]: 0x64-NA (0)
| | | file_comment: "" 0x64-NA (0)
| | | end_of_central_directory_record_zip64{}: 0x64-0x9b.7 (56)
0x60| 50 4b 06 06 | PK.. | signature: raw bits (valid) 0x64-0x67.7 (4)
0x60| 2c 00 00 00 00 00 00 00| ,.......| size_of_end_of_central_directory: 44 0x68-0x6f.7 (8)
0x70|1e 03 |.. | version_made_by: 798 0x70-0x71.7 (2)
0x70| 2d 00 | -. | version_needed_to_extract: 45 0x72-0x73.7 (2)
0x70| 00 00 00 00 | .... | disk_nr: 0 0x74-0x77.7 (4)
0x70| 00 00 00 00 | .... | central_directory_start_disk_nr: 0 0x78-0x7b.7 (4)
0x70| 01 00 00 00| ....| nr_of_central_directory_records_on_disk: 1 0x7c-0x83.7 (8)
0x80|00 00 00 00 |.... |
0x80| 01 00 00 00 00 00 00 00 | ........ | nr_of_central_directory_records: 1 0x84-0x8b.7 (8)
0x80| 2f 00 00 00| /...| size_of_central_directory: 47 0x8c-0x93.7 (8)
0x90|00 00 00 00 |.... |
0x90| 35 00 00 00 00 00 00 00 | 5....... | offset_of_start_of_central_directory: 53 0x94-0x9b.7 (8)
| | | end_of_central_directory_locator{}: 0x9c-0xaf.7 (20)
0x90| 50 4b 06 07| PK..| signature: raw bits (valid) 0x9c-0x9f.7 (4)
0xa0|00 00 00 00 |.... | disk_nr: 0 0xa0-0xa3.7 (4)
0xa0| 64 00 00 00 00 00 00 00 | d....... | offset_of_end_of_central_directory_record: 100 0xa4-0xab.7 (8)
0xa0| 01 00 00 00| ....| total_disk_nr: 1 0xac-0xaf.7 (4)
| | | end_of_central_directory_record{}: 0xb0-0xc5.7 (22)
0xb0|50 4b 05 06 |PK.. | signature: raw bits (valid) 0xb0-0xb3.7 (4)
0xb0| 00 00 | .. | disk_nr: 0 0xb4-0xb5.7 (2)
0xb0| 00 00 | .. | central_directory_start_disk_nr: 0 0xb6-0xb7.7 (2)
0xb0| 01 00 | .. | nr_of_central_directory_records_on_disk: 1 0xb8-0xb9.7 (2)
0xb0| 01 00 | .. | nr_of_central_directory_records: 1 0xba-0xbb.7 (2)
0xb0| 2f 00 00 00| /...| size_of_central_directory: 47 0xbc-0xbf.7 (4)
0xc0|35 00 00 00 |5... | offset_of_start_of_central_directory: 53 0xc0-0xc3.7 (4)
0xc0| 00 00| | ..| | comment_length: 0 0xc4-0xc5.7 (2)
| | | comment: "" 0xc6-NA (0)
17 changes: 7 additions & 10 deletions format/zip/zip.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func zipDecode(d *decode.D) any {
if err != nil {
d.Fatalf("can't find end of central directory")
}
d.SeekAbs(d.Len() + p)
d.SeekRel(p)

var offsetCD uint64
var sizeCD uint64
Expand All @@ -176,15 +176,12 @@ func zipDecode(d *decode.D) any {
d.FieldUTF8("comment", int(commentLength))
})

// there is a end of central directory locator, is zip64
if offsetCD == 0xff_ff_ff_ff {
p, _, err := d.TryPeekFind(32, -8, 10000, func(v uint64) bool {
return v == uint64(endOfCentralDirectoryLocatorSignatureN)
})
if err != nil {
d.Fatalf("can't find zip64 end of central directory")
}
d.SeekAbs(d.Len() + p)
// is there a zip64 end of central directory locator?
p, _, err = d.TryPeekFind(32, -8, 10000, func(v uint64) bool {
return v == uint64(endOfCentralDirectoryLocatorSignatureN)
})
if err == nil {
d.SeekRel(p)

var offsetEOCD uint64
d.FieldStruct("end_of_central_directory_locator", func(d *decode.D) {
Expand Down

0 comments on commit 5228fdd

Please sign in to comment.