Merge branch 'master' into nodejsclient

This commit is contained in:
dongyado 2016-07-03 21:35:13 +08:00
commit b187d1646a
2 changed files with 32 additions and 5 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;
} }
/** /**

View File

@ -2,7 +2,7 @@
""" """
" ip2region python seacher client module " ip2region python seacher client module
" "
" Autho: koma<komazhang@foxmail.com> " Author: koma<komazhang@foxmail.com>
" Date : 2015-11-06 " Date : 2015-11-06
""" """
import struct, io, socket, sys import struct, io, socket, sys