Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
XmlKeyHelper.cs
Go to the documentation of this file.
4using System.Text;
5
7
8internal static class XmlKeyHelper
9{
10 internal struct ParseState
11 {
12 private static class Functions
13 {
14 private static readonly Func<string, object> s_xDocumentCreate;
15
16 private static readonly PropertyInfo s_docRootProperty;
17
18 private static readonly MethodInfo s_getElementsMethod;
19
20 private static readonly PropertyInfo s_elementNameProperty;
21
22 private static readonly PropertyInfo s_elementValueProperty;
23
24 private static readonly PropertyInfo s_nameNameProperty;
25
26 static Functions()
27 {
28 Type type = Type.GetType("System.Xml.Linq.XDocument, System.Private.Xml.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51");
29 s_xDocumentCreate = type.GetMethod("Parse", BindingFlags.Static | BindingFlags.Public, new Type[1] { typeof(string) }).CreateDelegate<Func<string, object>>();
30 s_docRootProperty = type.GetProperty("Root");
31 Type type2 = Type.GetType("System.Xml.Linq.XElement, System.Private.Xml.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51");
32 s_getElementsMethod = type2.GetMethod("Elements", BindingFlags.Instance | BindingFlags.Public, Type.EmptyTypes);
33 s_elementNameProperty = type2.GetProperty("Name");
34 s_elementValueProperty = type2.GetProperty("Value");
35 Type type3 = Type.GetType("System.Xml.Linq.XName, System.Private.Xml.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51");
36 s_nameNameProperty = type3.GetProperty("LocalName");
37 }
38
39 internal static object ParseDocument(string xmlString)
40 {
41 return s_docRootProperty.GetValue(s_xDocumentCreate(xmlString));
42 }
43
44 internal static IEnumerable GetElements(object element)
45 {
46 return (IEnumerable)s_getElementsMethod.Invoke(element, Array.Empty<object>());
47 }
48
49 internal static string GetLocalName(object element)
50 {
51 return (string)s_nameNameProperty.GetValue(s_elementNameProperty.GetValue(element));
52 }
53
54 internal static string GetValue(object element)
55 {
56 return (string)s_elementValueProperty.GetValue(element);
57 }
58 }
59
61
63
64 private int _index;
65
66 internal static ParseState ParseDocument(string xmlString)
67 {
68 object element = Functions.ParseDocument(xmlString);
69 ParseState result = default(ParseState);
70 result._enumerable = Functions.GetElements(element);
71 result._enumerator = null;
72 result._index = -1;
73 return result;
74 }
75
76 internal bool HasElement(string localName)
77 {
78 string value = GetValue(localName);
79 bool flag = value != null;
80 if (flag)
81 {
82 _index--;
83 }
84 return flag;
85 }
86
87 internal string GetValue(string localName)
88 {
89 if (_enumerable == null)
90 {
91 return null;
92 }
93 if (_enumerator == null)
94 {
96 }
97 int index = _index;
98 int num = index;
99 if (!_enumerator.MoveNext())
100 {
101 num = -1;
103 if (!_enumerator.MoveNext())
104 {
105 _enumerable = null;
106 return null;
107 }
108 }
109 for (num++; num != index; num++)
110 {
111 string localName2 = Functions.GetLocalName(_enumerator.Current);
112 if (localName == localName2)
113 {
114 _index = num;
116 }
117 if (!_enumerator.MoveNext())
118 {
119 num = -1;
120 if (index < 0)
121 {
122 _enumerator = null;
123 return null;
124 }
126 if (!_enumerator.MoveNext())
127 {
128 _enumerable = null;
129 return null;
130 }
131 }
132 }
133 return null;
134 }
135 }
136
137 internal static ParseState ParseDocument(string xmlString)
138 {
139 if (xmlString == null)
140 {
141 throw new ArgumentNullException("xmlString");
142 }
143 try
144 {
145 return ParseState.ParseDocument(xmlString);
146 }
147 catch (Exception inner)
148 {
150 }
151 }
152
153 internal static bool HasElement(ref ParseState state, string name)
154 {
155 return state.HasElement(name);
156 }
157
158 internal static byte[] ReadCryptoBinary(ref ParseState state, string name, int sizeHint = -1)
159 {
160 string value = state.GetValue(name);
161 if (value == null)
162 {
163 return null;
164 }
165 if (value.Length == 0)
166 {
167 return Array.Empty<byte>();
168 }
169 if (sizeHint < 0)
170 {
172 }
173 byte[] array = new byte[sizeHint];
174 if (Convert.TryFromBase64Chars(value.AsSpan(), array, out var bytesWritten))
175 {
176 if (bytesWritten == sizeHint)
177 {
178 return array;
179 }
180 int num = sizeHint - bytesWritten;
181 Buffer.BlockCopy(array, 0, array, num, bytesWritten);
182 array.AsSpan(0, num).Clear();
183 return array;
184 }
186 }
187
188 internal static int ReadCryptoBinaryInt32(byte[] buf)
189 {
190 int num = 0;
191 for (int i = Math.Max(0, buf.Length - 4); i < buf.Length; i++)
192 {
193 num <<= 8;
194 num |= buf[i];
195 }
196 return num;
197 }
198
199 internal static void WriteCryptoBinary(string name, int value, StringBuilder builder)
200 {
201 if (value == 0)
202 {
203 Span<byte> span = stackalloc byte[1];
204 span[0] = 0;
205 WriteCryptoBinary(name, span, builder);
206 return;
207 }
208 Span<byte> destination = stackalloc byte[4];
210 int i;
211 for (i = 0; destination[i] == 0; i++)
212 {
213 }
214 WriteCryptoBinary(name, destination.Slice(i, destination.Length - i), builder);
215 }
216
217 internal static void WriteCryptoBinary(string name, ReadOnlySpan<byte> value, StringBuilder builder)
218 {
219 builder.Append('<');
220 builder.Append(name);
221 builder.Append('>');
222 int num = 0;
223 int num2 = value.Length;
224 Span<char> chars = stackalloc char[256];
225 while (num2 > 0)
226 {
227 int num3 = Math.Min(192, num2);
228 if (!Convert.TryToBase64Chars(value.Slice(num, num3), chars, out var charsWritten))
229 {
230 throw new CryptographicException();
231 }
232 builder.Append(chars.Slice(0, charsWritten));
233 num2 -= num3;
234 num += num3;
235 }
236 builder.Append('<');
237 builder.Append('/');
238 builder.Append(name);
239 builder.Append('>');
240 }
241}
static void BlockCopy(Array src, int srcOffset, Array dst, int dstOffset, int count)
Definition Buffer.cs:102
static void WriteInt32BigEndian(Span< byte > destination, int value)
static unsafe bool TryToBase64Chars(ReadOnlySpan< byte > bytes, Span< char > chars, out int charsWritten, Base64FormattingOptions options=Base64FormattingOptions.None)
Definition Convert.cs:2799
static bool TryFromBase64Chars(ReadOnlySpan< char > chars, Span< byte > bytes, out int bytesWritten)
Definition Convert.cs:2925
static unsafe byte[] FromBase64String(string s)
Definition Convert.cs:2904
static byte Min(byte val1, byte val2)
Definition Math.cs:912
static byte Max(byte val1, byte val2)
Definition Math.cs:738
static string Cryptography_FromXmlParseError
Definition SR.cs:58
Definition SR.cs:7
static readonly Func< string, object > s_xDocumentCreate
static void WriteCryptoBinary(string name, int value, StringBuilder builder)
static bool HasElement(ref ParseState state, string name)
static int ReadCryptoBinaryInt32(byte[] buf)
static void WriteCryptoBinary(string name, ReadOnlySpan< byte > value, StringBuilder builder)
static byte[] ReadCryptoBinary(ref ParseState state, string name, int sizeHint=-1)
static ParseState ParseDocument(string xmlString)
StringBuilder Append(char value, int repeatCount)
static ? Type GetType(string typeName, bool throwOnError, bool ignoreCase)
Definition Type.cs:408
PropertyInfo? GetProperty(string name)
Definition Type.cs:815
static readonly Type[] EmptyTypes
Definition Type.cs:19
MethodInfo? GetMethod(string name)
Definition Type.cs:675
static ParseState ParseDocument(string xmlString)