Exemplo n.º 1
0
        private unsafe int ReadBlock()
        {
            FlushPeek();

            var blockLength = (int)Peek32();

            if (blockLength == 0)
            {
                if (_frameInfo.ContentChecksum)
                {
                    Peek32();
                }
                CloseFrame();
                return(0);
            }

            var uncompressed = (blockLength & 0x80000000) != 0;

            blockLength &= 0x7FFFFFFF;

            PeekN(_buffer, 0, blockLength);

            if (_frameInfo.BlockChecksum)
            {
                Peek32();

                fixed(byte *bufferP = _buffer)
                return(uncompressed
                                        ? _decoder.Inject(bufferP, blockLength)
                                        : _decoder.Decode(bufferP, blockLength));
        }
Exemplo n.º 2
0
		private unsafe int ReadBlock()
		{
			Read0();

			int blockLength = (int) Read32();
			if (blockLength == 0)
			{
				if (_frameInfo.ContentChecksum)
					Read32();
				CloseFrame();
				return 0;
			}

			bool uncompressed = (blockLength & 0x80000000) != 0;
			blockLength &= 0x7FFFFFFF;

			ReadN(_buffer, 0, blockLength);

			if (_frameInfo.BlockChecksum)
				Read32();

			fixed (byte* bufferPtr = _buffer)
            {
                return uncompressed
                    ? _decoder.Inject(bufferPtr, blockLength)
                    : _decoder.Decode(bufferPtr, blockLength);
            }
        }
Exemplo n.º 3
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.º 4
0
        private unsafe int ReadBlock()
        {
            Read0();

            var blockLength = (int)Read32();

            if (blockLength == 0)
            {
                if (_frameInfo.ContentChecksum)
                {
                    Read32();
                }
                CloseFrame();
                return(0);
            }

            var uncompressed = (blockLength & 0x80000000) != 0;

            blockLength &= 0x7FFFFFFF;

            // keep reading from the stream until it gives us every byte we've asked for
            for (var blockCursor = 0; blockCursor < blockLength;)
            {
                var advance = _inner.Read(_buffer, blockCursor, blockLength - blockCursor);
                if (advance == 0)
                {
                    break;                     // Note: this probably means the stream is truncated and could just be a throw instead
                }
                blockCursor += advance;
            }

            if (_frameInfo.BlockChecksum)
            {
                Read32();

                fixed(byte *bufferP = _buffer)
                return(uncompressed
                                        ? _decoder.Inject(bufferP, blockLength)
                                        : _decoder.Decode(bufferP, blockLength));
        }
Exemplo n.º 5
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));
 }
 private int InjectOrDecode(int blockLength, bool uncompressed) =>
 uncompressed
                         ? _decoder.Inject(_buffer, 0, blockLength)
                         : _decoder.Decode(_buffer, 0, blockLength);