Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
Base64Stream.cs
Go to the documentation of this file.
1using System.IO;
3using System.Text;
4
5namespace System.Net;
6
8{
9 private sealed class ReadAsyncResult : System.Net.LazyAsyncResult
10 {
11 private readonly Base64Stream _parent;
12
13 private readonly byte[] _buffer;
14
15 private readonly int _offset;
16
17 private readonly int _count;
18
19 private int _read;
20
21 private static readonly AsyncCallback s_onRead = OnRead;
22
23 internal ReadAsyncResult(Base64Stream parent, byte[] buffer, int offset, int count, AsyncCallback callback, object state)
24 : base(null, state, callback)
25 {
26 _parent = parent;
29 _count = count;
30 }
31
32 private bool CompleteRead(IAsyncResult result)
33 {
35 if (_read == 0)
36 {
38 return true;
39 }
41 if (_read > 0)
42 {
44 return true;
45 }
46 return false;
47 }
48
49 internal void Read()
50 {
52 do
53 {
55 }
56 while (asyncResult.CompletedSynchronously && !CompleteRead(asyncResult));
57 }
58
59 private static void OnRead(IAsyncResult result)
60 {
61 if (result.CompletedSynchronously)
62 {
63 return;
64 }
65 ReadAsyncResult readAsyncResult = (ReadAsyncResult)result.AsyncState;
66 try
67 {
68 if (!readAsyncResult.CompleteRead(result))
69 {
70 readAsyncResult.Read();
71 }
72 }
73 catch (Exception result2)
74 {
75 if (readAsyncResult.IsCompleted)
76 {
77 throw;
78 }
79 readAsyncResult.InvokeCallback(result2);
80 }
81 }
82
83 internal static int End(IAsyncResult result)
84 {
85 ReadAsyncResult readAsyncResult = (ReadAsyncResult)result;
86 readAsyncResult.InternalWaitForCompletion();
87 return readAsyncResult._read;
88 }
89 }
90
91 private sealed class WriteAsyncResult : System.Net.LazyAsyncResult
92 {
93 private static readonly AsyncCallback s_onWrite = OnWrite;
94
95 private readonly Base64Stream _parent;
96
97 private readonly byte[] _buffer;
98
99 private readonly int _offset;
100
101 private readonly int _count;
102
103 private int _written;
104
105 internal WriteAsyncResult(Base64Stream parent, byte[] buffer, int offset, int count, AsyncCallback callback, object state)
106 : base(null, state, callback)
107 {
108 _parent = parent;
109 _buffer = buffer;
110 _offset = offset;
111 _count = count;
112 }
113
114 internal void Write()
115 {
116 while (true)
117 {
118 _written += _parent.EncodeBytes(_buffer, _offset + _written, _count - _written, dontDeferFinalBytes: false, shouldAppendSpaceToCRLF: false);
119 if (_written < _count)
120 {
122 if (asyncResult.CompletedSynchronously)
123 {
125 continue;
126 }
127 break;
128 }
130 break;
131 }
132 }
133
134 private void CompleteWrite(IAsyncResult result)
135 {
138 }
139
140 private static void OnWrite(IAsyncResult result)
141 {
142 if (result.CompletedSynchronously)
143 {
144 return;
145 }
146 WriteAsyncResult writeAsyncResult = (WriteAsyncResult)result.AsyncState;
147 try
148 {
149 writeAsyncResult.CompleteWrite(result);
150 writeAsyncResult.Write();
151 }
152 catch (Exception result2)
153 {
154 if (writeAsyncResult.IsCompleted)
155 {
156 throw;
157 }
158 writeAsyncResult.InvokeCallback(result2);
159 }
160 }
161
162 internal static void End(IAsyncResult result)
163 {
164 WriteAsyncResult writeAsyncResult = (WriteAsyncResult)result;
165 writeAsyncResult.InternalWaitForCompletion();
166 }
167 }
168
169 private sealed class ReadStateInfo
170 {
171 internal byte Val { get; set; }
172
173 internal byte Pos { get; set; }
174 }
175
177
179
180 private readonly IByteEncoder _encoder;
181
182 private static ReadOnlySpan<byte> Base64DecodeMap => new byte[256]
183 {
184 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
185 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
186 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
187 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
188 255, 255, 255, 62, 255, 255, 255, 63, 52, 53,
189 54, 55, 56, 57, 58, 59, 60, 61, 255, 255,
190 255, 255, 255, 255, 255, 0, 1, 2, 3, 4,
191 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
192 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
193 25, 255, 255, 255, 255, 255, 255, 26, 27, 28,
194 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
195 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
196 49, 50, 51, 255, 255, 255, 255, 255, 255, 255,
197 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
198 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
199 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
200 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
201 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
202 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
203 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
204 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
205 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
206 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
207 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
208 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
209 255, 255, 255, 255, 255, 255
210 };
211
212 private ReadStateInfo ReadState => _readState ?? (_readState = new ReadStateInfo());
213
214 internal WriteStateInfoBase WriteState => _writeState;
215
217 : base(stream)
218 {
220 _encoder = new Base64Encoder(_writeState, writeStateInfo.MaxLineLength);
221 }
222
223 internal Base64Stream(Base64WriteStateInfo writeStateInfo)
224 : base(new MemoryStream())
225 {
226 _writeState = writeStateInfo;
227 _encoder = new Base64Encoder(_writeState, writeStateInfo.MaxLineLength);
228 }
229
230 public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
231 {
233 ReadAsyncResult readAsyncResult = new ReadAsyncResult(this, buffer, offset, count, callback, state);
234 readAsyncResult.Read();
235 return readAsyncResult;
236 }
237
238 public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
239 {
241 WriteAsyncResult writeAsyncResult = new WriteAsyncResult(this, buffer, offset, count, callback, state);
242 writeAsyncResult.Write();
243 return writeAsyncResult;
244 }
245
246 public override void Close()
247 {
248 if (_writeState != null && WriteState.Length > 0)
249 {
252 }
253 base.Close();
254 }
255
256 public unsafe int DecodeBytes(byte[] buffer, int offset, int count)
257 {
258 fixed (byte* ptr = buffer)
259 {
260 byte* ptr2 = ptr + offset;
261 byte* ptr3 = ptr2;
262 byte* ptr4 = ptr2;
263 byte* ptr5 = ptr2 + count;
264 while (ptr3 < ptr5)
265 {
266 if (*ptr3 == 13 || *ptr3 == 10 || *ptr3 == 61 || *ptr3 == 32 || *ptr3 == 9)
267 {
268 ptr3++;
269 continue;
270 }
271 byte b = Base64DecodeMap[*ptr3];
272 if (b == byte.MaxValue)
273 {
275 }
276 switch (ReadState.Pos)
277 {
278 case 0:
279 ReadState.Val = (byte)(b << 2);
280 ReadState.Pos++;
281 break;
282 case 1:
283 *(ptr4++) = (byte)(ReadState.Val + (b >> 4));
284 ReadState.Val = (byte)(b << 4);
285 ReadState.Pos++;
286 break;
287 case 2:
288 *(ptr4++) = (byte)(ReadState.Val + (b >> 2));
289 ReadState.Val = (byte)(b << 6);
290 ReadState.Pos++;
291 break;
292 case 3:
293 *(ptr4++) = (byte)(ReadState.Val + b);
294 ReadState.Pos = 0;
295 break;
296 }
297 ptr3++;
298 }
299 return (int)(ptr4 - ptr2);
300 }
301 }
302
303 internal int EncodeBytes(byte[] buffer, int offset, int count, bool dontDeferFinalBytes, bool shouldAppendSpaceToCRLF)
304 {
305 return _encoder.EncodeBytes(buffer, offset, count, dontDeferFinalBytes, shouldAppendSpaceToCRLF);
306 }
307
308 public int EncodeString(string value, Encoding encoding)
309 {
310 return _encoder.EncodeString(value, encoding);
311 }
312
313 public string GetEncodedString()
314 {
315 return _encoder.GetEncodedString();
316 }
317
318 public override int EndRead(IAsyncResult asyncResult)
319 {
320 if (asyncResult == null)
321 {
322 throw new ArgumentNullException("asyncResult");
323 }
325 }
326
327 public override void EndWrite(IAsyncResult asyncResult)
328 {
329 if (asyncResult == null)
330 {
331 throw new ArgumentNullException("asyncResult");
332 }
334 }
335
336 public override void Flush()
337 {
338 if (_writeState != null && WriteState.Length > 0)
339 {
341 }
342 base.Flush();
343 }
344
345 private void FlushInternal()
346 {
347 base.Write(WriteState.Buffer, 0, WriteState.Length);
348 WriteState.Reset();
349 }
350
351 public override int Read(byte[] buffer, int offset, int count)
352 {
354 int num;
355 do
356 {
357 num = base.Read(buffer, offset, count);
358 if (num == 0)
359 {
360 return 0;
361 }
362 num = DecodeBytes(buffer, offset, num);
363 }
364 while (num <= 0);
365 return num;
366 }
367
368 public override void Write(byte[] buffer, int offset, int count)
369 {
371 int num = 0;
372 while (true)
373 {
374 num += EncodeBytes(buffer, offset + num, count - num, dontDeferFinalBytes: false, shouldAppendSpaceToCRLF: false);
375 if (num < count)
376 {
378 continue;
379 }
380 break;
381 }
382 }
383}
static void ValidateBufferArguments(byte[] buffer, int offset, int count)
Definition Stream.cs:1044
virtual void EndWrite(IAsyncResult asyncResult)
Definition Stream.cs:889
virtual IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state)
Definition Stream.cs:688
virtual IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state)
Definition Stream.cs:813
virtual int EndRead(IAsyncResult asyncResult)
Definition Stream.cs:737
bool CompleteRead(IAsyncResult result)
static int End(IAsyncResult result)
static readonly AsyncCallback s_onRead
ReadAsyncResult(Base64Stream parent, byte[] buffer, int offset, int count, AsyncCallback callback, object state)
static void OnRead(IAsyncResult result)
static readonly AsyncCallback s_onWrite
void CompleteWrite(IAsyncResult result)
WriteAsyncResult(Base64Stream parent, byte[] buffer, int offset, int count, AsyncCallback callback, object state)
static void OnWrite(IAsyncResult result)
static void End(IAsyncResult result)
Base64Stream(Base64WriteStateInfo writeStateInfo)
override int Read(byte[] buffer, int offset, int count)
override void Write(byte[] buffer, int offset, int count)
readonly Base64WriteStateInfo _writeState
unsafe int DecodeBytes(byte[] buffer, int offset, int count)
override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
override void Flush()
override void Close()
Base64Stream(Stream stream, Base64WriteStateInfo writeStateInfo)
WriteStateInfoBase WriteState
override int EndRead(IAsyncResult asyncResult)
override void EndWrite(IAsyncResult asyncResult)
static ReadOnlySpan< byte > Base64DecodeMap
readonly IByteEncoder _encoder
int EncodeString(string value, Encoding encoding)
int EncodeBytes(byte[] buffer, int offset, int count, bool dontDeferFinalBytes, bool shouldAppendSpaceToCRLF)
override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
void InvokeCallback(object result)
static string MailBase64InvalidCharacter
Definition SR.cs:30
Definition SR.cs:7
int EncodeString(string value, Encoding encoding)
int EncodeBytes(byte[] buffer, int offset, int count, bool dontDeferFinalBytes, bool shouldAppendSpaceToCRLF)