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