OLD | NEW |
1 // Copyright 2010 The Go Authors. All rights reserved. | 1 // Copyright 2010 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package zip | 5 package zip |
6 | 6 |
7 import ( | 7 import ( |
8 "bufio" | 8 "bufio" |
9 "compress/flate" | 9 "compress/flate" |
10 "encoding/binary" | 10 "encoding/binary" |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 } | 162 } |
163 } | 163 } |
164 if r.hash.Sum32() != r.f.CRC32 { | 164 if r.hash.Sum32() != r.f.CRC32 { |
165 err = ErrChecksum | 165 err = ErrChecksum |
166 } | 166 } |
167 return | 167 return |
168 } | 168 } |
169 | 169 |
170 func (r *checksumReader) Close() error { return r.rc.Close() } | 170 func (r *checksumReader) Close() error { return r.rc.Close() } |
171 | 171 |
172 func readFileHeader(f *File, r io.Reader) error { | |
173 var buf [fileHeaderLen]byte | |
174 if _, err := io.ReadFull(r, buf[:]); err != nil { | |
175 return err | |
176 } | |
177 b := readBuf(buf[:]) | |
178 if sig := b.uint32(); sig != fileHeaderSignature { | |
179 return ErrFormat | |
180 } | |
181 f.ReaderVersion = b.uint16() | |
182 f.Flags = b.uint16() | |
183 f.Method = b.uint16() | |
184 f.ModifiedTime = b.uint16() | |
185 f.ModifiedDate = b.uint16() | |
186 f.CRC32 = b.uint32() | |
187 f.CompressedSize = b.uint32() | |
188 f.UncompressedSize = b.uint32() | |
189 filenameLen := int(b.uint16()) | |
190 extraLen := int(b.uint16()) | |
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 | 172 // findBodyOffset does the minimum work to verify the file has a header |
201 // and returns the file body offset. | 173 // and returns the file body offset. |
202 func (f *File) findBodyOffset() (int64, error) { | 174 func (f *File) findBodyOffset() (int64, error) { |
203 r := io.NewSectionReader(f.zipr, f.headerOffset, f.zipsize-f.headerOffse
t) | 175 r := io.NewSectionReader(f.zipr, f.headerOffset, f.zipsize-f.headerOffse
t) |
204 var buf [fileHeaderLen]byte | 176 var buf [fileHeaderLen]byte |
205 if _, err := io.ReadFull(r, buf[:]); err != nil { | 177 if _, err := io.ReadFull(r, buf[:]); err != nil { |
206 return 0, err | 178 return 0, err |
207 } | 179 } |
208 b := readBuf(buf[:]) | 180 b := readBuf(buf[:]) |
209 if sig := b.uint32(); sig != fileHeaderSignature { | 181 if sig := b.uint32(); sig != fileHeaderSignature { |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
323 v := binary.LittleEndian.Uint16(*b) | 295 v := binary.LittleEndian.Uint16(*b) |
324 *b = (*b)[2:] | 296 *b = (*b)[2:] |
325 return v | 297 return v |
326 } | 298 } |
327 | 299 |
328 func (b *readBuf) uint32() uint32 { | 300 func (b *readBuf) uint32() uint32 { |
329 v := binary.LittleEndian.Uint32(*b) | 301 v := binary.LittleEndian.Uint32(*b) |
330 *b = (*b)[4:] | 302 *b = (*b)[4:] |
331 return v | 303 return v |
332 } | 304 } |
OLD | NEW |