diff --git a/CHANGELOG.md b/CHANGELOG.md index 088818f83..3c581381b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -68,6 +68,7 @@ All notable changes to this project will be documented in this file. - [#2510] AS direct editation - popped value - [#2511] Allowing to search or to jump from search window while in editation mode - [#2515] AS1/2 direct editation - getURL incorrect casing, generating ActionGetURL2 when not needed +- AS1/2 actions must use SWF charset when writing its length ### Changed - Icon of "Deobfuscation options" menu from pile of pills to medkit diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/Action.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/Action.java index 7e70312f8..0a0927b4d 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/Action.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/Action.java @@ -145,7 +145,7 @@ public abstract class Action implements GraphSourceItem { /** * Charset - SWFs version 5 and lower do not use UTF-8 */ - private String charset; + protected String charset; /** * Names of ActionScript properties @@ -538,7 +538,7 @@ public abstract class Action implements GraphSourceItem { ActionConstantPool cPool = (ActionConstantPool) action; List constantPool = constantPools.get(poolIdx); - int size = ActionConstantPool.calculateSize(constantPool); + int size = ActionConstantPool.calculateSize(constantPool, action.getCharset()); if (size > 0xffff && tryInline) { for (int i = 0; i < constantPool.size(); i++) { int refCount = actions.getConstantPoolIndexReferenceCount(i); @@ -548,7 +548,7 @@ public abstract class Action implements GraphSourceItem { } } - size = ActionConstantPool.calculateSize(constantPool); + size = ActionConstantPool.calculateSize(constantPool, action.getCharset()); } if (size > 0xffff) { diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/parser/script/ActionScript2Parser.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/parser/script/ActionScript2Parser.java index efeb8ddd0..0e2a09eda 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/parser/script/ActionScript2Parser.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/parser/script/ActionScript2Parser.java @@ -2204,7 +2204,7 @@ public class ActionScript2Parser { int index = constantPool.indexOf(s); if (index == -1) { - if (ActionConstantPool.calculateSize(constantPool) + ActionConstantPool.calculateSize(s) <= 0xffff) { + if (ActionConstantPool.calculateSize(constantPool, charset) + ActionConstantPool.calculateSize(s, charset) <= 0xffff) { // constant pool is not full constantPool.add(s); index = constantPool.indexOf(s); diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf1/ActionGetURL.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf1/ActionGetURL.java index ccc9e4a1e..9410e1ada 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf1/ActionGetURL.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf1/ActionGetURL.java @@ -121,7 +121,7 @@ public class ActionGetURL extends Action { */ @Override protected int getContentBytesLength() { - return Utf8Helper.getBytesLength(urlString) + Utf8Helper.getBytesLength(targetString) + 2; + return Utf8Helper.getBytesLength(urlString, charset) + Utf8Helper.getBytesLength(targetString, charset) + 2; } @Override diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf3/ActionGoToLabel.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf3/ActionGoToLabel.java index 9f9f42ace..7c73845e1 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf3/ActionGoToLabel.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf3/ActionGoToLabel.java @@ -98,7 +98,7 @@ public class ActionGoToLabel extends Action { */ @Override protected int getContentBytesLength() { - return Utf8Helper.getBytesLength(label) + 1; + return Utf8Helper.getBytesLength(label, charset) + 1; } /** diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf3/ActionSetTarget.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf3/ActionSetTarget.java index 281fbec54..313bb7c8d 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf3/ActionSetTarget.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf3/ActionSetTarget.java @@ -104,7 +104,7 @@ public class ActionSetTarget extends Action { */ @Override protected int getContentBytesLength() { - return Utf8Helper.getBytesLength(targetName) + 1; + return Utf8Helper.getBytesLength(targetName, charset) + 1; } /** diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf4/ActionPush.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf4/ActionPush.java index ac10b4221..cd73882dd 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf4/ActionPush.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf4/ActionPush.java @@ -216,7 +216,7 @@ public class ActionPush extends Action { int res = 0; for (Object o : vals) { if (o instanceof String) { - res += Utf8Helper.getBytesLength((String) o) + 2; + res += Utf8Helper.getBytesLength((String) o, charset) + 2; } else if (o instanceof Float) { res += 5; } else if (o == Null.INSTANCE) { diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf5/ActionConstantPool.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf5/ActionConstantPool.java index b4630295d..56e5e0e02 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf5/ActionConstantPool.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf5/ActionConstantPool.java @@ -125,29 +125,31 @@ public class ActionConstantPool extends Action { */ @Override protected int getContentBytesLength() { - return calculateSize(constantPool); + return calculateSize(constantPool, charset); } /** * Calculates size of string converted to bytes * * @param str String + * @param charset Charset * @return Size */ - public static int calculateSize(String str) { - return Utf8Helper.getBytesLength(str) + 1; + public static int calculateSize(String str, String charset) { + return Utf8Helper.getBytesLength(str, charset) + 1; } /** * Calculates the size of the action converted to bytes * * @param strings Strings + * @param charset Charset * @return Size */ - public static int calculateSize(List strings) { + public static int calculateSize(List strings, String charset) { int res = 2; for (String s : strings) { - res += Utf8Helper.getBytesLength(s) + 1; + res += Utf8Helper.getBytesLength(s, charset) + 1; } return res; diff --git a/libsrc/ffdec_lib/src/com/jpexs/helpers/utf8/Utf8Helper.java b/libsrc/ffdec_lib/src/com/jpexs/helpers/utf8/Utf8Helper.java index 968c32b6b..31fce0b4e 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/helpers/utf8/Utf8Helper.java +++ b/libsrc/ffdec_lib/src/com/jpexs/helpers/utf8/Utf8Helper.java @@ -152,6 +152,17 @@ public class Utf8Helper { // todo: make it faster without actually writing it to an array return getBytes(string).length; } + + public static int getBytesLength(String string, String charset) { + if (charset.toLowerCase().equals(charsetName)) { + return getBytesLength(string); + } + try { + return string.getBytes(charset).length; + } catch (UnsupportedEncodingException ex) { + return 0; //Should not happen + } + } private static String escapeInvalidUtf8Char(int v) { //Note: for writing the string "{invalid_utf8=xxx}" itself, you can escape it with "{+invalid_utf8=xxx}"