public function get($ip) { $ipFloat = (double) sprintf("%u", ip2long($ip)); if ($ipFloat < $this->hashMin || $ipFloat >= $this->hashMax) { return NULL; } $pos = $this->hashFunc($ipFloat); fseek($this->fp, -12 - $this->hashListCount * 4 + $pos * 4, SEEK_END); $ptr = binaryPacker::unpackInt(fread($this->fp, 4)); fseek($this->fp, $ptr); $listCount = binaryPacker::unpackInt(fread($this->fp, 4)); $found = false; if ($listCount == 0) { return NULL; } $buf = fread($this->fp, $listCount * 12); for ($i = 0; $i < $listCount; $i++) { $curSegmentMin = binaryPacker::unpackInt(substr($buf, $i * 12, 4)); if ($ipFloat < $curSegmentMin) { return NULL; } $curSegmentMax = binaryPacker::unpackInt(substr($buf, 4 + $i * 12, 4)); if ($curSegmentMin <= $ipFloat && $ipFloat <= $curSegmentMax) { $codePtrs = binaryPacker::unpackInt(substr($buf, 8 + $i * 12, 4)); $found = true; break; } } if (!$found) { return NULL; } fseek($this->fp, $codePtrs); $varCharSize = binaryPacker::unpackInt(fread($this->fp, 4)); return fread($this->fp, $varCharSize); }
private function readInt() { $buf = $this->dbStream->read(4); return binaryPacker::unpackInt($buf); }
private function writeInt($intVal) { $this->dbStream->write(binaryPacker::packInt($intVal)); }
public function testPackLargeInt() { $buf = binaryPacker::packInt(3000000000); $this->assertEquals(3000000000, binaryPacker::unpackInt($buf)); }