using System;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
//Introduced namespace
using ;
/// <summary>
/// Determine IP home category
/// </summary>
public class IpSearch
{
private static object lockHelper = new object();
static PHCZIP pcz = new PHCZIP();
static string filePath = "";
static bool fileIsExsit = true;
static IpSearch()
{
filePath = ("~/");
(filePath);
}
/// <summary>
/// Return IP search results
/// </summary>
/// <param name="IPValue">IP address to look for</param>
/// <returns></returns>
public static string GetAddressWithIP(string IPValue)
{
lock (lockHelper)
{
string result = (());
if (fileIsExsit)
{
if (("IANA") >= 0)
{
return "";
}
else
{
return result;
}
}
else
{
return null;
}
}
}
/// <summary>
/// Auxiliary class, used to save IP index information
/// </summary>
///
public class CZ_INDEX_INFO
{
public UInt32 IpSet;
public UInt32 IpEnd;
public UInt32 Offset;
public CZ_INDEX_INFO()
{
IpSet = 0;
IpEnd = 0;
Offset = 0;
}
}
//Read the innocent IP database class
public class PHCZIP
{
protected bool bFilePathInitialized;
protected string FilePath;
protected FileStream FileStrm;
protected UInt32 Index_Set;
protected UInt32 Index_End;
protected UInt32 Index_Count;
protected UInt32 Search_Index_Set;
protected UInt32 Search_Index_End;
protected CZ_INDEX_INFO Search_Set;
protected CZ_INDEX_INFO Search_Mid;
protected CZ_INDEX_INFO Search_End;
public PHCZIP()
{
bFilePathInitialized = false;
}
public PHCZIP(string dbFilePath)
{
bFilePathInitialized = false;
SetDbFilePath(dbFilePath);
}
// Use dichotomy to find the index area and initialize the search area
public void Initialize()
{
Search_Index_Set = 0;
Search_Index_End = Index_Count - 1;
}
//Close the file
public void Dispose()
{
if (bFilePathInitialized)
{
bFilePathInitialized = false;
();
//();
}
}
public bool SetDbFilePath(string dbFilePath)
{
if (dbFilePath == "")
{
return false;
}
try
{
FileStrm = new FileStream(dbFilePath, , , );
}
catch
{
return false;
}
//Check file length
if ( < 8)
{
();
//();
return false;
}
//Get the absolute offset of the first index and the absolute offset of the last index
(0, );
Index_Set = GetUInt32();
Index_End = GetUInt32();
//Get the total number of indexes
Index_Count = (Index_End - Index_Set) / 7 + 1;
bFilePathInitialized = true;
return true;
}
public string GetAddressWithIP(string IPValue)
{
if (!bFilePathInitialized)
{
return "";
}
Initialize();
UInt32 ip = IPToUInt32(IPValue);
while (true)
{
//First initialize the interval for this round of search
//The interval header
Search_Set = IndexInfoAtPos(Search_Index_Set);
//Terminal interval
Search_End = IndexInfoAtPos(Search_Index_End);
//Judge whether the IP is within the interval header
if (ip >= Search_Set.IpSet && ip <= Search_Set.IpEnd)
return ReadAddressInfoAtOffset(Search_Set.Offset);
//Judge whether the IP is within the end of the interval
if (ip >= Search_End.IpSet && ip <= Search_End.IpEnd)
return ReadAddressInfoAtOffset(Search_End.Offset);
// Calculate the midpoint of the interval
Search_Mid = IndexInfoAtPos((Search_Index_End + Search_Index_Set) / 2);
//Judge whether the IP is at the midpoint
if (ip >= Search_Mid.IpSet && ip <= Search_Mid.IpEnd)
return ReadAddressInfoAtOffset(Search_Mid.Offset);
//Not found this round, prepare for the next round
if (ip < Search_Mid.IpSet)
//The IP is smaller than the midpoint of the interval. Set the tail of the interval to the current midpoint and reduce the interval by 1 times.
Search_Index_End = (Search_Index_End + Search_Index_Set) / 2;
else
//The IP is larger than the midpoint of the interval. Set the interval header to the current midpoint and narrow the interval by 1 times.
Search_Index_Set = (Search_Index_End + Search_Index_Set) / 2;
}
//return "";
}
private string ReadAddressInfoAtOffset(UInt32 Offset)
{
string country = "";
string area = "";
UInt32 country_Offset = 0;
byte Tag = 0;
//Skip 4 bytes, because these 4 bytes are the upper limit of the IP interval of the index.
(Offset + 4, );
//Read a byte to get the "addressing method" that describes the country information "
Tag = GetTag();
if (Tag == 0x01)
{
//Mode 0x01 indicates that the next 3 bytes indicates the offset position
(GetOffset(), );
//Continue to check "addressing method"
Tag = GetTag();
if (Tag == 0x02)
{
//Mode 0x02 means that the next 3 bytes represent the offset position of the country information
//Save this offset position first, because we want to read the region information behind it.
country_Offset = GetOffset();
//Read regional information (Note: According to Luma's instructions, there seems to be no such possibilities, but some situations seem to be not taken into account during the test.
//So I wrote ReadArea() to read.
area = ReadArea();
//Read national information
(country_Offset, );
country = ReadString();
}
else
{
//This model means that the country and region information is saved, ending with '\0'.
(-1, );
country = ReadString();
area = ReadArea();
}
}
else if (Tag == 0x02)
{
//Mode 0x02 indicates that the country information is an offset position
country_Offset = GetOffset();
//Read the region information first
area = ReadArea();
//Read national information
(country_Offset, );
country = ReadString();
}
else
{
//This mode is the simplest, just read the country and region directly.
(-1, );
country = ReadString();
area = ReadArea();
}
string Address = country + " " + area;
return Address;
}
private UInt32 GetOffset()
{
byte[] TempByte4 = new byte[4];
TempByte4[0] = (byte)();
TempByte4[1] = (byte)();
TempByte4[2] = (byte)();
TempByte4[3] = 0;
return BitConverter.ToUInt32(TempByte4, 0);
}
protected string ReadArea()
{
byte Tag = GetTag();
if (Tag == 0x01 || Tag == 0x02)
{
(GetOffset(), );
return ReadString();
}
else
{
(-1, );
return ReadString();
}
}
protected string ReadString()
{
UInt32 Offset = 0;
byte[] TempByteArray = new byte[256];
TempByteArray[Offset] = (byte)();
while (TempByteArray[Offset] != 0x00)
{
Offset += 1;
TempByteArray[Offset] = (byte)();
}
return (TempByteArray).TrimEnd('\0');
}
protected byte GetTag()
{
return (byte)();
}
protected CZ_INDEX_INFO IndexInfoAtPos(UInt32 Index_Pos)
{
CZ_INDEX_INFO Index_Info = new CZ_INDEX_INFO();
// Calculate the offset position in the file based on the index number
(Index_Set + 7 * Index_Pos, );
Index_Info.IpSet = GetUInt32();
Index_Info.Offset = GetOffset();
(Index_Info.Offset, );
Index_Info.IpEnd = GetUInt32();
return Index_Info;
}
/// <summary>
/// Convert from IP to Int32
/// </summary>
/// <param name="IpValue"></param>
/// <returns></returns>
public UInt32 IPToUInt32(string IpValue)
{
string[] IpByte = ('.');
Int32 nUpperBound = (0);
if (nUpperBound != 3)
{
IpByte = new string[4];
for (Int32 i = 1; i <= 3 - nUpperBound; i++)
IpByte[nUpperBound + i] = "0";
}
byte[] TempByte4 = new byte[4];
for (Int32 i = 0; i <= 3; i++)
{
//'If it is .Net 2.0, TryParse can be supported.
//'If Not ((IpByte(i), TempByte4(3 - i))) Then
//' TempByte4(3 - i) = &H0
//'End If
if (IsNumeric(IpByte[i]))
TempByte4[3 - i] = (byte)(Convert.ToInt32(IpByte[i]) & 0xff);
}
return BitConverter.ToUInt32(TempByte4, 0);
}
/// <summary>
/// Determine whether it is a number
/// </summary>
/// <param name="str">Stand to be judged</param>
/// <returns></returns>
protected bool IsNumeric(string str)
{
if (str != null && (str, @"^-?\d+$"))
return true;
else
return false;
}
protected UInt32 GetUInt32()
{
byte[] TempByte4 = new byte[4];
(TempByte4, 0, 4);
return BitConverter.ToUInt32(TempByte4, 0);
}
}
}