/// <summary> /// Encodes all bytes currently stored in the encoder into target buffer. /// </summary> /// <param name="encoder">The encoder instance.</param> /// <param name="target">The target buffer.</param> /// <param name="offset">Offset in target buffer.</param> /// <param name="length">Length of target buffer.</param> /// <param name="allowCopy">If `true`, copying bytes is allowed.</param> /// <returns> /// Number of bytes encoded. If bytes were copied than this value is negative. /// </returns> public static unsafe int Encode(this ILZ4Encoder encoder, byte[] target, int offset, int length, bool allowCopy) { fixed(byte *targetPtr = target) { return(encoder.Encode(targetPtr + offset, length, allowCopy)); } }
private void CloseFrame() { if (_encoder == null) { return; } try { var action = _encoder.FlushAndEncode( _buffer, 0, _buffer.Length, true, out var encoded); WriteBlock(encoded, action); Stash32(0); FlushStash(); if (_descriptor.ContentChecksum) { throw NotImplemented("ContentChecksum"); } _buffer = null; _encoder.Dispose(); } finally { _encoder = null; } }
/// <summary> /// Tops encoder up with some data. /// </summary> /// <param name="encoder">The encoder instance.</param> /// <param name="source">The buffer data.</param> /// <param name="offset">Buffer offset, will be increased after operation by the number of bytes actually loaded.</param> /// <param name="length">Length of buffer.</param> /// <returns>`true` if buffer was topped up; or `false` if no bytes were loaded.</returns> public static bool Topup(this ILZ4Encoder encoder, byte[] source, ref int offset, int length) { int loaded = encoder.Topup(source, offset, length); offset += loaded; return(loaded != 0); }
public void CloseFrame() { if (_encoder == null) { return; } try { var action = _encoder.FlushAndEncode( _buffer, 0, _buffer.Length, true, out var encoded); WriteBlock(encoded, action); Write32(0); Flush16(); if (_frameInfo.ContentChecksum) { throw NotImplemented("ContentChecksum"); } _buffer = null; _encoder.Dispose(); } finally { _encoder = null; } }
/// <summary> /// Encoded remaining bytes in the encoder. /// </summary> /// <param name="encoder">The encoder instance.</param> /// <param name="target">Target buffer.</param> /// <param name="targetOffset">Offset within target buffer.</param> /// <param name="targetLength">Target buffer length.</param> /// <param name="allowCopy">Allows to copy bytes if compression was not possible.</param> /// <param name="encoded">Number if bytes encoded or copied. Value is 0 if no encoding was done.</param> /// <returns> /// An <see cref="EncoderAction"/> indicating the action performed. /// </returns> public static unsafe EncoderAction FlushAndEncode(this ILZ4Encoder encoder, byte[] target, int targetOffset, int targetLength, bool allowCopy, out int encoded) { fixed(byte *targetPtr = target) { return(encoder.FlushAndEncode(targetPtr + targetOffset, targetLength, true, allowCopy, 0, out encoded)); } }
/// <summary> /// Tops encoder up with some data. /// </summary> /// <param name="encoder">The encoder instance.</param> /// <param name="source">The buffer data.</param> /// <param name="offset">Offset in the buffer.</param> /// <param name="length">Length of buffer.</param> /// <returns> /// Number of bytes actually loaded. /// </returns> public static unsafe int Topup(this ILZ4Encoder encoder, byte[] source, int offset, int length) { fixed(byte *sourcePtr = source) { return(encoder.Topup(sourcePtr + offset, length)); } }
/// <summary> /// Tops encoder up with some data. /// </summary> /// <param name="encoder">The encoder instance.</param> /// <param name="source">Buffer pointer, will be shifted after operation by the number of bytes actually loaded.</param> /// <param name="length">Length of buffer.</param> /// <returns>`true` if buffer was topped up; or `false` if no bytes were loaded.</returns> public static unsafe bool Topup(this ILZ4Encoder encoder, ref byte *source, int length) { int loaded = encoder.Topup(source, length); source += loaded; return(loaded != 0); }
private void CloseFrame() { if (_encoder == null) return; try { EncoderAction action = _encoder.FlushAndEncode(_buffer, 0, _buffer.Length, true, out int encoded); WriteBlock(encoded, action); Write32(0); Flush16(); if (_descriptor.ContentChecksum) throw new NotImplementedException(string.Format(RS.FeatureNotImplementedInType, "Content Checksum", GetType().Name)); _buffer = null; _encoder.Dispose(); } finally { _encoder = null; } }
private static void Write( BinaryWriter outputWriter, ILZ4Encoder encoder, byte[] inputBuffer, int bytes, byte[] outputBuffer) { var offset = 0; while (bytes > 0) { encoder.TopupAndEncode( inputBuffer, offset, bytes, outputBuffer, 0, outputBuffer.Length, false, false, out var loaded, out var encoded); if (encoded > 0) { outputWriter.Write(encoded); outputWriter.Write(outputBuffer, 0, encoded); } bytes -= loaded; offset += loaded; } }
private static unsafe EncoderAction FlushAndEncode( this ILZ4Encoder encoder, byte *target, int targetLength, bool forceEncode, bool allowCopy, int loaded, out int encoded) { encoded = 0; var blockSize = encoder.BlockSize; var bytesReady = encoder.BytesReady; if (bytesReady < (forceEncode ? 1 : blockSize)) { return(loaded > 0 ? EncoderAction.Loaded : EncoderAction.None); } encoded = encoder.Encode(target, targetLength, allowCopy); if (allowCopy && encoded < 0) { encoded = -encoded; return(EncoderAction.Copied); } else { return(EncoderAction.Encoded); } }
public static bool Topup( this ILZ4Encoder encoder, byte[] source, ref int index, int length) { var loaded = encoder.Topup(source, index, length); index += loaded; return(loaded != 0); }
private ILZ4Encoder CreateEncoder() { ILZ4Encoder encoder = _encoderFactory(_descriptor); if (encoder.BlockSize > _descriptor.BlockSize) throw new ArgumentException(RS.BlockSizeLargerThanExpected); return encoder; }
public static unsafe bool Encode( this ILZ4Encoder encoder, ref byte *target, int length, bool allowCopy) { var encoded = encoder.Encode(target, length, allowCopy); target += encoded; return(encoded != 0); }
public static bool Encode( this ILZ4Encoder encoder, byte[] target, ref int index, int length, bool allowCopy) { var encoded = encoder.Encode(target, index, length, allowCopy); index += encoded; return(encoded != 0); }
/// <summary> /// Encodes all bytes currently stored in encoder into target buffer. /// </summary> /// <param name="encoder">The encoder instance.</param> /// <param name="target">Target buffer. Will be updated after operation.</param> /// <param name="length">Length of buffer.</param> /// <param name="allowCopy">If `true`, copying bytes is allowed.</param> /// <returns> /// Result of this action. Bytes can be copied (<see cref="EncoderAction.Copied"/>), /// encoded (<see cref="EncoderAction.Encoded"/>) or nothing could have /// happened (<see cref="EncoderAction.None"/>). /// </returns> public static unsafe EncoderAction Encode(this ILZ4Encoder encoder, ref byte *target, int length, bool allowCopy) { int encoded = encoder.Encode(target, length, allowCopy); target += Math.Abs(encoded); return(encoded == 0 ? EncoderAction.None : encoded < 0 ? EncoderAction.Copied : EncoderAction.Encoded); }
/// <summary>Encoded remaining bytes in encoder.</summary> /// <param name="encoder">Encoder.</param> /// <param name="target">Target buffer.</param> /// <param name="allowCopy">Allows to copy bytes if compression was not possible.</param> /// <param name="encoded">Number if bytes encoded or copied. /// Value is 0 if no encoding was done.</param> /// <returns>Action performed.</returns> public static unsafe EncoderAction FlushAndEncode( this ILZ4Encoder encoder, Span <byte> target, bool allowCopy, out int encoded) { fixed(byte *targetP = target) return(encoder.FlushAndEncode( targetP, target.Length, true, allowCopy, 0, out encoded)); }
/// <summary> /// Encodes all bytes currently stored in encoder into target buffer. /// </summary> /// <param name="encoder">The encoder instance.</param> /// <param name="target">Target buffer.</param> /// <param name="offset">Offset in target buffer. Will be updated after operation.</param> /// <param name="length">Length of target buffer.</param> /// <param name="allowCopy">If `true`, copying bytes is allowed.</param> /// <returns> /// Result of this action. Bytes can be copied (<see cref="EncoderAction.Copied"/>), /// encoded (<see cref="EncoderAction.Encoded"/>) or nothing could have /// happened (<see cref="EncoderAction.None"/>). /// </returns> public static EncoderAction Encode(this ILZ4Encoder encoder, byte[] target, ref int offset, int length, bool allowCopy) { int encoded = encoder.Encode(target, offset, length, allowCopy); offset += Math.Abs(encoded); return(encoded == 0 ? EncoderAction.None : encoded < 0 ? EncoderAction.Copied : EncoderAction.Encoded); }
private static void Flush(BinaryWriter outputWriter, ILZ4Encoder encoder, byte[] outputBuffer) { if (encoder.BytesReady <= 0) { return; } var encoded = encoder.Encode(outputBuffer, 0, outputBuffer.Length, false); outputWriter.Write(encoded); outputWriter.Write(outputBuffer, 0, encoded); }
/// <summary> /// Tops encoder and encodes content. /// </summary> /// <param name="encoder">The encoder instance.</param> /// <param name="source">Source buffer (used to top up from).</param> /// <param name="sourceOffset">Offset within source buffer.</param> /// <param name="sourceLength">Source buffer length.</param> /// <param name="target">Target buffer (used to encode into).</param> /// <param name="targetOffset">Offset within target buffer.</param> /// <param name="targetLength">Target buffer length.</param> /// <param name="forceEncode">Forces encoding even if encoder is not full.</param> /// <param name="allowCopy">Allows to copy bytes if compression was not possible.</param> /// <param name="loaded">Number of bytes loaded (topped up).</param> /// <param name="encoded">Number if bytes encoded or copied. Value is 0 if no encoding was done.</param> /// <returns> /// An <see cref="EncoderAction"/> indicating the action performed. /// </returns> public static unsafe EncoderAction TopupAndEncode(this ILZ4Encoder encoder, byte[] source, int sourceOffset, int sourceLength, byte[] target, int targetOffset, int targetLength, bool forceEncode, bool allowCopy, out int loaded, out int encoded) { fixed(byte *sourcePtr = source) fixed(byte *targetPtr = target) { return(encoder.TopupAndEncode( sourcePtr + sourceOffset, sourceLength, targetPtr + targetOffset, targetLength, forceEncode, allowCopy, out loaded, out encoded)); } }
/// <summary> /// Tops encoder and encodes content. /// </summary> /// <param name="encoder">The encoder instance.</param> /// <param name="source">Source buffer (used to top up from).</param> /// <param name="sourceLength">Source buffer length.</param> /// <param name="target">Target buffer (used to encode into).</param> /// <param name="targetLength">Target buffer length.</param> /// <param name="forceEncode">Forces encoding even if encoder is not full.</param> /// <param name="allowCopy">Allows to copy bytes if compression was not possible.</param> /// <param name="loaded">Number of bytes loaded (topped up).</param> /// <param name="encoded">Number if bytes encoded or copied. Value is 0 if no encoding was done.</param> /// <returns> /// An <see cref="EncoderAction"/> indicating the action performed. /// </returns> public static unsafe EncoderAction TopupAndEncode(this ILZ4Encoder encoder, byte *source, int sourceLength, byte *target, int targetLength, bool forceEncode, bool allowCopy, out int loaded, out int encoded) { loaded = 0; encoded = 0; if (sourceLength > 0) { loaded = encoder.Topup(source, sourceLength); } return(encoder.FlushAndEncode(target, targetLength, forceEncode, allowCopy, loaded, out encoded)); }
private bool TryStashFrame() { if (_encoder != null) { return(false); } Stash.Stash4(0x184D2204); var headerOffset = Stash.Length; 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; Stash.Stash2((ushort)((FLG & 0xFF) | (BD & 0xFF) << 8)); if (hasContentSize) { throw NotImplemented( "ContentSize feature is not implemented"); // Stash8(contentSize); } if (hasDictionary) { throw NotImplemented( "Predefined dictionaries feature is not implemented"); // Stash4(dictionaryId); } var HC = (byte)(DigestOfStash(headerOffset) >> 8); Stash.Stash1(HC); _encoder = CreateEncoder(); _buffer = new byte[LZ4Codec.MaximumOutputSize(blockSize)]; return(true); }
/// <summary>Tops encoder and encodes content.</summary> /// <param name="encoder">Encoder.</param> /// <param name="source">Source buffer (used to top up from).</param> /// <param name="target">Target buffer (used to encode into)</param> /// <param name="forceEncode">Forces encoding even if encoder is not full.</param> /// <param name="allowCopy">Allows to copy bytes if compression was not possible.</param> /// <param name="loaded">Number of bytes loaded (topped up)</param> /// <param name="encoded">Number if bytes encoded or copied. /// Value is 0 if no encoding was done.</param> /// <returns>Action performed.</returns> public static unsafe EncoderAction TopupAndEncode( this ILZ4Encoder encoder, ReadOnlySpan <byte> source, Span <byte> target, bool forceEncode, bool allowCopy, out int loaded, out int encoded) { fixed(byte *sourceP = source) fixed(byte *targetP = target) return(encoder.TopupAndEncode( sourceP, source.Length, targetP, target.Length, forceEncode, allowCopy, out loaded, out encoded)); }
private BlockInfo FlushAndEncode() { var action = _encoder.FlushAndEncode( _buffer.AsSpan(), true, out var encoded); try { _encoder.Dispose(); return(new BlockInfo(_buffer, action, encoded)); } finally { _encoder = null; _buffer = null; } }
private void WriteFrame() { Write32(0x184D2204); Flush16(); const int versionCode = 0x01; bool blockChaining = _descriptor.Chaining; bool blockChecksum = _descriptor.BlockChecksum; bool contentChecksum = _descriptor.ContentChecksum; bool hasContentSize = _descriptor.ContentLength.HasValue; bool hasDictionary = _descriptor.Dictionary.HasValue; int flg = (versionCode << 6) | ((blockChaining ? 0 : 1) << 5) | ((blockChecksum ? 1 : 0) << 4) | ((hasContentSize ? 1 : 0) << 3) | ((contentChecksum ? 1 : 0) << 2) | (hasDictionary ? 1 : 0); int blockSize = _descriptor.BlockSize; int bd = MaxBlockSizeCode(blockSize) << 4; Write16((ushort) ((flg & 0xFF) | (bd & 0xFF) << 8)); if (hasContentSize) { // Write64(contentSize) throw new NotImplementedException(string.Format(RS.FeatureNotImplementedInType, "Content Size", GetType().Name)); } if (hasDictionary) { // Write32(dictionaryId) throw new NotImplementedException(string.Format(RS.FeatureNotImplementedInType, "Predefined Dictionaries", GetType().Name)); } byte hc = (byte)(XXHash32.DigestOf(_buffer16, 0, _index16) >> 8); Write8(hc); Flush16(); _encoder = CreateEncoder(); _buffer = new byte[LZ4Codec.MaximumOutputSize(blockSize)]; }
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)]; }
public static unsafe int Topup( this ILZ4Encoder encoder, byte[] source, int index, int length) { fixed(byte *sourceP = source) return(encoder.Topup(sourceP + index, length)); }
public static unsafe int Encode( this ILZ4Encoder encoder, byte[] target, int index, int length, bool allowCopy) { fixed(byte *targetP = target) return(encoder.Encode(targetP + index, length, allowCopy)); }
/// <summary>Encoded remaining bytes in encoder.</summary> /// <param name="encoder">Encoder.</param> /// <param name="target">Target buffer.</param> /// <param name="targetLength">Target buffer length.</param> /// <param name="allowCopy">Allows to copy bytes if compression was not possible.</param> /// <param name="encoded">Number if bytes encoded or copied. /// Value is 0 if no encoding was done.</param> /// <returns>Action performed.</returns> public static unsafe EncoderAction FlushAndEncode( this ILZ4Encoder encoder, byte *target, int targetLength, bool allowCopy, out int encoded) => encoder.FlushAndEncode(target, targetLength, true, allowCopy, 0, out encoded);
public LZ4Encoder(LZ4Level level, int blockSize, int extraBlocks) { _encoder = Create(level, blockSize, extraBlocks); }