From c66a8b031c8a842b89b9453684288c964fd6b48a Mon Sep 17 00:00:00 2001 From: REAndroid Date: Wed, 27 Nov 2024 20:12:57 +0100 Subject: [PATCH] [DEX] Flexible instruction data formatting --- .../java/com/reandroid/dex/ins/Ins21lh.java | 4 ++++ .../reandroid/dex/ins/InsArrayDataList.java | 2 +- .../dex/ins/InsConstCommentHelper.java | 2 +- .../com/reandroid/dex/ins/InsConstWide.java | 5 ++++- .../com/reandroid/dex/ins/InsConstWide16.java | 4 ++++ .../com/reandroid/dex/ins/InsConstWide32.java | 4 ++++ .../java/com/reandroid/dex/ins/SizeXIns.java | 13 +++++-------- .../dex/smali/model/SmaliInstruction.java | 11 ++++++++--- .../dex/smali/model/SmaliValueByte.java | 2 +- .../dex/smali/model/SmaliValueDouble.java | 2 +- .../dex/smali/model/SmaliValueFloat.java | 2 +- .../dex/smali/model/SmaliValueInteger.java | 2 +- .../dex/smali/model/SmaliValueShort.java | 2 +- .../dex/smali/model/SmaliValueX.java | 19 +++---------------- 14 files changed, 39 insertions(+), 35 deletions(-) diff --git a/src/main/java/com/reandroid/dex/ins/Ins21lh.java b/src/main/java/com/reandroid/dex/ins/Ins21lh.java index 47f0f8377..5d6158054 100644 --- a/src/main/java/com/reandroid/dex/ins/Ins21lh.java +++ b/src/main/java/com/reandroid/dex/ins/Ins21lh.java @@ -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){ diff --git a/src/main/java/com/reandroid/dex/ins/InsArrayDataList.java b/src/main/java/com/reandroid/dex/ins/InsArrayDataList.java index 8feda67c9..5e1f6cf4d 100644 --- a/src/main/java/com/reandroid/dex/ins/InsArrayDataList.java +++ b/src/main/java/com/reandroid/dex/ins/InsArrayDataList.java @@ -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)); diff --git a/src/main/java/com/reandroid/dex/ins/InsConstCommentHelper.java b/src/main/java/com/reandroid/dex/ins/InsConstCommentHelper.java index a5e067b8c..c916edf6b 100644 --- a/src/main/java/com/reandroid/dex/ins/InsConstCommentHelper.java +++ b/src/main/java/com/reandroid/dex/ins/InsConstCommentHelper.java @@ -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); } diff --git a/src/main/java/com/reandroid/dex/ins/InsConstWide.java b/src/main/java/com/reandroid/dex/ins/InsConstWide.java index c929053ea..b2e6335d6 100644 --- a/src/main/java/com/reandroid/dex/ins/InsConstWide.java +++ b/src/main/java/com/reandroid/dex/ins/InsConstWide.java @@ -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); diff --git a/src/main/java/com/reandroid/dex/ins/InsConstWide16.java b/src/main/java/com/reandroid/dex/ins/InsConstWide16.java index ae81be8d3..534838a61 100644 --- a/src/main/java/com/reandroid/dex/ins/InsConstWide16.java +++ b/src/main/java/com/reandroid/dex/ins/InsConstWide16.java @@ -47,6 +47,10 @@ public long getLong() { public long getDataAsLong() { return getLong(); } + @Override + public void setData(long data) { + set(data); + } @Override public int getRegister() { diff --git a/src/main/java/com/reandroid/dex/ins/InsConstWide32.java b/src/main/java/com/reandroid/dex/ins/InsConstWide32.java index 361c62951..3a640ea0f 100644 --- a/src/main/java/com/reandroid/dex/ins/InsConstWide32.java +++ b/src/main/java/com/reandroid/dex/ins/InsConstWide32.java @@ -34,6 +34,10 @@ public long getLong() { public long getDataAsLong() { return getLong(); } + @Override + public void setData(long data) { + set(data); + } @Override public int getRegister() { diff --git a/src/main/java/com/reandroid/dex/ins/SizeXIns.java b/src/main/java/com/reandroid/dex/ins/SizeXIns.java index 009577aee..a72958a16 100644 --- a/src/main/java/com/reandroid/dex/ins/SizeXIns.java +++ b/src/main/java/com/reandroid/dex/ins/SizeXIns.java @@ -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); } @@ -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()); } } diff --git a/src/main/java/com/reandroid/dex/smali/model/SmaliInstruction.java b/src/main/java/com/reandroid/dex/smali/model/SmaliInstruction.java index 97737e621..b84e3ad1a 100644 --- a/src/main/java/com/reandroid/dex/smali/model/SmaliInstruction.java +++ b/src/main/java/com/reandroid/dex/smali/model/SmaliInstruction.java @@ -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; diff --git a/src/main/java/com/reandroid/dex/smali/model/SmaliValueByte.java b/src/main/java/com/reandroid/dex/smali/model/SmaliValueByte.java index e4fc95e5e..e53857587 100644 --- a/src/main/java/com/reandroid/dex/smali/model/SmaliValueByte.java +++ b/src/main/java/com/reandroid/dex/smali/model/SmaliValueByte.java @@ -63,7 +63,7 @@ public int unsignedInt() { } @Override public long asLongValue() { - return getValue() & 0xffL; + return getValue(); } @Override public PrimitiveKey getKey() { diff --git a/src/main/java/com/reandroid/dex/smali/model/SmaliValueDouble.java b/src/main/java/com/reandroid/dex/smali/model/SmaliValueDouble.java index 97bab90be..57db305d5 100644 --- a/src/main/java/com/reandroid/dex/smali/model/SmaliValueDouble.java +++ b/src/main/java/com/reandroid/dex/smali/model/SmaliValueDouble.java @@ -61,7 +61,7 @@ public int unsignedInt() { } @Override public long asLongValue() { - return getNumber().longValue(); + return Double.doubleToLongBits(getValue()); } @Override public PrimitiveKey getKey() { diff --git a/src/main/java/com/reandroid/dex/smali/model/SmaliValueFloat.java b/src/main/java/com/reandroid/dex/smali/model/SmaliValueFloat.java index 363e1b08c..6476166cb 100644 --- a/src/main/java/com/reandroid/dex/smali/model/SmaliValueFloat.java +++ b/src/main/java/com/reandroid/dex/smali/model/SmaliValueFloat.java @@ -60,7 +60,7 @@ public int unsignedInt() { } @Override public long asLongValue() { - return getNumber().intValue() & 0xffffffffL; + return Float.floatToIntBits(getValue()); } @Override public PrimitiveKey getKey() { diff --git a/src/main/java/com/reandroid/dex/smali/model/SmaliValueInteger.java b/src/main/java/com/reandroid/dex/smali/model/SmaliValueInteger.java index 9d66f84ab..0f01aec9c 100644 --- a/src/main/java/com/reandroid/dex/smali/model/SmaliValueInteger.java +++ b/src/main/java/com/reandroid/dex/smali/model/SmaliValueInteger.java @@ -62,7 +62,7 @@ public int unsignedInt() { } @Override public long asLongValue() { - return getValue() & 0xffffffffL; + return getValue(); } @Override public PrimitiveKey getKey() { diff --git a/src/main/java/com/reandroid/dex/smali/model/SmaliValueShort.java b/src/main/java/com/reandroid/dex/smali/model/SmaliValueShort.java index 3c2c56d89..09c49d839 100644 --- a/src/main/java/com/reandroid/dex/smali/model/SmaliValueShort.java +++ b/src/main/java/com/reandroid/dex/smali/model/SmaliValueShort.java @@ -62,7 +62,7 @@ public int unsignedInt() { } @Override public long asLongValue() { - return getValue() & 0xffffL; + return getValue(); } @Override public PrimitiveKey getKey() { diff --git a/src/main/java/com/reandroid/dex/smali/model/SmaliValueX.java b/src/main/java/com/reandroid/dex/smali/model/SmaliValueX.java index dc1ae247a..42af7b2cb 100644 --- a/src/main/java/com/reandroid/dex/smali/model/SmaliValueX.java +++ b/src/main/java/com/reandroid/dex/smali/model/SmaliValueX.java @@ -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; @@ -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); }