直接上代碼(哪一位有不明白的地方,請留言):
//
#include <stdio.h>
#include < stdlib.h >
#include < windows.h >
#include < iostream >
#include < fstream >
#include < iomanip >
using namespace std;
// #define _DEBUG_
long read4Bit(ifstream & f);
long read3Bit(ifstream & f);
int readString(ifstream & f, long exeOffset, char * str);
int read2Zero(ifstream & f, char * buf);
int findIPLocation( char * dat, long exeOffset, DWORD searchIpNum);
void printHelp( char * exePath);
DWORD convertIP( char * sip);
void printIpNum(DWORD ip);
int main( int argc, char ** argv) ... {
char *sip;
char *dat; long offSet;
DWORD intIP;
#if defined _DEBUG_
//for(int i=0;i<argc; i++){
// cout<<"argv["<<i<<"/"<<argc<<"]="<<argv[i]<<" ";
//}
#endif
dat=new char[256];
memset(dat, 0, 256);
if( argc==3 )...{
strcpy(dat, argv[2]);
offSet=0L;
}else...{
delete[]dat;
printHelp(argv[0]);
return 1;
}
sip=new char[16];
memset(sip, 0, 16);
strcpy(sip, argv[1]);
intIP = convertIP(sip);
findIPLocation(dat, offSet, intIP);
delete[]dat; delete[]sip;
return 0;
}
// 第一個參數 是數據文件
// 第二個參數 是如果合併成一個文件的話,開始的偏移
// 第三個參數 是查找的IP的十進制表達
// 第四個參數 是返回的結果
int findIPLocation( char * dat, long exeOffset,DWORD searchIpNum) ... {
#if defined _DEBUG_
int times=0; //查找了多少次
#endif
DWORD ipBeginPos, ipEndPos; //開始的偏移,結束的偏移
DWORD midNum, beginNum, endNum, totalNum; //中間的個數,開始的個數,結束的個數,總共的個數
DWORD ip1Num, ip2Num; //開始IP的十進制值,結束IP的十進制值
long pos, iRead; //當前的文件的指針的位置
char *ipAddr; //*ipAddr2, *ipAddr1, *ipAddr;
if(searchIpNum<0xFF)...{
return -1;
}
ifstream fs;
fs.open(dat, ios::binary | ios::in, 0);
if((!fs.is_open()) || (!fs.good())) ...{
return -2;
}
#if defined _DEBUG_
cout<<" searchIpNum="<<searchIpNum<<", exeOffset="<<exeOffset<<endl;
#endif
fs.seekg(exeOffset, ios_base::beg);
ipBeginPos=read4Bit(fs);
#if defined _DEBUG_
cout<<" ipBeginPos="<<ipBeginPos<<endl;
#endif
ipEndPos=read4Bit(fs);
#if defined _DEBUG_
cout<<" ipEndPos="<<ipEndPos<<endl;
#endif
totalNum = (ipEndPos - ipBeginPos) / 7;
#if defined _DEBUG_
cout<<" totalNum="<<totalNum<<endl;
#endif
if(totalNum<1)...{
return -3;
}
beginNum = 0;
endNum = totalNum;
while( 1 ) ...{ //IP的索引是有順序的,按照個數來二分查找.
midNum= beginNum+(DWORD)((endNum - beginNum)/2);
pos = ipBeginPos+midNum*7;
fs.seekg(exeOffset+pos, ios_base::beg);
#if defined _DEBUG_
cout<<"times="<<++times<<", beginNum="<<beginNum<<", endNum="<<endNum
<<", midNum="<<midNum<<", exeOffset+pos="<<exeOffset+pos
<<endl;
#endif
ip1Num=read4Bit(fs);
if( ip1Num<1 ) ...{
fs.close(); cout<<"[1]while(){...}"<<endl;
return -2;
}
#if defined _DEBUG_
//printIpNum(ip1Num);
#endif
fs.seekg(3, ios_base::cur);
ip2Num=read4Bit(fs);
if( ip2Num<1 ) ...{
fs.close(); cout<<"[3]while(){...}"<<endl;
return -2;
}
#if defined _DEBUG_
//printIpNum(ip2Num);
#endif
if( (ip1Num<=searchIpNum) && (searchIpNum<=ip2Num) )...{
fs.seekg(exeOffset+pos, ios_base::beg);
ip1Num=read4Bit(fs);
#if defined _DEBUG_
cout<<" Last position:"<<endl<<endl;
printIpNum(ip1Num);
cout<<"pos="<<pos<<" ";
#endif
pos=read3Bit(fs);
#if defined _DEBUG_
cout<<"pos="<<pos<<endl;
#endif
fs.seekg(exeOffset+pos, ios_base::beg);
ip2Num=read4Bit(fs);
#if defined _DEBUG_
printIpNum(ip2Num);
#endif
break;
}else if(ip1Num>searchIpNum)...{
endNum = midNum;
}else if(ip2Num<searchIpNum)...{
beginNum = midNum;
}
}
#if defined _DEBUG_
cout<<endl<<"Found position:"<<fs.tellg()<<endl;
#endif
ipAddr=new char[512];
memset(ipAddr, 0, 512);
iRead = readString(fs, exeOffset, ipAddr);
if(iRead>0)...{
cout<<ipAddr<<" ";
}
memset(ipAddr, 0, 512);
iRead = readString(fs, exeOffset, ipAddr);
if(iRead>0)...{
cout<<ipAddr;
}
delete[]ipAddr;
fs.close();
return 0;
}
long read4Bit(ifstream & f) ... {
long t; char c[4];
t=0;
f.read((char*)&t, 4);
return t;
}
long read3Bit(ifstream & f) ... {
long t;
f.read((char*)&t, 3);
return t;
}
int readString(ifstream & f, long exeOffset, char * str) ... {
char ipFlag; long ipSeek, tmpSeek;
int ret;
ipFlag=f.get();
if(ipFlag==0x1) ...{ // 重定向模式1: 城市信息隨國家信息定向
ipSeek=read3Bit(f);
f.seekg(exeOffset+ipSeek, ios_base::beg);
ret=readString(f, exeOffset, str);
}else if(ipFlag == 0x2) ...{ // 重定向模式2: 城市信息沒有隨國家信息定向
ipSeek=read3Bit(f);
tmpSeek=f.tellg();
f.seekg(exeOffset+ipSeek, ios_base::beg);
ret=readString(f, exeOffset, str);
f.seekg(exeOffset+tmpSeek, ios_base::beg);
} else ...{
f.seekg(-1, ios_base::cur);
ret=read2Zero(f, str);
}
return ret;
}
int read2Zero(ifstream & f, char * buf) ... {
f.get(buf, 128, 0);
return strlen(buf);
}
void printHelp( char * exePath) ... {
char *fileName;
fileName=strrchr(exePath,'/');
if(fileName==NULL)...{
strcpy(fileName, exePath);
}else...{
fileName++;
}
cout<<endl
<<"Wry IP Search Beta by cangwu.lee@gmail.com "
<<"Usage:"
<<" "<<fileName<<"<ip address> <Path to wry.dat> "
<<endl;
free(fileName);
}
DWORD convertIP( char * sip) ... {
char *c1, *c2, *c3, *c4;
DWORD i1, i2, i3, i4, dd;
#if defined _DEBUG_
cout<<" ip="<<sip<<endl;
#endif
c1 = strtok(sip, " ,.-");
if(c1==NULL)...{
cout<<"IP address incorrect."<<endl;
return 0;
}
c2 = strtok(NULL," ,.-");if(c2==NULL)c2="0";
c3 = strtok(NULL," ,.-");if(c3==NULL)c3="0";
c4 = strtok(NULL," ,.-");if(c4==NULL)c4="0";
i1=atol(c1); i2=atol(c2); i3=atol(c3); i4=atol(c4);
if(i1<0 || i2<0 || i3<0 || i4<0
|| i1>254 || i2>255 || i3>255 || i4>255)...{
cout<<"IP address incorrect."<<endl;
delete[]c1; delete[]c2; delete[]c3; delete[]c4;
return 0;
}
delete[]c1; delete[]c2; delete[]c3; delete[]c4;
dd =(i1<<24) + (i2<<16) + (i3<<8) + i4;
return dd;
}
void printIpNum(DWORD ip) ... {
long ip1, ip2, ip3, ip4, dd1, dd2, dd3;
char addr[16];
ip1=(ip>>24) & 0xFF; dd1=ip1<<24; // 16777216;
ip2=( (ip-dd1)>>16) & 0xFF; dd2=ip2<<16; //(ip-dd1) / 65536;
ip3=( (ip-dd1-dd2)>>8) & 0xFF; dd3=ip3<<8; //(ip-dd1-dd2) / 256;
ip4=ip-dd1-dd2-dd3;
memset(addr, 0, 16);
sprintf(addr,"%3.3d.%3.3d.%3.3d.%3.3d", ip1, ip2, ip3, ip4);
addr[15]=0;
cout<<" ip="<<ip<<" addr="<<addr<<endl;
delete[]addr;
}
#include < stdlib.h >
#include < windows.h >
#include < iostream >
#include < fstream >
#include < iomanip >
using namespace std;
// #define _DEBUG_
long read4Bit(ifstream & f);
long read3Bit(ifstream & f);
int readString(ifstream & f, long exeOffset, char * str);
int read2Zero(ifstream & f, char * buf);
int findIPLocation( char * dat, long exeOffset, DWORD searchIpNum);
void printHelp( char * exePath);
DWORD convertIP( char * sip);
void printIpNum(DWORD ip);
int main( int argc, char ** argv) ... {
char *sip;
char *dat; long offSet;
DWORD intIP;
#if defined _DEBUG_
//for(int i=0;i<argc; i++){
// cout<<"argv["<<i<<"/"<<argc<<"]="<<argv[i]<<" ";
//}
#endif
dat=new char[256];
memset(dat, 0, 256);
if( argc==3 )...{
strcpy(dat, argv[2]);
offSet=0L;
}else...{
delete[]dat;
printHelp(argv[0]);
return 1;
}
sip=new char[16];
memset(sip, 0, 16);
strcpy(sip, argv[1]);
intIP = convertIP(sip);
findIPLocation(dat, offSet, intIP);
delete[]dat; delete[]sip;
return 0;
}
// 第一個參數 是數據文件
// 第二個參數 是如果合併成一個文件的話,開始的偏移
// 第三個參數 是查找的IP的十進制表達
// 第四個參數 是返回的結果
int findIPLocation( char * dat, long exeOffset,DWORD searchIpNum) ... {
#if defined _DEBUG_
int times=0; //查找了多少次
#endif
DWORD ipBeginPos, ipEndPos; //開始的偏移,結束的偏移
DWORD midNum, beginNum, endNum, totalNum; //中間的個數,開始的個數,結束的個數,總共的個數
DWORD ip1Num, ip2Num; //開始IP的十進制值,結束IP的十進制值
long pos, iRead; //當前的文件的指針的位置
char *ipAddr; //*ipAddr2, *ipAddr1, *ipAddr;
if(searchIpNum<0xFF)...{
return -1;
}
ifstream fs;
fs.open(dat, ios::binary | ios::in, 0);
if((!fs.is_open()) || (!fs.good())) ...{
return -2;
}
#if defined _DEBUG_
cout<<" searchIpNum="<<searchIpNum<<", exeOffset="<<exeOffset<<endl;
#endif
fs.seekg(exeOffset, ios_base::beg);
ipBeginPos=read4Bit(fs);
#if defined _DEBUG_
cout<<" ipBeginPos="<<ipBeginPos<<endl;
#endif
ipEndPos=read4Bit(fs);
#if defined _DEBUG_
cout<<" ipEndPos="<<ipEndPos<<endl;
#endif
totalNum = (ipEndPos - ipBeginPos) / 7;
#if defined _DEBUG_
cout<<" totalNum="<<totalNum<<endl;
#endif
if(totalNum<1)...{
return -3;
}
beginNum = 0;
endNum = totalNum;
while( 1 ) ...{ //IP的索引是有順序的,按照個數來二分查找.
midNum= beginNum+(DWORD)((endNum - beginNum)/2);
pos = ipBeginPos+midNum*7;
fs.seekg(exeOffset+pos, ios_base::beg);
#if defined _DEBUG_
cout<<"times="<<++times<<", beginNum="<<beginNum<<", endNum="<<endNum
<<", midNum="<<midNum<<", exeOffset+pos="<<exeOffset+pos
<<endl;
#endif
ip1Num=read4Bit(fs);
if( ip1Num<1 ) ...{
fs.close(); cout<<"[1]while(){...}"<<endl;
return -2;
}
#if defined _DEBUG_
//printIpNum(ip1Num);
#endif
fs.seekg(3, ios_base::cur);
ip2Num=read4Bit(fs);
if( ip2Num<1 ) ...{
fs.close(); cout<<"[3]while(){...}"<<endl;
return -2;
}
#if defined _DEBUG_
//printIpNum(ip2Num);
#endif
if( (ip1Num<=searchIpNum) && (searchIpNum<=ip2Num) )...{
fs.seekg(exeOffset+pos, ios_base::beg);
ip1Num=read4Bit(fs);
#if defined _DEBUG_
cout<<" Last position:"<<endl<<endl;
printIpNum(ip1Num);
cout<<"pos="<<pos<<" ";
#endif
pos=read3Bit(fs);
#if defined _DEBUG_
cout<<"pos="<<pos<<endl;
#endif
fs.seekg(exeOffset+pos, ios_base::beg);
ip2Num=read4Bit(fs);
#if defined _DEBUG_
printIpNum(ip2Num);
#endif
break;
}else if(ip1Num>searchIpNum)...{
endNum = midNum;
}else if(ip2Num<searchIpNum)...{
beginNum = midNum;
}
}
#if defined _DEBUG_
cout<<endl<<"Found position:"<<fs.tellg()<<endl;
#endif
ipAddr=new char[512];
memset(ipAddr, 0, 512);
iRead = readString(fs, exeOffset, ipAddr);
if(iRead>0)...{
cout<<ipAddr<<" ";
}
memset(ipAddr, 0, 512);
iRead = readString(fs, exeOffset, ipAddr);
if(iRead>0)...{
cout<<ipAddr;
}
delete[]ipAddr;
fs.close();
return 0;
}
long read4Bit(ifstream & f) ... {
long t; char c[4];
t=0;
f.read((char*)&t, 4);
return t;
}
long read3Bit(ifstream & f) ... {
long t;
f.read((char*)&t, 3);
return t;
}
int readString(ifstream & f, long exeOffset, char * str) ... {
char ipFlag; long ipSeek, tmpSeek;
int ret;
ipFlag=f.get();
if(ipFlag==0x1) ...{ // 重定向模式1: 城市信息隨國家信息定向
ipSeek=read3Bit(f);
f.seekg(exeOffset+ipSeek, ios_base::beg);
ret=readString(f, exeOffset, str);
}else if(ipFlag == 0x2) ...{ // 重定向模式2: 城市信息沒有隨國家信息定向
ipSeek=read3Bit(f);
tmpSeek=f.tellg();
f.seekg(exeOffset+ipSeek, ios_base::beg);
ret=readString(f, exeOffset, str);
f.seekg(exeOffset+tmpSeek, ios_base::beg);
} else ...{
f.seekg(-1, ios_base::cur);
ret=read2Zero(f, str);
}
return ret;
}
int read2Zero(ifstream & f, char * buf) ... {
f.get(buf, 128, 0);
return strlen(buf);
}
void printHelp( char * exePath) ... {
char *fileName;
fileName=strrchr(exePath,'/');
if(fileName==NULL)...{
strcpy(fileName, exePath);
}else...{
fileName++;
}
cout<<endl
<<"Wry IP Search Beta by cangwu.lee@gmail.com "
<<"Usage:"
<<" "<<fileName<<"<ip address> <Path to wry.dat> "
<<endl;
free(fileName);
}
DWORD convertIP( char * sip) ... {
char *c1, *c2, *c3, *c4;
DWORD i1, i2, i3, i4, dd;
#if defined _DEBUG_
cout<<" ip="<<sip<<endl;
#endif
c1 = strtok(sip, " ,.-");
if(c1==NULL)...{
cout<<"IP address incorrect."<<endl;
return 0;
}
c2 = strtok(NULL," ,.-");if(c2==NULL)c2="0";
c3 = strtok(NULL," ,.-");if(c3==NULL)c3="0";
c4 = strtok(NULL," ,.-");if(c4==NULL)c4="0";
i1=atol(c1); i2=atol(c2); i3=atol(c3); i4=atol(c4);
if(i1<0 || i2<0 || i3<0 || i4<0
|| i1>254 || i2>255 || i3>255 || i4>255)...{
cout<<"IP address incorrect."<<endl;
delete[]c1; delete[]c2; delete[]c3; delete[]c4;
return 0;
}
delete[]c1; delete[]c2; delete[]c3; delete[]c4;
dd =(i1<<24) + (i2<<16) + (i3<<8) + i4;
return dd;
}
void printIpNum(DWORD ip) ... {
long ip1, ip2, ip3, ip4, dd1, dd2, dd3;
char addr[16];
ip1=(ip>>24) & 0xFF; dd1=ip1<<24; // 16777216;
ip2=( (ip-dd1)>>16) & 0xFF; dd2=ip2<<16; //(ip-dd1) / 65536;
ip3=( (ip-dd1-dd2)>>8) & 0xFF; dd3=ip3<<8; //(ip-dd1-dd2) / 256;
ip4=ip-dd1-dd2-dd3;
memset(addr, 0, 16);
sprintf(addr,"%3.3d.%3.3d.%3.3d.%3.3d", ip1, ip2, ip3, ip4);
addr[15]=0;
cout<<" ip="<<ip<<" addr="<<addr<<endl;
delete[]addr;
}
測試完就貼上來的,代碼可能有點亂。寫得不好的地方,請指正。