Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
ReadContentAsBinaryHelper.cs
Go to the documentation of this file.
2
3namespace System.Xml;
4
5internal sealed class ReadContentAsBinaryHelper
6{
7 private enum State
8 {
9 None,
12 }
13
14 private readonly XmlReader _reader;
15
16 private State _state;
17
18 private int _valueOffset;
19
20 private bool _isEnd;
21
22 private readonly bool _canReadValueChunk;
23
24 private readonly char[] _valueChunk;
25
26 private int _valueChunkLength;
27
29
31
33
35 {
36 _reader = reader;
39 {
40 _valueChunk = new char[256];
41 }
42 }
43
45 {
46 if (helper == null)
47 {
48 return new ReadContentAsBinaryHelper(reader);
49 }
50 helper.Reset();
51 return helper;
52 }
53
54 internal int ReadContentAsBase64(byte[] buffer, int index, int count)
55 {
56 if (buffer == null)
57 {
58 throw new ArgumentNullException("buffer");
59 }
60 if (count < 0)
61 {
62 throw new ArgumentOutOfRangeException("count");
63 }
64 if (index < 0)
65 {
66 throw new ArgumentOutOfRangeException("index");
67 }
68 if (buffer.Length - index < count)
69 {
70 throw new ArgumentOutOfRangeException("count");
71 }
72 switch (_state)
73 {
74 case State.None:
76 {
77 throw _reader.CreateReadContentAsException("ReadContentAsBase64");
78 }
79 if (!Init())
80 {
81 return 0;
82 }
83 break;
84 case State.InReadContent:
86 {
88 }
89 break;
90 case State.InReadElementContent:
92 default:
93 return 0;
94 }
97 }
98
99 internal int ReadContentAsBinHex(byte[] buffer, int index, int count)
100 {
101 if (buffer == null)
102 {
103 throw new ArgumentNullException("buffer");
104 }
105 if (count < 0)
106 {
107 throw new ArgumentOutOfRangeException("count");
108 }
109 if (index < 0)
110 {
111 throw new ArgumentOutOfRangeException("index");
112 }
113 if (buffer.Length - index < count)
114 {
115 throw new ArgumentOutOfRangeException("count");
116 }
117 switch (_state)
118 {
119 case State.None:
121 {
122 throw _reader.CreateReadContentAsException("ReadContentAsBinHex");
123 }
124 if (!Init())
125 {
126 return 0;
127 }
128 break;
129 case State.InReadContent:
131 {
133 }
134 break;
135 case State.InReadElementContent:
137 default:
138 return 0;
139 }
142 }
143
144 internal int ReadElementContentAsBase64(byte[] buffer, int index, int count)
145 {
146 if (buffer == null)
147 {
148 throw new ArgumentNullException("buffer");
149 }
150 if (count < 0)
151 {
152 throw new ArgumentOutOfRangeException("count");
153 }
154 if (index < 0)
155 {
156 throw new ArgumentOutOfRangeException("index");
157 }
158 if (buffer.Length - index < count)
159 {
160 throw new ArgumentOutOfRangeException("count");
161 }
162 switch (_state)
163 {
164 case State.None:
165 if (_reader.NodeType != XmlNodeType.Element)
166 {
167 throw _reader.CreateReadElementContentAsException("ReadElementContentAsBase64");
168 }
169 if (!InitOnElement())
170 {
171 return 0;
172 }
173 break;
174 case State.InReadContent:
176 case State.InReadElementContent:
178 {
180 }
181 break;
182 default:
183 return 0;
184 }
187 }
188
189 internal int ReadElementContentAsBinHex(byte[] buffer, int index, int count)
190 {
191 if (buffer == null)
192 {
193 throw new ArgumentNullException("buffer");
194 }
195 if (count < 0)
196 {
197 throw new ArgumentOutOfRangeException("count");
198 }
199 if (index < 0)
200 {
201 throw new ArgumentOutOfRangeException("index");
202 }
203 if (buffer.Length - index < count)
204 {
205 throw new ArgumentOutOfRangeException("count");
206 }
207 switch (_state)
208 {
209 case State.None:
210 if (_reader.NodeType != XmlNodeType.Element)
211 {
212 throw _reader.CreateReadElementContentAsException("ReadElementContentAsBinHex");
213 }
214 if (!InitOnElement())
215 {
216 return 0;
217 }
218 break;
219 case State.InReadContent:
221 case State.InReadElementContent:
223 {
225 }
226 break;
227 default:
228 return 0;
229 }
232 }
233
234 internal void Finish()
235 {
236 if (_state != 0)
237 {
239 {
240 }
241 if (_state == State.InReadElementContent)
242 {
243 if (_reader.NodeType != XmlNodeType.EndElement)
244 {
246 }
247 _reader.Read();
248 }
249 }
250 Reset();
251 }
252
253 internal void Reset()
254 {
255 _state = State.None;
256 _isEnd = false;
257 _valueOffset = 0;
258 }
259
260 private bool Init()
261 {
263 {
264 return false;
265 }
266 _state = State.InReadContent;
267 _isEnd = false;
268 return true;
269 }
270
271 private bool InitOnElement()
272 {
274 _reader.Read();
275 if (isEmptyElement)
276 {
277 return false;
278 }
280 {
281 if (_reader.NodeType != XmlNodeType.EndElement)
282 {
284 }
285 _reader.Read();
286 return false;
287 }
288 _state = State.InReadElementContent;
289 _isEnd = false;
290 return true;
291 }
292
293 private void InitBase64Decoder()
294 {
295 if (_base64Decoder == null)
296 {
298 }
299 else
300 {
302 }
304 }
305
306 private void InitBinHexDecoder()
307 {
308 if (_binHexDecoder == null)
309 {
311 }
312 else
313 {
315 }
317 }
318
319 private int ReadContentAsBinary(byte[] buffer, int index, int count)
320 {
321 if (_isEnd)
322 {
323 Reset();
324 return 0;
325 }
327 do
328 {
330 {
331 while (true)
332 {
334 {
336 _valueOffset += num;
337 }
338 if (_decoder.IsFull)
339 {
340 return _decoder.DecodedCount;
341 }
343 {
344 break;
345 }
346 _valueOffset = 0;
347 }
348 }
349 else
350 {
351 string value = _reader.Value;
354 if (_decoder.IsFull)
355 {
356 return _decoder.DecodedCount;
357 }
358 }
359 _valueOffset = 0;
360 }
362 _isEnd = true;
363 return _decoder.DecodedCount;
364 }
365
366 private int ReadElementContentAsBinary(byte[] buffer, int index, int count)
367 {
368 if (count == 0)
369 {
370 return 0;
371 }
373 if (num > 0)
374 {
375 return num;
376 }
377 if (_reader.NodeType != XmlNodeType.EndElement)
378 {
380 }
381 _reader.Read();
382 _state = State.None;
383 return 0;
384 }
385
387 {
388 do
389 {
390 switch (_reader.NodeType)
391 {
392 case XmlNodeType.Attribute:
393 return !moveIfOnContentNode;
394 case XmlNodeType.Text:
395 case XmlNodeType.CDATA:
396 case XmlNodeType.Whitespace:
397 case XmlNodeType.SignificantWhitespace:
399 {
400 return true;
401 }
402 goto IL_0078;
403 case XmlNodeType.EntityReference:
405 {
406 break;
407 }
409 goto IL_0078;
410 case XmlNodeType.ProcessingInstruction:
411 case XmlNodeType.Comment:
412 case XmlNodeType.EndEntity:
413 goto IL_0078;
414 }
415 return false;
416 IL_0078:
417 moveIfOnContentNode = false;
418 }
419 while (_reader.Read());
420 return false;
421 }
422
424 {
425 if (buffer == null)
426 {
427 throw new ArgumentNullException("buffer");
428 }
429 if (count < 0)
430 {
431 throw new ArgumentOutOfRangeException("count");
432 }
433 if (index < 0)
434 {
435 throw new ArgumentOutOfRangeException("index");
436 }
437 if (buffer.Length - index < count)
438 {
439 throw new ArgumentOutOfRangeException("count");
440 }
441 switch (_state)
442 {
443 case State.None:
445 {
446 throw _reader.CreateReadContentAsException("ReadContentAsBase64");
447 }
448 if (!(await InitAsync().ConfigureAwait(continueOnCapturedContext: false)))
449 {
450 return 0;
451 }
452 break;
453 case State.InReadContent:
455 {
457 }
458 break;
459 case State.InReadElementContent:
461 default:
462 return 0;
463 }
466 }
467
469 {
470 if (buffer == null)
471 {
472 throw new ArgumentNullException("buffer");
473 }
474 if (count < 0)
475 {
476 throw new ArgumentOutOfRangeException("count");
477 }
478 if (index < 0)
479 {
480 throw new ArgumentOutOfRangeException("index");
481 }
482 if (buffer.Length - index < count)
483 {
484 throw new ArgumentOutOfRangeException("count");
485 }
486 switch (_state)
487 {
488 case State.None:
490 {
491 throw _reader.CreateReadContentAsException("ReadContentAsBinHex");
492 }
493 if (!(await InitAsync().ConfigureAwait(continueOnCapturedContext: false)))
494 {
495 return 0;
496 }
497 break;
498 case State.InReadContent:
500 {
502 }
503 break;
504 case State.InReadElementContent:
506 default:
507 return 0;
508 }
511 }
512
514 {
515 if (buffer == null)
516 {
517 throw new ArgumentNullException("buffer");
518 }
519 if (count < 0)
520 {
521 throw new ArgumentOutOfRangeException("count");
522 }
523 if (index < 0)
524 {
525 throw new ArgumentOutOfRangeException("index");
526 }
527 if (buffer.Length - index < count)
528 {
529 throw new ArgumentOutOfRangeException("count");
530 }
531 switch (_state)
532 {
533 case State.None:
534 if (_reader.NodeType != XmlNodeType.Element)
535 {
536 throw _reader.CreateReadElementContentAsException("ReadElementContentAsBase64");
537 }
538 if (!(await InitOnElementAsync().ConfigureAwait(continueOnCapturedContext: false)))
539 {
540 return 0;
541 }
542 break;
543 case State.InReadContent:
545 case State.InReadElementContent:
547 {
549 }
550 break;
551 default:
552 return 0;
553 }
556 }
557
559 {
560 if (buffer == null)
561 {
562 throw new ArgumentNullException("buffer");
563 }
564 if (count < 0)
565 {
566 throw new ArgumentOutOfRangeException("count");
567 }
568 if (index < 0)
569 {
570 throw new ArgumentOutOfRangeException("index");
571 }
572 if (buffer.Length - index < count)
573 {
574 throw new ArgumentOutOfRangeException("count");
575 }
576 switch (_state)
577 {
578 case State.None:
579 if (_reader.NodeType != XmlNodeType.Element)
580 {
581 throw _reader.CreateReadElementContentAsException("ReadElementContentAsBinHex");
582 }
583 if (!(await InitOnElementAsync().ConfigureAwait(continueOnCapturedContext: false)))
584 {
585 return 0;
586 }
587 break;
588 case State.InReadContent:
590 case State.InReadElementContent:
592 {
594 }
595 break;
596 default:
597 return 0;
598 }
601 }
602
604 {
605 if (_state != 0)
606 {
608 {
609 }
610 if (_state == State.InReadElementContent)
611 {
612 if (_reader.NodeType != XmlNodeType.EndElement)
613 {
615 }
617 }
618 }
619 Reset();
620 }
621
623 {
625 {
626 return false;
627 }
628 _state = State.InReadContent;
629 _isEnd = false;
630 return true;
631 }
632
634 {
637 if (isEmpty)
638 {
639 return false;
640 }
642 {
643 if (_reader.NodeType != XmlNodeType.EndElement)
644 {
646 }
648 return false;
649 }
650 _state = State.InReadElementContent;
651 _isEnd = false;
652 return true;
653 }
654
656 {
657 if (_isEnd)
658 {
659 Reset();
660 return 0;
661 }
663 do
664 {
666 {
667 while (true)
668 {
670 {
672 _valueOffset += num;
673 }
674 if (_decoder.IsFull)
675 {
676 return _decoder.DecodedCount;
677 }
679 {
680 break;
681 }
682 _valueOffset = 0;
683 }
684 }
685 else
686 {
687 string text = await _reader.GetValueAsync().ConfigureAwait(continueOnCapturedContext: false);
690 if (_decoder.IsFull)
691 {
692 return _decoder.DecodedCount;
693 }
694 }
695 _valueOffset = 0;
696 }
698 _isEnd = true;
699 return _decoder.DecodedCount;
700 }
701
703 {
704 if (count == 0)
705 {
706 return 0;
707 }
709 if (num > 0)
710 {
711 return num;
712 }
713 if (_reader.NodeType != XmlNodeType.EndElement)
714 {
716 }
718 _state = State.None;
719 return 0;
720 }
721
723 {
724 do
725 {
726 switch (_reader.NodeType)
727 {
728 case XmlNodeType.Attribute:
729 return !moveIfOnContentNode;
730 case XmlNodeType.Text:
731 case XmlNodeType.CDATA:
732 case XmlNodeType.Whitespace:
733 case XmlNodeType.SignificantWhitespace:
735 {
736 return true;
737 }
738 goto IL_00a5;
739 case XmlNodeType.EntityReference:
741 {
742 break;
743 }
745 goto IL_00a5;
746 case XmlNodeType.ProcessingInstruction:
747 case XmlNodeType.Comment:
748 case XmlNodeType.EndEntity:
749 goto IL_00a5;
750 }
751 return false;
752 IL_00a5:
753 moveIfOnContentNode = false;
754 }
756 return false;
757 }
758}
static string Xml_InvalidNodeType
Definition SR.cs:88
static string Xml_MixingBinaryContentMethods
Definition SR.cs:184
Definition SR.cs:7
new ConfiguredTaskAwaitable< TResult > ConfigureAwait(bool continueOnCapturedContext)
Definition Task.cs:226
int Decode(char[] chars, int startPos, int len)
void SetNextOutputBuffer(Array array, int offset, int len)
async Task< int > ReadElementContentAsBase64Async(byte[] buffer, int index, int count)
int ReadElementContentAsBinary(byte[] buffer, int index, int count)
static ReadContentAsBinaryHelper CreateOrReset(ReadContentAsBinaryHelper helper, XmlReader reader)
async Task< int > ReadContentAsBinaryAsync(byte[] buffer, int index, int count)
int ReadContentAsBinary(byte[] buffer, int index, int count)
async Task< int > ReadContentAsBase64Async(byte[] buffer, int index, int count)
bool MoveToNextContentNode(bool moveIfOnContentNode)
int ReadContentAsBase64(byte[] buffer, int index, int count)
async Task< bool > MoveToNextContentNodeAsync(bool moveIfOnContentNode)
int ReadContentAsBinHex(byte[] buffer, int index, int count)
int ReadElementContentAsBinHex(byte[] buffer, int index, int count)
int ReadElementContentAsBase64(byte[] buffer, int index, int count)
async Task< int > ReadElementContentAsBinaryAsync(byte[] buffer, int index, int count)
async Task< int > ReadElementContentAsBinHexAsync(byte[] buffer, int index, int count)
async Task< int > ReadContentAsBinHexAsync(byte[] buffer, int index, int count)
Exception CreateReadElementContentAsException(string methodName)
virtual bool CanResolveEntity
Definition XmlReader.cs:118
Exception CreateReadContentAsException(string methodName)
virtual int ReadValueChunk(char[] buffer, int index, int count)
Definition XmlReader.cs:564
virtual Task< int > ReadValueChunkAsync(char[] buffer, int index, int count)
virtual Task< bool > ReadAsync()
XmlNodeType NodeType
Definition XmlReader.cs:62
static bool CanReadContentAs(XmlNodeType nodeType)
virtual Task< string > GetValueAsync()
virtual bool CanReadValueChunk
Definition XmlReader.cs:122