1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <iostream>
  3. #include <string>
  4. #include <stdlib.h>
  5. #include <windows.h>
  6. using namespace std;
  7.  
  8. char *ptr = NULL; //ptr of image//内存映射文件
  9. char *p = NULL; //point to index//索引区
  10. unsigned int total; //ip count
  11.  
  12. inline void Load(void)
  13. {
  14. HANDLE hnd; //file handle
  15. DWORD NumberOfBytesRead; //len
  16. char text[2048]; //patch
  17. char *temp;
  18. unsigned int len;
  19.  
  20. //get patch
  21. if( !GetModuleFileName(0, text, 2048))
  22. return;
  23.  
  24. temp = strrchr(text, 92); // 92 = '\'
  25. *(temp + 1) = NULL;
  26. strcat(temp, "QQwry.dat");
  27.  
  28. //CreateFile
  29. hnd = CreateFile(text, GENERIC_READ, FILE_SHARE_READ, NULL,
  30. OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  31. if(INVALID_HANDLE_VALUE == hnd)
  32. {
  33. ::MessageBox(NULL, text, "不能打开文件!", NULL);
  34. return;
  35. }
  36.  
  37. //get len
  38. len = SetFilePointer(hnd, NULL, NULL, FILE_END);
  39. SetFilePointer(hnd, NULL, NULL, FILE_BEGIN);
  40.  
  41. //malloc
  42. ptr = (char*)malloc(len + 9);
  43. if(!ptr)
  44. {
  45. CloseHandle(hnd);
  46. ::MessageBox(NULL, "不能分配内存!", NULL, NULL);
  47. return;
  48. }
  49.  
  50. //read
  51. if(!ReadFile(hnd, ptr, len, &NumberOfBytesRead, NULL))
  52. {
  53. CloseHandle(hnd);
  54. free(ptr);
  55. ::MessageBox(NULL, text, "不能读入文件!", NULL);
  56. return;
  57. }
  58. CloseHandle(hnd);
  59.  
  60. //calc total - 1
  61. total = (*((unsigned int*)ptr + 1) - *(unsigned int*)ptr);
  62.  
  63. //check file
  64. if(total % 7 != 0)
  65. {
  66. free(ptr);
  67. ::MessageBox(NULL, text, "QQwry.dat文件有损坏!", NULL);
  68. return;
  69. }
  70.  
  71. total /= 7;
  72. ++total;
  73. p = ptr + *(unsigned int*)ptr; //ptr of index area
  74. }
  75.  
  76. inline unsigned int get_3b(const char *mem)
  77. {
  78. return 0x00ffffff & *(unsigned int*)(mem);
  79. }
  80.  
  81. inline unsigned int str2ip(const char *lp)
  82. {
  83. unsigned int iIP = 0;
  84. unsigned int tmpIP = 0;
  85.  
  86. while(*lp)
  87. {
  88. if('.' == *lp)
  89. {
  90. iIP = 256 * iIP + tmpIP;
  91. tmpIP = 0;
  92. }
  93. else
  94. {
  95. tmpIP = 10 * tmpIP + *lp - '0';
  96. }
  97. ++lp;
  98. }
  99.  
  100. iIP = 256 * iIP + tmpIP;
  101. return iIP;
  102. }
  103.  
  104. string _GetAddress(string IPstr)
  105. {
  106. string ret;
  107. if(NULL == p)
  108. {
  109. ret = "";
  110. return ret;
  111. }
  112.  
  113. unsigned int ip = str2ip(IPstr.c_str());
  114. char *now_p;
  115.  
  116. unsigned int begin = 0, end = total;
  117. while(true)
  118. {
  119. if( begin >= end - 1 )
  120. {
  121. break;
  122. }
  123. if( ip < *(unsigned int*)(p + (begin + end)/2 * 7))
  124. {
  125. end = (begin + end) / 2;
  126. }
  127. else
  128. {
  129. begin = (begin + end) / 2;
  130. }
  131. }
  132.  
  133. unsigned int temp = get_3b(p + 7 * begin + 4);
  134. if(ip <= *(unsigned int*)(ptr + temp)) //ok, found
  135. {
  136. now_p = ptr + temp + 4;
  137. if( 0x01 == *now_p ) //如果0x01跳过去找国家
  138. {
  139. now_p = ptr + get_3b(now_p + 1);
  140. }
  141. //country
  142. if( 0x02 == *now_p ) //如果国家0x02再跳
  143. {
  144. ret = ptr + get_3b(now_p + 1);
  145. now_p += 4;
  146. }
  147. else
  148. {
  149. ret = now_p;
  150. for(; *now_p; ++now_p)
  151. ;
  152. ++now_p;
  153. }
  154. //local
  155. if( 0x02 == *now_p ) //找到国家以后还发现0x02跳过去找地区
  156. {
  157. ret += ptr + get_3b(now_p + 1);
  158. }
  159. else
  160. {
  161. ret += now_p;
  162. }
  163. }
  164. else
  165. {
  166. ret = "未知数据";
  167. }
  168. return ret;
  169. }
  170.  
  171. int main(void)
  172. {
  173. Load();
  174. string ip;
  175. while (cin >> ip)
  176. {
  177. cout << _GetAddress(ip) << endl;
  178. }
  179. return 0;
  180. }

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. smarty -- foreach用法详解

    {foreach},{foreachelse} 用于像访问序数数组一样访问关联数组 {foreach},{foreachelse} {foreach} is used to loop over an  ...

  2. 腾讯自研万亿级消息中间件TubeMQ为什么要捐赠给Apache?

    导语 | 近日,云+社区技术沙龙“腾讯开源技术”圆满落幕.本次沙龙邀请了多位腾讯技术专家围绕腾讯开源与各位开发者进行探讨,深度揭秘了腾讯开源项目TencentOS tiny.TubeMQ.Kona J ...

  3. Java对象头与锁

    对象由多部分构成的,对象头,属性字段.补齐区域等.所谓补齐区域是指如果对象总大小不是4字节的整数倍,会填充上一段内存地址使之成为整数倍. 后面两个很好理解,今天我主要想总结一下对象头: 对象头这部分在 ...

  4. SCU 4438 Censor|KMP变形题

    传送门 Censor frog is now a editor to censor so-called sensitive words (敏感词). She has a long text P. He ...

  5. Java面向对象之异常【一】

    目录 Java面向对象之异常[一] 异常的继承体系 Error Exception 异常是否受检 unchecked exceptions(不受检异常) checked exceptions(受检异常 ...

  6. Sql Server学习笔记

    1.指定路径创建数据库 create database student on--创建库的时候必须写 ( name=student, filename='E:\database\student.mdf' ...

  7. css3让元素自适应高度

    知识点: viewport:可视窗口,也就是浏览器.vw Viewport宽度, 1vw 等于viewport宽度的1%vh Viewport高度, 1vh 等于viewport高的的1% calc( ...

  8. APP开通支付宝支付 转账功能

    注意:支付宝单笔转账到账户功能的准入条件 首先要在商家中心注册,从商家中心可以跳转到开放平台. 登陆蚂蚁金服开放平台 1 开发中心->网页&移动应用 2 根据需求选择应用类型创建应用 3 ...

  9. P4550 收集邮票

    P4550 收集邮票 题目描述 有n种不同的邮票,皮皮想收集所有种类的邮票.唯一的收集方法是到同学凡凡那里购买,每次只能买一张,并且买到的邮票究竟是n种邮票中的哪一种是等概率的,概率均为1/n.但是由 ...

  10. 【Linux】---Linux系统下各种常用命令总结

    在Linux系统下,“万物皆文件”,之所以强调在强调这个概念,是因为很多人已经习惯了win系统下找找点点得那种方式和思维,因此总是会觉得linux系统下很多指令既复杂又难记.其实都是一样得东西,只是w ...