diff --git a/baksmali/src/main/java/org/jf/baksmali/dump.java b/baksmali/src/main/java/org/jf/baksmali/dump.java index afe48243..99125429 100644 --- a/baksmali/src/main/java/org/jf/baksmali/dump.java +++ b/baksmali/src/main/java/org/jf/baksmali/dump.java @@ -41,7 +41,6 @@ public class dump { if (dumpFileName != null) { FileWriter writer = null; - try { writer = new FileWriter(dumpFileName); @@ -50,7 +49,7 @@ public class dump { consoleWidth = 120; } - dexFile.dumpTo(writer, consoleWidth); + dexFile.asRaw().dumpTo(writer, consoleWidth); } catch (IOException ex) { System.err.println("There was an error while dumping the dex file to " + dumpFileName); ex.printStackTrace(System.err); 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 2a89cf62..78222e26 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/DexBackedDexFile.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/DexBackedDexFile.java @@ -34,14 +34,11 @@ package org.jf.dexlib2.dexbacked; import org.jf.dexlib2.dexbacked.raw.*; import org.jf.dexlib2.dexbacked.util.FixedSizeSet; import org.jf.dexlib2.iface.DexFile; -import org.jf.dexlib2.util.AnnotatedBytes; import org.jf.util.ExceptionWithContext; import org.jf.util.Utf8Utils; import javax.annotation.Nonnull; import javax.annotation.Nullable; -import java.io.IOException; -import java.io.Writer; import java.util.Set; public abstract class DexBackedDexFile extends BaseDexBuffer implements DexFile { @@ -61,7 +58,9 @@ public abstract class DexBackedDexFile extends BaseDexBuffer implements DexFile @Override @Nonnull public abstract DexReader readerAt(int offset); - public abstract void dumpTo(Writer out, int width) throws IOException; + @Nonnull public RawDexFile asRaw() { + return new RawDexFile(buf); + } public static class Impl extends DexBackedDexFile { private final int stringCount; @@ -77,11 +76,6 @@ public abstract class DexBackedDexFile extends BaseDexBuffer implements DexFile private final int classCount; private final int classStartOffset; - public static final int MAP_ITEM_SIZE = 12; - - public static final int TYPE_LIST_SIZE_OFFSET = 0; - public static final int TYPE_LIST_LIST_OFFSET = 4; - public Impl(@Nonnull byte[] buf) { super(buf); @@ -237,48 +231,5 @@ public abstract class DexBackedDexFile extends BaseDexBuffer implements DexFile public DexReader readerAt(int offset) { return new DexReader(this, offset); } - - public void dumpTo(Writer out, int width) throws IOException { - AnnotatedBytes annotatedBytes = new AnnotatedBytes(width); - HeaderItem.getAnnotator().annotateSection(annotatedBytes, this, 1); - - if (stringCount > 0) { - annotatedBytes.skipTo(getStringIdItemOffset(0)); - annotatedBytes.annotate(0, " "); - StringIdItem.getAnnotator().annotateSection(annotatedBytes, this, stringCount); - } - - 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); - } - - if (fieldCount > 0) { - annotatedBytes.skipTo(getFieldIdItemOffset(0)); - annotatedBytes.annotate(0, " "); - FieldIdItem.getAnnotator().annotateSection(annotatedBytes, this, fieldCount); - } - - if (methodCount > 0) { - annotatedBytes.skipTo(getMethodIdItemOffset(0)); - annotatedBytes.annotate(0, " "); - MethodIdItem.getAnnotator().annotateSection(annotatedBytes, this, methodCount); - } - - if (classCount > 0) { - annotatedBytes.skipTo(getClassDefItemOffset(0)); - annotatedBytes.annotate(0, " "); - ClassDefItem.getAnnotator().annotateSection(annotatedBytes, this, classCount); - } - - annotatedBytes.writeAnnotations(out, buf); - } } } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/DexBackedMethod.java b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/DexBackedMethod.java index 13cee37f..610e6bd7 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/DexBackedMethod.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/DexBackedMethod.java @@ -34,6 +34,7 @@ package org.jf.dexlib2.dexbacked; import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterators; import org.jf.dexlib2.base.reference.BaseMethodReference; +import org.jf.dexlib2.dexbacked.raw.TypeListItem; import org.jf.dexlib2.dexbacked.util.AnnotationsDirectory; import org.jf.dexlib2.dexbacked.util.FixedSizeList; import org.jf.dexlib2.dexbacked.util.ParameterIterator; @@ -161,9 +162,8 @@ public class DexBackedMethod extends BaseMethodReference implements Method { public List getParameterTypes() { final int parametersOffset = getParametersOffset(); if (parametersOffset > 0) { - final int parameterCount = dexFile.readSmallUint(parametersOffset + - DexBackedDexFile.Impl.TYPE_LIST_SIZE_OFFSET); - final int paramListStart = parametersOffset + DexBackedDexFile.Impl.TYPE_LIST_LIST_OFFSET; + final int parameterCount = dexFile.readSmallUint(parametersOffset + TypeListItem.SIZE_OFFSET); + final int paramListStart = parametersOffset + TypeListItem.LIST_OFFSET; return new FixedSizeList() { @Nonnull @Override diff --git a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/ClassDefItem.java b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/ClassDefItem.java index 449394d7..6bbcd42b 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/ClassDefItem.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/ClassDefItem.java @@ -31,6 +31,8 @@ package org.jf.dexlib2.dexbacked.raw; +import com.google.common.base.Joiner; +import org.jf.dexlib2.AccessFlags; import org.jf.dexlib2.dexbacked.DexBackedDexFile; import org.jf.dexlib2.util.AnnotatedBytes; @@ -61,7 +63,8 @@ public class ClassDefItem { out.annotate(4, "class_idx = %s", TypeIdItem.getReferenceAnnotation(dexFile, classIndex)); int accessFlags = dexFile.readInt(out.getCursor()); - out.annotate(4, "access_flags = 0x%x", accessFlags); + out.annotate(4, "access_flags = 0x%x: %s", accessFlags, + Joiner.on('|').join(AccessFlags.getAccessFlagsForClass(accessFlags))); int superclassIndex = dexFile.readSmallUint(out.getCursor()); out.annotate(4, "superclass_idx = %s", TypeIdItem.getReferenceAnnotation(dexFile, superclassIndex)); diff --git a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/HeaderItem.java b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/HeaderItem.java index 1dfff829..1ab799f4 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/HeaderItem.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/HeaderItem.java @@ -52,6 +52,8 @@ public class HeaderItem { public static final int SIGNATURE_OFFSET = 12; public static final int SIGNATURE_SIZE = 20; + public static final int HEADER_SIZE_OFFSET = 36; + public static final int ENDIAN_TAG_OFFSET = 40; public static final int MAP_OFFSET = 52; @@ -74,6 +76,68 @@ public class HeaderItem { public static final int CLASS_COUNT_OFFSET = 96; public static final int CLASS_START_OFFSET = 100; + @Nonnull private DexBackedDexFile dexFile; + + public HeaderItem(@Nonnull DexBackedDexFile dexFile) { + this.dexFile = dexFile; + } + + public int getMapOffset() { + return dexFile.readSmallUint(MAP_OFFSET); + } + + public int getHeaderSize() { + return dexFile.readSmallUint(HEADER_SIZE_OFFSET); + } + + public int getStringCount() { + return dexFile.readSmallUint(STRING_COUNT_OFFSET); + } + + public int getStringOffset() { + return dexFile.readSmallUint(STRING_START_OFFSET); + } + + public int getTypeCount() { + return dexFile.readSmallUint(TYPE_COUNT_OFFSET); + } + + public int getTypeOffset() { + return dexFile.readSmallUint(TYPE_START_OFFSET); + } + + public int getProtoCount() { + return dexFile.readSmallUint(PROTO_COUNT_OFFSET); + } + + public int getProtoOffset() { + return dexFile.readSmallUint(PROTO_START_OFFSET); + } + + public int getFieldCount() { + return dexFile.readSmallUint(FIELD_COUNT_OFFSET); + } + + public int getFieldOffset() { + return dexFile.readSmallUint(FIELD_START_OFFSET); + } + + public int getMethodCount() { + return dexFile.readSmallUint(METHOD_COUNT_OFFSET); + } + + public int getMethodOffset() { + return dexFile.readSmallUint(METHOD_START_OFFSET); + } + + public int getClassCount() { + return dexFile.readSmallUint(CLASS_COUNT_OFFSET); + } + + public int getClassOffset() { + return dexFile.readSmallUint(CLASS_START_OFFSET); + } + @Nonnull public static SectionAnnotator getAnnotator() { return new SectionAnnotator() { diff --git a/dexlib2/src/main/java/org/jf/dexlib2/writer/DexItemType.java b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/ItemType.java similarity index 68% rename from dexlib2/src/main/java/org/jf/dexlib2/writer/DexItemType.java rename to dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/ItemType.java index ba3f67ff..c7c8ed3a 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/writer/DexItemType.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/ItemType.java @@ -29,9 +29,11 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -package org.jf.dexlib2.writer; +package org.jf.dexlib2.dexbacked.raw; -public class DexItemType { +import javax.annotation.Nonnull; + +public class ItemType { public static final int HEADER_ITEM = 0x0000; public static final int STRING_ID_ITEM = 0x0001; public static final int TYPE_ID_ITEM = 0x0002; @@ -51,27 +53,28 @@ public class DexItemType { public static final int ENCODED_ARRAY_ITEM = 0x2005; public static final int ANNOTATION_DIRECTORY_ITEM = 0x2006; + @Nonnull public static String getItemTypeName(int itemType) { switch (itemType) { - case HEADER_ITEM: return "HEADER_ITEM"; - case STRING_ID_ITEM: return "STRING_ID_ITEM"; - case TYPE_ID_ITEM: return "TYPE_ID_ITEM"; - case PROTO_ID_ITEM: return "PROTO_ID_ITEM"; - case FIELD_ID_ITEM: return "FIELD_ID_ITEM"; - case METHOD_ID_ITEM: return "METHOD_ID_ITEM"; - case CLASS_DEF_ITEM: return "CLASS_DEF_ITEM"; - case MAP_LIST: return "MAP_LIST"; - case TYPE_LIST: return "TYPE_LIST"; - case ANNOTATION_SET_REF_LIST: return "ANNOTATION_SET_REF_LIST"; - case ANNOTATION_SET_ITEM: return "ANNOTATION_SET_ITEM"; - case CLASS_DATA_ITEM: return "CLASS_DATA_ITEM"; - case CODE_ITEM: return "CODE_ITEM"; - case STRING_DATA_ITEM: return "STRING_DATA_ITEM"; - case DEBUG_INFO_ITEM: return "DEBUG_INFO_ITEM"; - case ANNOTATION_ITEM: return "ANNOTATION_ITEM"; - case ENCODED_ARRAY_ITEM: return "ENCODED_ARRAY_ITEM"; - case ANNOTATION_DIRECTORY_ITEM: return "ANNOTATION_DIRECTORY_ITEM"; - default: return "Unknown dex item type"; + case HEADER_ITEM: return "header_item"; + case STRING_ID_ITEM: return "string_id_item"; + case TYPE_ID_ITEM: return "type_id_item"; + case PROTO_ID_ITEM: return "proto_id_item"; + case FIELD_ID_ITEM: return "field_id_item"; + case METHOD_ID_ITEM: return "method_id_item"; + case CLASS_DEF_ITEM: return "class_def_item"; + case MAP_LIST: return "map_list"; + case TYPE_LIST: return "type_list"; + case ANNOTATION_SET_REF_LIST: return "annotation_set_ref_list"; + case ANNOTATION_SET_ITEM: return "annotation_set_item"; + case CLASS_DATA_ITEM: return "class_data_item"; + case CODE_ITEM: return "code_item"; + case STRING_DATA_ITEM: return "string_data_item"; + case DEBUG_INFO_ITEM: return "debug_info_item"; + case ANNOTATION_ITEM: return "annotation_item"; + case ENCODED_ARRAY_ITEM: return "encoded_array_item"; + case ANNOTATION_DIRECTORY_ITEM: return "annotation_directory_item"; + default: return "unknown dex item type"; } } } diff --git a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/MapItem.java b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/MapItem.java new file mode 100644 index 00000000..2dd27ea6 --- /dev/null +++ b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/MapItem.java @@ -0,0 +1,69 @@ +/* + * 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 javax.annotation.Nonnull; + +public class MapItem { + public static final int ITEM_SIZE = 12; + + public static final int TYPE_OFFSET = 0; + public static final int SIZE_OFFSET = 4; + public static final int OFFSET_OFFSET = 8; + + private final DexBackedDexFile dexFile; + private final int offset; + + public MapItem(DexBackedDexFile dexFile, int offset) { + this.dexFile = dexFile; + this.offset = offset; + } + + public int getType() { + return dexFile.readUshort(offset + TYPE_OFFSET); + } + + @Nonnull + public String getName() { + return ItemType.getItemTypeName(getType()); + } + + public int getItemCount() { + return dexFile.readSmallUint(offset + SIZE_OFFSET); + } + + public int getOffset() { + return dexFile.readSmallUint(offset + OFFSET_OFFSET); + } +} diff --git a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/RawDexFile.java b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/RawDexFile.java new file mode 100644 index 00000000..e2e3a2bd --- /dev/null +++ b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/RawDexFile.java @@ -0,0 +1,138 @@ +/* + * 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 com.google.common.collect.ImmutableMap; +import org.jf.dexlib2.dexbacked.DexBackedDexFile; +import org.jf.dexlib2.dexbacked.util.FixedSizeList; +import org.jf.dexlib2.util.AnnotatedBytes; + +import javax.annotation.Nonnull; +import java.io.IOException; +import java.io.Writer; +import java.util.List; +import java.util.Map; + +public class RawDexFile { + @Nonnull private final byte[] buf; + @Nonnull public final DexBackedDexFile dexFile; + @Nonnull public final HeaderItem headerItem; + + public RawDexFile(byte[] buf) { + this.buf = buf; + this.dexFile = new DexBackedDexFile.Impl(buf); + this.headerItem = new HeaderItem(dexFile); + } + + public int getMapOffset() { + return headerItem.getMapOffset(); + } + + public List getMapItems() { + final int mapOffset = getMapOffset(); + final int mapSize = dexFile.readSmallUint(mapOffset); + + return new FixedSizeList() { + @Override + public MapItem readItem(int index) { + int mapItemOffset = mapOffset + 4 + index * MapItem.ITEM_SIZE; + return new MapItem(dexFile, mapItemOffset); + } + + @Override public int size() { + return mapSize; + } + }; + } + + private static final Map annotators; + static { + annotators = ImmutableMap.of(ItemType.TYPE_LIST, TypeListItem.getAnnotator()); + } + + public void dumpTo(Writer out, int width) throws IOException { + AnnotatedBytes annotatedBytes = new AnnotatedBytes(width); + HeaderItem.getAnnotator().annotateSection(annotatedBytes, dexFile, 1); + + int stringCount = headerItem.getStringCount(); + if (stringCount > 0) { + annotatedBytes.skipTo(headerItem.getStringOffset()); + annotatedBytes.annotate(0, " "); + StringIdItem.getAnnotator().annotateSection(annotatedBytes, dexFile, stringCount); + } + + int typeCount = headerItem.getTypeCount(); + if (typeCount > 0) { + annotatedBytes.skipTo(headerItem.getTypeOffset()); + annotatedBytes.annotate(0, " "); + TypeIdItem.getAnnotator().annotateSection(annotatedBytes, dexFile, typeCount); + } + + int protoCount = headerItem.getProtoCount(); + if (protoCount > 0) { + annotatedBytes.skipTo(headerItem.getProtoOffset()); + annotatedBytes.annotate(0, " "); + ProtoIdItem.getAnnotator().annotateSection(annotatedBytes, dexFile, protoCount); + } + + int fieldCount = headerItem.getFieldCount(); + if (fieldCount > 0) { + annotatedBytes.skipTo(headerItem.getFieldOffset()); + annotatedBytes.annotate(0, " "); + FieldIdItem.getAnnotator().annotateSection(annotatedBytes, dexFile, fieldCount); + } + + int methodCount = headerItem.getMethodCount(); + if (methodCount > 0) { + annotatedBytes.skipTo(headerItem.getMethodOffset()); + annotatedBytes.annotate(0, " "); + MethodIdItem.getAnnotator().annotateSection(annotatedBytes, dexFile, methodCount); + } + + int classCount = headerItem.getClassCount(); + if (classCount > 0) { + annotatedBytes.skipTo(headerItem.getClassOffset()); + annotatedBytes.annotate(0, " "); + ClassDefItem.getAnnotator().annotateSection(annotatedBytes, dexFile, classCount); + } + + for (MapItem mapItem: getMapItems()) { + SectionAnnotator annotator = annotators.get(mapItem.getType()); + if (annotator != null) { + annotatedBytes.skipTo(mapItem.getOffset()); + annotator.annotateSection(annotatedBytes, dexFile, mapItem.getItemCount()); + } + } + + annotatedBytes.writeAnnotations(out, buf); + } +} diff --git a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/SectionAnnotator.java b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/SectionAnnotator.java index c6872e44..90f7b3ea 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/SectionAnnotator.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/SectionAnnotator.java @@ -33,6 +33,7 @@ package org.jf.dexlib2.dexbacked.raw; import org.jf.dexlib2.dexbacked.DexBackedDexFile; import org.jf.dexlib2.util.AnnotatedBytes; +import org.jf.util.AlignmentUtils; import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -46,17 +47,20 @@ public abstract class SectionAnnotator { * * @param out The AnnotatedBytes object to annotate to * @param dexFile The DexBackedDexFile representing the dex file being annotated - * @param length The number of items in the section (from the header/map) + * @param itemCount The number of items in the section (from the header/map) */ - public void annotateSection(@Nonnull AnnotatedBytes out, @Nonnull DexBackedDexFile dexFile, int length) { + public void annotateSection(@Nonnull AnnotatedBytes out, @Nonnull DexBackedDexFile dexFile, int itemCount) { String itemName = getItemName(); - if (length > 0) { + int itemAlignment = getItemAlignment(); + if (itemCount > 0) { out.annotate(0, "-----------------------------"); out.annotate(0, "%s section", itemName); out.annotate(0, "-----------------------------"); out.annotate(0, ""); - for (int i=0; i 0) { - final int parameterCount = dexFile.readSmallUint(parametersOffset + - DexBackedDexFile.Impl.TYPE_LIST_SIZE_OFFSET); - final int paramListStart = parametersOffset + DexBackedDexFile.Impl.TYPE_LIST_LIST_OFFSET; + final int parameterCount = dexFile.readSmallUint(parametersOffset + TypeListItem.SIZE_OFFSET); + final int paramListStart = parametersOffset + TypeListItem.LIST_OFFSET; return new FixedSizeList() { @Nonnull @Override diff --git a/dexlib2/src/main/java/org/jf/dexlib2/writer/MapItem.java b/dexlib2/src/main/java/org/jf/dexlib2/writer/MapItem.java index 80f06dc9..8611623a 100644 --- a/dexlib2/src/main/java/org/jf/dexlib2/writer/MapItem.java +++ b/dexlib2/src/main/java/org/jf/dexlib2/writer/MapItem.java @@ -31,6 +31,7 @@ package org.jf.dexlib2.writer; +import org.jf.dexlib2.dexbacked.raw.ItemType; import org.jf.util.ExceptionWithContext; import javax.annotation.Nonnull; @@ -59,26 +60,26 @@ public class MapItem { writer.writeInt(numItems); // index section - writeItem(writer, DexItemType.HEADER_ITEM, 1, 0); - writeItem(writer, DexItemType.STRING_ID_ITEM, dexFile.stringPool.getNumItems(), dexFile.stringPool.getIndexSectionOffset()); - writeItem(writer, DexItemType.TYPE_ID_ITEM, dexFile.typePool.getNumItems(), dexFile.typePool.getSectionOffset()); - writeItem(writer, DexItemType.PROTO_ID_ITEM, dexFile.protoPool.getNumItems(), dexFile.protoPool.getSectionOffset()); - writeItem(writer, DexItemType.FIELD_ID_ITEM, dexFile.fieldPool.getNumItems(), dexFile.fieldPool.getSectionOffset()); - writeItem(writer, DexItemType.METHOD_ID_ITEM, dexFile.methodPool.getNumItems(), dexFile.methodPool.getSectionOffset()); - writeItem(writer, DexItemType.CLASS_DEF_ITEM, dexFile.classDefPool.getNumClassDefItems(), dexFile.classDefPool.getIndexSectionOffset()); + writeItem(writer, ItemType.HEADER_ITEM, 1, 0); + writeItem(writer, ItemType.STRING_ID_ITEM, dexFile.stringPool.getNumItems(), dexFile.stringPool.getIndexSectionOffset()); + writeItem(writer, ItemType.TYPE_ID_ITEM, dexFile.typePool.getNumItems(), dexFile.typePool.getSectionOffset()); + writeItem(writer, ItemType.PROTO_ID_ITEM, dexFile.protoPool.getNumItems(), dexFile.protoPool.getSectionOffset()); + writeItem(writer, ItemType.FIELD_ID_ITEM, dexFile.fieldPool.getNumItems(), dexFile.fieldPool.getSectionOffset()); + writeItem(writer, ItemType.METHOD_ID_ITEM, dexFile.methodPool.getNumItems(), dexFile.methodPool.getSectionOffset()); + writeItem(writer, ItemType.CLASS_DEF_ITEM, dexFile.classDefPool.getNumClassDefItems(), dexFile.classDefPool.getIndexSectionOffset()); // data section - writeItem(writer, DexItemType.STRING_DATA_ITEM, dexFile.stringPool.getNumItems(), dexFile.stringPool.getDataSectionOffset()); - writeItem(writer, DexItemType.TYPE_LIST, dexFile.typeListPool.getNumItems(), dexFile.typeListPool.getSectionOffset()); - writeItem(writer, DexItemType.ENCODED_ARRAY_ITEM, dexFile.encodedArrayPool.getNumItems(), dexFile.encodedArrayPool.getSectionOffset()); - writeItem(writer, DexItemType.ANNOTATION_ITEM, dexFile.annotationPool.getNumItems(), dexFile.annotationPool.getSectionOffset()); - writeItem(writer, DexItemType.ANNOTATION_SET_ITEM, dexFile.annotationSetPool.getNumItems(), dexFile.annotationSetPool.getSectionOffset()); - writeItem(writer, DexItemType.ANNOTATION_SET_REF_LIST, dexFile.annotationSetRefPool.getNumItems(), dexFile.annotationSetRefPool.getSectionOffset()); - writeItem(writer, DexItemType.ANNOTATION_DIRECTORY_ITEM, dexFile.annotationDirectoryPool.getNumItems(), dexFile.annotationDirectoryPool.getSectionOffset()); - writeItem(writer, DexItemType.DEBUG_INFO_ITEM, dexFile.debugInfoPool.getNumItems(), dexFile.debugInfoPool.getSectionOffset()); - writeItem(writer, DexItemType.CODE_ITEM, dexFile.codeItemPool.getNumItems(), dexFile.codeItemPool.getSectionOffset()); - writeItem(writer, DexItemType.CLASS_DATA_ITEM, dexFile.classDefPool.getNumClassDataItems(), dexFile.classDefPool.getDataSectionOffset()); - writeItem(writer, DexItemType.MAP_LIST, 1, sectionOffset); + writeItem(writer, ItemType.STRING_DATA_ITEM, dexFile.stringPool.getNumItems(), dexFile.stringPool.getDataSectionOffset()); + writeItem(writer, ItemType.TYPE_LIST, dexFile.typeListPool.getNumItems(), dexFile.typeListPool.getSectionOffset()); + writeItem(writer, ItemType.ENCODED_ARRAY_ITEM, dexFile.encodedArrayPool.getNumItems(), dexFile.encodedArrayPool.getSectionOffset()); + writeItem(writer, ItemType.ANNOTATION_ITEM, dexFile.annotationPool.getNumItems(), dexFile.annotationPool.getSectionOffset()); + writeItem(writer, ItemType.ANNOTATION_SET_ITEM, dexFile.annotationSetPool.getNumItems(), dexFile.annotationSetPool.getSectionOffset()); + writeItem(writer, ItemType.ANNOTATION_SET_REF_LIST, dexFile.annotationSetRefPool.getNumItems(), dexFile.annotationSetRefPool.getSectionOffset()); + writeItem(writer, ItemType.ANNOTATION_DIRECTORY_ITEM, dexFile.annotationDirectoryPool.getNumItems(), dexFile.annotationDirectoryPool.getSectionOffset()); + writeItem(writer, ItemType.DEBUG_INFO_ITEM, dexFile.debugInfoPool.getNumItems(), dexFile.debugInfoPool.getSectionOffset()); + writeItem(writer, ItemType.CODE_ITEM, dexFile.codeItemPool.getNumItems(), dexFile.codeItemPool.getSectionOffset()); + writeItem(writer, ItemType.CLASS_DATA_ITEM, dexFile.classDefPool.getNumClassDataItems(), dexFile.classDefPool.getDataSectionOffset()); + writeItem(writer, ItemType.MAP_LIST, 1, sectionOffset); } private int calcNumItems() {