/// <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); }
public uint FastStreamEncoder(int blockLength, int sourceLength, int extraBlocks = 0) { sourceLength = LZ4MemoryHelper.RoundUp(sourceLength, blockLength); var targetLength = 2 * sourceLength; var source = new byte[sourceLength]; var target = new byte[targetLength]; Lorem.Fill(source, 0, source.Length); using (var encoder = new LZ4FastChainEncoder(blockLength, extraBlocks)) { var sourceP = 0; var targetP = 0; while (sourceP < sourceLength && targetP < targetLength) { encoder.TopupAndEncode( source, sourceP, Math.Min(blockLength, sourceLength - sourceP), target, targetP, targetLength - targetP, true, false, out var loaded, out var encoded); sourceP += loaded; targetP += encoded; } return(Tools.Adler32(target, 0, targetP)); } }
/// <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); }
/// <summary> /// Creates new instance of the <see cref="LZ4BlockDecoder"/> class. /// </summary> /// <param name="blockSize">Block size. Must be equal or greater to one used for compression.</param> public LZ4BlockDecoder(int blockSize) { blockSize = LZ4MemoryHelper.RoundUp(Math.Max(blockSize, LZ4MemoryHelper.K1), LZ4MemoryHelper.K1); _blockSize = blockSize; _outputLength = _blockSize + 8; _outputIndex = 0; _outputBuffer = (byte *)LZ4MemoryHelper.Alloc(_outputLength + 8); }
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); }
/// <summary> /// Creates a new instance of the <see cref="LZ4Encoder"/> class. /// </summary> /// <param name="chaining">Needs to be `true` if using dependent blocks.</param> /// <param name="blockSize">Block size.</param> /// <param name="extraBlocks">Number of extra blocks.</param> protected LZ4Encoder(bool chaining, int blockSize, int extraBlocks) { blockSize = LZ4MemoryHelper.RoundUp(Math.Max(blockSize, LZ4MemoryHelper.K1), LZ4MemoryHelper.K1); extraBlocks = Math.Max(extraBlocks, 0); int dictSize = chaining ? LZ4MemoryHelper.K64 : 0; _blockSize = blockSize; _inputLength = dictSize + (1 + extraBlocks) * blockSize + 8; _inputIndex = _inputPointer = 0; _inputBuffer = (byte *)LZ4MemoryHelper.Alloc(_inputLength + 8); }
/// <summary> /// Creates new instance of the <see cref="LZ4ChainDecoder"/> class. /// </summary> /// <param name="blockSize">Block size.</param> /// <param name="extraBlocks">Number of extra blocks.</param> public LZ4ChainDecoder(int blockSize, int extraBlocks) { blockSize = LZ4MemoryHelper.RoundUp(Math.Max(blockSize, LZ4MemoryHelper.K1), LZ4MemoryHelper.K1); extraBlocks = Math.Max(extraBlocks, 0); _blockSize = blockSize; _outputLength = LZ4MemoryHelper.K64 + (1 + extraBlocks) * _blockSize + 8; _outputIndex = 0; _outputBuffer = (byte *)LZ4MemoryHelper.Alloc(_outputLength + 8); _context = (LZ4Context *)LZ4MemoryHelper.AllocZero(sizeof(LZ4Context)); }
/// <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); }
/// <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); }
/// <summary> /// Creates a new instance of the <see cref="LZ4HighChainEncoder"/> class. /// </summary> /// <param name="level">Compression level.</param> /// <param name="blockSize">Block size.</param> /// <param name="extraBlocks">Number of extra blocks.</param> public LZ4HighChainEncoder(LZ4CompressionLevel level, int blockSize, int extraBlocks = 0) : base(true, blockSize, extraBlocks) { if (level < LZ4CompressionLevel.Level3) { level = LZ4CompressionLevel.Level3; } if (level > LZ4CompressionLevel.Level12) { level = LZ4CompressionLevel.Level12; } _context = (LZ4Context *)LZ4MemoryHelper.AllocZero(sizeof(LZ4Context)); LZ4Engine64HC.ResetStreamHC(_context, (int)level); }
/// <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); }
/// <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); }
public uint FastStreamManual(int blockLength, int sourceLength) { sourceLength = LZ4MemoryHelper.RoundUp(sourceLength, blockLength); var targetLength = 2 * sourceLength; var context = (LZ4Engine.StreamT *)LZ4MemoryHelper.AllocZero(sizeof(LZ4Engine.StreamT)); var source = (byte *)LZ4MemoryHelper.Alloc(sourceLength); var target = (byte *)LZ4MemoryHelper.Alloc(targetLength); try { Lorem.Fill(source, sourceLength); var sourceP = 0; var targetP = 0; while (sourceP < sourceLength && targetP < targetLength) { targetP += LZ4Engine64.CompressFastContinue( context, source + sourceP, target + targetP, Math.Min(blockLength, sourceLength - sourceP), targetLength - targetP, 1); sourceP += blockLength; } return(Tools.Adler32(target, targetP)); } finally { LZ4MemoryHelper.Free(context); LZ4MemoryHelper.Free(source); LZ4MemoryHelper.Free(target); } }
/// <see cref="UnmanagedEncodingResource.ReleaseUnmanaged()"/> protected override void ReleaseUnmanaged() { base.ReleaseUnmanaged(); LZ4MemoryHelper.Free(_context); }
/// <see cref="UnmanagedEncodingResource.ReleaseUnmanaged()"/> protected override void ReleaseUnmanaged() { base.ReleaseUnmanaged(); LZ4MemoryHelper.Free(_inputBuffer); }
/// <summary> /// Creates a new instance of the <see cref="LZ4FastChainEncoder"/> class. /// </summary> /// <param name="blockSize">Block size.</param> /// <param name="extraBlocks">Number of extra blocks.</param> public LZ4FastChainEncoder(int blockSize, int extraBlocks = 0) : base(true, blockSize, extraBlocks) { _context = (LZ4Context *)LZ4MemoryHelper.AllocZero(sizeof(LZ4Context)); }