diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/SWFInputStream.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/SWFInputStream.java index 9e2877a39..34b29b85d 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/SWFInputStream.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/SWFInputStream.java @@ -302,7 +302,7 @@ public class SWFInputStream implements AutoCloseable { private static final Logger logger = Logger.getLogger(SWFInputStream.class.getName()); - private static final byte[] BYTE_ARRAY_EMPTY = new byte[0]; + public static final byte[] BYTE_ARRAY_EMPTY = new byte[0]; private final List listeners = new ArrayList<>(); diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/ABCInputStream.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/ABCInputStream.java index e154022f5..071c67736 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/ABCInputStream.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/ABCInputStream.java @@ -17,6 +17,7 @@ package com.jpexs.decompiler.flash.abc; import com.jpexs.decompiler.flash.EndOfStreamException; +import com.jpexs.decompiler.flash.SWFInputStream; import com.jpexs.decompiler.flash.abc.types.Decimal; import com.jpexs.decompiler.flash.abc.types.InstanceInfo; import com.jpexs.decompiler.flash.abc.types.MethodInfo; @@ -63,7 +64,7 @@ public class ABCInputStream implements AutoCloseable { public byte[] stopBuffer() { if (bufferOs == null) { - return new byte[0]; + return SWFInputStream.BYTE_ARRAY_EMPTY; } byte[] ret = bufferOs.toByteArray(); bufferOs.reset(); diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/parser/script/AVM2SourceGenerator.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/parser/script/AVM2SourceGenerator.java index 21bdce713..1b5dfc29e 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/parser/script/AVM2SourceGenerator.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/parser/script/AVM2SourceGenerator.java @@ -16,6 +16,7 @@ */ package com.jpexs.decompiler.flash.abc.avm2.parser.script; +import com.jpexs.decompiler.flash.SWFInputStream; import com.jpexs.decompiler.flash.SourceGeneratorLocalData; import com.jpexs.decompiler.flash.abc.ABC; import com.jpexs.decompiler.flash.abc.avm2.AVM2Code; @@ -218,7 +219,7 @@ public class AVM2SourceGenerator implements SourceGenerator { } catch (IOException ex) { Logger.getLogger(AVM2SourceGenerator.class.getName()).log(Level.SEVERE, null, ex); } - return new byte[0]; + return SWFInputStream.BYTE_ARRAY_EMPTY; } @Override diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/parser/script/ExceptionMarkAVM2Instruction.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/parser/script/ExceptionMarkAVM2Instruction.java index a592f9160..6a19fb00c 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/parser/script/ExceptionMarkAVM2Instruction.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/avm2/parser/script/ExceptionMarkAVM2Instruction.java @@ -16,6 +16,7 @@ */ package com.jpexs.decompiler.flash.abc.avm2.parser.script; +import com.jpexs.decompiler.flash.SWFInputStream; import com.jpexs.decompiler.flash.abc.avm2.instructions.AVM2Instruction; import com.jpexs.decompiler.flash.abc.avm2.instructions.InstructionDefinition; @@ -38,7 +39,7 @@ public class ExceptionMarkAVM2Instruction extends AVM2Instruction { @Override public byte[] getBytes() { - return new byte[0]; + return SWFInputStream.BYTE_ARRAY_EMPTY; } @Override diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/types/MethodBody.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/types/MethodBody.java index b3afc43b3..527b8c7ef 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/types/MethodBody.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/abc/types/MethodBody.java @@ -16,6 +16,7 @@ */ package com.jpexs.decompiler.flash.abc.types; +import com.jpexs.decompiler.flash.SWFInputStream; import com.jpexs.decompiler.flash.abc.ABC; import com.jpexs.decompiler.flash.abc.ABCInputStream; import com.jpexs.decompiler.flash.abc.avm2.AVM2Code; @@ -92,7 +93,7 @@ public final class MethodBody implements Cloneable { public MethodBody(ABC abc) { this.traits = new Traits(); - this.codeBytes = new byte[0]; + this.codeBytes = SWFInputStream.BYTE_ARRAY_EMPTY; this.exceptions = new ABCException[0]; this.abc = abc; } 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 d5878e724..4d6320b8c 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 @@ -195,7 +195,7 @@ public abstract class Action implements GraphSourceItem { } public int getTotalActionLength() { - return actionLength + 1 + ((actionCode >= 0x80) ? 2 : 0); + return actionLength + 1 + (actionCode >= 0x80 ? 2 : 0); } /** @@ -301,8 +301,19 @@ public abstract class Action implements GraphSourceItem { * @param version SWF version * @return Array of bytes */ - public byte[] getBytes(int version) { - return surroundWithAction(new byte[0], version); + public final byte[] getBytes(int version) { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + SWFOutputStream sos = new SWFOutputStream(baos, version); + try { + getContentBytes(sos); + sos.close(); + } catch (IOException e) { + throw new Error("This should never happen.", e); + } + return surroundWithAction(baos.toByteArray(), version); + } + + protected void getContentBytes(SWFOutputStream sos) throws IOException { } /** @@ -311,18 +322,27 @@ public abstract class Action implements GraphSourceItem { * @param version SWF version * @return Length */ - public int getBytesLength(int version) { - return getBytes(version).length; + public final int getBytesLength(int version) { + int contentLength = getContentBytesLength(); + if (contentLength == -1) { + return getBytes(version).length; + } + + return contentLength + 1 + (actionCode >= 0x80 ? 2 : 0); + } + + protected int getContentBytesLength() { + return 0; } /** - * Uptates the action length to the length calculated from action bytes + * Updates the action length to the length calculated from action bytes * * @param version SWF version */ public void updateLength(int version) { - int length = getBytes(version).length; - actionLength = length - 1 - ((actionCode >= 0x80) ? 2 : 0); + int length = getBytesLength(version); + actionLength = length - 1 - (actionCode >= 0x80 ? 2 : 0); } /** @@ -332,7 +352,7 @@ public abstract class Action implements GraphSourceItem { * @param version SWF version * @return Byte array */ - protected byte[] surroundWithAction(byte[] data, int version) { + private byte[] surroundWithAction(byte[] data, int version) { ByteArrayOutputStream baos2 = new ByteArrayOutputStream(); SWFOutputStream sos2 = new SWFOutputStream(baos2, version); try { @@ -942,7 +962,17 @@ public abstract class Action implements GraphSourceItem { } List out; try { - out = ActionGraph.translateViaGraph(cnt.getRegNames(), variables2, functions, actions.subList(adr2ip(actions, endAddr), adr2ip(actions, endAddr + size)), version, staticOperation, path + (cntName == null ? "" : "/" + cntName)); + try { + out = ActionGraph.translateViaGraph(cnt.getRegNames(), variables2, functions, actions.subList(adr2ip(actions, endAddr), adr2ip(actions, endAddr + size)), version, staticOperation, path + (cntName == null ? "" : "/" + cntName)); + } catch (InterruptedException ex) { + throw ex; + } catch (Exception ex) { + boolean inter = Thread.currentThread().isInterrupted(); + ActionList a = new ActionList(actions); + String b = a.toString(); + String c = b; + throw ex; + } } catch (OutOfMemoryError | TranslateException | StackOverflowError ex2) { logger.log(Level.SEVERE, "Decompilation error in: " + path, ex2); if (ex2 instanceof OutOfMemoryError) { diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/ActionGraphSource.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/ActionGraphSource.java index 0c124a16b..88de6e40e 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/ActionGraphSource.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/ActionGraphSource.java @@ -78,7 +78,7 @@ public class ActionGraphSource extends GraphSource { @Override public List translatePart(GraphPart part, BaseLocalData localData, TranslateStack stack, int start, int end, int staticOperation, String path) throws InterruptedException { - return (Action.actionsPartToTree(registerNames, variables, functions, stack, actions, start, end, version, staticOperation, path)); + return Action.actionsPartToTree(registerNames, variables, functions, stack, actions, start, end, version, staticOperation, path); } private List posCache = null; diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/flashlite/ActionStrictMode.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/flashlite/ActionStrictMode.java index 8248c4729..ac2aa636a 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/flashlite/ActionStrictMode.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/flashlite/ActionStrictMode.java @@ -1,18 +1,19 @@ /* * Copyright (C) 2010-2015 JPEXS, All rights reserved. - * + * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 3.0 of the License, or (at your option) any later version. - * + * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. - * + * * You should have received a copy of the GNU Lesser General Public - * License along with this library. */ + * License along with this library. + */ package com.jpexs.decompiler.flash.action.flashlite; import com.jpexs.decompiler.flash.SWFInputStream; @@ -23,7 +24,6 @@ import com.jpexs.decompiler.flash.action.parser.ActionParseException; import com.jpexs.decompiler.flash.action.parser.pcode.FlasmLexer; import com.jpexs.decompiler.graph.GraphTargetItem; import com.jpexs.decompiler.graph.TranslateStack; -import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.HashMap; import java.util.List; @@ -43,16 +43,18 @@ public class ActionStrictMode extends Action { } @Override - public byte[] getBytes(int version) { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - SWFOutputStream sos = new SWFOutputStream(baos, version); - try { - sos.writeUI8(mode); - sos.close(); - } catch (IOException e) { - throw new Error("This should never happen.", e); - } - return surroundWithAction(baos.toByteArray(), version); + protected void getContentBytes(SWFOutputStream sos) throws IOException { + sos.writeUI8(mode); + } + + /** + * Gets the length of action converted to bytes + * + * @return Length + */ + @Override + protected int getContentBytesLength() { + return 1; } @Override diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf3/ActionGetURL.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf3/ActionGetURL.java index 9a3db56a1..dc32ad192 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf3/ActionGetURL.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf3/ActionGetURL.java @@ -30,7 +30,7 @@ import com.jpexs.decompiler.flash.action.parser.pcode.FlasmLexer; import com.jpexs.decompiler.graph.GraphTargetItem; import com.jpexs.decompiler.graph.TranslateStack; import com.jpexs.helpers.Helper; -import java.io.ByteArrayOutputStream; +import com.jpexs.helpers.utf8.Utf8Helper; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; @@ -63,17 +63,19 @@ public class ActionGetURL extends Action { } @Override - public byte[] getBytes(int version) { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - SWFOutputStream sos = new SWFOutputStream(baos, version); - try { - sos.writeString(urlString); - sos.writeString(targetString); - sos.close(); - } catch (IOException e) { - throw new Error("This should never happen.", e); - } - return surroundWithAction(baos.toByteArray(), version); + protected void getContentBytes(SWFOutputStream sos) throws IOException { + sos.writeString(urlString); + sos.writeString(targetString); + } + + /** + * Gets the length of action converted to bytes + * + * @return Length + */ + @Override + protected int getContentBytesLength() { + return Utf8Helper.getBytesLength(urlString) + Utf8Helper.getBytesLength(targetString) + 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 5a788489b..1e9808715 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 @@ -25,7 +25,7 @@ import com.jpexs.decompiler.flash.action.parser.pcode.FlasmLexer; import com.jpexs.decompiler.graph.GraphTargetItem; import com.jpexs.decompiler.graph.TranslateStack; import com.jpexs.helpers.Helper; -import java.io.ByteArrayOutputStream; +import com.jpexs.helpers.utf8.Utf8Helper; import java.io.IOException; import java.util.HashMap; import java.util.List; @@ -52,16 +52,18 @@ public class ActionGoToLabel extends Action { } @Override - public byte[] getBytes(int version) { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - SWFOutputStream sos = new SWFOutputStream(baos, version); - try { - sos.writeString(label); - sos.close(); - } catch (IOException e) { - throw new Error("This should never happen.", e); - } - return surroundWithAction(baos.toByteArray(), version); + protected void getContentBytes(SWFOutputStream sos) throws IOException { + sos.writeString(label); + } + + /** + * Gets the length of action converted to bytes + * + * @return Length + */ + @Override + protected int getContentBytesLength() { + return Utf8Helper.getBytesLength(label) + 1; } public ActionGoToLabel(FlasmLexer lexer) throws IOException, ActionParseException { diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf3/ActionGotoFrame.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf3/ActionGotoFrame.java index 57876b701..a63f49b3b 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf3/ActionGotoFrame.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf3/ActionGotoFrame.java @@ -1,18 +1,19 @@ /* * Copyright (C) 2010-2015 JPEXS, All rights reserved. - * + * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 3.0 of the License, or (at your option) any later version. - * + * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. - * + * * You should have received a copy of the GNU Lesser General Public - * License along with this library. */ + * License along with this library. + */ package com.jpexs.decompiler.flash.action.swf3; import com.jpexs.decompiler.flash.SWFInputStream; @@ -23,7 +24,6 @@ import com.jpexs.decompiler.flash.action.parser.ActionParseException; import com.jpexs.decompiler.flash.action.parser.pcode.FlasmLexer; import com.jpexs.decompiler.graph.GraphTargetItem; import com.jpexs.decompiler.graph.TranslateStack; -import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.HashMap; import java.util.List; @@ -48,16 +48,18 @@ public class ActionGotoFrame extends Action { } @Override - public byte[] getBytes(int version) { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - SWFOutputStream sos = new SWFOutputStream(baos, version); - try { - sos.writeUI16(frame); - sos.close(); - } catch (IOException e) { - throw new Error("This should never happen.", e); - } - return surroundWithAction(baos.toByteArray(), version); + protected void getContentBytes(SWFOutputStream sos) throws IOException { + sos.writeUI16(frame); + } + + /** + * Gets the length of action converted to bytes + * + * @return Length + */ + @Override + protected int getContentBytesLength() { + return 2; } public ActionGotoFrame(FlasmLexer lexer) throws IOException, ActionParseException { 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 eb3abbfde..45991ad25 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 @@ -25,7 +25,7 @@ import com.jpexs.decompiler.flash.action.parser.pcode.FlasmLexer; import com.jpexs.decompiler.graph.GraphTargetItem; import com.jpexs.decompiler.graph.TranslateStack; import com.jpexs.helpers.Helper; -import java.io.ByteArrayOutputStream; +import com.jpexs.helpers.utf8.Utf8Helper; import java.io.IOException; import java.util.HashMap; import java.util.List; @@ -52,16 +52,18 @@ public class ActionSetTarget extends Action { } @Override - public byte[] getBytes(int version) { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - SWFOutputStream sos = new SWFOutputStream(baos, version); - try { - sos.writeString(targetName); - sos.close(); - } catch (IOException e) { - throw new Error("This should never happen.", e); - } - return surroundWithAction(baos.toByteArray(), version); + protected void getContentBytes(SWFOutputStream sos) throws IOException { + sos.writeString(targetName); + } + + /** + * Gets the length of action converted to bytes + * + * @return Length + */ + @Override + protected int getContentBytesLength() { + return Utf8Helper.getBytesLength(targetName) + 1; } public ActionSetTarget(FlasmLexer lexer) throws IOException, ActionParseException { diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf3/ActionWaitForFrame.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf3/ActionWaitForFrame.java index 3d7a51039..54ec7074b 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf3/ActionWaitForFrame.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf3/ActionWaitForFrame.java @@ -30,7 +30,6 @@ import com.jpexs.decompiler.flash.action.special.ActionStore; import com.jpexs.decompiler.flash.exporters.modes.ScriptExportMode; import com.jpexs.decompiler.graph.GraphTargetItem; import com.jpexs.decompiler.graph.TranslateStack; -import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; @@ -64,17 +63,19 @@ public class ActionWaitForFrame extends Action implements ActionStore { } @Override - public byte[] getBytes(int version) { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - SWFOutputStream sos = new SWFOutputStream(baos, version); - try { - sos.writeUI16(frame); - sos.writeUI8(skipCount); - sos.close(); - } catch (IOException e) { - throw new Error("This should never happen.", e); - } - return surroundWithAction(baos.toByteArray(), version); + protected void getContentBytes(SWFOutputStream sos) throws IOException { + sos.writeUI16(frame); + sos.writeUI8(skipCount); + } + + /** + * Gets the length of action converted to bytes + * + * @return Length + */ + @Override + protected int getContentBytesLength() { + return 3; } public ActionWaitForFrame(FlasmLexer lexer) throws IOException, ActionParseException { diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf4/ActionGetURL2.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf4/ActionGetURL2.java index 9cd4f5721..55d96db47 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf4/ActionGetURL2.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf4/ActionGetURL2.java @@ -36,7 +36,6 @@ import com.jpexs.decompiler.flash.action.parser.pcode.FlasmLexer; import com.jpexs.decompiler.flash.types.annotations.Reserved; import com.jpexs.decompiler.graph.GraphTargetItem; import com.jpexs.decompiler.graph.TranslateStack; -import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; @@ -78,19 +77,21 @@ public class ActionGetURL2 extends Action { } @Override - public byte[] getBytes(int version) { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - SWFOutputStream sos = new SWFOutputStream(baos, version); - try { - sos.writeUB(1, loadVariablesFlag ? 1 : 0); - sos.writeUB(1, loadTargetFlag ? 1 : 0); - sos.writeUB(4, reserved); - sos.writeUB(2, sendVarsMethod); - sos.close(); - } catch (IOException e) { - throw new Error("This should never happen.", e); - } - return surroundWithAction(baos.toByteArray(), version); + protected void getContentBytes(SWFOutputStream sos) throws IOException { + sos.writeUB(1, loadVariablesFlag ? 1 : 0); + sos.writeUB(1, loadTargetFlag ? 1 : 0); + sos.writeUB(4, reserved); + sos.writeUB(2, sendVarsMethod); + } + + /** + * Gets the length of action converted to bytes + * + * @return Length + */ + @Override + protected int getContentBytesLength() { + return 1; } public ActionGetURL2(FlasmLexer lexer) throws IOException, ActionParseException { diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf4/ActionGotoFrame2.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf4/ActionGotoFrame2.java index bf8990a92..94314c2a7 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf4/ActionGotoFrame2.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf4/ActionGotoFrame2.java @@ -25,7 +25,6 @@ import com.jpexs.decompiler.flash.action.parser.pcode.FlasmLexer; import com.jpexs.decompiler.flash.types.annotations.Reserved; import com.jpexs.decompiler.graph.GraphTargetItem; import com.jpexs.decompiler.graph.TranslateStack; -import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.HashMap; import java.util.List; @@ -59,21 +58,28 @@ public class ActionGotoFrame2 extends Action { } @Override - public byte[] getBytes(int version) { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - SWFOutputStream sos = new SWFOutputStream(baos, version); - try { - sos.writeUB(6, reserved); - sos.writeUB(1, sceneBiasFlag ? 1 : 0); - sos.writeUB(1, playFlag ? 1 : 0); - if (sceneBiasFlag) { - sos.writeUI16(sceneBias); - } - sos.close(); - } catch (IOException e) { - throw new Error("This should never happen.", e); + protected void getContentBytes(SWFOutputStream sos) throws IOException { + sos.writeUB(6, reserved); + sos.writeUB(1, sceneBiasFlag ? 1 : 0); + sos.writeUB(1, playFlag ? 1 : 0); + if (sceneBiasFlag) { + sos.writeUI16(sceneBias); } - return surroundWithAction(baos.toByteArray(), version); + } + + /** + * Gets the length of action converted to bytes + * + * @return Length + */ + @Override + protected int getContentBytesLength() { + int res = 1; + if (sceneBiasFlag) { + res += 2; + } + + return res; } @Override diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf4/ActionIf.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf4/ActionIf.java index eb4199b61..f9af92b1b 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf4/ActionIf.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf4/ActionIf.java @@ -25,7 +25,6 @@ import com.jpexs.decompiler.flash.action.parser.pcode.FlasmLexer; import com.jpexs.decompiler.flash.exporters.modes.ScriptExportMode; import com.jpexs.decompiler.graph.GraphSource; import com.jpexs.helpers.Helper; -import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.List; import java.util.Set; @@ -66,16 +65,18 @@ public class ActionIf extends Action { } @Override - public byte[] getBytes(int version) { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - SWFOutputStream sos = new SWFOutputStream(baos, version); - try { - sos.writeSI16(offset); - sos.close(); - } catch (IOException e) { - throw new Error("This should never happen.", e); - } - return surroundWithAction(baos.toByteArray(), version); + protected void getContentBytes(SWFOutputStream sos) throws IOException { + sos.writeSI16(offset); + } + + /** + * Gets the length of action converted to bytes + * + * @return Length + */ + @Override + protected int getContentBytesLength() { + return 2; } @Override diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf4/ActionJump.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf4/ActionJump.java index 92bf4e6ed..735a7056b 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf4/ActionJump.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf4/ActionJump.java @@ -26,7 +26,6 @@ import com.jpexs.decompiler.flash.action.parser.pcode.FlasmLexer; import com.jpexs.decompiler.flash.exporters.modes.ScriptExportMode; import com.jpexs.decompiler.graph.GraphSource; import com.jpexs.helpers.Helper; -import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.List; import java.util.Set; @@ -67,16 +66,18 @@ public class ActionJump extends Action { } @Override - public byte[] getBytes(int version) { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - SWFOutputStream sos = new SWFOutputStream(baos, version); - try { - sos.writeSI16(offset); - sos.close(); - } catch (IOException e) { - throw new Error("This should never happen.", e); - } - return surroundWithAction(baos.toByteArray(), version); + protected void getContentBytes(SWFOutputStream sos) throws IOException { + sos.writeSI16(offset); + } + + /** + * Gets the length of action converted to bytes + * + * @return Length + */ + @Override + protected int getContentBytesLength() { + return 2; } @Override 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 40d498120..70c327738 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 @@ -40,7 +40,7 @@ import com.jpexs.decompiler.graph.TranslateStack; import com.jpexs.decompiler.graph.model.FalseItem; import com.jpexs.decompiler.graph.model.TrueItem; import com.jpexs.helpers.Helper; -import java.io.ByteArrayOutputStream; +import com.jpexs.helpers.utf8.Utf8Helper; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; @@ -135,57 +135,95 @@ public class ActionPush extends Action { } @Override - public byte[] getBytes(int version) { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - SWFOutputStream sos = new SWFOutputStream(baos, version); - try { - for (Object o : values) { - if (o instanceof String) { - sos.writeUI8(0); - sos.writeString((String) o); - } else if (o instanceof Float) { - sos.writeUI8(1); - sos.writeFLOAT((Float) o); - } else if (o instanceof Null) { - sos.writeUI8(2); - } else if (o instanceof Undefined) { - sos.writeUI8(3); - } else if (o instanceof RegisterNumber) { - sos.writeUI8(4); - sos.writeUI8(((RegisterNumber) o).number); - } else if (o instanceof Boolean) { - sos.writeUI8(5); - sos.writeUI8((Boolean) o ? 1 : 0); - } else if (o instanceof Double || o instanceof Long) { - if (o instanceof Long) { - long l = (Long) o; - if (l < -0x80000000 || l > 0x7fffffff) { - o = (double) l; - } - } - if (o instanceof Double) { - sos.writeUI8(6); - sos.writeDOUBLE((Double) o); - } else if (o instanceof Long) { - sos.writeUI8(7); - sos.writeSI32((Long) o); - } - } else if (o instanceof ConstantIndex) { - int cIndex = ((ConstantIndex) o).index; - if (cIndex < 256) { - sos.writeUI8(8); - sos.writeUI8(cIndex); - } else { - sos.writeUI8(9); - sos.writeUI16(cIndex); + protected void getContentBytes(SWFOutputStream sos) throws IOException { + for (Object o : values) { + if (o instanceof String) { + sos.writeUI8(0); + sos.writeString((String) o); + } else if (o instanceof Float) { + sos.writeUI8(1); + sos.writeFLOAT((Float) o); + } else if (o instanceof Null) { + sos.writeUI8(2); + } else if (o instanceof Undefined) { + sos.writeUI8(3); + } else if (o instanceof RegisterNumber) { + sos.writeUI8(4); + sos.writeUI8(((RegisterNumber) o).number); + } else if (o instanceof Boolean) { + sos.writeUI8(5); + sos.writeUI8((Boolean) o ? 1 : 0); + } else if (o instanceof Double || o instanceof Long) { + if (o instanceof Long) { + long l = (Long) o; + if (l < -0x80000000 || l > 0x7fffffff) { + o = (double) l; } } + if (o instanceof Double) { + sos.writeUI8(6); + sos.writeDOUBLE((Double) o); + } else if (o instanceof Long) { + sos.writeUI8(7); + sos.writeSI32((Long) o); + } + } else if (o instanceof ConstantIndex) { + int cIndex = ((ConstantIndex) o).index; + if (cIndex < 256) { + sos.writeUI8(8); + sos.writeUI8(cIndex); + } else { + sos.writeUI8(9); + sos.writeUI16(cIndex); + } } - sos.close(); - } catch (IOException e) { - throw new Error("This should never happen.", e); } - return surroundWithAction(baos.toByteArray(), version); + } + + /** + * Gets the length of action converted to bytes + * + * @return Length + */ + @Override + protected int getContentBytesLength() { + int res = 0; + for (Object o : values) { + if (o instanceof String) { + res += Utf8Helper.getBytesLength((String) o) + 2; + } else if (o instanceof Float) { + res += 5; + } else if (o instanceof Null) { + res++; + } else if (o instanceof Undefined) { + res++; + } else if (o instanceof RegisterNumber) { + res += 2; + } else if (o instanceof Boolean) { + res += 2; + } else if (o instanceof Double || o instanceof Long) { + if (o instanceof Long) { + long l = (Long) o; + if (l < -0x80000000 || l > 0x7fffffff) { + o = (double) l; + } + } + if (o instanceof Double) { + res += 9; + } else if (o instanceof Long) { + res += 5; + } + } else if (o instanceof ConstantIndex) { + int cIndex = ((ConstantIndex) o).index; + if (cIndex < 256) { + res += 2; + } else { + res += 3; + } + } + } + + return res; } public static boolean isValidValue(Object value) { diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf4/ActionWaitForFrame2.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf4/ActionWaitForFrame2.java index 59569ca52..eedc9bd2d 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf4/ActionWaitForFrame2.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf4/ActionWaitForFrame2.java @@ -29,7 +29,6 @@ import com.jpexs.decompiler.flash.action.special.ActionStore; import com.jpexs.decompiler.flash.exporters.modes.ScriptExportMode; import com.jpexs.decompiler.graph.GraphTargetItem; import com.jpexs.decompiler.graph.TranslateStack; -import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; @@ -100,16 +99,18 @@ public class ActionWaitForFrame2 extends Action implements ActionStore { } @Override - public byte[] getBytes(int version) { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - SWFOutputStream sos = new SWFOutputStream(baos, version); - try { - sos.writeUI8(skipCount); - sos.close(); - } catch (IOException e) { - throw new Error("This should never happen.", e); - } - return surroundWithAction(baos.toByteArray(), version); + protected void getContentBytes(SWFOutputStream sos) throws IOException { + sos.writeUI8(skipCount); + } + + /** + * Gets the length of action converted to bytes + * + * @return Length + */ + @Override + protected int getContentBytesLength() { + return 1; } @Override 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 e3854d408..fd3f3ed0e 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 @@ -25,7 +25,7 @@ import com.jpexs.decompiler.flash.action.parser.pcode.FlasmLexer; import com.jpexs.decompiler.graph.GraphTargetItem; import com.jpexs.decompiler.graph.TranslateStack; import com.jpexs.helpers.Helper; -import java.io.ByteArrayOutputStream; +import com.jpexs.helpers.utf8.Utf8Helper; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; @@ -63,19 +63,26 @@ public class ActionConstantPool extends Action { } @Override - public byte[] getBytes(int version) { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - SWFOutputStream sos = new SWFOutputStream(baos, version); - try { - sos.writeUI16(constantPool.size()); - for (String s : constantPool) { - sos.writeString(s); - } - sos.close(); - } catch (IOException e) { - throw new Error("This should never happen.", e); + protected void getContentBytes(SWFOutputStream sos) throws IOException { + sos.writeUI16(constantPool.size()); + for (String s : constantPool) { + sos.writeString(s); } - return surroundWithAction(baos.toByteArray(), version); + } + + /** + * Gets the length of action converted to bytes + * + * @return Length + */ + @Override + protected int getContentBytesLength() { + int res = 2; + for (String s : constantPool) { + res += Utf8Helper.getBytesLength(s) + 1; + } + + return res; } @Override diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf5/ActionDefineFunction.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf5/ActionDefineFunction.java index 6943653e7..f74e189d7 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf5/ActionDefineFunction.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf5/ActionDefineFunction.java @@ -29,7 +29,7 @@ import com.jpexs.decompiler.graph.GraphSourceItemContainer; import com.jpexs.decompiler.graph.GraphTargetItem; import com.jpexs.decompiler.graph.TranslateStack; import com.jpexs.helpers.Helper; -import java.io.ByteArrayOutputStream; +import com.jpexs.helpers.utf8.Utf8Helper; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; @@ -84,28 +84,32 @@ public class ActionDefineFunction extends Action implements GraphSourceItemConta @Override public long getHeaderSize() { - return getBytes(version).length; + return getBytesLength(version); } @Override - public byte[] getBytes(int version) { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - SWFOutputStream sos = new SWFOutputStream(baos, version); - ByteArrayOutputStream baos2 = new ByteArrayOutputStream(); - try { - sos.writeString(functionName); - sos.writeUI16(paramNames.size()); - for (String s : paramNames) { - sos.writeString(s); - } - sos.writeUI16(codeSize); - sos.close(); - - baos2.write(surroundWithAction(baos.toByteArray(), version)); - } catch (IOException e) { - throw new Error("This should never happen.", e); + protected void getContentBytes(SWFOutputStream sos) throws IOException { + sos.writeString(functionName); + sos.writeUI16(paramNames.size()); + for (String s : paramNames) { + sos.writeString(s); } - return baos2.toByteArray(); + sos.writeUI16(codeSize); + } + + /** + * Gets the length of action converted to bytes + * + * @return Length + */ + @Override + protected int getContentBytesLength() { + int res = Utf8Helper.getBytesLength(functionName) + 5; + for (String s : paramNames) { + res += Utf8Helper.getBytesLength(s) + 1; + } + + return res; } @Override diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf5/ActionStoreRegister.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf5/ActionStoreRegister.java index 4100767a4..5433078c7 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf5/ActionStoreRegister.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf5/ActionStoreRegister.java @@ -34,7 +34,6 @@ import com.jpexs.decompiler.flash.action.swf4.RegisterNumber; import com.jpexs.decompiler.graph.GraphSourceItemPos; import com.jpexs.decompiler.graph.GraphTargetItem; import com.jpexs.decompiler.graph.TranslateStack; -import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.HashMap; import java.util.List; @@ -59,16 +58,18 @@ public class ActionStoreRegister extends Action implements StoreTypeAction { } @Override - public byte[] getBytes(int version) { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - SWFOutputStream sos = new SWFOutputStream(baos, version); - try { - sos.writeUI8(registerNumber); - sos.close(); - } catch (IOException e) { - throw new Error("This should never happen.", e); - } - return surroundWithAction(baos.toByteArray(), version); + protected void getContentBytes(SWFOutputStream sos) throws IOException { + sos.writeUI8(registerNumber); + } + + /** + * Gets the length of action converted to bytes + * + * @return Length + */ + @Override + protected int getContentBytesLength() { + return 1; } @Override diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf5/ActionWith.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf5/ActionWith.java index 0da5d8aa0..5a910f874 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf5/ActionWith.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf5/ActionWith.java @@ -27,7 +27,6 @@ import com.jpexs.decompiler.flash.exporters.modes.ScriptExportMode; import com.jpexs.decompiler.graph.GraphSourceItemContainer; import com.jpexs.decompiler.graph.GraphTargetItem; import com.jpexs.decompiler.graph.TranslateStack; -import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; @@ -63,18 +62,18 @@ public class ActionWith extends Action implements GraphSourceItemContainer { } @Override - public byte[] getBytes(int version) { - ByteArrayOutputStream baos2 = new ByteArrayOutputStream(); - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - SWFOutputStream sos = new SWFOutputStream(baos, version); - try { - sos.writeUI16(codeSize); - sos.close(); - baos2.write(surroundWithAction(baos.toByteArray(), version)); - } catch (IOException e) { - throw new Error("This should never happen.", e); - } - return baos2.toByteArray(); + protected void getContentBytes(SWFOutputStream sos) throws IOException { + sos.writeUI16(codeSize); + } + + /** + * Gets the length of action converted to bytes + * + * @return Length + */ + @Override + protected int getContentBytesLength() { + return 2; } @Override @@ -110,7 +109,7 @@ public class ActionWith extends Action implements GraphSourceItemContainer { @Override public long getHeaderSize() { - return surroundWithAction(new byte[]{0, 0}, version).length; + return super.getBytesLength(version) + 2; } @Override diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf7/ActionDefineFunction2.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf7/ActionDefineFunction2.java index 3fd1b3d44..0709f24db 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf7/ActionDefineFunction2.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf7/ActionDefineFunction2.java @@ -30,7 +30,7 @@ import com.jpexs.decompiler.graph.GraphSourceItemContainer; import com.jpexs.decompiler.graph.GraphTargetItem; import com.jpexs.decompiler.graph.TranslateStack; import com.jpexs.helpers.Helper; -import java.io.ByteArrayOutputStream; +import com.jpexs.helpers.utf8.Utf8Helper; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; @@ -143,40 +143,44 @@ public class ActionDefineFunction2 extends Action implements GraphSourceItemCont @Override public long getHeaderSize() { - return getBytes(version).length; + return getBytesLength(version); } @Override - public byte[] getBytes(int version) { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - SWFOutputStream sos = new SWFOutputStream(baos, version); - ByteArrayOutputStream baos2 = new ByteArrayOutputStream(); - try { - sos.writeString(functionName); - sos.writeUI16(paramNames.size()); - sos.writeUI8(registerCount); - sos.writeUB(1, preloadParentFlag ? 1 : 0); - sos.writeUB(1, preloadRootFlag ? 1 : 0); - sos.writeUB(1, suppressSuperFlag ? 1 : 0); - sos.writeUB(1, preloadSuperFlag ? 1 : 0); - sos.writeUB(1, suppressArgumentsFlag ? 1 : 0); - sos.writeUB(1, preloadArgumentsFlag ? 1 : 0); - sos.writeUB(1, suppressThisFlag ? 1 : 0); - sos.writeUB(1, preloadThisFlag ? 1 : 0); - sos.writeUB(7, reserved); - sos.writeUB(1, preloadGlobalFlag ? 1 : 0); - for (int i = 0; i < paramNames.size(); i++) { - sos.writeUI8(paramRegisters.get(i)); - sos.writeString(paramNames.get(i)); - } - sos.writeUI16(codeSize); - sos.close(); - - baos2.write(surroundWithAction(baos.toByteArray(), version)); - } catch (IOException e) { - throw new Error("This should never happen.", e); + protected void getContentBytes(SWFOutputStream sos) throws IOException { + sos.writeString(functionName); + sos.writeUI16(paramNames.size()); + sos.writeUI8(registerCount); + sos.writeUB(1, preloadParentFlag ? 1 : 0); + sos.writeUB(1, preloadRootFlag ? 1 : 0); + sos.writeUB(1, suppressSuperFlag ? 1 : 0); + sos.writeUB(1, preloadSuperFlag ? 1 : 0); + sos.writeUB(1, suppressArgumentsFlag ? 1 : 0); + sos.writeUB(1, preloadArgumentsFlag ? 1 : 0); + sos.writeUB(1, suppressThisFlag ? 1 : 0); + sos.writeUB(1, preloadThisFlag ? 1 : 0); + sos.writeUB(7, reserved); + sos.writeUB(1, preloadGlobalFlag ? 1 : 0); + for (int i = 0; i < paramNames.size(); i++) { + sos.writeUI8(paramRegisters.get(i)); + sos.writeString(paramNames.get(i)); } - return baos2.toByteArray(); + sos.writeUI16(codeSize); + } + + /** + * Gets the length of action converted to bytes + * + * @return Length + */ + @Override + protected int getContentBytesLength() { + int res = Utf8Helper.getBytesLength(functionName) + 8; + for (int i = 0; i < paramNames.size(); i++) { + res += Utf8Helper.getBytesLength(paramNames.get(i)) + 2; + } + + return res; } @Override diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf7/ActionTry.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf7/ActionTry.java index ce902c5b0..77b8f62c0 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf7/ActionTry.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/action/swf7/ActionTry.java @@ -33,7 +33,7 @@ import com.jpexs.decompiler.graph.GraphSourceItemContainer; import com.jpexs.decompiler.graph.GraphTargetItem; import com.jpexs.decompiler.graph.TranslateStack; import com.jpexs.helpers.Helper; -import java.io.ByteArrayOutputStream; +import com.jpexs.helpers.utf8.Utf8Helper; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; @@ -95,27 +95,34 @@ public class ActionTry extends Action implements GraphSourceItemContainer { } @Override - public byte[] getBytes(int version) { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - SWFOutputStream sos = new SWFOutputStream(baos, version); - try { - sos.writeUB(5, reserved); - sos.writeUB(1, catchInRegisterFlag ? 1 : 0); - sos.writeUB(1, finallyBlockFlag ? 1 : 0); - sos.writeUB(1, catchBlockFlag ? 1 : 0); - sos.writeUI16((int) trySize); - sos.writeUI16((int) catchSize); - sos.writeUI16((int) finallySize); - if (catchInRegisterFlag) { - sos.writeUI8(catchRegister); - } else { - sos.writeString(catchName == null ? "" : catchName); - } - sos.close(); - } catch (IOException e) { - throw new Error("This should never happen.", e); + protected void getContentBytes(SWFOutputStream sos) throws IOException { + sos.writeUB(5, reserved); + sos.writeUB(1, catchInRegisterFlag ? 1 : 0); + sos.writeUB(1, finallyBlockFlag ? 1 : 0); + sos.writeUB(1, catchBlockFlag ? 1 : 0); + sos.writeUI16((int) trySize); + sos.writeUI16((int) catchSize); + sos.writeUI16((int) finallySize); + if (catchInRegisterFlag) { + sos.writeUI8(catchRegister); + } else { + sos.writeString(catchName == null ? "" : catchName); } - return surroundWithAction(baos.toByteArray(), version); + } + + /** + * Gets the length of action converted to bytes + * + * @return Length + */ + @Override + protected int getContentBytesLength() { + int res = 8; + if (!catchInRegisterFlag) { + res += Utf8Helper.getBytesLength(catchName == null ? "" : catchName); + } + + return res; } public ActionTry(FlasmLexer lexer, int version) throws IOException, ActionParseException { @@ -173,7 +180,7 @@ public class ActionTry extends Action implements GraphSourceItemContainer { @Override public long getHeaderSize() { - return getBytes(version).length; + return getBytesLength(version); } public long getTrySize() { diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/exporters/FontExporter.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/exporters/FontExporter.java index f76a68eca..d14c41969 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/exporters/FontExporter.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/exporters/FontExporter.java @@ -25,6 +25,7 @@ import com.jpexs.decompiler.flash.ApplicationInfo; import com.jpexs.decompiler.flash.EventListener; import com.jpexs.decompiler.flash.RetryTask; import com.jpexs.decompiler.flash.SWF; +import com.jpexs.decompiler.flash.SWFInputStream; import com.jpexs.decompiler.flash.configuration.Configuration; import com.jpexs.decompiler.flash.exporters.modes.FontExportMode; import com.jpexs.decompiler.flash.exporters.settings.FontExportSettings; @@ -122,7 +123,7 @@ public class FontExporter { } catch (IOException ex) { Logger.getLogger(FontExporter.class.getName()).log(Level.SEVERE, null, ex); } - return new byte[0]; + return SWFInputStream.BYTE_ARRAY_EMPTY; } public void exportFont(FontTag ft, FontExportMode mode, File file) throws IOException { diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/exporters/MovieExporter.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/exporters/MovieExporter.java index d8e16cb2c..6483aeb33 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/exporters/MovieExporter.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/exporters/MovieExporter.java @@ -98,7 +98,7 @@ public class MovieExporter { HashMap frames = new HashMap<>(); SWF.populateVideoFrames(videoStream.characterID, swf.tags, frames); if (frames.isEmpty()) { - return new byte[0]; + return SWFInputStream.BYTE_ARRAY_EMPTY; } //double ms = 1000.0f / ((float) frameRate); diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/tags/DefineFont4Tag.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/tags/DefineFont4Tag.java index a8224373e..4c91576f2 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/tags/DefineFont4Tag.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/tags/DefineFont4Tag.java @@ -62,7 +62,7 @@ public class DefineFont4Tag extends CharacterTag { super(swf, ID, NAME, null); fontID = swf.getNextCharacterId(); fontName = "New font"; - fontData = new byte[0]; + fontData = SWFInputStream.BYTE_ARRAY_EMPTY; } public DefineFont4Tag(SWFInputStream sis, ByteArrayRange data) throws IOException { diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/tags/JPEGTablesTag.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/tags/JPEGTablesTag.java index a23b34c61..1517eaab4 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/tags/JPEGTablesTag.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/tags/JPEGTablesTag.java @@ -39,7 +39,7 @@ public class JPEGTablesTag extends Tag { */ public JPEGTablesTag(SWF swf) { super(swf, ID, NAME, null); - jpegData = new byte[0]; + jpegData = SWFInputStream.BYTE_ARRAY_EMPTY; } public JPEGTablesTag(SWFInputStream sis, ByteArrayRange data) throws IOException { diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/types/BUTTONCONDACTION.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/types/BUTTONCONDACTION.java index dfc3ed7df..f31ccfd27 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/types/BUTTONCONDACTION.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/types/BUTTONCONDACTION.java @@ -58,7 +58,7 @@ public class BUTTONCONDACTION implements ASMSource, Serializable { public BUTTONCONDACTION() { swf = null; tag = null; - actionBytes = new ByteArrayRange(new byte[0]); + actionBytes = new ByteArrayRange(SWFInputStream.BYTE_ARRAY_EMPTY); } @Override diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/types/gfx/GFxInputStream.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/types/gfx/GFxInputStream.java index f7b874b94..31b014632 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/types/gfx/GFxInputStream.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/types/gfx/GFxInputStream.java @@ -16,6 +16,7 @@ */ package com.jpexs.decompiler.flash.types.gfx; +import com.jpexs.decompiler.flash.SWFInputStream; import com.jpexs.decompiler.flash.dumpview.DumpInfo; import com.jpexs.helpers.MemoryInputStream; import com.jpexs.helpers.utf8.Utf8Helper; @@ -215,7 +216,7 @@ public class GFxInputStream { */ public byte[] readBytes(long count, String name) throws IOException { if (count <= 0) { - return new byte[0]; + return SWFInputStream.BYTE_ARRAY_EMPTY; } newDumpLevel(name, "bytes"); byte[] ret = new byte[(int) count]; diff --git a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/xfl/XFLConverter.java b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/xfl/XFLConverter.java index 2eb4a2d50..d693fa031 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/xfl/XFLConverter.java +++ b/libsrc/ffdec_lib/src/com/jpexs/decompiler/flash/xfl/XFLConverter.java @@ -1366,7 +1366,7 @@ public class XFLConverter { boolean soundType = false; boolean soundSize = false; long soundSampleCount = 0; - byte[] soundData = new byte[0]; + byte[] soundData = SWFInputStream.BYTE_ARRAY_EMPTY; int[] rateMap = {5, 11, 22, 44}; String exportFormat = "flv"; if (symbol instanceof SoundStreamHeadTypeTag) { @@ -1491,7 +1491,7 @@ public class XFLConverter { } SoundTag st = (SoundTag) symbol; SoundFormat fmt = st.getSoundFormat(); - byte[] data = new byte[0]; + byte[] data = SWFInputStream.BYTE_ARRAY_EMPTY; try { data = new SoundExporter().exportSound(st, SoundExportMode.MP3_WAV); } catch (IOException ex) { @@ -1544,7 +1544,7 @@ public class XFLConverter { break; } - byte[] data = new byte[0]; + byte[] data = SWFInputStream.BYTE_ARRAY_EMPTY; try { data = new MovieExporter().exportMovie(video, MovieExportMode.FLV); } catch (IOException ex) { diff --git a/libsrc/ffdec_lib/src/com/jpexs/helpers/ByteArrayRange.java b/libsrc/ffdec_lib/src/com/jpexs/helpers/ByteArrayRange.java index 9168ba7ae..2682a585e 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/helpers/ByteArrayRange.java +++ b/libsrc/ffdec_lib/src/com/jpexs/helpers/ByteArrayRange.java @@ -16,13 +16,15 @@ */ package com.jpexs.helpers; +import com.jpexs.decompiler.flash.SWFInputStream; + /** * * @author JPEXS */ public class ByteArrayRange { - public static final ByteArrayRange EMPTY = new ByteArrayRange(new byte[0]); + public static final ByteArrayRange EMPTY = new ByteArrayRange(SWFInputStream.BYTE_ARRAY_EMPTY); private final byte[] array; 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 ba938b072..e9691dbe2 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/helpers/utf8/Utf8Helper.java +++ b/libsrc/ffdec_lib/src/com/jpexs/helpers/utf8/Utf8Helper.java @@ -1,18 +1,19 @@ /* * Copyright (C) 2010-2015 JPEXS, All rights reserved. - * + * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 3.0 of the License, or (at your option) any later version. - * + * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. - * + * * You should have received a copy of the GNU Lesser General Public - * License along with this library. */ + * License along with this library. + */ package com.jpexs.helpers.utf8; import java.io.UnsupportedEncodingException; @@ -38,4 +39,9 @@ public class Utf8Helper { public static byte[] getBytes(String string) { return string.getBytes(charset); } + + public static int getBytesLength(String string) { + // todo: make it faster without actually writing it to an array + return string.getBytes(charset).length; + } } diff --git a/src/com/jpexs/decompiler/flash/gui/action/ActionPanel.java b/src/com/jpexs/decompiler/flash/gui/action/ActionPanel.java index 5f25d66ff..7dd06a435 100644 --- a/src/com/jpexs/decompiler/flash/gui/action/ActionPanel.java +++ b/src/com/jpexs/decompiler/flash/gui/action/ActionPanel.java @@ -52,6 +52,7 @@ import com.jpexs.decompiler.flash.tags.base.ASMSource; import com.jpexs.decompiler.graph.CompilationException; import com.jpexs.helpers.CancellableWorker; import com.jpexs.helpers.Helper; +import com.jpexs.helpers.Stopwatch; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.FlowLayout; @@ -423,7 +424,9 @@ public class ActionPanel extends JPanel implements SearchListener