Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
DomainNameHelper.cs
Go to the documentation of this file.
3using System.Text;
4
5namespace System;
6
7internal static class DomainNameHelper
8{
9 private static readonly IdnMapping s_idnMapping = new IdnMapping();
10
11 private static readonly char[] s_UnsafeForNormalizedHost = new char[8] { '\\', '/', '?', '@', '#', ':', '[', ']' };
12
13 internal static string ParseCanonicalName(string str, int start, int end, ref bool loopback)
14 {
15 string text = null;
16 for (int num = end - 1; num >= start; num--)
17 {
18 if (str[num] >= 'A' && str[num] <= 'Z')
19 {
20 text = str.Substring(start, end - start).ToLowerInvariant();
21 break;
22 }
23 if (str[num] == ':')
24 {
25 end = num;
26 }
27 }
28 if (text == null)
29 {
30 text = str.Substring(start, end - start);
31 }
32 if (text == "localhost" || text == "loopback")
33 {
34 loopback = true;
35 return "localhost";
36 }
37 return text;
38 }
39
40 internal unsafe static bool IsValid(char* name, int pos, ref int returnedEnd, ref bool notCanonical, bool notImplicitFile)
41 {
42 char* ptr = name + pos;
43 char* ptr2 = ptr;
44 char* ptr3;
45 for (ptr3 = name + returnedEnd; ptr2 < ptr3; ptr2++)
46 {
47 char c = *ptr2;
48 if (c > '\u007f')
49 {
50 return false;
51 }
52 if (c < 'a' && (c == '/' || c == '\\' || (notImplicitFile && (c == ':' || c == '?' || c == '#'))))
53 {
54 ptr3 = ptr2;
55 break;
56 }
57 }
58 if (ptr3 == ptr)
59 {
60 return false;
61 }
62 do
63 {
64 for (ptr2 = ptr; ptr2 < ptr3 && *ptr2 != '.'; ptr2++)
65 {
66 }
67 if (ptr == ptr2 || ptr2 - ptr > 63 || !IsASCIILetterOrDigit(*(ptr++), ref notCanonical))
68 {
69 return false;
70 }
71 while (ptr < ptr2)
72 {
73 if (!IsValidDomainLabelCharacter(*(ptr++), ref notCanonical))
74 {
75 return false;
76 }
77 }
78 ptr++;
79 }
80 while (ptr < ptr3);
81 returnedEnd = (int)(ptr3 - name);
82 return true;
83 }
84
85 internal unsafe static bool IsValidByIri(char* name, int pos, ref int returnedEnd, ref bool notCanonical, bool notImplicitFile)
86 {
87 char* ptr = name + pos;
88 char* ptr2 = ptr;
89 char* ptr3 = name + returnedEnd;
90 int num = 0;
91 for (; ptr2 < ptr3; ptr2++)
92 {
93 char c = *ptr2;
94 if (c == '/' || c == '\\' || (notImplicitFile && (c == ':' || c == '?' || c == '#')))
95 {
96 ptr3 = ptr2;
97 break;
98 }
99 }
100 if (ptr3 == ptr)
101 {
102 return false;
103 }
104 do
105 {
106 ptr2 = ptr;
107 num = 0;
108 bool flag = false;
109 for (; ptr2 < ptr3 && *ptr2 != '.' && *ptr2 != '。' && *ptr2 != '.' && *ptr2 != '。'; ptr2++)
110 {
111 num++;
112 if (*ptr2 > 'ÿ')
113 {
114 num++;
115 }
116 if (*ptr2 >= '\u00a0')
117 {
118 flag = true;
119 }
120 }
121 if (ptr == ptr2 || (flag ? (num + 4) : num) > 63 || (*(ptr++) < '\u00a0' && !IsASCIILetterOrDigit(*(ptr - 1), ref notCanonical)))
122 {
123 return false;
124 }
125 while (ptr < ptr2)
126 {
127 if (*(ptr++) < '\u00a0' && !IsValidDomainLabelCharacter(*(ptr - 1), ref notCanonical))
128 {
129 return false;
130 }
131 }
132 ptr++;
133 }
134 while (ptr < ptr3);
135 returnedEnd = (int)(ptr3 - name);
136 return true;
137 }
138
139 internal static string IdnEquivalent(string hostname)
140 {
141 if (hostname.Length == 0)
142 {
143 return hostname;
144 }
145 bool flag = true;
146 foreach (char c in hostname)
147 {
148 if (c > '\u007f')
149 {
150 flag = false;
151 break;
152 }
153 }
154 if (flag)
155 {
156 return hostname.ToLowerInvariant();
157 }
158 string unicode = UriHelper.StripBidiControlCharacters(hostname, hostname);
159 try
160 {
161 string ascii = s_idnMapping.GetAscii(unicode);
163 {
165 }
166 return ascii;
167 }
168 catch (ArgumentException)
169 {
171 }
172 }
173
174 internal static bool TryGetUnicodeEquivalent(string hostname, ref System.Text.ValueStringBuilder dest)
175 {
176 int num = 0;
177 do
178 {
179 if (num != 0)
180 {
181 dest.Append('.');
182 }
183 bool flag = true;
184 int i;
185 for (i = num; (uint)i < (uint)hostname.Length; i++)
186 {
187 char c = hostname[i];
188 if (c == '.')
189 {
190 break;
191 }
192 if (c > '\u007f')
193 {
194 flag = false;
195 if (c == '。' || c == '.' || c == '。')
196 {
197 break;
198 }
199 }
200 }
201 if (!flag)
202 {
203 try
204 {
205 string ascii = s_idnMapping.GetAscii(hostname, num, i - num);
206 dest.Append(s_idnMapping.GetUnicode(ascii));
207 }
208 catch (ArgumentException)
209 {
210 return false;
211 }
212 }
213 else
214 {
215 bool flag2 = false;
216 if ((uint)(num + 3) < (uint)hostname.Length && hostname[num] == 'x' && hostname[num + 1] == 'n' && hostname[num + 2] == '-' && hostname[num + 3] == '-')
217 {
218 try
219 {
220 dest.Append(s_idnMapping.GetUnicode(hostname, num, i - num));
221 flag2 = true;
222 }
223 catch (ArgumentException)
224 {
225 }
226 }
227 if (!flag2)
228 {
229 ReadOnlySpan<char> source = hostname.AsSpan(num, i - num);
230 int num2 = source.ToLowerInvariant(dest.AppendSpan(source.Length));
231 }
232 }
233 num = i + 1;
234 }
235 while (num < hostname.Length);
236 return true;
237 }
238
239 [MethodImpl(MethodImplOptions.AggressiveInlining)]
240 private static bool IsASCIILetterOrDigit(char character, ref bool notCanonical)
241 {
242 switch (character)
243 {
244 case '0':
245 case '1':
246 case '2':
247 case '3':
248 case '4':
249 case '5':
250 case '6':
251 case '7':
252 case '8':
253 case '9':
254 case 'a':
255 case 'b':
256 case 'c':
257 case 'd':
258 case 'e':
259 case 'f':
260 case 'g':
261 case 'h':
262 case 'i':
263 case 'j':
264 case 'k':
265 case 'l':
266 case 'm':
267 case 'n':
268 case 'o':
269 case 'p':
270 case 'q':
271 case 'r':
272 case 's':
273 case 't':
274 case 'u':
275 case 'v':
276 case 'w':
277 case 'x':
278 case 'y':
279 case 'z':
280 return true;
281 case 'A':
282 case 'B':
283 case 'C':
284 case 'D':
285 case 'E':
286 case 'F':
287 case 'G':
288 case 'H':
289 case 'I':
290 case 'J':
291 case 'K':
292 case 'L':
293 case 'M':
294 case 'N':
295 case 'O':
296 case 'P':
297 case 'Q':
298 case 'R':
299 case 'S':
300 case 'T':
301 case 'U':
302 case 'V':
303 case 'W':
304 case 'X':
305 case 'Y':
306 case 'Z':
307 notCanonical = true;
308 return true;
309 default:
310 return false;
311 }
312 }
313
314 [MethodImpl(MethodImplOptions.AggressiveInlining)]
315 private static bool IsValidDomainLabelCharacter(char character, ref bool notCanonical)
316 {
317 switch (character)
318 {
319 case '-':
320 case '0':
321 case '1':
322 case '2':
323 case '3':
324 case '4':
325 case '5':
326 case '6':
327 case '7':
328 case '8':
329 case '9':
330 case '_':
331 case 'a':
332 case 'b':
333 case 'c':
334 case 'd':
335 case 'e':
336 case 'f':
337 case 'g':
338 case 'h':
339 case 'i':
340 case 'j':
341 case 'k':
342 case 'l':
343 case 'm':
344 case 'n':
345 case 'o':
346 case 'p':
347 case 'q':
348 case 'r':
349 case 's':
350 case 't':
351 case 'u':
352 case 'v':
353 case 'w':
354 case 'x':
355 case 'y':
356 case 'z':
357 return true;
358 case 'A':
359 case 'B':
360 case 'C':
361 case 'D':
362 case 'E':
363 case 'F':
364 case 'G':
365 case 'H':
366 case 'I':
367 case 'J':
368 case 'K':
369 case 'L':
370 case 'M':
371 case 'N':
372 case 'O':
373 case 'P':
374 case 'Q':
375 case 'R':
376 case 'S':
377 case 'T':
378 case 'U':
379 case 'V':
380 case 'W':
381 case 'X':
382 case 'Y':
383 case 'Z':
384 notCanonical = true;
385 return true;
386 default:
387 return false;
388 }
389 }
390
391 internal static bool ContainsCharactersUnsafeForNormalizedHost(string host)
392 {
393 return host.IndexOfAny(s_UnsafeForNormalizedHost) != -1;
394 }
395}
static bool ContainsCharactersUnsafeForNormalizedHost(string host)
static bool IsValidDomainLabelCharacter(char character, ref bool notCanonical)
static bool IsASCIILetterOrDigit(char character, ref bool notCanonical)
static readonly IdnMapping s_idnMapping
static bool TryGetUnicodeEquivalent(string hostname, ref System.Text.ValueStringBuilder dest)
static string ParseCanonicalName(string str, int start, int end, ref bool loopback)
static unsafe bool IsValid(char *name, int pos, ref int returnedEnd, ref bool notCanonical, bool notImplicitFile)
static readonly char[] s_UnsafeForNormalizedHost
static string IdnEquivalent(string hostname)
static unsafe bool IsValidByIri(char *name, int pos, ref int returnedEnd, ref bool notCanonical, bool notImplicitFile)
static string net_uri_BadUnicodeHostForIdn
Definition SR.cs:54
Definition SR.cs:7
static unsafe string StripBidiControlCharacters(ReadOnlySpan< char > strToClean, string backingString=null)
Definition UriHelper.cs:485