Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches

◆ WriteAsyncCore()

async ValueTask System.Security.Cryptography.CryptoStream.WriteAsyncCore ( ReadOnlyMemory< byte > buffer,
CancellationToken cancellationToken,
bool useAsync )
inlineprivate

Definition at line 448 of file CryptoStream.cs.

449 {
450 int bytesToWrite = buffer.Length;
451 int currentInputIndex = 0;
452 if (_inputBufferIndex > 0)
453 {
455 {
456 buffer.CopyTo(_inputBuffer.AsMemory(_inputBufferIndex));
457 _inputBufferIndex += buffer.Length;
458 return;
459 }
461 currentInputIndex += _inputBlockSize - _inputBufferIndex;
462 bytesToWrite -= _inputBlockSize - _inputBufferIndex;
464 }
466 {
468 if (useAsync)
469 {
470 await _stream.WriteAsync(new ReadOnlyMemory<byte>(_outputBuffer, 0, numOutputBytes2), cancellationToken).ConfigureAwait(continueOnCapturedContext: false);
471 }
472 else
473 {
474 _stream.Write(_outputBuffer, 0, numOutputBytes2);
475 }
477 }
478 while (bytesToWrite > 0)
479 {
480 if (bytesToWrite >= _inputBlockSize)
481 {
482 int num = bytesToWrite / _inputBlockSize;
484 {
485 int numWholeBlocksInBytes = num * _inputBlockSize;
486 byte[] tempOutputBuffer = ArrayPool<byte>.Shared.Rent(checked(num * _outputBlockSize));
487 int numOutputBytes2 = 0;
488 try
489 {
490 numOutputBytes2 = TransformBlock(_transform, buffer.Slice(currentInputIndex, numWholeBlocksInBytes), tempOutputBuffer, 0);
491 if (useAsync)
492 {
493 await _stream.WriteAsync(new ReadOnlyMemory<byte>(tempOutputBuffer, 0, numOutputBytes2), cancellationToken).ConfigureAwait(continueOnCapturedContext: false);
494 }
495 else
496 {
497 _stream.Write(tempOutputBuffer, 0, numOutputBytes2);
498 }
499 currentInputIndex += numWholeBlocksInBytes;
500 bytesToWrite -= numWholeBlocksInBytes;
501 CryptographicOperations.ZeroMemory(new Span<byte>(tempOutputBuffer, 0, numOutputBytes2));
502 ArrayPool<byte>.Shared.Return(tempOutputBuffer);
503 tempOutputBuffer = null;
504 }
505 catch
506 {
507 CryptographicOperations.ZeroMemory(new Span<byte>(tempOutputBuffer, 0, numOutputBytes2));
508 throw;
509 }
510 }
511 else
512 {
513 int numOutputBytes2 = TransformBlock(_transform, buffer.Slice(currentInputIndex, _inputBlockSize), _outputBuffer, 0);
514 if (useAsync)
515 {
516 await _stream.WriteAsync(new ReadOnlyMemory<byte>(_outputBuffer, 0, numOutputBytes2), cancellationToken).ConfigureAwait(continueOnCapturedContext: false);
517 }
518 else
519 {
520 _stream.Write(_outputBuffer, 0, numOutputBytes2);
521 }
522 currentInputIndex += _inputBlockSize;
523 bytesToWrite -= _inputBlockSize;
524 }
525 continue;
526 }
527 buffer.Slice(currentInputIndex, bytesToWrite).CopyTo(_inputBuffer);
528 _inputBufferIndex += bytesToWrite;
529 break;
530 }
531 unsafe static int TransformBlock(ICryptoTransform transform, ReadOnlyMemory<byte> inputBuffer, byte[] outputBuffer, int outputOffset)
532 {
533 if (MemoryMarshal.TryGetArray(inputBuffer, out var segment))
534 {
535 return transform.TransformBlock(segment.Array, segment.Offset, inputBuffer.Length, outputBuffer, outputOffset);
536 }
537 byte[] array = ArrayPool<byte>.Shared.Rent(inputBuffer.Length);
538 int result = 0;
539 fixed (byte* ptr = &array[0])
540 {
541 try
542 {
543 inputBuffer.CopyTo(array);
544 result = transform.TransformBlock(array, 0, inputBuffer.Length, outputBuffer, outputOffset);
545 }
546 finally
547 {
548 CryptographicOperations.ZeroMemory(array.AsSpan(0, inputBuffer.Length));
549 }
550 }
552 array = null;
553 return result;
554 }
555 }
static ArrayPool< T > Shared
Definition ArrayPool.cs:7
Task WriteAsync(byte[] buffer, int offset, int count)
Definition Stream.cs:914
void Write(byte[] buffer, int offset, int count)
readonly ICryptoTransform _transform
new ConfiguredTaskAwaitable< TResult > ConfigureAwait(bool continueOnCapturedContext)
Definition Task.cs:226
int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)

References System.Security.Cryptography.CryptoStream._inputBlockSize, System.Security.Cryptography.CryptoStream._inputBuffer, System.Security.Cryptography.CryptoStream._inputBufferIndex, System.Security.Cryptography.CryptoStream._outputBlockSize, System.Security.Cryptography.CryptoStream._outputBuffer, System.Security.Cryptography.CryptoStream._stream, System.Security.Cryptography.CryptoStream._transform, System.array, System.buffer, System.cancellationToken, System.Security.Cryptography.ICryptoTransform.CanTransformMultipleBlocks, System.Threading.Tasks.Task< TResult >.ConfigureAwait(), System.ReadOnlyMemory< T >.CopyTo(), System.ReadOnlyMemory< T >.Length, System.Buffers.ArrayPool< T >.Shared, System.Security.Cryptography.ICryptoTransform.TransformBlock(), System.IO.Stream.Write(), System.IO.Stream.WriteAsync(), and System.Security.Cryptography.CryptographicOperations.ZeroMemory().

Referenced by System.Security.Cryptography.CryptoStream.Write(), and System.Security.Cryptography.CryptoStream.WriteAsyncInternal().