Skip to content

Commit f4a538e

Browse files
committed
Read unsigned int values as required by Zip spec (#514)
1 parent 5c0857e commit f4a538e

File tree

1 file changed

+17
-24
lines changed

1 file changed

+17
-24
lines changed

src/main/java/nonapi/io/github/classgraph/fastzipfilereader/LogicalZipFile.java

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ private void readCentralDirectory(final NestedJarHandler nestedJarHandler, final
447447
// initially just try reading back a maximum of 32 characters.
448448
long eocdPos = -1;
449449
for (long i = slice.sliceLength - 22, iMin = slice.sliceLength - 22 - 32; i >= iMin && i >= 0L; --i) {
450-
if (reader.readInt(i) == 0x06054b50) {
450+
if (reader.readUnsignedInt(i) == 0x06054b50L) {
451451
eocdPos = i;
452452
break;
453453
}
@@ -466,7 +466,7 @@ private void readCentralDirectory(final NestedJarHandler nestedJarHandler, final
466466
/* inflatedLengthHint = */ 0L, nestedJarHandler)) {
467467
final RandomAccessReader eocdReader = arraySlice.randomAccessReader();
468468
for (long i = eocdBytes.length - 22L; i >= 0L; --i) {
469-
if (eocdReader.readInt(i) == 0x06054b50) {
469+
if (eocdReader.readUnsignedInt(i) == 0x06054b50L) {
470470
eocdPos = i + readStartOff;
471471
break;
472472
}
@@ -491,17 +491,17 @@ private void readCentralDirectory(final NestedJarHandler nestedJarHandler, final
491491

492492
// Check for Zip64 End Of Central Directory Locator record
493493
final long zip64cdLocIdx = eocdPos - 20;
494-
if (zip64cdLocIdx >= 0 && reader.readInt(zip64cdLocIdx) == 0x07064b50) {
495-
if (reader.readInt(zip64cdLocIdx + 4) > 0 || reader.readInt(zip64cdLocIdx + 16) > 1) {
494+
if (zip64cdLocIdx >= 0 && reader.readUnsignedInt(zip64cdLocIdx) == 0x07064b50L) {
495+
if (reader.readUnsignedInt(zip64cdLocIdx + 4) > 0 || reader.readUnsignedInt(zip64cdLocIdx + 16) > 1) {
496496
throw new IOException("Multi-disk jarfiles not supported: " + getPath());
497497
}
498498
final long eocdPos64 = reader.readLong(zip64cdLocIdx + 8);
499-
if (reader.readInt(eocdPos64) != 0x06064b50) {
499+
if (reader.readUnsignedInt(eocdPos64) != 0x06064b50L) {
500500
throw new IOException("Zip64 central directory at location " + eocdPos64
501501
+ " does not have Zip64 central directory header: " + getPath());
502502
}
503503
final long numEnt64 = reader.readLong(eocdPos64 + 24);
504-
if (reader.readInt(eocdPos64 + 16) > 0 || reader.readInt(eocdPos64 + 20) > 0
504+
if (reader.readUnsignedInt(eocdPos64 + 16) > 0 || reader.readUnsignedInt(eocdPos64 + 20) > 0
505505
|| numEnt64 != reader.readLong(eocdPos64 + 32)) {
506506
throw new IOException("Multi-disk jarfiles not supported: " + getPath());
507507
}
@@ -565,10 +565,10 @@ private void readCentralDirectory(final NestedJarHandler nestedJarHandler, final
565565
// numEnt and numEnt64 were inconsistent -- manually count entries
566566
numEnt = 0;
567567
for (long entOff = 0; entOff + 46 <= cenSize;) {
568-
final int sig = cenReader.readInt(entOff);
569-
if (sig != 0x02014b50) {
570-
throw new IOException("Invalid central directory signature: 0x" + Integer.toString(sig, 16)
571-
+ ": " + getPath());
568+
final long sig = cenReader.readUnsignedInt(entOff);
569+
if (sig != 0x02014b50L) {
570+
throw new IOException("Invalid central directory signature: 0x"
571+
+ Integer.toString((int) sig, 16) + ": " + getPath());
572572
}
573573
final int filenameLen = cenReader.readUnsignedShort(entOff + 28);
574574
final int extraFieldLen = cenReader.readUnsignedShort(entOff + 30);
@@ -597,10 +597,10 @@ private void readCentralDirectory(final NestedJarHandler nestedJarHandler, final
597597
try {
598598
int entSize = 0;
599599
for (long entOff = 0; entOff + 46 <= cenSize; entOff += entSize) {
600-
final int sig = cenReader.readInt(entOff);
601-
if (sig != 0x02014b50) {
602-
throw new IOException("Invalid central directory signature: 0x" + Integer.toString(sig, 16)
603-
+ ": " + getPath());
600+
final long sig = cenReader.readUnsignedInt(entOff);
601+
if (sig != 0x02014b50L) {
602+
throw new IOException("Invalid central directory signature: 0x"
603+
+ Integer.toString((int) sig, 16) + ": " + getPath());
604604
}
605605
final int filenameLen = cenReader.readUnsignedShort(entOff + 28);
606606
final int extraFieldLen = cenReader.readUnsignedShort(entOff + 30);
@@ -651,7 +651,7 @@ private void readCentralDirectory(final NestedJarHandler nestedJarHandler, final
651651
// Get external file attributes
652652
final int fileAttributes = cenReader.readUnsignedShort(entOff + 40);
653653

654-
long pos = cenReader.readInt(entOff + 42);
654+
long pos = cenReader.readUnsignedInt(entOff + 42);
655655

656656
// Check for Zip64 header in extra fields
657657
// See:
@@ -700,7 +700,7 @@ private void readCentralDirectory(final NestedJarHandler nestedJarHandler, final
700700

701701
} else if (tag == 0x5455 && size >= 5) {
702702
// Extended Unix timestamp
703-
final byte bits = cenReader.readByte(tagOff + 4 + 0);
703+
final int bits = cenReader.readUnsignedByte(tagOff + 4 + 0);
704704
if ((bits & 1) == 1 && size >= 5 + 8) {
705705
lastModifiedMillis = cenReader.readLong(tagOff + 4 + 1) * 1000L;
706706
}
@@ -715,7 +715,7 @@ private void readCentralDirectory(final NestedJarHandler nestedJarHandler, final
715715

716716
} else if (tag == 0x7075) {
717717
// Info-ZIP Unicode path extra field
718-
final byte version = cenReader.readByte(tagOff + 4 + 0);
718+
final int version = cenReader.readUnsignedByte(tagOff + 4 + 0);
719719
if (version != 1) {
720720
throw new IOException("Unknown Unicode entry name format " + version
721721
+ " in extra field: " + entryNameSanitized);
@@ -777,13 +777,6 @@ private void readCentralDirectory(final NestedJarHandler nestedJarHandler, final
777777
continue;
778778
}
779779

780-
if (getPhysicalFile().getName().endsWith("bigjar.jar")) {
781-
System.out.println(entryNameSanitized);
782-
if (entryNameSanitized.equals("aa/output16.bin")) {
783-
System.out.println("here");
784-
}
785-
}
786-
787780
// Add zip entry
788781
final FastZipEntry entry = new FastZipEntry(this, locHeaderPos, entryNameSanitized, isDeflated,
789782
compressedSize, uncompressedSize, lastModifiedMillis, lastModifiedTimeMSDOS,

0 commit comments

Comments
 (0)