]> Cypherpunks.ru repositories - gostls13.git/blobdiff - src/time/zoneinfo_read.go
time: read 64-bit data if available
[gostls13.git] / src / time / zoneinfo_read.go
index d8d4070d5b51a1661344c11cf0e0fa0df39c920d..d54632fb49052f2288df9b3cc7f32fa6b5c57839 100644 (file)
@@ -59,6 +59,16 @@ func (d *dataIO) big4() (n uint32, ok bool) {
        return uint32(p[3]) | uint32(p[2])<<8 | uint32(p[1])<<16 | uint32(p[0])<<24, true
 }
 
+func (d *dataIO) big8() (n uint64, ok bool) {
+       n1, ok1 := d.big4()
+       n2, ok2 := d.big4()
+       if !ok1 || !ok2 {
+               d.error = true
+               return 0, false
+       }
+       return (uint64(n1) << 32) | uint64(n2), true
+}
+
 func (d *dataIO) byte() (n byte, ok bool) {
        p := d.read(1)
        if len(p) < 1 {
@@ -93,9 +103,21 @@ func LoadLocationFromTZData(name string, data []byte) (*Location, error) {
        }
 
        // 1-byte version, then 15 bytes of padding
+       var version int
        var p []byte
-       if p = d.read(16); len(p) != 16 || p[0] != 0 && p[0] != '2' && p[0] != '3' {
+       if p = d.read(16); len(p) != 16 {
                return nil, badData
+       } else {
+               switch p[0] {
+               case 0:
+                       version = 1
+               case '2':
+                       version = 2
+               case '3':
+                       version = 3
+               default:
+                       return nil, badData
+               }
        }
 
        // six big-endian 32-bit integers:
@@ -119,11 +141,53 @@ func LoadLocationFromTZData(name string, data []byte) (*Location, error) {
                if !ok {
                        return nil, badData
                }
+               if uint32(int(nn)) != nn {
+                       return nil, badData
+               }
                n[i] = int(nn)
        }
 
+       // If we have version 2 or 3, then the data is first written out
+       // in a 32-bit format, then written out again in a 64-bit format.
+       // Skip the 32-bit format and read the 64-bit one, as it can
+       // describe a broader range of dates.
+
+       is64 := false
+       if version > 1 {
+               // Skip the 32-bit data.
+               skip := n[NTime]*4 +
+                       n[NTime] +
+                       n[NZone]*6 +
+                       n[NChar] +
+                       n[NLeap]*8 +
+                       n[NStdWall] +
+                       n[NUTCLocal]
+               // Skip the version 2 header that we just read.
+               skip += 4 + 16
+               d.read(skip)
+
+               is64 = true
+
+               // Read the counts again, they can differ.
+               for i := 0; i < 6; i++ {
+                       nn, ok := d.big4()
+                       if !ok {
+                               return nil, badData
+                       }
+                       if uint32(int(nn)) != nn {
+                               return nil, badData
+                       }
+                       n[i] = int(nn)
+               }
+       }
+
+       size := 4
+       if is64 {
+               size = 8
+       }
+
        // Transition times.
-       txtimes := dataIO{d.read(n[NTime] * 4), false}
+       txtimes := dataIO{d.read(n[NTime] * size), false}
 
        // Time zone indices for transition times.
        txzones := d.read(n[NTime])
@@ -135,7 +199,7 @@ func LoadLocationFromTZData(name string, data []byte) (*Location, error) {
        abbrev := d.read(n[NChar])
 
        // Leap-second time pairs
-       d.read(n[NLeap] * 8)
+       d.read(n[NLeap] * (size + 4))
 
        // Whether tx times associated with local time types
        // are specified as standard time or wall time.
@@ -149,10 +213,6 @@ func LoadLocationFromTZData(name string, data []byte) (*Location, error) {
                return nil, badData
        }
 
-       // If version == 2 or 3, the entire file repeats, this time using
-       // 8-byte ints for txtimes and leap seconds.
-       // We won't need those until 2106.
-
        // Now we can build up a useful data structure.
        // First the zone information.
        //      utcoff[4] isdst[1] nameindex[1]
@@ -163,6 +223,9 @@ func LoadLocationFromTZData(name string, data []byte) (*Location, error) {
                if n, ok = zonedata.big4(); !ok {
                        return nil, badData
                }
+               if uint32(int(n)) != n {
+                       return nil, badData
+               }
                zone[i].offset = int(int32(n))
                var b byte
                if b, ok = zonedata.byte(); !ok {
@@ -186,12 +249,21 @@ func LoadLocationFromTZData(name string, data []byte) (*Location, error) {
        // Now the transition time info.
        tx := make([]zoneTrans, n[NTime])
        for i := range tx {
-               var ok bool
-               var n uint32
-               if n, ok = txtimes.big4(); !ok {
-                       return nil, badData
+               var n int64
+               if !is64 {
+                       if n4, ok := txtimes.big4(); !ok {
+                               return nil, badData
+                       } else {
+                               n = int64(int32(n4))
+                       }
+               } else {
+                       if n8, ok := txtimes.big8(); !ok {
+                               return nil, badData
+                       } else {
+                               n = int64(n8)
+                       }
                }
-               tx[i].when = int64(int32(n))
+               tx[i].when = n
                if int(txzones[i]) >= len(zone) {
                        return nil, badData
                }