Esempio n. 1
0
        public WorkItem(int size, Ionic.Zlib.CompressionLevel compressLevel, CompressionStrategy strategy)
        {
            buffer= new byte[size];
            // alloc 5 bytes overhead for every block (margin of safety= 2)
            int n = size + ((size / 32768)+1) * 5 * 2;
            compressed= new byte[n];

            status = (int)Status.None;
            compressor = new ZlibCodec();
            compressor.InitializeDeflate(compressLevel, false);
            compressor.OutputBuffer = compressed;
            compressor.InputBuffer = buffer;
        }
Esempio n. 2
0
		public WorkItem(int size,
		                CompressionLevel compressLevel,
		                CompressionStrategy strategy,
		                int ix)
		{
			this.buffer= new byte[size];
			// alloc 5 bytes overhead for every block (margin of safety= 2)
			int n = size + ((size / 32768)+1) * 5 * 2;
			this.compressed = new byte[n];
			this.compressor = new ZlibCodec();
			this.compressor.InitializeDeflate(compressLevel, false);
			this.compressor.OutputBuffer = this.compressed;
			this.compressor.InputBuffer = this.buffer;
			this.index = ix;
		}
Esempio n. 3
0
 internal int Initialize(ZlibCodec codec, int w)
 {
     this.Codec = codec;
     this.Codec.Message = (string)null;
     this.Blocks = (InflateBlocks)null;
     if (w < 8 || w > 15)
     {
         this.End();
         throw new ZlibException("Bad window size.");
     }
     else
     {
         this.Wbits = w;
         this.Blocks = new InflateBlocks(codec, this.HandleRfc1950HeaderBytes ? (object)this : (object)(InflateManager)null, 1 << w);
         this.Reset();
         return 0;
     }
 }
 internal int SyncPoint(ZlibCodec z)
 {
     return this.blocks.SyncPoint();
 }
Esempio n. 5
0
 private void end()
 {
     if (z == null)
         return;
     //if (_wantCompress)
     //{
     //    _z.EndDeflate();
     //}
     //else
     {
         _z.EndInflate();
     }
     _z = null;
 }
Esempio n. 6
0
 internal int SyncPoint(ZlibCodec z)
 {
     return(blocks.SyncPoint());
 }
    private void Run()
    {
        int rc;
        int j;
        int bufferSize = 40000;

        byte[] compressedBytes   = new byte[bufferSize];
        byte[] bufferToCompress  = new byte[bufferSize];
        byte[] decompressedBytes = new byte[bufferSize];

        ZlibCodec compressingStream = new ZlibCodec();

        rc = compressingStream.InitializeDeflate(CompressionLevel.BestSpeed);
        CheckForError(compressingStream, rc, "InitializeDeflate");

        compressingStream.OutputBuffer      = compressedBytes;
        compressingStream.NextOut           = 0;
        compressingStream.AvailableBytesOut = compressedBytes.Length;

        // At this point, bufferToCompress is all zeroes, so it should compress
        // very well:
        compressingStream.InputBuffer      = bufferToCompress;
        compressingStream.AvailableBytesIn = bufferToCompress.Length;
        rc = compressingStream.Deflate(FlushType.None);
        CheckForError(compressingStream, rc, "deflate");
        if (compressingStream.AvailableBytesIn != 0)
        {
            System.Console.Out.WriteLine("deflate not greedy");
            System.Environment.Exit(1);
        }

        Console.WriteLine("Stage 1: uncompressed bytes in so far:  {0,6}", compressingStream.TotalBytesIn);
        Console.WriteLine("          compressed bytes out so far:  {0,6}", compressingStream.TotalBytesOut);


        // Feed in already compressed data and switch to no compression:
        compressingStream.SetDeflateParams(CompressionLevel.None, CompressionStrategy.Default);
        compressingStream.InputBuffer      = compressedBytes;
        compressingStream.NextIn           = 0;
        compressingStream.AvailableBytesIn = bufferSize / 2; // why? - for fun
        rc = compressingStream.Deflate(FlushType.None);
        CheckForError(compressingStream, rc, "Deflate");

        Console.WriteLine("Stage 2: uncompressed bytes in so far:  {0,6}", compressingStream.TotalBytesIn);
        Console.WriteLine("          compressed bytes out so far:  {0,6}", compressingStream.TotalBytesOut);

        // Insert data into bufferToCompress, and Switch back to compressing mode:
        System.Random rnd = new Random();

        for (int i = 0; i < bufferToCompress.Length / 1000; i++)
        {
            byte b = (byte)rnd.Next();
            int  n = 500 + rnd.Next(500);
            for (j = 0; j < n; j++)
            {
                bufferToCompress[j + i] = b;
            }
            i += j - 1;
        }

        compressingStream.SetDeflateParams(CompressionLevel.BestCompression, CompressionStrategy.Filtered);
        compressingStream.InputBuffer      = bufferToCompress;
        compressingStream.NextIn           = 0;
        compressingStream.AvailableBytesIn = bufferToCompress.Length;
        rc = compressingStream.Deflate(FlushType.None);
        CheckForError(compressingStream, rc, "Deflate");

        Console.WriteLine("Stage 3: uncompressed bytes in so far:  {0,6}", compressingStream.TotalBytesIn);
        Console.WriteLine("          compressed bytes out so far:  {0,6}", compressingStream.TotalBytesOut);

        rc = compressingStream.Deflate(FlushType.Finish);
        if (rc != ZlibConstants.Z_STREAM_END)
        {
            Console.WriteLine("deflate reported {0}, should report Z_STREAM_END", rc);
            Environment.Exit(1);
        }
        rc = compressingStream.EndDeflate();
        CheckForError(compressingStream, rc, "EndDeflate");

        Console.WriteLine("Stage 4: uncompressed bytes in (final): {0,6}", compressingStream.TotalBytesIn);
        Console.WriteLine("          compressed bytes out (final): {0,6}", compressingStream.TotalBytesOut);

        ZlibCodec decompressingStream = new ZlibCodec(CompressionMode.Decompress);

        decompressingStream.InputBuffer      = compressedBytes;
        decompressingStream.NextIn           = 0;
        decompressingStream.AvailableBytesIn = bufferSize;

        // upon inflating, we overwrite the decompressedBytes buffer repeatedly
        while (true)
        {
            decompressingStream.OutputBuffer      = decompressedBytes;
            decompressingStream.NextOut           = 0;
            decompressingStream.AvailableBytesOut = decompressedBytes.Length;
            rc = decompressingStream.Inflate(FlushType.None);
            if (rc == ZlibConstants.Z_STREAM_END)
            {
                break;
            }
            CheckForError(decompressingStream, rc, "inflate large");
        }

        rc = decompressingStream.EndInflate();
        CheckForError(decompressingStream, rc, "EndInflate");

        if (decompressingStream.TotalBytesOut != 2 * decompressedBytes.Length + bufferSize / 2)
        {
            System.Console.WriteLine("bad large inflate: " + decompressingStream.TotalBytesOut);
            System.Environment.Exit(1);
        }

        for (j = 0; j < decompressedBytes.Length; j++)
        {
            if (decompressedBytes[j] == 0)
            {
                break;
            }
        }

        Console.WriteLine("compressed length: {0}", compressingStream.TotalBytesOut);
        Console.WriteLine("decompressed length (expected): {0}", 2 * decompressedBytes.Length + bufferSize / 2);
        Console.WriteLine("decompressed length (actual)  : {0}", decompressingStream.TotalBytesOut);
    }
Esempio n. 8
0
        public void testDisjointBuffers()
        {
            OutputCollector  collect = new OutputCollector();
            CompressionCodec codec   = new ZlibCodec();
            OutStream        @out    = new OutStream("test", 400, codec, collect);

            PositionCollector[] positions = new PositionCollector[1024];
            DataOutput          stream    = new DataOutputStream(@out);

            for (int i = 0; i < 1024; ++i)
            {
                positions[i] = new PositionCollector();
                @out.getPosition(positions[i]);
                stream.writeInt(i);
            }
            @out.Flush();
            Assert.Equal("test", @out.ToString());
            Assert.Equal(1674, collect.buffer.size());
            ByteBuffer[] inBuf = new ByteBuffer[3];
            inBuf[0] = ByteBuffer.allocate(500);
            inBuf[1] = ByteBuffer.allocate(1200);
            inBuf[2] = ByteBuffer.allocate(500);
            collect.buffer.setByteBuffer(inBuf[0], 0, 483);
            collect.buffer.setByteBuffer(inBuf[1], 483, 1625 - 483);
            collect.buffer.setByteBuffer(inBuf[2], 1625, 1674 - 1625);

            for (int i = 0; i < inBuf.Length; ++i)
            {
                inBuf[i].flip();
            }
            InStream @in = InStream.create(null, "test", inBuf,
                                           new long[] { 0, 483, 1625 }, 1674, codec, 400);

            Assert.Equal("compressed stream test position: 0 length: 1674 range: 0" +
                         " offset: 0 limit: 0 range 0 = 0 to 483;" +
                         "  range 1 = 483 to 1142;  range 2 = 1625 to 49",
                         @in.ToString());
            DataInputStream inStream = new DataInputStream(@in);

            for (int i = 0; i < 1024; ++i)
            {
                int x = inStream.readInt();
                Assert.Equal(i, x);
            }
            Assert.Equal(0, @in.available());
            for (int i = 1023; i >= 0; --i)
            {
                @in.seek(positions[i]);
                Assert.Equal(i, inStream.readInt());
            }

            @in = InStream.create(null, "test", new ByteBuffer[] { inBuf[1], inBuf[2] },
                                  new long[] { 483, 1625 }, 1674, codec, 400);
            inStream = new DataInputStream(@in);
            positions[303].reset();
            @in.seek(positions[303]);
            for (int i = 303; i < 1024; ++i)
            {
                Assert.Equal(i, inStream.readInt());
            }

            @in = InStream.create(null, "test", new ByteBuffer[] { inBuf[0], inBuf[2] },
                                  new long[] { 0, 1625 }, 1674, codec, 400);
            inStream = new DataInputStream(@in);
            positions[1001].reset();
            for (int i = 0; i < 300; ++i)
            {
                Assert.Equal(i, inStream.readInt());
            }
            @in.seek(positions[1001]);
            for (int i = 1001; i < 1024; ++i)
            {
                Assert.Equal(i, inStream.readInt());
            }
        }
Esempio n. 9
0
 internal static int inflate_trees_fixed(int[] bl, int[] bd, int[][] tl, int[][] td, ZlibCodec z)
 {
     bl[0] = fixed_bl;
     bd[0] = fixed_bd;
     tl[0] = fixed_tl;
     td[0] = fixed_td;
     return(Z_OK);
 }
Esempio n. 10
0
        internal int inflate_trees_dynamic(int nl, int nd, int[] c, int[] bl, int[] bd, int[] tl, int[] td, int[] hp, ZlibCodec z)
        {
            int result;

            // build literal/length tree
            initWorkArea(288);
            hn[0]  = 0;
            result = huft_build(c, 0, nl, 257, cplens, cplext, tl, bl, hp, hn, v);
            if (result != Z_OK || bl[0] == 0)
            {
                if (result == Z_DATA_ERROR)
                {
                    z.Message = "oversubscribed literal/length tree";
                }
                else if (result != Z_MEM_ERROR)
                {
                    z.Message = "incomplete literal/length tree";
                    result    = Z_DATA_ERROR;
                }
                return(result);
            }

            // build distance tree
            initWorkArea(288);
            result = huft_build(c, nl, nd, 0, cpdist, cpdext, td, bd, hp, hn, v);

            if (result != Z_OK || (bd[0] == 0 && nl > 257))
            {
                if (result == Z_DATA_ERROR)
                {
                    z.Message = "oversubscribed distance tree";
                }
                else if (result == Z_BUF_ERROR)
                {
                    z.Message = "incomplete distance tree";
                    result    = Z_DATA_ERROR;
                }
                else if (result != Z_MEM_ERROR)
                {
                    z.Message = "empty distance tree with lengths";
                    result    = Z_DATA_ERROR;
                }
                return(result);
            }

            return(Z_OK);
        }
Esempio n. 11
0
        internal int inflate_trees_dynamic(int nl, int nd, int[] c, int[] bl, int[] bd, int[] tl, int[] td, int[] hp, ZlibCodec z)
        {
            int result;

            // build literal/length tree
            initWorkArea(288);
            hn[0] = 0;
            result = huft_build(c, 0, nl, 257, cplens, cplext, tl, bl, hp, hn, v);
            if (result != Z_OK || bl[0] == 0)
            {
                if (result == Z_DATA_ERROR)
                {
                    z.Message = "oversubscribed literal/length tree";
                }
                else if (result != Z_MEM_ERROR)
                {
                    z.Message = "incomplete literal/length tree";
                    result = Z_DATA_ERROR;
                }
                return result;
            }

            // build distance tree
            initWorkArea(288);
            result = huft_build(c, nl, nd, 0, cpdist, cpdext, td, bd, hp, hn, v);

            if (result != Z_OK || (bd[0] == 0 && nl > 257))
            {
                if (result == Z_DATA_ERROR)
                {
                    z.Message = "oversubscribed distance tree";
                }
                else if (result == Z_BUF_ERROR)
                {
                    z.Message = "incomplete distance tree";
                    result = Z_DATA_ERROR;
                }
                else if (result != Z_MEM_ERROR)
                {
                    z.Message = "empty distance tree with lengths";
                    result = Z_DATA_ERROR;
                }
                return result;
            }

            return Z_OK;
        }
Esempio n. 12
0
        internal int inflate_trees_bits(int[] c, int[] bb, int[] tb, int[] hp, ZlibCodec z)
        {
            int result;
            initWorkArea(19);
            hn[0] = 0;
            result = huft_build(c, 0, 19, 19, null, null, tb, bb, hp, hn, v);

            if (result == Z_DATA_ERROR)
            {
                z.Message = "oversubscribed dynamic bit lengths tree";
            }
            else if (result == Z_BUF_ERROR || bb[0] == 0)
            {
                z.Message = "incomplete dynamic bit lengths tree";
                result = Z_DATA_ERROR;
            }
            return result;
        }
Esempio n. 13
0
 internal static int inflate_trees_fixed(int[] bl, int[] bd, int[][] tl, int[][] td, ZlibCodec z)
 {
     bl[0] = fixed_bl;
     bd[0] = fixed_bd;
     tl[0] = fixed_tl;
     td[0] = fixed_td;
     return Z_OK;
 }
        public static void Can_Deserialize_Captures_To_GamePacketPayloads(PacketCaptureTestEntry entry)
        {
            Console.WriteLine($"Entry Size: {entry.BinaryData.Length} OpCode: {entry.OpCode}");

            //TODO: Test compression another time.
            if (entry.OpCode == NetworkOperationCode.SMSG_COMPRESSED_UPDATE_OBJECT)
            {
                //Skip the opcode
                int    decompressedSize = entry.BinaryData.Reinterpret <int>(2);
                byte[] newBytes         = new byte[decompressedSize + 2];         // +2 for opcode

                ZlibCodec stream = new ZlibCodec(CompressionMode.Decompress)
                {
                    InputBuffer       = entry.BinaryData,
                    NextIn            = 2 + 4,          //opcode + size
                    AvailableBytesIn  = entry.BinaryData.Length,
                    OutputBuffer      = newBytes,
                    NextOut           = 2,
                    AvailableBytesOut = decompressedSize
                };

                stream.Inflate(FlushType.None);
                stream.Inflate(FlushType.Finish);
                stream.EndInflate();

                ((short)(NetworkOperationCode.SMSG_UPDATE_OBJECT)).Reinterpret(newBytes, 0);

                entry = new PacketCaptureTestEntry(NetworkOperationCode.SMSG_UPDATE_OBJECT, newBytes, entry.FileName);
            }

            //arrange
            SerializerService serializer = Serializer;

            GamePacketPayload payload;

            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            //act
            try
            {
                payload = serializer.Deserialize <GamePacketPayload>(entry.BinaryData);
            }
            catch (Exception e)
            {
                Console.WriteLine($"Critical failure. Cannot deserialize File: {entry.FileName} FileSize: {entry.BinaryData.Length} \n\n Exception: {e.Message} Stack: {e.StackTrace}");
                return;
            }
            finally
            {
                stopwatch.Stop();
            }

            Console.WriteLine($"Serialization time in ms: {stopwatch.ElapsedMilliseconds}");

            foreach (ObjectUpdateBlock block in ((IObjectUpdatePayload)payload).UpdateBlocks.Items)
            {
                Console.WriteLine($"Encountered: {block.GetType().Name} Block Type: {block.UpdateType}");
            }


            //assert
            if (payload == null)
            {
                Console.WriteLine($"Resulting capture capture deserialization attempt null for File: {entry.FileName}");
            }

            //We should have deserialized it. We want to make sure the opcode matches
            if (entry.OpCode != payload.GetOperationCode())
            {
                Console.WriteLine($"Mismatched {nameof(NetworkOperationCode)} on packet capture File: {entry.FileName}. Expected: {entry.OpCode} Was: {payload.GetOperationCode()}");
            }
        }