Skip to content

Commit

Permalink
[DEX] Visit integers
Browse files Browse the repository at this point in the history
  • Loading branch information
REAndroid committed Apr 23, 2024
1 parent b0005b3 commit 0b559d8
Show file tree
Hide file tree
Showing 9 changed files with 153 additions and 33 deletions.
20 changes: 20 additions & 0 deletions src/main/java/com/reandroid/dex/base/NumberArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import java.io.IOException;
import java.util.AbstractList;
import java.util.Iterator;
import java.util.List;

public class NumberArray extends DexBlockItem {
Expand All @@ -36,7 +37,26 @@ public NumberArray(IntegerReference widthReference, IntegerReference itemCount)
this.widthReference = widthReference;
this.itemCount = itemCount;
}

public Iterator<IntegerReference> getReferences(){
return new Iterator<IntegerReference>() {
private int mIndex;
@Override
public boolean hasNext() {
return mIndex < NumberArray.this.size();
}
@Override
public IntegerReference next() {
IntegerReference reference = NumberArray.this.getReference(mIndex);
mIndex ++;
return reference;
}
};
}
public IntegerReference getReference(int index) {
if(index >= size()){
return null;
}
return new Data(this, index);
}
public short[] getShortArray(){
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/reandroid/dex/ins/ConstNumberLong.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,17 @@
public interface ConstNumberLong extends ConstNumber{
long getLong();
void set(long value);
@Override
default int get() {
long l = getLong();
int i = (int) l;
if((i & 0xffffffffL) != l){
return 0;
}
return i;
}
@Override
default void set(int value) {
set((long) value);
}
}
3 changes: 3 additions & 0 deletions src/main/java/com/reandroid/dex/ins/InsArrayData.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ public void set(long[] values){
numberArray.putLong(values);
refreshAlignment();
}
public Iterator<IntegerReference> getReferences(){
return getNumberArray().getReferences();
}
public IntegerReference getReference(int i){
return getNumberArray().getReference(i);
}
Expand Down
9 changes: 0 additions & 9 deletions src/main/java/com/reandroid/dex/ins/InsConstWide.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package com.reandroid.dex.ins;

import com.reandroid.dex.smali.SmaliWriter;
import com.reandroid.utils.HexUtil;

import java.io.IOException;

Expand All @@ -26,14 +25,6 @@ public InsConstWide(){
super(Opcode.CONST_WIDE);
}

@Override
public int get() {
return (int) getLong();
}
@Override
public void set(int value) {
set((long) value);
}
@Override
public void set(long value) {
setLong(value);
Expand Down
8 changes: 0 additions & 8 deletions src/main/java/com/reandroid/dex/ins/InsConstWide16.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,6 @@ public InsConstWide16(){
super(Opcode.CONST_WIDE_16);
}

@Override
public int get() {
return getData();
}
@Override
public void set(int value) {
setData(value);
}
@Override
public void set(long value) {
InsConstWide insConstWide = mReplaced;
Expand Down
8 changes: 0 additions & 8 deletions src/main/java/com/reandroid/dex/ins/InsConstWide32.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,6 @@ public InsConstWide32(){
super(Opcode.CONST_WIDE_32);
}

@Override
public int get() {
return getData();
}
@Override
public void set(int value) {
setData(value);
}
@Override
public void set(long value) {
setLong(value);
Expand Down
8 changes: 0 additions & 8 deletions src/main/java/com/reandroid/dex/ins/InsConstWideHigh16.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,6 @@ public InsConstWideHigh16(){
super(Opcode.CONST_WIDE_HIGH16);
}

@Override
public int get() {
return getData();
}
@Override
public void set(int value) {
setData(value);
}
@Override
public void set(long value) {
setLong(value);
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/reandroid/dex/model/DexClassRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.reandroid.dex.model;

import com.reandroid.arsc.item.IntegerReference;
import com.reandroid.dex.common.SectionItem;
import com.reandroid.dex.key.FieldKey;
import com.reandroid.dex.key.Key;
Expand Down Expand Up @@ -97,4 +98,7 @@ public Iterator<DexField> iterator(DexClass dexClass) {
}
};
}
default Iterator<IntegerReference> visitIntegers(){
return new DexIntegerVisitor(this);
}
}
113 changes: 113 additions & 0 deletions src/main/java/com/reandroid/dex/model/DexIntegerVisitor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* 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.model;

import com.reandroid.arsc.item.IntegerReference;
import com.reandroid.dex.data.*;
import com.reandroid.dex.ins.ConstNumber;
import com.reandroid.dex.ins.Ins;
import com.reandroid.dex.ins.InsArrayData;
import com.reandroid.dex.ins.InsSparseSwitchData;
import com.reandroid.dex.sections.SectionType;
import com.reandroid.dex.value.AnnotationValue;
import com.reandroid.dex.value.ArrayValue;
import com.reandroid.dex.value.DexValueBlock;
import com.reandroid.dex.value.IntValue;
import com.reandroid.utils.collection.*;

import java.util.Iterator;

public class DexIntegerVisitor extends CombiningIterator<IntegerReference, IntegerReference> {

public DexIntegerVisitor(DexClassRepository classRepository) {
super(getEncodedArrayReferences(classRepository), getCodeItemReferences(classRepository));
}

private static Iterator<IntegerReference> getEncodedArrayReferences(DexClassRepository repository) {
return new IterableIterator<EncodedArray, IntegerReference>(
repository.getItems(SectionType.ENCODED_ARRAY)
) {
@Override
public Iterator<IntegerReference> iterator(EncodedArray element) {
return DexIntegerVisitor.iterator(element);
}
};
}
private static Iterator<IntegerReference> getCodeItemReferences(DexClassRepository repository) {
return new IterableIterator<CodeItem, IntegerReference>(
repository.getItems(SectionType.CODE)
) {
@Override
public Iterator<IntegerReference> iterator(CodeItem element) {
return DexIntegerVisitor.iterator(element);
}
};
}

static Iterator<IntegerReference> iterator(CodeItem codeItem) {
return new IterableIterator<Ins, IntegerReference>(codeItem.getInstructionList().iterator()) {
@Override
public Iterator<IntegerReference> iterator(Ins element) {
return DexIntegerVisitor.iterator(element);
}
};
}

static Iterator<IntegerReference> iterator(Ins ins) {
if (ins instanceof ConstNumber) {
return SingleIterator.of((ConstNumber) ins);
} else if (ins instanceof InsArrayData) {
return ((InsArrayData) ins).getReferences();
} else if (ins instanceof InsSparseSwitchData) {
return InstanceIterator.of(((InsSparseSwitchData) ins).getLabels(), IntegerReference.class);
}
return EmptyIterator.of();
}
static Iterator<IntegerReference> iterator(EncodedArray encodedArray) {
return new IterableIterator<DexValueBlock<?>, IntegerReference>(encodedArray.iterator()) {
@Override
public Iterator<IntegerReference> iterator(DexValueBlock<?> element) {
return DexIntegerVisitor.iterator(element);
}
};
}
static Iterator<IntegerReference> iterator(DexValueBlock<?> valueBlock) {
if(valueBlock instanceof IntValue){
return SingleIterator.of((IntValue) valueBlock);
}else if(valueBlock instanceof ArrayValue) {
return iterator((ArrayValue) valueBlock);
}else if(valueBlock instanceof AnnotationValue) {
return iterator(((AnnotationValue) valueBlock).get());
}
return EmptyIterator.of();
}
private static Iterator<IntegerReference> iterator(ArrayValue arrayValue) {
return new IterableIterator<DexValueBlock<?>, IntegerReference>(arrayValue.iterator()) {
@Override
public Iterator<IntegerReference> iterator(DexValueBlock<?> element) {
return DexIntegerVisitor.iterator(element);
}
};
}
private static Iterator<IntegerReference> iterator(AnnotationItem arrayValue) {
return new IterableIterator<AnnotationElement, IntegerReference>(arrayValue.iterator()) {
@Override
public Iterator<IntegerReference> iterator(AnnotationElement element) {
return DexIntegerVisitor.iterator(element.getValue());
}
};
}
}

0 comments on commit 0b559d8

Please sign in to comment.