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