IP2Location.php
private function binSearch($version, $ipNumber, $cidr=false) {
    if (false === $version) {
      // unrecognized version
      return false;
    }

    // initialize fields
    $base   = $this->ipBase[$version];
    $offset = $this->offset[$version];
    $width  = $this->columnWidth[$version];
    $high   = $this->ipCount[$version];
    $low    = 0;

	//hjlim
	$indexBaseStart = $this->indexBaseAddr[$version];
	if ($indexBaseStart > 0){
		$indexPos = 0;
		switch($version){
			case 4:
				$ipNum1_2 = intval($ipNumber / 65536);
				$indexPos = $indexBaseStart + ($ipNum1_2 << 3);

				break;

			case 6:
				$ipNum1 = intval(bcdiv($ipNumber, bcpow('2', '112')));
				$indexPos = $indexBaseStart + ($ipNum1 << 3);

				break;

			default:
				return false;
		}

		$low = $this->readWord($indexPos);
		$high = $this->readWord($indexPos + 4);
	}

    // as long as we can narrow down the search...
	while ($low <= $high) {
      $mid     = (int) ($low + (($high - $low) >> 1));

	  // Read IP ranges to get boundaries
      $ip_from = $this->readIp($version, $base + $width * $mid);
      $ip_to   = $this->readIp($version, $base + $width * ($mid + 1));

      // determine whether to return, repeat on the lower half, or repeat on the upper half
      switch (self::ipBetween($version, $ipNumber, $ip_from, $ip_to)) {
        case 0:
          return ($cidr) ? array($ip_from, $ip_to) : $base + $offset + $mid * $width;
        case -1:
          $high = $mid - 1;
          break;
        case 1:
          $low  = $mid + 1;
          break;
      }
    }

    // nothing found
    return false;
  }
private function binSearch($version, $ipNumber, $cidr=false) {
    if (false === $version) {
      // unrecognized version
      return false;
    }

    // initialize fields
    $base   = $this->ipBase[$version];
    $offset = $this->offset[$version];
    $width  = $this->columnWidth[$version];
    $high   = $this->ipCount[$version];
    $low    = 0;

	//hjlim
	$indexBaseStart = $this->indexBaseAddr[$version];
	if ($indexBaseStart > 0){
		$indexPos = 0;
		switch($version){
			case 4:
				$ipNum1_2 = intval($ipNumber / 65536);
				$indexPos = $indexBaseStart + ($ipNum1_2 << 3);

				break;

			case 6:
				$ipNum1 = intval(bcdiv($ipNumber, bcpow('2', '112')));
				$indexPos = $indexBaseStart + ($ipNum1 << 3);

				break;

			default:
				return false;
		}

		$low = $this->readWord($indexPos);
		$high = $this->readWord($indexPos + 4);
	}

    // as long as we can narrow down the search...
	while ($low <= $high) {
      $mid     = (int) ($low + (($high - $low) >> 1));

	  // Read IP ranges to get boundaries
      $ip_from = $this->readIp($version, $base + $width * $mid);
      $ip_to   = $this->readIp($version, $base + $width * ($mid + 1));

      // determine whether to return, repeat on the lower half, or repeat on the upper half
      switch (self::ipBetween($version, $ipNumber, $ip_from, $ip_to)) {
        case 0:
          return ($cidr) ? array($ip_from, $ip_to) : $base + $offset + $mid * $width;
        case -1:
          $high = $mid - 1;
          break;
        case 1:
          $low  = $mid + 1;
          break;
      }
    }

    // nothing found
    return false;
  }
private function binSearch ( $ version , $ ipNumber , $ cidr = false ) { if ( false === $ version ) { // unrecognized version return false ; } // initialize fields $ base = $ this -> ipBase [ $ version ] ; $ offset = $ this -> offset [ $ version ] ; $ width = $ this -> columnWidth [ $ version ] ; $ high = $ this -> ipCount [ $ version ] ; $ low = 0 ; //hjlim $ indexBaseStart = $ this -> indexBaseAddr [ $ version ] ; if ( $ indexBaseStart > 0 ) { $ indexPos = 0 ; switch ( $ version ) { case 4 : $ ipNum1_2 = intval ( $ ipNumber / 65536 ) ; $ indexPos = $ indexBaseStart + ( $ ipNum1_2 << 3 ) ; break ; case 6 : $ ipNum1 = intval ( bcdiv ( $ ipNumber , bcpow ( '2' , '112' ) ) ) ; $ indexPos = $ indexBaseStart + ( $ ipNum1 << 3 ) ; break ; default : return false ; } $ low = $ this -> readWord ( $ indexPos ) ; $ high = $ this -> readWord ( $ indexPos + 4 ) ; } // as long as we can narrow down the search... while ( $ low <= $ high ) { $ mid = ( int ) ( $ low + ( ( $ high - $ low ) >> 1 ) ) ; // Read IP ranges to get boundaries $ ip_from = $ this -> readIp ( $ version , $ base + $ width * $ mid ) ; $ ip_to = $ this -> readIp ( $ version , $ base + $ width * ( $ mid + 1 ) ) ; // determine whether to return, repeat on the lower half, or repeat on the upper half switch ( self :: ipBetween ( $ version , $ ipNumber , $ ip_from , $ ip_to ) ) { case 0 : return ( $ cidr ) ? array ( $ ip_from , $ ip_to ) : $ base + $ offset + $ mid * $ width ; case - 1 : $ high = $ mid - 1 ; break ; case 1 : $ low = $ mid + 1 ; break ; } } // nothing found return false ; }
Perform a binary search on the given IP number and return a pointer to its record

@access private
@param int $version  IP version to use for searching
@param int $ipNumber  IP number to look for
@return int|boolean
Perform a binary search on the given IP number and return a pointer to its record