Issue #306 AS1/2 direct editing - reserved words handling

This commit is contained in:
Jindra Petk
2014-03-02 10:11:26 +01:00
parent 1029d2385f
commit a10ebc8682
8 changed files with 1745 additions and 1585 deletions

View File

@@ -109,7 +109,26 @@ public class Action implements GraphSourceItem {
public int actionLength;
public long containerSWFOffset;
private long address;
public static final String[] reservedWords = {
"as", "break", "case", "catch", "class", "const", "continue", "default", "delete", "do", "each", "else",
"extends", "false", "finally", "for", "function", "get", "if", "implements", "import", "in", "instanceof",
"interface", "internal", "is", "native", "new", "null", "override", "package", "private", "protected", "public",
"return", "set", "super", "switch", "this", "throw", "true", "try", "typeof", "use", "var", /*"void",*/ "while",
"with", "dynamic", "default", "final", "in"};
public static boolean isReservedWord(String s) {
if(s == null){
return false;
}
for (String rw : reservedWords) {
if (rw.equals(s.trim())) {
return true;
}
}
return false;
}
public long getFileAddress() {
return containerSWFOffset + getAddress();
}

View File

@@ -39,25 +39,13 @@ public class ActionDeobfuscation {
public HashSet<String> allVariableNamesStr = new HashSet<>();
private final HashMap<String, Integer> typeCounts = new HashMap<>();
public static final String[] reservedWords = {
"as", "break", "case", "catch", "class", "const", "continue", "default", "delete", "do", "each", "else",
"extends", "false", "finally", "for", "function", "get", "if", "implements", "import", "in", "instanceof",
"interface", "internal", "is", "native", "new", "null", "override", "package", "private", "protected", "public",
"return", "set", "super", "switch", "this", "throw", "true", "try", "typeof", "use", "var", /*"void",*/ "while",
"with", "dynamic", "default", "final", "in"};
public static final String VALID_FIRST_CHARACTERS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_";
public static final String VALID_NEXT_CHARACTERS = VALID_FIRST_CHARACTERS + "0123456789";
public static final String FOO_CHARACTERS = "bcdfghjklmnpqrstvwz";
public static final String FOO_JOIN_CHARACTERS = "aeiouy";
private boolean isReserved(String s) {
for (String rw : reservedWords) {
if (rw.equals(s.trim())) {
return true;
}
}
return false;
}
private String fooString(HashMap<String, String> deobfuscated, String orig, boolean firstUppercase, int rndSize) {
boolean exists;
@@ -84,7 +72,7 @@ public class ActionDeobfuscation {
rndSize += 1;
continue loopfoo;
}
if (isReserved(ret)) {
if (Action.isReservedWord(ret)) {
exists = true;
rndSize += 1;
continue;
@@ -200,7 +188,7 @@ public class ActionDeobfuscation {
}
}
if (isReserved(s)) {
if (Action.isReservedWord(s)) {
isValid = false;
}

View File

@@ -17,6 +17,7 @@
package com.jpexs.decompiler.flash.action.model;
import com.jpexs.decompiler.flash.SourceGeneratorLocalData;
import com.jpexs.decompiler.flash.action.Action;
import com.jpexs.decompiler.flash.action.swf5.ActionGetMember;
import com.jpexs.decompiler.flash.helpers.GraphTextWriter;
import com.jpexs.decompiler.graph.GraphSourceItem;
@@ -47,16 +48,18 @@ public class GetMemberActionItem extends ActionItem {
@Override
public GraphTextWriter appendTo(GraphTextWriter writer, LocalData localData) throws InterruptedException {
if (!((memberName instanceof DirectValueActionItem) && (((DirectValueActionItem) memberName).value instanceof String))) {
//if(!(functionName instanceof GetVariableActionItem))
object.toString(writer, localData);
writer.append("[");
stripQuotes(memberName, localData, writer);
return writer.append("]");
}
object.toString(writer, localData);
writer.append(".");
return stripQuotes(memberName, localData, writer);
if ((memberName instanceof DirectValueActionItem) && (((DirectValueActionItem) memberName).value instanceof String)) {
String memNameStr = (String) ((DirectValueActionItem) memberName).value;
if (!Action.isReservedWord(memNameStr)) {
writer.append(".");
return stripQuotes(memberName, localData, writer);
}
}
writer.append("[");
memberName.toString(writer, localData);
return writer.append("]");
}
@Override

View File

@@ -17,6 +17,7 @@
package com.jpexs.decompiler.flash.action.model;
import com.jpexs.decompiler.flash.SourceGeneratorLocalData;
import com.jpexs.decompiler.flash.action.Action;
import com.jpexs.decompiler.flash.action.parser.script.ActionSourceGenerator;
import com.jpexs.decompiler.flash.action.swf4.ActionPush;
import com.jpexs.decompiler.flash.action.swf4.RegisterNumber;
@@ -81,20 +82,19 @@ public class SetMemberActionItem extends ActionItem implements SetTypeActionItem
@Override
public GraphTextWriter appendTo(GraphTextWriter writer, LocalData localData) throws InterruptedException {
if (!((objectName instanceof DirectValueActionItem) && (((DirectValueActionItem) objectName).value instanceof String))) {
//if(!(functionName instanceof GetVariableActionItem))
object.toString(writer, localData);
writer.append("[");
stripQuotes(objectName, localData, writer);
writer.append("]");
writer.append(" = ");
return value.toString(writer, localData);
}
object.toString(writer, localData);
writer.append(".");
stripQuotes(objectName, localData, writer);
if ((objectName instanceof DirectValueActionItem) && (((DirectValueActionItem) objectName).value instanceof String) && !Action.isReservedWord((String) ((DirectValueActionItem) objectName).value)) {
writer.append(".");
stripQuotes(objectName, localData, writer);
} else {
writer.append("[");
objectName.toString(writer, localData);
writer.append("]");
}
writer.append(" = ");
return value.toString(writer, localData);
}
@Override

View File

@@ -397,7 +397,7 @@ public class ActionScriptParser {
switch (s.type) {
case FUNCTION:
s = lex();
expected(s, lexer.yyline(), SymbolType.IDENTIFIER);
expected(s, lexer.yyline(), SymbolType.IDENTIFIER, SymbolGroup.GLOBALFUNC);
String fname = s.value.toString();
if (fname.equals(classNameStr)) { //constructor
constr = (function(!isInterface, "", true, variables));
@@ -925,7 +925,7 @@ public class ActionScriptParser {
break;
case FUNCTION:
s = lexer.lex();
expected(s, lexer.yyline(), SymbolType.IDENTIFIER);
expected(s, lexer.yyline(), SymbolType.IDENTIFIER, SymbolGroup.GLOBALFUNC);
ret = (function(true, s.value.toString(), false, variables));
break;
case VAR:
@@ -1553,7 +1553,7 @@ public class ActionScriptParser {
case FUNCTION:
s = lexer.lex();
String fname = "";
if (s.type == SymbolType.IDENTIFIER) {
if (s.isType(SymbolType.IDENTIFIER,SymbolGroup.GLOBALFUNC)) {
fname = s.value.toString();
} else {
lexer.pushback(s);

View File

@@ -43,10 +43,17 @@ public class ParsedSymbol {
return group.toString() + " " + type.toString() + " " + (value != null ? value.toString() : "");
}
public boolean isType(SymbolType... types) {
for (SymbolType t : types) {
if (type == t) {
return true;
public boolean isType(Object... types) {
for (Object t : types) {
if (t instanceof SymbolGroup) {
if (group == t) {
return true;
}
}
if (t instanceof SymbolType) {
if (type == t) {
return true;
}
}
}
return false;

View File

@@ -195,7 +195,7 @@ SingleCharacter = [^\r\n\'\\]
"stop" { return new ParsedSymbol(SymbolGroup.GLOBALFUNC,SymbolType.STOP,yytext()); }
"stopAllSounds" { return new ParsedSymbol(SymbolGroup.GLOBALFUNC,SymbolType.STOPALLSOUNDS,yytext()); }
"toggleHighQuality" { return new ParsedSymbol(SymbolGroup.GLOBALFUNC,SymbolType.TOGGLEHIGHQUALITY,yytext()); }
"ifFrameLoaded" { return new ParsedSymbol(SymbolGroup.GLOBALFUNC,SymbolType.IFFRAMELOADED,yytext()); }
"ifFrameLoaded" { return new ParsedSymbol(SymbolGroup.KEYWORD,SymbolType.IFFRAMELOADED,yytext()); }
"ord" { return new ParsedSymbol(SymbolGroup.GLOBALFUNC,SymbolType.ORD,yytext()); }
"chr" { return new ParsedSymbol(SymbolGroup.GLOBALFUNC,SymbolType.CHR,yytext()); }
"duplicateMovieClip" { return new ParsedSymbol(SymbolGroup.GLOBALFUNC,SymbolType.DUPLICATEMOVIECLIP,yytext()); }