blob: e1ff780ab9e07787bdb4f683f55f50cbe6489601 [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 {}
Kenny Rootdbf6f272010-10-01 18:28:28 -070067
68 ~ZipFileRO();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069
70 /*
71 * Open an archive.
72 */
73 status_t open(const char* zipFileName);
74
75 /*
76 * Find an entry, by name. Returns the entry identifier, or NULL if
77 * not found.
78 *
79 * If two entries have the same name, one will be chosen at semi-random.
80 */
81 ZipEntryRO findEntryByName(const char* fileName) const;
82
83 /*
84 * Return the #of entries in the Zip archive.
85 */
86 int getNumEntries(void) const {
87 return mNumEntries;
88 }
89
90 /*
91 * Return the Nth entry. Zip file entries are not stored in sorted
92 * order, and updated entries may appear at the end, so anyone walking
93 * the archive needs to avoid making ordering assumptions. We take
94 * that further by returning the Nth non-empty entry in the hash table
95 * rather than the Nth entry in the archive.
96 *
97 * Valid values are [0..numEntries).
98 *
99 * [This is currently O(n). If it needs to be fast we can allocate an
100 * additional data structure or provide an iterator interface.]
101 */
102 ZipEntryRO findEntryByIndex(int idx) const;
103
104 /*
105 * Copy the filename into the supplied buffer. Returns 0 on success,
106 * -1 if "entry" is invalid, or the filename length if it didn't fit. The
107 * length, and the returned string, include the null-termination.
108 */
109 int getEntryFileName(ZipEntryRO entry, char* buffer, int bufLen) const;
110
111 /*
112 * Get the vital stats for an entry. Pass in NULL pointers for anything
113 * you don't need.
114 *
115 * "*pOffset" holds the Zip file offset of the entry's data.
116 *
117 * Returns "false" if "entry" is bogus or if the data in the Zip file
118 * appears to be bad.
119 */
Kenny Rootd4066a42010-04-22 18:28:29 -0700120 bool getEntryInfo(ZipEntryRO entry, int* pMethod, size_t* pUncompLen,
121 size_t* pCompLen, off_t* pOffset, long* pModWhen, long* pCrc32) const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800122
123 /*
124 * Create a new FileMap object that maps a subset of the archive. For
125 * an uncompressed entry this effectively provides a pointer to the
126 * actual data, for a compressed entry this provides the input buffer
127 * for inflate().
128 */
129 FileMap* createEntryFileMap(ZipEntryRO entry) const;
130
131 /*
132 * Uncompress the data into a buffer. Depending on the compression
133 * format, this is either an "inflate" operation or a memcpy.
134 *
135 * Use "uncompLen" from getEntryInfo() to determine the required
136 * buffer size.
137 *
138 * Returns "true" on success.
139 */
140 bool uncompressEntry(ZipEntryRO entry, void* buffer) const;
141
142 /*
143 * Uncompress the data to an open file descriptor.
144 */
145 bool uncompressEntry(ZipEntryRO entry, int fd) const;
146
147 /* Zip compression methods we support */
148 enum {
149 kCompressStored = 0, // no compression
150 kCompressDeflated = 8, // standard deflate
151 };
152
153 /*
154 * Utility function: uncompress deflated data, buffer to buffer.
155 */
156 static bool inflateBuffer(void* outBuf, const void* inBuf,
Kenny Rootd4066a42010-04-22 18:28:29 -0700157 size_t uncompLen, size_t compLen);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800158
159 /*
160 * Utility function: uncompress deflated data, buffer to fd.
161 */
162 static bool inflateBuffer(int fd, const void* inBuf,
Kenny Rootd4066a42010-04-22 18:28:29 -0700163 size_t uncompLen, size_t compLen);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800164
165 /*
166 * Some basic functions for raw data manipulation. "LE" means
167 * Little Endian.
168 */
169 static inline unsigned short get2LE(const unsigned char* buf) {
170 return buf[0] | (buf[1] << 8);
171 }
172 static inline unsigned long get4LE(const unsigned char* buf) {
173 return buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
174 }
175
176private:
177 /* these are private and not defined */
178 ZipFileRO(const ZipFileRO& src);
179 ZipFileRO& operator=(const ZipFileRO& src);
180
Kenny Rootd4066a42010-04-22 18:28:29 -0700181 /* locate and parse the central directory */
182 bool mapCentralDirectory(void);
183
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800184 /* parse the archive, prepping internal structures */
185 bool parseZipArchive(void);
186
187 /* add a new entry to the hash table */
188 void addToHash(const char* str, int strLen, unsigned int hash);
189
190 /* compute string hash code */
191 static unsigned int computeHash(const char* str, int len);
192
193 /* convert a ZipEntryRO back to a hash table index */
194 int entryToIndex(const ZipEntryRO entry) const;
195
196 /*
197 * One entry in the hash table.
198 */
199 typedef struct HashEntry {
200 const char* name;
201 unsigned short nameLen;
202 //unsigned int hash;
203 } HashEntry;
204
205 /* open Zip archive */
206 int mFd;
207
Kenny Rootfa989202010-09-24 07:57:37 -0700208 /* Lock for handling the file descriptor (seeks, etc) */
209 mutable Mutex mFdLock;
210
Kenny Rootd4066a42010-04-22 18:28:29 -0700211 /* zip file name */
212 char* mFileName;
213
214 /* length of file */
215 size_t mFileLength;
216
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800217 /* mapped file */
Kenny Rootd4066a42010-04-22 18:28:29 -0700218 FileMap* mDirectoryMap;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800219
220 /* number of entries in the Zip archive */
221 int mNumEntries;
222
Kenny Rootd4066a42010-04-22 18:28:29 -0700223 /* CD directory offset in the Zip archive */
224 off_t mDirectoryOffset;
225
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800226 /*
227 * We know how many entries are in the Zip archive, so we have a
228 * fixed-size hash table. We probe for an empty slot.
229 */
230 int mHashTableSize;
231 HashEntry* mHashTable;
232};
233
234}; // namespace android
235
236#endif /*__LIBS_ZIPFILERO_H*/