Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
QuotedPrintableStream.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 {
19
20 private readonly byte[] _buffer;
21
22 private readonly int _offset;
23
24 private readonly int _count;
25
26 private static readonly AsyncCallback s_onWrite = OnWrite;
27
28 private int _written;
29
30 internal WriteAsyncResult(QuotedPrintableStream 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
89 private readonly bool _encodeCRLF;
90
91 private readonly int _lineLength;
92
94
96
97 private static ReadOnlySpan<byte> HexDecodeMap => new byte[256]
98 {
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, 255, 255,
102 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
103 255, 255, 255, 255, 255, 255, 255, 255, 0, 1,
104 2, 3, 4, 5, 6, 7, 8, 9, 255, 255,
105 255, 255, 255, 255, 255, 10, 11, 12, 13, 14,
106 15, 255, 255, 255, 255, 255, 255, 255, 255, 255,
107 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
108 255, 255, 255, 255, 255, 255, 255, 10, 11, 12,
109 13, 14, 15, 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, 255, 255, 255, 255,
123 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
124 255, 255, 255, 255, 255, 255
125 };
126
127 private static ReadOnlySpan<byte> HexEncodeMap => "0123456789ABCDEF"u8;
128
129 private ReadStateInfo ReadState => _readState ?? (_readState = new ReadStateInfo());
130
131 internal WriteStateInfoBase WriteState => _writeState ?? (_writeState = new WriteStateInfoBase(1024, null, null, _lineLength));
132
133 internal QuotedPrintableStream(Stream stream, int lineLength)
134 : base(stream)
135 {
136 if (lineLength < 0)
137 {
138 throw new ArgumentOutOfRangeException("lineLength");
139 }
140 _lineLength = lineLength;
141 }
142
143 internal QuotedPrintableStream(Stream stream, bool encodeCRLF)
144 : this(stream, 70)
145 {
146 _encodeCRLF = encodeCRLF;
147 }
148
149 public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
150 {
152 WriteAsyncResult writeAsyncResult = new WriteAsyncResult(this, buffer, offset, count, callback, state);
153 writeAsyncResult.Write();
154 return writeAsyncResult;
155 }
156
157 public override void Close()
158 {
160 base.Close();
161 }
162
163 public unsafe int DecodeBytes(byte[] buffer, int offset, int count)
164 {
165 fixed (byte* ptr = buffer)
166 {
167 byte* ptr2 = ptr + offset;
168 byte* ptr3 = ptr2;
169 byte* ptr4 = ptr2;
170 byte* ptr5 = ptr2 + count;
171 if (ReadState.IsEscaped)
172 {
173 if (ReadState.Byte == -1)
174 {
175 if (count == 1)
176 {
177 ReadState.Byte = *ptr3;
178 return 0;
179 }
180 if (*ptr3 != 13 || ptr3[1] != 10)
181 {
182 byte b = HexDecodeMap[*ptr3];
183 byte b2 = HexDecodeMap[ptr3[1]];
184 if (b == byte.MaxValue)
185 {
187 }
188 if (b2 == byte.MaxValue)
189 {
191 }
192 *(ptr4++) = (byte)((b << 4) + b2);
193 }
194 ptr3 += 2;
195 }
196 else
197 {
198 if (ReadState.Byte != 13 || *ptr3 != 10)
199 {
200 byte b3 = HexDecodeMap[ReadState.Byte];
201 byte b4 = HexDecodeMap[*ptr3];
202 if (b3 == byte.MaxValue)
203 {
205 }
206 if (b4 == byte.MaxValue)
207 {
209 }
210 *(ptr4++) = (byte)((b3 << 4) + b4);
211 }
212 ptr3++;
213 }
214 ReadState.IsEscaped = false;
215 ReadState.Byte = -1;
216 }
217 while (ptr3 < ptr5)
218 {
219 if (*ptr3 != 61)
220 {
221 *(ptr4++) = *(ptr3++);
222 continue;
223 }
224 long num = ptr5 - ptr3;
225 if (num != 1)
226 {
227 if (num != 2)
228 {
229 if (ptr3[1] != 13 || ptr3[2] != 10)
230 {
231 byte b5 = HexDecodeMap[ptr3[1]];
232 byte b6 = HexDecodeMap[ptr3[2]];
233 if (b5 == byte.MaxValue)
234 {
236 }
237 if (b6 == byte.MaxValue)
238 {
240 }
241 *(ptr4++) = (byte)((b5 << 4) + b6);
242 }
243 ptr3 += 3;
244 continue;
245 }
246 ReadState.Byte = ptr3[1];
247 }
248 ReadState.IsEscaped = true;
249 break;
250 }
251 return (int)(ptr4 - ptr2);
252 }
253 }
254
255 public int EncodeBytes(byte[] buffer, int offset, int count)
256 {
257 int i;
258 for (i = offset; i < count + offset; i++)
259 {
260 if ((_lineLength != -1 && WriteState.CurrentLineLength + 3 + 2 >= _lineLength && (buffer[i] == 32 || buffer[i] == 9 || buffer[i] == 13 || buffer[i] == 10)) || _writeState.CurrentLineLength + 3 + 2 >= 70)
261 {
262 if (WriteState.Buffer.Length - WriteState.Length < 3)
263 {
264 return i - offset;
265 }
266 WriteState.Append(61);
267 WriteState.AppendCRLF(includeSpace: false);
268 }
269 if (buffer[i] == 13 && i + 1 < count + offset && buffer[i + 1] == 10)
270 {
271 if (WriteState.Buffer.Length - WriteState.Length < (_encodeCRLF ? 6 : 2))
272 {
273 return i - offset;
274 }
275 i++;
276 if (_encodeCRLF)
277 {
278 WriteState.Append(61, 48, 68, 61, 48, 65);
279 }
280 else
281 {
282 WriteState.AppendCRLF(includeSpace: false);
283 }
284 continue;
285 }
286 if ((buffer[i] < 32 && buffer[i] != 9) || buffer[i] == 61 || buffer[i] > 126)
287 {
288 if (WriteState.Buffer.Length - WriteState.Length < 3)
289 {
290 return i - offset;
291 }
292 WriteState.Append(61);
293 WriteState.Append(HexEncodeMap[buffer[i] >> 4]);
294 WriteState.Append(HexEncodeMap[buffer[i] & 0xF]);
295 continue;
296 }
297 if (WriteState.Buffer.Length - WriteState.Length < 1)
298 {
299 return i - offset;
300 }
301 if ((buffer[i] == 9 || buffer[i] == 32) && i + 1 >= count + offset)
302 {
303 if (WriteState.Buffer.Length - WriteState.Length < 3)
304 {
305 return i - offset;
306 }
307 WriteState.Append(61);
308 WriteState.Append(HexEncodeMap[buffer[i] >> 4]);
309 WriteState.Append(HexEncodeMap[buffer[i] & 0xF]);
310 }
311 else
312 {
313 WriteState.Append(buffer[i]);
314 }
315 }
316 return i - offset;
317 }
318
319 public int EncodeString(string value, Encoding encoding)
320 {
321 byte[] bytes = encoding.GetBytes(value);
322 return EncodeBytes(bytes, 0, bytes.Length);
323 }
324
325 public string GetEncodedString()
326 {
327 return Encoding.ASCII.GetString(WriteState.Buffer, 0, WriteState.Length);
328 }
329
330 public override void EndWrite(IAsyncResult asyncResult)
331 {
333 }
334
335 public override void Flush()
336 {
338 base.Flush();
339 }
340
341 private void FlushInternal()
342 {
343 if (_writeState != null && _writeState.Length > 0)
344 {
345 base.Write(WriteState.Buffer, 0, WriteState.Length);
346 WriteState.BufferFlushed();
347 }
348 }
349
350 public override void Write(byte[] buffer, int offset, int count)
351 {
353 int num = 0;
354 while (true)
355 {
356 num += EncodeBytes(buffer, offset + num, count - num);
357 if (num < count)
358 {
360 continue;
361 }
362 break;
363 }
364 }
365}
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)
WriteAsyncResult(QuotedPrintableStream parent, byte[] buffer, int offset, int count, AsyncCallback callback, object state)
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)
int EncodeString(string value, Encoding encoding)
int EncodeBytes(byte[] buffer, int offset, int count)
override void EndWrite(IAsyncResult asyncResult)
QuotedPrintableStream(Stream stream, int lineLength)
QuotedPrintableStream(Stream stream, bool encodeCRLF)
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string InvalidHexDigit
Definition SR.cs:46
Definition SR.cs:7
static Encoding ASCII
Definition Encoding.cs:511
virtual byte[] GetBytes(char[] chars)
Definition Encoding.cs:781