C # Read the QQ pure IP Database QQWry.Dat Code _c# Tutorial

Source: Internet
Author: User

QQ IP Database of pure edition

Copy Code code as follows:

Using System;
Using System.Collections.Generic;
Using System.Text;
Using System.IO;
Using System.Web;
Using System.Configuration;

Namespace BLL
{
public class Iplocationsearch
{
private static readonly Qqwry QQ = new Qqwry (configurationmanager.appsettings["IP"] + "Qqwry.dat");

public static iplocation getiplocation (string IP)
{
Return QQ. Searchiplocation (IP);
}
}

/*
How to use:

Example:
Bdqq. Data.qqwry qq=new bdqq. Data.qqwry ("D:\\qqwry.dat");
Bdqq. Data.iplocation ip=qq. Searchiplocation ("127.0.0.1")/write IP address here
Console.WriteLine (ip.country);//Country
Console.WriteLine (Ip.area);//region
*/

The following are class files
Rewritten according to Lumaqq.

/**/
<summary>
Summary description of the Qqwry.
</summary>
public class Qqwry
{
The first of these models
The first mode of #region
/**/
<summary>
The first of these models
</summary>
#endregion
Private Const byte redirect_mode_1 = 0x01;

The second type of model
#region Second Model
/**/
<summary>
The second type of model
</summary>
#endregion
Private Const byte redirect_mode_2 = 0x02;

Length of each record
#region length of each record
/**/
<summary>
Length of each record
</summary>
#endregion
Private Const int ip_record_length = 7;

Database files
#region Database File
/**/
<summary>
File objects
</summary>
#endregion
Private FileStream ipfile;

Private Const string uncountry = "Unknown country";
Private Const string Unarea = "Unknown region";

Index start position
#region Index Start position
/**/
<summary>
Index start position
</summary>
#endregion
Private long Ipbegin;

Index End Position
#region Index End Position
/**/
<summary>
Index End Position
</summary>
#endregion
Private long ipend;

IP Address Object
#region IP Address Object
/**/
<summary>
IP Object
</summary>
#endregion
Private Iplocation Loc;

Storing text content
#region Store text content
/**/
<summary>
Storing text content
</summary>
#endregion
Private byte[] BUF;

Store 3 bytes
#region Store 3 bytes
/**/
<summary>
Store 3 bytes
</summary>
#endregion
Private byte[] B3;

Store 4 bytes
#region Store 4 bytes
/**/
<summary>
Storing 4-byte IP addresses
</summary>
#endregion
Private byte[] B4;

Constructors
#region Constructors
/**/
<summary>
Constructors
</summary>
<param name= "ipfile" >ip database file absolute path </param>
#endregion
Public Qqwry (String ipfile)
{

BUF = new BYTE[100];
B3 = new Byte[3];
B4 = new Byte[4];
Try
{
ipfile = new FileStream (ipfile, FileMode.Open);
}
catch (Exception ex)
{
throw new Exception (ex. message);
}
Ipbegin = ReadLong4 (0);
Ipend = ReadLong4 (4);
loc = new Iplocation ();
}

Search by IP Address
#region Search by IP address
/**/
<summary>
Search IP Address Search
</summary>
<param name= "IP" ></param>
<returns></returns>
#endregion
Public iplocation searchiplocation (string IP)
{
Convert character IP to byte
string[] ipsp = IP. Split ('. ');
if (ipsp.length!= 4)
{
throw new ArgumentOutOfRangeException ("Not a legitimate IP address!");
}
byte[] IP = new Byte[4];
for (int i = 0; i < IP. Length; i++)
{
Ip[i] = (byte) (Int32.Parse (ipsp[i)) & 0xFF);
}

Iplocation local = null;
Long offset = LOCATEIP (IP);

if (offset!=-1)
{
Local = getiplocation (offset);
}

if (local = null)
{
Local = new Iplocation ();
Local.area = Unarea;
Local.country = Uncountry;
}
return to local;
}

Get Specific information
#region Get specific information
/**/
<summary>
Get Specific information
</summary>
<param name= "offset" ></param>
<returns></returns>
#endregion
Private iplocation getiplocation (long offset)
{
Ipfile.position = offset + 4;
Read the first byte to determine if it is a flag byte
byte one = (byte) ipfile.readbyte ();
if (one = = Redirect_mode_1)
{
The first of these models
Read State offset
Long Countryoffset = ReadLong3 ();
Go to offset
Ipfile.position = Countryoffset;
Check flag byte again
byte B = (byte) ipfile.readbyte ();
if (b = = redirect_mode_2)
{
Loc.country = readString (ReadLong3 ());
Ipfile.position = Countryoffset + 4;
}
Else
Loc.country = readString (Countryoffset);

Read the area flag
Loc.area = Readarea (ipfile.position);

}
else if (one = = redirect_mode_2)
{
The second type of model
Loc.country = readString (ReadLong3 ());
Loc.area = Readarea (offset + 8);
}
Else
{
Normal mode
Loc.country = readString (--ipfile.position);
Loc.area = readString (ipfile.position);
}
Return LOC;
}

Access to regional information
#region Access to regional information
/**/
<summary>
Read region name
</summary>
<param name= "offset" ></param>
<returns></returns>
#endregion
private string Readarea (long offset)
{
Ipfile.position = offset;
byte one = (byte) ipfile.readbyte ();
if (one = = Redirect_mode_1 | | | one = redirect_mode_2)
{
Long Areaoffset = readLong3 (offset + 1);
if (areaoffset = 0)
return unarea;
Else
{
Return readString (Areaoffset);
}
}
Else
{
return readString (offset);
}
}

Read string
#region Read string
/**/
<summary>
Read string
</summary>
<param name= "offset" ></param>
<returns></returns>
#endregion
private string readString (long offset)
{
Ipfile.position = offset;
int i = 0;
for (i = 0, buf[i] = (byte) ipfile.readbyte (); Buf[i]!= (Byte) (0); Buf[++i] = (byte) ipfile.readbyte ());

if (i > 0)
Return Encoding.Default.GetString (buf, 0, I);
Else
Return "";
}

Find the absolute offset of an IP address
#region Find the absolute offset of an IP address
/**/
<summary>
Find the absolute offset of an IP address
</summary>
<param name= "IP" ></param>
<returns></returns>
#endregion
Private long Locateip (byte[] IP)
{
Long m = 0;
int R;

Compare the first IP entry
Readip (Ipbegin, B4);
r = Compareip (IP, B4);
if (r = = 0)
return ipbegin;
else if (R < 0)
return-1;
Start the Second Division search
for (Long i = ipbegin, j = ipend; I < J;)
{
m = This.getmiddleoffset (i, j);
Readip (M, B4);
r = Compareip (IP, B4);
if (R > 0)
i = m;
else if (R < 0)
{
if (m = = j)
{
J-= Ip_record_length;
m = j;
}
Else
{
j = m;
}
}
Else
Return ReadLong3 (M + 4);
}
m = readLong3 (M + 4);
Readip (M, B4);
r = Compareip (IP, B4);
if (r <= 0)
return m;
Else
return-1;
}

Read out 4 bytes of IP address
#region read out 4 bytes of IP address
/**/
<summary>
Reads four bytes from the current location, this four byte is an IP address
</summary>
<param name= "offset" ></param>
<param name= "IP" ></param>
#endregion
private void Readip (long offset, byte[] IP)
{
Ipfile.position = offset;
Ipfile.read (IP, 0, IP.) Length);
BYTE tmp = ip[0];
Ip[0] = ip[3];
IP[3] = tmp;
TMP = ip[1];
IP[1] = ip[2];
IP[2] = tmp;
}

Compare IP addresses for same
#region Compare IP addresses
/**/
<summary>
Compare IP addresses for same
</summary>
<param name= "IP" ></param>
<param name= "Beginip" ></param>
<returns>0: Equal, 1:ip greater than beginip,-1: less than </returns>
#endregion
private int Compareip (byte[] IP, byte[] beginip)
{
for (int i = 0; i < 4; i++)
{
int r = Comparebyte (Ip[i], beginip[i]);
if (r!= 0)
return R;
}
return 0;
}

Compare two bytes for equality
#region Compare whether two bytes are equal
/**/
<summary>
Compare two bytes for equality
</summary>
<param name= "BSRC" ></param>
<param name= "BDST" ></param>
<returns></returns>
#endregion
private int Comparebyte (byte bsrc, byte bdst)
{
if ((Bsrc & 0xFF) > (BDST & 0xFF))
return 1;
else if ((bsrc ^ bdst) = = 0)
return 0;
Else
return-1;
}

Reads 4 bytes from the current location
#region reads 4 bytes from the current location
/**/
<summary>
Reads 4 bytes from the current position and converts to a long integer
</summary>
<param name= "offset" ></param>
<returns></returns>
#endregion
Private long ReadLong4 (long offset)
{
LONG ret = 0;
Ipfile.position = offset;
RET |= (Ipfile.readbyte () & 0xFF);
RET |= ((Ipfile.readbyte () << 8) & 0xff00);
RET |= ((Ipfile.readbyte () <<) & 0xff0000);
RET |= ((Ipfile.readbyte () <<) & 0xff000000);
return ret;
}

Reads 3 bytes based on the current position
#region reads 3 bytes based on the current position
/**/
<summary>
Reads 3 bytes based on the current position
</summary>
<param name= "offset" ></param>
<returns></returns>
#endregion
Private long readLong3 (long offset)
{
LONG ret = 0;
Ipfile.position = offset;
RET |= (Ipfile.readbyte () & 0xFF);
RET |= ((Ipfile.readbyte () << 8) & 0xff00);
RET |= ((Ipfile.readbyte () <<) & 0xff0000);
return ret;
}

Read 3 bytes from current location
#region reads 3 bytes from the current location
/**/
<summary>
Read 3 bytes from current location
</summary>
<returns></returns>
#endregion
Private Long ReadLong3 ()
{
LONG ret = 0;
RET |= (Ipfile.readbyte () & 0xFF);
RET |= ((Ipfile.readbyte () << 8) & 0xff00);
RET |= ((Ipfile.readbyte () <<) & 0xff0000);
return ret;
}

Gets the offset between begin and end
#region gets the offset between begin and end
/**/
<summary>
Gets the offset between begin and end
</summary>
<param name= "Begin" ></param>
<param name= "End" ></param>
<returns></returns>
#endregion
Private long Getmiddleoffset (long begin, Long end)
{
Long records = (end-begin)/ip_record_length;
Records >>= 1;
if (records = 0)
Records = 1;
Return begin + Records * IP_RECORD_LENGTH;
}
}//class Qqwry

public class Iplocation
{
Public String country;
public String area;

Public Iplocation ()
{
Country = area = "";
}

Public Iplocation getcopy ()
{
iplocation ret = new iplocation ();
Ret.country = country;
Ret.area = area;
return ret;
}
}
}

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.