blob: 3a999797b476dbfbe050b31c05fe80ac6c321337 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Kenny Root259ec462010-10-04 14:20:14 -070017/*
18 * Read-only access to Zip archives, with minimal heap allocation.
19 *
20 * This is similar to the more-complete ZipFile class, but no attempt
21 * has been made to make them interchangeable. This class operates under
22 * a very different set of assumptions and constraints.
23 *
24 * One such assumption is that if you're getting file descriptors for
25 * use with this class as a child of a fork() operation, you must be on
26 * a pread() to guarantee correct operation. This is because pread() can
27 * atomically read at a file offset without worrying about a lock around an
28 * lseek() + read() pair.
29 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030#ifndef __LIBS_ZIPFILERO_H
31#define __LIBS_ZIPFILERO_H
32
Kenny Rootddb76c42010-11-24 12:56:06 -080033#include <utils/Compat.h>
Kenny Root6458fa02010-09-24 07:57:37 -070034#include <utils/Errors.h>
35#include <utils/FileMap.h>
36#include <utils/threads.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037
38#include <stdio.h>
39#include <stdlib.h>
40#include <unistd.h>
41
42namespace android {
43
44/*
45 * Trivial typedef to ensure that ZipEntryRO is not treated as a simple
46 * integer. We use NULL to indicate an invalid value.
47 */
48typedef void* ZipEntryRO;
49
50/*
51 * Open a Zip archive for reading.
52 *
53 * We want "open" and "find entry by name" to be fast operations, and we
54 * want to use as little memory as possible. We memory-map the file,
55 * and load a hash table with pointers to the filenames (which aren't
56 * null-terminated). The other fields are at a fixed offset from the
57 * filename, so we don't need to extract those (but we do need to byte-read
58 * and endian-swap them every time we want them).
59 *
60 * To speed comparisons when doing a lookup by name, we could make the mapping
61 * "private" (copy-on-write) and null-terminate the filenames after verifying
62 * the record structure. However, this requires a private mapping of
63 * every page that the Central Directory touches. Easier to tuck a copy
64 * of the string length into the hash table entry.
Kenny Root259ec462010-10-04 14:20:14 -070065 *
66 * NOTE: If this is used on file descriptors inherited from a fork() operation,
67 * you must be on a platform that implements pread() to guarantee correctness
68 * on the shared file descriptors.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069 */
70class ZipFileRO {
71public:
72 ZipFileRO()
Kenny Root68246dc2010-04-22 18:28:29 -070073 : mFd(-1), mFileName(NULL), mFileLength(-1),
74 mDirectoryMap(NULL),
75 mNumEntries(-1), mDirectoryOffset(-1),
76 mHashTableSize(-1), mHashTable(NULL)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 {}
Kenny Root6cb241f2010-10-01 18:28:28 -070078
79 ~ZipFileRO();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080
81 /*
82 * Open an archive.
83 */
84 status_t open(const char* zipFileName);
85
86 /*
87 * Find an entry, by name. Returns the entry identifier, or NULL if
88 * not found.
89 *
90 * If two entries have the same name, one will be chosen at semi-random.
91 */
92 ZipEntryRO findEntryByName(const char* fileName) const;
93
94 /*
95 * Return the #of entries in the Zip archive.
96 */
97 int getNumEntries(void) const {
98 return mNumEntries;
99 }
100
101 /*
102 * Return the Nth entry. Zip file entries are not stored in sorted
103 * order, and updated entries may appear at the end, so anyone walking
104 * the archive needs to avoid making ordering assumptions. We take
105 * that further by returning the Nth non-empty entry in the hash table
106 * rather than the Nth entry in the archive.
107 *
108 * Valid values are [0..numEntries).
109 *
110 * [This is currently O(n). If it needs to be fast we can allocate an
111 * additional data structure or provide an iterator interface.]
112 */
113 ZipEntryRO findEntryByIndex(int idx) const;
114
115 /*
116 * Copy the filename into the supplied buffer. Returns 0 on success,
117 * -1 if "entry" is invalid, or the filename length if it didn't fit. The
118 * length, and the returned string, include the null-termination.
119 */
120 int getEntryFileName(ZipEntryRO entry, char* buffer, int bufLen) const;
121
122 /*
123 * Get the vital stats for an entry. Pass in NULL pointers for anything
124 * you don't need.
125 *
126 * "*pOffset" holds the Zip file offset of the entry's data.
127 *
128 * Returns "false" if "entry" is bogus or if the data in the Zip file
129 * appears to be bad.
130 */
Kenny Root68246dc2010-04-22 18:28:29 -0700131 bool getEntryInfo(ZipEntryRO entry, int* pMethod, size_t* pUncompLen,
Kenny Rootddb76c42010-11-24 12:56:06 -0800132 size_t* pCompLen, off64_t* pOffset, long* pModWhen, long* pCrc32) const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133
134 /*
135 * Create a new FileMap object that maps a subset of the archive. For
136 * an uncompressed entry this effectively provides a pointer to the
137 * actual data, for a compressed entry this provides the input buffer
138 * for inflate().
139 */
140 FileMap* createEntryFileMap(ZipEntryRO entry) const;
141
142 /*
143 * Uncompress the data into a buffer. Depending on the compression
144 * format, this is either an "inflate" operation or a memcpy.
145 *
146 * Use "uncompLen" from getEntryInfo() to determine the required
147 * buffer size.
148 *
149 * Returns "true" on success.
150 */
151 bool uncompressEntry(ZipEntryRO entry, void* buffer) const;
152
153 /*
154 * Uncompress the data to an open file descriptor.
155 */
156 bool uncompressEntry(ZipEntryRO entry, int fd) const;
157
158 /* Zip compression methods we support */
159 enum {
160 kCompressStored = 0, // no compression
161 kCompressDeflated = 8, // standard deflate
162 };
163
164 /*
165 * Utility function: uncompress deflated data, buffer to buffer.
166 */
167 static bool inflateBuffer(void* outBuf, const void* inBuf,
Kenny Root68246dc2010-04-22 18:28:29 -0700168 size_t uncompLen, size_t compLen);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169
170 /*
171 * Utility function: uncompress deflated data, buffer to fd.
172 */
173 static bool inflateBuffer(int fd, const void* inBuf,
Kenny Root68246dc2010-04-22 18:28:29 -0700174 size_t uncompLen, size_t compLen);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175
176 /*
177 * Some basic functions for raw data manipulation. "LE" means
178 * Little Endian.
179 */
180 static inline unsigned short get2LE(const unsigned char* buf) {
181 return buf[0] | (buf[1] << 8);
182 }
183 static inline unsigned long get4LE(const unsigned char* buf) {
184 return buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
185 }
186
187private:
188 /* these are private and not defined */
189 ZipFileRO(const ZipFileRO& src);
190 ZipFileRO& operator=(const ZipFileRO& src);
191
Kenny Root68246dc2010-04-22 18:28:29 -0700192 /* locate and parse the central directory */
193 bool mapCentralDirectory(void);
194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 /* parse the archive, prepping internal structures */
196 bool parseZipArchive(void);
197
198 /* add a new entry to the hash table */
199 void addToHash(const char* str, int strLen, unsigned int hash);
200
201 /* compute string hash code */
202 static unsigned int computeHash(const char* str, int len);
203
204 /* convert a ZipEntryRO back to a hash table index */
205 int entryToIndex(const ZipEntryRO entry) const;
206
207 /*
208 * One entry in the hash table.
209 */
210 typedef struct HashEntry {
211 const char* name;
212 unsigned short nameLen;
213 //unsigned int hash;
214 } HashEntry;
215
216 /* open Zip archive */
217 int mFd;
218
Kenny Root6458fa02010-09-24 07:57:37 -0700219 /* Lock for handling the file descriptor (seeks, etc) */
220 mutable Mutex mFdLock;
221
Kenny Root68246dc2010-04-22 18:28:29 -0700222 /* zip file name */
223 char* mFileName;
224
225 /* length of file */
226 size_t mFileLength;
227
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 /* mapped file */
Kenny Root68246dc2010-04-22 18:28:29 -0700229 FileMap* mDirectoryMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230
231 /* number of entries in the Zip archive */
232 int mNumEntries;
233
Kenny Root68246dc2010-04-22 18:28:29 -0700234 /* CD directory offset in the Zip archive */
Kenny Rootddb76c42010-11-24 12:56:06 -0800235 off64_t mDirectoryOffset;
Kenny Root68246dc2010-04-22 18:28:29 -0700236
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 /*
238 * We know how many entries are in the Zip archive, so we have a
239 * fixed-size hash table. We probe for an empty slot.
240 */
241 int mHashTableSize;
242 HashEntry* mHashTable;
243};
244
245}; // namespace android
246
247#endif /*__LIBS_ZIPFILERO_H*/