问题
Maxmind offers a binary DAT file format for downloading their GeoIP database.
http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
Does anyone know how this has been packaged? Also, is there any kind of copy protection on the data?
I'd like to offer up a set of data in a similar way.
Anyone with any knowledge of this will receive my undying gratitude :-)
回答1:
It's just a proprietary binary format, heavily optimized for IP address querying. It doesn't have any copy protection.
If you really want to reverse-engineer the format, take a look at the C# or Java API.
回答2:
I don't know if this helps but here it's a sample code in PHP for the GeoLite Country DB:
const COUNTRY_BEGIN = 16776960;
const COUNTRY_EDITION = 106;
const STANDARD_RECORD_LENGTH = 3;
public function Seek_Country($ip)
{
$result = false;
$databases = glob('./application/repository/GeoIP/GeoIP_*.dat');
if (array_key_exists(0, $databases))
{
rsort($databases);
if (!$handle = fopen($databases[0], 'rb'))
{
return $result;
}
$offset = 0;
flock($handle, LOCK_SH);
for ($depth = 31; $depth >= 0; --$depth)
{
fseek($handle, 2 * self::STANDARD_RECORD_LENGTH * $offset, SEEK_SET);
$buffer = fread($handle, 2 * self::STANDARD_RECORD_LENGTH);
$x = array(0, 0);
for ($i = 0; $i < 2; ++$i)
{
for ($j = 0; $j < self::STANDARD_RECORD_LENGTH; ++$j)
{
$x[$i] += ord($buffer[self::STANDARD_RECORD_LENGTH * $i + $j]) << ($j * 8);
}
}
if ($ip & (1 << $depth))
{
if ($x[1] >= self::COUNTRY_BEGIN)
{
$result = $x[1];
break;
}
$offset = $x[1];
}
else
{
if ($x[0] >= self::COUNTRY_BEGIN)
{
$result = $x[0];
break;
}
$offset = $x[0];
}
}
flock($handle, LOCK_UN);
fclose($handle);
}
return $result;
}
来源:https://stackoverflow.com/questions/1294903/how-does-the-binary-dat-from-maxmind-work