Skip to content

Commit

Permalink
[DEX] Flexible instruction data formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
REAndroid committed Nov 27, 2024
1 parent f49b14f commit c66a8b0
Show file tree
Hide file tree
Showing 14 changed files with 39 additions and 35 deletions.
4 changes: 4 additions & 0 deletions src/main/java/com/reandroid/dex/ins/Ins21lh.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ public long getDataAsLong(){
return (long) getData() << 48;
}
@Override
public void setData(long data) {
setLong(data);
}
@Override
public void setLong(long data){
InsConstWide insConstWide = mReplaced;
if(insConstWide != null){
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/reandroid/dex/ins/InsArrayDataList.java
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ private void appendFloatOrDoubleComment(TypeKey arrayType, SmaliWriter writer) {
TypeKey typeKey = arrayType.setArrayDimension(0);
if (TypeKey.TYPE_F.equals(typeKey)) {
float f = Float.intBitsToFloat((int) data);
writer.appendComment(Float.toString(f));
writer.appendComment(f + "f");
} else if (TypeKey.TYPE_D.equals(typeKey)) {
double d = Double.longBitsToDouble(data);
writer.appendComment(Double.toString(d));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static String getCommentForConstNumber(SizeXIns sizeXIns) {
if (TypeKey.TYPE_D.equals(typeKey)) {
comment = Double.toString(Double.longBitsToDouble(data));
} else if (TypeKey.TYPE_F.equals(typeKey)) {
comment = Float.toString(Float.intBitsToFloat((int) data));
comment = Float.intBitsToFloat((int) data) + "f";
} else if (TypeKey.TYPE_C.equals(typeKey)) {
comment = safeQuotedChar((char) data);
}
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/com/reandroid/dex/ins/InsConstWide.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ public long getLong() {
public long getDataAsLong() {
return getLong();
}

@Override
public void setData(long data) {
set(data);
}
@Override
public int getRegister() {
return getRegister(0);
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/reandroid/dex/ins/InsConstWide16.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ public long getLong() {
public long getDataAsLong() {
return getLong();
}
@Override
public void setData(long data) {
set(data);
}

@Override
public int getRegister() {
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/reandroid/dex/ins/InsConstWide32.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public long getLong() {
public long getDataAsLong() {
return getLong();
}
@Override
public void setData(long data) {
set(data);
}

@Override
public int getRegister() {
Expand Down
13 changes: 5 additions & 8 deletions src/main/java/com/reandroid/dex/ins/SizeXIns.java
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ public int getSignedData(){
public long getDataAsLong() {
return getSignedData();
}
public void setData(long data){
setData((int) data);
}
public void setData(int data){
setShort(2, data);
}
Expand Down Expand Up @@ -383,14 +386,8 @@ private void fromSmaliKey(SmaliInstruction smaliInstruction){
setKey(smaliInstruction.getKey());
}
private void fromSmaliData(SmaliInstruction smaliInstruction) {
Number data = smaliInstruction.getData();
if(data == null){
return;
}
if(data instanceof Long){
setLong((Long) data);
}else {
setData(data.intValue());
if (getSectionType() == null && smaliInstruction.hasNumberData()) {
setData(smaliInstruction.getDataAsLong());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,20 @@ public Key getKey2(){
}
return null;
}
public Number getData() {
public boolean hasNumberData() {
SmaliInstructionOperand operand = getOperand();
return operand instanceof SmaliInstructionOperand.SmaliHexOperand ||
operand instanceof SmaliInstructionOperand.SmaliLabelOperand;
}
public long getDataAsLong() {
SmaliInstructionOperand operand = getOperand();
if(operand instanceof SmaliInstructionOperand.SmaliHexOperand){
return ((SmaliInstructionOperand.SmaliHexOperand) operand).getNumber();
return operand.asLongValue();
}
if(operand instanceof SmaliInstructionOperand.SmaliLabelOperand){
return operand.getIntegerData() - getAddress();
}
return null;
return 0;
}
public int getAddress() {
return address;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public int unsignedInt() {
}
@Override
public long asLongValue() {
return getValue() & 0xffL;
return getValue();
}
@Override
public PrimitiveKey getKey() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public int unsignedInt() {
}
@Override
public long asLongValue() {
return getNumber().longValue();
return Double.doubleToLongBits(getValue());
}
@Override
public PrimitiveKey getKey() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public int unsignedInt() {
}
@Override
public long asLongValue() {
return getNumber().intValue() & 0xffffffffL;
return Float.floatToIntBits(getValue());
}
@Override
public PrimitiveKey getKey() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public int unsignedInt() {
}
@Override
public long asLongValue() {
return getValue() & 0xffffffffL;
return getValue();
}
@Override
public PrimitiveKey getKey() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public int unsignedInt() {
}
@Override
public long asLongValue() {
return getValue() & 0xffffL;
return getValue();
}
@Override
public PrimitiveKey getKey() {
Expand Down
19 changes: 3 additions & 16 deletions src/main/java/com/reandroid/dex/smali/model/SmaliValueX.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@

import com.reandroid.dex.key.Key;
import com.reandroid.dex.key.PrimitiveKey;
import com.reandroid.dex.smali.SmaliParseException;
import com.reandroid.dex.smali.SmaliReader;
import com.reandroid.dex.smali.SmaliWriter;
import com.reandroid.dex.value.DexValueType;
import com.reandroid.utils.HexUtil;
import com.reandroid.utils.NumberX;
import com.reandroid.utils.NumbersUtil;

Expand Down Expand Up @@ -53,20 +51,9 @@ public void append(SmaliWriter writer) throws IOException {
@Override
public void parse(SmaliReader reader) throws IOException {
reader.skipSpaces();
int position = reader.position();
long l;
try {
String s = reader.readStringForNumber();
int i = s.length() - 1;
char last = s.charAt(i);
if (last == 'L' || last == 'S' || last == 's' || last == 't') {
s = s.substring(0, i);
}
l = HexUtil.parseHexLong(s);
} catch (NumberFormatException ex){
reader.position(position);
throw new SmaliParseException(ex.getMessage(), reader);
}
SmaliValueNumber<?> number = SmaliValueNumber.createNumber(reader);
number.parse(reader);
long l = number.asLongValue();
int width = NumbersUtil.max(this.getWidth(), NumberX.widthOfSigned(l));
set(width, l);
}
Expand Down

0 comments on commit c66a8b0

Please sign in to comment.