fix(dex-input): use length in header for checksum, fix error in Dex v41 (PR #2711)

* Use length in header for checksum, fix error in Dex v41

* check if length can be read, use utils method to read int values

---------

Co-authored-by: Skylot <118523+skylot@users.noreply.github.com>
This commit is contained in:
Midori Kochiya
2025-12-03 03:14:51 +08:00
committed by GitHub
parent 56ae4a6048
commit ea68024851
@@ -1,22 +1,22 @@
package jadx.plugins.input.dex.utils;
import java.nio.ByteBuffer;
import java.util.zip.Adler32;
import jadx.plugins.input.dex.DexException;
import static java.nio.ByteOrder.LITTLE_ENDIAN;
public class DexCheckSum {
public static void verify(String fileName, byte[] content, int offset) {
int len = content.length;
if (len < 12) {
throw new DexException("Dex file truncated, length: " + len + ", file: " + fileName);
if (offset + 32 + 4 > content.length) {
throw new DexException("Dex file truncated, can't read file length, file: " + fileName);
}
int checksum = ByteBuffer.wrap(content, offset + 8, 4).order(LITTLE_ENDIAN).getInt();
int len = DataReader.readU4(content, offset + 32);
if (offset + len > content.length) {
throw new DexException("Dex file truncated, length in header: " + len + ", file: " + fileName);
}
int checksum = DataReader.readU4(content, offset + 8);
Adler32 adler32 = new Adler32();
adler32.update(content, 12, len - 12);
adler32.update(content, offset + 12, len - 12);
int fileChecksum = (int) adler32.getValue();
if (checksum != fileChecksum) {
throw new DexException(String.format("Bad dex file checksum: 0x%08x, expected: 0x%08x, file: %s",