Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
UTF7Encoding.cs
Go to the documentation of this file.
3
4namespace System.Text;
5
6public class UTF7Encoding : Encoding
7{
8 private sealed class Decoder : DecoderNLS
9 {
10 internal int bits;
11
12 internal int bitCount;
13
14 internal bool firstByte;
15
16 internal override bool HasState => bitCount != -1;
17
18 public Decoder(UTF7Encoding encoding)
19 : base(encoding)
20 {
21 }
22
23 public override void Reset()
24 {
25 bits = 0;
26 bitCount = -1;
27 firstByte = false;
28 if (_fallbackBuffer != null)
29 {
31 }
32 }
33 }
34
35 private sealed class Encoder : EncoderNLS
36 {
37 internal int bits;
38
39 internal int bitCount;
40
41 internal override bool HasState
42 {
43 get
44 {
45 if (bits == 0)
46 {
47 return bitCount != -1;
48 }
49 return true;
50 }
51 }
52
53 public Encoder(UTF7Encoding encoding)
54 : base(encoding)
55 {
56 }
57
58 public override void Reset()
59 {
60 bitCount = -1;
61 bits = 0;
62 if (_fallbackBuffer != null)
63 {
65 }
66 }
67 }
68
69 private sealed class DecoderUTF7Fallback : DecoderFallback
70 {
71 public override int MaxCharCount => 1;
72
74 {
75 return new DecoderUTF7FallbackBuffer();
76 }
77
78 public override bool Equals([NotNullWhen(true)] object value)
79 {
80 return value is DecoderUTF7Fallback;
81 }
82
83 public override int GetHashCode()
84 {
85 return 984;
86 }
87 }
88
90 {
91 private char cFallback;
92
93 private int iCount = -1;
94
95 private int iSize;
96
97 public override int Remaining
98 {
99 get
100 {
101 if (iCount <= 0)
102 {
103 return 0;
104 }
105 return iCount;
106 }
107 }
108
109 public override bool Fallback(byte[] bytesUnknown, int index)
110 {
111 cFallback = (char)bytesUnknown[0];
112 if (cFallback == '\0')
113 {
114 return false;
115 }
116 iCount = (iSize = 1);
117 return true;
118 }
119
120 public override char GetNextChar()
121 {
122 if (iCount-- > 0)
123 {
124 return cFallback;
125 }
126 return '\0';
127 }
128
129 public override bool MovePrevious()
130 {
131 if (iCount >= 0)
132 {
133 iCount++;
134 }
135 if (iCount >= 0)
136 {
137 return iCount <= iSize;
138 }
139 return false;
140 }
141
142 public unsafe override void Reset()
143 {
144 iCount = -1;
145 byteStart = null;
146 }
147
148 internal unsafe override int InternalFallback(byte[] bytes, byte* pBytes)
149 {
150 if (bytes.Length != 1)
151 {
153 }
154 if (bytes[0] != 0)
155 {
156 return 1;
157 }
158 return 0;
159 }
160 }
161
162 internal static readonly UTF7Encoding s_default = new UTF7Encoding();
163
164 private byte[] _base64Bytes;
165
166 private sbyte[] _base64Values;
167
168 private bool[] _directEncode;
169
170 private readonly bool _allowOptionals;
171
172 [Obsolete("The UTF-7 encoding is insecure and should not be used. Consider using UTF-8 instead.", DiagnosticId = "SYSLIB0001", UrlFormat = "https://aka.ms/dotnet-warnings/{0}")]
174 : this(allowOptionals: false)
175 {
176 }
177
178 [Obsolete("The UTF-7 encoding is insecure and should not be used. Consider using UTF-8 instead.", DiagnosticId = "SYSLIB0001", UrlFormat = "https://aka.ms/dotnet-warnings/{0}")]
179 public UTF7Encoding(bool allowOptionals)
180 : base(65000)
181 {
182 _allowOptionals = allowOptionals;
183 MakeTables();
184 }
185
186 [MemberNotNull("_base64Bytes")]
187 [MemberNotNull("_base64Values")]
188 [MemberNotNull("_directEncode")]
189 private void MakeTables()
190 {
191 _base64Bytes = new byte[64];
192 for (int i = 0; i < 64; i++)
193 {
194 _base64Bytes[i] = (byte)"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[i];
195 }
196 _base64Values = new sbyte[128];
197 for (int j = 0; j < 128; j++)
198 {
199 _base64Values[j] = -1;
200 }
201 for (int k = 0; k < 64; k++)
202 {
203 _base64Values[_base64Bytes[k]] = (sbyte)k;
204 }
205 _directEncode = new bool[128];
206 int length = "\t\n\r '(),-./0123456789:?ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".Length;
207 for (int l = 0; l < length; l++)
208 {
209 _directEncode[(uint)"\t\n\r '(),-./0123456789:?ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"[l]] = true;
210 }
211 if (_allowOptionals)
212 {
213 length = "!\"#$%&*;<=>@[]^_`{|}".Length;
214 for (int m = 0; m < length; m++)
215 {
216 _directEncode[(uint)"!\"#$%&*;<=>@[]^_`{|}"[m]] = true;
217 }
218 }
219 }
220
221 internal sealed override void SetDefaultFallbacks()
222 {
225 }
226
227 public override bool Equals([NotNullWhen(true)] object? value)
228 {
229 if (value is UTF7Encoding uTF7Encoding)
230 {
231 if (_allowOptionals == uTF7Encoding._allowOptionals && base.EncoderFallback.Equals(uTF7Encoding.EncoderFallback))
232 {
233 return base.DecoderFallback.Equals(uTF7Encoding.DecoderFallback);
234 }
235 return false;
236 }
237 return false;
238 }
239
240 public override int GetHashCode()
241 {
242 return CodePage + base.EncoderFallback.GetHashCode() + base.DecoderFallback.GetHashCode();
243 }
244
245 public unsafe override int GetByteCount(char[] chars, int index, int count)
246 {
247 if (chars == null)
248 {
249 throw new ArgumentNullException("chars", SR.ArgumentNull_Array);
250 }
251 if (index < 0 || count < 0)
252 {
253 throw new ArgumentOutOfRangeException((index < 0) ? "index" : "count", SR.ArgumentOutOfRange_NeedNonNegNum);
254 }
255 if (chars.Length - index < count)
256 {
258 }
259 if (count == 0)
260 {
261 return 0;
262 }
263 fixed (char* ptr = chars)
264 {
265 return GetByteCount(ptr + index, count, null);
266 }
267 }
268
269 public unsafe override int GetByteCount(string s)
270 {
271 if (s == null)
272 {
274 }
275 fixed (char* pChars = s)
276 {
277 return GetByteCount(pChars, s.Length, null);
278 }
279 }
280
281 [CLSCompliant(false)]
282 public unsafe override int GetByteCount(char* chars, int count)
283 {
284 if (chars == null)
285 {
286 throw new ArgumentNullException("chars", SR.ArgumentNull_Array);
287 }
288 if (count < 0)
289 {
291 }
292 return GetByteCount(chars, count, null);
293 }
294
295 public unsafe override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex)
296 {
297 if (s == null || bytes == null)
298 {
299 throw new ArgumentNullException((s == null) ? "s" : "bytes", SR.ArgumentNull_Array);
300 }
301 if (charIndex < 0 || charCount < 0)
302 {
303 throw new ArgumentOutOfRangeException((charIndex < 0) ? "charIndex" : "charCount", SR.ArgumentOutOfRange_NeedNonNegNum);
304 }
305 if (s.Length - charIndex < charCount)
306 {
308 }
309 if (byteIndex < 0 || byteIndex > bytes.Length)
310 {
312 }
313 int byteCount = bytes.Length - byteIndex;
314 fixed (char* ptr = s)
315 {
316 fixed (byte* ptr2 = &MemoryMarshal.GetReference<byte>(bytes))
317 {
318 return GetBytes(ptr + charIndex, charCount, ptr2 + byteIndex, byteCount, null);
319 }
320 }
321 }
322
323 public unsafe override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
324 {
325 if (chars == null || bytes == null)
326 {
327 throw new ArgumentNullException((chars == null) ? "chars" : "bytes", SR.ArgumentNull_Array);
328 }
329 if (charIndex < 0 || charCount < 0)
330 {
331 throw new ArgumentOutOfRangeException((charIndex < 0) ? "charIndex" : "charCount", SR.ArgumentOutOfRange_NeedNonNegNum);
332 }
333 if (chars.Length - charIndex < charCount)
334 {
336 }
337 if (byteIndex < 0 || byteIndex > bytes.Length)
338 {
340 }
341 if (charCount == 0)
342 {
343 return 0;
344 }
345 int byteCount = bytes.Length - byteIndex;
346 fixed (char* ptr = chars)
347 {
348 fixed (byte* ptr2 = &MemoryMarshal.GetReference<byte>(bytes))
349 {
350 return GetBytes(ptr + charIndex, charCount, ptr2 + byteIndex, byteCount, null);
351 }
352 }
353 }
354
355 [CLSCompliant(false)]
356 public unsafe override int GetBytes(char* chars, int charCount, byte* bytes, int byteCount)
357 {
358 if (bytes == null || chars == null)
359 {
360 throw new ArgumentNullException((bytes == null) ? "bytes" : "chars", SR.ArgumentNull_Array);
361 }
362 if (charCount < 0 || byteCount < 0)
363 {
364 throw new ArgumentOutOfRangeException((charCount < 0) ? "charCount" : "byteCount", SR.ArgumentOutOfRange_NeedNonNegNum);
365 }
366 return GetBytes(chars, charCount, bytes, byteCount, null);
367 }
368
369 public unsafe override int GetCharCount(byte[] bytes, int index, int count)
370 {
371 if (bytes == null)
372 {
373 throw new ArgumentNullException("bytes", SR.ArgumentNull_Array);
374 }
375 if (index < 0 || count < 0)
376 {
377 throw new ArgumentOutOfRangeException((index < 0) ? "index" : "count", SR.ArgumentOutOfRange_NeedNonNegNum);
378 }
379 if (bytes.Length - index < count)
380 {
382 }
383 if (count == 0)
384 {
385 return 0;
386 }
387 fixed (byte* ptr = bytes)
388 {
389 return GetCharCount(ptr + index, count, null);
390 }
391 }
392
393 [CLSCompliant(false)]
394 public unsafe override int GetCharCount(byte* bytes, int count)
395 {
396 if (bytes == null)
397 {
398 throw new ArgumentNullException("bytes", SR.ArgumentNull_Array);
399 }
400 if (count < 0)
401 {
403 }
404 return GetCharCount(bytes, count, null);
405 }
406
407 public unsafe override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
408 {
409 if (bytes == null || chars == null)
410 {
411 throw new ArgumentNullException((bytes == null) ? "bytes" : "chars", SR.ArgumentNull_Array);
412 }
413 if (byteIndex < 0 || byteCount < 0)
414 {
415 throw new ArgumentOutOfRangeException((byteIndex < 0) ? "byteIndex" : "byteCount", SR.ArgumentOutOfRange_NeedNonNegNum);
416 }
417 if (bytes.Length - byteIndex < byteCount)
418 {
420 }
421 if (charIndex < 0 || charIndex > chars.Length)
422 {
424 }
425 if (byteCount == 0)
426 {
427 return 0;
428 }
429 int charCount = chars.Length - charIndex;
430 fixed (byte* ptr = bytes)
431 {
432 fixed (char* ptr2 = &MemoryMarshal.GetReference<char>(chars))
433 {
434 return GetChars(ptr + byteIndex, byteCount, ptr2 + charIndex, charCount, null);
435 }
436 }
437 }
438
439 [CLSCompliant(false)]
440 public unsafe override int GetChars(byte* bytes, int byteCount, char* chars, int charCount)
441 {
442 if (bytes == null || chars == null)
443 {
444 throw new ArgumentNullException((bytes == null) ? "bytes" : "chars", SR.ArgumentNull_Array);
445 }
446 if (charCount < 0 || byteCount < 0)
447 {
448 throw new ArgumentOutOfRangeException((charCount < 0) ? "charCount" : "byteCount", SR.ArgumentOutOfRange_NeedNonNegNum);
449 }
450 return GetChars(bytes, byteCount, chars, charCount, null);
451 }
452
453 public unsafe override string GetString(byte[] bytes, int index, int count)
454 {
455 if (bytes == null)
456 {
457 throw new ArgumentNullException("bytes", SR.ArgumentNull_Array);
458 }
459 if (index < 0 || count < 0)
460 {
461 throw new ArgumentOutOfRangeException((index < 0) ? "index" : "count", SR.ArgumentOutOfRange_NeedNonNegNum);
462 }
463 if (bytes.Length - index < count)
464 {
466 }
467 if (count == 0)
468 {
469 return string.Empty;
470 }
471 fixed (byte* ptr = bytes)
472 {
473 return string.CreateStringFromEncoding(ptr + index, count, this);
474 }
475 }
476
477 internal unsafe sealed override int GetByteCount(char* chars, int count, EncoderNLS baseEncoder)
478 {
479 return GetBytes(chars, count, null, 0, baseEncoder);
480 }
481
482 internal unsafe sealed override int GetBytes(char* chars, int charCount, byte* bytes, int byteCount, EncoderNLS baseEncoder)
483 {
484 Encoder encoder = (Encoder)baseEncoder;
485 int num = 0;
486 int num2 = -1;
487 EncodingByteBuffer encodingByteBuffer = new EncodingByteBuffer(this, encoder, bytes, byteCount, chars, charCount);
488 if (encoder != null)
489 {
490 num = encoder.bits;
491 num2 = encoder.bitCount;
492 while (num2 >= 6)
493 {
494 num2 -= 6;
495 if (!encodingByteBuffer.AddByte(_base64Bytes[(num >> num2) & 0x3F]))
496 {
497 ThrowBytesOverflow(encoder, encodingByteBuffer.Count == 0);
498 }
499 }
500 }
501 while (encodingByteBuffer.MoreData)
502 {
503 char nextChar = encodingByteBuffer.GetNextChar();
504 if (nextChar < '\u0080' && _directEncode[(uint)nextChar])
505 {
506 if (num2 >= 0)
507 {
508 if (num2 > 0)
509 {
510 if (!encodingByteBuffer.AddByte(_base64Bytes[(num << 6 - num2) & 0x3F]))
511 {
512 break;
513 }
514 num2 = 0;
515 }
516 if (!encodingByteBuffer.AddByte(45))
517 {
518 break;
519 }
520 num2 = -1;
521 }
522 if (!encodingByteBuffer.AddByte((byte)nextChar))
523 {
524 break;
525 }
526 continue;
527 }
528 if (num2 < 0 && nextChar == '+')
529 {
530 if (!encodingByteBuffer.AddByte((byte)43, (byte)45))
531 {
532 break;
533 }
534 continue;
535 }
536 if (num2 < 0)
537 {
538 if (!encodingByteBuffer.AddByte(43))
539 {
540 break;
541 }
542 num2 = 0;
543 }
544 num = (num << 16) | nextChar;
545 num2 += 16;
546 while (num2 >= 6)
547 {
548 num2 -= 6;
549 if (!encodingByteBuffer.AddByte(_base64Bytes[(num >> num2) & 0x3F]))
550 {
551 num2 += 6;
552 encodingByteBuffer.GetNextChar();
553 break;
554 }
555 }
556 if (num2 >= 6)
557 {
558 break;
559 }
560 }
561 if (num2 >= 0 && (encoder == null || encoder.MustFlush))
562 {
563 if (num2 > 0 && encodingByteBuffer.AddByte(_base64Bytes[(num << 6 - num2) & 0x3F]))
564 {
565 num2 = 0;
566 }
567 if (encodingByteBuffer.AddByte(45))
568 {
569 num = 0;
570 num2 = -1;
571 }
572 else
573 {
574 encodingByteBuffer.GetNextChar();
575 }
576 }
577 if (bytes != null && encoder != null)
578 {
579 encoder.bits = num;
580 encoder.bitCount = num2;
581 encoder._charsUsed = encodingByteBuffer.CharsUsed;
582 }
583 return encodingByteBuffer.Count;
584 }
585
586 internal unsafe sealed override int GetCharCount(byte* bytes, int count, DecoderNLS baseDecoder)
587 {
588 return GetChars(bytes, count, null, 0, baseDecoder);
589 }
590
591 internal unsafe sealed override int GetChars(byte* bytes, int byteCount, char* chars, int charCount, DecoderNLS baseDecoder)
592 {
593 Decoder decoder = (Decoder)baseDecoder;
594 EncodingCharBuffer encodingCharBuffer = new EncodingCharBuffer(this, decoder, chars, charCount, bytes, byteCount);
595 int num = 0;
596 int num2 = -1;
597 bool flag = false;
598 if (decoder != null)
599 {
600 num = decoder.bits;
601 num2 = decoder.bitCount;
602 flag = decoder.firstByte;
603 }
604 if (num2 >= 16)
605 {
606 if (!encodingCharBuffer.AddChar((char)((uint)(num >> num2 - 16) & 0xFFFFu)))
607 {
608 ThrowCharsOverflow(decoder, nothingDecoded: true);
609 }
610 num2 -= 16;
611 }
612 while (encodingCharBuffer.MoreData)
613 {
614 byte nextByte = encodingCharBuffer.GetNextByte();
615 int num3;
616 if (num2 >= 0)
617 {
618 sbyte b;
619 if (nextByte < 128 && (b = _base64Values[nextByte]) >= 0)
620 {
621 flag = false;
622 num = (num << 6) | (byte)b;
623 num2 += 6;
624 if (num2 < 16)
625 {
626 continue;
627 }
628 num3 = (num >> num2 - 16) & 0xFFFF;
629 num2 -= 16;
630 }
631 else
632 {
633 num2 = -1;
634 if (nextByte != 45)
635 {
636 if (!encodingCharBuffer.Fallback(nextByte))
637 {
638 break;
639 }
640 continue;
641 }
642 if (!flag)
643 {
644 continue;
645 }
646 num3 = 43;
647 }
648 }
649 else
650 {
651 if (nextByte == 43)
652 {
653 num2 = 0;
654 flag = true;
655 continue;
656 }
657 if (nextByte >= 128)
658 {
659 if (!encodingCharBuffer.Fallback(nextByte))
660 {
661 break;
662 }
663 continue;
664 }
665 num3 = nextByte;
666 }
667 if (num3 >= 0 && !encodingCharBuffer.AddChar((char)num3))
668 {
669 if (num2 >= 0)
670 {
671 encodingCharBuffer.AdjustBytes(1);
672 num2 += 16;
673 }
674 break;
675 }
676 }
677 if (chars != null && decoder != null)
678 {
679 if (decoder.MustFlush)
680 {
681 decoder.bits = 0;
682 decoder.bitCount = -1;
683 decoder.firstByte = false;
684 }
685 else
686 {
687 decoder.bits = num;
688 decoder.bitCount = num2;
689 decoder.firstByte = flag;
690 }
691 decoder._bytesUsed = encodingCharBuffer.BytesUsed;
692 }
693 return encodingCharBuffer.Count;
694 }
695
697 {
698 return new Decoder(this);
699 }
700
702 {
703 return new Encoder(this);
704 }
705
706 public override int GetMaxByteCount(int charCount)
707 {
708 if (charCount < 0)
709 {
711 }
712 long num = (long)charCount * 3L + 2;
713 if (num > int.MaxValue)
714 {
716 }
717 return (int)num;
718 }
719
720 public override int GetMaxCharCount(int byteCount)
721 {
722 if (byteCount < 0)
723 {
725 }
726 int num = byteCount;
727 if (num == 0)
728 {
729 num = 1;
730 }
731 return num;
732 }
733}
static string ArgumentOutOfRange_Index
Definition SR.cs:30
static string Argument_InvalidCharSequenceNoIndex
Definition SR.cs:92
static string ArgumentOutOfRange_IndexCount
Definition SR.cs:80
static string ArgumentOutOfRange_GetByteCountOverflow
Definition SR.cs:88
static string ArgumentOutOfRange_IndexCountBuffer
Definition SR.cs:78
static string ArgumentNull_Array
Definition SR.cs:24
static string ArgumentOutOfRange_NeedNonNegNum
Definition SR.cs:32
Definition SR.cs:7
DecoderFallbackBuffer _fallbackBuffer
Definition Decoder.cs:9
EncoderFallbackBuffer _fallbackBuffer
Definition Encoder.cs:9
unsafe bool AddByte(byte b, int moreBytesExpected)
Definition Encoding.cs:271
unsafe bool AddChar(char ch, int numBytes)
Definition Encoding.cs:145
bool Fallback(byte fallbackByte)
Definition Encoding.cs:180
unsafe void AdjustBytes(int count)
Definition Encoding.cs:166
EncoderFallback encoderFallback
Definition Encoding.cs:341
DecoderFallback decoderFallback
Definition Encoding.cs:343
virtual int CodePage
Definition Encoding.cs:515
unsafe override int InternalFallback(byte[] bytes, byte *pBytes)
override bool Fallback(byte[] bytesUnknown, int index)
override DecoderFallbackBuffer CreateFallbackBuffer()
override bool Equals([NotNullWhen(true)] object value)
Decoder(UTF7Encoding encoding)
Encoder(UTF7Encoding encoding)
unsafe override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
unsafe override int GetChars(byte *bytes, int byteCount, char *chars, int charCount, DecoderNLS baseDecoder)
override void SetDefaultFallbacks()
override System.Text.Decoder GetDecoder()
unsafe override int GetByteCount(string s)
unsafe override int GetCharCount(byte *bytes, int count, DecoderNLS baseDecoder)
unsafe override int GetChars(byte *bytes, int byteCount, char *chars, int charCount)
unsafe override int GetCharCount(byte[] bytes, int index, int count)
readonly bool _allowOptionals
override bool Equals([NotNullWhen(true)] object? value)
unsafe override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
UTF7Encoding(bool allowOptionals)
override System.Text.Encoder GetEncoder()
override int GetMaxCharCount(int byteCount)
unsafe override int GetByteCount(char[] chars, int index, int count)
unsafe override int GetByteCount(char *chars, int count)
static readonly UTF7Encoding s_default
unsafe override int GetCharCount(byte *bytes, int count)
unsafe override int GetBytes(char *chars, int charCount, byte *bytes, int byteCount, EncoderNLS baseEncoder)
unsafe override int GetByteCount(char *chars, int count, EncoderNLS baseEncoder)
override int GetHashCode()
unsafe override int GetBytes(char *chars, int charCount, byte *bytes, int byteCount)
unsafe override string GetString(byte[] bytes, int index, int count)
override int GetMaxByteCount(int charCount)
unsafe override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex)
static void ThrowArgumentNullException(string name)