Deflate() public méthode

Deflate one batch of data.
You must have set InputBuffer and OutputBuffer before calling this method.
public Deflate ( FlushType flush ) : int
flush FlushType whether to flush all data as you deflate. Generally you will want to /// use Z_NO_FLUSH here, in a series of calls to Deflate(), and then call EndDeflate() to /// flush everything. ///
Résultat int
Exemple #1
0
        public override void Write(System.Byte[] buffer, int offset, int count)
        {
            // workitem 7159
            // calculate the CRC on the unccompressed data  (before writing)
            if (crc != null)
            {
                crc.SlurpBlock(buffer, offset, count);
            }

            if (_streamMode == StreamMode.Undefined)
            {
                _streamMode = StreamMode.Writer;
            }
            else if (_streamMode != StreamMode.Writer)
            {
                throw new ZlibException("Cannot Write after Reading.");
            }

            if (count == 0)
            {
                return;
            }

            // first reference of z property will initialize the private var _z
            z.InputBuffer       = buffer;
            _z.NextIn           = offset;
            _z.AvailableBytesIn = count;
            bool done = false;

            do
            {
                _z.OutputBuffer      = workingBuffer;
                _z.NextOut           = 0;
                _z.AvailableBytesOut = _workingBuffer.Length;
                int rc = (_wantCompress)
                                        ? _z.Deflate(_flushMode)
                                        : _z.Inflate(_flushMode);
                if (rc != ZlibConstants.Z_OK && rc != ZlibConstants.Z_STREAM_END)
                {
                    throw new ZlibException((_wantCompress ? "de" : "in") + "flating: " + _z.Message);
                }

                //if (_workingBuffer.Length - _z.AvailableBytesOut > 0)
                _stream.Write(_workingBuffer, 0, _workingBuffer.Length - _z.AvailableBytesOut);

                done = _z.AvailableBytesIn == 0 && _z.AvailableBytesOut != 0;

                // If GZIP and de-compress, we're done when 8 bytes remain.
                if (_flavor == ZlibStreamFlavor.GZIP && !_wantCompress)
                {
                    done = (_z.AvailableBytesIn == 8 && _z.AvailableBytesOut != 0);
                }
            }while (!done);
        }
Exemple #2
0
        private void _FlushFinish()
        {
            byte[]    buffer    = new byte[128];
            ZlibCodec zlibCodec = new ZlibCodec();

            zlibCodec.InitializeDeflate(this._compressLevel, false);
            zlibCodec.InputBuffer       = (byte[])null;
            zlibCodec.NextIn            = 0;
            zlibCodec.AvailableBytesIn  = 0;
            zlibCodec.OutputBuffer      = buffer;
            zlibCodec.NextOut           = 0;
            zlibCodec.AvailableBytesOut = buffer.Length;
            int num = zlibCodec.Deflate(FlushType.Finish);

            if (num != 1 && num != 0)
            {
                throw new Exception("deflating: " + zlibCodec.Message);
            }
            if (buffer.Length - zlibCodec.AvailableBytesOut > 0)
            {
                this._outStream.Write(buffer, 0, buffer.Length - zlibCodec.AvailableBytesOut);
            }
            zlibCodec.EndDeflate();
            this._Crc32 = this._runningCrc.Crc32Result;
        }
Exemple #3
0
        public override void Write(byte[] buffer, int offset, int count)
        {
            if (crc != null)
            {
                crc.SlurpBlock(buffer, offset, count);
            }
            if (_streamMode == StreamMode.Undefined)
            {
                _streamMode = StreamMode.Writer;
            }
            else if (_streamMode != 0)
            {
                throw new ZlibException("Cannot Write after Reading.");
            }
            if (count == 0)
            {
                return;
            }
            z.InputBuffer       = buffer;
            _z.NextIn           = offset;
            _z.AvailableBytesIn = count;
            bool flag = false;

            while (true)
            {
                _z.OutputBuffer      = workingBuffer;
                _z.NextOut           = 0;
                _z.AvailableBytesOut = _workingBuffer.Length;
                int num = _wantCompress ? _z.Deflate(_flushMode) : _z.Inflate(_flushMode);
                if (num != 0 && num != 1)
                {
                    break;
                }
                _stream.Write(_workingBuffer, 0, _workingBuffer.Length - _z.AvailableBytesOut);
                flag = (_z.AvailableBytesIn == 0 && _z.AvailableBytesOut != 0);
                if (_flavor == ZlibStreamFlavor.GZIP && !_wantCompress)
                {
                    flag = (_z.AvailableBytesIn == 8 && _z.AvailableBytesOut != 0);
                }
                if (flag)
                {
                    return;
                }
            }
            throw new ZlibException((_wantCompress ? "de" : "in") + "flating: " + _z.Message);
        }
        private bool DeflateOneSegment(WorkItem workitem)
        {
            ZlibCodec compressor = workitem.compressor;

            compressor.ResetDeflate();
            compressor.NextIn            = 0;
            compressor.AvailableBytesIn  = workitem.inputBytesAvailable;
            compressor.NextOut           = 0;
            compressor.AvailableBytesOut = workitem.compressed.Length;
            do
            {
                compressor.Deflate(FlushType.None);
            }while (compressor.AvailableBytesIn > 0 || compressor.AvailableBytesOut == 0);
            compressor.Deflate(FlushType.Sync);
            workitem.compressedBytesAvailable = (int)compressor.TotalBytesOut;
            return(true);
        }
Exemple #5
0
        private bool DeflateOneSegment(WorkItem workitem)
        {
            ZlibCodec zlibCodec = workitem.compressor;
            int       num       = 0;

            zlibCodec.ResetDeflate();
            zlibCodec.NextIn            = 0;
            zlibCodec.AvailableBytesIn  = workitem.inputBytesAvailable;
            zlibCodec.NextOut           = 0;
            zlibCodec.AvailableBytesOut = workitem.compressed.Length;
            do
            {
                zlibCodec.Deflate(FlushType.None);
            }while (zlibCodec.AvailableBytesIn > 0 || zlibCodec.AvailableBytesOut == 0);
            num = zlibCodec.Deflate(FlushType.Sync);
            workitem.compressedBytesAvailable = (int)zlibCodec.TotalBytesOut;
            return(true);
        }
Exemple #6
0
        public override void Write(byte[] buffer, int offset, int count)
        {
            if (_streamMode == StreamMode.Undefined)
            {
                _streamMode = StreamMode.Writer;
            }
            else if (_streamMode != StreamMode.Writer)
            {
                throw new ZlibException("Cannot Write after Reading.");
            }

            if (count == 0)
            {
                return;
            }

            // first reference of z property will initialize the private var _z
            z.InputBuffer       = buffer;
            _z.NextIn           = offset;
            _z.AvailableBytesIn = count;
            var done = false;

            do
            {
                _z.OutputBuffer      = workingBuffer;
                _z.NextOut           = 0;
                _z.AvailableBytesOut = _workingBuffer.Length;
                var rc = _wantCompress
                    ? _z.Deflate(_flushMode)
                    : _z.Inflate(_flushMode);
                if (rc != ZlibConstants.Z_OK && rc != ZlibConstants.Z_STREAM_END)
                {
                    throw new ZlibException((_wantCompress ? "de" : "in") + "flating: " + _z.Message);
                }

                //if (_workingBuffer.Length - _z.AvailableBytesOut > 0)
                _stream.Write(_workingBuffer, 0, _workingBuffer.Length - _z.AvailableBytesOut);

                done = _z.AvailableBytesIn == 0 && _z.AvailableBytesOut != 0;
            } while (!done);
        }
 int HandleCompression(byte[] buffer, int count)
 {
     _compressor.InputBuffer      = buffer;
     _compressor.NextOut          = 0;
     _compressor.NextIn           = 0;
     _compressor.AvailableBytesIn = count;
     while (true)
     {
         _compressor.OutputBuffer      = _compressionBuffer;
         _compressor.AvailableBytesOut = _compressionBuffer.Length - _compressor.NextOut;
         var rc = _compressor.Deflate(Ionic.Zlib.FlushType.None);
         if (rc != Ionic.Zlib.ZlibConstants.Z_OK)
         {
             throw new IOException($"Error '{rc}' while compressing the data.");
         }
         if (_compressor.AvailableBytesIn > 0 || _compressor.AvailableBytesOut == 0)
         {
             ResizeBuffer(ref _compressionBuffer);
             continue;
         }
         break;
     }
     while (true)
     {
         _compressor.OutputBuffer      = _compressionBuffer;
         _compressor.AvailableBytesOut = _compressionBuffer.Length - _compressor.NextOut;
         var rc = _compressor.Deflate(Ionic.Zlib.FlushType.Sync);
         if (rc != Ionic.Zlib.ZlibConstants.Z_OK)
         {
             throw new IOException($"Error '{rc}' while compressing the data.");
         }
         if (_compressor.AvailableBytesIn > 0 || _compressor.AvailableBytesOut == 0)
         {
             ResizeBuffer(ref _compressionBuffer);
             continue;
         }
         break;
     }
     return(_compressor.NextOut);
 }
Exemple #8
0
        private static int DeflateOneSegment(WorkItem workitem)
        {
            ZlibCodec compressor = workitem.compressor;

            compressor.ResetDeflate();
            compressor.NextIn = 0;

            compressor.AvailableBytesIn = workitem.inputBytesAvailable;

            // step 1: deflate the buffer
            compressor.NextOut           = 0;
            compressor.AvailableBytesOut = workitem.compressed.Length;
            do
            {
                compressor.Deflate(FlushType.None);
            }while (compressor.AvailableBytesIn > 0 || compressor.AvailableBytesOut == 0);

            // step 2: flush (sync)
            int rc = compressor.Deflate(FlushType.Sync);

            workitem.compressedBytesAvailable = (int)compressor.TotalBytesOut;
            return(rc);
        }
Exemple #9
0
 /// <summary>
 /// Compress the specified inputData.
 /// </summary>
 /// <param name="inputData">Input data.</param>
 /// <returns>compressed byte array</returns>
 public static byte[] Compress(byte[] inputData)
 {
     var zlib = new ZlibCodec(Ionic.Zlib.CompressionMode.Compress);
     zlib.CompressLevel = Ionic.Zlib.CompressionLevel.BestCompression;
     zlib.InputBuffer = inputData;
     zlib.OutputBuffer = new byte[MAX_PACKET_SIZE];
     zlib.NextIn = 0;
     zlib.AvailableBytesIn = inputData.Length;
     zlib.NextOut = 0;
     zlib.AvailableBytesOut = MAX_PACKET_SIZE;
     zlib.Deflate(FlushType.Finish);
     var output = new byte[zlib.TotalBytesOut];
     Array.Copy(zlib.OutputBuffer, output, (int)zlib.TotalBytesOut);
     return output;
 }
Exemple #10
0
        public override void Flush()
        {
            CheckDisposed();

            var ms = (MemoryStream)_outputWriter.BaseStream;

            var buffer = ms.ToArraySegment();

            try
            {
                if (_compression)
                {
                    if (_deflate == null)
                    {
                        _deflate = new Ionic.Zlib.ZlibCodec(Ionic.Zlib.CompressionMode.Compress);
                    }
                    if (_compressionBuffer == null)
                    {
                        _compressionBuffer = new byte[1024 * 1024];
                    }

                    _deflate.OutputBuffer      = _compressionBuffer;
                    _deflate.AvailableBytesOut = _compressionBuffer.Length;
                    _deflate.NextOut           = 0;
                    _deflate.InputBuffer       = buffer.Array;
                    _deflate.AvailableBytesIn  = buffer.Count;
                    _deflate.NextIn            = 0;
                    var rc = _deflate.Deflate(Ionic.Zlib.FlushType.Sync);
                    if (rc != Ionic.Zlib.ZlibConstants.Z_OK)
                    {
                        throw new IOException($"Error '{rc}' while compressing the data.");
                    }
                    if (_deflate.AvailableBytesIn != 0)
                    {
                        throw new IOException("Compression buffer too small.");
                    }
                    buffer = new ArraySegment <byte>(_compressionBuffer, 0, _deflate.NextOut);
                }
                _innerStream.Write(buffer.Array, buffer.Offset, buffer.Count);
                _innerStream.Flush();
                _inputReader.BaseStream.Flush();
            }
            finally
            {
                _outputWriter.BaseStream.SetLength(0);
            }
        }
Exemple #11
0
        internal int SetParams(CompressionLevel level, CompressionStrategy strategy)
        {
            int result = 0;

            if (compressionLevel != level)
            {
                Config config = Config.Lookup(level);
                if (config.Flavor != this.config.Flavor && _codec.TotalBytesIn != 0L)
                {
                    result = _codec.Deflate(FlushType.Partial);
                }
                compressionLevel = level;
                this.config      = config;
                SetDeflater();
            }
            compressionStrategy = strategy;
            return(result);
        }
        private static byte[] ZlibCodecCompress(string textToCompress)
        {
            int outputSize = 2048;
            byte[] output = new Byte[outputSize];
            byte[] uncompressed = UTF8Encoding.UTF8.GetBytes(textToCompress);
            int lengthToCompress = uncompressed.Length;

            // If you want a ZLIB stream, set this to true.  If you want
            // a bare DEFLATE stream, set this to false.
            bool wantRfc1950Header = false;

            using (MemoryStream ms = new MemoryStream())
            {
                ZlibCodec compressor = new ZlibCodec();
                compressor.InitializeDeflate(CompressionLevel.BestCompression, wantRfc1950Header);

                compressor.InputBuffer = uncompressed;
                compressor.AvailableBytesIn = lengthToCompress;
                compressor.NextIn = 0;
                compressor.OutputBuffer = output;

                foreach (var f in new FlushType[] { FlushType.None, FlushType.Finish })
                {
                    int bytesToWrite = 0;
                    do
                    {
                        compressor.AvailableBytesOut = outputSize;
                        compressor.NextOut = 0;
                        compressor.Deflate(f);

                        bytesToWrite = outputSize - compressor.AvailableBytesOut;
                        if (bytesToWrite > 0)
                            ms.Write(output, 0, bytesToWrite);
                    }
                    while ((f == FlushType.None && (compressor.AvailableBytesIn != 0 || compressor.AvailableBytesOut == 0)) ||
                           (f == FlushType.Finish && bytesToWrite != 0));
                }

                compressor.EndDeflate();

                ms.Flush();
                return ms.ToArray();
            }
        }
        private void _FlushFinish()
        {
            // After writing a series of compressed buffers, each one closed
            // with Flush.Sync, we now write the final one as Flush.Finish,
            // and then stop.
            byte[] buffer     = new byte[128];
            var    compressor = new ZlibCodec();
            int    rc         = compressor.InitializeDeflate(_compressLevel, false);

            compressor.InputBuffer       = null;
            compressor.NextIn            = 0;
            compressor.AvailableBytesIn  = 0;
            compressor.OutputBuffer      = buffer;
            compressor.NextOut           = 0;
            compressor.AvailableBytesOut = buffer.Length;
            rc = compressor.Deflate(FlushType.Finish);

            if (rc != ZlibConstants.Z_STREAM_END && rc != ZlibConstants.Z_OK)
            {
                throw new Exception("deflating: " + compressor.Message);
            }

            if (buffer.Length - compressor.AvailableBytesOut > 0)
            {
                TraceOutput(TraceBits.EmitBegin,
                            "Emit     begin    flush bytes({0})",
                            buffer.Length - compressor.AvailableBytesOut);

                _outStream.Write(buffer, 0, buffer.Length - compressor.AvailableBytesOut);

                TraceOutput(TraceBits.EmitDone,
                            "Emit     done     flush");
            }

            compressor.EndDeflate();

            _Crc32 = _runningCrc.Crc32Result;
        }
Exemple #14
0
        private void _FlushFinish()
        {
            byte[]    array     = new byte[128];
            ZlibCodec zlibCodec = new ZlibCodec();
            int       num       = zlibCodec.InitializeDeflate(_compressLevel, wantRfc1950Header: false);

            zlibCodec.InputBuffer       = null;
            zlibCodec.NextIn            = 0;
            zlibCodec.AvailableBytesIn  = 0;
            zlibCodec.OutputBuffer      = array;
            zlibCodec.NextOut           = 0;
            zlibCodec.AvailableBytesOut = array.Length;
            num = zlibCodec.Deflate(FlushType.Finish);
            if (num != 1 && num != 0)
            {
                throw new Exception("deflating: " + zlibCodec.Message);
            }
            if (array.Length - zlibCodec.AvailableBytesOut > 0)
            {
                _outStream.Write(array, 0, array.Length - zlibCodec.AvailableBytesOut);
            }
            zlibCodec.EndDeflate();
            _Crc32 = _runningCrc.Crc32Result;
        }
Exemple #15
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);
        }
        private static ZlibCodec deflate(byte[] buffer, int nMaxInflateBuffer)
        {
            var deflater = new ZlibCodec(CompressionMode.Compress);
            deflater.InitializeDeflate(CompressionLevel.BestCompression);
            deflater.InputBuffer = buffer;
            deflater.AvailableBytesIn = buffer.Length;
            deflater.NextIn = 0;
            //deflater.setInput(buffer, 0, length, false);

            int nMaxDeflateBuffer = nMaxInflateBuffer + 1024;
            var deBuffer = new byte[nMaxDeflateBuffer];
            deflater.OutputBuffer = deBuffer;
            deflater.AvailableBytesOut = deBuffer.Length;
            deflater.NextOut = 0;

            int nResult = deflater.Deflate(FlushType.Finish);
            checkResultStatus(nResult);

            if (deflater.TotalBytesOut > nMaxDeflateBuffer)
            {
                throw new Exception("deflater output buffer was too small");
            }

            return deflater;
        }
        private void _PerpetualWriterMethod(object state)
        {
            TraceOutput(TraceBits.WriterThread, "_PerpetualWriterMethod START");

            try
            {
                do
                {
                    // wait for the next session
                    TraceOutput(TraceBits.Synch | TraceBits.WriterThread, "Synch    _sessionReset.WaitOne(begin) PWM");
                    _sessionReset.WaitOne();
                    TraceOutput(TraceBits.Synch | TraceBits.WriterThread, "Synch    _sessionReset.WaitOne(done)  PWM");

                    if (_isDisposed)
                    {
                        break;
                    }

                    TraceOutput(TraceBits.Synch | TraceBits.WriterThread, "Synch    _sessionReset.Reset()        PWM");
                    _sessionReset.Reset();

                    // repeatedly write buffers as they become ready
                    WorkItem         workitem = null;
                    Ionic.Zlib.CRC32 c        = new Ionic.Zlib.CRC32();
                    do
                    {
                        workitem = _pool[_nextToWrite % _pc];
                        lock (workitem)
                        {
                            if (_noMoreInputForThisSegment)
                            {
                                TraceOutput(TraceBits.Write,
                                            "Write    drain    wi({0}) stat({1}) canuse({2})  cba({3})",
                                            workitem.index,
                                            workitem.status,
                                            (workitem.status == (int)WorkItem.Status.Compressed),
                                            workitem.compressedBytesAvailable);
                            }

                            do
                            {
                                if (workitem.status == (int)WorkItem.Status.Compressed)
                                {
                                    TraceOutput(TraceBits.WriteBegin,
                                                "Write    begin    wi({0}) stat({1})              cba({2})",
                                                workitem.index,
                                                workitem.status,
                                                workitem.compressedBytesAvailable);

                                    workitem.status = (int)WorkItem.Status.Writing;
                                    _outStream.Write(workitem.compressed, 0, workitem.compressedBytesAvailable);
                                    c.Combine(workitem.crc, workitem.inputBytesAvailable);
                                    _totalBytesProcessed += workitem.inputBytesAvailable;
                                    _nextToWrite++;
                                    workitem.inputBytesAvailable = 0;
                                    workitem.status = (int)WorkItem.Status.Done;

                                    TraceOutput(TraceBits.WriteDone,
                                                "Write    done     wi({0}) stat({1})              cba({2})",
                                                workitem.index,
                                                workitem.status,
                                                workitem.compressedBytesAvailable);


                                    Monitor.Pulse(workitem);
                                    break;
                                }
                                else
                                {
                                    int wcycles = 0;
                                    // I've locked a workitem I cannot use.
                                    // Therefore, wake someone else up, and then release the lock.
                                    while (workitem.status != (int)WorkItem.Status.Compressed)
                                    {
                                        TraceOutput(TraceBits.WriteWait,
                                                    "Write    waiting  wi({0}) stat({1}) nw({2}) nf({3}) nomore({4})",
                                                    workitem.index,
                                                    workitem.status,
                                                    _nextToWrite, _nextToFill,
                                                    _noMoreInputForThisSegment);

                                        if (_noMoreInputForThisSegment && _nextToWrite == _nextToFill)
                                        {
                                            break;
                                        }

                                        wcycles++;

                                        // wake up someone else
                                        Monitor.Pulse(workitem);
                                        // release and wait
                                        Monitor.Wait(workitem);

                                        if (workitem.status == (int)WorkItem.Status.Compressed)
                                        {
                                            TraceOutput(TraceBits.WriteWait,
                                                        "Write    A-OK     wi({0}) stat({1}) iba({2}) cba({3}) cyc({4})",
                                                        workitem.index,
                                                        workitem.status,
                                                        workitem.inputBytesAvailable,
                                                        workitem.compressedBytesAvailable,
                                                        wcycles);
                                        }
                                    }

                                    if (_noMoreInputForThisSegment && _nextToWrite == _nextToFill)
                                    {
                                        break;
                                    }
                                }
                            }while (true);
                        }

                        if (_noMoreInputForThisSegment)
                        {
                            TraceOutput(TraceBits.Write,
                                        "Write    nomore  nw({0}) nf({1}) break({2})",
                                        _nextToWrite, _nextToFill, (_nextToWrite == _nextToFill));
                        }

                        if (_noMoreInputForThisSegment && _nextToWrite == _nextToFill)
                        {
                            break;
                        }
                    } while (true);


                    // Finish:
                    // After writing a series of buffers, closing each one with
                    // Flush.Sync, we now write the final one as Flush.Finish, and
                    // then stop.
                    byte[]    buffer     = new byte[128];
                    ZlibCodec compressor = new ZlibCodec();
                    int       rc         = compressor.InitializeDeflate(_compressLevel, false);
                    compressor.InputBuffer       = null;
                    compressor.NextIn            = 0;
                    compressor.AvailableBytesIn  = 0;
                    compressor.OutputBuffer      = buffer;
                    compressor.NextOut           = 0;
                    compressor.AvailableBytesOut = buffer.Length;
                    rc = compressor.Deflate(FlushType.Finish);

                    if (rc != ZlibConstants.Z_STREAM_END && rc != ZlibConstants.Z_OK)
                    {
                        throw new Exception("deflating: " + compressor.Message);
                    }

                    if (buffer.Length - compressor.AvailableBytesOut > 0)
                    {
                        TraceOutput(TraceBits.WriteBegin,
                                    "Write    begin    flush bytes({0})",
                                    buffer.Length - compressor.AvailableBytesOut);

                        _outStream.Write(buffer, 0, buffer.Length - compressor.AvailableBytesOut);

                        TraceOutput(TraceBits.WriteBegin,
                                    "Write    done     flush");
                    }

                    compressor.EndDeflate();

                    _Crc32 = c.Crc32Result;

                    // signal that writing is complete:
                    TraceOutput(TraceBits.Synch, "Synch    _writingDone.Set()           PWM");
                    _writingDone.Set();
                }while (true);
            }
            catch (System.Exception exc1)
            {
                lock (_eLock)
                {
                    // expose the exception to the main thread
                    if (_pendingException != null)
                    {
                        _pendingException = exc1;
                    }
                }
            }

            TraceOutput(TraceBits.WriterThread, "_PerpetualWriterMethod FINIS");
        }
    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);
    }
 private void _PerpetualWriterMethod(object state)
 {
     try
     {
         ZlibCodec zlibCodec;
         while (true)
         {
             _sessionReset.WaitOne();
             if (_isDisposed)
             {
                 return;
             }
             _sessionReset.Reset();
             WorkItem workItem = null;
             CRC32    cRC      = new CRC32();
             bool     flag;
             while (true)
             {
                 workItem = _pool[_nextToWrite % _pc];
                 lock (workItem)
                 {
                     if (_noMoreInputForThisSegment)
                     {
                     }
                     while (true)
                     {
                         if (workItem.status == 4)
                         {
                             workItem.status = 5;
                             _outStream.Write(workItem.compressed, 0, workItem.compressedBytesAvailable);
                             cRC.Combine(workItem.crc, workItem.inputBytesAvailable);
                             _totalBytesProcessed += workItem.inputBytesAvailable;
                             _nextToWrite++;
                             workItem.inputBytesAvailable = 0;
                             workItem.status = 6;
                             Monitor.Pulse(workItem);
                             break;
                         }
                         int num = 0;
                         while (workItem.status != 4 && (!_noMoreInputForThisSegment || _nextToWrite != _nextToFill))
                         {
                             num++;
                             Monitor.Pulse(workItem);
                             Monitor.Wait(workItem);
                             if (workItem.status != 4)
                             {
                             }
                         }
                         if (_noMoreInputForThisSegment && _nextToWrite == _nextToFill)
                         {
                             break;
                         }
                         flag = true;
                     }
                 }
                 if (_noMoreInputForThisSegment)
                 {
                 }
                 if (_noMoreInputForThisSegment && _nextToWrite == _nextToFill)
                 {
                     break;
                 }
                 flag = true;
             }
             byte[] array = new byte[128];
             zlibCodec = new ZlibCodec();
             int num2 = zlibCodec.InitializeDeflate(_compressLevel, wantRfc1950Header: false);
             zlibCodec.InputBuffer       = null;
             zlibCodec.NextIn            = 0;
             zlibCodec.AvailableBytesIn  = 0;
             zlibCodec.OutputBuffer      = array;
             zlibCodec.NextOut           = 0;
             zlibCodec.AvailableBytesOut = array.Length;
             num2 = zlibCodec.Deflate(FlushType.Finish);
             if (num2 != 1 && num2 != 0)
             {
                 break;
             }
             if (array.Length - zlibCodec.AvailableBytesOut > 0)
             {
                 _outStream.Write(array, 0, array.Length - zlibCodec.AvailableBytesOut);
             }
             zlibCodec.EndDeflate();
             _Crc32 = cRC.Crc32Result;
             _writingDone.Set();
             flag = true;
         }
         throw new Exception("deflating: " + zlibCodec.Message);
     }
     catch (Exception pendingException)
     {
         lock (_eLock)
         {
             if (_pendingException != null)
             {
                 _pendingException = pendingException;
             }
         }
     }
 }
        private void _PerpetualWriterMethod(object state)
        {
            TraceOutput(TraceBits.WriterThread, "_PerpetualWriterMethod START");

            try
            {
                do
                {
                    // wait for the next session
                    TraceOutput(TraceBits.Synch | TraceBits.WriterThread, "Synch    _sessionReset.WaitOne(begin) PWM");
                    _sessionReset.WaitOne();
                    TraceOutput(TraceBits.Synch | TraceBits.WriterThread, "Synch    _sessionReset.WaitOne(done)  PWM");

                    if (_isDisposed) break;

                    TraceOutput(TraceBits.Synch | TraceBits.WriterThread, "Synch    _sessionReset.Reset()        PWM");
                    _sessionReset.Reset();

                    // repeatedly write buffers as they become ready
                    WorkItem workitem = null;
                    CRC32 c= new CRC32();
                    do
                    {
                        workitem = _pool[_nextToWrite % _pc];
                        lock(workitem)
                        {
                            if (_noMoreInputForThisSegment)
                                TraceOutput(TraceBits.Write,
                                               "Write    drain    wi({0}) stat({1}) canuse({2})  cba({3})",
                                               workitem.index,
                                               workitem.status,
                                               (workitem.status == (int)WorkItem.Status.Compressed),
                                               workitem.compressedBytesAvailable);

                            do
                            {
                                if (workitem.status == (int)WorkItem.Status.Compressed)
                                {
                                    TraceOutput(TraceBits.WriteBegin,
                                                   "Write    begin    wi({0}) stat({1})              cba({2})",
                                                   workitem.index,
                                                   workitem.status,
                                                   workitem.compressedBytesAvailable);

                                    workitem.status = (int)WorkItem.Status.Writing;
                                    _outStream.Write(workitem.compressed, 0, workitem.compressedBytesAvailable);
                                    c.Combine(workitem.crc, workitem.inputBytesAvailable);
                                    _totalBytesProcessed += workitem.inputBytesAvailable;
                                    _nextToWrite++;
                                    workitem.inputBytesAvailable= 0;
                                    workitem.status = (int)WorkItem.Status.Done;

                                    TraceOutput(TraceBits.WriteDone,
                                                   "Write    done     wi({0}) stat({1})              cba({2})",
                                                   workitem.index,
                                                   workitem.status,
                                                   workitem.compressedBytesAvailable);


                                    Monitor.Pulse(workitem);
                                    break;
                                }
                                else
                                {
                                    int wcycles = 0;
                                    // I've locked a workitem I cannot use.
                                    // Therefore, wake someone else up, and then release the lock.
                                    while (workitem.status != (int)WorkItem.Status.Compressed)
                                    {
                                        TraceOutput(TraceBits.WriteWait,
                                                       "Write    waiting  wi({0}) stat({1}) nw({2}) nf({3}) nomore({4})",
                                                       workitem.index,
                                                       workitem.status,
                                                       _nextToWrite, _nextToFill,
                                                       _noMoreInputForThisSegment );

                                        if (_noMoreInputForThisSegment && _nextToWrite == _nextToFill)
                                            break;

                                        wcycles++;

                                        // wake up someone else
                                        Monitor.Pulse(workitem);
                                        // release and wait
                                        Monitor.Wait(workitem);

                                        if (workitem.status == (int)WorkItem.Status.Compressed)
                                            TraceOutput(TraceBits.WriteWait,
                                                           "Write    A-OK     wi({0}) stat({1}) iba({2}) cba({3}) cyc({4})",
                                                           workitem.index,
                                                           workitem.status,
                                                           workitem.inputBytesAvailable,
                                                           workitem.compressedBytesAvailable,
                                                           wcycles);
                                    }

                                    if (_noMoreInputForThisSegment && _nextToWrite == _nextToFill)
                                        break;

                                }
                            }
                            while (true);
                        }

                        if (_noMoreInputForThisSegment)
                            TraceOutput(TraceBits.Write,
                                           "Write    nomore  nw({0}) nf({1}) break({2})",
                                           _nextToWrite, _nextToFill, (_nextToWrite == _nextToFill));

                        if (_noMoreInputForThisSegment && _nextToWrite == _nextToFill)
                            break;

                    } while (true);


                    // Finish:
                    // After writing a series of buffers, closing each one with
                    // Flush.Sync, we now write the final one as Flush.Finish, and
                    // then stop.
                    byte[] buffer = new byte[128];
                    ZlibCodec compressor = new ZlibCodec();
                    int rc = compressor.InitializeDeflate(_compressLevel, false);
                    compressor.InputBuffer = null;
                    compressor.NextIn = 0;
                    compressor.AvailableBytesIn = 0;
                    compressor.OutputBuffer = buffer;
                    compressor.NextOut = 0;
                    compressor.AvailableBytesOut = buffer.Length;
                    rc = compressor.Deflate(FlushType.Finish);

                    if (rc != ZlibConstants.Z_STREAM_END && rc != ZlibConstants.Z_OK)
                        throw new Exception("deflating: " + compressor.Message);

                    if (buffer.Length - compressor.AvailableBytesOut > 0)
                    {
                        TraceOutput(TraceBits.WriteBegin,
                                       "Write    begin    flush bytes({0})",
                                       buffer.Length - compressor.AvailableBytesOut);

                        _outStream.Write(buffer, 0, buffer.Length - compressor.AvailableBytesOut);

                        TraceOutput(TraceBits.WriteBegin,
                                       "Write    done     flush");
                    }

                    compressor.EndDeflate();

                    _Crc32 = c.Crc32Result;

                    // signal that writing is complete:
                    TraceOutput(TraceBits.Synch, "Synch    _writingDone.Set()           PWM");
                    _writingDone.Set();
                }
                while (true);
            }
            catch (System.Exception exc1)
            {
                lock(_eLock)
                {
                    // expose the exception to the main thread
                    if (_pendingException!=null)
                        _pendingException = exc1;
                }
            }

            TraceOutput(TraceBits.WriterThread, "_PerpetualWriterMethod FINIS");
        }
        private void _FlushFinish()
        {
            // After writing a series of compressed buffers, each one closed
            // with Flush.Sync, we now write the final one as Flush.Finish,
            // and then stop.
            byte[] buffer = new byte[128];
            var compressor = new ZlibCodec();
            int rc = compressor.InitializeDeflate(_compressLevel, false);
            compressor.InputBuffer = null;
            compressor.NextIn = 0;
            compressor.AvailableBytesIn = 0;
            compressor.OutputBuffer = buffer;
            compressor.NextOut = 0;
            compressor.AvailableBytesOut = buffer.Length;
            rc = compressor.Deflate(FlushType.Finish);

            if (rc != ZlibConstants.Z_STREAM_END && rc != ZlibConstants.Z_OK)
                throw new Exception("deflating: " + compressor.Message);

            if (buffer.Length - compressor.AvailableBytesOut > 0)
            {
                TraceOutput(TraceBits.EmitBegin,
                            "Emit     begin    flush bytes({0})",
                            buffer.Length - compressor.AvailableBytesOut);

                _outStream.Write(buffer, 0, buffer.Length - compressor.AvailableBytesOut);

                TraceOutput(TraceBits.EmitDone,
                            "Emit     done     flush");
            }

            compressor.EndDeflate();

            _Crc32 = _runningCrc.Crc32Result;
        }
Exemple #22
0
        private byte[] DeflateBuffer(byte[] b, CompressionLevel level)
        {
            int bufferSize = 1024;
            byte[] buffer = new byte[bufferSize];
            ZlibCodec compressor = new ZlibCodec();

            TestContext.WriteLine("\n============================================");
            TestContext.WriteLine("Size of Buffer to Deflate: {0} bytes.", b.Length);
            MemoryStream ms = new MemoryStream();

            int rc = compressor.InitializeDeflate(level);

            compressor.InputBuffer = b;
            compressor.NextIn = 0;
            compressor.AvailableBytesIn = b.Length;

            compressor.OutputBuffer = buffer;

            for (int pass = 0; pass < 2; pass++)
            {
                FlushType flush = (pass==0)
                    ? FlushType.None
                    : FlushType.Finish;
                do
                {
                    compressor.NextOut = 0;
                    compressor.AvailableBytesOut = buffer.Length;
                    rc = compressor.Deflate(flush);

                    if (rc != ZlibConstants.Z_OK && rc != ZlibConstants.Z_STREAM_END)
                        throw new Exception("deflating: " + compressor.Message);

                    if (buffer.Length - compressor.AvailableBytesOut > 0)
                        ms.Write(compressor.OutputBuffer, 0, buffer.Length - compressor.AvailableBytesOut);
                }
                while (compressor.AvailableBytesIn > 0 || compressor.AvailableBytesOut == 0);
            }

            compressor.EndDeflate();
            Console.WriteLine("TBO({0}).", compressor.TotalBytesOut);

            ms.Seek(0, SeekOrigin.Begin);
            byte[] c = new byte[compressor.TotalBytesOut];
            ms.Read(c, 0, c.Length);
            return c;
        }
    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);
    }
Exemple #24
0
        public void Zlib_BasicDeflateAndInflate()
        {
            string TextToCompress = LoremIpsum;

            int rc;
            int bufferSize = 40000;
            byte[] compressedBytes = new byte[bufferSize];
            byte[] decompressedBytes = new byte[bufferSize];

            ZlibCodec compressingStream = new ZlibCodec();

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

            compressingStream.InputBuffer = System.Text.ASCIIEncoding.ASCII.GetBytes(TextToCompress);
            compressingStream.NextIn = 0;

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

            while (compressingStream.TotalBytesIn != TextToCompress.Length && compressingStream.TotalBytesOut < bufferSize)
            {
                compressingStream.AvailableBytesIn = compressingStream.AvailableBytesOut = 1; // force small buffers
                rc = compressingStream.Deflate(FlushType.None);
                Assert.AreEqual<int>(ZlibConstants.Z_OK, rc, String.Format("at Deflate(1) [{0}]", compressingStream.Message));
            }

            while (true)
            {
                compressingStream.AvailableBytesOut = 1;
                rc = compressingStream.Deflate(FlushType.Finish);
                if (rc == ZlibConstants.Z_STREAM_END)
                    break;
                Assert.AreEqual<int>(ZlibConstants.Z_OK, rc, String.Format("at Deflate(2) [{0}]", compressingStream.Message));
            }

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

            ZlibCodec decompressingStream = new ZlibCodec();

            decompressingStream.InputBuffer = compressedBytes;
            decompressingStream.NextIn = 0;
            decompressingStream.OutputBuffer = decompressedBytes;
            decompressingStream.NextOut = 0;

            rc = decompressingStream.InitializeInflate();
            Assert.AreEqual<int>(ZlibConstants.Z_OK, rc, String.Format("at InitializeInflate() [{0}]", decompressingStream.Message));
            //CheckForError(decompressingStream, rc, "inflateInit");

            while (decompressingStream.TotalBytesOut < decompressedBytes.Length && decompressingStream.TotalBytesIn < bufferSize)
            {
                decompressingStream.AvailableBytesIn = decompressingStream.AvailableBytesOut = 1; /* force small buffers */
                rc = decompressingStream.Inflate(FlushType.None);
                if (rc == ZlibConstants.Z_STREAM_END)
                    break;
                Assert.AreEqual<int>(ZlibConstants.Z_OK, rc, String.Format("at Inflate() [{0}]", decompressingStream.Message));
                //CheckForError(decompressingStream, rc, "inflate");
            }

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

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

            Assert.AreEqual<int>(TextToCompress.Length, j, String.Format("Unequal lengths"));

            int i = 0;
            for (i = 0; i < j; i++)
                if (TextToCompress[i] != decompressedBytes[i])
                    break;

            Assert.AreEqual<int>(j, i, String.Format("Non-identical content"));

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

            TestContext.WriteLine("orig length: {0}", TextToCompress.Length);
            TestContext.WriteLine("compressed length: {0}", compressingStream.TotalBytesOut);
            TestContext.WriteLine("decompressed length: {0}", decompressingStream.TotalBytesOut);
            TestContext.WriteLine("result length: {0}", result.Length);
            TestContext.WriteLine("result of inflate:\n{0}", result);
            return;
        }
Exemple #25
0
        public void Zlib_BasicDictionaryDeflateInflate()
        {
            int rc;
            int comprLen = 40000;
            int uncomprLen = comprLen;
            byte[] uncompr = new byte[uncomprLen];
            byte[] compr = new byte[comprLen];
            //long dictId;

            ZlibCodec compressor = new ZlibCodec();
            rc = compressor.InitializeDeflate(CompressionLevel.BestCompression);
            Assert.AreEqual<int>(ZlibConstants.Z_OK, rc, String.Format("at InitializeDeflate() [{0}]", compressor.Message));

            string dictionaryWord = "hello ";
            byte[] dictionary = System.Text.ASCIIEncoding.ASCII.GetBytes(dictionaryWord);
            string TextToCompress = "hello, hello!  How are you, Joe? I said hello. ";
            byte[] BytesToCompress = System.Text.ASCIIEncoding.ASCII.GetBytes(TextToCompress);

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

            int dictId = compressor.Adler32;

            compressor.OutputBuffer = compr;
            compressor.NextOut = 0;
            compressor.AvailableBytesOut = comprLen;

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

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

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


            ZlibCodec decompressor = new ZlibCodec();

            decompressor.InputBuffer = compr;
            decompressor.NextIn = 0;
            decompressor.AvailableBytesIn = comprLen;

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

            decompressor.OutputBuffer = uncompr;
            decompressor.NextOut = 0;
            decompressor.AvailableBytesOut = uncomprLen;

            while (true)
            {
                rc = decompressor.Inflate(FlushType.None);
                if (rc == ZlibConstants.Z_STREAM_END)
                {
                    break;
                }
                if (rc == ZlibConstants.Z_NEED_DICT)
                {
                    Assert.AreEqual<long>(dictId, decompressor.Adler32, "Unexpected Dictionary");
                    rc = decompressor.SetDictionary(dictionary);
                }
                Assert.AreEqual<int>(ZlibConstants.Z_OK, rc, String.Format("at Inflate/SetInflateDictionary() [{0}]", decompressor.Message));
            }

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

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

            Assert.AreEqual<int>(TextToCompress.Length, j, String.Format("Unequal lengths"));

            int i = 0;
            for (i = 0; i < j; i++)
                if (TextToCompress[i] != uncompr[i])
                    break;

            Assert.AreEqual<int>(j, i, String.Format("Non-identical content"));

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

            TestContext.WriteLine("orig length: {0}", TextToCompress.Length);
            TestContext.WriteLine("compressed length: {0}", compressor.TotalBytesOut);
            TestContext.WriteLine("uncompressed length: {0}", decompressor.TotalBytesOut);
            TestContext.WriteLine("result length: {0}", result.Length);
            TestContext.WriteLine("result of inflate:\n{0}", result);
        }
Exemple #26
0
        public void Zlib_Codec_TestLargeDeflateInflate()
        {
            int rc;
            int j;
            int bufferSize = 80000;
            byte[] compressedBytes = new byte[bufferSize];
            byte[] workBuffer = new byte[bufferSize / 4];

            ZlibCodec compressingStream = new ZlibCodec();

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

            compressingStream.OutputBuffer = compressedBytes;
            compressingStream.AvailableBytesOut = compressedBytes.Length;
            compressingStream.NextOut = 0;
            System.Random rnd = new Random();

            for (int k = 0; k < 4; k++)
            {
                switch (k)
                {
                    case 0:
                        // At this point, workBuffer is all zeroes, so it should compress very well.
                        break;

                    case 1:
                        // switch to no compression, keep same workBuffer (all zeroes):
                        compressingStream.SetDeflateParams(CompressionLevel.None, CompressionStrategy.Default);
                        break;

                    case 2:
                        // Insert data into workBuffer, and switch back to compressing mode.
                        // we'll use lengths of the same random byte:
                        for (int i = 0; i < workBuffer.Length / 1000; i++)
                        {
                            byte b = (byte)rnd.Next();
                            int n = 500 + rnd.Next(500);
                            for (j = 0; j < n; j++)
                                workBuffer[j + i] = b;
                            i += j - 1;
                        }
                        compressingStream.SetDeflateParams(CompressionLevel.BestCompression, CompressionStrategy.Filtered);
                        break;

                    case 3:
                        // insert totally random data into the workBuffer
                        rnd.NextBytes(workBuffer);
                        break;
                }

                compressingStream.InputBuffer = workBuffer;
                compressingStream.NextIn = 0;
                compressingStream.AvailableBytesIn = workBuffer.Length;
                rc = compressingStream.Deflate(FlushType.None);
                Assert.AreEqual<int>(ZlibConstants.Z_OK, rc, String.Format("at Deflate({0}) [{1}]", k, compressingStream.Message));

                if (k == 0)
                    Assert.AreEqual<int>(0, compressingStream.AvailableBytesIn, "Deflate should be greedy.");

                TestContext.WriteLine("Stage {0}: uncompressed/compresssed bytes so far:  ({1,6}/{2,6})",
                      k, compressingStream.TotalBytesIn, compressingStream.TotalBytesOut);
            }

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

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

            TestContext.WriteLine("Final: uncompressed/compressed bytes: ({0,6},{1,6})",
                  compressingStream.TotalBytesIn, 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
            int nCycles = 0;
            while (true)
            {
                decompressingStream.OutputBuffer = workBuffer;
                decompressingStream.NextOut = 0;
                decompressingStream.AvailableBytesOut = workBuffer.Length;
                rc = decompressingStream.Inflate(FlushType.None);

                nCycles++;

                if (rc == ZlibConstants.Z_STREAM_END)
                    break;

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

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

            Assert.AreEqual<int>(4 * workBuffer.Length, (int)decompressingStream.TotalBytesOut);

            TestContext.WriteLine("compressed length: {0}", compressingStream.TotalBytesOut);
            TestContext.WriteLine("decompressed length (expected): {0}", 4 * workBuffer.Length);
            TestContext.WriteLine("decompressed length (actual)  : {0}", decompressingStream.TotalBytesOut);
            TestContext.WriteLine("decompression cycles: {0}", nCycles);
        }
    private void Run()
    {
        int rc;
        int bufferSize = 40000;
        byte[] compressedBytes = new byte[bufferSize];
        byte[] decompressedBytes = new byte[bufferSize];
        
        ZlibCodec compressingStream = new ZlibCodec();
        rc = compressingStream.InitializeDeflate(CompressionLevel.LEVEL9_BEST_COMPRESSION);
        CheckForError(compressingStream, rc, "InitializeDeflate");

        string dictionaryWord = "hello ";
        byte[] dictionary = System.Text.ASCIIEncoding.ASCII.GetBytes(dictionaryWord);
        string TextToCompress = "hello, hello!  How are you, Joe? ";
        byte[] BytesToCompress = System.Text.ASCIIEncoding.ASCII.GetBytes(TextToCompress);

        rc = compressingStream.SetDictionary(dictionary);
        CheckForError(compressingStream, rc, "SetDeflateDictionary");

        long dictId = compressingStream.Adler32;

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

        compressingStream.InputBuffer = BytesToCompress;
        compressingStream.NextIn = 0;
        compressingStream.AvailableBytesIn = BytesToCompress.Length;

        rc = compressingStream.Deflate(ZlibConstants.Z_FINISH);
        if (rc != ZlibConstants.Z_STREAM_END)
        {
            System.Console.Out.WriteLine("deflate should report Z_STREAM_END");
            System.Environment.Exit(1);
        }
        rc = compressingStream.EndDeflate();
        CheckForError(compressingStream, rc, "deflateEnd");

        ZlibCodec decompressingStream = new ZlibCodec();

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

        rc = decompressingStream.InitializeInflate();
        CheckForError(decompressingStream, rc, "inflateInit");
        decompressingStream.OutputBuffer = decompressedBytes;
        decompressingStream.NextOut = 0;
        decompressingStream.AvailableBytesOut = decompressedBytes.Length;

        while (true)
        {
            rc = decompressingStream.Inflate(ZlibConstants.Z_NO_FLUSH);
            if (rc == ZlibConstants.Z_STREAM_END)
            {
                break;
            }
            if (rc == ZlibConstants.Z_NEED_DICT)
            {
                if ((int)decompressingStream.Adler32 != (int)dictId)
                {
                    System.Console.Out.WriteLine("unexpected dictionary");
                    System.Environment.Exit(1);
                }
                rc = decompressingStream.SetDictionary(dictionary);
            }
            CheckForError(decompressingStream, rc, "inflate with dict");
        }

        rc = decompressingStream.EndInflate();
        CheckForError(decompressingStream, 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}", compressingStream.TotalBytesOut);
        Console.WriteLine("decompressed length: {0}", decompressingStream.TotalBytesOut);
        Console.WriteLine("result length: {0}", result.Length);
        Console.WriteLine("result of inflate:\n{0}", result);
    }