Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 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 | // General-purpose Zip archive access. This class allows both reading and |
| 19 | // writing to Zip archives, including deletion of existing entries. |
| 20 | // |
| 21 | #ifndef __LIBS_ZIPFILE_H |
| 22 | #define __LIBS_ZIPFILE_H |
| 23 | |
| 24 | #include "BigBuffer.h" |
| 25 | #include "ZipEntry.h" |
| 26 | |
| 27 | #include <stdio.h> |
| 28 | #include <utils/Errors.h> |
| 29 | #include <vector> |
| 30 | |
| 31 | namespace aapt { |
| 32 | |
| 33 | using android::status_t; |
| 34 | |
| 35 | /* |
| 36 | * Manipulate a Zip archive. |
| 37 | * |
| 38 | * Some changes will not be visible in the until until "flush" is called. |
| 39 | * |
| 40 | * The correct way to update a file archive is to make all changes to a |
| 41 | * copy of the archive in a temporary file, and then unlink/rename over |
| 42 | * the original after everything completes. Because we're only interested |
| 43 | * in using this for packaging, we don't worry about such things. Crashing |
| 44 | * after making changes and before flush() completes could leave us with |
| 45 | * an unusable Zip archive. |
| 46 | */ |
| 47 | class ZipFile { |
| 48 | public: |
| 49 | ZipFile(void) |
| 50 | : mZipFp(NULL), mReadOnly(false), mNeedCDRewrite(false) |
| 51 | {} |
| 52 | ~ZipFile(void) { |
| 53 | if (!mReadOnly) |
| 54 | flush(); |
| 55 | if (mZipFp != NULL) |
| 56 | fclose(mZipFp); |
| 57 | discardEntries(); |
| 58 | } |
| 59 | |
| 60 | /* |
| 61 | * Open a new or existing archive. |
| 62 | */ |
| 63 | enum { |
| 64 | kOpenReadOnly = 0x01, |
| 65 | kOpenReadWrite = 0x02, |
| 66 | kOpenCreate = 0x04, // create if it doesn't exist |
| 67 | kOpenTruncate = 0x08, // if it exists, empty it |
| 68 | }; |
| 69 | status_t open(const char* zipFileName, int flags); |
| 70 | |
| 71 | /* |
| 72 | * Add a file to the end of the archive. Specify whether you want the |
| 73 | * library to try to store it compressed. |
| 74 | * |
| 75 | * If "storageName" is specified, the archive will use that instead |
| 76 | * of "fileName". |
| 77 | * |
| 78 | * If there is already an entry with the same name, the call fails. |
| 79 | * Existing entries with the same name must be removed first. |
| 80 | * |
| 81 | * If "ppEntry" is non-NULL, a pointer to the new entry will be returned. |
| 82 | */ |
| 83 | status_t add(const char* fileName, int compressionMethod, |
| 84 | ZipEntry** ppEntry) |
| 85 | { |
| 86 | return add(fileName, fileName, compressionMethod, ppEntry); |
| 87 | } |
| 88 | status_t add(const char* fileName, const char* storageName, |
| 89 | int compressionMethod, ZipEntry** ppEntry) |
| 90 | { |
| 91 | return addCommon(fileName, NULL, 0, storageName, |
| 92 | ZipEntry::kCompressStored, |
| 93 | compressionMethod, ppEntry); |
| 94 | } |
| 95 | |
| 96 | /* |
| 97 | * Add a file that is already compressed with gzip. |
| 98 | * |
| 99 | * If "ppEntry" is non-NULL, a pointer to the new entry will be returned. |
| 100 | */ |
| 101 | status_t addGzip(const char* fileName, const char* storageName, |
| 102 | ZipEntry** ppEntry) |
| 103 | { |
| 104 | return addCommon(fileName, NULL, 0, storageName, |
| 105 | ZipEntry::kCompressDeflated, |
| 106 | ZipEntry::kCompressDeflated, ppEntry); |
| 107 | } |
| 108 | |
| 109 | /* |
| 110 | * Add a file from an in-memory data buffer. |
| 111 | * |
| 112 | * If "ppEntry" is non-NULL, a pointer to the new entry will be returned. |
| 113 | */ |
| 114 | status_t add(const void* data, size_t size, const char* storageName, |
| 115 | int compressionMethod, ZipEntry** ppEntry) |
| 116 | { |
| 117 | return addCommon(NULL, data, size, storageName, |
| 118 | ZipEntry::kCompressStored, |
| 119 | compressionMethod, ppEntry); |
| 120 | } |
| 121 | |
| 122 | status_t add(const BigBuffer& data, const char* storageName, |
| 123 | int compressionMethod, ZipEntry** ppEntry); |
| 124 | |
| 125 | /* |
| 126 | * Add an entry by copying it from another zip file. If "padding" is |
| 127 | * nonzero, the specified number of bytes will be added to the "extra" |
| 128 | * field in the header. |
| 129 | * |
| 130 | * If "ppEntry" is non-NULL, a pointer to the new entry will be returned. |
| 131 | */ |
| 132 | status_t add(const ZipFile* pSourceZip, const ZipEntry* pSourceEntry, |
| 133 | int padding, ZipEntry** ppEntry); |
| 134 | |
| 135 | /* |
| 136 | * Mark an entry as having been removed. It is not actually deleted |
| 137 | * from the archive or our internal data structures until flush() is |
| 138 | * called. |
| 139 | */ |
| 140 | status_t remove(ZipEntry* pEntry); |
| 141 | |
| 142 | /* |
| 143 | * Flush changes. If mNeedCDRewrite is set, this writes the central dir. |
| 144 | */ |
| 145 | status_t flush(void); |
| 146 | |
| 147 | /* |
| 148 | * Expand the data into the buffer provided. The buffer must hold |
| 149 | * at least <uncompressed len> bytes. Variation expands directly |
| 150 | * to a file. |
| 151 | * |
| 152 | * Returns "false" if an error was encountered in the compressed data. |
| 153 | */ |
| 154 | //bool uncompress(const ZipEntry* pEntry, void* buf) const; |
| 155 | //bool uncompress(const ZipEntry* pEntry, FILE* fp) const; |
| 156 | void* uncompress(const ZipEntry* pEntry); |
| 157 | |
| 158 | /* |
| 159 | * Get an entry, by name. Returns NULL if not found. |
| 160 | * |
| 161 | * Does not return entries pending deletion. |
| 162 | */ |
| 163 | ZipEntry* getEntryByName(const char* fileName) const; |
| 164 | |
| 165 | /* |
| 166 | * Get the Nth entry in the archive. |
| 167 | * |
| 168 | * This will return an entry that is pending deletion. |
| 169 | */ |
| 170 | int getNumEntries(void) const { return mEntries.size(); } |
| 171 | ZipEntry* getEntryByIndex(int idx) const; |
| 172 | |
| 173 | private: |
| 174 | /* these are private and not defined */ |
| 175 | ZipFile(const ZipFile& src); |
| 176 | ZipFile& operator=(const ZipFile& src); |
| 177 | |
| 178 | class EndOfCentralDir { |
| 179 | public: |
| 180 | EndOfCentralDir(void) : |
| 181 | mDiskNumber(0), |
| 182 | mDiskWithCentralDir(0), |
| 183 | mNumEntries(0), |
| 184 | mTotalNumEntries(0), |
| 185 | mCentralDirSize(0), |
| 186 | mCentralDirOffset(0), |
| 187 | mCommentLen(0), |
| 188 | mComment(NULL) |
| 189 | {} |
| 190 | virtual ~EndOfCentralDir(void) { |
| 191 | delete[] mComment; |
| 192 | } |
| 193 | |
| 194 | status_t readBuf(const unsigned char* buf, int len); |
| 195 | status_t write(FILE* fp); |
| 196 | |
| 197 | //unsigned long mSignature; |
| 198 | unsigned short mDiskNumber; |
| 199 | unsigned short mDiskWithCentralDir; |
| 200 | unsigned short mNumEntries; |
| 201 | unsigned short mTotalNumEntries; |
| 202 | unsigned long mCentralDirSize; |
| 203 | unsigned long mCentralDirOffset; // offset from first disk |
| 204 | unsigned short mCommentLen; |
| 205 | unsigned char* mComment; |
| 206 | |
| 207 | enum { |
| 208 | kSignature = 0x06054b50, |
| 209 | kEOCDLen = 22, // EndOfCentralDir len, excl. comment |
| 210 | |
| 211 | kMaxCommentLen = 65535, // longest possible in ushort |
| 212 | kMaxEOCDSearch = kMaxCommentLen + EndOfCentralDir::kEOCDLen, |
| 213 | |
| 214 | }; |
| 215 | |
| 216 | void dump(void) const; |
| 217 | }; |
| 218 | |
| 219 | |
| 220 | /* read all entries in the central dir */ |
| 221 | status_t readCentralDir(void); |
| 222 | |
| 223 | /* crunch deleted entries out */ |
| 224 | status_t crunchArchive(void); |
| 225 | |
| 226 | /* clean up mEntries */ |
| 227 | void discardEntries(void); |
| 228 | |
| 229 | /* common handler for all "add" functions */ |
| 230 | status_t addCommon(const char* fileName, const void* data, size_t size, |
| 231 | const char* storageName, int sourceType, int compressionMethod, |
| 232 | ZipEntry** ppEntry); |
| 233 | |
| 234 | /* copy all of "srcFp" into "dstFp" */ |
| 235 | status_t copyFpToFp(FILE* dstFp, FILE* srcFp, unsigned long* pCRC32); |
| 236 | /* copy all of "data" into "dstFp" */ |
| 237 | status_t copyDataToFp(FILE* dstFp, |
| 238 | const void* data, size_t size, unsigned long* pCRC32); |
| 239 | /* copy some of "srcFp" into "dstFp" */ |
| 240 | status_t copyPartialFpToFp(FILE* dstFp, FILE* srcFp, long length, |
| 241 | unsigned long* pCRC32); |
| 242 | /* like memmove(), but on parts of a single file */ |
| 243 | status_t filemove(FILE* fp, off_t dest, off_t src, size_t n); |
| 244 | /* compress all of "srcFp" into "dstFp", using Deflate */ |
| 245 | status_t compressFpToFp(FILE* dstFp, FILE* srcFp, |
| 246 | const void* data, size_t size, unsigned long* pCRC32); |
| 247 | |
| 248 | /* get modification date from a file descriptor */ |
| 249 | time_t getModTime(int fd); |
| 250 | |
| 251 | /* |
| 252 | * We use stdio FILE*, which gives us buffering but makes dealing |
| 253 | * with files >2GB awkward. Until we support Zip64, we're fine. |
| 254 | */ |
| 255 | FILE* mZipFp; // Zip file pointer |
| 256 | |
| 257 | /* one of these per file */ |
| 258 | EndOfCentralDir mEOCD; |
| 259 | |
| 260 | /* did we open this read-only? */ |
| 261 | bool mReadOnly; |
| 262 | |
| 263 | /* set this when we trash the central dir */ |
| 264 | bool mNeedCDRewrite; |
| 265 | |
| 266 | /* |
| 267 | * One ZipEntry per entry in the zip file. I'm using pointers instead |
| 268 | * of objects because it's easier than making operator= work for the |
| 269 | * classes and sub-classes. |
| 270 | */ |
| 271 | std::vector<ZipEntry*> mEntries; |
| 272 | }; |
| 273 | |
| 274 | }; // namespace aapt |
| 275 | |
| 276 | #endif // __LIBS_ZIPFILE_H |