From e6737466ee655b266e84387c2cdd614afe7a9ff5 Mon Sep 17 00:00:00 2001 From: Twist Fate Date: Fri, 1 Jul 2016 17:46:01 +0800 Subject: [PATCH 1/2] Update ip2Region.py --- binding/python/ip2Region.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/binding/python/ip2Region.py b/binding/python/ip2Region.py index 7a3372a..351fb9b 100644 --- a/binding/python/ip2Region.py +++ b/binding/python/ip2Region.py @@ -2,7 +2,7 @@ """ " ip2region python seacher client module " -" Autho: koma +" Author: koma " Date : 2015-11-06 """ import struct, io, socket, sys From 81c495058c4bbb0ed5f6dcf7029e2cd3986d84ab Mon Sep 17 00:00:00 2001 From: dongyado Date: Sun, 3 Jul 2016 00:35:48 +0800 Subject: [PATCH 2/2] fix ip2long/getLong function return unsigned int ip value on 32-bit operating system --- binding/php/Ip2Region.class.php | 35 +++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/binding/php/Ip2Region.class.php b/binding/php/Ip2Region.class.php index 14c12d0..3c26879 100644 --- a/binding/php/Ip2Region.class.php +++ b/binding/php/Ip2Region.class.php @@ -69,7 +69,7 @@ class Ip2Region $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 $l = 0; @@ -114,7 +114,7 @@ class Ip2Region public function binarySearch( $ip ) { //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 ) { //check and open the original db file if ( $this->dbFileHandler == NULL ) { @@ -182,7 +182,7 @@ class Ip2Region */ public function btreeSearch( $ip ) { - if ( is_string($ip) ) $ip = ip2long($ip); + if ( is_string($ip) ) $ip = self::safeIp2long($ip); //check and load the header 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 * @@ -309,12 +329,19 @@ class Ip2Region */ public static function getLong( $b, $offset ) { - return ( + $val = ( (ord($b[$offset++])) | (ord($b[$offset++]) << 8) | (ord($b[$offset++]) << 16) | (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; } /**