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