Esempio n. 1
0
        void StreamCompressedContent(IO.EndianStream s)
        {
            if (s.IsReading)
            {
                using (var cs = new CompressedStream(true))
                {
                    Stream(s, EnumFlags.Test(Flags, FileFlags.EncryptContent), cs,
                           userKey: Header.DataCryptKey, streamLeftovers: StreamLeftovers);

                    cs.Decompress();
                    Content = cs.UncompressedData;
                }
            }
            else if (s.IsWriting)
            {
                using (var cs = new CompressedStream(true))
                    using (var ms = new System.IO.MemoryStream(kMaxContentSize))
                        using (var sout = new IO.EndianWriter(ms, s.ByteOrder))
                        {
                            sout.Write(Content);
                            sout.Seek(0);

                            cs.InitializeFromStream(ms);
                            cs.Compress();

                            Stream(s, EnumFlags.Test(Flags, FileFlags.EncryptContent), cs,
                                   userKey: Header.DataCryptKey, streamLeftovers: StreamLeftovers);
                        }
            }
        }
Esempio n. 2
0
        public void Write(IO.EndianWriter s)
        {
            //Flags = EnumFlags.Remove(Flags, FileFlags.EncryptHeader | FileFlags.EncryptContent);

            s.Write(Flags, FileFlagsStreamer.Instance);
            s.Write((ushort)kVersion);

            Write(s, EnumFlags.Test(Flags, FileFlags.EncryptHeader), Header, MediaHeader.kSizeOf);
            GenerateHash();

            if (EnumFlags.Test(Flags, FileFlags.CompressContent))
            {
                using (var cs = new CompressedStream(true))
                    using (var ms = new System.IO.MemoryStream(kMaxContentSize))
                        using (var sout = new IO.EndianWriter(ms, Shell.EndianFormat.Big))
                        {
                            sout.Write(Content);
                            sout.Seek(0);

                            cs.InitializeFromStream(ms);
                            cs.Compress();

                            Write(s, EnumFlags.Test(Flags, FileFlags.EncryptContent), cs,
                                  userKey: Header.DataCryptKey, writeLeftovers: WriteLeftovers);
                        }
            }
            else
            {
                s.Write(Content);
            }
        }
        public static void CompressFromStream(IO.EndianWriter blockStream, System.IO.Stream source,
                                              out uint streamAdler, out int streamSize)
        {
            Contract.Requires <ArgumentNullException>(blockStream != null);
            Contract.Requires <ArgumentNullException>(source != null);

            using (var ms = new System.IO.MemoryStream((int)source.Length + Header.kSizeOf))
                using (var s = new IO.EndianStream(ms, Shell.EndianFormat.Big, permissions: FA.Write))
                    using (var cs = new CompressedStream())
                    {
                        s.StreamMode = FA.Write;

                        cs.InitializeFromStream(source);
                        cs.Compress();

                        cs.Serialize(s);

                        ms.Position = 0;
                        streamSize  = (int)ms.Length;
                        streamAdler = Adler32.Compute(ms, streamSize);

                        ms.WriteTo(blockStream.BaseStream);
                    }
        }