-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.go
158 lines (132 loc) · 3.52 KB
/
search.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package ipdb
import (
"encoding/binary"
"fmt"
"net"
"strings"
"unsafe"
)
// Search the database for the providing ip, returning the offset in the database
// referring to the data, or error.
func (d *IPDb) Search(ip net.IP) (uint32, error) {
nodeCount := d.Metadata.NodeCount
var iplen int
if d.IsIPv4Db() {
ip = ip.To4()
if ip == nil {
return 0, errIPv4Only
}
iplen = 32
} else {
if !d.IsIPv6Db() {
return 0, errDatabase
}
ip = ip.To16()
if ip == nil {
return 0, errIPv6Only
}
iplen = 128
}
offset := d.startFrom
for i := 0; i < iplen; i++ {
if offset >= nodeCount {
break
}
// To get exactly the ith bit
bit := 0xFF & ip[i>>3] >> uint(7-(i%8)) & 0x01
pos := offset*8 + uint32(bit*4)
offset = binary.BigEndian.Uint32(d.Data[pos : pos+4])
}
if offset < nodeCount {
return 0, errNoData
}
return offset, nil
}
// GetRaw returns the raw data by giving the offset, or error.
func (d *IPDb) GetRaw(offset uint32) ([]byte, error) {
if offset < d.Metadata.NodeCount {
return nil, errNoData
}
dbDataLen := uint32(len(d.Data))
// pos := d.Metadata.NodeCount*8 + (offset - d.Metadata.NodeCount)
pos := offset + d.Metadata.NodeCount*7
if pos >= dbDataLen {
return nil, errDatabase
}
len := binary.BigEndian.Uint16(d.Data[pos : pos+2])
if pos+2+uint32(len) > dbDataLen {
return nil, errDatabase
}
return d.Data[pos+2 : pos+2+uint32(len)], nil
}
// GetAllFields returns the data mapping by giving the offset, or error.
// BE AWARE: The return value stores the data in the form
// ``map[field]map[lang]data''.
func (d *IPDb) GetAllFields(offset uint32) (map[string]map[string]string, error) {
raw, err := d.GetRaw(offset)
if err != nil {
return nil, err
}
rawPtr := (*string)(unsafe.Pointer(&raw))
rawStrings := strings.Split(*rawPtr, "\t")
res := make(map[string]map[string]string)
for lang, pos := range d.Metadata.Languages {
if pos+len(d.Metadata.Fields) > len(rawStrings) {
return nil, errDatabase
}
for i, field := range d.Metadata.Fields {
if res[field] == nil {
res[field] = make(map[string]string)
}
res[field][lang] = rawStrings[pos+i]
}
}
return res, nil
}
// GetAllFieldsLocale returns the data mapping by giving the offset, in the
// providing locale ``lang'', or error.
func (d *IPDb) GetAllFieldsLocale(offset uint32, lang string) (map[string]string, error) {
raw, err := d.GetRaw(offset)
if err != nil {
return nil, err
}
rawPtr := (*string)(unsafe.Pointer(&raw))
rawStrings := strings.Split(*rawPtr, "\t")
res := make(map[string]string)
pos, ok := d.Metadata.Languages[lang]
if !ok {
return nil, fmt.Errorf("language %s not supported", lang)
}
if pos+len(d.Metadata.Fields) > len(rawStrings) {
return nil, errDatabase
}
for i, field := range d.Metadata.Fields {
res[field] = rawStrings[pos+i]
}
return res, nil
}
// GetValue returns the values in all locale, by giving offset and field, or error.
func (d *IPDb) GetValue(offset uint32, field string) (map[string]string, error) {
v, err := d.GetAllFields(offset)
if err != nil {
return nil, err
}
res, ok := v[field]
if ok {
return res, nil
}
return nil, fmt.Errorf("field %s not found", field)
}
// GetValueLocale returns the values in the providing locale ``lang'', by giving
// offset and field, or error.
func (d *IPDb) GetValueLocale(offset uint32, field, lang string) (string, error) {
v, err := d.GetAllFieldsLocale(offset, lang)
if err != nil {
return "", err
}
res, ok := v[field]
if ok {
return res, nil
}
return "", fmt.Errorf("field %s not found", field)
}