IP::fromHexString handle ascii (#15504)

* IP::fromHexString handle ascii
Some bad MIBs convert the hex to ascii on display.
Attempt to decode that if the situation is detected.

fixes #15501

* add ascii tests
This commit is contained in:
Tony Murray
2023-10-26 10:03:03 -05:00
committed by GitHub
parent 91b829edd3
commit 7e0b41a05a
4 changed files with 20 additions and 8 deletions

View File

@@ -54,6 +54,12 @@ abstract class IP
$hex = str_replace([':', '.'], '', $hex);
// check if hex was incorrectly converted to ascii
$len = strlen($hex);
if (($len == 4 || $len == 16) && preg_match('/[^0-9a-fA-F]/', $hex)) {
$hex = StringHelpers::asciiToHex($hex);
}
try {
if (strlen($hex) == 8) {
return new IPv4(long2ip(hexdec($hex)));

View File

@@ -162,4 +162,15 @@ class StringHelpers
{
return $var === null || is_scalar($var) || (is_object($var) && method_exists($var, '__toString'));
}
public static function asciiToHex(string $ascii, string $seperator = ''): string
{
$hex = [];
$len = strlen($ascii);
for ($i = 0; $i < $len; $i++) {
$hex[] = str_pad(strtoupper(dechex(ord($ascii[$i]))), 2, '0', STR_PAD_LEFT);
}
return implode($seperator, $hex);
}
}