Skip to content

Commit

Permalink
Include diskNumberStart in zip64 header check and fix invalid code
Browse files Browse the repository at this point in the history
  • Loading branch information
srikanth-lingala committed Jan 26, 2020
1 parent 9aea97f commit 97b9624
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
12 changes: 7 additions & 5 deletions src/main/java/net/lingala/zip4j/headers/HeaderReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,8 @@ private void readZip64ExtendedInfo(FileHeader fileHeader, RawIO rawIO) throws Zi
}

Zip64ExtendedInfo zip64ExtendedInfo = readZip64ExtendedInfo(fileHeader.getExtraDataRecords(), rawIO,
fileHeader.getUncompressedSize(), fileHeader.getCompressedSize(), fileHeader.getOffsetLocalHeader());
fileHeader.getUncompressedSize(), fileHeader.getCompressedSize(), fileHeader.getOffsetLocalHeader(),
fileHeader.getDiskNumberStart());

if (zip64ExtendedInfo == null) {
return;
Expand Down Expand Up @@ -461,7 +462,7 @@ private void readZip64ExtendedInfo(LocalFileHeader localFileHeader, RawIO rawIO)
}

Zip64ExtendedInfo zip64ExtendedInfo = readZip64ExtendedInfo(localFileHeader.getExtraDataRecords(), rawIO,
localFileHeader.getUncompressedSize(), localFileHeader.getCompressedSize(), 0);
localFileHeader.getUncompressedSize(), localFileHeader.getCompressedSize(), 0, 0);

if (zip64ExtendedInfo == null) {
return;
Expand All @@ -479,7 +480,8 @@ private void readZip64ExtendedInfo(LocalFileHeader localFileHeader, RawIO rawIO)
}

private Zip64ExtendedInfo readZip64ExtendedInfo(List<ExtraDataRecord> extraDataRecords, RawIO rawIO,
long uncompressedSize, long compressedSize, long offsetLocalHeader) {
long uncompressedSize, long compressedSize, long offsetLocalHeader,
int diskNumberStart) {

for (ExtraDataRecord extraDataRecord : extraDataRecords) {
if (extraDataRecord == null) {
Expand All @@ -501,7 +503,7 @@ private Zip64ExtendedInfo readZip64ExtendedInfo(List<ExtraDataRecord> extraDataR
counter += 8;
}

if ( counter < extraDataRecord.getSizeOfData() && compressedSize == ZIP_64_SIZE_LIMIT) {
if (counter < extraDataRecord.getSizeOfData() && compressedSize == ZIP_64_SIZE_LIMIT) {
zip64ExtendedInfo.setCompressedSize(rawIO.readLongLittleEndian(extraData, counter));
counter += 8;
}
Expand All @@ -511,7 +513,7 @@ private Zip64ExtendedInfo readZip64ExtendedInfo(List<ExtraDataRecord> extraDataR
counter += 8;
}

if (counter < extraDataRecord.getSizeOfData() && offsetLocalHeader == ZIP_64_NUMBER_OF_ENTRIES_LIMIT) {
if (counter < extraDataRecord.getSizeOfData() && diskNumberStart == ZIP_64_NUMBER_OF_ENTRIES_LIMIT) {
zip64ExtendedInfo.setDiskNumberStart(rawIO.readIntLittleEndian(extraData, counter));
}

Expand Down
20 changes: 11 additions & 9 deletions src/main/java/net/lingala/zip4j/headers/HeaderWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.util.List;

import static net.lingala.zip4j.util.FileUtils.getZipFileNameWithoutExtension;
import static net.lingala.zip4j.util.InternalZipConstants.ZIP_64_NUMBER_OF_ENTRIES_LIMIT;
import static net.lingala.zip4j.util.InternalZipConstants.ZIP_64_SIZE_LIMIT;
import static net.lingala.zip4j.util.Zip4jUtil.isStringNotNullAndNotEmpty;

Expand All @@ -48,6 +49,7 @@ public class HeaderWriter {

private RawIO rawIO = new RawIO();
private byte[] longBuff = new byte[8];
private byte[] intBuff = new byte[4];

public void writeLocalFileHeader(ZipModel zipModel, LocalFileHeader localFileHeader, OutputStream outputStream,
Charset charset) throws IOException {
Expand Down Expand Up @@ -389,11 +391,7 @@ private void writeFileHeader(ZipModel zipModel, FileHeader fileHeader, ByteArray

try {
final byte[] emptyShortByte = {0, 0};
boolean writeZip64ExtendedInfo = false;

if (isZip64Entry(fileHeader)) {
writeZip64ExtendedInfo = true;
}
boolean writeZip64ExtendedInfo = isZip64Entry(fileHeader);

rawIO.writeIntLittleEndian(byteArrayOutputStream, (int) fileHeader.getSignature().getValue());
rawIO.writeShortLittleEndian(byteArrayOutputStream, fileHeader.getVersionMadeBy());
Expand Down Expand Up @@ -446,10 +444,13 @@ private void writeFileHeader(ZipModel zipModel, FileHeader fileHeader, ByteArray
}
rawIO.writeShortLittleEndian(byteArrayOutputStream, fileCommentBytes.length);

//Skip disk number start for now
rawIO.writeShortLittleEndian(byteArrayOutputStream, fileHeader.getDiskNumberStart());
if (writeZip64ExtendedInfo) {
rawIO.writeIntLittleEndian(intBuff, 0, ZIP_64_NUMBER_OF_ENTRIES_LIMIT);
byteArrayOutputStream.write(intBuff, 0, 2);
} else {
rawIO.writeShortLittleEndian(byteArrayOutputStream, fileHeader.getDiskNumberStart());
}

//Skip internal file attributes for now
byteArrayOutputStream.write(emptyShortByte);

//External file attributes
Expand Down Expand Up @@ -660,6 +661,7 @@ private long countNumberOfFileHeaderEntriesOnDisk(List<FileHeader> fileHeaders,
private boolean isZip64Entry(FileHeader fileHeader) {
return fileHeader.getCompressedSize() >= ZIP_64_SIZE_LIMIT
|| fileHeader.getUncompressedSize() >= ZIP_64_SIZE_LIMIT
|| fileHeader.getOffsetLocalHeader() >= ZIP_64_SIZE_LIMIT;
|| fileHeader.getOffsetLocalHeader() >= ZIP_64_SIZE_LIMIT
|| fileHeader.getDiskNumberStart() >= ZIP_64_NUMBER_OF_ENTRIES_LIMIT;
}
}

0 comments on commit 97b9624

Please sign in to comment.