|
30 | 30 | import java.io.IOException; |
31 | 31 | import java.io.InputStream; |
32 | 32 | import java.io.PushbackInputStream; |
33 | | -import java.math.BigInteger; |
34 | 33 | import java.nio.ByteBuffer; |
35 | 34 | import java.nio.charset.StandardCharsets; |
36 | | -import java.util.Arrays; |
37 | 35 | import java.util.function.Function; |
38 | 36 | import java.util.zip.CRC32; |
39 | 37 | import java.util.zip.DataFormatException; |
@@ -327,8 +325,6 @@ private <T extends InputStream> T checkInputStream() throws ZipException { |
327 | 325 | private static final byte[] LFH = ZipLong.LFH_SIG.getBytes(); |
328 | 326 | private static final byte[] CFH = ZipLong.CFH_SIG.getBytes(); |
329 | 327 | private static final byte[] DD = ZipLong.DD_SIG.getBytes(); |
330 | | - private static final byte[] APK_SIGNING_BLOCK_MAGIC = { 'A', 'P', 'K', ' ', 'S', 'i', 'g', ' ', 'B', 'l', 'o', 'c', 'k', ' ', '4', '2', }; |
331 | | - private static final BigInteger LONG_MAX = BigInteger.valueOf(Long.MAX_VALUE); |
332 | 328 |
|
333 | 329 | /** |
334 | 330 | * Creates a new builder. |
@@ -838,12 +834,9 @@ public ZipArchiveEntry getNextZipEntry() throws IOException { |
838 | 834 | } |
839 | 835 | final ZipLong sig = new ZipLong(lfhBuf); |
840 | 836 | if (!sig.equals(ZipLong.LFH_SIG)) { |
841 | | - if (sig.equals(ZipLong.CFH_SIG) || sig.equals(ZipLong.AED_SIG) || isApkSigningBlock(lfhBuf) || sig.equals(ZipLong.ZERO)) { |
842 | | - hitCentralDirectory = true; |
843 | | - skipRemainderOfArchive(); |
844 | | - return null; |
845 | | - } |
846 | | - throw new ZipException(String.format("Unexpected record signature: 0x%x", sig.getValue())); |
| 837 | + hitCentralDirectory = true; |
| 838 | + skipRemainderOfArchive(); |
| 839 | + return null; |
847 | 840 | } |
848 | 841 | // off: go past the signature |
849 | 842 | int off = WORD; |
@@ -946,52 +939,6 @@ public long getUncompressedCount() { |
946 | 939 | return uncompressedCount; |
947 | 940 | } |
948 | 941 |
|
949 | | - /** |
950 | | - * Checks whether this might be an APK Signing Block. |
951 | | - * <p> |
952 | | - * Unfortunately the APK signing block does not start with some kind of signature, it rather ends with one. It starts with a length, so what we do is parse |
953 | | - * the suspect length, skip ahead far enough, look for the signature and if we've found it, return true. |
954 | | - * </p> |
955 | | - * |
956 | | - * @param suspectLocalFileHeader the bytes read from the underlying stream in the expectation that they would hold the local file header of the next entry. |
957 | | - * @return true if this looks like an APK signing block. |
958 | | - * @see <a href="https://source.android.com/security/apksigning/v2">https://source.android.com/security/apksigning/v2</a> |
959 | | - */ |
960 | | - private boolean isApkSigningBlock(final byte[] suspectLocalFileHeader) throws IOException { |
961 | | - // length of block excluding the size field itself |
962 | | - final BigInteger len = ZipEightByteInteger.getValue(suspectLocalFileHeader); |
963 | | - // LFH has already been read and all but the first eight bytes contain (part of) the APK signing block, |
964 | | - // also subtract 16 bytes in order to position us at the magic string |
965 | | - BigInteger toSkip = len.add(BigInteger.valueOf(DWORD - suspectLocalFileHeader.length - (long) APK_SIGNING_BLOCK_MAGIC.length)); |
966 | | - final byte[] magic = new byte[APK_SIGNING_BLOCK_MAGIC.length]; |
967 | | - try { |
968 | | - if (toSkip.signum() < 0) { |
969 | | - // suspectLocalFileHeader contains the start of suspect magic string |
970 | | - final int off = suspectLocalFileHeader.length + toSkip.intValue(); |
971 | | - // length was shorter than magic length |
972 | | - if (off < DWORD) { |
973 | | - return false; |
974 | | - } |
975 | | - final int bytesInBuffer = Math.abs(toSkip.intValue()); |
976 | | - System.arraycopy(suspectLocalFileHeader, off, magic, 0, Math.min(bytesInBuffer, magic.length)); |
977 | | - if (bytesInBuffer < magic.length) { |
978 | | - readFully(magic, bytesInBuffer); |
979 | | - } |
980 | | - } else { |
981 | | - while (toSkip.compareTo(LONG_MAX) > 0) { |
982 | | - realSkip(Long.MAX_VALUE); |
983 | | - toSkip = toSkip.add(LONG_MAX.negate()); |
984 | | - } |
985 | | - realSkip(toSkip.longValue()); |
986 | | - readFully(magic); |
987 | | - } |
988 | | - } catch (final EOFException ex) { // NOSONAR |
989 | | - // length was invalid |
990 | | - return false; |
991 | | - } |
992 | | - return Arrays.equals(magic, APK_SIGNING_BLOCK_MAGIC); |
993 | | - } |
994 | | - |
995 | 942 | private boolean isFirstByteOfEocdSig(final int b) { |
996 | 943 | return b == ZipArchiveOutputStream.EOCD_SIG[0]; |
997 | 944 | } |
|
0 commit comments