...
Run Format

Source file src/archive/zip/reader.go

     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	)
    17	
    18	var (
    19		ErrFormat    = errors.New("zip: not a valid zip file")
    20		ErrAlgorithm = errors.New("zip: unsupported compression algorithm")
    21		ErrChecksum  = errors.New("zip: checksum error")
    22	)
    23	
    24	type Reader struct {
    25		r             io.ReaderAt
    26		File          []*File
    27		Comment       string
    28		decompressors map[uint16]Decompressor
    29	}
    30	
    31	type ReadCloser struct {
    32		f *os.File
    33		Reader
    34	}
    35	
    36	type File struct {
    37		FileHeader
    38		zip          *Reader
    39		zipr         io.ReaderAt
    40		zipsize      int64
    41		headerOffset int64
    42	}
    43	
    44	func (f *File) hasDataDescriptor() bool {
    45		return f.Flags&0x8 != 0
    46	}
    47	
    48	// OpenReader will open the Zip file specified by name and return a ReadCloser.
    49	func OpenReader(name string) (*ReadCloser, error) {
    50		f, err := os.Open(name)
    51		if err != nil {
    52			return nil, err
    53		}
    54		fi, err := f.Stat()
    55		if err != nil {
    56			f.Close()
    57			return nil, err
    58		}
    59		r := new(ReadCloser)
    60		if err := r.init(f, fi.Size()); err != nil {
    61			f.Close()
    62			return nil, err
    63		}
    64		r.f = f
    65		return r, nil
    66	}
    67	
    68	// NewReader returns a new Reader reading from r, which is assumed to
    69	// have the given size in bytes.
    70	func NewReader(r io.ReaderAt, size int64) (*Reader, error) {
    71		zr := new(Reader)
    72		if err := zr.init(r, size); err != nil {
    73			return nil, err
    74		}
    75		return zr, nil
    76	}
    77	
    78	func (z *Reader) init(r io.ReaderAt, size int64) error {
    79		end, err := readDirectoryEnd(r, size)
    80		if err != nil {
    81			return err
    82		}
    83		if end.directoryRecords > uint64(size)/fileHeaderLen {
    84			return fmt.Errorf("archive/zip: TOC declares impossible %d files in %d byte zip", end.directoryRecords, size)
    85		}
    86		z.r = r
    87		z.File = make([]*File, 0, end.directoryRecords)
    88		z.Comment = end.comment
    89		rs := io.NewSectionReader(r, 0, size)
    90		if _, err = rs.Seek(int64(end.directoryOffset), os.SEEK_SET); err != nil {
    91			return err
    92		}
    93		buf := bufio.NewReader(rs)
    94	
    95		// The count of files inside a zip is truncated to fit in a uint16.
    96		// Gloss over this by reading headers until we encounter
    97		// a bad one, and then only report a ErrFormat or UnexpectedEOF if
    98		// the file count modulo 65536 is incorrect.
    99		for {
   100			f := &File{zip: z, zipr: r, zipsize: size}
   101			err = readDirectoryHeader(f, buf)
   102			if err == ErrFormat || err == io.ErrUnexpectedEOF {
   103				break
   104			}
   105			if err != nil {
   106				return err
   107			}
   108			z.File = append(z.File, f)
   109		}
   110		if uint16(len(z.File)) != uint16(end.directoryRecords) { // only compare 16 bits here
   111			// Return the readDirectoryHeader error if we read
   112			// the wrong number of directory entries.
   113			return err
   114		}
   115		return nil
   116	}
   117	
   118	// RegisterDecompressor registers or overrides a custom decompressor for a
   119	// specific method ID. If a decompressor for a given method is not found,
   120	// Reader will default to looking up the decompressor at the package level.
   121	func (z *Reader) RegisterDecompressor(method uint16, dcomp Decompressor) {
   122		if z.decompressors == nil {
   123			z.decompressors = make(map[uint16]Decompressor)
   124		}
   125		z.decompressors[method] = dcomp
   126	}
   127	
   128	func (z *Reader) decompressor(method uint16) Decompressor {
   129		dcomp := z.decompressors[method]
   130		if dcomp == nil {
   131			dcomp = decompressor(method)
   132		}
   133		return dcomp
   134	}
   135	
   136	// Close closes the Zip file, rendering it unusable for I/O.
   137	func (rc *ReadCloser) Close() error {
   138		return rc.f.Close()
   139	}
   140	
   141	// DataOffset returns the offset of the file's possibly-compressed
   142	// data, relative to the beginning of the zip file.
   143	//
   144	// Most callers should instead use Open, which transparently
   145	// decompresses data and verifies checksums.
   146	func (f *File) DataOffset() (offset int64, err error) {
   147		bodyOffset, err := f.findBodyOffset()
   148		if err != nil {
   149			return
   150		}
   151		return f.headerOffset + bodyOffset, nil
   152	}
   153	
   154	// Open returns a ReadCloser that provides access to the File's contents.
   155	// Multiple files may be read concurrently.
   156	func (f *File) Open() (rc io.ReadCloser, err error) {
   157		bodyOffset, err := f.findBodyOffset()
   158		if err != nil {
   159			return
   160		}
   161		size := int64(f.CompressedSize64)
   162		r := io.NewSectionReader(f.zipr, f.headerOffset+bodyOffset, size)
   163		dcomp := f.zip.decompressor(f.Method)
   164		if dcomp == nil {
   165			err = ErrAlgorithm
   166			return
   167		}
   168		rc = 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
   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		needUSize := f.UncompressedSize == ^uint32(0)
   285		needCSize := f.CompressedSize == ^uint32(0)
   286		needHeaderOffset := f.headerOffset == int64(^uint32(0))
   287	
   288		if len(f.Extra) > 0 {
   289			// Best effort to find what we need.
   290			// Other zip authors might not even follow the basic format,
   291			// and we'll just ignore the Extra content in that case.
   292			b := readBuf(f.Extra)
   293			for len(b) >= 4 { // need at least tag and size
   294				tag := b.uint16()
   295				size := b.uint16()
   296				if int(size) > len(b) {
   297					break
   298				}
   299				if tag == zip64ExtraId {
   300					// update directory values from the zip64 extra block.
   301					// They should only be consulted if the sizes read earlier
   302					// are maxed out.
   303					// See golang.org/issue/13367.
   304					eb := readBuf(b[:size])
   305	
   306					if needUSize {
   307						needUSize = false
   308						if len(eb) < 8 {
   309							return ErrFormat
   310						}
   311						f.UncompressedSize64 = eb.uint64()
   312					}
   313					if needCSize {
   314						needCSize = false
   315						if len(eb) < 8 {
   316							return ErrFormat
   317						}
   318						f.CompressedSize64 = eb.uint64()
   319					}
   320					if needHeaderOffset {
   321						needHeaderOffset = false
   322						if len(eb) < 8 {
   323							return ErrFormat
   324						}
   325						f.headerOffset = int64(eb.uint64())
   326					}
   327					break
   328				}
   329				b = b[size:]
   330			}
   331		}
   332	
   333		// Assume that uncompressed size 2³²-1 could plausibly happen in
   334		// an old zip32 file that was sharding inputs into the largest chunks
   335		// possible (or is just malicious; search the web for 42.zip).
   336		// If needUSize is true still, it means we didn't see a zip64 extension.
   337		// As long as the compressed size is not also 2³²-1 (implausible)
   338		// and the header is not also 2³²-1 (equally implausible),
   339		// accept the uncompressed size 2³²-1 as valid.
   340		// If nothing else, this keeps archive/zip working with 42.zip.
   341		_ = needUSize
   342	
   343		if needCSize || needHeaderOffset {
   344			return ErrFormat
   345		}
   346	
   347		return nil
   348	}
   349	
   350	func readDataDescriptor(r io.Reader, f *File) error {
   351		var buf [dataDescriptorLen]byte
   352	
   353		// The spec says: "Although not originally assigned a
   354		// signature, the value 0x08074b50 has commonly been adopted
   355		// as a signature value for the data descriptor record.
   356		// Implementers should be aware that ZIP files may be
   357		// encountered with or without this signature marking data
   358		// descriptors and should account for either case when reading
   359		// ZIP files to ensure compatibility."
   360		//
   361		// dataDescriptorLen includes the size of the signature but
   362		// first read just those 4 bytes to see if it exists.
   363		if _, err := io.ReadFull(r, buf[:4]); err != nil {
   364			return err
   365		}
   366		off := 0
   367		maybeSig := readBuf(buf[:4])
   368		if maybeSig.uint32() != dataDescriptorSignature {
   369			// No data descriptor signature. Keep these four
   370			// bytes.
   371			off += 4
   372		}
   373		if _, err := io.ReadFull(r, buf[off:12]); err != nil {
   374			return err
   375		}
   376		b := readBuf(buf[:12])
   377		if b.uint32() != f.CRC32 {
   378			return ErrChecksum
   379		}
   380	
   381		// The two sizes that follow here can be either 32 bits or 64 bits
   382		// but the spec is not very clear on this and different
   383		// interpretations has been made causing incompatibilities. We
   384		// already have the sizes from the central directory so we can
   385		// just ignore these.
   386	
   387		return nil
   388	}
   389	
   390	func readDirectoryEnd(r io.ReaderAt, size int64) (dir *directoryEnd, err error) {
   391		// look for directoryEndSignature in the last 1k, then in the last 65k
   392		var buf []byte
   393		var directoryEndOffset int64
   394		for i, bLen := range []int64{1024, 65 * 1024} {
   395			if bLen > size {
   396				bLen = size
   397			}
   398			buf = make([]byte, int(bLen))
   399			if _, err := r.ReadAt(buf, size-bLen); err != nil && err != io.EOF {
   400				return nil, err
   401			}
   402			if p := findSignatureInBlock(buf); p >= 0 {
   403				buf = buf[p:]
   404				directoryEndOffset = size - bLen + int64(p)
   405				break
   406			}
   407			if i == 1 || bLen == size {
   408				return nil, ErrFormat
   409			}
   410		}
   411	
   412		// read header into struct
   413		b := readBuf(buf[4:]) // skip signature
   414		d := &directoryEnd{
   415			diskNbr:            uint32(b.uint16()),
   416			dirDiskNbr:         uint32(b.uint16()),
   417			dirRecordsThisDisk: uint64(b.uint16()),
   418			directoryRecords:   uint64(b.uint16()),
   419			directorySize:      uint64(b.uint32()),
   420			directoryOffset:    uint64(b.uint32()),
   421			commentLen:         b.uint16(),
   422		}
   423		l := int(d.commentLen)
   424		if l > len(b) {
   425			return nil, errors.New("zip: invalid comment length")
   426		}
   427		d.comment = string(b[:l])
   428	
   429		// These values mean that the file can be a zip64 file
   430		if d.directoryRecords == 0xffff || d.directorySize == 0xffff || d.directoryOffset == 0xffffffff {
   431			p, err := findDirectory64End(r, directoryEndOffset)
   432			if err == nil && p >= 0 {
   433				err = readDirectory64End(r, p, d)
   434			}
   435			if err != nil {
   436				return nil, err
   437			}
   438		}
   439		// Make sure directoryOffset points to somewhere in our file.
   440		if o := int64(d.directoryOffset); o < 0 || o >= size {
   441			return nil, ErrFormat
   442		}
   443		return d, nil
   444	}
   445	
   446	// findDirectory64End tries to read the zip64 locator just before the
   447	// directory end and returns the offset of the zip64 directory end if
   448	// found.
   449	func findDirectory64End(r io.ReaderAt, directoryEndOffset int64) (int64, error) {
   450		locOffset := directoryEndOffset - directory64LocLen
   451		if locOffset < 0 {
   452			return -1, nil // no need to look for a header outside the file
   453		}
   454		buf := make([]byte, directory64LocLen)
   455		if _, err := r.ReadAt(buf, locOffset); err != nil {
   456			return -1, err
   457		}
   458		b := readBuf(buf)
   459		if sig := b.uint32(); sig != directory64LocSignature {
   460			return -1, nil
   461		}
   462		if b.uint32() != 0 { // number of the disk with the start of the zip64 end of central directory
   463			return -1, nil // the file is not a valid zip64-file
   464		}
   465		p := b.uint64()      // relative offset of the zip64 end of central directory record
   466		if b.uint32() != 1 { // total number of disks
   467			return -1, nil // the file is not a valid zip64-file
   468		}
   469		return int64(p), nil
   470	}
   471	
   472	// readDirectory64End reads the zip64 directory end and updates the
   473	// directory end with the zip64 directory end values.
   474	func readDirectory64End(r io.ReaderAt, offset int64, d *directoryEnd) (err error) {
   475		buf := make([]byte, directory64EndLen)
   476		if _, err := r.ReadAt(buf, offset); err != nil {
   477			return err
   478		}
   479	
   480		b := readBuf(buf)
   481		if sig := b.uint32(); sig != directory64EndSignature {
   482			return ErrFormat
   483		}
   484	
   485		b = b[12:]                        // skip dir size, version and version needed (uint64 + 2x uint16)
   486		d.diskNbr = b.uint32()            // number of this disk
   487		d.dirDiskNbr = b.uint32()         // number of the disk with the start of the central directory
   488		d.dirRecordsThisDisk = b.uint64() // total number of entries in the central directory on this disk
   489		d.directoryRecords = b.uint64()   // total number of entries in the central directory
   490		d.directorySize = b.uint64()      // size of the central directory
   491		d.directoryOffset = b.uint64()    // offset of start of central directory with respect to the starting disk number
   492	
   493		return nil
   494	}
   495	
   496	func findSignatureInBlock(b []byte) int {
   497		for i := len(b) - directoryEndLen; i >= 0; i-- {
   498			// defined from directoryEndSignature in struct.go
   499			if b[i] == 'P' && b[i+1] == 'K' && b[i+2] == 0x05 && b[i+3] == 0x06 {
   500				// n is length of comment
   501				n := int(b[i+directoryEndLen-2]) | int(b[i+directoryEndLen-1])<<8
   502				if n+directoryEndLen+i <= len(b) {
   503					return i
   504				}
   505			}
   506		}
   507		return -1
   508	}
   509	
   510	type readBuf []byte
   511	
   512	func (b *readBuf) uint16() uint16 {
   513		v := binary.LittleEndian.Uint16(*b)
   514		*b = (*b)[2:]
   515		return v
   516	}
   517	
   518	func (b *readBuf) uint32() uint32 {
   519		v := binary.LittleEndian.Uint32(*b)
   520		*b = (*b)[4:]
   521		return v
   522	}
   523	
   524	func (b *readBuf) uint64() uint64 {
   525		v := binary.LittleEndian.Uint64(*b)
   526		*b = (*b)[8:]
   527		return v
   528	}
   529	

View as plain text