From 56fb0bc9f9e2557e259e4d0cdc996016a6ba23f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jindra=20Pet=C5=99=C3=ADk?= Date: Sun, 1 Oct 2023 17:31:25 +0200 Subject: [PATCH] Unicode escape nonenglish characters in namespaces in P-code --- .../decompiler/flash/abc/types/Multiname.java | 4 +- .../src/com/jpexs/helpers/Helper.java | 82 +++++++++++++++---- 2 files changed, 70 insertions(+), 16 deletions(-) diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/types/Multiname.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/types/Multiname.java index 72d3d9dcc..3dfa95972 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/types/Multiname.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/types/Multiname.java @@ -269,7 +269,9 @@ public class Multiname { break; } } - return constants.getNamespace(index).getKindStr() + "(" + (name == null ? "null" : "\"" + Helper.escapePCodeString(name) + "\"") + (sub > 0 ? ",\"" + sub + "\"" : "") + ")"; + return constants.getNamespace(index).getKindStr() + "(" + (name == null ? "null" : "\"" + + Helper.escapePCodeEnglishString(name) //Note: Versioned ABC files have high unicode characters in namespaces, escape them + + "\"") + (sub > 0 ? ",\"" + sub + "\"" : "") + ")"; } public static String namespaceSetToString(AVM2ConstantPool constants, int index) { diff --git a/libsrc/ffdec_lib/src/com/jpexs/helpers/Helper.java b/libsrc/ffdec_lib/src/com/jpexs/helpers/Helper.java index a89437996..9d7d613bc 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/helpers/Helper.java +++ b/libsrc/ffdec_lib/src/com/jpexs/helpers/Helper.java @@ -243,7 +243,7 @@ public class Helper { public static String joinEscapePCodeString(String glue, Collection collection) { StringBuilder sb = new StringBuilder(); - for (String s:collection) { + for (String s : collection) { if (sb.length() > 0) { sb.append(glue); } @@ -251,7 +251,7 @@ public class Helper { } return sb.toString(); } - + /** * Escapes string by adding backslashes * @@ -300,6 +300,59 @@ public class Helper { return ret.toString(); } + /** + * Escapes string by adding backslashes - limits to english characters - + * other are unicode escaped. + * + * @param s String to escape + * @return Escaped string + */ + public static String escapePCodeEnglishString(String s) { + StringBuilder ret = new StringBuilder(s.length()); + for (int i = 0; i < s.length(); i++) { + char c = s.charAt(i); + if (c == '\n') { + ret.append("\\n"); + } else if (c == '\r') { + ret.append("\\r"); + } else if (c == '\t') { + ret.append("\\t"); + } else if (c == '\b') { + ret.append("\\b"); + } else if (c == '\f') { + ret.append("\\f"); + } else if (c == '\\') { + ret.append("\\\\"); + } else if (c == '"') { + ret.append("\\\""); + } else if (c == '\'') { + ret.append("\\'"); + } else if (c < 32) { + ret.append("\\x").append(byteToHex((byte) c)); + } else { + int num = 1; + for (int j = i + 1; j < s.length(); j++) { + if (s.charAt(j) == c) { + num++; + } else { + break; + } + } + if (num > Configuration.limitSameChars.get()) { + ret.append("\\{").append(num).append("}"); + i += num - 1; + } + if (c >= 128) { + ret.append(String.format("\\u%04x", (int) c)); + } else { + ret.append(c); + } + } + } + + return ret.toString(); + } + /** * Escapes string by adding backslashes * @@ -659,12 +712,12 @@ public class Helper { public static E deepCopy(E o) { try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); - try ( ObjectOutputStream oos = new ObjectOutputStream(baos)) { + try (ObjectOutputStream oos = new ObjectOutputStream(baos)) { oos.writeObject(o); oos.flush(); } E copy; - try ( ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()))) { + try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()))) { copy = (E) ois.readObject(); } return copy; @@ -674,7 +727,7 @@ public class Helper { } catch (StackOverflowError se) { throw new StackOverflowError("Stack overflow in deepcopy"); } - + } public static List toList(Object... rest) { @@ -708,7 +761,7 @@ public class Helper { public static byte[] readFile(String... file) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); for (String f : file) { - try ( FileInputStream fis = new FileInputStream(f)) { + try (FileInputStream fis = new FileInputStream(f)) { byte[] buf = new byte[4096]; int cnt; while ((cnt = fis.read(buf)) > 0) { @@ -812,7 +865,7 @@ public class Helper { } public static void appendFile(String file, byte[]... data) { - try ( FileOutputStream fos = new FileOutputStream(file, true)) { + try (FileOutputStream fos = new FileOutputStream(file, true)) { for (byte[] d : data) { fos.write(d); } @@ -822,7 +875,7 @@ public class Helper { } public static void writeFile(String file, byte[]... data) { - try ( FileOutputStream fos = new FileOutputStream(file)) { + try (FileOutputStream fos = new FileOutputStream(file)) { for (byte[] d : data) { fos.write(d); } @@ -832,7 +885,7 @@ public class Helper { } public static void writeFile(String file, InputStream stream) { - try ( FileOutputStream fos = new FileOutputStream(file)) { + try (FileOutputStream fos = new FileOutputStream(file)) { copyStream(stream, fos); } catch (IOException ex) { // ignore @@ -1181,7 +1234,7 @@ public class Helper { public static void saveStream(InputStream is, File output) throws IOException { byte[] buf = new byte[4096]; int cnt; - try ( OutputStream fos = new BufferedOutputStream(new FileOutputStream(output))) { + try (OutputStream fos = new BufferedOutputStream(new FileOutputStream(output))) { while ((cnt = is.read(buf)) > 0) { fos.write(buf, 0, cnt); fos.flush(); @@ -1255,8 +1308,7 @@ public class Helper { return false; } - - + public static String escapeXmlExportString(String s) { StringBuilder ret = new StringBuilder(s.length()); for (int i = 0; i < s.length(); i++) { @@ -1275,7 +1327,7 @@ public class Helper { ret.append("\\\\"); } else if (c < 32) { ret.append("\\u00").append(byteToHex((byte) c)); - } else if (!isCharacterValidInXml(c)){ + } else if (!isCharacterValidInXml(c)) { ret.append("\\u").append(String.format("%04x", (int) c)); } else { ret.append(c); @@ -1284,7 +1336,7 @@ public class Helper { return ret.toString(); } - + public static String unescapeXmlExportString(String st) { StringBuilder sb = new StringBuilder(st.length()); @@ -1702,7 +1754,7 @@ public class Helper { int dot = version.indexOf("."); if (dot != -1) { version = version.substring(0, dot); - } + } return Integer.parseInt(version); } }