Php reads qqwry. datip address database file program _ PHP Tutorial

Source: Internet
Author: User
Php reads the qqwry. datip address database file program. The article first introduces the content structure of the qqwry. dat file, and based on its characteristics, we can write the content that reads the qqwry. datip library to find what we want. First, let's take a look at the QQWry. Data File. First, let's introduce the content structure of the qqwry. dat file. then, based on its characteristics, we can write the content that reads the qqwry. dat ip library and find what we want.

First, let's take a look at the content Structure and Interpretation methods of the QQWry. Data file.

I. File structure
The file consists of three structures:
1. file header, 8 bytes;
2. Data Record area, with an indefinite length;
3. index area, which is an integer multiple of 7;

II. file header
The eight bytes in the file header are divided into two parts, each of which is 4 bytes, respectively specifying the start address and end address of the index area. Therefore, you can calculate the total number of records by dividing the difference between the two addresses by 7 and adding 1.

II. Record area
The data in the record area needs to obtain the starting position of each data through the data in the index area. The data in this area records the ending address and region string of the IP address; all region strings end with 0x00.

III. index area
To search the region corresponding to the IP address, the key is to find the index content corresponding to the IP address start address. An IP index contains seven bytes. the first four bytes are the IP address start value, and the last three bytes are the corresponding IP address data recorded in the file's offset address. in the IP address data record, the first four bytes are the IP end address, followed by two data modes: 0x01 and 0x02.

In the 0 × 01 mode, that is, if the 5th bytes of the IP data are 0 × 01, the next 3 bytes are the offset address of the country data; country and region data includes the country and region strings. That is
---------------------
4-byte | 3-byte redirection 0x NN-> File offset address of country/region data
---------------------

In the 0 × 02 mode, that is, if the 5th bytes of the IP data are 0 × 02, the third byte is the country data offset address, and the region data is the subsequent string, 0 × 00 as. That is
--------------------------
4-byte | 3-byte redirection 0x NN-> country data file offset address | region string | 0 × 00
--------------------------

For country/region data obtained in the 0 × 01 mode, it may have another redirection structure, that is
-------------
Country string | 0 × 00 | region string | 0 × 00
-------------
Or
-------------------------
Country string | 0 × 00 | 0 × 02 | 3-byte 0x NN-> region string file offset address
-------------------------

In the previous case, it is relatively simple to directly read the data of two strings. in the latter case, you need to redirect the data to the offset address of the region string again, then, the value 0 × 00 is read to the end of the string.

This method of address ING is mainly used to prevent record of string values. In the entire IP address library file, there are too many identical string records. Using a 3-byte ing address saves too much space than record string values.

PHP code reading operation QQWry. dat file:

The code is as follows:

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 data obtained by the index of each IP data file (7 bytes ).
Fseek ($ f, $ I * 7 + $ index_begin );
$ Ip4 = fread ($ f, 4); // IP start address
If (strlen ($ ip4) <4) exit ('data file error ');

$ Ip3 = fread ($ f, 3); // IP record offset address
If (strlen ($ ip3) <3) exit ('data file error ');

$ Dataseek = implode ('', unpack ('L', $ ip3. chr (0 )));
If ($ dataseek <0) $ index_ip_record + = pow (2, 32 );

// Point to the record area $ dataseek to locate the record
Fseek ($ f, $ dataseek );
$ Ipdata = fread ($ f, 4); // IP end address
If (strlen ($ ipdata) <4) exit ('data file error ');

$ Area = '';
$ Country = '';

// Read a tag
$ Flag = fread ($ f, 1 );
If ($ flag = chr (1) {// country name offset flag mode 0x01
$ Area1seek = fread ($ f, 3 );
If (strlen ($ area1seek) <3) exit ('data file error ');
$ Area1seek = implode ('', unpack ('L', $ area1seek. chr (0 )));
Fseek ($ f, $ area1seek );
$ Flag = fread ($ f, 1); // it may be a flag
}
If ($ flag = chr (2) {// country region redirection
$ Area1seek = fread ($ f, 3 );
If (strlen ($ area1seek) <3) exit ('data file error ');
$ Area1seek = implode ('', unpack ('L', $ area1seek. chr (0 )));
$ Flag = fread ($ f, 1 );
If ($ flag = chr (2 )){
$ Area2seek = fread ($ f, 3 );
$ Area2seek = implode ('', unpack ('L', $ area2seek. chr (0 )));
Fseek ($ f, $ area2seek );
} Else {
Fseek ($ f,-1, SEEK_CUR );
}
While ($ c = fread ($ f, 1 ))! = Chr (0) $ area. = $ c;
Fseek ($ f, $ area1seek );
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 )){
$ Area2seek = fread ($ f, 3 );
$ Area2seek = implode ('', unpack ('L', $ area2seek. chr (0 )));
Fseek ($ f, $ area2seek );
} Else {
Fseek ($ f,-1, SEEK_CUR );
}
While ($ c = fread ($ f, 1 ))! = Chr (0) $ area. = $ c;
}
$ Adata = trim ($ country). trim ($ area); // $ country is a country string and $ area is a region string.
}
Fclose ($ f );

What we see most about this function is file operation related functions such as fopen, fseek, and fread. if you need this function, you can check it out.


Find the desired content for the ip library. First look at the QQWry. Data file...

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.