blob: c932c1178ef9d8a702c2ec5024037e7cd500f9f1 [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
Elliott Hughes4bbd5bf2016-04-01 18:24:39 -070021struct selabel_handle;
Stephen Smalley779701d2012-02-09 14:13:23 -050022
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080023/*
24 * One entry in the Zip archive. Treat this as opaque -- use accessors below.
25 *
26 * TODO: we're now keeping the pages mapped so we don't have to copy the
27 * filename. We can change the accessors to retrieve the various pieces
28 * directly from the source file instead of copying them out, for a very
29 * slight speed hit and a modest reduction in memory usage.
30 */
31typedef struct ZipEntry {
32 unsigned int fileNameLen;
33 const char* fileName; // not null-terminated
caozhiyuan2584f9c2016-03-11 15:18:29 +080034 uint32_t offset;
35 uint32_t compLen;
36 uint32_t uncompLen;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080037 int compression;
38 long modTime;
39 long crc32;
40 int versionMadeBy;
41 long externalFileAttributes;
42} ZipEntry;
43
44/*
45 * One Zip archive. Treat as opaque.
46 */
47typedef struct ZipArchive {
Doug Zongker99916f02014-01-13 14:16:58 -080048 unsigned int numEntries;
49 ZipEntry* pEntries;
50 HashTable* pHash; // maps file name to ZipEntry
51 unsigned char* addr;
52 size_t length;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080053} ZipArchive;
54
55/*
56 * Represents a non-NUL-terminated string,
57 * which is how entry names are stored.
58 */
59typedef struct {
60 const char *str;
61 size_t len;
62} UnterminatedString;
63
64/*
65 * Open a Zip archive.
66 *
67 * On success, returns 0 and populates "pArchive". Returns nonzero errno
68 * value on failure.
69 */
Doug Zongker99916f02014-01-13 14:16:58 -080070int mzOpenZipArchive(unsigned char* addr, size_t length, ZipArchive* pArchive);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080071
72/*
73 * Close archive, releasing resources associated with it.
74 *
75 * Depending on the implementation this could unmap pages used by classes
76 * stored in a Jar. This should only be done after unloading classes.
77 */
78void mzCloseZipArchive(ZipArchive* pArchive);
79
80
81/*
82 * Find an entry in the Zip archive, by name.
83 */
84const ZipEntry* mzFindZipEntry(const ZipArchive* pArchive,
85 const char* entryName);
86
caozhiyuan2584f9c2016-03-11 15:18:29 +080087INLINE uint32_t mzGetZipEntryOffset(const ZipEntry* pEntry) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080088 return pEntry->offset;
89}
caozhiyuan2584f9c2016-03-11 15:18:29 +080090INLINE uint32_t mzGetZipEntryUncompLen(const ZipEntry* pEntry) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080091 return pEntry->uncompLen;
92}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080093
94/*
95 * Type definition for the callback function used by
96 * mzProcessZipEntryContents().
97 */
98typedef bool (*ProcessZipEntryContentsFunction)(const unsigned char *data,
99 int dataLen, void *cookie);
100
101/*
102 * Stream the uncompressed data through the supplied function,
103 * passing cookie to it each time it gets called. processFunction
104 * may be called more than once.
105 *
106 * If processFunction returns false, the operation is abandoned and
107 * mzProcessZipEntryContents() immediately returns false.
108 *
109 * This is useful for calculating the hash of an entry's uncompressed contents.
110 */
111bool mzProcessZipEntryContents(const ZipArchive *pArchive,
112 const ZipEntry *pEntry, ProcessZipEntryContentsFunction processFunction,
113 void *cookie);
114
115/*
116 * Read an entry into a buffer allocated by the caller.
117 */
118bool mzReadZipEntry(const ZipArchive* pArchive, const ZipEntry* pEntry,
119 char* buf, int bufLen);
120
121/*
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800122 * Inflate and write an entry to a file.
123 */
124bool mzExtractZipEntryToFile(const ZipArchive *pArchive,
Doug Zongker1c4ceae2009-05-08 09:43:28 -0700125 const ZipEntry *pEntry, int fd);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800126
127/*
Doug Zongker6aece332010-02-01 14:40:12 -0800128 * Inflate and write an entry to a memory buffer, which must be long
129 * enough to hold mzGetZipEntryUncomplen(pEntry) bytes.
130 */
131bool mzExtractZipEntryToBuffer(const ZipArchive *pArchive,
132 const ZipEntry *pEntry, unsigned char* buffer);
133
134/*
Narayan Kamath9c0f5d62015-02-23 14:09:31 +0000135 * Inflate all files under zipDir to the directory specified by
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800136 * targetDir, which must exist and be a writable directory.
137 *
Narayan Kamath9c0f5d62015-02-23 14:09:31 +0000138 * Directory entries and symlinks are not extracted.
139 *
140 *
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800141 * The immediate children of zipDir will become the immediate
142 * children of targetDir; e.g., if the archive contains the entries
143 *
144 * a/b/c/one
145 * a/b/c/two
146 * a/b/c/d/three
147 *
148 * and mzExtractRecursive(a, "a/b/c", "/tmp", ...) is called, the resulting
149 * files will be
150 *
151 * /tmp/one
152 * /tmp/two
153 * /tmp/d/three
154 *
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800155 * If timestamp is non-NULL, file timestamps will be set accordingly.
156 *
157 * If callback is non-NULL, it will be invoked with each unpacked file.
158 *
159 * Returns true on success, false on failure.
160 */
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800161bool mzExtractRecursive(const ZipArchive *pArchive,
162 const char *zipDir, const char *targetDir,
Narayan Kamath9c0f5d62015-02-23 14:09:31 +0000163 const struct utimbuf *timestamp,
Stephen Smalley779701d2012-02-09 14:13:23 -0500164 void (*callback)(const char *fn, void*), void *cookie,
165 struct selabel_handle *sehnd);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800166
Doug Zongker28ce47c2011-10-28 10:33:05 -0700167#ifdef __cplusplus
168}
169#endif
170
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800171#endif /*_MINZIP_ZIP*/