blob: a2b2c26fca2f00db6c01117594e84bf6b608d135 [file] [log] [blame]
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001/*
2 * Copyright 2006 The Android Open Source Project
3 *
4 * Simple Zip archive support.
5 */
6#ifndef _MINZIP_ZIP
7#define _MINZIP_ZIP
8
9#include "inline_magic.h"
10
11#include <stdlib.h>
12#include <utime.h>
13
14#include "Hash.h"
15#include "SysUtil.h"
16
Doug Zongker28ce47c2011-10-28 10:33:05 -070017#ifdef __cplusplus
18extern "C" {
19#endif
20
Stephen Smalley779701d2012-02-09 14:13:23 -050021#include <selinux/selinux.h>
22#include <selinux/label.h>
Stephen Smalley779701d2012-02-09 14:13:23 -050023
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080024/*
25 * One entry in the Zip archive. Treat this as opaque -- use accessors below.
26 *
27 * TODO: we're now keeping the pages mapped so we don't have to copy the
28 * filename. We can change the accessors to retrieve the various pieces
29 * directly from the source file instead of copying them out, for a very
30 * slight speed hit and a modest reduction in memory usage.
31 */
32typedef struct ZipEntry {
33 unsigned int fileNameLen;
34 const char* fileName; // not null-terminated
35 long offset;
36 long compLen;
37 long uncompLen;
38 int compression;
39 long modTime;
40 long crc32;
41 int versionMadeBy;
42 long externalFileAttributes;
43} ZipEntry;
44
45/*
46 * One Zip archive. Treat as opaque.
47 */
48typedef struct ZipArchive {
Doug Zongker99916f02014-01-13 14:16:58 -080049 unsigned int numEntries;
50 ZipEntry* pEntries;
51 HashTable* pHash; // maps file name to ZipEntry
52 unsigned char* addr;
53 size_t length;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080054} ZipArchive;
55
56/*
57 * Represents a non-NUL-terminated string,
58 * which is how entry names are stored.
59 */
60typedef struct {
61 const char *str;
62 size_t len;
63} UnterminatedString;
64
65/*
66 * Open a Zip archive.
67 *
68 * On success, returns 0 and populates "pArchive". Returns nonzero errno
69 * value on failure.
70 */
Doug Zongker99916f02014-01-13 14:16:58 -080071int mzOpenZipArchive(unsigned char* addr, size_t length, ZipArchive* pArchive);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080072
73/*
74 * Close archive, releasing resources associated with it.
75 *
76 * Depending on the implementation this could unmap pages used by classes
77 * stored in a Jar. This should only be done after unloading classes.
78 */
79void mzCloseZipArchive(ZipArchive* pArchive);
80
81
82/*
83 * Find an entry in the Zip archive, by name.
84 */
85const ZipEntry* mzFindZipEntry(const ZipArchive* pArchive,
86 const char* entryName);
87
88/*
89 * Get the number of entries in the Zip archive.
90 */
91INLINE unsigned int mzZipEntryCount(const ZipArchive* pArchive) {
92 return pArchive->numEntries;
93}
94
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080095INLINE long mzGetZipEntryOffset(const ZipEntry* pEntry) {
96 return pEntry->offset;
97}
98INLINE long mzGetZipEntryUncompLen(const ZipEntry* pEntry) {
99 return pEntry->uncompLen;
100}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800101INLINE long mzGetZipEntryCrc32(const ZipEntry* pEntry) {
102 return pEntry->crc32;
103}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800104
105/*
106 * Type definition for the callback function used by
107 * mzProcessZipEntryContents().
108 */
109typedef bool (*ProcessZipEntryContentsFunction)(const unsigned char *data,
110 int dataLen, void *cookie);
111
112/*
113 * Stream the uncompressed data through the supplied function,
114 * passing cookie to it each time it gets called. processFunction
115 * may be called more than once.
116 *
117 * If processFunction returns false, the operation is abandoned and
118 * mzProcessZipEntryContents() immediately returns false.
119 *
120 * This is useful for calculating the hash of an entry's uncompressed contents.
121 */
122bool mzProcessZipEntryContents(const ZipArchive *pArchive,
123 const ZipEntry *pEntry, ProcessZipEntryContentsFunction processFunction,
124 void *cookie);
125
126/*
127 * Read an entry into a buffer allocated by the caller.
128 */
129bool mzReadZipEntry(const ZipArchive* pArchive, const ZipEntry* pEntry,
130 char* buf, int bufLen);
131
132/*
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800133 * Inflate and write an entry to a file.
134 */
135bool mzExtractZipEntryToFile(const ZipArchive *pArchive,
Doug Zongker1c4ceae2009-05-08 09:43:28 -0700136 const ZipEntry *pEntry, int fd);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800137
138/*
Doug Zongker6aece332010-02-01 14:40:12 -0800139 * Inflate and write an entry to a memory buffer, which must be long
140 * enough to hold mzGetZipEntryUncomplen(pEntry) bytes.
141 */
142bool mzExtractZipEntryToBuffer(const ZipArchive *pArchive,
143 const ZipEntry *pEntry, unsigned char* buffer);
144
145/*
Narayan Kamath9c0f5d62015-02-23 14:09:31 +0000146 * Inflate all files under zipDir to the directory specified by
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800147 * targetDir, which must exist and be a writable directory.
148 *
Narayan Kamath9c0f5d62015-02-23 14:09:31 +0000149 * Directory entries and symlinks are not extracted.
150 *
151 *
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800152 * The immediate children of zipDir will become the immediate
153 * children of targetDir; e.g., if the archive contains the entries
154 *
155 * a/b/c/one
156 * a/b/c/two
157 * a/b/c/d/three
158 *
159 * and mzExtractRecursive(a, "a/b/c", "/tmp", ...) is called, the resulting
160 * files will be
161 *
162 * /tmp/one
163 * /tmp/two
164 * /tmp/d/three
165 *
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800166 * If timestamp is non-NULL, file timestamps will be set accordingly.
167 *
168 * If callback is non-NULL, it will be invoked with each unpacked file.
169 *
170 * Returns true on success, false on failure.
171 */
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800172bool mzExtractRecursive(const ZipArchive *pArchive,
173 const char *zipDir, const char *targetDir,
Narayan Kamath9c0f5d62015-02-23 14:09:31 +0000174 const struct utimbuf *timestamp,
Stephen Smalley779701d2012-02-09 14:13:23 -0500175 void (*callback)(const char *fn, void*), void *cookie,
176 struct selabel_handle *sehnd);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800177
Doug Zongker28ce47c2011-10-28 10:33:05 -0700178#ifdef __cplusplus
179}
180#endif
181
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800182#endif /*_MINZIP_ZIP*/