maxminddb 0.1.9 → 0.1.10

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2b881c09e148380d6bc204590f4a0393e4409ce9
4
- data.tar.gz: fcd4038055f584dc9d406fc16cdf63e5b8a96ab8
3
+ metadata.gz: 8637e3f4ecc8e4f58d5c8af760d7e2a2ead66a03
4
+ data.tar.gz: ef77114d3b291de887419089610ae7dcba866661
5
5
  SHA512:
6
- metadata.gz: 18b101858b0a7727d14bf4c8f3dbf76398b65d302e56ed2da6b345b9a1693613af7add1c9e179625c38e9f5a75388ba7b828dad4079fe1df603c312e68709cc0
7
- data.tar.gz: 518820cf5a7c61dee2e42d78621a7645ae1738aca5cad4a125d9b0f2d2660f21c2530baf111fb3500117b5282c635f0d1d7edcd2b28c483649b2b1a1911714b3
6
+ metadata.gz: b05b703f559465f178bcfa6d5ad37a6a5a2a166cfbfcebb79380e95d52f974a9793fa4957a3c238780823816b15d45ab46b548de6c97904df9b9f220ba0a771f
7
+ data.tar.gz: 505663ceb677df3deb1702f8984ac01c35b17a25c6f42eda0dcdde4eb3c4254f14ba1a1ec8d61681bba0cf7328bbad58ef86712879044d8106ec671e70bbb24f
@@ -21,10 +21,11 @@ module MaxMindDB
21
21
  pos = @data.rindex(METADATA_BEGIN_MARKER)
22
22
  raise 'invalid file format' unless pos
23
23
  pos += METADATA_BEGIN_MARKER.size
24
+ metadata = decode(pos, 0)[1]
24
25
 
25
- @metadata = decode(pos, 0)[1]
26
- @metadata['node_byte_size'] = @metadata['record_size'] * 2 / 8
27
- @metadata['search_tree_size'] = @metadata['node_count'] * @metadata['node_byte_size']
26
+ @node_count = metadata['node_count']
27
+ @node_byte_size = metadata['record_size'] * 2 / 8
28
+ @search_tree_size = @node_count * @node_byte_size
28
29
  end
29
30
 
30
31
  def inspect
@@ -39,9 +40,9 @@ module MaxMindDB
39
40
  next_node_no = read_record(node_no, flag)
40
41
  if next_node_no == 0
41
42
  raise 'invalid file format'
42
- elsif next_node_no >= @metadata['node_count']
43
- data_section_start = @metadata['search_tree_size'] + DATA_SECTION_SEPARATOR_SIZE;
44
- pos = (next_node_no - @metadata['node_count']) - DATA_SECTION_SEPARATOR_SIZE
43
+ elsif next_node_no >= @node_count
44
+ data_section_start = @search_tree_size + DATA_SECTION_SEPARATOR_SIZE
45
+ pos = (next_node_no - @node_count) - DATA_SECTION_SEPARATOR_SIZE
45
46
  return MaxMindDB::Result.new(decode(pos, data_section_start)[1])
46
47
  else
47
48
  node_no = next_node_no
@@ -53,22 +54,21 @@ module MaxMindDB
53
54
  private
54
55
 
55
56
  def read_record(node_no, flag)
56
- node_byte_size = @metadata['node_byte_size']
57
- rec_byte_size = node_byte_size / 2
58
- pos = node_byte_size * node_no
59
- middle = @data[pos + rec_byte_size, 1].unpack('C')[0] if node_byte_size.odd?
57
+ rec_byte_size = @node_byte_size / 2
58
+ pos = @node_byte_size * node_no
59
+ middle = @data[pos + rec_byte_size].ord if @node_byte_size.odd?
60
60
  if flag == 0 # left
61
- val = read_value(pos, 0, rec_byte_size)[1]
61
+ val = read_value(pos, 0, rec_byte_size)
62
62
  val += ((middle & 0xf0) << 20) if middle
63
63
  else # right
64
- val = read_value(pos + node_byte_size - rec_byte_size, 0, rec_byte_size)[1]
64
+ val = read_value(pos + @node_byte_size - rec_byte_size, 0, rec_byte_size)
65
65
  val += ((middle & 0xf) << 24) if middle
66
66
  end
67
67
  val
68
68
  end
69
69
 
70
70
  def decode(pos, base_pos)
71
- ctrl = @data[pos + base_pos].unpack('C')[0]
71
+ ctrl = @data[pos + base_pos].ord
72
72
  pos += 1
73
73
 
74
74
  type = ctrl >> 5
@@ -76,20 +76,22 @@ module MaxMindDB
76
76
  if type == 1 # pointer
77
77
  size = ((ctrl >> 3) & 0x3) + 1
78
78
  v1 = ctrl & 0x7
79
- pos, v2 = read_value(pos, base_pos, size)
79
+ v2 = read_value(pos, base_pos, size)
80
+ pos += size
80
81
 
81
82
  pointer = (v1 << (8 * size)) + v2 + POINTER_BASE_VALUES[size]
82
83
  val = decode(pointer, base_pos)[1]
83
84
  else
84
85
  if type == 0 # extended type
85
- type = 7 + @data[pos + base_pos].unpack('C')[0]
86
+ type = 7 + @data[pos + base_pos].ord
86
87
  pos += 1
87
88
  end
88
89
 
89
90
  size = ctrl & 0x1f
90
91
  if size >= 29
91
92
  byte_size = size - 29 + 1
92
- pos, val = read_value(pos, base_pos, byte_size)
93
+ val = read_value(pos, base_pos, byte_size)
94
+ pos += byte_size
93
95
  size = val + SIZE_BASE_VALUES[byte_size]
94
96
  end
95
97
 
@@ -104,9 +106,11 @@ module MaxMindDB
104
106
  val = @data[pos + base_pos, size]
105
107
  pos += size
106
108
  when 5 # unsigned 16-bit int
107
- pos, val = read_value(pos, base_pos, size)
109
+ val = read_value(pos, base_pos, size)
110
+ pos += size
108
111
  when 6 # unsigned 32-bit int
109
- pos, val = read_value(pos, base_pos, size)
112
+ val = read_value(pos, base_pos, size)
113
+ pos += size
110
114
  when 7 # map
111
115
  val = {}
112
116
  size.times do
@@ -120,9 +124,11 @@ module MaxMindDB
120
124
  val = (v1 & ~(1 << bits)) - (v1 & (1 << bits))
121
125
  pos += size
122
126
  when 9 # unsigned 64-bit int
123
- pos, val = read_value(pos, base_pos, size)
127
+ val = read_value(pos, base_pos, size)
128
+ pos += size
124
129
  when 10 # unsigned 128-bit int
125
- pos, val = read_value(pos, base_pos, size)
130
+ val = read_value(pos, base_pos, size)
131
+ pos += size
126
132
  when 11 # array
127
133
  val = []
128
134
  size.times do
@@ -146,8 +152,7 @@ module MaxMindDB
146
152
 
147
153
  def read_value(pos, base_pos, size)
148
154
  bytes = @data[pos + base_pos, size].unpack('C*')
149
- val = bytes.inject(0){|r, v| (r << 8) + v }
150
- [pos + size, val]
155
+ bytes.inject(0){|r, v| (r << 8) + v }
151
156
  end
152
157
 
153
158
  def addr_from_ip(ip)
@@ -1,3 +1,3 @@
1
1
  module MaxMindDB
2
- VERSION = "0.1.9"
2
+ VERSION = "0.1.10"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: maxminddb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.9
4
+ version: 0.1.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - yhirose
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-02-21 00:00:00.000000000 Z
11
+ date: 2016-02-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler