zoneinfo_read.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. // Copyright 2009 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Parse "zoneinfo" time zone file.
  5. // This is a fairly standard file format used on OS X, Linux, BSD, Sun, and others.
  6. // See tzfile(5), https://en.wikipedia.org/wiki/Zoneinfo,
  7. // and ftp://munnari.oz.au/pub/oldtz/
  8. package time
  9. import (
  10. "errors"
  11. "runtime"
  12. "syscall"
  13. _ "unsafe" // for go:linkname
  14. )
  15. // registerLoadFromEmbeddedTZData is called by the time/tzdata package,
  16. // if it is imported.
  17. //go:linkname registerLoadFromEmbeddedTZData
  18. func registerLoadFromEmbeddedTZData(f func(string) (string, error)) {
  19. loadFromEmbeddedTZData = f
  20. }
  21. // loadFromEmbeddedTZData is used to load a specific tzdata file
  22. // from tzdata information embedded in the binary itself.
  23. // This is set when the time/tzdata package is imported,
  24. // via registerLoadFromEmbeddedTzdata.
  25. var loadFromEmbeddedTZData func(zipname string) (string, error)
  26. // maxFileSize is the max permitted size of files read by readFile.
  27. // As reference, the zoneinfo.zip distributed by Go is ~350 KB,
  28. // so 10MB is overkill.
  29. const maxFileSize = 10 << 20
  30. type fileSizeError string
  31. func (f fileSizeError) Error() string {
  32. return "time: file " + string(f) + " is too large"
  33. }
  34. // Copies of io.Seek* constants to avoid importing "io":
  35. const (
  36. seekStart = 0
  37. seekCurrent = 1
  38. seekEnd = 2
  39. )
  40. // Simple I/O interface to binary blob of data.
  41. type dataIO struct {
  42. p []byte
  43. error bool
  44. }
  45. func (d *dataIO) read(n int) []byte {
  46. if len(d.p) < n {
  47. d.p = nil
  48. d.error = true
  49. return nil
  50. }
  51. p := d.p[0:n]
  52. d.p = d.p[n:]
  53. return p
  54. }
  55. func (d *dataIO) big4() (n uint32, ok bool) {
  56. p := d.read(4)
  57. if len(p) < 4 {
  58. d.error = true
  59. return 0, false
  60. }
  61. return uint32(p[3]) | uint32(p[2])<<8 | uint32(p[1])<<16 | uint32(p[0])<<24, true
  62. }
  63. func (d *dataIO) big8() (n uint64, ok bool) {
  64. n1, ok1 := d.big4()
  65. n2, ok2 := d.big4()
  66. if !ok1 || !ok2 {
  67. d.error = true
  68. return 0, false
  69. }
  70. return (uint64(n1) << 32) | uint64(n2), true
  71. }
  72. func (d *dataIO) byte() (n byte, ok bool) {
  73. p := d.read(1)
  74. if len(p) < 1 {
  75. d.error = true
  76. return 0, false
  77. }
  78. return p[0], true
  79. }
  80. // read returns the read of the data in the buffer.
  81. func (d *dataIO) rest() []byte {
  82. r := d.p
  83. d.p = nil
  84. return r
  85. }
  86. // Make a string by stopping at the first NUL
  87. func byteString(p []byte) string {
  88. for i := 0; i < len(p); i++ {
  89. if p[i] == 0 {
  90. return string(p[0:i])
  91. }
  92. }
  93. return string(p)
  94. }
  95. var badData = errors.New("malformed time zone information")
  96. // LoadLocationFromTZData returns a Location with the given name
  97. // initialized from the IANA Time Zone database-formatted data.
  98. // The data should be in the format of a standard IANA time zone file
  99. // (for example, the content of /etc/localtime on Unix systems).
  100. func LoadLocationFromTZData(name string, data []byte) (*Location, error) {
  101. d := dataIO{data, false}
  102. // 4-byte magic "TZif"
  103. if magic := d.read(4); string(magic) != "TZif" {
  104. return nil, badData
  105. }
  106. // 1-byte version, then 15 bytes of padding
  107. var version int
  108. var p []byte
  109. if p = d.read(16); len(p) != 16 {
  110. return nil, badData
  111. } else {
  112. switch p[0] {
  113. case 0:
  114. version = 1
  115. case '2':
  116. version = 2
  117. case '3':
  118. version = 3
  119. default:
  120. return nil, badData
  121. }
  122. }
  123. // six big-endian 32-bit integers:
  124. // number of UTC/local indicators
  125. // number of standard/wall indicators
  126. // number of leap seconds
  127. // number of transition times
  128. // number of local time zones
  129. // number of characters of time zone abbrev strings
  130. const (
  131. NUTCLocal = iota
  132. NStdWall
  133. NLeap
  134. NTime
  135. NZone
  136. NChar
  137. )
  138. var n [6]int
  139. for i := 0; i < 6; i++ {
  140. nn, ok := d.big4()
  141. if !ok {
  142. return nil, badData
  143. }
  144. if uint32(int(nn)) != nn {
  145. return nil, badData
  146. }
  147. n[i] = int(nn)
  148. }
  149. // If we have version 2 or 3, then the data is first written out
  150. // in a 32-bit format, then written out again in a 64-bit format.
  151. // Skip the 32-bit format and read the 64-bit one, as it can
  152. // describe a broader range of dates.
  153. is64 := false
  154. if version > 1 {
  155. // Skip the 32-bit data.
  156. skip := n[NTime]*4 +
  157. n[NTime] +
  158. n[NZone]*6 +
  159. n[NChar] +
  160. n[NLeap]*8 +
  161. n[NStdWall] +
  162. n[NUTCLocal]
  163. // Skip the version 2 header that we just read.
  164. skip += 4 + 16
  165. d.read(skip)
  166. is64 = true
  167. // Read the counts again, they can differ.
  168. for i := 0; i < 6; i++ {
  169. nn, ok := d.big4()
  170. if !ok {
  171. return nil, badData
  172. }
  173. if uint32(int(nn)) != nn {
  174. return nil, badData
  175. }
  176. n[i] = int(nn)
  177. }
  178. }
  179. size := 4
  180. if is64 {
  181. size = 8
  182. }
  183. // Transition times.
  184. txtimes := dataIO{d.read(n[NTime] * size), false}
  185. // Time zone indices for transition times.
  186. txzones := d.read(n[NTime])
  187. // Zone info structures
  188. zonedata := dataIO{d.read(n[NZone] * 6), false}
  189. // Time zone abbreviations.
  190. abbrev := d.read(n[NChar])
  191. // Leap-second time pairs
  192. d.read(n[NLeap] * (size + 4))
  193. // Whether tx times associated with local time types
  194. // are specified as standard time or wall time.
  195. isstd := d.read(n[NStdWall])
  196. // Whether tx times associated with local time types
  197. // are specified as UTC or local time.
  198. isutc := d.read(n[NUTCLocal])
  199. if d.error { // ran out of data
  200. return nil, badData
  201. }
  202. var extend string
  203. rest := d.rest()
  204. if len(rest) > 2 && rest[0] == '\n' && rest[len(rest)-1] == '\n' {
  205. extend = string(rest[1 : len(rest)-1])
  206. }
  207. // Now we can build up a useful data structure.
  208. // First the zone information.
  209. // utcoff[4] isdst[1] nameindex[1]
  210. nzone := n[NZone]
  211. if nzone == 0 {
  212. // Reject tzdata files with no zones. There's nothing useful in them.
  213. // This also avoids a panic later when we add and then use a fake transition (golang.org/issue/29437).
  214. return nil, badData
  215. }
  216. zones := make([]zone, nzone)
  217. for i := range zones {
  218. var ok bool
  219. var n uint32
  220. if n, ok = zonedata.big4(); !ok {
  221. return nil, badData
  222. }
  223. if uint32(int(n)) != n {
  224. return nil, badData
  225. }
  226. zones[i].offset = int(int32(n))
  227. var b byte
  228. if b, ok = zonedata.byte(); !ok {
  229. return nil, badData
  230. }
  231. zones[i].isDST = b != 0
  232. if b, ok = zonedata.byte(); !ok || int(b) >= len(abbrev) {
  233. return nil, badData
  234. }
  235. zones[i].name = byteString(abbrev[b:])
  236. if runtime.GOOS == "aix" && len(name) > 8 && (name[:8] == "Etc/GMT+" || name[:8] == "Etc/GMT-") {
  237. // There is a bug with AIX 7.2 TL 0 with files in Etc,
  238. // GMT+1 will return GMT-1 instead of GMT+1 or -01.
  239. if name != "Etc/GMT+0" {
  240. // GMT+0 is OK
  241. zones[i].name = name[4:]
  242. }
  243. }
  244. }
  245. // Now the transition time info.
  246. tx := make([]zoneTrans, n[NTime])
  247. for i := range tx {
  248. var n int64
  249. if !is64 {
  250. if n4, ok := txtimes.big4(); !ok {
  251. return nil, badData
  252. } else {
  253. n = int64(int32(n4))
  254. }
  255. } else {
  256. if n8, ok := txtimes.big8(); !ok {
  257. return nil, badData
  258. } else {
  259. n = int64(n8)
  260. }
  261. }
  262. tx[i].when = n
  263. if int(txzones[i]) >= len(zones) {
  264. return nil, badData
  265. }
  266. tx[i].index = txzones[i]
  267. if i < len(isstd) {
  268. tx[i].isstd = isstd[i] != 0
  269. }
  270. if i < len(isutc) {
  271. tx[i].isutc = isutc[i] != 0
  272. }
  273. }
  274. if len(tx) == 0 {
  275. // Build fake transition to cover all time.
  276. // This happens in fixed locations like "Etc/GMT0".
  277. tx = append(tx, zoneTrans{when: alpha, index: 0})
  278. }
  279. // Committed to succeed.
  280. l := &Location{zone: zones, tx: tx, name: name, extend: extend}
  281. // Fill in the cache with information about right now,
  282. // since that will be the most common lookup.
  283. sec, _, _ := now()
  284. for i := range tx {
  285. if tx[i].when <= sec && (i+1 == len(tx) || sec < tx[i+1].when) {
  286. l.cacheStart = tx[i].when
  287. l.cacheEnd = omega
  288. l.cacheZone = &l.zone[tx[i].index]
  289. if i+1 < len(tx) {
  290. l.cacheEnd = tx[i+1].when
  291. } else if l.extend != "" {
  292. // If we're at the end of the known zone transitions,
  293. // try the extend string.
  294. if name, offset, estart, eend, isDST, ok := tzset(l.extend, l.cacheEnd, sec); ok {
  295. l.cacheStart = estart
  296. l.cacheEnd = eend
  297. // Find the zone that is returned by tzset to avoid allocation if possible.
  298. if zoneIdx := findZone(l.zone, name, offset, isDST); zoneIdx != -1 {
  299. l.cacheZone = &l.zone[zoneIdx]
  300. } else {
  301. l.cacheZone = &zone{
  302. name: name,
  303. offset: offset,
  304. isDST: isDST,
  305. }
  306. }
  307. }
  308. }
  309. break
  310. }
  311. }
  312. return l, nil
  313. }
  314. func findZone(zones []zone, name string, offset int, isDST bool) int {
  315. for i, z := range zones {
  316. if z.name == name && z.offset == offset && z.isDST == isDST {
  317. return i
  318. }
  319. }
  320. return -1
  321. }
  322. // loadTzinfoFromDirOrZip returns the contents of the file with the given name
  323. // in dir. dir can either be an uncompressed zip file, or a directory.
  324. func loadTzinfoFromDirOrZip(dir, name string) ([]byte, error) {
  325. if len(dir) > 4 && dir[len(dir)-4:] == ".zip" {
  326. return loadTzinfoFromZip(dir, name)
  327. }
  328. if dir != "" {
  329. name = dir + "/" + name
  330. }
  331. return readFile(name)
  332. }
  333. // There are 500+ zoneinfo files. Rather than distribute them all
  334. // individually, we ship them in an uncompressed zip file.
  335. // Used this way, the zip file format serves as a commonly readable
  336. // container for the individual small files. We choose zip over tar
  337. // because zip files have a contiguous table of contents, making
  338. // individual file lookups faster, and because the per-file overhead
  339. // in a zip file is considerably less than tar's 512 bytes.
  340. // get4 returns the little-endian 32-bit value in b.
  341. func get4(b []byte) int {
  342. if len(b) < 4 {
  343. return 0
  344. }
  345. return int(b[0]) | int(b[1])<<8 | int(b[2])<<16 | int(b[3])<<24
  346. }
  347. // get2 returns the little-endian 16-bit value in b.
  348. func get2(b []byte) int {
  349. if len(b) < 2 {
  350. return 0
  351. }
  352. return int(b[0]) | int(b[1])<<8
  353. }
  354. // loadTzinfoFromZip returns the contents of the file with the given name
  355. // in the given uncompressed zip file.
  356. func loadTzinfoFromZip(zipfile, name string) ([]byte, error) {
  357. fd, err := open(zipfile)
  358. if err != nil {
  359. return nil, err
  360. }
  361. defer closefd(fd)
  362. const (
  363. zecheader = 0x06054b50
  364. zcheader = 0x02014b50
  365. ztailsize = 22
  366. zheadersize = 30
  367. zheader = 0x04034b50
  368. )
  369. buf := make([]byte, ztailsize)
  370. if err := preadn(fd, buf, -ztailsize); err != nil || get4(buf) != zecheader {
  371. return nil, errors.New("corrupt zip file " + zipfile)
  372. }
  373. n := get2(buf[10:])
  374. size := get4(buf[12:])
  375. off := get4(buf[16:])
  376. buf = make([]byte, size)
  377. if err := preadn(fd, buf, off); err != nil {
  378. return nil, errors.New("corrupt zip file " + zipfile)
  379. }
  380. for i := 0; i < n; i++ {
  381. // zip entry layout:
  382. // 0 magic[4]
  383. // 4 madevers[1]
  384. // 5 madeos[1]
  385. // 6 extvers[1]
  386. // 7 extos[1]
  387. // 8 flags[2]
  388. // 10 meth[2]
  389. // 12 modtime[2]
  390. // 14 moddate[2]
  391. // 16 crc[4]
  392. // 20 csize[4]
  393. // 24 uncsize[4]
  394. // 28 namelen[2]
  395. // 30 xlen[2]
  396. // 32 fclen[2]
  397. // 34 disknum[2]
  398. // 36 iattr[2]
  399. // 38 eattr[4]
  400. // 42 off[4]
  401. // 46 name[namelen]
  402. // 46+namelen+xlen+fclen - next header
  403. //
  404. if get4(buf) != zcheader {
  405. break
  406. }
  407. meth := get2(buf[10:])
  408. size := get4(buf[24:])
  409. namelen := get2(buf[28:])
  410. xlen := get2(buf[30:])
  411. fclen := get2(buf[32:])
  412. off := get4(buf[42:])
  413. zname := buf[46 : 46+namelen]
  414. buf = buf[46+namelen+xlen+fclen:]
  415. if string(zname) != name {
  416. continue
  417. }
  418. if meth != 0 {
  419. return nil, errors.New("unsupported compression for " + name + " in " + zipfile)
  420. }
  421. // zip per-file header layout:
  422. // 0 magic[4]
  423. // 4 extvers[1]
  424. // 5 extos[1]
  425. // 6 flags[2]
  426. // 8 meth[2]
  427. // 10 modtime[2]
  428. // 12 moddate[2]
  429. // 14 crc[4]
  430. // 18 csize[4]
  431. // 22 uncsize[4]
  432. // 26 namelen[2]
  433. // 28 xlen[2]
  434. // 30 name[namelen]
  435. // 30+namelen+xlen - file data
  436. //
  437. buf = make([]byte, zheadersize+namelen)
  438. if err := preadn(fd, buf, off); err != nil ||
  439. get4(buf) != zheader ||
  440. get2(buf[8:]) != meth ||
  441. get2(buf[26:]) != namelen ||
  442. string(buf[30:30+namelen]) != name {
  443. return nil, errors.New("corrupt zip file " + zipfile)
  444. }
  445. xlen = get2(buf[28:])
  446. buf = make([]byte, size)
  447. if err := preadn(fd, buf, off+30+namelen+xlen); err != nil {
  448. return nil, errors.New("corrupt zip file " + zipfile)
  449. }
  450. return buf, nil
  451. }
  452. return nil, syscall.ENOENT
  453. }
  454. // loadTzinfoFromTzdata returns the time zone information of the time zone
  455. // with the given name, from a tzdata database file as they are typically
  456. // found on android.
  457. var loadTzinfoFromTzdata func(file, name string) ([]byte, error)
  458. // loadTzinfo returns the time zone information of the time zone
  459. // with the given name, from a given source. A source may be a
  460. // timezone database directory, tzdata database file or an uncompressed
  461. // zip file, containing the contents of such a directory.
  462. func loadTzinfo(name string, source string) ([]byte, error) {
  463. if len(source) >= 6 && source[len(source)-6:] == "tzdata" {
  464. return loadTzinfoFromTzdata(source, name)
  465. }
  466. return loadTzinfoFromDirOrZip(source, name)
  467. }
  468. // loadLocation returns the Location with the given name from one of
  469. // the specified sources. See loadTzinfo for a list of supported sources.
  470. // The first timezone data matching the given name that is successfully loaded
  471. // and parsed is returned as a Location.
  472. func loadLocation(name string, sources []string) (z *Location, firstErr error) {
  473. for _, source := range sources {
  474. var zoneData, err = loadTzinfo(name, source)
  475. if err == nil {
  476. if z, err = LoadLocationFromTZData(name, zoneData); err == nil {
  477. return z, nil
  478. }
  479. }
  480. if firstErr == nil && err != syscall.ENOENT {
  481. firstErr = err
  482. }
  483. }
  484. if loadFromEmbeddedTZData != nil {
  485. zonedata, err := loadFromEmbeddedTZData(name)
  486. if err == nil {
  487. if z, err = LoadLocationFromTZData(name, []byte(zonedata)); err == nil {
  488. return z, nil
  489. }
  490. }
  491. if firstErr == nil && err != syscall.ENOENT {
  492. firstErr = err
  493. }
  494. }
  495. if firstErr != nil {
  496. return nil, firstErr
  497. }
  498. return nil, errors.New("unknown time zone " + name)
  499. }
  500. // readFile reads and returns the content of the named file.
  501. // It is a trivial implementation of os.ReadFile, reimplemented
  502. // here to avoid depending on io/ioutil or os.
  503. // It returns an error if name exceeds maxFileSize bytes.
  504. func readFile(name string) ([]byte, error) {
  505. f, err := open(name)
  506. if err != nil {
  507. return nil, err
  508. }
  509. defer closefd(f)
  510. var (
  511. buf [4096]byte
  512. ret []byte
  513. n int
  514. )
  515. for {
  516. n, err = read(f, buf[:])
  517. if n > 0 {
  518. ret = append(ret, buf[:n]...)
  519. }
  520. if n == 0 || err != nil {
  521. break
  522. }
  523. if len(ret) > maxFileSize {
  524. return nil, fileSizeError(name)
  525. }
  526. }
  527. return ret, err
  528. }