예제 #1
0
            public DecompressionSegment Add(ReadOnlyMemory <byte> memory)
            {
                var segment = new DecompressionSegment(memory);

                segment.RunningIndex = RunningIndex + Memory.Length;
                Next = segment;
                return(segment);
            }
예제 #2
0
        /// <inheritdoc />
        public override unsafe ReadOnlySequence <byte> Decompress(ReadOnlySpan <byte> compressedCiphertext)
        {
            fixed(byte *pinnedCompressedCiphertext = compressedCiphertext)
            {
                using var inputStream       = new UnmanagedMemoryStream(pinnedCompressedCiphertext, compressedCiphertext.Length, compressedCiphertext.Length, FileAccess.Read);
                using var compressionStream = CreateDecompressionStream(inputStream);
                var buffer = new byte[Constants.DecompressionBufferLength];
                DecompressionSegment?firstSegment = null;
                DecompressionSegment?segment      = null;
                int uncompressedLength            = 0;
                int readData;

                while ((readData = compressionStream.Read(buffer, 0, Constants.DecompressionBufferLength)) != 0)
                {
                    uncompressedLength += readData;
                    if (firstSegment is null)
                    {
                        firstSegment = new DecompressionSegment(buffer.AsMemory(0, readData));
                    }
                    else
                    {
                        segment = (segment ?? firstSegment).Add(buffer.AsMemory(0, readData));
                    }

                    if (readData < Constants.DecompressionBufferLength)
                    {
                        break;
                    }

                    buffer = new byte[Constants.DecompressionBufferLength];
                }

                if (segment is null)
                {
                    return(new ReadOnlySequence <byte>(buffer.AsMemory(0, readData)));
                }
                else
                {
                    return(new ReadOnlySequence <byte>(firstSegment !, 0, segment, readData));
                }
            }
        }