getOffset renamed to getAddress to be the same in AS2 and AS3

This commit is contained in:
honfika@gmail.com
2015-12-08 13:35:39 +01:00
parent e2e4bd70ce
commit adf9a5b49c
12 changed files with 65 additions and 71 deletions

View File

@@ -1043,10 +1043,10 @@ public class AVM2Code implements Cloneable {
}
public void markOffsets() {
long offset = 0;
long address = 0;
for (int i = 0; i < code.size(); i++) {
code.get(i).setOffset(offset);
offset += code.get(i).getBytesLength();
code.get(i).setAddress(address);
address += code.get(i).getBytesLength();
}
}
@@ -1240,14 +1240,14 @@ public class AVM2Code implements Cloneable {
Helper.byteArrayToHexWithHeader(writer, getBytes());
} else if (exportMode == ScriptExportMode.PCODE || exportMode == ScriptExportMode.PCODE_HEX) {
for (AVM2Instruction ins : code) {
long ofs = ins.getOffset();
long addr = ins.getAddress();
if (exportMode == ScriptExportMode.PCODE_HEX) {
writer.appendNoHilight("; ");
writer.appendNoHilight(Helper.bytesToHexString(ins.getBytes()));
writer.newLine();
}
if (Configuration.showAllAddresses.get() || importantOffsets.contains(ofs)) {
writer.appendNoHilight("ofs" + Helper.formatAddress(ofs) + ":");
if (Configuration.showAllAddresses.get() || importantOffsets.contains(addr)) {
writer.appendNoHilight("ofs" + Helper.formatAddress(addr) + ":");
}
/*for (int e = 0; e < body.exceptions.length; e++) {
if (body.exceptions[e].start == ofs) {
@@ -1263,7 +1263,7 @@ public class AVM2Code implements Cloneable {
if (!ins.isIgnored()) {
if (markOffsets) {
writer.append("", ofs, ins.getFileOffset());
writer.append("", addr, ins.getFileOffset());
}
writer.appendNoHilight(ins.toStringNoAddress(constants, new ArrayList<>()));
@@ -1328,7 +1328,7 @@ public class AVM2Code implements Cloneable {
while (max >= min) {
int mid = (min + max) / 2;
long midValue = code.get(mid).getOffset();
long midValue = code.get(mid).getAddress();
if (midValue == address) {
return mid;
} else if (midValue < address) {
@@ -1349,7 +1349,7 @@ public class AVM2Code implements Cloneable {
if (pos == code.size()) {
return getEndOffset();
}
return (int) code.get(pos).getOffset();
return (int) code.get(pos).getAddress();
}
public long getEndOffset() {
@@ -1358,7 +1358,7 @@ public class AVM2Code implements Cloneable {
}
AVM2Instruction ins = code.get(code.size() - 1);
return (int) (ins.getOffset() + ins.getBytesLength());
return (int) (ins.getAddress() + ins.getBytesLength());
}
/**
@@ -1974,11 +1974,11 @@ public class AVM2Code implements Cloneable {
for (int i = 0; i < code.size(); i++) {
AVM2Instruction ins = code.get(i);
if (ins.definition instanceof LookupSwitchIns) {
long target = ins.getOffset() + ins.operands[0];
ins.operands[0] = updater.updateOperandOffset(ins.getOffset(), target, ins.operands[0]);
long target = ins.getAddress() + ins.operands[0];
ins.operands[0] = updater.updateOperandOffset(ins.getAddress(), target, ins.operands[0]);
for (int k = 2; k < ins.operands.length; k++) {
target = ins.getOffset() + ins.operands[k];
ins.operands[k] = updater.updateOperandOffset(ins.getOffset(), target, ins.operands[k]);
target = ins.getAddress() + ins.operands[k];
ins.operands[k] = updater.updateOperandOffset(ins.getAddress(), target, ins.operands[k]);
}
} else {
/*for (int j = 0; j < ins.definition.operands.length; j++) {
@@ -1991,13 +1991,13 @@ public class AVM2Code implements Cloneable {
if (ins.definition instanceof IfTypeIns) {
long target = ins.getTargetAddress();
try {
ins.operands[0] = updater.updateOperandOffset(ins.getOffset(), target, ins.operands[0]);
ins.operands[0] = updater.updateOperandOffset(ins.getAddress(), target, ins.operands[0]);
} catch (ConvertException cex) {
throw new ConvertException("Invalid offset (" + ins + ")", i);
}
}
}
ins.setOffset(updater.updateInstructionOffset(ins.getOffset()));
ins.setAddress(updater.updateInstructionOffset(ins.getAddress()));
}
for (ABCException ex : body.exceptions) {
@@ -2071,7 +2071,7 @@ public class AVM2Code implements Cloneable {
}
AVM2Instruction ins = code.get(pos);
final long remOffset = ins.getOffset();
final long remOffset = ins.getAddress();
int bc = ins.getBytesLength();
final int byteCount = bc;
@@ -2136,7 +2136,7 @@ public class AVM2Code implements Cloneable {
*/
public void replaceInstruction(int pos, AVM2Instruction instruction, MethodBody body) {
AVM2Instruction oldInstruction = code.get(pos);
instruction.setOffset(oldInstruction.getOffset());
instruction.setAddress(oldInstruction.getAddress());
int oldByteCount = oldInstruction.getBytesLength();
int newByteCount = instruction.getBytesLength();
int byteDelta = newByteCount - oldByteCount;
@@ -2146,7 +2146,7 @@ public class AVM2Code implements Cloneable {
@Override
public long updateInstructionOffset(long address) {
if (address > instruction.getOffset()) {
if (address > instruction.getAddress()) {
return address + byteDelta;
}
return address;
@@ -2154,10 +2154,10 @@ public class AVM2Code implements Cloneable {
@Override
public int updateOperandOffset(long insAddr, long targetAddress, int offset) {
if (targetAddress > instruction.getOffset() && insAddr <= instruction.getOffset()) {
if (targetAddress > instruction.getAddress() && insAddr <= instruction.getAddress()) {
return offset + byteDelta;
}
if (targetAddress <= instruction.getOffset() && insAddr > instruction.getOffset()) {
if (targetAddress <= instruction.getAddress() && insAddr > instruction.getAddress()) {
return offset - byteDelta;
}
return offset;
@@ -2188,11 +2188,11 @@ public class AVM2Code implements Cloneable {
}
final int byteCount = instruction.getBytesLength();
if (pos == code.size()) {
instruction.setOffset(code.get(pos - 1).getOffset() + code.get(pos - 1).getBytesLength());
instruction.setAddress(code.get(pos - 1).getAddress() + code.get(pos - 1).getBytesLength());
} else {
instruction.setOffset(code.get(pos).getOffset());
instruction.setAddress(code.get(pos).getAddress());
}
final long x = instruction.getOffset();
final long x = instruction.getAddress();
updateOffsets(new OffsetUpdater() {
@Override
@@ -2240,7 +2240,7 @@ public class AVM2Code implements Cloneable {
return offset_jt;
}
}, body);
instruction.setOffset(x);
instruction.setAddress(x);
code.add(pos, instruction);
//checkValidOffsets(body);
}

View File

@@ -149,7 +149,7 @@ public class AVM2DeobfuscatorGetSet extends SWFDecompilerAdapter {
int regId = ((SetLocalTypeIns) def).getRegisterId(ins);
if (!stack.isEmpty() && (stack.peek() instanceof LocalRegAVM2Item) && (((LocalRegAVM2Item) stack.peek()).regIndex == regId)) {
stack.pop();
code.replaceInstruction(idx, new AVM2Instruction(ins.getOffset(), DeobfuscatePopIns.getInstance(), null), body);
code.replaceInstruction(idx, new AVM2Instruction(ins.getAddress(), DeobfuscatePopIns.getInstance(), null), body);
idx++;
continue;
}

View File

@@ -58,7 +58,7 @@ public class AVM2DeobfuscatorJumps extends SWFDecompilerAdapter {
if ((srcIns.definition instanceof JumpIns) || ((srcIns.definition instanceof IfTypeIns) && (r != i - 1))) {
int oldop = srcIns.operands[0];
srcIns.operands[0] = (int) (targetAddr - (srcIns.getOffset() + srcIns.getBytesLength()));
srcIns.operands[0] = (int) (targetAddr - (srcIns.getAddress() + srcIns.getBytesLength()));
if (srcIns.operands[0] != oldop) {
found = true;
}

View File

@@ -167,7 +167,7 @@ public class AVM2DeobfuscatorRegisters extends AVM2DeobfuscatorSimple {
SetLocalTypeIns slt = (SetLocalTypeIns) ins.definition;
int regId = slt.getRegisterId(ins);
if (singleRegisters.containsKey(regId)) {
code.replaceInstruction(i, new AVM2Instruction(ins.getOffset(), DeobfuscatePopIns.getInstance(), null), body);
code.replaceInstruction(i, new AVM2Instruction(ins.getAddress(), DeobfuscatePopIns.getInstance(), null), body);
}
}

View File

@@ -170,7 +170,7 @@ public class AVM2DeobfuscatorSimple extends SWFDecompilerAdapter {
}
AVM2Instruction ins = code.code.get(idx);
if (instructionsProcessed > 0 && refs.contains(ins.getOffset())) {
if (instructionsProcessed > 0 && refs.contains(ins.getAddress())) {
break;
}
@@ -315,17 +315,17 @@ public class AVM2DeobfuscatorSimple extends SWFDecompilerAdapter {
AVM2Instruction jumpIns = new AVM2Instruction(0, AVM2Instructions.Jump, new int[]{0});
//jumpIns.operands[0] = ins.operands[0] /*- ins.getBytes().length*/ + jumpIns.getBytes().length;
code.replaceInstruction(idx, jumpIns, body);
jumpIns.operands[0] = (int) (tarIns.getOffset() - jumpIns.getOffset() - jumpIns.getBytesLength());
jumpIns.operands[0] = (int) (tarIns.getAddress() - jumpIns.getAddress() - jumpIns.getBytesLength());
for (int s = 0; s < stackCount; s++) {
code.insertInstruction(idx, new AVM2Instruction(ins.getOffset(), DeobfuscatePopIns.getInstance(), null), true, body);
code.insertInstruction(idx, new AVM2Instruction(ins.getAddress(), DeobfuscatePopIns.getInstance(), null), true, body);
}
idx = code.adr2pos(jumpIns.getTargetAddress());
} else {
//System.err.println("replacing " + ins + " on " + idx + " with pop");
code.replaceInstruction(idx, new AVM2Instruction(ins.getOffset(), DeobfuscatePopIns.getInstance(), null), body);
code.replaceInstruction(idx, new AVM2Instruction(ins.getAddress(), DeobfuscatePopIns.getInstance(), null), body);
for (int s = 1 /*first is replaced*/; s < stackCount; s++) {
code.insertInstruction(idx, new AVM2Instruction(ins.getOffset(), DeobfuscatePopIns.getInstance(), null), true, body);
code.insertInstruction(idx, new AVM2Instruction(ins.getAddress(), DeobfuscatePopIns.getInstance(), null), true, body);
}
//ins.definition = DeobfuscatePopIns.getInstance();
idx++;

View File

@@ -367,17 +367,17 @@ public class AVM2DeobfuscatorSimpleOld extends SWFDecompilerAdapter {
AVM2Instruction jumpIns = new AVM2Instruction(0, AVM2Instructions.Jump, new int[]{0});
//jumpIns.operands[0] = ins.operands[0] /*- ins.getBytes().length*/ + jumpIns.getBytes().length;
code.replaceInstruction(idx, jumpIns, body);
jumpIns.operands[0] = (int) (tarIns.getOffset() - jumpIns.getOffset() - jumpIns.getBytesLength());
jumpIns.operands[0] = (int) (tarIns.getAddress() - jumpIns.getAddress() - jumpIns.getBytesLength());
for (int s = 0; s < stackCount; s++) {
code.insertInstruction(idx, new AVM2Instruction(ins.getOffset(), DeobfuscatePopIns.getInstance(), null), true, body);
code.insertInstruction(idx, new AVM2Instruction(ins.getAddress(), DeobfuscatePopIns.getInstance(), null), true, body);
}
idx = code.adr2pos(jumpIns.getTargetAddress());
} else {
//System.err.println("replacing " + ins + " on " + idx + " with pop");
code.replaceInstruction(idx, new AVM2Instruction(ins.getOffset(), DeobfuscatePopIns.getInstance(), null), body);
code.replaceInstruction(idx, new AVM2Instruction(ins.getAddress(), DeobfuscatePopIns.getInstance(), null), body);
for (int s = 1 /*first is replaced*/; s < stackCount; s++) {
code.insertInstruction(idx, new AVM2Instruction(ins.getOffset(), DeobfuscatePopIns.getInstance(), null), true, body);
code.insertInstruction(idx, new AVM2Instruction(ins.getAddress(), DeobfuscatePopIns.getInstance(), null), true, body);
}
//ins.definition = DeobfuscatePopIns.getInstance();
idx++;

View File

@@ -47,7 +47,7 @@ public class AVM2Instruction implements Cloneable, GraphSourceItem {
public int[] operands;
private long offset;
private long address;
public String comment;
@@ -64,7 +64,7 @@ public class AVM2Instruction implements Cloneable, GraphSourceItem {
@Override
public long getLineOffset() {
return getOffset();
return getAddress();
}
public void setFileLine(String file, int line) {
@@ -76,10 +76,10 @@ public class AVM2Instruction implements Cloneable, GraphSourceItem {
this(offset, AVM2Code.instructionSet[insructionCode], operands);
}
public AVM2Instruction(long offset, InstructionDefinition definition, int[] operands) {
public AVM2Instruction(long address, InstructionDefinition definition, int[] operands) {
this.definition = definition;
this.operands = operands != null && operands.length > 0 ? operands : null;
this.offset = offset;
this.address = address;
}
public byte[] getBytes() {
@@ -168,14 +168,14 @@ public class AVM2Instruction implements Cloneable, GraphSourceItem {
for (int i = 0; i < definition.operands.length; i++) {
switch (definition.operands[i]) {
case AVM2Code.DAT_OFFSET:
ret.add(offset + operands[i] + getBytesLength());
ret.add(address + operands[i] + getBytesLength());
break;
case AVM2Code.DAT_CASE_BASEOFFSET:
ret.add(offset + operands[i]);
ret.add(address + operands[i]);
break;
case AVM2Code.OPT_CASE_OFFSETS:
for (int j = i + 1; j < operands.length; j++) {
ret.add(offset + operands[j]);
ret.add(address + operands[j]);
}
break;
}
@@ -201,9 +201,9 @@ public class AVM2Instruction implements Cloneable, GraphSourceItem {
case AVM2Code.DAT_DOUBLE_INDEX:
return constants.getDouble(operands[idx]);
case AVM2Code.DAT_OFFSET:
return offset + operands[idx] + getBytesLength();
return address + operands[idx] + getBytesLength();
case AVM2Code.DAT_CASE_BASEOFFSET:
return offset + operands[idx];
return address + operands[idx];
case AVM2Code.OPT_CASE_OFFSETS:
return (long) operands[idx]; // offsets: offset + operands[i];
default:
@@ -278,12 +278,12 @@ public class AVM2Instruction implements Cloneable, GraphSourceItem {
case AVM2Code.DAT_OFFSET:
s.append(" ");
s.append("ofs");
s.append(Helper.formatAddress(offset + operands[i] + getBytesLength()));
s.append(Helper.formatAddress(address + operands[i] + getBytesLength()));
break;
case AVM2Code.DAT_CASE_BASEOFFSET:
s.append(" ");
s.append("ofs");
s.append(Helper.formatAddress(offset + operands[i]));
s.append(Helper.formatAddress(address + operands[i]));
break;
case AVM2Code.OPT_CASE_OFFSETS:
s.append(" ");
@@ -291,7 +291,7 @@ public class AVM2Instruction implements Cloneable, GraphSourceItem {
for (int j = i + 1; j < operands.length; j++) {
s.append(" ");
s.append("ofs");
s.append(Helper.formatAddress(offset + operands[j]));
s.append(Helper.formatAddress(address + operands[j]));
}
break;
default:
@@ -319,7 +319,7 @@ public class AVM2Instruction implements Cloneable, GraphSourceItem {
}
public GraphTextWriter toString(GraphTextWriter writer, LocalData localData) {
writer.appendNoHilight(Helper.formatAddress(offset) + " " + String.format("%-30s", Helper.byteArrToString(getBytes())) + definition.instructionName);
writer.appendNoHilight(Helper.formatAddress(address) + " " + String.format("%-30s", Helper.byteArrToString(getBytes())) + definition.instructionName);
writer.appendNoHilight(getParams(localData.constantsAvm2, localData.fullyQualifiedNames) + getComment());
return writer;
}
@@ -376,16 +376,16 @@ public class AVM2Instruction implements Cloneable, GraphSourceItem {
}
@Override
public long getOffset() {
return offset;
public long getAddress() {
return address;
}
public void setOffset(long offset) {
this.offset = offset;
public void setAddress(long address) {
this.address = address;
}
public long getTargetAddress() {
return offset + 4 /*getBytesLength()*/ + operands[0];
return address + 4 /*getBytesLength()*/ + operands[0];
}
public void setTargetOffset(int offset) {
@@ -399,13 +399,13 @@ public class AVM2Instruction implements Cloneable, GraphSourceItem {
ret.add(code.adr2pos(getTargetAddress()));
if (!(definition instanceof JumpIns)) {
ret.add(code.adr2pos(offset + getBytesLength()));
ret.add(code.adr2pos(address + getBytesLength()));
}
}
if (definition instanceof LookupSwitchIns) {
ret.add(code.adr2pos(offset + operands[0]));
ret.add(code.adr2pos(address + operands[0]));
for (int k = 2; k < operands.length; k++) {
ret.add(code.adr2pos(offset + operands[k]));
ret.add(code.adr2pos(address + operands[k]));
}
}
return ret;

View File

@@ -135,7 +135,7 @@ public abstract class InstructionDefinition implements Serializable {
}
protected void illegalOpCode(LocalDataArea lda, AVM2Instruction ins) throws AVM2VerifyErrorException {
throw new AVM2VerifyErrorException(AVM2VerifyErrorException.ILLEGAL_OPCODE, lda.isDebug(), new Object[]{lda.methodName, instructionCode, ins.getOffset()});
throw new AVM2VerifyErrorException(AVM2VerifyErrorException.ILLEGAL_OPCODE, lda.isDebug(), new Object[]{lda.methodName, instructionCode, ins.getAddress()});
}
public void translate(AVM2LocalData localData, TranslateStack stack, AVM2Instruction ins, List<GraphTargetItem> output, String path) throws InterruptedException {
@@ -238,7 +238,7 @@ public abstract class InstructionDefinition implements Serializable {
if (constants.getMultiname(multinameIndex).needsName()) {
name = stack.get(pos).toString();
} else {
name = GraphTextWriter.hilighOffset(constants.getMultiname(multinameIndex).getName(constants, fullyQualifiedNames, false), ins.getOffset());
name = GraphTextWriter.hilighOffset(constants.getMultiname(multinameIndex).getName(constants, fullyQualifiedNames, false), ins.getAddress());
}
return name + ns;
}

View File

@@ -969,9 +969,9 @@ public class ASM3Parser {
AVM2Instruction ins = code.code.get((int) oi.insPosition);
int relOffset;
if (oi instanceof CaseOffsetItem) {
relOffset = li.offset - (int) ins.getOffset();
relOffset = li.offset - (int) ins.getAddress();
} else {
relOffset = li.offset - ((int) ins.getOffset() + ins.getBytesLength());
relOffset = li.offset - ((int) ins.getAddress() + ins.getBytesLength());
}
ins.operands[oi.insOperandIndex] = relOffset;
}

View File

@@ -122,7 +122,6 @@ public abstract class Action implements GraphSourceItem {
*/
public int actionLength;
// todo: honfika: rename to offset to be similar with AS3
private long address;
@Override
@@ -181,6 +180,7 @@ public abstract class Action implements GraphSourceItem {
*
* @return address of this action
*/
@Override
public long getAddress() {
return address;
}
@@ -209,7 +209,6 @@ public abstract class Action implements GraphSourceItem {
}
public int getTotalActionLength() {
// honfika: todo rename to getBytesLength to match the name with the similar method in AS3
return actionLength + 1 + (actionCode >= 0x80 ? 2 : 0);
}
@@ -931,11 +930,6 @@ public abstract class Action implements GraphSourceItem {
return false;
}
@Override
public long getOffset() {
return getAddress();
}
@Override
public List<Integer> getBranches(GraphSource code) {
return new ArrayList<>();

View File

@@ -201,7 +201,7 @@ public class HighlightedTextWriter extends GraphTextWriter {
HighlightData ndata = new HighlightData();
ndata.merge(itemPos.data);
ndata.merge(data);
ndata.offset = src.getOffset() + pos;
ndata.offset = src.getAddress() + pos;
ndata.fileOffset = src.getFileOffset();
if (itemPos.startLineItem != null) {
ndata.firstLineOffset = itemPos.startLineItem.getLineOffset();

View File

@@ -37,7 +37,7 @@ public interface GraphSourceItem extends Serializable, Cloneable {
public boolean isExit();
public long getOffset();
public long getAddress();
public long getLineOffset();