@@ -32,7 +32,7 @@ class Database {
3232 *
3333 * @var string
3434 */
35- const VERSION = '7.2.4 ' ;
35+ const VERSION = '8.0.2 ' ;
3636
3737 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
3838 // Error field constants ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -609,7 +609,14 @@ class Database {
609609 * @var array
610610 */
611611 private $ ipBase = [];
612-
612+
613+
614+ //hjlim
615+ private $ indexBaseAddr = [];
616+ private $ year ;
617+ private $ month ;
618+ private $ day ;
619+
613620 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
614621 // Default fields //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
615622 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -735,15 +742,17 @@ public function __construct($file = null, $mode = self::FILE_IO, $defaultFields
735742 $ this ->offset [4 ] = -4 ;
736743 $ this ->offset [6 ] = 8 ;
737744 //
738- $ year = 2000 + $ this ->readByte (3 );
739- $ month = $ this ->readByte (4 );
740- $ day = $ this ->readByte (5 );
741- $ this ->date = date ('Y-m-d ' , strtotime ("{$ year }- {$ month }- {$ day }" ));
745+ $ this -> year = 2000 + $ this ->readByte (3 );
746+ $ this -> month = $ this ->readByte (4 );
747+ $ this -> day = $ this ->readByte (5 );
748+ $ this ->date = date ('Y-m-d ' , strtotime ("{$ this -> year }- {$ this -> month }- {$ this -> day }" ));
742749 //
743750 $ this ->ipCount [4 ] = $ this ->readWord (6 );
744- $ this ->ipBase [4 ] = $ this ->readByte (10 );
751+ $ this ->ipBase [4 ] = $ this ->readWord (10 ); //hjlim readword
745752 $ this ->ipCount [6 ] = $ this ->readWord (14 );
746753 $ this ->ipBase [6 ] = $ this ->readWord (18 );
754+ $ this ->indexBaseAddr [4 ] = $ this ->readWord (22 ); //hjlim
755+ $ this ->indexBaseAddr [6 ] = $ this ->readWord (26 ); //hjlim
747756 }
748757
749758 /**
@@ -1473,18 +1482,43 @@ private function binSearch($version, $ipNumber) {
14731482 $ high = $ this ->ipCount [$ version ];
14741483 $ low = 0 ;
14751484
1485+ //hjlim
1486+ $ indexBaseStart = $ this ->indexBaseAddr [$ version ];
1487+ if ($ indexBaseStart > 0 ){
1488+ $ indexPos = 0 ;
1489+ switch ($ version ){
1490+ case 4 :
1491+ $ ipNum1_2 = intval ($ ipNumber / 65536 );
1492+ $ indexPos = $ indexBaseStart + ($ ipNum1_2 << 3 );
1493+
1494+ break ;
1495+
1496+ case 6 :
1497+ $ ipNum1 = intval (bcdiv ($ ipNumber , bcpow ('2 ' , '112 ' )));
1498+ $ indexPos = $ indexBaseStart + ($ ipNum1 << 3 );
1499+
1500+ break ;
1501+
1502+ default :
1503+ return false ;
1504+ }
1505+
1506+ $ low = $ this ->readWord ($ indexPos );
1507+ $ high = $ this ->readWord ($ indexPos + 4 );
1508+ }
1509+
14761510 // as long as we can narrow down the search...
1477- while ($ low <= $ high ) {
1478- $ mid = (int ) ($ low + (($ high - $ low ) / 2 ));
1479-
1511+ while ($ low <= $ high ) {
1512+ $ mid = (int ) ($ low + (($ high - $ low ) >> 1 ));
1513+
14801514 // Read IP ranges to get boundaries
14811515 $ ip_from = $ this ->readIp ($ version , $ base + $ width * $ mid );
14821516 $ ip_to = $ this ->readIp ($ version , $ base + $ width * ($ mid + 1 ));
1483-
1517+
14841518 // determine whether to return, repeat on the lower half, or repeat on the upper half
14851519 switch (self ::ipBetween ($ version , $ ipNumber , $ ip_from , $ ip_to )) {
14861520 case 0 :
1487- return $ base + $ offset + $ mid * $ width ;
1521+ return $ base + $ offset + $ mid * $ width ;
14881522 case -1 :
14891523 $ high = $ mid - 1 ;
14901524 break ;
@@ -1544,6 +1578,20 @@ public function getFields($asNames = false) {
15441578 }
15451579 }
15461580
1581+ /**
1582+ * Return the version of module
1583+ */
1584+ public function getModuleVersion () {
1585+ return self ::VERSION ;
1586+ }
1587+
1588+ /**
1589+ * Return the version of module
1590+ */
1591+ public function getDatabaseVersion () {
1592+ return $ this ->year . '. ' . $ this ->month . '. ' . $ this ->day ;
1593+ }
1594+
15471595 /**
15481596 * This function will look the given IP address up in the database and return the result(s) asked for
15491597 *
@@ -1853,4 +1901,4 @@ public function lookup($ip, $fields = null, $asNamed = true) {
18531901 }
18541902 }
18551903
1856- }
1904+ }
0 commit comments