Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
IPv4AddressHelper.cs
Go to the documentation of this file.
2
3namespace System;
4
5internal static class IPv4AddressHelper
6{
7 internal static int ParseHostNumber(ReadOnlySpan<char> str, int start, int end)
8 {
9 Span<byte> span = stackalloc byte[4];
10 for (int i = 0; i < span.Length; i++)
11 {
12 int num = 0;
13 char c;
14 while (start < end && (c = str[start]) != '.' && c != ':')
15 {
16 num = num * 10 + c - 48;
17 start++;
18 }
19 span[i] = (byte)num;
20 start++;
21 }
23 }
24
25 internal unsafe static bool IsValid(char* name, int start, ref int end, bool allowIPv6, bool notImplicitFile, bool unknownScheme)
26 {
27 if (allowIPv6 || unknownScheme)
28 {
29 return IsValidCanonical(name, start, ref end, allowIPv6, notImplicitFile);
30 }
31 return ParseNonCanonical(name, start, ref end, notImplicitFile) != -1;
32 }
33
34 internal unsafe static bool IsValidCanonical(char* name, int start, ref int end, bool allowIPv6, bool notImplicitFile)
35 {
36 int num = 0;
37 int num2 = 0;
38 bool flag = false;
39 bool flag2 = false;
40 while (start < end)
41 {
42 char c = name[start];
43 if (allowIPv6)
44 {
45 if (c == ']' || c == '/' || c == '%')
46 {
47 break;
48 }
49 }
50 else if (c == '/' || c == '\\' || (notImplicitFile && (c == ':' || c == '?' || c == '#')))
51 {
52 break;
53 }
54 if (c <= '9' && c >= '0')
55 {
56 if (!flag && c == '0')
57 {
58 if (start + 1 < end && name[start + 1] == '0')
59 {
60 return false;
61 }
62 flag2 = true;
63 }
64 flag = true;
65 num2 = num2 * 10 + (name[start] - 48);
66 if (num2 > 255)
67 {
68 return false;
69 }
70 }
71 else
72 {
73 if (c != '.')
74 {
75 return false;
76 }
77 if (!flag || (num2 > 0 && flag2))
78 {
79 return false;
80 }
81 num++;
82 flag = false;
83 num2 = 0;
84 flag2 = false;
85 }
86 start++;
87 }
88 bool flag3 = num == 3 && flag;
89 if (flag3)
90 {
91 end = start;
92 }
93 return flag3;
94 }
95
96 internal unsafe static long ParseNonCanonical(char* name, int start, ref int end, bool notImplicitFile)
97 {
98 int num = 10;
99 long* ptr = stackalloc long[4];
100 long num2 = 0L;
101 bool flag = false;
102 int num3 = 0;
103 int i;
104 for (i = start; i < end; i++)
105 {
106 char c = name[i];
107 num2 = 0L;
108 num = 10;
109 if (c == '0')
110 {
111 num = 8;
112 i++;
113 flag = true;
114 if (i < end)
115 {
116 c = name[i];
117 if (c == 'x' || c == 'X')
118 {
119 num = 16;
120 i++;
121 flag = false;
122 }
123 }
124 }
125 for (; i < end; i++)
126 {
127 c = name[i];
128 int num4;
129 if ((num == 10 || num == 16) && '0' <= c && c <= '9')
130 {
131 num4 = c - 48;
132 }
133 else if (num == 8 && '0' <= c && c <= '7')
134 {
135 num4 = c - 48;
136 }
137 else if (num == 16 && 'a' <= c && c <= 'f')
138 {
139 num4 = c + 10 - 97;
140 }
141 else
142 {
143 if (num != 16 || 'A' > c || c > 'F')
144 {
145 break;
146 }
147 num4 = c + 10 - 65;
148 }
149 num2 = num2 * num + num4;
150 if (num2 > uint.MaxValue)
151 {
152 return -1L;
153 }
154 flag = true;
155 }
156 if (i >= end || name[i] != '.')
157 {
158 break;
159 }
160 if (num3 >= 3 || !flag || num2 > 255)
161 {
162 return -1L;
163 }
164 ptr[num3] = num2;
165 num3++;
166 flag = false;
167 }
168 if (!flag)
169 {
170 return -1L;
171 }
172 if (i < end)
173 {
174 char c;
175 if ((c = name[i]) != '/' && c != '\\' && (!notImplicitFile || (c != ':' && c != '?' && c != '#')))
176 {
177 return -1L;
178 }
179 end = i;
180 }
181 ptr[num3] = num2;
182 switch (num3)
183 {
184 case 0:
185 if (*ptr > uint.MaxValue)
186 {
187 return -1L;
188 }
189 return *ptr;
190 case 1:
191 if (ptr[1] > 16777215)
192 {
193 return -1L;
194 }
195 return (*ptr << 24) | (ptr[1] & 0xFFFFFF);
196 case 2:
197 if (ptr[2] > 65535)
198 {
199 return -1L;
200 }
201 return (*ptr << 24) | ((ptr[1] & 0xFF) << 16) | (ptr[2] & 0xFFFF);
202 case 3:
203 if (ptr[3] > 255)
204 {
205 return -1L;
206 }
207 return (*ptr << 24) | ((ptr[1] & 0xFF) << 16) | ((ptr[2] & 0xFF) << 8) | (ptr[3] & 0xFF);
208 default:
209 return -1L;
210 }
211 }
212}
static int ReadInt32BigEndian(ReadOnlySpan< byte > source)
static unsafe bool IsValid(char *name, int start, ref int end, bool allowIPv6, bool notImplicitFile, bool unknownScheme)
static unsafe bool IsValidCanonical(char *name, int start, ref int end, bool allowIPv6, bool notImplicitFile)
static int ParseHostNumber(ReadOnlySpan< char > str, int start, int end)
static unsafe long ParseNonCanonical(char *name, int start, ref int end, bool notImplicitFile)
int Length
Definition Span.cs:70