PHP read qqwry.dat IP address database file program _php Tutorial

Source: Internet
Author: User
Tags fread string back unpack
The article first introduces the structure of the file content of Qqwry.dat and then according to its characteristics we can write read the contents of the Qqwry.dat IP library to find what we want.

First look at the content structure of the Qqwry.data file and how to interpret it.

I. Structure of the document
The file consists of three structures
1, file header, 8 bytes;
2, data record area, indefinite length;
3, the index area, the length is 7 integer times;

Second, the file header
The 8 bytes of the header are divided into two parts, each of which is 4 bytes, specifying the starting and ending addresses of the index area, respectively. So the total number of records can be calculated by adding 1 to the difference of two addresses except 7.

Second, the record area
The data of the record area needs to get the starting position of each data through the data of the index area, the end address and the region string of the IP address are recorded in this area, and all the region strings end in 0x00.

Third, the index area
The key to retrieving the IP corresponding region is to find the index content corresponding to the IP start address. An IP index data contains 7 bytes, the first 4 bytes is the IP address start value, the last 3 bytes is the corresponding IP data record in the file offset address; In an IP data record, the first 4 bytes are the IP end address; the data that follows is in two modes: 0x01 mode and 0x02 mode.

The 0x01 mode, that is, the 5th byte in the IP data is 0x01, then the 3 bytes in the back is the offset address of the national region data, and the country region data includes the country and region of the two strings. That
—————————————————————
4 bytes | 3-byte redirection 0x nn nn nn-zone data file offset address
—————————————————————

The 0x02 mode, that is, the 5th byte of the IP data is 0x02, then the 3 bytes in the back is the offset address of the national data, and the region data is then the string back to 0x00 as of. That
—————————————————————————–
4 bytes | File offset address for 3-byte redirect 0x nn nn nn-country data | Region String | 0x00
—————————————————————————–

For the national area data obtained by the 0x01 pattern, it may also have a redirect structure, i.e.
————————————–
Country String | 0x00 | Region String | 0x00
————————————–
Or
————————————————————————-
Country String | 0x00 | 0x02 | 3-byte 0x nn nn nn-zone string file offset address
————————————————————————-

For the former case, it is simpler to read the two string data directly; for the latter case, redirect the offset address to the region string again, and then read to 0x00 to the end of the string.

The main purpose of this approach is to avoid repeating record string values in a way that addresses map actual string values. In the entire IP address library file, there are too many identical string records, with a 3-byte mapping address to save too much space than the duplicate record string value.

PHP code read operation QQWry.dat file:

The code is as follows Copy Code

function Bin2ip ($bin) {
$ip = ";
$BD = Str_split ($bin, 1);
for ($i = 4; $i > 0; $i-) {
$ip. = ".". sprintf ("%03d", implode (', unpack (' s '), $BD [$i-1]. chr (0)));
}
Return substr ($IP, 1);
}

//--------------------------------------------------
$f = fopen (' QQWry.Dat ', ' R ');
$c = Fread ($f, 4);
$d = Fread ($f, 4);

$index _begin = implode (' ', Unpack (' L ', $c));
$index _end = implode (' ', Unpack (' L ', $d));
if ($index _begin < 0) $index _begin + = POW (2, 32);
if ($index _end < 0) $index _end + = POW (2, 32);

$ip _num = ($index _end-$index _begin)/7 + 1;

echo "index begin at: $index _beginn";
echo "Index end at: $index _endn";
echo "IP data count: $ip _numn";

$output = ";

for ($i = 0; $i < $ip _num; $i + +) {

The file pointer refers to the index of each IP data file to get Index data (7 bytes) on
Fseek ($f, $i * 7 + $index _begin);
$ip 4 = fread ($f, 4); IP Start Address
if (strlen ($ip 4) < 4) exit (' Data file error ');

$ip 3 = fread ($f, 3); IP Record offset Address
if (strlen ($ip 3) < 3) exit (' Data file error ');

$dataseek = Implode (' ', Unpack (' L ', $ip 3. chr (0)));
if ($dataseek < 0) $index _ip_record + = POW (2, 32);

Find records $dataseek location to the record area
Fseek ($f, $dataseek);
$ipdata = Fread ($f, 4); IP End Address
if (strlen ($ipdata) < 4) exit (' Data file error ');

$area = ";
$country = ";

//Read a marker bit
$flag = fread ($f, 1),
if ($flag = Chr (1)) {//Country name offset mark bit mode one 0x01
$area 1seek = Fread ($f, 3);
if ( strlen ($area 1seek) < 3) exit (' Data file error ');
$area 1seek = implode (' ', Unpack (' L ', $area 1seek. chr (0)));
Fseek ($f, $area 1seek);
$flag = Fread ($f, 1);//May be the token bit
}
if ($flag = = Chr (2)) {//Country redirect
$area 1seek = Fread ($f, 3);
if (Strle N ($area 1seek) < 3) exit (' Data file error ');
$area 1seek = implode (' ', Unpack (' L ', $area 1seek. chr (0)));
$flag = Fread ($f, 1);
if ($flag = Chr (2)) {
$area 2seek = Fread ($f, 3),
$area 2seek = Implode (", Unpack (' L ', $area 2seek. chr (0))); fseek ($f, $area 2seek);
}else{
Fseek ($f, 1, seek_cur);
} The
while ($c = Fread ($f, 1))! = Chr (0)) $area. = $c;
Fseek ($f, $area 1seek); The
while ($c = Fread ($f, 1))! = Chr (0)) $country. = $c;
}else{
Fseek ($f, 1, seek_cur),
while (($c = Fread ($f, 1))! = Chr (0)) $country. = $c;

$flag = Fread ($f, 1); If the region is redirected
if ($flag = = Chr (2)) {
$area 2seek = Fread ($f, 3);
$area 2seek = implode (' ', Unpack (' L ', $area 2seek. chr (0)));
Fseek ($f, $area 2seek);
}else{
Fseek ($f,-1, seek_cur);
}
while ($c = Fread ($f, 1))! = Chr (0)) $area. = $c;
}
$adata = Trim ($country). Trim ($area); $country is the country string, $area is the region string
}
Fclose ($f);

This function we see the most is the file operation related functions such as fopen,fseek,fread these, there is a need for friends to see.


http://www.bkjia.com/PHPjc/444691.html www.bkjia.com true http://www.bkjia.com/PHPjc/444691.html techarticle The article first introduces the structure of the file content of Qqwry.dat and then according to its characteristics we can write read the contents of the Qqwry.dat IP library to find what we want. First look at the Qqwry.data file ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.