From 94abcd3332bbc12b4f1099bdd5d93ea8b6fb9c89 Mon Sep 17 00:00:00 2001 From: Ben Gruver Date: Tue, 12 Jun 2012 13:34:26 -0700 Subject: [PATCH] Add the ability to retrieve the checksum and signature from the header_item --- .../main/java/org/jf/dexlib/HeaderItem.java | 40 ++++++++++++++++++- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/dexlib/src/main/java/org/jf/dexlib/HeaderItem.java b/dexlib/src/main/java/org/jf/dexlib/HeaderItem.java index 57e0a130..cbbbe9bc 100644 --- a/dexlib/src/main/java/org/jf/dexlib/HeaderItem.java +++ b/dexlib/src/main/java/org/jf/dexlib/HeaderItem.java @@ -28,6 +28,7 @@ package org.jf.dexlib; +import com.google.common.base.Preconditions; import org.jf.dexlib.Util.AnnotatedOutput; import org.jf.dexlib.Util.Input; import org.jf.dexlib.Util.Utf8Utils; @@ -52,6 +53,10 @@ public class HeaderItem extends Item { /* Which magic value to use when writing out the header item */ private int magic_index = 0; + private boolean checksumSignatureSet = false; + private int checksum; + private byte[] signature; + /** * Create a new uninitialized HeaderItem * @param dexFile The DexFile containing this HeaderItem @@ -85,8 +90,10 @@ public class HeaderItem extends Item { throw new RuntimeException("Unrecognized dex magic value"); } - in.readBytes(20); //checksum - in.readInt(); //signature + checksum = in.readInt(); //checksum + signature = in.readBytes(20); //signature + checksumSignatureSet = true; + in.readInt(); //filesize if (in.readInt() != HEADER_SIZE) { throw new RuntimeException("The header size is not the expected value (0x70)"); @@ -264,4 +271,33 @@ public class HeaderItem extends Item { //there is only 1 header item return 0; } + + /** + * Get the checksum that was originally stored as part of this header item + * + * Note that this should only be called if this HeaderItem is from a DexFile that was read from disk, as opposed + * to one that is created from scratch. + * + * @return The addler32 checksum (as an integer) of the dex file + */ + public int getChecksum() { + Preconditions.checkState(checksumSignatureSet, + "This can only be called on a DexFile that was read from disk."); + return checksum; + } + + /** + * Get the signature that was originally stored as part of this header item + * + * Note that this should only be called if this HeaderItem is from a DexFile that was read from disk, as opposed + * to one that is created from scratch. + * + * @return The sha1 checksum of the dex file, as a 20-element byte array + */ + public byte[] getSignature() { + Preconditions.checkState(checksumSignatureSet, + "This can only be called on a DexFile that was read from disk."); + return signature; + } + }