From 8c568e2b3750a0ded61477bc6fe584188d742dc4 Mon Sep 17 00:00:00 2001 From: "JesusFreke@JesusFreke.com" Date: Sat, 20 Jun 2009 20:30:12 +0000 Subject: [PATCH] - added logic to set the offset and index of items when reading them in, in order to support an immediate write without having to call place (for dumping) - moved the logic in MapItem that sorts the items for the various sections from writeTo() to place(), so that the order of items aren't changed when doing a read then immediate write (for dumping) - changed the logic in makeBlankMapItem to get the map values from the MapValue enum, instead of hard coded values - added logic to the OffsettedSection so that it sorts the item list by offset after reading it in, so that they items are in the same order as in the file it just read in (for dumping) git-svn-id: https://smali.googlecode.com/svn/trunk@180 55b6fa8a-2a1e-11de-a435-ffa8d773f76a --- .../java/org/jf/dexlib/IndexedSection.java | 3 +- dexlib/src/main/java/org/jf/dexlib/Item.java | 4 +- .../src/main/java/org/jf/dexlib/MapItem.java | 54 +++++++++---------- .../java/org/jf/dexlib/OffsettedSection.java | 13 ++++- 4 files changed, 41 insertions(+), 33 deletions(-) diff --git a/dexlib/src/main/java/org/jf/dexlib/IndexedSection.java b/dexlib/src/main/java/org/jf/dexlib/IndexedSection.java index 363887e1..2d3bd1b7 100644 --- a/dexlib/src/main/java/org/jf/dexlib/IndexedSection.java +++ b/dexlib/src/main/java/org/jf/dexlib/IndexedSection.java @@ -50,10 +50,11 @@ public abstract class IndexedSection> extends Section { return offset; } - public void readFrom(Input in) { + public void readFrom(Input in, int index) { + this.offset = in.getCursor(); + this.index = index; for (Field field: fields) { field.readFrom(in); } diff --git a/dexlib/src/main/java/org/jf/dexlib/MapItem.java b/dexlib/src/main/java/org/jf/dexlib/MapItem.java index 56eced76..bd79de3c 100644 --- a/dexlib/src/main/java/org/jf/dexlib/MapItem.java +++ b/dexlib/src/main/java/org/jf/dexlib/MapItem.java @@ -29,6 +29,7 @@ package org.jf.dexlib; import org.jf.dexlib.util.AnnotatedOutput; +import org.jf.dexlib.util.Input; import java.util.ArrayList; import java.util.Collections; @@ -51,6 +52,12 @@ public class MapItem extends IndexedItem { } public int place(int index, int offset) { + Collections.sort(mapEntries, new Comparator() { + public int compare(MapField o1, MapField o2) { + return ((Integer)o1.getSectionOffset()).compareTo(o2.getSectionOffset()); + } + }); + for (int i=0; i { return super.place(index, offset); } - public void writeTo(AnnotatedOutput out) { - Collections.sort(mapEntries, new Comparator() { - - public int compare(MapField o1, MapField o2) { - return ((Integer)o1.getSectionOffset()).compareTo(o2.getSectionOffset()); - } - }); - - super.writeTo(out); - } - public static MapItem makeBlankMapItem(DexFile dexFile) { MapItem mapItem = new MapItem(dexFile, 0); - mapItem.mapEntries.add(new MapField(dexFile, (short)0x0000)); - mapItem.mapEntries.add(new MapField(dexFile, (short)0x0001)); - mapItem.mapEntries.add(new MapField(dexFile, (short)0x0002)); - mapItem.mapEntries.add(new MapField(dexFile, (short)0x0003)); - mapItem.mapEntries.add(new MapField(dexFile, (short)0x0004)); - mapItem.mapEntries.add(new MapField(dexFile, (short)0x0005)); - mapItem.mapEntries.add(new MapField(dexFile, (short)0x0006)); - mapItem.mapEntries.add(new MapField(dexFile, (short)0x1002)); - mapItem.mapEntries.add(new MapField(dexFile, (short)0x1003)); - mapItem.mapEntries.add(new MapField(dexFile, (short)0x2001)); - mapItem.mapEntries.add(new MapField(dexFile, (short)0x2006)); - mapItem.mapEntries.add(new MapField(dexFile, (short)0x1001)); - mapItem.mapEntries.add(new MapField(dexFile, (short)0x2002)); - mapItem.mapEntries.add(new MapField(dexFile, (short)0x2003)); - mapItem.mapEntries.add(new MapField(dexFile, (short)0x2004)); - mapItem.mapEntries.add(new MapField(dexFile, (short)0x2005)); - mapItem.mapEntries.add(new MapField(dexFile, (short)0x2000)); - mapItem.mapEntries.add(new MapField(dexFile, (short)0x1000)); + mapItem.mapEntries.add(new MapField(dexFile, (short)ItemType.TYPE_HEADER_ITEM.getMapValue())); + mapItem.mapEntries.add(new MapField(dexFile, (short)ItemType.TYPE_STRING_ID_ITEM.getMapValue())); + mapItem.mapEntries.add(new MapField(dexFile, (short)ItemType.TYPE_TYPE_ID_ITEM.getMapValue())); + mapItem.mapEntries.add(new MapField(dexFile, (short)ItemType.TYPE_PROTO_ID_ITEM.getMapValue())); + mapItem.mapEntries.add(new MapField(dexFile, (short)ItemType.TYPE_FIELD_ID_ITEM.getMapValue())); + mapItem.mapEntries.add(new MapField(dexFile, (short)ItemType.TYPE_METHOD_ID_ITEM.getMapValue())); + mapItem.mapEntries.add(new MapField(dexFile, (short)ItemType.TYPE_CLASS_DEF_ITEM.getMapValue())); + mapItem.mapEntries.add(new MapField(dexFile, (short)ItemType.TYPE_STRING_DATA_ITEM.getMapValue())); + mapItem.mapEntries.add(new MapField(dexFile, (short)ItemType.TYPE_ENCODED_ARRAY_ITEM.getMapValue())); + mapItem.mapEntries.add(new MapField(dexFile, (short)ItemType.TYPE_ANNOTATION_ITEM.getMapValue())); + mapItem.mapEntries.add(new MapField(dexFile, (short)ItemType.TYPE_ANNOTATION_SET_ITEM.getMapValue())); + mapItem.mapEntries.add(new MapField(dexFile, (short)ItemType.TYPE_ANNOTATION_SET_REF_LIST.getMapValue())); + mapItem.mapEntries.add(new MapField(dexFile, (short)ItemType.TYPE_ANNOTATIONS_DIRECTORY_ITEM.getMapValue())); + mapItem.mapEntries.add(new MapField(dexFile, (short)ItemType.TYPE_TYPE_LIST.getMapValue())); + mapItem.mapEntries.add(new MapField(dexFile, (short)ItemType.TYPE_DEBUG_INFO_ITEM.getMapValue())); + mapItem.mapEntries.add(new MapField(dexFile, (short)ItemType.TYPE_CODE_ITEM.getMapValue())); + mapItem.mapEntries.add(new MapField(dexFile, (short)ItemType.TYPE_CLASS_DATA_ITEM.getMapValue())); + mapItem.mapEntries.add(new MapField(dexFile, (short)ItemType.TYPE_MAP_LIST.getMapValue())); return mapItem; diff --git a/dexlib/src/main/java/org/jf/dexlib/OffsettedSection.java b/dexlib/src/main/java/org/jf/dexlib/OffsettedSection.java index aad5cd48..20b851ce 100644 --- a/dexlib/src/main/java/org/jf/dexlib/OffsettedSection.java +++ b/dexlib/src/main/java/org/jf/dexlib/OffsettedSection.java @@ -31,6 +31,8 @@ package org.jf.dexlib; import org.jf.dexlib.util.Input; import java.util.HashMap; +import java.util.Collections; +import java.util.Comparator; public abstract class OffsettedSection> extends Section { protected HashMap itemsByOffset; @@ -57,14 +59,21 @@ public abstract class OffsettedSection> extends Secti return item; } - public void readFrom(int size, Input in) { + public void readFrom(int size, Input in) { + this.offset = in.getCursor(); for (int i = 0; i < size; i++) { T item = getByOffset(in.getCursor()); - item.readFrom(in); + item.readFrom(in, i); //TODO: why are we aligning afterwards? in.alignTo(item.getAlignment()); } + //sort the items list by offset + Collections.sort(items, new Comparator() { + public int compare(T t, T t1) { + return ((Integer)t.getOffset()).compareTo(t1.getOffset()); + } + }); } protected abstract T make(int offset);