fix ip2long/getLong function return unsigned int ip value on 32-bit operating system

This commit is contained in:
dongyado 2016-07-03 00:35:48 +08:00
parent 0dc779766b
commit 81c495058c
1 changed files with 31 additions and 4 deletions

View File

@ -69,7 +69,7 @@ class Ip2Region
$this->totalBlocks = ($this->lastIndexPtr-$this->firstIndexPtr)/INDEX_BLOCK_LENGTH + 1; $this->totalBlocks = ($this->lastIndexPtr-$this->firstIndexPtr)/INDEX_BLOCK_LENGTH + 1;
} }
if ( is_string($ip) ) $ip = ip2long($ip); if ( is_string($ip) ) $ip = self::safeIp2long($ip);
//binary search to define the data //binary search to define the data
$l = 0; $l = 0;
@ -114,7 +114,7 @@ class Ip2Region
public function binarySearch( $ip ) public function binarySearch( $ip )
{ {
//check and conver the ip address //check and conver the ip address
if ( is_string($ip) ) $ip = ip2long($ip); if ( is_string($ip) ) $ip = self::safeIp2long($ip);
if ( $this->totalBlocks == 0 ) { if ( $this->totalBlocks == 0 ) {
//check and open the original db file //check and open the original db file
if ( $this->dbFileHandler == NULL ) { if ( $this->dbFileHandler == NULL ) {
@ -182,7 +182,7 @@ class Ip2Region
*/ */
public function btreeSearch( $ip ) public function btreeSearch( $ip )
{ {
if ( is_string($ip) ) $ip = ip2long($ip); if ( is_string($ip) ) $ip = self::safeIp2long($ip);
//check and load the header //check and load the header
if ( $this->HeaderSip == NULL ) { if ( $this->HeaderSip == NULL ) {
@ -301,6 +301,26 @@ class Ip2Region
); );
} }
/**
* safe self::safeIp2long function
*
* @param ip
* */
public static function safeIp2long($ip)
{
$ip = ip2long($ip);
// convert signed int to unsigned int if on 32 bit operating system
if ($ip < 0 && PHP_INT_SIZE == 4) {
$ip = sprintf("%u", $ip);
}
return $ip;
}
/** /**
* read a long from a byte buffer * read a long from a byte buffer
* *
@ -309,12 +329,19 @@ class Ip2Region
*/ */
public static function getLong( $b, $offset ) public static function getLong( $b, $offset )
{ {
return ( $val = (
(ord($b[$offset++])) | (ord($b[$offset++])) |
(ord($b[$offset++]) << 8) | (ord($b[$offset++]) << 8) |
(ord($b[$offset++]) << 16) | (ord($b[$offset++]) << 16) |
(ord($b[$offset ]) << 24) (ord($b[$offset ]) << 24)
); );
// convert signed int to unsigned int if on 32 bit operating system
if ($val < 0 && PHP_INT_SIZE == 4) {
$val = sprintf("%u", $val);
}
return $val;
} }
/** /**