...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package tzdata
22
23
24
25 import (
26 "errors"
27 "syscall"
28 _ "unsafe"
29 )
30
31
32
33 func registerLoadFromEmbeddedTZData(func(string) (string, error))
34
35 func init() {
36 registerLoadFromEmbeddedTZData(loadFromEmbeddedTZData)
37 }
38
39
40 func get4s(s string) int {
41 if len(s) < 4 {
42 return 0
43 }
44 return int(s[0]) | int(s[1])<<8 | int(s[2])<<16 | int(s[3])<<24
45 }
46
47
48 func get2s(s string) int {
49 if len(s) < 2 {
50 return 0
51 }
52 return int(s[0]) | int(s[1])<<8
53 }
54
55
56
57
58
59 func loadFromEmbeddedTZData(name string) (string, error) {
60 const (
61 zecheader = 0x06054b50
62 zcheader = 0x02014b50
63 ztailsize = 22
64
65 zheadersize = 30
66 zheader = 0x04034b50
67 )
68
69 z := zipdata
70
71 idx := len(z) - ztailsize
72 n := get2s(z[idx+10:])
73 idx = get4s(z[idx+16:])
74
75 for i := 0; i < n; i++ {
76
77 if get4s(z[idx:]) != zcheader {
78 break
79 }
80 meth := get2s(z[idx+10:])
81 size := get4s(z[idx+24:])
82 namelen := get2s(z[idx+28:])
83 xlen := get2s(z[idx+30:])
84 fclen := get2s(z[idx+32:])
85 off := get4s(z[idx+42:])
86 zname := z[idx+46 : idx+46+namelen]
87 idx += 46 + namelen + xlen + fclen
88 if zname != name {
89 continue
90 }
91 if meth != 0 {
92 return "", errors.New("unsupported compression for " + name + " in embedded tzdata")
93 }
94
95
96 idx = off
97 if get4s(z[idx:]) != zheader ||
98 get2s(z[idx+8:]) != meth ||
99 get2s(z[idx+26:]) != namelen ||
100 z[idx+30:idx+30+namelen] != name {
101 return "", errors.New("corrupt embedded tzdata")
102 }
103 xlen = get2s(z[idx+28:])
104 idx += 30 + namelen + xlen
105 return z[idx : idx+size], nil
106 }
107
108 return "", syscall.ENOENT
109 }
110
View as plain text