Skip to content
Permalink
Browse files

Get rid of unnecessary pack/unpack

  • Loading branch information...
oschwald committed Oct 1, 2019
1 parent 356f720 commit 9cf4f4009c856b8696ae3e54da44b9d3773cc302
Showing with 5 additions and 5 deletions.
  1. +5 −5 src/Util.php
@@ -16,21 +16,21 @@ class Util
*/
public static function cidr($ipAddress, $prefixLen)
{
$ipBytes = array_merge(unpack('C*', inet_pton($ipAddress)));
$networkBytes = array_fill(0, \count($ipBytes), 0);
$ipBytes = inet_pton($ipAddress);
$networkBytes = str_repeat("\0", \strlen($ipBytes));
$curPrefix = $prefixLen;
for ($i = 0; $i < \count($ipBytes) && $curPrefix > 0; $i++) {
for ($i = 0; $i < \strlen($ipBytes) && $curPrefix > 0; $i++) {
$b = $ipBytes[$i];
if ($curPrefix < 8) {
$shiftN = 8 - $curPrefix;
$b = (0xFF & ($b >> $shiftN) << $shiftN);
$b = \chr(0xFF & (\ord($b) >> $shiftN) << $shiftN);
}
$networkBytes[$i] = $b;
$curPrefix -= 8;
}
$network = inet_ntop(pack('C*', ...$networkBytes));
$network = inet_ntop($networkBytes);
return "$network/$prefixLen";
}

0 comments on commit 9cf4f40

Please sign in to comment.
You can’t perform that action at this time.