8314891: Additional Zip64 extra header validation
Reviewed-by: coffeys
This commit is contained in:
parent
0dcd730f5c
commit
82747132b0
src
java.base/share/classes/java/util/zip
jdk.zipfs/share/classes/jdk/nio/zipfs
test/jdk/java/util/zip/ZipFile
@ -1220,17 +1220,23 @@ public class ZipFile implements ZipConstants, Closeable {
|
||||
}
|
||||
int entryPos = pos + CENHDR;
|
||||
int nlen = CENNAM(cen, pos);
|
||||
int elen = CENEXT(cen, pos);
|
||||
int clen = CENCOM(cen, pos);
|
||||
if (entryPos + nlen > cen.length - ENDHDR) {
|
||||
zerror("invalid CEN header (bad header size)");
|
||||
}
|
||||
|
||||
int elen = CENEXT(cen, pos);
|
||||
if (elen > 0 && !DISABLE_ZIP64_EXTRA_VALIDATION) {
|
||||
long extraStartingOffset = pos + CENHDR + nlen;
|
||||
if ((int)extraStartingOffset != extraStartingOffset) {
|
||||
zerror("invalid CEN header (bad extra offset)");
|
||||
}
|
||||
checkExtraFields(pos, (int)extraStartingOffset, elen);
|
||||
} else if (elen == 0 && (CENSIZ(cen, pos) == ZIP64_MAGICVAL
|
||||
|| CENLEN(cen, pos) == ZIP64_MAGICVAL
|
||||
|| CENOFF(cen, pos) == ZIP64_MAGICVAL
|
||||
|| CENDSK(cen, pos) == ZIP64_MAGICCOUNT)) {
|
||||
zerror("Invalid CEN header (invalid zip64 extra len size)");
|
||||
}
|
||||
|
||||
try {
|
||||
@ -1243,10 +1249,9 @@ public class ZipFile implements ZipConstants, Closeable {
|
||||
entries[index++] = hash;
|
||||
entries[index++] = next;
|
||||
entries[index ] = pos;
|
||||
// Validate comment if it exists
|
||||
// if the bytes representing the comment cannot be converted to
|
||||
// Validate comment if it exists.
|
||||
// If the bytes representing the comment cannot be converted to
|
||||
// a String via zcp.toString, an Exception will be thrown
|
||||
int clen = CENCOM(cen, pos);
|
||||
if (clen > 0) {
|
||||
int start = entryPos + nlen + elen;
|
||||
zcp.toString(cen, start, clen);
|
||||
@ -1259,6 +1264,7 @@ public class ZipFile implements ZipConstants, Closeable {
|
||||
|
||||
/**
|
||||
* Validate the Zip64 Extra block fields
|
||||
* @param cenPos The CEN offset for the current Entry
|
||||
* @param startingOffset Extra Field starting offset within the CEN
|
||||
* @param extraFieldLen Length of this Extra field
|
||||
* @throws ZipException If an error occurs validating the Zip64 Extra
|
||||
@ -1273,7 +1279,7 @@ public class ZipFile implements ZipConstants, Closeable {
|
||||
}
|
||||
// CEN Offset where this Extra field ends
|
||||
int extraEndOffset = startingOffset + extraFieldLen;
|
||||
if (extraEndOffset > cen.length) {
|
||||
if (extraEndOffset > cen.length - ENDHDR) {
|
||||
zerror("Invalid CEN header (extra data field size too long)");
|
||||
}
|
||||
int currentOffset = startingOffset;
|
||||
@ -1302,9 +1308,13 @@ public class ZipFile implements ZipConstants, Closeable {
|
||||
long csize = CENSIZ(cen, cenPos);
|
||||
// Get the uncompressed size;
|
||||
long size = CENLEN(cen, cenPos);
|
||||
// Get the LOC offset
|
||||
long locoff = CENOFF(cen, cenPos);
|
||||
// Get the Disk Number
|
||||
int diskNo = CENDSK(cen, cenPos);
|
||||
|
||||
checkZip64ExtraFieldValues(currentOffset, tagBlockSize,
|
||||
csize, size);
|
||||
csize, size, locoff, diskNo);
|
||||
}
|
||||
currentOffset += tagBlockSize;
|
||||
}
|
||||
@ -1312,25 +1322,29 @@ public class ZipFile implements ZipConstants, Closeable {
|
||||
|
||||
/**
|
||||
* Validate the Zip64 Extended Information Extra Field (0x0001) block
|
||||
* size and that the uncompressed size and compressed size field
|
||||
* values are not negative.
|
||||
* Note: As we do not use the LOC offset or Starting disk number
|
||||
* field value we will not validate them
|
||||
* size; that the uncompressed size, compressed size field and LOC
|
||||
* offset fields are not negative. Also make sure the field exists if
|
||||
* the CEN header field is set to 0xFFFFFFFF.
|
||||
* Note: As we do not use the Starting disk number field,
|
||||
* we will not validate its value
|
||||
* @param off the starting offset for the Zip64 field value
|
||||
* @param blockSize the size of the Zip64 Extended Extra Field
|
||||
* @param csize CEN header compressed size value
|
||||
* @param size CEN header uncompressed size value
|
||||
* @param locoff CEN header LOC offset
|
||||
* @param diskNo CEN header Disk number
|
||||
* @throws ZipException if an error occurs
|
||||
*/
|
||||
private void checkZip64ExtraFieldValues(int off, int blockSize, long csize,
|
||||
long size)
|
||||
long size, long locoff, int diskNo)
|
||||
throws ZipException {
|
||||
byte[] cen = this.cen;
|
||||
// if ZIP64_EXTID blocksize == 0, which may occur with some older
|
||||
// versions of Apache Ant and Commons Compress, validate csize and size
|
||||
// to make sure neither field == ZIP64_MAGICVAL
|
||||
if (blockSize == 0) {
|
||||
if (csize == ZIP64_MAGICVAL || size == ZIP64_MAGICVAL) {
|
||||
if (csize == ZIP64_MAGICVAL || size == ZIP64_MAGICVAL ||
|
||||
locoff == ZIP64_MAGICVAL || diskNo == ZIP64_MAGICCOUNT) {
|
||||
zerror("Invalid CEN header (invalid zip64 extra data field size)");
|
||||
}
|
||||
// Only validate the ZIP64_EXTID data if the block size > 0
|
||||
@ -1338,22 +1352,45 @@ public class ZipFile implements ZipConstants, Closeable {
|
||||
}
|
||||
// Validate the Zip64 Extended Information Extra Field (0x0001)
|
||||
// length.
|
||||
if (!isZip64ExtBlockSizeValid(blockSize)) {
|
||||
if (!isZip64ExtBlockSizeValid(blockSize, csize, size, locoff, diskNo)) {
|
||||
zerror("Invalid CEN header (invalid zip64 extra data field size)");
|
||||
}
|
||||
// Check the uncompressed size is not negative
|
||||
// Note we do not need to check blockSize is >= 8 as
|
||||
// we know its length is at least 8 from the call to
|
||||
// isZip64ExtBlockSizeValid()
|
||||
if ((size == ZIP64_MAGICVAL)) {
|
||||
if(get64(cen, off) < 0) {
|
||||
zerror("Invalid zip64 extra block size value");
|
||||
if (size == ZIP64_MAGICVAL) {
|
||||
if ( blockSize >= Long.BYTES) {
|
||||
if (get64(cen, off) < 0) {
|
||||
zerror("Invalid zip64 extra block size value");
|
||||
}
|
||||
off += Long.BYTES;
|
||||
blockSize -= Long.BYTES;
|
||||
} else {
|
||||
zerror("Invalid Zip64 extra block, missing size");
|
||||
}
|
||||
}
|
||||
// Check the compressed size is not negative
|
||||
if ((csize == ZIP64_MAGICVAL) && (blockSize >= 16)) {
|
||||
if (get64(cen, off + 8) < 0) {
|
||||
zerror("Invalid zip64 extra block compressed size value");
|
||||
if (csize == ZIP64_MAGICVAL) {
|
||||
if (blockSize >= Long.BYTES) {
|
||||
if (get64(cen, off) < 0) {
|
||||
zerror("Invalid zip64 extra block compressed size value");
|
||||
}
|
||||
off += Long.BYTES;
|
||||
blockSize -= Long.BYTES;
|
||||
} else {
|
||||
zerror("Invalid Zip64 extra block, missing compressed size");
|
||||
}
|
||||
}
|
||||
// Check the LOC offset is not negative
|
||||
if (locoff == ZIP64_MAGICVAL) {
|
||||
if (blockSize >= Long.BYTES) {
|
||||
if (get64(cen, off) < 0) {
|
||||
zerror("Invalid zip64 extra block LOC OFFSET value");
|
||||
}
|
||||
// Note: We do not need to adjust the following fields as
|
||||
// this is the last field we are leveraging
|
||||
// off += Long.BYTES;
|
||||
// blockSize -= Long.BYTES;
|
||||
} else {
|
||||
zerror("Invalid Zip64 extra block, missing LOC offset value");
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1370,22 +1407,22 @@ public class ZipFile implements ZipConstants, Closeable {
|
||||
* See PKWare APP.Note Section 4.5.3 for more details
|
||||
*
|
||||
* @param blockSize the Zip64 Extended Information Extra Field size
|
||||
* @param csize CEN header compressed size value
|
||||
* @param size CEN header uncompressed size value
|
||||
* @param locoff CEN header LOC offset
|
||||
* @param diskNo CEN header Disk number
|
||||
* @return true if the extra block size is valid; false otherwise
|
||||
*/
|
||||
private static boolean isZip64ExtBlockSizeValid(int blockSize) {
|
||||
/*
|
||||
* As the fields must appear in order, the block size indicates which
|
||||
* fields to expect:
|
||||
* 8 - uncompressed size
|
||||
* 16 - uncompressed size, compressed size
|
||||
* 24 - uncompressed size, compressed sise, LOC Header offset
|
||||
* 28 - uncompressed size, compressed sise, LOC Header offset,
|
||||
* and Disk start number
|
||||
*/
|
||||
return switch(blockSize) {
|
||||
case 8, 16, 24, 28 -> true;
|
||||
default -> false;
|
||||
};
|
||||
private static boolean isZip64ExtBlockSizeValid(int blockSize, long csize,
|
||||
long size, long locoff,
|
||||
int diskNo) {
|
||||
int expectedBlockSize =
|
||||
(csize == ZIP64_MAGICVAL ? Long.BYTES : 0) +
|
||||
(size == ZIP64_MAGICVAL ? Long.BYTES : 0) +
|
||||
(locoff == ZIP64_MAGICVAL ? Long.BYTES : 0) +
|
||||
(diskNo == ZIP64_MAGICCOUNT ? Integer.BYTES : 0);
|
||||
return expectedBlockSize == blockSize;
|
||||
|
||||
}
|
||||
private int getEntryHash(int index) { return entries[index]; }
|
||||
private int getEntryNext(int index) { return entries[index + 1]; }
|
||||
|
@ -1583,6 +1583,10 @@ class ZipFileSystem extends FileSystem {
|
||||
int elen = CENEXT(cen, pos);
|
||||
int clen = CENCOM(cen, pos);
|
||||
int flag = CENFLG(cen, pos);
|
||||
long csize = CENSIZ(cen, pos);
|
||||
long size = CENLEN(cen, pos);
|
||||
long locoff = CENOFF(cen, pos);
|
||||
int diskNo = CENDSK(cen, pos);
|
||||
if ((flag & 1) != 0) {
|
||||
throw new ZipException("invalid CEN header (encrypted entry)");
|
||||
}
|
||||
@ -1592,6 +1596,13 @@ class ZipFileSystem extends FileSystem {
|
||||
if (pos + CENHDR + nlen > limit) {
|
||||
throw new ZipException("invalid CEN header (bad header size)");
|
||||
}
|
||||
if (elen > 0) {
|
||||
checkExtraFields(cen, pos, size, csize, locoff, diskNo,
|
||||
pos + CENHDR + nlen, elen);
|
||||
} else if (elen == 0 && (size == ZIP64_MINVAL || csize == ZIP64_MINVAL ||
|
||||
locoff == ZIP64_MINVAL || diskNo == ZIP64_MINVAL32)) {
|
||||
throw new ZipException("Invalid CEN header (invalid zip64 extra len size)");
|
||||
}
|
||||
IndexNode inode = new IndexNode(cen, pos, nlen);
|
||||
if (inode.pathHasDotOrDotDot()) {
|
||||
throw new ZipException("ZIP file can't be opened as a file system " +
|
||||
@ -1613,6 +1624,165 @@ class ZipFileSystem extends FileSystem {
|
||||
return cen;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate the Zip64 Extra block fields
|
||||
* @param cen CEN array
|
||||
* @param cenPos starting offset in the CEN for the Extra field
|
||||
* @param size CEN size value
|
||||
* @param csize CEN csize value
|
||||
* @param locoff CEN LOC offset value
|
||||
* @param diskNo CEN Disk number value
|
||||
* @param startingOffset Extra Field starting offset within the CEN
|
||||
* @param extraFieldLen Length of this Extra field
|
||||
* @throws ZipException If an error occurs validating the Zip64 Extra
|
||||
* block
|
||||
*/
|
||||
private void checkExtraFields( byte[] cen, int cenPos, long size, long csize,
|
||||
long locoff, int diskNo, int startingOffset,
|
||||
int extraFieldLen) throws ZipException {
|
||||
// Extra field Length cannot exceed 65,535 bytes per the PKWare
|
||||
// APP.note 4.4.11
|
||||
if (extraFieldLen > 0xFFFF) {
|
||||
zerror("invalid extra field length");
|
||||
}
|
||||
// CEN Offset where this Extra field ends
|
||||
int extraEndOffset = startingOffset + extraFieldLen;
|
||||
if (extraEndOffset > cen.length - ENDHDR) {
|
||||
zerror("Invalid CEN header (extra data field size too long)");
|
||||
}
|
||||
int currentOffset = startingOffset;
|
||||
// Walk through each Extra Header. Each Extra Header Must consist of:
|
||||
// Header ID - 2 bytes
|
||||
// Data Size - 2 bytes:
|
||||
while (currentOffset + Integer.BYTES <= extraEndOffset) {
|
||||
int tag = SH(cen, currentOffset);
|
||||
currentOffset += Short.BYTES;
|
||||
|
||||
int tagBlockSize = SH(cen, currentOffset);
|
||||
currentOffset += Short.BYTES;
|
||||
int tagBlockEndingOffset = currentOffset + tagBlockSize;
|
||||
|
||||
// The ending offset for this tag block should not go past the
|
||||
// offset for the end of the extra field
|
||||
if (tagBlockEndingOffset > extraEndOffset) {
|
||||
zerror(String.format(
|
||||
"Invalid CEN header (invalid extra data field size for " +
|
||||
"tag: 0x%04x at %d)",
|
||||
tag, cenPos));
|
||||
}
|
||||
|
||||
if (tag == EXTID_ZIP64) {
|
||||
checkZip64ExtraFieldValues(cen, currentOffset, tagBlockSize,
|
||||
csize, size, locoff, diskNo);
|
||||
}
|
||||
currentOffset += tagBlockSize;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate the Zip64 Extended Information Extra Field (0x0001) block
|
||||
* size; that the uncompressed size, compressed size field and LOC
|
||||
* offset fields are not negative. Also make sure the field exists if
|
||||
* the CEN header field is set to 0xFFFFFFFF.
|
||||
* Note: As we do not use the Starting disk number field,
|
||||
* we will not validate its value
|
||||
* @param cen CEN array
|
||||
* @param off the starting offset for the Zip64 field value
|
||||
* @param blockSize the size of the Zip64 Extended Extra Field
|
||||
* @param csize CEN header compressed size value
|
||||
* @param size CEN header uncompressed size value
|
||||
* @param locoff CEN header LOC offset
|
||||
* @param diskNo CEN header Disk Number
|
||||
* @throws ZipException if an error occurs
|
||||
*/
|
||||
private void checkZip64ExtraFieldValues(byte[] cen, int off, int blockSize, long csize,
|
||||
long size, long locoff, int diskNo)
|
||||
throws ZipException {
|
||||
// if ZIP64_EXTID blocksize == 0, which may occur with some older
|
||||
// versions of Apache Ant and Commons Compress, validate csize and size
|
||||
// to make sure neither field == ZIP64_MAGICVAL
|
||||
if (blockSize == 0) {
|
||||
if (csize == ZIP64_MINVAL || size == ZIP64_MINVAL ||
|
||||
locoff == ZIP64_MINVAL || diskNo == ZIP64_MINVAL32) {
|
||||
zerror("Invalid CEN header (invalid zip64 extra data field size)");
|
||||
}
|
||||
// Only validate the ZIP64_EXTID data if the block size > 0
|
||||
return;
|
||||
}
|
||||
// Validate the Zip64 Extended Information Extra Field (0x0001)
|
||||
// length.
|
||||
if (!isZip64ExtBlockSizeValid(blockSize, csize, size, locoff, diskNo)) {
|
||||
zerror("Invalid CEN header (invalid zip64 extra data field size)");
|
||||
}
|
||||
// Check the uncompressed size is not negative
|
||||
if (size == ZIP64_MINVAL) {
|
||||
if (blockSize >= Long.BYTES) {
|
||||
if (LL(cen, off) < 0) {
|
||||
zerror("Invalid zip64 extra block size value");
|
||||
}
|
||||
off += Long.BYTES;
|
||||
blockSize -= Long.BYTES;
|
||||
} else {
|
||||
zerror("Invalid Zip64 extra block, missing size");
|
||||
}
|
||||
}
|
||||
// Check the compressed size is not negative
|
||||
if (csize == ZIP64_MINVAL) {
|
||||
if (blockSize >= Long.BYTES) {
|
||||
if (LL(cen, off) < 0) {
|
||||
zerror("Invalid zip64 extra block compressed size value");
|
||||
}
|
||||
off += Long.BYTES;
|
||||
blockSize -= Long.BYTES;
|
||||
} else {
|
||||
zerror("Invalid Zip64 extra block, missing compressed size");
|
||||
}
|
||||
}
|
||||
// Check the LOC offset is not negative
|
||||
if (locoff == ZIP64_MINVAL) {
|
||||
if (blockSize >= Long.BYTES) {
|
||||
if (LL(cen, off) < 0) {
|
||||
zerror("Invalid zip64 extra block LOC OFFSET value");
|
||||
}
|
||||
// Note: We do not need to adjust the following fields as
|
||||
// this is the last field we are leveraging
|
||||
// off += Long.BYTES;
|
||||
// blockSize -= Long.BYTES;
|
||||
} else {
|
||||
zerror("Invalid Zip64 extra block, missing LOC offset value");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate the size and contents of a Zip64 extended information field
|
||||
* The order of the Zip64 fields is fixed, but the fields MUST
|
||||
* only appear if the corresponding LOC or CEN field is set to 0xFFFF:
|
||||
* or 0xFFFFFFFF:
|
||||
* Uncompressed Size - 8 bytes
|
||||
* Compressed Size - 8 bytes
|
||||
* LOC Header offset - 8 bytes
|
||||
* Disk Start Number - 4 bytes
|
||||
* See PKWare APP.Note Section 4.5.3 for more details
|
||||
*
|
||||
* @param blockSize the Zip64 Extended Information Extra Field size
|
||||
* @param csize CEN header compressed size value
|
||||
* @param size CEN header uncompressed size value
|
||||
* @param locoff CEN header LOC offset
|
||||
* @param diskNo CEN header Disk Number
|
||||
* @return true if the extra block size is valid; false otherwise
|
||||
*/
|
||||
private static boolean isZip64ExtBlockSizeValid(int blockSize, long csize,
|
||||
long size, long locoff,
|
||||
int diskNo) {
|
||||
int expectedBlockSize =
|
||||
(csize == ZIP64_MINVAL ? Long.BYTES : 0) +
|
||||
(size == ZIP64_MINVAL ? Long.BYTES : 0) +
|
||||
(locoff == ZIP64_MINVAL ? Long.BYTES : 0) +
|
||||
(diskNo == ZIP64_MINVAL32 ? Integer.BYTES : 0);
|
||||
return expectedBlockSize == blockSize;
|
||||
}
|
||||
|
||||
private final void checkUTF8(byte[] a) throws ZipException {
|
||||
try {
|
||||
int end = a.length;
|
||||
@ -1632,6 +1802,10 @@ class ZipFileSystem extends FileSystem {
|
||||
}
|
||||
}
|
||||
|
||||
private static void zerror(String msg) throws ZipException {
|
||||
throw new ZipException(msg);
|
||||
}
|
||||
|
||||
private final void checkEncoding( byte[] a) throws ZipException {
|
||||
try {
|
||||
zc.toString(a);
|
||||
@ -1640,7 +1814,6 @@ class ZipFileSystem extends FileSystem {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void ensureOpen() {
|
||||
if (!isOpen)
|
||||
throw new ClosedFileSystemException();
|
||||
@ -2820,9 +2993,9 @@ class ZipFileSystem extends FileSystem {
|
||||
int nlen = CENNAM(cen, pos);
|
||||
int elen = CENEXT(cen, pos);
|
||||
int clen = CENCOM(cen, pos);
|
||||
int diskNo = CENDSK(cen, pos);
|
||||
/*
|
||||
versionMade = CENVEM(cen, pos);
|
||||
disk = CENDSK(cen, pos);
|
||||
attrs = CENATT(cen, pos);
|
||||
attrsEx = CENATX(cen, pos);
|
||||
*/
|
||||
@ -2840,6 +3013,9 @@ class ZipFileSystem extends FileSystem {
|
||||
extra = Arrays.copyOfRange(cen, pos, pos + elen);
|
||||
pos += elen;
|
||||
readExtra(zipfs);
|
||||
} else if (elen == 0 && (size == ZIP64_MINVAL || csize == ZIP64_MINVAL
|
||||
|| locoff == ZIP64_MINVAL || diskNo == ZIP64_MINVAL32)) {
|
||||
throw new ZipException("Invalid CEN header (invalid zip64 extra len size)");
|
||||
}
|
||||
if (clen > 0) {
|
||||
comment = Arrays.copyOfRange(cen, pos, pos + clen);
|
||||
@ -3089,7 +3265,7 @@ class ZipFileSystem extends FileSystem {
|
||||
int off = 0;
|
||||
int newOff = 0;
|
||||
boolean hasZip64LocOffset = false;
|
||||
while (off + 4 < elen) {
|
||||
while (off + 4 <= elen) {
|
||||
// extra spec: HeaderID+DataSize+Data
|
||||
int pos = off;
|
||||
int tag = SH(extra, pos);
|
||||
@ -3102,20 +3278,7 @@ class ZipFileSystem extends FileSystem {
|
||||
tag, sz));
|
||||
}
|
||||
switch (tag) {
|
||||
case EXTID_ZIP64 :
|
||||
// if ZIP64_EXTID blocksize == 0, which may occur with some older
|
||||
// versions of Apache Ant and Commons Compress, validate csize
|
||||
// size, and locoff to make sure the fields != ZIP64_MAGICVAL
|
||||
if (sz == 0) {
|
||||
if (csize == ZIP64_MINVAL || size == ZIP64_MINVAL || locoff == ZIP64_MINVAL) {
|
||||
throw new ZipException("Invalid CEN header (invalid zip64 extra data field size)");
|
||||
}
|
||||
break;
|
||||
}
|
||||
// Check to see if we have a valid block size
|
||||
if (!isZip64ExtBlockSizeValid(sz)) {
|
||||
throw new ZipException("Invalid CEN header (invalid zip64 extra data field size)");
|
||||
}
|
||||
case EXTID_ZIP64:
|
||||
if (size == ZIP64_MINVAL) {
|
||||
if (pos + 8 > elen) // invalid zip64 extra
|
||||
break; // fields, just skip
|
||||
@ -3169,8 +3332,8 @@ class ZipFileSystem extends FileSystem {
|
||||
break;
|
||||
}
|
||||
// If the LOC offset is 0xFFFFFFFF, then we need to read the
|
||||
// LOC offset from the EXTID_ZIP64 extra data. Therefore
|
||||
// wait until all of the CEN extra data fields have been processed
|
||||
// LOC offset from the EXTID_ZIP64 extra data. Therefore,
|
||||
// wait until all the CEN extra data fields have been processed
|
||||
// prior to reading the LOC extra data field in order to obtain
|
||||
// the Info-ZIP Extended Timestamp.
|
||||
if (locoff != ZIP64_MINVAL) {
|
||||
@ -3198,36 +3361,6 @@ class ZipFileSystem extends FileSystem {
|
||||
extra = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate the size and contents of a Zip64 extended information field
|
||||
* The order of the Zip64 fields is fixed, but the fields MUST
|
||||
* only appear if the corresponding LOC or CEN field is set to 0xFFFF:
|
||||
* or 0xFFFFFFFF:
|
||||
* Uncompressed Size - 8 bytes
|
||||
* Compressed Size - 8 bytes
|
||||
* LOC Header offset - 8 bytes
|
||||
* Disk Start Number - 4 bytes
|
||||
* See PKWare APP.Note Section 4.5.3 for more details
|
||||
*
|
||||
* @param blockSize the Zip64 Extended Information Extra Field size
|
||||
* @return true if the extra block size is valid; false otherwise
|
||||
*/
|
||||
private static boolean isZip64ExtBlockSizeValid(int blockSize) {
|
||||
/*
|
||||
* As the fields must appear in order, the block size indicates which
|
||||
* fields to expect:
|
||||
* 8 - uncompressed size
|
||||
* 16 - uncompressed size, compressed size
|
||||
* 24 - uncompressed size, compressed sise, LOC Header offset
|
||||
* 28 - uncompressed size, compressed sise, LOC Header offset,
|
||||
* and Disk start number
|
||||
*/
|
||||
return switch(blockSize) {
|
||||
case 8, 16, 24, 28 -> true;
|
||||
default -> false;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the LOC extra field to obtain the Info-ZIP Extended Timestamp fields
|
||||
* @param zipfs The Zip FS to use
|
||||
|
816
test/jdk/java/util/zip/ZipFile/MissingZIP64EntriesTest.java
Normal file
816
test/jdk/java/util/zip/ZipFile/MissingZIP64EntriesTest.java
Normal file
@ -0,0 +1,816 @@
|
||||
/*
|
||||
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.nio.file.FileSystem;
|
||||
import java.nio.file.FileSystems;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.HexFormat;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Stream;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipException;
|
||||
import java.util.zip.ZipFile;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/* @test
|
||||
* @bug 8314891
|
||||
* @summary Validate that a ZipException is thrown when the extra len is 0
|
||||
* and the CEN size, csize,LOC offset fields are set to 0xFFFFFFFF, the disk
|
||||
* starting number is set to 0xFFFF or when we have a valid Zip64 Extra header
|
||||
* size but missing the expected header fields.
|
||||
* @run junit MissingZIP64EntriesTest
|
||||
*/
|
||||
public class MissingZIP64EntriesTest {
|
||||
|
||||
/*
|
||||
* Byte array representing a ZIP file which contains a
|
||||
* Zip64 Extra Header with only the size field.
|
||||
* ----------------#1--------------------
|
||||
* [Central Directory Header]
|
||||
* 0x4d: Signature : 0x02014b50
|
||||
* 0x51: Created Zip Spec : 0x2d [4.5]
|
||||
* 0x52: Created OS : 0x0 [MS-DOS]
|
||||
* 0x53: VerMadeby : 0x2d [0, 4.5]
|
||||
* 0x54: VerExtract : 0x2d [4.5]
|
||||
* 0x55: Flag : 0x808
|
||||
* 0x57: Method : 0x8 [DEFLATED]
|
||||
* 0x59: Last Mod Time : 0x57116922 [Thu Aug 17 13:09:04 EDT 2023]
|
||||
* 0x5d: CRC : 0x57de98d2
|
||||
* 0x61: Compressed Size : 0x16
|
||||
* 0x65: Uncompressed Size: 0xffffffff
|
||||
* 0x69: Name Length : 0x9
|
||||
* 0x6b: Extra Length : 0xc
|
||||
* Extra data:[01, 00, 08, 00, 14, 00, 00, 00, 00, 00, 00, 00]
|
||||
* [tag=0x0001, sz=8]
|
||||
* ->ZIP64: size *0x14
|
||||
* [data= 14 00 00 00 00 00 00 00 ]
|
||||
* 0x6d: Comment Length : 0x0
|
||||
* 0x6f: Disk Start : 0x0
|
||||
* 0x71: Attrs : 0x0
|
||||
* 0x73: AttrsEx : 0x0
|
||||
* 0x77: Loc Header Offset: 0x0
|
||||
* 0x7b: File Name : Hello.txt
|
||||
*/
|
||||
public static byte[] ZIP_WITH_ZIP64_EXTRAHDR_SIZE_ONLY_BYTEARRAY = {
|
||||
(byte) 0x50, (byte) 0x4b, (byte) 0x3, (byte) 0x4, (byte) 0x14, (byte) 0x0, (byte) 0x8, (byte) 0x8,
|
||||
(byte) 0x8, (byte) 0x0, (byte) 0x22, (byte) 0x69, (byte) 0x11, (byte) 0x57, (byte) 0x0, (byte) 0x0,
|
||||
(byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0,
|
||||
(byte) 0x0, (byte) 0x0, (byte) 0x9, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x48, (byte) 0x65,
|
||||
(byte) 0x6c, (byte) 0x6c, (byte) 0x6f, (byte) 0x2e, (byte) 0x74, (byte) 0x78, (byte) 0x74, (byte) 0xf3,
|
||||
(byte) 0x48, (byte) 0xcd, (byte) 0xc9, (byte) 0xc9, (byte) 0x57, (byte) 0x8, (byte) 0x49, (byte) 0xcd,
|
||||
(byte) 0xcb, (byte) 0xcb, (byte) 0x2c, (byte) 0x56, (byte) 0x8, (byte) 0xc8, (byte) 0x49, (byte) 0xac,
|
||||
(byte) 0x4c, (byte) 0x2d, (byte) 0x2a, (byte) 0x6, (byte) 0x0, (byte) 0x50, (byte) 0x4b, (byte) 0x7,
|
||||
(byte) 0x8, (byte) 0xd2, (byte) 0x98, (byte) 0xde, (byte) 0x57, (byte) 0x16, (byte) 0x0, (byte) 0x0,
|
||||
(byte) 0x0, (byte) 0x14, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x50, (byte) 0x4b, (byte) 0x1,
|
||||
(byte) 0x2, (byte) 0x2d, (byte) 0x0, (byte) 0x2d, (byte) 0x0, (byte) 0x8, (byte) 0x8, (byte) 0x8,
|
||||
(byte) 0x0, (byte) 0x22, (byte) 0x69, (byte) 0x11, (byte) 0x57, (byte) 0xd2, (byte) 0x98, (byte) 0xde,
|
||||
(byte) 0x57, (byte) 0x16, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0xff, (byte) 0xff, (byte) 0xff,
|
||||
(byte) 0xff, (byte) 0x9, (byte) 0x0, (byte) 0xc, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0,
|
||||
(byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0,
|
||||
(byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x48, (byte) 0x65, (byte) 0x6c, (byte) 0x6c, (byte) 0x6f,
|
||||
(byte) 0x2e, (byte) 0x74, (byte) 0x78, (byte) 0x74, (byte) 0x1, (byte) 0x0, (byte) 0x8, (byte) 0x0,
|
||||
(byte) 0x14, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0,
|
||||
(byte) 0x50, (byte) 0x4b, (byte) 0x5, (byte) 0x6, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0,
|
||||
(byte) 0x1, (byte) 0x0, (byte) 0x1, (byte) 0x0, (byte) 0x43, (byte) 0x0, (byte) 0x0, (byte) 0x0,
|
||||
(byte) 0x4d, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0,
|
||||
};
|
||||
|
||||
/**
|
||||
* Byte array representing a Zip file with no extra header fields
|
||||
* ----------------#1--------------------
|
||||
* [Central Directory Header]
|
||||
* 0x2f: Signature : 0x02014b50
|
||||
* 0x33: Created Zip Spec : 0x14 [2.0]
|
||||
* 0x34: Created OS : 0x3 [UNIX]
|
||||
* 0x35: VerMadeby : 0x314 [3, 2.0]
|
||||
* 0x36: VerExtract : 0x14 [2.0]
|
||||
* 0x37: Flag : 0x2
|
||||
* 0x39: Method : 0x8 [DEFLATED]
|
||||
* 0x3b: Last Mod Time : 0x57039c0d [Thu Aug 03 19:32:26 EDT 2023]
|
||||
* 0x3f: CRC : 0x31963516
|
||||
* 0x43: Compressed Size : 0x8
|
||||
* 0x47: Uncompressed Size: 0x6
|
||||
* 0x4b: Name Length : 0x9
|
||||
* 0x4d: Extra Length : 0x0
|
||||
* 0x4f: Comment Length : 0x0
|
||||
* 0x51: Disk Start : 0x0
|
||||
* 0x53: Attrs : 0x1
|
||||
* 0x55: AttrsEx : 0x81a40000
|
||||
* 0x59: Loc Header Offset: 0x0
|
||||
* 0x5d: File Name : Hello.txt
|
||||
*/
|
||||
public static byte[] ZIP_WITH_NO_EXTRA_LEN_BYTEARRAY = {
|
||||
(byte) 0x50, (byte) 0x4b, (byte) 0x3, (byte) 0x4, (byte) 0x14, (byte) 0x0, (byte) 0x2, (byte) 0x0,
|
||||
(byte) 0x8, (byte) 0x0, (byte) 0xd, (byte) 0x9c, (byte) 0x3, (byte) 0x57, (byte) 0x16, (byte) 0x35,
|
||||
(byte) 0x96, (byte) 0x31, (byte) 0x8, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x6, (byte) 0x0,
|
||||
(byte) 0x0, (byte) 0x0, (byte) 0x9, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x48, (byte) 0x65,
|
||||
(byte) 0x6c, (byte) 0x6c, (byte) 0x6f, (byte) 0x2e, (byte) 0x74, (byte) 0x78, (byte) 0x74, (byte) 0xf3,
|
||||
(byte) 0x48, (byte) 0xcd, (byte) 0xc9, (byte) 0xc9, (byte) 0xe7, (byte) 0x2, (byte) 0x0, (byte) 0x50,
|
||||
(byte) 0x4b, (byte) 0x1, (byte) 0x2, (byte) 0x14, (byte) 0x3, (byte) 0x14, (byte) 0x0, (byte) 0x2,
|
||||
(byte) 0x0, (byte) 0x8, (byte) 0x0, (byte) 0xd, (byte) 0x9c, (byte) 0x3, (byte) 0x57, (byte) 0x16,
|
||||
(byte) 0x35, (byte) 0x96, (byte) 0x31, (byte) 0x8, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x6,
|
||||
(byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x9, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0,
|
||||
(byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x1, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0xa4,
|
||||
(byte) 0x81, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x48, (byte) 0x65, (byte) 0x6c,
|
||||
(byte) 0x6c, (byte) 0x6f, (byte) 0x2e, (byte) 0x74, (byte) 0x78, (byte) 0x74, (byte) 0x50, (byte) 0x4b,
|
||||
(byte) 0x5, (byte) 0x6, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x1, (byte) 0x0,
|
||||
(byte) 0x1, (byte) 0x0, (byte) 0x37, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x2f, (byte) 0x0,
|
||||
(byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0,
|
||||
};
|
||||
|
||||
/**
|
||||
* Byte array representing a ZIP file which contains a
|
||||
* Zip64 Extra Header with only the LOC offset field.
|
||||
* ----------------#1--------------------
|
||||
* [Central Directory Header]
|
||||
* 0x4d: Signature : 0x02014b50
|
||||
* 0x51: Created Zip Spec : 0x2d [4.5]
|
||||
* 0x52: Created OS : 0x0 [MS-DOS]
|
||||
* 0x53: VerMadeby : 0x2d [0, 4.5]
|
||||
* 0x54: VerExtract : 0x2d [4.5]
|
||||
* 0x55: Flag : 0x808
|
||||
* 0x57: Method : 0x8 [DEFLATED]
|
||||
* 0x59: Last Mod Time : 0x572d69c5 [Wed Sep 13 13:14:10 EDT 2023]
|
||||
* 0x5d: CRC : 0x57de98d2
|
||||
* 0x61: Compressed Size : 0x16
|
||||
* 0x65: Uncompressed Size: 0x14
|
||||
* 0x69: Name Length : 0x9
|
||||
* 0x6b: Extra Length : 0xc
|
||||
* Extra data:[01, 00, 08, 00, 00, 00, 00, 00, 00, 00, 00, 00]
|
||||
* [tag=0x0001, sz=8]
|
||||
* ->ZIP64: LOC Off *0x0
|
||||
* [data= 00 00 00 00 00 00 00 00 ]
|
||||
* 0x6d: Comment Length : 0x0
|
||||
* 0x6f: Disk Start : 0x0
|
||||
* 0x71: Attrs : 0x0
|
||||
* 0x73: AttrsEx : 0x0
|
||||
* 0x77: Loc Header Offset: 0xffffffff
|
||||
* 0x7b: File Name : Hello.txt
|
||||
*/
|
||||
public static byte[] ZIP_WITH_ZIP64_EXTRAHDR_LOC_ONLY_BYTEARRAY = {
|
||||
(byte) 0x50, (byte) 0x4b, (byte) 0x3, (byte) 0x4, (byte) 0x14, (byte) 0x0, (byte) 0x8, (byte) 0x8,
|
||||
(byte) 0x8, (byte) 0x0, (byte) 0xc5, (byte) 0x69, (byte) 0x2d, (byte) 0x57, (byte) 0x0, (byte) 0x0,
|
||||
(byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0,
|
||||
(byte) 0x0, (byte) 0x0, (byte) 0x9, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x48, (byte) 0x65,
|
||||
(byte) 0x6c, (byte) 0x6c, (byte) 0x6f, (byte) 0x2e, (byte) 0x74, (byte) 0x78, (byte) 0x74, (byte) 0xf3,
|
||||
(byte) 0x48, (byte) 0xcd, (byte) 0xc9, (byte) 0xc9, (byte) 0x57, (byte) 0x8, (byte) 0x49, (byte) 0xcd,
|
||||
(byte) 0xcb, (byte) 0xcb, (byte) 0x2c, (byte) 0x56, (byte) 0x8, (byte) 0xc8, (byte) 0x49, (byte) 0xac,
|
||||
(byte) 0x4c, (byte) 0x2d, (byte) 0x2a, (byte) 0x6, (byte) 0x0, (byte) 0x50, (byte) 0x4b, (byte) 0x7,
|
||||
(byte) 0x8, (byte) 0xd2, (byte) 0x98, (byte) 0xde, (byte) 0x57, (byte) 0x16, (byte) 0x0, (byte) 0x0,
|
||||
(byte) 0x0, (byte) 0x14, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x50, (byte) 0x4b, (byte) 0x1,
|
||||
(byte) 0x2, (byte) 0x2d, (byte) 0x0, (byte) 0x2d, (byte) 0x0, (byte) 0x8, (byte) 0x8, (byte) 0x8,
|
||||
(byte) 0x0, (byte) 0xc5, (byte) 0x69, (byte) 0x2d, (byte) 0x57, (byte) 0xd2, (byte) 0x98, (byte) 0xde,
|
||||
(byte) 0x57, (byte) 0x16, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x14, (byte) 0x0, (byte) 0x0,
|
||||
(byte) 0x0, (byte) 0x9, (byte) 0x0, (byte) 0xc, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0,
|
||||
(byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0xff,
|
||||
(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0x48, (byte) 0x65, (byte) 0x6c, (byte) 0x6c, (byte) 0x6f,
|
||||
(byte) 0x2e, (byte) 0x74, (byte) 0x78, (byte) 0x74, (byte) 0x1, (byte) 0x0, (byte) 0x8, (byte) 0x0,
|
||||
(byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0,
|
||||
(byte) 0x50, (byte) 0x4b, (byte) 0x5, (byte) 0x6, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0,
|
||||
(byte) 0x1, (byte) 0x0, (byte) 0x1, (byte) 0x0, (byte) 0x43, (byte) 0x0, (byte) 0x0, (byte) 0x0,
|
||||
(byte) 0x4d, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0,
|
||||
};
|
||||
|
||||
/**
|
||||
* Byte array representing a ZIP file which contains a
|
||||
* Zip64 Extra Header with only the compressed size field.
|
||||
* ----------------#1--------------------
|
||||
* [Central Directory Header]
|
||||
* 0x4d: Signature : 0x02014b50
|
||||
* 0x51: Created Zip Spec : 0x2d [4.5]
|
||||
* 0x52: Created OS : 0x0 [MS-DOS]
|
||||
* 0x53: VerMadeby : 0x2d [0, 4.5]
|
||||
* 0x54: VerExtract : 0x2d [4.5]
|
||||
* 0x55: Flag : 0x808
|
||||
* 0x57: Method : 0x8 [DEFLATED]
|
||||
* 0x59: Last Mod Time : 0x572d6960 [Wed Sep 13 13:11:00 EDT 2023]
|
||||
* 0x5d: CRC : 0x57de98d2
|
||||
* 0x61: Compressed Size : 0xffffffff
|
||||
* 0x65: Uncompressed Size: 0x14
|
||||
* 0x69: Name Length : 0x9
|
||||
* 0x6b: Extra Length : 0xc
|
||||
* Extra data:[01, 00, 08, 00, 16, 00, 00, 00, 00, 00, 00, 00]
|
||||
* [tag=0x0001, sz=8]
|
||||
* ->ZIP64: csize *0x16
|
||||
* [data= 16 00 00 00 00 00 00 00 ]
|
||||
* 0x6d: Comment Length : 0x0
|
||||
* 0x6f: Disk Start : 0x0
|
||||
* 0x71: Attrs : 0x0
|
||||
* 0x73: AttrsEx : 0x0
|
||||
* 0x77: Loc Header Offset: 0x0
|
||||
* 0x7b: File Name : Hello.txt
|
||||
*/
|
||||
public static byte[] ZIP_WITH_ZIP64_EXTRAHDR_CSIZE_ONLY_BYTEARRAY = {
|
||||
(byte) 0x50, (byte) 0x4b, (byte) 0x3, (byte) 0x4, (byte) 0x14, (byte) 0x0, (byte) 0x8, (byte) 0x8,
|
||||
(byte) 0x8, (byte) 0x0, (byte) 0x60, (byte) 0x69, (byte) 0x2d, (byte) 0x57, (byte) 0x0, (byte) 0x0,
|
||||
(byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0,
|
||||
(byte) 0x0, (byte) 0x0, (byte) 0x9, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x48, (byte) 0x65,
|
||||
(byte) 0x6c, (byte) 0x6c, (byte) 0x6f, (byte) 0x2e, (byte) 0x74, (byte) 0x78, (byte) 0x74, (byte) 0xf3,
|
||||
(byte) 0x48, (byte) 0xcd, (byte) 0xc9, (byte) 0xc9, (byte) 0x57, (byte) 0x8, (byte) 0x49, (byte) 0xcd,
|
||||
(byte) 0xcb, (byte) 0xcb, (byte) 0x2c, (byte) 0x56, (byte) 0x8, (byte) 0xc8, (byte) 0x49, (byte) 0xac,
|
||||
(byte) 0x4c, (byte) 0x2d, (byte) 0x2a, (byte) 0x6, (byte) 0x0, (byte) 0x50, (byte) 0x4b, (byte) 0x7,
|
||||
(byte) 0x8, (byte) 0xd2, (byte) 0x98, (byte) 0xde, (byte) 0x57, (byte) 0x16, (byte) 0x0, (byte) 0x0,
|
||||
(byte) 0x0, (byte) 0x14, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x50, (byte) 0x4b, (byte) 0x1,
|
||||
(byte) 0x2, (byte) 0x2d, (byte) 0x0, (byte) 0x2d, (byte) 0x0, (byte) 0x8, (byte) 0x8, (byte) 0x8,
|
||||
(byte) 0x0, (byte) 0x60, (byte) 0x69, (byte) 0x2d, (byte) 0x57, (byte) 0xd2, (byte) 0x98, (byte) 0xde,
|
||||
(byte) 0x57, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0x14, (byte) 0x0, (byte) 0x0,
|
||||
(byte) 0x0, (byte) 0x9, (byte) 0x0, (byte) 0xc, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0,
|
||||
(byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0,
|
||||
(byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x48, (byte) 0x65, (byte) 0x6c, (byte) 0x6c, (byte) 0x6f,
|
||||
(byte) 0x2e, (byte) 0x74, (byte) 0x78, (byte) 0x74, (byte) 0x1, (byte) 0x0, (byte) 0x8, (byte) 0x0,
|
||||
(byte) 0x16, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0,
|
||||
(byte) 0x50, (byte) 0x4b, (byte) 0x5, (byte) 0x6, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0,
|
||||
(byte) 0x1, (byte) 0x0, (byte) 0x1, (byte) 0x0, (byte) 0x43, (byte) 0x0, (byte) 0x0, (byte) 0x0,
|
||||
(byte) 0x4d, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0,
|
||||
};
|
||||
|
||||
/**
|
||||
* Byte array representing a Zip file with a zero length ZIP64 Extra Header
|
||||
* ----------------#1--------------------
|
||||
* [Central Directory Header]
|
||||
* 0x43: Signature : 0x02014b50
|
||||
* 0x47: Created Zip Spec : 0x2d [4.5]
|
||||
* 0x48: Created OS : 0x3 [UNIX]
|
||||
* 0x49: VerMadeby : 0x32d [3, 4.5]
|
||||
* 0x4a: VerExtract : 0x2d [4.5]
|
||||
* 0x4b: Flag : 0x800
|
||||
* 0x4d: Method : 0x8 [DEFLATED]
|
||||
* 0x4f: Last Mod Time : 0x572c3477 [Tue Sep 12 06:35:46 EDT 2023]
|
||||
* 0x53: CRC : 0x31963516
|
||||
* 0x57: Compressed Size : 0x8
|
||||
* 0x5b: Uncompressed Size: 0x6
|
||||
* 0x5f: Name Length : 0x9
|
||||
* 0x61: Extra Length : 0x4
|
||||
* Extra data:[01, 00, 00, 00]
|
||||
* [tag=0x0001, sz=0]
|
||||
* ->ZIP64:
|
||||
* 0x63: Comment Length : 0x0
|
||||
* 0x65: Disk Start : 0x0
|
||||
* 0x67: Attrs : 0x0
|
||||
* 0x69: AttrsEx : 0x81a40000
|
||||
* 0x6d: Loc Header Offset: 0x0
|
||||
* 0x71: File Name : Hello.txt
|
||||
*/
|
||||
public static byte[] ZIP_WITH_ZEROLEN_ZIP64_EXTRAHDR_BYTEARRAY = {
|
||||
(byte) 0x50, (byte) 0x4b, (byte) 0x3, (byte) 0x4, (byte) 0x2d, (byte) 0x0, (byte) 0x0, (byte) 0x8,
|
||||
(byte) 0x8, (byte) 0x0, (byte) 0x77, (byte) 0x34, (byte) 0x2c, (byte) 0x57, (byte) 0x16, (byte) 0x35,
|
||||
(byte) 0x96, (byte) 0x31, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
|
||||
(byte) 0xff, (byte) 0xff, (byte) 0x9, (byte) 0x0, (byte) 0x14, (byte) 0x0, (byte) 0x48, (byte) 0x65,
|
||||
(byte) 0x6c, (byte) 0x6c, (byte) 0x6f, (byte) 0x2e, (byte) 0x74, (byte) 0x78, (byte) 0x74, (byte) 0x1,
|
||||
(byte) 0x0, (byte) 0x10, (byte) 0x0, (byte) 0x6, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0,
|
||||
(byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x8, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0,
|
||||
(byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0xf3, (byte) 0x48, (byte) 0xcd, (byte) 0xc9, (byte) 0xc9,
|
||||
(byte) 0xe7, (byte) 0x2, (byte) 0x0, (byte) 0x50, (byte) 0x4b, (byte) 0x1, (byte) 0x2, (byte) 0x2d,
|
||||
(byte) 0x3, (byte) 0x2d, (byte) 0x0, (byte) 0x0, (byte) 0x8, (byte) 0x8, (byte) 0x0, (byte) 0x77,
|
||||
(byte) 0x34, (byte) 0x2c, (byte) 0x57, (byte) 0x16, (byte) 0x35, (byte) 0x96, (byte) 0x31, (byte) 0x8,
|
||||
(byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x6, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x9,
|
||||
(byte) 0x0, (byte) 0x4, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0,
|
||||
(byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0xa4, (byte) 0x81, (byte) 0x0, (byte) 0x0, (byte) 0x0,
|
||||
(byte) 0x0, (byte) 0x48, (byte) 0x65, (byte) 0x6c, (byte) 0x6c, (byte) 0x6f, (byte) 0x2e, (byte) 0x74,
|
||||
(byte) 0x78, (byte) 0x74, (byte) 0x1, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x50, (byte) 0x4b,
|
||||
(byte) 0x6, (byte) 0x6, (byte) 0x2c, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0,
|
||||
(byte) 0x0, (byte) 0x0, (byte) 0x2d, (byte) 0x0, (byte) 0x2d, (byte) 0x0, (byte) 0x0, (byte) 0x0,
|
||||
(byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x1, (byte) 0x0,
|
||||
(byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x1, (byte) 0x0,
|
||||
(byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x3b, (byte) 0x0,
|
||||
(byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x43, (byte) 0x0,
|
||||
(byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x50, (byte) 0x4b,
|
||||
(byte) 0x6, (byte) 0x7, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x7e, (byte) 0x0,
|
||||
(byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x1, (byte) 0x0,
|
||||
(byte) 0x0, (byte) 0x0, (byte) 0x50, (byte) 0x4b, (byte) 0x5, (byte) 0x6, (byte) 0x0, (byte) 0x0,
|
||||
(byte) 0x0, (byte) 0x0, (byte) 0x1, (byte) 0x0, (byte) 0x1, (byte) 0x0, (byte) 0x3b, (byte) 0x0,
|
||||
(byte) 0x0, (byte) 0x0, (byte) 0x43, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0,
|
||||
};
|
||||
|
||||
/**
|
||||
* Byte array representing a ZIP file which contains a
|
||||
* Zip64 Extra Header with the size and csize fields.
|
||||
* ----------------#1--------------------
|
||||
* [Central Directory Header]
|
||||
* 0x4d: Signature : 0x02014b50
|
||||
* 0x51: Created Zip Spec : 0x2d [4.5]
|
||||
* 0x52: Created OS : 0x0 [MS-DOS]
|
||||
* 0x53: VerMadeby : 0x2d [0, 4.5]
|
||||
* 0x54: VerExtract : 0x2d [4.5]
|
||||
* 0x55: Flag : 0x808
|
||||
* 0x57: Method : 0x8 [DEFLATED]
|
||||
* 0x59: Last Mod Time : 0x572c6445 [Tue Sep 12 12:34:10 EDT 2023]
|
||||
* 0x5d: CRC : 0x57de98d2
|
||||
* 0x61: Compressed Size : 0xffffffff
|
||||
* 0x65: Uncompressed Size: 0xffffffff
|
||||
* 0x69: Name Length : 0x9
|
||||
* 0x6b: Extra Length : 0x14
|
||||
* Extra data:[01, 00, 10, 00, 14, 00, 00, 00, 00, 00, 00, 00, 16, 00, 00, 00, 00, 00, 00, 00]
|
||||
* [tag=0x0001, sz=16]
|
||||
* ->ZIP64: size *0x14 csize *0x16
|
||||
* [data= 14 00 00 00 00 00 00 00 16 00 00 00 00 00 00 00 ]
|
||||
* 0x6d: Comment Length : 0x0
|
||||
* 0x6f: Disk Start : 0x0
|
||||
* 0x71: Attrs : 0x0
|
||||
* 0x73: AttrsEx : 0x0
|
||||
* 0x77: Loc Header Offset: 0x0
|
||||
* 0x7b: File Name : Hello.txt
|
||||
*/
|
||||
public static byte[] ZIP_WITH_TWO_ZIP64_HEADER_ENTRIES_BYTEARRAY = {
|
||||
(byte) 0x50, (byte) 0x4b, (byte) 0x3, (byte) 0x4, (byte) 0x14, (byte) 0x0, (byte) 0x8, (byte) 0x8,
|
||||
(byte) 0x8, (byte) 0x0, (byte) 0x45, (byte) 0x64, (byte) 0x2c, (byte) 0x57, (byte) 0x0, (byte) 0x0,
|
||||
(byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0,
|
||||
(byte) 0x0, (byte) 0x0, (byte) 0x9, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x48, (byte) 0x65,
|
||||
(byte) 0x6c, (byte) 0x6c, (byte) 0x6f, (byte) 0x2e, (byte) 0x74, (byte) 0x78, (byte) 0x74, (byte) 0xf3,
|
||||
(byte) 0x48, (byte) 0xcd, (byte) 0xc9, (byte) 0xc9, (byte) 0x57, (byte) 0x8, (byte) 0x49, (byte) 0xcd,
|
||||
(byte) 0xcb, (byte) 0xcb, (byte) 0x2c, (byte) 0x56, (byte) 0x8, (byte) 0xc8, (byte) 0x49, (byte) 0xac,
|
||||
(byte) 0x4c, (byte) 0x2d, (byte) 0x2a, (byte) 0x6, (byte) 0x0, (byte) 0x50, (byte) 0x4b, (byte) 0x7,
|
||||
(byte) 0x8, (byte) 0xd2, (byte) 0x98, (byte) 0xde, (byte) 0x57, (byte) 0x16, (byte) 0x0, (byte) 0x0,
|
||||
(byte) 0x0, (byte) 0x14, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x50, (byte) 0x4b, (byte) 0x1,
|
||||
(byte) 0x2, (byte) 0x2d, (byte) 0x0, (byte) 0x2d, (byte) 0x0, (byte) 0x8, (byte) 0x8, (byte) 0x8,
|
||||
(byte) 0x0, (byte) 0x45, (byte) 0x64, (byte) 0x2c, (byte) 0x57, (byte) 0xd2, (byte) 0x98, (byte) 0xde,
|
||||
(byte) 0x57, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
|
||||
(byte) 0xff, (byte) 0x9, (byte) 0x0, (byte) 0x14, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0,
|
||||
(byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0,
|
||||
(byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x48, (byte) 0x65, (byte) 0x6c, (byte) 0x6c, (byte) 0x6f,
|
||||
(byte) 0x2e, (byte) 0x74, (byte) 0x78, (byte) 0x74, (byte) 0x1, (byte) 0x0, (byte) 0x10, (byte) 0x0,
|
||||
(byte) 0x14, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0,
|
||||
(byte) 0x16, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0,
|
||||
(byte) 0x50, (byte) 0x4b, (byte) 0x5, (byte) 0x6, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0,
|
||||
(byte) 0x1, (byte) 0x0, (byte) 0x1, (byte) 0x0, (byte) 0x4b, (byte) 0x0, (byte) 0x0, (byte) 0x0,
|
||||
(byte) 0x4d, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0,
|
||||
};
|
||||
|
||||
/**
|
||||
* Byte array representing a ZIP file which contains a
|
||||
* Zip64 Extra Header with the size,csize, and LOC offset fields.
|
||||
* ----------------#1--------------------
|
||||
* [Central Directory Header]
|
||||
* 0x4d: Signature : 0x02014b50
|
||||
* 0x51: Created Zip Spec : 0x2d [4.5]
|
||||
* 0x52: Created OS : 0x0 [MS-DOS]
|
||||
* 0x53: VerMadeby : 0x2d [0, 4.5]
|
||||
* 0x54: VerExtract : 0x2d [4.5]
|
||||
* 0x55: Flag : 0x808
|
||||
* 0x57: Method : 0x8 [DEFLATED]
|
||||
* 0x59: Last Mod Time : 0x572d7214 [Wed Sep 13 14:16:40 EDT 2023]
|
||||
* 0x5d: CRC : 0x57de98d2
|
||||
* 0x61: Compressed Size : 0xffffffff
|
||||
* 0x65: Uncompressed Size: 0xffffffff
|
||||
* 0x69: Name Length : 0x9
|
||||
* 0x6b: Extra Length : 0x1c
|
||||
* Extra data:[01, 00, 18, 00, 14, 00, 00, 00, 00, 00, 00, 00, 16, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00]
|
||||
* [tag=0x0001, sz=24]
|
||||
* ->ZIP64: size *0x14 csize *0x16 LOC Off *0x0
|
||||
* [data= 14 00 00 00 00 00 00 00 16 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ]
|
||||
* 0x6d: Comment Length : 0x0
|
||||
* 0x6f: Disk Start : 0x0
|
||||
* 0x71: Attrs : 0x0
|
||||
* 0x73: AttrsEx : 0x0
|
||||
* 0x77: Loc Header Offset: 0xffffffff
|
||||
* 0x7b: File Name : Hello.txt
|
||||
*/
|
||||
public static byte[] ZIP_WITH_ZIP64_EXTRAHDR_ALL_BYTEARRAY = {
|
||||
(byte) 0x50, (byte) 0x4b, (byte) 0x3, (byte) 0x4, (byte) 0x14, (byte) 0x0, (byte) 0x8, (byte) 0x8,
|
||||
(byte) 0x8, (byte) 0x0, (byte) 0x14, (byte) 0x72, (byte) 0x2d, (byte) 0x57, (byte) 0x0, (byte) 0x0,
|
||||
(byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0,
|
||||
(byte) 0x0, (byte) 0x0, (byte) 0x9, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x48, (byte) 0x65,
|
||||
(byte) 0x6c, (byte) 0x6c, (byte) 0x6f, (byte) 0x2e, (byte) 0x74, (byte) 0x78, (byte) 0x74, (byte) 0xf3,
|
||||
(byte) 0x48, (byte) 0xcd, (byte) 0xc9, (byte) 0xc9, (byte) 0x57, (byte) 0x8, (byte) 0x49, (byte) 0xcd,
|
||||
(byte) 0xcb, (byte) 0xcb, (byte) 0x2c, (byte) 0x56, (byte) 0x8, (byte) 0xc8, (byte) 0x49, (byte) 0xac,
|
||||
(byte) 0x4c, (byte) 0x2d, (byte) 0x2a, (byte) 0x6, (byte) 0x0, (byte) 0x50, (byte) 0x4b, (byte) 0x7,
|
||||
(byte) 0x8, (byte) 0xd2, (byte) 0x98, (byte) 0xde, (byte) 0x57, (byte) 0x16, (byte) 0x0, (byte) 0x0,
|
||||
(byte) 0x0, (byte) 0x14, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x50, (byte) 0x4b, (byte) 0x1,
|
||||
(byte) 0x2, (byte) 0x2d, (byte) 0x0, (byte) 0x2d, (byte) 0x0, (byte) 0x8, (byte) 0x8, (byte) 0x8,
|
||||
(byte) 0x0, (byte) 0x14, (byte) 0x72, (byte) 0x2d, (byte) 0x57, (byte) 0xd2, (byte) 0x98, (byte) 0xde,
|
||||
(byte) 0x57, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
|
||||
(byte) 0xff, (byte) 0x9, (byte) 0x0, (byte) 0x1c, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0,
|
||||
(byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0xff,
|
||||
(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0x48, (byte) 0x65, (byte) 0x6c, (byte) 0x6c, (byte) 0x6f,
|
||||
(byte) 0x2e, (byte) 0x74, (byte) 0x78, (byte) 0x74, (byte) 0x1, (byte) 0x0, (byte) 0x18, (byte) 0x0,
|
||||
(byte) 0x14, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0,
|
||||
(byte) 0x16, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0,
|
||||
(byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0,
|
||||
(byte) 0x50, (byte) 0x4b, (byte) 0x5, (byte) 0x6, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0,
|
||||
(byte) 0x1, (byte) 0x0, (byte) 0x1, (byte) 0x0, (byte) 0x53, (byte) 0x0, (byte) 0x0, (byte) 0x0,
|
||||
(byte) 0x4d, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0, (byte) 0x0,
|
||||
};
|
||||
|
||||
/**
|
||||
* Enable debug output
|
||||
*/
|
||||
private static final boolean DEBUG = false;
|
||||
|
||||
/**
|
||||
* Name of the Zip file that we create from the byte array
|
||||
*/
|
||||
public static final String ZIPFILE_NAME = "validZipFile.zip";
|
||||
|
||||
/**
|
||||
* Name of the Zip file that we modify/corrupt
|
||||
*/
|
||||
public static final String BAD_ZIP_NAME = "zipWithInvalidZip64ExtraField.zip";
|
||||
|
||||
/**
|
||||
* Zip file entry that will be accessed by some the tests
|
||||
*/
|
||||
private static final String ZIP_FILE_ENTRY_NAME = "Hello.txt";
|
||||
|
||||
/**
|
||||
* Expected Error messages
|
||||
*/
|
||||
private static final String INVALID_EXTRA_LENGTH =
|
||||
"Invalid CEN header (invalid zip64 extra len size)";
|
||||
|
||||
private static final String INVALID_ZIP64_EXTRAHDR_SIZE =
|
||||
"Invalid CEN header (invalid zip64 extra data field size)";
|
||||
|
||||
/**
|
||||
* Disk starting number offset for the Zip file created from the
|
||||
* ZIP_WITH_NO_EXTRA_LEN_BYTEARRAY array
|
||||
*/
|
||||
private static final int DISKNO_OFFSET_ZIP_NO_EXTRA_LEN = 0x51;
|
||||
|
||||
/**
|
||||
* Value to set the size, csize, or LOC offset CEN fields to when their
|
||||
* actual value is stored in the Zip64 Extra Header
|
||||
*/
|
||||
private static final long ZIP64_MAGICVAL = 0xFFFFFFFFL;
|
||||
|
||||
/**
|
||||
* Value to set the Disk Start number offset CEN field to when the
|
||||
* actual value is stored in the Zip64 Extra Header
|
||||
*/
|
||||
private static final int ZIP64_MAGICCOUNT = 0xFFFF;
|
||||
|
||||
/**
|
||||
* Copy of the byte array for the ZIP to be modified by a given test run
|
||||
*/
|
||||
private byte[] zipArrayCopy;
|
||||
|
||||
/**
|
||||
* Little-endian ByteBuffer for manipulating the ZIP copy
|
||||
*/
|
||||
private ByteBuffer buffer;
|
||||
|
||||
/**
|
||||
* The DataProvider returning a byte array representing the Zip file,
|
||||
* CEN offsets to set to 0xFFFFFFFF and the expected
|
||||
* ZipException error message when there are missing Zip64 Extra header fields
|
||||
* @return Arguments used in each test run
|
||||
*/
|
||||
private static Stream<Arguments> InvalidZip64MagicValues() {
|
||||
return Stream.of(
|
||||
// Byte array representing the Zip file, compressed size offset,
|
||||
// and expected ZipException Message
|
||||
Arguments.of(ZIP_WITH_ZIP64_EXTRAHDR_SIZE_ONLY_BYTEARRAY,
|
||||
0x61, INVALID_ZIP64_EXTRAHDR_SIZE),
|
||||
// Byte array representing the Zip file, LOC offset and expected ZipException Message
|
||||
Arguments.of(ZIP_WITH_ZIP64_EXTRAHDR_SIZE_ONLY_BYTEARRAY,
|
||||
0x77, INVALID_ZIP64_EXTRAHDR_SIZE),
|
||||
// Byte array representing the Zip file, LOC offset and expected ZipException Message
|
||||
Arguments.of(ZIP_WITH_TWO_ZIP64_HEADER_ENTRIES_BYTEARRAY,
|
||||
0x77, INVALID_ZIP64_EXTRAHDR_SIZE)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* The DataProvider of CEN offsets to set to 0xFFFFFFFF or 0xFFFF when the Extra Length
|
||||
* size is 0 for the Zip file created using ZIP_WITH_NO_EXTRA_LEN_BYTEARRAY
|
||||
* @return Arguments used in each test run
|
||||
*/
|
||||
private static Stream<Arguments> MissingZip64ExtraFieldEntries() {
|
||||
return Stream.of(
|
||||
// Compressed size offset
|
||||
Arguments.of(0x43),
|
||||
// Size offset
|
||||
Arguments.of(0x47),
|
||||
// Disk start number offset
|
||||
Arguments.of(DISKNO_OFFSET_ZIP_NO_EXTRA_LEN),
|
||||
// LOC offset
|
||||
Arguments.of(0x59)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* The DataProvider of CEN offsets to set to 0xFFFFFFFF when the ZIP64 extra header
|
||||
* Length size is 0 for the Zip file created using
|
||||
* ZIP_WITH_ZEROLEN_ZIP64_EXTRAHDR_BYTEARRAY
|
||||
* @return Arguments used in each test run
|
||||
*/
|
||||
private static Stream<Arguments> zip64ZeroLenHeaderExtraFieldEntries() {
|
||||
return Stream.of(
|
||||
// Compressed size offset
|
||||
Arguments.of(0x57),
|
||||
// Size offset
|
||||
Arguments.of(0x5b),
|
||||
// LOC offset
|
||||
Arguments.of(0x6d)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* The DataProvider which will return a byte array representing a
|
||||
* valid Zip file and the expected content for the Zip file entry 'Hello.txt'.
|
||||
* @return Arguments used in each test run
|
||||
*/
|
||||
private static Stream<Arguments> validZipFiles() {
|
||||
return Stream.of(
|
||||
// Byte array representing the Zip file, and the expected entry content
|
||||
Arguments.of(ZIP_WITH_ZIP64_EXTRAHDR_SIZE_ONLY_BYTEARRAY,
|
||||
"Hello Tennis Players"),
|
||||
Arguments.of(ZIP_WITH_TWO_ZIP64_HEADER_ENTRIES_BYTEARRAY,
|
||||
"Hello Tennis Players"),
|
||||
Arguments.of(ZIP_WITH_ZIP64_EXTRAHDR_LOC_ONLY_BYTEARRAY,
|
||||
"Hello Tennis Players"),
|
||||
Arguments.of(ZIP_WITH_ZIP64_EXTRAHDR_CSIZE_ONLY_BYTEARRAY,
|
||||
"Hello Tennis Players"),
|
||||
Arguments.of(ZIP_WITH_ZIP64_EXTRAHDR_ALL_BYTEARRAY,
|
||||
"Hello Tennis Players"),
|
||||
Arguments.of(ZIP_WITH_NO_EXTRA_LEN_BYTEARRAY,
|
||||
"Hello\n")
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initial test setup
|
||||
* @throws IOException if an error occurs
|
||||
*/
|
||||
@BeforeAll
|
||||
public static void setup() throws IOException {
|
||||
Files.deleteIfExists(Path.of(ZIPFILE_NAME));
|
||||
Files.deleteIfExists(Path.of(BAD_ZIP_NAME));
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the Zip file that will be modified by each test
|
||||
* @throws IOException if an error occurs
|
||||
*/
|
||||
@BeforeEach
|
||||
public void beforeEachTestRun() throws IOException {
|
||||
Files.deleteIfExists(Path.of(ZIPFILE_NAME));
|
||||
Files.deleteIfExists(Path.of(BAD_ZIP_NAME));
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that a ZipException is thrown by ZipFile if the Zip64 header
|
||||
* does not contain the required field
|
||||
* @param zipArray Byte array representing the Zip file
|
||||
* @param offset Offset of the CEN Header field to set to 0xFFFFFFFF
|
||||
* @param errorMessage Expected ZipException error message
|
||||
* @throws IOException if an error occurs
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@MethodSource("InvalidZip64MagicValues")
|
||||
public void invalidZip64ExtraHeaderZipFileTest(byte[] zipArray, int offset,
|
||||
String errorMessage) throws IOException {
|
||||
// Set the CEN csize or LOC offset field to 0xFFFFFFFF. There will not
|
||||
// be the expected Zip64 Extra Header field resulting in a ZipException
|
||||
// being thrown
|
||||
zipArrayCopy = zipArray.clone();
|
||||
buffer = ByteBuffer.wrap(zipArrayCopy).order(ByteOrder.LITTLE_ENDIAN);
|
||||
buffer.putInt(offset, (int) ZIP64_MAGICVAL);
|
||||
Files.write(Path.of(BAD_ZIP_NAME), zipArrayCopy);
|
||||
|
||||
ZipException ex = assertThrows(ZipException.class, () -> {
|
||||
openWithZipFile(BAD_ZIP_NAME, ZIP_FILE_ENTRY_NAME, null);
|
||||
});
|
||||
assertTrue(ex.getMessage().equals(errorMessage),
|
||||
"Unexpected ZipException message: " + ex.getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that a ZipException is thrown by Zip FS if the Zip64 header
|
||||
* does not contain the required field
|
||||
* @param zipArray Byte array representing the Zip file
|
||||
* @param offset Offset of the CEN Header field to set to 0xFFFFFFFF
|
||||
* @param errorMessage Expected ZipException error message
|
||||
* @throws IOException if an error occurs
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@MethodSource("InvalidZip64MagicValues")
|
||||
public void invalidZip64ExtraHeaderZipFSTest(byte[] zipArray, int offset,
|
||||
String errorMessage) throws IOException {
|
||||
// Set the CEN csize or LOC offset field to 0xFFFFFFFF. There will not
|
||||
// be the expected Zip64 Extra Header field resulting in a ZipException
|
||||
// being thrown
|
||||
zipArrayCopy = zipArray.clone();
|
||||
buffer = ByteBuffer.wrap(zipArrayCopy).order(ByteOrder.LITTLE_ENDIAN);
|
||||
buffer.putInt(offset, (int)ZIP64_MAGICVAL);
|
||||
Files.write(Path.of(BAD_ZIP_NAME), zipArrayCopy);
|
||||
|
||||
ZipException ex = assertThrows(ZipException.class, () -> {
|
||||
openWithZipFS(BAD_ZIP_NAME, ZIP_FILE_ENTRY_NAME, null);
|
||||
});
|
||||
assertTrue(ex.getMessage().equals(errorMessage),
|
||||
"Unexpected ZipException message: " + ex.getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that ZipFile will throw a ZipException if the CEN
|
||||
* Extra length is 0 and the CEN size, csize, LOC offset field is set to
|
||||
* 0xFFFFFFFF or the disk starting number is set to 0xFFFF
|
||||
* @param offset Offset of the CEN Header field to set to 0xFFFFFFFF or 0xFFFF
|
||||
* @throws IOException if an error occurs
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@MethodSource("MissingZip64ExtraFieldEntries")
|
||||
public void zipFileBadExtraLength(int offset) throws IOException {
|
||||
zipArrayCopy = ZIP_WITH_NO_EXTRA_LEN_BYTEARRAY.clone();
|
||||
buffer = ByteBuffer.wrap(zipArrayCopy).order(ByteOrder.LITTLE_ENDIAN);
|
||||
if (offset == DISKNO_OFFSET_ZIP_NO_EXTRA_LEN) {
|
||||
buffer.putShort(offset, (short) ZIP64_MAGICCOUNT);
|
||||
} else {
|
||||
buffer.putInt(offset, (int) ZIP64_MAGICVAL);
|
||||
}
|
||||
Files.write(Path.of(BAD_ZIP_NAME), zipArrayCopy);
|
||||
|
||||
ZipException ex = assertThrows(ZipException.class, () -> {
|
||||
openWithZipFile(BAD_ZIP_NAME, ZIP_FILE_ENTRY_NAME, null);
|
||||
});
|
||||
assertTrue(ex.getMessage().equals(INVALID_EXTRA_LENGTH),
|
||||
"Unexpected ZipException message: " + ex.getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that ZipFS will throw a ZipException if the CEN
|
||||
* Extra length is 0 and the CEN size, csize, LOC offset field is set to
|
||||
* 0xFFFFFFFF or the disk starting number is set to 0xFFFF
|
||||
* @param offset the offset of the CEN Header field to set to 0xFFFFFFFF or 0xFFFF
|
||||
* @throws IOException if an error occurs
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@MethodSource("MissingZip64ExtraFieldEntries")
|
||||
public void zipFSBadExtraLength(int offset) throws IOException {
|
||||
zipArrayCopy = ZIP_WITH_NO_EXTRA_LEN_BYTEARRAY.clone();
|
||||
buffer = ByteBuffer.wrap(zipArrayCopy).order(ByteOrder.LITTLE_ENDIAN);
|
||||
if (offset == DISKNO_OFFSET_ZIP_NO_EXTRA_LEN) {
|
||||
buffer.putShort(offset, (short) ZIP64_MAGICCOUNT);
|
||||
} else {
|
||||
buffer.putInt(offset, (int) ZIP64_MAGICVAL);
|
||||
}
|
||||
Files.write(Path.of(BAD_ZIP_NAME), zipArrayCopy);
|
||||
|
||||
ZipException ex = assertThrows(ZipException.class, () -> {
|
||||
openWithZipFS(BAD_ZIP_NAME, ZIP_FILE_ENTRY_NAME, null);
|
||||
});
|
||||
assertTrue(ex.getMessage().equals(INVALID_EXTRA_LENGTH),
|
||||
"Unexpected ZipException message: " + ex.getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that ZipFile will throw a ZipException if the ZIP64 extra header
|
||||
* has a size of 0 and the CEN size, csize, or the LOC offset field is set to
|
||||
* 0xFFFFFFFF
|
||||
* @param offset the offset of the CEN Header field to set to 0xFFFFFFFF
|
||||
* @throws IOException if an error occurs
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@MethodSource("zip64ZeroLenHeaderExtraFieldEntries")
|
||||
public void zipFileZeroLenExtraHeader(int offset) throws IOException {
|
||||
zipArrayCopy = ZIP_WITH_ZEROLEN_ZIP64_EXTRAHDR_BYTEARRAY.clone();
|
||||
buffer = ByteBuffer.wrap(zipArrayCopy).order(ByteOrder.LITTLE_ENDIAN);
|
||||
buffer.putInt(offset, (int) ZIP64_MAGICVAL);
|
||||
Files.write(Path.of(BAD_ZIP_NAME), zipArrayCopy);
|
||||
ZipException ex = assertThrows(ZipException.class, () -> {
|
||||
openWithZipFile(BAD_ZIP_NAME, ZIP_FILE_ENTRY_NAME, null);
|
||||
});
|
||||
assertTrue(ex.getMessage().equals(INVALID_ZIP64_EXTRAHDR_SIZE),
|
||||
"Unexpected ZipException message: " + ex.getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that ZipFS will throw a ZipException if the ZIP64 extra header
|
||||
* has a size of 0 and the CEN size, csize, or the LOC offset field is set to
|
||||
* 0xFFFFFFFF
|
||||
* @param offset the offset of the CEN Header field to set to 0xFFFFFFFF
|
||||
* @throws IOException if an error occurs
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@MethodSource("zip64ZeroLenHeaderExtraFieldEntries")
|
||||
public void zipFSZeroLenExtraHeader(int offset) throws IOException {
|
||||
zipArrayCopy = ZIP_WITH_ZEROLEN_ZIP64_EXTRAHDR_BYTEARRAY.clone();
|
||||
buffer = ByteBuffer.wrap(zipArrayCopy).order(ByteOrder.LITTLE_ENDIAN);
|
||||
buffer.putInt(offset, (int) ZIP64_MAGICVAL);
|
||||
Files.write(Path.of(BAD_ZIP_NAME), zipArrayCopy);
|
||||
ZipException ex = assertThrows(ZipException.class, () -> {
|
||||
openWithZipFS(BAD_ZIP_NAME, ZIP_FILE_ENTRY_NAME, null);
|
||||
});
|
||||
assertTrue(ex.getMessage().equals(INVALID_ZIP64_EXTRAHDR_SIZE),
|
||||
"Unexpected ZipException message: " + ex.getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that ZipFile will read the Zip files created from the
|
||||
* byte arrays prior to modifying the arrays to check that the
|
||||
* expected ZipException is thrown.
|
||||
* @param zipFile the byte array which represents the Zip file that should
|
||||
* be opened and read successfully.
|
||||
* @param message the expected text contained within the Zip entry
|
||||
* @throws IOException if an error occurs
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@MethodSource("validZipFiles")
|
||||
public void readValidZipFile(byte[] zipFile, String message) throws IOException {
|
||||
// Write out the Zip file from the byte array
|
||||
Files.write(Path.of(ZIPFILE_NAME), zipFile);
|
||||
openWithZipFile(ZIPFILE_NAME, ZIP_FILE_ENTRY_NAME, message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that ZipFS will read the Zip files created from the
|
||||
* byte arrays prior to modifying the arrays to check that the
|
||||
* expected ZipException is thrown.
|
||||
* @param zipFile the byte array which represents the Zip file that should
|
||||
* be opened and read successfully.
|
||||
* @param message the expected text contained within the Zip entry
|
||||
* @throws IOException if an error occurs
|
||||
*/
|
||||
@ParameterizedTest
|
||||
@MethodSource("validZipFiles")
|
||||
public void readValidZipFileWithZipFs(byte[] zipFile, String message)
|
||||
throws IOException {
|
||||
// Write out the Zip file from the byte array
|
||||
Files.write(Path.of(ZIPFILE_NAME), zipFile);
|
||||
openWithZipFS(ZIPFILE_NAME, ZIP_FILE_ENTRY_NAME, message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility method used to open a Zip file using ZipFile by the tests.
|
||||
* @param zipFile name of the Zip file to open
|
||||
* @param entryName Zip entry to read when the Zip file is expected to be
|
||||
* able to be opened
|
||||
* @param entryContents the expected contents for the Zip entry
|
||||
* @throws IOException if an error occurs
|
||||
*/
|
||||
private static void openWithZipFile(String zipFile, String entryName,
|
||||
String entryContents) throws IOException {
|
||||
try (ZipFile zf = new ZipFile(zipFile)) {
|
||||
ZipEntry ze = zf.getEntry(entryName);
|
||||
try (InputStream is = zf.getInputStream(ze)) {
|
||||
String result = new String(is.readAllBytes());
|
||||
if (DEBUG) {
|
||||
var hx = HexFormat.ofDelimiter(", ").withPrefix("0x");
|
||||
System.out.printf("Error: Zip File read :%s%n[%s]%n", result,
|
||||
hx.formatHex(result.getBytes()));
|
||||
}
|
||||
// entryContents will be null when an exception is expected
|
||||
if (entryContents != null) {
|
||||
assertEquals(entryContents, result);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Utility method used to open a Zip file using ZipFS by the tests.
|
||||
* @param zipFile name of the Zip file to open
|
||||
* @param entryName Zip entry to read when the Zip file is expected to be
|
||||
* able to be opened
|
||||
* @param entryContents the expected contents for the Zip entry
|
||||
* @throws IOException if an error occurs
|
||||
*/
|
||||
private static void openWithZipFS(String zipFile, String entryName,
|
||||
String entryContents) throws IOException {
|
||||
try (FileSystem fs = FileSystems.newFileSystem(
|
||||
Path.of(zipFile), Map.of())) {
|
||||
Path p = fs.getPath(entryName);
|
||||
String result = new String(Files.readAllBytes(p));
|
||||
if (DEBUG) {
|
||||
var hx = HexFormat.ofDelimiter(", ").withPrefix("0x");
|
||||
System.out.printf("Error: Zip FS read :%s%n[%s]%n", result,
|
||||
hx.formatHex(result.getBytes()));
|
||||
}
|
||||
// entryContents will be null when an exception is expected
|
||||
if (entryContents != null) {
|
||||
assertEquals(entryContents, result);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user