Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
Base64Decoder.cs
Go to the documentation of this file.
1namespace System.Xml;
2
3internal sealed class Base64Decoder : IncrementalReadDecoder
4{
5 private byte[] _buffer;
6
7 private int _startIndex;
8
9 private int _curIndex;
10
11 private int _endIndex;
12
13 private int _bits;
14
15 private int _bitsFilled;
16
17 private static readonly byte[] s_mapBase64 = ConstructMapBase64();
18
19 internal override int DecodedCount => _curIndex - _startIndex;
20
21 internal override bool IsFull => _curIndex == _endIndex;
22
23 internal override int Decode(char[] chars, int startPos, int len)
24 {
25 if (chars == null)
26 {
27 throw new ArgumentNullException("chars");
28 }
29 if (len < 0)
30 {
31 throw new ArgumentOutOfRangeException("len");
32 }
33 if (startPos < 0)
34 {
35 throw new ArgumentOutOfRangeException("startPos");
36 }
37 if (chars.Length - startPos < len)
38 {
39 throw new ArgumentOutOfRangeException("len");
40 }
41 if (len == 0)
42 {
43 return 0;
44 }
47 return charsDecoded;
48 }
49
50 internal override int Decode(string str, int startPos, int len)
51 {
52 if (str == null)
53 {
54 throw new ArgumentNullException("str");
55 }
56 if (len < 0)
57 {
58 throw new ArgumentOutOfRangeException("len");
59 }
60 if (startPos < 0)
61 {
62 throw new ArgumentOutOfRangeException("startPos");
63 }
64 if (str.Length - startPos < len)
65 {
66 throw new ArgumentOutOfRangeException("len");
67 }
68 if (len == 0)
69 {
70 return 0;
71 }
74 return charsDecoded;
75 }
76
77 internal override void Reset()
78 {
79 _bitsFilled = 0;
80 _bits = 0;
81 }
82
83 internal override void SetNextOutputBuffer(Array buffer, int index, int count)
84 {
85 _buffer = (byte[])buffer;
89 }
90
91 private static byte[] ConstructMapBase64()
92 {
93 byte[] array = new byte[123];
94 for (int i = 0; i < array.Length; i++)
95 {
96 array[i] = byte.MaxValue;
97 }
98 for (int j = 0; j < "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".Length; j++)
99 {
100 array[(uint)"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[j]] = (byte)j;
101 }
102 return array;
103 }
104
106 {
107 int num = 0;
108 int num2 = 0;
109 int num3 = _bits;
110 int num4 = _bitsFilled;
111 while (true)
112 {
113 if ((uint)num2 < (uint)chars.Length && (uint)num < (uint)bytes.Length)
114 {
115 char c = chars[num2];
116 if (c != '=')
117 {
118 num2++;
120 {
121 continue;
122 }
123 int num5;
124 if (c > 'z' || (num5 = s_mapBase64[(uint)c]) == 255)
125 {
126 throw new XmlException(System.SR.Xml_InvalidBase64Value, chars.ToString());
127 }
128 num3 = (num3 << 6) | num5;
129 num4 += 6;
130 if (num4 >= 8)
131 {
132 bytes[num++] = (byte)((uint)(num3 >> num4 - 8) & 0xFFu);
133 num4 -= 8;
134 if (num == bytes.Length)
135 {
136 break;
137 }
138 }
139 continue;
140 }
141 }
142 if ((uint)num2 >= (uint)chars.Length || chars[num2] != '=')
143 {
144 break;
145 }
146 num4 = 0;
147 do
148 {
149 num2++;
150 }
151 while ((uint)num2 < (uint)chars.Length && chars[num2] == '=');
152 while ((uint)num2 < (uint)chars.Length)
153 {
155 {
156 throw new XmlException(System.SR.Xml_InvalidBase64Value, chars.ToString());
157 }
158 }
159 break;
160 }
161 _bits = num3;
163 bytesDecoded = num;
165 }
166}
static string Xml_InvalidBase64Value
Definition SR.cs:118
Definition SR.cs:7
override int Decode(string str, int startPos, int len)
static readonly byte[] s_mapBase64
override int Decode(char[] chars, int startPos, int len)
override void SetNextOutputBuffer(Array buffer, int index, int count)
void Decode(ReadOnlySpan< char > chars, Span< byte > bytes, out int charsDecoded, out int bytesDecoded)
static byte[] ConstructMapBase64()
static bool IsWhiteSpace(char ch)