Ejemplo n.º 1
0
        /// <summary>Compresses input buffer into self-contained package.</summary>
        /// <param name="source">Input buffer.</param>
        /// <param name="sourceLength">Length of input data.</param>
        /// <param name="level">Compression level.</param>
        /// <returns>Output buffer.</returns>
        public static unsafe byte[] Pickle(
            byte *source, int sourceLength, LZ4Level level = LZ4Level.L00_FAST)
        {
            if (sourceLength <= 0)
            {
                return(Mem.Empty);
            }

            var targetLength = sourceLength - 1;
            var target       = (byte *)Mem.Alloc(sourceLength);

            try
            {
                var encodedLength = LZ4Codec.Encode(
                    source, sourceLength, target, targetLength, level);

                return(encodedLength <= 0
                    ? PickleV0(source, sourceLength, sourceLength)
                    : PickleV0(target, encodedLength, sourceLength));
            }
            finally
            {
                Mem.Free(target);
            }
        }
Ejemplo n.º 2
0
 /// <summary>Creates new instance of block decoder.</summary>
 /// <param name="blockSize">Block size. Must be equal or greater to one used for compression.</param>
 public LZ4BlockDecoder(int blockSize)
 {
     blockSize     = Mem.RoundUp(Math.Max(blockSize, Mem.K1), Mem.K1);
     _blockSize    = blockSize;
     _outputLength = _blockSize + 8;
     _outputIndex  = 0;
     _outputBuffer = (byte *)Mem.Alloc(_outputLength + 8);
 }
Ejemplo n.º 3
0
        /// <summary>Creates new instance of encoder.</summary>
        /// <param name="chaining">Needs to be <c>true</c> if using dependent blocks.</param>
        /// <param name="blockSize">Block size.</param>
        /// <param name="extraBlocks">Number of extra blocks.</param>
        protected LZ4EncoderBase(bool chaining, int blockSize, int extraBlocks)
        {
            blockSize   = Mem.RoundUp(Math.Max(blockSize, Mem.K1), Mem.K1);
            extraBlocks = Math.Max(extraBlocks, 0);
            var dictSize = chaining ? Mem.K64 : 0;

            _blockSize   = blockSize;
            _inputLength = dictSize + (1 + extraBlocks) * blockSize + 8;
            _inputIndex  = _inputPointer = 0;
            _inputBuffer = (byte *)Mem.Alloc(_inputLength + 8);
        }
Ejemplo n.º 4
0
        /// <summary>Creates new instance of <see cref="LZ4ChainDecoder"/>.</summary>
        /// <param name="blockSize">Block size.</param>
        /// <param name="extraBlocks">Number of extra blocks.</param>
        public LZ4ChainDecoder(int blockSize, int extraBlocks)
        {
            blockSize   = Mem.RoundUp(Math.Max(blockSize, Mem.K1), Mem.K1);
            extraBlocks = Math.Max(extraBlocks, 0);

            _blockSize    = blockSize;
            _outputLength = Mem.K64 + (1 + extraBlocks) * _blockSize + 8;
            _outputIndex  = 0;
            _outputBuffer = (byte *)Mem.Alloc(_outputLength + 8);
            _context      = (LZ4Context *)Mem.AllocZero(sizeof(LZ4Context));
        }