diff --git a/dexlib/pom.xml b/dexlib/pom.xml deleted file mode 100644 index c315164c..00000000 --- a/dexlib/pom.xml +++ /dev/null @@ -1,28 +0,0 @@ - - - 4.0.0 - org.jf - dexlib - ${aversion} - - org.jf - smali-pom - 1.0-SNAPSHOT - - - - - org.apache.maven.plugins - maven-javadoc-plugin - - - - - - junit - junit - 4.6 - - - \ No newline at end of file diff --git a/dexlib/src/main/java/org/jf/dexlib/AnnotationDirectoryItem.java b/dexlib/src/main/java/org/jf/dexlib/AnnotationDirectoryItem.java deleted file mode 100644 index 7605ecd6..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/AnnotationDirectoryItem.java +++ /dev/null @@ -1,293 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -public class AnnotationDirectoryItem extends OffsettedItem { - private final ArrayList fieldAnnotationList = new ArrayList(); - private final ArrayList methodAnnotationList = new ArrayList(); - private final ArrayList parameterAnnotationList = new ArrayList(); - - private final OffsettedItemReference classAnnotationsReferenceField; - private final ListSizeField annotatedFieldsCountField; - private final ListSizeField annotatedMethodsCountField; - private final ListSizeField annotatedParametersCountField; - private final FieldListField fieldAnnotationListField; - private final FieldListField methodAnnotationListField; - private final FieldListField parameterAnnotationListField; - - - //typically each AnnotationDirectoryItem will have a distinct parent. The only case that isn't true is when - //the AnnotationDirectoryItem *only* contains class annotations, with no other type of annotation. In that - //case, the same AnnotationDirectoryItem could be references from multiple classes. - //This isn't a problem though, because this field is only used in compareTo to determine the sort order, - //which handles it as a special case - private ClassDefItem parent = null; - - protected AnnotationDirectoryItem(final DexFile dexFile, int offset) { - super(dexFile, offset); - - fields = new Field[] { - classAnnotationsReferenceField = new OffsettedItemReference( - dexFile.AnnotationSetsSection, new IntegerField(null), "class_annotations_off"), - annotatedFieldsCountField = new ListSizeField(fieldAnnotationList, new IntegerField("fields_size")), - annotatedMethodsCountField = new ListSizeField(methodAnnotationList, - new IntegerField("annotated_methods_size")), - annotatedParametersCountField = new ListSizeField(parameterAnnotationList, - new IntegerField("annotated_parameters_size")), - fieldAnnotationListField = new FieldListField(fieldAnnotationList, - "field_annotations") { - protected FieldAnnotation make() { - return new FieldAnnotation(dexFile); - } - }, - methodAnnotationListField = new FieldListField(methodAnnotationList, - "method_annotations") { - protected MethodAnnotation make() { - return new MethodAnnotation(dexFile); - } - }, - parameterAnnotationListField = new FieldListField(parameterAnnotationList, - "parameter_annotations") { - protected ParameterAnnotation make() { - return new ParameterAnnotation(dexFile); - } - } - }; - } - - public AnnotationDirectoryItem(final DexFile dexFile, - AnnotationSetItem classAnnotations, - List fieldAnnotations, - List methodAnnotations, - List parameterAnnotations) { - this(dexFile, -1); - - classAnnotationsReferenceField.setReference(classAnnotations); - - if (fieldAnnotations != null) { - fieldAnnotationList.addAll(fieldAnnotations); - } - - if (methodAnnotations != null) { - methodAnnotationList.addAll(methodAnnotations); - } - - if (parameterAnnotations != null) { - parameterAnnotationList.addAll(parameterAnnotations); - } - } - - public int place(int index, int offset) - { - if (!dexFile.getInplace()) { - Collections.sort(fieldAnnotationList); - Collections.sort(methodAnnotationList); - Collections.sort(parameterAnnotationList); - } - return super.place(index, offset); - } - - protected int getAlignment() { - return 4; - } - - public ItemType getItemType() { - return ItemType.TYPE_ANNOTATIONS_DIRECTORY_ITEM; - } - - public String getConciseIdentity() { - return "annotation_directory_item @0x" + Integer.toHexString(getOffset()); - } - - public AnnotationSetItem getClassAnnotations() { - return classAnnotationsReferenceField.getReference(); - } - - public List getMethodAnnotations() { - return Collections.unmodifiableList(methodAnnotationList); - } - - public List getFieldAnnotations() { - return Collections.unmodifiableList(fieldAnnotationList); - } - - public List getParameterAnnotations() { - return Collections.unmodifiableList(parameterAnnotationList); - } - - - private boolean isInternable() { - //an instance is only internable if it has only class annotations, but - //no other type of annotation - if (classAnnotationsReferenceField.getReference() == null || - fieldAnnotationList.size() > 0 || - methodAnnotationList.size() > 0 || - parameterAnnotationList.size() > 0) { - return false; - } - return true; - } - - public int hashCode() { - //an instance is only internable if it has only class annotations, but - //no other type of annotation - if (!isInternable()) { - return super.hashCode(); - } - return classAnnotationsReferenceField.getReference().hashCode(); - } - - protected void setParent(ClassDefItem classDefItem) { - this.parent = classDefItem; - } - - public int compareTo(AnnotationDirectoryItem annotationDirectoryItem) { - if (!isInternable()) { - if (!annotationDirectoryItem.isInternable()) { - return parent.compareTo(annotationDirectoryItem.parent); - } - return -1; - } - - if (!annotationDirectoryItem.isInternable()) { - return 1; - } - - return classAnnotationsReferenceField.getReference().compareTo( - annotationDirectoryItem.classAnnotationsReferenceField.getReference()); - } - - public static class FieldAnnotation extends CompositeField - implements Comparable { - private final IndexedItemReference fieldReferenceField; - private final OffsettedItemReference annotationSetReferenceField; - - public FieldAnnotation(DexFile dexFile) { - super("field_annotation"); - fields = new Field[] { - fieldReferenceField = new IndexedItemReference(dexFile.FieldIdsSection, - new IntegerField(null), "field_idx"), - annotationSetReferenceField = new OffsettedItemReference( - dexFile.AnnotationSetsSection, new IntegerField(null), "annotations_off") - }; - } - - public FieldAnnotation(DexFile dexFile, FieldIdItem field, AnnotationSetItem annotationSet) { - this(dexFile); - this.fieldReferenceField.setReference(field); - this.annotationSetReferenceField.setReference(annotationSet); - } - - public int compareTo(FieldAnnotation o) { - return ((Integer) fieldReferenceField.getReference().getIndex()).compareTo( - o.fieldReferenceField.getReference().getIndex()); - } - - public FieldIdItem getField() { - return fieldReferenceField.getReference(); - } - - public AnnotationSetItem getAnnotationSet() { - return annotationSetReferenceField.getReference(); - } - } - - public static class MethodAnnotation extends CompositeField - implements Comparable { - private final IndexedItemReference methodReferenceField; - private final OffsettedItemReference annotationSetReferenceField; - - public MethodAnnotation(DexFile dexFile) { - super("method_annotation"); - fields = new Field[] { - methodReferenceField = new IndexedItemReference(dexFile.MethodIdsSection, - new IntegerField(null), "method_idx"), - annotationSetReferenceField = new OffsettedItemReference(dexFile.AnnotationSetsSection, - new IntegerField(null), "annotations_off") - }; - } - - public MethodAnnotation(DexFile dexFile, MethodIdItem method, AnnotationSetItem annotationSet) { - this(dexFile); - this.methodReferenceField.setReference(method); - this.annotationSetReferenceField.setReference(annotationSet); - } - - public int compareTo(MethodAnnotation o) { - return ((Integer) methodReferenceField.getReference().getIndex()).compareTo(o.methodReferenceField.getReference().getIndex()); - } - - public MethodIdItem getMethod() { - return methodReferenceField.getReference(); - } - - public AnnotationSetItem getAnnotationSet() { - return annotationSetReferenceField.getReference(); - } - } - - public static class ParameterAnnotation extends CompositeField - implements Comparable { - private final IndexedItemReference methodReferenceField; - private final OffsettedItemReference parameterAnnotationsReferenceField; - - public ParameterAnnotation(DexFile dexFile) { - super("parameter_annotation"); - fields = new Field[] { - methodReferenceField = new IndexedItemReference(dexFile.MethodIdsSection, - new IntegerField(null), "method_idx"), - parameterAnnotationsReferenceField = new OffsettedItemReference( - dexFile.AnnotationSetRefListsSection, new IntegerField(null), "annotations_off") - }; - } - - public ParameterAnnotation(DexFile dexFile, MethodIdItem method, AnnotationSetRefList parameterAnnotations) { - this(dexFile); - this.methodReferenceField.setReference(method); - this.parameterAnnotationsReferenceField.setReference(parameterAnnotations); - } - - public int compareTo(ParameterAnnotation o) { - return ((Integer) methodReferenceField.getReference().getIndex()).compareTo(o.methodReferenceField.getReference().getIndex()); - } - - public MethodIdItem getMethod() { - return methodReferenceField.getReference(); - } - - public AnnotationSetRefList getParameterAnnotations() { - return parameterAnnotationsReferenceField.getReference(); - } - - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/AnnotationItem.java b/dexlib/src/main/java/org/jf/dexlib/AnnotationItem.java deleted file mode 100644 index e2bc94f8..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/AnnotationItem.java +++ /dev/null @@ -1,103 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib; - -import org.jf.dexlib.EncodedValue.AnnotationEncodedValueSubField; - -public class AnnotationItem extends OffsettedItem { - private final ByteField visibilityField; - private final AnnotationEncodedValueSubField annotationField; - - public AnnotationItem(DexFile dexFile, int offset) { - super(dexFile, offset); - - fields = new Field[] { - visibilityField = new ByteField("visibility"), - annotationField = new AnnotationEncodedValueSubField(dexFile) - }; - } - - public AnnotationItem(DexFile dexFile, AnnotationVisibility visibility, - AnnotationEncodedValueSubField annotation) { - super(dexFile, -1); - - fields = new Field[] { - this.visibilityField = new ByteField(visibility.value, "visibility"), - this.annotationField = annotation - }; - } - - public ItemType getItemType() { - return ItemType.TYPE_ANNOTATION_ITEM; - } - - public String getConciseIdentity() { - return "annotation_item @0x" + Integer.toHexString(getOffset()); - } - - public Visibility getVisibility() { - return Visibility.get((byte)visibilityField.getCachedValue()); - } - - public AnnotationEncodedValueSubField getEncodedAnnotation() { - return annotationField; - } - - public int compareTo(AnnotationItem annotationItem) { - int comp = ((Integer)visibilityField.getCachedValue()).compareTo(annotationItem.visibilityField.getCachedValue()); - if (comp == 0) { - comp = annotationField.compareTo(annotationItem.annotationField); - } - return comp; - } - - public enum Visibility { - build(0x00), - runtime(0x01), - system(0x02); - - public final byte value; - - private Visibility(int value) { - this.value = (byte)value; - } - - public static Visibility get(byte value) { - switch (value) { - case 0x00: - return build; - case 0x01: - return runtime; - case 0x02: - return system; - } - return null; - } - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/AnnotationSetItem.java b/dexlib/src/main/java/org/jf/dexlib/AnnotationSetItem.java deleted file mode 100644 index 48356f07..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/AnnotationSetItem.java +++ /dev/null @@ -1,104 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib; - -import java.util.ArrayList; -import java.util.List; - -public class AnnotationSetItem extends OffsettedItem { - private final ArrayList> annotationReferences = - new ArrayList>(); - - private final ListSizeField annotationCountField; - private final FieldListField> annotationsListField; - - public AnnotationSetItem(final DexFile dexFile, int offset) { - super(dexFile, offset); - - fields = new Field[] { - annotationCountField = new ListSizeField(annotationReferences, new IntegerField("size")), - annotationsListField = new FieldListField>( - annotationReferences, "annotation") { - protected OffsettedItemReference make() { - return new OffsettedItemReference(dexFile.AnnotationsSection, - new IntegerField(null), "annotation_off"); - } - } - }; - } - - public AnnotationSetItem(final DexFile dexFile, List annotations) { - this(dexFile, -1); - - for (AnnotationItem annotationItem: annotations) { - OffsettedItemReference annotationReference = annotationsListField.make(); - annotationReference.setReference(annotationItem); - this.annotationReferences.add(annotationReference); - } - } - - protected int getAlignment() { - return 4; - } - - public ItemType getItemType() { - return ItemType.TYPE_ANNOTATION_SET_ITEM; - } - - public String getConciseIdentity() { - return "annotation_set_item @0x" + Integer.toHexString(getOffset()); - } - - public List getAnnotationItems() { - List annotationItems = new ArrayList(); - - for (OffsettedItemReference annotationItemReference: annotationReferences) { - annotationItems.add(annotationItemReference.getReference()); - } - return annotationItems; - } - - public int compareTo(AnnotationSetItem annotationSetItem) { - if (annotationSetItem == null) { - return 1; - } - - int comp = ((Integer)annotationReferences.size()).compareTo(annotationSetItem.annotationReferences.size()); - if (comp == 0) { - for (int i=0; i { - private final ArrayList> annotationSetReferences = - new ArrayList>(); - - private final ListSizeField annotationSetCountField; - private final FieldListField> annotationSetsListField; - - public AnnotationSetRefList(final DexFile dexFile, int offset) { - super(dexFile, offset); - - fields = new Field[] { - annotationSetCountField = new ListSizeField(annotationSetReferences, new IntegerField("size")), - annotationSetsListField = new FieldListField>( - annotationSetReferences, "list") { - protected OffsettedItemReference make() { - return new OffsettedItemReference(dexFile.AnnotationSetsSection, - new IntegerField(null), "annotation_set_ref_item"); - } - } - }; - } - - public AnnotationSetRefList(final DexFile dexFile, List annotationSets) { - this(dexFile, -1); - - for (AnnotationSetItem annotationSet: annotationSets) { - OffsettedItemReference annotationSetReference = annotationSetsListField.make(); - annotationSetReference.setReference(annotationSet); - this.annotationSetReferences.add(annotationSetReference); - } - } - - protected int getAlignment() { - return 4; - } - - public ItemType getItemType() { - return ItemType.TYPE_ANNOTATION_SET_REF_LIST; - } - - public String getConciseIdentity() { - return "annotation_set_item @0x" + Integer.toHexString(getOffset()); - } - - public int getCount() { - return annotationSetReferences.size(); - } - - public List getAnnotationSets() { - List annotationSets = new ArrayList(); - - for (OffsettedItemReference annotationSetReference: annotationSetReferences) { - annotationSets.add(annotationSetReference.getReference()); - } - - return annotationSets; - } - - public int compareTo(AnnotationSetRefList annotationSetRefList) { - int comp = ((Integer)annotationSetReferences.size()).compareTo(annotationSetRefList.annotationSetReferences.size()); - if (comp == 0) { - for (int i=0; i - implements Field { - - private final String fieldName; - protected int value; - - protected CachedIntegerValueField(String fieldName) { - this.fieldName = fieldName; - } - - protected CachedIntegerValueField(int value, String fieldName) { - this(fieldName); - this.value = value; - } - - public void copyTo(DexFile dexFile, T copy) { - copy.value = value; - } - - public int hashCode() { - return value; - } - - protected abstract void writeValue(Output out); - - public void writeTo(AnnotatedOutput out) { - if (fieldName != null) { - out.annotate(fieldName + ": 0x" + Integer.toHexString(getCachedValue())); - } - writeValue(out); - } - - public boolean equals(Object o) { - return (this.getClass() == o.getClass()) && - (getCachedValue() == ((CachedIntegerValueField)o).getCachedValue()); - } - - /** - * This method returns the integer value that has been cached. This - * value is either the value that the field was constructed with, the - * value that was read via readFrom, or the value that was - * cached when place was called - * @return the cached value - */ - public int getCachedValue() { - return value; - } - - public void cacheValue(int value) { - this.value = value; - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/ClassDataItem.java b/dexlib/src/main/java/org/jf/dexlib/ClassDataItem.java deleted file mode 100644 index fac084bf..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/ClassDataItem.java +++ /dev/null @@ -1,438 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib; - -import org.jf.dexlib.Util.*; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -public class ClassDataItem extends OffsettedItem { - private final ArrayList staticFieldList = new ArrayList(); - private final ArrayList instanceFieldList = new ArrayList(); - private final ArrayList directMethodList = new ArrayList(); - private final ArrayList virtualMethodList = new ArrayList(); - - private final ListSizeField staticFieldsCountField; - private final ListSizeField instanceFieldsCountField; - private final ListSizeField directMethodsCountField; - private final ListSizeField virtualMethodsCountField; - private final EncodedMemberList staticFieldsListField; - private final EncodedMemberList instanceFieldsListField; - private final EncodedMemberList directMethodsListField; - private final EncodedMemberList virtualMethodsListField; - - private ClassDefItem parent = null; - - public ClassDataItem(final DexFile dexFile, int offset) { - super(dexFile, offset); - - fields = new Field[] { - staticFieldsCountField = new ListSizeField(staticFieldList, - new Leb128Field("static_fields_size")), - instanceFieldsCountField = new ListSizeField(instanceFieldList, - new Leb128Field("instance_fields_size")), - directMethodsCountField = new ListSizeField(directMethodList, - new Leb128Field("direct_methods_size")), - virtualMethodsCountField = new ListSizeField(virtualMethodList, - new Leb128Field("virtual_methods_size")), - staticFieldsListField = new EncodedMemberList(staticFieldList, "static_fields") { - protected EncodedField make(EncodedField previousField) { - return new EncodedField(dexFile, previousField); - } - }, - instanceFieldsListField = new EncodedMemberList(instanceFieldList, "instance_fields") { - protected EncodedField make(EncodedField previousField) { - return new EncodedField(dexFile, previousField); - } - }, - directMethodsListField = new EncodedMemberList(directMethodList, "direct_methods") { - protected EncodedMethod make(EncodedMethod previousMethod) { - return new EncodedMethod(dexFile, previousMethod); - } - }, - virtualMethodsListField = new EncodedMemberList(virtualMethodList, "virtual_methods") { - protected EncodedMethod make(EncodedMethod previousMethod) { - return new EncodedMethod(dexFile, previousMethod); - } - } - }; - } - - public void addMethod(EncodedMethod encodedMethod) { - if (encodedMethod.isDirect()) { - directMethodList.add(encodedMethod); - } else { - virtualMethodList.add(encodedMethod); - } - } - - public int addField(EncodedField encodedField) { - if (encodedField.isStatic()) { - int index = Collections.binarySearch(staticFieldList, encodedField); - if (index >= 0) { - throw new RuntimeException("A static field of that name and type is already present"); - } - index = (index + 1) * -1; - staticFieldList.add(index, encodedField); - return index; - } else { - int index = Collections.binarySearch(instanceFieldList, encodedField); - if (index >= 0) { - throw new RuntimeException("An instance field of that name and type is already present"); - } - index = (index + 1) * -1; - instanceFieldList.add(index, encodedField); - return index; - } - } - - public List getStaticFields() { - return Collections.unmodifiableList(staticFieldList); - } - - public List getInstanceFields() { - return Collections.unmodifiableList(instanceFieldList); - } - - public List getDirectMethods() { - return Collections.unmodifiableList(directMethodList); - } - - public List getVirtualMethods() { - return Collections.unmodifiableList(virtualMethodList); - } - - private static abstract class EncodedMember> extends CompositeField implements Field, Comparable - { - public EncodedMember(String fieldName) { - super(fieldName); - } - - protected abstract void setPreviousMember(T previousMember); - } - - private static abstract class EncodedMemberList> implements Field> { - private final ArrayList list; - private final String fieldName; - - public EncodedMemberList(ArrayList list, String fieldName) { - this.list = list; - this.fieldName = fieldName; - } - - public void writeTo(AnnotatedOutput out) { - out.annotate(0, fieldName + ":"); - int i=0; - for (T field: list) { - out.annotate(0, "[0x" + Integer.toHexString(i) + "]"); - field.writeTo(out); - i++; - } - } - - protected abstract T make(T previousField); - - public void readFrom(Input in) { - for (int i = 0; i < list.size(); i++) { - T previousField = null; - if (i > 0) { - previousField = list.get(i-1); - } - T field = make(previousField); - list.set(i, field); - field.readFrom(in); - } - } - - public int place(int offset) { - Collections.sort(list); - - T previousMember = null; - for (T encodedMember: list) { - encodedMember.setPreviousMember(previousMember); - offset = encodedMember.place(offset); - previousMember = encodedMember; - } - return offset; - } - - public void copyTo(DexFile dexFile, EncodedMemberList copy) { - copy.list.clear(); - copy.list.ensureCapacity(list.size()); - for (int i = 0; i < list.size(); i++) { - T previousField = null; - if (i > 0) { - previousField = copy.list.get(i-1); - } - T fieldCopy = copy.make(previousField); - list.get(i).copyTo(dexFile, fieldCopy); - copy.list.add(fieldCopy); - } - } - - public int hashCode() { - int h = 1; - for (int i = 0; i < list.size(); i++) { - h = h * 31 + list.get(i).hashCode(); - } - return h; - } - - public boolean equals(Object o) { - if (!(o instanceof EncodedMemberList)) { - return false; - } - - EncodedMemberList other = (EncodedMemberList)o; - if (list.size() != other.list.size()) { - return false; - } - - for (int i = 0; i < list.size(); i++) { - if (!list.get(i).equals(other.list.get(i))) { - return false; - } - } - return true; - } - } - - public static class EncodedField extends EncodedMember { - private final IndexedItemReference fieldReferenceField; - private final Leb128DeltaField fieldIndexField; - private final Leb128Field accessFlagsField; - - public EncodedField(DexFile dexFile, final EncodedField previousField) { - super("encoded_field"); - Leb128DeltaField previousIndexField = null; - if (previousField != null) { - previousIndexField = previousField.fieldIndexField; - } - - - fields = new Field[] { - fieldReferenceField = new IndexedItemReference(dexFile.FieldIdsSection, - fieldIndexField = new Leb128DeltaField(previousIndexField, null), "field_idx_diff"), - accessFlagsField = new Leb128Field("access_flags") - }; - } - - public EncodedField(DexFile dexFile, FieldIdItem field, int accessFlags) { - super("encoded_field"); - fields = new Field[] { - this.fieldReferenceField = new IndexedItemReference(dexFile, field, - fieldIndexField = new Leb128DeltaField(null), "field_idx_diff"), - this.accessFlagsField = new Leb128Field(accessFlags, "access_flags") - }; - } - - protected void setPreviousMember(EncodedField previousField) { - if (previousField != null) { - fieldIndexField.setPreviousField(previousField.fieldIndexField); - } else { - fieldIndexField.setPreviousField(null); - } - } - - public int compareTo(EncodedField other) - { - return fieldReferenceField.getReference().compareTo(other.fieldReferenceField.getReference()); - } - - public boolean isStatic() { - return (accessFlagsField.getCachedValue() & AccessFlags.STATIC.getValue()) != 0; - } - - public FieldIdItem getField() { - return fieldReferenceField.getReference(); - } - - public int getAccessFlags() { - return accessFlagsField.getCachedValue(); - } - } - - public static class EncodedMethod extends EncodedMember { - private final IndexedItemReference methodReferenceField; - private final Leb128DeltaField methodIndexField; - private final Leb128Field accessFlagsField; - private final OffsettedItemReference codeItemReferenceField; - - public EncodedMethod(DexFile dexFile, final EncodedMethod previousMethod) { - super("encoded_method"); - Leb128DeltaField previousIndexField = null; - if (previousMethod != null) { - previousIndexField = previousMethod.methodIndexField; - } - - fields = new Field[] { - methodReferenceField = new IndexedItemReference(dexFile.MethodIdsSection, - methodIndexField = new Leb128DeltaField(previousIndexField, null), "method_idx_diff"), - accessFlagsField = new Leb128Field("access_flags"), - codeItemReferenceField = new OffsettedItemReference(dexFile.CodeItemsSection, - new Leb128Field(null), "code_off") - }; - } - - public EncodedMethod(DexFile dexFile, MethodIdItem methodIdItem, int accessFlags, CodeItem codeItem) { - super("encoded_method"); - fields = new Field[] { - this.methodReferenceField = new IndexedItemReference(dexFile, methodIdItem, - methodIndexField = new Leb128DeltaField(null), "method_idx_diff"), - this.accessFlagsField = new Leb128Field(accessFlags, "access_flags"), - this.codeItemReferenceField = new OffsettedItemReference(dexFile, codeItem, - new Leb128Field(null), "code_off") - }; - - if (codeItem != null) { - codeItem.setParent(methodIdItem); - } - } - - protected void setPreviousMember(EncodedMethod previousMethod) { - if (previousMethod != null) { - methodIndexField.setPreviousField(previousMethod.methodIndexField); - } else { - methodIndexField.setPreviousField(null); - } - } - - public int compareTo(EncodedMethod other) { - return methodReferenceField.getReference().compareTo(other.methodReferenceField.getReference()); - } - - public boolean isDirect() { - return ((accessFlagsField.getCachedValue() & (AccessFlags.STATIC.getValue() | AccessFlags.PRIVATE.getValue() | - AccessFlags.CONSTRUCTOR.getValue())) != 0); - } - - public void readFrom(Input in) { - super.readFrom(in); - CodeItem codeItem = codeItemReferenceField.getReference(); - if (codeItem != null) { - codeItem.setParent(methodReferenceField.getReference()); - } - } - - public void copyTo(DexFile dexFile, EncodedMethod copy) { - super.copyTo(dexFile, copy); - - CodeItem codeItem = copy.codeItemReferenceField.getReference(); - if (codeItem != null) { - codeItem.setParent(copy.methodReferenceField.getReference()); - } - } - - public int getAccessFlags() { - return accessFlagsField.getCachedValue(); - } - - public MethodIdItem getMethod() { - return methodReferenceField.getReference(); - } - - public CodeItem getCodeItem() { - return codeItemReferenceField.getReference(); - } - } - - - /** - * An Leb128 integer that encodes its value as the difference between - * itself and the previous Leb128DeltaField in the list. The first - * item encodes the value as per normal - */ - protected static class Leb128DeltaField extends Leb128Field { - private Leb128DeltaField previousField = null; - - public Leb128DeltaField(String fieldName) { - super(fieldName); - } - - public void readFrom(Input in) { - super.readFrom(in); - value += getPreviousValue(); - } - - public int place(int offset) { - return offset + Leb128Utils.unsignedLeb128Size(value - getPreviousValue()); - } - - private int getPreviousValue() { - if (previousField == null) { - return 0; - } - return previousField.value; - } - - public void writeValue(Output out) { - out.writeUnsignedLeb128(value - getPreviousValue()); - } - - public Leb128DeltaField(Leb128DeltaField previousField, String fieldName) { - super(fieldName); - this.previousField = previousField; - } - - public void setPreviousField(Leb128DeltaField previousField) { - this.previousField = previousField; - } - } - - protected int getAlignment() { - return 1; - } - - public ItemType getItemType() { - return ItemType.TYPE_CLASS_DATA_ITEM; - } - - public String getConciseIdentity() { - return "class_data_item @0x" + Integer.toHexString(getOffset()); - } - - protected void setParent(ClassDefItem classDefItem) { - this.parent = classDefItem; - } - - public int compareTo(ClassDataItem other) { - if (parent == null) { - if (other.parent == null) { - return 0; - } - return -1; - } - if (other.parent == null) { - return 1; - } - return parent.compareTo(other.parent); - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/ClassDefItem.java b/dexlib/src/main/java/org/jf/dexlib/ClassDefItem.java deleted file mode 100644 index 897871da..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/ClassDefItem.java +++ /dev/null @@ -1,329 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib; - -import org.jf.dexlib.EncodedValue.EncodedValue; -import org.jf.dexlib.EncodedValue.EncodedValueSubField; -import org.jf.dexlib.Util.TypeUtils; -import org.jf.dexlib.Util.Input; - -import java.util.*; - -public class ClassDefItem extends IndexedItem { - private final IndexedItemReference classTypeReferenceField; - private final IntegerField accessFlagsField; - private final IndexedItemReference superclassTypeReferenceField; - private final OffsettedItemReference classInterfacesListReferenceField; - private final IndexedItemReference sourceFileReferenceField; - private final OffsettedItemReference classAnnotationsReferenceField; - private final OffsettedItemReference classDataReferenceField; - private final OffsettedItemReference staticFieldInitialValuesReferenceField; - - private ArrayList staticFieldInitialValuesList; - - private final DexFile dexFile; - - public ClassDefItem(DexFile dexFile, int index) { - super(dexFile, index); - - this.dexFile = dexFile; - - fields = new Field[] { - classTypeReferenceField = new IndexedItemReference(dexFile.TypeIdsSection, - new IntegerField(null), "class_idx"), - //TODO: add annotated output showing the flags - accessFlagsField = new IntegerField("access_flags:"), - superclassTypeReferenceField = new IndexedItemReference(dexFile.TypeIdsSection, - new IntegerField(null), "superclass_idx"), - classInterfacesListReferenceField = new OffsettedItemReference(dexFile.TypeListsSection, - new IntegerField(null), "interfaces_off"), - sourceFileReferenceField = new IndexedItemReference(dexFile.StringIdsSection, - new IntegerField(null), "source_file_off"), - classAnnotationsReferenceField = new OffsettedItemReference( - dexFile.AnnotationDirectoriesSection, new IntegerField(null), "annotations_off"), - classDataReferenceField = new OffsettedItemReference(dexFile.ClassDataSection, - new IntegerField(null), "class_data_off"), - staticFieldInitialValuesReferenceField = new OffsettedItemReference( - dexFile.EncodedArraysSection, new IntegerField(null), "static_values_off") - }; - } - - public ClassDefItem(DexFile dexFile, - TypeIdItem classType, - int accessFlags, - TypeIdItem superType, - TypeListItem implementsList, - StringIdItem source, - ClassDataItem classDataItem) { - this(dexFile, -1); - - classTypeReferenceField.setReference(classType); - accessFlagsField.cacheValue(accessFlags); - superclassTypeReferenceField.setReference(superType); - classInterfacesListReferenceField.setReference(implementsList); - sourceFileReferenceField.setReference(source); - classDataReferenceField.setReference(classDataItem); - - if (classDataItem != null) { - classDataItem.setParent(this); - } - } - - public TypeIdItem getSuperclass() { - return superclassTypeReferenceField.getReference(); - } - - public TypeIdItem getClassType() { - return classTypeReferenceField.getReference(); - } - - protected int getAlignment() { - return 4; - } - - public ItemType getItemType() { - return ItemType.TYPE_CLASS_DEF_ITEM; - } - - public String getClassName() { - return classTypeReferenceField.getReference().getTypeDescriptor(); - } - - public String getSourceFile() { - StringIdItem stringIdItem = sourceFileReferenceField.getReference(); - if (stringIdItem == null) { - return null; - } - return stringIdItem.getStringValue(); - } - - public int getAccessFlags() { - return accessFlagsField.getCachedValue(); - } - - public List getInterfaces() { - TypeListItem interfaceList = classInterfacesListReferenceField.getReference(); - if (interfaceList == null) { - return null; - } - - return interfaceList.getTypes(); - } - - public ClassDataItem getClassData() { - return classDataReferenceField.getReference(); - } - - public AnnotationDirectoryItem getAnnotationDirectory() { - return classAnnotationsReferenceField.getReference(); - } - - public String getConciseIdentity() { - return "class_def_item: " + getClassName(); - } - - public int hashCode() { - return classTypeReferenceField.getReference().hashCode(); - } - - public boolean equals(Object o) { - if (!(o instanceof ClassDefItem)) { - return false; - } - ClassDefItem other = (ClassDefItem)o; - return classTypeReferenceField.equals(other.classTypeReferenceField); - } - - public EncodedArrayItem getStaticInitializers() { - return staticFieldInitialValuesReferenceField.getReference(); - } - - public int compareTo(ClassDefItem o) { - //The actual sorting for this class is implemented in SortClassDefItemSection. - //This method is just used for sorting the associated ClassDataItem items, so - //we can just do the comparison based on the offsets of the items - return ((Integer)this.offset).compareTo(o.offset); - } - - public void copyTo(DexFile dexFile, ClassDefItem copy) { - super.copyTo(dexFile, copy); - - AnnotationDirectoryItem annotationDirectoryItem = copy.classAnnotationsReferenceField.getReference(); - if (annotationDirectoryItem != null) { - annotationDirectoryItem.setParent(copy); - } - - ClassDataItem classDataItem = copy.classDataReferenceField.getReference(); - if (classDataItem != null) { - classDataItem.setParent(copy); - } - } - - public void addField(ClassDataItem.EncodedField encodedField, EncodedValue initialValue) { - //fields are added in ClassDefItem instead of ClassDataItem because we need to grab - //the static initializers for StaticFieldInitialValues - if (!encodedField.isStatic() && initialValue != null) { - throw new RuntimeException("Initial values are only allowed for static fields."); - } - - ClassDataItem classDataItem = this.classDataReferenceField.getReference(); - - int fieldIndex = classDataItem.addField(encodedField); - if (initialValue != null) { - if (staticFieldInitialValuesList == null) { - staticFieldInitialValuesList = new ArrayList(); - - EncodedArrayItem encodedArrayItem = new EncodedArrayItem(dexFile, staticFieldInitialValuesList); - staticFieldInitialValuesReferenceField.setReference(encodedArrayItem); - } - - //All static fields before this one must have an initial value. Add any default values as needed - for (int i=staticFieldInitialValuesList.size(); i < fieldIndex; i++) { - ClassDataItem.EncodedField staticField = classDataItem.getStaticFields().get(i); - EncodedValueSubField subField = TypeUtils.makeDefaultValueForType(dexFile, - staticField.getField().getFieldType().getTypeDescriptor()); - EncodedValue encodedValue = new EncodedValue(dexFile, subField); - staticFieldInitialValuesList.add(i, encodedValue); - } - - staticFieldInitialValuesList.add(fieldIndex, initialValue); - } else if (staticFieldInitialValuesList != null && encodedField.isStatic() && fieldIndex < staticFieldInitialValuesList.size()) { - EncodedValueSubField subField = TypeUtils.makeDefaultValueForType(dexFile, - encodedField.getField().getFieldType().getTypeDescriptor()); - EncodedValue encodedValue = new EncodedValue(dexFile, subField); - staticFieldInitialValuesList.add(fieldIndex, encodedValue); - } - } - - public void setAnnotations(AnnotationDirectoryItem annotations) { - this.classAnnotationsReferenceField.setReference(annotations); - if (annotations != null) { - annotations.setParent(this); - } - } - - public void setClassDataItem(ClassDataItem classDataItem) { - this.classDataReferenceField.setReference(classDataItem); - if (classDataItem != null) { - classDataItem.setParent(this); - } - } - - public void readFrom(Input in, int index) { - super.readFrom(in, index); - - ClassDataItem classDataItem = classDataReferenceField.getReference(); - if (classDataItem != null) { - classDataItem.setParent(this); - } - - AnnotationDirectoryItem annotationDirectoryItem = classAnnotationsReferenceField.getReference(); - if (annotationDirectoryItem != null) { - annotationDirectoryItem.setParent(this); - } - } - - public static int placeClassDefItems(IndexedSection section, int offset) { - ClassDefPlacer cdp = new ClassDefPlacer(section); - return cdp.placeSection(offset); - } - - /** - * This class places the items within a ClassDefItem section, such that superclasses and interfaces are - * placed before sub/implementing classes - */ - private static class ClassDefPlacer { - private final IndexedSection section; - private final HashMap classDefsByType = new HashMap(); - - private int currentIndex = 0; - private int currentOffset; - - public ClassDefPlacer(IndexedSection section) { - this.section = section; - - for (ClassDefItem classDefItem: section.items) { - TypeIdItem typeIdItem = classDefItem.classTypeReferenceField.getReference(); - classDefsByType.put(typeIdItem, classDefItem); - } - } - - public int placeSection(int offset) { - currentOffset = offset; - - if (section.dexFile.getSortAllItems()) { - //presort the list, to guarantee a unique ordering - Collections.sort(section.items, new Comparator() { - public int compare(ClassDefItem classDefItem, ClassDefItem classDefItem1) { - return classDefItem.getClassType().compareTo(classDefItem1.getClassType()); - } - }); - } - - for (ClassDefItem classDefItem: section.items) { - classDefItem.offset = -1; - } - - for (ClassDefItem classDefItem: section.items) { - placeClass(classDefItem); - } - - for (ClassDefItem classDefItem: classDefsByType.values()) { - section.items.set(classDefItem.getIndex(), classDefItem); - } - - return currentOffset; - } - - private void placeClass(ClassDefItem classDefItem) { - if (!classDefItem.isPlaced()) { - TypeIdItem superType = classDefItem.superclassTypeReferenceField.getReference(); - ClassDefItem superClassDefItem = classDefsByType.get(superType); - - if (superClassDefItem != null) { - placeClass(superClassDefItem); - } - - TypeListItem interfaces = classDefItem.classInterfacesListReferenceField.getReference(); - - if (interfaces != null) { - for (TypeIdItem interfaceType: interfaces.getTypes()) { - ClassDefItem interfaceClass = classDefsByType.get(interfaceType); - if (interfaceClass != null) { - placeClass(interfaceClass); - } - } - } - - currentOffset = classDefItem.place(currentIndex++, currentOffset); - } - } - - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/Code/Format/ArrayDataPseudoInstruction.java b/dexlib/src/main/java/org/jf/dexlib/Code/Format/ArrayDataPseudoInstruction.java deleted file mode 100644 index 3f4f0001..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Code/Format/ArrayDataPseudoInstruction.java +++ /dev/null @@ -1,139 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.Code.Format; - -import org.jf.dexlib.DexFile; -import org.jf.dexlib.IndexedItem; -import org.jf.dexlib.Code.Instruction; -import org.jf.dexlib.Code.Opcode; -import org.jf.dexlib.Util.Input; - -import java.util.ArrayList; -import java.util.List; - -public class ArrayDataPseudoInstruction extends Instruction -{ - private int elementWidth; - private List values; - - public ArrayDataPseudoInstruction(DexFile dexFile, int elementWidth, List values) { - super(dexFile, Opcode.NOP, (IndexedItem)null); - - this.elementWidth = elementWidth; - this.values = values; - - int byteCount = 0; - - for (byte[] value: values) { - byteCount += value.length; - } - - if (byteCount % elementWidth != 0) { - throw new RuntimeException("There are not a whole number of " + ((Integer)elementWidth).toString() + " byte elements"); - } - - int elementCount = byteCount / elementWidth; - - if (byteCount % 2 != 0) { - byteCount++; - } - - encodedInstruction = new byte[byteCount+8]; - encodedInstruction[0] = 0x00; - encodedInstruction[1] = 0x03; //fill-array-data psuedo-opcode - - encodedInstruction[2] = (byte)elementWidth; - encodedInstruction[3] = (byte)(elementWidth >> 8); - - encodedInstruction[4] = (byte)elementCount; - encodedInstruction[5] = (byte)(elementCount >> 8); - encodedInstruction[6] = (byte)(elementCount >> 16); - encodedInstruction[7] = (byte)(elementCount >> 24); - - int position = 8; - - for (byte[] value: values) { - for (byte byteValue: value) { - encodedInstruction[position++] = byteValue; - } - } - } - - private ArrayDataPseudoInstruction() { - } - - protected void checkFormat(Format format) { - //no need to check the format - } - - public static ArrayDataPseudoInstruction make(DexFile dexFile, Input input) { - byte opcodeByte = input.readByte(); - if (opcodeByte != 0x00) { - throw new RuntimeException("Invalid opcode byte for an ArrayData pseudo-instruction"); - } - byte subopcodeByte = input.readByte(); - if (subopcodeByte != 0x03) { - throw new RuntimeException("Invalid sub-opcode byte for an ArrayData pseudo-instruction"); - } - - int elementWidth = input.readShort(); - int size = input.readInt(); - - List elementsList = new ArrayList(); - - for (int i=0; i getValues() { - return values; - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/Code/Format/Format.java b/dexlib/src/main/java/org/jf/dexlib/Code/Format/Format.java deleted file mode 100644 index a6603567..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Code/Format/Format.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.Code.Format; - -import org.jf.dexlib.Code.Instruction; - -public enum Format -{ - Format10t(Instruction10t.Factory, 2), - Format10x(Instruction10x.Factory, 2), - Format11n(Instruction11n.Factory, 2), - Format11x(Instruction11x.Factory, 2), - Format12x(Instruction12x.Factory, 2), - Format20t(Instruction20t.Factory, 4), - Format21c(Instruction21c.Factory, 4), - Format21h(Instruction21h.Factory, 4), - Format21s(Instruction21s.Factory, 4), - Format21t(Instruction21t.Factory, 4), - Format22b(Instruction22b.Factory, 4), - Format22c(Instruction22c.Factory, 4), - Format22s(Instruction22s.Factory, 4), - Format22t(Instruction22t.Factory, 4), - Format22x(Instruction22x.Factory, 4), - Format23x(Instruction23x.Factory, 4), - Format30t(Instruction30t.Factory, 6), - Format31c(Instruction31c.Factory, 6), - Format31i(Instruction31i.Factory, 6), - Format31t(Instruction31t.Factory, 6), - Format32x(Instruction32x.Factory, 6), - Format35c(Instruction35c.Factory, 6), - Format3rc(Instruction3rc.Factory, 6), - Format51l(Instruction51l.Factory, 10), - ArrayData(null, -1), - PackedSwitchData(null, -1), - SparseSwitchData(null, -1); - - public final Instruction.InstructionFactory Factory; - public final int size; - - private Format(Instruction.InstructionFactory factory, int size) { - this.Factory = factory; - this.size = size; - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction10t.java b/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction10t.java deleted file mode 100644 index cb1d2ab5..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction10t.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.Code.Format; - -import org.jf.dexlib.DexFile; -import org.jf.dexlib.IndexedItem; -import org.jf.dexlib.Code.Instruction; -import org.jf.dexlib.Code.Opcode; - -public class Instruction10t extends Instruction -{ - public static final InstructionFactory Factory = new Factory(); - - public Instruction10t(DexFile dexFile, Opcode opcode, byte offA) { - super(dexFile, opcode, (IndexedItem)null); - - if (offA == 0) { - throw new RuntimeException("The offset cannot be 0. Use goto/32 instead."); - } - - encodedInstruction = new byte[2]; - encodedInstruction[0] = opcode.value; - encodedInstruction[1] = offA; - } - - private Instruction10t(DexFile dexFile, Opcode opcode, byte[] rest) { - super(dexFile, opcode, rest); - - if (getOffset() == 0) { - throw new RuntimeException("The offset cannot be 0. Use goto/32 instead."); - } - } - - private Instruction10t() { - } - - public Format getFormat() { - return Format.Format10t; - } - - protected Instruction makeClone() { - return new Instruction10t(); - } - - private static class Factory implements InstructionFactory { - public Instruction makeInstruction(DexFile dexFile, Opcode opcode, byte[] rest) { - return new Instruction10t(dexFile, opcode, rest); - } - } - - - public byte getOffset() { - return encodedInstruction[1]; - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction10x.java b/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction10x.java deleted file mode 100644 index dd434346..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction10x.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.Code.Format; - -import org.jf.dexlib.DexFile; -import org.jf.dexlib.IndexedItem; -import org.jf.dexlib.Code.Instruction; -import org.jf.dexlib.Code.Opcode; - -public class Instruction10x extends Instruction -{ - public static final InstructionFactory Factory = new Factory(); - - public Instruction10x(DexFile dexFile, Opcode opcode) { - super(dexFile, opcode, (IndexedItem)null); - - encodedInstruction = new byte[2]; - encodedInstruction[0] = opcode.value; - } - - public Instruction10x(DexFile dexFile, Opcode opcode, byte[] rest) { - super(dexFile, opcode, rest); - - if (rest[0] != 0x00) { - throw new RuntimeException("The second byte of the instruction must be 0"); - } - } - - private Instruction10x() { - } - - public Format getFormat() { - return Format.Format10x; - } - - protected Instruction makeClone() { - return new Instruction10x(); - } - - private static class Factory implements InstructionFactory { - public Instruction makeInstruction(DexFile dexFile, Opcode opcode, byte[] rest) { - return new Instruction10x(dexFile, opcode, rest); - } - } -} \ No newline at end of file diff --git a/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction11n.java b/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction11n.java deleted file mode 100644 index e885df8f..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction11n.java +++ /dev/null @@ -1,87 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.Code.Format; - -import org.jf.dexlib.DexFile; -import org.jf.dexlib.IndexedItem; -import org.jf.dexlib.Code.Instruction; -import org.jf.dexlib.Code.Opcode; -import org.jf.dexlib.Util.NumberUtils; - -public class Instruction11n extends Instruction -{ - public static final InstructionFactory Factory = new Factory(); - - public Instruction11n(DexFile dexFile, Opcode opcode, byte regA, byte litB) { - super(dexFile, opcode, (IndexedItem)null); - - if (regA >= 1<<4) { - throw new RuntimeException("The register number must be less than v16"); - } - - if (litB < -(1<<3) || - litB >= 1<<3) { - throw new RuntimeException("The literal value must be between -8 and 7 inclusive"); - } - - encodedInstruction = new byte[2]; - encodedInstruction[0] = opcode.value; - encodedInstruction[1] = (byte)((litB << 4) | regA); - } - - private Instruction11n(DexFile dexFile, Opcode opcode, byte[] rest) { - super(dexFile, opcode, rest); - } - - private Instruction11n() { - } - - public Format getFormat() { - return Format.Format11n; - } - - protected Instruction makeClone() { - return new Instruction11n(); - } - - private static class Factory implements Instruction.InstructionFactory { - public Instruction makeInstruction(DexFile dexFile, Opcode opcode, byte[] rest) { - return new Instruction11n(dexFile, opcode, rest); - } - } - - - public byte getRegister() { - return NumberUtils.decodeLowUnsignedNibble(encodedInstruction[1]); - } - - public byte getLiteral() { - return NumberUtils.decodeHighSignedNibble(encodedInstruction[1]); - } -} \ No newline at end of file diff --git a/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction11x.java b/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction11x.java deleted file mode 100644 index ada38e67..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction11x.java +++ /dev/null @@ -1,78 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.Code.Format; - -import org.jf.dexlib.DexFile; -import org.jf.dexlib.IndexedItem; -import org.jf.dexlib.Code.Instruction; -import org.jf.dexlib.Code.Opcode; -import org.jf.dexlib.Util.NumberUtils; - -public class Instruction11x extends Instruction -{ - public static final Instruction.InstructionFactory Factory = new Factory(); - - public Instruction11x(DexFile dexFile, Opcode opcode, short regA) { - super(dexFile, opcode, (IndexedItem)null); - - if (regA >= 1<<8) { - throw new RuntimeException("The register number must be less than v256"); - } - - encodedInstruction = new byte[2]; - encodedInstruction[0] = opcode.value; - encodedInstruction[1] = (byte)regA; - } - - private Instruction11x(DexFile dexFile, Opcode opcode, byte[] rest) { - super(dexFile, opcode, rest); - } - - private Instruction11x() { - } - - public Format getFormat() { - return Format.Format11x; - } - - protected Instruction makeClone() { - return new Instruction11x(); - } - - private static class Factory implements Instruction.InstructionFactory { - public Instruction makeInstruction(DexFile dexFile, Opcode opcode, byte[] rest) { - return new Instruction11x(dexFile, opcode, rest); - } - } - - - public short getRegister() { - return NumberUtils.decodeUnsignedByte(encodedInstruction[1]); - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction12x.java b/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction12x.java deleted file mode 100644 index 94024b12..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction12x.java +++ /dev/null @@ -1,83 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.Code.Format; - -import org.jf.dexlib.DexFile; -import org.jf.dexlib.IndexedItem; -import org.jf.dexlib.Code.Instruction; -import org.jf.dexlib.Code.Opcode; -import org.jf.dexlib.Util.NumberUtils; - -public class Instruction12x extends Instruction -{ - public static final Instruction.InstructionFactory Factory = new Factory(); - - public Instruction12x(DexFile dexFile, Opcode opcode, byte regA, byte regB) { - super(dexFile, opcode, (IndexedItem)null); - - if (regA >= 1<<4 || - regB >= 1<<4) { - throw new RuntimeException("The register number must be less than v16"); - } - - encodedInstruction = new byte[2]; - encodedInstruction[0] = opcode.value; - encodedInstruction[1] = (byte)((regB << 4) | regA); - } - - private Instruction12x(DexFile dexFile, Opcode opcode, byte[] rest) { - super(dexFile, opcode, rest); - } - - private Instruction12x() { - } - - public Format getFormat() { - return Format.Format12x; - } - - protected Instruction makeClone() { - return new Instruction12x(); - } - - private static class Factory implements Instruction.InstructionFactory { - public Instruction makeInstruction(DexFile dexFile, Opcode opcode, byte[] rest) { - return new Instruction12x(dexFile, opcode, rest); - } - } - - - public byte getRegisterA() { - return NumberUtils.decodeLowUnsignedNibble(encodedInstruction[1]); - } - - public byte getRegisterB() { - return NumberUtils.decodeHighUnsignedNibble(encodedInstruction[1]); - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction20t.java b/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction20t.java deleted file mode 100644 index 31078958..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction20t.java +++ /dev/null @@ -1,83 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.Code.Format; - -import org.jf.dexlib.DexFile; -import org.jf.dexlib.IndexedItem; -import org.jf.dexlib.Code.Instruction; -import org.jf.dexlib.Code.Opcode; -import org.jf.dexlib.Util.NumberUtils; - -public class Instruction20t extends Instruction -{ - public static final Instruction.InstructionFactory Factory = new Factory(); - - public Instruction20t(DexFile dexFile, Opcode opcode, short offA) { - super(dexFile, opcode, (IndexedItem)null); - - if (offA == 0) { - throw new RuntimeException("The offset cannot be 0. Use goto/32 instead."); - } - - encodedInstruction = new byte[4]; - encodedInstruction[0] = opcode.value; - encodedInstruction[2] = (byte)offA; - encodedInstruction[3] = (byte)(offA>>8); - } - - private Instruction20t(DexFile dexFile, Opcode opcode, byte[] rest) { - super(dexFile, opcode, rest); - - if (getOffset() == 0) { - throw new RuntimeException("The offset cannot be 0. Use goto/32 instead."); - } - } - - private Instruction20t() { - } - - public Format getFormat() { - return Format.Format20t; - } - - protected Instruction makeClone() { - return new Instruction20t(); - } - - private static class Factory implements Instruction.InstructionFactory { - public Instruction makeInstruction(DexFile dexFile, Opcode opcode, byte[] rest) { - return new Instruction20t(dexFile, opcode, rest); - } - } - - - public short getOffset() { - return NumberUtils.decodeShort(encodedInstruction[2], encodedInstruction[3]); - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction21c.java b/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction21c.java deleted file mode 100644 index 7333aae8..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction21c.java +++ /dev/null @@ -1,88 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.Code.Format; - -import org.jf.dexlib.DexFile; -import org.jf.dexlib.IndexedItem; -import org.jf.dexlib.TypeIdItem; -import org.jf.dexlib.Code.Instruction; -import org.jf.dexlib.Code.Opcode; -import org.jf.dexlib.Util.NumberUtils; - -public class Instruction21c extends Instruction -{ - public static final Instruction.InstructionFactory Factory = new Factory(); - - public Instruction21c(DexFile dexFile, Opcode opcode, short regA, IndexedItem item) { - super(dexFile, opcode, item); - - if (regA >= 1<<8) { - throw new RuntimeException("The register number must be less than v256"); - } - - if (opcode == Opcode.NEW_INSTANCE && ((TypeIdItem)item).getTypeDescriptor().charAt(0) != 'L') { - throw new RuntimeException("Only class references can be used with the new-instance opcode"); - } - - encodedInstruction = new byte[4]; - encodedInstruction[0] = opcode.value; - encodedInstruction[1] = (byte)regA; - //the item index will be set later, during placement/writing - } - - private Instruction21c(DexFile dexFile, Opcode opcode, byte[] rest) { - super(dexFile, opcode, rest); - - if (opcode == Opcode.NEW_INSTANCE && ((TypeIdItem)this.getReferencedItem()).getTypeDescriptor().charAt(0) != 'L') { - throw new RuntimeException("Only class references can be used with the new-instance opcode"); - } - } - - private Instruction21c() { - } - - public Format getFormat() { - return Format.Format21c; - } - - protected Instruction makeClone() { - return new Instruction21c(); - } - - private static class Factory implements Instruction.InstructionFactory { - public Instruction makeInstruction(DexFile dexFile, Opcode opcode, byte[] rest) { - return new Instruction21c(dexFile, opcode, rest); - } - } - - - public short getRegister() { - return NumberUtils.decodeUnsignedByte(encodedInstruction[1]); - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction21h.java b/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction21h.java deleted file mode 100644 index 5969f0a8..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction21h.java +++ /dev/null @@ -1,84 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.Code.Format; - -import org.jf.dexlib.DexFile; -import org.jf.dexlib.IndexedItem; -import org.jf.dexlib.Code.Instruction; -import org.jf.dexlib.Code.Opcode; -import org.jf.dexlib.Util.NumberUtils; - -public class Instruction21h extends Instruction -{ - public static final Instruction.InstructionFactory Factory = new Factory(); - - public Instruction21h(DexFile dexFile, Opcode opcode, short regA, short litB) { - super(dexFile, opcode, (IndexedItem)null); - - if (regA >= 1<<8) { - throw new RuntimeException("The register number must be less than v256"); - } - - encodedInstruction = new byte[4]; - encodedInstruction[0] = opcode.value; - encodedInstruction[1] = (byte)regA; - encodedInstruction[2] = (byte)litB; - encodedInstruction[3] = (byte)(litB >> 8); - } - - private Instruction21h(DexFile dexFile, Opcode opcode, byte[] rest) { - super(dexFile, opcode, rest); - } - - private Instruction21h() { - } - - public Format getFormat() { - return Format.Format21h; - } - - protected Instruction makeClone() { - return new Instruction21h(); - } - - private static class Factory implements Instruction.InstructionFactory { - public Instruction makeInstruction(DexFile dexFile, Opcode opcode, byte[] rest) { - return new Instruction21h(dexFile, opcode, rest); - } - } - - - public short getRegister() { - return NumberUtils.decodeUnsignedByte(encodedInstruction[1]); - } - - public short getLiteral() { - return NumberUtils.decodeShort(encodedInstruction[2], encodedInstruction[3]); - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction21s.java b/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction21s.java deleted file mode 100644 index fb382b91..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction21s.java +++ /dev/null @@ -1,84 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.Code.Format; - -import org.jf.dexlib.DexFile; -import org.jf.dexlib.IndexedItem; -import org.jf.dexlib.Code.Instruction; -import org.jf.dexlib.Code.Opcode; -import org.jf.dexlib.Util.NumberUtils; - -public class Instruction21s extends Instruction -{ - public static final Instruction.InstructionFactory Factory = new Factory(); - - public Instruction21s(DexFile dexFile, Opcode opcode, short regA, short litB) { - super(dexFile, opcode, (IndexedItem)null); - - if (regA >= 1<<8) { - throw new RuntimeException("The register number must be less than v256"); - } - - encodedInstruction = new byte[4]; - encodedInstruction[0] = opcode.value; - encodedInstruction[1] = (byte)regA; - encodedInstruction[2] = (byte)litB; - encodedInstruction[3] = (byte)(litB >> 8); - } - - private Instruction21s(DexFile dexFile, Opcode opcode, byte[] rest) { - super(dexFile, opcode, rest); - } - - private Instruction21s() { - } - - public Format getFormat() { - return Format.Format21s; - } - - protected Instruction makeClone() { - return new Instruction21s(); - } - - private static class Factory implements Instruction.InstructionFactory { - public Instruction makeInstruction(DexFile dexFile, Opcode opcode, byte[] rest) { - return new Instruction21s(dexFile, opcode, rest); - } - } - - - public short getRegister() { - return NumberUtils.decodeUnsignedByte(encodedInstruction[1]); - } - - public short getLiteral() { - return NumberUtils.decodeShort(encodedInstruction[2], encodedInstruction[3]); - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction21t.java b/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction21t.java deleted file mode 100644 index e12df804..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction21t.java +++ /dev/null @@ -1,92 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.Code.Format; - -import org.jf.dexlib.DexFile; -import org.jf.dexlib.IndexedItem; -import org.jf.dexlib.Code.Instruction; -import org.jf.dexlib.Code.Opcode; -import org.jf.dexlib.Util.NumberUtils; - -public class Instruction21t extends Instruction -{ - public static final Instruction.InstructionFactory Factory = new Factory(); - - public Instruction21t(DexFile dexFile, Opcode opcode, short regA, short offB) { - super(dexFile, opcode, (IndexedItem)null); - - if (regA >= 1<<8) { - throw new RuntimeException("The register number must be less than v256"); - } - - if (offB == 0) { - throw new RuntimeException("The offset cannot be 0."); - } - - encodedInstruction = new byte[4]; - encodedInstruction[0] = opcode.value; - encodedInstruction[1] = (byte)regA; - encodedInstruction[2] = (byte)offB; - encodedInstruction[3] = (byte)(offB >> 8); - } - - private Instruction21t(DexFile dexFile, Opcode opcode, byte[] rest) { - super(dexFile, opcode, rest); - - if (getOffset() == 0) { - throw new RuntimeException("The offset cannot be 0."); - } - } - - private Instruction21t() { - } - - public Format getFormat() { - return Format.Format21t; - } - - protected Instruction makeClone() { - return new Instruction21t(); - } - - private static class Factory implements Instruction.InstructionFactory { - public Instruction makeInstruction(DexFile dexFile, Opcode opcode, byte[] rest) { - return new Instruction21t(dexFile, opcode, rest); - } - } - - - public short getRegister() { - return NumberUtils.decodeUnsignedByte(encodedInstruction[1]); - } - - public short getOffset() { - return NumberUtils.decodeShort(encodedInstruction[2], encodedInstruction[3]); - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction22b.java b/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction22b.java deleted file mode 100644 index 23f8c981..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction22b.java +++ /dev/null @@ -1,89 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.Code.Format; - -import org.jf.dexlib.DexFile; -import org.jf.dexlib.IndexedItem; -import org.jf.dexlib.Code.Instruction; -import org.jf.dexlib.Code.Opcode; -import org.jf.dexlib.Util.NumberUtils; - -public class Instruction22b extends Instruction -{ - public static final Instruction.InstructionFactory Factory = new Factory(); - - public Instruction22b(DexFile dexFile, Opcode opcode, short regA, short regB, byte litC) { - super(dexFile, opcode, (IndexedItem)null); - - if (regA >= 1<<8 || - regB >= 1<<8) { - throw new RuntimeException("The register number must be less than v256"); - } - - encodedInstruction = new byte[4]; - encodedInstruction[0] = opcode.value; - encodedInstruction[1] = (byte)regA; - encodedInstruction[2] = (byte)regB; - encodedInstruction[3] = litC; - } - - private Instruction22b(DexFile dexFile, Opcode opcode, byte[] rest) { - super(dexFile, opcode, rest); - } - - private Instruction22b() { - } - - public Format getFormat() { - return Format.Format22b; - } - - protected Instruction makeClone() { - return new Instruction22b(); - } - - private static class Factory implements Instruction.InstructionFactory { - public Instruction makeInstruction(DexFile dexFile, Opcode opcode, byte[] rest) { - return new Instruction22b(dexFile, opcode, rest); - } - } - - - public short getRegisterA() { - return NumberUtils.decodeUnsignedByte(encodedInstruction[1]); - } - - public short getRegisterB() { - return NumberUtils.decodeUnsignedByte(encodedInstruction[2]); - } - - public byte getLiteral() { - return encodedInstruction[3]; - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction22c.java b/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction22c.java deleted file mode 100644 index 79dfbde0..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction22c.java +++ /dev/null @@ -1,84 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.Code.Format; - -import org.jf.dexlib.DexFile; -import org.jf.dexlib.IndexedItem; -import org.jf.dexlib.Code.Instruction; -import org.jf.dexlib.Code.Opcode; -import org.jf.dexlib.Util.NumberUtils; - -public class Instruction22c extends Instruction -{ - public static final Instruction.InstructionFactory Factory = new Factory(); - - public Instruction22c(DexFile dexFile, Opcode opcode, byte regA, byte regB, IndexedItem item) { - super(dexFile, opcode, item); - - if (regA >= 1<<4 || - regB >= 1<<4) { - throw new RuntimeException("The register number must be less than v16"); - } - - encodedInstruction = new byte[4]; - encodedInstruction[0] = opcode.value; - encodedInstruction[1] = (byte)((regB << 4) | regA); - //the item index will be set later, during placement/writing - } - - private Instruction22c(DexFile dexFile, Opcode opcode, byte[] rest) { - super(dexFile, opcode, rest); - } - - private Instruction22c() { - } - - public Format getFormat() { - return Format.Format22c; - } - - protected Instruction makeClone() { - return new Instruction22c(); - } - - private static class Factory implements Instruction.InstructionFactory { - public Instruction makeInstruction(DexFile dexFile, Opcode opcode, byte[] rest) { - return new Instruction22c(dexFile, opcode, rest); - } - } - - - public byte getRegisterA() { - return NumberUtils.decodeLowUnsignedNibble(encodedInstruction[1]); - } - - public byte getRegisterB() { - return NumberUtils.decodeHighUnsignedNibble(encodedInstruction[1]); - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction22s.java b/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction22s.java deleted file mode 100644 index c9905df7..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction22s.java +++ /dev/null @@ -1,89 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.Code.Format; - -import org.jf.dexlib.DexFile; -import org.jf.dexlib.IndexedItem; -import org.jf.dexlib.Code.Instruction; -import org.jf.dexlib.Code.Opcode; -import org.jf.dexlib.Util.NumberUtils; - -public class Instruction22s extends Instruction -{ - public static final Instruction.InstructionFactory Factory = new Factory(); - - public Instruction22s(DexFile dexFile, Opcode opcode, byte regA, byte regB, short litC) { - super(dexFile, opcode, (IndexedItem)null); - - if (regA >= 1<<4 || - regB >= 1<<4) { - throw new RuntimeException("The register number must be less than v16"); - } - - encodedInstruction = new byte[4]; - encodedInstruction[0] = opcode.value; - encodedInstruction[1] = (byte)((regB << 4) | regA); - encodedInstruction[2] = (byte)litC; - encodedInstruction[3] = (byte)(litC >> 8); - } - - private Instruction22s(DexFile dexFile, Opcode opcode, byte[] rest) { - super(dexFile, opcode, rest); - } - - private Instruction22s() { - } - - public Format getFormat() { - return Format.Format22s; - } - - protected Instruction makeClone() { - return new Instruction22s(); - } - - private static class Factory implements Instruction.InstructionFactory { - public Instruction makeInstruction(DexFile dexFile, Opcode opcode, byte[] rest) { - return new Instruction22s(dexFile, opcode, rest); - } - } - - - public byte getRegisterA() { - return NumberUtils.decodeLowUnsignedNibble(encodedInstruction[1]); - } - - public byte getRegisterB() { - return NumberUtils.decodeHighUnsignedNibble(encodedInstruction[1]); - } - - public short getLiteral() { - return NumberUtils.decodeShort(encodedInstruction[2], encodedInstruction[3]); - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction22t.java b/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction22t.java deleted file mode 100644 index f37991af..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction22t.java +++ /dev/null @@ -1,97 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.Code.Format; - -import org.jf.dexlib.DexFile; -import org.jf.dexlib.IndexedItem; -import org.jf.dexlib.Code.Instruction; -import org.jf.dexlib.Code.Opcode; -import org.jf.dexlib.Util.NumberUtils; - -public class Instruction22t extends Instruction -{ - public static final Instruction.InstructionFactory Factory = new Factory(); - - public Instruction22t(DexFile dexFile, Opcode opcode, byte regA, byte regB, short offC) { - super(dexFile, opcode, (IndexedItem)null); - - if (regA >= 1<<4 || - regB >= 1<<4) { - throw new RuntimeException("The register number must be less than v16"); - } - - if (offC == 0) { - throw new RuntimeException("The offset cannot be 0."); - } - - encodedInstruction = new byte[4]; - encodedInstruction[0] = opcode.value; - encodedInstruction[1] = (byte)((regB << 4) | regA); - encodedInstruction[2] = (byte)offC; - encodedInstruction[3] = (byte)(offC >> 8); - } - - private Instruction22t(DexFile dexFile, Opcode opcode, byte[] rest) { - super(dexFile, opcode, rest); - - if (getOffset() == 0) { - throw new RuntimeException("The offset cannot be 0."); - } - } - - private Instruction22t() { - } - - public Format getFormat() { - return Format.Format22t; - } - - protected Instruction makeClone() { - return new Instruction22t(); - } - - private static class Factory implements Instruction.InstructionFactory { - public Instruction makeInstruction(DexFile dexFile, Opcode opcode, byte[] rest) { - return new Instruction22t(dexFile, opcode, rest); - } - } - - - public byte getRegisterA() { - return NumberUtils.decodeLowUnsignedNibble(encodedInstruction[1]); - } - - public byte getRegisterB() { - return NumberUtils.decodeHighUnsignedNibble(encodedInstruction[1]); - } - - public short getOffset() { - return NumberUtils.decodeShort(encodedInstruction[2], encodedInstruction[3]); - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction22x.java b/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction22x.java deleted file mode 100644 index cd044afd..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction22x.java +++ /dev/null @@ -1,88 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.Code.Format; - -import org.jf.dexlib.DexFile; -import org.jf.dexlib.IndexedItem; -import org.jf.dexlib.Code.Instruction; -import org.jf.dexlib.Code.Opcode; -import org.jf.dexlib.Util.NumberUtils; - -public class Instruction22x extends Instruction -{ - public static final Instruction.InstructionFactory Factory = new Factory(); - - public Instruction22x(DexFile dexFile, Opcode opcode, short regA, int regB) { - super(dexFile, opcode, (IndexedItem)null); - - if (regA >= 1<<8) { - throw new RuntimeException("The register number must be less than v16"); - } - - if (regB >= 1<<16) { - throw new RuntimeException("The register number must be less than v65536"); - } - - encodedInstruction = new byte[4]; - encodedInstruction[0] = opcode.value; - encodedInstruction[1] = (byte)regA; - encodedInstruction[2] = (byte)regB; - encodedInstruction[3] = (byte)(regB >> 8); - } - - private Instruction22x(DexFile dexFile, Opcode opcode, byte[] rest) { - super(dexFile, opcode, rest); - } - - private Instruction22x() { - } - - public Format getFormat() { - return Format.Format22x; - } - - protected Instruction makeClone() { - return new Instruction22x(); - } - - private static class Factory implements Instruction.InstructionFactory { - public Instruction makeInstruction(DexFile dexFile, Opcode opcode, byte[] rest) { - return new Instruction22x(dexFile, opcode, rest); - } - } - - - public short getRegisterA() { - return NumberUtils.decodeUnsignedByte(encodedInstruction[1]); - } - - public int getRegisterB() { - return NumberUtils.decodeUnsignedShort(encodedInstruction[2], encodedInstruction[3]); - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction23x.java b/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction23x.java deleted file mode 100644 index f1b940d8..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction23x.java +++ /dev/null @@ -1,90 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.Code.Format; - -import org.jf.dexlib.DexFile; -import org.jf.dexlib.IndexedItem; -import org.jf.dexlib.Code.Instruction; -import org.jf.dexlib.Code.Opcode; -import org.jf.dexlib.Util.NumberUtils; - -public class Instruction23x extends Instruction -{ - public static final Instruction.InstructionFactory Factory = new Factory(); - - public Instruction23x(DexFile dexFile, Opcode opcode, short regA, short regB, short regC) { - super(dexFile, opcode, (IndexedItem)null); - - if (regA >= 1<<8 || - regB >= 1<<8 || - regC >= 1<<8) { - throw new RuntimeException("The register number must be less than v256"); - } - - encodedInstruction = new byte[4]; - encodedInstruction[0] = opcode.value; - encodedInstruction[1] = (byte)regA; - encodedInstruction[2] = (byte)regB; - encodedInstruction[3] = (byte)regC; - } - - private Instruction23x(DexFile dexFile, Opcode opcode, byte[] rest) { - super(dexFile, opcode, rest); - } - - private Instruction23x() { - } - - public Format getFormat() { - return Format.Format23x; - } - - protected Instruction makeClone() { - return new Instruction23x(); - } - - private static class Factory implements Instruction.InstructionFactory { - public Instruction makeInstruction(DexFile dexFile, Opcode opcode, byte[] rest) { - return new Instruction23x(dexFile, opcode, rest); - } - } - - - public short getRegisterA() { - return NumberUtils.decodeUnsignedByte(encodedInstruction[1]); - } - - public short getRegisterB() { - return NumberUtils.decodeUnsignedByte(encodedInstruction[2]); - } - - public short getRegisterC() { - return NumberUtils.decodeUnsignedByte(encodedInstruction[3]); - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction30t.java b/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction30t.java deleted file mode 100644 index 0219b95c..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction30t.java +++ /dev/null @@ -1,78 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.Code.Format; - -import org.jf.dexlib.DexFile; -import org.jf.dexlib.IndexedItem; -import org.jf.dexlib.Code.Instruction; -import org.jf.dexlib.Code.Opcode; -import org.jf.dexlib.Util.NumberUtils; - -public class Instruction30t extends Instruction -{ - public static final Instruction.InstructionFactory Factory = new Factory(); - - public Instruction30t(DexFile dexFile, Opcode opcode, int offA) { - super(dexFile, opcode, (IndexedItem)null); - - encodedInstruction = new byte[6]; - encodedInstruction[0] = opcode.value; - encodedInstruction[2] = (byte)offA; - encodedInstruction[3] = (byte)(offA >> 8); - encodedInstruction[4] = (byte)(offA >> 16); - encodedInstruction[5] = (byte)(offA >> 24); - } - - private Instruction30t(DexFile dexFile, Opcode opcode, byte[] rest) { - super(dexFile, opcode, rest); - } - - private Instruction30t() { - } - - public Format getFormat() { - return Format.Format30t; - } - - protected Instruction makeClone() { - return new Instruction30t(); - } - - private static class Factory implements Instruction.InstructionFactory { - public Instruction makeInstruction(DexFile dexFile, Opcode opcode, byte[] rest) { - return new Instruction30t(dexFile, opcode, rest); - } - } - - - public int getOffset() { - return NumberUtils.decodeInt(encodedInstruction[2], encodedInstruction[3], encodedInstruction[4], - encodedInstruction[5]); - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction31c.java b/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction31c.java deleted file mode 100644 index 8f5f17c3..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction31c.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.Code.Format; - -import org.jf.dexlib.DexFile; -import org.jf.dexlib.IndexedItem; -import org.jf.dexlib.Code.Instruction; -import org.jf.dexlib.Code.Opcode; -import org.jf.dexlib.Util.NumberUtils; - -public class Instruction31c extends Instruction -{ - public static final Instruction.InstructionFactory Factory = new Factory(); - - public Instruction31c(DexFile dexFile, Opcode opcode, short regA, IndexedItem item) { - super(dexFile, opcode, item); - - if (regA >= 1<<8) { - throw new RuntimeException("The register number must be less than v256"); - } - - encodedInstruction = new byte[6]; - encodedInstruction[0] = opcode.value; - encodedInstruction[1] = (byte)regA; - //the item index will be set later, during placement/writing - } - - private Instruction31c(DexFile dexFile, Opcode opcode, byte[] rest) { - super(dexFile, opcode, rest); - } - - private Instruction31c() { - } - - public Format getFormat() { - return Format.Format31c; - } - - protected Instruction makeClone() { - return new Instruction31c(); - } - - private static class Factory implements Instruction.InstructionFactory { - public Instruction makeInstruction(DexFile dexFile, Opcode opcode, byte[] rest) { - return new Instruction31c(dexFile, opcode, rest); - } - } - - - public short getRegister() { - return NumberUtils.decodeUnsignedByte(encodedInstruction[1]); - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction31i.java b/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction31i.java deleted file mode 100644 index 6d9fa6c3..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction31i.java +++ /dev/null @@ -1,87 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.Code.Format; - -import org.jf.dexlib.DexFile; -import org.jf.dexlib.IndexedItem; -import org.jf.dexlib.Code.Instruction; -import org.jf.dexlib.Code.Opcode; -import org.jf.dexlib.Util.NumberUtils; - -public class Instruction31i extends Instruction -{ - public static final Instruction.InstructionFactory Factory = new Factory(); - - public Instruction31i(DexFile dexFile, Opcode opcode, short regA, int litB) { - super(dexFile, opcode, (IndexedItem)null); - - if (regA >= 1<<8) { - throw new RuntimeException("The register number must be less than v256"); - } - - encodedInstruction = new byte[6]; - encodedInstruction[0] = opcode.value; - encodedInstruction[1] = (byte)regA; - encodedInstruction[2] = (byte)litB; - encodedInstruction[3] = (byte)(litB >> 8); - encodedInstruction[4] = (byte)(litB >> 16); - encodedInstruction[5] = (byte)(litB >> 24); - } - - private Instruction31i(DexFile dexFile, Opcode opcode, byte[] rest) { - super(dexFile, opcode, rest); - } - - private Instruction31i() { - } - - public Format getFormat() { - return Format.Format31i; - } - - protected Instruction makeClone() { - return new Instruction31i(); - } - - private static class Factory implements Instruction.InstructionFactory { - public Instruction makeInstruction(DexFile dexFile, Opcode opcode, byte[] rest) { - return new Instruction31i(dexFile, opcode, rest); - } - } - - - public short getRegister() { - return NumberUtils.decodeUnsignedByte(encodedInstruction[1]); - } - - public int getLiteral() { - return NumberUtils.decodeInt(encodedInstruction[2], encodedInstruction[3], encodedInstruction[4], - encodedInstruction[5]); - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction31t.java b/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction31t.java deleted file mode 100644 index c52de6bb..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction31t.java +++ /dev/null @@ -1,87 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.Code.Format; - -import org.jf.dexlib.DexFile; -import org.jf.dexlib.IndexedItem; -import org.jf.dexlib.Code.Instruction; -import org.jf.dexlib.Code.Opcode; -import org.jf.dexlib.Util.NumberUtils; - -public class Instruction31t extends Instruction -{ - public static final Instruction.InstructionFactory Factory = new Factory(); - - public Instruction31t(DexFile dexFile, Opcode opcode, short regA, int offB) { - super(dexFile, opcode, (IndexedItem)null); - - if (regA >= 1<<8) { - throw new RuntimeException("The register number must be less than v256"); - } - - encodedInstruction = new byte[6]; - encodedInstruction[0] = opcode.value; - encodedInstruction[1] = (byte)regA; - encodedInstruction[2] = (byte)offB; - encodedInstruction[3] = (byte)(offB >> 8); - encodedInstruction[4] = (byte)(offB >> 16); - encodedInstruction[5] = (byte)(offB >> 24); - } - - private Instruction31t(DexFile dexFile, Opcode opcode, byte[] rest) { - super(dexFile, opcode, rest); - } - - private Instruction31t() { - } - - public Format getFormat() { - return Format.Format31t; - } - - protected Instruction makeClone() { - return new Instruction31t(); - } - - private static class Factory implements Instruction.InstructionFactory { - public Instruction makeInstruction(DexFile dexFile, Opcode opcode, byte[] rest) { - return new Instruction31t(dexFile, opcode, rest); - } - } - - - public short getRegister() { - return NumberUtils.decodeUnsignedByte(encodedInstruction[1]); - } - - public int getOffset() { - return NumberUtils.decodeInt(encodedInstruction[2], encodedInstruction[3], encodedInstruction[4], - encodedInstruction[5]); - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction32x.java b/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction32x.java deleted file mode 100644 index 0e057c99..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction32x.java +++ /dev/null @@ -1,86 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.Code.Format; - -import org.jf.dexlib.DexFile; -import org.jf.dexlib.IndexedItem; -import org.jf.dexlib.Code.Instruction; -import org.jf.dexlib.Code.Opcode; -import org.jf.dexlib.Util.NumberUtils; - -public class Instruction32x extends Instruction -{ - public static final Instruction.InstructionFactory Factory = new Factory(); - - public Instruction32x(DexFile dexFile, Opcode opcode, int regA, int regB) { - super(dexFile, opcode, (IndexedItem)null); - - if (regA >= 1<<16 || - regB >= 1<<16) { - throw new RuntimeException("The register number must be less than v65536"); - } - - encodedInstruction = new byte[6]; - encodedInstruction[0] = opcode.value; - encodedInstruction[2] = (byte)regA; - encodedInstruction[3] = (byte)(regA >> 8); - encodedInstruction[4] = (byte)regB; - encodedInstruction[5] = (byte)(regB >> 8); - } - - private Instruction32x(DexFile dexFile, Opcode opcode, byte[] rest) { - super(dexFile, opcode, rest); - } - - private Instruction32x() { - } - - public Format getFormat() { - return Format.Format32x; - } - - protected Instruction makeClone() { - return new Instruction32x(); - } - - private static class Factory implements Instruction.InstructionFactory { - public Instruction makeInstruction(DexFile dexFile, Opcode opcode, byte[] rest) { - return new Instruction32x(dexFile, opcode, rest); - } - } - - - public int getRegisterA() { - return NumberUtils.decodeUnsignedShort(encodedInstruction[2], encodedInstruction[3]); - } - - public int getRegisterB() { - return NumberUtils.decodeUnsignedShort(encodedInstruction[4], encodedInstruction[5]); - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction35c.java b/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction35c.java deleted file mode 100644 index 9fc37ce5..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction35c.java +++ /dev/null @@ -1,144 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.Code.Format; - -import org.jf.dexlib.DexFile; -import org.jf.dexlib.IndexedItem; -import org.jf.dexlib.MethodIdItem; -import org.jf.dexlib.TypeIdItem; -import org.jf.dexlib.Code.Instruction; -import org.jf.dexlib.Code.Opcode; -import static org.jf.dexlib.Code.Opcode.*; -import org.jf.dexlib.Util.NumberUtils; - -public class Instruction35c extends Instruction -{ - public static final Instruction.InstructionFactory Factory = new Factory(); - - public Instruction35c(DexFile dexFile, Opcode opcode, int regCount, byte regD, byte regE, byte regF, byte regG, - byte regA, IndexedItem item) { - super(dexFile, opcode, item); - - if (regCount > 5) { - throw new RuntimeException("regCount cannot be greater than 5"); - } - - if (regD >= 1<<4 || - regE >= 1<<4 || - regF >= 1<<4 || - regG >= 1<<4 || - regA >= 1<<4) { - throw new RuntimeException("All register args must fit in 4 bits"); - } - - encodedInstruction = new byte[6]; - encodedInstruction[0] = opcode.value; - encodedInstruction[1] = (byte)((regCount << 4) | regA); - //the item index will be set later, during placement/writing - encodedInstruction[4] = (byte)((regE << 4) | regD); - encodedInstruction[5] = (byte)((regG << 4) | regF); - - checkItem(); - } - - private Instruction35c(DexFile dexFile, Opcode opcode, byte[] rest) { - super(dexFile, opcode, rest); - - if (getRegCount() > 5) { - throw new RuntimeException("regCount cannot be greater than 5"); - } - - checkItem(); - } - - private Instruction35c() { - } - - public Format getFormat() { - return Format.Format35c; - } - - protected Instruction makeClone() { - return new Instruction35c(); - } - - private static class Factory implements Instruction.InstructionFactory { - public Instruction makeInstruction(DexFile dexFile, Opcode opcode, byte[] rest) { - return new Instruction35c(dexFile, opcode, rest); - } - } - - - public byte getRegisterA() { - return NumberUtils.decodeLowUnsignedNibble(encodedInstruction[1]); - } - - public byte getRegCount() { - return NumberUtils.decodeHighUnsignedNibble(encodedInstruction[1]); - } - - public byte getRegisterD() { - return NumberUtils.decodeLowUnsignedNibble(encodedInstruction[4]); - } - - public byte getRegisterE() { - return NumberUtils.decodeHighUnsignedNibble(encodedInstruction[4]); - } - - public byte getRegisterF() { - return NumberUtils.decodeLowUnsignedNibble(encodedInstruction[5]); - } - - public byte getRegisterG() { - return NumberUtils.decodeHighUnsignedNibble(encodedInstruction[5]); - } - - private void checkItem() { - Opcode opcode = getOpcode(); - IndexedItem item = getReferencedItem(); - - if (opcode == FILLED_NEW_ARRAY) { - //check data for filled-new-array opcode - String type = ((TypeIdItem)item).getTypeDescriptor(); - if (type.charAt(0) != '[') { - throw new RuntimeException("The type must be an array type"); - } - if (type.charAt(1) == 'J' || type.charAt(1) == 'D') { - throw new RuntimeException("The type cannot be an array of longs or doubles"); - } - } else if (opcode.value >= INVOKE_VIRTUAL.value && opcode.value <= INVOKE_INTERFACE.value) { - //check data for invoke-* opcodes - MethodIdItem methodIdItem = (MethodIdItem)item; - if (methodIdItem.getParameterRegisterCount(opcode == INVOKE_STATIC) != getRegCount()) { - throw new RuntimeException("regCount does not match the number of arguments of the method"); - } - } - } - -} diff --git a/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction3rc.java b/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction3rc.java deleted file mode 100644 index 6de44a1a..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction3rc.java +++ /dev/null @@ -1,124 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.Code.Format; - -import org.jf.dexlib.DexFile; -import org.jf.dexlib.IndexedItem; -import org.jf.dexlib.MethodIdItem; -import org.jf.dexlib.TypeIdItem; -import org.jf.dexlib.Code.Instruction; -import org.jf.dexlib.Code.Opcode; -import static org.jf.dexlib.Code.Opcode.*; -import org.jf.dexlib.Util.NumberUtils; - -public class Instruction3rc extends Instruction -{ - public static final Instruction.InstructionFactory Factory = new Factory(); - - public Instruction3rc(DexFile dexFile, Opcode opcode, short regCount, int startReg, IndexedItem item) { - super(dexFile, opcode, item); - - if (regCount >= 1<<8) { - throw new RuntimeException("regCount must be less than 256"); - } - if (regCount < 0) { - throw new RuntimeException("regCount cannot be negative"); - } - - if (startReg >= 1<<16) { - throw new RuntimeException("The beginning register of the range must be less than 65536"); - } - if (startReg < 0) { - throw new RuntimeException("The beginning register of the range cannot be negative"); - } - - encodedInstruction = new byte[6]; - encodedInstruction[0] = opcode.value; - encodedInstruction[1] = (byte)regCount; - //the item index will be set later, during placement/writing - encodedInstruction[4] = (byte)startReg; - encodedInstruction[5] = (byte)(startReg >> 8); - - checkItem(); - } - - private Instruction3rc(DexFile dexFile, Opcode opcode, byte[] rest) { - super(dexFile, opcode, rest); - - checkItem(); - } - - private Instruction3rc() { - } - - public Format getFormat() { - return Format.Format3rc; - } - - protected Instruction makeClone() { - return new Instruction3rc(); - } - - private static class Factory implements Instruction.InstructionFactory { - public Instruction makeInstruction(DexFile dexFile, Opcode opcode, byte[] rest) { - return new Instruction3rc(dexFile, opcode, rest); - } - } - - - public short getRegCount() { - return NumberUtils.decodeUnsignedByte(encodedInstruction[1]); - } - - public int getStartRegister() { - return NumberUtils.decodeUnsignedShort(encodedInstruction[4], encodedInstruction[5]); - } - - private void checkItem() { - Opcode opcode = getOpcode(); - IndexedItem item = getReferencedItem(); - - if (opcode == FILLED_NEW_ARRAY_RANGE) { - //check data for filled-new-array/range opcode - String type = ((TypeIdItem)item).getTypeDescriptor(); - if (type.charAt(0) != '[') { - throw new RuntimeException("The type must be an array type"); - } - if (type.charAt(1) == 'J' || type.charAt(1) == 'D') { - throw new RuntimeException("The type cannot be an array of longs or doubles"); - } - } else if (opcode.value >= INVOKE_VIRTUAL_RANGE.value && opcode.value <= INVOKE_INTERFACE_RANGE.value) { - //check data for invoke-*/range opcodes - MethodIdItem methodIdItem = (MethodIdItem)item; - if (methodIdItem.getParameterRegisterCount(opcode == INVOKE_STATIC_RANGE) != getRegCount()) { - throw new RuntimeException("regCount does not match the number of arguments of the method"); - } - } - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction51l.java b/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction51l.java deleted file mode 100644 index 959126fe..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Code/Format/Instruction51l.java +++ /dev/null @@ -1,90 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.Code.Format; - -import org.jf.dexlib.DexFile; -import org.jf.dexlib.IndexedItem; -import org.jf.dexlib.Code.Instruction; -import org.jf.dexlib.Code.Opcode; -import org.jf.dexlib.Util.NumberUtils; - -public class Instruction51l extends Instruction -{ - public static final Instruction.InstructionFactory Factory = new Factory(); - - public Instruction51l(DexFile dexFile, Opcode opcode, short regA, long litB) { - super(dexFile, opcode, (IndexedItem)null); - - if (regA >= 1<<8) { - throw new RuntimeException("The register number must be less than v256"); - } - - encodedInstruction = new byte[10]; - encodedInstruction[0] = opcode.value; - encodedInstruction[1] = (byte)regA; - encodedInstruction[2] = (byte)litB; - encodedInstruction[3] = (byte)(litB >> 8); - encodedInstruction[4] = (byte)(litB >> 16); - encodedInstruction[5] = (byte)(litB >> 24); - encodedInstruction[6] = (byte)(litB >> 32); - encodedInstruction[7] = (byte)(litB >> 40); - encodedInstruction[8] = (byte)(litB >> 48); - encodedInstruction[9] = (byte)(litB >> 56); - } - - private Instruction51l(DexFile dexFile, Opcode opcode, byte[] rest) { - super(dexFile, opcode, rest); - } - - private Instruction51l() { - } - - public Format getFormat() { - return Format.Format51l; - } - - protected Instruction makeClone() { - return new Instruction51l(); - } - - private static class Factory implements Instruction.InstructionFactory { - public Instruction makeInstruction(DexFile dexFile, Opcode opcode, byte[] rest) { - return new Instruction51l(dexFile, opcode, rest); - } - } - - - public short getRegister() { - return NumberUtils.decodeUnsignedByte(encodedInstruction[1]); - } - - public long getLiteral() { - return NumberUtils.decodeLong(encodedInstruction, 2); - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/Code/Format/PackedSwitchDataPseudoInstruction.java b/dexlib/src/main/java/org/jf/dexlib/Code/Format/PackedSwitchDataPseudoInstruction.java deleted file mode 100644 index 50864adb..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Code/Format/PackedSwitchDataPseudoInstruction.java +++ /dev/null @@ -1,118 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.Code.Format; - -import org.jf.dexlib.DexFile; -import org.jf.dexlib.IndexedItem; -import org.jf.dexlib.Code.Instruction; -import org.jf.dexlib.Code.Opcode; -import org.jf.dexlib.Util.Input; - -public class PackedSwitchDataPseudoInstruction extends Instruction -{ - private int firstKey; - private int[] targets; - - public PackedSwitchDataPseudoInstruction(DexFile dexFile, int firstKey, int[] targets) { - super(dexFile, Opcode.NOP, (IndexedItem)null); - this.firstKey = firstKey; - this.targets = targets; - - if (targets.length > 0xFFFF) { - throw new RuntimeException("The packed-switch data contains too many elements. " + - "The maximum number of switch elements is 65535"); - } - - encodedInstruction = new byte[targets.length * 4 + 8]; - encodedInstruction[0] = 0x00; - encodedInstruction[1] = 0x01; //packed-switch pseudo-opcode - - encodedInstruction[2] = (byte)targets.length; - encodedInstruction[3] = (byte)(targets.length >> 8); - - encodedInstruction[4] = (byte)firstKey; - encodedInstruction[5] = (byte)(firstKey >> 8); - encodedInstruction[6] = (byte)(firstKey >> 16); - encodedInstruction[7] = (byte)(firstKey >> 24); - - int position= 8; - for (int target: targets) { - encodedInstruction[position++] = (byte)target; - encodedInstruction[position++] = (byte)(target >> 8); - encodedInstruction[position++] = (byte)(target >> 16); - encodedInstruction[position++] = (byte)(target >> 24); - } - } - - protected void checkFormat(Format format) { - //no need to check the format - } - - private PackedSwitchDataPseudoInstruction() { - } - - protected Instruction makeClone() { - return new PackedSwitchDataPseudoInstruction(); - } - - - public static PackedSwitchDataPseudoInstruction make(DexFile dexFile, Input input) { - byte opcodeByte = input.readByte(); - if (opcodeByte != 0x00) { - throw new RuntimeException("Invalid opcode byte for a PackedSwitchData pseudo-instruction"); - } - byte subopcodeByte = input.readByte(); - if (subopcodeByte != 0x01) { - throw new RuntimeException("Invalid sub-opcode byte for a PackedSwitchData pseudo-instruction"); - } - - int targetCount = input.readShort(); - - int firstKey = input.readInt(); - int[] targets = new int[targetCount]; - - for (int i=0; i 0xFFFF) { - throw new RuntimeException("The sparse-switch data contains too many elements. " + - "The maximum number of switch elements is 65535"); - } - - encodedInstruction = new byte[targets.length * 8 + 4]; - encodedInstruction[0] = 0x00; - encodedInstruction[1] = 0x02; //sparse-switch psuedo-opcode - - encodedInstruction[2] = (byte)targets.length; - encodedInstruction[3] = (byte)(targets.length >> 8); - - int position = 8; - - if (targets.length > 0) { - int key = keys[0]; - encodedInstruction[4] = (byte)key; - encodedInstruction[5] = (byte)(key >> 8); - encodedInstruction[6] = (byte)(key >> 16); - encodedInstruction[7] = (byte)(key >> 24); - - for (int i=1; i> 8); - encodedInstruction[position++] = (byte)(key >> 16); - encodedInstruction[position++] = (byte)(key >> 24); - } - - for (int target: targets) { - encodedInstruction[position++] = (byte)target; - encodedInstruction[position++] = (byte)(target >> 8); - encodedInstruction[position++] = (byte)(target >> 16); - encodedInstruction[position++] = (byte)(target >> 24); - } - } - } - - protected void checkFormat(Format format) { - //no need to check the format - } - - private SparseSwitchDataPseudoInstruction() { - } - - protected Instruction makeClone() { - return new SparseSwitchDataPseudoInstruction(); - } - - public static SparseSwitchDataPseudoInstruction make(DexFile dexFile, Input input) { - byte opcodeByte = input.readByte(); - if (opcodeByte != 0x00) { - throw new RuntimeException("Invalid opcode byte for a SparseSwitchData pseudo-instruction"); - } - byte subopcodeByte = input.readByte(); - if (subopcodeByte != 0x02) { - throw new RuntimeException("Invalid sub-opcode byte for a SparseSwitchData pseudo-instruction"); - } - - int targetCount = input.readShort(); - - int[] keys = new int[targetCount]; - int[] targets = new int[targetCount]; - - for (int i=0; i 0xFFFF) { -// throw new RuntimeException("String index doesn't fit."); -// } -// out.writeShort(reference.getIndex()); -// out.write(bytes, 4, bytes.length - 4); -// } -// } -// } -// -// public void copyTo(DexFile dexFile, Instruction copy) { -// copy.bytes = bytes; -// copy.opcode = opcode; -// -// switch (opcode.referenceType) { -// case string: -// copy.reference = dexFile.StringIdsSection.intern(dexFile, (StringIdItem)reference); -// break; -// case type: -// copy.reference = dexFile.TypeIdsSection.intern(dexFile, (TypeIdItem)reference); -// break; -// case field: -// copy.reference = dexFile.FieldIdsSection.intern(dexFile, (FieldIdItem)reference); -// break; -// case method: -// copy.reference = dexFile.MethodIdsSection.intern(dexFile, (MethodIdItem)reference); -// break; -// case none: -// break; -// } -// } -// -// public int place(int offset) { -// return offset + getSize(offset); -// } -// -// public int getSize(int offset) { -// if (this.needsAlign() && (offset % 4) != 0) { -// return bytes.length + 2; -// } else { -// return bytes.length; -// } -// } -// -// private boolean needsAlign() { -// //true if the opcode is one of the "special format" opcodes -// return bytes[0] == 0 && bytes[1] > 0; -// } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/Code/InstructionField.java b/dexlib/src/main/java/org/jf/dexlib/Code/InstructionField.java deleted file mode 100644 index 42f6995c..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Code/InstructionField.java +++ /dev/null @@ -1,141 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.Code; - -import org.jf.dexlib.DexFile; -import org.jf.dexlib.Field; -import org.jf.dexlib.IndexedItem; -import org.jf.dexlib.Code.Format.ArrayDataPseudoInstruction; -import org.jf.dexlib.Code.Format.PackedSwitchDataPseudoInstruction; -import org.jf.dexlib.Code.Format.SparseSwitchDataPseudoInstruction; -import org.jf.dexlib.Util.AnnotatedOutput; -import org.jf.dexlib.Util.Input; - -public class InstructionField implements Field { - private Instruction instruction; - private DexFile dexFile; - - public InstructionField(DexFile dexFile) { - this.dexFile = dexFile; - } - - public InstructionField(DexFile dexFile, Instruction instruction) { - this.dexFile = dexFile; - this.instruction = instruction; - } - - public void writeTo(AnnotatedOutput out) { - byte[] bytes = instruction.encodedInstruction; - IndexedItem reference = instruction.getReferencedItem(); - - out.annotate(bytes.length, "instruction"); - if (needsAlign()) { - //the "special instructions" must be 4 byte aligned - out.alignTo(4); - out.write(bytes); - } else if (reference == null) { - out.write(bytes); - } else { - out.write(bytes,0,2); - //handle const-string/jumbo as a special case - if (bytes[0] == 0x1b) { - out.writeInt(reference.getIndex()); - } else { - int index = reference.getIndex(); - if (index > 0xFFFF) { - throw new RuntimeException("String index doesn't fit."); - } - out.writeShort(reference.getIndex()); - out.write(bytes, 4, bytes.length - 4); - } - } - } - - public void readFrom(Input in) { - int startPos = in.getCursor(); - - byte opByte = in.readByte(); - - if (opByte == 0x00) { - byte secondByte = in.readByte(); - - switch (secondByte) { - case 0x00: - //nop - instruction = Opcode.NOP.format.Factory.makeInstruction(dexFile, Opcode.NOP, new byte[]{0x00}); - return; - case 0x01: - //packed switch - in.setCursor(startPos); - instruction = PackedSwitchDataPseudoInstruction.make(dexFile, in); - return; - case 0x02: - //sparse switch - in.setCursor(startPos); - instruction = SparseSwitchDataPseudoInstruction.make(dexFile, in); - return; - case 0x03: - //array data - in.setCursor(startPos); - instruction = ArrayDataPseudoInstruction.make(dexFile, in); - return; - default: - throw new RuntimeException("Invalid 2nd byte for opcode 0x00"); - } - } - - Opcode opcode = Opcode.getOpcodeByValue(opByte); - instruction = opcode.format.Factory.makeInstruction(dexFile, opcode, in.readBytes(opcode.format.size - 1)); - } - - public int place(int offset) { - return offset + getSize(offset); - } - - public void copyTo(DexFile dexFile, InstructionField copy) { - copy.instruction = instruction.cloneTo(dexFile); - } - - public int getSize(int offset) { - if (this.needsAlign() && (offset % 4) != 0) { - return instruction.encodedInstruction.length + 2; - } else { - return instruction.encodedInstruction.length; - } - } - - private boolean needsAlign() { - //true if the opcode is one of the "special format" opcodes - return instruction.encodedInstruction[0] == 0 && instruction.encodedInstruction[1] > 0; - } - - public Instruction getInstruction() { - return instruction; - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/Code/Opcode.java b/dexlib/src/main/java/org/jf/dexlib/Code/Opcode.java deleted file mode 100644 index a6dc2efb..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Code/Opcode.java +++ /dev/null @@ -1,300 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.Code; - -import org.jf.dexlib.Code.Format.Format; - -import java.util.ArrayList; -import java.util.HashMap; - -public enum Opcode -{ - NOP((byte)0x00, "nop", ReferenceType.none, Format.Format10x), - MOVE((byte)0x01, "move", ReferenceType.none, Format.Format12x), - MOVE_FROM16((byte)0x02, "move/from16", ReferenceType.none, Format.Format22x), - MOVE_16((byte)0x03, "move/16", ReferenceType.none, Format.Format32x), - MOVE_WIDE((byte)0x04, "move-wide", ReferenceType.none, Format.Format12x), - MOVE_WIDE_FROM16((byte)0x05, "move-wide/from16", ReferenceType.none, Format.Format22x), - MOVE_WIDE_16((byte)0x06, "move-wide/16", ReferenceType.none, Format.Format32x), - MOVE_OBJECT((byte)0x07, "move-object", ReferenceType.none, Format.Format12x), - MOVE_OBJECT_FROM16((byte)0x08, "move-object/from16", ReferenceType.none, Format.Format22x), - MOVE_OBJECT_16((byte)0x09, "move-object/16", ReferenceType.none, Format.Format32x), - MOVE_RESULT((byte)0x0a, "move-result", ReferenceType.none, Format.Format11x), - MOVE_RESULT_WIDE((byte)0x0b, "move-result-wide", ReferenceType.none, Format.Format11x), - MOVE_RESULT_OBJECT((byte)0x0c, "move-result-object", ReferenceType.none, Format.Format11x), - MOVE_EXCEPTION((byte)0x0d, "move-exception", ReferenceType.none, Format.Format11x), - RETURN_VOID((byte)0x0e, "return-void", ReferenceType.none, Format.Format10x), - RETURN((byte)0x0f, "return", ReferenceType.none, Format.Format11x), - RETURN_WIDE((byte)0x10, "return-wide", ReferenceType.none, Format.Format11x), - RETURN_OBJECT((byte)0x11, "return-object", ReferenceType.none, Format.Format11x), - CONST_4((byte)0x12, "const/4", ReferenceType.none, Format.Format11n), - CONST_16((byte)0x13, "const/16", ReferenceType.none, Format.Format21s), - CONST((byte)0x14, "const", ReferenceType.none, Format.Format31i), - CONST_HIGH16((byte)0x15, "const/high16", ReferenceType.none, Format.Format21h), - CONST_WIDE_16((byte)0x16, "const-wide/16", ReferenceType.none, Format.Format21s), - CONST_WIDE_32((byte)0x17, "const-wide/32", ReferenceType.none, Format.Format31i), - CONST_WIDE((byte)0x18, "const-wide", ReferenceType.none, Format.Format51l), - CONST_WIDE_HIGH16((byte)0x19, "const-wide/high16", ReferenceType.none, Format.Format21h), - CONST_STRING((byte)0x1a, "const-string", ReferenceType.string, Format.Format21c), - CONST_STRING_JUMBO((byte)0x1b, "const-string/jumbo", ReferenceType.string, Format.Format31c), - CONST_CLASS((byte)0x1c, "const-class", ReferenceType.type, Format.Format21c), - MONITOR_ENTER((byte)0x1d, "monitor-enter", ReferenceType.none, Format.Format11x), - MONITOR_EXIT((byte)0x1e, "monitor-exit", ReferenceType.none, Format.Format11x), - CHECK_CAST((byte)0x1f, "check-cast", ReferenceType.type, Format.Format21c), - INSTANCE_OF((byte)0x20, "instance-of", ReferenceType.type, Format.Format22c), - ARRAY_LENGTH((byte)0x21, "array-length", ReferenceType.none, Format.Format12x), - NEW_INSTANCE((byte)0x22, "new-instance", ReferenceType.type, Format.Format21c), - NEW_ARRAY((byte)0x23, "new-array", ReferenceType.type, Format.Format22c), - FILLED_NEW_ARRAY((byte)0x24, "filled-new-array", ReferenceType.type, Format.Format35c), - FILLED_NEW_ARRAY_RANGE((byte)0x25, "filled-new-array/range", ReferenceType.type, Format.Format3rc), - FILL_ARRAY_DATA((byte)0x26, "fill-array-data", ReferenceType.none, Format.Format31t), - THROW((byte)0x27, "throw", ReferenceType.none, Format.Format11x), - GOTO((byte)0x28, "goto", ReferenceType.none, Format.Format10t), - GOTO_16((byte)0x29, "goto/16", ReferenceType.none, Format.Format20t), - GOTO_32((byte)0x2a, "goto/32", ReferenceType.none, Format.Format30t), - PACKED_SWITCH((byte)0x2b, "packed-switch", ReferenceType.none, Format.Format31t), - SPARSE_SWITCH((byte)0x2c, "sparse-switch", ReferenceType.none, Format.Format31t), - CMPL_FLOAT((byte)0x2d, "cmpl-float", ReferenceType.none, Format.Format23x), - CMPG_FLOAT((byte)0x2e, "cmpg-float", ReferenceType.none, Format.Format23x), - CMPL_DOUBLE((byte)0x2f, "cmpl-double", ReferenceType.none, Format.Format23x), - CMPG_DOUBLE((byte)0x30, "cmpg-double", ReferenceType.none, Format.Format23x), - CMP_LONG((byte)0x31, "cmp-long", ReferenceType.none, Format.Format23x), - IF_EQ((byte)0x32, "if-eq", ReferenceType.none, Format.Format22t), - IF_NE((byte)0x33, "if-ne", ReferenceType.none, Format.Format22t), - IF_LT((byte)0x34, "if-lt", ReferenceType.none, Format.Format22t), - IF_GE((byte)0x35, "if-ge", ReferenceType.none, Format.Format22t), - IF_GT((byte)0x36, "if-gt", ReferenceType.none, Format.Format22t), - IF_LE((byte)0x37, "if-le", ReferenceType.none, Format.Format22t), - IF_EQZ((byte)0x38, "if-eqz", ReferenceType.none, Format.Format21t), - IF_NEZ((byte)0x39, "if-nez", ReferenceType.none, Format.Format21t), - IF_LTZ((byte)0x3a, "if-ltz", ReferenceType.none, Format.Format21t), - IF_GEZ((byte)0x3b, "if-gez", ReferenceType.none, Format.Format21t), - IF_GTZ((byte)0x3c, "if-gtz", ReferenceType.none, Format.Format21t), - IF_LEZ((byte)0x3d, "if-lez", ReferenceType.none, Format.Format21t), - AGET((byte)0x44, "aget", ReferenceType.none, Format.Format23x), - AGET_WIDE((byte)0x45, "aget-wide", ReferenceType.none, Format.Format23x), - AGET_OBJECT((byte)0x46, "aget-object", ReferenceType.none, Format.Format23x), - AGET_BOOLEAN((byte)0x47, "aget-boolean", ReferenceType.none, Format.Format23x), - AGET_BYTE((byte)0x48, "aget-byte", ReferenceType.none, Format.Format23x), - AGET_CHAR((byte)0x49, "aget-char", ReferenceType.none, Format.Format23x), - AGET_SHORT((byte)0x4a, "aget-short", ReferenceType.none, Format.Format23x), - APUT((byte)0x4b, "aput", ReferenceType.none, Format.Format23x), - APUT_WIDE((byte)0x4c, "aput-wide", ReferenceType.none, Format.Format23x), - APUT_OBJECT((byte)0x4d, "aput-object", ReferenceType.none, Format.Format23x), - APUT_BOOLEAN((byte)0x4e, "aput-boolean", ReferenceType.none, Format.Format23x), - APUT_BYTE((byte)0x4f, "aput-byte", ReferenceType.none, Format.Format23x), - APUT_CHAR((byte)0x50, "aput-char", ReferenceType.none, Format.Format23x), - APUT_SHORT((byte)0x51, "aput-short", ReferenceType.none, Format.Format23x), - IGET((byte)0x52, "iget", ReferenceType.field, Format.Format22c), - IGET_WIDE((byte)0x53, "iget-wide", ReferenceType.field, Format.Format22c), - IGET_OBJECT((byte)0x54, "iget-object", ReferenceType.field, Format.Format22c), - IGET_BOOLEAN((byte)0x55, "iget-boolean", ReferenceType.field, Format.Format22c), - IGET_BYTE((byte)0x56, "iget-byte", ReferenceType.field, Format.Format22c), - IGET_CHAR((byte)0x57, "iget-char", ReferenceType.field, Format.Format22c), - IGET_SHORT((byte)0x58, "iget-short", ReferenceType.field, Format.Format22c), - IPUT((byte)0x59, "iput", ReferenceType.field, Format.Format22c), - IPUT_WIDE((byte)0x5a, "iput-wide", ReferenceType.field, Format.Format22c), - IPUT_OBJECT((byte)0x5b, "iput-object", ReferenceType.field, Format.Format22c), - IPUT_BOOLEAN((byte)0x5c, "iput-boolean", ReferenceType.field, Format.Format22c), - IPUT_BYTE((byte)0x5d, "iput-byte", ReferenceType.field, Format.Format22c), - IPUT_CHAR((byte)0x5e, "iput-char", ReferenceType.field, Format.Format22c), - IPUT_SHORT((byte)0x5f, "iput-short", ReferenceType.field, Format.Format22c), - SGET((byte)0x60, "sget", ReferenceType.field, Format.Format21c), - SGET_WIDE((byte)0x61, "sget-wide", ReferenceType.field, Format.Format21c), - SGET_OBJECT((byte)0x62, "sget-object", ReferenceType.field, Format.Format21c), - SGET_BOOLEAN((byte)0x63, "sget-boolean", ReferenceType.field, Format.Format21c), - SGET_BYTE((byte)0x64, "sget-byte", ReferenceType.field, Format.Format21c), - SGET_CHAR((byte)0x65, "sget-char", ReferenceType.field, Format.Format21c), - SGET_SHORT((byte)0x66, "sget-short", ReferenceType.field, Format.Format21c), - SPUT((byte)0x67, "sput", ReferenceType.field, Format.Format21c), - SPUT_WIDE((byte)0x68, "sput-wide", ReferenceType.field, Format.Format21c), - SPUT_OBJECT((byte)0x69, "sput-object", ReferenceType.field, Format.Format21c), - SPUT_BOOLEAN((byte)0x6a, "sput-boolean", ReferenceType.field, Format.Format21c), - SPUT_BYTE((byte)0x6b, "sput-byte", ReferenceType.field, Format.Format21c), - SPUT_CHAR((byte)0x6c, "sput-char", ReferenceType.field, Format.Format21c), - SPUT_SHORT((byte)0x6d, "sput-short", ReferenceType.field, Format.Format21c), - INVOKE_VIRTUAL((byte)0x6e, "invoke-virtual", ReferenceType.method, Format.Format35c), - INVOKE_SUPER((byte)0x6f, "invoke-super", ReferenceType.method, Format.Format35c), - INVOKE_DIRECT((byte)0x70, "invoke-direct", ReferenceType.method, Format.Format35c), - INVOKE_STATIC((byte)0x71, "invoke-static", ReferenceType.method, Format.Format35c), - INVOKE_INTERFACE((byte)0x72, "invoke-interface", ReferenceType.method, Format.Format35c), - INVOKE_VIRTUAL_RANGE((byte)0x74, "invoke-virtual/range", ReferenceType.method, Format.Format3rc), - INVOKE_SUPER_RANGE((byte)0x75, "invoke-super/range", ReferenceType.method, Format.Format3rc), - INVOKE_DIRECT_RANGE((byte)0x76, "invoke-direct/range", ReferenceType.method, Format.Format3rc), - INVOKE_STATIC_RANGE((byte)0x77, "invoke-static/range", ReferenceType.method, Format.Format3rc), - INVOKE_INTERFACE_RANGE((byte)0x78, "invoke-interface/range", ReferenceType.method, Format.Format3rc), - NEG_INT((byte)0x7b, "neg-int", ReferenceType.none, Format.Format12x), - NOT_INT((byte)0x7c, "not-int", ReferenceType.none, Format.Format12x), - NEG_LONG((byte)0x7d, "neg-long", ReferenceType.none, Format.Format12x), - NOT_LONG((byte)0x7e, "not-long", ReferenceType.none, Format.Format12x), - NEG_FLOAT((byte)0x7f, "neg-float", ReferenceType.none, Format.Format12x), - NEG_DOUBLE((byte)0x80, "neg-double", ReferenceType.none, Format.Format12x), - INT_TO_LONG((byte)0x81, "int-to-long", ReferenceType.none, Format.Format12x), - INT_TO_FLOAT((byte)0x82, "int-to-float", ReferenceType.none, Format.Format12x), - INT_TO_DOUBLE((byte)0x83, "int-to-double", ReferenceType.none, Format.Format12x), - LONG_TO_INT((byte)0x84, "long-to-int", ReferenceType.none, Format.Format12x), - LONG_TO_FLOAT((byte)0x85, "long-to-float", ReferenceType.none, Format.Format12x), - LONG_TO_DOUBLE((byte)0x86, "long-to-double", ReferenceType.none, Format.Format12x), - FLOAT_TO_INT((byte)0x87, "float-to-int", ReferenceType.none, Format.Format12x), - FLOAT_TO_LONG((byte)0x88, "float-to-long", ReferenceType.none, Format.Format12x), - FLOAT_TO_DOUBLE((byte)0x89, "float-to-double", ReferenceType.none, Format.Format12x), - DOUBLE_TO_INT((byte)0x8a, "double-to-int", ReferenceType.none, Format.Format12x), - DOUBLE_TO_LONG((byte)0x8b, "double-to-long", ReferenceType.none, Format.Format12x), - DOUBLE_TO_FLOAT((byte)0x8c, "double-to-float", ReferenceType.none, Format.Format12x), - INT_TO_BYTE((byte)0x8d, "int-to-byte", ReferenceType.none, Format.Format12x), - INT_TO_CHAR((byte)0x8e, "int-to-char", ReferenceType.none, Format.Format12x), - INT_TO_SHORT((byte)0x8f, "int-to-short", ReferenceType.none, Format.Format12x), - ADD_INT((byte)0x90, "add-int", ReferenceType.none, Format.Format23x), - SUB_INT((byte)0x91, "sub-int", ReferenceType.none, Format.Format23x), - MUL_INT((byte)0x92, "mul-int", ReferenceType.none, Format.Format23x), - DIV_INT((byte)0x93, "div-int", ReferenceType.none, Format.Format23x), - REM_INT((byte)0x94, "rem-int", ReferenceType.none, Format.Format23x), - AND_INT((byte)0x95, "and-int", ReferenceType.none, Format.Format23x), - OR_INT((byte)0x96, "or-int", ReferenceType.none, Format.Format23x), - XOR_INT((byte)0x97, "xor-int", ReferenceType.none, Format.Format23x), - SHL_INT((byte)0x98, "shl-int", ReferenceType.none, Format.Format23x), - SHR_INT((byte)0x99, "shr-int", ReferenceType.none, Format.Format23x), - USHR_INT((byte)0x9a, "ushr-int", ReferenceType.none, Format.Format23x), - ADD_LONG((byte)0x9b, "add-long", ReferenceType.none, Format.Format23x), - SUB_LONG((byte)0x9c, "sub-long", ReferenceType.none, Format.Format23x), - MUL_LONG((byte)0x9d, "mul-long", ReferenceType.none, Format.Format23x), - DIV_LONG((byte)0x9e, "div-long", ReferenceType.none, Format.Format23x), - REM_LONG((byte)0x9f, "rem-long", ReferenceType.none, Format.Format23x), - AND_LONG((byte)0xa0, "and-long", ReferenceType.none, Format.Format23x), - OR_LONG((byte)0xa1, "or-long", ReferenceType.none, Format.Format23x), - XOR_LONG((byte)0xa2, "xor-long", ReferenceType.none, Format.Format23x), - SHL_LONG((byte)0xa3, "shl-long", ReferenceType.none, Format.Format23x), - SHR_LONG((byte)0xa4, "shr-long", ReferenceType.none, Format.Format23x), - USHR_LONG((byte)0xa5, "ushr-long", ReferenceType.none, Format.Format23x), - ADD_FLOAT((byte)0xa6, "add-float", ReferenceType.none, Format.Format23x), - SUB_FLOAT((byte)0xa7, "sub-float", ReferenceType.none, Format.Format23x), - MUL_FLOAT((byte)0xa8, "mul-float", ReferenceType.none, Format.Format23x), - DIV_FLOAT((byte)0xa9, "div-float", ReferenceType.none, Format.Format23x), - REM_FLOAT((byte)0xaa, "rem-float", ReferenceType.none, Format.Format23x), - ADD_DOUBLE((byte)0xab, "add-double", ReferenceType.none, Format.Format23x), - SUB_DOUBLE((byte)0xac, "sub-double", ReferenceType.none, Format.Format23x), - MUL_DOUBLE((byte)0xad, "mul-double", ReferenceType.none, Format.Format23x), - DIV_DOUBLE((byte)0xae, "div-double", ReferenceType.none, Format.Format23x), - REM_DOUBLE((byte)0xaf, "rem-double", ReferenceType.none, Format.Format23x), - ADD_INT_2ADDR((byte)0xb0, "add-int/2addr", ReferenceType.none, Format.Format12x), - SUB_INT_2ADDR((byte)0xb1, "sub-int/2addr", ReferenceType.none, Format.Format12x), - MUL_INT_2ADDR((byte)0xb2, "mul-int/2addr", ReferenceType.none, Format.Format12x), - DIV_INT_2ADDR((byte)0xb3, "div-int/2addr", ReferenceType.none, Format.Format12x), - REM_INT_2ADDR((byte)0xb4, "rem-int/2addr", ReferenceType.none, Format.Format12x), - AND_INT_2ADDR((byte)0xb5, "and-int/2addr", ReferenceType.none, Format.Format12x), - OR_INT_2ADDR((byte)0xb6, "or-int/2addr", ReferenceType.none, Format.Format12x), - XOR_INT_2ADDR((byte)0xb7, "xor-int/2addr", ReferenceType.none, Format.Format12x), - SHL_INT_2ADDR((byte)0xb8, "shl-int/2addr", ReferenceType.none, Format.Format12x), - SHR_INT_2ADDR((byte)0xb9, "shr-int/2addr", ReferenceType.none, Format.Format12x), - USHR_INT_2ADDR((byte)0xba, "ushr-int/2addr", ReferenceType.none, Format.Format12x), - ADD_LONG_2ADDR((byte)0xbb, "add-long/2addr", ReferenceType.none, Format.Format12x), - SUB_LONG_2ADDR((byte)0xbc, "sub-long/2addr", ReferenceType.none, Format.Format12x), - MUL_LONG_2ADDR((byte)0xbd, "mul-long/2addr", ReferenceType.none, Format.Format12x), - DIV_LONG_2ADDR((byte)0xbe, "div-long/2addr", ReferenceType.none, Format.Format12x), - REM_LONG_2ADDR((byte)0xbf, "rem-long/2addr", ReferenceType.none, Format.Format12x), - AND_LONG_2ADDR((byte)0xc0, "and-long/2addr", ReferenceType.none, Format.Format12x), - OR_LONG_2ADDR((byte)0xc1, "or-long/2addr", ReferenceType.none, Format.Format12x), - XOR_LONG_2ADDR((byte)0xc2, "xor-long/2addr", ReferenceType.none, Format.Format12x), - SHL_LONG_2ADDR((byte)0xc3, "shl-long/2addr", ReferenceType.none, Format.Format12x), - SHR_LONG_2ADDR((byte)0xc4, "shr-long/2addr", ReferenceType.none, Format.Format12x), - USHR_LONG_2ADDR((byte)0xc5, "ushr-long/2addr", ReferenceType.none, Format.Format12x), - ADD_FLOAT_2ADDR((byte)0xc6, "add-float/2addr", ReferenceType.none, Format.Format12x), - SUB_FLOAT_2ADDR((byte)0xc7, "sub-float/2addr", ReferenceType.none, Format.Format12x), - MUL_FLOAT_2ADDR((byte)0xc8, "mul-float/2addr", ReferenceType.none, Format.Format12x), - DIV_FLOAT_2ADDR((byte)0xc9, "div-float/2addr", ReferenceType.none, Format.Format12x), - REM_FLOAT_2ADDR((byte)0xca, "rem-float/2addr", ReferenceType.none, Format.Format12x), - ADD_DOUBLE_2ADDR((byte)0xcb, "add-double/2addr", ReferenceType.none, Format.Format12x), - SUB_DOUBLE_2ADDR((byte)0xcc, "sub-double/2addr", ReferenceType.none, Format.Format12x), - MUL_DOUBLE_2ADDR((byte)0xcd, "mul-double/2addr", ReferenceType.none, Format.Format12x), - DIV_DOUBLE_2ADDR((byte)0xce, "div-double/2addr", ReferenceType.none, Format.Format12x), - REM_DOUBLE_2ADDR((byte)0xcf, "rem-double/2addr", ReferenceType.none, Format.Format12x), - ADD_INT_LIT16((byte)0xd0, "add-int/lit16", ReferenceType.none, Format.Format22s), - RSUB_INT((byte)0xd1, "rsub-int", ReferenceType.none, Format.Format22s), - MUL_INT_LIT16((byte)0xd2, "mul-int/lit16", ReferenceType.none, Format.Format22s), - DIV_INT_LIT16((byte)0xd3, "div-int/lit16", ReferenceType.none, Format.Format22s), - REM_INT_LIT16((byte)0xd4, "rem-int/lit16", ReferenceType.none, Format.Format22s), - AND_INT_LIT16((byte)0xd5, "and-int/lit16", ReferenceType.none, Format.Format22s), - OR_INT_LIT16((byte)0xd6, "or-int/lit16", ReferenceType.none, Format.Format22s), - XOR_INT_LIT16((byte)0xd7, "xor-int/lit16", ReferenceType.none, Format.Format22s), - ADD_INT_LIT8((byte)0xd8, "add-int/lit8", ReferenceType.none, Format.Format22b), - RSUB_INT_LIT8((byte)0xd9, "rsub-int/lit8", ReferenceType.none, Format.Format22b), - MUL_INT_LIT8((byte)0xda, "mul-int/lit8", ReferenceType.none, Format.Format22b), - DIV_INT_LIT8((byte)0xdb, "div-int/lit8", ReferenceType.none, Format.Format22b), - REM_INT_LIT8((byte)0xdc, "rem-int/lit8", ReferenceType.none, Format.Format22b), - AND_INT_LIT8((byte)0xdd, "and-int/lit8", ReferenceType.none, Format.Format22b), - OR_INT_LIT8((byte)0xde, "or-int/lit8", ReferenceType.none, Format.Format22b), - XOR_INT_LIT8((byte)0xdf, "xor-int/lit8", ReferenceType.none, Format.Format22b), - SHL_INT_LIT8((byte)0xe0, "shl-int/lit8", ReferenceType.none, Format.Format22b), - SHR_INT_LIT8((byte)0xe1, "shr-int/lit8", ReferenceType.none, Format.Format22b), - USHR_INT_LIT8((byte)0xe2, "ushr-int/lit8", ReferenceType.none, Format.Format22b); - - - - private static ArrayList opcodesByValue; - private static HashMap opcodesByName; - - static { - try - { - opcodesByValue = new ArrayList(); - opcodesByName = new HashMap(); - - for (int i=0; i<0x100; i++) { - opcodesByValue.add(null); - } - - for (Opcode opcode: Opcode.values()) { - opcodesByValue.set((opcode.value & 0xFF), opcode); - opcodesByName.put(opcode.name.hashCode(), opcode); - } - }catch (Exception ex) { - System.out.println(ex.toString()); - } - } - - public static Opcode getOpcodeByName(String opcodeName) { - return opcodesByName.get(opcodeName.toLowerCase().hashCode()); - } - - public static Opcode getOpcodeByValue(byte opcodeValue) { - return opcodesByValue.get(opcodeValue & 0xFF); - } - - public final byte value; - public final String name; - public final ReferenceType referenceType; - public final Format format; - - Opcode(byte opcodeValue, String opcodeName, ReferenceType referenceType, Format format) { - this.value = opcodeValue; - this.name = opcodeName; - this.referenceType = referenceType; - this.format = format; - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/Code/ReferenceType.java b/dexlib/src/main/java/org/jf/dexlib/Code/ReferenceType.java deleted file mode 100644 index fa714743..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Code/ReferenceType.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.Code; - -import org.jf.dexlib.*; - -public enum ReferenceType -{ - string, - type, - field, - method, - none; - - public boolean checkItem(IndexedItem item) { - switch (this) { - case string: - return item instanceof StringIdItem; - case type: - return item instanceof TypeIdItem; - case field: - return item instanceof FieldIdItem; - case method: - return item instanceof MethodIdItem; - case none: - return item == null; - } - return false; - } -} \ No newline at end of file diff --git a/dexlib/src/main/java/org/jf/dexlib/CodeItem.java b/dexlib/src/main/java/org/jf/dexlib/CodeItem.java deleted file mode 100644 index d51f3cca..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/CodeItem.java +++ /dev/null @@ -1,634 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib; - -import org.jf.dexlib.Code.InstructionField; -import org.jf.dexlib.Code.Opcode; -import org.jf.dexlib.Util.AnnotatedOutput; -import org.jf.dexlib.Util.Input; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Collections; - -public class CodeItem extends OffsettedItem { - private final ArrayList instructionList; - private final ArrayList tryItems = new ArrayList(); - private final ArrayList catchHandlerList = new ArrayList(); - - private final ShortIntegerField registersCountField; - private final ShortIntegerField inArgumentCountField; - private final ShortIntegerField outArgumentCountField; - private final ListSizeField triesCountField; - private final OffsettedItemReference debugInfoReferenceField; - private final IntegerField instructionsSizeField; - private final InstructionListField instructionListField; - private final PaddingField paddingField; - private final FieldListField triesListField; - private final EncodedCatchHandlerList catchHandlersListField; - - private MethodIdItem parent = null; - - public CodeItem(final DexFile dexFile, int offset) { - super(dexFile, offset); - - instructionList = new ArrayList(); - - fields = new Field[] { - registersCountField = new ShortIntegerField("registers_size"), - inArgumentCountField = new ShortIntegerField("ins_size"), - outArgumentCountField = new ShortIntegerField("outs_size"), - triesCountField = new ListSizeField(tryItems, new ShortIntegerField("tries_size")), - debugInfoReferenceField = new OffsettedItemReference(dexFile.DebugInfoItemsSection, - new IntegerField(null), "debug_off"), - instructionsSizeField = new IntegerField("insns_size"), - instructionListField = new InstructionListField(dexFile), - paddingField = new PaddingField(), - triesListField = new FieldListField(tryItems, "try_item") { - protected TryItem make() { - return new TryItem(catchHandlersListField); - } - }, - - catchHandlersListField = new EncodedCatchHandlerList(dexFile) - }; - } - - public CodeItem(final DexFile dexFile, - int registersCount, - int inArguments, - List instructions, - DebugInfoItem debugInfo, - List tries, - List handlers) { - this(dexFile, 0); - - instructionList.addAll(instructions); - instructionsSizeField.cacheValue(instructionListField.getInstructionWordCount()); - - if (tries != null) { - tryItems.addAll(tries); - if (handlers == null) { - throw new RuntimeException("The handlers parameter cannot be null if tries parameter is not null"); - } - catchHandlerList.addAll(handlers); - } else if (handlers != null) { - throw new RuntimeException("The handlers parameter must be null if the tries parameter is null"); - } - - registersCountField.cacheValue(registersCount); - inArgumentCountField.cacheValue(inArguments); - outArgumentCountField.cacheValue(instructionListField.getOutArguments()); - debugInfoReferenceField.setReference(debugInfo); - - if (debugInfo != null) { - debugInfo.setParent(this); - } - } - - protected int getAlignment() { - return 4; - } - - public ItemType getItemType() { - return ItemType.TYPE_CODE_ITEM; - } - - public int getRegisterCount() { - return registersCountField.getCachedValue(); - } - - public List getInstructions() { - return Collections.unmodifiableList(instructionList); - } - - public List getTries() { - return Collections.unmodifiableList(tryItems); - } - - public DebugInfoItem getDebugInfo() { - return debugInfoReferenceField.getReference(); - } - - protected void setParent(MethodIdItem methodIdItem) { - this.parent = methodIdItem; - } - - public void copyTo(DexFile dexFile, CodeItem copy) - { - for (int i = 0; i < fields.length-2; i++) { - fields[i].copyTo(dexFile, copy.fields[i]); - } - //we need to do this in reverse order, so when the tries are copied, - //the catchHandler copies will already exist - catchHandlersListField.copyTo(dexFile, copy.catchHandlersListField); - triesListField.copyTo(dexFile, copy.triesListField); - - DebugInfoItem copyDebugInfo = copy.getDebugInfo(); - if (copyDebugInfo != null) { - copyDebugInfo.setParent(copy); - } - } - - public void readFrom(Input in, int index) { - super.readFrom(in, index); - - DebugInfoItem debugInfoItem = debugInfoReferenceField.getReference(); - if (debugInfoItem != null) { - debugInfoItem.setParent(this); - } - } - - public String getConciseIdentity() { - //TODO: should mention the method name here - return "code_item @0x" + Integer.toHexString(getOffset()); - } - - public int compareTo(CodeItem other) { - if (parent == null) { - if (other.parent == null) { - return 0; - } - return -1; - } - if (other.parent == null) { - return 1; - } - return parent.compareTo(other.parent); - } - - public static class TryItem extends CompositeField { - private final IntegerField startAddr; - private final ShortIntegerField insnCount; - private final EncodedCatchHandlerReference encodedCatchHandlerReference; - - public TryItem(EncodedCatchHandlerList encodedCatchHandlerList) { - super("try_item"); - fields = new Field[] { - startAddr = new IntegerField("start_addr"), - insnCount = new ShortIntegerField("insn_count"), - encodedCatchHandlerReference = new EncodedCatchHandlerReference(encodedCatchHandlerList) - }; - } - - public TryItem(int startAddr, int insnCount, EncodedCatchHandler encodedCatchHandler) { - super("try_item"); - fields = new Field[] { - this.startAddr = new IntegerField(startAddr, "start_addr"), - this.insnCount = new ShortIntegerField(insnCount, "insn_count"), - this.encodedCatchHandlerReference = new EncodedCatchHandlerReference(encodedCatchHandler) - }; - } - - public int getStartAddress() { - return startAddr.getCachedValue(); - } - - public int getEndAddress() { - return startAddr.getCachedValue() + insnCount.getCachedValue(); - } - - public EncodedCatchHandler getHandler() { - return encodedCatchHandlerReference.getReference(); - } - } - - public static class EncodedCatchHandlerReference extends ShortIntegerField { - private final EncodedCatchHandlerList encodedCatchHandlerList; - private EncodedCatchHandler encodedCatchHandler; - - public EncodedCatchHandlerReference(EncodedCatchHandlerList encodedCatchHandlerList) { - super("encoded_catch_handler"); - this.encodedCatchHandlerList = encodedCatchHandlerList; - } - - public EncodedCatchHandlerReference(EncodedCatchHandler encodedCatchHandler) { - super("encoded_catch_handler"); - this.encodedCatchHandlerList = null; - this.encodedCatchHandler = encodedCatchHandler; - } - - public EncodedCatchHandlerList getEncodedCatchHandlerList() { - return encodedCatchHandlerList; - } - - private void setReference(EncodedCatchHandler encodedCatchHandler) { - this.encodedCatchHandler = encodedCatchHandler; - } - - public EncodedCatchHandler getReference() { - return encodedCatchHandler; - } - - public void copyTo(DexFile dexFile, CachedIntegerValueField _copy) { - EncodedCatchHandlerReference copy = (EncodedCatchHandlerReference)_copy; - EncodedCatchHandler copiedItem = copy.getEncodedCatchHandlerList().intern(encodedCatchHandler); - copy.setReference(copiedItem); - } - - public void writeTo(AnnotatedOutput out) { - cacheValue(encodedCatchHandler.getOffsetInList()); - - super.writeTo(out); - } - - public void readFrom(Input in) { - super.readFrom(in); - - encodedCatchHandler = encodedCatchHandlerList.getByOffset(getCachedValue()); - } - - public int place(int offset) { - cacheValue(encodedCatchHandler.getOffsetInList()); - return super.place(offset); - } - } - - public class EncodedCatchHandlerList extends CompositeField { - private boolean fieldPresent = false; - //this field is only valid when reading a dex file in - protected HashMap itemsByOffset = - new HashMap(); - - protected HashMap uniqueItems = null; - - private final DexFile dexFile; - - public EncodedCatchHandler getByOffset(int offset) { - EncodedCatchHandler encodedCatchHandler = itemsByOffset.get(offset); - if (encodedCatchHandler == null) { - encodedCatchHandler = new EncodedCatchHandler(dexFile, offset); - itemsByOffset.put(offset, encodedCatchHandler); - } - return encodedCatchHandler; - } - - public EncodedCatchHandler intern(EncodedCatchHandler item) { - if (uniqueItems == null) { - buildInternedItemMap(); - } - EncodedCatchHandler encodedCatchHandler = uniqueItems.get(item); - if (encodedCatchHandler == null) { - encodedCatchHandler = new EncodedCatchHandler(dexFile, -1); - catchHandlerList.add(encodedCatchHandler); - item.copyTo(dexFile, encodedCatchHandler); - uniqueItems.put(encodedCatchHandler, encodedCatchHandler); - } - return encodedCatchHandler; - } - - private void buildInternedItemMap() { - uniqueItems = new HashMap(); - for (EncodedCatchHandler item: catchHandlerList) { - uniqueItems.put(item, item); - } - } - - public EncodedCatchHandlerList(final DexFile dexFile) { - super("encoded_catch_handler_list"); - this.dexFile = dexFile; - - fields = new Field[] { - sizeField = new ListSizeField(catchHandlerList, new Leb128Field("size")), - listField = new FieldListField(catchHandlerList, "encoded_catch_handler") { - protected EncodedCatchHandler make() { - return new EncodedCatchHandler(dexFile, 0); - } - - public void readFrom(Input in) { - int currentOffset = sizeField.place(0); - - for (int i = 0; i < list.size(); i++) { - EncodedCatchHandler field = list.get(i); - - if (field == null) { - field = itemsByOffset.get(currentOffset); - if (field == null) { - field = new EncodedCatchHandler(dexFile, currentOffset); - } - list.set(i, field); - } - int savedOffset = in.getCursor(); - field.readFrom(in); - currentOffset += in.getCursor() - savedOffset; - } - } - } - }; - } - - private final ListSizeField sizeField; - private final FieldListField listField; - - public void readFrom(Input in) { - if (tryItems.size() > 0) { - fieldPresent = true; - super.readFrom(in); - } - } - - public void writeTo(AnnotatedOutput out) { - if (fieldPresent) { - super.writeTo(out); - } - } - - public int place(int offset) { - for (EncodedCatchHandler encodedCatchHandler: listField.list) { - encodedCatchHandler.setBaseOffset(offset); - } - if (tryItems.size() > 0) { - fieldPresent = true; - return super.place(offset); - } else { - return offset; - } - } - - public void copyTo(DexFile dexFile, EncodedCatchHandlerList copy) { - super.copyTo(dexFile, copy); - copy.fieldPresent = fieldPresent; - copy.itemsByOffset.clear(); - int offset = 0; - for (EncodedCatchHandler encodedCatchHandler: copy.listField.list) { - copy.itemsByOffset.put(encodedCatchHandler.offset, encodedCatchHandler); - } - } - } - - public static class EncodedCatchHandler extends CompositeField { - private ArrayList list; - boolean hasCatchAll = false; - private int baseOffset = 0; - - private final ListSizeField size; - private final FieldListField handlers; - private final Leb128Field catchAllAddress; - - private int offset; - - public EncodedCatchHandler(final DexFile dexFile, int offset) { - super("encoded_catch_handler"); - this.offset = offset; - - list = new ArrayList(); - fields = new Field[] { - size = new ListSizeField(list, new SignedLeb128Field("size") { - public void readFrom(Input in) { - super.readFrom(in); - hasCatchAll = (getCachedValue() <= 0); - } - - public void cacheValue(int value) { - super.cacheValue(value * (hasCatchAll?-1:1)); - }}) - , - handlers = new FieldListField(list, "encoded_type_addr_pair") { - protected EncodedTypeAddrPair make() { - return new EncodedTypeAddrPair(dexFile); - } - }, - catchAllAddress = new Leb128Field("catch_all_addr") { - public void readFrom(Input in) { - if (hasCatchAll) { - super.readFrom(in); - } - } - - public void writeTo(AnnotatedOutput out) { - if (hasCatchAll) { - super.writeTo(out); - } - } - - public int place(int offset) { - if (hasCatchAll) { - return super.place(offset); - } - return offset; - } - } - }; - } - - public EncodedCatchHandler(final DexFile dexFile, List handlers, int catchAllHandler) { - this(dexFile, 0); - - list.addAll(handlers); - if (catchAllHandler >= 0) { - hasCatchAll = true; - catchAllAddress.cacheValue(catchAllHandler); - } - } - - public int getOffsetInList() { - return offset-baseOffset; - } - - public void setBaseOffset(int baseOffset) { - this.baseOffset = baseOffset; - } - - public void copyTo(DexFile dexFile, EncodedCatchHandler copy) { - super.copyTo(dexFile, copy); - copy.hasCatchAll = hasCatchAll; - copy.offset = offset; - } - - public int place(int offset) { - this.offset = offset; - return super.place(offset); - } - - public int getCatchAllAddress() { - if (hasCatchAll) { - return catchAllAddress.getCachedValue(); - } else { - return -1; - } - } - - public List getHandlers() { - return Collections.unmodifiableList(list); - } - } - - public static class EncodedTypeAddrPair extends CompositeField { - public final IndexedItemReference typeReferenceField; - public final Leb128Field handlerAddressField; - - public EncodedTypeAddrPair(DexFile dexFile) { - super("encoded_type_addr_pair"); - fields = new Field[] { - typeReferenceField = new IndexedItemReference(dexFile.TypeIdsSection, - new Leb128Field(null), "type_idx"), - handlerAddressField = new Leb128Field("addr") - }; - } - - public EncodedTypeAddrPair(DexFile dexFile, TypeIdItem type, int handlerOffset) { - this(dexFile); - typeReferenceField.setReference(type); - handlerAddressField.cacheValue(handlerOffset); - } - - public TypeIdItem getTypeReferenceField() { - return typeReferenceField.getReference(); - } - - public int getHandlerAddress() { - return handlerAddressField.getCachedValue(); - } - } - - private class InstructionListField implements Field { - private final DexFile dexFile; - - public InstructionListField(DexFile dexFile) { - this.dexFile = dexFile; - } - - public void writeTo(AnnotatedOutput out) { - int startPosition = out.getCursor(); - for (InstructionField instruction: instructionList) { - instruction.writeTo(out); - } - if ((out.getCursor() - startPosition) != (instructionsSizeField.getCachedValue() * 2)) { - throw new RuntimeException("Did not write the expected amount of bytes"); - } - } - - public void readFrom(Input in) { - int numBytes = instructionsSizeField.getCachedValue() * 2; - int startPosition = in.getCursor(); - - do { - InstructionField instruction = new InstructionField(dexFile); - instruction.readFrom(in); - instructionList.add(instruction); - } while (in.getCursor() - startPosition < numBytes); - - if (in.getCursor() - startPosition != numBytes) { - throw new RuntimeException("Read past the end of the code section"); - } - } - - public int place(int offset) { - return offset + (instructionsSizeField.getCachedValue() * 2); - } - - public void copyTo(DexFile dexFile, InstructionListField copy) { - ArrayList copyInstructionList = copy.getInstructionList(); - copyInstructionList.clear(); - for (InstructionField instruction: instructionList) { - InstructionField instructionCopy = new InstructionField(dexFile); - instruction.copyTo(dexFile, instructionCopy); - copyInstructionList.add(instructionCopy); - } - } - - private ArrayList getInstructionList() { - return instructionList; - } - - //return the word size of the instruction list - public int getInstructionWordCount() { - int bytes = 0; - for (InstructionField instruction: instructionList) { - bytes += instruction.getSize(bytes); - } - return bytes/2; - } - - //return the highest parameter word count of any method invokation - public int getOutArguments() { - int maxParamWordCount = 0; - for (InstructionField instruction: instructionList) { - IndexedItem item = instruction.getInstruction().getReferencedItem(); - if (item instanceof MethodIdItem) { - MethodIdItem methodIdItem = (MethodIdItem)item; - Opcode opcode = instruction.getInstruction().getOpcode(); - - boolean isStatic = false; - if (opcode == Opcode.INVOKE_STATIC || opcode == Opcode.INVOKE_STATIC_RANGE) { - isStatic = true; - } - int paramWordCount = methodIdItem.getParameterRegisterCount(isStatic); - - if (maxParamWordCount < paramWordCount) { - maxParamWordCount = paramWordCount; - } - } - } - return maxParamWordCount; - } - } - - private class PaddingField implements Field { - - public PaddingField() { - } - - private boolean needsAlign() { - return (triesCountField.getCachedValue() > 0) && (instructionsSizeField.getCachedValue() % 2 == 1); - } - - public void writeTo(AnnotatedOutput out) { - if (needsAlign()) { - out.writeShort(0); - } - } - - public void readFrom(Input in) { - if (needsAlign()) { - in.skipBytes(2); - } - } - - public int place(int offset) { - if (needsAlign()) { - return offset + 2; - } else { - return offset; - } - } - - public int hashCode() { - return 0; - } - - public boolean equals(Object o) { - return getClass() == o.getClass(); - } - - public void copyTo(DexFile dexFile, Field field) { - } - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/CompositeField.java b/dexlib/src/main/java/org/jf/dexlib/CompositeField.java deleted file mode 100644 index bc79b776..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/CompositeField.java +++ /dev/null @@ -1,105 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib; - -import org.jf.dexlib.Util.AnnotatedOutput; -import org.jf.dexlib.Util.Input; - - -public abstract class CompositeField> implements Field { - protected Field[] fields; - private final String fieldName; - - /** - * Every instance of a specific subclass have a field array with the same structure. - * In other words have the same size, and the same type of field at each position. - */ - public CompositeField(String fieldName){ - this.fieldName = fieldName; - } - - public void writeTo(AnnotatedOutput out) { - out.annotate(0, fieldName + ":"); - out.indent(); - for (Field field: fields) { - field.writeTo(out); - } - out.deindent(); - } - - public void readFrom(Input in) { - for (Field field: fields) { - field.readFrom(in); - } - } - - public int place(int offset) { - for (Field field: fields) { - offset = field.place(offset); - } - return offset; - } - - public void copyTo(DexFile dexFile, T copy) { - for (int i = 0; i < fields.length; i++) { - /** - * This assumes that the fields will be the same for every instance - * of a specific concrete subclass. By making this assumption, every subclass is - * prevented from having to implement copyTo - */ - fields[i].copyTo(dexFile, copy.fields[i]); - } - } - - public int hashCode() { - int h = 1; - for (Field field: fields) { - h = h * 31 + field.hashCode(); - } - return h; - } - - public boolean equals(Object o) { - if (!(o instanceof CompositeField)) { - return false; - } - - CompositeField other = (CompositeField)o; - if (fields.length != other.fields.length) { - return false; - } - for (int i = 0; i < fields.length; i++) { - if (!fields[i].equals(other.fields[i])) { - return false; - } - } - - return true; - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/Debug/AdvanceLine.java b/dexlib/src/main/java/org/jf/dexlib/Debug/AdvanceLine.java deleted file mode 100644 index 4f976973..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Debug/AdvanceLine.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.Debug; - -import org.jf.dexlib.ByteField; -import org.jf.dexlib.CompositeField; -import org.jf.dexlib.Field; -import org.jf.dexlib.SignedLeb128Field; - -public class AdvanceLine extends CompositeField implements DebugInstruction { - private final ByteField opcodeField; - private final SignedLeb128Field lineDeltaField; - - public AdvanceLine() { - super("DBG_ADVANCE_LINE"); - fields = new Field[] { - opcodeField = new ByteField((byte)0x02, "opcode"), - lineDeltaField = new SignedLeb128Field("line_diff") - }; - } - - public AdvanceLine(int lineDelta) { - this(); - lineDeltaField.cacheValue(lineDelta); - } - - public byte getOpcode() { - return 0x02; - } - - public int getLineDelta() { - return lineDeltaField.getCachedValue(); - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/Debug/AdvancePC.java b/dexlib/src/main/java/org/jf/dexlib/Debug/AdvancePC.java deleted file mode 100644 index 03255c94..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Debug/AdvancePC.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.Debug; - -import org.jf.dexlib.ByteField; -import org.jf.dexlib.CompositeField; -import org.jf.dexlib.Field; -import org.jf.dexlib.Leb128Field; - -public class AdvancePC extends CompositeField implements DebugInstruction { - private final ByteField opcodeField; - private final Leb128Field addressDeltaField; - - public AdvancePC() { - super("DBG_ADVANCE_PC"); - fields = new Field[] { - opcodeField = new ByteField((byte)0x01, "opcode"), - addressDeltaField = new Leb128Field("addr_diff") - }; - } - - public AdvancePC(int addressDelta) { - this(); - addressDeltaField.cacheValue(addressDelta); - } - - public byte getOpcode() { - return 0x01; - } - - public int getAddressDelta() { - return addressDeltaField.getCachedValue(); - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/Debug/DebugInstruction.java b/dexlib/src/main/java/org/jf/dexlib/Debug/DebugInstruction.java deleted file mode 100644 index db4a1436..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Debug/DebugInstruction.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.Debug; - -import org.jf.dexlib.Field; - -public interface DebugInstruction extends Field { - public byte getOpcode(); -} diff --git a/dexlib/src/main/java/org/jf/dexlib/Debug/DebugInstructionFactory.java b/dexlib/src/main/java/org/jf/dexlib/Debug/DebugInstructionFactory.java deleted file mode 100644 index 4b4e2ffd..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Debug/DebugInstructionFactory.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.Debug; - -import org.jf.dexlib.DexFile; -import org.jf.dexlib.Util.Input; - -public abstract class DebugInstructionFactory { - public static DebugInstruction readDebugInstruction(DexFile dexFile, Input in) { - int startCursor = in.getCursor(); - byte opcode = in.readByte(); - in.setCursor(startCursor); - - DebugInstruction debugInstruction = makeDebugInstruction(dexFile, opcode); - debugInstruction.readFrom(in); - return debugInstruction; - } - - public static DebugInstruction makeDebugInstruction(DexFile dexFile, byte opcode) { - switch (opcode) { - case 0x00: - return new EndSequence(); - case 0x01: - return new AdvancePC(); - case 0x02: - return new AdvanceLine(); - case 0x03: - return new StartLocal(dexFile); - case 0x04: - return new StartLocalExtended(dexFile); - case 0x05: - return new EndLocal(); - case 0x06: - return new RestartLocal(dexFile); - case 0x07: - return new SetPrologueEnd(); - case 0x08: - return new SetEpilogueBegin(); - case 0x09: - return new SetFile(dexFile); - default: - return new SpecialOpcode(opcode); - } - - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/Debug/EndLocal.java b/dexlib/src/main/java/org/jf/dexlib/Debug/EndLocal.java deleted file mode 100644 index 6810f9e1..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Debug/EndLocal.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.Debug; - -import org.jf.dexlib.ByteField; -import org.jf.dexlib.CompositeField; -import org.jf.dexlib.Field; -import org.jf.dexlib.Leb128Field; - -public class EndLocal extends CompositeField implements DebugInstruction { - private final ByteField opcode; - private final Leb128Field registerNumber; - - public EndLocal() { - super("DBG_END_LOCAL"); - fields = new Field[] { - opcode = new ByteField((byte)0x05, "opcode"), - registerNumber = new Leb128Field("register_num") - }; - } - - public EndLocal(int registerNumber) { - this(); - this.registerNumber.cacheValue(registerNumber); - } - - public byte getOpcode() { - return 0x05; - } - - public int getRegisterNumber() { - return registerNumber.getCachedValue(); - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/Debug/EndSequence.java b/dexlib/src/main/java/org/jf/dexlib/Debug/EndSequence.java deleted file mode 100644 index 5fc8ea3e..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Debug/EndSequence.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.Debug; - -import org.jf.dexlib.ByteField; -import org.jf.dexlib.CompositeField; -import org.jf.dexlib.Field; - -public class EndSequence extends CompositeField implements DebugInstruction { - public EndSequence() { - super("DBG_END_SEQUENCE"); - fields = new Field[] { - new ByteField((byte)0x00, "opcode") - }; - } - - public byte getOpcode() { - return 0x00; - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/Debug/RestartLocal.java b/dexlib/src/main/java/org/jf/dexlib/Debug/RestartLocal.java deleted file mode 100644 index 9c4e55c1..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Debug/RestartLocal.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.Debug; - -import org.jf.dexlib.*; - -public class RestartLocal extends CompositeField implements DebugInstruction { - private final ByteField opcode; - private final Leb128Field registerNumber; - - public RestartLocal(DexFile dexFile) { - super("DBG_RESTART_LOCAL"); - fields = new Field[] { - opcode = new ByteField((byte)0x06, "opcode"), - registerNumber = dexFile.getPreserveSignedRegisters()?new Leb128Field.PossiblySignedLeb128Field("register_num"): - new Leb128Field("register_num") - }; - } - - public RestartLocal(int registerNumber) { - super("DBG_RESTART_LOCAL"); - fields = new Field[] { - this.opcode = new ByteField((byte)0x06, "opcode"), - this.registerNumber = new Leb128Field(registerNumber, "register_num") - }; - - - } - - public byte getOpcode() { - return 0x06; - } - - public int getRegisterNumber() { - return registerNumber.getCachedValue(); - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/Debug/SetEpilogueBegin.java b/dexlib/src/main/java/org/jf/dexlib/Debug/SetEpilogueBegin.java deleted file mode 100644 index cbdc0331..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Debug/SetEpilogueBegin.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.Debug; - -import org.jf.dexlib.ByteField; -import org.jf.dexlib.CompositeField; -import org.jf.dexlib.Field; - -public class SetEpilogueBegin extends CompositeField implements DebugInstruction { - public SetEpilogueBegin() { - super("DBG_SET_EPILOGUE_BEGIN"); - fields = new Field[] { - new ByteField((byte)0x08, "opcode") - }; - } - - public byte getOpcode() { - return 0x08; - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/Debug/SetFile.java b/dexlib/src/main/java/org/jf/dexlib/Debug/SetFile.java deleted file mode 100644 index 7e5dd845..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Debug/SetFile.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.Debug; - -import org.jf.dexlib.*; - -public class SetFile extends CompositeField implements DebugInstruction { - private final ByteField opcode; - private final IndexedItemReference fileName; - - public SetFile(DexFile dexFile) { - super("DBG_SET_FILE"); - fields = new Field[] { - opcode = new ByteField((byte)0x09, "opcode"), - fileName = new IndexedItemReference(dexFile.StringIdsSection, - new Leb128p1Field(null), "name_idx") - }; - } - - public SetFile(DexFile dexFile, StringIdItem fileName) { - this(dexFile); - this.fileName.setReference(fileName); - } - - public byte getOpcode() { - return 0x09; - } - - public StringIdItem getFileName() { - return fileName.getReference(); - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/Debug/SetPrologueEnd.java b/dexlib/src/main/java/org/jf/dexlib/Debug/SetPrologueEnd.java deleted file mode 100644 index 8ac5caca..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Debug/SetPrologueEnd.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.Debug; - -import org.jf.dexlib.ByteField; -import org.jf.dexlib.CompositeField; -import org.jf.dexlib.Field; - -public class SetPrologueEnd extends CompositeField implements DebugInstruction { - public SetPrologueEnd() { - super("DBG_SET_PROLOGUE_END"); - fields = new Field[] { - new ByteField((byte)0x07, "opcode") - }; - } - - public byte getOpcode() { - return 0x07; - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/Debug/SpecialOpcode.java b/dexlib/src/main/java/org/jf/dexlib/Debug/SpecialOpcode.java deleted file mode 100644 index 6b716574..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Debug/SpecialOpcode.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.Debug; - -import org.jf.dexlib.ByteField; -import org.jf.dexlib.CompositeField; -import org.jf.dexlib.Field; - -public class SpecialOpcode extends CompositeField implements DebugInstruction { - private final byte opcode; - - public SpecialOpcode(byte opcode) { - super("SPECIAL_OPCODE"); - this.opcode = opcode; - fields = new Field[] { - //TODO: annotate the line and address delta - new ByteField(opcode, "opcode") - }; - } - - public byte getOpcode() { - return opcode; - } - - public byte getLineDelta() { - return (byte)((((opcode & 0xFF) - 0x0A) % 15) - 4); - } - - public byte getAddressDelta() { - return (byte)(((opcode & 0xFF) - 0x0A) / 15); - } -} - diff --git a/dexlib/src/main/java/org/jf/dexlib/Debug/StartLocal.java b/dexlib/src/main/java/org/jf/dexlib/Debug/StartLocal.java deleted file mode 100644 index cd6fb7db..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Debug/StartLocal.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.Debug; - -import org.jf.dexlib.*; - -public class StartLocal extends CompositeField implements DebugInstruction { - private final ByteField opcodeField; - private final Leb128Field registerNumber; - private final IndexedItemReference localName; - private final IndexedItemReference localType; - - public StartLocal(DexFile dexFile) { - super("DBG_START_LOCAL"); - fields = new Field[] { - opcodeField = new ByteField((byte)0x03, "opcode"), - registerNumber = dexFile.getPreserveSignedRegisters()?new Leb128Field.PossiblySignedLeb128Field("register_num"): - new Leb128Field("register_num"), - localName = new IndexedItemReference(dexFile.StringIdsSection, - new Leb128p1Field(null), "name_idx"), - localType = new IndexedItemReference(dexFile.TypeIdsSection, - new Leb128p1Field(null), "type_idx"), - }; - } - - public StartLocal(DexFile dexFile, int registerNumber, StringIdItem localName, TypeIdItem localType) { - this(dexFile); - this.registerNumber.cacheValue(registerNumber); - this.localName.setReference(localName); - this.localType.setReference(localType); - } - - public byte getOpcode() { - return 0x03; - } - - public int getRegisterNumber() { - return registerNumber.getCachedValue(); - } - - public StringIdItem getName() { - return localName.getReference(); - } - - public TypeIdItem getType() { - return localType.getReference(); - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/Debug/StartLocalExtended.java b/dexlib/src/main/java/org/jf/dexlib/Debug/StartLocalExtended.java deleted file mode 100644 index c05c7caf..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Debug/StartLocalExtended.java +++ /dev/null @@ -1,83 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.Debug; - -import org.jf.dexlib.*; - -public class StartLocalExtended extends CompositeField implements DebugInstruction { - private final ByteField opcodeField; - private final Leb128Field registerNumber; - private final IndexedItemReference localName; - private final IndexedItemReference localType; - private final IndexedItemReference signature; - - public StartLocalExtended(DexFile dexFile) { - super("DBG_START_LOCAL_EXTENDED"); - fields = new Field[] { - opcodeField = new ByteField((byte)0x04, "opcode"), - registerNumber = dexFile.getPreserveSignedRegisters()?new Leb128Field.PossiblySignedLeb128Field("register_num"): - new Leb128Field("register_num"), - localName = new IndexedItemReference(dexFile.StringIdsSection, - new Leb128p1Field(null), "name_idx"), - localType = new IndexedItemReference(dexFile.TypeIdsSection, - new Leb128p1Field(null), "type_idx"), - signature = new IndexedItemReference(dexFile.StringIdsSection, - new Leb128p1Field(null), "sig_idx") - }; - } - - public StartLocalExtended(DexFile dexFile, int registerNumber, StringIdItem localName, TypeIdItem localType, - StringIdItem signature) { - this(dexFile); - this.registerNumber.cacheValue(registerNumber); - this.localName.setReference(localName); - this.localType.setReference(localType); - this.signature.setReference(signature); - } - - public byte getOpcode() { - return 0x04; - } - - public int getRegisterNumber() { - return registerNumber.getCachedValue(); - } - - public StringIdItem getName() { - return localName.getReference(); - } - - public TypeIdItem getType() { - return localType.getReference(); - } - - public StringIdItem getSignature() { - return signature.getReference(); - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/DebugInfoItem.java b/dexlib/src/main/java/org/jf/dexlib/DebugInfoItem.java deleted file mode 100644 index 82d6c20f..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/DebugInfoItem.java +++ /dev/null @@ -1,177 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib; - -import org.jf.dexlib.Debug.DebugInstruction; -import org.jf.dexlib.Debug.DebugInstructionFactory; -import org.jf.dexlib.Debug.EndSequence; -import org.jf.dexlib.Util.AnnotatedOutput; -import org.jf.dexlib.Util.Input; - -import java.util.ArrayList; -import java.util.List; -import java.util.Collections; - -public class DebugInfoItem extends OffsettedItem { - private final ArrayList> parameterNames = - new ArrayList>(); - - private ArrayList instructionFields = new ArrayList(); - - private final Leb128Field lineStartField; - private final ListSizeField parameterNamesSizeField; - private final FieldListField> parameterNamesField; - private final DebugInstructionList debugInstructionListField; - - private CodeItem parent = null; - - public DebugInfoItem(final DexFile dexFile, int offset) { - super(dexFile, offset); - - fields = new Field[] { - lineStartField = new Leb128Field("line_start"), - parameterNamesSizeField = new ListSizeField(parameterNames, new Leb128Field("parameters_size")), - parameterNamesField = new FieldListField>( - parameterNames, "parameter_names") { - protected IndexedItemReference make() { - return new IndexedItemReference(dexFile.StringIdsSection, - new Leb128p1Field(null), "parameter_name"); - } - }, - debugInstructionListField = new DebugInstructionList(dexFile) - }; - } - - public DebugInfoItem(final DexFile dexFile, - int lineStart, - List parameterNames, - List debugInstructions) { - this(dexFile, 0); - - this.lineStartField.cacheValue(lineStart); - - for (StringIdItem parameterName: parameterNames) { - IndexedItemReference parameterReference = parameterNamesField.make(); - parameterReference.setReference(parameterName); - this.parameterNames.add(parameterReference); - } - - this.instructionFields.addAll(debugInstructions); - } - - public ItemType getItemType() { - return ItemType.TYPE_DEBUG_INFO_ITEM; - } - - protected void setParent(CodeItem codeItem) { - this.parent = codeItem; - } - - public String getConciseIdentity() { - return "debug_info_item @0x" + Integer.toHexString(getOffset()); - } - - public int getLineStart() { - return lineStartField.getCachedValue(); - } - - public List getDebugInstructions() { - return Collections.unmodifiableList(instructionFields); - } - - public List getParameterNames() { - List parameterNamesList = new ArrayList(); - - for (IndexedItemReference parameterNameReference: parameterNames) { - StringIdItem parameterNameItem = parameterNameReference.getReference(); - if (parameterNameItem == null) { - parameterNamesList.add(null); - } else { - parameterNamesList.add(parameterNameItem.getStringValue()); - } - } - return parameterNamesList; - } - - public int compareTo(DebugInfoItem other) { - if (parent == null) { - if (other.parent == null) { - return 0; - } - return -1; - } - if (other.parent == null) { - return 1; - } - return parent.compareTo(other.parent); - } - - - private class DebugInstructionList implements Field { - private final DexFile dexFile; - private final ArrayList list; - - public DebugInstructionList(DexFile dexFile) { - this.dexFile = dexFile; - list = instructionFields; - } - - public void writeTo(AnnotatedOutput out) { - for (DebugInstruction debugInstruction: list) { - debugInstruction.writeTo(out); - } - } - - public void readFrom(Input in) { - DebugInstruction debugInstruction; - do { - debugInstruction = DebugInstructionFactory.readDebugInstruction(dexFile, in); - list.add(debugInstruction); - } while (!(debugInstruction instanceof EndSequence)); - } - - public int place(int offset) { - for (Field field: list) { - offset = field.place(offset); - } - return offset; - } - - public void copyTo(DexFile dexFile, DebugInstructionList copy) { - copy.list.clear(); - copy.list.ensureCapacity(list.size()); - for (int i = 0; i < list.size(); i++) { - DebugInstruction debugInstruction = list.get(i); - DebugInstruction debugInstructionCopy = DebugInstructionFactory.makeDebugInstruction(dexFile, debugInstruction.getOpcode()); - debugInstruction.copyTo(dexFile, debugInstructionCopy); - copy.list.add(debugInstructionCopy); - } - } - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/DexFile.java b/dexlib/src/main/java/org/jf/dexlib/DexFile.java deleted file mode 100644 index 3bc7e488..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/DexFile.java +++ /dev/null @@ -1,738 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib; - -import org.jf.dexlib.Util.AnnotatedOutput; -import org.jf.dexlib.Util.ByteArrayInput; -import org.jf.dexlib.Util.FileUtils; -import org.jf.dexlib.Util.Input; - -import java.io.File; -import java.security.DigestException; -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; -import java.util.HashMap; -import java.util.zip.Adler32; - - /** - *

Main use cases

- * - *

These are the main use cases that drove the design of this library

- * - *
    - *
  1. Annotate an existing dex file - In this case, the intent is to document the structure of - * an existing dex file. We want to be able to read in the dex file, and then write out a dex file - * that is exactly the same (while adding annotation information to an AnnotatedOutput object)

  2. - * - *
  3. Canonicalize an existing dex file - In this case, the intent is to rewrite an existing dex file - * so that it is in a canonical form. There is a certain amount of leeway in how various types of - * tems in a dex file are ordered or represented. It is sometimes useful to be able to easily - * compare a disassebled and reassembled dex file with the original dex file. If both dex-files are - * written canonically, they "should" match exactly, barring any explicit changes to the reassembled - * file.

    - * - *

    Currently, there are a couple of pieces of information that probably won't match exactly - *

      - *
    • the order of exception handlers in the EncodedCatchHandlerList for a method
    • - *
    • the ordering of some of the debug info in the {@link DebugInfoItem} for a method
    • - *

    - * - * - *

    Note that the above discrepancies should typically only be "intra-item" differences. They - * shouldn't change the size of the item, or affect how anything else is placed or laid out

  4. - * - *
  5. Creating a dex file from scratch - In this case, a blank dex file is created and then classes - * are added to it incrementally by calling the {@link Section#intern intern} method of - * {@link DexFile#ClassDefsSection}, which will add all the information necessary to represent the given - * class. For example, when assembling a dex file from a set of assembly text files.

    - * - *

    In this case, we can choose to write the dex file in a canonical form or not. It is somewhat - * slower to write it in a canonical format, due to the extra sorting and calculations that are - * required.

  6. - * - * - *
  7. Reading in the dex file - In this case, the intent is to read in a dex file and expose all the - * data to the calling application. For example, when disassembling a dex file into a text based - * assembly format, or doing other misc processing of the dex file.

  8. - * - * - *

    Other use cases

    - * - *

    These are other use cases that are possible, but did not drive the design of the library. - * No effort was made to test these use cases or ensure that they work. Some of these could - * probably be better achieved with a disassemble - modify - reassemble type process, using - * smali/baksmali or another assembler/disassembler pair that are compatible with each other

    - * - *
      - *
    • deleting classes/methods/etc. from a dex file
    • - *
    • merging 2 dex files
    • - *
    • splitting a dex file
    • - *
    • moving classes from 1 dex file to another
    • - *
    • removing the debug information from a dex file
    • - *
    • obfustication of a dex file
    • - *
    - */ -public class DexFile -{ - /** - * A mapping from ItemType to the section that contains items of the given type - */ - private final HashMap sectionsByType; - - /** - * Ordered lists of the indexed and offsetted sections. The order of these lists specifies the order - * that the sections will be written in - */ - private final IndexedSection[] indexedSections; - private final OffsettedSection[] offsettedSections; - - /** - * dalvik had a bug where it wrote the registers for certain types of debug info in a signed leb - * format, instead of an unsigned leb format. There are no negative registers of course, but - * certain positive values have a different encoding depending on whether they are encoded as - * an unsigned leb128 or a signed leb128. Specifically, the signed leb128 is 1 byte longer in some cases. - * - * This determine whether we should keep any signed registers as signed, or force all register to - * unsigned. By default we don't keep track of whether they were signed or not, and write them back - * out as unsigned. This option only has an effect when reading an existing dex file. It has no - * effect when a dex file is created from scratch - * - * The 2 main use-cases in play are - * 1. Annotate an existing dex file - In this case, preserveSignedRegisters should be false, so that we keep - * track of any signed registers and write them back out as signed Leb128 values. - * - * 2. Canonicalize an existing dex file - In this case, fixRegisters should be true, so that all - * registers in the debug info are written as unsigned Leb128 values regardless of how they were - * originally encoded - */ - private final boolean preserveSignedRegisters; - - /** - * When true, this prevents any sorting of the items during placement of the dex file. This - * should *only* be set to true when this dex file was read in from an existing (valid) dex file, - * and no modifications were made (i.e. no items added or deleted). Otherwise it is likely that - * an invalid dex file will be generated. - * - * This is useful for the first use case (annotating an existing dex file). This ensures the items - * retain the same order as in the original dex file. - */ - private boolean inplace = false; - - /** - * When true, this imposes an full ordering on all the items, to force them into a (possibly - * arbitrary) canonical order. When false, only the items that the dex format specifies - * an order for are sorted. The rest of the items are not ordered. - * - * This is useful for the second use case (canonicalizing an existing dex file) or possibly for - * the third use case (creating a dex file from scratch), if there is a need to write the new - * dex file in a canonical form. - */ - private boolean sortAllItems = false; - - - /** - * this is used to access the dex file from within inner classes, when they declare fields or - * variable that hide fields on this object - */ - private final DexFile dexFile = this; - - - /** - * A private constructor containing common code to initialize the section maps and lists - * @param preserveSignedRegisters If true, keep track of any registers in the debug information - * that are signed, so they will be written in the same format. See - * getPreserveSignedRegisters() - */ - private DexFile(boolean preserveSignedRegisters) { - this.preserveSignedRegisters = preserveSignedRegisters; - - sectionsByType = new HashMap(18); - - sectionsByType.put(ItemType.TYPE_ANNOTATION_ITEM, AnnotationsSection); - sectionsByType.put(ItemType.TYPE_ANNOTATION_SET_ITEM, AnnotationSetsSection); - sectionsByType.put(ItemType.TYPE_ANNOTATION_SET_REF_LIST, AnnotationSetRefListsSection); - sectionsByType.put(ItemType.TYPE_ANNOTATIONS_DIRECTORY_ITEM, AnnotationDirectoriesSection); - sectionsByType.put(ItemType.TYPE_CLASS_DATA_ITEM, ClassDataSection); - sectionsByType.put(ItemType.TYPE_CLASS_DEF_ITEM, ClassDefsSection); - sectionsByType.put(ItemType.TYPE_CODE_ITEM, CodeItemsSection); - sectionsByType.put(ItemType.TYPE_DEBUG_INFO_ITEM, DebugInfoItemsSection); - sectionsByType.put(ItemType.TYPE_ENCODED_ARRAY_ITEM, EncodedArraysSection); - sectionsByType.put(ItemType.TYPE_FIELD_ID_ITEM, FieldIdsSection); - sectionsByType.put(ItemType.TYPE_HEADER_ITEM, HeaderItemSection); - sectionsByType.put(ItemType.TYPE_MAP_LIST, MapSection); - sectionsByType.put(ItemType.TYPE_METHOD_ID_ITEM, MethodIdsSection); - sectionsByType.put(ItemType.TYPE_PROTO_ID_ITEM, ProtoIdsSection); - sectionsByType.put(ItemType.TYPE_STRING_DATA_ITEM, StringDataSection); - sectionsByType.put(ItemType.TYPE_STRING_ID_ITEM, StringIdsSection); - sectionsByType.put(ItemType.TYPE_TYPE_ID_ITEM, TypeIdsSection); - sectionsByType.put(ItemType.TYPE_TYPE_LIST, TypeListsSection); - - indexedSections = new IndexedSection[] { - StringIdsSection, - TypeIdsSection, - ProtoIdsSection, - FieldIdsSection, - MethodIdsSection, - ClassDefsSection - }; - - offsettedSections = new OffsettedSection[] { - AnnotationSetRefListsSection, - AnnotationSetsSection, - CodeItemsSection, - AnnotationDirectoriesSection, - TypeListsSection, - StringDataSection, - AnnotationsSection, - EncodedArraysSection, - ClassDataSection, - DebugInfoItemsSection - }; - } - - - /** - * Construct a new DexFile instance by reading in the given dex file. - * @param file The dex file to read in - */ - public DexFile(String file) { - this(new File(file), true); - } - - /** - * Construct a new DexFile instance by reading in the given dex file, - * and optionally keep track of any registers in the debug information that are signed, - * so they will be written in the same format. - * @param file The dex file to read in - * @param preserveSignedRegisters If true, keep track of any registers in the debug information - * that are signed, so they will be written in the same format. See - * getPreserveSignedRegisters() - */ - public DexFile(String file, boolean preserveSignedRegisters) { - this(new File(file), preserveSignedRegisters); - } - - /** - * Construct a new DexFile instead by reading in the given dex file. - * @param file The dex file to read in - */ - public DexFile(File file) { - this(file, true); - } - - /** - * Construct a new DexFile instance by reading in the given dex file, - * and optionally keep track of any registers in the debug information that are signed, - * so they will be written in the same format. - * @param file The dex file to read in - * @param preserveSignedRegisters If true, keep track of any registers in the debug information - * that are signed, so they will be written in the same format. - * @see #getPreserveSignedRegisters - */ - public DexFile(File file, boolean preserveSignedRegisters) { - this(preserveSignedRegisters); - - Input in = new ByteArrayInput(FileUtils.readFile(file)); - - HeaderItemSection.readFrom(1, in); - HeaderItem headerItem = HeaderItemSection.items.get(0); - - in.setCursor(headerItem.getMapOffset()); - MapSection.readFrom(1, in); - - MapField[] mapEntries = MapSection.items.get(0).getMapEntries(); - HashMap mapMap = new HashMap(); - for (MapField mapField: mapEntries) { - mapMap.put(mapField.getSectionItemType().getMapValue(), mapField); - } - - /** - * This defines the order in which the sections are read in. This is not - * necessarily the order in which the appear in the file. - */ - //TODO: do we *need* to read them in a specific order, rather than the order that is in the file? - int[] sectionTypes = new int[] { - ItemType.TYPE_HEADER_ITEM.getMapValue(), - ItemType.TYPE_STRING_ID_ITEM.getMapValue(), - ItemType.TYPE_TYPE_ID_ITEM.getMapValue(), - ItemType.TYPE_PROTO_ID_ITEM.getMapValue(), - ItemType.TYPE_FIELD_ID_ITEM.getMapValue(), - ItemType.TYPE_METHOD_ID_ITEM.getMapValue(), - ItemType.TYPE_CLASS_DEF_ITEM.getMapValue(), - ItemType.TYPE_STRING_DATA_ITEM.getMapValue(), - ItemType.TYPE_ENCODED_ARRAY_ITEM.getMapValue(), - ItemType.TYPE_ANNOTATION_ITEM.getMapValue(), - ItemType.TYPE_ANNOTATION_SET_ITEM.getMapValue(), - ItemType.TYPE_ANNOTATION_SET_REF_LIST.getMapValue(), - ItemType.TYPE_ANNOTATIONS_DIRECTORY_ITEM.getMapValue(), - ItemType.TYPE_TYPE_LIST.getMapValue(), - ItemType.TYPE_CODE_ITEM.getMapValue(), - ItemType.TYPE_CLASS_DATA_ITEM.getMapValue(), - ItemType.TYPE_DEBUG_INFO_ITEM.getMapValue(), - ItemType.TYPE_MAP_LIST.getMapValue() - }; - - for (int sectionType: sectionTypes) { - MapField mapField = mapMap.get(sectionType); - if (mapField != null) { - Section section = sectionsByType.get(mapField.getSectionItemType()); - if (section != null) { - in.setCursor(mapField.getCachedSectionOffset()); - section.readFrom(mapField.getCachedSectionSize(), in); - } - } - } - } - - /** - * Constructs a new, blank dex file. Classes can be added to this dex file by calling - * the Section.intern() method of ClassDefsSection - */ - public DexFile() { - this(true); - - HeaderItemSection.intern(new HeaderItem(dexFile, 0)); - MapSection.intern(new MapItem(dexFile)); - } - - /** - * Convenience method to retrieve the header item - * @return the header item - */ - public HeaderItem getHeaderItem() { - return HeaderItemSection.getItems().get(0); - } - - /** - * Convenience method to retrieve the map item - * @return the map item - */ - public MapItem getMapItem() { - return MapSection.getItems().get(0); - } - - /** - * Get the Section containing items of the same type as the given item - * @param item Get the Section that contains items of this type - * @param The specific item subclass - inferred from the passed item - * @return the Section containing items of the same type as the given item - */ - public Section getSectionForItem(T item) { - return sectionsByType.get(item.getItemType()); - } - - /** - * Get the Section containing items of the given type - * @param itemType the type of item - * @return the Section containing items of the given type - */ - public Section getSectionForType(ItemType itemType) { - return sectionsByType.get(itemType); - } - - /** - * Get a boolean value indicating whether this dex file preserved any signed - * registers in the debug info as it read the dex file in. By default, the dex file - * doesn't check whether the registers are encoded as unsigned or signed values. - * - * This does *not* affect the actual register value that is read in. The value is - * read correctly regardless - * - * This does affect whether any signed registers will retain the same encoding or be - * forced to the (correct) unsigned encoding when the dex file is written back out. - * - * See the discussion about signed register values in the documentation for - * DexFile - * @return a boolean indicating whether this dex file preserved any signed registers - * as it was read in - */ - public boolean getPreserveSignedRegisters() { - return preserveSignedRegisters; - } - - /** - * Get a boolean value indicating whether all items should be placed into a - * (possibly arbitrary) "canonical" ordering. If false, then only the items - * that must be ordered per the dex specification are sorted. - * - * When true, writing the dex file involves somewhat more overhead - * - * If both SortAllItems and Inplace are true, Inplace takes precedence - * @return a boolean value indicating whether all items should be sorted - */ - public boolean getSortAllItems() { - return this.sortAllItems; - } - - /** - * Set a boolean value indicating whether all items should be placed into a - * (possibly arbitrary) "canonical" ordering. If false, then only the items - * that must be ordered per the dex specification are sorted. - * - * When true, writing the dex file involves somewhat more overhead - * - * If both SortAllItems and Inplace are true, Inplace takes precedence - * @param value a boolean value indicating whether all items should be sorted - */ - public void setSortAllItems(boolean value) { - this.sortAllItems = value; - } - - /** - * Get a boolean value indicating whether items in this dex file should be - * written back out "in-place", or whether the normal layout logic should be - * applied. - * - * This should only be used for a dex file that has been read from an existing - * dex file, and no modifications have been made to the dex file. Otherwise, - * there is a good chance that the resulting dex file will be invalid due to - * items that aren't placed correctly - * - * If both SortAllItems and Inplace are true, Inplace takes precedence - * @return a boolean value indicating whether items in this dex file should be - * written back out in-place. - */ - public boolean getInplace() { - return this.inplace; - } - - /** - * Set a boolean value indicating whether items in this dex file should be - * written back out "in-place", or whether the normal layout logic should be - * applied. - * - * This should only be used for a dex file that has been read from an existing - * dex file, and no modifications have been made to the dex file. Otherwise, - * there is a good chance that the resulting dex file will be invalid due to - * items that aren't placed correctly - * - * If both SortAllItems and Inplace are true, Inplace takes precedence - * @param value a boolean value indicating whether items in this dex file should be - * written back out in-place. - */ - public void setInplace(boolean value) { - this.inplace = value; - } - - /** - * This method should be called before writing a dex file. It sorts the sections - * as needed or as indicated by getSortAllItems() and getInplace(), - * and then performs a pass through all of the items, finalizing the position (i.e. - * index and/or offset) of each item in the dex file. - * - * This step is needed primarily so that the indexes and offsets of all indexed and - * offsetted items are available when writing references to those items elsewhere. - */ - public void place() { - HeaderItem headerItem = getHeaderItem(); - - int offset = HeaderItemSection.place(0); - - int dataOffset; - - for (IndexedSection indexedSection: indexedSections) { - offset = indexedSection.place(offset); - } - - dataOffset = offset; - headerItem.setDataOffset(dataOffset); - - //TODO: if inplace is true, we need to use the order of the sections as they were in the original file - for (OffsettedSection offsettedSection: offsettedSections) { - if (this.sortAllItems && !this.inplace) { - offsettedSection.sortSection(); - } - offset = offsettedSection.place(offset); - } - - offset = MapSection.place(offset); - - - headerItem.setFileSize(offset); - headerItem.setDataSize(offset - dataOffset); - } - - /** - * Writes the dex file to the give AnnotatedOutput object. If - * out.Annotates() is true, then annotations that document the format - * of the dex file are written. - * - * You must call place() on this dex file, before calling this method - * @param out the AnnotatedOutput object to write the dex file and annotations to - * - * After calling this method, you should call calcSignature() and - * then calcChecksum() on the resulting byte array, to calculate the - * signature and checksum in the header - */ - public void writeTo(AnnotatedOutput out) { - HeaderItemSection.writeTo(out); - - for (IndexedSection indexedSection: indexedSections) { - indexedSection.writeTo(out); - } - - for (OffsettedSection offsettedSection: offsettedSections) { - offsettedSection.writeTo(out); - } - - MapSection.writeTo(out); - } - - /** - * The IndexedSection containing the sole HeaderItem item. Use - * getHeaderItem() instead. - */ - public final IndexedSection HeaderItemSection = new IndexedSection(this) { - protected HeaderItem make(int index) { - return new HeaderItem(dexFile, index); - } - }; - - /** - * The IndexedSection containing StringIdItem items - */ - public final IndexedSection StringIdsSection = new IndexedSection(this) { - protected StringIdItem make(int index) { - return new StringIdItem(dexFile, index); - } - }; - - /** - * The IndexedSection containing TypeIdItem items - */ - public final IndexedSection TypeIdsSection = new IndexedSection(this) { - protected TypeIdItem make(int index) { - return new TypeIdItem(dexFile, index); - } - }; - - /** - * The IndexedSection containing ProtoIdItem items - */ - public final IndexedSection ProtoIdsSection = new IndexedSection(this) { - protected ProtoIdItem make(int index) { - return new ProtoIdItem(dexFile, index); - } - }; - - /** - * The IndexedSection containing FieldIdItem items - */ - public final IndexedSection FieldIdsSection = new IndexedSection(this) { - protected FieldIdItem make(int index) { - return new FieldIdItem(dexFile, index); - } - }; - - /** - * The IndexedSection containing MethodIdItem items - */ - public final IndexedSection MethodIdsSection = new IndexedSection(this) { - protected MethodIdItem make(int index) { - return new MethodIdItem(dexFile, index); - } - }; - - /** - * The IndexedSection containing ClassDefItem items - */ - public final IndexedSection ClassDefsSection = new IndexedSection(this) { - protected ClassDefItem make(int index) { - return new ClassDefItem(dexFile, index); - } - - public int place(int offset) { - if (dexFile.getInplace()) { - return super.place(offset); - } - - int ret = ClassDefItem.placeClassDefItems(this, offset); - - this.offset = items.get(0).getOffset(); - return ret; - } - }; - - /** - * The IndexedSection containing the sole MapItem. Use - * getMapItem() instead - */ - public final IndexedSection MapSection = new IndexedSection(this) { - protected MapItem make(int index) { - return new MapItem(dexFile, index); - } - - public MapItem intern(MapItem item) { - this.items.add(item); - return item; - } - }; - - /** - * The OffsettedSection containing TypeListItem items - */ - public final OffsettedSection TypeListsSection = new OffsettedSection(this) { - protected TypeListItem make(int offset) { - return new TypeListItem(dexFile, offset); - } - }; - - /** - * The OffsettedSection containing AnnotationSetRefList items - */ - public final OffsettedSection AnnotationSetRefListsSection = - new OffsettedSection(this) { - protected AnnotationSetRefList make(int offset) { - return new AnnotationSetRefList(dexFile, offset); - } - }; - - /** - * The OffsettedSection containing AnnotationSetItem items - */ - public final OffsettedSection AnnotationSetsSection = - new OffsettedSection(this) { - protected AnnotationSetItem make(int offset) { - return new AnnotationSetItem(dexFile, offset); - } - }; - - /** - * The OffsettedSection containing ClassDataItem items - */ - public final OffsettedSection ClassDataSection = new OffsettedSection(this) { - protected ClassDataItem make(int offset) { - return new ClassDataItem(dexFile, offset); - } - }; - - /** - * The OffsettedSection containing CodeItem items - */ - public final OffsettedSection CodeItemsSection = new OffsettedSection(this) { - protected CodeItem make(int offset) { - return new CodeItem(dexFile, offset); - } - }; - - /** - * The OffsettedSection containing StringDataItem items - */ - public final OffsettedSection StringDataSection = new OffsettedSection(this) { - protected StringDataItem make(int offset) { - return new StringDataItem(dexFile, offset); - } - }; - - /** - * The OffsettedSection containing DebugInfoItem items - */ - public final OffsettedSection DebugInfoItemsSection = new OffsettedSection(this) { - protected DebugInfoItem make(int offset) { - return new DebugInfoItem(dexFile, offset); - } - }; - - /** - * The OffsettedSection containing AnnotationItem items - */ - public final OffsettedSection AnnotationsSection = new OffsettedSection(this) { - protected AnnotationItem make(int offset) { - return new AnnotationItem(dexFile, offset); - } - }; - - /** - * The OffsettedSection containing EncodedArrayItem items - */ - public final OffsettedSection EncodedArraysSection = new OffsettedSection(this) { - protected EncodedArrayItem make(int offset) { - return new EncodedArrayItem(dexFile, offset); - } - }; - - /** - * The OffsettedSection containing AnnotationDirectoryItem items - */ - public final OffsettedSection AnnotationDirectoriesSection = - new OffsettedSection(this) { - protected AnnotationDirectoryItem make(int offset) { - return new AnnotationDirectoryItem(dexFile, offset); - } - }; - - - /** - * Calculates the signature for the dex file in the given byte array, - * and then writes the signature to the appropriate location in the header - * containing in the array - * - * @param bytes non-null; the bytes of the file - */ - public static void calcSignature(byte[] bytes) { - MessageDigest md; - - try { - md = MessageDigest.getInstance("SHA-1"); - } catch (NoSuchAlgorithmException ex) { - throw new RuntimeException(ex); - } - - md.update(bytes, 32, bytes.length - 32); - - try { - int amt = md.digest(bytes, 12, 20); - if (amt != 20) { - throw new RuntimeException("unexpected digest write: " + amt + - " bytes"); - } - } catch (DigestException ex) { - throw new RuntimeException(ex); - } - } - - /** - * Calculates the checksum for the .dex file in the - * given array, and modify the array to contain it. - * - * @param bytes non-null; the bytes of the file - */ - public static void calcChecksum(byte[] bytes) { - Adler32 a32 = new Adler32(); - - a32.update(bytes, 12, bytes.length - 12); - - int sum = (int) a32.getValue(); - - bytes[8] = (byte) sum; - bytes[9] = (byte) (sum >> 8); - bytes[10] = (byte) (sum >> 16); - bytes[11] = (byte) (sum >> 24); - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/EncodedArrayItem.java b/dexlib/src/main/java/org/jf/dexlib/EncodedArrayItem.java deleted file mode 100644 index 5a4c0303..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/EncodedArrayItem.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib; - -import org.jf.dexlib.EncodedValue.ArrayEncodedValueSubField; -import org.jf.dexlib.EncodedValue.EncodedValue; - -import java.util.ArrayList; - -public class EncodedArrayItem extends OffsettedItem { - private final ArrayEncodedValueSubField encodedArray; - - public EncodedArrayItem(DexFile dexFile, int offset) { - super(dexFile, offset); - - fields = new Field[] { - encodedArray = new ArrayEncodedValueSubField(dexFile) - }; - } - - public EncodedArrayItem(DexFile dexFile, ArrayList encodedValues) { - super(dexFile, 0); - - fields = new Field[] { - encodedArray = new ArrayEncodedValueSubField(dexFile, encodedValues) - }; - } - - - protected int getAlignment() { - return 1; - } - - public void add(int index, EncodedValue value) { - encodedArray.add(index, value); - } - - public ItemType getItemType() { - return ItemType.TYPE_ENCODED_ARRAY_ITEM; - } - - public String getConciseIdentity() { - return "encoded_array @0x" + Integer.toHexString(getOffset()); - } - - public ArrayEncodedValueSubField getEncodedArray() { - return encodedArray; - } - - public int compareTo(EncodedArrayItem encodedArrayItem) { - return encodedArray.compareTo(encodedArrayItem.encodedArray); - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/EncodedValue/AnnotationElement.java b/dexlib/src/main/java/org/jf/dexlib/EncodedValue/AnnotationElement.java deleted file mode 100644 index b12fa70f..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/EncodedValue/AnnotationElement.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.EncodedValue; - -import org.jf.dexlib.*; - -public class AnnotationElement extends CompositeField - implements Comparable { - private final IndexedItemReference elementName; - private final EncodedValue encodedValue; - - public AnnotationElement(final DexFile dexFile) { - super("annotation_element"); - fields = new Field[] { - elementName = new IndexedItemReference(dexFile.StringIdsSection, - new Leb128Field(null), "element_name"), - encodedValue = new EncodedValue(dexFile) - }; - } - - public AnnotationElement(final DexFile dexFile, StringIdItem elementName, EncodedValue encodedValue) { - super("annotation_element"); - fields = new Field[] { - this.elementName = new IndexedItemReference(dexFile, elementName, - new Leb128Field(null), "element_name"), - this.encodedValue = encodedValue - }; - } - - public int compareTo(AnnotationElement annotationElement) { - int comp = elementName.compareTo(annotationElement.elementName); - if (comp == 0) { - comp = encodedValue.compareTo(annotationElement.encodedValue); - } - return comp; - } - - public StringIdItem getName() { - return elementName.getReference(); - } - - public EncodedValue getEncodedValue() { - return encodedValue; - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/EncodedValue/AnnotationEncodedValueSubField.java b/dexlib/src/main/java/org/jf/dexlib/EncodedValue/AnnotationEncodedValueSubField.java deleted file mode 100644 index 2f0db126..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/EncodedValue/AnnotationEncodedValueSubField.java +++ /dev/null @@ -1,112 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.EncodedValue; - -import org.jf.dexlib.*; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -public class AnnotationEncodedValueSubField extends CompositeField - implements EncodedValueSubField, Comparable { - - private final ArrayList annotationElementList = new ArrayList(); - - private final IndexedItemReference annotationType; - private final ListSizeField annotationCount; - private final FieldListField annotationElements; - - public AnnotationEncodedValueSubField(final DexFile dexFile) { - super("encoded_annotation"); - fields = new Field[] { - annotationType = new IndexedItemReference(dexFile.TypeIdsSection, - new Leb128Field(null), "type_idx"), - annotationCount = new ListSizeField(annotationElementList, new Leb128Field("size")), - annotationElements = new FieldListField(annotationElementList, "elements") { - protected AnnotationElement make() { - return new AnnotationElement(dexFile); - } - } - }; - } - - public AnnotationEncodedValueSubField(final DexFile dexFile, TypeIdItem annotationType, - List annotationElements) { - this(dexFile); - this.annotationType.setReference(annotationType); - this.annotationElementList.addAll(annotationElements); - } - - public void setInitialValueArg(byte valueArg) { - //valueArg is ignored for annotations - } - - public byte getValueArg() { - return 0; - } - - public int place(int offset) { - Collections.sort(annotationElementList); - return super.place(offset); - } - - public ValueType getValueType() { - return ValueType.VALUE_ANNOTATION; - } - - public TypeIdItem getAnnotationType() { - return annotationType.getReference(); - } - - public List getAnnotationElements() { - return Collections.unmodifiableList(annotationElementList); - } - - public int compareTo(EncodedValueSubField t) { - int comp = getValueType().compareTo(t.getValueType()); - if (comp == 0) { - AnnotationEncodedValueSubField other = (AnnotationEncodedValueSubField)t; - - comp = annotationType.compareTo(other.annotationType); - if (comp == 0) { - comp = ((Integer)annotationElementList.size()).compareTo(other.annotationElementList.size()); - if (comp == 0) { - for (int i=0; i - implements EncodedValueSubField, Comparable -{ - - private final ArrayList encodedValues; - - public ArrayEncodedValueSubField(final DexFile dexFile) { - super("encoded_array"); - - encodedValues = new ArrayList(); - fields = new Field[] { - new ListSizeField(encodedValues, new Leb128Field("size")), - new FieldListField(encodedValues, "values") { - protected EncodedValue make() { - return new EncodedValue(dexFile); - } - } - }; - } - - public ArrayEncodedValueSubField(final DexFile dexFile, ArrayList encodedValues) { - super("encoded_array"); - - this.encodedValues = encodedValues; - - fields = new Field[] { - new ListSizeField(this.encodedValues, new Leb128Field("size")), - new FieldListField(encodedValues, "values") { - protected EncodedValue make() { - return new EncodedValue(dexFile); - } - } - }; - } - - public void setInitialValueArg(byte valueArg) { - //valueArg is ignored for arrays - } - - public byte getValueArg() { - return 0; - } - - public ValueType getValueType() { - return ValueType.VALUE_ARRAY; - } - - public void add(int index, EncodedValue encodedValue) { - encodedValues.add(index, encodedValue); - } - - public List getValues() { - return Collections.unmodifiableList(encodedValues); - } - - public int compareTo(EncodedValueSubField t) { - int comp = getValueType().compareTo(t.getValueType()); - if (comp == 0) { - ArrayEncodedValueSubField other = (ArrayEncodedValueSubField)t; - - comp = ((Integer)encodedValues.size()).compareTo(other.encodedValues.size()); - if (comp == 0) { - for (int i=0; i -{ - public BoolEncodedValueSubField() { - } - - public BoolEncodedValueSubField(boolean value) { - this.value = value; - } - - public void writeTo(AnnotatedOutput out) { - } - - public void readFrom(Input in) { - value = (valueArg != 0); - } - - public int place(int offset) { - return offset; - } - - public byte getValueArg() { - return (byte)(value?1:0); - } - - public ValueType getValueType() { - return ValueType.VALUE_BOOLEAN; - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/EncodedValue/ByteEncodedValueSubField.java b/dexlib/src/main/java/org/jf/dexlib/EncodedValue/ByteEncodedValueSubField.java deleted file mode 100644 index 9b7adf4c..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/EncodedValue/ByteEncodedValueSubField.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.EncodedValue; - -import org.jf.dexlib.Util.AnnotatedOutput; -import org.jf.dexlib.Util.Input; - -public class ByteEncodedValueSubField - extends SimpleEncodedValueSubField -{ - public ByteEncodedValueSubField() { - } - - public ByteEncodedValueSubField(byte value) { - this.value = value; - } - - public void writeTo(AnnotatedOutput out) { - out.annotate(1, "byte" + Integer.toHexString(value) + " " + Integer.toString(value)); - out.writeByte(value); - } - - public void readFrom(Input in) { - value = in.readByte(); - } - - public int place(int offset) { - return offset + 1; - } - - public byte getValueArg() { - return 0; - } - - public ValueType getValueType() { - return ValueType.VALUE_BYTE; - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/EncodedValue/CharEncodedValueSubField.java b/dexlib/src/main/java/org/jf/dexlib/EncodedValue/CharEncodedValueSubField.java deleted file mode 100644 index 2c309855..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/EncodedValue/CharEncodedValueSubField.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.EncodedValue; - -import org.jf.dexlib.Util.AnnotatedOutput; -import org.jf.dexlib.Util.EncodedValueUtils; -import org.jf.dexlib.Util.Input; - -public class CharEncodedValueSubField - extends SimpleEncodedValueSubField -{ - public CharEncodedValueSubField() { - } - - public CharEncodedValueSubField(char value) { - this.value = value; - } - - public void writeTo(AnnotatedOutput out) { - byte[] bytes = EncodedValueUtils.encodeUnsignedIntegralValue(value); - out.annotate(bytes.length, "CharEncodedValueSubField"); - out.write(bytes); - } - - public void readFrom(Input in) { - value = (char)EncodedValueUtils.decodeUnsignedIntegralValue( - in.readBytes(valueArg + 1)); - } - - public int place(int offset) { - return offset + EncodedValueUtils.getRequiredBytesForUnsignedIntegralValue(value); - } - - public byte getValueArg() { - return (byte)(EncodedValueUtils.getRequiredBytesForUnsignedIntegralValue(value) - 1); - } - - public ValueType getValueType() { - return ValueType.VALUE_CHAR; - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/EncodedValue/DoubleEncodedValueSubField.java b/dexlib/src/main/java/org/jf/dexlib/EncodedValue/DoubleEncodedValueSubField.java deleted file mode 100644 index 44f83204..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/EncodedValue/DoubleEncodedValueSubField.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.EncodedValue; - -import org.jf.dexlib.Util.AnnotatedOutput; -import org.jf.dexlib.Util.EncodedValueUtils; -import org.jf.dexlib.Util.Input; - -public class DoubleEncodedValueSubField - extends SimpleEncodedValueSubField -{ - public DoubleEncodedValueSubField() { - } - - public DoubleEncodedValueSubField(double value) { - this.value = value; - } - - public void writeTo(AnnotatedOutput out) { - byte[] bytes = EncodedValueUtils.encodeRightZeroExtendedValue(Double.doubleToLongBits(value)); - out.annotate(bytes.length, "DoubleEncodedValueSubField"); - out.write(bytes); - } - - public void readFrom(Input in) { - value = Double.longBitsToDouble(EncodedValueUtils.decodeRightZeroExtendedValue( - in.readBytes(valueArg + 1))); - } - - public int place(int offset) { - return offset + EncodedValueUtils.getRequiredBytesForRightZeroExtendedValue( - Double.doubleToLongBits(value)); - } - - public byte getValueArg() { - return (byte)(EncodedValueUtils.getRequiredBytesForRightZeroExtendedValue( - Double.doubleToLongBits(value)) - 1); - } - - public ValueType getValueType() { - return ValueType.VALUE_DOUBLE; - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/EncodedValue/EncodedIndexedItemReference.java b/dexlib/src/main/java/org/jf/dexlib/EncodedValue/EncodedIndexedItemReference.java deleted file mode 100644 index c6726961..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/EncodedValue/EncodedIndexedItemReference.java +++ /dev/null @@ -1,150 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.EncodedValue; - -import org.jf.dexlib.*; -import org.jf.dexlib.Util.AnnotatedOutput; -import org.jf.dexlib.Util.EncodedValueUtils; -import org.jf.dexlib.Util.Input; - -public class EncodedIndexedItemReference> - implements EncodedValueSubField>, Comparable { - private int initialValueArg; - private ValueType valueType; - - private T item = null; - private IndexedSection section; - - public EncodedIndexedItemReference(IndexedSection section, ValueType valueType) { - this.valueType = valueType; - this.section = section; - } - - public EncodedIndexedItemReference(DexFile dexFile, T item) { - this(dexFile, item, false); - } - - public EncodedIndexedItemReference(DexFile dexFile, T item, boolean isEnum) { - if (item.getClass() == StringIdItem.class) { - valueType = ValueType.VALUE_STRING; - } else if (item.getClass() == TypeIdItem.class) { - valueType = ValueType.VALUE_TYPE; - } else if (item.getClass() == FieldIdItem.class) { - if (isEnum) { - valueType = ValueType.VALUE_ENUM; - } else { - valueType = ValueType.VALUE_FIELD; - } - } else if (item.getClass() == MethodIdItem.class) { - valueType = ValueType.VALUE_METHOD; - } - this.item = item; - } - - public void writeTo(AnnotatedOutput out) { - - if (!item.isPlaced()) { - throw new RuntimeException("Trying to write a reference to an item that hasn't been placed."); - } - - byte[] bytes = EncodedValueUtils.encodeUnsignedIntegralValue(item.getIndex()); - - if (item != null) { - out.annotate(bytes.length, item.getItemType().getTypeName() + " reference"); - } else { - out.annotate(bytes.length, "null reference"); - } - - out.write(bytes); - } - - public void readFrom(Input in) { - item = section.getByIndex( - (int)EncodedValueUtils.decodeUnsignedIntegralValue(in.readBytes(initialValueArg + 1))); - } - - public int place(int offset) { - if (!item.isPlaced()) { - throw new RuntimeException("Trying to place a reference to an item that hasn't been placed."); - } - return offset + EncodedValueUtils.getRequiredBytesForUnsignedIntegralValue(item.getIndex()); - } - - public void copyTo(DexFile dexFile, EncodedIndexedItemReference copy) { - if (item == null) { - return; - } - copy.item = copy.section.intern(item); - } - - public T getValue() { - return item; - } - - public void setInitialValueArg(byte valueArg) - { - initialValueArg = valueArg; - } - - public byte getValueArg() { - return (byte)(EncodedValueUtils.getRequiredBytesForUnsignedIntegralValue(item.getIndex()) - 1); - } - - public ValueType getValueType() { - return valueType; - } - - public int hashCode() { - if (item == null) { - return 0; - } - return item.hashCode(); - } - - public boolean equals(Object o) { - if (!(o instanceof EncodedIndexedItemReference)) { - return false; - } - - EncodedIndexedItemReference other = (EncodedIndexedItemReference)o; - if (item != null) { - return item.equals(other.item); - } else { - return other.item == null; - } - } - - public int compareTo(EncodedValueSubField t) { - int comp = getValueType().compareTo(t.getValueType()); - if (comp == 0) { - return item.compareTo(((EncodedIndexedItemReference)t).item); - } - return comp; - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/EncodedValue/EncodedValue.java b/dexlib/src/main/java/org/jf/dexlib/EncodedValue/EncodedValue.java deleted file mode 100644 index 4919fd67..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/EncodedValue/EncodedValue.java +++ /dev/null @@ -1,193 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.EncodedValue; - -import org.jf.dexlib.CompositeField; -import org.jf.dexlib.DexFile; -import org.jf.dexlib.Field; -import org.jf.dexlib.Util.AnnotatedOutput; -import org.jf.dexlib.Util.Input; - -public class EncodedValue extends CompositeField implements Comparable { - private class ValueTypeArgField implements Field { - private ValueType valueType; - private byte valueArg; - - public ValueTypeArgField() { - } - - public ValueTypeArgField(ValueType valueType) { - this.valueType = valueType; - } - - public void writeTo(AnnotatedOutput out) { - out.annotate(1, "valuetype=" + Integer.toString(valueType.getMapValue()) + " valueArg=" + Integer.toString(valueArg)); - byte value = (byte)(valueType.getMapValue() | (valueArg << 5)); - out.writeByte(value); - } - - public void readFrom(Input in) { - byte value = in.readByte(); - valueType = ValueType.fromByte((byte)(value & 0x1F)); - valueArg = (byte)((value & 0xFF) >>> 5); - } - - public int place(int offset) { - return offset + 1; - } - - public ValueType getValueType() { - return valueType; - } - - public byte getValueArg() { - return valueArg; - } - - public void copyTo(DexFile dexFile, ValueTypeArgField copy) { - copy.valueType = valueType; - copy.valueArg = valueArg; - } - - public int hashCode() { - return valueType.hashCode() * 31 + ((Byte)valueArg).hashCode(); - } - - public boolean equals(Object o) { - if (!(o instanceof ValueTypeArgField)) { - return false; - } - - ValueTypeArgField other = (ValueTypeArgField)o; - return valueType.equals(other.valueType) && (valueArg == other.valueArg); - } - } - - private class EncodedValueSubFieldWrapper - implements Field, Comparable { - private final DexFile dexFile; - private EncodedValueSubField subField; - - public EncodedValueSubFieldWrapper(DexFile dexFile) { - this.dexFile = dexFile; - } - - public EncodedValueSubFieldWrapper(DexFile dexFile, EncodedValueSubField subField) { - this.dexFile = dexFile; - this.subField = subField; - } - - public void writeTo(AnnotatedOutput out) { - subField.writeTo(out); - } - - public void readFrom(Input in) { - subField = EncodedValueSubFieldFactory.makeEncodedValueField(dexFile, getValueType()); - subField.setInitialValueArg(getValueArg()); - subField.readFrom(in); - } - - public int place(int offset) { - return subField.place(offset); - } - - public EncodedValueSubField getEncodedValueSubField() { - return subField; - } - - public void copyTo(DexFile dexFile, EncodedValueSubFieldWrapper copy) { - EncodedValueSubField fieldCopy = EncodedValueSubFieldFactory.makeEncodedValueField(dexFile, getValueType()); - copy.subField = fieldCopy; - - //both fields should be the same type because they were both made with the a call to - //EncodedValueSubFieldFactory.makeEncodedValueField using the same value type. - subField.copyTo(dexFile, fieldCopy); - } - - public int hashCode() { - return subField.hashCode(); - } - - public boolean equals(Object o) { - if (!(o instanceof EncodedValueSubFieldWrapper)) { - return false; - } - - EncodedValueSubFieldWrapper other = (EncodedValueSubFieldWrapper)o; - return subField.equals(other.subField); - } - - public int compareTo(EncodedValueSubFieldWrapper encodedValueSubFieldWrapper) { - return subField.compareTo(encodedValueSubFieldWrapper.subField); - } - } - - private final ValueTypeArgField valueTypeArg; - private final EncodedValueSubFieldWrapper encodedValue; - - public EncodedValue(final DexFile dexFile) { - super("encoded_value"); - fields = new Field[] { - valueTypeArg = new ValueTypeArgField(), - encodedValue = new EncodedValueSubFieldWrapper(dexFile) - }; - } - - public EncodedValue(final DexFile dexFile, EncodedValueSubField subField) { - super("encoded_value"); - fields = new Field[] { - valueTypeArg = new ValueTypeArgField(subField.getValueType()), - encodedValue = new EncodedValueSubFieldWrapper(dexFile, subField) - }; - } - - public int place(int offset) { - offset = valueTypeArg.place(offset); - int ret = encodedValue.place(offset); - - valueTypeArg.valueArg = encodedValue.getEncodedValueSubField().getValueArg(); - return ret; - } - - public ValueType getValueType() { - return valueTypeArg.getValueType(); - } - - public byte getValueArg() { - return valueTypeArg.getValueArg(); - } - - public EncodedValueSubField getValue() { - return encodedValue.getEncodedValueSubField(); - } - - public int compareTo(EncodedValue encodedValue) { - return this.encodedValue.compareTo(encodedValue.encodedValue); - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/EncodedValue/EncodedValueSubField.java b/dexlib/src/main/java/org/jf/dexlib/EncodedValue/EncodedValueSubField.java deleted file mode 100644 index 78af5cb4..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/EncodedValue/EncodedValueSubField.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.EncodedValue; - -import org.jf.dexlib.Field; - -public interface EncodedValueSubField extends Field -{ - public void setInitialValueArg(byte valueArg); - public byte getValueArg(); - public ValueType getValueType(); - public int compareTo(EncodedValueSubField encodedValueSubField); -} diff --git a/dexlib/src/main/java/org/jf/dexlib/EncodedValue/EncodedValueSubFieldFactory.java b/dexlib/src/main/java/org/jf/dexlib/EncodedValue/EncodedValueSubFieldFactory.java deleted file mode 100644 index 3aefd573..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/EncodedValue/EncodedValueSubFieldFactory.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.EncodedValue; - -import org.jf.dexlib.*; - - -public abstract class EncodedValueSubFieldFactory -{ - public static EncodedValueSubField makeEncodedValueField(DexFile dexFile, ValueType valueType) { - switch (valueType) { - case VALUE_NULL: - return new NullEncodedValueSubField(); - case VALUE_BOOLEAN: - return new BoolEncodedValueSubField(); - case VALUE_BYTE: - return new ByteEncodedValueSubField(); - case VALUE_CHAR: - return new CharEncodedValueSubField(); - case VALUE_SHORT: - return new ShortEncodedValueSubField(); - case VALUE_INT: - return new IntEncodedValueSubField(); - case VALUE_LONG: - return new LongEncodedValueSubField(); - case VALUE_FLOAT: - return new FloatEncodedValueSubField(); - case VALUE_DOUBLE: - return new DoubleEncodedValueSubField(); - case VALUE_STRING: - return new EncodedIndexedItemReference(dexFile.StringIdsSection, valueType); - case VALUE_TYPE: - return new EncodedIndexedItemReference(dexFile.TypeIdsSection, valueType); - case VALUE_FIELD: - return new EncodedIndexedItemReference(dexFile.FieldIdsSection, valueType); - case VALUE_ENUM: - return new EncodedIndexedItemReference(dexFile.FieldIdsSection, valueType); - case VALUE_METHOD: - return new EncodedIndexedItemReference(dexFile.MethodIdsSection, valueType); - case VALUE_ARRAY: - return new ArrayEncodedValueSubField(dexFile); - case VALUE_ANNOTATION: - return new AnnotationEncodedValueSubField(dexFile); - default: - throw new RuntimeException("Invalid ValueType"); - } - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/EncodedValue/FloatEncodedValueSubField.java b/dexlib/src/main/java/org/jf/dexlib/EncodedValue/FloatEncodedValueSubField.java deleted file mode 100644 index f9f7e6b7..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/EncodedValue/FloatEncodedValueSubField.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.EncodedValue; - -import org.jf.dexlib.Util.AnnotatedOutput; -import org.jf.dexlib.Util.EncodedValueUtils; -import org.jf.dexlib.Util.Input; - -public class FloatEncodedValueSubField - extends SimpleEncodedValueSubField -{ - public FloatEncodedValueSubField() { - } - - public FloatEncodedValueSubField(float value) { - this.value = value; - } - public void writeTo(AnnotatedOutput out) { - byte[] bytes = EncodedValueUtils.encodeRightZeroExtendedValue(((long)Float.floatToIntBits(value)) << 32); - out.annotate(bytes.length, "FloatEncodedValueSubField"); - out.write(bytes); - } - - public void readFrom(Input in) { - long longValue = EncodedValueUtils.decodeRightZeroExtendedValue(in.readBytes(valueArg + 1)); - value = Float.intBitsToFloat((int)((longValue >> 32) & 0xFFFFFFFF)); - } - - public int place(int offset) { - return offset + EncodedValueUtils.getRequiredBytesForRightZeroExtendedValue( - ((long)Float.floatToRawIntBits(value)) << 32); - } - - public byte getValueArg() { - return (byte)(EncodedValueUtils.getRequiredBytesForRightZeroExtendedValue( - ((long)Float.floatToRawIntBits(value)) << 32) - 1); - } - - public ValueType getValueType() { - return ValueType.VALUE_FLOAT; - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/EncodedValue/IntEncodedValueSubField.java b/dexlib/src/main/java/org/jf/dexlib/EncodedValue/IntEncodedValueSubField.java deleted file mode 100644 index c974c2c4..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/EncodedValue/IntEncodedValueSubField.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.EncodedValue; - -import org.jf.dexlib.Util.AnnotatedOutput; -import org.jf.dexlib.Util.EncodedValueUtils; -import org.jf.dexlib.Util.Input; - -public class IntEncodedValueSubField - extends SimpleEncodedValueSubField -{ - public IntEncodedValueSubField() { - return; - } - - public IntEncodedValueSubField(int value) { - this.value = value; - } - - public void writeTo(AnnotatedOutput out) { - byte[] bytes = EncodedValueUtils.encodeSignedIntegralValue(value); - out.annotate(bytes.length, "IntEncodedValueSubField"); - out.write(bytes); - } - - public void readFrom(Input in) { - value = (int)EncodedValueUtils.decodeSignedIntegralValue(in.readBytes(valueArg+1)); - } - - public int place(int offset) { - return offset + EncodedValueUtils.getRequiredBytesForSignedIntegralValue(value); - } - - public byte getValueArg() { - return (byte)(EncodedValueUtils.getRequiredBytesForSignedIntegralValue(value) - 1); - } - - public ValueType getValueType() { - return ValueType.VALUE_INT; - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/EncodedValue/LongEncodedValueSubField.java b/dexlib/src/main/java/org/jf/dexlib/EncodedValue/LongEncodedValueSubField.java deleted file mode 100644 index 6292517d..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/EncodedValue/LongEncodedValueSubField.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.EncodedValue; - -import org.jf.dexlib.Util.AnnotatedOutput; -import org.jf.dexlib.Util.EncodedValueUtils; -import org.jf.dexlib.Util.Input; - -public class LongEncodedValueSubField - extends SimpleEncodedValueSubField -{ - public LongEncodedValueSubField() { - } - - public LongEncodedValueSubField(long value) { - this.value = value; - } - - public void writeTo(AnnotatedOutput out) { - byte[] bytes = EncodedValueUtils.encodeSignedIntegralValue(value); - out.annotate(bytes.length, "LongEncodedValueSubField"); - out.write(bytes); - } - - public void readFrom(Input in) { - value = EncodedValueUtils.decodeSignedIntegralValue(in.readBytes(valueArg+1)); - } - - public int place(int offset) { - return offset + EncodedValueUtils.getRequiredBytesForSignedIntegralValue(value); - } - - public byte getValueArg() { - return (byte)(EncodedValueUtils.getRequiredBytesForSignedIntegralValue(value) - 1); - } - - public ValueType getValueType() { - return ValueType.VALUE_LONG; - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/EncodedValue/NullEncodedValueSubField.java b/dexlib/src/main/java/org/jf/dexlib/EncodedValue/NullEncodedValueSubField.java deleted file mode 100644 index aafbf70d..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/EncodedValue/NullEncodedValueSubField.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.EncodedValue; - -import org.jf.dexlib.Util.AnnotatedOutput; -import org.jf.dexlib.Util.Input; - -public class NullEncodedValueSubField - extends SimpleEncodedValueSubField -{ - public NullEncodedValueSubField() { - } - - public void writeTo(AnnotatedOutput out) { - } - - public void readFrom(Input in) { - value = null; - } - - public int place(int offset) { - return offset; - } - - public byte getValueArg() { - return 0; - } - - public ValueType getValueType() { - return ValueType.VALUE_NULL; - } - - public int compareTo(EncodedValueSubField t) { - return getValueType().compareTo(t.getValueType()); - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/EncodedValue/ShortEncodedValueSubField.java b/dexlib/src/main/java/org/jf/dexlib/EncodedValue/ShortEncodedValueSubField.java deleted file mode 100644 index 464604c9..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/EncodedValue/ShortEncodedValueSubField.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.EncodedValue; - -import org.jf.dexlib.Util.AnnotatedOutput; -import org.jf.dexlib.Util.EncodedValueUtils; -import org.jf.dexlib.Util.Input; - -public class ShortEncodedValueSubField - extends SimpleEncodedValueSubField -{ - public ShortEncodedValueSubField() { - } - - public ShortEncodedValueSubField(short value) { - this.value = value; - } - - public void writeTo(AnnotatedOutput out) { - byte[] bytes = EncodedValueUtils.encodeSignedIntegralValue(value); - out.annotate(bytes.length, "ShortEncodedValueSubField"); - out.write(bytes); - } - - public void readFrom(Input in) { - value = (short)EncodedValueUtils.decodeSignedIntegralValue(in.readBytes(valueArg+1)); - } - - public int place(int offset) { - return offset + EncodedValueUtils.getRequiredBytesForSignedIntegralValue(value); - } - - public byte getValueArg() { - return (byte)(EncodedValueUtils.getRequiredBytesForSignedIntegralValue(value) - 1); - } - - public ValueType getValueType() { - return ValueType.VALUE_SHORT; - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/EncodedValue/SimpleEncodedValueSubField.java b/dexlib/src/main/java/org/jf/dexlib/EncodedValue/SimpleEncodedValueSubField.java deleted file mode 100644 index 45d366f3..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/EncodedValue/SimpleEncodedValueSubField.java +++ /dev/null @@ -1,77 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.EncodedValue; - -import org.jf.dexlib.DexFile; - -public abstract class SimpleEncodedValueSubField, T extends SimpleEncodedValueSubField> - implements EncodedValueSubField, Comparable -{ - protected V value; - protected byte valueArg = 0; - - public V getValue() { - return value; - } - - public void setInitialValueArg(byte valueArg) { - this.valueArg = valueArg; - } - - public void copyTo(DexFile dexFile, T copy) { - copy.value = value; - } - - public int hashCode() { - if (value == null) { - return 0; - } - return value.hashCode(); - } - - public boolean equals(Object o) { - if (!(o instanceof SimpleEncodedValueSubField)) { - return false; - } - - SimpleEncodedValueSubField other = (SimpleEncodedValueSubField)o; - if (value == null) { - return other.value == null; - } - return value.equals(other.value); - } - - public int compareTo(EncodedValueSubField t) { - int comp = getValueType().compareTo(t.getValueType()); - if (comp == 0) { - return value.compareTo(((SimpleEncodedValueSubField)t).value); - } - return comp; - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/EncodedValue/ValueType.java b/dexlib/src/main/java/org/jf/dexlib/EncodedValue/ValueType.java deleted file mode 100644 index 609250ca..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/EncodedValue/ValueType.java +++ /dev/null @@ -1,87 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.EncodedValue; - -import java.util.TreeMap; - -public enum ValueType { - - VALUE_BYTE((byte) 0x00), - VALUE_SHORT((byte) 0x02), - VALUE_CHAR((byte) 0x03), - VALUE_INT((byte) 0x04), - VALUE_LONG((byte) 0x06), - VALUE_FLOAT((byte) 0x10), - VALUE_DOUBLE((byte) 0x11), - VALUE_STRING((byte) 0x17), - VALUE_TYPE((byte) 0x18), - VALUE_FIELD((byte) 0x19), - VALUE_METHOD((byte) 0x1a), - VALUE_ENUM((byte) 0x1b), - VALUE_ARRAY((byte) 0x1c), - VALUE_ANNOTATION((byte) 0x1d), - VALUE_NULL((byte) 0x1e), - VALUE_BOOLEAN((byte) 0x1f); - - /** - * A map to facilitate looking up an ItemType by ordinal - */ - private final static TreeMap valueTypeIntegerMap; - - /** builds the itemTypeIntegerMap object */ - static { - valueTypeIntegerMap = new TreeMap(); - - for (ValueType valueType : ValueType.values()) { - valueTypeIntegerMap.put(valueType.getMapValue(), valueType); - } - } - - private final byte mapValue; - - private ValueType(byte mapValue) { - this.mapValue = mapValue; - } - - public byte getMapValue() { - return mapValue; - } - - /** - * Converts a byte value to the corresponding ValueType enum value, - * or null if the value isn't a valid ValueType value - * - * @param valueType the int value to convert to a ValueType - * @return the ValueType enum value corresponding to valueType, or null - * if not a valid ValueType value - */ - public static ValueType fromByte(byte valueType) { - return valueTypeIntegerMap.get(valueType); - } -} \ No newline at end of file diff --git a/dexlib/src/main/java/org/jf/dexlib/Field.java b/dexlib/src/main/java/org/jf/dexlib/Field.java deleted file mode 100644 index abfa04c9..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Field.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib; - -import org.jf.dexlib.Util.AnnotatedOutput; -import org.jf.dexlib.Util.Input; - -public interface Field { - public void writeTo(AnnotatedOutput out); - public void readFrom(Input in); - public int place(int offset); - public void copyTo(DexFile dexFile, T copy); -} diff --git a/dexlib/src/main/java/org/jf/dexlib/FieldIdItem.java b/dexlib/src/main/java/org/jf/dexlib/FieldIdItem.java deleted file mode 100644 index 7564c5d1..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/FieldIdItem.java +++ /dev/null @@ -1,102 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib; - -public class FieldIdItem extends IndexedItem { - private final IndexedItemReference classTypeReferenceField; - private final IndexedItemReference fieldTypeReferenceField; - private final IndexedItemReference fieldNameReferenceField; - - public FieldIdItem(DexFile dexFile, int index) { - super(dexFile, index); - fields = new Field[] { - classTypeReferenceField = new IndexedItemReference(dexFile.TypeIdsSection, - new ShortIntegerField(null), "class_idx"), - fieldTypeReferenceField = new IndexedItemReference(dexFile.TypeIdsSection, - new ShortIntegerField(null), "type_idx"), - fieldNameReferenceField = new IndexedItemReference(dexFile.StringIdsSection, - new IntegerField(null), "name_idx") - }; - } - - public FieldIdItem(DexFile dexFile, TypeIdItem classType, StringIdItem fieldName, TypeIdItem fieldType) { - this(dexFile, -1); - classTypeReferenceField.setReference(classType); - fieldTypeReferenceField.setReference(fieldType); - fieldNameReferenceField.setReference(fieldName); - } - - public FieldIdItem(DexFile dexFile, TypeIdItem classType, String fieldName, TypeIdItem fieldType) { - this(dexFile, classType, new StringIdItem(dexFile, fieldName), fieldType); - } - - protected int getAlignment() { - return 4; - } - - public ItemType getItemType() { - return ItemType.TYPE_FIELD_ID_ITEM; - } - - public StringIdItem getFieldName() { - return fieldNameReferenceField.getReference(); - } - - public String getConciseIdentity() { - String parentClass = classTypeReferenceField.getReference().getTypeDescriptor(); - //strip off the leading L and trailing ; - parentClass = parentClass.substring(1, parentClass.length() - 1); - - return parentClass + "/" + fieldNameReferenceField.getReference().getStringValue() + - ":" + fieldTypeReferenceField.getReference().getTypeDescriptor(); - } - - public int compareTo(FieldIdItem o) { - int result = classTypeReferenceField.compareTo(o.classTypeReferenceField); - if (result != 0) { - return result; - } - - result = fieldNameReferenceField.compareTo(o.fieldNameReferenceField); - if (result != 0) { - return result; - } - - return fieldTypeReferenceField.compareTo(o.fieldTypeReferenceField); - - } - - public TypeIdItem getFieldType() { - return fieldTypeReferenceField.getReference(); - } - - public TypeIdItem getContainingClass() { - return classTypeReferenceField.getReference(); - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/FieldListField.java b/dexlib/src/main/java/org/jf/dexlib/FieldListField.java deleted file mode 100644 index c93cd400..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/FieldListField.java +++ /dev/null @@ -1,114 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib; - -import org.jf.dexlib.Util.AnnotatedOutput; -import org.jf.dexlib.Util.Input; - -import java.util.ArrayList; - -public abstract class FieldListField implements Field> { - final ArrayList list; - private final String fieldName; - - public FieldListField(ArrayList list, String fieldName) { - this.list = list; - this.fieldName = fieldName; - } - - public void writeTo(AnnotatedOutput out) { - if (list.size() > 0) { - int i=0; - for (Field field: list) { - out.annotate(0, fieldName + "[" + Integer.toString(i) + "]"); - out.indent(); - field.writeTo(out); - out.deindent(); - i++; - } - } - } - - public void readFrom(Input in) { - for (int i = 0; i < list.size(); i++) { - T field = list.get(i); - - if (field == null) { - field = make(); - list.set(i, field); - } - field.readFrom(in); - } - } - - protected abstract T make(); - - public int place(int offset) { - for (Field field: list) { - offset = field.place(offset); - } - return offset; - } - - public void copyTo(DexFile dexFile, FieldListField copy) { - copy.list.clear(); - copy.list.ensureCapacity(list.size()); - for (int i = 0; i < list.size(); i++) { - T fieldCopy = copy.make(); - list.get(i).copyTo(dexFile, fieldCopy); - copy.list.add(fieldCopy); - } - } - - public int hashCode() { - int h = 1; - for (int i = 0; i < list.size(); i++) { - h = h * 31 + list.get(i).hashCode(); - } - return h; - } - - public boolean equals(Object o) { - if (!(o instanceof FieldListField)) { - return false; - } - - FieldListField other = (FieldListField)o; - if (list.size() != other.list.size()) { - return false; - } - - for (int i = 0; i < list.size(); i++) { - if (!list.get(i).equals(other.list.get(i))) { - return false; - } - } - return true; - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/FixedSizeByteArrayField.java b/dexlib/src/main/java/org/jf/dexlib/FixedSizeByteArrayField.java deleted file mode 100644 index ccb4cf90..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/FixedSizeByteArrayField.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib; - -import org.jf.dexlib.Util.AnnotatedOutput; -import org.jf.dexlib.Util.Input; - -public class FixedSizeByteArrayField implements Field { - protected byte[] value; - private final String fieldName; - - public FixedSizeByteArrayField(int size, String fieldName) { - value = new byte[size]; - this.fieldName = fieldName; - } - - public FixedSizeByteArrayField(byte[] bytes, String fieldName) { - this.value = bytes.clone(); - this.fieldName = fieldName; - } - - public void writeTo(AnnotatedOutput out) { - if (fieldName != null) { - out.annotate(fieldName); - } - out.write(value); - } - - public void readFrom(Input in) { - in.read(value); - } - - public int place(int offset) { - return offset + value.length; - } - - public void copyTo(DexFile dexFile, FixedSizeByteArrayField copy) { - copy.value = value.clone(); - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/HeaderItem.java b/dexlib/src/main/java/org/jf/dexlib/HeaderItem.java deleted file mode 100644 index 120e56e4..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/HeaderItem.java +++ /dev/null @@ -1,168 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib; - -import org.jf.dexlib.Util.AnnotatedOutput; - -import java.io.UnsupportedEncodingException; - -public class HeaderItem extends IndexedItem { - /** - * non-null; the file format magic number, represented as the - * low-order bytes of a string - */ - private static final String MAGIC = "dex\n035" + '\0'; - - /** size of this section, in bytes */ - private static final int HEADER_SIZE = 0x70; - - /** the endianness tag */ - private static final int ENDIAN_TAG = 0x12345678; - - private final FixedSizeByteArrayField magicField; - private final IntegerField checksumField; - private final FixedSizeByteArrayField signatureField; - private final IntegerField fileSizeField; - private final IntegerField headerSizeField; - private final IntegerField endianTagField; - private final IntegerField linkSizeField; - private final IntegerField linkOffField; - private final IntegerField mapOffField; - private final SectionHeaderInfo StringIdsHeaderField; - private final SectionHeaderInfo TypeIdsHeaderField; - private final SectionHeaderInfo ProtoIdsHeaderField; - private final SectionHeaderInfo FieldIdsHeaderField; - private final SectionHeaderInfo MethodIdsHeaderField; - private final SectionHeaderInfo ClassDefsHeaderField; - private final IntegerField dataSizeField; - private final IntegerField dataOffField; - - public HeaderItem(final DexFile dexFile, int index) { - super(dexFile, index); - - try - { - fields = new Field[] { - magicField = new FixedSizeByteArrayField(MAGIC.getBytes("US-ASCII"), "magic"), - checksumField = new IntegerField("checksum") { - public void writeTo(AnnotatedOutput out) { - cacheValue(0); - super.writeTo(out); - } - }, - signatureField = new FixedSizeByteArrayField(20, "signature") { - public void writeTo(AnnotatedOutput out) { - for (int i = 0; i < value.length; i++) { - value[i] = 0; - } - super.writeTo(out); - } - }, - fileSizeField = new IntegerField("file_size"), - headerSizeField = new IntegerField(HEADER_SIZE,"header_size"), - endianTagField = new IntegerField(ENDIAN_TAG,"endian_tag"), - linkSizeField = new IntegerField(0,"link_size"), - linkOffField = new IntegerField(0,"link_off"), - mapOffField = new IntegerField("map_off") { - public void writeTo(AnnotatedOutput out) { - cacheValue(dexFile.MapSection.getOffset()); - super.writeTo(out); - } - }, - StringIdsHeaderField = new SectionHeaderInfo("string_ids") { - protected Section getSection() { - return dexFile.StringIdsSection; - } - }, - TypeIdsHeaderField = new SectionHeaderInfo("type_ids") { - protected Section getSection() { - return dexFile.TypeIdsSection; - } - }, - ProtoIdsHeaderField = new SectionHeaderInfo("proto_ids") { - protected Section getSection() { - return dexFile.ProtoIdsSection; - } - }, - FieldIdsHeaderField = new SectionHeaderInfo("field_ids") { - protected Section getSection() { - return dexFile.FieldIdsSection; - } - }, - MethodIdsHeaderField = new SectionHeaderInfo("method_ids") { - protected Section getSection() { - return dexFile.MethodIdsSection; - } - }, - ClassDefsHeaderField = new SectionHeaderInfo("class_defs") { - protected Section getSection() { - return dexFile.ClassDefsSection; - } - }, - dataSizeField = new IntegerField("data_size"), - dataOffField = new IntegerField("data_off") - }; - } catch (UnsupportedEncodingException ex) { - throw new RuntimeException("Error while creating the magic header field.", ex); - } - } - - public int getMapOffset() { - return mapOffField.getCachedValue(); - } - - protected int getAlignment() { - return 4; - } - - public ItemType getItemType() { - return ItemType.TYPE_HEADER_ITEM; - } - - public String getConciseIdentity() { - return "header_item"; - } - - public int compareTo(HeaderItem o) { - //there is only 1 header item - return 0; - } - - protected void setFileSize(int size) { - this.fileSizeField.cacheValue(size); - } - - protected void setDataSize(int size) { - this.dataSizeField.cacheValue(size); - } - - protected void setDataOffset(int offset) { - this.dataOffField.cacheValue(offset); - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/IndexedItem.java b/dexlib/src/main/java/org/jf/dexlib/IndexedItem.java deleted file mode 100644 index f075a52c..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/IndexedItem.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib; - -public abstract class IndexedItem extends Item { - protected IndexedItem(DexFile dexFile, int index) { - super(dexFile); - this.index = index; - } -} \ No newline at end of file diff --git a/dexlib/src/main/java/org/jf/dexlib/IndexedItemReference.java b/dexlib/src/main/java/org/jf/dexlib/IndexedItemReference.java deleted file mode 100644 index c606ef84..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/IndexedItemReference.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib; - -public class IndexedItemReference> extends - ItemReference> implements Comparable> { - - public IndexedItemReference(IndexedSection section, CachedIntegerValueField underlyingField, - String fieldName) { - super(section, underlyingField, fieldName); - } - - public IndexedItemReference(DexFile dexFile, T item, CachedIntegerValueField underlyingField, - String fieldName) { - super(dexFile, item, underlyingField, fieldName); - } - - public IndexedSection getSection() { - return (IndexedSection)super.getSection(); - } - - protected int getReferenceValue() { - T item = getReference(); - if (item == null) { - return -1; - } else { - return item.getIndex(); - } - } - - protected T getReferencedItem(int referenceValue) { - if (referenceValue == -1) { - return null; - } else { - return getSection().getByIndex(referenceValue); - } - } - - public int compareTo(IndexedItemReference o) { - return getReference().compareTo(o.getReference()); - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/IndexedSection.java b/dexlib/src/main/java/org/jf/dexlib/IndexedSection.java deleted file mode 100644 index 38e7cfee..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/IndexedSection.java +++ /dev/null @@ -1,88 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib; - -import org.jf.dexlib.Util.Input; - -import java.util.Collections; - -public abstract class IndexedSection> extends Section { - public IndexedSection(DexFile dexFile) { - super(dexFile); - } - - public T getByIndex(int index) { - for (int i = items.size(); i <= index; i++) { - items.add(null); - } - T item = items.get(index); - if (item == null) { - item = make(index); - items.set(index, item); - } - return item; - } - - public void readFrom(int size, Input in) { - super.setSize(size); - this.offset = in.getCursor(); - - for (int i = 0; i < size(); i++) { - T item = getByIndex(i); - item.readFrom(in, i); - in.alignTo(item.getAlignment()); - } - } - - protected abstract T make(int index); - - public T intern(T item) { - T itemToReturn = getInternedItem(item); - - if (itemToReturn == null) { - /** - * Make a new item at the end of this section, and copy the fields - * to the new item - */ - itemToReturn = getByIndex(size()); - item.copyTo(dexFile, itemToReturn); - uniqueItems.put(itemToReturn, itemToReturn); - } - - return itemToReturn; - } - - public int place(int offset) { - if (!dexFile.getInplace()) { - sortSection(); - } - - return super.place(offset); - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/IntegerField.java b/dexlib/src/main/java/org/jf/dexlib/IntegerField.java deleted file mode 100644 index ba4ac2f2..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/IntegerField.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib; - -import org.jf.dexlib.Util.Input; -import org.jf.dexlib.Util.Output; - -public class IntegerField extends CachedIntegerValueField { - public IntegerField(String fieldName) { - super(fieldName); - } - - public IntegerField(int value, String fieldName) { - super(value, fieldName); - } - - public void readFrom(Input in) { - value = in.readInt(); - } - - public int place(int offset) { - return offset + 4; - } - - protected void writeValue(Output out) { - out.writeInt(value); - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/Item.java b/dexlib/src/main/java/org/jf/dexlib/Item.java deleted file mode 100644 index b6ad9d3c..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Item.java +++ /dev/null @@ -1,148 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib; - -import org.jf.dexlib.Util.AnnotatedOutput; -import org.jf.dexlib.Util.Input; - -public abstract class Item implements Comparable { - protected int offset = -1; - protected int index = -1; - protected final DexFile dexFile; - - protected Field[] fields; - - protected Item(DexFile dexFile) { - this.dexFile = dexFile; - } - - public boolean isPlaced() { - return offset > -1; - } - - public int place(int index, int offset) { - offset = alignOffset(offset); - - this.index = index; - this.offset = offset; - - for (Field field: fields) { - offset = field.place(offset); - } - return offset; - } - - public void readFrom(Input in, int index) { - this.offset = in.getCursor(); - this.index = index; - for (Field field: fields) { - field.readFrom(in); - } - } - - public void writeTo(AnnotatedOutput out) { - out.alignTo(getAlignment()); - - out.annotate(0, "[0x" + Integer.toHexString(this.getIndex()) + "] " + this.getItemType().getTypeName() ); - out.indent(); - - if (out.getCursor() != offset) { - throw new RuntimeException("Item is being written somewhere other than where it was placed"); - } - for (Field field: fields) { - field.writeTo(out); - } - - out.deindent(); - } - - protected int getAlignment() { - return 1; - } - - protected int alignOffset(int offset) { - int mask = getAlignment() - 1; - - return (offset + mask) & ~mask; - } - - public int getOffset() { - return offset; - } - - public int getIndex() { - return index; - } - - public abstract ItemType getItemType(); - - public void copyTo(DexFile dexFile, T copy) { - for (int i = 0; i < fields.length; i++) { - fields[i].copyTo(dexFile, copy.fields[i]); - } - } - - public int hashCode() { - int h = 1; - for (Field field: fields) { - h = h*31 + field.hashCode(); - } - return h; - } - - public boolean equals(Object o) { - if (!this.getClass().isInstance(o)) { - return false; - } - - Item other = (Item)o; - - if (fields.length != other.fields.length) { - return false; - } - - for (int i = 0; i < fields.length; i++) { - if (!fields[i].equals(other.fields[i])) { - return false; - } - } - - return true; - } - - /** - * Returns a concise string value that conveys the identity of this item - * @return A concise string value that conveys the identity of this item - */ - public abstract String getConciseIdentity(); - - public String toString() { - return getConciseIdentity(); - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/ItemReference.java b/dexlib/src/main/java/org/jf/dexlib/ItemReference.java deleted file mode 100644 index 16e38bab..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/ItemReference.java +++ /dev/null @@ -1,140 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib; - -import org.jf.dexlib.Util.AnnotatedOutput; -import org.jf.dexlib.Util.Input; - -public abstract class ItemReference, S extends ItemReference> implements Field { - private T item = null; - private Section section; - private final CachedIntegerValueField underlyingField; - private final String fieldName; - - public ItemReference(Section section, CachedIntegerValueField underlyingField, String fieldName) { - this.section = section; - this.underlyingField = underlyingField; - this.fieldName = fieldName; - } - - public ItemReference(DexFile dexFile, T item, CachedIntegerValueField underlyingField, String fieldName) { - if (item != null) { - section = dexFile.getSectionForItem(item); - this.item = item; - } - this.underlyingField = underlyingField; - this.fieldName = fieldName; - } - - public T getReference() { - return item; - } - - public void setReference(T item) { - this.item = item; - } - - public Section getSection() { - return section; - } - - public void copyTo(DexFile dexFile, S copy) { - T referencedItem = getReference(); - if (referencedItem == null) { - return; - } - Section section = copy.getSection(); - T copiedItem = section.intern(referencedItem); - copy.setReference(copiedItem); - } - - public void writeTo(AnnotatedOutput out) { - T item = getReference(); - - if (item != null) { - if (!item.isPlaced()) { - throw new RuntimeException("Trying to write reference to an item that hasn't been placed."); - } - } - - //in some cases, we have to re-cache the value here, because the exact offset wasn't known yet - //during placement. Luckily, all variable sized reference offsets (Leb128) are known at placement, - //so this won't change the size of anything - underlyingField.cacheValue(getReferenceValue()); - - String annotation = fieldName + ": 0x" + Integer.toHexString(underlyingField.getCachedValue()); - if (item != null) { - annotation += " (" + item.getConciseIdentity() + ")"; - } - - out.annotate(annotation); - underlyingField.writeTo(out); - } - - public void readFrom(Input in) { - underlyingField.readFrom(in); - setReference(getReferencedItem(underlyingField.getCachedValue())); - } - - public int hashCode() { - if (item == null) { - return 0; - } - return item.hashCode(); - } - - public boolean equals(Object o) { - if (!(o instanceof ItemReference)) { - return false; - } - - ItemReference other = (ItemReference)o; - if (item != null) { - return item.equals(other.item); - } else { - return other.item == null; - } - } - - public int place(int offset) { - /** - * We need to cache the reference value so that the underlying field can calculate its length. - * Sometimes this value isn't correct, but the order that items are placed is such that - * the value is correct for any references using a variable sized field (i.e. leb128) to - * store the reference. For non-variable sized fields, it doesn't matter if the value is - * correct or not. We'll get the correct value for these in writeTo(), just before it is - * written - */ - underlyingField.cacheValue(getReferenceValue()); - return underlyingField.place(offset); - } - - protected abstract int getReferenceValue(); - protected abstract T getReferencedItem(int referenceValue); -} diff --git a/dexlib/src/main/java/org/jf/dexlib/ItemType.java b/dexlib/src/main/java/org/jf/dexlib/ItemType.java deleted file mode 100644 index 60fe6cea..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/ItemType.java +++ /dev/null @@ -1,102 +0,0 @@ -/* - * Copyright (C) 2008 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jf.dexlib; - -import java.util.TreeMap; - -/** - * Enumeration of all the top-level item types. - */ -public enum ItemType { - TYPE_HEADER_ITEM( 0x0000, "header_item"), - TYPE_STRING_ID_ITEM( 0x0001, "string_id_item"), - TYPE_TYPE_ID_ITEM( 0x0002, "type_id_item"), - TYPE_PROTO_ID_ITEM( 0x0003, "proto_id_item"), - TYPE_FIELD_ID_ITEM( 0x0004, "field_id_item"), - TYPE_METHOD_ID_ITEM( 0x0005, "method_id_item"), - TYPE_CLASS_DEF_ITEM( 0x0006, "class_def_item"), - TYPE_MAP_LIST( 0x1000, "map_list"), - TYPE_TYPE_LIST( 0x1001, "type_list"), - TYPE_ANNOTATION_SET_REF_LIST( 0x1002, "annotation_set_ref_list"), - TYPE_ANNOTATION_SET_ITEM( 0x1003, "annotation_set_item"), - TYPE_CLASS_DATA_ITEM( 0x2000, "class_data_item"), - TYPE_CODE_ITEM( 0x2001, "code_item"), - TYPE_STRING_DATA_ITEM( 0x2002, "string_data_item"), - TYPE_DEBUG_INFO_ITEM( 0x2003, "debug_info_item"), - TYPE_ANNOTATION_ITEM( 0x2004, "annotation_item"), - TYPE_ENCODED_ARRAY_ITEM( 0x2005, "encoded_array_item"), - TYPE_ANNOTATIONS_DIRECTORY_ITEM(0x2006, "annotations_directory_item"); - - /** A map to facilitate looking up an ItemType by ordinal */ - private final static TreeMap itemTypeIntegerMap; - - /** builds the itemTypeIntegerMap object */ - static { - itemTypeIntegerMap = new TreeMap(); - - for (ItemType itemType: ItemType.values()) { - itemTypeIntegerMap.put(itemType.getMapValue(), itemType); - } - } - - /** value when represented in a MapItem */ - private final int mapValue; - - /** non-null; name of the type */ - private final String typeName; - - /** - * Constructs an instance. - * - * @param mapValue value when represented in a MapItem - * @param typeName non-null; name of the type - */ - private ItemType(int mapValue, String typeName) { - this.mapValue = mapValue; - this.typeName = typeName; - } - - /** - * Gets the map value. - * - * @return the map value - */ - public int getMapValue() { - return mapValue; - } - - /** - * Gets the type name. - * - * @return non-null; the type name - */ - public String getTypeName() { - return typeName; - } - - /** - * Converts an int value to the corresponding ItemType enum value, - * or null if the value isn't a valid ItemType value - * - * @param itemType the int value to convert to an ItemType - * @return the ItemType enum value corresponding to itemType, or null - * if not a valid ItemType value - */ - public static ItemType fromInt(int itemType) { - return itemTypeIntegerMap.get(itemType); - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/Leb128Field.java b/dexlib/src/main/java/org/jf/dexlib/Leb128Field.java deleted file mode 100644 index 59e2c670..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Leb128Field.java +++ /dev/null @@ -1,95 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib; - -import org.jf.dexlib.Util.Input; -import org.jf.dexlib.Util.Leb128Utils; -import org.jf.dexlib.Util.Output; - -public class Leb128Field extends CachedIntegerValueField { - public Leb128Field(String fieldName) { - super(fieldName); - } - - public Leb128Field(int value, String fieldName) { - super(value, fieldName); - } - - public void readFrom(Input in) { - value = in.readUnsignedLeb128(); - } - - public int place(int offset) { - return offset + Leb128Utils.unsignedLeb128Size(value); - } - - public void writeValue(Output out) { - out.writeUnsignedLeb128(value); - } - - /** - * dx had a bug where it would write registers in the debug - * info as signed leb 128 values instead of unsigned. This class - * is used when it is important to keep the same format as the - * file being read in - for example when the intent is to immediately - * write the file back out (typically for dumping/annotation purposes) - */ - public static class PossiblySignedLeb128Field extends Leb128Field { - private boolean signed = false; - - public PossiblySignedLeb128Field(String fieldName) { - super (fieldName); - } - - public void readFrom(Input in) { - int start = in.getCursor(); - value = in.readUnsignedLeb128(); - int end = in.getCursor(); - - if (Leb128Utils.unsignedLeb128Size(value) != (end - start)) { - signed = true; - } - } - - public int place(int offset) { - if (signed) { - return offset + Leb128Utils.signedLeb128Size(value); - } - return offset + Leb128Utils.unsignedLeb128Size(value); - } - - public void writeValue(Output out) { - if (signed) { - out.writeSignedLeb128(value); - } else { - out.writeUnsignedLeb128(value); - } - } - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/Leb128p1Field.java b/dexlib/src/main/java/org/jf/dexlib/Leb128p1Field.java deleted file mode 100644 index 963c9878..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Leb128p1Field.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib; - -import org.jf.dexlib.Util.Input; -import org.jf.dexlib.Util.Leb128Utils; -import org.jf.dexlib.Util.Output; - -public class Leb128p1Field extends CachedIntegerValueField { - public Leb128p1Field(String fieldName) { - super(fieldName); - } - - public Leb128p1Field(int value, String fieldName) { - super(value, fieldName); - } - - public void readFrom(Input in) { - value = in.readUnsignedLeb128() - 1; - } - - public int place(int offset) { - return offset + Leb128Utils.unsignedLeb128Size(value + 1); - } - - public void writeValue(Output out) { - out.writeUnsignedLeb128(value + 1); - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/ListSizeField.java b/dexlib/src/main/java/org/jf/dexlib/ListSizeField.java deleted file mode 100644 index 36e91b39..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/ListSizeField.java +++ /dev/null @@ -1,95 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib; - -import org.jf.dexlib.Util.AnnotatedOutput; -import org.jf.dexlib.Util.Input; - -import java.util.ArrayList; - -public class ListSizeField implements Field { - private final ArrayList list; - private final CachedIntegerValueField underlyingField; - - public ListSizeField(ArrayList list, CachedIntegerValueField underlyingField) { - this.list = list; - this.underlyingField = underlyingField; - } - - public void writeTo(AnnotatedOutput out) { - underlyingField.writeTo(out); - } - - public void readFrom(Input in) { - underlyingField.readFrom(in); - /** - * the absolute value operation is needed for the case when a list size is - * encoded as the absolute value of a signed integer - */ - int listSize = Math.abs(underlyingField.getCachedValue()); - - list.clear(); - list.ensureCapacity(listSize); - for (int i = 0; i < listSize; i++) { - list.add(null); - } - } - - public int place(int offset) { - underlyingField.cacheValue(list.size()); - return underlyingField.place(offset); - } - - public void copyTo(DexFile dexFile, ListSizeField copy) { - //nothing to do, the value is retrieved from the list - } - - public int getCachedValue() { - return underlyingField.getCachedValue(); - } - - public void cacheValue(int value) { - underlyingField.cacheValue(value); - } - - public int hashCode() { - //don't affect hash code calculations - return 0; - } - - public boolean equals(Object o) { - if (!(o instanceof ListSizeField)) { - return false; - } - - ListSizeField other = (ListSizeField)o; - - return list.size() == other.list.size(); - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/MapField.java b/dexlib/src/main/java/org/jf/dexlib/MapField.java deleted file mode 100644 index 3366aa3b..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/MapField.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib; - -public class MapField extends CompositeField { - private final ShortIntegerField sectionTypeField; - private final SectionHeaderInfo sectionInfoField; - - protected MapField(final DexFile dexFile) { - super("map_entry"); - fields = new Field[] { - //TODO: add an annotation for the item type - sectionTypeField = new ShortIntegerField("type"), - new ShortIntegerField((short)0, "padding"), - sectionInfoField = new SectionHeaderInfo("section") { - protected Section getSection() { - return dexFile.getSectionForType(getSectionItemType()); - } - } - }; - } - - protected MapField(final DexFile dexFile, short sectionType) { - this(dexFile); - sectionTypeField.cacheValue(sectionType); - } - - /** - * Get the ItemType of the section that this map field represents - * @return The ItemType of the section that this map field represents - */ - public ItemType getSectionItemType() { - return ItemType.fromInt(sectionTypeField.getCachedValue()); - } - - /** - * Get the Section object that this map field represents - * @return The Section object that this map field represents - */ - public Section getSection() { - Section s; - return sectionInfoField.getSection(); - } - - /** - * This returns the cached size of the section that this map field represents. This is used while - * reading in the given section, to retrieve the size of the section that is stored in this map - * field. - * - * @return the cached size of the section that this map field represents - */ - protected int getCachedSectionSize() { - return sectionInfoField.getSectionSize(); - } - - /** - * This returns the cached size of the section that this map field represents. This is used while - * reading in the given section, to retrieve the offset of the section that is stored in this map - * field - * @return - */ - protected int getCachedSectionOffset() { - return sectionInfoField.getSectionOffset(); - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/MapItem.java b/dexlib/src/main/java/org/jf/dexlib/MapItem.java deleted file mode 100644 index 04bf6cde..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/MapItem.java +++ /dev/null @@ -1,130 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; - -public class MapItem extends IndexedItem { - private ArrayList mapEntries = new ArrayList(); - - protected MapItem(final DexFile dexFile, int index) { - super(dexFile, index); - - fields = new Field[] { - new ListSizeField(mapEntries, new IntegerField("size")), - new FieldListField(mapEntries, "map_entry") { - protected MapField make() { - return new MapField(dexFile); - } - } - }; - } - - protected MapItem(final DexFile dexFile) { - this(dexFile, 0); - - mapEntries.add(new MapField(dexFile, (short)ItemType.TYPE_HEADER_ITEM.getMapValue())); - mapEntries.add(new MapField(dexFile, (short)ItemType.TYPE_STRING_ID_ITEM.getMapValue())); - mapEntries.add(new MapField(dexFile, (short)ItemType.TYPE_TYPE_ID_ITEM.getMapValue())); - mapEntries.add(new MapField(dexFile, (short)ItemType.TYPE_PROTO_ID_ITEM.getMapValue())); - mapEntries.add(new MapField(dexFile, (short)ItemType.TYPE_FIELD_ID_ITEM.getMapValue())); - mapEntries.add(new MapField(dexFile, (short)ItemType.TYPE_METHOD_ID_ITEM.getMapValue())); - mapEntries.add(new MapField(dexFile, (short)ItemType.TYPE_CLASS_DEF_ITEM.getMapValue())); - mapEntries.add(new MapField(dexFile, (short)ItemType.TYPE_STRING_DATA_ITEM.getMapValue())); - mapEntries.add(new MapField(dexFile, (short)ItemType.TYPE_ENCODED_ARRAY_ITEM.getMapValue())); - mapEntries.add(new MapField(dexFile, (short)ItemType.TYPE_ANNOTATION_ITEM.getMapValue())); - mapEntries.add(new MapField(dexFile, (short)ItemType.TYPE_ANNOTATION_SET_ITEM.getMapValue())); - mapEntries.add(new MapField(dexFile, (short)ItemType.TYPE_ANNOTATION_SET_REF_LIST.getMapValue())); - mapEntries.add(new MapField(dexFile, (short)ItemType.TYPE_ANNOTATIONS_DIRECTORY_ITEM.getMapValue())); - mapEntries.add(new MapField(dexFile, (short)ItemType.TYPE_TYPE_LIST.getMapValue())); - mapEntries.add(new MapField(dexFile, (short)ItemType.TYPE_DEBUG_INFO_ITEM.getMapValue())); - mapEntries.add(new MapField(dexFile, (short)ItemType.TYPE_CODE_ITEM.getMapValue())); - mapEntries.add(new MapField(dexFile, (short)ItemType.TYPE_CLASS_DATA_ITEM.getMapValue())); - mapEntries.add(new MapField(dexFile, (short)ItemType.TYPE_MAP_LIST.getMapValue())); - } - - public int place(int index, int offset) { - - //we have to check if there are any empty sections before we place this item, - //because we need to remove the empty sections, which will change the size of - //this item - for (int i=0; i() { - public int compare(MapField o1, MapField o2) { - return ((Integer)o1.getSection().getOffset()).compareTo(o2.getSection().getOffset()); - } - }); - - return offset; - } - - public MapField[] getMapEntries() { - return mapEntries.toArray(new MapField[1]); - } - - protected int getAlignment() { - return 4; - } - - public ItemType getItemType() { - return ItemType.TYPE_MAP_LIST; - } - - public void copyTo(DexFile dexFile, MapItem copy) { - //nothing to do - } - - public int hashCode() { - return 1; - } - - public boolean equals(Object o) { - return getClass() == o.getClass(); - } - - public String getConciseIdentity() { - return "map_item"; - } - - public int compareTo(MapItem o) { - //there is only 1 map item - return 1; - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/MethodIdItem.java b/dexlib/src/main/java/org/jf/dexlib/MethodIdItem.java deleted file mode 100644 index 388c188c..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/MethodIdItem.java +++ /dev/null @@ -1,115 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib; - -public class MethodIdItem extends IndexedItem { - private final IndexedItemReference classTypeReferenceField; - private final IndexedItemReference prototypeReferenceField; - private final IndexedItemReference methodNameReferenceField; - - public MethodIdItem(DexFile dexFile, int index) { - super(dexFile, index); - fields = new Field[] { - classTypeReferenceField = new IndexedItemReference(dexFile.TypeIdsSection, - new ShortIntegerField(null), "class_idx"), - prototypeReferenceField = new IndexedItemReference(dexFile.ProtoIdsSection, - new ShortIntegerField(null), "proto_idx"), - methodNameReferenceField = new IndexedItemReference(dexFile.StringIdsSection, - new IntegerField(null), "name_idx") - }; - } - - public MethodIdItem(DexFile dexFile, TypeIdItem classType, StringIdItem methodName, ProtoIdItem prototype) { - this(dexFile, -1); - classTypeReferenceField.setReference(classType); - prototypeReferenceField.setReference(prototype); - methodNameReferenceField.setReference(methodName); - } - - protected int getAlignment() { - return 4; - } - - public ItemType getItemType() { - return ItemType.TYPE_METHOD_ID_ITEM; - } - - public String getConciseIdentity() { - return "method_id_item: " + getMethodString(); - } - - private String cachedMethodString = null; - public String getMethodString() { - if (cachedMethodString == null) { - String parentClass = classTypeReferenceField.getReference().getTypeDescriptor(); - - cachedMethodString = parentClass + "->" + methodNameReferenceField.getReference().getStringValue() + - prototypeReferenceField.getReference().getPrototypeString(); - } - return cachedMethodString; - } - - public ProtoIdItem getPrototype() { - return prototypeReferenceField.getReference(); - } - - public StringIdItem getMethodName() { - return methodNameReferenceField.getReference(); - } - - public int getParameterRegisterCount(boolean isStatic) { - return prototypeReferenceField.getReference().getParameterRegisterCount() + (isStatic?0:1); - } - - public TypeIdItem getContainingClass() { - return classTypeReferenceField.getReference(); - } - - /** - * Return the number of parameters, not including the "this" parameter, if any - * @return The number of parameters, not including the "this" parameter, if any - */ - public int getParameterCount() { - return prototypeReferenceField.getReference().getParameterCount(); - } - - public int compareTo(MethodIdItem o) { - int result = classTypeReferenceField.compareTo(o.classTypeReferenceField); - if (result != 0) { - return result; - } - - result = methodNameReferenceField.compareTo(o.methodNameReferenceField); - if (result != 0) { - return result; - } - - return prototypeReferenceField.compareTo(o.prototypeReferenceField); - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/NullTerminatedByteArrayField.java b/dexlib/src/main/java/org/jf/dexlib/NullTerminatedByteArrayField.java deleted file mode 100644 index d5397690..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/NullTerminatedByteArrayField.java +++ /dev/null @@ -1,97 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib; - -import org.jf.dexlib.Util.AnnotatedOutput; -import org.jf.dexlib.Util.Input; - -public class NullTerminatedByteArrayField implements Field { - protected byte[] value; - private final String fieldName; - - public NullTerminatedByteArrayField(String fieldName) { - this.fieldName = fieldName; - } - - public NullTerminatedByteArrayField(byte[] value, String fieldName) { - this(fieldName); - this.value = value.clone(); - } - - public void writeTo(AnnotatedOutput out) { - out.annotate(fieldName); - out.write(value); - out.writeByte(0); - } - - public void readFrom(Input in) { - int startPosition = in.getCursor(); - while (in.readByte() != 0); - int size = in.getCursor() - startPosition - 1; - - in.setCursor(startPosition); - value = in.readBytes(size); - in.skipBytes(1); - } - - public int place(int offset) { - return offset + value.length + 1; - } - - public void copyTo(DexFile dexFile, NullTerminatedByteArrayField copy) { - copy.value = value; - } - - public int hashCode() { - int h=1; - for (int i = 0; i < value.length; i++) { - h = h*7 + value[i]; - } - return h; - } - - public boolean equals(Object o) { - if (!(o instanceof NullTerminatedByteArrayField)) { - return false; - } - - NullTerminatedByteArrayField other = (NullTerminatedByteArrayField)o; - if (value.length != other.value.length) { - return false; - } - - for (int i = 0; i < value.length; i++) { - if (value[i] != other.value[i]) { - return false; - } - } - - return true; - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/OffsettedItem.java b/dexlib/src/main/java/org/jf/dexlib/OffsettedItem.java deleted file mode 100644 index 637b9073..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/OffsettedItem.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib; - -public abstract class OffsettedItem> extends Item { - public OffsettedItem(DexFile dexFile, int offset) { - super(dexFile); - this.offset = offset; - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/OffsettedItemReference.java b/dexlib/src/main/java/org/jf/dexlib/OffsettedItemReference.java deleted file mode 100644 index 0c44cf71..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/OffsettedItemReference.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib; - -public class OffsettedItemReference> extends - ItemReference> { - - public OffsettedItemReference(DexFile dexFile, T item, CachedIntegerValueField underlyingField, - String fieldName) { - super(dexFile, item, underlyingField, fieldName); - } - - public OffsettedItemReference(OffsettedSection section, CachedIntegerValueField underlyingField, - String fieldName) { - super(section, underlyingField, fieldName); - } - - - public OffsettedSection getSection() { - return (OffsettedSection)super.getSection(); - } - - protected int getReferenceValue() { - T item = getReference(); - - if (item == null) { - return 0; - } else { - return item.getOffset(); - } - } - - protected T getReferencedItem(int referenceValue) { - if (referenceValue == 0) { - return null; - } - return getSection().getByOffset(referenceValue); - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/OffsettedSection.java b/dexlib/src/main/java/org/jf/dexlib/OffsettedSection.java deleted file mode 100644 index 29157448..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/OffsettedSection.java +++ /dev/null @@ -1,93 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.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; - - public OffsettedSection(DexFile dexFile) { - super(dexFile); - itemsByOffset = new HashMap(); - } - - /** - * Retrieves the item that starts at the given offset, or null if - * there are none found. This method is intended to only be used - * while reading in a dex file. - * @param offset the offset of the item to get - * @return the item that starts at the given offset, or null if there - * are none found - */ - public T getByOffset(int offset) { - T item = itemsByOffset.get(offset); - if (item == null) { - item = make(offset); - items.add(item); - itemsByOffset.put(offset, item); - } - return item; - } - - 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, i); - - 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); - - public T intern(T item) { - T itemToReturn = getInternedItem(item); - - if (itemToReturn == null) { - itemToReturn = make(-1); - items.add(itemToReturn); - item.copyTo(dexFile, itemToReturn); - uniqueItems.put(itemToReturn, itemToReturn); - } - - return itemToReturn; - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/ProtoIdItem.java b/dexlib/src/main/java/org/jf/dexlib/ProtoIdItem.java deleted file mode 100644 index ae06985c..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/ProtoIdItem.java +++ /dev/null @@ -1,135 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib; - -import java.util.ArrayList; - -public class ProtoIdItem extends IndexedItem { - private final IndexedItemReference shortyDescriptorReferenceField; - private final IndexedItemReference returnTypeReferenceField; - private final OffsettedItemReference parametersReferenceField; - - public ProtoIdItem(DexFile dexFile, int index) { - super(dexFile, index); - fields = new Field[] { - shortyDescriptorReferenceField = new IndexedItemReference(dexFile.StringIdsSection, - new IntegerField(null), "shorty_idx"), - returnTypeReferenceField = new IndexedItemReference(dexFile.TypeIdsSection, - new IntegerField(null), "return_type_idx"), - parametersReferenceField = new OffsettedItemReference(dexFile.TypeListsSection, - new IntegerField(null), "parameters_off") - }; - } - - public ProtoIdItem(DexFile dexFile, TypeIdItem returnType, ArrayList parameters) - { - this(dexFile, -1); - shortyDescriptorReferenceField.setReference( - new StringIdItem(dexFile, createShortyDescriptor(returnType, parameters))); - returnTypeReferenceField.setReference(returnType); - if (parameters != null && parameters.size() > 0) { - parametersReferenceField.setReference(new TypeListItem(dexFile, parameters)); - } - } - - private String createShortyDescriptor(TypeIdItem returnType, ArrayList parameters) { - StringBuilder sb = new StringBuilder(); - sb.append(returnType.toShorty()); - - if (parameters != null) { - for (TypeIdItem typeIdItem: parameters) { - sb.append(typeIdItem.toShorty()); - } - } - return sb.toString(); - } - - protected int getAlignment() { - return 4; - } - - public int getParameterRegisterCount() { - TypeListItem typeList = parametersReferenceField.getReference(); - if (typeList == null) { - return 0; - } else { - return typeList.getRegisterCount(); - } - } - - public int getParameterCount() { - TypeListItem typeList = parametersReferenceField.getReference(); - if (typeList == null) { - return 0; - } else { - return typeList.getTypeCount(); - } - } - - public ItemType getItemType() { - return ItemType.TYPE_PROTO_ID_ITEM; - } - - public int compareTo(ProtoIdItem o) { - int result = returnTypeReferenceField.compareTo(o.returnTypeReferenceField); - if (result != 0) { - return result; - } - - TypeListItem thisParameters = parametersReferenceField.getReference(); - if (thisParameters == null) { - return -1; - } - - return thisParameters.compareTo(o.parametersReferenceField.getReference()); - } - - public String getConciseIdentity() { - return "proto_id_item: " + getPrototypeString(); - } - - private String cachedPrototypeString = null; - public String getPrototypeString() { - if (cachedPrototypeString == null) { - StringBuilder sb = new StringBuilder(); - - TypeListItem parameterList = this.parametersReferenceField.getReference(); - - if (parameterList != null) { - for (TypeIdItem type: parameterList.getTypes()) { - sb.append(type.getTypeDescriptor()); - } - } - - cachedPrototypeString = "(" + sb.toString() + ")" + - this.returnTypeReferenceField.getReference().getTypeDescriptor(); - } - return cachedPrototypeString; - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/Section.java b/dexlib/src/main/java/org/jf/dexlib/Section.java deleted file mode 100644 index c513f49b..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Section.java +++ /dev/null @@ -1,142 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib; - -import org.jf.dexlib.Util.AnnotatedOutput; -import org.jf.dexlib.Util.Input; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Collections; - -public abstract class Section { - protected final ArrayList items; - protected HashMap uniqueItems = null; - protected final DexFile dexFile; - - /** - * When offset > -1, this section is "placed" at the specified offset. All - * items should have sequential indexes, and be placed appropriately. - * - * To unplace the section, set offset to -1, and set the offset of all - * items to -1 - */ - protected int offset = -1; - - public Section(DexFile dexFile) { - this.dexFile = dexFile; - items = new ArrayList(); - } - - public int place(int offset) { - this.offset = offset; - for (int i=0; i < items.size(); i++) { - T item = items.get(i); - if (item == null) { - throw new RuntimeException("This section contains a null item"); - } - offset = item.place(i, offset); - if (i == 0) { - /** - * if this item type has an alignment requirement, - * then item.getOffset() may be different than the - * offset that was passed in to this method, so we have - * to initialize the section offset to the actual - * (post-alignment) offset of the first item - */ - this.offset = item.getOffset(); - } - } - - return offset; - } - - public void writeTo(AnnotatedOutput out) { - for (int i = 0; i < size(); i++) { - T item = items.get(i); - if (item == null) { - throw new RuntimeException("Cannot write section because all items haven't been initialized"); - } - item.writeTo(out); - out.annotate(0, " "); - } - out.annotate(0, " "); - } - - public abstract void readFrom(int size, Input in); - - protected void setSize(int size) { - if (items.size() > size) { - throw new RuntimeException("There are references elsewhere to items in this section, that are " + - "beyond the end of the section"); - } - - items.ensureCapacity(size); - for (int i = items.size(); i < size; i++) { - items.add(null); - } - } - - public int size() { - return items.size(); - } - - public boolean isPlaced() { - return offset > -1; - } - - public int getOffset() { - return offset; - } - - public List getItems() { - return Collections.unmodifiableList(items); - } - - protected T getInternedItem(T item) { - if (uniqueItems == null) { - buildInternedItemMap(); - } - return uniqueItems.get(item); - } - - protected void buildInternedItemMap() { - uniqueItems = new HashMap(); - for (T item: items) { - uniqueItems.put(item, item); - } - } - - protected void sortSection() { - Collections.sort(items); - } - - public abstract T intern(T item); -} diff --git a/dexlib/src/main/java/org/jf/dexlib/SectionHeaderInfo.java b/dexlib/src/main/java/org/jf/dexlib/SectionHeaderInfo.java deleted file mode 100644 index 3417f91e..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/SectionHeaderInfo.java +++ /dev/null @@ -1,82 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib; - -import org.jf.dexlib.Util.AnnotatedOutput; - -public abstract class SectionHeaderInfo extends CompositeField { - private final String sectionName; - - private final IntegerField sectionSizeField; - private final IntegerField sectionOffsetField; - - - public SectionHeaderInfo(String sectionName) { - super(sectionName); - fields = new Field[] { - sectionSizeField = new IntegerField(sectionName + "_size"), - sectionOffsetField = new IntegerField(sectionName + "_off") - }; - this.sectionName = sectionName; - } - - protected abstract Section getSection(); - - public void writeTo(AnnotatedOutput out) { - Section section = getSection(); - - if (!section.isPlaced()) { - throw new RuntimeException("Trying to write a reference to a section that hasn't been placed."); - } - - int size = section.size(); - sectionSizeField.cacheValue(size); - - if (size == 0) { - //we have to set the offset to 0 or dalvik will complain - sectionOffsetField.cacheValue(0); - } else { - sectionOffsetField.cacheValue(section.getOffset()); - } - - super.writeTo(out); - } - - public int place(int offset) { - return super.place(offset); - } - - public int getSectionSize() { - return sectionSizeField.getCachedValue(); - } - - public int getSectionOffset() { - return sectionOffsetField.getCachedValue(); - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/ShortIntegerField.java b/dexlib/src/main/java/org/jf/dexlib/ShortIntegerField.java deleted file mode 100644 index 775a1f9b..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/ShortIntegerField.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib; - -import org.jf.dexlib.Util.Input; -import org.jf.dexlib.Util.Output; - -public class ShortIntegerField extends CachedIntegerValueField { - public ShortIntegerField(String fieldName) { - super(fieldName); - } - - public ShortIntegerField(int value, String fieldName) { - super(value, fieldName); - } - - public void readFrom(Input in) { - value = in.readShort(); - } - - public int place(int offset) { - return offset + 2; - } - - protected void writeValue(Output out) { - out.writeShort(value); - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/SignedLeb128Field.java b/dexlib/src/main/java/org/jf/dexlib/SignedLeb128Field.java deleted file mode 100644 index 825088ad..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/SignedLeb128Field.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib; - -import org.jf.dexlib.Util.Input; -import org.jf.dexlib.Util.Leb128Utils; -import org.jf.dexlib.Util.Output; - -public class SignedLeb128Field extends CachedIntegerValueField { - public SignedLeb128Field(String fieldName) { - super(fieldName); - } - - public SignedLeb128Field(int value, String fieldName) { - super(value, fieldName); - } - - public void readFrom(Input in) { - value = in.readSignedLeb128(); - } - - public int place(int offset) { - return offset + Leb128Utils.signedLeb128Size(value); - } - - public void writeValue(Output out) { - out.writeSignedLeb128(value); - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/StringDataItem.java b/dexlib/src/main/java/org/jf/dexlib/StringDataItem.java deleted file mode 100644 index 4d6b85f6..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/StringDataItem.java +++ /dev/null @@ -1,85 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib; - -import org.jf.dexlib.Util.ByteArray; -import org.jf.dexlib.Util.Utf8Utils; - -public class StringDataItem extends OffsettedItem implements Comparable { - private String value = null; - - private final Leb128Field stringSize; - private final NullTerminatedByteArrayField stringByteArray; - - public StringDataItem(DexFile dexFile, int offset) { - super(dexFile, offset); - - fields = new Field[] { - stringSize = new Leb128Field("string_length"), - stringByteArray = new NullTerminatedByteArrayField("data") - }; - } - - public StringDataItem(DexFile dexFile, String value) { - super(dexFile, -1); - - this.value = value; - - fields = new Field[] { - stringSize = new Leb128Field(value.length(), "string_length"), - stringByteArray = new NullTerminatedByteArrayField(Utf8Utils.stringToUtf8Bytes(value), "data") - }; - } - - public ItemType getItemType() { - return ItemType.TYPE_STRING_DATA_ITEM; - } - - public String getStringValue() { - if (value == null) { - value = Utf8Utils.utf8BytesToString(new ByteArray(((NullTerminatedByteArrayField)fields[1]).value)); - } - - return value; - } - - public byte[] getStringBytes() { - return stringByteArray.value; - } - - public String getConciseIdentity() { - return "string_data_item: " + Utf8Utils.escapeString(getStringValue()); - } - - public int compareTo(StringDataItem o) { - return getStringValue().compareTo(o.getStringValue()); - } - - -} diff --git a/dexlib/src/main/java/org/jf/dexlib/StringIdItem.java b/dexlib/src/main/java/org/jf/dexlib/StringIdItem.java deleted file mode 100644 index 6337f926..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/StringIdItem.java +++ /dev/null @@ -1,77 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib; - -import org.jf.dexlib.Util.Utf8Utils; - -public class StringIdItem extends IndexedItem { - private final OffsettedItemReference stringDataReferenceField; - - public StringIdItem(DexFile dexFile, int index) { - super(dexFile, index); - fields = new Field[] { - stringDataReferenceField = new OffsettedItemReference(dexFile.StringDataSection, - new IntegerField(null), "string_data_off") - }; - } - - public StringIdItem(DexFile dexFile, StringDataItem stringDataItem) { - this(dexFile, -1); - stringDataReferenceField.setReference(stringDataItem); - } - - public StringIdItem(DexFile dexFile, String value) { - this(dexFile, new StringDataItem(dexFile, value)); - } - - protected int getAlignment() { - return 4; - } - - public ItemType getItemType() { - return ItemType.TYPE_STRING_ID_ITEM; - } - - public String getConciseIdentity() { - return "string_id_item: " + Utf8Utils.escapeString(getStringValue()); - } - - public String getStringValue() { - return stringDataReferenceField.getReference().getStringValue(); - } - - public StringDataItem getStringData() { - return stringDataReferenceField.getReference(); - } - - public int compareTo(StringIdItem o) { - //sort by the string value - return getStringValue().compareTo(o.getStringValue()); - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/TypeIdItem.java b/dexlib/src/main/java/org/jf/dexlib/TypeIdItem.java deleted file mode 100644 index 6c3bfb3e..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/TypeIdItem.java +++ /dev/null @@ -1,96 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib; - -public class TypeIdItem extends IndexedItem { - private final IndexedItemReference typeDescriptorReferenceField; - - public TypeIdItem(DexFile dexFile, int index) { - super(dexFile, index); - fields = new Field[] { - typeDescriptorReferenceField = new IndexedItemReference(dexFile.StringIdsSection, - new IntegerField(null), "descriptor_idx") - }; - } - - public TypeIdItem(DexFile dexFile, StringIdItem stringIdItem) { - this(dexFile, -1); - typeDescriptorReferenceField.setReference(stringIdItem); - } - - public TypeIdItem(DexFile dexFile, String value) { - this(dexFile, new StringIdItem(dexFile, value)); - } - - protected int getAlignment() { - return 4; - } - - /** - * Returns the number of 2-byte registers that an instance of this type requires - * @return The number of 2-byte registers that an instance of this type requires - */ - public int getRegisterCount() { - String type = this.getTypeDescriptor(); - /** Only the long and double primitive types are 2 words, - * everything else is a single word - */ - if (type.equals("J") || type.equals("D")) { - return 2; - } else { - return 1; - } - } - - public ItemType getItemType() { - return ItemType.TYPE_TYPE_ID_ITEM; - } - - public String getConciseIdentity() { - return "type_id_item: " + getTypeDescriptor(); - } - - public String getTypeDescriptor() { - return typeDescriptorReferenceField.getReference().getStringValue(); - } - - public int compareTo(TypeIdItem o) { - //sort by the index of the StringIdItem - return typeDescriptorReferenceField.compareTo(o.typeDescriptorReferenceField); - } - - public String toShorty() { - String type = getTypeDescriptor(); - if (type.length() > 1) { - return "L"; - } else { - return type; - } - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/TypeListItem.java b/dexlib/src/main/java/org/jf/dexlib/TypeListItem.java deleted file mode 100644 index 0bc688c4..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/TypeListItem.java +++ /dev/null @@ -1,136 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib; - -import java.util.ArrayList; -import java.util.List; - -public class TypeListItem extends OffsettedItem { - private final ArrayList> typeList = new ArrayList>(); - - private final FieldListField> listField; - - public TypeListItem(final DexFile dexFile, int offset) { - super(dexFile, offset); - - fields = new Field[] { - new ListSizeField(typeList, new IntegerField("size")), - listField = new FieldListField>(typeList, "type_item") { - protected IndexedItemReference make() { - return new IndexedItemReference(dexFile.TypeIdsSection, - new ShortIntegerField(null), "type_idx"); - } - } - }; - } - - public TypeListItem(final DexFile dexFile, List types) { - this(dexFile, 0); - - for (TypeIdItem typeIdItem: types) { - IndexedItemReference typeReference = listField.make(); - typeReference.setReference(typeIdItem); - typeList.add(typeReference); - } - } - - //TODO: write a read only List wrapper for List> and return that instead - public List getTypes() { - ArrayList list = new ArrayList(typeList.size()); - - for (IndexedItemReference typeIdItemReference: typeList) { - list.add(typeIdItemReference.getReference()); - } - - return list; - } - - public int getRegisterCount() { - int wordCount = 0; - for (IndexedItemReference typeRef: typeList) { - TypeIdItem item = typeRef.getReference(); - wordCount += item.getRegisterCount(); - } - return wordCount; - } - - public int getTypeCount() { - return typeList.size(); - } - - public int getAlignment() { - return 4; - } - - public ItemType getItemType() { - return ItemType.TYPE_TYPE_LIST; - } - - public String getConciseIdentity() { - return "type_list: " + getTypeListString(); - } - - private String cachedTypeListString = null; - public String getTypeListString() { - if (cachedTypeListString == null) { - StringBuilder sb = new StringBuilder(); - - for (IndexedItemReference typeReference: typeList) { - sb.append(typeReference.getReference().getTypeDescriptor()); - } - cachedTypeListString = sb.toString(); - } - return cachedTypeListString; - } - - public int compareTo(TypeListItem o) { - if (o == null) { - return 1; - } - - int thisSize = typeList.size(); - int otherSize = o.typeList.size(); - int size = Math.min(thisSize, otherSize); - - for (int i = 0; i < size; i++) { - int result = typeList.get(i).compareTo(o.typeList.get(i)); - if (result != 0) { - return result; - } - } - - if (thisSize < otherSize) { - return -1; - } else if (thisSize > otherSize) { - return 1; - } else { - return 0; - } - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/Util/AccessFlags.java b/dexlib/src/main/java/org/jf/dexlib/Util/AccessFlags.java deleted file mode 100644 index 3efdea9f..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Util/AccessFlags.java +++ /dev/null @@ -1,125 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.Util; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; - -public enum AccessFlags -{ - PUBLIC(0x1, "public", true, true, true), - PRIVATE(0x2, "private", true, true, true), - PROTECTED(0x4, "protected", true, true, true), - STATIC(0x8, "static", true, true, true), - FINAL(0x10, "final", true, true, true), - SYNCHRONIZED(0x20, "synchronized", false, true, false), - VOLATILE(0x40, "volatile", false, false, true), - BRIDGE(0x40, "bridge", false, true, false), - TRANSIENT(0x80, "transient", false, false, true), - VARARGS(0x80, "varargs", false, true, false), - NATIVE(0x100, "native", false, true, false), - INTERFACE(0x200, "interface", true, false, false), - ABSTRACT(0x400, "abstract", true, true, false), - STRICTFP(0x800, "strictfp", false, true, false), - SYNTHETIC(0x1000, "synthetic", true, true, true), - ANNOTATION(0x2000, "annotation", true, false, false), - ENUM(0x4000, "enum", true, false, true), - CONSTRUCTOR(0x10000, "constructor", false, true, false), - DECLARED_SYNCHRONIZED(0x20000, "declared-synchronized", false, true, false); - - private int value; - private String accessFlagName; - private boolean validForClass; - private boolean validForMethod; - private boolean validForField; - - private static HashMap accessFlagsByName; - - static { - accessFlagsByName = new HashMap(); - for (AccessFlags accessFlag: AccessFlags.values()) { - accessFlagsByName.put(accessFlag.accessFlagName, accessFlag); - } - } - - private AccessFlags(int value, String accessFlagName, boolean validForClass, boolean validForMethod, - boolean validForField) { - this.value = value; - this.accessFlagName = accessFlagName; - this.validForClass = validForClass; - this.validForMethod = validForMethod; - this.validForField = validForField; - } - - public static List getAccessFlagsForClass(int accessFlagValue) { - ArrayList accessFlags = new ArrayList(); - - for (AccessFlags accessFlag: AccessFlags.values()) { - if (accessFlag.validForClass && (accessFlagValue & accessFlag.value) != 0) { - accessFlags.add(accessFlag); - } - } - return accessFlags; - } - - public static List getAccessFlagsForMethod(int accessFlagValue) { - ArrayList accessFlags = new ArrayList(); - - for (AccessFlags accessFlag: AccessFlags.values()) { - if (accessFlag.validForMethod && (accessFlagValue & accessFlag.value) != 0) { - accessFlags.add(accessFlag); - } - } - return accessFlags; - } - - public static List getAccessFlagsForField(int accessFlagValue) { - ArrayList accessFlags = new ArrayList(); - - for (AccessFlags accessFlag: AccessFlags.values()) { - if (accessFlag.validForField && (accessFlagValue & accessFlag.value) != 0) { - accessFlags.add(accessFlag); - } - } - return accessFlags; - } - - public static AccessFlags getAccessFlag(String accessFlag) { - return accessFlagsByName.get(accessFlag); - } - - public int getValue() { - return value; - } - - public String toString() { - return accessFlagName; - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/Util/AnnotatedOutput.java b/dexlib/src/main/java/org/jf/dexlib/Util/AnnotatedOutput.java deleted file mode 100644 index 41254d56..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Util/AnnotatedOutput.java +++ /dev/null @@ -1,83 +0,0 @@ -/* - * Copyright (C) 2007 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jf.dexlib.Util; - -/** - * Interface for a binary output destination that may be augmented - * with textual annotations. - */ -public interface AnnotatedOutput - extends Output { - /** - * Get whether this instance will actually keep annotations. - * - * @return true iff annotations are being kept - */ - public boolean annotates(); - - /** - * Get whether this instance is intended to keep verbose annotations. - * Annotators may use the result of calling this method to inform their - * annotation activity. - * - * @return true iff annotations are to be verbose - */ - public boolean isVerbose(); - - /** - * Add an annotation for the subsequent output. Any previously - * open annotation will be closed by this call, and the new - * annotation marks all subsequent output until another annotation - * call. - * - * @param msg non-null; the annotation message - */ - public void annotate(String msg); - - /** - * Add an annotation for a specified amount of subsequent - * output. Any previously open annotation will be closed by this - * call. If there is already pending annotation from one or more - * previous calls to this method, the new call "consumes" output - * after all the output covered by the previous calls. - * - * @param amt >= 0; the amount of output for this annotation to - * cover - * @param msg non-null; the annotation message - */ - public void annotate(int amt, String msg); - - /** - * End the most recent annotation. Subsequent output will be unannotated, - * until the next call to {@link #annotate}. - */ - public void endAnnotation(); - - /** - * Get the maximum width of the annotated output. This is advisory: - * Implementations of this interface are encouraged to deal with too-wide - * output, but annotaters are encouraged to attempt to avoid exceeding - * the indicated width. - * - * @return >= 1; the maximum width - */ - public int getAnnotationWidth(); - - public void setIndentAmount(int indentAmount); - public void indent(); - public void deindent(); -} diff --git a/dexlib/src/main/java/org/jf/dexlib/Util/ByteArray.java b/dexlib/src/main/java/org/jf/dexlib/Util/ByteArray.java deleted file mode 100644 index ff5fb947..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Util/ByteArray.java +++ /dev/null @@ -1,249 +0,0 @@ -/* - * Copyright (C) 2007 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jf.dexlib.Util; - -import java.io.DataInputStream; -import java.io.IOException; -import java.io.InputStream; - -/** - * Wrapper for a byte[], which provides read-only access and - * can "reveal" a partial slice of the underlying array. - * - * Note: Multibyte accessors all use big-endian order. - */ -public final class ByteArray { - /** non-null; underlying array */ - private final byte[] bytes; - - /** >= 0; start index of the slice (inclusive) */ - private final int start; - - /** >= 0, <= bytes.length; size computed as - * end - start (in the constructor) */ - private final int size; - - /** - * Constructs an instance. - * - * @param bytes non-null; the underlying array - * @param start >= 0; start index of the slice (inclusive) - * @param end >= start, <= bytes.length; end index of - * the slice (exclusive) - */ - public ByteArray(byte[] bytes, int start, int end) { - if (bytes == null) { - throw new NullPointerException("bytes == null"); - } - - if (start < 0) { - throw new IllegalArgumentException("start < 0"); - } - - if (end < start) { - throw new IllegalArgumentException("end < start"); - } - - if (end > bytes.length) { - throw new IllegalArgumentException("end > bytes.length"); - } - - this.bytes = bytes; - this.start = start; - this.size = end - start; - } - - /** - * Constructs an instance from an entire byte[]. - * - * @param bytes non-null; the underlying array - */ - public ByteArray(byte[] bytes) { - this(bytes, 0, bytes.length); - } - - /** - * Gets the size of the array, in bytes. - * - * @return >= 0; the size - */ - public int size() { - return size; - } - - /** - * Returns a slice (that is, a sub-array) of this instance. - * - * @param start >= 0; start index of the slice (inclusive) - * @param end >= start, <= size(); end index of - * the slice (exclusive) - * @return non-null; the slice - */ - public ByteArray slice(int start, int end) { - checkOffsets(start, end); - return new ByteArray(bytes, start + this.start, end + this.start); - } - - /** - * Returns the offset into the given array represented by the given - * offset into this instance. - * - * @param offset offset into this instance - * @param bytes non-null; (alleged) underlying array - * @return corresponding offset into bytes - * @throws IllegalArgumentException thrown if bytes is - * not the underlying array of this instance - */ - public int underlyingOffset(int offset, byte[] bytes) { - if (bytes != this.bytes) { - throw new IllegalArgumentException("wrong bytes"); - } - - return start + offset; - } - - /** - * Gets the signed byte value at a particular offset. - * - * @param off >= 0, < size(); offset to fetch - * @return signed byte at that offset - */ - public int getByte(int off) { - checkOffsets(off, off + 1); - return getByte0(off); - } - - /** - * Gets the signed short value at a particular offset. - * - * @param off >= 0, < (size() - 1); offset to fetch - * @return signed short at that offset - */ - public int getShort(int off) { - checkOffsets(off, off + 2); - return (getByte0(off) << 8) | getUnsignedByte0(off + 1); - } - - /** - * Gets the signed int value at a particular offset. - * - * @param off >= 0, < (size() - 3); offset to fetch - * @return signed int at that offset - */ - public int getInt(int off) { - checkOffsets(off, off + 4); - return (getByte0(off) << 24) | - (getUnsignedByte0(off + 1) << 16) | - (getUnsignedByte0(off + 2) << 8) | - getUnsignedByte0(off + 3); - } - - /** - * Gets the signed long value at a particular offset. - * - * @param off >= 0, < (size() - 7); offset to fetch - * @return signed int at that offset - */ - public long getLong(int off) { - checkOffsets(off, off + 8); - int part1 = (getByte0(off) << 24) | - (getUnsignedByte0(off + 1) << 16) | - (getUnsignedByte0(off + 2) << 8) | - getUnsignedByte0(off + 3); - int part2 = (getByte0(off + 4) << 24) | - (getUnsignedByte0(off + 5) << 16) | - (getUnsignedByte0(off + 6) << 8) | - getUnsignedByte0(off + 7); - - return (part2 & 0xffffffffL) | ((long) part1) << 32; - } - - /** - * Gets the unsigned byte value at a particular offset. - * - * @param off >= 0, < size(); offset to fetch - * @return unsigned byte at that offset - */ - public int getUnsignedByte(int off) { - checkOffsets(off, off + 1); - return getUnsignedByte0(off); - } - - /** - * Gets the unsigned short value at a particular offset. - * - * @param off >= 0, < (size() - 1); offset to fetch - * @return unsigned short at that offset - */ - public int getUnsignedShort(int off) { - checkOffsets(off, off + 2); - return (getUnsignedByte0(off) << 8) | getUnsignedByte0(off + 1); - } - - /** - * Copies the contents of this instance into the given raw - * byte[] at the given offset. The given array must be - * large enough. - * - * @param out non-null; array to hold the output - * @param offset non-null; index into out for the first - * byte of output - */ - public void getBytes(byte[] out, int offset) { - if ((out.length - offset) < size) { - throw new IndexOutOfBoundsException("(out.length - offset) < " + - "size()"); - } - - System.arraycopy(bytes, start, out, offset, size); - } - - /** - * Checks a range of offsets for validity, throwing if invalid. - * - * @param s start offset (inclusive) - * @param e end offset (exclusive) - */ - private void checkOffsets(int s, int e) { - if ((s < 0) || (e < s) || (e > size)) { - throw new IllegalArgumentException("bad range: " + s + ".." + e + - "; actual size " + size); - } - } - - /** - * Gets the signed byte value at the given offset, - * without doing any argument checking. - * - * @param off offset to fetch - * @return byte at that offset - */ - private int getByte0(int off) { - return bytes[start + off]; - } - - /** - * Gets the unsigned byte value at the given offset, - * without doing any argument checking. - * - * @param off offset to fetch - * @return byte at that offset - */ - private int getUnsignedByte0(int off) { - return bytes[start + off] & 0xff; - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/Util/ByteArrayAnnotatedOutput.java b/dexlib/src/main/java/org/jf/dexlib/Util/ByteArrayAnnotatedOutput.java deleted file mode 100644 index 70d86257..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Util/ByteArrayAnnotatedOutput.java +++ /dev/null @@ -1,670 +0,0 @@ -/* - * Copyright (C) 2007 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jf.dexlib.Util; - -import java.io.IOException; -import java.io.Writer; -import java.util.ArrayList; - -/** - * Implementation of {@link AnnotatedOutput} which stores the written data - * into a byte[]. - * - *

    Note: As per the {@link Output} interface, multi-byte - * writes all use little-endian order.

    - */ -public final class ByteArrayAnnotatedOutput - implements AnnotatedOutput { - /** default size for stretchy instances */ - private static final int DEFAULT_SIZE = 1000; - - /** - * whether the instance is stretchy, that is, whether its array - * may be resized to increase capacity - */ - private final boolean stretchy; - - /** non-null; the data itself */ - private byte[] data; - - /** >= 0; current output cursor */ - private int cursor; - - /** whether annotations are to be verbose */ - private boolean verbose; - - /** - * null-ok; list of annotations, or null if this instance - * isn't keeping them - */ - private ArrayList annotations; - - /** >= 40 (if used); the desired maximum annotation width */ - private int annotationWidth; - - /** - * >= 8 (if used); the number of bytes of hex output to use - * in annotations - */ - private int hexCols; - - private int currentIndent = 0; - private int indentAmount = 2; - - /** - * Constructs an instance with a fixed maximum size. Note that the - * given array is the only one that will be used to store data. In - * particular, no reallocation will occur in order to expand the - * capacity of the resulting instance. Also, the constructed - * instance does not keep annotations by default. - * - * @param data non-null; data array to use for output - */ - public ByteArrayAnnotatedOutput(byte[] data) { - this(data, false); - } - - /** - * Constructs a "stretchy" instance. The underlying array may be - * reallocated. The constructed instance does not keep annotations - * by default. - */ - public ByteArrayAnnotatedOutput() { - this(new byte[DEFAULT_SIZE], true); - } - - /** - * Internal constructor. - * - * @param data non-null; data array to use for output - * @param stretchy whether the instance is to be stretchy - */ - private ByteArrayAnnotatedOutput(byte[] data, boolean stretchy) { - if (data == null) { - throw new NullPointerException("data == null"); - } - - this.stretchy = stretchy; - this.data = data; - this.cursor = 0; - this.verbose = false; - this.annotations = null; - this.annotationWidth = 0; - this.hexCols = 0; - } - - /** - * Gets the underlying byte[] of this instance, which - * may be larger than the number of bytes written - * - * @see #toByteArray - * - * @return non-null; the byte[] - */ - public byte[] getArray() { - return data; - } - - /** - * Constructs and returns a new byte[] that contains - * the written contents exactly (that is, with no extra unwritten - * bytes at the end). - * - * @see #getArray - * - * @return non-null; an appropriately-constructed array - */ - public byte[] toByteArray() { - byte[] result = new byte[cursor]; - System.arraycopy(data, 0, result, 0, cursor); - return result; - } - - /** {@inheritDoc} */ - public int getCursor() { - return cursor; - } - - /** {@inheritDoc} */ - public void assertCursor(int expectedCursor) { - if (cursor != expectedCursor) { - throw new ExceptionWithContext("expected cursor " + - expectedCursor + "; actual value: " + cursor); - } - } - - /** {@inheritDoc} */ - public void writeByte(int value) { - int writeAt = cursor; - int end = writeAt + 1; - - if (stretchy) { - ensureCapacity(end); - } else if (end > data.length) { - throwBounds(); - return; - } - - data[writeAt] = (byte) value; - cursor = end; - } - - /** {@inheritDoc} */ - public void writeShort(int value) { - int writeAt = cursor; - int end = writeAt + 2; - - if (stretchy) { - ensureCapacity(end); - } else if (end > data.length) { - throwBounds(); - return; - } - - data[writeAt] = (byte) value; - data[writeAt + 1] = (byte) (value >> 8); - cursor = end; - } - - /** {@inheritDoc} */ - public void writeInt(int value) { - int writeAt = cursor; - int end = writeAt + 4; - - if (stretchy) { - ensureCapacity(end); - } else if (end > data.length) { - throwBounds(); - return; - } - - data[writeAt] = (byte) value; - data[writeAt + 1] = (byte) (value >> 8); - data[writeAt + 2] = (byte) (value >> 16); - data[writeAt + 3] = (byte) (value >> 24); - cursor = end; - } - - /** {@inheritDoc} */ - public void writeLong(long value) { - int writeAt = cursor; - int end = writeAt + 8; - - if (stretchy) { - ensureCapacity(end); - } else if (end > data.length) { - throwBounds(); - return; - } - - int half = (int) value; - data[writeAt] = (byte) half; - data[writeAt + 1] = (byte) (half >> 8); - data[writeAt + 2] = (byte) (half >> 16); - data[writeAt + 3] = (byte) (half >> 24); - - half = (int) (value >> 32); - data[writeAt + 4] = (byte) half; - data[writeAt + 5] = (byte) (half >> 8); - data[writeAt + 6] = (byte) (half >> 16); - data[writeAt + 7] = (byte) (half >> 24); - - cursor = end; - } - - /** {@inheritDoc} */ - public int writeUnsignedLeb128(int value) { - long remaining = (value & 0xFFFFFFFFL) >> 7; - long lValue = value; - int count = 0; - - while (remaining != 0) { - writeByte((int)(lValue & 0x7f) | 0x80); - lValue = remaining; - remaining >>= 7; - count++; - } - - writeByte((int)(lValue & 0x7f)); - return count + 1; - } - - /** {@inheritDoc} */ - public int writeSignedLeb128(int value) { - int remaining = value >> 7; - int count = 0; - boolean hasMore = true; - int end = ((value & Integer.MIN_VALUE) == 0) ? 0 : -1; - - while (hasMore) { - hasMore = (remaining != end) - || ((remaining & 1) != ((value >> 6) & 1)); - - writeByte((value & 0x7f) | (hasMore ? 0x80 : 0)); - value = remaining; - remaining >>= 7; - count++; - } - - return count; - } - - /** {@inheritDoc} */ - public void write(ByteArray bytes) { - int blen = bytes.size(); - int writeAt = cursor; - int end = writeAt + blen; - - if (stretchy) { - ensureCapacity(end); - } else if (end > data.length) { - throwBounds(); - return; - } - - bytes.getBytes(data, writeAt); - cursor = end; - } - - /** {@inheritDoc} */ - public void write(byte[] bytes, int offset, int length) { - int writeAt = cursor; - int end = writeAt + length; - int bytesEnd = offset + length; - - // twos-complement math trick: ((x < 0) || (y < 0)) <=> ((x|y) < 0) - if (((offset | length | end) < 0) || (bytesEnd > bytes.length)) { - throw new IndexOutOfBoundsException("bytes.length " + - bytes.length + "; " + - offset + "..!" + end); - } - - if (stretchy) { - ensureCapacity(end); - } else if (end > data.length) { - throwBounds(); - return; - } - - System.arraycopy(bytes, offset, data, writeAt, length); - cursor = end; - } - - /** {@inheritDoc} */ - public void write(byte[] bytes) { - write(bytes, 0, bytes.length); - } - - /** {@inheritDoc} */ - public void writeZeroes(int count) { - if (count < 0) { - throw new IllegalArgumentException("count < 0"); - } - - int end = cursor + count; - - if (stretchy) { - ensureCapacity(end); - } else if (end > data.length) { - throwBounds(); - return; - } - - /* - * There is no need to actually write zeroes, since the array is - * already preinitialized with zeroes. - */ - - cursor = end; - } - - /** {@inheritDoc} */ - public void alignTo(int alignment) { - int mask = alignment - 1; - - if ((alignment < 0) || ((mask & alignment) != 0)) { - throw new IllegalArgumentException("bogus alignment"); - } - - int end = (cursor + mask) & ~mask; - - if (stretchy) { - ensureCapacity(end); - } else if (end > data.length) { - throwBounds(); - return; - } - - /* - * There is no need to actually write zeroes, since the array is - * already preinitialized with zeroes. - */ - - cursor = end; - } - - /** {@inheritDoc} */ - public boolean annotates() { - return (annotations != null); - } - - /** {@inheritDoc} */ - public boolean isVerbose() { - return verbose; - } - - /** {@inheritDoc} */ - public void annotate(String msg) { - if (annotations == null) { - return; - } - - endAnnotation(); - annotations.add(new Annotation(cursor, msg, currentIndent)); - } - - public void indent() { - currentIndent++; - } - - public void deindent() { - currentIndent--; - if (currentIndent < 0) { - currentIndent = 0; - } - } - - public void setIndentAmount(int indentAmount) { - this.indentAmount = indentAmount; - } - - /** {@inheritDoc} */ - public void annotate(int amt, String msg) { - if (annotations == null) { - return; - } - - endAnnotation(); - - int asz = annotations.size(); - int lastEnd = (asz == 0) ? 0 : annotations.get(asz - 1).getEnd(); - int startAt; - - if (lastEnd <= cursor) { - startAt = cursor; - } else { - startAt = lastEnd; - } - - annotations.add(new Annotation(startAt, startAt + amt, msg, currentIndent)); - } - - /** {@inheritDoc} */ - public void endAnnotation() { - if (annotations == null) { - return; - } - - int sz = annotations.size(); - - if (sz != 0) { - annotations.get(sz - 1).setEndIfUnset(cursor); - } - } - - /** {@inheritDoc} */ - public int getAnnotationWidth() { - int leftWidth = 8 + (hexCols * 2) + (hexCols / 2); - - return annotationWidth - leftWidth; - } - - /** - * Indicates that this instance should keep annotations. This method may - * be called only once per instance, and only before any data has been - * written to the it. - * - * @param annotationWidth >= 40; the desired maximum annotation width - * @param verbose whether or not to indicate verbose annotations - */ - public void enableAnnotations(int annotationWidth, boolean verbose) { - if ((annotations != null) || (cursor != 0)) { - throw new RuntimeException("cannot enable annotations"); - } - - if (annotationWidth < 40) { - throw new IllegalArgumentException("annotationWidth < 40"); - } - - int hexCols = (((annotationWidth - 7) / 15) + 1) & ~1; - if (hexCols < 6) { - hexCols = 6; - } else if (hexCols > 10) { - hexCols = 10; - } - - this.annotations = new ArrayList(1000); - this.annotationWidth = annotationWidth; - this.hexCols = hexCols; - this.verbose = verbose; - } - - /** - * Finishes up annotation processing. This closes off any open - * annotations and removes annotations that don't refer to written - * data. - */ - public void finishAnnotating() { - // Close off the final annotation, if any. - endAnnotation(); - - // Remove annotations that refer to unwritten data. - if (annotations != null) { - int asz = annotations.size(); - while (asz > 0) { - Annotation last = annotations.get(asz - 1); - if (last.getStart() > cursor) { - annotations.remove(asz - 1); - asz--; - } else if (last.getEnd() > cursor) { - last.setEnd(cursor); - break; - } else { - break; - } - } - } - } - - /** - * Writes the annotated content of this instance to the given writer. - * - * @param out non-null; where to write to - */ - public void writeAnnotationsTo(Writer out) throws IOException { - int width2 = getAnnotationWidth(); - int width1 = annotationWidth - width2 - 1; - - StringBuilder padding = new StringBuilder(); - for (int i=0; i<1000; i++) { - padding.append(' '); - } - - TwoColumnOutput twoc = new TwoColumnOutput(out, width1, width2, "|"); - Writer left = twoc.getLeft(); - Writer right = twoc.getRight(); - int leftAt = 0; // left-hand byte output cursor - int rightAt = 0; // right-hand annotation index - int rightSz = annotations.size(); - - while ((leftAt < cursor) && (rightAt < rightSz)) { - Annotation a = annotations.get(rightAt); - int start = a.getStart(); - int end; - String text; - - if (leftAt < start) { - // This is an area with no annotation. - end = start; - start = leftAt; - text = ""; - } else { - // This is an area with an annotation. - end = a.getEnd(); - text = padding.substring(0, a.getIndent() * this.indentAmount) + a.getText(); - rightAt++; - } - - left.write(Hex.dump(data, start, end - start, start, hexCols, 6)); - right.write(text); - twoc.flush(); - leftAt = end; - } - - if (leftAt < cursor) { - // There is unannotated output at the end. - left.write(Hex.dump(data, leftAt, cursor - leftAt, leftAt, - hexCols, 6)); - } - - while (rightAt < rightSz) { - // There are zero-byte annotations at the end. - right.write(annotations.get(rightAt).getText()); - rightAt++; - } - - twoc.flush(); - } - - /** - * Throws the excpetion for when an attempt is made to write past the - * end of the instance. - */ - private static void throwBounds() { - throw new IndexOutOfBoundsException("attempt to write past the end"); - } - - /** - * Reallocates the underlying array if necessary. Calls to this method - * should be guarded by a test of {@link #stretchy}. - * - * @param desiredSize >= 0; the desired minimum total size of the array - */ - private void ensureCapacity(int desiredSize) { - if (data.length < desiredSize) { - byte[] newData = new byte[desiredSize * 2 + 1000]; - System.arraycopy(data, 0, newData, 0, cursor); - data = newData; - } - } - - /** - * Annotation on output. - */ - private static class Annotation { - /** >= 0; start of annotated range (inclusive) */ - private final int start; - - /** - * >= 0; end of annotated range (exclusive); - * Integer.MAX_VALUE if unclosed - */ - private int end; - - /** non-null; annotation text */ - private final String text; - - private int indent; - - /** - * Constructs an instance. - * - * @param start >= 0; start of annotated range - * @param end >= start; end of annotated range (exclusive) or - * Integer.MAX_VALUE if unclosed - * @param text non-null; annotation text - */ - public Annotation(int start, int end, String text, int indent) { - this.start = start; - this.end = end; - this.text = text; - this.indent = indent; - } - - /** - * Constructs an instance. It is initally unclosed. - * - * @param start >= 0; start of annotated range - * @param text non-null; annotation text - */ - public Annotation(int start, String text, int indent) { - this(start, Integer.MAX_VALUE, text, indent); - } - - /** - * Sets the end as given, but only if the instance is unclosed; - * otherwise, do nothing. - * - * @param end >= start; the end - */ - public void setEndIfUnset(int end) { - if (this.end == Integer.MAX_VALUE) { - this.end = end; - } - } - - /** - * Sets the end as given. - * - * @param end >= start; the end - */ - public void setEnd(int end) { - this.end = end; - } - - /** - * Gets the start. - * - * @return the start - */ - public int getStart() { - return start; - } - - /** - * Gets the end. - * - * @return the end - */ - public int getEnd() { - return end; - } - - /** - * Gets the text. - * - * @return non-null; the text - */ - public String getText() { - return text; - } - - public int getIndent() { - return indent; - } - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/Util/ByteArrayInput.java b/dexlib/src/main/java/org/jf/dexlib/Util/ByteArrayInput.java deleted file mode 100644 index 7206e3e2..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Util/ByteArrayInput.java +++ /dev/null @@ -1,306 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.Util; - -/** - * Implementation of {@link Input} which reads the data from a - * byte[] instance. - * - *

    Note: As per the {@link Input } interface, multi-byte - * reads all use little-endian order.

    - */ -public class ByteArrayInput - implements Input { - - /** non-null; the data itself */ - private byte[] data; - - /** >= 0; current read cursor */ - private int cursor; - - /** - * Constructs an instance with the given data - * - * @param data non-null; data array to use for input - */ - public ByteArrayInput(byte[] data) { - if (data == null) { - throw new NullPointerException("data == null"); - } - - this.data = data; - this.cursor = 0; - } - - /** - * Gets the underlying byte[] of this instance - * - * @return non-null; the byte[] - */ - public byte[] getArray() { - return data; - } - - /** {@inheritDoc} */ - public int getCursor() { - return cursor; - } - - /** {@inheritDoc} */ - public void setCursor(int cursor) { - if (cursor < 0 || cursor >= data.length) - throw new IndexOutOfBoundsException("The provided cursor value " + - "is not within the bounds of this instance's data array"); - this.cursor = cursor; - } - - /** {@inheritDoc} */ - public void assertCursor(int expectedCursor) { - if (cursor != expectedCursor) { - throw new ExceptionWithContext("expected cursor " + - expectedCursor + "; actual value: " + cursor); - } - } - - /** {@inheritDoc} */ - public byte readByte() { - int readAt = cursor; - int end = readAt + 1; - - if (end > data.length) { - throwBounds(); - } - - cursor = end; - return data[readAt]; - } - - /** {@inheritDoc} */ - public int readShort() { - int readAt = cursor; - int end = readAt + 2; - - if (end > data.length) { - throwBounds(); - } - - cursor = end; - return (int)((data[readAt] & 0xff) + - ((data[readAt + 1] & 0xff) << 8)); - } - - /** {@inheritDoc} */ - public int readInt() { - int readAt = cursor; - int end = readAt + 4; - - if (end > data.length) { - throwBounds(); - } - - cursor = end; - return (data[readAt] & 0xff) + - ((data[readAt + 1] & 0xff) << 8) + - ((data[readAt + 2] & 0xff) << 16) + - ((data[readAt + 3] & 0xff) << 24); - } - - /** {@inheritDoc} */ - public long readLong() { - int readAt = cursor; - int end = readAt + 8; - - if (end > data.length) { - throwBounds(); - } - - cursor = end; - - return (data[readAt] & 0xffL) | - ((data[readAt + 1] & 0xffL) << 8) | - ((data[readAt + 2] & 0xffL) << 16) | - ((data[readAt + 3] & 0xffL) << 24) | - ((data[readAt + 4] & 0xffL) << 32) | - ((data[readAt + 5] & 0xffL) << 40) | - ((data[readAt + 6] & 0xffL) << 48) | - ((data[readAt + 7] & 0xffL) << 58); - } - - /** {@inheritDoc} */ - public int readUnsignedLeb128() { - int end = cursor; - int currentByteValue; - int result; - - result = data[end++] & 0xff; - if (result > 0x7f) { - currentByteValue = data[end++] & 0xff; - result = (result & 0x7f) | ((currentByteValue & 0x7f) << 7); - if (currentByteValue > 0x7f) { - currentByteValue = data[end++] & 0xff; - result |= (currentByteValue & 0x7f) << 14; - if (currentByteValue > 0x7f) { - currentByteValue = data[end++] & 0xff; - result |= (currentByteValue & 0x7f) << 21; - if (currentByteValue > 0x7f) { - currentByteValue = data[end++] & 0xff; - if (currentByteValue > 0x0f) { - throwInvalidLeb(); - } - result |= currentByteValue << 28; - } - } - } - } - - cursor = end; - return result; - } - - /** {@inheritDoc} */ - public int readSignedLeb128() { - int end = cursor; - int currentByteValue; - int result; - - result = data[end++] & 0xff; - if (result <= 0x7f) { - result = (result << 25) >> 25; - } else { - currentByteValue = data[end++] & 0xff; - result = (result & 0x7f) | ((currentByteValue & 0x7f) << 7); - if (currentByteValue <= 0x7f) { - result = (result << 18) >> 18; - } else { - currentByteValue = data[end++] & 0xff; - result |= (currentByteValue & 0x7f) << 14; - if (currentByteValue <= 0x7f) { - result = (result << 11) >> 11; - } else { - currentByteValue = data[end++] & 0xff; - result |= (currentByteValue & 0x7f) << 21; - if (currentByteValue <= 0x7f) { - result = (result << 4) >> 4; - } else { - currentByteValue = data[end++] & 0xff; - if (currentByteValue > 0x0f) { - throwInvalidLeb(); - } - result |= currentByteValue << 28; - } - } - } - } - - cursor = end; - return result; - } - - /** {@inheritDoc} */ - public void read(byte[] bytes, int offset, int length) { - int end = cursor + length; - - if (end > data.length) { - throwBounds(); - } - - System.arraycopy(data, cursor, bytes, offset, length); - cursor = end; - } - - /** {@inheritDoc} */ - public void read(byte[] bytes) { - int length = bytes.length; - int end = cursor + length; - - if (end > data.length) { - throwBounds(); - } - - System.arraycopy(data, cursor, bytes, 0, length); - cursor = end; - } - - /** {@inheritDoc} */ - public byte[] readBytes(int length) { - int end = cursor + length; - - if (end > data.length) { - throwBounds(); - } - - byte[] result = new byte[length]; - System.arraycopy(data, cursor, result, 0, length); - cursor = end; - return result; - } - - /** {@inheritDoc} */ - public void skipBytes(int count) { - int end = cursor + count; - - if (end > data.length) { - throwBounds(); - } - - cursor = end; - } - - /** {@inheritDoc} */ - public void alignTo(int alignment) { - int mask = alignment - 1; - - if ((alignment < 0) || ((mask & alignment) != 0)) { - throw new IllegalArgumentException("bogus alignment"); - } - - int end = (cursor + mask) & ~mask; - - if (end > data.length) { - throwBounds(); - } - - cursor = end; - } - - /** - * Throws the excpetion for when an attempt is made to read past the - * end of the instance. - */ - private static void throwBounds() { - throw new IndexOutOfBoundsException("attempt to read past the end"); - } - - /** - * Throws the exception for when an invalid LEB128 value is encountered - */ - private static void throwInvalidLeb() { - throw new RuntimeException("invalid LEB128 integer encountered"); - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/Util/ByteArrayOutput.java b/dexlib/src/main/java/org/jf/dexlib/Util/ByteArrayOutput.java deleted file mode 100644 index 577b2a74..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Util/ByteArrayOutput.java +++ /dev/null @@ -1,582 +0,0 @@ -/* - * Copyright (C) 2007 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jf.dexlib.Util; - -import java.util.ArrayList; - -/** - * Implementation of {@link AnnotatedOutput} which stores the written data - * into a byte[]. - * - *

    Note: As per the {@link Output} interface, multi-byte - * writes all use little-endian order.

    - */ -public final class ByteArrayOutput implements Output -{ - /** default size for stretchy instances */ - private static final int DEFAULT_SIZE = 1000; - - /** - * whether the instance is stretchy, that is, whether its array - * may be resized to increase capacity - */ - private final boolean stretchy; - - /** non-null; the data itself */ - private byte[] data; - - /** >= 0; current output cursor */ - private int cursor; - - /** whether annotations are to be verbose */ - private boolean verbose; - - /** - * null-ok; list of annotations, or null if this instance - * isn't keeping them - */ - private ArrayList annotations; - - /** >= 40 (if used); the desired maximum annotation width */ - private int annotationWidth; - - /** - * >= 8 (if used); the number of bytes of hex output to use - * in annotations - */ - private int hexCols; - - /** - * Constructs an instance with a fixed maximum size. Note that the - * given array is the only one that will be used to store data. In - * particular, no reallocation will occur in order to expand the - * capacity of the resulting instance. Also, the constructed - * instance does not keep annotations by default. - * - * @param data non-null; data array to use for output - */ - public ByteArrayOutput(byte[] data) { - this(data, false); - } - - /** - * Constructs a "stretchy" instance. The underlying array may be - * reallocated. The constructed instance does not keep annotations - * by default. - */ - public ByteArrayOutput() { - this(new byte[DEFAULT_SIZE], true); - } - - /** - * Internal constructor. - * - * @param data non-null; data array to use for output - * @param stretchy whether the instance is to be stretchy - */ - private ByteArrayOutput(byte[] data, boolean stretchy) { - if (data == null) { - throw new NullPointerException("data == null"); - } - - this.stretchy = stretchy; - this.data = data; - this.cursor = 0; - this.verbose = false; - this.annotations = null; - this.annotationWidth = 0; - this.hexCols = 0; - } - - /** - * Gets the underlying byte[] of this instance, which - * may be larger than the number of bytes written - * - * @see #toByteArray - * - * @return non-null; the byte[] - */ - public byte[] getArray() { - return data; - } - - /** - * Constructs and returns a new byte[] that contains - * the written contents exactly (that is, with no extra unwritten - * bytes at the end). - * - * @see #getArray - * - * @return non-null; an appropriately-constructed array - */ - public byte[] toByteArray() { - byte[] result = new byte[cursor]; - System.arraycopy(data, 0, result, 0, cursor); - return result; - } - - /** {@inheritDoc} */ - public int getCursor() { - return cursor; - } - - /** {@inheritDoc} */ - public void assertCursor(int expectedCursor) { - if (cursor != expectedCursor) { - throw new ExceptionWithContext("expected cursor " + - expectedCursor + "; actual value: " + cursor); - } - } - - /** {@inheritDoc} */ - public void writeByte(int value) { - int writeAt = cursor; - int end = writeAt + 1; - - if (stretchy) { - ensureCapacity(end); - } else if (end > data.length) { - throwBounds(); - return; - } - - data[writeAt] = (byte) value; - cursor = end; - } - - /** {@inheritDoc} */ - public void writeShort(int value) { - int writeAt = cursor; - int end = writeAt + 2; - - if (stretchy) { - ensureCapacity(end); - } else if (end > data.length) { - throwBounds(); - return; - } - - data[writeAt] = (byte) value; - data[writeAt + 1] = (byte) (value >> 8); - cursor = end; - } - - /** {@inheritDoc} */ - public void writeInt(int value) { - int writeAt = cursor; - int end = writeAt + 4; - - if (stretchy) { - ensureCapacity(end); - } else if (end > data.length) { - throwBounds(); - return; - } - - data[writeAt] = (byte) value; - data[writeAt + 1] = (byte) (value >> 8); - data[writeAt + 2] = (byte) (value >> 16); - data[writeAt + 3] = (byte) (value >> 24); - cursor = end; - } - - /** {@inheritDoc} */ - public void writeLong(long value) { - int writeAt = cursor; - int end = writeAt + 8; - - if (stretchy) { - ensureCapacity(end); - } else if (end > data.length) { - throwBounds(); - return; - } - - int half = (int) value; - data[writeAt] = (byte) half; - data[writeAt + 1] = (byte) (half >> 8); - data[writeAt + 2] = (byte) (half >> 16); - data[writeAt + 3] = (byte) (half >> 24); - - half = (int) (value >> 32); - data[writeAt + 4] = (byte) half; - data[writeAt + 5] = (byte) (half >> 8); - data[writeAt + 6] = (byte) (half >> 16); - data[writeAt + 7] = (byte) (half >> 24); - - cursor = end; - } - - /** {@inheritDoc} */ - public int writeUnsignedLeb128(int value) { - int remaining = value >> 7; - int count = 0; - - while (remaining != 0) { - writeByte((value & 0x7f) | 0x80); - value = remaining; - remaining >>= 7; - count++; - } - - writeByte(value & 0x7f); - return count + 1; - } - - /** {@inheritDoc} */ - public int writeSignedLeb128(int value) { - int remaining = value >> 7; - int count = 0; - boolean hasMore = true; - int end = ((value & Integer.MIN_VALUE) == 0) ? 0 : -1; - - while (hasMore) { - hasMore = (remaining != end) - || ((remaining & 1) != ((value >> 6) & 1)); - - writeByte((value & 0x7f) | (hasMore ? 0x80 : 0)); - value = remaining; - remaining >>= 7; - count++; - } - - return count; - } - - /** {@inheritDoc} */ - public void write(ByteArray bytes) { - int blen = bytes.size(); - int writeAt = cursor; - int end = writeAt + blen; - - if (stretchy) { - ensureCapacity(end); - } else if (end > data.length) { - throwBounds(); - return; - } - - bytes.getBytes(data, writeAt); - cursor = end; - } - - /** {@inheritDoc} */ - public void write(byte[] bytes, int offset, int length) { - int writeAt = cursor; - int end = writeAt + length; - int bytesEnd = offset + length; - - // twos-complement math trick: ((x < 0) || (y < 0)) <=> ((x|y) < 0) - if (((offset | length | end) < 0) || (bytesEnd > bytes.length)) { - throw new IndexOutOfBoundsException("bytes.length " + - bytes.length + "; " + - offset + "..!" + end); - } - - if (stretchy) { - ensureCapacity(end); - } else if (end > data.length) { - throwBounds(); - return; - } - - System.arraycopy(bytes, offset, data, writeAt, length); - cursor = end; - } - - /** {@inheritDoc} */ - public void write(byte[] bytes) { - write(bytes, 0, bytes.length); - } - - /** {@inheritDoc} */ - public void writeZeroes(int count) { - if (count < 0) { - throw new IllegalArgumentException("count < 0"); - } - - int end = cursor + count; - - if (stretchy) { - ensureCapacity(end); - } else if (end > data.length) { - throwBounds(); - return; - } - - /* - * There is no need to actually write zeroes, since the array is - * already preinitialized with zeroes. - */ - - cursor = end; - } - - /** {@inheritDoc} */ - public void alignTo(int alignment) { - int mask = alignment - 1; - - if ((alignment < 0) || ((mask & alignment) != 0)) { - throw new IllegalArgumentException("bogus alignment"); - } - - int end = (cursor + mask) & ~mask; - - if (stretchy) { - ensureCapacity(end); - } else if (end > data.length) { - throwBounds(); - return; - } - - /* - * There is no need to actually write zeroes, since the array is - * already preinitialized with zeroes. - */ - - cursor = end; - } - - /** {@inheritDoc} */ - public boolean annotates() { - return (annotations != null); - } - - /** {@inheritDoc} */ - public boolean isVerbose() { - return verbose; - } - - /** {@inheritDoc} */ - public void annotate(String msg) { - if (annotations == null) { - return; - } - - endAnnotation(); - annotations.add(new Annotation(cursor, msg)); - } - - /** {@inheritDoc} */ - public void annotate(int amt, String msg) { - if (annotations == null) { - return; - } - - endAnnotation(); - - int asz = annotations.size(); - int lastEnd = (asz == 0) ? 0 : annotations.get(asz - 1).getEnd(); - int startAt; - - if (lastEnd <= cursor) { - startAt = cursor; - } else { - startAt = lastEnd; - } - - annotations.add(new Annotation(startAt, startAt + amt, msg)); - } - - /** {@inheritDoc} */ - public void endAnnotation() { - if (annotations == null) { - return; - } - - int sz = annotations.size(); - - if (sz != 0) { - annotations.get(sz - 1).setEndIfUnset(cursor); - } - } - - /** {@inheritDoc} */ - public int getAnnotationWidth() { - int leftWidth = 8 + (hexCols * 2) + (hexCols / 2); - - return annotationWidth - leftWidth; - } - - /** - * Indicates that this instance should keep annotations. This method may - * be called only once per instance, and only before any data has been - * written to the it. - * - * @param annotationWidth >= 40; the desired maximum annotation width - * @param verbose whether or not to indicate verbose annotations - */ - public void enableAnnotations(int annotationWidth, boolean verbose) { - if ((annotations != null) || (cursor != 0)) { - throw new RuntimeException("cannot enable annotations"); - } - - if (annotationWidth < 40) { - throw new IllegalArgumentException("annotationWidth < 40"); - } - - int hexCols = (((annotationWidth - 7) / 15) + 1) & ~1; - if (hexCols < 6) { - hexCols = 6; - } else if (hexCols > 10) { - hexCols = 10; - } - - this.annotations = new ArrayList(1000); - this.annotationWidth = annotationWidth; - this.hexCols = hexCols; - this.verbose = verbose; - } - - /** - * Finishes up annotation processing. This closes off any open - * annotations and removes annotations that don't refer to written - * data. - */ - public void finishAnnotating() { - // Close off the final annotation, if any. - endAnnotation(); - - // Remove annotations that refer to unwritten data. - if (annotations != null) { - int asz = annotations.size(); - while (asz > 0) { - Annotation last = annotations.get(asz - 1); - if (last.getStart() > cursor) { - annotations.remove(asz - 1); - asz--; - } else if (last.getEnd() > cursor) { - last.setEnd(cursor); - break; - } else { - break; - } - } - } - } - - /** - * Throws the excpetion for when an attempt is made to write past the - * end of the instance. - */ - private static void throwBounds() { - throw new IndexOutOfBoundsException("attempt to write past the end"); - } - - /** - * Reallocates the underlying array if necessary. Calls to this method - * should be guarded by a test of {@link #stretchy}. - * - * @param desiredSize >= 0; the desired minimum total size of the array - */ - private void ensureCapacity(int desiredSize) { - if (data.length < desiredSize) { - byte[] newData = new byte[desiredSize * 2 + 1000]; - System.arraycopy(data, 0, newData, 0, cursor); - data = newData; - } - } - - /** - * Annotation on output. - */ - private static class Annotation { - /** >= 0; start of annotated range (inclusive) */ - private final int start; - - /** - * >= 0; end of annotated range (exclusive); - * Integer.MAX_VALUE if unclosed - */ - private int end; - - /** non-null; annotation text */ - private final String text; - - /** - * Constructs an instance. - * - * @param start >= 0; start of annotated range - * @param end >= start; end of annotated range (exclusive) or - * Integer.MAX_VALUE if unclosed - * @param text non-null; annotation text - */ - public Annotation(int start, int end, String text) { - this.start = start; - this.end = end; - this.text = text; - } - - /** - * Constructs an instance. It is initally unclosed. - * - * @param start >= 0; start of annotated range - * @param text non-null; annotation text - */ - public Annotation(int start, String text) { - this(start, Integer.MAX_VALUE, text); - } - - /** - * Sets the end as given, but only if the instance is unclosed; - * otherwise, do nothing. - * - * @param end >= start; the end - */ - public void setEndIfUnset(int end) { - if (this.end == Integer.MAX_VALUE) { - this.end = end; - } - } - - /** - * Sets the end as given. - * - * @param end >= start; the end - */ - public void setEnd(int end) { - this.end = end; - } - - /** - * Gets the start. - * - * @return the start - */ - public int getStart() { - return start; - } - - /** - * Gets the end. - * - * @return the end - */ - public int getEnd() { - return end; - } - - /** - * Gets the text. - * - * @return non-null; the text - */ - public String getText() { - return text; - } - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/Util/DebugInfoBuilder.java b/dexlib/src/main/java/org/jf/dexlib/Util/DebugInfoBuilder.java deleted file mode 100644 index 167dd1bd..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Util/DebugInfoBuilder.java +++ /dev/null @@ -1,392 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.Util; - -import org.jf.dexlib.DebugInfoItem; -import org.jf.dexlib.DexFile; -import org.jf.dexlib.StringIdItem; -import org.jf.dexlib.TypeIdItem; -import org.jf.dexlib.Debug.*; - -import java.util.ArrayList; -import java.util.List; - -/** - * This class is intended to provide an easy to use container to build up a method's debug info. You can easily add - * an "event" at a specific address, where an event is something like a line number, start/end local, etc. - * The events must be added such that the code addresses increase monotonically. This matches how a parser would - * generally behave, and is intended to increase performance. - */ -public class DebugInfoBuilder -{ - private static final int LINE_BASE = -4; - private static final int LINE_RANGE = 15; - private static final int FIRST_SPECIAL = 0x0a; - - private int lineStart = 0; - private ArrayList parameterNames = new ArrayList(); - private ArrayList events = new ArrayList(); - private int lastAddress = 0; - - private boolean hasData; - - private int currentAddress; - private int currentLine; - - public DebugInfoBuilder() { - } - - private void checkAddress(int address) { - if (lastAddress > address) { - throw new RuntimeException("Cannot add an event with an address before the address of the prior event"); - } - } - - public void addParameterName(String parameterName) { - if (parameterName != null) { - hasData = true; - } - - parameterNames.add(parameterName); - } - - public void addLine(int address, int line) { - hasData = true; - - checkAddress(address); - - if (lineStart == 0) { - lineStart = line; - } - - events.add(new LineEvent(address, line)); - } - - public void addLocal(int address, int registerNumber, String localName, String localType) { - hasData = true; - - checkAddress(address); - - events.add(new StartLocalEvent(address, registerNumber, localName, localType)); - } - - public void addLocalExtended(int address, int registerNumber, String localName, String localType, - String signature) { - hasData = true; - - checkAddress(address); - - events.add(new StartLocalExtendedEvent(address, registerNumber, localName, localType, signature)); - } - - public void addEndLocal(int address, int registerNumber) { - hasData = true; - - checkAddress(address); - - events.add(new EndLocalEvent(address, registerNumber)); - } - - public void addRestartLocal(int address, int registerNumber) { - hasData = true; - - checkAddress(address); - - events.add(new RestartLocalEvent(address, registerNumber)); - } - - public void addPrologue(int address) { - hasData = true; - - checkAddress(address); - - events.add(new PrologueEvent(address)); - } - - public void addEpilogue(int address) { - hasData = true; - - checkAddress(address); - - events.add(new EpilogueEvent(address)); - } - - public void addSetFile(int address, String fileName) { - hasData = true; - - checkAddress(address); - - events.add(new SetFileEvent(address, fileName)); - } - - public int getParameterNameCount() { - return parameterNames.size(); - } - - public DebugInfoItem encodeDebugInfo(DexFile dexFile) { - if (!hasData) { - return null; - } - - ArrayList debugInstructions = new ArrayList(); - ArrayList parameterNameReferences = new ArrayList(); - - if (lineStart == 0) { - lineStart = 1; - } - - currentLine = lineStart; - - for (Event event: events) { - event.emit(dexFile, debugInstructions); - } - debugInstructions.add(new EndSequence()); - - for (String parameterName: parameterNames) { - if (parameterName == null) { - parameterNameReferences.add(null); - } else { - parameterNameReferences.add(new StringIdItem(dexFile, parameterName)); - } - } - - return new DebugInfoItem(dexFile, lineStart, parameterNameReferences, debugInstructions); - } - - private interface Event - { - int getAddress(); - void emit(DexFile dexFile, List debugInstructions); - } - - private void emitAdvancePC(int address, List debugInstructions) { - int addressDelta = address-currentAddress; - - if (addressDelta > 0) { - debugInstructions.add(new AdvancePC(addressDelta)); - currentAddress = address; - } - } - - private class LineEvent implements Event - { - private final int address; - private final int line; - - public LineEvent(int address, int line) { - this.address = address; - this.line = line; - } - - public int getAddress() { - return address; - } - - public void emit(DexFile dexFile, List debugInstructions) { - int lineDelta = line - currentLine; - int addressDelta = address - currentAddress; - - if (lineDelta < -4 || lineDelta > 10) { - debugInstructions.add(new AdvanceLine(lineDelta)); - lineDelta = 0; - } - if (lineDelta < 2 && addressDelta > 16 || lineDelta > 1 && addressDelta > 15) { - debugInstructions.add(new AdvancePC(addressDelta)); - addressDelta = 0; - } - - //TODO: need to handle the case when the line delta is larger than a signed int - debugInstructions.add(new SpecialOpcode(calculateSpecialOpcode(lineDelta, addressDelta))); - - - currentAddress = address; - currentLine = line; - } - - private byte calculateSpecialOpcode(int lineDelta, int addressDelta) { - return (byte)(FIRST_SPECIAL + (addressDelta * LINE_RANGE) + (lineDelta - LINE_BASE)); - } - } - - private class StartLocalEvent implements Event - { - private final int address; - private final int registerNum; - private final String localName; - private final String localType; - - public StartLocalEvent(int address, int registerNum, String localName, String localType) { - this.address = address; - this.registerNum = registerNum; - this.localName = localName; - this.localType = localType; - } - - public int getAddress() { - return address; - } - - public void emit(DexFile dexFile, List debugInstructions) { - emitAdvancePC(address, debugInstructions); - - debugInstructions.add(new StartLocal(dexFile, registerNum, new StringIdItem(dexFile, localName), - new TypeIdItem(dexFile, localType))); - } - } - - private class StartLocalExtendedEvent implements Event - { - private final int address; - private final int registerNum; - private final String localName; - private final String localType; - private final String signature; - - public StartLocalExtendedEvent(int address, int registerNum, String localName, String localType, - String signature) { - this.address = address; - this.registerNum = registerNum; - this.localName = localName; - this.localType = localType; - this.signature = signature; - } - - public int getAddress() { - return address; - } - - public void emit(DexFile dexFile, List debugInstructions) { - emitAdvancePC(address, debugInstructions); - - debugInstructions.add(new StartLocalExtended(dexFile, registerNum, new StringIdItem(dexFile, localName), - new TypeIdItem(dexFile, localType), new StringIdItem(dexFile, signature))); - } - } - - private class EndLocalEvent implements Event - { - private final int address; - private final int registerNum; - - public EndLocalEvent(int address, int registerNum) { - this.address = address; - this.registerNum = registerNum; - } - - public int getAddress() { - return address; - } - - public void emit(DexFile dexFile, List debugInstructions) { - emitAdvancePC(address, debugInstructions); - - debugInstructions.add(new EndLocal(registerNum)); - } - } - - private class RestartLocalEvent implements Event - { - private final int address; - private final int registerNum; - - public RestartLocalEvent(int address, int registerNum) { - this.address = address; - this.registerNum = registerNum; - } - - public int getAddress() { - return address; - } - - public void emit(DexFile dexFile, List debugInstructions) { - emitAdvancePC(address, debugInstructions); - - debugInstructions.add(new RestartLocal(registerNum)); - } - } - - private class PrologueEvent implements Event - { - private final int address; - - public PrologueEvent(int address) { - this.address = address; - } - - public int getAddress() { - return address; - } - - public void emit(DexFile dexFile, List debugInstructions) { - emitAdvancePC(address, debugInstructions); - - debugInstructions.add(new SetPrologueEnd()); - } - } - - private class EpilogueEvent implements Event - { - private final int address; - - public EpilogueEvent(int address) { - this.address = address; - } - - public int getAddress() { - return address; - } - - public void emit(DexFile dexFile, List debugInstructions) { - emitAdvancePC(address, debugInstructions); - - debugInstructions.add(new SetEpilogueBegin()); - } - } - - private class SetFileEvent implements Event - { - private final int address; - private final String fileName; - - public SetFileEvent(int address, String fileName) { - this.address = address; - this.fileName = fileName; - } - - public int getAddress() { - return address; - } - - public void emit(DexFile dexFile, List debugInstructions) { - emitAdvancePC(address, debugInstructions); - - debugInstructions.add(new SetFile(dexFile, new StringIdItem(dexFile, fileName))); - } - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/Util/DebugInfoDecoder.java b/dexlib/src/main/java/org/jf/dexlib/Util/DebugInfoDecoder.java deleted file mode 100644 index 4a33aabf..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Util/DebugInfoDecoder.java +++ /dev/null @@ -1,142 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.Util; - -import org.jf.dexlib.StringIdItem; -import org.jf.dexlib.TypeIdItem; -import org.jf.dexlib.DebugInfoItem; -import org.jf.dexlib.Debug.*; - -public class DebugInfoDecoder { - private final DebugInfoItem debugItem; - private final DebugInfoDelegate delegate; - private final int registers; - - public DebugInfoDecoder(DebugInfoItem debugItem, DebugInfoDelegate delegate, int registers) { - this.debugItem = debugItem; - this.delegate = delegate; - this.registers = registers; - } - - public void decode() { - int address = 0; - int line = debugItem.getLineStart(); - Local[] locals = new Local[registers]; - - for (DebugInstruction debugInst: debugItem.getDebugInstructions()) { - switch (debugInst.getOpcode()) { - case 0x00: - return; - case 0x01: - address += ((AdvancePC)debugInst).getAddressDelta(); - break; - case 0x02: - line += ((AdvanceLine)debugInst).getLineDelta(); - break; - case 0x03: - { - StartLocal inst = (StartLocal)debugInst; - Local local = new Local(inst.getRegisterNumber(), inst.getName(), inst.getType(), null); - locals[inst.getRegisterNumber()] = local; - delegate.startLocal(address, local); - break; - } - case 0x04: - { - StartLocalExtended inst = (StartLocalExtended)debugInst; - Local local = new Local(inst.getRegisterNumber(), inst.getName(), inst.getType(), - inst.getSignature()); - locals[inst.getRegisterNumber()] = local; - delegate.startLocal(address, local); - break; - } - case 0x05: - { - EndLocal inst = (EndLocal)debugInst; - Local local = locals[inst.getRegisterNumber()]; - if (local == null) { - local = new Local(inst.getRegisterNumber(), null, null, null); - } - delegate.endLocal(address, local); - break; - } - case 0x06: - { - RestartLocal inst = (RestartLocal)debugInst; - Local local = locals[inst.getRegisterNumber()]; - if (local == null) { - local = new Local(inst.getRegisterNumber(), null, null, null); - } - delegate.restartLocal(address, local); - break; - } - case 0x07: - delegate.endPrologue(address); - break; - case 0x08: - delegate.startEpilogue(address); - break; - case 0x09: - delegate.setFile(address, ((SetFile)debugInst).getFileName()); - break; - default: - { - SpecialOpcode inst = (SpecialOpcode)debugInst; - address += inst.getAddressDelta(); - line += inst.getLineDelta(); - delegate.line(address, line); - break; - } - } - } - } - - public class Local { - public final int register; - public final StringIdItem name; - public final TypeIdItem type; - public final StringIdItem signature; - public Local(int register, StringIdItem name, TypeIdItem type, StringIdItem signature) { - this.register = register; - this.name = name; - this.type = type; - this.signature = signature; - } - } - - public static interface DebugInfoDelegate { - public void endPrologue(int address); - public void startEpilogue(int address); - public void startLocal(int address, Local local); - public void endLocal(int address, Local local); - public void restartLocal(int address, Local local); - public void setFile(int address, StringIdItem fileName); - public void line(int address, int line); - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/Util/EncodedValueUtils.java b/dexlib/src/main/java/org/jf/dexlib/Util/EncodedValueUtils.java deleted file mode 100644 index 5d0f3d2d..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Util/EncodedValueUtils.java +++ /dev/null @@ -1,143 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.Util; - -public class EncodedValueUtils { - public static byte getRequiredBytesForSignedIntegralValue(long value) { - /* - * Figure out how many bits are needed to represent the value, - * including a sign bit: The bit count is subtracted from 65 - * and not 64 to account for the sign bit. The xor operation - * has the effect of leaving non-negative values alone and - * unary complementing negative values (so that a leading zero - * count always returns a useful number for our present - * purpose). - */ - int requiredBits = - 65 - Long.numberOfLeadingZeros(value ^ (value >> 63)); - - // Round up the requiredBits to a number of bytes. - return (byte)((requiredBits + 0x07) >> 3); - } - - public static long decodeSignedIntegralValue(byte[] bytes) { - long value = 0; - for (int i = 0; i < bytes.length; i++) { - value |= (((long)(bytes[i] & 0xFF)) << (i * 8)); - } - - int shift = (8 - bytes.length) * 8; - return value << shift >> shift; - } - - public static byte[] encodeSignedIntegralValue(long value) { - int requiredBytes = getRequiredBytesForSignedIntegralValue(value); - - byte[] bytes = new byte[requiredBytes]; - - for (int i = 0; i < requiredBytes; i++) { - bytes[i] = (byte) value; - value >>= 8; - } - return bytes; - } - - - - - - public static byte getRequiredBytesForUnsignedIntegralValue(long value) { - // Figure out how many bits are needed to represent the value. - int requiredBits = 64 - Long.numberOfLeadingZeros(value); - if (requiredBits == 0) { - requiredBits = 1; - } - - // Round up the requiredBits to a number of bytes. - return (byte)((requiredBits + 0x07) >> 3); - } - - public static long decodeUnsignedIntegralValue(byte[] bytes) { - long value = 0; - for (int i = 0; i < bytes.length; i++) { - value |= (((long)(bytes[i] & 0xFF)) << i * 8); - } - return value; - } - - public static byte[] encodeUnsignedIntegralValue(long value) { - int requiredBytes = getRequiredBytesForUnsignedIntegralValue(value); - - byte[] bytes = new byte[requiredBytes]; - - for (int i = 0; i < requiredBytes; i++) { - bytes[i] = (byte) value; - value >>= 8; - } - return bytes; - } - - - - - - public static int getRequiredBytesForRightZeroExtendedValue(long value) { - // Figure out how many bits are needed to represent the value. - int requiredBits = 64 - Long.numberOfTrailingZeros(value); - if (requiredBits == 0) { - requiredBits = 1; - } - - // Round up the requiredBits to a number of bytes. - return (requiredBits + 0x07) >> 3; - } - - public static long decodeRightZeroExtendedValue(byte[] bytes) { - long value = 0; - for (int i = 0; i < bytes.length; i++) { - value |= (((long)(bytes[i] & 0xFF)) << (i * 8)); - } - return value << (8 - bytes.length) * 8; - } - - public static byte[] encodeRightZeroExtendedValue(long value) { - int requiredBytes = getRequiredBytesForRightZeroExtendedValue(value); - - // Scootch the first bits to be written down to the low-order bits. - value >>= 64 - (requiredBytes * 8); - - byte[] bytes = new byte[requiredBytes]; - - for(int i = 0; i < requiredBytes; i++) { - bytes[i] = (byte)value; - value >>= 8; - } - return bytes; - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/Util/ExceptionWithContext.java b/dexlib/src/main/java/org/jf/dexlib/Util/ExceptionWithContext.java deleted file mode 100644 index fcca7fcb..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Util/ExceptionWithContext.java +++ /dev/null @@ -1,149 +0,0 @@ -/* - * Copyright (C) 2007 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jf.dexlib.Util; - -import java.io.PrintStream; -import java.io.PrintWriter; - -/** - * Exception which carries around structured context. - */ -public class ExceptionWithContext - extends RuntimeException { - /** non-null; human-oriented context of the exception */ - private StringBuffer context; - - /** - * Augments the given exception with the given context, and return the - * result. The result is either the given exception if it was an - * {@link ExceptionWithContext}, or a newly-constructed exception if it - * was not. - * - * @param ex non-null; the exception to augment - * @param str non-null; context to add - * @return non-null; an appropriate instance - */ - public static ExceptionWithContext withContext(Throwable ex, String str) { - ExceptionWithContext ewc; - - if (ex instanceof ExceptionWithContext) { - ewc = (ExceptionWithContext) ex; - } else { - ewc = new ExceptionWithContext(ex); - } - - ewc.addContext(str); - return ewc; - } - - /** - * Constructs an instance. - * - * @param message human-oriented message - */ - public ExceptionWithContext(String message) { - this(message, null); - } - - /** - * Constructs an instance. - * - * @param cause null-ok; exception that caused this one - */ - public ExceptionWithContext(Throwable cause) { - this(null, cause); - } - - /** - * Constructs an instance. - * - * @param message human-oriented message - * @param cause null-ok; exception that caused this one - */ - public ExceptionWithContext(String message, Throwable cause) { - super((message != null) ? message : - (cause != null) ? cause.getMessage() : null, - cause); - - if (cause instanceof ExceptionWithContext) { - String ctx = ((ExceptionWithContext) cause).context.toString(); - context = new StringBuffer(ctx.length() + 200); - context.append(ctx); - } else { - context = new StringBuffer(200); - } - } - - /** {@inheritDoc} */ - @Override - public void printStackTrace(PrintStream out) { - super.printStackTrace(out); - out.println(context); - } - - /** {@inheritDoc} */ - @Override - public void printStackTrace(PrintWriter out) { - super.printStackTrace(out); - out.println(context); - } - - /** - * Adds a line of context to this instance. - * - * @param str non-null; new context - */ - public void addContext(String str) { - if (str == null) { - throw new NullPointerException("str == null"); - } - - context.append(str); - if (!str.endsWith("\n")) { - context.append('\n'); - } - } - - /** - * Gets the context. - * - * @return non-null; the context - */ - public String getContext() { - return context.toString(); - } - - /** - * Prints the message and context. - * - * @param out non-null; where to print to - */ - public void printContext(PrintStream out) { - out.println(getMessage()); - out.print(context); - } - - /** - * Prints the message and context. - * - * @param out non-null; where to print to - */ - public void printContext(PrintWriter out) { - out.println(getMessage()); - out.print(context); - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/Util/FileUtils.java b/dexlib/src/main/java/org/jf/dexlib/Util/FileUtils.java deleted file mode 100644 index 2da3baec..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Util/FileUtils.java +++ /dev/null @@ -1,92 +0,0 @@ -/* - * Copyright (C) 2007 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jf.dexlib.Util; - -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; - -/** - * File I/O utilities. - */ -public final class FileUtils { - /** - * This class is uninstantiable. - */ - private FileUtils() { - // This space intentionally left blank. - } - - /** - * Reads the named file, translating {@link IOException} to a - * {@link RuntimeException} of some sort. - * - * @param fileName non-null; name of the file to read - * @return non-null; contents of the file - */ - public static byte[] readFile(String fileName) { - File file = new File(fileName); - return readFile(file); - } - - /** - * Reads the given file, translating {@link IOException} to a - * {@link RuntimeException} of some sort. - * - * @param file non-null; the file to read - * @return non-null; contents of the file - */ - public static byte[] readFile(File file) { - if (!file.exists()) { - throw new RuntimeException(file + ": file not found"); - } - - if (!file.isFile()) { - throw new RuntimeException(file + ": not a file"); - } - - if (!file.canRead()) { - throw new RuntimeException(file + ": file not readable"); - } - - long longLength = file.length(); - int length = (int) longLength; - if (length != longLength) { - throw new RuntimeException(file + ": file too long"); - } - - byte[] result = new byte[length]; - - try { - FileInputStream in = new FileInputStream(file); - int at = 0; - while (length > 0) { - int amt = in.read(result, at, length); - if (amt == -1) { - throw new RuntimeException(file + ": unexpected EOF"); - } - at += amt; - length -= amt; - } - in.close(); - } catch (IOException ex) { - throw new RuntimeException(file + ": trouble reading", ex); - } - - return result; - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/Util/Hex.java b/dexlib/src/main/java/org/jf/dexlib/Util/Hex.java deleted file mode 100644 index 60561654..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Util/Hex.java +++ /dev/null @@ -1,303 +0,0 @@ -/* - * Copyright (C) 2007 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jf.dexlib.Util; - -/** - * Utilities for formatting numbers as hexadecimal. - */ -public final class Hex { - /** - * This class is uninstantiable. - */ - private Hex() { - // This space intentionally left blank. - } - - /** - * Formats a long as an 8-byte unsigned hex value. - * - * @param v value to format - * @return non-null; formatted form - */ - public static String u8(long v) { - char[] result = new char[16]; - for (int i = 0; i < 16; i++) { - result[15 - i] = Character.forDigit((int) v & 0x0f, 16); - v >>= 4; - } - - return new String(result); - } - - /** - * Formats an int as a 4-byte unsigned hex value. - * - * @param v value to format - * @return non-null; formatted form - */ - public static String u4(int v) { - char[] result = new char[8]; - for (int i = 0; i < 8; i++) { - result[7 - i] = Character.forDigit(v & 0x0f, 16); - v >>= 4; - } - - return new String(result); - } - - /** - * Formats an int as a 3-byte unsigned hex value. - * - * @param v value to format - * @return non-null; formatted form - */ - public static String u3(int v) { - char[] result = new char[6]; - for (int i = 0; i < 6; i++) { - result[5 - i] = Character.forDigit(v & 0x0f, 16); - v >>= 4; - } - - return new String(result); - } - - /** - * Formats an int as a 2-byte unsigned hex value. - * - * @param v value to format - * @return non-null; formatted form - */ - public static String u2(int v) { - char[] result = new char[4]; - for (int i = 0; i < 4; i++) { - result[3 - i] = Character.forDigit(v & 0x0f, 16); - v >>= 4; - } - - return new String(result); - } - - /** - * Formats an int as either a 2-byte unsigned hex value - * (if the value is small enough) or a 4-byte unsigned hex value (if - * not). - * - * @param v value to format - * @return non-null; formatted form - */ - public static String u2or4(int v) { - if (v == (char) v) { - return u2(v); - } else { - return u4(v); - } - } - - /** - * Formats an int as a 1-byte unsigned hex value. - * - * @param v value to format - * @return non-null; formatted form - */ - public static String u1(int v) { - char[] result = new char[2]; - for (int i = 0; i < 2; i++) { - result[1 - i] = Character.forDigit(v & 0x0f, 16); - v >>= 4; - } - - return new String(result); - } - - /** - * Formats an int as a 4-bit unsigned hex nibble. - * - * @param v value to format - * @return non-null; formatted form - */ - public static String uNibble(int v) { - char[] result = new char[1]; - - result[0] = Character.forDigit(v & 0x0f, 16); - return new String(result); - } - - /** - * Formats a long as an 8-byte signed hex value. - * - * @param v value to format - * @return non-null; formatted form - */ - public static String s8(long v) { - char[] result = new char[17]; - - if (v < 0) { - result[0] = '-'; - v = -v; - } else { - result[0] = '+'; - } - - for (int i = 0; i < 16; i++) { - result[16 - i] = Character.forDigit((int) v & 0x0f, 16); - v >>= 4; - } - - return new String(result); - } - - /** - * Formats an int as a 4-byte signed hex value. - * - * @param v value to format - * @return non-null; formatted form - */ - public static String s4(int v) { - char[] result = new char[9]; - - if (v < 0) { - result[0] = '-'; - v = -v; - } else { - result[0] = '+'; - } - - for (int i = 0; i < 8; i++) { - result[8 - i] = Character.forDigit(v & 0x0f, 16); - v >>= 4; - } - - return new String(result); - } - - /** - * Formats an int as a 2-byte signed hex value. - * - * @param v value to format - * @return non-null; formatted form - */ - public static String s2(int v) { - char[] result = new char[5]; - - if (v < 0) { - result[0] = '-'; - v = -v; - } else { - result[0] = '+'; - } - - for (int i = 0; i < 4; i++) { - result[4 - i] = Character.forDigit(v & 0x0f, 16); - v >>= 4; - } - - return new String(result); - } - - /** - * Formats an int as a 1-byte signed hex value. - * - * @param v value to format - * @return non-null; formatted form - */ - public static String s1(int v) { - char[] result = new char[3]; - - if (v < 0) { - result[0] = '-'; - v = -v; - } else { - result[0] = '+'; - } - - for (int i = 0; i < 2; i++) { - result[2 - i] = Character.forDigit(v & 0x0f, 16); - v >>= 4; - } - - return new String(result); - } - - /** - * Formats a hex dump of a portion of a byte[]. The result - * is always newline-terminated, unless the passed-in length was zero, - * in which case the result is always the empty string (""). - * - * @param arr non-null; array to format - * @param offset >= 0; offset to the part to dump - * @param length >= 0; number of bytes to dump - * @param outOffset >= 0; first output offset to print - * @param bpl >= 0; number of bytes of output per line - * @param addressLength {2,4,6,8}; number of characters for each address - * header - * @return non-null; a string of the dump - */ - public static String dump(byte[] arr, int offset, int length, - int outOffset, int bpl, int addressLength) { - int end = offset + length; - - // twos-complement math trick: ((x < 0) || (y < 0)) <=> ((x|y) < 0) - if (((offset | length | end) < 0) || (end > arr.length)) { - throw new IndexOutOfBoundsException("arr.length " + - arr.length + "; " + - offset + "..!" + end); - } - - if (outOffset < 0) { - throw new IllegalArgumentException("outOffset < 0"); - } - - if (length == 0) { - return ""; - } - - StringBuffer sb = new StringBuffer(length * 4 + 6); - boolean bol = true; - int col = 0; - - while (length > 0) { - if (col == 0) { - String astr; - switch (addressLength) { - case 2: astr = Hex.u1(outOffset); break; - case 4: astr = Hex.u2(outOffset); break; - case 6: astr = Hex.u3(outOffset); break; - default: astr = Hex.u4(outOffset); break; - } - sb.append(astr); - sb.append(": "); - } else if ((col & 1) == 0) { - sb.append(' '); - } - sb.append(Hex.u1(arr[offset])); - outOffset++; - offset++; - col++; - if (col == bpl) { - sb.append('\n'); - col = 0; - } - length--; - } - - if (col != 0) { - sb.append('\n'); - } - - return sb.toString(); - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/Util/IndentingWriter.java b/dexlib/src/main/java/org/jf/dexlib/Util/IndentingWriter.java deleted file mode 100644 index e2be59f9..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Util/IndentingWriter.java +++ /dev/null @@ -1,169 +0,0 @@ -/* - * Copyright (C) 2007 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jf.dexlib.Util; - -import java.io.FilterWriter; -import java.io.IOException; -import java.io.Writer; - -/** - * Writer that wraps another writer and passes width-limited and - * optionally-prefixed output to its subordinate. When lines are - * wrapped they are automatically indented based on the start of the - * line. - */ -public final class IndentingWriter extends FilterWriter { - /** null-ok; optional prefix for every line */ - private final String prefix; - - /** > 0; the maximum output width */ - private final int width; - - /** > 0; the maximum indent */ - private final int maxIndent; - - /** >= 0; current output column (zero-based) */ - private int column; - - /** whether indent spaces are currently being collected */ - private boolean collectingIndent; - - /** >= 0; current indent amount */ - private int indent; - - /** - * Constructs an instance. - * - * @param out non-null; writer to send final output to - * @param width >= 0; the maximum output width (not including - * prefix), or 0 for no maximum - * @param prefix non-null; the prefix for each line - */ - public IndentingWriter(Writer out, int width, String prefix) { - super(out); - - if (out == null) { - throw new NullPointerException("out == null"); - } - - if (width < 0) { - throw new IllegalArgumentException("width < 0"); - } - - if (prefix == null) { - throw new NullPointerException("prefix == null"); - } - - this.width = (width != 0) ? width : Integer.MAX_VALUE; - this.maxIndent = width >> 1; - this.prefix = (prefix.length() == 0) ? null : prefix; - - bol(); - } - - /** - * Constructs a no-prefix instance. - * - * @param out non-null; writer to send final output to - * @param width >= 0; the maximum output width (not including - * prefix), or 0 for no maximum - */ - public IndentingWriter(Writer out, int width) { - this(out, width, ""); - } - - /** {@inheritDoc} */ - @Override - public void write(int c) throws IOException { - synchronized (lock) { - if (collectingIndent) { - if (c == ' ') { - indent++; - if (indent >= maxIndent) { - indent = maxIndent; - collectingIndent = false; - } - } else { - collectingIndent = false; - } - } - - if ((column == width) && (c != '\n')) { - out.write('\n'); - column = 0; - /* - * Note: No else, so this should fall through to the next - * if statement. - */ - } - - if (column == 0) { - if (prefix != null) { - out.write(prefix); - } - - if (!collectingIndent) { - for (int i = 0; i < indent; i++) { - out.write(' '); - } - column = indent; - } - } - - out.write(c); - - if (c == '\n') { - bol(); - } else { - column++; - } - } - } - - /** {@inheritDoc} */ - @Override - public void write(char[] cbuf, int off, int len) throws IOException { - synchronized (lock) { - while (len > 0) { - write(cbuf[off]); - off++; - len--; - } - } - } - - /** {@inheritDoc} */ - @Override - public void write(String str, int off, int len) throws IOException { - synchronized (lock) { - while (len > 0) { - write(str.charAt(off)); - off++; - len--; - } - } - } - - /** - * Indicates that output is at the beginning of a line. - */ - private void bol() { - column = 0; - collectingIndent = (maxIndent != 0); - indent = 0; - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/Util/Input.java b/dexlib/src/main/java/org/jf/dexlib/Util/Input.java deleted file mode 100644 index 1ac4f7bb..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Util/Input.java +++ /dev/null @@ -1,149 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.Util; - -/** - * Interface for a source for binary input. This is similar to - * java.util.DataInput, but no IOExceptions - * are declared, and multibyte input is defined to be little-endian. - */ -public interface Input { - /** - * Gets the current cursor position. This is the same as the number of - * bytes read from this instance. - * - * @return >= 0; the cursor position - */ - public int getCursor(); - - /** - * Sets the current cursor position. - * - * @return >= 0; the cursor position - */ - public void setCursor(int cursor); - - /** - * Asserts that the cursor is the given value. - * - * @param expectedCursor the expected cursor value - * @throws RuntimeException thrown if getCursor() != - * expectedCursor - */ - public void assertCursor(int expectedCursor); - - /** - * Reads a byte from this instance. - * - * @return the byte value that was read - */ - public byte readByte(); - - /** - * Reads a short from this instance. - * - * @return the short value that was read, as an int - */ - public int readShort(); - - /** - * Reads an int from this instance. - * - * @return the unsigned int value that was read - */ - public int readInt(); - - /** - * Reads a long from this instance. - * - * @return the long value that was read - */ - public long readLong(); - - - /** - * Reads a DWARFv3-style signed LEB128 integer. For details, - * see the "Dalvik Executable Format" document or DWARF v3 section - * 7.6. - * - * @return the integer value that was read - */ - public int readSignedLeb128(); - - /** - * Reads a DWARFv3-style unsigned LEB128 integer. For details, - * see the "Dalvik Executable Format" document or DWARF v3 section - * 7.6. - * - * @return the integer value that was read - */ - public int readUnsignedLeb128(); - - /** - * reads a byte[] from this instance. - * - * @param bytes non-null; the buffer to read the data into - * @param offset >= 0; offset into bytes for the first - * byte to write - * @param length >= 0; number of bytes to read - */ - public void read(byte[] bytes, int offset, int length); - - /** - * reads a byte[] from this instance. This is just - * a convenient shorthand for read(bytes, 0, bytes.length). - * - * @param bytes non-null; the buffer to read the data into - */ - public void read(byte[] bytes); - - - /** - * reads a byte[] from this instance - * - * @param length >= 0; number of bytes to read - * @return a byte array containing length bytes - */ - public byte[] readBytes(int length); - - /** - * Skips the given number of bytes. - * - * @param count >= 0; the number of bytes to skip - */ - public void skipBytes(int count); - - /** - * Skip extra bytes if necessary to force alignment of the output - * cursor as given. - * - * @param alignment > 0; the alignment; must be a power of two - */ - public void alignTo(int alignment); -} diff --git a/dexlib/src/main/java/org/jf/dexlib/Util/Leb128Utils.java b/dexlib/src/main/java/org/jf/dexlib/Util/Leb128Utils.java deleted file mode 100644 index b76189bc..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Util/Leb128Utils.java +++ /dev/null @@ -1,78 +0,0 @@ -/* - * Copyright (C) 2008 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jf.dexlib.Util; - -/** - * LEB128 (little-endian base 128) utilities. - */ -public final class Leb128Utils { - /** - * This class is uninstantiable. - */ - private Leb128Utils() { - // This space intentionally left blank. - } - - /** - * Gets the number of bytes in the unsigned LEB128 encoding of the - * given value. - * - * @param value the value in question - * @return its write size, in bytes - */ - public static int unsignedLeb128Size(int value) { - // TODO: This could be much cleverer. - long lValue = value & 0xFFFFFFFFL; - - long remaining = lValue >> 7L; - int count = 0; - - while (remaining != 0L) { - remaining >>= 7L; - count++; - } - - return count + 1; - } - - /** - * Gets the number of bytes in the signed LEB128 encoding of the - * given value. - * - * @param value the value in question - * @return its write size, in bytes - */ - public static int signedLeb128Size(int value) { - // TODO: This could be much cleverer. - - int remaining = value >> 7; - int count = 0; - boolean hasMore = true; - int end = ((value & Integer.MIN_VALUE) == 0) ? 0 : -1; - - while (hasMore) { - hasMore = (remaining != end) - || ((remaining & 1) != ((value >> 6) & 1)); - - value = remaining; - remaining >>= 7; - count++; - } - - return count; - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/Util/NumberUtils.java b/dexlib/src/main/java/org/jf/dexlib/Util/NumberUtils.java deleted file mode 100644 index 644c8b1c..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Util/NumberUtils.java +++ /dev/null @@ -1,78 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.Util; - -public class NumberUtils { - - public static byte decodeHighSignedNibble(byte b) { - return (byte)(b >> 4); - } - - public static byte decodeHighUnsignedNibble(byte b) { - return (byte)((b & 0xFF) >>> 4); - } - - public static byte decodeLowUnsignedNibble(byte b) { - return (byte)(b & 0x0F); - } - - public static short decodeUnsignedByte(byte b) { - return (short)(b & 0xFF); - } - - public static short decodeShort(byte lsb, byte msb) { - return (short) - ( (lsb & 0xFF) | - (msb << 8) - ); - } - - public static int decodeUnsignedShort(byte lsb, byte msb) { - return ((msb & 0xFF) << 8) | - (lsb & 0xFF); - } - - public static int decodeInt(byte lsb, byte mlsb, byte mmsb, byte msb) { - return (lsb & 0xFF) | - ((mlsb & 0xFF) << 8) | - ((mmsb & 0xFF) << 16) | - (msb << 24); - } - - public static long decodeLong(byte[] array, int startIndex) { - return (array[startIndex++] & 0xffL) | - ((array[startIndex++] & 0xffL) << 8) | - ((array[startIndex++] & 0xffL) << 16) | - ((array[startIndex++] & 0xffL) << 24) | - ((array[startIndex++] & 0xffL) << 32) | - ((array[startIndex++] & 0xffL) << 40) | - ((array[startIndex++] & 0xffL) << 48) | - ((array[startIndex] & 0xffL) << 56); - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/Util/Output.java b/dexlib/src/main/java/org/jf/dexlib/Util/Output.java deleted file mode 100644 index 44959876..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Util/Output.java +++ /dev/null @@ -1,129 +0,0 @@ -/* - * Copyright (C) 2007 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jf.dexlib.Util; - -/** - * Interface for a sink for binary output. This is similar to - * java.util.DataOutput, but no IOExceptions - * are declared, and multibyte output is defined to be little-endian. - */ -public interface Output { - /** - * Gets the current cursor position. This is the same as the number of - * bytes written to this instance. - * - * @return >= 0; the cursor position - */ - public int getCursor(); - - /** - * Asserts that the cursor is the given value. - * - * @param expectedCursor the expected cursor value - * @throws RuntimeException thrown if getCursor() != - * expectedCursor - */ - public void assertCursor(int expectedCursor); - - /** - * Writes a byte to this instance. - * - * @param value the value to write; all but the low 8 bits are ignored - */ - public void writeByte(int value); - - /** - * Writes a short to this instance. - * - * @param value the value to write; all but the low 16 bits are ignored - */ - public void writeShort(int value); - - /** - * Writes an int to this instance. - * - * @param value the value to write - */ - public void writeInt(int value); - - /** - * Writes a long to this instance. - * - * @param value the value to write - */ - public void writeLong(long value); - - /** - * Writes a DWARFv3-style unsigned LEB128 integer. For details, - * see the "Dalvik Executable Format" document or DWARF v3 section - * 7.6. - * - * @param value value to write, treated as an unsigned value - * @return 1..5; the number of bytes actually written - */ - public int writeUnsignedLeb128(int value); - - /** - * Writes a DWARFv3-style unsigned LEB128 integer. For details, - * see the "Dalvik Executable Format" document or DWARF v3 section - * 7.6. - * - * @param value value to write - * @return 1..5; the number of bytes actually written - */ - public int writeSignedLeb128(int value); - - /** - * Writes a {@link ByteArray} to this instance. - * - * @param bytes non-null; the array to write - */ - public void write(ByteArray bytes); - - /** - * Writes a portion of a byte[] to this instance. - * - * @param bytes non-null; the array to write - * @param offset >= 0; offset into bytes for the first - * byte to write - * @param length >= 0; number of bytes to write - */ - public void write(byte[] bytes, int offset, int length); - - /** - * Writes a byte[] to this instance. This is just - * a convenient shorthand for write(bytes, 0, bytes.length). - * - * @param bytes non-null; the array to write - */ - public void write(byte[] bytes); - - /** - * Writes the given number of 0 bytes. - * - * @param count >= 0; the number of zeroes to write - */ - public void writeZeroes(int count); - - /** - * Adds extra bytes if necessary (with value 0) to - * force alignment of the output cursor as given. - * - * @param alignment > 0; the alignment; must be a power of two - */ - public void alignTo(int alignment); -} diff --git a/dexlib/src/main/java/org/jf/dexlib/Util/Pair.java b/dexlib/src/main/java/org/jf/dexlib/Util/Pair.java deleted file mode 100644 index 2eb09616..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Util/Pair.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.Util; - -public class Pair { - public final A first; - public final B second; - - public Pair(A first, B second) { - this.first = first; - this.second = second; - } -} - diff --git a/dexlib/src/main/java/org/jf/dexlib/Util/TryListBuilder.java b/dexlib/src/main/java/org/jf/dexlib/Util/TryListBuilder.java deleted file mode 100644 index d65913cf..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Util/TryListBuilder.java +++ /dev/null @@ -1,350 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.Util; - -import org.jf.dexlib.CodeItem; -import org.jf.dexlib.DexFile; -import org.jf.dexlib.TypeIdItem; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; - -public class TryListBuilder -{ - /*TODO: add logic to merge adjacent, identical try blocks, and remove superflous handlers - Also provide a "strict" mode, where the above isn't performed, which will be useful to be able to - exactly reproduce the original .dex file (for testing/verification purposes)*/ - - - private TryRange firstTryRange = new TryRange(0,0); - private TryRange lastTryRange = new TryRange(0,0); - - public TryListBuilder() { - firstTryRange.next = lastTryRange; - lastTryRange.previous = firstTryRange; - } - - private class TryRange - { - public TryRange previous = null; - public TryRange next = null; - - public int startAddress; - public int endAddress; - public LinkedList handlers; - - public int catchAllHandlerAddress; - - public TryRange(int startAddress, int endAddress) { - this.startAddress = startAddress; - this.endAddress = endAddress; - this.handlers = new LinkedList(); - this.previous = null; - this.next = null; - catchAllHandlerAddress = -1; - } - - public void append(TryRange tryRange) { - /*we use a dummy last item, so this.next will always - have a value*/ - this.next.previous = tryRange; - tryRange.next = this.next; - - this.next = tryRange; - tryRange.previous = this; - } - - public void prepend(TryRange tryRange){ - /*we use a dummy first item, so this.previous will always - have a value*/ - this.previous.next = tryRange; - tryRange.previous = this.previous; - - this.previous = tryRange; - tryRange.next = this; - } - - /** - * This splits the current range into two ranges at the given - * address. The existing range will be shortened to the first - * half, and a new range will be created and returned for the - * 2nd half. - * @param address The address to split at - * @return The 2nd half of the - */ - public TryRange split(int address) { - //this is a private class, so address is assumed - //to be valid - - TryRange tryRange = new TryRange(address, endAddress); - tryRange.catchAllHandlerAddress = this.catchAllHandlerAddress; - tryRange.handlers.addAll(this.handlers); - append(tryRange); - - this.endAddress = address; - - return tryRange; - } - - public void appendHandler(Handler handler) { - handlers.addLast(handler); - } - - public void prependHandler(Handler handler) { - handlers.addFirst(handler); - } - } - - private class Handler - { - public final TypeIdItem type; - public final int handlerAddress; - - public Handler(TypeIdItem type, int handlerAddress) { - this.type = type; - this.handlerAddress = handlerAddress; - } - } - - public Pair, List> encodeTries(DexFile dexFile) { - if (firstTryRange.next == lastTryRange) { - return new Pair, List>(null, null); - } - - ArrayList tries = new ArrayList(); - ArrayList handlers = new ArrayList(); - - HashMap handlerDict = - new HashMap(); - - TryRange tryRange = firstTryRange.next; - - while (tryRange != lastTryRange) { - ArrayList encodedTypeAddrPairs = - new ArrayList(); - - for (Handler handler: tryRange.handlers) { - CodeItem.EncodedTypeAddrPair encodedTypeAddrPair = new CodeItem.EncodedTypeAddrPair( - dexFile, - handler.type, - handler.handlerAddress); - encodedTypeAddrPairs.add(encodedTypeAddrPair); - } - - - CodeItem.EncodedCatchHandler encodedCatchHandler = new CodeItem.EncodedCatchHandler( - dexFile, - encodedTypeAddrPairs, - tryRange.catchAllHandlerAddress); - CodeItem.EncodedCatchHandler internedEncodedCatchHandler = handlerDict.get(encodedCatchHandler); - if (internedEncodedCatchHandler == null) { - handlerDict.put(encodedCatchHandler, encodedCatchHandler); - handlers.add(encodedCatchHandler); - } else { - encodedCatchHandler = internedEncodedCatchHandler; - } - - CodeItem.TryItem tryItem = new CodeItem.TryItem( - tryRange.startAddress, - tryRange.endAddress - tryRange.startAddress, - encodedCatchHandler); - tries.add(tryItem); - - tryRange = tryRange.next; - } - - return new Pair, List>(tries, handlers); - } - - public void addCatchAllHandler(int startAddress, int endAddress, int handlerAddress) { - TryRange startRange = null; - TryRange endRange = null; - - Pair ranges = getBoundingRanges(startAddress, endAddress); - startRange = ranges.first; - endRange = ranges.second; - - int previousEnd = startAddress; - TryRange tryRange = startRange; - - /*Now we have the start and end ranges that exactly match the start and end - of the range being added. We need to iterate over all the ranges from the start - to end range inclusively, and append the handler to the end of each range's handler - list. We also need to create a new range for any "holes" in the existing ranges*/ - do - { - //is there a hole? If so, add a new range to fill the hole - if (tryRange.startAddress > previousEnd) { - TryRange newRange = new TryRange(previousEnd, tryRange.startAddress); - tryRange.prepend(newRange); - tryRange = newRange; - } - - if (tryRange.catchAllHandlerAddress == -1) { - tryRange.catchAllHandlerAddress = handlerAddress; - } - - previousEnd = tryRange.endAddress; - tryRange = tryRange.next; - } while (tryRange.previous != endRange); - } - - public Pair getBoundingRanges(int startAddress, int endAddress) { - TryRange startRange = null; - TryRange endRange = null; - - TryRange tryRange = firstTryRange.next; - while (tryRange != lastTryRange) { - if (startAddress == tryRange.startAddress) { - //|-----| - //^------ - /*Bam. We hit the start of the range right on the head*/ - startRange = tryRange; - break; - } else if (startAddress > tryRange.startAddress && startAddress < tryRange.endAddress) { - //|-----| - // ^---- - /*Almost. The start of the range being added is in the middle - of an existing try range. We need to split the existing range - at the start address of the range being added*/ - startRange = tryRange.split(startAddress); - break; - }else if (startAddress < tryRange.startAddress) { - if (endAddress <= tryRange.startAddress) { - // |-----| - //^--^ - /*Oops, totally too far! The new range doesn't overlap any existing - ones, so we just add it and return*/ - startRange = new TryRange(startAddress, endAddress); - tryRange.prepend(startRange); - return new Pair(startRange, startRange); - } else { - // |-----| - //^--------- - /*Oops, too far! We've passed the start of the range being added, but - the new range does overlap this one. We need to add a new range just - before this one*/ - startRange = new TryRange(startAddress, tryRange.startAddress); - tryRange.prepend(startRange); - break; - } - } - - tryRange = tryRange.next; - } - - //|-----| - // ^----- - /*Either the list of tries is blank, or all the tries in the list - end before the range being added starts. In either case, we just need - to add a new range at the end of the list*/ - if (startRange == null) { - startRange = new TryRange(startAddress, endAddress); - lastTryRange.prepend(startRange); - return new Pair(startRange, startRange); - } - - tryRange = startRange; - while (tryRange != lastTryRange) { - if (tryRange.endAddress == endAddress) { - //|-----| - //------^ - /*Bam! We hit the end right on the head.*/ - endRange = tryRange; - break; - } else if (tryRange.startAddress < endAddress && tryRange.endAddress > endAddress) { - //|-----| - //--^ - /*Almost. The range being added ends in the middle of an - existing range. We need to split the existing range - at the end of the range being added.*/ - tryRange.split(endAddress); - endRange = tryRange; - break; - } else if (tryRange.startAddress >= endAddress) { - //|-----| |-----| - //-----------^ - /*Oops, too far! The current range starts after the range being added - ends. We need to create a new range that starts at the end of the - previous range, and ends at the end of the range being added*/ - endRange = new TryRange(tryRange.previous.endAddress, endAddress); - tryRange.prepend(endRange); - break; - } - tryRange = tryRange.next; - } - - //|-----| - //--------^ - /*The last range in the list ended before the end of the range being added. - We need to add a new range that starts at the end of the last range in the - list, and ends at the end of the range being added.*/ - if (endRange == null) { - endRange = new TryRange(lastTryRange.previous.endAddress, endAddress); - lastTryRange.prepend(endRange); - } - - return new Pair(startRange, endRange); - } - - public void addHandler(TypeIdItem type, int startAddress, int endAddress, int handlerAddress) { - TryRange startRange = null; - TryRange endRange = null; - - //TODO: need to check for pre-existing exception types in the handler list? - - Pair ranges = getBoundingRanges(startAddress, endAddress); - startRange = ranges.first; - endRange = ranges.second; - Handler handler = new Handler(type, handlerAddress); - - int previousEnd = startAddress; - TryRange tryRange = startRange; - - /*Now we have the start and end ranges that exactly match the start and end - of the range being added. We need to iterate over all the ranges from the start - to end range inclusively, and append the handler to the end of each range's handler - list. We also need to create a new range for any "holes" in the existing ranges*/ - do - { - //is there a hole? If so, add a new range to fill the hole - if (tryRange.startAddress > previousEnd) { - TryRange newRange = new TryRange(previousEnd, tryRange.startAddress); - tryRange.prepend(newRange); - tryRange = newRange; - } - - tryRange.appendHandler(handler); - previousEnd = tryRange.endAddress; - tryRange = tryRange.next; - } while (tryRange.previous != endRange); - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/Util/TwoColumnOutput.java b/dexlib/src/main/java/org/jf/dexlib/Util/TwoColumnOutput.java deleted file mode 100644 index fde4bca1..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Util/TwoColumnOutput.java +++ /dev/null @@ -1,250 +0,0 @@ -/* - * Copyright (C) 2007 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jf.dexlib.Util; - -import java.io.*; - -/** - * Class that takes a combined output destination and provides two - * output writers, one of which ends up writing to the left column and - * one which goes on the right. - */ -public final class TwoColumnOutput { - /** non-null; underlying writer for final output */ - private final Writer out; - - /** > 0; the left column width */ - private final int leftWidth; - - /** non-null; pending left column output */ - private final StringBuffer leftBuf; - - /** non-null; pending right column output */ - private final StringBuffer rightBuf; - - /** non-null; left column writer */ - private final IndentingWriter leftColumn; - - /** non-null; right column writer */ - private final IndentingWriter rightColumn; - - /** - * Turns the given two strings (with widths) and spacer into a formatted - * two-column string. - * - * @param s1 non-null; first string - * @param width1 > 0; width of the first column - * @param spacer non-null; spacer string - * @param s2 non-null; second string - * @param width2 > 0; width of the second column - * @return non-null; an appropriately-formatted string - */ - public static String toString(String s1, int width1, String spacer, - String s2, int width2) { - int len1 = s1.length(); - int len2 = s2.length(); - - StringWriter sw = new StringWriter((len1 + len2) * 3); - TwoColumnOutput twoOut = - new TwoColumnOutput(sw, width1, width2, spacer); - - try { - twoOut.getLeft().write(s1); - twoOut.getRight().write(s2); - } catch (IOException ex) { - throw new RuntimeException("shouldn't happen", ex); - } - - twoOut.flush(); - return sw.toString(); - } - - /** - * Constructs an instance. - * - * @param out non-null; writer to send final output to - * @param leftWidth > 0; width of the left column, in characters - * @param rightWidth > 0; width of the right column, in characters - * @param spacer non-null; spacer string to sit between the two columns - */ - public TwoColumnOutput(Writer out, int leftWidth, int rightWidth, - String spacer) { - if (out == null) { - throw new NullPointerException("out == null"); - } - - if (leftWidth < 1) { - throw new IllegalArgumentException("leftWidth < 1"); - } - - if (rightWidth < 1) { - throw new IllegalArgumentException("rightWidth < 1"); - } - - if (spacer == null) { - throw new NullPointerException("spacer == null"); - } - - StringWriter leftWriter = new StringWriter(1000); - StringWriter rightWriter = new StringWriter(1000); - - this.out = out; - this.leftWidth = leftWidth; - this.leftBuf = leftWriter.getBuffer(); - this.rightBuf = rightWriter.getBuffer(); - this.leftColumn = new IndentingWriter(leftWriter, leftWidth); - this.rightColumn = - new IndentingWriter(rightWriter, rightWidth, spacer); - } - - /** - * Constructs an instance. - * - * @param out non-null; stream to send final output to - * @param leftWidth >= 1; width of the left column, in characters - * @param rightWidth >= 1; width of the right column, in characters - * @param spacer non-null; spacer string to sit between the two columns - */ - public TwoColumnOutput(OutputStream out, int leftWidth, int rightWidth, - String spacer) { - this(new OutputStreamWriter(out), leftWidth, rightWidth, spacer); - } - - /** - * Gets the writer to use to write to the left column. - * - * @return non-null; the left column writer - */ - public Writer getLeft() { - return leftColumn; - } - - /** - * Gets the writer to use to write to the right column. - * - * @return non-null; the right column writer - */ - public Writer getRight() { - return rightColumn; - } - - /** - * Flushes the output. If there are more lines of pending output in one - * column, then the other column will get filled with blank lines. - */ - public void flush() { - try { - appendNewlineIfNecessary(leftBuf, leftColumn); - appendNewlineIfNecessary(rightBuf, rightColumn); - outputFullLines(); - flushLeft(); - flushRight(); - } catch (IOException ex) { - throw new RuntimeException(ex); - } - } - - /** - * Outputs to the final destination as many full line pairs as - * there are in the pending output, removing those lines from - * their respective buffers. This method terminates when at - * least one of the two column buffers is empty. - */ - private void outputFullLines() throws IOException { - for (;;) { - int leftLen = leftBuf.indexOf("\n"); - if (leftLen < 0) { - return; - } - - int rightLen = rightBuf.indexOf("\n"); - if (rightLen < 0) { - return; - } - - if (leftLen != 0) { - out.write(leftBuf.substring(0, leftLen)); - } - - if (rightLen != 0) { - writeSpaces(out, leftWidth - leftLen); - out.write(rightBuf.substring(0, rightLen)); - } - - out.write('\n'); - - leftBuf.delete(0, leftLen + 1); - rightBuf.delete(0, rightLen + 1); - } - } - - /** - * Flushes the left column buffer, printing it and clearing the buffer. - * If the buffer is already empty, this does nothing. - */ - private void flushLeft() throws IOException { - appendNewlineIfNecessary(leftBuf, leftColumn); - - while (leftBuf.length() != 0) { - rightColumn.write('\n'); - outputFullLines(); - } - } - - /** - * Flushes the right column buffer, printing it and clearing the buffer. - * If the buffer is already empty, this does nothing. - */ - private void flushRight() throws IOException { - appendNewlineIfNecessary(rightBuf, rightColumn); - - while (rightBuf.length() != 0) { - leftColumn.write('\n'); - outputFullLines(); - } - } - - /** - * Appends a newline to the given buffer via the given writer, but - * only if it isn't empty and doesn't already end with one. - * - * @param buf non-null; the buffer in question - * @param out non-null; the writer to use - */ - private static void appendNewlineIfNecessary(StringBuffer buf, - Writer out) - throws IOException { - int len = buf.length(); - - if ((len != 0) && (buf.charAt(len - 1) != '\n')) { - out.write('\n'); - } - } - - /** - * Writes the given number of spaces to the given writer. - * - * @param out non-null; where to write - * @param amt >= 0; the number of spaces to write - */ - private static void writeSpaces(Writer out, int amt) throws IOException { - while (amt > 0) { - out.write(' '); - amt--; - } - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/Util/TypeUtils.java b/dexlib/src/main/java/org/jf/dexlib/Util/TypeUtils.java deleted file mode 100644 index a01f8364..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Util/TypeUtils.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.dexlib.Util; - -import org.jf.dexlib.DexFile; -import org.jf.dexlib.EncodedValue.*; - -public class TypeUtils -{ - public static EncodedValueSubField makeDefaultValueForType(DexFile dexFile, String type) { - EncodedValueSubField subField; - switch (type.charAt(0)) { - case 'Z': - return new BoolEncodedValueSubField(false); - case 'B': - return new ByteEncodedValueSubField((byte)0); - case 'S': - return new ShortEncodedValueSubField((short)0); - case 'C': - return new CharEncodedValueSubField((char)0); - case 'I': - return new IntEncodedValueSubField(0); - case 'J': - return new LongEncodedValueSubField(0); - case 'F': - return new FloatEncodedValueSubField(0); - case 'D': - return new DoubleEncodedValueSubField(0); - case 'L': - case '[': - return new NullEncodedValueSubField(); - } - return null; - } -} diff --git a/dexlib/src/main/java/org/jf/dexlib/Util/Utf8Utils.java b/dexlib/src/main/java/org/jf/dexlib/Util/Utf8Utils.java deleted file mode 100644 index dfd073d8..00000000 --- a/dexlib/src/main/java/org/jf/dexlib/Util/Utf8Utils.java +++ /dev/null @@ -1,192 +0,0 @@ -/* - * Copyright (C) 2007 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jf.dexlib.Util; - -/** - * Constants of type CONSTANT_Utf8_info. - */ -public final class Utf8Utils { - - - /** - * Converts a string into its Java-style UTF-8 form. Java-style UTF-8 - * differs from normal UTF-8 in the handling of character '\0' and - * surrogate pairs. - * - * @param string non-null; the string to convert - * @return non-null; the UTF-8 bytes for it - */ - public static byte[] stringToUtf8Bytes(String string) { - int len = string.length(); - byte[] bytes = new byte[len * 3]; // Avoid having to reallocate. - int outAt = 0; - - for (int i = 0; i < len; i++) { - char c = string.charAt(i); - if ((c != 0) && (c < 0x80)) { - bytes[outAt] = (byte) c; - outAt++; - } else if (c < 0x800) { - bytes[outAt] = (byte) (((c >> 6) & 0x1f) | 0xc0); - bytes[outAt + 1] = (byte) ((c & 0x3f) | 0x80); - outAt += 2; - } else { - bytes[outAt] = (byte) (((c >> 12) & 0x0f) | 0xe0); - bytes[outAt + 1] = (byte) (((c >> 6) & 0x3f) | 0x80); - bytes[outAt + 2] = (byte) ((c & 0x3f) | 0x80); - outAt += 3; - } - } - - byte[] result = new byte[outAt]; - System.arraycopy(bytes, 0, result, 0, outAt); - return result; - } - - /** - * Converts an array of UTF-8 bytes into a string. - * - * @param bytes non-null; the bytes to convert - * @return non-null; the converted string - */ - public static String utf8BytesToString(ByteArray bytes) { - int length = bytes.size(); - char[] chars = new char[length]; // This is sized to avoid a realloc. - int outAt = 0; - - for (int at = 0; length > 0; /*at*/) { - int v0 = bytes.getUnsignedByte(at); - char out; - switch (v0 >> 4) { - case 0x00: case 0x01: case 0x02: case 0x03: - case 0x04: case 0x05: case 0x06: case 0x07: { - // 0XXXXXXX -- single-byte encoding - length--; - if (v0 == 0) { - // A single zero byte is illegal. - return throwBadUtf8(v0, at); - } - out = (char) v0; - at++; - break; - } - case 0x0c: case 0x0d: { - // 110XXXXX -- two-byte encoding - length -= 2; - if (length < 0) { - return throwBadUtf8(v0, at); - } - int v1 = bytes.getUnsignedByte(at + 1); - if ((v1 & 0xc0) != 0x80) { - return throwBadUtf8(v1, at + 1); - } - int value = ((v0 & 0x1f) << 6) | (v1 & 0x3f); - if ((value != 0) && (value < 0x80)) { - /* - * This should have been represented with - * one-byte encoding. - */ - return throwBadUtf8(v1, at + 1); - } - out = (char) value; - at += 2; - break; - } - case 0x0e: { - // 1110XXXX -- three-byte encoding - length -= 3; - if (length < 0) { - return throwBadUtf8(v0, at); - } - int v1 = bytes.getUnsignedByte(at + 1); - if ((v1 & 0xc0) != 0x80) { - return throwBadUtf8(v1, at + 1); - } - int v2 = bytes.getUnsignedByte(at + 2); - if ((v1 & 0xc0) != 0x80) { - return throwBadUtf8(v2, at + 2); - } - int value = ((v0 & 0x0f) << 12) | ((v1 & 0x3f) << 6) | - (v2 & 0x3f); - if (value < 0x800) { - /* - * This should have been represented with one- or - * two-byte encoding. - */ - return throwBadUtf8(v2, at + 2); - } - out = (char) value; - at += 3; - break; - } - default: { - // 10XXXXXX, 1111XXXX -- illegal - return throwBadUtf8(v0, at); - } - } - chars[outAt] = out; - outAt++; - } - - return new String(chars, 0, outAt); - } - - /** - * Helper for {@link #utf8BytesToString}, which throws the right - * exception for a bogus utf-8 byte. - * - * @param value the byte value - * @param offset the file offset - * @return never - * @throws IllegalArgumentException always thrown - */ - private static String throwBadUtf8(int value, int offset) { - throw new IllegalArgumentException("bad utf-8 byte " + Hex.u1(value) + - " at offset " + Hex.u4(offset)); - } - - public static String escapeString(String value) { - int len = value.length(); - StringBuilder sb = new StringBuilder(len * 3 / 2); - - for (int i = 0; i < len; i++) { - char c = value.charAt(i); - - if ((c >= ' ') && (c < 0x7f)) { - if ((c == '\'') || (c == '\"') || (c == '\\')) { - sb.append('\\'); - } - sb.append(c); - continue; - } else if (c <= 0x7f) { - switch (c) { - case '\n': sb.append("\\n"); continue; - case '\r': sb.append("\\r"); continue; - case '\t': sb.append("\\t"); continue; - } - } - - sb.append("\\u"); - sb.append(Character.forDigit(c >> 12, 16)); - sb.append(Character.forDigit((c >> 8) & 0x0f, 16)); - sb.append(Character.forDigit((c >> 4) & 0x0f, 16)); - sb.append(Character.forDigit(c & 0x0f, 16)); - } - - return sb.toString(); - } -} diff --git a/dexlib/src/test/java/TryListBuilderTest.java b/dexlib/src/test/java/TryListBuilderTest.java deleted file mode 100644 index 55df4f31..00000000 --- a/dexlib/src/test/java/TryListBuilderTest.java +++ /dev/null @@ -1,473 +0,0 @@ -/* - * [The "BSD licence"] - * Copyright (c) 2009 Ben Gruver - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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. - */ - - -import org.jf.dexlib.CodeItem; -import org.jf.dexlib.DexFile; -import org.jf.dexlib.TypeIdItem; -import org.jf.dexlib.Util.Pair; -import org.jf.dexlib.Util.TryListBuilder; -import org.junit.Assert; -import org.junit.Test; - -import java.util.List; - - -public class TryListBuilderTest -{ - - private static class Handler - { - public String type; - public int handlerAddress; - - public Handler(String type, int handlerAddress) { - this.type = type; - this.handlerAddress = handlerAddress; - } - } - - public static void checkTry(CodeItem.TryItem tryItem, - int startAddress, - int endAddress, - int catchAllAddress, - Handler[] handlers) { - - Assert.assertTrue(tryItem.getStartAddress() == startAddress); - Assert.assertTrue(tryItem.getEndAddress() == endAddress); - - CodeItem.EncodedCatchHandler encodedCatchHandler = tryItem.getHandler(); - - Assert.assertTrue(encodedCatchHandler.getCatchAllAddress() == catchAllAddress); - - List typeAddrPairs = encodedCatchHandler.getHandlers(); - - Assert.assertTrue(typeAddrPairs.size() == handlers.length); - - for (int i=0; i, List> retVal = tryListBuilder.encodeTries(dexFile); - List tries = retVal.first; - - Assert.assertTrue(tries.size() == 1); - checkTry(tries.get(0), 2, 5, -1, new Handler[]{new Handler("Ljava/lang/Exception;", 100)}); - } - - - @Test - public void singleTryWithCatchAllTest() { - TryListBuilder tryListBuilder = new TryListBuilder(); - - DexFile dexFile = new DexFile(); - TypeIdItem typeIdItem = new TypeIdItem(dexFile, "Ljava/lang/Exception;"); - tryListBuilder.addHandler(typeIdItem, 2, 5, 100); - - tryListBuilder.addCatchAllHandler(2, 5, 101); - - Pair, List> retVal = tryListBuilder.encodeTries(dexFile); - List tries = retVal.first; - - Assert.assertTrue(tries.size() == 1); - checkTry(tries.get(0), 2, 5, 101, new Handler[]{new Handler("Ljava/lang/Exception;", 100)}); - } - - @Test - public void twoTriesTest1() { - //|-----| - // |-----| - TryListBuilder tryListBuilder = new TryListBuilder(); - - DexFile dexFile = new DexFile(); - TypeIdItem typeIdItem = new TypeIdItem(dexFile, "Ljava/lang/Exception;"); - tryListBuilder.addHandler(typeIdItem, 2, 5, 100); - - tryListBuilder.addHandler(typeIdItem, 5, 10, 101); - - - Pair, List> retVal = tryListBuilder.encodeTries(dexFile); - List tries = retVal.first; - - Assert.assertTrue(tries.size() == 2); - checkTry(tries.get(0), 2, 5, -1, new Handler[]{new Handler("Ljava/lang/Exception;", 100)}); - checkTry(tries.get(1), 5, 10, -1, new Handler[]{new Handler("Ljava/lang/Exception;", 101)}); - } - - @Test - public void twoTriesTest2() { - //|-----| - // |-----| - TryListBuilder tryListBuilder = new TryListBuilder(); - - DexFile dexFile = new DexFile(); - TypeIdItem typeIdItem = new TypeIdItem(dexFile, "Ljava/lang/Exception;"); - tryListBuilder.addHandler(typeIdItem, 2, 5, 100); - - tryListBuilder.addHandler(typeIdItem, 10, 15, 101); - - - Pair, List> retVal = tryListBuilder.encodeTries(dexFile); - List tries = retVal.first; - - Assert.assertTrue(tries.size() == 2); - checkTry(tries.get(0), 2, 5, -1, new Handler[]{new Handler("Ljava/lang/Exception;", 100)}); - checkTry(tries.get(1), 10, 15, -1, new Handler[]{new Handler("Ljava/lang/Exception;", 101)}); - } - - @Test - public void twoTriesTest3() { - // |-----| - //|-----| - TryListBuilder tryListBuilder = new TryListBuilder(); - - DexFile dexFile = new DexFile(); - TypeIdItem typeIdItem = new TypeIdItem(dexFile, "Ljava/lang/Exception;"); - tryListBuilder.addHandler(typeIdItem, 5, 10, 101); - tryListBuilder.addHandler(typeIdItem, 2, 5, 100); - - - Pair, List> retVal = tryListBuilder.encodeTries(dexFile); - List tries = retVal.first; - - Assert.assertTrue(tries.size() == 2); - checkTry(tries.get(0), 2, 5, -1, new Handler[]{new Handler("Ljava/lang/Exception;", 100)}); - checkTry(tries.get(1), 5, 10, -1, new Handler[]{new Handler("Ljava/lang/Exception;", 101)}); - } - - @Test - public void twoTriesTest4() { - // |-----| - //|-----| - TryListBuilder tryListBuilder = new TryListBuilder(); - - DexFile dexFile = new DexFile(); - TypeIdItem typeIdItem = new TypeIdItem(dexFile, "Ljava/lang/Exception;"); - - tryListBuilder.addHandler(typeIdItem, 10, 15, 101); - - tryListBuilder.addHandler(typeIdItem, 2, 5, 100); - - - Pair, List> retVal = tryListBuilder.encodeTries(dexFile); - List tries = retVal.first; - - Assert.assertTrue(tries.size() == 2); - checkTry(tries.get(0), 2, 5, -1, new Handler[]{new Handler("Ljava/lang/Exception;", 100)}); - checkTry(tries.get(1), 10, 15, -1, new Handler[]{new Handler("Ljava/lang/Exception;", 101)}); - } - - @Test - public void twoTriesTest5() { - //|-----| - //|-----| - TryListBuilder tryListBuilder = new TryListBuilder(); - - DexFile dexFile = new DexFile(); - TypeIdItem typeIdItem1 = new TypeIdItem(dexFile, "Ljava/lang/Exception1;"); - TypeIdItem typeIdItem2 = new TypeIdItem(dexFile, "Ljava/lang/Exception2;"); - - tryListBuilder.addHandler(typeIdItem1, 2, 5, 100); - tryListBuilder.addHandler(typeIdItem2, 2, 5, 101); - - Pair, List> retVal = tryListBuilder.encodeTries(dexFile); - List tries = retVal.first; - - Assert.assertTrue(tries.size() == 1); - checkTry(tries.get(0), 2, 5, -1, new Handler[]{new Handler("Ljava/lang/Exception1;", 100), new Handler("Ljava/lang/Exception2;", 101)}); - } - - @Test - public void twoTriesTest6() { - //|-----| - // |-----| - TryListBuilder tryListBuilder = new TryListBuilder(); - - DexFile dexFile = new DexFile(); - TypeIdItem typeIdItem1 = new TypeIdItem(dexFile, "Ljava/lang/Exception1;"); - TypeIdItem typeIdItem2 = new TypeIdItem(dexFile, "Ljava/lang/Exception2;"); - - tryListBuilder.addHandler(typeIdItem1, 2, 5, 100); - tryListBuilder.addHandler(typeIdItem2, 4, 10, 101); - - - Pair, List> retVal = tryListBuilder.encodeTries(dexFile); - List tries = retVal.first; - - Assert.assertTrue(tries.size() == 3); - checkTry(tries.get(0), 2, 4, -1, new Handler[]{new Handler("Ljava/lang/Exception1;", 100)}); - checkTry(tries.get(1), 4, 5, -1, new Handler[]{new Handler("Ljava/lang/Exception1;", 100), new Handler("Ljava/lang/Exception2;", 101)}); - checkTry(tries.get(2), 5, 10, -1, new Handler[]{new Handler("Ljava/lang/Exception2;", 101)}); - } - - - @Test - public void twoTriesTest7() { - // |-----| - //|-----| - TryListBuilder tryListBuilder = new TryListBuilder(); - - DexFile dexFile = new DexFile(); - TypeIdItem typeIdItem1 = new TypeIdItem(dexFile, "Ljava/lang/Exception1;"); - TypeIdItem typeIdItem2 = new TypeIdItem(dexFile, "Ljava/lang/Exception2;"); - - tryListBuilder.addHandler(typeIdItem2, 4, 10, 101); - tryListBuilder.addHandler(typeIdItem1, 2, 5, 100); - - - Pair, List> retVal = tryListBuilder.encodeTries(dexFile); - List tries = retVal.first; - - Assert.assertTrue(tries.size() == 3); - checkTry(tries.get(0), 2, 4, -1, new Handler[]{new Handler("Ljava/lang/Exception1;", 100)}); - checkTry(tries.get(1), 4, 5, -1, new Handler[]{new Handler("Ljava/lang/Exception2;", 101), new Handler("Ljava/lang/Exception1;", 100)}); - checkTry(tries.get(2), 5, 10, -1, new Handler[]{new Handler("Ljava/lang/Exception2;", 101)}); - } - - @Test - public void twoTriesTest8() { - //|-----| - // |---| - TryListBuilder tryListBuilder = new TryListBuilder(); - - DexFile dexFile = new DexFile(); - TypeIdItem typeIdItem1 = new TypeIdItem(dexFile, "Ljava/lang/Exception1;"); - TypeIdItem typeIdItem2 = new TypeIdItem(dexFile, "Ljava/lang/Exception2;"); - - tryListBuilder.addHandler(typeIdItem1, 2, 10, 100); - tryListBuilder.addHandler(typeIdItem2, 4, 6, 101); - - - Pair, List> retVal = tryListBuilder.encodeTries(dexFile); - List tries = retVal.first; - - Assert.assertTrue(tries.size() == 3); - checkTry(tries.get(0), 2, 4, -1, new Handler[]{new Handler("Ljava/lang/Exception1;", 100)}); - checkTry(tries.get(1), 4, 6, -1, new Handler[]{new Handler("Ljava/lang/Exception1;", 100), new Handler("Ljava/lang/Exception2;", 101)}); - checkTry(tries.get(2), 6, 10, -1, new Handler[]{new Handler("Ljava/lang/Exception1;", 100)}); - } - - @Test - public void twoTriesTest9() { - // |---| - //|-----| - TryListBuilder tryListBuilder = new TryListBuilder(); - - DexFile dexFile = new DexFile(); - TypeIdItem typeIdItem1 = new TypeIdItem(dexFile, "Ljava/lang/Exception1;"); - TypeIdItem typeIdItem2 = new TypeIdItem(dexFile, "Ljava/lang/Exception2;"); - - tryListBuilder.addHandler(typeIdItem2, 4, 6, 101); - tryListBuilder.addHandler(typeIdItem1, 2, 10, 100); - - - Pair, List> retVal = tryListBuilder.encodeTries(dexFile); - List tries = retVal.first; - - Assert.assertTrue(tries.size() == 3); - checkTry(tries.get(0), 2, 4, -1, new Handler[]{new Handler("Ljava/lang/Exception1;", 100)}); - checkTry(tries.get(1), 4, 6, -1, new Handler[]{new Handler("Ljava/lang/Exception2;", 101), new Handler("Ljava/lang/Exception1;", 100)}); - checkTry(tries.get(2), 6, 10, -1, new Handler[]{new Handler("Ljava/lang/Exception1;", 100)}); - } - - @Test - public void twoTriesTest10() { - //|-----| - //|---| - TryListBuilder tryListBuilder = new TryListBuilder(); - - DexFile dexFile = new DexFile(); - TypeIdItem typeIdItem1 = new TypeIdItem(dexFile, "Ljava/lang/Exception1;"); - TypeIdItem typeIdItem2 = new TypeIdItem(dexFile, "Ljava/lang/Exception2;"); - - tryListBuilder.addHandler(typeIdItem1, 2, 10, 100); - tryListBuilder.addHandler(typeIdItem2, 2, 6, 101); - - - Pair, List> retVal = tryListBuilder.encodeTries(dexFile); - List tries = retVal.first; - - Assert.assertTrue(tries.size() == 2); - checkTry(tries.get(0), 2, 6, -1, new Handler[]{new Handler("Ljava/lang/Exception1;", 100), new Handler("Ljava/lang/Exception2;", 101)}); - checkTry(tries.get(1), 6, 10, -1, new Handler[]{new Handler("Ljava/lang/Exception1;", 100)}); - } - - @Test - public void twoTriesTest11() { - //|---| - //|-----| - TryListBuilder tryListBuilder = new TryListBuilder(); - - DexFile dexFile = new DexFile(); - TypeIdItem typeIdItem1 = new TypeIdItem(dexFile, "Ljava/lang/Exception1;"); - TypeIdItem typeIdItem2 = new TypeIdItem(dexFile, "Ljava/lang/Exception2;"); - - tryListBuilder.addHandler(typeIdItem2, 2, 6, 101); - tryListBuilder.addHandler(typeIdItem1, 2, 10, 100); - - - Pair, List> retVal = tryListBuilder.encodeTries(dexFile); - List tries = retVal.first; - - Assert.assertTrue(tries.size() == 2); - checkTry(tries.get(0), 2, 6, -1, new Handler[]{new Handler("Ljava/lang/Exception2;", 101), new Handler("Ljava/lang/Exception1;", 100)}); - checkTry(tries.get(1), 6, 10, -1, new Handler[]{new Handler("Ljava/lang/Exception1;", 100)}); - } - - @Test - public void twoTriesTest12() { - //|-----| - // |---| - TryListBuilder tryListBuilder = new TryListBuilder(); - - DexFile dexFile = new DexFile(); - TypeIdItem typeIdItem1 = new TypeIdItem(dexFile, "Ljava/lang/Exception1;"); - TypeIdItem typeIdItem2 = new TypeIdItem(dexFile, "Ljava/lang/Exception2;"); - - tryListBuilder.addHandler(typeIdItem1, 2, 10, 100); - tryListBuilder.addHandler(typeIdItem2, 6, 10, 101); - - - Pair, List> retVal = tryListBuilder.encodeTries(dexFile); - List tries = retVal.first; - - Assert.assertTrue(tries.size() == 2); - checkTry(tries.get(0), 2, 6, -1, new Handler[]{new Handler("Ljava/lang/Exception1;", 100)}); - checkTry(tries.get(1), 6, 10, -1, new Handler[]{new Handler("Ljava/lang/Exception1;", 100), new Handler("Ljava/lang/Exception2;", 101)}); - } - - @Test - public void twoTriesTest13() { - // |---| - //|-----| - TryListBuilder tryListBuilder = new TryListBuilder(); - - DexFile dexFile = new DexFile(); - TypeIdItem typeIdItem1 = new TypeIdItem(dexFile, "Ljava/lang/Exception1;"); - TypeIdItem typeIdItem2 = new TypeIdItem(dexFile, "Ljava/lang/Exception2;"); - - tryListBuilder.addHandler(typeIdItem2, 6, 10, 101); - tryListBuilder.addHandler(typeIdItem1, 2, 10, 100); - - - Pair, List> retVal = tryListBuilder.encodeTries(dexFile); - List tries = retVal.first; - - Assert.assertTrue(tries.size() == 2); - checkTry(tries.get(0), 2, 6, -1, new Handler[]{new Handler("Ljava/lang/Exception1;", 100)}); - checkTry(tries.get(1), 6, 10, -1, new Handler[]{new Handler("Ljava/lang/Exception2;", 101), new Handler("Ljava/lang/Exception1;", 100)}); - } - - - @Test - public void threeTriesTest1() { - // |-----| - // |-----| - //|--------------------| - TryListBuilder tryListBuilder = new TryListBuilder(); - - DexFile dexFile = new DexFile(); - TypeIdItem typeIdItem1 = new TypeIdItem(dexFile, "Ljava/lang/Exception1;"); - TypeIdItem typeIdItem2 = new TypeIdItem(dexFile, "Ljava/lang/Exception2;"); - TypeIdItem typeIdItem3= new TypeIdItem(dexFile, "Ljava/lang/Exception3;"); - - tryListBuilder.addHandler(typeIdItem1, 2, 4, 100); - tryListBuilder.addHandler(typeIdItem2, 6, 10, 101); - tryListBuilder.addHandler(typeIdItem3, 0, 12, 102); - - Pair, List> retVal = tryListBuilder.encodeTries(dexFile); - List tries = retVal.first; - - Handler handler1 = new Handler("Ljava/lang/Exception1;", 100); - Handler handler2 = new Handler("Ljava/lang/Exception2;", 101); - Handler handler3 = new Handler("Ljava/lang/Exception3;", 102); - - Assert.assertTrue(tries.size() == 5); - checkTry(tries.get(0), 0, 2, -1, new Handler[]{handler3}); - checkTry(tries.get(1), 2, 4, -1, new Handler[]{handler1, handler3}); - checkTry(tries.get(2), 4, 6, -1, new Handler[]{handler3}); - checkTry(tries.get(3), 6, 10, -1, new Handler[]{handler2, handler3}); - checkTry(tries.get(4), 10, 12, -1, new Handler[]{handler3}); - } - - @Test - public void catchAllTest1() { - //|-----| - // |---| - TryListBuilder tryListBuilder = new TryListBuilder(); - - DexFile dexFile = new DexFile(); - - tryListBuilder.addCatchAllHandler(2, 8, 100); - tryListBuilder.addCatchAllHandler(4, 6, 101); - - Pair, List> retVal = tryListBuilder.encodeTries(dexFile); - List tries = retVal.first; - - Assert.assertTrue(tries.size() == 3); - checkTry(tries.get(0), 2, 4, 100, new Handler[]{}); - checkTry(tries.get(1), 4, 6, 100, new Handler[]{}); - checkTry(tries.get(2), 6, 8, 100, new Handler[]{}); - } - - @Test - public void catchAllTest2() { - // |---| - //|-----| - TryListBuilder tryListBuilder = new TryListBuilder(); - - DexFile dexFile = new DexFile(); - - tryListBuilder.addCatchAllHandler(4, 6, 100); - tryListBuilder.addCatchAllHandler(2, 8, 101); - - Pair, List> retVal = tryListBuilder.encodeTries(dexFile); - List tries = retVal.first; - - Assert.assertTrue(tries.size() == 3); - checkTry(tries.get(0), 2, 4, 101, new Handler[]{}); - checkTry(tries.get(1), 4, 6, 100, new Handler[]{}); - checkTry(tries.get(2), 6, 8, 101, new Handler[]{}); - } - - - -}