Exemple #1
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sourceStream"></param>
        /// <param name="sourceVersion"></param>
        /// <returns></returns>
        public static AVM1.AbstractAction Create(BinaryReader sourceStream, byte sourceVersion)
        {
            AbstractAction product = null;

            byte firstByte = sourceStream.ReadByte();
            sourceStream.BaseStream.Seek(-1, SeekOrigin.Current);

            //
            // Obtain the name of the action
            //
            string actionName = ActionName((AVM1Actions)firstByte);

            if (null != actionName)
            {
                product = (AbstractAction)MethodBase.GetCurrentMethod().DeclaringType.Assembly.CreateInstance("Recurity.Swf.AVM1." + actionName);
            }
            else
            {
                AVM1ExceptionByteCodeFormat e = new AVM1ExceptionByteCodeFormat("Illegal ActionCode 0x" + firstByte.ToString("X"));
                Log.Error(System.Reflection.MethodBase.GetCurrentMethod(), e);
                throw e;
            }

            product.Read(sourceStream, sourceVersion);

            return product;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="output"></param>
        public void Write( Stream output )
        {
            long pos = output.Position;

            BinaryWriter bw = new BinaryWriter( output );

            bw.Write( _actionCode );

            //
            // When the MSB of the action is set, a length field MUST follow
            if ( 0 != ( _actionCode & 0x80 ) )
            {
                //
                // fancy action, let it do the rendering into a MemoryStream
                //
                using ( MemoryStream ms = new MemoryStream() )
                {
                    BinaryWriter mbw = new BinaryWriter( ms );
                    ulong innerLength = Render( mbw );

                    if ( innerLength > 0xFFFF )
                    {
                        AVM1ExceptionByteCodeFormat e = new AVM1ExceptionByteCodeFormat( "Rendered action is larger than 0xFFFF bytes" );
                       Log.Error(this,  e );
                        throw e;
                    }

                    UInt16 writeLength = ( UInt16 )innerLength;
                    bw.Write( writeLength );

                    //
                    // Yes, this check makes sense. See ActionCall.
                    //
                    if ( innerLength > 0 )
                    {
                        ms.WriteTo( output );
                    }

                    if ( innerLength != _length )
                    {
                        AVM1ExceptionByteCodeFormat e = new AVM1ExceptionByteCodeFormat( "Rendered action size differs from _length (" + innerLength.ToString( "d" ) + " vs. " + _length.ToString( "d" ) );
                       Log.Error(this,  e );
                        throw e;
                    }
                }
            }

               //Log.Debug(this,  this.ToString() + " (" + ( output.Position - pos ).ToString("d") + " bytes)" );
        }