Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
WebUtility.cs
Go to the documentation of this file.
5using System.IO;
7using System.Text;
8
9namespace System.Net;
10
11public static class WebUtility
12{
13 private struct UrlDecoder
14 {
15 private readonly int _bufferSize;
16
17 private int _numChars;
18
19 private char[] _charBuffer;
20
21 private int _numBytes;
22
23 private byte[] _byteBuffer;
24
25 private readonly Encoding _encoding;
26
27 private void FlushBytes()
28 {
29 if (_charBuffer == null)
30 {
31 _charBuffer = new char[_bufferSize];
32 }
34 _numBytes = 0;
35 }
36
37 internal UrlDecoder(int bufferSize, Encoding encoding)
38 {
40 _encoding = encoding;
41 _charBuffer = null;
42 _numChars = 0;
43 _numBytes = 0;
44 _byteBuffer = null;
45 }
46
47 internal void AddChar(char ch)
48 {
49 if (_numBytes > 0)
50 {
51 FlushBytes();
52 }
53 if (_charBuffer == null)
54 {
55 _charBuffer = new char[_bufferSize];
56 }
58 }
59
60 internal void AddByte(byte b)
61 {
62 if (_byteBuffer == null)
63 {
64 _byteBuffer = new byte[_bufferSize];
65 }
67 }
68
69 internal string GetString()
70 {
71 if (_numBytes > 0)
72 {
73 FlushBytes();
74 }
75 return new string(_charBuffer, 0, _numChars);
76 }
77 }
78
79 private static class HtmlEntities
80 {
82
84 {
85 ReadOnlySpan<byte> source = new byte[2530]
86 {
87 116, 111, 117, 113, 0, 0, 0, 0, 34, 0,
88 112, 109, 97, 0, 0, 0, 0, 0, 38, 0,
89 115, 111, 112, 97, 0, 0, 0, 0, 39, 0,
90 116, 108, 0, 0, 0, 0, 0, 0, 60, 0,
91 116, 103, 0, 0, 0, 0, 0, 0, 62, 0,
92 112, 115, 98, 110, 0, 0, 0, 0, 160, 0,
93 108, 99, 120, 101, 105, 0, 0, 0, 161, 0,
94 116, 110, 101, 99, 0, 0, 0, 0, 162, 0,
95 100, 110, 117, 111, 112, 0, 0, 0, 163, 0,
96 110, 101, 114, 114, 117, 99, 0, 0, 164, 0,
97 110, 101, 121, 0, 0, 0, 0, 0, 165, 0,
98 114, 97, 98, 118, 114, 98, 0, 0, 166, 0,
99 116, 99, 101, 115, 0, 0, 0, 0, 167, 0,
100 108, 109, 117, 0, 0, 0, 0, 0, 168, 0,
101 121, 112, 111, 99, 0, 0, 0, 0, 169, 0,
102 102, 100, 114, 111, 0, 0, 0, 0, 170, 0,
103 111, 117, 113, 97, 108, 0, 0, 0, 171, 0,
104 116, 111, 110, 0, 0, 0, 0, 0, 172, 0,
105 121, 104, 115, 0, 0, 0, 0, 0, 173, 0,
106 103, 101, 114, 0, 0, 0, 0, 0, 174, 0,
107 114, 99, 97, 109, 0, 0, 0, 0, 175, 0,
108 103, 101, 100, 0, 0, 0, 0, 0, 176, 0,
109 110, 109, 115, 117, 108, 112, 0, 0, 177, 0,
110 50, 112, 117, 115, 0, 0, 0, 0, 178, 0,
111 51, 112, 117, 115, 0, 0, 0, 0, 179, 0,
112 101, 116, 117, 99, 97, 0, 0, 0, 180, 0,
113 111, 114, 99, 105, 109, 0, 0, 0, 181, 0,
114 97, 114, 97, 112, 0, 0, 0, 0, 182, 0,
115 116, 111, 100, 100, 105, 109, 0, 0, 183, 0,
116 108, 105, 100, 101, 99, 0, 0, 0, 184, 0,
117 49, 112, 117, 115, 0, 0, 0, 0, 185, 0,
118 109, 100, 114, 111, 0, 0, 0, 0, 186, 0,
119 111, 117, 113, 97, 114, 0, 0, 0, 187, 0,
120 52, 49, 99, 97, 114, 102, 0, 0, 188, 0,
121 50, 49, 99, 97, 114, 102, 0, 0, 189, 0,
122 52, 51, 99, 97, 114, 102, 0, 0, 190, 0,
123 116, 115, 101, 117, 113, 105, 0, 0, 191, 0,
124 101, 118, 97, 114, 103, 65, 0, 0, 192, 0,
125 101, 116, 117, 99, 97, 65, 0, 0, 193, 0,
126 99, 114, 105, 99, 65, 0, 0, 0, 194, 0,
127 101, 100, 108, 105, 116, 65, 0, 0, 195, 0,
128 108, 109, 117, 65, 0, 0, 0, 0, 196, 0,
129 103, 110, 105, 114, 65, 0, 0, 0, 197, 0,
130 103, 105, 108, 69, 65, 0, 0, 0, 198, 0,
131 108, 105, 100, 101, 99, 67, 0, 0, 199, 0,
132 101, 118, 97, 114, 103, 69, 0, 0, 200, 0,
133 101, 116, 117, 99, 97, 69, 0, 0, 201, 0,
134 99, 114, 105, 99, 69, 0, 0, 0, 202, 0,
135 108, 109, 117, 69, 0, 0, 0, 0, 203, 0,
136 101, 118, 97, 114, 103, 73, 0, 0, 204, 0,
137 101, 116, 117, 99, 97, 73, 0, 0, 205, 0,
138 99, 114, 105, 99, 73, 0, 0, 0, 206, 0,
139 108, 109, 117, 73, 0, 0, 0, 0, 207, 0,
140 72, 84, 69, 0, 0, 0, 0, 0, 208, 0,
141 101, 100, 108, 105, 116, 78, 0, 0, 209, 0,
142 101, 118, 97, 114, 103, 79, 0, 0, 210, 0,
143 101, 116, 117, 99, 97, 79, 0, 0, 211, 0,
144 99, 114, 105, 99, 79, 0, 0, 0, 212, 0,
145 101, 100, 108, 105, 116, 79, 0, 0, 213, 0,
146 108, 109, 117, 79, 0, 0, 0, 0, 214, 0,
147 115, 101, 109, 105, 116, 0, 0, 0, 215, 0,
148 104, 115, 97, 108, 115, 79, 0, 0, 216, 0,
149 101, 118, 97, 114, 103, 85, 0, 0, 217, 0,
150 101, 116, 117, 99, 97, 85, 0, 0, 218, 0,
151 99, 114, 105, 99, 85, 0, 0, 0, 219, 0,
152 108, 109, 117, 85, 0, 0, 0, 0, 220, 0,
153 101, 116, 117, 99, 97, 89, 0, 0, 221, 0,
154 78, 82, 79, 72, 84, 0, 0, 0, 222, 0,
155 103, 105, 108, 122, 115, 0, 0, 0, 223, 0,
156 101, 118, 97, 114, 103, 97, 0, 0, 224, 0,
157 101, 116, 117, 99, 97, 97, 0, 0, 225, 0,
158 99, 114, 105, 99, 97, 0, 0, 0, 226, 0,
159 101, 100, 108, 105, 116, 97, 0, 0, 227, 0,
160 108, 109, 117, 97, 0, 0, 0, 0, 228, 0,
161 103, 110, 105, 114, 97, 0, 0, 0, 229, 0,
162 103, 105, 108, 101, 97, 0, 0, 0, 230, 0,
163 108, 105, 100, 101, 99, 99, 0, 0, 231, 0,
164 101, 118, 97, 114, 103, 101, 0, 0, 232, 0,
165 101, 116, 117, 99, 97, 101, 0, 0, 233, 0,
166 99, 114, 105, 99, 101, 0, 0, 0, 234, 0,
167 108, 109, 117, 101, 0, 0, 0, 0, 235, 0,
168 101, 118, 97, 114, 103, 105, 0, 0, 236, 0,
169 101, 116, 117, 99, 97, 105, 0, 0, 237, 0,
170 99, 114, 105, 99, 105, 0, 0, 0, 238, 0,
171 108, 109, 117, 105, 0, 0, 0, 0, 239, 0,
172 104, 116, 101, 0, 0, 0, 0, 0, 240, 0,
173 101, 100, 108, 105, 116, 110, 0, 0, 241, 0,
174 101, 118, 97, 114, 103, 111, 0, 0, 242, 0,
175 101, 116, 117, 99, 97, 111, 0, 0, 243, 0,
176 99, 114, 105, 99, 111, 0, 0, 0, 244, 0,
177 101, 100, 108, 105, 116, 111, 0, 0, 245, 0,
178 108, 109, 117, 111, 0, 0, 0, 0, 246, 0,
179 101, 100, 105, 118, 105, 100, 0, 0, 247, 0,
180 104, 115, 97, 108, 115, 111, 0, 0, 248, 0,
181 101, 118, 97, 114, 103, 117, 0, 0, 249, 0,
182 101, 116, 117, 99, 97, 117, 0, 0, 250, 0,
183 99, 114, 105, 99, 117, 0, 0, 0, 251, 0,
184 108, 109, 117, 117, 0, 0, 0, 0, 252, 0,
185 101, 116, 117, 99, 97, 121, 0, 0, 253, 0,
186 110, 114, 111, 104, 116, 0, 0, 0, 254, 0,
187 108, 109, 117, 121, 0, 0, 0, 0, 255, 0,
188 103, 105, 108, 69, 79, 0, 0, 0, 82, 1,
189 103, 105, 108, 101, 111, 0, 0, 0, 83, 1,
190 110, 111, 114, 97, 99, 83, 0, 0, 96, 1,
191 110, 111, 114, 97, 99, 115, 0, 0, 97, 1,
192 108, 109, 117, 89, 0, 0, 0, 0, 120, 1,
193 102, 111, 110, 102, 0, 0, 0, 0, 146, 1,
194 99, 114, 105, 99, 0, 0, 0, 0, 198, 2,
195 101, 100, 108, 105, 116, 0, 0, 0, 220, 2,
196 97, 104, 112, 108, 65, 0, 0, 0, 145, 3,
197 97, 116, 101, 66, 0, 0, 0, 0, 146, 3,
198 97, 109, 109, 97, 71, 0, 0, 0, 147, 3,
199 97, 116, 108, 101, 68, 0, 0, 0, 148, 3,
200 110, 111, 108, 105, 115, 112, 69, 0, 149, 3,
201 97, 116, 101, 90, 0, 0, 0, 0, 150, 3,
202 97, 116, 69, 0, 0, 0, 0, 0, 151, 3,
203 97, 116, 101, 104, 84, 0, 0, 0, 152, 3,
204 97, 116, 111, 73, 0, 0, 0, 0, 153, 3,
205 97, 112, 112, 97, 75, 0, 0, 0, 154, 3,
206 97, 100, 98, 109, 97, 76, 0, 0, 155, 3,
207 117, 77, 0, 0, 0, 0, 0, 0, 156, 3,
208 117, 78, 0, 0, 0, 0, 0, 0, 157, 3,
209 105, 88, 0, 0, 0, 0, 0, 0, 158, 3,
210 110, 111, 114, 99, 105, 109, 79, 0, 159, 3,
211 105, 80, 0, 0, 0, 0, 0, 0, 160, 3,
212 111, 104, 82, 0, 0, 0, 0, 0, 161, 3,
213 97, 109, 103, 105, 83, 0, 0, 0, 163, 3,
214 117, 97, 84, 0, 0, 0, 0, 0, 164, 3,
215 110, 111, 108, 105, 115, 112, 85, 0, 165, 3,
216 105, 104, 80, 0, 0, 0, 0, 0, 166, 3,
217 105, 104, 67, 0, 0, 0, 0, 0, 167, 3,
218 105, 115, 80, 0, 0, 0, 0, 0, 168, 3,
219 97, 103, 101, 109, 79, 0, 0, 0, 169, 3,
220 97, 104, 112, 108, 97, 0, 0, 0, 177, 3,
221 97, 116, 101, 98, 0, 0, 0, 0, 178, 3,
222 97, 109, 109, 97, 103, 0, 0, 0, 179, 3,
223 97, 116, 108, 101, 100, 0, 0, 0, 180, 3,
224 110, 111, 108, 105, 115, 112, 101, 0, 181, 3,
225 97, 116, 101, 122, 0, 0, 0, 0, 182, 3,
226 97, 116, 101, 0, 0, 0, 0, 0, 183, 3,
227 97, 116, 101, 104, 116, 0, 0, 0, 184, 3,
228 97, 116, 111, 105, 0, 0, 0, 0, 185, 3,
229 97, 112, 112, 97, 107, 0, 0, 0, 186, 3,
230 97, 100, 98, 109, 97, 108, 0, 0, 187, 3,
231 117, 109, 0, 0, 0, 0, 0, 0, 188, 3,
232 117, 110, 0, 0, 0, 0, 0, 0, 189, 3,
233 105, 120, 0, 0, 0, 0, 0, 0, 190, 3,
234 110, 111, 114, 99, 105, 109, 111, 0, 191, 3,
235 105, 112, 0, 0, 0, 0, 0, 0, 192, 3,
236 111, 104, 114, 0, 0, 0, 0, 0, 193, 3,
237 102, 97, 109, 103, 105, 115, 0, 0, 194, 3,
238 97, 109, 103, 105, 115, 0, 0, 0, 195, 3,
239 117, 97, 116, 0, 0, 0, 0, 0, 196, 3,
240 110, 111, 108, 105, 115, 112, 117, 0, 197, 3,
241 105, 104, 112, 0, 0, 0, 0, 0, 198, 3,
242 105, 104, 99, 0, 0, 0, 0, 0, 199, 3,
243 105, 115, 112, 0, 0, 0, 0, 0, 200, 3,
244 97, 103, 101, 109, 111, 0, 0, 0, 201, 3,
245 109, 121, 115, 97, 116, 101, 104, 116, 209, 3,
246 104, 105, 115, 112, 117, 0, 0, 0, 210, 3,
247 118, 105, 112, 0, 0, 0, 0, 0, 214, 3,
248 112, 115, 110, 101, 0, 0, 0, 0, 2, 32,
249 112, 115, 109, 101, 0, 0, 0, 0, 3, 32,
250 112, 115, 110, 105, 104, 116, 0, 0, 9, 32,
251 106, 110, 119, 122, 0, 0, 0, 0, 12, 32,
252 106, 119, 122, 0, 0, 0, 0, 0, 13, 32,
253 109, 114, 108, 0, 0, 0, 0, 0, 14, 32,
254 109, 108, 114, 0, 0, 0, 0, 0, 15, 32,
255 104, 115, 97, 100, 110, 0, 0, 0, 19, 32,
256 104, 115, 97, 100, 109, 0, 0, 0, 20, 32,
257 111, 117, 113, 115, 108, 0, 0, 0, 24, 32,
258 111, 117, 113, 115, 114, 0, 0, 0, 25, 32,
259 111, 117, 113, 98, 115, 0, 0, 0, 26, 32,
260 111, 117, 113, 100, 108, 0, 0, 0, 28, 32,
261 111, 117, 113, 100, 114, 0, 0, 0, 29, 32,
262 111, 117, 113, 100, 98, 0, 0, 0, 30, 32,
263 114, 101, 103, 103, 97, 100, 0, 0, 32, 32,
264 114, 101, 103, 103, 97, 68, 0, 0, 33, 32,
265 108, 108, 117, 98, 0, 0, 0, 0, 34, 32,
266 112, 105, 108, 108, 101, 104, 0, 0, 38, 32,
267 108, 105, 109, 114, 101, 112, 0, 0, 48, 32,
268 101, 109, 105, 114, 112, 0, 0, 0, 50, 32,
269 101, 109, 105, 114, 80, 0, 0, 0, 51, 32,
270 111, 117, 113, 97, 115, 108, 0, 0, 57, 32,
271 111, 117, 113, 97, 115, 114, 0, 0, 58, 32,
272 101, 110, 105, 108, 111, 0, 0, 0, 62, 32,
273 108, 115, 97, 114, 102, 0, 0, 0, 68, 32,
274 111, 114, 117, 101, 0, 0, 0, 0, 172, 32,
275 101, 103, 97, 109, 105, 0, 0, 0, 17, 33,
276 112, 114, 101, 105, 101, 119, 0, 0, 24, 33,
277 108, 97, 101, 114, 0, 0, 0, 0, 28, 33,
278 101, 100, 97, 114, 116, 0, 0, 0, 34, 33,
279 109, 121, 115, 102, 101, 108, 97, 0, 53, 33,
280 114, 114, 97, 108, 0, 0, 0, 0, 144, 33,
281 114, 114, 97, 117, 0, 0, 0, 0, 145, 33,
282 114, 114, 97, 114, 0, 0, 0, 0, 146, 33,
283 114, 114, 97, 100, 0, 0, 0, 0, 147, 33,
284 114, 114, 97, 104, 0, 0, 0, 0, 148, 33,
285 114, 114, 97, 114, 99, 0, 0, 0, 181, 33,
286 114, 114, 65, 108, 0, 0, 0, 0, 208, 33,
287 114, 114, 65, 117, 0, 0, 0, 0, 209, 33,
288 114, 114, 65, 114, 0, 0, 0, 0, 210, 33,
289 114, 114, 65, 100, 0, 0, 0, 0, 211, 33,
290 114, 114, 65, 104, 0, 0, 0, 0, 212, 33,
291 108, 108, 97, 114, 111, 102, 0, 0, 0, 34,
292 116, 114, 97, 112, 0, 0, 0, 0, 2, 34,
293 116, 115, 105, 120, 101, 0, 0, 0, 3, 34,
294 121, 116, 112, 109, 101, 0, 0, 0, 5, 34,
295 97, 108, 98, 97, 110, 0, 0, 0, 7, 34,
296 110, 105, 115, 105, 0, 0, 0, 0, 8, 34,
297 110, 105, 116, 111, 110, 0, 0, 0, 9, 34,
298 105, 110, 0, 0, 0, 0, 0, 0, 11, 34,
299 100, 111, 114, 112, 0, 0, 0, 0, 15, 34,
300 109, 117, 115, 0, 0, 0, 0, 0, 17, 34,
301 115, 117, 110, 105, 109, 0, 0, 0, 18, 34,
302 116, 115, 97, 119, 111, 108, 0, 0, 23, 34,
303 99, 105, 100, 97, 114, 0, 0, 0, 26, 34,
304 112, 111, 114, 112, 0, 0, 0, 0, 29, 34,
305 110, 105, 102, 110, 105, 0, 0, 0, 30, 34,
306 103, 110, 97, 0, 0, 0, 0, 0, 32, 34,
307 100, 110, 97, 0, 0, 0, 0, 0, 39, 34,
308 114, 111, 0, 0, 0, 0, 0, 0, 40, 34,
309 112, 97, 99, 0, 0, 0, 0, 0, 41, 34,
310 112, 117, 99, 0, 0, 0, 0, 0, 42, 34,
311 116, 110, 105, 0, 0, 0, 0, 0, 43, 34,
312 52, 101, 114, 101, 104, 116, 0, 0, 52, 34,
313 109, 105, 115, 0, 0, 0, 0, 0, 60, 34,
314 103, 110, 111, 99, 0, 0, 0, 0, 69, 34,
315 112, 109, 121, 115, 97, 0, 0, 0, 72, 34,
316 101, 110, 0, 0, 0, 0, 0, 0, 96, 34,
317 118, 105, 117, 113, 101, 0, 0, 0, 97, 34,
318 101, 108, 0, 0, 0, 0, 0, 0, 100, 34,
319 101, 103, 0, 0, 0, 0, 0, 0, 101, 34,
320 98, 117, 115, 0, 0, 0, 0, 0, 130, 34,
321 112, 117, 115, 0, 0, 0, 0, 0, 131, 34,
322 98, 117, 115, 110, 0, 0, 0, 0, 132, 34,
323 101, 98, 117, 115, 0, 0, 0, 0, 134, 34,
324 101, 112, 117, 115, 0, 0, 0, 0, 135, 34,
325 115, 117, 108, 112, 111, 0, 0, 0, 149, 34,
326 115, 101, 109, 105, 116, 111, 0, 0, 151, 34,
327 112, 114, 101, 112, 0, 0, 0, 0, 165, 34,
328 116, 111, 100, 115, 0, 0, 0, 0, 197, 34,
329 108, 105, 101, 99, 108, 0, 0, 0, 8, 35,
330 108, 105, 101, 99, 114, 0, 0, 0, 9, 35,
331 114, 111, 111, 108, 102, 108, 0, 0, 10, 35,
332 114, 111, 111, 108, 102, 114, 0, 0, 11, 35,
333 103, 110, 97, 108, 0, 0, 0, 0, 41, 35,
334 103, 110, 97, 114, 0, 0, 0, 0, 42, 35,
335 122, 111, 108, 0, 0, 0, 0, 0, 202, 37,
336 115, 101, 100, 97, 112, 115, 0, 0, 96, 38,
337 115, 98, 117, 108, 99, 0, 0, 0, 99, 38,
338 115, 116, 114, 97, 101, 104, 0, 0, 101, 38,
339 115, 109, 97, 105, 100, 0, 0, 0, 102, 38
340 };
342 while (!source.IsEmpty)
343 {
347 source = source.Slice(10);
348 }
349 return dictionary;
350 }
351
352 public static char Lookup(ReadOnlySpan<char> entity)
353 {
354 if (entity.Length <= 8)
355 {
356 s_lookupTable.TryGetValue(ToUInt64Key(entity), out var value);
357 return value;
358 }
359 return '\0';
360 }
361
362 private static ulong ToUInt64Key(ReadOnlySpan<char> entity)
363 {
364 ulong num = 0uL;
365 for (int i = 0; i < entity.Length; i++)
366 {
367 if (entity[i] > 'ÿ')
368 {
369 return 0uL;
370 }
371 num = (num << 8) | entity[i];
372 }
373 return num;
374 }
375 }
376
377 [return: NotNullIfNotNull("value")]
378 public static string? HtmlEncode(string? value)
379 {
380 if (string.IsNullOrEmpty(value))
381 {
382 return value;
383 }
384 ReadOnlySpan<char> input = value.AsSpan();
386 if (num == -1)
387 {
388 return value;
389 }
391 if (value.Length < 80)
392 {
395 }
396 else
397 {
398 valueStringBuilder = new ValueStringBuilder(value.Length + 200);
399 }
401 output.Append(input.Slice(0, num));
402 HtmlEncode(input.Slice(num), ref output);
403 return output.ToString();
404 }
405
406 public static void HtmlEncode(string? value, TextWriter output)
407 {
408 if (output == null)
409 {
410 throw new ArgumentNullException("output");
411 }
412 if (string.IsNullOrEmpty(value))
413 {
414 output.Write(value);
415 return;
416 }
417 ReadOnlySpan<char> input = value.AsSpan();
419 if (num == -1)
420 {
421 output.Write(value);
422 return;
423 }
425 if (value.Length < 80)
426 {
429 }
430 else
431 {
432 valueStringBuilder = new ValueStringBuilder(value.Length + 200);
433 }
435 output2.Append(input.Slice(0, num));
436 HtmlEncode(input.Slice(num), ref output2);
437 output.Write(output2.AsSpan());
438 output2.Dispose();
439 }
440
442 {
443 for (int i = 0; i < input.Length; i++)
444 {
445 char c = input[i];
446 if (c <= '>')
447 {
448 switch (c)
449 {
450 case '<':
451 output.Append("&lt;");
452 break;
453 case '>':
454 output.Append("&gt;");
455 break;
456 case '"':
457 output.Append("&quot;");
458 break;
459 case '\'':
460 output.Append("&#39;");
461 break;
462 case '&':
463 output.Append("&amp;");
464 break;
465 default:
466 output.Append(c);
467 break;
468 }
469 continue;
470 }
471 int num = -1;
472 if (c >= '\u00a0' && c < 'Ä€')
473 {
474 num = c;
475 }
476 else if (char.IsSurrogate(c))
477 {
480 {
482 }
483 else
484 {
486 }
487 }
488 if (num >= 0)
489 {
490 output.Append("&#");
491 Span<char> destination = output.AppendSpan(10);
492 num.TryFormat(destination, out var charsWritten);
493 output.Length -= 10 - charsWritten;
494 output.Append(';');
495 }
496 else
497 {
498 output.Append(c);
499 }
500 }
501 }
502
503 [return: NotNullIfNotNull("value")]
504 public static string? HtmlDecode(string? value)
505 {
506 if (string.IsNullOrEmpty(value))
507 {
508 return value;
509 }
510 ReadOnlySpan<char> input = value.AsSpan();
512 if (num == -1)
513 {
514 return value;
515 }
517 if (value.Length <= 256)
518 {
521 }
522 else
523 {
525 }
527 output.Append(input.Slice(0, num));
528 HtmlDecode(input.Slice(num), ref output);
529 return output.ToString();
530 }
531
532 public static void HtmlDecode(string? value, TextWriter output)
533 {
534 if (output == null)
535 {
536 throw new ArgumentNullException("output");
537 }
538 if (string.IsNullOrEmpty(value))
539 {
540 output.Write(value);
541 return;
542 }
543 ReadOnlySpan<char> input = value.AsSpan();
545 if (num == -1)
546 {
547 output.Write(value);
548 return;
549 }
551 if (value.Length <= 256)
552 {
555 }
556 else
557 {
559 }
561 output2.Append(input.Slice(0, num));
562 HtmlDecode(input.Slice(num), ref output2);
563 output.Write(output2.AsSpan());
564 output2.Dispose();
565 }
566
568 {
569 for (int i = 0; i < input.Length; i++)
570 {
571 char c = input[i];
572 if (c == '&')
573 {
574 ReadOnlySpan<char> span = input.Slice(i + 1);
575 int num = span.IndexOfAny(';', '&');
576 if (num >= 0 && span[num] == ';')
577 {
578 int num2 = i + 1 + num;
579 if (num > 1 && span[0] == '#')
580 {
581 uint result;
582 bool flag = ((span[1] == 'x' || span[1] == 'X') ? uint.TryParse(span.Slice(2, num - 2), NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture, out result) : uint.TryParse(span.Slice(1, num - 1), NumberStyles.Integer, CultureInfo.InvariantCulture, out result));
583 if (flag)
584 {
585 flag = result < 55296 || (57343 < result && result <= 1114111);
586 }
587 if (flag)
588 {
589 if (result <= 65535)
590 {
591 output.Append((char)result);
592 }
593 else
594 {
596 output.Append(leadingSurrogate);
598 }
599 i = num2;
600 continue;
601 }
602 }
603 else
604 {
605 ReadOnlySpan<char> readOnlySpan = span.Slice(0, num);
606 i = num2;
608 if (c2 == '\0')
609 {
610 output.Append('&');
611 output.Append(readOnlySpan);
612 output.Append(';');
613 continue;
614 }
615 c = c2;
616 }
617 }
618 }
619 output.Append(c);
620 }
621 }
622
624 {
625 for (int i = 0; i < input.Length; i++)
626 {
627 char c = input[i];
628 if (c <= '>')
629 {
630 switch (c)
631 {
632 case '"':
633 case '&':
634 case '\'':
635 case '<':
636 case '>':
637 return i;
638 }
639 continue;
640 }
641 if (c >= '\u00a0' && c < 'Ä€')
642 {
643 return i;
644 }
645 if (char.IsSurrogate(c))
646 {
647 return i;
648 }
649 }
650 return -1;
651 }
652
653 private static void GetEncodedBytes(byte[] originalBytes, int offset, int count, byte[] expandedBytes)
654 {
655 int num = 0;
656 int num2 = offset + count;
657 for (int i = offset; i < num2; i++)
658 {
659 byte b = originalBytes[i];
660 char c = (char)b;
661 if (IsUrlSafeChar(c))
662 {
663 expandedBytes[num++] = b;
664 continue;
665 }
666 if (c == ' ')
667 {
668 expandedBytes[num++] = 43;
669 continue;
670 }
671 expandedBytes[num++] = 37;
672 expandedBytes[num++] = (byte)HexConverter.ToCharUpper(b >> 4);
673 expandedBytes[num++] = (byte)HexConverter.ToCharUpper(b);
674 }
675 }
676
677 [return: NotNullIfNotNull("value")]
678 public static string? UrlEncode(string? value)
679 {
680 if (string.IsNullOrEmpty(value))
681 {
682 return value;
683 }
684 int num = 0;
685 int num2 = 0;
686 foreach (char c in value)
687 {
688 if (IsUrlSafeChar(c))
689 {
690 num++;
691 }
692 else if (c == ' ')
693 {
694 num2++;
695 }
696 }
697 int num3 = num + num2;
698 if (num3 == value.Length)
699 {
700 if (num2 != 0)
701 {
702 return value.Replace(' ', '+');
703 }
704 return value;
705 }
706 int byteCount = Encoding.UTF8.GetByteCount(value);
707 int num4 = byteCount - num3;
708 int num5 = num4 * 2;
709 byte[] array = new byte[byteCount + num5];
710 Encoding.UTF8.GetBytes(value, 0, value.Length, array, num5);
712 return Encoding.UTF8.GetString(array);
713 }
714
715 [return: NotNullIfNotNull("value")]
716 public static byte[]? UrlEncodeToBytes(byte[]? value, int offset, int count)
717 {
719 {
720 return null;
721 }
722 bool flag = false;
723 int num = 0;
724 for (int i = 0; i < count; i++)
725 {
726 char c = (char)value[offset + i];
727 if (c == ' ')
728 {
729 flag = true;
730 }
731 else if (!IsUrlSafeChar(c))
732 {
733 num++;
734 }
735 }
736 if (!flag && num == 0)
737 {
738 byte[] array = new byte[count];
740 return array;
741 }
742 byte[] array2 = new byte[count + num * 2];
744 return array2;
745 }
746
747 [return: NotNullIfNotNull("value")]
748 private static string UrlDecodeInternal(string value, Encoding encoding)
749 {
750 if (string.IsNullOrEmpty(value))
751 {
752 return value;
753 }
754 int length = value.Length;
755 UrlDecoder urlDecoder = new UrlDecoder(length, encoding);
756 bool flag = false;
757 bool flag2 = false;
758 for (int i = 0; i < length; i++)
759 {
760 char c = value[i];
761 switch (c)
762 {
763 case '+':
764 flag2 = true;
765 c = ' ';
766 break;
767 case '%':
768 if (i < length - 2)
769 {
770 int num = HexConverter.FromChar(value[i + 1]);
771 int num2 = HexConverter.FromChar(value[i + 2]);
772 if ((num | num2) != 255)
773 {
774 byte b = (byte)((num << 4) | num2);
775 i += 2;
776 urlDecoder.AddByte(b);
777 flag = true;
778 continue;
779 }
780 }
781 break;
782 }
783 if ((c & 0xFF80) == 0)
784 {
785 urlDecoder.AddByte((byte)c);
786 }
787 else
788 {
789 urlDecoder.AddChar(c);
790 }
791 }
792 if (!flag)
793 {
794 if (flag2)
795 {
796 return value.Replace('+', ' ');
797 }
798 return value;
799 }
800 return urlDecoder.GetString();
801 }
802
803 [return: NotNullIfNotNull("bytes")]
804 private static byte[] UrlDecodeInternal(byte[] bytes, int offset, int count)
805 {
807 {
808 return null;
809 }
810 int num = 0;
811 byte[] array = new byte[count];
812 for (int i = 0; i < count; i++)
813 {
814 int num2 = offset + i;
815 byte b = bytes[num2];
816 switch (b)
817 {
818 case 43:
819 b = 32;
820 break;
821 case 37:
822 if (i < count - 2)
823 {
824 int num3 = HexConverter.FromChar(bytes[num2 + 1]);
825 int num4 = HexConverter.FromChar(bytes[num2 + 2]);
826 if ((num3 | num4) != 255)
827 {
828 b = (byte)((num3 << 4) | num4);
829 i += 2;
830 }
831 }
832 break;
833 }
834 array[num++] = b;
835 }
836 if (num < array.Length)
837 {
838 Array.Resize(ref array, num);
839 }
840 return array;
841 }
842
843 [return: NotNullIfNotNull("encodedValue")]
844 public static string? UrlDecode(string? encodedValue)
845 {
847 }
848
849 [return: NotNullIfNotNull("encodedValue")]
850 public static byte[]? UrlDecodeToBytes(byte[]? encodedValue, int offset, int count)
851 {
853 }
854
856 {
857 int num = (int)(smpChar - 65536);
858 leadingSurrogate = (char)(num / 1024 + 55296);
859 trailingSurrogate = (char)(num % 1024 + 56320);
860 }
861
863 {
864 if (input.Length - index <= 1)
865 {
866 return 65533;
867 }
868 char c = input[index];
869 char c2 = input[index + 1];
870 if (!char.IsSurrogatePair(c, c2))
871 {
872 return 65533;
873 }
874 index++;
875 return (c - 55296) * 1024 + (c2 - 56320) + 65536;
876 }
877
878 [MethodImpl(MethodImplOptions.AggressiveInlining)]
879 private static bool IsUrlSafeChar(char ch)
880 {
881 if ((uint)(ch - 97) > 25u && (uint)(ch - 65) > 25u && ((uint)(ch - 32) > 25u || ((1 << ch - 32) & 0x3FF6702) == 0))
882 {
883 return ch == '_';
884 }
885 return true;
886 }
887
888 private static bool ValidateUrlEncodingParameters(byte[] bytes, int offset, int count)
889 {
890 if (bytes == null && count == 0)
891 {
892 return false;
893 }
894 if (bytes == null)
895 {
896 throw new ArgumentNullException("bytes");
897 }
898 if (offset < 0 || offset > bytes.Length)
899 {
900 throw new ArgumentOutOfRangeException("offset");
901 }
903 {
904 throw new ArgumentOutOfRangeException("count");
905 }
906 return true;
907 }
908
910 {
911 for (int i = 0; i < input.Length; i++)
912 {
913 char c = input[i];
914 if (c == '&' || char.IsSurrogate(c))
915 {
916 return i;
917 }
918 }
919 return -1;
920 }
921}
static void BlockCopy(Array src, int srcOffset, Array dst, int dstOffset, int count)
Definition Buffer.cs:102
static ulong ReadUInt64LittleEndian(ReadOnlySpan< byte > source)
static ushort ReadUInt16LittleEndian(ReadOnlySpan< byte > source)
static CultureInfo InvariantCulture
static int FromChar(int c)
static char ToCharUpper(int value)
static readonly Dictionary< ulong, char > s_lookupTable
Definition WebUtility.cs:81
static char Lookup(ReadOnlySpan< char > entity)
static Dictionary< ulong, char > InitializeLookupTable()
Definition WebUtility.cs:83
static ulong ToUInt64Key(ReadOnlySpan< char > entity)
static ? string HtmlDecode(string? value)
static void HtmlDecode(ReadOnlySpan< char > input, ref ValueStringBuilder output)
static ? string UrlDecode(string? encodedValue)
static ? string HtmlEncode(string? value)
static void HtmlEncode(string? value, TextWriter output)
static void HtmlDecode(string? value, TextWriter output)
static ? byte[] UrlEncodeToBytes(byte[]? value, int offset, int count)
static void HtmlEncode(ReadOnlySpan< char > input, ref ValueStringBuilder output)
static int GetNextUnicodeScalarValueFromUtf16Surrogate(ReadOnlySpan< char > input, ref int index)
static int IndexOfHtmlDecodingChars(ReadOnlySpan< char > input)
static string UrlDecodeInternal(string value, Encoding encoding)
static void ConvertSmpToUtf16(uint smpChar, out char leadingSurrogate, out char trailingSurrogate)
static ? string UrlEncode(string? value)
static int IndexOfHtmlEncodingChars(ReadOnlySpan< char > input)
static void GetEncodedBytes(byte[] originalBytes, int offset, int count, byte[] expandedBytes)
static bool ValidateUrlEncodingParameters(byte[] bytes, int offset, int count)
static byte[] UrlDecodeInternal(byte[] bytes, int offset, int count)
static bool IsUrlSafeChar(char ch)
static ? byte[] UrlDecodeToBytes(byte[]? encodedValue, int offset, int count)
static Encoding UTF8
Definition Encoding.cs:526
virtual char[] GetChars(byte[] bytes)
Definition Encoding.cs:921
UrlDecoder(int bufferSize, Encoding encoding)
Definition WebUtility.cs:37