@@ -50,7 +50,7 @@ def inet_pton(t, addr):
5050 return out_addr_p .raw
5151 socket .inet_pton = inet_pton
5252
53- _VERSION = '2.1 .0'
53+ _VERSION = '2.2 .0'
5454_NO_IP = 'MISSING IP ADDRESS'
5555_FIELD_NOT_SUPPORTED = 'NOT SUPPORTED'
5656_INVALID_IP_ADDRESS = 'INVALID IP ADDRESS'
@@ -303,6 +303,10 @@ def _reads(self, offset):
303303 n = struct .unpack ('B' , self ._f .read (1 ))[0 ]
304304 return u (self ._f .read (n ))
305305 # return self._f.read(n).decode('iso-8859-1').encode('utf-8')
306+ # if sys.version < '3':
307+ # return str(self._f.read(n).decode('iso-8859-1').encode('utf-8'))
308+ # else :
309+ # return u(self._f.read(n).decode('iso-8859-1').encode('utf-8')
306310
307311 def _readi (self , offset ):
308312 self ._f .seek (offset - 1 )
@@ -327,65 +331,69 @@ def _read_record(self, mid, ipv):
327331 if ipv == 4 :
328332 off = 0
329333 baseaddr = self ._ipv4dbaddr
334+ dbcolumn_width = self ._dbcolumn * 4 + 4
330335 elif ipv == 6 :
331336 off = 12
332337 baseaddr = self ._ipv6dbaddr
338+ dbcolumn_width = self ._dbcolumn * 4
333339
334340 rec .ip = self ._readips (baseaddr + (mid ) * self ._dbcolumn * 4 , ipv )
335341
336342 def calc_off (what , mid ):
337343 return baseaddr + mid * (self ._dbcolumn * 4 + off ) + off + 4 * (what [self ._dbtype ]- 1 )
338344
339- if _COUNTRY_POSITION [ self ._dbtype ] != 0 :
340- rec . country_short = self ._reads ( self . _readi ( calc_off ( _COUNTRY_POSITION , mid )) + 1 )
345+ self ._f . seek (( calc_off ( _PROXYTYPE_POSITION , mid )) - 1 )
346+ raw_positions_row = self ._f . read ( dbcolumn_width )
341347
342- rec .country_long = self ._reads (self ._readi (calc_off (_COUNTRY_POSITION , mid )) + 4 )
348+ if _COUNTRY_POSITION [self ._dbtype ] != 0 :
349+ rec .country_short = self ._reads (struct .unpack ('<I' , raw_positions_row [((_COUNTRY_POSITION [self ._dbtype ]- 1 ) * 4 - 4 ) : ((_COUNTRY_POSITION [self ._dbtype ]- 1 ) * 4 )])[0 ] + 1 )
350+ rec .country_long = self ._reads (struct .unpack ('<I' , raw_positions_row [((_COUNTRY_POSITION [self ._dbtype ]- 1 ) * 4 - 4 ) : ((_COUNTRY_POSITION [self ._dbtype ]- 1 ) * 4 )])[0 ] + 4 )
343351 elif _COUNTRY_POSITION [self ._dbtype ] == 0 :
344352 rec .country_short = _FIELD_NOT_SUPPORTED
345353 rec .country_long = _FIELD_NOT_SUPPORTED
346354
347355 if _REGION_POSITION [self ._dbtype ] != 0 :
348- rec .region = self ._reads (self ._readi ( calc_off (_REGION_POSITION , mid )) + 1 )
356+ rec .region = self ._reads (struct . unpack ( '<I' , raw_positions_row [(( _REGION_POSITION [ self ._dbtype ] - 1 ) * 4 - 4 ) : ( (_REGION_POSITION [ self . _dbtype ] - 1 ) * 4 )])[ 0 ] + 1 )
349357 elif _REGION_POSITION [self ._dbtype ] == 0 :
350358 rec .region = _FIELD_NOT_SUPPORTED
351359
352360 if _CITY_POSITION [self ._dbtype ] != 0 :
353- rec .city = self ._reads (self ._readi ( calc_off (_CITY_POSITION , mid )) + 1 )
361+ rec .city = self ._reads (struct . unpack ( '<I' , raw_positions_row [(( _CITY_POSITION [ self ._dbtype ] - 1 ) * 4 - 4 ) : ( (_CITY_POSITION [ self . _dbtype ] - 1 ) * 4 )])[ 0 ] + 1 )
354362 elif _CITY_POSITION [self ._dbtype ] == 0 :
355363 rec .city = _FIELD_NOT_SUPPORTED
356364
357365 if _ISP_POSITION [self ._dbtype ] != 0 :
358- rec .isp = self ._reads (self ._readi ( calc_off (_ISP_POSITION , mid )) + 1 )
366+ rec .isp = self ._reads (struct . unpack ( '<I' , raw_positions_row [(( _ISP_POSITION [ self ._dbtype ] - 1 ) * 4 - 4 ) : ( (_ISP_POSITION [ self . _dbtype ] - 1 ) * 4 )])[ 0 ] + 1 )
359367 elif _ISP_POSITION [self ._dbtype ] == 0 :
360368 rec .isp = _FIELD_NOT_SUPPORTED
361369
362370 if _PROXYTYPE_POSITION [self ._dbtype ] != 0 :
363- rec .proxy_type = self ._reads (self . _readi ( calc_off ( _PROXYTYPE_POSITION , mid )) + 1 )
371+ rec .proxy_type = self ._reads (struct . unpack ( '<I' , raw_positions_row [ 0 : (( _PROXYTYPE_POSITION [ self . _dbtype ] - 1 ) * 4 )])[ 0 ] + 1 )
364372 elif _PROXYTYPE_POSITION [self ._dbtype ] == 0 :
365373 rec .proxy_type = _FIELD_NOT_SUPPORTED
366374
367375 if _DOMAIN_POSITION [self ._dbtype ] != 0 :
368- rec .domain = self ._reads (self ._readi ( calc_off (_DOMAIN_POSITION , mid )) + 1 )
376+ rec .domain = self ._reads (struct . unpack ( '<I' , raw_positions_row [(( _DOMAIN_POSITION [ self ._dbtype ] - 1 ) * 4 - 4 ) : ( (_DOMAIN_POSITION [ self . _dbtype ] - 1 ) * 4 )])[ 0 ] + 1 )
369377 elif _DOMAIN_POSITION [self ._dbtype ] == 0 :
370378 rec .domain = _FIELD_NOT_SUPPORTED
371379
372380 if _USAGETYPE_POSITION [self ._dbtype ] != 0 :
373- rec .usage_type = self ._reads (self ._readi ( calc_off (_USAGETYPE_POSITION , mid )) + 1 )
381+ rec .usage_type = self ._reads (struct . unpack ( '<I' , raw_positions_row [(( _USAGETYPE_POSITION [ self ._dbtype ] - 1 ) * 4 - 4 ) : ( (_USAGETYPE_POSITION [ self . _dbtype ] - 1 ) * 4 )])[ 0 ] + 1 )
374382 elif _USAGETYPE_POSITION [self ._dbtype ] == 0 :
375383 rec .usage_type = _FIELD_NOT_SUPPORTED
376384
377385 if _ASN_POSITION [self ._dbtype ] != 0 :
378- rec .asn = self ._reads (self ._readi ( calc_off (_ASN_POSITION , mid )) + 1 )
386+ rec .asn = self ._reads (struct . unpack ( '<I' , raw_positions_row [(( _ASN_POSITION [ self ._dbtype ] - 1 ) * 4 - 4 ) : ( (_ASN_POSITION [ self . _dbtype ] - 1 ) * 4 )])[ 0 ] + 1 )
379387 elif _ASN_POSITION [self ._dbtype ] == 0 :
380388 rec .asn = _FIELD_NOT_SUPPORTED
381389
382390 if _AS_POSITION [self ._dbtype ] != 0 :
383- rec .as_name = self ._reads (self ._readi ( calc_off (_AS_POSITION , mid )) + 1 )
391+ rec .as_name = self ._reads (struct . unpack ( '<I' , raw_positions_row [(( _AS_POSITION [ self ._dbtype ] - 1 ) * 4 - 4 ) : ( (_AS_POSITION [ self . _dbtype ] - 1 ) * 4 )])[ 0 ] + 1 )
384392 elif _AS_POSITION [self ._dbtype ] == 0 :
385393 rec .as_name = _FIELD_NOT_SUPPORTED
386394
387395 if _LASTSEEN_POSITION [self ._dbtype ] != 0 :
388- rec .last_seen = self ._reads (self ._readi ( calc_off (_LASTSEEN_POSITION , mid )) + 1 )
396+ rec .last_seen = self ._reads (struct . unpack ( '<I' , raw_positions_row [(( _LASTSEEN_POSITION [ self ._dbtype ] - 1 ) * 4 - 4 ) : ( (_LASTSEEN_POSITION [ self . _dbtype ] - 1 ) * 4 )])[ 0 ] + 1 )
389397 elif _LASTSEEN_POSITION [self ._dbtype ] == 0 :
390398 rec .last_seen = _FIELD_NOT_SUPPORTED
391399
0 commit comments