Skip to content

Commit

Permalink
[DEX] Optimize performance
Browse files Browse the repository at this point in the history
  • Loading branch information
REAndroid committed Sep 21, 2023
1 parent 0ba7e96 commit 170c121
Show file tree
Hide file tree
Showing 47 changed files with 1,012 additions and 628 deletions.
17 changes: 17 additions & 0 deletions src/main/java/com/reandroid/dex/base/DexBlockItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.reandroid.arsc.item.IntegerReference;
import com.reandroid.dex.io.ByteReader;
import com.reandroid.dex.io.StreamUtil;
import com.reandroid.dex.pool.DexIdPool;
import com.reandroid.dex.sections.Section;
import com.reandroid.dex.sections.SectionList;
import com.reandroid.dex.sections.SectionType;
Expand All @@ -33,6 +34,14 @@ public DexBlockItem(int bytesLength) {
super(bytesLength);
}

public<T1 extends Block> T1 createOffsetItem(SectionType<T1> sectionType) {
Section<T1> section = getSection(sectionType);
if(section != null){
return section.createOffsetItem();
}
return null;
}

public<T1 extends Block> T1 getAt(SectionType<T1> sectionType, IntegerReference offset){
if(offset != null){
return getAt(sectionType, offset.get());
Expand Down Expand Up @@ -84,6 +93,14 @@ public<T1 extends Block> Section<T1> getSection(SectionType<T1> sectionType){
return null;
}

public<T1 extends Block> DexIdPool<T1> getPool(SectionType<T1> sectionType){
Section<T1> section = getSection(sectionType);
if(section != null){
return section.getPool();
}
return null;
}

public static int writeUleb128(byte[] bytes, int offset, int value) {
int index = 0;
while ((value & 0xffffffffL) > 0x7f) {
Expand Down
43 changes: 43 additions & 0 deletions src/main/java/com/reandroid/dex/common/DexUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,50 @@
// originally copied from JesusFreke/smali
package com.reandroid.dex.common;

import java.io.IOException;

public class DexUtils {

public static String quoteString(String text){
StringBuilder builder = new StringBuilder(text.length() + 2);
try {
appendQuotedString(builder, text);
} catch (IOException ignored) {
}
return builder.toString();
}
public static void appendQuotedString(Appendable appendable, String text) throws IOException {
appendable.append('"');
for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);

if ((c >= ' ') && (c < 0x7f)) {
if ((c == '\'') || (c == '\"') || (c == '\\')) {
appendable.append('\\');
}
appendable.append(c);
continue;
} else if (c <= 0x7f) {
switch (c) {
case '\n':
appendable.append("\\n");
continue;
case '\r':
appendable.append("\\r");
continue;
case '\t':
appendable.append("\\t");
continue;
}
}
appendable.append("\\u");
appendable.append(Character.forDigit(c >> 12, 16));
appendable.append(Character.forDigit((c >> 8) & 0x0f, 16));
appendable.append(Character.forDigit((c >> 4) & 0x0f, 16));
appendable.append(Character.forDigit(c & 0x0f, 16));
}
appendable.append('"');
}
public static boolean isNative(String type){
if(type == null){
return false;
Expand Down
47 changes: 0 additions & 47 deletions src/main/java/com/reandroid/dex/header/Alder32OutputStream.java

This file was deleted.

16 changes: 8 additions & 8 deletions src/main/java/com/reandroid/dex/header/Checksum.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
import com.reandroid.utils.HexUtil;

import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.zip.Adler32;

public class Checksum extends HeaderPiece {

Expand All @@ -33,14 +36,11 @@ public void setChecksum(int checksum){
setSize(4);
putInteger(0, checksum);
}
public void update(Block parent) {
Alder32OutputStream outputStream = new Alder32OutputStream();
try {
parent.writeBytes(outputStream);
} catch (IOException ex) {
throw new IllegalArgumentException(ex);
}
setChecksum(outputStream.getValue());
public void update(Block parent, byte[] bytes) {
int start = parent.countUpTo(this) + countBytes();
Adler32 adler32 = new Adler32();
adler32.update(bytes, start, bytes.length - start);
setChecksum((int) adler32.getValue());
}

@Override
Expand Down
28 changes: 4 additions & 24 deletions src/main/java/com/reandroid/dex/header/DexHeader.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class DexHeader extends FixedDexContainer implements OffsetSupplier, BlockLoad {

Expand Down Expand Up @@ -106,10 +105,11 @@ public DexHeader(){
}

public void updateHeaderInternal(Block parent){
byte[] bytes = parent.getBytes();
headerSize.set(countBytes());
fileSize.set(parent.countBytes());
signature.update(parent);
checksum.update(parent);
fileSize.set(bytes.length);
signature.update(parent, bytes);
checksum.update(parent, bytes);
}

@Override
Expand All @@ -128,26 +128,6 @@ public void onBlockLoaded(BlockReader reader, Block sender) throws IOException {
}
}

@Override
public int onWriteBytes(OutputStream stream) throws IOException {
int start = 0;
Class<?> streamClass = stream.getClass();
if(streamClass == Alder32OutputStream.class){
start = 3;
}else if(streamClass == Sha1OutputStream.class){
start = 4;
}
int result = 0;
Block[] childes = getChildes();
for(int i = start; i < childes.length; i++){
Block block = childes[i];
if(block != null){
result += block.writeBytes(stream);
}
}
return result;
}

@Override
public String toString() {
return "Header {" +
Expand Down
52 changes: 0 additions & 52 deletions src/main/java/com/reandroid/dex/header/Sha1OutputStream.java

This file was deleted.

17 changes: 11 additions & 6 deletions src/main/java/com/reandroid/dex/header/Signature.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,27 @@
package com.reandroid.dex.header;

import com.reandroid.arsc.base.Block;
import com.reandroid.dex.model.DexFile;

import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Signature extends HeaderPiece{
public Signature(){
super(20);
}
public void update(Block parent) {
Sha1OutputStream outputStream = new Sha1OutputStream();
public void update(Block parent, byte[] bytes) {
int start = parent.countUpTo(this) + countBytes();
MessageDigest messageDigest;
try {
parent.writeBytes(outputStream);
} catch (IOException ex) {
messageDigest = MessageDigest.getInstance("SHA-1");
} catch (NoSuchAlgorithmException ex) {
throw new IllegalArgumentException(ex);
}
byte[] bytes = outputStream.digest();
putByteArray(0, bytes);
messageDigest.update(bytes, start, bytes.length - start);
byte[] digest = messageDigest.digest();
putByteArray(0, digest);
}
@Override
public String toString() {
Expand Down
21 changes: 18 additions & 3 deletions src/main/java/com/reandroid/dex/index/ClassId.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import com.reandroid.dex.common.AccessFlag;
import com.reandroid.dex.item.*;
import com.reandroid.dex.sections.SectionType;
import com.reandroid.dex.value.DexValue;
import com.reandroid.dex.value.DexValueBlock;
import com.reandroid.dex.writer.SmaliWriter;
import com.reandroid.utils.CompareUtil;

Expand Down Expand Up @@ -74,14 +74,20 @@ public TypeId getSuperClass(){
return superClass.getItem();
}
public void setSuperClass(TypeId typeId){
superClass.setItem(typeId);
this.superClass.setItem(typeId);
}
public void setSuperClass(String superClass){
this.superClass.setItem(superClass);
}
public StringData getSourceFile(){
return sourceFile.getItem();
}
public void setSourceFile(StringData stringData){
this.sourceFile.setItem(stringData);
}
public void setSourceFile(String sourceFile){
this.sourceFile.setString(sourceFile);
}
public TypeId[] getInterfaceTypeIds(){
TypeList interfaceList = getInterfaces();
if(interfaceList != null){
Expand All @@ -92,6 +98,9 @@ public TypeId[] getInterfaceTypeIds(){
public TypeList getInterfaces(){
return interfaces.getItem();
}
public ItemOffsetReference<TypeList> getInterfacesReference() {
return this.interfaces;
}
public void setInterfaces(TypeList interfaces){
this.interfaces.setItem(interfaces);
}
Expand All @@ -102,6 +111,12 @@ public AnnotationSet getClassAnnotations(){
}
return null;
}
public void setClassAnnotations(AnnotationSet annotationSet){
AnnotationsDirectory annotationsDirectory = getAnnotationsDirectory();
if(annotationsDirectory != null){
annotationsDirectory.setClassAnnotations(annotationSet);
}
}
public AnnotationsDirectory getAnnotationsDirectory(){
return annotationsDirectory.getItem();
}
Expand All @@ -117,7 +132,7 @@ public void setClassData(ClassData classData){
public EncodedArray getStaticValues(){
return staticValues.getItem();
}
public DexValue<?> getStaticValue(int i){
public DexValueBlock<?> getStaticValue(int i){
EncodedArray encodedArray = getStaticValues();
if(encodedArray != null){
return encodedArray.get(i);
Expand Down
Loading

0 comments on commit 170c121

Please sign in to comment.