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 +4 -4
- data/lib/maxminddb.rb +27 -22
- data/lib/maxminddb/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8637e3f4ecc8e4f58d5c8af760d7e2a2ead66a03
|
4
|
+
data.tar.gz: ef77114d3b291de887419089610ae7dcba866661
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b05b703f559465f178bcfa6d5ad37a6a5a2a166cfbfcebb79380e95d52f974a9793fa4957a3c238780823816b15d45ab46b548de6c97904df9b9f220ba0a771f
|
7
|
+
data.tar.gz: 505663ceb677df3deb1702f8984ac01c35b17a25c6f42eda0dcdde4eb3c4254f14ba1a1ec8d61681bba0cf7328bbad58ef86712879044d8106ec671e70bbb24f
|
data/lib/maxminddb.rb
CHANGED
@@ -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
|
-
@
|
26
|
-
@
|
27
|
-
@
|
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 >= @
|
43
|
-
data_section_start = @
|
44
|
-
pos = (next_node_no - @
|
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
|
-
|
57
|
-
|
58
|
-
|
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)
|
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)
|
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].
|
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
|
-
|
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].
|
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
|
-
|
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
|
-
|
109
|
+
val = read_value(pos, base_pos, size)
|
110
|
+
pos += size
|
108
111
|
when 6 # unsigned 32-bit int
|
109
|
-
|
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
|
-
|
127
|
+
val = read_value(pos, base_pos, size)
|
128
|
+
pos += size
|
124
129
|
when 10 # unsigned 128-bit int
|
125
|
-
|
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
|
-
|
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)
|
data/lib/maxminddb/version.rb
CHANGED
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.
|
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-
|
11
|
+
date: 2016-02-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|