| | | 1 | | /******************************************************************************** |
| | | 2 | | * Base64BodyReaderStream.cs * |
| | | 3 | | * * |
| | | 4 | | * Author: Denes Solti * |
| | | 5 | | ********************************************************************************/ |
| | | 6 | | using System; |
| | | 7 | | using System.IO; |
| | | 8 | | using System.Threading; |
| | | 9 | | using System.Threading.Tasks; |
| | | 10 | | |
| | | 11 | | namespace NanoRoute.AwsLambda |
| | | 12 | | { |
| | 1 | 13 | | internal sealed class Base64BodyReaderStream(string body) : Stream |
| | | 14 | | { |
| | | 15 | | #region Private |
| | 1 | 16 | | private readonly byte[] _stagedBytes = new byte[3]; |
| | 1 | 17 | | private ReadOnlyMemory<char> _remainingBody = body.AsMemory(); |
| | | 18 | | private Memory<byte> _remainingStagedBytes; |
| | | 19 | | |
| | | 20 | | private bool _disposed; |
| | | 21 | | |
| | | 22 | | private int CopyStagedBytes(ref Span<byte> buffer) |
| | 1 | 23 | | { |
| | 1 | 24 | | int bytesToCopy = Math.Min(_remainingStagedBytes.Length, buffer.Length); |
| | 1 | 25 | | if (bytesToCopy is 0) |
| | 1 | 26 | | return 0; |
| | | 27 | | |
| | 1 | 28 | | _remainingStagedBytes.Span.Slice(0, bytesToCopy).CopyTo(buffer.Slice(0, bytesToCopy)); |
| | 1 | 29 | | _remainingStagedBytes = _remainingStagedBytes.Slice(bytesToCopy); |
| | 1 | 30 | | buffer = buffer.Slice(bytesToCopy); |
| | | 31 | | |
| | 1 | 32 | | return bytesToCopy; |
| | 1 | 33 | | } |
| | | 34 | | |
| | | 35 | | private int DecodeStep(Span<byte> buffer) |
| | 1 | 36 | | { |
| | 1 | 37 | | ReadOnlySpan<char> bodySpan = _remainingBody.Span; |
| | 1 | 38 | | int inputLength = Math.Min(bodySpan.Length / 4, buffer.Length / 3) * 4; |
| | | 39 | | |
| | 1 | 40 | | if (inputLength is 0) |
| | 1 | 41 | | { |
| | 1 | 42 | | if (!bodySpan.IsEmpty) |
| | 1 | 43 | | throw new FormatException(); |
| | | 44 | | |
| | 0 | 45 | | return 0; |
| | | 46 | | } |
| | | 47 | | |
| | 1 | 48 | | if (!Convert.TryFromBase64Chars(bodySpan.Slice(0, inputLength), buffer, out int written)) |
| | 1 | 49 | | throw new FormatException(); |
| | | 50 | | |
| | 1 | 51 | | _remainingBody = _remainingBody.Slice(inputLength); |
| | | 52 | | |
| | 1 | 53 | | return written; |
| | 1 | 54 | | } |
| | | 55 | | |
| | | 56 | | private int ReadCore(Span<byte> buffer, CancellationToken cancellation) |
| | 1 | 57 | | { |
| | 1 | 58 | | ObjectDisposedException.ThrowIf(_disposed, this); |
| | | 59 | | |
| | 1 | 60 | | if (buffer.IsEmpty) |
| | 1 | 61 | | return 0; |
| | | 62 | | |
| | 1 | 63 | | int read = CopyStagedBytes(ref buffer); |
| | | 64 | | |
| | 1 | 65 | | while (buffer.Length >= 3 && !_remainingBody.IsEmpty) |
| | 1 | 66 | | { |
| | 1 | 67 | | cancellation.ThrowIfCancellationRequested(); |
| | | 68 | | |
| | 1 | 69 | | int decoded = DecodeStep(buffer); |
| | 1 | 70 | | if (decoded is 0) |
| | 0 | 71 | | break; |
| | | 72 | | |
| | 1 | 73 | | buffer = buffer.Slice(decoded); |
| | 1 | 74 | | read += decoded; |
| | 1 | 75 | | } |
| | | 76 | | |
| | 1 | 77 | | if (!buffer.IsEmpty && !_remainingBody.IsEmpty) |
| | 1 | 78 | | { |
| | 1 | 79 | | cancellation.ThrowIfCancellationRequested(); |
| | | 80 | | |
| | 1 | 81 | | int stagedByteCount = DecodeStep(_stagedBytes); |
| | 1 | 82 | | if (stagedByteCount is not 0) |
| | 1 | 83 | | { |
| | 1 | 84 | | _remainingStagedBytes = _stagedBytes.AsMemory(0, stagedByteCount); |
| | | 85 | | |
| | 1 | 86 | | read += CopyStagedBytes(ref buffer); |
| | 1 | 87 | | } |
| | 1 | 88 | | } |
| | | 89 | | |
| | 1 | 90 | | return read; |
| | 1 | 91 | | } |
| | | 92 | | |
| | | 93 | | protected override void Dispose(bool disposing) |
| | 1 | 94 | | { |
| | 1 | 95 | | _disposed = true; |
| | | 96 | | |
| | 1 | 97 | | base.Dispose(disposing); |
| | 1 | 98 | | } |
| | | 99 | | #endregion |
| | | 100 | | |
| | 1 | 101 | | public override bool CanRead => !_disposed; |
| | | 102 | | |
| | 1 | 103 | | public override bool CanSeek => false; |
| | | 104 | | |
| | 1 | 105 | | public override bool CanWrite => false; |
| | | 106 | | |
| | | 107 | | public override int Read(byte[] buffer, int offset, int count) |
| | 1 | 108 | | { |
| | 1 | 109 | | ValidateBufferArguments(buffer, offset, count); |
| | | 110 | | |
| | 1 | 111 | | return ReadCore(buffer.AsSpan(offset, count), CancellationToken.None); |
| | 1 | 112 | | } |
| | | 113 | | |
| | 1 | 114 | | public override int Read(Span<byte> buffer) => ReadCore(buffer, CancellationToken.None); |
| | | 115 | | |
| | | 116 | | public override async Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellation) |
| | | 117 | | { |
| | | 118 | | ValidateBufferArguments(buffer, offset, count); |
| | | 119 | | |
| | | 120 | | await Task.Yield(); |
| | | 121 | | |
| | | 122 | | return ReadCore(buffer.AsSpan(offset, count), cancellation); |
| | | 123 | | } |
| | | 124 | | |
| | | 125 | | public override async ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellation) |
| | | 126 | | { |
| | | 127 | | await Task.Yield(); |
| | | 128 | | |
| | | 129 | | return ReadCore(buffer.Span, cancellation); |
| | | 130 | | } |
| | | 131 | | |
| | | 132 | | #region Not Supported members |
| | 1 | 133 | | public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException(); |
| | | 134 | | |
| | 1 | 135 | | public override void SetLength(long value) => throw new NotSupportedException(); |
| | | 136 | | |
| | 1 | 137 | | public override void Write(byte[] buffer, int offset, int count) => throw new NotSupportedException(); |
| | | 138 | | |
| | 1 | 139 | | public override long Length => throw new NotSupportedException(); |
| | | 140 | | |
| | | 141 | | public override long Position |
| | | 142 | | { |
| | 1 | 143 | | get => throw new NotSupportedException(); |
| | 1 | 144 | | set => throw new NotSupportedException(); |
| | | 145 | | } |
| | | 146 | | |
| | 1 | 147 | | public override void Flush() { } |
| | | 148 | | #endregion |
| | | 149 | | } |
| | | 150 | | } |