forked from IncSW/geoip2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
country.go
82 lines (79 loc) · 1.96 KB
/
country.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
package geoip2
import (
"errors"
"strconv"
)
func readCountry(country *Country, buffer []byte, offset uint) (uint, error) {
dataType, size, offset, err := readControl(buffer, offset)
if err != nil {
return 0, err
}
switch dataType {
case dataTypeMap:
return readCountryMap(country, 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 country pointer type: " + strconv.Itoa(int(dataType)))
}
_, err = readCountryMap(country, buffer, size, offset)
if err != nil {
return 0, err
}
return newOffset, nil
default:
return 0, errors.New("invalid country type: " + strconv.Itoa(int(dataType)))
}
}
func readCountryMap(country *Country, 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 "geoname_id":
country.GeoNameID, offset, err = readUInt32(buffer, offset)
if err != nil {
return 0, err
}
case "iso_code":
country.ISOCode, offset, err = readString(buffer, offset)
if err != nil {
return 0, err
}
case "names":
country.Names, offset, err = readStringMap(buffer, offset)
if err != nil {
return 0, err
}
case "is_in_european_union":
country.IsInEuropeanUnion, offset, err = readBool(buffer, offset)
if err != nil {
return 0, err
}
case "type":
country.Type, offset, err = readString(buffer, offset)
if err != nil {
return 0, err
}
case "confidence":
country.Confidence, offset, err = readUInt16(buffer, offset)
if err != nil {
return 0, err
}
default:
return 0, errors.New("unknown country key: " + string(key))
}
}
return offset, nil
}