forked from IncSW/geoip2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subdivision.go
114 lines (109 loc) · 3.03 KB
/
subdivision.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
package geoip2
import (
"errors"
"strconv"
)
func readSubdivisions(buffer []byte, offset uint) ([]Subdivision, uint, error) {
dataType, size, offset, err := readControl(buffer, offset)
if err != nil {
return nil, 0, err
}
switch dataType {
case dataTypeSlice:
return readSubdivisionsSlice(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 != dataTypeSlice {
return nil, 0, errors.New("invalid subdivisions pointer type: " + strconv.Itoa(int(dataType)))
}
subdivisions, _, err := readSubdivisionsSlice(buffer, size, offset)
if err != nil {
return nil, 0, err
}
return subdivisions, newOffset, nil
default:
return nil, 0, errors.New("invalid subdivisions type: " + strconv.Itoa(int(dataType)))
}
}
func readSubdivisionsSlice(buffer []byte, subdivisionsSize uint, offset uint) ([]Subdivision, uint, error) {
var err error
subdivisions := make([]Subdivision, subdivisionsSize)
for i := uint(0); i < subdivisionsSize; i++ {
offset, err = readSubdivision(&subdivisions[i], buffer, offset)
if err != nil {
return nil, 0, err
}
}
return subdivisions, offset, nil
}
func readSubdivision(subdivision *Subdivision, buffer []byte, offset uint) (uint, error) {
dataType, size, offset, err := readControl(buffer, offset)
if err != nil {
return 0, err
}
switch dataType {
case dataTypeMap:
return readSubdivisionMap(subdivision, 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 subdivision pointer type: " + strconv.Itoa(int(dataType)))
}
_, err = readSubdivisionMap(subdivision, buffer, size, offset)
if err != nil {
return 0, err
}
return newOffset, nil
default:
return 0, errors.New("invalid subdivision type: " + strconv.Itoa(int(dataType)))
}
}
func readSubdivisionMap(subdivision *Subdivision, 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":
subdivision.GeoNameID, offset, err = readUInt32(buffer, offset)
if err != nil {
return 0, err
}
case "iso_code":
subdivision.ISOCode, offset, err = readString(buffer, offset)
if err != nil {
return 0, err
}
case "names":
subdivision.Names, offset, err = readStringMap(buffer, offset)
if err != nil {
return 0, err
}
case "confidence":
subdivision.Confidence, offset, err = readUInt16(buffer, offset)
if err != nil {
return 0, err
}
default:
return 0, errors.New("unknown subdivision key: " + string(key))
}
}
return offset, nil
}