search
HomeBackend DevelopmentPHP TutorialPHP read qqwry.dat ip address database file program_PHP tutorial

PHP read qqwry.dat ip address database file program_PHP tutorial

Jul 20, 2016 am 11:11 AM
phpintroduceaboutcontentaddressdatabasedocumentarticleofprogramread

The article first introduces the file content structure of qqwry.dat. Then according to its characteristics, we can write to read the content of the qqwry.dat ip library to find the content we want.

First, let’s take a look at the content structure of the QQWry.Data file and how to interpret it.

1. File structure
Files are mainly divided into three structures
1. File header, 8 bytes;
2. Data recording area, variable length;
3. Index Area, the length is an integer multiple of 7;

2. File header
The 8 bytes of the file header are divided into two parts, each part is 4 bytes, respectively specifying the starting address and the index area end address. Therefore, the total number of records can be calculated by dividing the difference between the two addresses by 7 and then adding 1.

2. Recording area
The data in the recording area needs to obtain the starting position of each data through the data in the index area; the data in this area records the end address of the IP address and the region string; all regional characters Strings end with 0×00.

3. Index area
To retrieve the area corresponding to the IP, the key is to find the index content corresponding to the IP starting address. An IP index data contains 7 bytes, the first 4 bytes are the starting value of the IP address, and the last 3 bytes are the offset address of the corresponding IP data record in the file; in the IP data record, the first 4 bytes The stanza is the IP end address; the data that follows has two patterns: 0×01 pattern and 0×02 pattern.

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

0×02 mode, that is, the 5th byte of IP data is 0× 02, then the next 3 bytes are the offset address of the national data, and the regional data is the following string, ending with 0×00. i.e.
——————————————————————————–
4 bytes | 3 bytes Redirect 0x NN NN NN -> Country File offset address of data | Region string | 0×00
————————————————————————————–

for In the country and region data obtained by the 0×01 pattern, it may also contain a redirection structure, that is,
————————————–
Country string | 0×00 | Region string | 0×00
————————————–
or
—————————————————— ————-
Country string | 0×00 | 0×02 | 3 bytes 0x NN NN NN -> File offset address of region string
—————————— ————————————————-

For the former case, it is relatively simple, just read the two string data directly; for the latter case, you need Redirect again to the offset address of the region string, and then read to 0×00 as the end of the string.

For this method of mapping actual string values ​​by address, the main function is to avoid repeated recording of string values. There are too many identical string records in the entire IP address library file. Using a 3-byte mapped address saves much space than repeatedly recording string values.

PHP code reading 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 if($index_end

$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

//The file pointer points to each Get the index data (7 bytes) of the IP data file on
fseek($f, $i * 7 + $index_begin);
$ip4 = fread($f, 4); //IP starts Starting address
if(strlen($ip4)

$ip3 = fread($f, 3); //IP record offset address
if(strlen($ip3)

$dataseek = implode('', unpack('L', $ip3 . chr(0)) );
if($dataseek

//Point to the record area $dataseek location to find the record
fseek($f, $dataseek );
$ipdata = fread($f, 4); //IP end address
if(strlen($ipdata)

$area = '';
$country = '';

//Read a flag bit
$flag = fread($f, 1);
if($flag == chr(1)){ //Country name offset mark bit pattern 0x01
$area1seek = fread($f, 3);
if(strlen($area1seek) $area1seek = implode('', unpack('L', $area1seek . chr(0)));
fseek($f, $area1seek);
$flag = fread ($f, 1); //It may be another flag bit
}
if($flag == chr(2)){ //Country and region redirection
$area1seek = fread($f, 3);
if(strlen($area1seek) $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 area 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 the country string, $area is the region string
}
fclose($f);

This function we What I see most are the file operation related functions such as fopen, fseek, and fread. Friends in need can take a look.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/444691.htmlTechArticleThe article first introduces the file content structure of qqwry.dat and then based on its characteristics we can write to read qqwry The contents of the .dat ip library find what we want. First look at the QQWry.Data file...
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

How does PHP handle object cloning (clone keyword) and the __clone magic method?How does PHP handle object cloning (clone keyword) and the __clone magic method?Apr 17, 2025 am 12:24 AM

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP vs. Python: Use Cases and ApplicationsPHP vs. Python: Use Cases and ApplicationsApr 17, 2025 am 12:23 AM

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor