OSDN Git Service

4dd0f4f434421a8d8b1f473ffa1086e4a0f1c0c0
[pf3gnuchains/gcc-fork.git] / libgo / go / 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         "compress/flate"
10         "encoding/binary"
11         "errors"
12         "hash"
13         "hash/crc32"
14         "io"
15         "io/ioutil"
16         "os"
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 }
30
31 type ReadCloser struct {
32         f *os.File
33         Reader
34 }
35
36 type File struct {
37         FileHeader
38         zipr         io.ReaderAt
39         zipsize      int64
40         headerOffset int64
41 }
42
43 func (f *File) hasDataDescriptor() bool {
44         return f.Flags&0x8 != 0
45 }
46
47 // OpenReader will open the Zip file specified by name and return a ReadCloser.
48 func OpenReader(name string) (*ReadCloser, error) {
49         f, err := os.Open(name)
50         if err != nil {
51                 return nil, err
52         }
53         fi, err := f.Stat()
54         if err != nil {
55                 f.Close()
56                 return nil, err
57         }
58         r := new(ReadCloser)
59         if err := r.init(f, fi.Size()); err != nil {
60                 f.Close()
61                 return nil, err
62         }
63         r.f = f
64         return r, nil
65 }
66
67 // NewReader returns a new Reader reading from r, which is assumed to
68 // have the given size in bytes.
69 func NewReader(r io.ReaderAt, size int64) (*Reader, error) {
70         zr := new(Reader)
71         if err := zr.init(r, size); err != nil {
72                 return nil, err
73         }
74         return zr, nil
75 }
76
77 func (z *Reader) init(r io.ReaderAt, size int64) error {
78         end, err := readDirectoryEnd(r, size)
79         if err != nil {
80                 return err
81         }
82         z.r = r
83         z.File = make([]*File, 0, end.directoryRecords)
84         z.Comment = end.comment
85         rs := io.NewSectionReader(r, 0, size)
86         if _, err = rs.Seek(int64(end.directoryOffset), os.SEEK_SET); err != nil {
87                 return err
88         }
89         buf := bufio.NewReader(rs)
90
91         // The count of files inside a zip is truncated to fit in a uint16.
92         // Gloss over this by reading headers until we encounter
93         // a bad one, and then only report a ErrFormat or UnexpectedEOF if
94         // the file count modulo 65536 is incorrect.
95         for {
96                 f := &File{zipr: r, zipsize: size}
97                 err = readDirectoryHeader(f, buf)
98                 if err == ErrFormat || err == io.ErrUnexpectedEOF {
99                         break
100                 }
101                 if err != nil {
102                         return err
103                 }
104                 z.File = append(z.File, f)
105         }
106         if uint16(len(z.File)) != end.directoryRecords {
107                 // Return the readDirectoryHeader error if we read
108                 // the wrong number of directory entries.
109                 return err
110         }
111         return nil
112 }
113
114 // Close closes the Zip file, rendering it unusable for I/O.
115 func (rc *ReadCloser) Close() error {
116         return rc.f.Close()
117 }
118
119 // Open returns a ReadCloser that provides access to the File's contents.
120 // It is safe to Open and Read from files concurrently.
121 func (f *File) Open() (rc io.ReadCloser, err error) {
122         bodyOffset, err := f.findBodyOffset()
123         if err != nil {
124                 return
125         }
126         size := int64(f.CompressedSize)
127         if size == 0 && f.hasDataDescriptor() {
128                 // permit SectionReader to see the rest of the file
129                 size = f.zipsize - (f.headerOffset + bodyOffset)
130         }
131         r := io.NewSectionReader(f.zipr, f.headerOffset+bodyOffset, size)
132         switch f.Method {
133         case Store: // (no compression)
134                 rc = ioutil.NopCloser(r)
135         case Deflate:
136                 rc = flate.NewReader(r)
137         default:
138                 err = ErrAlgorithm
139         }
140         if rc != nil {
141                 rc = &checksumReader{rc, crc32.NewIEEE(), f, r}
142         }
143         return
144 }
145
146 type checksumReader struct {
147         rc   io.ReadCloser
148         hash hash.Hash32
149         f    *File
150         zipr io.Reader // for reading the data descriptor
151 }
152
153 func (r *checksumReader) Read(b []byte) (n int, err error) {
154         n, err = r.rc.Read(b)
155         r.hash.Write(b[:n])
156         if err != io.EOF {
157                 return
158         }
159         if r.f.hasDataDescriptor() {
160                 if err = readDataDescriptor(r.zipr, r.f); err != nil {
161                         return
162                 }
163         }
164         if r.hash.Sum32() != r.f.CRC32 {
165                 err = ErrChecksum
166         }
167         return
168 }
169
170 func (r *checksumReader) Close() error { return r.rc.Close() }
171
172 func readFileHeader(f *File, r io.Reader) error {
173         var b [fileHeaderLen]byte
174         if _, err := io.ReadFull(r, b[:]); err != nil {
175                 return err
176         }
177         c := binary.LittleEndian
178         if sig := c.Uint32(b[:4]); sig != fileHeaderSignature {
179                 return ErrFormat
180         }
181         f.ReaderVersion = c.Uint16(b[4:6])
182         f.Flags = c.Uint16(b[6:8])
183         f.Method = c.Uint16(b[8:10])
184         f.ModifiedTime = c.Uint16(b[10:12])
185         f.ModifiedDate = c.Uint16(b[12:14])
186         f.CRC32 = c.Uint32(b[14:18])
187         f.CompressedSize = c.Uint32(b[18:22])
188         f.UncompressedSize = c.Uint32(b[22:26])
189         filenameLen := int(c.Uint16(b[26:28]))
190         extraLen := int(c.Uint16(b[28:30]))
191         d := make([]byte, filenameLen+extraLen)
192         if _, err := io.ReadFull(r, d); err != nil {
193                 return err
194         }
195         f.Name = string(d[:filenameLen])
196         f.Extra = d[filenameLen:]
197         return nil
198 }
199
200 // findBodyOffset does the minimum work to verify the file has a header
201 // and returns the file body offset.
202 func (f *File) findBodyOffset() (int64, error) {
203         r := io.NewSectionReader(f.zipr, f.headerOffset, f.zipsize-f.headerOffset)
204         var b [fileHeaderLen]byte
205         if _, err := io.ReadFull(r, b[:]); err != nil {
206                 return 0, err
207         }
208         c := binary.LittleEndian
209         if sig := c.Uint32(b[:4]); sig != fileHeaderSignature {
210                 return 0, ErrFormat
211         }
212         filenameLen := int(c.Uint16(b[26:28]))
213         extraLen := int(c.Uint16(b[28:30]))
214         return int64(fileHeaderLen + filenameLen + extraLen), nil
215 }
216
217 // readDirectoryHeader attempts to read a directory header from r.
218 // It returns io.ErrUnexpectedEOF if it cannot read a complete header,
219 // and ErrFormat if it doesn't find a valid header signature.
220 func readDirectoryHeader(f *File, r io.Reader) error {
221         var b [directoryHeaderLen]byte
222         if _, err := io.ReadFull(r, b[:]); err != nil {
223                 return err
224         }
225         c := binary.LittleEndian
226         if sig := c.Uint32(b[:4]); sig != directoryHeaderSignature {
227                 return ErrFormat
228         }
229         f.CreatorVersion = c.Uint16(b[4:6])
230         f.ReaderVersion = c.Uint16(b[6:8])
231         f.Flags = c.Uint16(b[8:10])
232         f.Method = c.Uint16(b[10:12])
233         f.ModifiedTime = c.Uint16(b[12:14])
234         f.ModifiedDate = c.Uint16(b[14:16])
235         f.CRC32 = c.Uint32(b[16:20])
236         f.CompressedSize = c.Uint32(b[20:24])
237         f.UncompressedSize = c.Uint32(b[24:28])
238         filenameLen := int(c.Uint16(b[28:30]))
239         extraLen := int(c.Uint16(b[30:32]))
240         commentLen := int(c.Uint16(b[32:34]))
241         // startDiskNumber := c.Uint16(b[34:36])    // Unused
242         // internalAttributes := c.Uint16(b[36:38]) // Unused
243         f.ExternalAttrs = c.Uint32(b[38:42])
244         f.headerOffset = int64(c.Uint32(b[42:46]))
245         d := make([]byte, filenameLen+extraLen+commentLen)
246         if _, err := io.ReadFull(r, d); err != nil {
247                 return err
248         }
249         f.Name = string(d[:filenameLen])
250         f.Extra = d[filenameLen : filenameLen+extraLen]
251         f.Comment = string(d[filenameLen+extraLen:])
252         return nil
253 }
254
255 func readDataDescriptor(r io.Reader, f *File) error {
256         var b [dataDescriptorLen]byte
257         if _, err := io.ReadFull(r, b[:]); err != nil {
258                 return err
259         }
260         c := binary.LittleEndian
261         f.CRC32 = c.Uint32(b[:4])
262         f.CompressedSize = c.Uint32(b[4:8])
263         f.UncompressedSize = c.Uint32(b[8:12])
264         return nil
265 }
266
267 func readDirectoryEnd(r io.ReaderAt, size int64) (dir *directoryEnd, err error) {
268         // look for directoryEndSignature in the last 1k, then in the last 65k
269         var b []byte
270         for i, bLen := range []int64{1024, 65 * 1024} {
271                 if bLen > size {
272                         bLen = size
273                 }
274                 b = make([]byte, int(bLen))
275                 if _, err := r.ReadAt(b, size-bLen); err != nil && err != io.EOF {
276                         return nil, err
277                 }
278                 if p := findSignatureInBlock(b); p >= 0 {
279                         b = b[p:]
280                         break
281                 }
282                 if i == 1 || bLen == size {
283                         return nil, ErrFormat
284                 }
285         }
286
287         // read header into struct
288         c := binary.LittleEndian
289         d := new(directoryEnd)
290         d.diskNbr = c.Uint16(b[4:6])
291         d.dirDiskNbr = c.Uint16(b[6:8])
292         d.dirRecordsThisDisk = c.Uint16(b[8:10])
293         d.directoryRecords = c.Uint16(b[10:12])
294         d.directorySize = c.Uint32(b[12:16])
295         d.directoryOffset = c.Uint32(b[16:20])
296         d.commentLen = c.Uint16(b[20:22])
297         d.comment = string(b[22 : 22+int(d.commentLen)])
298         return d, nil
299 }
300
301 func findSignatureInBlock(b []byte) int {
302         for i := len(b) - directoryEndLen; i >= 0; i-- {
303                 // defined from directoryEndSignature in struct.go
304                 if b[i] == 'P' && b[i+1] == 'K' && b[i+2] == 0x05 && b[i+3] == 0x06 {
305                         // n is length of comment
306                         n := int(b[i+directoryEndLen-2]) | int(b[i+directoryEndLen-1])<<8
307                         if n+directoryEndLen+i == len(b) {
308                                 return i
309                         }
310                 }
311         }
312         return -1
313 }