...
Run Format

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  // 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) byte() (n byte, ok bool) {
    63  	p := d.read(1)
    64  	if len(p) < 1 {
    65  		d.error = true
    66  		return 0, false
    67  	}
    68  	return p[0], true
    69  }
    70  
    71  // Make a string by stopping at the first NUL
    72  func byteString(p []byte) string {
    73  	for i := 0; i < len(p); i++ {
    74  		if p[i] == 0 {
    75  			return string(p[0:i])
    76  		}
    77  	}
    78  	return string(p)
    79  }
    80  
    81  var badData = errors.New("malformed time zone information")
    82  
    83  // LoadLocationFromTZData returns a Location with the given name
    84  // initialized from the IANA Time Zone database-formatted data.
    85  // The data should be in the format of a standard IANA time zone file
    86  // (for example, the content of /etc/localtime on Unix systems).
    87  func LoadLocationFromTZData(name string, data []byte) (*Location, error) {
    88  	d := dataIO{data, false}
    89  
    90  	// 4-byte magic "TZif"
    91  	if magic := d.read(4); string(magic) != "TZif" {
    92  		return nil, badData
    93  	}
    94  
    95  	// 1-byte version, then 15 bytes of padding
    96  	var p []byte
    97  	if p = d.read(16); len(p) != 16 || p[0] != 0 && p[0] != '2' && p[0] != '3' {
    98  		return nil, badData
    99  	}
   100  
   101  	// six big-endian 32-bit integers:
   102  	//	number of UTC/local indicators
   103  	//	number of standard/wall indicators
   104  	//	number of leap seconds
   105  	//	number of transition times
   106  	//	number of local time zones
   107  	//	number of characters of time zone abbrev strings
   108  	const (
   109  		NUTCLocal = iota
   110  		NStdWall
   111  		NLeap
   112  		NTime
   113  		NZone
   114  		NChar
   115  	)
   116  	var n [6]int
   117  	for i := 0; i < 6; i++ {
   118  		nn, ok := d.big4()
   119  		if !ok {
   120  			return nil, badData
   121  		}
   122  		n[i] = int(nn)
   123  	}
   124  
   125  	// Transition times.
   126  	txtimes := dataIO{d.read(n[NTime] * 4), false}
   127  
   128  	// Time zone indices for transition times.
   129  	txzones := d.read(n[NTime])
   130  
   131  	// Zone info structures
   132  	zonedata := dataIO{d.read(n[NZone] * 6), false}
   133  
   134  	// Time zone abbreviations.
   135  	abbrev := d.read(n[NChar])
   136  
   137  	// Leap-second time pairs
   138  	d.read(n[NLeap] * 8)
   139  
   140  	// Whether tx times associated with local time types
   141  	// are specified as standard time or wall time.
   142  	isstd := d.read(n[NStdWall])
   143  
   144  	// Whether tx times associated with local time types
   145  	// are specified as UTC or local time.
   146  	isutc := d.read(n[NUTCLocal])
   147  
   148  	if d.error { // ran out of data
   149  		return nil, badData
   150  	}
   151  
   152  	// If version == 2 or 3, the entire file repeats, this time using
   153  	// 8-byte ints for txtimes and leap seconds.
   154  	// We won't need those until 2106.
   155  
   156  	// Now we can build up a useful data structure.
   157  	// First the zone information.
   158  	//	utcoff[4] isdst[1] nameindex[1]
   159  	zone := make([]zone, n[NZone])
   160  	for i := range zone {
   161  		var ok bool
   162  		var n uint32
   163  		if n, ok = zonedata.big4(); !ok {
   164  			return nil, badData
   165  		}
   166  		zone[i].offset = int(int32(n))
   167  		var b byte
   168  		if b, ok = zonedata.byte(); !ok {
   169  			return nil, badData
   170  		}
   171  		zone[i].isDST = b != 0
   172  		if b, ok = zonedata.byte(); !ok || int(b) >= len(abbrev) {
   173  			return nil, badData
   174  		}
   175  		zone[i].name = byteString(abbrev[b:])
   176  		if runtime.GOOS == "aix" && len(name) > 8 && (name[:8] == "Etc/GMT+" || name[:8] == "Etc/GMT-") {
   177  			// There is a bug with AIX 7.2 TL 0 with files in Etc,
   178  			// GMT+1 will return GMT-1 instead of GMT+1 or -01.
   179  			if name != "Etc/GMT+0" {
   180  				// GMT+0 is OK
   181  				zone[i].name = name[4:]
   182  			}
   183  		}
   184  	}
   185  
   186  	// Now the transition time info.
   187  	tx := make([]zoneTrans, n[NTime])
   188  	for i := range tx {
   189  		var ok bool
   190  		var n uint32
   191  		if n, ok = txtimes.big4(); !ok {
   192  			return nil, badData
   193  		}
   194  		tx[i].when = int64(int32(n))
   195  		if int(txzones[i]) >= len(zone) {
   196  			return nil, badData
   197  		}
   198  		tx[i].index = txzones[i]
   199  		if i < len(isstd) {
   200  			tx[i].isstd = isstd[i] != 0
   201  		}
   202  		if i < len(isutc) {
   203  			tx[i].isutc = isutc[i] != 0
   204  		}
   205  	}
   206  
   207  	if len(tx) == 0 {
   208  		// Build fake transition to cover all time.
   209  		// This happens in fixed locations like "Etc/GMT0".
   210  		tx = append(tx, zoneTrans{when: alpha, index: 0})
   211  	}
   212  
   213  	// Committed to succeed.
   214  	l := &Location{zone: zone, tx: tx, name: name}
   215  
   216  	// Fill in the cache with information about right now,
   217  	// since that will be the most common lookup.
   218  	sec, _, _ := now()
   219  	for i := range tx {
   220  		if tx[i].when <= sec && (i+1 == len(tx) || sec < tx[i+1].when) {
   221  			l.cacheStart = tx[i].when
   222  			l.cacheEnd = omega
   223  			if i+1 < len(tx) {
   224  				l.cacheEnd = tx[i+1].when
   225  			}
   226  			l.cacheZone = &l.zone[tx[i].index]
   227  		}
   228  	}
   229  
   230  	return l, nil
   231  }
   232  
   233  // loadTzinfoFromDirOrZip returns the contents of the file with the given name
   234  // in dir. dir can either be an uncompressed zip file, or a directory.
   235  func loadTzinfoFromDirOrZip(dir, name string) ([]byte, error) {
   236  	if len(dir) > 4 && dir[len(dir)-4:] == ".zip" {
   237  		return loadTzinfoFromZip(dir, name)
   238  	}
   239  	if dir != "" {
   240  		name = dir + "/" + name
   241  	}
   242  	return readFile(name)
   243  }
   244  
   245  // There are 500+ zoneinfo files. Rather than distribute them all
   246  // individually, we ship them in an uncompressed zip file.
   247  // Used this way, the zip file format serves as a commonly readable
   248  // container for the individual small files. We choose zip over tar
   249  // because zip files have a contiguous table of contents, making
   250  // individual file lookups faster, and because the per-file overhead
   251  // in a zip file is considerably less than tar's 512 bytes.
   252  
   253  // get4 returns the little-endian 32-bit value in b.
   254  func get4(b []byte) int {
   255  	if len(b) < 4 {
   256  		return 0
   257  	}
   258  	return int(b[0]) | int(b[1])<<8 | int(b[2])<<16 | int(b[3])<<24
   259  }
   260  
   261  // get2 returns the little-endian 16-bit value in b.
   262  func get2(b []byte) int {
   263  	if len(b) < 2 {
   264  		return 0
   265  	}
   266  	return int(b[0]) | int(b[1])<<8
   267  }
   268  
   269  // loadTzinfoFromZip returns the contents of the file with the given name
   270  // in the given uncompressed zip file.
   271  func loadTzinfoFromZip(zipfile, name string) ([]byte, error) {
   272  	fd, err := open(zipfile)
   273  	if err != nil {
   274  		return nil, err
   275  	}
   276  	defer closefd(fd)
   277  
   278  	const (
   279  		zecheader = 0x06054b50
   280  		zcheader  = 0x02014b50
   281  		ztailsize = 22
   282  
   283  		zheadersize = 30
   284  		zheader     = 0x04034b50
   285  	)
   286  
   287  	buf := make([]byte, ztailsize)
   288  	if err := preadn(fd, buf, -ztailsize); err != nil || get4(buf) != zecheader {
   289  		return nil, errors.New("corrupt zip file " + zipfile)
   290  	}
   291  	n := get2(buf[10:])
   292  	size := get4(buf[12:])
   293  	off := get4(buf[16:])
   294  
   295  	buf = make([]byte, size)
   296  	if err := preadn(fd, buf, off); err != nil {
   297  		return nil, errors.New("corrupt zip file " + zipfile)
   298  	}
   299  
   300  	for i := 0; i < n; i++ {
   301  		// zip entry layout:
   302  		//	0	magic[4]
   303  		//	4	madevers[1]
   304  		//	5	madeos[1]
   305  		//	6	extvers[1]
   306  		//	7	extos[1]
   307  		//	8	flags[2]
   308  		//	10	meth[2]
   309  		//	12	modtime[2]
   310  		//	14	moddate[2]
   311  		//	16	crc[4]
   312  		//	20	csize[4]
   313  		//	24	uncsize[4]
   314  		//	28	namelen[2]
   315  		//	30	xlen[2]
   316  		//	32	fclen[2]
   317  		//	34	disknum[2]
   318  		//	36	iattr[2]
   319  		//	38	eattr[4]
   320  		//	42	off[4]
   321  		//	46	name[namelen]
   322  		//	46+namelen+xlen+fclen - next header
   323  		//
   324  		if get4(buf) != zcheader {
   325  			break
   326  		}
   327  		meth := get2(buf[10:])
   328  		size := get4(buf[24:])
   329  		namelen := get2(buf[28:])
   330  		xlen := get2(buf[30:])
   331  		fclen := get2(buf[32:])
   332  		off := get4(buf[42:])
   333  		zname := buf[46 : 46+namelen]
   334  		buf = buf[46+namelen+xlen+fclen:]
   335  		if string(zname) != name {
   336  			continue
   337  		}
   338  		if meth != 0 {
   339  			return nil, errors.New("unsupported compression for " + name + " in " + zipfile)
   340  		}
   341  
   342  		// zip per-file header layout:
   343  		//	0	magic[4]
   344  		//	4	extvers[1]
   345  		//	5	extos[1]
   346  		//	6	flags[2]
   347  		//	8	meth[2]
   348  		//	10	modtime[2]
   349  		//	12	moddate[2]
   350  		//	14	crc[4]
   351  		//	18	csize[4]
   352  		//	22	uncsize[4]
   353  		//	26	namelen[2]
   354  		//	28	xlen[2]
   355  		//	30	name[namelen]
   356  		//	30+namelen+xlen - file data
   357  		//
   358  		buf = make([]byte, zheadersize+namelen)
   359  		if err := preadn(fd, buf, off); err != nil ||
   360  			get4(buf) != zheader ||
   361  			get2(buf[8:]) != meth ||
   362  			get2(buf[26:]) != namelen ||
   363  			string(buf[30:30+namelen]) != name {
   364  			return nil, errors.New("corrupt zip file " + zipfile)
   365  		}
   366  		xlen = get2(buf[28:])
   367  
   368  		buf = make([]byte, size)
   369  		if err := preadn(fd, buf, off+30+namelen+xlen); err != nil {
   370  			return nil, errors.New("corrupt zip file " + zipfile)
   371  		}
   372  
   373  		return buf, nil
   374  	}
   375  
   376  	return nil, syscall.ENOENT
   377  }
   378  
   379  // loadTzinfoFromTzdata returns the time zone information of the time zone
   380  // with the given name, from a tzdata database file as they are typically
   381  // found on android.
   382  var loadTzinfoFromTzdata func(file, name string) ([]byte, error)
   383  
   384  // loadTzinfo returns the time zone information of the time zone
   385  // with the given name, from a given source. A source may be a
   386  // timezone database directory, tzdata database file or an uncompressed
   387  // zip file, containing the contents of such a directory.
   388  func loadTzinfo(name string, source string) ([]byte, error) {
   389  	if len(source) >= 6 && source[len(source)-6:] == "tzdata" {
   390  		return loadTzinfoFromTzdata(source, name)
   391  	}
   392  	return loadTzinfoFromDirOrZip(source, name)
   393  }
   394  
   395  // loadLocation returns the Location with the given name from one of
   396  // the specified sources. See loadTzinfo for a list of supported sources.
   397  // The first timezone data matching the given name that is successfully loaded
   398  // and parsed is returned as a Location.
   399  func loadLocation(name string, sources []string) (z *Location, firstErr error) {
   400  	for _, source := range sources {
   401  		var zoneData, err = loadTzinfo(name, source)
   402  		if err == nil {
   403  			if z, err = LoadLocationFromTZData(name, zoneData); err == nil {
   404  				return z, nil
   405  			}
   406  		}
   407  		if firstErr == nil && err != syscall.ENOENT {
   408  			firstErr = err
   409  		}
   410  	}
   411  	if firstErr != nil {
   412  		return nil, firstErr
   413  	}
   414  	return nil, errors.New("unknown time zone " + name)
   415  }
   416  
   417  // readFile reads and returns the content of the named file.
   418  // It is a trivial implementation of ioutil.ReadFile, reimplemented
   419  // here to avoid depending on io/ioutil or os.
   420  // It returns an error if name exceeds maxFileSize bytes.
   421  func readFile(name string) ([]byte, error) {
   422  	f, err := open(name)
   423  	if err != nil {
   424  		return nil, err
   425  	}
   426  	defer closefd(f)
   427  	var (
   428  		buf [4096]byte
   429  		ret []byte
   430  		n   int
   431  	)
   432  	for {
   433  		n, err = read(f, buf[:])
   434  		if n > 0 {
   435  			ret = append(ret, buf[:n]...)
   436  		}
   437  		if n == 0 || err != nil {
   438  			break
   439  		}
   440  		if len(ret) > maxFileSize {
   441  			return nil, fileSizeError(name)
   442  		}
   443  	}
   444  	return ret, err
   445  }
   446  

View as plain text