...
Run Format

Source file src/archive/zip/reader.go

Documentation: archive/zip

     1  // Copyright 2010 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  package zip
     6  
     7  import (
     8  	"bufio"
     9  	"encoding/binary"
    10  	"errors"
    11  	"fmt"
    12  	"hash"
    13  	"hash/crc32"
    14  	"io"
    15  	"os"
    16  	"time"
    17  )
    18  
    19  var (
    20  	ErrFormat    = errors.New("zip: not a valid zip file")
    21  	ErrAlgorithm = errors.New("zip: unsupported compression algorithm")
    22  	ErrChecksum  = errors.New("zip: checksum error")
    23  )
    24  
    25  type Reader struct {
    26  	r             io.ReaderAt
    27  	File          []*File
    28  	Comment       string
    29  	decompressors map[uint16]Decompressor
    30  }
    31  
    32  type ReadCloser struct {
    33  	f *os.File
    34  	Reader
    35  }
    36  
    37  type File struct {
    38  	FileHeader
    39  	zip          *Reader
    40  	zipr         io.ReaderAt
    41  	zipsize      int64
    42  	headerOffset int64
    43  }
    44  
    45  func (f *File) hasDataDescriptor() bool {
    46  	return f.Flags&0x8 != 0
    47  }
    48  
    49  // OpenReader will open the Zip file specified by name and return a ReadCloser.
    50  func OpenReader(name string) (*ReadCloser, error) {
    51  	f, err := os.Open(name)
    52  	if err != nil {
    53  		return nil, err
    54  	}
    55  	fi, err := f.Stat()
    56  	if err != nil {
    57  		f.Close()
    58  		return nil, err
    59  	}
    60  	r := new(ReadCloser)
    61  	if err := r.init(f, fi.Size()); err != nil {
    62  		f.Close()
    63  		return nil, err
    64  	}
    65  	r.f = f
    66  	return r, nil
    67  }
    68  
    69  // NewReader returns a new Reader reading from r, which is assumed to
    70  // have the given size in bytes.
    71  func NewReader(r io.ReaderAt, size int64) (*Reader, error) {
    72  	zr := new(Reader)
    73  	if err := zr.init(r, size); err != nil {
    74  		return nil, err
    75  	}
    76  	return zr, nil
    77  }
    78  
    79  func (z *Reader) init(r io.ReaderAt, size int64) error {
    80  	end, err := readDirectoryEnd(r, size)
    81  	if err != nil {
    82  		return err
    83  	}
    84  	if end.directoryRecords > uint64(size)/fileHeaderLen {
    85  		return fmt.Errorf("archive/zip: TOC declares impossible %d files in %d byte zip", end.directoryRecords, size)
    86  	}
    87  	z.r = r
    88  	z.File = make([]*File, 0, end.directoryRecords)
    89  	z.Comment = end.comment
    90  	rs := io.NewSectionReader(r, 0, size)
    91  	if _, err = rs.Seek(int64(end.directoryOffset), io.SeekStart); err != nil {
    92  		return err
    93  	}
    94  	buf := bufio.NewReader(rs)
    95  
    96  	// The count of files inside a zip is truncated to fit in a uint16.
    97  	// Gloss over this by reading headers until we encounter
    98  	// a bad one, and then only report an ErrFormat or UnexpectedEOF if
    99  	// the file count modulo 65536 is incorrect.
   100  	for {
   101  		f := &File{zip: z, zipr: r, zipsize: size}
   102  		err = readDirectoryHeader(f, buf)
   103  		if err == ErrFormat || err == io.ErrUnexpectedEOF {
   104  			break
   105  		}
   106  		if err != nil {
   107  			return err
   108  		}
   109  		z.File = append(z.File, f)
   110  	}
   111  	if uint16(len(z.File)) != uint16(end.directoryRecords) { // only compare 16 bits here
   112  		// Return the readDirectoryHeader error if we read
   113  		// the wrong number of directory entries.
   114  		return err
   115  	}
   116  	return nil
   117  }
   118  
   119  // RegisterDecompressor registers or overrides a custom decompressor for a
   120  // specific method ID. If a decompressor for a given method is not found,
   121  // Reader will default to looking up the decompressor at the package level.
   122  func (z *Reader) RegisterDecompressor(method uint16, dcomp Decompressor) {
   123  	if z.decompressors == nil {
   124  		z.decompressors = make(map[uint16]Decompressor)
   125  	}
   126  	z.decompressors[method] = dcomp
   127  }
   128  
   129  func (z *Reader) decompressor(method uint16) Decompressor {
   130  	dcomp := z.decompressors[method]
   131  	if dcomp == nil {
   132  		dcomp = decompressor(method)
   133  	}
   134  	return dcomp
   135  }
   136  
   137  // Close closes the Zip file, rendering it unusable for I/O.
   138  func (rc *ReadCloser) Close() error {
   139  	return rc.f.Close()
   140  }
   141  
   142  // DataOffset returns the offset of the file's possibly-compressed
   143  // data, relative to the beginning of the zip file.
   144  //
   145  // Most callers should instead use Open, which transparently
   146  // decompresses data and verifies checksums.
   147  func (f *File) DataOffset() (offset int64, err error) {
   148  	bodyOffset, err := f.findBodyOffset()
   149  	if err != nil {
   150  		return
   151  	}
   152  	return f.headerOffset + bodyOffset, nil
   153  }
   154  
   155  // Open returns a ReadCloser that provides access to the File's contents.
   156  // Multiple files may be read concurrently.
   157  func (f *File) Open() (io.ReadCloser, error) {
   158  	bodyOffset, err := f.findBodyOffset()
   159  	if err != nil {
   160  		return nil, err
   161  	}
   162  	size := int64(f.CompressedSize64)
   163  	r := io.NewSectionReader(f.zipr, f.headerOffset+bodyOffset, size)
   164  	dcomp := f.zip.decompressor(f.Method)
   165  	if dcomp == nil {
   166  		return nil, ErrAlgorithm
   167  	}
   168  	var rc io.ReadCloser = dcomp(r)
   169  	var desr io.Reader
   170  	if f.hasDataDescriptor() {
   171  		desr = io.NewSectionReader(f.zipr, f.headerOffset+bodyOffset+size, dataDescriptorLen)
   172  	}
   173  	rc = &checksumReader{
   174  		rc:   rc,
   175  		hash: crc32.NewIEEE(),
   176  		f:    f,
   177  		desr: desr,
   178  	}
   179  	return rc, nil
   180  }
   181  
   182  type checksumReader struct {
   183  	rc    io.ReadCloser
   184  	hash  hash.Hash32
   185  	nread uint64 // number of bytes read so far
   186  	f     *File
   187  	desr  io.Reader // if non-nil, where to read the data descriptor
   188  	err   error     // sticky error
   189  }
   190  
   191  func (r *checksumReader) Read(b []byte) (n int, err error) {
   192  	if r.err != nil {
   193  		return 0, r.err
   194  	}
   195  	n, err = r.rc.Read(b)
   196  	r.hash.Write(b[:n])
   197  	r.nread += uint64(n)
   198  	if err == nil {
   199  		return
   200  	}
   201  	if err == io.EOF {
   202  		if r.nread != r.f.UncompressedSize64 {
   203  			return 0, io.ErrUnexpectedEOF
   204  		}
   205  		if r.desr != nil {
   206  			if err1 := readDataDescriptor(r.desr, r.f); err1 != nil {
   207  				if err1 == io.EOF {
   208  					err = io.ErrUnexpectedEOF
   209  				} else {
   210  					err = err1
   211  				}
   212  			} else if r.hash.Sum32() != r.f.CRC32 {
   213  				err = ErrChecksum
   214  			}
   215  		} else {
   216  			// If there's not a data descriptor, we still compare
   217  			// the CRC32 of what we've read against the file header
   218  			// or TOC's CRC32, if it seems like it was set.
   219  			if r.f.CRC32 != 0 && r.hash.Sum32() != r.f.CRC32 {
   220  				err = ErrChecksum
   221  			}
   222  		}
   223  	}
   224  	r.err = err
   225  	return
   226  }
   227  
   228  func (r *checksumReader) Close() error { return r.rc.Close() }
   229  
   230  // findBodyOffset does the minimum work to verify the file has a header
   231  // and returns the file body offset.
   232  func (f *File) findBodyOffset() (int64, error) {
   233  	var buf [fileHeaderLen]byte
   234  	if _, err := f.zipr.ReadAt(buf[:], f.headerOffset); err != nil {
   235  		return 0, err
   236  	}
   237  	b := readBuf(buf[:])
   238  	if sig := b.uint32(); sig != fileHeaderSignature {
   239  		return 0, ErrFormat
   240  	}
   241  	b = b[22:] // skip over most of the header
   242  	filenameLen := int(b.uint16())
   243  	extraLen := int(b.uint16())
   244  	return int64(fileHeaderLen + filenameLen + extraLen), nil
   245  }
   246  
   247  // readDirectoryHeader attempts to read a directory header from r.
   248  // It returns io.ErrUnexpectedEOF if it cannot read a complete header,
   249  // and ErrFormat if it doesn't find a valid header signature.
   250  func readDirectoryHeader(f *File, r io.Reader) error {
   251  	var buf [directoryHeaderLen]byte
   252  	if _, err := io.ReadFull(r, buf[:]); err != nil {
   253  		return err
   254  	}
   255  	b := readBuf(buf[:])
   256  	if sig := b.uint32(); sig != directoryHeaderSignature {
   257  		return ErrFormat
   258  	}
   259  	f.CreatorVersion = b.uint16()
   260  	f.ReaderVersion = b.uint16()
   261  	f.Flags = b.uint16()
   262  	f.Method = b.uint16()
   263  	f.ModifiedTime = b.uint16()
   264  	f.ModifiedDate = b.uint16()
   265  	f.CRC32 = b.uint32()
   266  	f.CompressedSize = b.uint32()
   267  	f.UncompressedSize = b.uint32()
   268  	f.CompressedSize64 = uint64(f.CompressedSize)
   269  	f.UncompressedSize64 = uint64(f.UncompressedSize)
   270  	filenameLen := int(b.uint16())
   271  	extraLen := int(b.uint16())
   272  	commentLen := int(b.uint16())
   273  	b = b[4:] // skipped start disk number and internal attributes (2x uint16)
   274  	f.ExternalAttrs = b.uint32()
   275  	f.headerOffset = int64(b.uint32())
   276  	d := make([]byte, filenameLen+extraLen+commentLen)
   277  	if _, err := io.ReadFull(r, d); err != nil {
   278  		return err
   279  	}
   280  	f.Name = string(d[:filenameLen])
   281  	f.Extra = d[filenameLen : filenameLen+extraLen]
   282  	f.Comment = string(d[filenameLen+extraLen:])
   283  
   284  	// Determine the character encoding.
   285  	utf8Valid1, utf8Require1 := detectUTF8(f.Name)
   286  	utf8Valid2, utf8Require2 := detectUTF8(f.Comment)
   287  	switch {
   288  	case !utf8Valid1 || !utf8Valid2:
   289  		// Name and Comment definitely not UTF-8.
   290  		f.NonUTF8 = true
   291  	case !utf8Require1 && !utf8Require2:
   292  		// Name and Comment use only single-byte runes that overlap with UTF-8.
   293  		f.NonUTF8 = false
   294  	default:
   295  		// Might be UTF-8, might be some other encoding; preserve existing flag.
   296  		// Some ZIP writers use UTF-8 encoding without setting the UTF-8 flag.
   297  		// Since it is impossible to always distinguish valid UTF-8 from some
   298  		// other encoding (e.g., GBK or Shift-JIS), we trust the flag.
   299  		f.NonUTF8 = f.Flags&0x800 == 0
   300  	}
   301  
   302  	needUSize := f.UncompressedSize == ^uint32(0)
   303  	needCSize := f.CompressedSize == ^uint32(0)
   304  	needHeaderOffset := f.headerOffset == int64(^uint32(0))
   305  
   306  	// Best effort to find what we need.
   307  	// Other zip authors might not even follow the basic format,
   308  	// and we'll just ignore the Extra content in that case.
   309  	var modified time.Time
   310  parseExtras:
   311  	for extra := readBuf(f.Extra); len(extra) >= 4; { // need at least tag and size
   312  		fieldTag := extra.uint16()
   313  		fieldSize := int(extra.uint16())
   314  		if len(extra) < fieldSize {
   315  			break
   316  		}
   317  		fieldBuf := extra.sub(fieldSize)
   318  
   319  		switch fieldTag {
   320  		case zip64ExtraID:
   321  			// update directory values from the zip64 extra block.
   322  			// They should only be consulted if the sizes read earlier
   323  			// are maxed out.
   324  			// See golang.org/issue/13367.
   325  			if needUSize {
   326  				needUSize = false
   327  				if len(fieldBuf) < 8 {
   328  					return ErrFormat
   329  				}
   330  				f.UncompressedSize64 = fieldBuf.uint64()
   331  			}
   332  			if needCSize {
   333  				needCSize = false
   334  				if len(fieldBuf) < 8 {
   335  					return ErrFormat
   336  				}
   337  				f.CompressedSize64 = fieldBuf.uint64()
   338  			}
   339  			if needHeaderOffset {
   340  				needHeaderOffset = false
   341  				if len(fieldBuf) < 8 {
   342  					return ErrFormat
   343  				}
   344  				f.headerOffset = int64(fieldBuf.uint64())
   345  			}
   346  		case ntfsExtraID:
   347  			if len(fieldBuf) < 4 {
   348  				continue parseExtras
   349  			}
   350  			fieldBuf.uint32()        // reserved (ignored)
   351  			for len(fieldBuf) >= 4 { // need at least tag and size
   352  				attrTag := fieldBuf.uint16()
   353  				attrSize := int(fieldBuf.uint16())
   354  				if len(fieldBuf) < attrSize {
   355  					continue parseExtras
   356  				}
   357  				attrBuf := fieldBuf.sub(attrSize)
   358  				if attrTag != 1 || attrSize != 24 {
   359  					continue // Ignore irrelevant attributes
   360  				}
   361  
   362  				const ticksPerSecond = 1e7    // Windows timestamp resolution
   363  				ts := int64(attrBuf.uint64()) // ModTime since Windows epoch
   364  				secs := int64(ts / ticksPerSecond)
   365  				nsecs := (1e9 / ticksPerSecond) * int64(ts%ticksPerSecond)
   366  				epoch := time.Date(1601, time.January, 1, 0, 0, 0, 0, time.UTC)
   367  				modified = time.Unix(epoch.Unix()+secs, nsecs)
   368  			}
   369  		case unixExtraID, infoZipUnixExtraID:
   370  			if len(fieldBuf) < 8 {
   371  				continue parseExtras
   372  			}
   373  			fieldBuf.uint32()              // AcTime (ignored)
   374  			ts := int64(fieldBuf.uint32()) // ModTime since Unix epoch
   375  			modified = time.Unix(ts, 0)
   376  		case extTimeExtraID:
   377  			if len(fieldBuf) < 5 || fieldBuf.uint8()&1 == 0 {
   378  				continue parseExtras
   379  			}
   380  			ts := int64(fieldBuf.uint32()) // ModTime since Unix epoch
   381  			modified = time.Unix(ts, 0)
   382  		}
   383  	}
   384  
   385  	msdosModified := msDosTimeToTime(f.ModifiedDate, f.ModifiedTime)
   386  	f.Modified = msdosModified
   387  	if !modified.IsZero() {
   388  		f.Modified = modified.UTC()
   389  
   390  		// If legacy MS-DOS timestamps are set, we can use the delta between
   391  		// the legacy and extended versions to estimate timezone offset.
   392  		//
   393  		// A non-UTC timezone is always used (even if offset is zero).
   394  		// Thus, FileHeader.Modified.Location() == time.UTC is useful for
   395  		// determining whether extended timestamps are present.
   396  		// This is necessary for users that need to do additional time
   397  		// calculations when dealing with legacy ZIP formats.
   398  		if f.ModifiedTime != 0 || f.ModifiedDate != 0 {
   399  			f.Modified = modified.In(timeZone(msdosModified.Sub(modified)))
   400  		}
   401  	}
   402  
   403  	// Assume that uncompressed size 2³²-1 could plausibly happen in
   404  	// an old zip32 file that was sharding inputs into the largest chunks
   405  	// possible (or is just malicious; search the web for 42.zip).
   406  	// If needUSize is true still, it means we didn't see a zip64 extension.
   407  	// As long as the compressed size is not also 2³²-1 (implausible)
   408  	// and the header is not also 2³²-1 (equally implausible),
   409  	// accept the uncompressed size 2³²-1 as valid.
   410  	// If nothing else, this keeps archive/zip working with 42.zip.
   411  	_ = needUSize
   412  
   413  	if needCSize || needHeaderOffset {
   414  		return ErrFormat
   415  	}
   416  
   417  	return nil
   418  }
   419  
   420  func readDataDescriptor(r io.Reader, f *File) error {
   421  	var buf [dataDescriptorLen]byte
   422  
   423  	// The spec says: "Although not originally assigned a
   424  	// signature, the value 0x08074b50 has commonly been adopted
   425  	// as a signature value for the data descriptor record.
   426  	// Implementers should be aware that ZIP files may be
   427  	// encountered with or without this signature marking data
   428  	// descriptors and should account for either case when reading
   429  	// ZIP files to ensure compatibility."
   430  	//
   431  	// dataDescriptorLen includes the size of the signature but
   432  	// first read just those 4 bytes to see if it exists.
   433  	if _, err := io.ReadFull(r, buf[:4]); err != nil {
   434  		return err
   435  	}
   436  	off := 0
   437  	maybeSig := readBuf(buf[:4])
   438  	if maybeSig.uint32() != dataDescriptorSignature {
   439  		// No data descriptor signature. Keep these four
   440  		// bytes.
   441  		off += 4
   442  	}
   443  	if _, err := io.ReadFull(r, buf[off:12]); err != nil {
   444  		return err
   445  	}
   446  	b := readBuf(buf[:12])
   447  	if b.uint32() != f.CRC32 {
   448  		return ErrChecksum
   449  	}
   450  
   451  	// The two sizes that follow here can be either 32 bits or 64 bits
   452  	// but the spec is not very clear on this and different
   453  	// interpretations has been made causing incompatibilities. We
   454  	// already have the sizes from the central directory so we can
   455  	// just ignore these.
   456  
   457  	return nil
   458  }
   459  
   460  func readDirectoryEnd(r io.ReaderAt, size int64) (dir *directoryEnd, err error) {
   461  	// look for directoryEndSignature in the last 1k, then in the last 65k
   462  	var buf []byte
   463  	var directoryEndOffset int64
   464  	for i, bLen := range []int64{1024, 65 * 1024} {
   465  		if bLen > size {
   466  			bLen = size
   467  		}
   468  		buf = make([]byte, int(bLen))
   469  		if _, err := r.ReadAt(buf, size-bLen); err != nil && err != io.EOF {
   470  			return nil, err
   471  		}
   472  		if p := findSignatureInBlock(buf); p >= 0 {
   473  			buf = buf[p:]
   474  			directoryEndOffset = size - bLen + int64(p)
   475  			break
   476  		}
   477  		if i == 1 || bLen == size {
   478  			return nil, ErrFormat
   479  		}
   480  	}
   481  
   482  	// read header into struct
   483  	b := readBuf(buf[4:]) // skip signature
   484  	d := &directoryEnd{
   485  		diskNbr:            uint32(b.uint16()),
   486  		dirDiskNbr:         uint32(b.uint16()),
   487  		dirRecordsThisDisk: uint64(b.uint16()),
   488  		directoryRecords:   uint64(b.uint16()),
   489  		directorySize:      uint64(b.uint32()),
   490  		directoryOffset:    uint64(b.uint32()),
   491  		commentLen:         b.uint16(),
   492  	}
   493  	l := int(d.commentLen)
   494  	if l > len(b) {
   495  		return nil, errors.New("zip: invalid comment length")
   496  	}
   497  	d.comment = string(b[:l])
   498  
   499  	// These values mean that the file can be a zip64 file
   500  	if d.directoryRecords == 0xffff || d.directorySize == 0xffff || d.directoryOffset == 0xffffffff {
   501  		p, err := findDirectory64End(r, directoryEndOffset)
   502  		if err == nil && p >= 0 {
   503  			err = readDirectory64End(r, p, d)
   504  		}
   505  		if err != nil {
   506  			return nil, err
   507  		}
   508  	}
   509  	// Make sure directoryOffset points to somewhere in our file.
   510  	if o := int64(d.directoryOffset); o < 0 || o >= size {
   511  		return nil, ErrFormat
   512  	}
   513  	return d, nil
   514  }
   515  
   516  // findDirectory64End tries to read the zip64 locator just before the
   517  // directory end and returns the offset of the zip64 directory end if
   518  // found.
   519  func findDirectory64End(r io.ReaderAt, directoryEndOffset int64) (int64, error) {
   520  	locOffset := directoryEndOffset - directory64LocLen
   521  	if locOffset < 0 {
   522  		return -1, nil // no need to look for a header outside the file
   523  	}
   524  	buf := make([]byte, directory64LocLen)
   525  	if _, err := r.ReadAt(buf, locOffset); err != nil {
   526  		return -1, err
   527  	}
   528  	b := readBuf(buf)
   529  	if sig := b.uint32(); sig != directory64LocSignature {
   530  		return -1, nil
   531  	}
   532  	if b.uint32() != 0 { // number of the disk with the start of the zip64 end of central directory
   533  		return -1, nil // the file is not a valid zip64-file
   534  	}
   535  	p := b.uint64()      // relative offset of the zip64 end of central directory record
   536  	if b.uint32() != 1 { // total number of disks
   537  		return -1, nil // the file is not a valid zip64-file
   538  	}
   539  	return int64(p), nil
   540  }
   541  
   542  // readDirectory64End reads the zip64 directory end and updates the
   543  // directory end with the zip64 directory end values.
   544  func readDirectory64End(r io.ReaderAt, offset int64, d *directoryEnd) (err error) {
   545  	buf := make([]byte, directory64EndLen)
   546  	if _, err := r.ReadAt(buf, offset); err != nil {
   547  		return err
   548  	}
   549  
   550  	b := readBuf(buf)
   551  	if sig := b.uint32(); sig != directory64EndSignature {
   552  		return ErrFormat
   553  	}
   554  
   555  	b = b[12:]                        // skip dir size, version and version needed (uint64 + 2x uint16)
   556  	d.diskNbr = b.uint32()            // number of this disk
   557  	d.dirDiskNbr = b.uint32()         // number of the disk with the start of the central directory
   558  	d.dirRecordsThisDisk = b.uint64() // total number of entries in the central directory on this disk
   559  	d.directoryRecords = b.uint64()   // total number of entries in the central directory
   560  	d.directorySize = b.uint64()      // size of the central directory
   561  	d.directoryOffset = b.uint64()    // offset of start of central directory with respect to the starting disk number
   562  
   563  	return nil
   564  }
   565  
   566  func findSignatureInBlock(b []byte) int {
   567  	for i := len(b) - directoryEndLen; i >= 0; i-- {
   568  		// defined from directoryEndSignature in struct.go
   569  		if b[i] == 'P' && b[i+1] == 'K' && b[i+2] == 0x05 && b[i+3] == 0x06 {
   570  			// n is length of comment
   571  			n := int(b[i+directoryEndLen-2]) | int(b[i+directoryEndLen-1])<<8
   572  			if n+directoryEndLen+i <= len(b) {
   573  				return i
   574  			}
   575  		}
   576  	}
   577  	return -1
   578  }
   579  
   580  type readBuf []byte
   581  
   582  func (b *readBuf) uint8() uint8 {
   583  	v := (*b)[0]
   584  	*b = (*b)[1:]
   585  	return v
   586  }
   587  
   588  func (b *readBuf) uint16() uint16 {
   589  	v := binary.LittleEndian.Uint16(*b)
   590  	*b = (*b)[2:]
   591  	return v
   592  }
   593  
   594  func (b *readBuf) uint32() uint32 {
   595  	v := binary.LittleEndian.Uint32(*b)
   596  	*b = (*b)[4:]
   597  	return v
   598  }
   599  
   600  func (b *readBuf) uint64() uint64 {
   601  	v := binary.LittleEndian.Uint64(*b)
   602  	*b = (*b)[8:]
   603  	return v
   604  }
   605  
   606  func (b *readBuf) sub(n int) readBuf {
   607  	b2 := (*b)[:n]
   608  	*b = (*b)[n:]
   609  	return b2
   610  }
   611  

View as plain text