Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
QEncodedStream.cs
Go to the documentation of this file.
1using System.IO;
2using System.Text;
3
4namespace System.Net.Mime;
5
7{
8 private sealed class ReadStateInfo
9 {
10 internal bool IsEscaped { get; set; }
11
12 internal short Byte { get; set; } = -1;
13
14 }
15
16 private sealed class WriteAsyncResult : System.Net.LazyAsyncResult
17 {
18 private static readonly AsyncCallback s_onWrite = OnWrite;
19
20 private readonly QEncodedStream _parent;
21
22 private readonly byte[] _buffer;
23
24 private readonly int _offset;
25
26 private readonly int _count;
27
28 private int _written;
29
30 internal WriteAsyncResult(QEncodedStream parent, byte[] buffer, int offset, int count, AsyncCallback callback, object state)
31 : base(null, state, callback)
32 {
33 _parent = parent;
36 _count = count;
37 }
38
39 private void CompleteWrite(IAsyncResult result)
40 {
43 }
44
45 internal static void End(IAsyncResult result)
46 {
47 WriteAsyncResult writeAsyncResult = (WriteAsyncResult)result;
48 writeAsyncResult.InternalWaitForCompletion();
49 }
50
51 private static void OnWrite(IAsyncResult result)
52 {
53 if (!result.CompletedSynchronously)
54 {
55 WriteAsyncResult writeAsyncResult = (WriteAsyncResult)result.AsyncState;
56 try
57 {
58 writeAsyncResult.CompleteWrite(result);
59 writeAsyncResult.Write();
60 }
61 catch (Exception result2)
62 {
63 writeAsyncResult.InvokeCallback(result2);
64 }
65 }
66 }
67
68 internal void Write()
69 {
70 while (true)
71 {
73 if (_written < _count)
74 {
76 if (asyncResult.CompletedSynchronously)
77 {
79 continue;
80 }
81 break;
82 }
84 break;
85 }
86 }
87 }
88
90
92
93 private readonly IByteEncoder _encoder;
94
95 private static ReadOnlySpan<byte> HexDecodeMap => new byte[256]
96 {
97 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
98 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
99 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
100 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
101 255, 255, 255, 255, 255, 255, 255, 255, 0, 1,
102 2, 3, 4, 5, 6, 7, 8, 9, 255, 255,
103 255, 255, 255, 255, 255, 10, 11, 12, 13, 14,
104 15, 255, 255, 255, 255, 255, 255, 255, 255, 255,
105 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
106 255, 255, 255, 255, 255, 255, 255, 10, 11, 12,
107 13, 14, 15, 255, 255, 255, 255, 255, 255, 255,
108 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
109 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
110 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
111 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
112 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
113 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
114 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
115 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
116 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
117 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
118 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
119 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
120 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
121 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
122 255, 255, 255, 255, 255, 255
123 };
124
125 private ReadStateInfo ReadState => _readState ?? (_readState = new ReadStateInfo());
126
127 internal WriteStateInfoBase WriteState => _writeState;
128
130 : base(new MemoryStream())
131 {
132 _writeState = wsi;
134 }
135
136 public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
137 {
139 WriteAsyncResult writeAsyncResult = new WriteAsyncResult(this, buffer, offset, count, callback, state);
140 writeAsyncResult.Write();
141 return writeAsyncResult;
142 }
143
144 public override void Close()
145 {
147 base.Close();
148 }
149
150 public unsafe int DecodeBytes(byte[] buffer, int offset, int count)
151 {
152 fixed (byte* ptr = buffer)
153 {
154 byte* ptr2 = ptr + offset;
155 byte* ptr3 = ptr2;
156 byte* ptr4 = ptr2;
157 byte* ptr5 = ptr2 + count;
158 if (ReadState.IsEscaped)
159 {
160 if (ReadState.Byte == -1)
161 {
162 if (count == 1)
163 {
164 ReadState.Byte = *ptr3;
165 return 0;
166 }
167 if (*ptr3 != 13 || ptr3[1] != 10)
168 {
169 byte b = HexDecodeMap[*ptr3];
170 byte b2 = HexDecodeMap[ptr3[1]];
171 if (b == byte.MaxValue)
172 {
174 }
175 if (b2 == byte.MaxValue)
176 {
178 }
179 *(ptr4++) = (byte)((b << 4) + b2);
180 }
181 ptr3 += 2;
182 }
183 else
184 {
185 if (ReadState.Byte != 13 || *ptr3 != 10)
186 {
187 byte b3 = HexDecodeMap[ReadState.Byte];
188 byte b4 = HexDecodeMap[*ptr3];
189 if (b3 == byte.MaxValue)
190 {
192 }
193 if (b4 == byte.MaxValue)
194 {
196 }
197 *(ptr4++) = (byte)((b3 << 4) + b4);
198 }
199 ptr3++;
200 }
201 ReadState.IsEscaped = false;
202 ReadState.Byte = -1;
203 }
204 while (ptr3 < ptr5)
205 {
206 if (*ptr3 != 61)
207 {
208 if (*ptr3 == 95)
209 {
210 *(ptr4++) = 32;
211 ptr3++;
212 }
213 else
214 {
215 *(ptr4++) = *(ptr3++);
216 }
217 continue;
218 }
219 long num = ptr5 - ptr3;
220 if (num != 1)
221 {
222 if (num != 2)
223 {
224 if (ptr3[1] != 13 || ptr3[2] != 10)
225 {
226 byte b5 = HexDecodeMap[ptr3[1]];
227 byte b6 = HexDecodeMap[ptr3[2]];
228 if (b5 == byte.MaxValue)
229 {
231 }
232 if (b6 == byte.MaxValue)
233 {
235 }
236 *(ptr4++) = (byte)((b5 << 4) + b6);
237 }
238 ptr3 += 3;
239 continue;
240 }
241 ReadState.Byte = ptr3[1];
242 }
243 ReadState.IsEscaped = true;
244 break;
245 }
246 return (int)(ptr4 - ptr2);
247 }
248 }
249
250 public int EncodeBytes(byte[] buffer, int offset, int count)
251 {
252 return _encoder.EncodeBytes(buffer, offset, count, dontDeferFinalBytes: true, shouldAppendSpaceToCRLF: true);
253 }
254
255 public int EncodeString(string value, Encoding encoding)
256 {
257 return _encoder.EncodeString(value, encoding);
258 }
259
260 public string GetEncodedString()
261 {
262 return _encoder.GetEncodedString();
263 }
264
265 public override void EndWrite(IAsyncResult asyncResult)
266 {
268 }
269
270 public override void Flush()
271 {
273 base.Flush();
274 }
275
276 private void FlushInternal()
277 {
278 if (_writeState != null && _writeState.Length > 0)
279 {
280 base.Write(WriteState.Buffer, 0, WriteState.Length);
281 WriteState.Reset();
282 }
283 }
284
285 public override void Write(byte[] buffer, int offset, int count)
286 {
288 int num = 0;
289 while (true)
290 {
291 num += EncodeBytes(buffer, offset + num, count - num);
292 if (num < count)
293 {
295 continue;
296 }
297 break;
298 }
299 }
300}
static void ValidateBufferArguments(byte[] buffer, int offset, int count)
Definition Stream.cs:1044
virtual void EndWrite(IAsyncResult asyncResult)
Definition Stream.cs:889
virtual IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state)
Definition Stream.cs:813
void InvokeCallback(object result)
static void OnWrite(IAsyncResult result)
WriteAsyncResult(QEncodedStream parent, byte[] buffer, int offset, int count, AsyncCallback callback, object state)
readonly IByteEncoder _encoder
override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
override void Write(byte[] buffer, int offset, int count)
unsafe int DecodeBytes(byte[] buffer, int offset, int count)
static ReadOnlySpan< byte > HexDecodeMap
int EncodeString(string value, Encoding encoding)
override void EndWrite(IAsyncResult asyncResult)
int EncodeBytes(byte[] buffer, int offset, int count)
QEncodedStream(WriteStateInfoBase wsi)
readonly WriteStateInfoBase _writeState
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string InvalidHexDigit
Definition SR.cs:46
Definition SR.cs:7
int EncodeString(string value, Encoding encoding)
int EncodeBytes(byte[] buffer, int offset, int count, bool dontDeferFinalBytes, bool shouldAppendSpaceToCRLF)