-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathzipreader.go
420 lines (351 loc) · 8.24 KB
/
zipreader.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
package apkparser
import (
"archive/zip"
"encoding/binary"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"sync"
"github.com/klauspost/compress/flate"
)
type zipReaderFileSubEntry struct {
offset int64
method uint16
}
// This struct mimics of Reader from archive/zip. It's purpose is to handle
// even broken archives that Android can read, but archive/zip cannot.
type ZipReader struct {
File map[string]*ZipReaderFile
// Files in the order they were found in the zip. May contain the same ZipReaderFile
// multiple times in case of broken/crafted ZIPs
FilesOrdered []*ZipReaderFile
zipFileReader io.ReadSeeker
ownedZipFile *os.File
}
// This struct mimics of File from archive/zip. The main difference is it can represent
// multiple actual entries in the ZIP file in case it has more than one with the same name.
type ZipReaderFile struct {
Name string
IsDir bool
zipFile io.ReadSeeker
internalReader io.Reader
internalCloser io.Closer
zipEntry *zip.File
entries []zipReaderFileSubEntry
curEntry int
}
// Opens the file(s) for reading. After calling open, you should iterate through all possible entries that
// go by that Filename with for f.Next() { f.Read()... }
func (zr *ZipReaderFile) Open() error {
if zr.internalReader != nil {
return errors.New("File is already opened.")
}
if zr.zipEntry != nil {
var err error
zr.curEntry = 0
rc, err := zr.zipEntry.Open()
if err != nil {
return err
}
zr.internalReader = rc
zr.internalCloser = rc
} else {
zr.curEntry = -1
}
return nil
}
// Reads data from current opened file. Returns io.EOF at the end of current file, but another file entry might exist.
// Use Next() to check for that.
func (zr *ZipReaderFile) Read(p []byte) (int, error) {
if zr.internalReader == nil {
if zr.curEntry == -1 && !zr.Next() {
return 0, io.ErrUnexpectedEOF
}
if zr.curEntry >= len(zr.entries) {
return 0, io.ErrUnexpectedEOF
}
_, err := zr.zipFile.Seek(zr.entries[zr.curEntry].offset, 0)
if err != nil {
return 0, err
}
switch zr.entries[zr.curEntry].method {
case zip.Store:
zr.internalReader = zr.zipFile
default: // case zip.Deflate: // Android treats everything but 0 as deflate
rc := flate.NewReader(zr.zipFile)
zr.internalReader = rc
zr.internalCloser = rc
}
}
return zr.internalReader.Read(p)
}
// Moves this reader to the next file represented under it's Name. Returns false if there are no more to read.
func (zr *ZipReaderFile) Next() bool {
if len(zr.entries) == 0 && zr.internalReader != nil {
zr.curEntry++
return zr.curEntry == 1
}
zr.Close()
if zr.curEntry+1 >= len(zr.entries) {
return false
}
zr.curEntry++
return true
}
// Closes this reader and all opened files.
func (zr *ZipReaderFile) Close() error {
if zr.internalReader != nil {
if zr.internalCloser != nil {
zr.internalCloser.Close()
zr.internalCloser = nil
}
zr.internalReader = nil
}
return nil
}
// Get the file header from ZIP (can return nil with broken archives)
func (zr *ZipReaderFile) ZipHeader() *zip.FileHeader {
if zr.zipEntry != nil {
return &zr.zipEntry.FileHeader
}
return nil
}
// Open, Read all bytes until limit and close the file
func (zr *ZipReaderFile) ReadAll(limit int64) ([]byte, error) {
if err := zr.Open(); err != nil {
return nil, err
}
defer zr.Close()
var data []byte
var lastErr error
for zr.Next() {
data, lastErr = ioutil.ReadAll(io.LimitReader(zr, limit))
if lastErr == nil {
return data, nil
}
}
if lastErr == nil {
return nil, io.ErrUnexpectedEOF
}
return nil, lastErr
}
// Closes this ZIP archive and all it's ZipReaderFile entries.
func (zr *ZipReader) Close() error {
if zr.zipFileReader == nil {
return nil
}
for _, zf := range zr.File {
zf.Close()
}
var err error
if zr.ownedZipFile != nil {
err = zr.ownedZipFile.Close()
zr.ownedZipFile = nil
}
zr.zipFileReader = nil
return err
}
type readAtWrapper struct {
io.ReadSeeker
}
func (wr *readAtWrapper) ReadAt(b []byte, off int64) (n int, err error) {
if readerAt, ok := wr.ReadSeeker.(io.ReaderAt); ok {
return readerAt.ReadAt(b, off)
}
oldpos, err := wr.Seek(off, io.SeekCurrent)
if err != nil {
return
}
if _, err = wr.Seek(off, io.SeekStart); err != nil {
return
}
if n, err = wr.Read(b); err != nil {
return
}
_, err = wr.Seek(oldpos, io.SeekStart)
return
}
// Attempts to open ZIP for reading.
func OpenZip(path string) (zr *ZipReader, err error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
zr, err = OpenZipReader(f)
if err != nil {
f.Close()
} else {
zr.ownedZipFile = f
}
return
}
// Attempts to open ZIP for reading. Might Seek the reader to arbitrary
// positions.
func OpenZipReader(zipReader io.ReadSeeker) (zr *ZipReader, err error) {
zr = &ZipReader{
File: make(map[string]*ZipReaderFile),
zipFileReader: zipReader,
}
f := &readAtWrapper{zipReader}
var zipinfo *zip.Reader
zipinfo, err = tryReadZip(f)
if err == nil {
for i, zf := range zipinfo.File {
// Android treats anything but 0 as deflate.
if zf.Method != zip.Store && zf.Method != zip.Deflate {
zipinfo.File[i].Method = zip.Deflate
}
cl := path.Clean(zf.Name)
if zr.File[cl] == nil {
zf := &ZipReaderFile{
Name: cl,
IsDir: zf.FileInfo().IsDir(),
zipFile: f,
zipEntry: zf,
}
zr.File[cl] = zf
zr.FilesOrdered = append(zr.FilesOrdered, zf)
}
}
return
}
if _, err = f.Seek(0, io.SeekStart); err != nil {
return
}
var off int64
for {
off, err = findNextFileHeader(f)
if off == -1 || err != nil {
return
}
var nameLen, extraLen, method uint16
if _, err = f.Seek(off+8, 0); err != nil {
return
}
if err = binary.Read(f, binary.LittleEndian, &method); err != nil {
return
}
if _, err = f.Seek(off+26, 0); err != nil {
return
}
if err = binary.Read(f, binary.LittleEndian, &nameLen); err != nil {
return
}
if err = binary.Read(f, binary.LittleEndian, &extraLen); err != nil {
return
}
buf := make([]byte, nameLen)
if _, err = f.ReadAt(buf, off+30); err != nil {
return
}
fileName := path.Clean(string(buf))
fileOffset := off + 30 + int64(nameLen) + int64(extraLen)
zrf := zr.File[fileName]
if zrf == nil {
zrf = &ZipReaderFile{
Name: fileName,
zipFile: f,
curEntry: -1,
}
zr.File[fileName] = zrf
}
zr.FilesOrdered = append(zr.FilesOrdered, zrf)
zrf.entries = append([]zipReaderFileSubEntry{zipReaderFileSubEntry{
offset: fileOffset,
method: method,
}}, zrf.entries...)
if _, err = f.Seek(off+4, 0); err != nil {
return
}
}
}
func tryReadZip(f *readAtWrapper) (r *zip.Reader, err error) {
defer func() {
if pn := recover(); pn != nil {
err = fmt.Errorf("%v", pn)
r = nil
}
}()
size, err := f.Seek(0, io.SeekEnd)
if err != nil {
return
}
r, err = zip.NewReader(f, size)
if err != nil {
return
}
r.RegisterDecompressor(zip.Deflate, newFlateReader)
return
}
func findNextFileHeader(f io.ReadSeeker) (offset int64, err error) {
start, err := f.Seek(0, 1)
if err != nil {
return -1, err
}
defer func() {
if _, serr := f.Seek(start, 0); serr != nil && err == nil {
err = serr
}
}()
buf := make([]byte, 64*1024)
toCmp := []byte{0x50, 0x4B, 0x03, 0x04}
ok := 0
offset = start
for {
n, err := f.Read(buf)
if err != nil && err != io.EOF {
return -1, err
}
if n == 0 {
return -1, nil
}
for i := 0; i < n; i++ {
if buf[i] == toCmp[ok] {
ok++
if ok == len(toCmp) {
offset += int64(i) - int64(len(toCmp)-1)
return offset, nil
}
} else {
ok = 0
}
}
offset += int64(n)
}
}
var flateReaderPool sync.Pool
func newFlateReader(r io.Reader) io.ReadCloser {
fr, ok := flateReaderPool.Get().(io.ReadCloser)
if ok {
fr.(flate.Resetter).Reset(r, nil)
} else {
fr = flate.NewReader(r)
}
return &pooledFlateReader{fr: fr}
}
type pooledFlateReader struct {
mu sync.Mutex // guards Close and Read
fr io.ReadCloser
}
func (r *pooledFlateReader) Read(p []byte) (n int, err error) {
r.mu.Lock()
defer r.mu.Unlock()
if r.fr == nil {
return 0, errors.New("Read after Close")
}
return r.fr.Read(p)
}
func (r *pooledFlateReader) Close() error {
r.mu.Lock()
defer r.mu.Unlock()
var err error
if r.fr != nil {
err = r.fr.Close()
flateReaderPool.Put(r.fr)
r.fr = nil
}
return err
}