function _geoip_seek_country_v6 in Smart IP 6.2
Same name and namespace in other branches
- 7.2 includes/geoip.inc \_geoip_seek_country_v6()
3 calls to _geoip_seek_country_v6()
- geoip_country_id_by_addr_v6 in includes/
geoip.inc - _get_org_v6 in includes/
geoip.inc - _get_record_v6 in includes/
geoipcity.inc
File
- includes/
geoip.inc, line 581
Code
function _geoip_seek_country_v6($gi, $ipnum) {
# arrays from unpack start with offset 1
# yet another php mystery. array_merge work around
# this broken behaviour
$v6vec = array_merge(unpack("C16", $ipnum));
$offset = 0;
for ($depth = 127; $depth >= 0; --$depth) {
if ($gi->flags & GEOIP_MEMORY_CACHE) {
// workaround php's broken substr, strpos, etc handling with
// mbstring.func_overload and mbstring.internal_encoding
$enc = mb_internal_encoding();
mb_internal_encoding('ISO-8859-1');
$buf = substr($gi->memory_buffer, 2 * $gi->record_length * $offset, 2 * $gi->record_length);
mb_internal_encoding($enc);
}
elseif ($gi->flags & GEOIP_SHARED_MEMORY) {
$buf = @shmop_read($gi->shmid, 2 * $gi->record_length * $offset, 2 * $gi->record_length);
}
else {
fseek($gi->filehandle, 2 * $gi->record_length * $offset, SEEK_SET) == 0 or die("fseek failed");
$buf = fread($gi->filehandle, 2 * $gi->record_length);
}
$x = array(
0,
0,
);
for ($i = 0; $i < 2; ++$i) {
for ($j = 0; $j < $gi->record_length; ++$j) {
$x[$i] += ord($buf[$gi->record_length * $i + $j]) << $j * 8;
}
}
$bnum = 127 - $depth;
$idx = $bnum >> 3;
$b_mask = 1 << ($bnum & 7 ^ 7);
if (($v6vec[$idx] & $b_mask) > 0) {
if ($x[1] >= $gi->databaseSegments) {
return $x[1];
}
$offset = $x[1];
}
else {
if ($x[0] >= $gi->databaseSegments) {
return $x[0];
}
$offset = $x[0];
}
}
trigger_error("error traversing database - perhaps it is corrupt?", E_USER_ERROR);
return false;
}