-
Notifications
You must be signed in to change notification settings - Fork 149
/
serialize.go
180 lines (151 loc) · 4 KB
/
serialize.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
package phonenumbers
import (
"bytes"
"compress/gzip"
"encoding/base64"
"encoding/binary"
fmt "fmt"
"io/ioutil"
"strings"
)
// intStringMap is our data structure for maps from prefixes to a single string
// this is used for our carrier and geocoding maps
type intStringMap struct {
Map map[int]string
MaxLength int
}
func loadPrefixMap(data string) (*intStringMap, error) {
rawBytes, err := decodeUnzipString(data)
if err != nil {
return nil, err
}
reader := bytes.NewReader(rawBytes)
// ok, first read in our number of values
var valueSize uint32
err = binary.Read(reader, binary.LittleEndian, &valueSize)
if err != nil {
return nil, err
}
// then our values
valueBytes := make([]byte, valueSize)
n, err := reader.Read(valueBytes)
if uint32(n) < valueSize {
return nil, fmt.Errorf("unable to read all values: %v", err)
}
values := strings.Split(string(valueBytes), "\n")
// read our # of mappings
var mappingCount uint32
err = binary.Read(reader, binary.LittleEndian, &mappingCount)
if err != nil {
return nil, err
}
maxLength := 0
mappings := make(map[int]string, mappingCount)
prefix := 0
for i := 0; i < int(mappingCount); i++ {
// first read our diff
diff, err := binary.ReadUvarint(reader)
if err != nil {
return nil, err
}
prefix += int(diff)
// then our map
var valueIntern uint16
err = binary.Read(reader, binary.LittleEndian, &valueIntern)
if err != nil || int(valueIntern) >= len(values) {
return nil, fmt.Errorf("unable to read interned value: %v", err)
}
mappings[prefix] = values[valueIntern]
strPrefix := fmt.Sprintf("%d", prefix)
if len(strPrefix) > maxLength {
maxLength = len(strPrefix)
}
}
// return our values
return &intStringMap{
Map: mappings,
MaxLength: maxLength,
}, nil
}
// intStringArrayMap is our map from an int to an array of strings
// this is used for our timezone and region maps
type intStringArrayMap struct {
Map map[int][]string
MaxLength int
}
func loadIntStringArrayMap(data string) (*intStringArrayMap, error) {
rawBytes, err := decodeUnzipString(data)
if err != nil {
return nil, err
}
reader := bytes.NewReader(rawBytes)
// ok, first read in our number of values
var valueSize uint32
err = binary.Read(reader, binary.LittleEndian, &valueSize)
if err != nil {
return nil, err
}
// then our values
valueBytes := make([]byte, valueSize)
n, err := reader.Read(valueBytes)
if uint32(n) < valueSize {
return nil, fmt.Errorf("unable to read all values: %v", err)
}
values := strings.Split(string(valueBytes), "\n")
// read our # of mappings
var mappingCount uint32
err = binary.Read(reader, binary.LittleEndian, &mappingCount)
if err != nil {
return nil, err
}
maxLength := 0
mappings := make(map[int][]string, mappingCount)
key := 0
for i := 0; i < int(mappingCount); i++ {
// first read our diff
diff, err := binary.ReadUvarint(reader)
if err != nil {
return nil, err
}
key += int(diff)
// then our values
var valueCount uint8
if err = binary.Read(reader, binary.LittleEndian, &valueCount); err != nil {
return nil, err
}
keyValues := make([]string, valueCount)
for i := 0; i < int(valueCount); i++ {
var valueIntern uint16
err = binary.Read(reader, binary.LittleEndian, &valueIntern)
if err != nil || int(valueIntern) >= len(values) {
return nil, fmt.Errorf("unable to read interned value: %v", err)
}
keyValues[i] = values[valueIntern]
}
mappings[key] = keyValues
strPrefix := fmt.Sprintf("%d", key)
if len(strPrefix) > maxLength {
maxLength = len(strPrefix)
}
}
// return our values
return &intStringArrayMap{
Map: mappings,
MaxLength: maxLength,
}, nil
}
func decodeUnzipString(data string) ([]byte, error) {
decodedBytes, err := base64.StdEncoding.DecodeString(data)
if err != nil {
return nil, err
}
zipReader, err := gzip.NewReader(bytes.NewReader(decodedBytes))
if err != nil {
return nil, err
}
rawBytes, err := ioutil.ReadAll(zipReader)
if err != nil {
return nil, err
}
return rawBytes, nil
}