コード例 #1
0
ファイル: Base64Transforms.cs プロジェクト: tkapin/runtime
        public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
        {
            // inputCount < InputBlockSize is not allowed
            ThrowHelper.ValidateTransformBlock(inputBuffer, inputOffset, inputCount, InputBlockSize);

            if (outputBuffer == null)
            {
                ThrowHelper.ThrowArgumentNull(ThrowHelper.ExceptionArgument.outputBuffer);
            }

            // For now, only convert 3 bytes to 4
            Span <byte> input  = inputBuffer.AsSpan(inputOffset, InputBlockSize);
            Span <byte> output = outputBuffer.AsSpan(outputOffset, OutputBlockSize);

            OperationStatus status = Base64.EncodeToUtf8(input, output, out int consumed, out int written, isFinalBlock: false);

            if (written != OutputBlockSize)
            {
                ThrowHelper.ThrowCryptographicException();
            }

            Debug.Assert(status == OperationStatus.NeedMoreData);
            Debug.Assert(consumed == InputBlockSize);

            return(written);
        }
コード例 #2
0
ファイル: Base64Transforms.cs プロジェクト: tkapin/runtime
        public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
        {
            // inputCount != InputBlockSize is allowed
            ThrowHelper.ValidateTransformBlock(inputBuffer, inputOffset, inputCount);

            if (_inputBuffer == null)
            {
                ThrowHelper.ThrowObjectDisposed();
            }

            if (outputBuffer == null)
            {
                ThrowHelper.ThrowArgumentNull(ThrowHelper.ExceptionArgument.outputBuffer);
            }

            // The common case is inputCount = InputBlockSize
            byte[]? tmpBufferArray = null;
            Span <byte> tmpBuffer = stackalloc byte[StackAllocSize];

            if (inputCount > StackAllocSize)
            {
                tmpBuffer = tmpBufferArray = CryptoPool.Rent(inputCount);
            }

            tmpBuffer = GetTempBuffer(inputBuffer.AsSpan(inputOffset, inputCount), tmpBuffer);
            int bytesToTransform = _inputIndex + tmpBuffer.Length;

            // Too little data to decode: save data to _inputBuffer, so it can be transformed later
            if (bytesToTransform < InputBlockSize)
            {
                tmpBuffer.CopyTo(_inputBuffer.AsSpan(_inputIndex));

                _inputIndex = bytesToTransform;

                ReturnToCryptoPool(tmpBufferArray, tmpBuffer.Length);

                return(0);
            }

            ConvertFromBase64(tmpBuffer, outputBuffer.AsSpan(outputOffset), out _, out int written);

            ReturnToCryptoPool(tmpBufferArray, tmpBuffer.Length);

            return(written);
        }
コード例 #3
0
        public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
        {
            ThrowHelper.ValidateTransformBlock(inputBuffer, inputOffset, inputCount);

            int inputBlocks = Math.DivRem(inputCount, InputBlockSize, out int inputRemainder);

            if (inputBlocks == 0)
            {
                ThrowHelper.ThrowArgumentOutOfRange(ThrowHelper.ExceptionArgument.inputCount);
            }

            if (outputBuffer == null)
            {
                ThrowHelper.ThrowArgumentNull(ThrowHelper.ExceptionArgument.outputBuffer);
            }

            if (inputRemainder != 0)
            {
                ThrowHelper.ThrowArgumentOutOfRange(ThrowHelper.ExceptionArgument.inputCount);
            }

            int requiredOutputLength = checked (inputBlocks * OutputBlockSize);

            if (requiredOutputLength > outputBuffer.Length - outputOffset)
            {
                ThrowHelper.ThrowArgumentOutOfRange(ThrowHelper.ExceptionArgument.outputBuffer);
            }

            Span <byte> input  = inputBuffer.AsSpan(inputOffset, inputCount);
            Span <byte> output = outputBuffer.AsSpan(outputOffset, requiredOutputLength);

            OperationStatus status = Base64.EncodeToUtf8(input, output, out int consumed, out int written, isFinalBlock: false);

            Debug.Assert(status == OperationStatus.Done);
            Debug.Assert(consumed == input.Length);
            Debug.Assert(written == output.Length);

            return(written);
        }