示例#1
0
        /// <see cref="ILZ4Decoder.Inject(byte*, int)"/>
        public int Inject(byte *source, int length)
        {
            if (length <= 0)
            {
                return(0);
            }

            if (length > Math.Max(_blockSize, LZ4MemoryHelper.K64))
            {
                throw new InvalidOperationException();
            }

            if (_outputIndex + length < _outputLength)
            {
                LZ4MemoryHelper.Move(_outputBuffer + _outputIndex, source, length);
                _outputIndex = ApplyDict(_outputIndex + length);
            }
            else if (length >= LZ4MemoryHelper.K64)
            {
                LZ4MemoryHelper.Move(_outputBuffer, source, length);
                _outputIndex = ApplyDict(length);
            }
            else
            {
                int tailSize = Math.Min(LZ4MemoryHelper.K64 - length, _outputIndex);
                LZ4MemoryHelper.Move(_outputBuffer, _outputBuffer + _outputIndex - tailSize, tailSize);
                LZ4MemoryHelper.Move(_outputBuffer + tailSize, source, length);
                _outputIndex = ApplyDict(tailSize + length);
            }

            return(length);
        }
示例#2
0
        /// <see cref="ILZ4Encoder.Encode(byte*, int, bool)"/>
        public int Encode(byte *target, int length, bool allowCopy)
        {
            ThrowIfDisposed();

            int sourceLength = _inputPointer - _inputIndex;

            if (sourceLength <= 0)
            {
                return(0);
            }

            int encoded = EncodeBlock(_inputBuffer + _inputIndex, sourceLength, target, length);

            if (encoded <= 0)
            {
                throw new InvalidOperationException(RS.EncodeChunkTargetBufferTooSmall);
            }

            if (allowCopy && encoded >= sourceLength)
            {
                LZ4MemoryHelper.Move(target, _inputBuffer + _inputIndex, sourceLength);
                encoded = -sourceLength;
            }

            Commit();

            return(encoded);
        }
示例#3
0
        private int CopyDict(int index)
        {
            int dictStart = Math.Max(index - LZ4MemoryHelper.K64, 0);
            int dictSize  = index - dictStart;

            LZ4MemoryHelper.Move(_outputBuffer, _outputBuffer + dictStart, dictSize);
            LZ4Engine.SetStreamDecode(_context, _outputBuffer, dictSize);
            return(dictSize);
        }
示例#4
0
        /// <see cref="ILZ4Decoder.Drain(byte*, int, int)"/>
        public void Drain(byte *target, int offset, int length)
        {
            offset = _outputIndex + offset;             // negative value!

            // #todo more helpful error required!
            if (offset < 0 || length < 0 || offset + length > _outputIndex)
            {
                throw new InvalidOperationException();
            }

            LZ4MemoryHelper.Move(target, _outputBuffer + offset, length);
        }
示例#5
0
        /// <see cref="ILZ4Decoder.Drain(byte*, int, int)"/>
        public void Drain(byte *target, int offset, int length)
        {
            ThrowIfDisposed();

            offset = _outputIndex + offset;             // negative value!
            if (offset < 0 || length < 0 || offset + length > _outputIndex)
            {
                throw new InvalidOperationException();
            }

            LZ4MemoryHelper.Move(target, _outputBuffer + offset, length);
        }
示例#6
0
        /// <see cref="ILZ4Decoder.Inject(byte*, int)"/>
        public int Inject(byte *source, int length)
        {
            ThrowIfDisposed();

            if (length <= 0)
            {
                return(_outputIndex = 0);
            }

            if (length > _outputLength)
            {
                throw new InvalidOperationException();
            }

            LZ4MemoryHelper.Move(_outputBuffer, source, length);
            _outputIndex = length;
            return(length);
        }
示例#7
0
        /// <see cref="ILZ4Encoder.Topup(byte*, int)"/>
        public int Topup(byte *source, int length)
        {
            ThrowIfDisposed();

            if (length == 0)
            {
                return(0);
            }

            int spaceLeft = _inputIndex + _blockSize - _inputPointer;

            if (spaceLeft <= 0)
            {
                return(0);
            }

            int chunk = Math.Min(spaceLeft, length);

            LZ4MemoryHelper.Move(_inputBuffer + _inputPointer, source, chunk);
            _inputPointer += chunk;

            return(chunk);
        }