Skip to content

Commit

Permalink
Add support for 0x564B KV-Pair extra field
Browse files Browse the repository at this point in the history
  • Loading branch information
pmqs committed Dec 29, 2023
1 parent d3ba7af commit 9b6a0c7
Show file tree
Hide file tree
Showing 9 changed files with 355 additions and 3 deletions.
8 changes: 7 additions & 1 deletion MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -962,4 +962,10 @@ t/files/0023-0x756e-ASi-Unix/test.zip
t/files/0024-lpaq8/readme.md
t/files/0024-lpaq8/stdout
t/files/0024-lpaq8/stdout-v
t/files/0024-lpaq8/test.zip
t/files/0024-lpaq8/test.zip
t/files/0025-0x564B-KV-Pair/bad-signature/stdout-v
t/files/0025-0x564B-KV-Pair/bad-signature/stdout
t/files/0025-0x564B-KV-Pair/bad-signature/byte.tif.zip
t/files/0025-0x564B-KV-Pair/valid/stdout-v
t/files/0025-0x564B-KV-Pair/valid/stdout
t/files/0025-0x564B-KV-Pair/valid/byte.tif.zip
56 changes: 55 additions & 1 deletion bin/zipdetails
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ my %Extras = (
0x5356, ['AOS/VS (binary ACL) [VS]', undef],
0x5455, ['Extended Timestamp [UT]', \&decode_UT, 1, 13, 1, 13],
0x554e, ['Xceed unicode extra field [UN]', \&decode_Xceed_unicode, 6, undef, 8, undef],
0x564B, ['Key-Value Pairs [KV]', undef], # TODO -- https://github.com/sozip/keyvaluepairs-spec/blob/master/zip_keyvalue_extra_field_specification.md
0x564B, ['Key-Value Pairs [KV]', \&decode_Key_Value_Pair, 13, undef, 13, undef],# TODO -- https://github.com/sozip/keyvaluepairs-spec/blob/master/zip_keyvalue_extra_field_specification.md
0x5855, ['Unix Extra type 1 [UX]', \&decode_UX, 12, 12, 8, 8],
0x5a4c, ['ZipArchive Unicode Filename [LZ]', undef], # https://www.artpol-software.com/ZipArchive
0x5a4d, ['ZipArchive Offsets Array [MZ]', undef], # https://www.artpol-software.com/ZipArchive
Expand Down Expand Up @@ -5112,6 +5112,60 @@ sub decode_Xceed_unicode
}
}

sub decode_Key_Value_Pair
{
# 0x564B 'KV'
# https://github.com/sozip/keyvaluepairs-spec/blob/master/zip_keyvalue_extra_field_specification.md

my $extraID = shift ;
my $len = shift;
my $entry = shift;

my $remaining = $len;

myRead(my $signature, 13);
$remaining -= 13;

if ($signature ne 'KeyValuePairs')
{
error $FH->tell() - 13, extraFieldIdentifier($extraID) . ": 'Signature' field not 'KeyValuePairs'" ;
myRead(my $payload, $remaining);
my $data = hexDump16($signature . $payload);

out2 $signature . $payload, "Extra Payload", $data;

return ;
}

out $signature, ' Signature', "'KeyValuePairs'";
my $kvPairs = out_C " KV Count";
$remaining -= 1;

for my $index (1 .. $kvPairs)
{
my $key;
my $klen = out_v " Key size #$index";
$remaining -= 4;

myRead($key, $klen);
outputFilename $key, 1, " Key #$index";
$remaining -= $klen;

my $value;
my $vlen = out_v " Value size #$index";
$remaining -= 4;

myRead($value, $vlen);
outputFilename $value, 1, " Value #$index";
$remaining -= $vlen;
}

# TODO check that
# * count of kv pairs is accurate
# * no truncation in middle of kv data
# * no trailing data
}

sub decode_NT_security
{
# IZ Appnote
Expand Down
2 changes: 1 addition & 1 deletion t/002-main.t
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use Fcntl qw(SEEK_SET);

my $tests_per_zip = 6 ;
my $tests_per_zip_full = $tests_per_zip * 2 * 3 * 2 ;
plan tests => 210 * $tests_per_zip_full ;
plan tests => 212 * $tests_per_zip_full ;

sub run;
sub compareWithGolden;
Expand Down
Binary file not shown.
66 changes: 66 additions & 0 deletions t/files/0025-0x564B-KV-Pair/bad-signature/stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@

0000 LOCAL HEADER #1 04034B50 (67324752)
0004 Extract Zip Spec 14 (20) '2.0'
0005 Extract OS 00 (0) 'MS-DOS'
0006 General Purpose Flag 0000 (0)
[Bits 1-2] 0 'Normal Compression'
0008 Compression Method 0008 (8) 'Deflated'
000A Last Mod Date/Time 5792884C (1469220940) 'Mon Dec 18 17:02:24 2023'
000E CRC 0C634711 (207832849)
0012 Compressed Size 000001B7 (439)
0016 Uncompressed Size 000002F6 (758)
001A Filename Length 0008 (8)
001C Extra Length 002C (44)
001E Filename 'byte.tif'
0026 Extra ID #1 564B (22091) 'Key-Value Pairs [KV]'
0028 Length 0028 (40)
#
# ERROR: Offset 0x2A: Extra Field 'Key-Value Pairs [KV]' (ID 0x564B): 'Signature' field not 'KeyValuePairs'
#
002A Extra Payload 4B 65 79 56 61 6C 75 65 50 6F 6F 72 73 01 0C 00
43 6F 6E 74 65 6E 74 2D 54 79 70 65 0A 00 69 6D
61 67 65 2F 74 69 66 66
0052 PAYLOAD

0209 CENTRAL HEADER #1 02014B50 (33639248)
020D Created Zip Spec 00 (0) '0.0'
020E Created OS 00 (0) 'MS-DOS'
020F Extract Zip Spec 14 (20) '2.0'
0210 Extract OS 00 (0) 'MS-DOS'
0211 General Purpose Flag 0000 (0)
[Bits 1-2] 0 'Normal Compression'
0213 Compression Method 0008 (8) 'Deflated'
0215 Last Mod Date/Time 5792884C (1469220940) 'Mon Dec 18 17:02:24 2023'
0219 CRC 0C634711 (207832849)
021D Compressed Size 000001B7 (439)
0221 Uncompressed Size 000002F6 (758)
0225 Filename Length 0008 (8)
0227 Extra Length 002C (44)
0229 Comment Length 0000 (0)
022B Disk Start 0000 (0)
022D Int File Attributes 0000 (0)
[Bit 0] 0 'Binary Data'
022F Ext File Attributes 00000000 (0)
0233 Local Header Offset 00000000 (0)
0237 Filename 'byte.tif'
023F Extra ID #1 564B (22091) 'Key-Value Pairs [KV]'
0241 Length 0028 (40)
#
# ERROR: Offset 0x243: Extra Field 'Key-Value Pairs [KV]' (ID 0x564B): 'Signature' field not 'KeyValuePairs'
#
0243 Extra Payload 4B 65 79 56 61 6C 75 65 50 6F 6F 72 73 01 0C 00
43 6F 6E 74 65 6E 74 2D 54 79 70 65 0A 00 69 6D
61 67 65 2F 74 69 66 66

026B END CENTRAL HEADER 06054B50 (101010256)
026F Number of this disk 0000 (0)
0271 Central Dir Disk no 0000 (0)
0273 Entries in this disk 0001 (1)
0275 Total Entries 0001 (1)
0277 Size of Central Dir 00000062 (98)
027B Offset to Central Dir 00000209 (521)
027F Comment Length 0000 (0)
#
# Error Count: 2
#
# Done
82 changes: 82 additions & 0 deletions t/files/0025-0x564B-KV-Pair/bad-signature/stdout-v
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@

0000 0003 0004 50 4B 03 04 LOCAL HEADER #1 04034B50 (67324752)
0004 0004 0001 14 Extract Zip Spec 14 (20) '2.0'
0005 0005 0001 00 Extract OS 00 (0) 'MS-DOS'
0006 0007 0002 00 00 General Purpose Flag 0000 (0)
[Bits 1-2] 0 'Normal Compression'
0008 0009 0002 08 00 Compression Method 0008 (8) 'Deflated'
000A 000D 0004 4C 88 92 57 Last Mod Date/Time 5792884C (1469220940) 'Mon Dec 18 17:02:24 2023'
000E 0011 0004 11 47 63 0C CRC 0C634711 (207832849)
0012 0015 0004 B7 01 00 00 Compressed Size 000001B7 (439)
0016 0019 0004 F6 02 00 00 Uncompressed Size 000002F6 (758)
001A 001B 0002 08 00 Filename Length 0008 (8)
001C 001D 0002 2C 00 Extra Length 002C (44)
001E 0025 0008 62 79 74 65 Filename 'byte.tif'
2E 74 69 66
0026 0027 0002 4B 56 Extra ID #1 564B (22091) 'Key-Value Pairs [KV]'
0028 0029 0002 28 00 Length 0028 (40)
#
# ERROR: Offset 0x2A: Extra Field 'Key-Value Pairs [KV]' (ID 0x564B): 'Signature' field not 'KeyValuePairs'
#
002A 0051 0028 4B 65 79 56 Extra Payload 4B 65 79 56 61 6C 75 65 50 6F 6F 72 73 01 0C 00
61 6C 75 65 43 6F 6E 74 65 6E 74 2D 54 79 70 65 0A 00 69 6D
50 6F 6F 72 61 67 65 2F 74 69 66 66
73 01 0C 00
43 6F 6E 74
65 6E 74 2D
54 79 70 65
0A 00 69 6D
61 67 65 2F
74 69 66 66
0052 0208 01B7 ... PAYLOAD

0209 020C 0004 50 4B 01 02 CENTRAL HEADER #1 02014B50 (33639248)
020D 020D 0001 00 Created Zip Spec 00 (0) '0.0'
020E 020E 0001 00 Created OS 00 (0) 'MS-DOS'
020F 020F 0001 14 Extract Zip Spec 14 (20) '2.0'
0210 0210 0001 00 Extract OS 00 (0) 'MS-DOS'
0211 0212 0002 00 00 General Purpose Flag 0000 (0)
[Bits 1-2] 0 'Normal Compression'
0213 0214 0002 08 00 Compression Method 0008 (8) 'Deflated'
0215 0218 0004 4C 88 92 57 Last Mod Date/Time 5792884C (1469220940) 'Mon Dec 18 17:02:24 2023'
0219 021C 0004 11 47 63 0C CRC 0C634711 (207832849)
021D 0220 0004 B7 01 00 00 Compressed Size 000001B7 (439)
0221 0224 0004 F6 02 00 00 Uncompressed Size 000002F6 (758)
0225 0226 0002 08 00 Filename Length 0008 (8)
0227 0228 0002 2C 00 Extra Length 002C (44)
0229 022A 0002 00 00 Comment Length 0000 (0)
022B 022C 0002 00 00 Disk Start 0000 (0)
022D 022E 0002 00 00 Int File Attributes 0000 (0)
[Bit 0] 0 'Binary Data'
022F 0232 0004 00 00 00 00 Ext File Attributes 00000000 (0)
0233 0236 0004 00 00 00 00 Local Header Offset 00000000 (0)
0237 023E 0008 62 79 74 65 Filename 'byte.tif'
2E 74 69 66
023F 0240 0002 4B 56 Extra ID #1 564B (22091) 'Key-Value Pairs [KV]'
0241 0242 0002 28 00 Length 0028 (40)
#
# ERROR: Offset 0x243: Extra Field 'Key-Value Pairs [KV]' (ID 0x564B): 'Signature' field not 'KeyValuePairs'
#
0243 026A 0028 4B 65 79 56 Extra Payload 4B 65 79 56 61 6C 75 65 50 6F 6F 72 73 01 0C 00
61 6C 75 65 43 6F 6E 74 65 6E 74 2D 54 79 70 65 0A 00 69 6D
50 6F 6F 72 61 67 65 2F 74 69 66 66
73 01 0C 00
43 6F 6E 74
65 6E 74 2D
54 79 70 65
0A 00 69 6D
61 67 65 2F
74 69 66 66

026B 026E 0004 50 4B 05 06 END CENTRAL HEADER 06054B50 (101010256)
026F 0270 0002 00 00 Number of this disk 0000 (0)
0271 0272 0002 00 00 Central Dir Disk no 0000 (0)
0273 0274 0002 01 00 Entries in this disk 0001 (1)
0275 0276 0002 01 00 Total Entries 0001 (1)
0277 027A 0004 62 00 00 00 Size of Central Dir 00000062 (98)
027B 027E 0004 09 02 00 00 Offset to Central Dir 00000209 (521)
027F 0280 0002 00 00 Comment Length 0000 (0)
#
# Error Count: 2
#
# Done
Binary file added t/files/0025-0x564B-KV-Pair/valid/byte.tif.zip
Binary file not shown.
64 changes: 64 additions & 0 deletions t/files/0025-0x564B-KV-Pair/valid/stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@

0000 LOCAL HEADER #1 04034B50 (67324752)
0004 Extract Zip Spec 14 (20) '2.0'
0005 Extract OS 00 (0) 'MS-DOS'
0006 General Purpose Flag 0000 (0)
[Bits 1-2] 0 'Normal Compression'
0008 Compression Method 0008 (8) 'Deflated'
000A Last Mod Date/Time 5792884C (1469220940) 'Mon Dec 18 17:02:24 2023'
000E CRC 0C634711 (207832849)
0012 Compressed Size 000001B7 (439)
0016 Uncompressed Size 000002F6 (758)
001A Filename Length 0008 (8)
001C Extra Length 002C (44)
001E Filename 'byte.tif'
0026 Extra ID #1 564B (22091) 'Key-Value Pairs [KV]'
0028 Length 0028 (40)
002A Signature 'KeyValuePairs'
0037 KV Count 01 (1)
0038 Key size #1 000C (12)
003A Key #1 'Content-Type'
0046 Value size #1 000A (10)
0048 Value #1 'image/tiff'
0052 PAYLOAD

0209 CENTRAL HEADER #1 02014B50 (33639248)
020D Created Zip Spec 00 (0) '0.0'
020E Created OS 00 (0) 'MS-DOS'
020F Extract Zip Spec 14 (20) '2.0'
0210 Extract OS 00 (0) 'MS-DOS'
0211 General Purpose Flag 0000 (0)
[Bits 1-2] 0 'Normal Compression'
0213 Compression Method 0008 (8) 'Deflated'
0215 Last Mod Date/Time 5792884C (1469220940) 'Mon Dec 18 17:02:24 2023'
0219 CRC 0C634711 (207832849)
021D Compressed Size 000001B7 (439)
0221 Uncompressed Size 000002F6 (758)
0225 Filename Length 0008 (8)
0227 Extra Length 002C (44)
0229 Comment Length 0000 (0)
022B Disk Start 0000 (0)
022D Int File Attributes 0000 (0)
[Bit 0] 0 'Binary Data'
022F Ext File Attributes 00000000 (0)
0233 Local Header Offset 00000000 (0)
0237 Filename 'byte.tif'
023F Extra ID #1 564B (22091) 'Key-Value Pairs [KV]'
0241 Length 0028 (40)
0243 Signature 'KeyValuePairs'
0250 KV Count 01 (1)
0251 Key size #1 000C (12)
0253 Key #1 'Content-Type'
025F Value size #1 000A (10)
0261 Value #1 'image/tiff'

026B END CENTRAL HEADER 06054B50 (101010256)
026F Number of this disk 0000 (0)
0271 Central Dir Disk no 0000 (0)
0273 Entries in this disk 0001 (1)
0275 Total Entries 0001 (1)
0277 Size of Central Dir 00000062 (98)
027B Offset to Central Dir 00000209 (521)
027F Comment Length 0000 (0)
#
# Done
80 changes: 80 additions & 0 deletions t/files/0025-0x564B-KV-Pair/valid/stdout-v
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@

0000 0003 0004 50 4B 03 04 LOCAL HEADER #1 04034B50 (67324752)
0004 0004 0001 14 Extract Zip Spec 14 (20) '2.0'
0005 0005 0001 00 Extract OS 00 (0) 'MS-DOS'
0006 0007 0002 00 00 General Purpose Flag 0000 (0)
[Bits 1-2] 0 'Normal Compression'
0008 0009 0002 08 00 Compression Method 0008 (8) 'Deflated'
000A 000D 0004 4C 88 92 57 Last Mod Date/Time 5792884C (1469220940) 'Mon Dec 18 17:02:24 2023'
000E 0011 0004 11 47 63 0C CRC 0C634711 (207832849)
0012 0015 0004 B7 01 00 00 Compressed Size 000001B7 (439)
0016 0019 0004 F6 02 00 00 Uncompressed Size 000002F6 (758)
001A 001B 0002 08 00 Filename Length 0008 (8)
001C 001D 0002 2C 00 Extra Length 002C (44)
001E 0025 0008 62 79 74 65 Filename 'byte.tif'
2E 74 69 66
0026 0027 0002 4B 56 Extra ID #1 564B (22091) 'Key-Value Pairs [KV]'
0028 0029 0002 28 00 Length 0028 (40)
002A 0036 000D 4B 65 79 56 Signature 'KeyValuePairs'
61 6C 75 65
50 61 69 72
73
0037 0037 0001 01 KV Count 01 (1)
0038 0039 0002 0C 00 Key size #1 000C (12)
003A 0045 000C 43 6F 6E 74 Key #1 'Content-Type'
65 6E 74 2D
54 79 70 65
0046 0047 0002 0A 00 Value size #1 000A (10)
0048 0051 000A 69 6D 61 67 Value #1 'image/tiff'
65 2F 74 69
66 66
0052 0208 01B7 ... PAYLOAD

0209 020C 0004 50 4B 01 02 CENTRAL HEADER #1 02014B50 (33639248)
020D 020D 0001 00 Created Zip Spec 00 (0) '0.0'
020E 020E 0001 00 Created OS 00 (0) 'MS-DOS'
020F 020F 0001 14 Extract Zip Spec 14 (20) '2.0'
0210 0210 0001 00 Extract OS 00 (0) 'MS-DOS'
0211 0212 0002 00 00 General Purpose Flag 0000 (0)
[Bits 1-2] 0 'Normal Compression'
0213 0214 0002 08 00 Compression Method 0008 (8) 'Deflated'
0215 0218 0004 4C 88 92 57 Last Mod Date/Time 5792884C (1469220940) 'Mon Dec 18 17:02:24 2023'
0219 021C 0004 11 47 63 0C CRC 0C634711 (207832849)
021D 0220 0004 B7 01 00 00 Compressed Size 000001B7 (439)
0221 0224 0004 F6 02 00 00 Uncompressed Size 000002F6 (758)
0225 0226 0002 08 00 Filename Length 0008 (8)
0227 0228 0002 2C 00 Extra Length 002C (44)
0229 022A 0002 00 00 Comment Length 0000 (0)
022B 022C 0002 00 00 Disk Start 0000 (0)
022D 022E 0002 00 00 Int File Attributes 0000 (0)
[Bit 0] 0 'Binary Data'
022F 0232 0004 00 00 00 00 Ext File Attributes 00000000 (0)
0233 0236 0004 00 00 00 00 Local Header Offset 00000000 (0)
0237 023E 0008 62 79 74 65 Filename 'byte.tif'
2E 74 69 66
023F 0240 0002 4B 56 Extra ID #1 564B (22091) 'Key-Value Pairs [KV]'
0241 0242 0002 28 00 Length 0028 (40)
0243 024F 000D 4B 65 79 56 Signature 'KeyValuePairs'
61 6C 75 65
50 61 69 72
73
0250 0250 0001 01 KV Count 01 (1)
0251 0252 0002 0C 00 Key size #1 000C (12)
0253 025E 000C 43 6F 6E 74 Key #1 'Content-Type'
65 6E 74 2D
54 79 70 65
025F 0260 0002 0A 00 Value size #1 000A (10)
0261 026A 000A 69 6D 61 67 Value #1 'image/tiff'
65 2F 74 69
66 66

026B 026E 0004 50 4B 05 06 END CENTRAL HEADER 06054B50 (101010256)
026F 0270 0002 00 00 Number of this disk 0000 (0)
0271 0272 0002 00 00 Central Dir Disk no 0000 (0)
0273 0274 0002 01 00 Entries in this disk 0001 (1)
0275 0276 0002 01 00 Total Entries 0001 (1)
0277 027A 0004 62 00 00 00 Size of Central Dir 00000062 (98)
027B 027E 0004 09 02 00 00 Offset to Central Dir 00000209 (521)
027F 0280 0002 00 00 Comment Length 0000 (0)
#
# Done

0 comments on commit 9b6a0c7

Please sign in to comment.