#define  _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <stdlib.h>
#include <windows.h>
using namespace std; char *ptr = NULL; //ptr of image//内存映射文件
char *p = NULL; //point to index//索引区
unsigned int total; //ip count inline void Load(void)
{
HANDLE hnd; //file handle
DWORD NumberOfBytesRead; //len
char text[2048]; //patch
char *temp;
unsigned int len; //get patch
if( !GetModuleFileName(0, text, 2048))
return; temp = strrchr(text, 92); // 92 = '\'
*(temp + 1) = NULL;
strcat(temp, "QQwry.dat"); //CreateFile
hnd = CreateFile(text, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if(INVALID_HANDLE_VALUE == hnd)
{
::MessageBox(NULL, text, "不能打开文件!", NULL);
return;
} //get len
len = SetFilePointer(hnd, NULL, NULL, FILE_END);
SetFilePointer(hnd, NULL, NULL, FILE_BEGIN); //malloc
ptr = (char*)malloc(len + 9);
if(!ptr)
{
CloseHandle(hnd);
::MessageBox(NULL, "不能分配内存!", NULL, NULL);
return;
} //read
if(!ReadFile(hnd, ptr, len, &NumberOfBytesRead, NULL))
{
CloseHandle(hnd);
free(ptr);
::MessageBox(NULL, text, "不能读入文件!", NULL);
return;
}
CloseHandle(hnd); //calc total - 1
total = (*((unsigned int*)ptr + 1) - *(unsigned int*)ptr); //check file
if(total % 7 != 0)
{
free(ptr);
::MessageBox(NULL, text, "QQwry.dat文件有损坏!", NULL);
return;
} total /= 7;
++total;
p = ptr + *(unsigned int*)ptr; //ptr of index area
} inline unsigned int get_3b(const char *mem)
{
return 0x00ffffff & *(unsigned int*)(mem);
} inline unsigned int str2ip(const char *lp)
{
unsigned int iIP = 0;
unsigned int tmpIP = 0; while(*lp)
{
if('.' == *lp)
{
iIP = 256 * iIP + tmpIP;
tmpIP = 0;
}
else
{
tmpIP = 10 * tmpIP + *lp - '0';
}
++lp;
} iIP = 256 * iIP + tmpIP;
return iIP;
} string _GetAddress(string IPstr)
{
string ret;
if(NULL == p)
{
ret = "";
return ret;
} unsigned int ip = str2ip(IPstr.c_str());
char *now_p; unsigned int begin = 0, end = total;
while(true)
{
if( begin >= end - 1 )
{
break;
}
if( ip < *(unsigned int*)(p + (begin + end)/2 * 7))
{
end = (begin + end) / 2;
}
else
{
begin = (begin + end) / 2;
}
} unsigned int temp = get_3b(p + 7 * begin + 4);
if(ip <= *(unsigned int*)(ptr + temp)) //ok, found
{
now_p = ptr + temp + 4;
if( 0x01 == *now_p ) //如果0x01跳过去找国家
{
now_p = ptr + get_3b(now_p + 1);
}
//country
if( 0x02 == *now_p ) //如果国家0x02再跳
{
ret = ptr + get_3b(now_p + 1);
now_p += 4;
}
else
{
ret = now_p;
for(; *now_p; ++now_p)
;
++now_p;
}
//local
if( 0x02 == *now_p ) //找到国家以后还发现0x02跳过去找地区
{
ret += ptr + get_3b(now_p + 1);
}
else
{
ret += now_p;
}
}
else
{
ret = "未知数据";
}
return ret;
} int main(void)
{
Load();
string ip;
while (cin >> ip)
{
cout << _GetAddress(ip) << endl;
}
return 0;
}

http://hzy3774.iteye.com/blog/1851364

http://hzy3774.iteye.com/blog/1851364

最新文章

  1. JSP分页显示实例(基于Bootstrap)
  2. Three.js学习(相机,场景,渲染,形状)
  3. 难道只有我一个人想吐槽npm这种包管理方式么
  4. 网络编程(一)——InetAddress
  5. POJ1144 Network 无向图的割顶
  6. c程序设计语言_习题1-18_删除输入流中每一行末尾的空格和制表符,并删除完全是空格的行
  7. git rebase实战
  8. 使用requirejs实现模块化编程
  9. 算法打基础——顺序统计(找第k小数)
  10. on IRC, how to use secure connection(SSL) and get a cloak/vhost to hide your IP
  11. JSON数据传递
  12. Linux中mongodb定时远程备份
  13. LVS-DR模式 SOP
  14. SQL: Cannot create JDBC driver of class &#39;&#39; for connect URL
  15. Jenkins 配置CI/CD任务
  16. VBA二次学习笔记(2)——两个Excel表内容比较
  17. Android Studio中Run按钮是灰色的问题解决
  18. Machine Learning - Andrew Ng - Coursera
  19. php -- 文件读写
  20. 第2章 css边框属性

热门文章

  1. C# async await 死锁问题总结
  2. pyspider遇到的第一个坑:Active Tasks成功,Results无内容
  3. Mybatis是如何实现SQL防注入的
  4. 对接百度地图API 实现地址转经纬度
  5. 2019 年 stackoverflow 网站最受欢迎的 20 个 Python 问题
  6. MyBatis项目实战 快速将MySQL转换成Oracle语句
  7. JVM之堆的体系结构
  8. 修饰符new与override
  9. 干货!直击JVM底层 —— Java Class字节码文件解析
  10. map文件分析