#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数据库的更多相关文章

  1. 优化读取纯真IP数据库QQWry.dat获取地区信息

    改自HeDaode 2007-12-28的代码 将之改为从硬盘读取后文件后,将MemoryStream放到内存中,提高后续查询速度 ///<summary> /// 提供从纯真IP数据库搜 ...

  2. PHP利用纯真IP数据库在本地实现IP地址信息查询

    https://blog.csdn.net/myweishanli/article/details/45098693 准备工作: 建议本地IP地址数据库,请到http://www.cz88.net/这 ...

  3. 纯真IP数据库导入mysql

    下载纯真IP数据库 安装后解压到本地为ip.txt 格式为: 1.1.145.0       1.1.147.255     泰国 沙功那空 1.1.148.0       1.1.149.255   ...

  4. python3通过纯真IP数据库查询IP归属地信息

    在网上看到的别人写的python2的代码,修改成了python3. 把纯真IP数据库文件qqwry.dat放到czip.py同一目录下. #! /usr/bin/env python # -*- co ...

  5. 纯真IP数据库(qqwry.dat)转换成最新的IP数据库格式(ipwry.dat)

    纯真IP数据库(qqwry.dat)转换成最新的IP数据库格式(ipwry.dat) 转载自:http://blog.cafeboy.org/2011/02/25/qqwry-to-ipwry/ ht ...

  6. PHP调用纯真IP数据库返回具体地址

    function convertip($ip) { $ip1num = 0; $ip2num = 0; $ipAddr1 =""; $ipAddr2 =""; ...

  7. PHP获取IP及地区信息(纯真IP数据库)

    昨天在写程序的时候,发现在用户的时候记录IP和地区信息也许以后用得上,去网上找了找,发现实现的方式有好多好多,因为我用的ThinkPHP,后来又去TP官网找了找,最后采用了下面这种方法. <?p ...

  8. 纯真IP数据库格式详解

    纯真版IP数据库,优点是记录多,查询速度快,它只用一个文件QQWry.dat就包含了所有记录,方便嵌入到其他程序中,也方便升级.缺点是你想要编辑它却是比较麻烦的,由于其文件格式的限制,你要直接添加IP ...

  9. 纯真IP数据库格式详解 附demo

    纯真版IP数据库,优点是记录多,查询速度快,它只用一个文件QQWry.dat就包含了所有记录,方便嵌入到其他程序中,也方便升级.缺点是你想要编辑它却是比较麻烦的,由于其文件格式的限制,你要直接添加IP ...

随机推荐

  1. 洛谷训练新手村之“BOSS战-入门综合练习1”题解

    P1478 陶陶摘苹果(升级版) 题目链接:https://www.luogu.com.cn/problem/P1478 题目大意:陶陶有s点体力值,每个苹果消耗体力值,问s体力值最多能摘多少苹果. ...

  2. 「P5004」专心OI - 跳房子 解题报告

    题面 把\(N\)个无色格子排成一行,选若干个格子染成黑色,要求每个黑色格子之间至少间隔\(M\)个格子,求方案数 思路: 矩阵加速 根据题面,这一题似乎可以用递推 设第\(i\)个格子的编号为\(i ...

  3. 1028 人口普查 (20 分)C语言

    题目描述 某城镇进行人口普查,得到了全体居民的生日.现请你写个程序,找出镇上最年长和最年轻的人. 这里确保每个输入的日期都是合法的,但不一定是合理的--假设已知镇上没有超过200岁的老人,而今天是20 ...

  4. 我与Git的那些破事--代码管理实践

    1. Git是什么? 作为一名程序猿,我相信大家都或多或少接触过git--分布式版本控制软件. 有人说,它是目前世界上最先进的分布式版本控制系统,我想说,是否最先进不知道,但确实好用,实用. 作为一款 ...

  5. 大量SQL的解决方案——sdmap

    大量SQL的解决方案--sdmap 最近看到群里面经常讨论大型应用中SQL的管理办法,有人说用EF/EF Core,但很多人不信任它生成SQL的语句:有人说用Dapper,但将SQL写到代码中有些人觉 ...

  6. 2013 ACM/ICPC Asia Regional Online —— Warmup2 ABEGKL

    HDU4716 A. A Computer Graphics Problem A题目描述 题意:输出手机剩余电量,保证给出的数是10的倍数. 题解:水题,按题意输出即可. 代码: #include & ...

  7. 低副瓣阵列天线综合1 matlab HFSS

    车载雷达天线多采用微带贴片天线,贴片振子的形状多种多样,较常用的是矩形: 组阵时多采用先串馈再把串馈好的行或列单元采取并馈的方式组阵,无论是串馈或并馈,想要获得较低的副瓣效果,都需要采取电流幅度加权的 ...

  8. ArcGIS Desktop直连PostgreSQL安装及配置图解(windows)

    目录 1 PostgreSQL 11.0安装及配置 2 psqlODBC安装及配置 3 PostGIS安装及配置 4 pgAdmin4使用入门 5 空间数据导入 5.1 将PostgreSQL的bin ...

  9. spring get方法 中文(UTF-8)乱码

    问题: 前端用Get方法进行如下请求: 在浏览器中输入:http://localhost:8080/dmaList/ExportBySQL?sql=&names=分区级别&size=1 ...

  10. Django之Session与Cookie

    目录 一.cookie Cookie的由来 什么是Cookie Cookie的原理 查看Cookie cookie与session的作用 二.Django中操作Cookie 获取Cookie 设置Co ...