Annotate ProtoIdItems

This commit is contained in:
Ben Gruver 2013-02-24 20:02:58 -08:00
parent 658dfb0880
commit 5b71582325
5 changed files with 125 additions and 27 deletions

View File

@ -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);
}

View File

@ -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<length; i++) {
out.annotate(0, "[%d] proto_id_item", i);
out.indent();
annotateProto(out, dexFile);
out.deindent();
}
}
}
};
}
private static void annotateProto(@Nonnull AnnotatedBytes out, @Nonnull DexBackedDexFile dexFile) {
int shortyIndex = dexFile.readSmallUint(out.getCursor());
out.annotate(4, "shorty_idx = %s", StringIdItem.getReferenceAnnotation(dexFile, shortyIndex));
int returnTypeIndex = dexFile.readSmallUint(out.getCursor());
out.annotate(4, "return_type_idx = %s", TypeIdItem.getReferenceAnnotation(dexFile, returnTypeIndex));
// TODO: add formatted type list to output
int parametersOffset = dexFile.readSmallUint(out.getCursor());
if (parametersOffset != 0) {
out.annotate(4, "parameters_off = type_list_item[0x%x]", parametersOffset);
} else {
out.annotate(4, "parameters_off = 0");
}
}
}

View File

@ -40,6 +40,7 @@ import javax.annotation.Nonnull;
public class StringIdItem {
public static final int ITEM_SIZE = 4;
public static SectionAnnotator getAnnotator() {
return new SectionAnnotator() {
@Override
@ -77,4 +78,14 @@ public class StringIdItem {
out.annotate(4, "string_id_item[0x%x]", stringDataOffset);
}
public static String getReferenceAnnotation(@Nonnull DexBackedDexFile dexFile, int stringIndex) {
try {
String string = dexFile.getString(stringIndex);
return String.format("string_id_item[%d]: \"%s\"", stringIndex, StringUtils.escapeString(string));
} catch (Exception ex) {
ex.printStackTrace(System.err);
}
return String.format("string_id_item[%d]", stringIndex);
}
}

View File

@ -44,7 +44,6 @@ public class TypeIdItem {
return new SectionAnnotator() {
@Override
public void annotateSection(@Nonnull AnnotatedBytes out, @Nonnull DexBackedDexFile dexFile, int length) {
int startOffset = out.getCursor();
if (length > 0) {
out.annotate(0, "-----------------------------");
out.annotate(0, "type_id_item section");
@ -54,7 +53,7 @@ public class TypeIdItem {
for (int i=0; i<length; i++) {
out.annotate(0, "[%d] type_id_item", i);
out.indent();
annotateString(out, dexFile, i, startOffset + i * ITEM_SIZE);
annotateString(out, dexFile);
out.deindent();
}
}
@ -62,19 +61,18 @@ public class TypeIdItem {
};
}
private static void annotateString(@Nonnull AnnotatedBytes out, @Nonnull DexBackedDexFile dexFile, int index,
int offset) {
int stringIndex = dexFile.readSmallUint(offset);
private static void annotateString(@Nonnull AnnotatedBytes out, @Nonnull DexBackedDexFile dexFile) {
int stringIndex = dexFile.readSmallUint(out.getCursor());
out.annotate(4, StringIdItem.getReferenceAnnotation(dexFile, stringIndex));
}
public static String getReferenceAnnotation(@Nonnull DexBackedDexFile dexFile, int typeIndex) {
try {
String stringValue = dexFile.getString(stringIndex);
out.annotate(4, "string_id_item[%d]: \"%s\"", stringIndex, StringUtils.escapeString(stringValue));
return;
String typeString = dexFile.getType(typeIndex);
return String.format("type_id_item[%d]: \"%s\"", typeIndex, StringUtils.escapeString(typeString));
} catch (Exception ex) {
System.err.print("Error while resolving string value of type at index: ");
System.err.print(index);
ex.printStackTrace(System.err);
}
out.annotate(4, "string_id_item[%d]", stringIndex);
return String.format("type_id_item[%d]", typeIndex);
}
}

View File

@ -34,6 +34,7 @@ package org.jf.dexlib2.dexbacked.reference;
import com.google.common.collect.ImmutableList;
import org.jf.dexlib2.base.reference.BaseMethodReference;
import org.jf.dexlib2.dexbacked.DexBackedDexFile;
import org.jf.dexlib2.dexbacked.raw.ProtoIdItem;
import org.jf.dexlib2.dexbacked.util.FixedSizeList;
import javax.annotation.Nonnull;
@ -66,8 +67,7 @@ public class DexBackedMethodReference extends BaseMethodReference {
@Override
public List<String> 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() {