Example #1
0
        private static async Task <Stream> TrimStreamInternal(
            Stream stream,
            HttpRange originalRange,
            ContentRange?receivedRange,
            // iv or nonce in stream could have already been trimmed during decryption
            int alreadyTrimmedOffsetAmount,
            bool async,
            CancellationToken cancellationToken)
        {
            // retrim start of stream to original requested location
            // keeping in mind whether we already trimmed due to an IV or nonce
            int gap = (int)(originalRange.Offset - (receivedRange?.Start ?? 0)) - alreadyTrimmedOffsetAmount;

            int read = 0;

            while (gap > read)
            {
                int toRead = gap - read;
                // throw away initial bytes we want to trim off; stream cannot seek into future
                if (async)
                {
                    read += await stream.ReadAsync(new byte[toRead], 0, toRead, cancellationToken).ConfigureAwait(false);
                }
                else
                {
                    read += stream.Read(new byte[toRead], 0, toRead);
                }
            }

            if (originalRange.Length.HasValue)
            {
                stream = WindowStream.GetWindow(stream, originalRange.Length.Value);
            }

            return(stream);
        }
        private static async Task <Stream> TrimStreamInternal(
            Stream stream,
            HttpRange originalRange,
            ContentRange?receivedRange,
            bool pulledOutIV,
            bool async,
            CancellationToken cancellationToken)
        {
            // retrim start of stream to original requested location
            // keeping in mind whether we already pulled the IV out of the stream as well
            int gap = (int)(originalRange.Offset - (receivedRange?.Start ?? 0))
                      - (pulledOutIV ? Constants.ClientSideEncryption.EncryptionBlockSize : 0);

            int read = 0;

            while (gap > read)
            {
                int toRead = gap - read;
                // throw away initial bytes we want to trim off; stream cannot seek into future
                if (async)
                {
                    read += await stream.ReadAsync(new byte[toRead], 0, toRead, cancellationToken).ConfigureAwait(false);
                }
                else
                {
                    read += stream.Read(new byte[toRead], 0, toRead);
                }
            }

            if (originalRange.Length.HasValue)
            {
                stream = new WindowStream(stream, originalRange.Length.Value);
            }

            return(stream);
        }