Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #ifndef nsZipArchive_h_ |
michael@0 | 7 | #define nsZipArchive_h_ |
michael@0 | 8 | |
michael@0 | 9 | #include "mozilla/Attributes.h" |
michael@0 | 10 | |
michael@0 | 11 | #define ZIP_TABSIZE 256 |
michael@0 | 12 | #define ZIP_BUFLEN (4*1024) /* Used as output buffer when deflating items to a file */ |
michael@0 | 13 | |
michael@0 | 14 | #include "plarena.h" |
michael@0 | 15 | #include "zlib.h" |
michael@0 | 16 | #include "zipstruct.h" |
michael@0 | 17 | #include "nsAutoPtr.h" |
michael@0 | 18 | #include "nsIFile.h" |
michael@0 | 19 | #include "nsISupportsImpl.h" // For mozilla::ThreadSafeAutoRefCnt |
michael@0 | 20 | #include "mozilla/FileUtils.h" |
michael@0 | 21 | #include "mozilla/FileLocation.h" |
michael@0 | 22 | |
michael@0 | 23 | #if defined(XP_WIN) && defined(_MSC_VER) |
michael@0 | 24 | #define MOZ_WIN_MEM_TRY_BEGIN __try { |
michael@0 | 25 | #define MOZ_WIN_MEM_TRY_CATCH(cmd) } \ |
michael@0 | 26 | __except(GetExceptionCode()==EXCEPTION_IN_PAGE_ERROR ? \ |
michael@0 | 27 | EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH) \ |
michael@0 | 28 | { \ |
michael@0 | 29 | NS_WARNING("EXCEPTION_IN_PAGE_ERROR in " __FUNCTION__); \ |
michael@0 | 30 | cmd; \ |
michael@0 | 31 | } |
michael@0 | 32 | #else |
michael@0 | 33 | #define MOZ_WIN_MEM_TRY_BEGIN { |
michael@0 | 34 | #define MOZ_WIN_MEM_TRY_CATCH(cmd) } |
michael@0 | 35 | #endif |
michael@0 | 36 | |
michael@0 | 37 | class nsZipFind; |
michael@0 | 38 | struct PRFileDesc; |
michael@0 | 39 | |
michael@0 | 40 | /** |
michael@0 | 41 | * This file defines some of the basic structures used by libjar to |
michael@0 | 42 | * read Zip files. It makes use of zlib in order to do the decompression. |
michael@0 | 43 | * |
michael@0 | 44 | * A few notes on the classes/structs: |
michael@0 | 45 | * nsZipArchive represents a single Zip file, and maintains an index |
michael@0 | 46 | * of all the items in the file. |
michael@0 | 47 | * nsZipItem represents a single item (file) in the Zip archive. |
michael@0 | 48 | * nsZipFind represents the metadata involved in doing a search, |
michael@0 | 49 | * and current state of the iteration of found objects. |
michael@0 | 50 | * 'MT''safe' reading from the zipfile is performed through JARInputStream, |
michael@0 | 51 | * which maintains its own file descriptor, allowing for multiple reads |
michael@0 | 52 | * concurrently from the same zip file. |
michael@0 | 53 | */ |
michael@0 | 54 | |
michael@0 | 55 | /** |
michael@0 | 56 | * nsZipItem -- a helper struct for nsZipArchive |
michael@0 | 57 | * |
michael@0 | 58 | * each nsZipItem represents one file in the archive and all the |
michael@0 | 59 | * information needed to manipulate it. |
michael@0 | 60 | */ |
michael@0 | 61 | class nsZipItem |
michael@0 | 62 | { |
michael@0 | 63 | public: |
michael@0 | 64 | const char* Name() { return ((const char*)central) + ZIPCENTRAL_SIZE; } |
michael@0 | 65 | |
michael@0 | 66 | uint32_t LocalOffset(); |
michael@0 | 67 | uint32_t Size(); |
michael@0 | 68 | uint32_t RealSize(); |
michael@0 | 69 | uint32_t CRC32(); |
michael@0 | 70 | uint16_t Date(); |
michael@0 | 71 | uint16_t Time(); |
michael@0 | 72 | uint16_t Compression(); |
michael@0 | 73 | bool IsDirectory(); |
michael@0 | 74 | uint16_t Mode(); |
michael@0 | 75 | const uint8_t* GetExtraField(uint16_t aTag, uint16_t *aBlockSize); |
michael@0 | 76 | PRTime LastModTime(); |
michael@0 | 77 | |
michael@0 | 78 | #ifdef XP_UNIX |
michael@0 | 79 | bool IsSymlink(); |
michael@0 | 80 | #endif |
michael@0 | 81 | |
michael@0 | 82 | nsZipItem* next; |
michael@0 | 83 | const ZipCentral* central; |
michael@0 | 84 | uint16_t nameLength; |
michael@0 | 85 | bool isSynthetic; |
michael@0 | 86 | }; |
michael@0 | 87 | |
michael@0 | 88 | class nsZipHandle; |
michael@0 | 89 | |
michael@0 | 90 | /** |
michael@0 | 91 | * nsZipArchive -- a class for reading the PKZIP file format. |
michael@0 | 92 | * |
michael@0 | 93 | */ |
michael@0 | 94 | class nsZipArchive |
michael@0 | 95 | { |
michael@0 | 96 | friend class nsZipFind; |
michael@0 | 97 | |
michael@0 | 98 | public: |
michael@0 | 99 | /** constructing does not open the archive. See OpenArchive() */ |
michael@0 | 100 | nsZipArchive(); |
michael@0 | 101 | |
michael@0 | 102 | /** destructing the object closes the archive */ |
michael@0 | 103 | ~nsZipArchive(); |
michael@0 | 104 | |
michael@0 | 105 | /** |
michael@0 | 106 | * OpenArchive |
michael@0 | 107 | * |
michael@0 | 108 | * It's an error to call this more than once on the same nsZipArchive |
michael@0 | 109 | * object. If we were allowed to use exceptions this would have been |
michael@0 | 110 | * part of the constructor |
michael@0 | 111 | * |
michael@0 | 112 | * @param aZipHandle The nsZipHandle used to access the zip |
michael@0 | 113 | * @param aFd Optional PRFileDesc for Windows readahead optimization |
michael@0 | 114 | * @return status code |
michael@0 | 115 | */ |
michael@0 | 116 | nsresult OpenArchive(nsZipHandle *aZipHandle, PRFileDesc *aFd = nullptr); |
michael@0 | 117 | |
michael@0 | 118 | /** |
michael@0 | 119 | * OpenArchive |
michael@0 | 120 | * |
michael@0 | 121 | * Convenience function that generates nsZipHandle |
michael@0 | 122 | * |
michael@0 | 123 | * @param aFile The file used to access the zip |
michael@0 | 124 | * @return status code |
michael@0 | 125 | */ |
michael@0 | 126 | nsresult OpenArchive(nsIFile *aFile); |
michael@0 | 127 | |
michael@0 | 128 | /** |
michael@0 | 129 | * Test the integrity of items in this archive by running |
michael@0 | 130 | * a CRC check after extracting each item into a memory |
michael@0 | 131 | * buffer. If an entry name is supplied only the |
michael@0 | 132 | * specified item is tested. Else, if null is supplied |
michael@0 | 133 | * then all the items in the archive are tested. |
michael@0 | 134 | * |
michael@0 | 135 | * @return status code |
michael@0 | 136 | */ |
michael@0 | 137 | nsresult Test(const char *aEntryName); |
michael@0 | 138 | |
michael@0 | 139 | /** |
michael@0 | 140 | * Closes an open archive. |
michael@0 | 141 | */ |
michael@0 | 142 | nsresult CloseArchive(); |
michael@0 | 143 | |
michael@0 | 144 | /** |
michael@0 | 145 | * GetItem |
michael@0 | 146 | * @param aEntryName Name of file in the archive |
michael@0 | 147 | * @return pointer to nsZipItem |
michael@0 | 148 | */ |
michael@0 | 149 | nsZipItem* GetItem(const char * aEntryName); |
michael@0 | 150 | |
michael@0 | 151 | /** |
michael@0 | 152 | * ExtractFile |
michael@0 | 153 | * |
michael@0 | 154 | * @param zipEntry Name of file in archive to extract |
michael@0 | 155 | * @param outFD Filedescriptor to write contents to |
michael@0 | 156 | * @param outname Name of file to write to |
michael@0 | 157 | * @return status code |
michael@0 | 158 | */ |
michael@0 | 159 | nsresult ExtractFile(nsZipItem * zipEntry, const char *outname, PRFileDesc * outFD); |
michael@0 | 160 | |
michael@0 | 161 | /** |
michael@0 | 162 | * FindInit |
michael@0 | 163 | * |
michael@0 | 164 | * Initializes a search for files in the archive. FindNext() returns |
michael@0 | 165 | * the actual matches. The nsZipFind must be deleted when you're done |
michael@0 | 166 | * |
michael@0 | 167 | * @param aPattern a string or RegExp pattern to search for |
michael@0 | 168 | * (may be nullptr to find all files in archive) |
michael@0 | 169 | * @param aFind a pointer to a pointer to a structure used |
michael@0 | 170 | * in FindNext. In the case of an error this |
michael@0 | 171 | * will be set to nullptr. |
michael@0 | 172 | * @return status code |
michael@0 | 173 | */ |
michael@0 | 174 | nsresult FindInit(const char * aPattern, nsZipFind** aFind); |
michael@0 | 175 | |
michael@0 | 176 | /* |
michael@0 | 177 | * Gets an undependent handle to the mapped file. |
michael@0 | 178 | */ |
michael@0 | 179 | nsZipHandle* GetFD(); |
michael@0 | 180 | |
michael@0 | 181 | /** |
michael@0 | 182 | * Get pointer to the data of the item. |
michael@0 | 183 | * @param aItem Pointer to nsZipItem |
michael@0 | 184 | * reutrns null when zip file is corrupt. |
michael@0 | 185 | */ |
michael@0 | 186 | const uint8_t* GetData(nsZipItem* aItem); |
michael@0 | 187 | |
michael@0 | 188 | bool GetComment(nsACString &aComment); |
michael@0 | 189 | |
michael@0 | 190 | /** |
michael@0 | 191 | * Gets the amount of memory taken up by the archive's mapping. |
michael@0 | 192 | * @return the size |
michael@0 | 193 | */ |
michael@0 | 194 | int64_t SizeOfMapping(); |
michael@0 | 195 | |
michael@0 | 196 | /* |
michael@0 | 197 | * Refcounting |
michael@0 | 198 | */ |
michael@0 | 199 | NS_METHOD_(MozExternalRefCountType) AddRef(void); |
michael@0 | 200 | NS_METHOD_(MozExternalRefCountType) Release(void); |
michael@0 | 201 | |
michael@0 | 202 | private: |
michael@0 | 203 | //--- private members --- |
michael@0 | 204 | mozilla::ThreadSafeAutoRefCnt mRefCnt; /* ref count */ |
michael@0 | 205 | NS_DECL_OWNINGTHREAD |
michael@0 | 206 | |
michael@0 | 207 | nsZipItem* mFiles[ZIP_TABSIZE]; |
michael@0 | 208 | PLArenaPool mArena; |
michael@0 | 209 | |
michael@0 | 210 | const char* mCommentPtr; |
michael@0 | 211 | uint16_t mCommentLen; |
michael@0 | 212 | |
michael@0 | 213 | // Whether we synthesized the directory entries |
michael@0 | 214 | bool mBuiltSynthetics; |
michael@0 | 215 | |
michael@0 | 216 | // file handle |
michael@0 | 217 | nsRefPtr<nsZipHandle> mFd; |
michael@0 | 218 | |
michael@0 | 219 | // file URI, for logging |
michael@0 | 220 | nsCString mURI; |
michael@0 | 221 | |
michael@0 | 222 | private: |
michael@0 | 223 | //--- private methods --- |
michael@0 | 224 | nsZipItem* CreateZipItem(); |
michael@0 | 225 | nsresult BuildFileList(PRFileDesc *aFd = nullptr); |
michael@0 | 226 | nsresult BuildSynthetics(); |
michael@0 | 227 | |
michael@0 | 228 | nsZipArchive& operator=(const nsZipArchive& rhs) MOZ_DELETE; |
michael@0 | 229 | nsZipArchive(const nsZipArchive& rhs) MOZ_DELETE; |
michael@0 | 230 | }; |
michael@0 | 231 | |
michael@0 | 232 | /** |
michael@0 | 233 | * nsZipFind |
michael@0 | 234 | * |
michael@0 | 235 | * a helper class for nsZipArchive, representing a search |
michael@0 | 236 | */ |
michael@0 | 237 | class nsZipFind |
michael@0 | 238 | { |
michael@0 | 239 | public: |
michael@0 | 240 | nsZipFind(nsZipArchive* aZip, char* aPattern, bool regExp); |
michael@0 | 241 | ~nsZipFind(); |
michael@0 | 242 | |
michael@0 | 243 | nsresult FindNext(const char** aResult, uint16_t* aNameLen); |
michael@0 | 244 | |
michael@0 | 245 | private: |
michael@0 | 246 | nsRefPtr<nsZipArchive> mArchive; |
michael@0 | 247 | char* mPattern; |
michael@0 | 248 | nsZipItem* mItem; |
michael@0 | 249 | uint16_t mSlot; |
michael@0 | 250 | bool mRegExp; |
michael@0 | 251 | |
michael@0 | 252 | nsZipFind& operator=(const nsZipFind& rhs) MOZ_DELETE; |
michael@0 | 253 | nsZipFind(const nsZipFind& rhs) MOZ_DELETE; |
michael@0 | 254 | }; |
michael@0 | 255 | |
michael@0 | 256 | /** |
michael@0 | 257 | * nsZipCursor -- a low-level class for reading the individual items in a zip. |
michael@0 | 258 | */ |
michael@0 | 259 | class nsZipCursor { |
michael@0 | 260 | public: |
michael@0 | 261 | /** |
michael@0 | 262 | * Initializes the cursor |
michael@0 | 263 | * |
michael@0 | 264 | * @param aItem Item of interest |
michael@0 | 265 | * @param aZip Archive |
michael@0 | 266 | * @param aBuf Buffer used for decompression. |
michael@0 | 267 | * This determines the maximum Read() size in the compressed case. |
michael@0 | 268 | * @param aBufSize Buffer size |
michael@0 | 269 | * @param doCRC When set to true Read() will check crc |
michael@0 | 270 | */ |
michael@0 | 271 | nsZipCursor(nsZipItem *aItem, nsZipArchive *aZip, uint8_t* aBuf = nullptr, uint32_t aBufSize = 0, bool doCRC = false); |
michael@0 | 272 | |
michael@0 | 273 | ~nsZipCursor(); |
michael@0 | 274 | |
michael@0 | 275 | /** |
michael@0 | 276 | * Performs reads. In the compressed case it uses aBuf(passed in constructor), for stored files |
michael@0 | 277 | * it returns a zero-copy buffer. |
michael@0 | 278 | * |
michael@0 | 279 | * @param aBytesRead Outparam for number of bytes read. |
michael@0 | 280 | * @return data read or nullptr if item is corrupted. |
michael@0 | 281 | */ |
michael@0 | 282 | uint8_t* Read(uint32_t *aBytesRead) { |
michael@0 | 283 | return ReadOrCopy(aBytesRead, false); |
michael@0 | 284 | } |
michael@0 | 285 | |
michael@0 | 286 | /** |
michael@0 | 287 | * Performs a copy. It always uses aBuf(passed in constructor). |
michael@0 | 288 | * |
michael@0 | 289 | * @param aBytesRead Outparam for number of bytes read. |
michael@0 | 290 | * @return data read or nullptr if item is corrupted. |
michael@0 | 291 | */ |
michael@0 | 292 | uint8_t* Copy(uint32_t *aBytesRead) { |
michael@0 | 293 | return ReadOrCopy(aBytesRead, true); |
michael@0 | 294 | } |
michael@0 | 295 | |
michael@0 | 296 | private: |
michael@0 | 297 | /* Actual implementation for both Read and Copy above */ |
michael@0 | 298 | uint8_t* ReadOrCopy(uint32_t *aBytesRead, bool aCopy); |
michael@0 | 299 | |
michael@0 | 300 | nsZipItem *mItem; |
michael@0 | 301 | uint8_t *mBuf; |
michael@0 | 302 | uint32_t mBufSize; |
michael@0 | 303 | z_stream mZs; |
michael@0 | 304 | uint32_t mCRC; |
michael@0 | 305 | bool mDoCRC; |
michael@0 | 306 | }; |
michael@0 | 307 | |
michael@0 | 308 | /** |
michael@0 | 309 | * nsZipItemPtr - a RAII convenience class for reading the individual items in a zip. |
michael@0 | 310 | * It reads whole files and does zero-copy IO for stored files. A buffer is allocated |
michael@0 | 311 | * for decompression. |
michael@0 | 312 | * Do not use when the file may be very large. |
michael@0 | 313 | */ |
michael@0 | 314 | class nsZipItemPtr_base { |
michael@0 | 315 | public: |
michael@0 | 316 | /** |
michael@0 | 317 | * Initializes the reader |
michael@0 | 318 | * |
michael@0 | 319 | * @param aZip Archive |
michael@0 | 320 | * @param aEntryName Archive membername |
michael@0 | 321 | * @param doCRC When set to true Read() will check crc |
michael@0 | 322 | */ |
michael@0 | 323 | nsZipItemPtr_base(nsZipArchive *aZip, const char *aEntryName, bool doCRC); |
michael@0 | 324 | |
michael@0 | 325 | uint32_t Length() const { |
michael@0 | 326 | return mReadlen; |
michael@0 | 327 | } |
michael@0 | 328 | |
michael@0 | 329 | protected: |
michael@0 | 330 | nsRefPtr<nsZipHandle> mZipHandle; |
michael@0 | 331 | nsAutoArrayPtr<uint8_t> mAutoBuf; |
michael@0 | 332 | uint8_t *mReturnBuf; |
michael@0 | 333 | uint32_t mReadlen; |
michael@0 | 334 | }; |
michael@0 | 335 | |
michael@0 | 336 | template <class T> |
michael@0 | 337 | class nsZipItemPtr : public nsZipItemPtr_base { |
michael@0 | 338 | public: |
michael@0 | 339 | nsZipItemPtr(nsZipArchive *aZip, const char *aEntryName, bool doCRC = false) : nsZipItemPtr_base(aZip, aEntryName, doCRC) { } |
michael@0 | 340 | /** |
michael@0 | 341 | * @return buffer containing the whole zip member or nullptr on error. |
michael@0 | 342 | * The returned buffer is owned by nsZipItemReader. |
michael@0 | 343 | */ |
michael@0 | 344 | const T* Buffer() const { |
michael@0 | 345 | return (const T*)mReturnBuf; |
michael@0 | 346 | } |
michael@0 | 347 | |
michael@0 | 348 | operator const T*() const { |
michael@0 | 349 | return Buffer(); |
michael@0 | 350 | } |
michael@0 | 351 | |
michael@0 | 352 | /** |
michael@0 | 353 | * Relinquish ownership of zip member if compressed. |
michael@0 | 354 | * Copy member into a new buffer if uncompressed. |
michael@0 | 355 | * @return a buffer with whole zip member. It is caller's responsibility to free() it. |
michael@0 | 356 | */ |
michael@0 | 357 | T* Forget() { |
michael@0 | 358 | if (!mReturnBuf) |
michael@0 | 359 | return nullptr; |
michael@0 | 360 | // In uncompressed mmap case, give up buffer |
michael@0 | 361 | if (mAutoBuf.get() == mReturnBuf) { |
michael@0 | 362 | mReturnBuf = nullptr; |
michael@0 | 363 | return (T*) mAutoBuf.forget(); |
michael@0 | 364 | } |
michael@0 | 365 | T *ret = (T*) malloc(Length()); |
michael@0 | 366 | memcpy(ret, mReturnBuf, Length()); |
michael@0 | 367 | mReturnBuf = nullptr; |
michael@0 | 368 | return ret; |
michael@0 | 369 | } |
michael@0 | 370 | }; |
michael@0 | 371 | |
michael@0 | 372 | class nsZipHandle { |
michael@0 | 373 | friend class nsZipArchive; |
michael@0 | 374 | friend class mozilla::FileLocation; |
michael@0 | 375 | public: |
michael@0 | 376 | static nsresult Init(nsIFile *file, nsZipHandle **ret, |
michael@0 | 377 | PRFileDesc **aFd = nullptr); |
michael@0 | 378 | static nsresult Init(nsZipArchive *zip, const char *entry, |
michael@0 | 379 | nsZipHandle **ret); |
michael@0 | 380 | |
michael@0 | 381 | NS_METHOD_(MozExternalRefCountType) AddRef(void); |
michael@0 | 382 | NS_METHOD_(MozExternalRefCountType) Release(void); |
michael@0 | 383 | |
michael@0 | 384 | int64_t SizeOfMapping(); |
michael@0 | 385 | |
michael@0 | 386 | protected: |
michael@0 | 387 | const uint8_t * mFileData; /* pointer to mmaped file */ |
michael@0 | 388 | uint32_t mLen; /* length of file and memory mapped area */ |
michael@0 | 389 | mozilla::FileLocation mFile; /* source file if any, for logging */ |
michael@0 | 390 | |
michael@0 | 391 | private: |
michael@0 | 392 | nsZipHandle(); |
michael@0 | 393 | ~nsZipHandle(); |
michael@0 | 394 | |
michael@0 | 395 | PRFileMap * mMap; /* nspr datastructure for mmap */ |
michael@0 | 396 | nsAutoPtr<nsZipItemPtr<uint8_t> > mBuf; |
michael@0 | 397 | mozilla::ThreadSafeAutoRefCnt mRefCnt; /* ref count */ |
michael@0 | 398 | NS_DECL_OWNINGTHREAD |
michael@0 | 399 | }; |
michael@0 | 400 | |
michael@0 | 401 | nsresult gZlibInit(z_stream *zs); |
michael@0 | 402 | |
michael@0 | 403 | #endif /* nsZipArchive_h_ */ |