The SwfReader class reads and parses a swf file from a stream.

The SwfReader creates a Swf object, that is a sequence of tags readed from a swf binary file. The tag sequences follows the Swf version 7 format specifications from Macromedia, and is compatible with Swf version 1 to 7.

The working process of the tags sequence is explain in the Macromedia specifications. The tags sequence is composed of objects from the SwfDotNet.IO.Tags namespace.

SwfReader gets all the content of a file and provides some functionnalities to decompile bitmap, sounds, video or scripting bytecode.

The tag reading process doesnt decompile actionscript bytecode, though. This is handled by SwfDotNet.IO.ByteCode.Decompiler. The action script tags contain directly actionscript bytecodes as a byte array. The SwfDotNet.IO.ByteCode.Decompiler object provides the way to get actionscript command as objects of the SwfDotNet.IO.ByteCode namespace.

Esempio n. 1
0
 public SwfConverter(Stream stream)
 {
     SwfReader reader = new SwfReader(stream);
     swf = reader.ReadSwf();
     reader.Close();
     Convert();
 }
Esempio n. 2
0
        public string GetBytesCode()
        {
            StringBuilder code = new StringBuilder();
            SwfReader Reader = new SwfReader(this.File);
            Swf swf = null;
            try
            {
                swf = Reader.ReadSwf();
            }
            catch (Exception Ex){ throw new Exception(Ex.ToString()); }

            IEnumerator enumerator = swf.Tags.GetEnumerator();
            while (enumerator.MoveNext())
            {
                BaseTag current = (BaseTag)enumerator.Current;
                if (current.ActionRecCount != 0)
                {
                    IEnumerator currentenumerator = current.GetEnumerator();
                    while (currentenumerator.MoveNext())
                    {
                        Decompiler decompiler = new Decompiler(swf.Version);
                        foreach (BaseAction action in decompiler.Decompile((byte[])currentenumerator.Current))
                        {
                            code.AppendLine(action.ToString());
                        }
                    }
                }
            }

            return code.ToString();
        }
Esempio n. 3
0
 public SwfConverter(string file)
 {
     SwfReader reader = new SwfReader(file);
     swf = reader.ReadSwf();
     reader.Close();
     Convert();
 }
Esempio n. 4
0
        protected override byte[] OnData(byte[] data, object userData)
        {
            dynamic o = userData;
            decrypt(data, o.id, o.cid, o.pid);
            data[3] = 7;

            using (var stream = new MemoryStream(data))
            {
                var swfreader = new SwfReader(stream);
                var swf = swfreader.ReadSwf();
                var j = swf.Tags.OfType<DefineBitsJpeg2Tag>().First();
                return j.JpegData;
            }
        }
Esempio n. 5
0
        private void TestFile(string file)
        {
            if (log.IsInfoEnabled)
                log.Info("--- " + file + " (READING)");

            this.Cursor = Cursors.WaitCursor;

            Swf swf = null;

            DateTime readStart = DateTime.Now;
            DateTime readEnd;
            try
            {
                SwfReader reader = new SwfReader(file, true);
                swf = reader.ReadSwf();
                reader.Close();

                readEnd = DateTime.Now;
                TimeSpan duration = new TimeSpan(readEnd.Ticks - readStart.Ticks);
                string min = duration.Minutes.ToString();
                if (min.Length == 1)
                    min = "0" + min;
                string sec = duration.Seconds.ToString();
                if (sec.Length == 1)
                    sec = "0" + sec;
                string msec = duration.Milliseconds.ToString();
                if (msec.Length == 1)
                    msec = "0" + msec;
                string dur = min + ":" + sec + ":" + msec;

                System.Windows.Forms.ListViewItem listViewItem1 = new System.Windows.Forms.ListViewItem(new string[] {"READ: " + file,"OK",dur}, -1);
                listViewItem1.ForeColor = Color.Black;
                this.listView1.Items.Add(listViewItem1);
                this.listView1.Refresh();
                succeded++;

                swfList.Add(swf);
            }
            catch (Exception e)
            {
                readEnd = DateTime.Now;
                TimeSpan duration = new TimeSpan(readEnd.Ticks - readStart.Ticks);
                string min = duration.Minutes.ToString();
                if (min.Length == 1)
                    min = "0" + min;
                string sec = duration.Seconds.ToString();
                if (sec.Length == 1)
                    sec = "0" + sec;
                string msec = duration.Milliseconds.ToString();
                if (msec.Length == 1)
                    msec = "0" + msec;
                string dur = min + ":" + sec + ":" + msec;

                System.Windows.Forms.ListViewItem listViewItem1 = new System.Windows.Forms.ListViewItem(new string[] {"READ: " + file,"KO",dur}, -1);
                listViewItem1.ForeColor = Color.Red;
                this.listView1.Items.Add(listViewItem1);
                this.listView1.Refresh();
                if (log.IsErrorEnabled)
                    log.Error("READING KO", e);
                swfList.Add(null);
            }

            if (log.IsInfoEnabled)
                log.Info("--- " + file + " (WRITING)");

            readStart = DateTime.Now;
            try
            {
                string fileName = System.IO.Path.GetFileName(file);
                string path = this.textBoxOutput.Text + fileName;

                SwfWriter writer = new SwfWriter(path);
                writer.Write(swf);
                writer.Close();

                readEnd = DateTime.Now;
                TimeSpan duration = new TimeSpan(readEnd.Ticks - readStart.Ticks);
                string min = duration.Minutes.ToString();
                if (min.Length == 1)
                    min = "0" + min;
                string sec = duration.Seconds.ToString();
                if (sec.Length == 1)
                    sec = "0" + sec;
                string msec = duration.Milliseconds.ToString();
                if (msec.Length == 1)
                    msec = "0" + msec;
                string dur = min + ":" + sec + ":" + msec;

                System.Windows.Forms.ListViewItem listViewItem1 = new System.Windows.Forms.ListViewItem(new string[] {"WRITE: " + path, "OK",dur}, -1);
                listViewItem1.ForeColor = Color.Black;
                this.listView1.Items.Add(listViewItem1);
                this.listView1.Refresh();

                succededWrite++;
            }
            catch (Exception ee)
            {
                readEnd = DateTime.Now;
                TimeSpan duration = new TimeSpan(readEnd.Ticks - readStart.Ticks);
                string min = duration.Minutes.ToString();
                if (min.Length == 1)
                    min = "0" + min;
                string sec = duration.Seconds.ToString();
                if (sec.Length == 1)
                    sec = "0" + sec;
                string msec = duration.Milliseconds.ToString();
                if (msec.Length == 1)
                    msec = "0" + msec;
                string dur = min + ":" + sec + ":" + msec;

                if (log.IsErrorEnabled)
                    log.Error("WRITING KO", ee);

                System.Windows.Forms.ListViewItem listViewItem1 = new System.Windows.Forms.ListViewItem(new string[] {"WRITE: " + file,"KO",dur}, -1);
                listViewItem1.ForeColor = Color.Red;
                this.listView1.Items.Add(listViewItem1);
                this.listView1.Refresh();
            }

            totalParsed++;
            this.Cursor = Cursors.Default;
            Result.Text = succeded.ToString() + "/" + totalParsed.ToString();
            Result.Refresh();
            WriteResult.Text = succededWrite.ToString() + "/" + totalParsed.ToString();
            WriteResult.Refresh();
        }
Esempio n. 6
0
        private void buttSerialize_Click(object sender, System.EventArgs e)
        {
            if (txtPath.Text == string.Empty)
                return;

            string swfPath = txtPath.Text;
            string xmlPath = txtPath.Text + ".xml";

            SwfReader reader = new SwfReader(swfPath);
            Swf swf = reader.ReadSwf();
            reader.Close();

            XmlTextWriter writer = new XmlTextWriter(xmlPath, Encoding.UTF8);
            swf.Serialize(writer);
            writer.Close();

            object o = null;
            this.axWebBrowser1.Navigate("file://" + xmlPath, ref o, ref o, ref o, ref o);
        }
Esempio n. 7
0
        private void buttonGenerate_Click(object sender, EventArgs e)
        {
            int numImg = 0;
            int numSound = 0;
            int numActionScript = 0;

            string fileName = textBoxSwf.Text;
            string outputDir = textBoxDir.Text;
            TestPath(fileName, outputDir);

            if (log.IsDebugEnabled)
                log.Debug("**************** Start to decompile file " + fileName);

            listViewPix.Items.Clear();
            listViewSounds.Items.Clear();
            listViewActionScript.Items.Clear();

            SwfReader swfReader = null;
            try
            {
                Cursor.Current = Cursors.WaitCursor;
                //var flvReader = new FlvReader(fileName);
                //var flv = flvReader.ReadFlv();

                swfReader = new SwfReader(fileName); // Create a swf stream reader
                Swf swf = swfReader.ReadSwf(); // Read the completed swf file

                //Read headers infos
                labelSwfVersion.Text = swf.Version.ToString();
                labelSwfDim.Text = swf.Header.Width + "x" + swf.Header.Height;
                labelSwfFps.Text = swf.Header.Fps.ToString();
                labelSwfFrames.Text = swf.Header.Frames.ToString();
                labelSwfSize.Text = swf.Header.FileSize.ToString();
                labelSwfSign.Text = swf.Header.Signature;

                //Read tags info
                IEnumerator tagsEnu = swf.Tags.GetEnumerator(); //Browse swf tags list
                while (tagsEnu.MoveNext())
                {
                    var tag = (BaseTag) tagsEnu.Current;
                    if (tag is SetBackgroundColorTag)
                    {
                        Color bgColor = ((SetBackgroundColorTag) tag).RGB.ToWinColor();
                        labelSwfBgColor.Text = "R:" + bgColor.R + " G:" + bgColor.G + " B:" + bgColor.B;
                    }
                    else if (tag is DefineBitsJpeg2Tag)
                    {
                        numImg++;
                        string outfileName = outputDir + GetRandomName() + ".jpg";
                        ((DefineBitsJpeg2Tag) tag).DecompileToFile(outfileName);

                        string shortName = Path.GetFileName(outfileName);
                        var listViewItem1 = new ListViewItem(new[] {shortName, "jpg"}, -1);
                        listViewPix.Items.Add(listViewItem1);
                    }
                    else if (tag is DefineSoundTag) //Extract a sound file:
                    {
                        numSound++;
                        string outfileName = outputDir + GetRandomName();
                        var soundTag = (DefineSoundTag) tag;
                        if (soundTag.SoundFormat == SoundCodec.MP3)
                            outfileName += ".mp3";
                        else
                            outfileName += ".wav";
                        soundTag.DecompileToFile(outfileName);

                        string shortName = Path.GetFileName(outfileName);
                        var listViewItem1 = new ListViewItem(new[] {shortName, ""}, -1);
                        listViewSounds.Items.Add(listViewItem1);
                    }

                    //If tag contains action script...
                    if (tag.ActionRecCount != 0)
                    {
                        var sb = new StringBuilder();
                        IEnumerator enum2 = tag.GetEnumerator();
                        while (enum2.MoveNext())
                        {
                            var dc = new Decompiler(swf.Version);
                            ArrayList actions = dc.Decompile((byte[]) enum2.Current);
                            foreach (BaseAction obj in actions)
                            {
                                sb.AppendLine(obj.ToString());
                            }
                        }
                        string outfileName = outputDir + GetRandomName() + ".as";

                        var writer = new StreamWriter(outfileName);
                        writer.Write(sb.ToString());
                        writer.Close();
                        numActionScript++;

                        string shortName = Path.GetFileName(outfileName);
                        var listViewItem1 = new ListViewItem(new[] {shortName, ""}, -1);
                        listViewActionScript.Items.Add(listViewItem1);
                    }
                }

                Cursor.Current = Cursors.Default;
                string mssg = "Swf decompiler extracts:\n";
                mssg += numImg + " pictures files\n";
                mssg += numSound + " sound files\n";
                mssg += numActionScript + " action script blocks\n";

                MessageBox.Show(mssg, "Decompilation finished", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            finally
            {
                if (swfReader != null)
                    swfReader.Close(); // Closing stream reader
                Cursor.Current = Cursors.Default;
            }
        }