Apply review suggestions

This commit is contained in:
Shadowghost 2023-05-25 17:10:53 +02:00
parent d8d5c86d49
commit a381cd3c76

View File

@ -93,31 +93,36 @@ namespace MediaBrowser.Common.Net
ArgumentNullException.ThrowIfNull(mask); ArgumentNullException.ThrowIfNull(mask);
byte cidrnet = 0; byte cidrnet = 0;
if (!mask.Equals(IPAddress.Any)) if (mask.Equals(IPAddress.Any))
{ {
// GetAddressBytes return cidrnet;
Span<byte> bytes = stackalloc byte[mask.AddressFamily == AddressFamily.InterNetwork ? 4 : 16]; }
mask.TryWriteBytes(bytes, out _);
var zeroed = false; // GetAddressBytes
for (var i = 0; i < bytes.Length; i++) Span<byte> bytes = stackalloc byte[mask.AddressFamily == AddressFamily.InterNetwork ? 4 : 16];
if (!mask.TryWriteBytes(bytes, out var bytesWritten))
{
Console.WriteLine("Unable to write address bytes, only {Bytes} bytes written.", bytesWritten);
}
var zeroed = false;
for (var i = 0; i < bytes.Length; i++)
{
for (int v = bytes[i]; (v & 0xFF) != 0; v <<= 1)
{ {
for (int v = bytes[i]; (v & 0xFF) != 0; v <<= 1) if (zeroed)
{ {
if (zeroed) // Invalid netmask.
{ return (byte)~cidrnet;
// Invalid netmask. }
return (byte)~cidrnet;
}
if ((v & 0x80) == 0) if ((v & 0x80) == 0)
{ {
zeroed = true; zeroed = true;
} }
else else
{ {
cidrnet++; cidrnet++;
}
} }
} }
} }
@ -273,10 +278,9 @@ namespace MediaBrowser.Common.Net
} }
var hosts = new List<string>(); var hosts = new List<string>();
var splitSpan = host.Split(':'); foreach (var splitSpan in host.Split(':'))
while (splitSpan.MoveNext())
{ {
hosts.Add(splitSpan.Current.ToString()); hosts.Add(splitSpan.ToString());
} }
if (hosts.Count <= 2) if (hosts.Count <= 2)
@ -316,7 +320,7 @@ namespace MediaBrowser.Common.Net
} }
else if (hosts.Count <= 9) // 8 octets + port else if (hosts.Count <= 9) // 8 octets + port
{ {
splitSpan = host.Split('/'); var splitSpan = host.Split('/');
if (splitSpan.MoveNext() && IPAddress.TryParse(splitSpan.Current, out var address)) if (splitSpan.MoveNext() && IPAddress.TryParse(splitSpan.Current, out var address))
{ {
addresses = new[] { address }; addresses = new[] { address };