1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Copyright 2006 The Android Open Source Project
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Simple Zip archive support.
5*4882a593Smuzhiyun */
6*4882a593Smuzhiyun #ifndef _MINZIP_ZIP
7*4882a593Smuzhiyun #define _MINZIP_ZIP
8*4882a593Smuzhiyun
9*4882a593Smuzhiyun #include <stdlib.h>
10*4882a593Smuzhiyun #include <utime.h>
11*4882a593Smuzhiyun
12*4882a593Smuzhiyun #include "Hash.h"
13*4882a593Smuzhiyun #include "SysUtil.h"
14*4882a593Smuzhiyun
15*4882a593Smuzhiyun /*
16*4882a593Smuzhiyun * One entry in the Zip archive. Treat this as opaque -- use accessors below.
17*4882a593Smuzhiyun *
18*4882a593Smuzhiyun * TODO: we're now keeping the pages mapped so we don't have to copy the
19*4882a593Smuzhiyun * filename. We can change the accessors to retrieve the various pieces
20*4882a593Smuzhiyun * directly from the source file instead of copying them out, for a very
21*4882a593Smuzhiyun * slight speed hit and a modest reduction in memory usage.
22*4882a593Smuzhiyun */
23*4882a593Smuzhiyun typedef struct ZipEntry {
24*4882a593Smuzhiyun unsigned int fileNameLen;
25*4882a593Smuzhiyun const char* fileName; // not null-terminated
26*4882a593Smuzhiyun long offset;
27*4882a593Smuzhiyun long compLen;
28*4882a593Smuzhiyun long uncompLen;
29*4882a593Smuzhiyun int compression;
30*4882a593Smuzhiyun long modTime;
31*4882a593Smuzhiyun long crc32;
32*4882a593Smuzhiyun int versionMadeBy;
33*4882a593Smuzhiyun long externalFileAttributes;
34*4882a593Smuzhiyun } ZipEntry;
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun /*
37*4882a593Smuzhiyun * One Zip archive. Treat as opaque.
38*4882a593Smuzhiyun */
39*4882a593Smuzhiyun typedef struct ZipArchive {
40*4882a593Smuzhiyun int fd;
41*4882a593Smuzhiyun unsigned int numEntries;
42*4882a593Smuzhiyun ZipEntry* pEntries;
43*4882a593Smuzhiyun HashTable* pHash; // maps file name to ZipEntry
44*4882a593Smuzhiyun MemMapping map;
45*4882a593Smuzhiyun } ZipArchive;
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun /*
48*4882a593Smuzhiyun * Represents a non-NUL-terminated string,
49*4882a593Smuzhiyun * which is how entry names are stored.
50*4882a593Smuzhiyun */
51*4882a593Smuzhiyun typedef struct {
52*4882a593Smuzhiyun const char *str;
53*4882a593Smuzhiyun size_t len;
54*4882a593Smuzhiyun } UnterminatedString;
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun /*
57*4882a593Smuzhiyun * Open a Zip archive.
58*4882a593Smuzhiyun *
59*4882a593Smuzhiyun * On success, returns 0 and populates "pArchive". Returns nonzero errno
60*4882a593Smuzhiyun * value on failure.
61*4882a593Smuzhiyun */
62*4882a593Smuzhiyun int mzOpenZipArchive(const char* fileName, ZipArchive* pArchive);
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun /*
65*4882a593Smuzhiyun * Close archive, releasing resources associated with it.
66*4882a593Smuzhiyun *
67*4882a593Smuzhiyun * Depending on the implementation this could unmap pages used by classes
68*4882a593Smuzhiyun * stored in a Jar. This should only be done after unloading classes.
69*4882a593Smuzhiyun */
70*4882a593Smuzhiyun void mzCloseZipArchive(ZipArchive* pArchive);
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun /*
74*4882a593Smuzhiyun * Find an entry in the Zip archive, by name.
75*4882a593Smuzhiyun */
76*4882a593Smuzhiyun const ZipEntry* mzFindZipEntry(const ZipArchive* pArchive,
77*4882a593Smuzhiyun const char* entryName);
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun /*
80*4882a593Smuzhiyun * Get the number of entries in the Zip archive.
81*4882a593Smuzhiyun */
mzZipEntryCount(const ZipArchive * pArchive)82*4882a593Smuzhiyun INLINE unsigned int mzZipEntryCount(const ZipArchive* pArchive)
83*4882a593Smuzhiyun {
84*4882a593Smuzhiyun return pArchive->numEntries;
85*4882a593Smuzhiyun }
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun /*
88*4882a593Smuzhiyun * Get an entry by index. Returns NULL if the index is out-of-bounds.
89*4882a593Smuzhiyun */
90*4882a593Smuzhiyun INLINE const ZipEntry*
mzGetZipEntryAt(const ZipArchive * pArchive,unsigned int index)91*4882a593Smuzhiyun mzGetZipEntryAt(const ZipArchive* pArchive, unsigned int index)
92*4882a593Smuzhiyun {
93*4882a593Smuzhiyun if (index < pArchive->numEntries) {
94*4882a593Smuzhiyun return pArchive->pEntries + index;
95*4882a593Smuzhiyun }
96*4882a593Smuzhiyun return NULL;
97*4882a593Smuzhiyun }
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun /*
100*4882a593Smuzhiyun * Get the index number of an entry in the archive.
101*4882a593Smuzhiyun */
102*4882a593Smuzhiyun INLINE unsigned int
mzGetZipEntryIndex(const ZipArchive * pArchive,const ZipEntry * pEntry)103*4882a593Smuzhiyun mzGetZipEntryIndex(const ZipArchive *pArchive, const ZipEntry *pEntry)
104*4882a593Smuzhiyun {
105*4882a593Smuzhiyun return pEntry - pArchive->pEntries;
106*4882a593Smuzhiyun }
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun /*
109*4882a593Smuzhiyun * Simple accessors.
110*4882a593Smuzhiyun */
mzGetZipEntryFileName(const ZipEntry * pEntry)111*4882a593Smuzhiyun INLINE UnterminatedString mzGetZipEntryFileName(const ZipEntry* pEntry)
112*4882a593Smuzhiyun {
113*4882a593Smuzhiyun UnterminatedString ret;
114*4882a593Smuzhiyun ret.str = pEntry->fileName;
115*4882a593Smuzhiyun ret.len = pEntry->fileNameLen;
116*4882a593Smuzhiyun return ret;
117*4882a593Smuzhiyun }
mzGetZipEntryOffset(const ZipEntry * pEntry)118*4882a593Smuzhiyun INLINE long mzGetZipEntryOffset(const ZipEntry* pEntry)
119*4882a593Smuzhiyun {
120*4882a593Smuzhiyun return pEntry->offset;
121*4882a593Smuzhiyun }
mzGetZipEntryUncompLen(const ZipEntry * pEntry)122*4882a593Smuzhiyun INLINE long mzGetZipEntryUncompLen(const ZipEntry* pEntry)
123*4882a593Smuzhiyun {
124*4882a593Smuzhiyun return pEntry->uncompLen;
125*4882a593Smuzhiyun }
mzGetZipEntryModTime(const ZipEntry * pEntry)126*4882a593Smuzhiyun INLINE long mzGetZipEntryModTime(const ZipEntry* pEntry)
127*4882a593Smuzhiyun {
128*4882a593Smuzhiyun return pEntry->modTime;
129*4882a593Smuzhiyun }
mzGetZipEntryCrc32(const ZipEntry * pEntry)130*4882a593Smuzhiyun INLINE long mzGetZipEntryCrc32(const ZipEntry* pEntry)
131*4882a593Smuzhiyun {
132*4882a593Smuzhiyun return pEntry->crc32;
133*4882a593Smuzhiyun }
134*4882a593Smuzhiyun bool mzIsZipEntrySymlink(const ZipEntry* pEntry);
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun
137*4882a593Smuzhiyun /*
138*4882a593Smuzhiyun * Type definition for the callback function used by
139*4882a593Smuzhiyun * mzProcessZipEntryContents().
140*4882a593Smuzhiyun */
141*4882a593Smuzhiyun typedef bool (*ProcessZipEntryContentsFunction)(const unsigned char *data,
142*4882a593Smuzhiyun int dataLen, void *cookie);
143*4882a593Smuzhiyun
144*4882a593Smuzhiyun /*
145*4882a593Smuzhiyun * Stream the uncompressed data through the supplied function,
146*4882a593Smuzhiyun * passing cookie to it each time it gets called. processFunction
147*4882a593Smuzhiyun * may be called more than once.
148*4882a593Smuzhiyun *
149*4882a593Smuzhiyun * If processFunction returns false, the operation is abandoned and
150*4882a593Smuzhiyun * mzProcessZipEntryContents() immediately returns false.
151*4882a593Smuzhiyun *
152*4882a593Smuzhiyun * This is useful for calculating the hash of an entry's uncompressed contents.
153*4882a593Smuzhiyun */
154*4882a593Smuzhiyun bool mzProcessZipEntryContents(const ZipArchive *pArchive,
155*4882a593Smuzhiyun const ZipEntry *pEntry, ProcessZipEntryContentsFunction processFunction,
156*4882a593Smuzhiyun void *cookie);
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun /*
159*4882a593Smuzhiyun * Read an entry into a buffer allocated by the caller.
160*4882a593Smuzhiyun */
161*4882a593Smuzhiyun bool mzReadZipEntry(const ZipArchive* pArchive, const ZipEntry* pEntry,
162*4882a593Smuzhiyun char* buf, int bufLen);
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun /*
165*4882a593Smuzhiyun * Check the CRC on this entry; return true if it is correct.
166*4882a593Smuzhiyun * May do other internal checks as well.
167*4882a593Smuzhiyun */
168*4882a593Smuzhiyun bool mzIsZipEntryIntact(const ZipArchive *pArchive, const ZipEntry *pEntry);
169*4882a593Smuzhiyun
170*4882a593Smuzhiyun /*
171*4882a593Smuzhiyun * Inflate and write an entry to a file.
172*4882a593Smuzhiyun */
173*4882a593Smuzhiyun bool mzExtractZipEntryToFile(const ZipArchive *pArchive,
174*4882a593Smuzhiyun const ZipEntry *pEntry, int fd);
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun /*
177*4882a593Smuzhiyun * Inflate and write an entry to a memory buffer, which must be long
178*4882a593Smuzhiyun * enough to hold mzGetZipEntryUncomplen(pEntry) bytes.
179*4882a593Smuzhiyun */
180*4882a593Smuzhiyun bool mzExtractZipEntryToBuffer(const ZipArchive *pArchive,
181*4882a593Smuzhiyun const ZipEntry *pEntry, unsigned char* buffer);
182*4882a593Smuzhiyun
183*4882a593Smuzhiyun /*
184*4882a593Smuzhiyun * Inflate all entries under zipDir to the directory specified by
185*4882a593Smuzhiyun * targetDir, which must exist and be a writable directory.
186*4882a593Smuzhiyun *
187*4882a593Smuzhiyun * The immediate children of zipDir will become the immediate
188*4882a593Smuzhiyun * children of targetDir; e.g., if the archive contains the entries
189*4882a593Smuzhiyun *
190*4882a593Smuzhiyun * a/b/c/one
191*4882a593Smuzhiyun * a/b/c/two
192*4882a593Smuzhiyun * a/b/c/d/three
193*4882a593Smuzhiyun *
194*4882a593Smuzhiyun * and mzExtractRecursive(a, "a/b/c", "/tmp", ...) is called, the resulting
195*4882a593Smuzhiyun * files will be
196*4882a593Smuzhiyun *
197*4882a593Smuzhiyun * /tmp/one
198*4882a593Smuzhiyun * /tmp/two
199*4882a593Smuzhiyun * /tmp/d/three
200*4882a593Smuzhiyun *
201*4882a593Smuzhiyun * flags is zero or more of the following:
202*4882a593Smuzhiyun *
203*4882a593Smuzhiyun * MZ_EXTRACT_FILES_ONLY - only unpack files, not directories or symlinks
204*4882a593Smuzhiyun * MZ_EXTRACT_DRY_RUN - don't do anything, but do invoke the callback
205*4882a593Smuzhiyun *
206*4882a593Smuzhiyun * If timestamp is non-NULL, file timestamps will be set accordingly.
207*4882a593Smuzhiyun *
208*4882a593Smuzhiyun * If callback is non-NULL, it will be invoked with each unpacked file.
209*4882a593Smuzhiyun *
210*4882a593Smuzhiyun * Returns true on success, false on failure.
211*4882a593Smuzhiyun */
212*4882a593Smuzhiyun enum { MZ_EXTRACT_FILES_ONLY = 1, MZ_EXTRACT_DRY_RUN = 2 };
213*4882a593Smuzhiyun bool mzExtractRecursive(const ZipArchive *pArchive,
214*4882a593Smuzhiyun const char *zipDir, const char *targetDir,
215*4882a593Smuzhiyun int flags, const struct utimbuf *timestamp,
216*4882a593Smuzhiyun void (*callback)(const char *fn, void*), void *cookie);
217*4882a593Smuzhiyun
218*4882a593Smuzhiyun #endif /*_MINZIP_ZIP*/
219