/// <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);
            }
        }
示例#2
0
        internal static byte[] Encode(byte[] source, LZ4Level level = LZ4Level.L00_FAST)
        {
            unsafe
            {
                int sourceLength = source.Length;

                source.Validate(0, sourceLength);

                fixed (byte* sourceTemp = source)
                {
                    if (sourceLength <= 0) return Mem.Empty;
                    int targetLength1 = sourceLength - 1;
                    byte* target = (byte*) Mem.Alloc(sourceLength);

                    try
                    {
                        int targetLength2 = LZ4Codec.Encode(
                            sourceTemp, sourceLength,
                            target, targetLength1,
                            level);

                        return targetLength2 <= 0
                            ? PickleV0(sourceTemp, sourceLength, sourceLength)
                            : PickleV0(target, targetLength2, sourceLength);
                    }
                    finally
                    {
                        Mem.Free((void*) target);
                    }
                }
            }
        }
 /// <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);
 }
        protected LZ4EncoderBase(int blockSize, int extraBlocks = 0)
        {
            blockSize   = Mem.RoundUp(Math.Max(blockSize, Mem.K1), Mem.K1);
            extraBlocks = Math.Max(extraBlocks, 0);

            _blockSize   = blockSize;
            _inputLength = Mem.K64 + (1 + extraBlocks) * blockSize + 8;
            _inputIndex  = _inputPointer = 0;
            _inputBuffer = (byte *)Mem.Alloc(_inputLength + 8);
        }
示例#5
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 + 32;
            _outputIndex  = 0;
            _outputBuffer = (byte *)Mem.Alloc(_outputLength + 8);
            _context      = LL.LZ4_createStreamDecode();
        }
        /// <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);
        }
        public LZ4Decoder(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));
        }
示例#8
0
        public static LZ4_streamHC_t *LZ4_createStreamHC()
        {
            LZ4_streamHC_t *LZ4_streamHCPtr = (LZ4_streamHC_t *)Mem.Alloc(sizeof(LZ4_streamHC_t));

            if (LZ4_streamHCPtr == null)
            {
                return(null);
            }

            LZ4_initStreamHC(LZ4_streamHCPtr);
            return(LZ4_streamHCPtr);
        }
示例#9
0
        public uint FastStreamManual(int blockLength, int sourceLength)
        {
            sourceLength = Mem.RoundUp(sourceLength, blockLength);
            var targetLength = 2 * sourceLength;

            var context = (LZ4_xx.LZ4_stream_t *)Mem.AllocZero(sizeof(LZ4_xx.LZ4_stream_t));
            var source  = (byte *)Mem.Alloc(sourceLength);
            var target  = (byte *)Mem.Alloc(targetLength);

            try
            {
                Lorem.Fill(source, sourceLength);

                var sourceP = 0;
                var targetP = 0;

                while (sourceP < sourceLength && targetP < targetLength)
                {
                    targetP += LZ4_64.LZ4_compress_fast_continue(
                        context,
                        source + sourceP,
                        target + targetP,
                        Math.Min(blockLength, sourceLength - sourceP),
                        targetLength - targetP,
                        1);
                    sourceP += blockLength;
                }

                return(Tools.Adler32(target, targetP));
            }
            finally
            {
                Mem.Free(context);
                Mem.Free(source);
                Mem.Free(target);
            }
        }
        public uint FastStreamManual(int blockLength, int sourceLength)
        {
            sourceLength = Mem.RoundUp(sourceLength, blockLength);
            var targetLength = 2 * sourceLength;

            var context = new Pubternal.FastContext();
            var source  = (byte *)Mem.Alloc(sourceLength);
            var target  = (byte *)Mem.Alloc(targetLength);

            try
            {
                Lorem.Fill(source, sourceLength);

                var sourceP = 0;
                var targetP = 0;

                while (sourceP < sourceLength && targetP < targetLength)
                {
                    targetP += Pubternal.CompressFast(
                        context,
                        source + sourceP,
                        target + targetP,
                        Math.Min(blockLength, sourceLength - sourceP),
                        targetLength - targetP,
                        1);
                    sourceP += blockLength;
                }

                return(Tools.Adler32(target, targetP));
            }
            finally
            {
                context.Dispose();
                Mem.Free(source);
                Mem.Free(target);
            }
        }
示例#11
0
 public void Setup()
 {
     _source = (byte *)Mem.Alloc(Size);
     _target = (byte *)Mem.Alloc(Size);
 }