Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
SecurityElement.cs
Go to the documentation of this file.
3using System.Text;
4
5namespace System.Security;
6
7public sealed class SecurityElement
8{
9 internal string _tag;
10
11 internal string _text;
12
14
16
17 private static readonly char[] s_tagIllegalCharacters = new char[3] { ' ', '<', '>' };
18
19 private static readonly char[] s_textIllegalCharacters = new char[2] { '<', '>' };
20
21 private static readonly char[] s_valueIllegalCharacters = new char[3] { '<', '>', '"' };
22
23 private static readonly char[] s_escapeChars = new char[5] { '<', '>', '"', '\'', '&' };
24
25 private static readonly string[] s_escapeStringPairs = new string[10] { "<", "&lt;", ">", "&gt;", "\"", "&quot;", "'", "&apos;", "&", "&amp;" };
26
27 public string Tag
28 {
29 get
30 {
31 return _tag;
32 }
33 set
34 {
35 if (value == null)
36 {
37 throw new ArgumentNullException("Tag");
38 }
39 if (!IsValidTag(value))
40 {
42 }
43 _tag = value;
44 }
45 }
46
48 {
49 get
50 {
51 if (_attributes == null || _attributes.Count == 0)
52 {
53 return null;
54 }
55 Hashtable hashtable = new Hashtable(_attributes.Count / 2);
57 for (int i = 0; i < count; i += 2)
58 {
59 hashtable.Add(_attributes[i], _attributes[i + 1]);
60 }
61 return hashtable;
62 }
63 set
64 {
65 if (value == null || value.Count == 0)
66 {
67 _attributes = null;
68 return;
69 }
70 ArrayList arrayList = new ArrayList(value.Count);
71 IDictionaryEnumerator enumerator = value.GetEnumerator();
72 while (enumerator.MoveNext())
73 {
74 string text = (string)enumerator.Key;
75 string text2 = (string)enumerator.Value;
77 {
79 }
80 if (!IsValidAttributeValue(text2))
81 {
83 }
84 arrayList.Add(text);
85 arrayList.Add(text2);
86 }
87 _attributes = arrayList;
88 }
89 }
90
91 public string? Text
92 {
93 get
94 {
95 return Unescape(_text);
96 }
97 set
98 {
99 if (value == null)
100 {
101 _text = null;
102 return;
103 }
104 if (!IsValidText(value))
105 {
107 }
108 _text = value;
109 }
110 }
111
113 {
114 get
115 {
116 return _children;
117 }
118 set
119 {
120 if (value != null && value.Contains(null))
121 {
123 }
125 }
126 }
127
128 public SecurityElement(string tag)
129 {
130 if (tag == null)
131 {
132 throw new ArgumentNullException("tag");
133 }
134 if (!IsValidTag(tag))
135 {
137 }
138 _tag = tag;
139 }
140
141 public SecurityElement(string tag, string? text)
142 {
143 if (tag == null)
144 {
145 throw new ArgumentNullException("tag");
146 }
147 if (!IsValidTag(tag))
148 {
150 }
151 if (text != null && !IsValidText(text))
152 {
154 }
155 _tag = tag;
156 _text = text;
157 }
158
159 internal void AddAttributeSafe(string name, string value)
160 {
161 if (_attributes == null)
162 {
163 _attributes = new ArrayList(8);
164 }
165 else
166 {
167 int count = _attributes.Count;
168 for (int i = 0; i < count; i += 2)
169 {
170 string a = (string)_attributes[i];
171 if (string.Equals(a, name))
172 {
174 }
175 }
176 }
177 _attributes.Add(name);
179 }
180
181 public void AddAttribute(string name, string value)
182 {
183 if (name == null)
184 {
185 throw new ArgumentNullException("name");
186 }
187 if (value == null)
188 {
189 throw new ArgumentNullException("value");
190 }
191 if (!IsValidAttributeName(name))
192 {
194 }
196 {
198 }
199 AddAttributeSafe(name, value);
200 }
201
202 public void AddChild(SecurityElement child)
203 {
204 if (child == null)
205 {
206 throw new ArgumentNullException("child");
207 }
208 if (_children == null)
209 {
210 _children = new ArrayList(1);
211 }
212 _children.Add(child);
213 }
214
215 public bool Equal([NotNullWhen(true)] SecurityElement? other)
216 {
217 if (other == null)
218 {
219 return false;
220 }
221 if (!string.Equals(_tag, other._tag))
222 {
223 return false;
224 }
225 if (!string.Equals(_text, other._text))
226 {
227 return false;
228 }
229 if (_attributes == null || other._attributes == null)
230 {
231 if (_attributes != other._attributes)
232 {
233 return false;
234 }
235 }
236 else
237 {
238 int count = _attributes.Count;
239 if (count != other._attributes.Count)
240 {
241 return false;
242 }
243 for (int i = 0; i < count; i++)
244 {
245 string a = (string)_attributes[i];
246 string b = (string)other._attributes[i];
247 if (!string.Equals(a, b))
248 {
249 return false;
250 }
251 }
252 }
253 if (_children == null || other._children == null)
254 {
255 if (_children != other._children)
256 {
257 return false;
258 }
259 }
260 else
261 {
262 if (_children.Count != other._children.Count)
263 {
264 return false;
265 }
266 IEnumerator enumerator = _children.GetEnumerator();
267 IEnumerator enumerator2 = other._children.GetEnumerator();
268 while (enumerator.MoveNext())
269 {
270 enumerator2.MoveNext();
271 SecurityElement securityElement = (SecurityElement)enumerator.Current;
272 SecurityElement other2 = (SecurityElement)enumerator2.Current;
273 if (securityElement == null || !securityElement.Equal(other2))
274 {
275 return false;
276 }
277 }
278 }
279 return true;
280 }
281
283 {
284 SecurityElement securityElement = new SecurityElement(_tag, _text);
285 securityElement._children = ((_children == null) ? null : new ArrayList(_children));
286 securityElement._attributes = ((_attributes == null) ? null : new ArrayList(_attributes));
287 return securityElement;
288 }
289
290 public static bool IsValidTag([NotNullWhen(true)] string? tag)
291 {
292 if (tag == null)
293 {
294 return false;
295 }
296 return tag.IndexOfAny(s_tagIllegalCharacters) == -1;
297 }
298
299 public static bool IsValidText([NotNullWhen(true)] string? text)
300 {
301 if (text == null)
302 {
303 return false;
304 }
305 return text.IndexOfAny(s_textIllegalCharacters) == -1;
306 }
307
308 public static bool IsValidAttributeName([NotNullWhen(true)] string? name)
309 {
310 return IsValidTag(name);
311 }
312
313 public static bool IsValidAttributeValue([NotNullWhen(true)] string? value)
314 {
315 if (value == null)
316 {
317 return false;
318 }
319 return value.IndexOfAny(s_valueIllegalCharacters) == -1;
320 }
321
322 private static string GetEscapeSequence(char c)
323 {
324 int num = s_escapeStringPairs.Length;
325 for (int i = 0; i < num; i += 2)
326 {
327 string text = s_escapeStringPairs[i];
328 string result = s_escapeStringPairs[i + 1];
329 if (text[0] == c)
330 {
331 return result;
332 }
333 }
334 return c.ToString();
335 }
336
337 [return: NotNullIfNotNull("str")]
338 public static string? Escape(string? str)
339 {
340 if (str == null)
341 {
342 return null;
343 }
344 StringBuilder stringBuilder = null;
345 int length = str.Length;
346 int num = 0;
347 while (true)
348 {
349 int num2 = str.IndexOfAny(s_escapeChars, num);
350 if (num2 == -1)
351 {
352 break;
353 }
354 if (stringBuilder == null)
355 {
356 stringBuilder = new StringBuilder();
357 }
358 stringBuilder.Append(str, num, num2 - num);
359 stringBuilder.Append(GetEscapeSequence(str[num2]));
360 num = num2 + 1;
361 }
362 if (stringBuilder == null)
363 {
364 return str;
365 }
366 stringBuilder.Append(str, num, length - num);
367 return stringBuilder.ToString();
368 }
369
370 private static string GetUnescapeSequence(string str, int index, out int newIndex)
371 {
372 int num = str.Length - index;
373 int num2 = s_escapeStringPairs.Length;
374 for (int i = 0; i < num2; i += 2)
375 {
376 string result = s_escapeStringPairs[i];
377 string text = s_escapeStringPairs[i + 1];
378 int length = text.Length;
379 if (length <= num && string.Compare(text, 0, str, index, length, StringComparison.Ordinal) == 0)
380 {
381 newIndex = index + text.Length;
382 return result;
383 }
384 }
385 newIndex = index + 1;
386 return str[index].ToString();
387 }
388
389 private static string Unescape(string str)
390 {
391 if (str == null)
392 {
393 return null;
394 }
395 StringBuilder stringBuilder = null;
396 int length = str.Length;
397 int newIndex = 0;
398 while (true)
399 {
400 int num = str.IndexOf('&', newIndex);
401 if (num == -1)
402 {
403 break;
404 }
405 if (stringBuilder == null)
406 {
407 stringBuilder = new StringBuilder();
408 }
409 stringBuilder.Append(str, newIndex, num - newIndex);
410 stringBuilder.Append(GetUnescapeSequence(str, num, out newIndex));
411 }
412 if (stringBuilder == null)
413 {
414 return str;
415 }
416 stringBuilder.Append(str, newIndex, length - newIndex);
417 return stringBuilder.ToString();
418 }
419
420 public override string ToString()
421 {
422 StringBuilder stringBuilder = new StringBuilder();
423 ToString(stringBuilder, delegate(object obj, string str)
424 {
426 });
427 return stringBuilder.ToString();
428 }
429
430 private void ToString(object obj, Action<object, string> write)
431 {
432 write(obj, "<");
433 write(obj, _tag);
434 if (_attributes != null && _attributes.Count > 0)
435 {
436 write(obj, " ");
437 int count = _attributes.Count;
438 for (int i = 0; i < count; i += 2)
439 {
440 string arg = (string)_attributes[i];
441 string arg2 = (string)_attributes[i + 1];
442 write(obj, arg);
443 write(obj, "=\"");
444 write(obj, arg2);
445 write(obj, "\"");
446 if (i != _attributes.Count - 2)
447 {
448 write(obj, "\r\n");
449 }
450 }
451 }
452 if (_text == null && (_children == null || _children.Count == 0))
453 {
454 write(obj, "/>");
455 write(obj, "\r\n");
456 return;
457 }
458 write(obj, ">");
459 write(obj, _text);
460 if (_children != null)
461 {
462 write(obj, "\r\n");
463 for (int j = 0; j < _children.Count; j++)
464 {
465 ((SecurityElement)_children[j]).ToString(obj, write);
466 }
467 }
468 write(obj, "</");
469 write(obj, _tag);
470 write(obj, ">");
471 write(obj, "\r\n");
472 }
473
474 public string? Attribute(string name)
475 {
476 if (name == null)
477 {
478 throw new ArgumentNullException("name");
479 }
480 if (_attributes == null)
481 {
482 return null;
483 }
484 int count = _attributes.Count;
485 for (int i = 0; i < count; i += 2)
486 {
487 string a = (string)_attributes[i];
488 if (string.Equals(a, name))
489 {
490 string str = (string)_attributes[i + 1];
491 return Unescape(str);
492 }
493 }
494 return null;
495 }
496
498 {
499 if (tag == null)
500 {
501 throw new ArgumentNullException("tag");
502 }
503 if (_children == null)
504 {
505 return null;
506 }
507 foreach (SecurityElement child in _children)
508 {
509 if (child != null && string.Equals(child.Tag, tag))
510 {
511 return child;
512 }
513 }
514 return null;
515 }
516
517 public string? SearchForTextOfTag(string tag)
518 {
519 if (tag == null)
520 {
521 throw new ArgumentNullException("tag");
522 }
523 if (string.Equals(_tag, tag))
524 {
525 return Unescape(_text);
526 }
527 if (_children == null)
528 {
529 return null;
530 }
531 foreach (SecurityElement child in Children)
532 {
533 string text = child?.SearchForTextOfTag(tag);
534 if (text != null)
535 {
536 return text;
537 }
538 }
539 return null;
540 }
541
542 public static SecurityElement? FromString(string xml)
543 {
544 if (xml == null)
545 {
546 throw new ArgumentNullException("xml");
547 }
548 return null;
549 }
550}
virtual IEnumerator GetEnumerator()
virtual int Add(object? value)
virtual void Add(object key, object? value)
Definition Hashtable.cs:676
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string Argument_InvalidElementTag
Definition SR.cs:674
static string Argument_InvalidElementName
Definition SR.cs:672
static string Argument_InvalidElementValue
Definition SR.cs:678
static string Argument_AttributeNamesMustBeUnique
Definition SR.cs:474
static string Argument_InvalidElementText
Definition SR.cs:676
static string ArgumentNull_Child
Definition SR.cs:942
Definition SR.cs:7
static bool IsValidTag([NotNullWhen(true)] string? tag)
static bool IsValidAttributeValue([NotNullWhen(true)] string? value)
static string Unescape(string str)
SecurityElement(string tag, string? text)
static readonly char[] s_textIllegalCharacters
string? SearchForTextOfTag(string tag)
void ToString(object obj, Action< object, string > write)
static ? SecurityElement FromString(string xml)
static string GetEscapeSequence(char c)
static string GetUnescapeSequence(string str, int index, out int newIndex)
bool Equal([NotNullWhen(true)] SecurityElement? other)
static readonly char[] s_valueIllegalCharacters
static bool IsValidText([NotNullWhen(true)] string? text)
SecurityElement? SearchForChildByTag(string tag)
static readonly char[] s_escapeChars
static readonly string[] s_escapeStringPairs
static ? string Escape(string? str)
void AddAttribute(string name, string value)
void AddChild(SecurityElement child)
static readonly char[] s_tagIllegalCharacters
void AddAttributeSafe(string name, string value)
static bool IsValidAttributeName([NotNullWhen(true)] string? name)
override string ToString()
StringBuilder Append(char value, int repeatCount)