-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.go
More file actions
247 lines (218 loc) · 6 KB
/
parse.go
File metadata and controls
247 lines (218 loc) · 6 KB
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
package gotz
import (
"encoding/binary"
"errors"
"fmt"
)
var ErrBadData = errors.New("gotz: malformed timezone data")
// parseData parses TZif-format binary data into a Zone.
func parseData(name string, data []byte) (*Zone, error) {
if len(data) < 44 {
return nil, fmt.Errorf("%w: file too short", ErrBadData)
}
// Validate magic number.
if string(data[:4]) != "TZif" {
return nil, fmt.Errorf("%w: invalid magic number", ErrBadData)
}
// Read version.
var version int
switch data[4] {
case 0:
version = 1
case '2':
version = 2
case '3':
version = 3
case '4':
version = 4
default:
return nil, fmt.Errorf("%w: unknown version byte %#x", ErrBadData, data[4])
}
// Parse header counts (6 big-endian uint32 at offset 20).
d := data[20:]
if len(d) < 24 {
return nil, fmt.Errorf("%w: header truncated", ErrBadData)
}
isutcnt := int(binary.BigEndian.Uint32(d[0:4]))
isstdcnt := int(binary.BigEndian.Uint32(d[4:8]))
leapcnt := int(binary.BigEndian.Uint32(d[8:12]))
timecnt := int(binary.BigEndian.Uint32(d[12:16]))
typecnt := int(binary.BigEndian.Uint32(d[16:20]))
charcnt := int(binary.BigEndian.Uint32(d[20:24]))
if typecnt == 0 {
return nil, fmt.Errorf("%w: no time types", ErrBadData)
}
// Calculate v1 data block size to skip it for v2+ files.
v1dataSize := timecnt*4 + // transition times (int32)
timecnt*1 + // transition type indices
typecnt*6 + // ttinfo records
charcnt + // abbreviation chars
leapcnt*8 + // leap second records (v1: 4+4)
isstdcnt + // std/wall indicators
isutcnt // UT/local indicators
d = data[44:] // skip header
if len(d) < v1dataSize {
return nil, fmt.Errorf("%w: v1 data block truncated", ErrBadData)
}
if version >= 2 {
// Skip v1 data block and read v2+ header.
d = d[v1dataSize:]
if len(d) < 44 {
return nil, fmt.Errorf("%w: v2 header truncated", ErrBadData)
}
if string(d[:4]) != "TZif" {
return nil, fmt.Errorf("%w: v2 magic mismatch", ErrBadData)
}
// Re-read counts from v2 header.
d = d[20:]
isutcnt = int(binary.BigEndian.Uint32(d[0:4]))
isstdcnt = int(binary.BigEndian.Uint32(d[4:8]))
leapcnt = int(binary.BigEndian.Uint32(d[8:12]))
timecnt = int(binary.BigEndian.Uint32(d[12:16]))
typecnt = int(binary.BigEndian.Uint32(d[16:20]))
charcnt = int(binary.BigEndian.Uint32(d[20:24]))
if typecnt == 0 {
return nil, fmt.Errorf("%w: no time types in v2 block", ErrBadData)
}
d = d[24:] // skip v2 counts
} else {
d = data[44:] // start of v1 data
}
// Now parse the data block.
// For v1: transition times are 4 bytes (int32).
// For v2+: transition times are 8 bytes (int64).
timeSize := 4
leapSize := 8 // v1: 4 (time) + 4 (correction)
if version >= 2 {
timeSize = 8
leapSize = 12 // v2+: 8 (time) + 4 (correction)
}
totalNeeded := timecnt*timeSize + // transition times
timecnt*1 + // transition type indices
typecnt*6 + // ttinfo records
charcnt + // abbreviation chars
leapcnt*leapSize + // leap second records
isstdcnt + // std/wall indicators
isutcnt // UT/local indicators
if len(d) < totalNeeded {
return nil, fmt.Errorf("%w: data block truncated", ErrBadData)
}
// Read transition times.
transitions := make([]Transition, timecnt)
for i := 0; i < timecnt; i++ {
if version >= 2 {
transitions[i].When = int64(binary.BigEndian.Uint64(d[:8]))
d = d[8:]
} else {
transitions[i].When = int64(int32(binary.BigEndian.Uint32(d[:4])))
d = d[4:]
}
}
// Read transition type indices.
for i := 0; i < timecnt; i++ {
idx := int(d[0])
if idx >= typecnt {
return nil, fmt.Errorf("%w: transition type index %d out of range (typecnt=%d)", ErrBadData, idx, typecnt)
}
transitions[i].Type = idx
d = d[1:]
}
// Read ttinfo records (6 bytes each).
types := make([]ZoneType, typecnt)
abbrIndices := make([]int, typecnt) // save for abbreviation lookup
for i := 0; i < typecnt; i++ {
offset := int32(binary.BigEndian.Uint32(d[0:4]))
isDST := d[4] != 0
abbrIdx := int(d[5])
types[i] = ZoneType{
Offset: int(offset),
IsDST: isDST,
}
abbrIndices[i] = abbrIdx
d = d[6:]
}
// Read abbreviation string block.
if charcnt > 0 {
abbrevBlock := d[:charcnt]
for i, idx := range abbrIndices {
if idx >= charcnt {
return nil, fmt.Errorf("%w: abbreviation index %d out of range", ErrBadData, idx)
}
types[i].Abbrev = byteString(abbrevBlock[idx:])
}
d = d[charcnt:]
}
// Read leap second records.
leapSeconds := make([]LeapSecond, leapcnt)
for i := 0; i < leapcnt; i++ {
if version >= 2 {
leapSeconds[i].When = int64(binary.BigEndian.Uint64(d[:8]))
d = d[8:]
} else {
leapSeconds[i].When = int64(int32(binary.BigEndian.Uint32(d[:4])))
d = d[4:]
}
leapSeconds[i].Correction = int32(binary.BigEndian.Uint32(d[:4]))
d = d[4:]
}
// Read std/wall indicators.
for i := 0; i < isstdcnt && i < timecnt; i++ {
transitions[i].IsStd = d[0] != 0
d = d[1:]
}
if isstdcnt > timecnt {
d = d[isstdcnt-timecnt:]
}
// Read UT/local indicators.
for i := 0; i < isutcnt && i < timecnt; i++ {
transitions[i].IsUT = d[0] != 0
d = d[1:]
}
if isutcnt > timecnt {
d = d[isutcnt-timecnt:]
}
// Read POSIX TZ footer string (v2+ only).
var extendRaw string
var extend *PosixTZ
if version >= 2 && len(d) > 1 && d[0] == '\n' {
d = d[1:]
if idx := indexByte(d, '\n'); idx >= 0 {
extendRaw = string(d[:idx])
if extendRaw != "" {
p, err := ParsePosixTZ(extendRaw)
if err == nil {
extend = p
}
}
}
}
z := &Zone{
name: name,
version: version,
types: types,
transitions: transitions,
leapSeconds: leapSeconds,
extend: extend,
extendRaw: extendRaw,
rawData: append([]byte(nil), data...), // defensive copy
}
return z, nil
}
// byteString extracts a NUL-terminated string from a byte slice.
func byteString(p []byte) string {
for i, b := range p {
if b == 0 {
return string(p[:i])
}
}
return string(p)
}
// indexByte returns the index of the first occurrence of c in s, or -1.
func indexByte(s []byte, c byte) int {
for i, b := range s {
if b == c {
return i
}
}
return -1
}