-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
162 lines (127 loc) · 6.66 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// (c) gfoidl, all rights reserved
using System.Buffers;
using System.Diagnostics;
using System.IO.Pipelines;
namespace gfoidl.Base64.Demo;
public static class Program
{
static async Task Main()
{
MachineInfo.PrintMachineInfo(Console.Out);
Func<Task>[] demos = { RunGuidEncoding, RunGuidDecoding, RunBufferChainEncoding, RunDetectEncoding, RunSequenceEncoding };
foreach (Func<Task> demo in demos)
{
Console.Write($"{demo.Method.Name}...");
await demo();
Console.WriteLine("done");
}
}
//-------------------------------------------------------------------------
private static Task RunGuidEncoding()
{
byte[] guid = Guid.NewGuid().ToByteArray();
string guidBase64 = Base64.Default.Encode(guid);
string guidBases64Url = Base64.Url.Encode(guid);
int guidBase64EncodedLength = Base64.Default.GetEncodedLength(guid.Length);
Span<byte> guidBase64UTF8 = stackalloc byte[guidBase64EncodedLength];
OperationStatus status = Base64.Default.Encode(guid, guidBase64UTF8, out int consumed, out int written);
Debug.Assert(status == OperationStatus.Done);
Debug.Assert(consumed == guid.Length);
Debug.Assert(written == guidBase64UTF8.Length);
int guidBase64UrlEncodedLength = Base64.Url.GetEncodedLength(guid.Length);
Span<byte> guidBase64UrlUTF8 = stackalloc byte[guidBase64UrlEncodedLength];
status = Base64.Url.Encode(guid, guidBase64UrlUTF8, out consumed, out written);
Debug.Assert(status == OperationStatus.Done);
Debug.Assert(consumed == guid.Length);
Debug.Assert(written == guidBase64UrlUTF8.Length);
return Task.CompletedTask;
}
//-------------------------------------------------------------------------
private static Task RunGuidDecoding()
{
Guid guid = Guid.NewGuid();
string guidBase64 = Convert.ToBase64String(guid.ToByteArray());
string guidBase64Url = guidBase64.Replace('+', '-').Replace('/', '_').TrimEnd('=');
byte[] guidBase64Decoded = Base64.Default.Decode(guidBase64);
byte[] guidBase64UrlDecoded = Base64.Url.Decode(guidBase64Url);
Debug.Assert(guid == new Guid(guidBase64Decoded));
Debug.Assert(guid == new Guid(guidBase64UrlDecoded));
int guidBase64DecodedLen = Base64.Default.GetDecodedLength(guidBase64);
int guidBase64UrlDecodedLen = Base64.Url.GetDecodedLength(guidBase64Url);
Span<byte> guidBase64DecodedBuffer = stackalloc byte[guidBase64DecodedLen];
Span<byte> guidBase64UrlDecodedBuffer = stackalloc byte[guidBase64UrlDecodedLen];
OperationStatus status = Base64.Default.Decode(guidBase64, guidBase64DecodedBuffer, out int consumed, out int written);
Debug.Assert(status == OperationStatus.Done);
Debug.Assert(consumed == guidBase64.Length);
Debug.Assert(written == guidBase64DecodedBuffer.Length);
status = Base64.Url.Decode(guidBase64Url, guidBase64UrlDecodedBuffer, out consumed, out written);
Debug.Assert(status == OperationStatus.Done);
Debug.Assert(consumed == guidBase64Url.Length);
Debug.Assert(written == guidBase64UrlDecodedBuffer.Length);
return Task.CompletedTask;
}
//-------------------------------------------------------------------------
private static Task RunBufferChainEncoding()
{
var rnd = new Random();
Span<byte> data = new byte[1000];
rnd.NextBytes(data);
// exact length could be computed by Base64.Default.GetEncodedLength, here for demo exzessive size
Span<char> base64 = new char[5000];
OperationStatus status = Base64.Default.Encode(data[..400], base64, out int consumed, out int written, isFinalBlock: false);
Debug.Assert(status == OperationStatus.NeedMoreData); // 400 is not a multiple of 3, so more data is needed
status = Base64.Default.Encode(data[consumed..], base64[written..], out _, out int written1, isFinalBlock: true);
Debug.Assert(status == OperationStatus.Done);
base64 = base64[..(written + written1)];
Span<byte> decoded = new byte[5000];
status = Base64.Default.Decode(base64[..100], decoded, out consumed, out written, isFinalBlock: false);
Debug.Assert(status == OperationStatus.Done); // 100 encoded bytes can be decoded to 75 bytes, so Done
status = Base64.Default.Decode(base64[consumed..], decoded[written..], out _, out written1, isFinalBlock: true);
Debug.Assert(status == OperationStatus.Done);
decoded = decoded[..(written + written1)];
Debug.Assert(data.SequenceEqual(decoded));
return Task.CompletedTask;
}
//-------------------------------------------------------------------------
private static Task RunDetectEncoding()
{
// Let's assume we don't know whether this string is base64 or base64Url
const string EncodedString = "a-_9";
Span<byte> data = stackalloc byte[Base64.Default.GetMaxDecodedLength(EncodedString.Length)];
EncodingType encodingType = Base64.DetectEncoding(EncodedString);
int written = 0;
switch (encodingType)
{
case EncodingType.Base64:
Base64.Default.Decode(EncodedString, data, out int _, out written);
break;
case EncodingType.Base64Url:
Base64.Url.Decode(EncodedString, data, out int _, out written);
break;
case EncodingType.Unknown:
throw new InvalidOperationException("should not be here");
}
data = data[..written];
Debug.Assert(data.Length == 3);
return Task.CompletedTask;
}
//-------------------------------------------------------------------------
private static async Task RunSequenceEncoding()
{
var pipeOptions = PipeOptions.Default;
var pipe = new Pipe(pipeOptions);
var rnd = new Random(42);
var data = new byte[4097];
rnd.NextBytes(data);
pipe.Writer.Write(data);
await pipe.Writer.CompleteAsync();
ReadResult readResult = await pipe.Reader.ReadAsync();
var resultPipe = new Pipe();
Base64.Default.Encode(readResult.Buffer, resultPipe.Writer, out long consumed, out long written);
await resultPipe.Writer.FlushAsync();
await resultPipe.Writer.CompleteAsync();
pipe.Reader.AdvanceTo(readResult.Buffer.End);
Debug.Assert(consumed == data.Length);
Debug.Assert(written == (data.Length + 2) / 3 * 4);
}
}