Skip to content

Commit 9a94e98

Browse files
committed
support IP2Location v8 BIN format
1 parent 5f637f2 commit 9a94e98

2 files changed

Lines changed: 44 additions & 8 deletions

File tree

ChangeLog

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
Revision History for PHP Module for IP2Location
2+
8.0.0 Tue Jul 5 10:00:00 2016
3+
* Fixes
4+
5+
- Add code to lookup for index section which available in new BIN format
6+
27
7.2.5 Fri Jun 10 15:40:23 2016
38
* Fixes:
49

IP2Location.php

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class Database {
3232
*
3333
* @var string
3434
*/
35-
const VERSION = '7.2.5';
35+
const VERSION = '8.0.0';
3636

3737
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
3838
// Error field constants ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -609,7 +609,11 @@ class Database {
609609
* @var array
610610
*/
611611
private $ipBase = [];
612-
612+
613+
614+
//hjlim
615+
private $indexBaseAddr = [];
616+
613617
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
614618
// Default fields //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
615619
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -741,9 +745,11 @@ public function __construct($file = null, $mode = self::FILE_IO, $defaultFields
741745
$this->date = date('Y-m-d', strtotime("{$year}-{$month}-{$day}"));
742746
//
743747
$this->ipCount[4] = $this->readWord(6);
744-
$this->ipBase[4] = $this->readWord(10);
748+
$this->ipBase[4] = $this->readWord(10); //hjlim readword
745749
$this->ipCount[6] = $this->readWord(14);
746750
$this->ipBase[6] = $this->readWord(18);
751+
$this->indexBaseAddr[4] = $this->readWord(22); //hjlim
752+
$this->indexBaseAddr[6] = $this->readWord(26); //hjlim
747753
}
748754

749755
/**
@@ -1473,18 +1479,43 @@ private function binSearch($version, $ipNumber) {
14731479
$high = $this->ipCount[$version];
14741480
$low = 0;
14751481

1482+
//hjlim
1483+
$indexBaseStart = $this->indexBaseAddr[$version];
1484+
if ($indexBaseStart > 0){
1485+
$indexPos = 0;
1486+
switch($version){
1487+
case 4:
1488+
$ipNum1_2 = intval($ipNumber >> 16);
1489+
$indexPos = $indexBaseStart + ($ipNum1_2 << 3);
1490+
1491+
break;
1492+
1493+
case 6:
1494+
$ipNum1 = intval(bcdiv($ipNumber, bcpow('2', '112')));
1495+
$indexPos = $indexBaseStart + ($ipNum1 << 3);
1496+
1497+
break;
1498+
1499+
default:
1500+
return false;
1501+
}
1502+
1503+
$low = $this->readWord($indexPos);
1504+
$high = $this->readWord($indexPos + 4);
1505+
}
1506+
14761507
// as long as we can narrow down the search...
1477-
while ($low <= $high) {
1478-
$mid = (int) ($low + (($high - $low) / 2));
1479-
1508+
while ($low <= $high) {
1509+
$mid = (int) ($low + (($high - $low) >> 1));
1510+
14801511
// Read IP ranges to get boundaries
14811512
$ip_from = $this->readIp($version, $base + $width * $mid);
14821513
$ip_to = $this->readIp($version, $base + $width * ($mid + 1));
1483-
1514+
14841515
// determine whether to return, repeat on the lower half, or repeat on the upper half
14851516
switch (self::ipBetween($version, $ipNumber, $ip_from, $ip_to)) {
14861517
case 0:
1487-
return $base + $offset + $mid * $width;
1518+
return $base + $offset + $mid * $width;
14881519
case -1:
14891520
$high = $mid - 1;
14901521
break;

0 commit comments

Comments
 (0)