读取纯真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 ...
随机推荐
- smarty -- foreach用法详解
{foreach},{foreachelse} 用于像访问序数数组一样访问关联数组 {foreach},{foreachelse} {foreach} is used to loop over an ...
- 腾讯自研万亿级消息中间件TubeMQ为什么要捐赠给Apache?
导语 | 近日,云+社区技术沙龙“腾讯开源技术”圆满落幕.本次沙龙邀请了多位腾讯技术专家围绕腾讯开源与各位开发者进行探讨,深度揭秘了腾讯开源项目TencentOS tiny.TubeMQ.Kona J ...
- Java对象头与锁
对象由多部分构成的,对象头,属性字段.补齐区域等.所谓补齐区域是指如果对象总大小不是4字节的整数倍,会填充上一段内存地址使之成为整数倍. 后面两个很好理解,今天我主要想总结一下对象头: 对象头这部分在 ...
- SCU 4438 Censor|KMP变形题
传送门 Censor frog is now a editor to censor so-called sensitive words (敏感词). She has a long text P. He ...
- Java面向对象之异常【一】
目录 Java面向对象之异常[一] 异常的继承体系 Error Exception 异常是否受检 unchecked exceptions(不受检异常) checked exceptions(受检异常 ...
- Sql Server学习笔记
1.指定路径创建数据库 create database student on--创建库的时候必须写 ( name=student, filename='E:\database\student.mdf' ...
- css3让元素自适应高度
知识点: viewport:可视窗口,也就是浏览器.vw Viewport宽度, 1vw 等于viewport宽度的1%vh Viewport高度, 1vh 等于viewport高的的1% calc( ...
- APP开通支付宝支付 转账功能
注意:支付宝单笔转账到账户功能的准入条件 首先要在商家中心注册,从商家中心可以跳转到开放平台. 登陆蚂蚁金服开放平台 1 开发中心->网页&移动应用 2 根据需求选择应用类型创建应用 3 ...
- P4550 收集邮票
P4550 收集邮票 题目描述 有n种不同的邮票,皮皮想收集所有种类的邮票.唯一的收集方法是到同学凡凡那里购买,每次只能买一张,并且买到的邮票究竟是n种邮票中的哪一种是等概率的,概率均为1/n.但是由 ...
- 【Linux】---Linux系统下各种常用命令总结
在Linux系统下,“万物皆文件”,之所以强调在强调这个概念,是因为很多人已经习惯了win系统下找找点点得那种方式和思维,因此总是会觉得linux系统下很多指令既复杂又难记.其实都是一样得东西,只是w ...