You are here

private function IP2Location::readByte in Smart IP 6

Same name and namespace in other branches
  1. 7 includes/IP2Location.inc \IP2Location::readByte()
2 calls to IP2Location::readByte()
IP2Location::lookup in includes/IP2Location.inc
IP2Location::__construct in includes/IP2Location.inc

File

includes/IP2Location.inc, line 175

Class

IP2Location

Code

private function readByte($pos, $mode = 'string', $autoSize = false) {
  switch ($this->mode) {
    case self::SHARED_MEMORY:
      if ($mode == 'string') {
        $data = shmop_read($this->shmId, $pos, $autoSize ? shmop_size($this->shmId) - $pos : 100);
      }
      else {
        $data = shmop_read($this->shmId, $pos - 1, 50);
      }
      break;
    case self::MEMORY_CACHE:
      $data = substr($this->buffer, $mode == 'string' ? $pos : $pos - 1, 100);
      break;
    default:
      if ($mode == 'string') {
        fseek($this->handle, $pos, SEEK_SET);
        $data = @fread($this->handle, 1);
      }
      else {
        fseek($this->handle, $pos - 1, SEEK_SET);
        $data = @fread($this->handle, 50);
      }
  }
  switch ($mode) {
    case '8':
      $out = $this
        ->readBinary('C', $data);
      return $out[1];
      break;
    case '32':
      $out = $this
        ->readBinary('V', $data);
      if ($out[1] < 0) {
        $out[1] += 4294967296;
      }
      return (int) $out[1];
      break;
    case '128':
      $array = preg_split('//', $data, -1, PREG_SPLIT_NO_EMPTY);
      if (count($array) != 16) {
        return 0;
      }
      $ip96_127 = $this
        ->readBinary('V', $array[0] . $array[1] . $array[2] . $array[3]);
      $ip64_95 = $this
        ->readBinary('V', $array[4] . $array[5] . $array[6] . $array[7]);
      $ip32_63 = $this
        ->readBinary('V', $array[8] . $array[9] . $array[10] . $array[11]);
      $ip1_31 = $this
        ->readBinary('V', $array[12] . $array[13] . $array[14] . $array[15]);
      if ($ip96_127[1] < 0) {
        $ip96_127[1] += 4294967296;
      }
      if ($ip64_95[1] < 0) {
        $ip64_95[1] += 4294967296;
      }
      if ($ip32_63[1] < 0) {
        $ip32_63[1] += 4294967296;
      }
      if ($ip1_31[1] < 0) {
        $ip1_31[1] += 4294967296;
      }
      return bcadd(bcadd(bcmul($ip1_31[1], bcpow(4294967296, 3)), bcmul($ip32_63[1], bcpow(4294967296, 2))), bcadd(bcmul($ip64_95[1], 4294967296), $ip96_127[1]));
      break;
    case 'float':
      $out = $this
        ->readBinary('f', $data);
      return $out[1];
      break;
    default:
      $out = $this
        ->readBinary('C', $data);
      return in_array($this->mode, array(
        self::SHARED_MEMORY,
        self::MEMORY_CACHE,
      )) ? substr($data, 1, $out[1]) : @fread($this->handle, $out[1]);
  }
}