Skip to content

Commit

Permalink
[DEX] sort strings
Browse files Browse the repository at this point in the history
  • Loading branch information
REAndroid committed Sep 18, 2023
1 parent 5c8e966 commit ec5625d
Show file tree
Hide file tree
Showing 52 changed files with 879 additions and 182 deletions.
5 changes: 5 additions & 0 deletions src/main/java/com/reandroid/dex/base/DexItemArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ public DexItemArray(IntegerPair countAndOffset,
super(creator);
this.countAndOffset = countAndOffset;
}
@Override
public int countBytes() {
return super.countBytes();
}

public void setPreloadArray(PreloadArray<T> preloadArray) {
this.mPreloadArray = preloadArray;
}
Expand Down
54 changes: 53 additions & 1 deletion src/main/java/com/reandroid/dex/common/DexUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,59 @@
package com.reandroid.dex.common;

public class DexUtils {
public static boolean isNative(String type){
if(type == null){
return false;
}
int length = type.length();
if(length == 0){
return false;
}
int i = 0;
while (i < length && type.charAt(i) == '['){
i++;
}
if(i >= length){
return false;
}
return isNative(type.charAt(i));
}
public static boolean isNative(char ch){
switch (ch){
case 'B':
case 'C':
case 'D':
case 'F':
case 'I':
case 'J':
case 'S':
case 'Z':
return true;
default:
return false;
}
}
public static boolean looksSignatureType(String name){
int length = name.length();
if(length < 3){
return false;
}
return name.charAt(0) == 'L' && name.charAt(length - 1) == '<';
}

public static String toJavaName(String dalvikName){
int i = dalvikName.indexOf('L');
dalvikName = dalvikName.substring(i + 1);
i = dalvikName.indexOf(';');
if(i < 0){
i = dalvikName.indexOf('<');
}
if(i > 0){
dalvikName = dalvikName.substring(0, i);
}
return dalvikName.replace('/', '.');
}
public static String toDalvikName(String javaName){
return "L" + javaName.replace('.', '/') + ";";
return 'L' + javaName.replace('.', '/') + ';';
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/reandroid/dex/debug/Base1Ule128Item.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
import com.reandroid.arsc.base.BlockRefresh;
import com.reandroid.arsc.io.BlockReader;
import com.reandroid.dex.base.Ule128Item;
import com.reandroid.dex.index.ItemId;
import com.reandroid.dex.index.IndexItemEntry;
import com.reandroid.dex.sections.SectionType;

import java.io.IOException;

public class Base1Ule128Item<T extends ItemId> extends Ule128Item implements BlockRefresh {
public class Base1Ule128Item<T extends IndexItemEntry> extends Ule128Item implements BlockRefresh {
private final SectionType<T> sectionType;
private T item;

Expand Down
14 changes: 9 additions & 5 deletions src/main/java/com/reandroid/dex/debug/DebugParameter.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,24 @@
*/
package com.reandroid.dex.debug;

import com.reandroid.dex.index.StringData;
import com.reandroid.dex.sections.SectionList;
import com.reandroid.dex.index.StringId;
import com.reandroid.dex.item.StringData;
import com.reandroid.dex.sections.SectionType;
import com.reandroid.dex.writer.SmaliFormat;
import com.reandroid.dex.writer.SmaliWriter;

import java.io.IOException;

public class DebugParameter extends Base1Ule128Item<StringData> implements SmaliFormat {
public class DebugParameter extends Base1Ule128Item<StringId> implements SmaliFormat {
public DebugParameter(){
super(SectionType.STRING_DATA);
super(SectionType.STRING_ID);
}
public StringData getParameterName(){
return getItem();
StringId stringId = getItem();
if(stringId != null){
return stringId.getStringData();
}
return null;
}
@Override
public void append(SmaliWriter writer) throws IOException {
Expand Down
13 changes: 9 additions & 4 deletions src/main/java/com/reandroid/dex/debug/DebugSetSourceFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,25 @@
*/
package com.reandroid.dex.debug;

import com.reandroid.dex.index.StringData;
import com.reandroid.dex.index.StringId;
import com.reandroid.dex.item.StringData;
import com.reandroid.dex.sections.SectionType;

public class DebugSetSourceFile extends DebugElement {
private final Base1Ule128Item<StringData> nameData;
private final Base1Ule128Item<StringId> nameData;

public DebugSetSourceFile() {
super(1, DebugElementType.SET_SOURCE_FILE);
this.nameData = new Base1Ule128Item<>(SectionType.STRING_DATA);
this.nameData = new Base1Ule128Item<>(SectionType.STRING_ID);
addChild(1, nameData);
}

public StringData getName(){
return nameData.getItem();
StringId stringId = nameData.getItem();
if(stringId != null){
return stringId.getStringData();
}
return null;
}

@Override
Expand Down
13 changes: 9 additions & 4 deletions src/main/java/com/reandroid/dex/debug/DebugStartLocal.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
*/
package com.reandroid.dex.debug;

import com.reandroid.dex.index.StringData;
import com.reandroid.dex.index.StringId;
import com.reandroid.dex.item.StringData;
import com.reandroid.dex.index.TypeId;
import com.reandroid.dex.sections.SectionType;
import com.reandroid.dex.writer.SmaliWriter;
Expand All @@ -24,13 +25,13 @@

public class DebugStartLocal extends DebugRegisterNumber {

private final Base1Ule128Item<StringData> nameIndex;
private final Base1Ule128Item<StringId> nameIndex;
private final Base1Ule128Item<TypeId> typeIndex;

DebugStartLocal(int childesCount, int flag) {
super(childesCount + 2, flag);

this.nameIndex = new Base1Ule128Item<>(SectionType.STRING_DATA);
this.nameIndex = new Base1Ule128Item<>(SectionType.STRING_ID);
this.typeIndex = new Base1Ule128Item<>(SectionType.TYPE_ID);

addChild(2, nameIndex);
Expand All @@ -44,7 +45,11 @@ public DebugStartLocal() {
}

public StringData getName(){
return nameIndex.getItem();
StringId stringId = nameIndex.getItem();
if(stringId != null){
return stringId.getStringData();
}
return null;
}
public TypeId getTypeId(){
return typeIndex.getItem();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
package com.reandroid.dex.debug;

import com.reandroid.dex.index.StringData;
import com.reandroid.dex.index.StringId;
import com.reandroid.dex.item.StringData;
import com.reandroid.dex.sections.SectionType;
import com.reandroid.dex.writer.SmaliWriter;

import java.io.IOException;

public class DebugStartLocalExtended extends DebugStartLocal {

private final Base1Ule128Item<StringData> signatureData;
private final Base1Ule128Item<StringId> signatureData;

public DebugStartLocalExtended(){
super(1, DebugElementType.START_LOCAL_EXTENDED);
this.signatureData = new Base1Ule128Item<>(SectionType.STRING_DATA);
this.signatureData = new Base1Ule128Item<>(SectionType.STRING_ID);

addChild(4, signatureData);
}
public StringData getSignature(){
return signatureData.getItem();
StringId stringId = signatureData.getItem();
if(stringId != null){
return stringId.getStringData();
}
return null;
}

public void appendExtra(SmaliWriter writer) throws IOException {
Expand Down
23 changes: 20 additions & 3 deletions src/main/java/com/reandroid/dex/index/ClassId.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,17 @@
import com.reandroid.dex.sections.SectionType;
import com.reandroid.dex.value.DexValue;
import com.reandroid.dex.writer.SmaliWriter;
import com.reandroid.utils.CompareUtil;

import java.io.IOException;

public class ClassId extends ItemId {
public class ClassId extends IndexItemEntry implements Comparable<ClassId>{

private final ItemIndexReference<TypeId> classType;
private final IndirectInteger accessFlagValue;
private final ItemIndexReference<TypeId> superClass;
private final ItemOffsetReference<TypeList> interfaces;
private final ItemIndexReference<StringData> sourceFile;
private final StringReference sourceFile;
private final ItemOffsetReference<AnnotationsDirectory> annotationsDirectory;
private final ItemOffsetReference<ClassData> classData;
private final ItemOffsetReference<EncodedArray> staticValues;
Expand All @@ -43,7 +44,7 @@ public ClassId() {
this.accessFlagValue = new IndirectInteger(this, offset += 4);
this.superClass = new ItemIndexReference<>(SectionType.TYPE_ID, this, offset += 4);
this.interfaces = new ItemOffsetReference<>(SectionType.TYPE_LIST, this, offset += 4);
this.sourceFile = new ItemIndexReference<>(SectionType.STRING_DATA,this, offset += 4);
this.sourceFile = new StringReference(this, offset += 4);
this.annotationsDirectory = new ItemOffsetReference<>(SectionType.ANNOTATIONS_DIRECTORY, this, offset += 4);
this.classData = new ItemOffsetReference<>(SectionType.CLASS_DATA, this, offset += 4);
this.staticValues = new ItemOffsetReference<>(SectionType.ENCODED_ARRAY, this, offset += 4);
Expand Down Expand Up @@ -146,6 +147,13 @@ void cacheItems(){
this.annotationsDirectory.getItem();
this.classData.getItem();
this.staticValues.getItem();
linkStringData();
}
private void linkStringData(){
StringData stringData = this.sourceFile.getItem();
if(stringData != null){
stringData.addStringUsage(StringData.USAGE_SOURCE);
}
}

@Override
Expand Down Expand Up @@ -193,6 +201,15 @@ public void append(SmaliWriter writer) throws IOException {
writer.appendComment("Null class data: " + this.classData.get());
}
}

@Override
public int compareTo(ClassId classId) {
if(classId == null){
return -1;
}
return CompareUtil.compare(getClassType(), classId.getClassType());
}

@Override
public String toString(){
StringBuilder builder = new StringBuilder();
Expand Down
40 changes: 32 additions & 8 deletions src/main/java/com/reandroid/dex/index/FieldId.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,28 @@
*/
package com.reandroid.dex.index;

import com.reandroid.dex.item.StringData;
import com.reandroid.dex.pool.DexIdPool;
import com.reandroid.dex.sections.SectionType;
import com.reandroid.dex.writer.SmaliWriter;
import com.reandroid.utils.CompareUtil;

import java.io.IOException;

public class FieldId extends ItemId {
public class FieldId extends IndexItemEntry implements Comparable<FieldId>{
private final ItemIndexReference<TypeId> classType;
private final ItemIndexReference<TypeId> fieldType;
private final ItemIndexReference<StringData> name;
private final StringReference nameReference;

public FieldId() {
super(8);
this.classType = new ItemShortReference<>(SectionType.TYPE_ID, this, 0);
this.fieldType = new ItemShortReference<>(SectionType.TYPE_ID, this, 2);
this.name = new ItemIndexReference<>(SectionType.STRING_DATA, this, 4);
this.nameReference = new StringReference( this, 4);
}

public StringReference getNameReference() {
return nameReference;
}
public String getName(){
StringData stringData = getNameString();
Expand All @@ -44,9 +50,10 @@ public void setName(String name){
StringData stringData = stringPool.getOrCreate(name);
setName(stringData);
}
public void setName(StringData name){
this.name.setItem(name);
public void setName(StringData stringData){
this.nameReference.setItem(stringData);
}
@Override
public String getKey(){
StringBuilder builder = new StringBuilder();
TypeId type = getClassType();
Expand Down Expand Up @@ -89,7 +96,7 @@ public TypeId getClassType(){
return classType.getItem();
}
public StringData getNameString(){
return name.getItem();
return nameReference.getItem();
}
public TypeId getFieldType(){
return fieldType.getItem();
Expand All @@ -99,13 +106,13 @@ public TypeId getFieldType(){
public void refresh() {
classType.refresh();
fieldType.refresh();
name.refresh();
nameReference.refresh();
}
@Override
void cacheItems(){
classType.getItem();
fieldType.getItem();
name.getItem();
nameReference.getStringId();
}

@Override
Expand All @@ -116,6 +123,23 @@ public void append(SmaliWriter writer) throws IOException {
writer.append(':');
getFieldType().append(writer);
}

@Override
public int compareTo(FieldId fieldId) {
if(fieldId == null){
return -1;
}
int i = CompareUtil.compare(getClassType(), fieldId.getClassType());
if(i != 0){
return i;
}
i = CompareUtil.compare(getNameReference(), fieldId.getNameReference());
if(i != 0){
return i;
}
return CompareUtil.compare(getFieldType(), fieldId.getFieldType());
}

@Override
public String toString(){
String key = getKey();
Expand Down
Loading

0 comments on commit ec5625d

Please sign in to comment.