Example #1
0
        public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
        {
            if (TotalIn == 0 && Mode == CompressionMode.Decompress && inputBuffer[inputOffset] == 0x78 && inputBuffer[inputOffset + 1] == 0x9c)
            {
                inputOffset += 2;
                inputCount  -= 2;
            }

            var read = 0;

            if (TotalOut == 0 && Mode == CompressionMode.Compress)
            {
                outputBuffer[outputOffset]     = 0x78;
                outputBuffer[outputOffset + 1] = 0x9c;
                outputOffset += 2;
                read         += 2;
            }

            var inputBufferHandle = new GCHandle?(GCHandle.Alloc((object)inputBuffer, GCHandleType.Pinned));

            handle.NextIn  = inputBufferHandle.Value.AddrOfPinnedObject() + inputOffset;
            handle.AvailIn = (uint)inputCount;
            var outputBufferHandle = new GCHandle?(GCHandle.Alloc((object)outputBuffer, GCHandleType.Pinned));

            handle.NextOut  = outputBufferHandle.Value.AddrOfPinnedObject() + outputOffset;
            handle.AvailOut = (uint)(outputBuffer.Length - outputOffset);
            ZLibNative.ErrorCode code;
            if (Mode == CompressionMode.Decompress)
            {
                code = handle.Inflate(ZLibNative.FlushCode.PartialFlush);
            }
            else
            {
                code = handle.Deflate(ZLibNative.FlushCode.PartialFlush);
            }

            read += (outputBuffer.Length - outputOffset) - (int)handle.AvailOut;
            return(read);
        }