-
Notifications
You must be signed in to change notification settings - Fork 12
/
location.go
77 lines (74 loc) · 1.87 KB
/
location.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
package geoip2
import (
"errors"
"strconv"
)
func readLocation(location *Location, buffer []byte, offset uint) (uint, error) {
dataType, size, offset, err := readControl(buffer, offset)
if err != nil {
return 0, err
}
switch dataType {
case dataTypeMap:
return readLocationMap(location, buffer, size, offset)
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 != dataTypeMap {
return 0, errors.New("invalid location pointer type: " + strconv.Itoa(int(dataType)))
}
_, err = readLocationMap(location, buffer, size, offset)
if err != nil {
return 0, err
}
return newOffset, nil
default:
return 0, errors.New("invalid location type: " + strconv.Itoa(int(dataType)))
}
}
func readLocationMap(location *Location, buffer []byte, mapSize uint, offset uint) (uint, error) {
var key []byte
var err error
for i := uint(0); i < mapSize; i++ {
key, offset, err = readMapKey(buffer, offset)
if err != nil {
return 0, err
}
switch b2s(key) {
case "latitude":
location.Latitude, offset, err = readFloat64(buffer, offset)
if err != nil {
return 0, err
}
case "longitude":
location.Longitude, offset, err = readFloat64(buffer, offset)
if err != nil {
return 0, err
}
case "accuracy_radius":
location.AccuracyRadius, offset, err = readUInt16(buffer, offset)
if err != nil {
return 0, err
}
case "time_zone":
location.TimeZone, offset, err = readString(buffer, offset)
if err != nil {
return 0, err
}
case "metro_code":
location.MetroCode, offset, err = readUInt16(buffer, offset)
if err != nil {
return 0, err
}
default:
return 0, errors.New("unknown location key: " + string(key))
}
}
return offset, nil
}