Esempio n. 1
0
        public static void Decompress(Stream source, Stream target)
        {
            var reader       = new SwfStreamReader(source);
            var outputWriter = new SwfStreamWriter(target);

            var fileInfo = reader.ReadSwfFileInfo();

            if (fileInfo.Format == SwfFormat.Unknown)
            {
                throw new NotSupportedException("Illegal file format");
            }

            var rest = reader.ReadRest();

            if (fileInfo.Format == SwfFormat.FWS)
            {
                outputWriter.WriteSwfFileInfo(fileInfo);
                outputWriter.WriteBytes(rest);
                return;
            }

            var uncompressed = new MemoryStream();

            SwfZip.Decompress(new MemoryStream(rest), uncompressed, fileInfo.Format);

            outputWriter.WriteSwfFileInfo(new SwfFileInfo {
                Format     = SwfFormat.FWS,
                FileLength = (uint)(uncompressed.Length + 8),
                Version    = fileInfo.Version
            });
            outputWriter.WriteBytes(uncompressed.ToArray());
            outputWriter.Flush();
        }
Esempio n. 2
0
        public static void Compress(Stream source, Stream target, SwfFormat compressionFormat)
        {
            var reader       = new SwfStreamReader(source);
            var outputWriter = new SwfStreamWriter(target);

            var fileInfo = reader.ReadSwfFileInfo();
            var rest     = reader.ReadRest();

            if (fileInfo.Format != SwfFormat.FWS)
            {
                MemoryStream mem = new MemoryStream();
                source.Seek(0, SeekOrigin.Begin);
                Decompress(source, mem);
                mem.Seek(0, SeekOrigin.Begin);
                reader   = new SwfStreamReader(mem);
                fileInfo = reader.ReadSwfFileInfo();
                rest     = reader.ReadRest();
            }

            outputWriter.WriteSwfFileInfo(new SwfFileInfo {
                Format     = compressionFormat,
                FileLength = (uint)(rest.Length) + 8,
                Version    = fileInfo.Version
            });

            var compressed = new MemoryStream();

            SwfZip.Compress(new MemoryStream(rest), compressed, compressionFormat);
            outputWriter.WriteBytes(compressed.ToArray());

            outputWriter.Flush();
        }
Esempio n. 3
0
        public void WriteTo(Stream stream, SwfFormat swfFormat)
        {
            var outputWriter = new SwfStreamWriter(stream);

            if (swfFormat != SwfFormat.FWS)
            {
                var res = new MemoryStream();
                WriteTo(res, SwfFormat.FWS);
                res.Seek(0, SeekOrigin.Begin);
                Compress(res, stream, swfFormat);
            }
            else
            {
                var mem = new MemoryStream();
                WriteBodyTo(mem);
                mem.Seek(0, SeekOrigin.Begin);

                outputWriter.WriteSwfFileInfo(new SwfFileInfo {
                    Format     = SwfFormat.FWS,
                    FileLength = (uint)(mem.Length + 8),
                    Version    = FileInfo.Version
                });
                mem.WriteTo(stream);
            }
            outputWriter.Flush();
            stream.Flush();
        }
Esempio n. 4
0
        public void WriteTo(Stream stream, SwfFormat swfFormat)
        {
            var outputWriter = new SwfStreamWriter(stream);

            if (swfFormat != SwfFormat.FWS)
            {
                var res = new MemoryStream();
                WriteTo(res, SwfFormat.FWS);
                res.Seek(0, SeekOrigin.Begin);
                Compress(res, stream, swfFormat);
            }
            else
            {
                var mem    = new MemoryStream();
                var writer = new SwfStreamWriter(mem);
                writer.WriteSwfHeader(Header);
                var bin = new SwfTagSerializer(this);
                foreach (var tag in Tags)
                {
                    var tagData = bin.GetTagData(tag);
                    writer.WriteTagData(tagData);
                }
                mem.Seek(0, SeekOrigin.Begin);

                outputWriter.WriteSwfFileInfo(new SwfFileInfo {
                    Format     = SwfFormat.FWS,
                    FileLength = (uint)(mem.Length + 8),
                    Version    = FileInfo.Version
                });
                mem.WriteTo(stream);
            }
            outputWriter.Flush();
            stream.Flush();
        }
Esempio n. 5
0
        public static void Decompress(Stream source, Stream target)
        {
            var reader = new SwfStreamReader(source);
            var outputWriter = new SwfStreamWriter(target);

            var fileInfo = reader.ReadSwfFileInfo();
            var rest = reader.ReadRest();
            if (fileInfo.Format == "CWS") {
                var uncompressed = new MemoryStream();
                SwfZip.Decompress(new MemoryStream(rest), uncompressed);
                outputWriter.WriteSwfFileInfo(new SwfFileInfo {
                    Format = "FWS",
                    FileLength = (uint)(uncompressed.Length),
                    Version = fileInfo.Version
                });

                outputWriter.WriteBytes(uncompressed.ToArray());
            } else if (fileInfo.Format == "FWS") {
                outputWriter.WriteSwfFileInfo(fileInfo);
                outputWriter.WriteBytes(rest);
            } else {
                throw new NotSupportedException("Illegal file format");
            }
            outputWriter.Flush();
        }
Esempio n. 6
0
        private byte[] SerializeGlyphsData(IEnumerable <Glyph> glyphs, IList <uint> offsets)
        {
            var shapesStream    = new MemoryStream();
            var shapesSwfWriter = new SwfStreamWriter(shapesStream);

            foreach (var glyph in glyphs)
            {
                offsets.Add((uint)shapesStream.Position);
                shapesSwfWriter.WriteShapeRecordsRGB(glyph.Records, 1, 0);
                shapesSwfWriter.FlushBits();
            }
            return(shapesStream.ToArray());
        }
Esempio n. 7
0
        public SwfTagData GetTagData(SwfTagBase tag)
        {
            var mem    = new MemoryStream();
            var writer = new SwfStreamWriter(mem);

            tag.AcceptVistor(this, writer);
            writer.FlushBits();
            if (tag.RestData != null && tag.RestData.Length > 0)
            {
                writer.WriteBytes(tag.RestData);
            }
            return(new SwfTagData {
                Type = tag.TagType, Data = mem.ToArray()
            });
        }
Esempio n. 8
0
        public void WriteBodyTo(Stream mem)
        {
            var writer = new SwfStreamWriter(mem);

            writer.WriteSwfHeader(Header);
            var bin = new SwfTagSerializer(this);

            foreach (var tag in Tags)
            {
                var tagData = bin.GetTagData(tag);

                if (ReserializeAbcOnly && tag is not DoABCTag)
                {
                    tagData.Data = tag.OriginalData;
                }

                writer.WriteTagData(tagData);
            }
        }
Esempio n. 9
0
        SwfTagData ISwfTagVisitor <ISwfStreamWriter, SwfTagData> .Visit(DefineButton2Tag tag, ISwfStreamWriter writer)
        {
            writer.WriteUInt16(tag.ButtonID);
            writer.WriteUnsignedBits(tag.ReservedFlags, 7);
            writer.WriteBit(tag.TrackAsMenu);

            var mem           = new MemoryStream();
            var buttonsWriter = new SwfStreamWriter(mem);

            foreach (var record in tag.Characters)
            {
                buttonsWriter.WriteButtonRecordEx(record);
            }
            var actionsOffset = tag.Conditions.Count > 0 ? (mem.Length + 2) : 0;

            writer.WriteUInt16((ushort)actionsOffset);

            writer.WriteBytes(mem.ToArray());

            if (tag.Conditions.Count > 0)
            {
                for (var i = 0; i < tag.Conditions.Count; i++)
                {
                    var cond       = tag.Conditions[i];
                    var condMem    = new MemoryStream();
                    var condWriter = new SwfStreamWriter(condMem);
                    condWriter.WriteButtonCondition(cond);

                    if (i != tag.Conditions.Count - 1)
                    {
                        writer.WriteUInt16((ushort)(condMem.Length + 2));
                    }
                    else
                    {
                        writer.WriteUInt16(0);
                    }

                    writer.WriteBytes(condMem.ToArray());
                }
            }

            return(null);
        }
Esempio n. 10
0
        public static void Compress(Stream source, Stream target)
        {
            var reader = new SwfStreamReader(source);
            var outputWriter = new SwfStreamWriter(target);

            var fileInfo = reader.ReadSwfFileInfo();
            var rest = reader.ReadRest();
            if (fileInfo.Format == "FWS") {
                var compressed = new MemoryStream();
                SwfZip.Compress(new MemoryStream(rest), compressed);
                outputWriter.WriteSwfFileInfo(new SwfFileInfo {
                    Format = "CWS",
                    FileLength = (uint)(rest.Length) + 8,
                    Version = fileInfo.Version
                });

                outputWriter.WriteBytes(compressed.ToArray());
            } else {
                outputWriter.WriteSwfFileInfo(fileInfo);
                outputWriter.WriteBytes(rest);
            }
            outputWriter.Flush();
        }
Esempio n. 11
0
        public void WriteTo(Stream stream, bool compress)
        {
            var outputWriter = new SwfStreamWriter(stream);

            if (compress) {
                var res = new MemoryStream();
                WriteTo(res, false);
                res.Seek(0, SeekOrigin.Begin);
                Compress(res, stream);
            } else {
                var mem = new MemoryStream();
                var writer = new SwfStreamWriter(mem);
                writer.WriteSwfHeader(Header);
                var bin = new SwfTagSerializer(this);
                foreach (var tag in Tags) {
                    var tagData = bin.GetTagData(tag);
                    writer.WriteTagData(tagData);
                }
                mem.Seek(0, SeekOrigin.Begin);

                outputWriter.WriteSwfFileInfo(new SwfFileInfo {
                    Format = "FWS",
                    FileLength = (uint)(mem.Length + 8),
                    Version = FileInfo.Version
                });
                mem.WriteTo(stream);
            }
            outputWriter.Flush();
            stream.Flush();
        }