forked from IncSW/geoip2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.go
377 lines (360 loc) · 11.3 KB
/
common.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package geoip2
import (
"encoding/binary"
"errors"
"math"
"strconv"
"unsafe"
)
func readControl(buffer []byte, offset uint) (byte, uint, uint, error) {
controlByte := buffer[offset]
offset++
dataType := controlByte >> 5
if dataType == dataTypeExtended {
dataType = buffer[offset] + 7
offset++
}
size := uint(controlByte & 0x1f)
if dataType == dataTypeExtended || size < 29 {
return dataType, size, offset, nil
}
bytesToRead := size - 28
newOffset := offset + bytesToRead
if newOffset > uint(len(buffer)) {
return 0, 0, 0, errors.New("invalid offset")
}
size = uint(bytesToUInt64(buffer[offset:newOffset]))
switch bytesToRead {
case 1:
size += 29
case 2:
size += 285
default:
size += 65821
}
return dataType, size, newOffset, nil
}
func readPointer(buffer []byte, size uint, offset uint) (uint, uint, error) {
pointerSize := ((size >> 3) & 0x3) + 1
newOffset := offset + pointerSize
if newOffset > uint(len(buffer)) {
return 0, 0, errors.New("invalid offset")
}
prefix := uint64(0)
if pointerSize != 4 {
prefix = uint64(size) & 0x7
}
unpacked := uint(bytesToUInt64WithPrefix(prefix, buffer[offset:newOffset]))
pointerValueOffset := uint(0)
switch pointerSize {
case 2:
pointerValueOffset = 2048
case 3:
pointerValueOffset = 526336
case 4:
pointerValueOffset = 0
}
return unpacked + pointerValueOffset, newOffset, nil
}
func readFloat64(buffer []byte, offset uint) (float64, uint, error) {
dataType, size, offset, err := readControl(buffer, offset)
if err != nil {
return 0, 0, err
}
switch dataType {
case dataTypeFloat64:
newOffset := offset + size
return bytesToFloat64(buffer[offset:newOffset]), newOffset, nil
case dataTypePointer:
pointer, newOffset, err := readPointer(buffer, size, offset)
if err != nil {
return 0, 0, err
}
dataType, size, offset, err := readControl(buffer, pointer)
if err != nil {
return 0, 0, err
}
if dataType != dataTypeFloat64 {
return 0, 0, errors.New("invalid float64 pointer type: " + strconv.Itoa(int(dataType)))
}
return bytesToFloat64(buffer[offset : offset+size]), newOffset, nil
default:
return 0, 0, errors.New("invalid float64 type: " + strconv.Itoa(int(dataType)))
}
}
func readUInt16(buffer []byte, offset uint) (uint16, uint, error) {
dataType, size, offset, err := readControl(buffer, offset)
if err != nil {
return 0, 0, err
}
switch dataType {
case dataTypeUint16:
newOffset := offset + size
return uint16(bytesToUInt64(buffer[offset:newOffset])), newOffset, nil
case dataTypePointer:
pointer, newOffset, err := readPointer(buffer, size, offset)
if err != nil {
return 0, 0, err
}
dataType, size, offset, err := readControl(buffer, pointer)
if err != nil {
return 0, 0, err
}
if dataType != dataTypeUint16 {
return 0, 0, errors.New("invalid uint16 pointer type: " + strconv.Itoa(int(dataType)))
}
return uint16(bytesToUInt64(buffer[offset : offset+size])), newOffset, nil
default:
return 0, 0, errors.New("invalid uint16 type: " + strconv.Itoa(int(dataType)))
}
}
func readUInt32(buffer []byte, offset uint) (uint32, uint, error) {
dataType, size, offset, err := readControl(buffer, offset)
if err != nil {
return 0, 0, err
}
switch dataType {
case dataTypeUint32:
newOffset := offset + size
return uint32(bytesToUInt64(buffer[offset:newOffset])), newOffset, nil
case dataTypePointer:
pointer, newOffset, err := readPointer(buffer, size, offset)
if err != nil {
return 0, 0, err
}
dataType, size, offset, err := readControl(buffer, pointer)
if err != nil {
return 0, 0, err
}
if dataType != dataTypeUint32 {
return 0, 0, errors.New("invalid uint32 pointer type: " + strconv.Itoa(int(dataType)))
}
return uint32(bytesToUInt64(buffer[offset : offset+size])), newOffset, nil
default:
return 0, 0, errors.New("invalid uint32 type: " + strconv.Itoa(int(dataType)))
}
}
func readBool(buffer []byte, offset uint) (bool, uint, error) {
dataType, size, offset, err := readControl(buffer, offset)
if err != nil {
return false, 0, err
}
switch dataType {
case dataTypeBool:
return size != 0, offset, nil
case dataTypePointer:
pointer, newOffset, err := readPointer(buffer, size, offset)
if err != nil {
return false, 0, err
}
dataType, size, _, err := readControl(buffer, pointer)
if err != nil {
return false, 0, err
}
if dataType != dataTypeBool {
return false, 0, errors.New("invalid bool pointer type: " + strconv.Itoa(int(dataType)))
}
return size != 0, newOffset, nil
default:
return false, 0, errors.New("invalid bool type: " + strconv.Itoa(int(dataType)))
}
}
func readString(buffer []byte, offset uint) (string, uint, error) {
dataType, size, offset, err := readControl(buffer, offset)
if err != nil {
return "", 0, err
}
switch dataType {
case dataTypeString:
newOffset := offset + size
return b2s(buffer[offset:newOffset]), newOffset, nil
case dataTypePointer:
pointer, newOffset, err := readPointer(buffer, size, offset)
if err != nil {
return "", 0, err
}
dataType, size, offset, err := readControl(buffer, pointer)
if err != nil {
return "", 0, err
}
if dataType != dataTypeString {
return "", 0, errors.New("invalid string pointer type: " + strconv.Itoa(int(dataType)))
}
return b2s(buffer[offset : offset+size]), newOffset, nil
default:
return "", 0, errors.New("invalid string type: " + strconv.Itoa(int(dataType)))
}
}
func readStringMap(buffer []byte, offset uint) (map[string]string, uint, error) {
dataType, size, offset, err := readControl(buffer, offset)
if err != nil {
return nil, 0, err
}
switch dataType {
case dataTypeMap:
return readStringMapMap(buffer, size, offset)
case dataTypePointer:
pointer, newOffset, err := readPointer(buffer, size, offset)
if err != nil {
return nil, 0, err
}
dataType, size, offset, err := readControl(buffer, pointer)
if err != nil {
return nil, 0, err
}
if dataType != dataTypeMap {
return nil, 0, errors.New("invalid stringMap pointer type: " + strconv.Itoa(int(dataType)))
}
value, _, err := readStringMapMap(buffer, size, offset)
if err != nil {
return nil, 0, err
}
return value, newOffset, nil
default:
return nil, 0, errors.New("invalid stringMap type: " + strconv.Itoa(int(dataType)))
}
}
func readStringMapMap(buffer []byte, mapSize uint, offset uint) (map[string]string, uint, error) {
var key []byte
var err error
var dataType byte
var size uint
result := map[string]string{}
for i := uint(0); i < mapSize; i++ {
key, offset, err = readMapKey(buffer, offset)
if err != nil {
return nil, 0, err
}
dataType, size, offset, err = readControl(buffer, offset)
if err != nil {
return nil, 0, err
}
switch dataType {
case dataTypePointer:
pointer, newOffset, err := readPointer(buffer, size, offset)
if err != nil {
return nil, 0, err
}
dataType, size, valueOffset, err := readControl(buffer, pointer)
if err != nil {
return nil, 0, err
}
if dataType != dataTypeString {
return nil, 0, errors.New("map key must be a string, got: " + strconv.Itoa(int(dataType)))
}
offset = newOffset
result[b2s(key)] = b2s(buffer[valueOffset : valueOffset+size])
case dataTypeString:
newOffset := offset + size
value := b2s(buffer[offset:newOffset])
offset = newOffset
result[b2s(key)] = value
default:
return nil, 0, errors.New("invalid data type of key " + string(key) + ": " + strconv.Itoa(int(dataType)))
}
}
return result, offset, nil
}
func readMapKey(buffer []byte, offset uint) ([]byte, uint, error) {
dataType, size, offset, err := readControl(buffer, offset)
if err != nil {
return nil, 0, err
}
if dataType == dataTypePointer {
pointer, newOffset, err := readPointer(buffer, size, offset)
if err != nil {
return nil, 0, err
}
dataType, size, offset, err := readControl(buffer, pointer)
if err != nil {
return nil, 0, err
}
if dataType != dataTypeString {
return nil, 0, errors.New("map key must be a string, got: " + strconv.Itoa(int(dataType)))
}
return buffer[offset : offset+size], newOffset, nil
}
if dataType != dataTypeString {
return nil, 0, errors.New("map key must be a string, got: " + strconv.Itoa(int(dataType)))
}
newOffset := offset + size
if newOffset > uint(len(buffer)) {
return nil, 0, errors.New("invalid offset")
}
return buffer[offset:newOffset], newOffset, nil
}
func readStringSlice(buffer []byte, sliceSize uint, offset uint) ([]string, uint, error) {
var err error
var dataType byte
var size uint
result := make([]string, sliceSize)
for i := uint(0); i < sliceSize; i++ {
dataType, size, offset, err = readControl(buffer, offset)
if err != nil {
return nil, 0, err
}
if dataType != dataTypeString {
return nil, 0, errors.New("invalid data type: " + strconv.Itoa(int(dataType)))
}
newOffset := offset + size
value := b2s(buffer[offset:newOffset])
offset = newOffset
result[i] = value
}
return result, offset, nil
}
func bytesToUInt64(buffer []byte) uint64 {
switch len(buffer) {
case 1:
return uint64(buffer[0])
case 2:
return uint64(buffer[0])<<8 | uint64(buffer[1])
case 3:
return (uint64(buffer[0])<<8|uint64(buffer[1]))<<8 | uint64(buffer[2])
case 4:
return ((uint64(buffer[0])<<8|uint64(buffer[1]))<<8|uint64(buffer[2]))<<8 | uint64(buffer[3])
case 5:
return (((uint64(buffer[0])<<8|uint64(buffer[1]))<<8|uint64(buffer[2]))<<8|uint64(buffer[3]))<<8 | uint64(buffer[4])
case 6:
return ((((uint64(buffer[0])<<8|uint64(buffer[1]))<<8|uint64(buffer[2]))<<8|uint64(buffer[3]))<<8|uint64(buffer[4]))<<8 | uint64(buffer[5])
case 7:
return (((((uint64(buffer[0])<<8|uint64(buffer[1]))<<8|uint64(buffer[2]))<<8|uint64(buffer[3]))<<8|uint64(buffer[4]))<<8|uint64(buffer[5]))<<8 | uint64(buffer[6])
case 8:
return ((((((uint64(buffer[0])<<8|uint64(buffer[1]))<<8|uint64(buffer[2]))<<8|uint64(buffer[3]))<<8|uint64(buffer[4]))<<8|uint64(buffer[5]))<<8|uint64(buffer[6]))<<8 | uint64(buffer[7])
}
return 0
}
func bytesToUInt64WithPrefix(prefix uint64, buffer []byte) uint64 {
switch len(buffer) {
case 0:
return prefix
case 1:
return prefix<<8 | uint64(buffer[0])
case 2:
return (prefix<<8|uint64(buffer[0]))<<8 | uint64(buffer[1])
case 3:
return ((prefix<<8|uint64(buffer[0]))<<8|uint64(buffer[1]))<<8 | uint64(buffer[2])
case 4:
return (((prefix<<8|uint64(buffer[0]))<<8|uint64(buffer[1]))<<8|uint64(buffer[2]))<<8 | uint64(buffer[3])
case 5:
return ((((prefix<<8|uint64(buffer[0]))<<8|uint64(buffer[1]))<<8|uint64(buffer[2]))<<8|uint64(buffer[3]))<<8 | uint64(buffer[4])
case 6:
return (((((prefix<<8|uint64(buffer[0]))<<8|uint64(buffer[1]))<<8|uint64(buffer[2]))<<8|uint64(buffer[3]))<<8|uint64(buffer[4]))<<8 | uint64(buffer[5])
case 7:
return ((((((prefix<<8|uint64(buffer[0]))<<8|uint64(buffer[1]))<<8|uint64(buffer[2]))<<8|uint64(buffer[3]))<<8|uint64(buffer[4]))<<8|uint64(buffer[5]))<<8 | uint64(buffer[6])
case 8:
return (((((((prefix<<8|uint64(buffer[0]))<<8|uint64(buffer[1]))<<8|uint64(buffer[2]))<<8|uint64(buffer[3]))<<8|uint64(buffer[4]))<<8|uint64(buffer[5]))<<8|uint64(buffer[6]))<<8 | uint64(buffer[7])
}
return 0
}
func bytesToFloat32(buffer []byte) float32 {
bits := binary.BigEndian.Uint32(buffer)
return math.Float32frombits(bits)
}
func bytesToFloat64(buffer []byte) float64 {
bits := binary.BigEndian.Uint64(buffer)
return math.Float64frombits(bits)
}
func b2s(value []byte) string {
return *(*string)(unsafe.Pointer(&value))
}