Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
IPv6AddressHelper.cs
Go to the documentation of this file.
1namespace System;
2
3internal static class IPv6AddressHelper
4{
5 internal static (int longestSequenceStart, int longestSequenceLength) FindCompressionRange(ReadOnlySpan<ushort> numbers)
6 {
7 int num = 0;
8 int num2 = -1;
9 int num3 = 0;
10 for (int i = 0; i < numbers.Length; i++)
11 {
12 if (numbers[i] == 0)
13 {
14 num3++;
15 if (num3 > num)
16 {
17 num = num3;
18 num2 = i - num3 + 1;
19 }
20 }
21 else
22 {
23 num3 = 0;
24 }
25 }
26 if (num <= 1)
27 {
28 return (longestSequenceStart: -1, longestSequenceLength: -1);
29 }
30 return (longestSequenceStart: num2, longestSequenceLength: num2 + num);
31 }
32
33 internal static bool ShouldHaveIpv4Embedded(ReadOnlySpan<ushort> numbers)
34 {
35 if (numbers[0] == 0 && numbers[1] == 0 && numbers[2] == 0 && numbers[3] == 0 && numbers[6] != 0)
36 {
37 if (numbers[4] == 0 && (numbers[5] == 0 || numbers[5] == ushort.MaxValue))
38 {
39 return true;
40 }
41 if (numbers[4] == ushort.MaxValue && numbers[5] == 0)
42 {
43 return true;
44 }
45 }
46 if (numbers[4] == 0)
47 {
48 return numbers[5] == 24318;
49 }
50 return false;
51 }
52
53 internal unsafe static bool IsValidStrict(char* name, int start, ref int end)
54 {
55 int num = 0;
56 int num2 = 0;
57 bool flag = false;
58 bool flag2 = false;
59 bool flag3 = true;
60 int start2 = 1;
61 bool flag4 = false;
62 if (start < end && name[start] == '[')
63 {
64 start++;
65 flag4 = true;
66 }
67 if (name[start] == ':' && (start + 1 >= end || name[start + 1] != ':'))
68 {
69 return false;
70 }
71 for (int i = start; i < end; i++)
72 {
73 if (Uri.IsHexDigit(name[i]))
74 {
75 num2++;
76 flag3 = false;
77 continue;
78 }
79 if (num2 > 4)
80 {
81 return false;
82 }
83 if (num2 != 0)
84 {
85 num++;
86 start2 = i - num2;
87 num2 = 0;
88 }
89 char c = name[i];
90 if ((uint)c <= 46u)
91 {
92 if (c != '%')
93 {
94 if (c != '.')
95 {
96 goto IL_01e6;
97 }
98 if (flag2)
99 {
100 return false;
101 }
102 i = end;
103 if (!System.IPv4AddressHelper.IsValid(name, start2, ref i, allowIPv6: true, notImplicitFile: false, unknownScheme: false))
104 {
105 return false;
106 }
107 num++;
108 start2 = i - num2;
109 num2 = 0;
110 flag2 = true;
111 i--;
112 }
113 else
114 {
115 while (i + 1 < end)
116 {
117 i++;
118 if (name[i] == ']')
119 {
120 goto IL_00f1;
121 }
122 if (name[i] != '/')
123 {
124 continue;
125 }
126 goto IL_01b4;
127 }
128 }
129 }
130 else
131 {
132 if (c == '/')
133 {
134 goto IL_01b4;
135 }
136 if (c != ':')
137 {
138 if (c == ']')
139 {
140 goto IL_00f1;
141 }
142 goto IL_01e6;
143 }
144 if (i > 0 && name[i - 1] == ':')
145 {
146 if (flag)
147 {
148 return false;
149 }
150 flag = true;
151 flag3 = false;
152 }
153 else
154 {
155 flag3 = true;
156 }
157 }
158 num2 = 0;
159 continue;
160 IL_01e6:
161 return false;
162 IL_01b4:
163 return false;
164 IL_00f1:
165 if (!flag4)
166 {
167 return false;
168 }
169 flag4 = false;
170 if (i + 1 < end && name[i + 1] != ':')
171 {
172 return false;
173 }
174 if (i + 3 < end && name[i + 2] == '0' && name[i + 3] == 'x')
175 {
176 for (i += 4; i < end; i++)
177 {
178 if (!Uri.IsHexDigit(name[i]))
179 {
180 return false;
181 }
182 }
183 continue;
184 }
185 for (i += 2; i < end; i++)
186 {
187 if (name[i] < '0' || name[i] > '9')
188 {
189 return false;
190 }
191 }
192 }
193 if (num2 != 0)
194 {
195 if (num2 > 4)
196 {
197 return false;
198 }
199 num++;
200 }
201 if (!flag3 && (flag ? (num < 8) : (num == 8)))
202 {
203 return !flag4;
204 }
205 return false;
206 }
207
208 internal static void Parse(ReadOnlySpan<char> address, Span<ushort> numbers, int start, ref string scopeId)
209 {
210 int num = 0;
211 int num2 = 0;
212 int num3 = -1;
213 bool flag = true;
214 int num4 = 0;
215 if (address[start] == '[')
216 {
217 start++;
218 }
219 int i = start;
220 while (i < address.Length && address[i] != ']')
221 {
222 switch (address[i])
223 {
224 case '%':
225 if (flag)
226 {
227 numbers[num2++] = (ushort)num;
228 flag = false;
229 }
230 start = i;
231 for (i++; i < address.Length && address[i] != ']' && address[i] != '/'; i++)
232 {
233 }
234 scopeId = new string(address.Slice(start, i - start));
235 for (; i < address.Length && address[i] != ']'; i++)
236 {
237 }
238 break;
239 case ':':
240 {
241 numbers[num2++] = (ushort)num;
242 num = 0;
243 i++;
244 if (address[i] == ':')
245 {
246 num3 = num2;
247 i++;
248 }
249 else if (num3 < 0 && num2 < 6)
250 {
251 break;
252 }
253 for (int j = i; j < address.Length && address[j] != ']' && address[j] != ':' && address[j] != '%' && address[j] != '/' && j < i + 4; j++)
254 {
255 if (address[j] == '.')
256 {
257 for (; j < address.Length && address[j] != ']' && address[j] != '/' && address[j] != '%'; j++)
258 {
259 }
260 num = System.IPv4AddressHelper.ParseHostNumber(address, i, j);
261 numbers[num2++] = (ushort)(num >> 16);
262 numbers[num2++] = (ushort)num;
263 i = j;
264 num = 0;
265 flag = false;
266 break;
267 }
268 }
269 break;
270 }
271 case '/':
272 if (flag)
273 {
274 numbers[num2++] = (ushort)num;
275 flag = false;
276 }
277 for (i++; address[i] != ']'; i++)
278 {
279 num4 = num4 * 10 + (address[i] - 48);
280 }
281 break;
282 default:
283 num = num * 16 + Uri.FromHex(address[i++]);
284 break;
285 }
286 }
287 if (flag)
288 {
289 numbers[num2++] = (ushort)num;
290 }
291 if (num3 <= 0)
292 {
293 return;
294 }
295 int num5 = 7;
296 int num6 = num2 - 1;
297 if (num6 != num5)
298 {
299 for (int num7 = num2 - num3; num7 > 0; num7--)
300 {
301 numbers[num5--] = numbers[num6];
302 numbers[num6--] = 0;
303 }
304 }
305 }
306}
static unsafe bool IsValid(char *name, int start, ref int end, bool allowIPv6, bool notImplicitFile, bool unknownScheme)
static int ParseHostNumber(ReadOnlySpan< char > str, int start, int end)
static unsafe bool IsValidStrict(char *name, int start, ref int end)
static void Parse(ReadOnlySpan< char > address, Span< ushort > numbers, int start, ref string scopeId)
static bool ShouldHaveIpv4Embedded(ReadOnlySpan< ushort > numbers)
static int int longestSequenceLength FindCompressionRange(ReadOnlySpan< ushort > numbers)
static int FromHex(char digit)
Definition Uri.cs:1093
static bool IsHexDigit(char character)
Definition Uri.cs:1088
ReadOnlySpan< T > Slice(int start)