Exemplo n.º 1
0
 /// <summary>
 /// Drains decoder by reading all bytes which are ready.
 /// </summary>
 /// <param name="decoder">The decoder instance.</param>
 /// <param name="target">Target buffer.</param>
 /// <param name="targetOffset">Offset within target buffer.</param>
 /// <param name="offset">Offset in decoder relatively to decoder's head. This should be a negative value.</param>
 /// <param name="length">Number of bytes.</param>
 public static unsafe void Drain(this ILZ4Decoder decoder, byte[] target, int targetOffset, int offset, int length)
 {
     fixed(byte *targetPtr = target)
     {
         decoder.Drain(targetPtr + targetOffset, offset, length);
     }
 }
Exemplo n.º 2
0
 /// <summary>Drains decoder by reading all bytes which are ready.</summary>
 /// <param name="decoder">Decoder.</param>
 /// <param name="target">Target buffer.</param>
 /// <param name="offset">Offset in decoder relatively to decoder's head.
 /// Please note, it should be negative value.</param>
 /// <param name="length">Number of bytes.</param>
 public static unsafe void Drain(
     this ILZ4Decoder decoder,
     Span <byte> target,
     int offset, int length)
 {
     fixed(byte *targetP = target)
     decoder.Drain(targetP, offset, length);
 }
 public static unsafe void Drain(
     this ILZ4Decoder decoder,
     byte[] target, int targetIndex,
     int offset, int length)
 {
     fixed(byte *targetP = target)
     decoder.Drain(targetP + targetIndex, offset, length);
 }
Exemplo n.º 4
0
        private void ReadFrame()
        {
            FlushPeek();

            var magic = TryPeek32();

            if (magic != 0x184D2204)
            {
                throw MagicNumberExpected();
            }

            FlushPeek();

            var FLG_BD = Peek16();

            var FLG = FLG_BD & 0xFF;
            var BD  = (FLG_BD >> 8) & 0xFF;

            var version = (FLG >> 6) & 0x11;

            if (version != 1)
            {
                throw UnknownFrameVersion(version);
            }

            var blockChaining   = ((FLG >> 5) & 0x01) == 0;
            var blockChecksum   = ((FLG >> 4) & 0x01) != 0;
            var hasContentSize  = ((FLG >> 3) & 0x01) != 0;
            var contentChecksum = ((FLG >> 2) & 0x01) != 0;
            var hasDictionary   = (FLG & 0x01) != 0;
            var blockSizeCode   = (BD >> 4) & 0x07;

            var contentLength = hasContentSize ? (long?)Peek64() : null;
            var dictionaryId  = hasDictionary ? (uint?)Peek32() : null;

            var actualHC   = (byte)(XXH32.DigestOf(_buffer16, 0, _index16) >> 8);
            var expectedHC = Peek8();

            if (actualHC != expectedHC)
            {
                throw InvalidHeaderChecksum();
            }

            var blockSize = MaxBlockSize(blockSizeCode);

            if (hasDictionary)
            {
                throw NotImplemented(
                          "Predefined dictionaries feature is not implemented");               // Write32(dictionaryId);
            }
            // ReSharper disable once ExpressionIsAlwaysNull
            _frameInfo = new LZ4Descriptor(
                contentLength, contentChecksum, blockChaining, blockChecksum, dictionaryId,
                blockSize);
            _decoder = _decoderFactory(_frameInfo);
            _buffer  = new byte[blockSize];
        }
Exemplo n.º 5
0
 /// <summary>Decodes data and immediately drains it into target buffer.</summary>
 /// <param name="decoder">Decoder.</param>
 /// <param name="source">Source buffer (with compressed data, to be decoded).</param>
 /// <param name="target">Target buffer (to drained into).</param>
 /// <param name="decoded">Number of bytes actually decoded.</param>
 /// <returns><c>true</c> decoder was drained, <c>false</c> otherwise.</returns>
 public static unsafe bool DecodeAndDrain(
     this ILZ4Decoder decoder,
     ReadOnlySpan <byte> source,
     Span <byte> target,
     out int decoded)
 {
     fixed(byte *sourceP = source)
     fixed(byte *targetP = target)
     return(decoder.DecodeAndDrain(
                sourceP, source.Length,
                targetP, target.Length,
                out decoded));
 }
Exemplo n.º 6
0
 /// <summary>Decodes data and immediately drains it into target buffer.</summary>
 /// <param name="decoder">Decoder.</param>
 /// <param name="source">Source buffer (with compressed data, to be decoded).</param>
 /// <param name="sourceOffset">Offset within source buffer.</param>
 /// <param name="sourceLength">Source buffer length.</param>
 /// <param name="target">Target buffer (to drained into).</param>
 /// <param name="targetOffset">Offset within target buffer.</param>
 /// <param name="targetLength">Target buffer length.</param>
 /// <param name="decoded">Number of bytes actually decoded.</param>
 /// <returns><c>true</c> decoder was drained, <c>false</c> otherwise.</returns>
 public static unsafe bool DecodeAndDrain(
     this ILZ4Decoder decoder,
     byte[] source, int sourceOffset, int sourceLength,
     byte[] target, int targetOffset, int targetLength,
     out int decoded)
 {
     fixed(byte *sourceP = source)
     fixed(byte *targetP = target)
     return(decoder.DecodeAndDrain(
                sourceP + sourceOffset,
                sourceLength,
                targetP + targetOffset,
                targetLength,
                out decoded));
 }
Exemplo n.º 7
0
        private void ReadFrame()
        {
            Read0();

            var magic = TryRead32();

            if (magic != 0x184D2204)
            {
                throw MagicNumberExpected();
            }

            Read0();

            var FLG_BD = Read16();

            var FLG = FLG_BD & 0xFF;
            var BD  = (FLG_BD >> 8) & 0xFF;

            var version = (FLG >> 6) & 0x11;

            if (version != 1)
            {
                throw UnknownFrameVersion(version);
            }

            var blockChaining   = ((FLG >> 5) & 0x01) == 0;
            var blockChecksum   = ((FLG >> 4) & 0x01) != 0;
            var hasContentSize  = ((FLG >> 3) & 0x01) != 0;
            var contentChecksum = ((FLG >> 2) & 0x01) != 0;

            var blockSizeCode = (BD >> 4) & 0x07;

            var contentLength = hasContentSize ? (long?)Read64() : null;

            var actualHC   = (byte)(XXH32.DigestOf(_buffer16, 0, _index16) >> 8);
            var expectedHC = Read8();

            if (actualHC != expectedHC)
            {
                throw InvalidHeaderChecksum();
            }

            var blockSize = MaxBlockSize(blockSizeCode);

            _frameInfo = new LZ4FrameInfo(contentLength, contentChecksum, blockChaining, blockChecksum, blockSize);
            _decoder   = _decoderFactory(_frameInfo);
            _buffer    = new byte[blockSize];
        }
Exemplo n.º 8
0
		private void ReadFrame()
		{
			Read0();

			uint? magic = TryRead32();
			if (magic != 0x184D2204)
				throw new InvalidDataException(RS.ExpectLZ4MagicNumber);

            Read0();

			ushort flgBd = Read16();

			int flg = flgBd & 0xFF;
			int bd = (flgBd >> 8) & 0xFF;

			int version = (flg >> 6) & 0x11;

			if (version != 1)
				throw new InvalidDataException(string.Format(RS.LZ4VersionNotSupported, version)); 

			bool blockChaining = ((flg >> 5) & 0x01) == 0;
            bool blockChecksum = ((flg >> 4) & 0x01) != 0;
            bool hasContentSize = ((flg >> 3) & 0x01) != 0;
            bool contentChecksum = ((flg >> 2) & 0x01) != 0;
            bool hasDictionary = (flg & 0x01) != 0;
			int blockSizeCode = (bd >> 4) & 0x07;

			long? contentLength = hasContentSize ? (long?) Read64() : null;
			uint? dictionaryId = hasDictionary ? (uint?) Read32() : null;

			byte actualHC = (byte)(XXHash32.DigestOf(_buffer16, 0, _index16) >> 8);
			byte expectedHC = Read8();

			if (actualHC != expectedHC)
                throw new InvalidDataException(RS.BadLZ4FrameHeaderChecksum);

			int blockSize = MaxBlockSize(blockSizeCode);

			if (hasDictionary)
            {
                // Write32(dictionaryId);
                throw new NotImplementedException(string.Format(RS.FeatureNotImplementedInType, "Predefined Dictionaries", GetType().Name));
            }

			_frameInfo = new LZ4FrameDescriptor(contentLength, contentChecksum, blockChaining, blockChecksum, dictionaryId, blockSize);
			_decoder = _decoderFactory(_frameInfo);
			_buffer = new byte[blockSize];
		}
Exemplo n.º 9
0
        /// <summary>
        /// Decodes data and immediately drains it into target buffer.
        /// </summary>
        /// <param name="decoder">The decoder instance.</param>
        /// <param name="source">Source buffer (with compressed data, to be decoded).</param>
        /// <param name="sourceLength">Source buffer length.</param>
        /// <param name="target">Target buffer (to drained into).</param>
        /// <param name="targetLength">Target buffer length.</param>
        /// <param name="decoded">Number of bytes actually decoded.</param>
        /// <returns>
        /// `true` if the decoder was drained; otherwise, `false`.
        /// </returns>
        public static unsafe bool DecodeAndDrain(this ILZ4Decoder decoder, byte *source, int sourceLength, byte *target, int targetLength, out int decoded)
        {
            decoded = 0;

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

            decoded = decoder.Decode(source, sourceLength);
            if (decoded <= 0 || targetLength < decoded)
            {
                return(false);
            }

            decoder.Drain(target, -decoded, decoded);

            return(true);
        }
Exemplo n.º 10
0
		private void CloseFrame()
		{
			if (_decoder == null)
				return;

			try
			{
				_frameInfo = null;
				_buffer = null;

				// if you need any exceptions throw them here

				_decoder.Dispose();
			}
			finally
			{
				_decoder = null;
			}
		}
Exemplo n.º 11
0
 /// <summary>
 /// Decodes previously compressed block and caches decompressed block in decoder.
 /// Returns number of bytes decoded.
 /// See <see cref="ILZ4Decoder.Decode"/>.
 /// </summary>
 /// <param name="decoder">Decoder.</param>
 /// <param name="buffer">Compressed block.</param>
 /// <param name="offset">Offset in compressed block.</param>
 /// <param name="length">Length of compressed block.</param>
 /// <param name="blockSize">Size of the block. Value <c>0</c> indicates default block size.</param>
 /// <returns>Number of decoded bytes.</returns>
 public static unsafe int Decode(
     this ILZ4Decoder decoder, byte[] buffer, int offset, int length, int blockSize = 0)
 {
     fixed(byte *bufferP = buffer)
     return(decoder.Decode(bufferP + offset, length, blockSize));
 }
Exemplo n.º 12
0
 /// <summary>
 /// Inject already decompressed block and caches it in decoder.
 /// Used with uncompressed-yet-chained blocks and pre-made dictionaries.
 /// See <see cref="ILZ4Decoder.Inject"/>.
 /// </summary>
 /// <param name="decoder">Decoder.</param>
 /// <param name="buffer">Uncompressed block.</param>
 /// <param name="offset">Offset in uncompressed block.</param>
 /// <param name="length">Length of uncompressed block.</param>
 /// <returns>Number of decoded bytes.</returns>
 public static unsafe int Inject(
     this ILZ4Decoder decoder, byte[] buffer, int offset, int length)
 {
     fixed(byte *bufferP = buffer)
     return(decoder.Inject(bufferP + offset, length));
 }