blob: 9668bdeef55b9f709d2a1419ecf49e9419f92237 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-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
17//
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#ifndef __LIBS_ZIPFILERO_H
25#define __LIBS_ZIPFILERO_H
26
Kenny Rootfa989202010-09-24 07:57:37 -070027#include <utils/Errors.h>
28#include <utils/FileMap.h>
29#include <utils/threads.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080030
31#include <stdio.h>
32#include <stdlib.h>
33#include <unistd.h>
34
35namespace android {
36
37/*
38 * Trivial typedef to ensure that ZipEntryRO is not treated as a simple
39 * integer. We use NULL to indicate an invalid value.
40 */
41typedef void* ZipEntryRO;
42
43/*
44 * Open a Zip archive for reading.
45 *
46 * We want "open" and "find entry by name" to be fast operations, and we
47 * want to use as little memory as possible. We memory-map the file,
48 * and load a hash table with pointers to the filenames (which aren't
49 * null-terminated). The other fields are at a fixed offset from the
50 * filename, so we don't need to extract those (but we do need to byte-read
51 * and endian-swap them every time we want them).
52 *
53 * To speed comparisons when doing a lookup by name, we could make the mapping
54 * "private" (copy-on-write) and null-terminate the filenames after verifying
55 * the record structure. However, this requires a private mapping of
56 * every page that the Central Directory touches. Easier to tuck a copy
57 * of the string length into the hash table entry.
58 */
59class ZipFileRO {
60public:
61 ZipFileRO()
Kenny Rootd4066a42010-04-22 18:28:29 -070062 : mFd(-1), mFileName(NULL), mFileLength(-1),
63 mDirectoryMap(NULL),
64 mNumEntries(-1), mDirectoryOffset(-1),
65 mHashTableSize(-1), mHashTable(NULL)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080066 {}
67 ~ZipFileRO() {
68 free(mHashTable);
Kenny Rootd4066a42010-04-22 18:28:29 -070069 if (mDirectoryMap)
70 mDirectoryMap->release();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080071 if (mFd >= 0)
72 close(mFd);
Kenny Rootd4066a42010-04-22 18:28:29 -070073 if (mFileName)
74 free(mFileName);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080075 }
76
77 /*
78 * Open an archive.
79 */
80 status_t open(const char* zipFileName);
81
82 /*
83 * Find an entry, by name. Returns the entry identifier, or NULL if
84 * not found.
85 *
86 * If two entries have the same name, one will be chosen at semi-random.
87 */
88 ZipEntryRO findEntryByName(const char* fileName) const;
89
90 /*
91 * Return the #of entries in the Zip archive.
92 */
93 int getNumEntries(void) const {
94 return mNumEntries;
95 }
96
97 /*
98 * Return the Nth entry. Zip file entries are not stored in sorted
99 * order, and updated entries may appear at the end, so anyone walking
100 * the archive needs to avoid making ordering assumptions. We take
101 * that further by returning the Nth non-empty entry in the hash table
102 * rather than the Nth entry in the archive.
103 *
104 * Valid values are [0..numEntries).
105 *
106 * [This is currently O(n). If it needs to be fast we can allocate an
107 * additional data structure or provide an iterator interface.]
108 */
109 ZipEntryRO findEntryByIndex(int idx) const;
110
111 /*
112 * Copy the filename into the supplied buffer. Returns 0 on success,
113 * -1 if "entry" is invalid, or the filename length if it didn't fit. The
114 * length, and the returned string, include the null-termination.
115 */
116 int getEntryFileName(ZipEntryRO entry, char* buffer, int bufLen) const;
117
118 /*
119 * Get the vital stats for an entry. Pass in NULL pointers for anything
120 * you don't need.
121 *
122 * "*pOffset" holds the Zip file offset of the entry's data.
123 *
124 * Returns "false" if "entry" is bogus or if the data in the Zip file
125 * appears to be bad.
126 */
Kenny Rootd4066a42010-04-22 18:28:29 -0700127 bool getEntryInfo(ZipEntryRO entry, int* pMethod, size_t* pUncompLen,
128 size_t* pCompLen, off_t* pOffset, long* pModWhen, long* pCrc32) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800129
130 /*
131 * Create a new FileMap object that maps a subset of the archive. For
132 * an uncompressed entry this effectively provides a pointer to the
133 * actual data, for a compressed entry this provides the input buffer
134 * for inflate().
135 */
136 FileMap* createEntryFileMap(ZipEntryRO entry) const;
137
138 /*
139 * Uncompress the data into a buffer. Depending on the compression
140 * format, this is either an "inflate" operation or a memcpy.
141 *
142 * Use "uncompLen" from getEntryInfo() to determine the required
143 * buffer size.
144 *
145 * Returns "true" on success.
146 */
147 bool uncompressEntry(ZipEntryRO entry, void* buffer) const;
148
149 /*
150 * Uncompress the data to an open file descriptor.
151 */
152 bool uncompressEntry(ZipEntryRO entry, int fd) const;
153
154 /* Zip compression methods we support */
155 enum {
156 kCompressStored = 0, // no compression
157 kCompressDeflated = 8, // standard deflate
158 };
159
160 /*
161 * Utility function: uncompress deflated data, buffer to buffer.
162 */
163 static bool inflateBuffer(void* outBuf, const void* inBuf,
Kenny Rootd4066a42010-04-22 18:28:29 -0700164 size_t uncompLen, size_t compLen);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800165
166 /*
167 * Utility function: uncompress deflated data, buffer to fd.
168 */
169 static bool inflateBuffer(int fd, const void* inBuf,
Kenny Rootd4066a42010-04-22 18:28:29 -0700170 size_t uncompLen, size_t compLen);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800171
172 /*
173 * Some basic functions for raw data manipulation. "LE" means
174 * Little Endian.
175 */
176 static inline unsigned short get2LE(const unsigned char* buf) {
177 return buf[0] | (buf[1] << 8);
178 }
179 static inline unsigned long get4LE(const unsigned char* buf) {
180 return buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
181 }
182
183private:
184 /* these are private and not defined */
185 ZipFileRO(const ZipFileRO& src);
186 ZipFileRO& operator=(const ZipFileRO& src);
187
Kenny Rootd4066a42010-04-22 18:28:29 -0700188 /* locate and parse the central directory */
189 bool mapCentralDirectory(void);
190
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800191 /* parse the archive, prepping internal structures */
192 bool parseZipArchive(void);
193
194 /* add a new entry to the hash table */
195 void addToHash(const char* str, int strLen, unsigned int hash);
196
197 /* compute string hash code */
198 static unsigned int computeHash(const char* str, int len);
199
200 /* convert a ZipEntryRO back to a hash table index */
201 int entryToIndex(const ZipEntryRO entry) const;
202
203 /*
204 * One entry in the hash table.
205 */
206 typedef struct HashEntry {
207 const char* name;
208 unsigned short nameLen;
209 //unsigned int hash;
210 } HashEntry;
211
212 /* open Zip archive */
213 int mFd;
214
Kenny Rootfa989202010-09-24 07:57:37 -0700215 /* Lock for handling the file descriptor (seeks, etc) */
216 mutable Mutex mFdLock;
217
Kenny Rootd4066a42010-04-22 18:28:29 -0700218 /* zip file name */
219 char* mFileName;
220
221 /* length of file */
222 size_t mFileLength;
223
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800224 /* mapped file */
Kenny Rootd4066a42010-04-22 18:28:29 -0700225 FileMap* mDirectoryMap;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800226
227 /* number of entries in the Zip archive */
228 int mNumEntries;
229
Kenny Rootd4066a42010-04-22 18:28:29 -0700230 /* CD directory offset in the Zip archive */
231 off_t mDirectoryOffset;
232
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800233 /*
234 * We know how many entries are in the Zip archive, so we have a
235 * fixed-size hash table. We probe for an empty slot.
236 */
237 int mHashTableSize;
238 HashEntry* mHashTable;
239};
240
241}; // namespace android
242
243#endif /*__LIBS_ZIPFILERO_H*/