Skip to content

Commit

Permalink
[DEX] Marker and section sort
Browse files Browse the repository at this point in the history
  • Loading branch information
REAndroid committed Sep 19, 2023
1 parent b7803b8 commit 0ba7e96
Show file tree
Hide file tree
Showing 25 changed files with 674 additions and 168 deletions.
20 changes: 20 additions & 0 deletions src/main/java/com/reandroid/dex/base/PositionAlignedItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright (C) 2022 github.com/REAndroid
*
* 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 com.reandroid.dex.base;

public interface PositionAlignedItem {
DexPositionAlign getPositionAlign();
}
5 changes: 5 additions & 0 deletions src/main/java/com/reandroid/dex/debug/Base1Ule128Item.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import com.reandroid.arsc.io.BlockReader;
import com.reandroid.dex.base.Ule128Item;
import com.reandroid.dex.index.IndexItemEntry;
import com.reandroid.dex.index.StringId;
import com.reandroid.dex.item.StringData;
import com.reandroid.dex.sections.SectionType;

import java.io.IOException;
Expand Down Expand Up @@ -48,6 +50,9 @@ public void setItem(T item) {

private void updateItem(){
item = get(sectionType, get() - 1);
if(item instanceof StringId){
((StringId) item).addStringUsage(StringData.USAGE_DEBUG);
}
}
@Override
public void onReadBytes(BlockReader reader) throws IOException {
Expand Down
5 changes: 1 addition & 4 deletions src/main/java/com/reandroid/dex/header/DexHeader.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,18 @@
package com.reandroid.dex.header;

import com.reandroid.arsc.base.Block;
import com.reandroid.arsc.base.OffsetSupplier;
import com.reandroid.arsc.io.BlockLoad;
import com.reandroid.arsc.io.BlockReader;
import com.reandroid.arsc.item.ByteArray;
import com.reandroid.arsc.item.IntegerItem;
import com.reandroid.arsc.item.IntegerReference;
import com.reandroid.dex.base.FixedDexContainer;
import com.reandroid.dex.base.NumberIntegerReference;
import com.reandroid.arsc.base.OffsetSupplier;
import com.reandroid.dex.sections.SectionList;

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

public class DexHeader extends FixedDexContainer implements OffsetSupplier, BlockLoad {

Expand Down
9 changes: 1 addition & 8 deletions src/main/java/com/reandroid/dex/index/ClassId.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,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 StringReference(this, offset += 4);
this.sourceFile = new StringReference(this, offset += 4, StringData.USAGE_SOURCE);
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 @@ -147,13 +147,6 @@ 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
2 changes: 1 addition & 1 deletion src/main/java/com/reandroid/dex/index/FieldId.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public FieldId() {
super(8);
this.classType = new ItemShortReference<>(SectionType.TYPE_ID, this, 0);
this.fieldType = new ItemShortReference<>(SectionType.TYPE_ID, this, 2);
this.nameReference = new StringReference( this, 4);
this.nameReference = new StringReference( this, 4, StringData.USAGE_FIELD);
}

public StringReference getNameReference() {
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/reandroid/dex/index/IndexItemEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.reandroid.arsc.base.BlockRefresh;
import com.reandroid.arsc.io.BlockReader;
import com.reandroid.dex.base.DexBlockItem;
import com.reandroid.dex.base.DexItemArray;
import com.reandroid.dex.base.FixedSizeBlock;
import com.reandroid.dex.base.StringKeyItem;
import com.reandroid.dex.pool.DexIdPool;
Expand All @@ -35,6 +36,14 @@ public abstract class IndexItemEntry extends DexBlockItem
super(bytesLength);
}


@SuppressWarnings("unchecked")
public void removeSelf(){
DexItemArray<IndexItemEntry> itemArray = getParentInstance(DexItemArray.class);
if(itemArray != null){
itemArray.remove(this);
}
}
@Override
public String getKey(){
return null;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/reandroid/dex/index/MethodId.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public MethodId() {
super(SIZE);
this.classType = new ItemShortReference<>(SectionType.TYPE_ID, this, 0);
this.proto = new ItemShortReference<>(SectionType.PROTO_ID, this, 2);
this.nameReference = new StringReference(this, 4);
this.nameReference = new StringReference(this, 4, StringData.USAGE_METHOD);
}

public String getName(){
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/reandroid/dex/index/ProtoId.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class ProtoId extends IndexItemEntry implements Comparable<ProtoId> {
public ProtoId() {
super(SIZE);

this.shorty = new StringReference(this, 0);
this.shorty = new StringReference(this, 0, StringData.USAGE_SHORTY);
this.returnType = new ItemIndexReference<>(SectionType.TYPE_ID, this, 4);
this.parameters = new ItemOffsetReference<>(SectionType.TYPE_LIST, this, 8);
}
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/com/reandroid/dex/index/StringId.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,43 @@ public StringId(){
super(4);
}

@Override
public void removeSelf() {
StringData stringData = this.stringData;
this.stringData = null;
if(stringData != null){
stringData.removeSelf();
}else {
super.removeSelf();
}
}

public StringData getStringData() {
return stringData;
}
public void setStringData(StringData stringData) {
this.stringData = stringData;
}
public boolean containsUsage(int usage){
StringData stringData = getStringData();
if(stringData != null){
return stringData.containsUsage(usage);
}
return false;
}
public int getStringUsage() {
StringData stringData = getStringData();
if(stringData != null){
return stringData.getStringUsage();
}
return StringData.USAGE_NONE;
}
public void addStringUsage(int usage){
StringData stringData = getStringData();
if(stringData != null){
stringData.addStringUsage(usage);
}
}
@Override
public void set(int value) {
putInteger(getBytesInternal(), 0, value);
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/com/reandroid/dex/index/StringReference.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,21 @@ public class StringReference extends IndirectItem<DexBlockItem> implements
IntegerReference, BlockRefresh, Comparable<StringReference> {

private StringId stringId;
private final int stringUsage;

public StringReference(DexBlockItem blockItem, int offset) {
public StringReference(DexBlockItem blockItem, int offset, int usage) {
super(blockItem, offset);
this.stringUsage = usage;
}

public StringId getStringId() {
StringId stringId = this.stringId;
if(stringId == null){
stringId = getBlockItem().get(SectionType.STRING_ID, get());
this.stringId = stringId;
if(stringId != null){
stringId.addStringUsage(stringUsage);
}
}
return stringId;
}
Expand All @@ -50,6 +55,7 @@ public void setStringId(StringId stringId) {
int value = 0;
if(stringId != null){
value = stringId.getIndex();
stringId.addStringUsage(stringUsage);
}
set(value);
}
Expand Down
14 changes: 2 additions & 12 deletions src/main/java/com/reandroid/dex/index/TypeId.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class TypeId extends IndexItemEntry implements Comparable<TypeId>{
private TypeName typeName;
public TypeId() {
super(4);
this.nameReference = new StringReference(this, 0);
this.nameReference = new StringReference(this, 0, StringData.USAGE_TYPE);
}

@Override
Expand Down Expand Up @@ -101,25 +101,15 @@ public StringReference getNameReference() {

public void setName(StringData name){
nameReference.setItem(name);
if(name != null){
name.addStringUsage(StringData.USAGE_TYPE);
}
}

@Override
public void refresh() {
nameReference.refresh();
StringData stringData = nameReference.getItem();
if(stringData != null){
stringData.addStringUsage(StringData.USAGE_TYPE);
}
}
@Override
void cacheItems(){
StringData stringData = nameReference.getItem();
if(stringData != null){
stringData.addStringUsage(StringData.USAGE_TYPE);
}
nameReference.getItem();
}

@Override
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/com/reandroid/dex/ins/Ins31c.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,11 @@ public class Ins31c extends Size6Ins {
public Ins31c(Opcode<?> opcode) {
super(opcode);
}
@Override
public int getData(){
return getValueBytes().getInteger(2);
}
public void setData(int data){
getValueBytes().putInteger(2, data);
}
}
8 changes: 8 additions & 0 deletions src/main/java/com/reandroid/dex/ins/Ins3rc.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,12 @@ public class Ins3rc extends Size6Ins {
public Ins3rc(Opcode<?> opcode) {
super(opcode);
}

@Override
public int getData(){
return getValueBytes().getShortUnsigned(2);
}
public void setData(int data){
getValueBytes().putShort(2, data);
}
}
41 changes: 25 additions & 16 deletions src/main/java/com/reandroid/dex/ins/SizeXIns.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,30 @@
*/
package com.reandroid.dex.ins;

import com.reandroid.arsc.base.Block;
import com.reandroid.arsc.io.BlockReader;
import com.reandroid.arsc.item.ByteArray;
import com.reandroid.dex.index.IndexItemEntry;
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 com.reandroid.utils.HexUtil;

import java.io.IOException;

public class SizeXIns extends Ins {

private final ByteArray valueBytes;
private IndexItemEntry mItemId;
private IndexItemEntry mSectionItem;

public SizeXIns(Opcode<?> opcode) {
super(opcode);
this.valueBytes = new ByteArray();
addChild(0, valueBytes);
valueBytes.setSize(opcode.size());
valueBytes.putShort(0, opcode.getValue());
}

public int getInteger(int offset){
return valueBytes.getInteger(2 + offset);
}
Expand Down Expand Up @@ -71,13 +75,23 @@ public int countBytes(){
@Override
public void onReadBytes(BlockReader reader) throws IOException {
valueBytes.onReadBytes(reader);
cacheSectionItem();
}
private void cacheSectionItem(){
SectionType<? extends IndexItemEntry> sectionType = getOpcode().getSectionType();
if(sectionType == null){
return;
}
int data = getData();
this.mItemId = get(sectionType, data);
this.mSectionItem = get(sectionType, data);
if(mSectionItem instanceof StringId){
((StringId) mSectionItem).addStringUsage(StringData.USAGE_INSTRUCTION);
}
}
public IndexItemEntry getSectionItem() {
return mSectionItem;
}

public int getData(){
return getValueBytes().getShortUnsigned(2);
}
Expand All @@ -87,7 +101,7 @@ public void setData(int data){

@Override
protected void onRefreshed() {
IndexItemEntry itemId = this.mItemId;
IndexItemEntry itemId = this.mSectionItem;
if(itemId != null){
setData(itemId.getIndex());
}
Expand All @@ -110,12 +124,9 @@ void appendCode(SmaliWriter writer) throws IOException {
}
writer.append(", ");
int data = getData();
SectionType<? extends IndexItemEntry> sectionType = opcode.getSectionType();
if(sectionType != null){
IndexItemEntry sectionData = get(sectionType, data);
if(sectionData != null){
sectionData.append(writer);
}
IndexItemEntry sectionItem = getSectionItem();
if(sectionItem != null){
sectionItem.append(writer);
}else {
writer.append(HexUtil.toHex(data, 1));
}
Expand All @@ -131,13 +142,11 @@ public String toString() {
builder.append(" v");
builder.append(getRegisterA());
builder.append(", ");
int data = getValueBytes().getShortUnsigned(2);
SectionType<?> sectionType = opcode.getSectionType();
if(sectionType != null){
Block sectionData = get(sectionType, data);
builder.append(sectionData);
IndexItemEntry sectionItem = getSectionItem();
if(sectionItem != null){
builder.append(sectionItem);
}else {
builder.append(HexUtil.toHex(data, 2));
builder.append(HexUtil.toHex(getData(), 2));
}
return builder.toString();
}
Expand Down
Loading

0 comments on commit 0ba7e96

Please sign in to comment.