Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
Base64Encoding.cs
Go to the documentation of this file.
2
3namespace System.Text;
4
5internal sealed class Base64Encoding : Encoding
6{
7 private const string Val2Char = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
8
9 private static ReadOnlySpan<byte> Char2val => new byte[128]
10 {
11 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
12 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
13 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
14 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
15 255, 255, 255, 62, 255, 255, 255, 63, 52, 53,
16 54, 55, 56, 57, 58, 59, 60, 61, 255, 255,
17 255, 64, 255, 255, 255, 0, 1, 2, 3, 4,
18 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
19 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
20 25, 255, 255, 255, 255, 255, 255, 26, 27, 28,
21 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
22 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
23 49, 50, 51, 255, 255, 255, 255, 255
24 };
25
26 private static ReadOnlySpan<byte> Val2byte => "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"u8;
27
28 public override int GetMaxByteCount(int charCount)
29 {
30 if (charCount < 0)
31 {
33 }
34 if (charCount % 4 != 0)
35 {
37 }
38 return charCount / 4 * 3;
39 }
40
41 private bool IsValidLeadBytes(int v1, int v2, int v3, int v4)
42 {
43 if ((v1 | v2) < 64)
44 {
45 return (v3 | v4) != 255;
46 }
47 return false;
48 }
49
50 private bool IsValidTailBytes(int v3, int v4)
51 {
52 if (v3 == 64)
53 {
54 return v4 == 64;
55 }
56 return true;
57 }
58
59 public unsafe override int GetByteCount(char[] chars, int index, int count)
60 {
61 if (chars == null)
62 {
64 }
65 if (index < 0)
66 {
68 }
69 if (index > chars.Length)
70 {
72 }
73 if (count < 0)
74 {
76 }
77 if (count > chars.Length - index)
78 {
80 }
81 if (count == 0)
82 {
83 return 0;
84 }
85 if (count % 4 != 0)
86 {
88 }
89 fixed (byte* ptr4 = &Char2val[0])
90 {
91 fixed (char* ptr = &chars[index])
92 {
93 int num = 0;
94 char* ptr2 = ptr;
95 for (char* ptr3 = ptr + count; ptr2 < ptr3; ptr2 += 4)
96 {
97 char c = *ptr2;
98 char c2 = ptr2[1];
99 char c3 = ptr2[2];
100 char c4 = ptr2[3];
101 if ((c | c2 | c3 | c4) >= 128)
102 {
104 }
105 int v = ptr4[(int)c];
106 int v2 = ptr4[(int)c2];
107 int num2 = ptr4[(int)c3];
108 int num3 = ptr4[(int)c4];
110 {
112 }
113 int num4 = ((num3 != 64) ? 3 : ((num2 == 64) ? 1 : 2));
114 num += num4;
115 }
116 return num;
117 }
118 }
119 }
120
121 public unsafe override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
122 {
123 if (chars == null)
124 {
126 }
127 if (charIndex < 0)
128 {
130 }
131 if (charIndex > chars.Length)
132 {
134 }
135 if (charCount < 0)
136 {
138 }
139 if (charCount > chars.Length - charIndex)
140 {
142 }
143 if (bytes == null)
144 {
146 }
147 if (byteIndex < 0)
148 {
150 }
151 if (byteIndex > bytes.Length)
152 {
154 }
155 if (charCount == 0)
156 {
157 return 0;
158 }
159 if (charCount % 4 != 0)
160 {
162 }
163 fixed (byte* ptr7 = &Char2val[0])
164 {
165 fixed (char* ptr = &chars[charIndex])
166 {
167 fixed (byte* ptr4 = &bytes[byteIndex])
168 {
169 char* ptr2 = ptr;
170 char* ptr3 = ptr + charCount;
171 byte* ptr5 = ptr4;
172 byte* ptr6 = ptr4 + bytes.Length - byteIndex;
173 for (; ptr2 < ptr3; ptr2 += 4)
174 {
175 char c = *ptr2;
176 char c2 = ptr2[1];
177 char c3 = ptr2[2];
178 char c4 = ptr2[3];
179 if ((c | c2 | c3 | c4) >= 128)
180 {
182 }
183 int num = ptr7[(int)c];
184 int num2 = ptr7[(int)c2];
185 int num3 = ptr7[(int)c3];
186 int num4 = ptr7[(int)c4];
188 {
190 }
191 int num5 = ((num4 != 64) ? 3 : ((num3 == 64) ? 1 : 2));
192 if (ptr5 + num5 > ptr6)
193 {
195 }
196 *ptr5 = (byte)((uint)(num << 2) | ((uint)(num2 >> 4) & 3u));
197 if (num5 > 1)
198 {
199 ptr5[1] = (byte)((uint)(num2 << 4) | ((uint)(num3 >> 2) & 0xFu));
200 if (num5 > 2)
201 {
202 ptr5[2] = (byte)((uint)(num3 << 6) | ((uint)num4 & 0x3Fu));
203 }
204 }
205 ptr5 += num5;
206 }
207 return (int)(ptr5 - ptr4);
208 }
209 }
210 }
211 }
212
213 public unsafe int GetBytes(byte[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
214 {
215 if (chars == null)
216 {
218 }
219 if (charIndex < 0)
220 {
222 }
223 if (charIndex > chars.Length)
224 {
226 }
227 if (charCount < 0)
228 {
230 }
231 if (charCount > chars.Length - charIndex)
232 {
234 }
235 if (bytes == null)
236 {
238 }
239 if (byteIndex < 0)
240 {
242 }
243 if (byteIndex > bytes.Length)
244 {
246 }
247 if (charCount == 0)
248 {
249 return 0;
250 }
251 if (charCount % 4 != 0)
252 {
254 }
255 fixed (byte* ptr7 = &Char2val[0])
256 {
257 fixed (byte* ptr = &chars[charIndex])
258 {
259 fixed (byte* ptr4 = &bytes[byteIndex])
260 {
261 byte* ptr2 = ptr;
262 byte* ptr3 = ptr + charCount;
263 byte* ptr5 = ptr4;
264 byte* ptr6 = ptr4 + bytes.Length - byteIndex;
265 for (; ptr2 < ptr3; ptr2 += 4)
266 {
267 byte b = *ptr2;
268 byte b2 = ptr2[1];
269 byte b3 = ptr2[2];
270 byte b4 = ptr2[3];
271 if ((b | b2 | b3 | b4) >= 128)
272 {
274 }
275 int num = ptr7[(int)b];
276 int num2 = ptr7[(int)b2];
277 int num3 = ptr7[(int)b3];
278 int num4 = ptr7[(int)b4];
280 {
282 }
283 int num5 = ((num4 != 64) ? 3 : ((num3 == 64) ? 1 : 2));
284 if (ptr5 + num5 > ptr6)
285 {
287 }
288 *ptr5 = (byte)((uint)(num << 2) | ((uint)(num2 >> 4) & 3u));
289 if (num5 > 1)
290 {
291 ptr5[1] = (byte)((uint)(num2 << 4) | ((uint)(num3 >> 2) & 0xFu));
292 if (num5 > 2)
293 {
294 ptr5[2] = (byte)((uint)(num3 << 6) | ((uint)num4 & 0x3Fu));
295 }
296 }
297 ptr5 += num5;
298 }
299 return (int)(ptr5 - ptr4);
300 }
301 }
302 }
303 }
304
305 public override int GetMaxCharCount(int byteCount)
306 {
307 if (byteCount < 0 || byteCount > 1610612731)
308 {
310 }
311 return (byteCount + 2) / 3 * 4;
312 }
313
314 public override int GetCharCount(byte[] bytes, int index, int count)
315 {
316 return GetMaxCharCount(count);
317 }
318
319 public unsafe override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
320 {
321 if (bytes == null)
322 {
324 }
325 if (byteIndex < 0)
326 {
328 }
329 if (byteIndex > bytes.Length)
330 {
332 }
333 if (byteCount < 0)
334 {
336 }
337 if (byteCount > bytes.Length - byteIndex)
338 {
340 }
342 if (chars == null)
343 {
345 }
346 if (charIndex < 0)
347 {
349 }
350 if (charIndex > chars.Length)
351 {
353 }
355 {
357 }
358 if (byteCount > 0)
359 {
360 fixed (char* ptr6 = &"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".GetPinnableReference())
361 {
362 fixed (byte* ptr = &bytes[byteIndex])
363 {
364 fixed (char* ptr4 = &chars[charIndex])
365 {
366 byte* ptr2 = ptr;
367 byte* ptr3 = ptr2 + byteCount - 3;
368 char* ptr5 = ptr4;
369 while (ptr2 <= ptr3)
370 {
371 *ptr5 = ptr6[*ptr2 >> 2];
372 ptr5[1] = ptr6[((*ptr2 & 3) << 4) | (ptr2[1] >> 4)];
373 ptr5[2] = ptr6[((ptr2[1] & 0xF) << 2) | (ptr2[2] >> 6)];
374 ptr5[3] = ptr6[ptr2[2] & 0x3F];
375 ptr2 += 3;
376 ptr5 += 4;
377 }
378 if (ptr2 - ptr3 == 2)
379 {
380 *ptr5 = ptr6[*ptr2 >> 2];
381 ptr5[1] = ptr6[(*ptr2 & 3) << 4];
382 ptr5[2] = '=';
383 ptr5[3] = '=';
384 }
385 else if (ptr2 - ptr3 == 1)
386 {
387 *ptr5 = ptr6[*ptr2 >> 2];
388 ptr5[1] = ptr6[((*ptr2 & 3) << 4) | (ptr2[1] >> 4)];
389 ptr5[2] = ptr6[(ptr2[1] & 0xF) << 2];
390 ptr5[3] = '=';
391 }
392 }
393 }
394 }
395 }
396 return charCount;
397 }
398
399 public unsafe int GetChars(byte[] bytes, int byteIndex, int byteCount, byte[] chars, int charIndex)
400 {
401 if (bytes == null)
402 {
404 }
405 if (byteIndex < 0)
406 {
408 }
409 if (byteIndex > bytes.Length)
410 {
412 }
413 if (byteCount < 0)
414 {
416 }
417 if (byteCount > bytes.Length - byteIndex)
418 {
420 }
422 if (chars == null)
423 {
425 }
426 if (charIndex < 0)
427 {
429 }
430 if (charIndex > chars.Length)
431 {
433 }
435 {
437 }
438 if (byteCount > 0)
439 {
440 fixed (byte* ptr6 = &Val2byte[0])
441 {
442 fixed (byte* ptr = &bytes[byteIndex])
443 {
444 fixed (byte* ptr4 = &chars[charIndex])
445 {
446 byte* ptr2 = ptr;
447 byte* ptr3 = ptr2 + byteCount - 3;
448 byte* ptr5 = ptr4;
449 while (ptr2 <= ptr3)
450 {
451 *ptr5 = ptr6[*ptr2 >> 2];
452 ptr5[1] = ptr6[((*ptr2 & 3) << 4) | (ptr2[1] >> 4)];
453 ptr5[2] = ptr6[((ptr2[1] & 0xF) << 2) | (ptr2[2] >> 6)];
454 ptr5[3] = ptr6[ptr2[2] & 0x3F];
455 ptr2 += 3;
456 ptr5 += 4;
457 }
458 if (ptr2 - ptr3 == 2)
459 {
460 *ptr5 = ptr6[*ptr2 >> 2];
461 ptr5[1] = ptr6[(*ptr2 & 3) << 4];
462 ptr5[2] = 61;
463 ptr5[3] = 61;
464 }
465 else if (ptr2 - ptr3 == 1)
466 {
467 *ptr5 = ptr6[*ptr2 >> 2];
468 ptr5[1] = ptr6[((*ptr2 & 3) << 4) | (ptr2[1] >> 4)];
469 ptr5[2] = ptr6[(ptr2[1] & 0xF) << 2];
470 ptr5[3] = 61;
471 }
472 }
473 }
474 }
475 }
476 return charCount;
477 }
478}
static string XmlInvalidBase64Sequence
Definition SR.cs:334
static string XmlInvalidBase64Length
Definition SR.cs:332
static string ValueMustBeInRange
Definition SR.cs:326
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string ValueMustBeNonNegative
Definition SR.cs:296
static string SizeExceedsRemainingBufferSpace
Definition SR.cs:324
static string OffsetExceedsBufferSize
Definition SR.cs:322
static string XmlArrayTooSmall
Definition SR.cs:348
Definition SR.cs:7
bool IsValidTailBytes(int v3, int v4)
override int GetMaxCharCount(int byteCount)
unsafe int GetBytes(byte[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
override int GetMaxByteCount(int charCount)
static ReadOnlySpan< byte > Char2val
override int GetCharCount(byte[] bytes, int index, int count)
unsafe int GetChars(byte[] bytes, int byteIndex, int byteCount, byte[] chars, int charIndex)
unsafe override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
unsafe override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
unsafe override int GetByteCount(char[] chars, int index, int count)
bool IsValidLeadBytes(int v1, int v2, int v3, int v4)
static ReadOnlySpan< byte > Val2byte