Example #1
0
        protected StatementList ReadBytecode()
        {
            var s          = new MemoryStream(_bytecode);
            var reader     = new BinaryReader(s);
            var bcReader   = new BytecodeReader(_self.FileRef, reader);
            var statements = new StatementList((Statement)null);

            while (s.Position < s.Length)
            {
                int startOffset = (int)s.Position;
                //Debug.WriteLine("Reading token at 0x" + startOffset.ToString("X4"));

                BytecodeToken bc;
                try
                {
                    bc = bcReader.ReadNext();
                }
                catch (Exception)
                {
                    //extra bytes at the end can trigger this. They are not used. may be something like byte aligning or something
                    break;
                }
                if (bc == null /*|| bc is EndOfScriptToken*/)
                {
                    break;
                }
                statements.Add(new Statement(startOffset, (int)s.Position, bc, bcReader));
                if (bc is ErrorBytecodeToken)
                {
                    var errorToken = (ErrorBytecodeToken)bc;
                    int bytecode   = errorToken.UnknownBytecode;
                    if (bytecode >= 0)
                    {
                        ProblemRegistry.RegisterUnknownBytecode((byte)bytecode, this, errorToken.SubsequentBytes);
                    }
                    else
                    {
                        ProblemRegistry.RegisterBytecodeError(this, errorToken.ToString());
                    }
                    break;
                }
            }
            return(statements);
        }
Example #2
0
        public void DecompileBytecode(StatementList statements, TextBuilder result, bool createControlStatements)
        {
            var labelTableStatement = statements.Find(s => s.Token is LabelTableToken);
            var labelTable          = labelTableStatement != null ? (LabelTableToken)labelTableStatement.Token : null;

            result.HasErrors = statements.HasErrors();
            if (createControlStatements)
            {
                try
                {
                    statements.CreateControlStatements();
                }
                catch (Exception)
                {
                    ProblemRegistry.RegisterIncompleteControlFlow(this);
                }
                if (statements.IsIncompleteControlFlow())
                {
                    ProblemRegistry.RegisterIncompleteControlFlow(this);
                }
                statements.RemoveRedundantReturns();
            }
            statements.Print(result, labelTable, !createControlStatements);
        }