/// <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); } }
private void WriteFrame() { Stash32(0x184D2204); FlushStash(); const int versionCode = 0x01; var blockChaining = _descriptor.Chaining; var blockChecksum = _descriptor.BlockChecksum; var contentChecksum = _descriptor.ContentChecksum; var hasContentSize = _descriptor.ContentLength.HasValue; var hasDictionary = _descriptor.Dictionary.HasValue; var FLG = (versionCode << 6) | ((blockChaining ? 0 : 1) << 5) | ((blockChecksum ? 1 : 0) << 4) | ((hasContentSize ? 1 : 0) << 3) | ((contentChecksum ? 1 : 0) << 2) | (hasDictionary ? 1 : 0); var blockSize = _descriptor.BlockSize; var BD = MaxBlockSizeCode(blockSize) << 4; Stash16((ushort)((FLG & 0xFF) | (BD & 0xFF) << 8)); if (hasContentSize) { throw NotImplemented( "ContentSize feature is not implemented"); // Write64(contentSize); } if (hasDictionary) { throw NotImplemented( "Predefined dictionaries feature is not implemented"); // Write32(dictionaryId); } var HC = (byte)(XXH32.DigestOf(_buffer16, 0, _index16) >> 8); Stash8(HC); FlushStash(); _encoder = CreateEncoder(); _buffer = new byte[LZ4Codec.MaximumOutputSize(blockSize)]; }
/// <inheritdoc /> public int Decode(byte *source, int length, int blockSize = 0) { ThrowIfDisposed(); if (blockSize <= 0) { blockSize = _blockSize; } if (blockSize > _blockSize) { throw new InvalidOperationException(); } var decoded = LZ4Codec.Decode(source, length, _outputBuffer, _outputLength); if (decoded < 0) { throw new InvalidOperationException(); } _outputIndex = decoded; return(_outputIndex); }