Black Lives Matter. Support the Equal Justice Initiative.

Source file src/time/zoneinfo_read.go

Documentation: time

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

View as plain text