From 58596ca5e5ca1a12dd47ea6ec8b34dc82e209fc6 Mon Sep 17 00:00:00 2001 From: "honfika@gmail.com" Date: Tue, 3 Nov 2015 11:36:32 +0100 Subject: [PATCH] execution of StringExtract and MBStringExtract implemented --- .../model/MBStringExtractActionItem.java | 30 +++++++++++++++++++ .../action/model/StringExtractActionItem.java | 30 +++++++++++++++++++ .../action/swf4/ActionMBStringExtract.java | 11 +++++++ .../action/swf4/ActionStringExtract.java | 11 +++++++ 4 files changed, 82 insertions(+) diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/model/MBStringExtractActionItem.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/model/MBStringExtractActionItem.java index 88a192f3b..24c239588 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/model/MBStringExtractActionItem.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/model/MBStringExtractActionItem.java @@ -18,6 +18,7 @@ package com.jpexs.decompiler.flash.action.model; import com.jpexs.decompiler.flash.SourceGeneratorLocalData; import com.jpexs.decompiler.flash.action.swf4.ActionMBStringExtract; +import com.jpexs.decompiler.flash.ecma.EcmaScript; import com.jpexs.decompiler.flash.helpers.GraphTextWriter; import com.jpexs.decompiler.graph.CompilationException; import com.jpexs.decompiler.graph.GraphSourceItem; @@ -72,6 +73,35 @@ public class MBStringExtractActionItem extends ActionItem { return ret; } + @Override + public Object getResult() { + return getResult(count.getResult(), index.getResult(), value.getResult()); + } + + public static String getResult(Object count, Object index, Object value) { + String str = EcmaScript.toString(value); + int idx = (int) (double) EcmaScript.toNumber(index); + idx--; // index seems to be 1 based + + int cnt = (int) (double) EcmaScript.toNumber(count); + + /*if (idx < 0) { + idx = str.length() + idx; + }*/ + if (idx < 0) { + idx = 0; + } else if (idx > str.length()) { + return ""; + } + + if (cnt < 0) { + cnt = str.length(); + } + + int endIdx = Math.min(str.length(), idx + cnt); + return str.substring(idx, endIdx); + } + @Override public List toSource(SourceGeneratorLocalData localData, SourceGenerator generator) throws CompilationException { return toSourceMerge(localData, generator, value, index, count, new ActionMBStringExtract()); diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/model/StringExtractActionItem.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/model/StringExtractActionItem.java index 3169789de..c85cb5116 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/model/StringExtractActionItem.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/model/StringExtractActionItem.java @@ -18,6 +18,7 @@ package com.jpexs.decompiler.flash.action.model; import com.jpexs.decompiler.flash.SourceGeneratorLocalData; import com.jpexs.decompiler.flash.action.swf4.ActionStringExtract; +import com.jpexs.decompiler.flash.ecma.EcmaScript; import com.jpexs.decompiler.flash.helpers.GraphTextWriter; import com.jpexs.decompiler.graph.CompilationException; import com.jpexs.decompiler.graph.GraphSourceItem; @@ -61,6 +62,35 @@ public class StringExtractActionItem extends ActionItem { return ret; } + @Override + public Object getResult() { + return getResult(count.getResult(), index.getResult(), value.getResult()); + } + + public static String getResult(Object count, Object index, Object value) { + String str = EcmaScript.toString(value); + int idx = (int) (double) EcmaScript.toNumber(index); + idx--; // index seems to be 1 based + + int cnt = (int) (double) EcmaScript.toNumber(count); + + /*if (idx < 0) { + idx = str.length() + idx; + }*/ + if (idx < 0) { + idx = 0; + } else if (idx > str.length()) { + return ""; + } + + if (cnt < 0) { + cnt = str.length(); + } + + int endIdx = Math.min(str.length(), idx + cnt); + return str.substring(idx, endIdx); + } + @Override public List toSource(SourceGeneratorLocalData localData, SourceGenerator generator) throws CompilationException { return toSourceMerge(localData, generator, value, index, count, new ActionStringExtract()); diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf4/ActionMBStringExtract.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf4/ActionMBStringExtract.java index 2ad4cac36..d424f0188 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf4/ActionMBStringExtract.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf4/ActionMBStringExtract.java @@ -18,6 +18,7 @@ package com.jpexs.decompiler.flash.action.swf4; import com.jpexs.decompiler.flash.BaseLocalData; import com.jpexs.decompiler.flash.action.Action; +import com.jpexs.decompiler.flash.action.LocalDataArea; import com.jpexs.decompiler.flash.action.model.MBStringExtractActionItem; import com.jpexs.decompiler.graph.GraphTargetItem; import com.jpexs.decompiler.graph.TranslateStack; @@ -35,6 +36,16 @@ public class ActionMBStringExtract extends Action { return "MBStringExtract"; } + @Override + public boolean execute(LocalDataArea lda) { + if (lda.stack.size() < 3) { + return false; + } + + lda.stack.push(MBStringExtractActionItem.getResult(lda.pop(), lda.pop(), lda.pop())); + return true; + } + @Override public void translate(TranslateStack stack, List output, HashMap regNames, HashMap variables, HashMap functions, int staticOperation, String path) { GraphTargetItem count = stack.pop(); diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf4/ActionStringExtract.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf4/ActionStringExtract.java index 11d2bf9e6..c49a76c34 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf4/ActionStringExtract.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf4/ActionStringExtract.java @@ -18,6 +18,7 @@ package com.jpexs.decompiler.flash.action.swf4; import com.jpexs.decompiler.flash.BaseLocalData; import com.jpexs.decompiler.flash.action.Action; +import com.jpexs.decompiler.flash.action.LocalDataArea; import com.jpexs.decompiler.flash.action.model.StringExtractActionItem; import com.jpexs.decompiler.graph.GraphTargetItem; import com.jpexs.decompiler.graph.TranslateStack; @@ -35,6 +36,16 @@ public class ActionStringExtract extends Action { return "StringExtract"; } + @Override + public boolean execute(LocalDataArea lda) { + if (lda.stack.size() < 3) { + return false; + } + + lda.stack.push(StringExtractActionItem.getResult(lda.pop(), lda.pop(), lda.pop())); + return true; + } + @Override public void translate(TranslateStack stack, List output, HashMap regNames, HashMap variables, HashMap functions, int staticOperation, String path) { GraphTargetItem count = stack.pop();