Added support for getting dex file stats: checkum, signature, map.

This commit is contained in:
Izzat Bahadirov 2012-12-20 18:13:11 -05:00 committed by Ben Gruver
parent 8aef982e0c
commit fcd1286d41
3 changed files with 108 additions and 0 deletions

View File

@ -31,10 +31,12 @@
package org.jf.dexlib2.dexbacked;
import org.jf.dexlib2.dexbacked.util.FixedSizeList;
import org.jf.dexlib2.dexbacked.util.FixedSizeSet;
import org.jf.dexlib2.iface.DexFile;
import javax.annotation.Nonnull;
import java.util.List;
import java.util.Set;
public class DexBackedDexFile implements DexFile {
@ -63,4 +65,30 @@ public class DexBackedDexFile implements DexFile {
}
};
}
public int getChecksum() {
return dexBuf.getChecksum();
}
public byte[] getSignature() {
return dexBuf.getSignature();
}
public List<DexBackedMapItem> getMap() {
final int mapOffset = dexBuf.getMapOffset();
final int sectionCount = dexBuf.readSmallUint(mapOffset);
return new FixedSizeList<DexBackedMapItem>() {
@Override
public DexBackedMapItem readItem(int index) {
int mapItemOffset = mapOffset + index * DexBuffer.MAP_ITEM_SIZE;
return new DexBackedMapItem(dexBuf, mapItemOffset);
}
@Override
public int size() {
return sectionCount;
}
};
}
}

View File

@ -0,0 +1,61 @@
/*
* Copyright 2012, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.jf.dexlib2.dexbacked;
import javax.annotation.Nonnull;
public class DexBackedMapItem {
@Nonnull public final DexBuffer dexBuf;
private final int mapItemOffset;
private static final int TYPE_OFFSET = 0;
private static final int SIZE_OFFSET = 4;
private static final int OFFSET_OFFSET = 8;
DexBackedMapItem(@Nonnull DexBuffer dexBuf,
int mapItemOffset) {
this.dexBuf = dexBuf;
this.mapItemOffset = mapItemOffset;
}
public int getType() {
return dexBuf.readUshort(mapItemOffset + TYPE_OFFSET);
}
public int getNumItems() {
return dexBuf.readSmallUint(mapItemOffset + SIZE_OFFSET);
}
public int getOffset() {
return dexBuf.readSmallUint(mapItemOffset + OFFSET_OFFSET);
}
}

View File

@ -37,6 +37,7 @@ import org.jf.util.Utf8Utils;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Arrays;
public class DexBuffer {
// TODO: consider using a direct ByteBuffer instead
@ -61,7 +62,10 @@ public class DexBuffer {
private static final int LITTLE_ENDIAN_TAG = 0x12345678;
private static final int BIG_ENDIAN_TAG = 0x78563412;
private static final int CHECKSUM_OFFSET = 8;
private static final int SIGNATURE_OFFSET = 12;
private static final int ENDIAN_TAG_OFFSET = 40;
private static final int MAP_OFFSET = 52;
private static final int STRING_COUNT_OFFSET = 56;
private static final int STRING_START_OFFSET = 60;
private static final int TYPE_COUNT_OFFSET = 64;
@ -75,12 +79,15 @@ public class DexBuffer {
private static final int CLASS_COUNT_OFFSET = 96;
private static final int CLASS_START_OFFSET = 100;
private static final int SIGNATURE_SIZE = 20;
private static final int STRING_ID_ITEM_SIZE = 4;
private static final int TYPE_ID_ITEM_SIZE = 4;
private static final int PROTO_ID_ITEM_SIZE = 12;
private static final int FIELD_ID_ITEM_SIZE = 8;
private static final int METHOD_ID_ITEM_SIZE = 8;
private static final int CLASS_DEF_ITEM_SIZE = 32;
public static final int MAP_ITEM_SIZE = 12;
public static final int FIELD_CLASS_IDX_OFFSET = 0;
public static final int FIELD_TYPE_IDX_OFFSET = 2;
@ -164,6 +171,18 @@ public class DexBuffer {
}
}
public int getChecksum() {
return readInt(CHECKSUM_OFFSET);
}
public byte[] getSignature() {
return Arrays.copyOfRange(this.buf, SIGNATURE_OFFSET, SIGNATURE_OFFSET + SIGNATURE_SIZE);
}
public int getMapOffset() {
return readSmallUint(MAP_OFFSET);
}
public int getStringIdItemOffset(int stringIndex) {
if (stringIndex < 0 || stringIndex >= stringCount) {
throw new ExceptionWithContext("String index out of bounds: %d", stringIndex);