SyncInflate() public method

I don't know what this does!
public SyncInflate ( ) : int
return int
Exemplo n.º 1
0
        public void Zlib_TestFlushSync()
        {
            int rc;
            int bufferSize = 40000;
            byte[] CompressedBytes = new byte[bufferSize];
            byte[] DecompressedBytes = new byte[bufferSize];
            string TextToCompress = "This is the text that will be compressed.";
            byte[] BytesToCompress = System.Text.ASCIIEncoding.ASCII.GetBytes(TextToCompress);

            ZlibCodec compressor = new ZlibCodec(CompressionMode.Compress);

            compressor.InputBuffer = BytesToCompress;
            compressor.NextIn = 0;
            compressor.AvailableBytesIn = 3;

            compressor.OutputBuffer = CompressedBytes;
            compressor.NextOut = 0;
            compressor.AvailableBytesOut = CompressedBytes.Length;

            rc = compressor.Deflate(FlushType.Full);

            CompressedBytes[3]++; // force an error in first compressed block // dinoch - ??
            compressor.AvailableBytesIn = TextToCompress.Length - 3;

            rc = compressor.Deflate(FlushType.Finish);
            Assert.AreEqual<int>(ZlibConstants.Z_STREAM_END, rc, String.Format("at Deflate() [{0}]", compressor.Message));

            rc = compressor.EndDeflate();
            bufferSize = (int)(compressor.TotalBytesOut);

            ZlibCodec decompressor = new ZlibCodec(CompressionMode.Decompress);

            decompressor.InputBuffer = CompressedBytes;
            decompressor.NextIn = 0;
            decompressor.AvailableBytesIn = 2;

            decompressor.OutputBuffer = DecompressedBytes;
            decompressor.NextOut = 0;
            decompressor.AvailableBytesOut = DecompressedBytes.Length;

            rc = decompressor.Inflate(FlushType.None);
            decompressor.AvailableBytesIn = bufferSize - 2;

            rc = decompressor.SyncInflate();

            bool gotException = false;
            try
            {
                rc = decompressor.Inflate(FlushType.Finish);
            }
            catch (ZlibException ex1)
            {
                TestContext.WriteLine("Got Expected Exception: " + ex1);
                gotException = true;
            }

            Assert.IsTrue(gotException, "inflate should report DATA_ERROR");

            rc = decompressor.EndInflate();
            Assert.AreEqual<int>(ZlibConstants.Z_OK, rc, String.Format("at EndInflate() [{0}]", decompressor.Message));

            int j = 0;
            for (; j < DecompressedBytes.Length; j++)
                if (DecompressedBytes[j] == 0)
                    break;

            var result = System.Text.ASCIIEncoding.ASCII.GetString(DecompressedBytes, 0, j);

            Assert.AreEqual<int>(TextToCompress.Length, result.Length + 3, "Strings are unequal lengths");

            Console.WriteLine("orig length: {0}", TextToCompress.Length);
            Console.WriteLine("compressed length: {0}", compressor.TotalBytesOut);
            Console.WriteLine("uncompressed length: {0}", decompressor.TotalBytesOut);
            Console.WriteLine("result length: {0}", result.Length);
            Console.WriteLine("result of inflate:\n(Thi){0}", result);
        }
Exemplo n.º 2
0
    private void Run()
    {
        int rc;
        int comprLen = 40000;
        int uncomprLen = comprLen;
        byte[] CompressedBytes = new byte[comprLen];
        byte[] DecompressedBytes = new byte[uncomprLen];
        string TextToCompress = "This is the text that will be compressed.";
        byte[] BytesToCompress = System.Text.ASCIIEncoding.ASCII.GetBytes(TextToCompress);

        ZlibCodec compressor = new ZlibCodec(CompressionMode.Compress);

        compressor.InputBuffer = BytesToCompress;
        compressor.NextIn = 0;
        compressor.OutputBuffer = CompressedBytes;
        compressor.NextOut = 0;
        compressor.AvailableBytesIn = 3;
        compressor.AvailableBytesOut = CompressedBytes.Length;
                
        rc = compressor.Deflate(ZlibConstants.Z_FULL_FLUSH);
        CheckForError(compressor, rc, "Deflate");
                
        CompressedBytes[3]++; // force an error in first compressed block // dinoch 
        compressor.AvailableBytesIn = TextToCompress.Length - 3;

        rc = compressor.Deflate(ZlibConstants.Z_FINISH);
        if (rc != ZlibConstants.Z_STREAM_END)
        {
            CheckForError(compressor, rc, "Deflate");
        }
        rc = compressor.EndDeflate();
        CheckForError(compressor, rc, "EndDeflate");
        comprLen = (int) (compressor.TotalBytesOut);
                
        ZlibCodec decompressor = new ZlibCodec(CompressionMode.Decompress);
                
        decompressor.InputBuffer = CompressedBytes;
        decompressor.NextIn = 0;
        decompressor.AvailableBytesIn = 2;

        decompressor.OutputBuffer = DecompressedBytes;
        decompressor.NextOut = 0;
        decompressor.AvailableBytesOut = DecompressedBytes.Length;

        rc = decompressor.Inflate(ZlibConstants.Z_NO_FLUSH);
        CheckForError(decompressor, rc, "Inflate");
                
        decompressor.AvailableBytesIn = CompressedBytes.Length - 2;

        rc = decompressor.SyncInflate();
        CheckForError(decompressor, rc, "SyncInflate");

        bool gotException = false;
        try
        {
            rc = decompressor.Inflate(ZlibConstants.Z_FINISH);
        }
        catch (ZlibException ex1)
        {
            Console.WriteLine("Got Expected Exception: " + ex1);
            gotException = true;
        }

        if (!gotException)
        {
            System.Console.Out.WriteLine("inflate should report DATA_ERROR");
            /* Because of incorrect adler32 */
            System.Environment.Exit(1);
        }
                
        rc = decompressor.EndInflate();
        CheckForError(decompressor, rc, "EndInflate");
                
        int j = 0;
        for (; j < DecompressedBytes.Length; j++)
            if (DecompressedBytes[j] == 0)
                break;

        var result = System.Text.ASCIIEncoding.ASCII.GetString(DecompressedBytes, 0, j);

        Console.WriteLine("orig length: {0}", TextToCompress.Length);
        Console.WriteLine("compressed length: {0}", compressor.TotalBytesOut);
        Console.WriteLine("uncompressed length: {0}", decompressor.TotalBytesOut);
        Console.WriteLine("result length: {0}", result.Length);
        Console.WriteLine("result of inflate:\n(Thi){0}", result);
    }