Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
PhysicalAddress.cs
Go to the documentation of this file.
3
5
6public class PhysicalAddress
7{
8 private readonly byte[] _address;
9
10 private int _hash;
11
12 public static readonly PhysicalAddress None = new PhysicalAddress(Array.Empty<byte>());
13
14 public PhysicalAddress(byte[] address)
15 {
16 _address = address;
17 }
18
19 public override int GetHashCode()
20 {
21 if (_hash == 0)
22 {
23 int num = 0;
24 int num2 = _address.Length & -4;
25 int i;
26 for (i = 0; i < num2; i += 4)
27 {
29 }
30 if (((uint)_address.Length & 3u) != 0)
31 {
32 int num3 = 0;
33 int num4 = 0;
34 for (; i < _address.Length; i++)
35 {
36 num3 |= _address[i] << num4;
37 num4 += 8;
38 }
39 num ^= num3;
40 }
41 if (num == 0)
42 {
43 num = 1;
44 }
45 _hash = num;
46 }
47 return _hash;
48 }
49
50 public override bool Equals([NotNullWhen(true)] object? comparand)
51 {
52 if (!(comparand is PhysicalAddress physicalAddress))
53 {
54 return false;
55 }
56 if (_address.Length != physicalAddress._address.Length)
57 {
58 return false;
59 }
60 if (GetHashCode() != physicalAddress.GetHashCode())
61 {
62 return false;
63 }
64 for (int i = 0; i < physicalAddress._address.Length; i++)
65 {
66 if (_address[i] != physicalAddress._address[i])
67 {
68 return false;
69 }
70 }
71 return true;
72 }
73
74 public override string ToString()
75 {
76 return Convert.ToHexString(_address.AsSpan());
77 }
78
79 public byte[] GetAddressBytes()
80 {
81 return (byte[])_address.Clone();
82 }
83
84 public static PhysicalAddress Parse(string? address)
85 {
86 if (address == null)
87 {
88 return None;
89 }
90 return Parse(address.AsSpan());
91 }
92
94 {
95 if (!TryParse(address, out PhysicalAddress value))
96 {
97 throw new FormatException(System.SR.Format(System.SR.net_bad_mac_address, new string(address)));
98 }
99 return value;
100 }
101
102 public static bool TryParse(string? address, [NotNullWhen(true)] out PhysicalAddress? value)
103 {
104 if (address == null)
105 {
106 value = None;
107 return true;
108 }
109 return TryParse(address.AsSpan(), out value);
110 }
111
112 public static bool TryParse(ReadOnlySpan<char> address, [NotNullWhen(true)] out PhysicalAddress? value)
113 {
114 char? c = null;
115 value = null;
116 byte[] array;
117 int value2;
118 if (address.Contains('-'))
119 {
120 if ((address.Length + 1) % 3 != 0)
121 {
122 return false;
123 }
124 c = '-';
125 array = new byte[(address.Length + 1) / 3];
126 value2 = 2;
127 }
128 else if (address.Contains(':'))
129 {
130 c = ':';
131 if (!TryGetValidSegmentLength(address, ':', out value2))
132 {
133 return false;
134 }
135 if (value2 != 2 && value2 != 4)
136 {
137 return false;
138 }
139 array = new byte[6];
140 }
141 else if (address.Contains('.'))
142 {
143 c = '.';
144 if (!TryGetValidSegmentLength(address, '.', out value2))
145 {
146 return false;
147 }
148 if (value2 != 4)
149 {
150 return false;
151 }
152 array = new byte[6];
153 }
154 else
155 {
156 if (address.Length % 2 > 0)
157 {
158 return false;
159 }
160 value2 = address.Length;
161 array = new byte[address.Length / 2];
162 }
163 int num = 0;
164 int num2 = 0;
165 for (int i = 0; i < address.Length; i++)
166 {
167 int num3 = address[i];
168 int num4;
169 if ((num4 = System.HexConverter.FromChar(num3)) == 255)
170 {
171 if (c == num3 && num == value2)
172 {
173 num = 0;
174 continue;
175 }
176 return false;
177 }
178 num3 = num4;
179 if (num >= value2)
180 {
181 return false;
182 }
183 if (num % 2 == 0)
184 {
185 array[num2] = (byte)(num3 << 4);
186 }
187 else
188 {
189 array[num2++] |= (byte)num3;
190 }
191 num++;
192 }
193 if (num < value2)
194 {
195 return false;
196 }
198 return true;
199 }
200
201 private static bool TryGetValidSegmentLength(ReadOnlySpan<char> address, char delimiter, out int value)
202 {
203 value = -1;
204 int num = 1;
205 int num2 = 0;
206 for (int i = 0; i < address.Length; i++)
207 {
208 if (address[i] == delimiter)
209 {
210 if (num2 == 0)
211 {
212 num2 = i;
213 }
214 else if ((i - (num - 1)) % num2 != 0)
215 {
216 return false;
217 }
218 num++;
219 }
220 }
221 if (num * num2 != 12)
222 {
223 return false;
224 }
225 value = num2;
226 return true;
227 }
228}
static int ReadInt32LittleEndian(ReadOnlySpan< byte > source)
static string ToHexString(byte[] inArray)
Definition Convert.cs:3159
static int FromChar(int c)
static bool TryGetValidSegmentLength(ReadOnlySpan< char > address, char delimiter, out int value)
override bool Equals([NotNullWhen(true)] object? comparand)
static PhysicalAddress Parse(ReadOnlySpan< char > address)
static bool TryParse(string? address, [NotNullWhen(true)] out PhysicalAddress? value)
static bool TryParse(ReadOnlySpan< char > address, [NotNullWhen(true)] out PhysicalAddress? value)
static PhysicalAddress Parse(string? address)
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string net_bad_mac_address
Definition SR.cs:18
Definition SR.cs:7