From 5b71582325a2e9df14f627d731b46a91ee8064c5 Mon Sep 17 00:00:00 2001 From: Ben Gruver Date: Sun, 24 Feb 2013 20:02:58 -0800 Subject: [PATCH] Annotate ProtoIdItems --- .../dexlib2/dexbacked/DexBackedDexFile.java | 30 ++++--- .../jf/dexlib2/dexbacked/raw/ProtoIdItem.java | 82 +++++++++++++++++++ .../dexlib2/dexbacked/raw/StringIdItem.java | 11 +++ .../jf/dexlib2/dexbacked/raw/TypeIdItem.java | 22 +++-- .../reference/DexBackedMethodReference.java | 7 +- 5 files changed, 125 insertions(+), 27 deletions(-) create mode 100644 dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/ProtoIdItem.java diff --git a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/DexBackedDexFile.java b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/DexBackedDexFile.java index 440c52a4..89153ea2 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/DexBackedDexFile.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/DexBackedDexFile.java @@ -32,6 +32,7 @@ package org.jf.dexlib2.dexbacked; import org.jf.dexlib2.dexbacked.raw.HeaderItem; +import org.jf.dexlib2.dexbacked.raw.ProtoIdItem; import org.jf.dexlib2.dexbacked.raw.StringIdItem; import org.jf.dexlib2.dexbacked.raw.TypeIdItem; import org.jf.dexlib2.dexbacked.util.FixedSizeSet; @@ -79,7 +80,7 @@ public abstract class DexBackedDexFile extends BaseDexBuffer implements DexFile private final int classCount; private final int classStartOffset; - 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; @@ -93,9 +94,6 @@ public abstract class DexBackedDexFile extends BaseDexBuffer implements DexFile public static final int METHOD_PROTO_IDX_OFFSET = 2; public static final int METHOD_NAME_IDX_OFFSET = 4; - public static final int PROTO_RETURN_TYPE_IDX_OFFSET = 4; - public static final int PROTO_PARAM_LIST_OFF_OFFSET = 8; - public static final int TYPE_LIST_SIZE_OFFSET = 0; public static final int TYPE_LIST_LIST_OFFSET = 4; @@ -199,7 +197,7 @@ public abstract class DexBackedDexFile extends BaseDexBuffer implements DexFile if (protoIndex < 0 || protoIndex >= protoCount) { throw new ExceptionWithContext("Proto index out of bounds: %d", protoIndex); } - return protoStartOffset + protoIndex*PROTO_ID_ITEM_SIZE; + return protoStartOffset + protoIndex*ProtoIdItem.ITEM_SIZE; } public int getClassDefItemOffset(int classIndex) { @@ -259,13 +257,23 @@ public abstract class DexBackedDexFile extends BaseDexBuffer implements DexFile AnnotatedBytes annotatedBytes = new AnnotatedBytes(width); HeaderItem.getAnnotator().annotateSection(annotatedBytes, this, 1); - annotatedBytes.skipTo(getStringIdItemOffset(0)); - annotatedBytes.annotate(0, " "); - StringIdItem.getAnnotator().annotateSection(annotatedBytes, this, stringCount); + if (stringCount > 0) { + annotatedBytes.skipTo(getStringIdItemOffset(0)); + annotatedBytes.annotate(0, " "); + StringIdItem.getAnnotator().annotateSection(annotatedBytes, this, stringCount); + } - annotatedBytes.skipTo(getTypeIdItemOffset(0)); - annotatedBytes.annotate(0, " "); - TypeIdItem.getAnnotator().annotateSection(annotatedBytes, this, typeCount); + if (typeCount > 0) { + annotatedBytes.skipTo(getTypeIdItemOffset(0)); + annotatedBytes.annotate(0, " "); + TypeIdItem.getAnnotator().annotateSection(annotatedBytes, this, typeCount); + } + + if (protoCount > 0) { + annotatedBytes.skipTo(getProtoIdItemOffset(0)); + annotatedBytes.annotate(0, " "); + ProtoIdItem.getAnnotator().annotateSection(annotatedBytes, this, protoCount); + } annotatedBytes.writeAnnotations(out, buf); } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/ProtoIdItem.java b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/ProtoIdItem.java new file mode 100644 index 00000000..977621d9 --- /dev/null +++ b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/ProtoIdItem.java @@ -0,0 +1,82 @@ +/* + * Copyright 2013, 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.raw; + +import org.jf.dexlib2.dexbacked.DexBackedDexFile; +import org.jf.dexlib2.util.AnnotatedBytes; + +import javax.annotation.Nonnull; + +public class ProtoIdItem { + public static final int ITEM_SIZE = 12; + + public static final int SHORTY_OFFSET = 0; + public static final int RETURN_TYPE_OFFSET = 4; + public static final int PARAMETERS_OFFSET = 8; + + public static SectionAnnotator getAnnotator() { + return new SectionAnnotator() { + @Override + public void annotateSection(@Nonnull AnnotatedBytes out, @Nonnull DexBackedDexFile dexFile, int length) { + if (length > 0) { + out.annotate(0, "-----------------------------"); + out.annotate(0, "proto_id_item section"); + out.annotate(0, "-----------------------------"); + out.annotate(0, ""); + + for (int i=0; i 0) { out.annotate(0, "-----------------------------"); out.annotate(0, "type_id_item section"); @@ -54,7 +53,7 @@ public class TypeIdItem { for (int i=0; i getParameterTypes() { int protoIdItemOffset = getProtoIdItemOffset(); - final int parametersOffset = dexFile.readSmallUint(protoIdItemOffset + - DexBackedDexFile.Impl.PROTO_PARAM_LIST_OFF_OFFSET); + final int parametersOffset = dexFile.readSmallUint(protoIdItemOffset + ProtoIdItem.PARAMETERS_OFFSET); if (parametersOffset > 0) { final int parameterCount = dexFile.readSmallUint(parametersOffset + DexBackedDexFile.Impl.TYPE_LIST_SIZE_OFFSET); @@ -88,8 +88,7 @@ public class DexBackedMethodReference extends BaseMethodReference { @Override public String getReturnType() { int protoIdItemOffset = getProtoIdItemOffset(); - return dexFile.getType(dexFile.readSmallUint(protoIdItemOffset + - DexBackedDexFile.Impl.PROTO_RETURN_TYPE_IDX_OFFSET)); + return dexFile.getType(dexFile.readSmallUint(protoIdItemOffset + ProtoIdItem.RETURN_TYPE_OFFSET)); } private int getProtoIdItemOffset() {