Debian Codegraph @

zipreader.go

raw symbols SCIP
1 package apkparser
2
3 import (
4 "archive/zip"
5 "compress/flate"
6 "encoding/binary"
7 "errors"
8 "fmt"
9 "io"
10 "os"
11 "path"
12 )
13
14 type zipReaderFileSubEntry struct {
15 offset int64
16 method uint16
17 }
18
19 // This struct mimics of Reader from archive/zip. It's purpose is to handle
20 // even broken archives that Android can read, but archive/zip cannot.
21 type ZipReader struct {
22 File map[string]*ZipReaderFile
23
24 // Files in the order they were found in the zip. May contain the same ZipReaderFile
25 // multiple times in case of broken/crafted ZIPs
26 FilesOrdered []*ZipReaderFile
27
28 zipFile *os.File
29 }
30
31 // This struct mimics of File from archive/zip. The main difference is it can represent
32 // multiple actual entries in the ZIP file in case it has more than one with the same name.
33 type ZipReaderFile struct {
34 Name string
35 IsDir bool
36
37 zipFile *os.File
38 internalReader io.ReadCloser
39
40 zipEntry *zip.File
41
42 entries []zipReaderFileSubEntry
43 curEntry int
44 }
45
46 // Opens the file(s) for reading. After calling open, you should iterate through all possible entries that
47 // go by that Filename with for f.Next() { f.Read()... }
48 func (zr *ZipReaderFile) Open() error {
49 if zr.internalReader != nil {
50 return errors.New("File is already opened.")
51 }
52
53 if zr.zipEntry != nil {
54 var err error
55 zr.curEntry = 0
56 zr.internalReader, err = zr.zipEntry.Open()
57 return err
58 } else {
59 zr.curEntry = -1
60 }
61
62 return nil
63 }
64
65 // Reads data from current opened file. Returns io.EOF at the end of current file, but another file entry might exist.
66 // Use Next() to check for that.
67 func (zr *ZipReaderFile) Read(p []byte) (int, error) {
68 if zr.internalReader == nil {
69 if zr.curEntry == -1 && !zr.Next() {
70 return 0, io.ErrUnexpectedEOF
71 }
72
73 if zr.curEntry >= len(zr.entries) {
74 return 0, io.ErrUnexpectedEOF
75 }
76
77 _, err := zr.zipFile.Seek(zr.entries[zr.curEntry].offset, 0)
78 if err != nil {
79 return 0, err
80 }
81
82 switch zr.entries[zr.curEntry].method {
83 case zip.Store:
84 zr.internalReader = zr.zipFile
85 default: // case zip.Deflate: // Android treats everything but 0 as deflate
86 zr.internalReader = flate.NewReader(zr.zipFile)
87 }
88 }
89 return zr.internalReader.Read(p)
90 }
91
92 // Moves this reader to the next file represented under it's Name. Returns false if there are no more to read.
93 func (zr *ZipReaderFile) Next() bool {
94 if len(zr.entries) == 0 && zr.internalReader != nil {
95 zr.curEntry++
96 return zr.curEntry == 1
97 }
98
99 if zr.internalReader != nil {
100 if zr.internalReader != zr.zipFile {
101 zr.internalReader.Close()
102 }
103 zr.internalReader = nil
104 }
105
106 if zr.curEntry+1 >= len(zr.entries) {
107 return false
108 }
109 zr.curEntry++
110 return true
111 }
112
113 // Closes this reader and all opened files.
114 func (zr *ZipReaderFile) Close() error {
115 if zr.internalReader != nil {
116 if zr.internalReader != zr.zipFile {
117 zr.internalReader.Close()
118 }
119 zr.internalReader = nil
120 }
121 return nil
122 }
123
124 // Get the file header from ZIP (can return nil with broken archives)
125 func (zr *ZipReaderFile) ZipHeader() *zip.FileHeader {
126 if zr.zipEntry != nil {
127 return &zr.zipEntry.FileHeader
128 }
129 return nil
130 }
131
132 // Closes this ZIP archive and all it's ZipReaderFile entries.
133 func (zr *ZipReader) Close() error {
134 if zr.zipFile == nil {
135 return nil
136 }
137
138 for _, zf := range zr.File {
139 zf.Close()
140 }
141
142 err := zr.zipFile.Close()
143 zr.zipFile = nil
144 return err
145 }
146
147 // Attempts to open ZIP for reading.
148 func OpenZip(zippath string) (zr *ZipReader, err error) {
149 f, err := os.Open(zippath)
150 if err != nil {
151 return
152 }
153
154 defer func() {
155 if err != nil {
156 zr = nil
157 f.Close()
158 }
159 }()
160
161 zr = &ZipReader{
162 File: make(map[string]*ZipReaderFile),
163 zipFile: f,
164 }
165
166 var zipinfo *zip.Reader
167 zipinfo, err = tryReadZip(f)
168 if err == nil {
169 for i, zf := range zipinfo.File {
170 // Android treats anything but 0 as deflate.
171 if zf.Method != zip.Store && zf.Method != zip.Deflate {
172 zipinfo.File[i].Method = zip.Deflate
173 }
174
175 cl := path.Clean(zf.Name)
176 if zr.File[cl] == nil {
177 zf := &ZipReaderFile{
178 Name: cl,
179 IsDir: zf.FileInfo().IsDir(),
180 zipFile: f,
181 zipEntry: zf,
182 }
183 zr.File[cl] = zf
184 zr.FilesOrdered = append(zr.FilesOrdered, zf)
185 }
186 }
187 return
188 }
189
190 if _, err = f.Seek(0, 0); err != nil {
191 return
192 }
193
194 var off int64
195 for {
196 off, err = findNextFileHeader(f)
197 if off == -1 || err != nil {
198 return
199 }
200
201 var nameLen, extraLen, method uint16
202 if _, err = f.Seek(off+8, 0); err != nil {
203 return
204 }
205
206 if err = binary.Read(f, binary.LittleEndian, &method); err != nil {
207 return
208 }
209
210 if _, err = f.Seek(off+26, 0); err != nil {
211 return
212 }
213
214 if err = binary.Read(f, binary.LittleEndian, &nameLen); err != nil {
215 return
216 }
217
218 if err = binary.Read(f, binary.LittleEndian, &extraLen); err != nil {
219 return
220 }
221
222 buf := make([]byte, nameLen)
223 if _, err = f.ReadAt(buf, off+30); err != nil {
224 return
225 }
226
227 fileName := path.Clean(string(buf))
228 fileOffset := off + 30 + int64(nameLen) + int64(extraLen)
229
230 zrf := zr.File[fileName]
231 if zrf == nil {
232 zrf = &ZipReaderFile{
233 Name: fileName,
234 zipFile: f,
235 curEntry: -1,
236 }
237 zr.File[fileName] = zrf
238 }
239 zr.FilesOrdered = append(zr.FilesOrdered, zrf)
240
241 zrf.entries = append([]zipReaderFileSubEntry{zipReaderFileSubEntry{
242 offset: fileOffset,
243 method: method,
244 }}, zrf.entries...)
245
246 if _, err = f.Seek(off+4, 0); err != nil {
247 return
248 }
249 }
250 }
251
252 func tryReadZip(f *os.File) (r *zip.Reader, err error) {
253 defer func() {
254 if pn := recover(); pn != nil {
255 err = fmt.Errorf("%v", pn)
256 r = nil
257 }
258 }()
259
260 fi, err := f.Stat()
261 if err != nil {
262 return
263 }
264
265 r, err = zip.NewReader(f, fi.Size())
266 return
267 }
268
269 func findNextFileHeader(f *os.File) (offset int64, err error) {
270 start, err := f.Seek(0, 1)
271 if err != nil {
272 return -1, err
273 }
274 defer func() {
275 if _, serr := f.Seek(start, 0); serr != nil && err == nil {
276 err = serr
277 }
278 }()
279
280 buf := make([]byte, 64*1024)
281 toCmp := []byte{0x50, 0x4B, 0x03, 0x04}
282
283 ok := 0
284 offset = start
285
286 for {
287 n, err := f.Read(buf)
288 if err != nil && err != io.EOF {
289 return -1, err
290 }
291
292 if n == 0 {
293 return -1, nil
294 }
295
296 for i := 0; i < n; i++ {
297 if buf[i] == toCmp[ok] {
298 ok++
299 if ok == len(toCmp) {
300 offset += int64(i) - int64(len(toCmp)-1)
301 return offset, nil
302 }
303 } else {
304 ok = 0
305 }
306 }
307
308 offset += int64(n)
309 }
310 }