// NL_QQwry.cpp: implementation of the NL_QQwry class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
//#include "DlgPortView.h"
#include "NL_QQwry.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
NL_QQwry::NL_QQwry()
{
m_bOpen=OpenQQwry("C:\\Program Files\\Tencent\\QQWry.dat");
GetBE();
}
NL_QQwry::~NL_QQwry()
{
CloseQQwry();
}
/*********************************************************************
* OpenQQwry(CString szFileName)
*
* 用来打开IP数据库文件的函数函数
*
* 说明:一般打开QQWry.dat文件
*********************************************************************/
bool NL_QQwry::OpenQQwry(CString szFileName)
{
if(!m_file.Open(szFileName,CFile::modeRead|CFile::typeBinary))
{
::MessageBox(NULL,
_T("打开文件失败!“QQwry.dat”不在路径“C:\\Program Files\\Tencent\\QQ”下,请下载!"), _T("提示信息"),
MB_OK|MB_ICONINFORMATION);
return false;
}
else
return true;
}
void NL_QQwry::CloseQQwry()
{
if(m_bOpen)m_file.Close();
}
bool NL_QQwry::GetBE()
{
if(!m_bOpen)return false;
m_file.Seek(0,CFile::begin);
if(m_file.Read(&m_be,sizeof(BE))>0)
return true;
else
return false;
}
/*********************************************************************
* GetStartIPInfo(int iIndex)
*
* 根据输入IP的索引得到该段IP所属的IP段的起始IP
*********************************************************************/
int NL_QQwry::GetStartIPInfo(int iIndex)
{
BYTE buf[MAXBUF];
int ioff;
if(!m_bOpen) return 0;
ioff=m_be.uBOff+iIndex*7;
if(ioff>m_be.uEOff) return 0;
m_file.Seek(m_be.uBOff+iIndex*7,CFile::begin);
m_file.Read(&m_ipoff,sizeof(IPOFF));
ioff=(m_ipoff.off1+m_ipoff.off2*256+m_ipoff.off3*256*256);
m_file.Seek(ioff,CFile::begin);
m_file.Read(&m_ei,sizeof(EndInfo));
if(m_ei.buf[0]!=1 && m_ei.buf[0]!=2)
{
m_ei.bMode=1;//没有跳
for(int i=0;i<MAXBUF;i++)
{
if(m_ei.buf[i]==0)
{
if(m_ei.buf[i+1]==2)
{
m_ei.bMode=2;//Local 跳
m_ei.offset1=m_ei.buf[i+2]+
m_ei.buf[i+3]*256+
m_ei.buf[i+4]*256*256;
}
break;
}
}
}
else if(m_ei.buf[0]==2)
{
m_ei.bMode=3;//Country 跳 local不跳
m_ei.offset1=m_ei.buf[1]+m_ei.buf[2]*256+m_ei.buf[3]*256*256;
if(m_ei.buf[4]!=2)
{
m_ei.bMode=3;
}
else
{
m_ei.bMode=4;//Country跳 local跳
m_ei.offset2=m_ei.buf[5]+m_ei.buf[6]*256+m_ei.buf[7]*256*256;
}
}
else if(m_ei.buf[0]==1)
{
m_ei.offset1=m_ei.buf[1]+m_ei.buf[2]*256+m_ei.buf[3]*256*256;
m_file.Seek(m_ei.offset1,CFile::begin);
m_file.Read(buf,MAXBUF);
memcpy(m_ei.buf,buf,MAXBUF);
m_ei.bMode=0;
if(m_ei.buf[0]!=2)
{
for(int i=0;i<MAXBUF;i++)
{
if(m_ei.buf[i]==0)
{
if(m_ei.buf[i+1]!=2)
{
m_ei.bMode=5;//1 没有跳
}
else
{
m_ei.bMode=6;//1 Country不跳 Local 跳
m_ei.offset2=m_ei.buf[i+2]+
m_ei.buf[i+3]*256+
m_ei.buf[i+4]*256*256;
}
break;
}
}
}
else
{
if(m_ei.buf[4]!=2)
{
m_ei.bMode=7;// 1 Country跳 Local不跳
m_ei.offset2=m_ei.buf[1]+
m_ei.buf[2]*256+
m_ei.buf[3]*256*256;
}
else
{
m_ei.bMode=8;// 1 Country跳 Local跳
m_ei.offset1=m_ei.buf[1]+
m_ei.buf[2]*256+
m_ei.buf[3]*256*256;
m_ei.offset2=m_ei.buf[5]+
m_ei.buf[6]*256+
m_ei.buf[7]*256*256;
}
}
}
return ioff;
}
//得到总的IP记录数
int NL_QQwry::GetRecordCount(void)
{
if(!m_bOpen) return 0;
if((m_be.uEOff-m_be.uBOff)<0) return 0;
return (m_be.uEOff-m_be.uBOff)/7+1;
}
//根据位置读信息
CString NL_QQwry::GetStr(int ioffset)
{
if(ioffset>m_be.uEOff) return "";
BYTE ch;
CString buf="";
m_file.Seek(ioffset,CFile::begin);
int i=0;
while(1)
{
m_file.Read(&ch,1);
if(ch==0)
break;
buf+=ch;
i++;
if(i>50)break;
}
return buf;
}
/*********************************************************************
* GetCountryLocal(int index)
*
* 根据索引得到地址
*********************************************************************/
CString NL_QQwry::GetCountryLocal(int index)
{
if(index<0 || index>GetRecordCount()-1)
return "没有查到";
return GetCountryLocal(m_ei.bMode,GetStartIPInfo(index)+4);
}
CString NL_QQwry::GetCountryLocal(BYTE bMode,int ioffset)
{
CString buf="";
if(bMode==1)//X 没有跳
{
buf=GetStr(ioffset);
buf+=" ";
buf+=GetStr();
}
if(bMode==2)//X Country不跳 Local 跳
{
buf=GetStr(ioffset);
buf+=" ";
buf+=GetStr(m_ei.offset1);
}
if(bMode==3)//2 Country跳 local不跳
{
buf=GetStr(m_ei.offset1);
buf+=" ";
buf+=GetStr(ioffset+4);
}
if(bMode==4)//2 Country跳 local跳
{
buf=GetStr(m_ei.offset1);
buf+=" ";
buf+=GetStr(m_ei.offset2);
}
if(bMode==5)//1 没有跳
{
buf=GetStr(m_ei.offset1);
buf+=" ";
buf+=GetStr();
}
if(bMode==6)//1 Country不跳 Local 跳
{
buf=GetStr(m_ei.offset1);
buf+=" ";
buf+=GetStr(m_ei.offset2);
}
if(bMode==7)//1 Country跳 Local 不跳
{
buf=GetStr(m_ei.offset2);
buf+=" ";
buf+=GetStr(m_ei.offset1+4);
}
if(bMode==8)//1 Country跳 Local跳
{
buf=GetStr(m_ei.offset1);
buf+=" ";
buf+=GetStr(m_ei.offset2);
}
return buf;
}
CString NL_QQwry::GetStr()
{
BYTE ch;
CString buf="";
int i=0;
while(1)
{
m_file.Read(&ch,1);
if(ch==0)
break;
buf+=ch;
i++;
if(i>50)break;
}
return buf;
}
/*********************************************************************
* SaveToFile()
*
* 保存所有信息到文件
*********************************************************************/
void NL_QQwry::SaveToFile()
{
//
//在内存中操作可能速度更快一些
//利用缓存,就这样了,提高速度就自己解决了
//
FILE *out;
CString str1,str2;
out=fopen("out.txt","wb");
int ioff;
//m_buf.Format("%08X %08X",m_be.uBOff,m_be.uEOff);
m_buf.Format("Total %d\r\n",GetRecordCount());
fwrite(m_buf,1,m_buf.GetLength(),out);
for(m_i=0;m_i<GetRecordCount();m_i++)
//for(m_i=0;m_i<2000;m_i++)
{
ioff=GetStartIPInfo(m_i);
str1.Format("%d.%d.%d.%d",m_ipoff.b3,m_ipoff.b2,m_ipoff.b1,m_ipoff.b0);
str2.Format("%d.%d.%d.%d",m_ei.b3,m_ei.b2,m_ei.b1,m_ei.b0);
m_buf.Format("%-15s %-15s %s\r\n",
str1,str2,GetCountryLocal(m_ei.bMode,ioff+4));
fwrite(m_buf,1,m_buf.GetLength(),out);
}
fclose(out);
}
/*********************************************************************
* IP2Add(CString szIP)
*
* 根据IP找到地址
*
* 说明:
*********************************************************************/
CString NL_QQwry::IP2Add(CString szIP)
{
if(szIP=="")return "请输入IP地址";
return GetCountryLocal(GetIndex(szIP));
}
/*********************************************************************
* GetIndex(CString szIP)
*
* 根据IP找到索引
*
* 说明:
*********************************************************************/
int NL_QQwry::GetIndex(CString szIP)
{
int index=-1;
DWORD dwInputIP;
DWORD dwStartIP;
dwInputIP=IP2DWORD(szIP);
//顺序查找速度肯定慢
/*
if(dwInputIP<=0x7FFFFFFF)
for (int i=0;i<GetRecordCount();i++)
{
dwStartIP=GetSIP(i);
if(dwStartIP<=dwInputIP && dwInputIP<=m_dwLastIP)
{index=i;break;}
}
else
for (int i=GetRecordCount()-1;i>=0;i--)
{
dwStartIP=GetSIP(i);
if(dwStartIP<=dwInputIP && dwInputIP<=m_dwLastIP)
{index=i;break;}
}
*/
//
//利用半跳方法速度快一些
//
int iT;
int iB,iE;
iB=0;
iE=GetRecordCount()-1;
iT=iE/2;
while(iB<iE)
{
dwStartIP=GetSIP(iT);
if(dwInputIP==dwStartIP)
{
index =iT;
break;
}
if((iE-iB)<=1)
{
/*
CString s;
s.Format("%d %d %d %d