读取纯真IP数据库
#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
读取纯真IP数据库的更多相关文章
- 优化读取纯真IP数据库QQWry.dat获取地区信息
改自HeDaode 2007-12-28的代码 将之改为从硬盘读取后文件后,将MemoryStream放到内存中,提高后续查询速度 ///<summary> /// 提供从纯真IP数据库搜 ...
- PHP利用纯真IP数据库在本地实现IP地址信息查询
https://blog.csdn.net/myweishanli/article/details/45098693 准备工作: 建议本地IP地址数据库,请到http://www.cz88.net/这 ...
- 纯真IP数据库导入mysql
下载纯真IP数据库 安装后解压到本地为ip.txt 格式为: 1.1.145.0 1.1.147.255 泰国 沙功那空 1.1.148.0 1.1.149.255 ...
- python3通过纯真IP数据库查询IP归属地信息
在网上看到的别人写的python2的代码,修改成了python3. 把纯真IP数据库文件qqwry.dat放到czip.py同一目录下. #! /usr/bin/env python # -*- co ...
- 纯真IP数据库(qqwry.dat)转换成最新的IP数据库格式(ipwry.dat)
纯真IP数据库(qqwry.dat)转换成最新的IP数据库格式(ipwry.dat) 转载自:http://blog.cafeboy.org/2011/02/25/qqwry-to-ipwry/ ht ...
- PHP调用纯真IP数据库返回具体地址
function convertip($ip) { $ip1num = 0; $ip2num = 0; $ipAddr1 =""; $ipAddr2 =""; ...
- PHP获取IP及地区信息(纯真IP数据库)
昨天在写程序的时候,发现在用户的时候记录IP和地区信息也许以后用得上,去网上找了找,发现实现的方式有好多好多,因为我用的ThinkPHP,后来又去TP官网找了找,最后采用了下面这种方法. <?p ...
- 纯真IP数据库格式详解
纯真版IP数据库,优点是记录多,查询速度快,它只用一个文件QQWry.dat就包含了所有记录,方便嵌入到其他程序中,也方便升级.缺点是你想要编辑它却是比较麻烦的,由于其文件格式的限制,你要直接添加IP ...
- 纯真IP数据库格式详解 附demo
纯真版IP数据库,优点是记录多,查询速度快,它只用一个文件QQWry.dat就包含了所有记录,方便嵌入到其他程序中,也方便升级.缺点是你想要编辑它却是比较麻烦的,由于其文件格式的限制,你要直接添加IP ...
随机推荐
- apache相关实验-1
一.目录别名实验 当 apache 接受请求时,在默认情况下会将 DocumentRoot 目录中的文件送到客户端,如果想将某一不在 DocumentRoot 目录中的文件共享到网站上,并希望将它们留 ...
- 03_常用的JS正则表达式54种形式类型
1.由数字.26个英文字母或者下划线组成的字符串: ^[0-9a-zA-Z_]{1,}$ 2.非负整数(正整数 + 0 ): ^/d+$ 3. 正整数: ^[0-9]*[1-9][0-9]*$ 4.非 ...
- day4(dict和set)
dict 内置字典,使用key-value存储,具有极快的查找速度. >>>d = {'michael': 95, 'bob': 75, 'tracy': 85} >>& ...
- 二、Spring Cloud之注册中心 Eureka
前言 算是正式开始学习 spring cloud 的项目知识了,大概的知道Springcloud 是由众多的微服务组成的,所以我们现在一个一个的来学习吧. 注册中心,在微服务中算是核心了.所有的服务都 ...
- MySQL数据库用户、角色、授权
权限包括 insert delete update select all privileges 登录MySQL > mysql -uroot -p Enter password ...
- Jquery图片上传功能整理
最近在做一个图片上传到服务器的功能,之前基本没有什么JS的经验,用的也是网上的插件.做了一个星期才把他弄好,现在做一下总结,方便以后查看. 用的插件是WebUploader,上面有很多例子,我找的例子 ...
- Serverless 微服务实践-移动应用包分发服务
背景 阿里云函数计算是事件驱动的全托管计算服务.通过函数计算,您无需管理服务器等基础设施,只需编写代码并上传.函数计算会为您准备好计算资源,以弹性.可靠的方式运行您的代码,并提供日志查询.性能监控.报 ...
- 为什么双击打开py文件时窗口瞬间关闭了?
当前理解,py文件里没有input() 等暂停程序运行的函数,程序运行速度太快,运行完就立马关闭了. input()调用后,程序会立即暂停,等待用户输入
- 幽灵java进程引起的: FATAL ERROR in native method
FATAL ERROR in native method: JDWP No transports initialized, jvmtiError=AGENT_ERROR_TRANSPORT_INIT( ...
- python 线程池实用总结
线程池的两张方法 submit 和map from concurrent.futures import ThreadPoolExecutor import time # def sayhello(a) ...