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: 4; 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 nsJAR_h_ |
michael@0 | 7 | #define nsJAR_h_ |
michael@0 | 8 | |
michael@0 | 9 | #include "nscore.h" |
michael@0 | 10 | #include "prio.h" |
michael@0 | 11 | #include "plstr.h" |
michael@0 | 12 | #include "prlog.h" |
michael@0 | 13 | #include "prinrval.h" |
michael@0 | 14 | |
michael@0 | 15 | #include "mozilla/Mutex.h" |
michael@0 | 16 | #include "nsIComponentManager.h" |
michael@0 | 17 | #include "nsCOMPtr.h" |
michael@0 | 18 | #include "nsClassHashtable.h" |
michael@0 | 19 | #include "nsString.h" |
michael@0 | 20 | #include "nsIFile.h" |
michael@0 | 21 | #include "nsStringEnumerator.h" |
michael@0 | 22 | #include "nsHashKeys.h" |
michael@0 | 23 | #include "nsRefPtrHashtable.h" |
michael@0 | 24 | #include "nsTHashtable.h" |
michael@0 | 25 | #include "nsIZipReader.h" |
michael@0 | 26 | #include "nsZipArchive.h" |
michael@0 | 27 | #include "nsICertificatePrincipal.h" |
michael@0 | 28 | #include "nsISignatureVerifier.h" |
michael@0 | 29 | #include "nsIObserverService.h" |
michael@0 | 30 | #include "nsWeakReference.h" |
michael@0 | 31 | #include "nsIObserver.h" |
michael@0 | 32 | #include "mozilla/Attributes.h" |
michael@0 | 33 | |
michael@0 | 34 | class nsIInputStream; |
michael@0 | 35 | class nsJARManifestItem; |
michael@0 | 36 | class nsZipReaderCache; |
michael@0 | 37 | |
michael@0 | 38 | /* For mManifestStatus */ |
michael@0 | 39 | typedef enum |
michael@0 | 40 | { |
michael@0 | 41 | JAR_MANIFEST_NOT_PARSED = 0, |
michael@0 | 42 | JAR_VALID_MANIFEST = 1, |
michael@0 | 43 | JAR_INVALID_SIG = 2, |
michael@0 | 44 | JAR_INVALID_UNKNOWN_CA = 3, |
michael@0 | 45 | JAR_INVALID_MANIFEST = 4, |
michael@0 | 46 | JAR_INVALID_ENTRY = 5, |
michael@0 | 47 | JAR_NO_MANIFEST = 6, |
michael@0 | 48 | JAR_NOT_SIGNED = 7 |
michael@0 | 49 | } JARManifestStatusType; |
michael@0 | 50 | |
michael@0 | 51 | /*------------------------------------------------------------------------- |
michael@0 | 52 | * Class nsJAR declaration. |
michael@0 | 53 | * nsJAR serves as an XPCOM wrapper for nsZipArchive with the addition of |
michael@0 | 54 | * JAR manifest file parsing. |
michael@0 | 55 | *------------------------------------------------------------------------*/ |
michael@0 | 56 | class nsJAR : public nsIZipReader |
michael@0 | 57 | { |
michael@0 | 58 | // Allows nsJARInputStream to call the verification functions |
michael@0 | 59 | friend class nsJARInputStream; |
michael@0 | 60 | // Allows nsZipReaderCache to access mOuterZipEntry |
michael@0 | 61 | friend class nsZipReaderCache; |
michael@0 | 62 | |
michael@0 | 63 | public: |
michael@0 | 64 | |
michael@0 | 65 | nsJAR(); |
michael@0 | 66 | virtual ~nsJAR(); |
michael@0 | 67 | |
michael@0 | 68 | NS_DEFINE_STATIC_CID_ACCESSOR( NS_ZIPREADER_CID ) |
michael@0 | 69 | |
michael@0 | 70 | NS_DECL_THREADSAFE_ISUPPORTS |
michael@0 | 71 | |
michael@0 | 72 | NS_DECL_NSIZIPREADER |
michael@0 | 73 | |
michael@0 | 74 | nsresult GetJarPath(nsACString& aResult); |
michael@0 | 75 | |
michael@0 | 76 | PRIntervalTime GetReleaseTime() { |
michael@0 | 77 | return mReleaseTime; |
michael@0 | 78 | } |
michael@0 | 79 | |
michael@0 | 80 | bool IsReleased() { |
michael@0 | 81 | return mReleaseTime != PR_INTERVAL_NO_TIMEOUT; |
michael@0 | 82 | } |
michael@0 | 83 | |
michael@0 | 84 | void SetReleaseTime() { |
michael@0 | 85 | mReleaseTime = PR_IntervalNow(); |
michael@0 | 86 | } |
michael@0 | 87 | |
michael@0 | 88 | void ClearReleaseTime() { |
michael@0 | 89 | mReleaseTime = PR_INTERVAL_NO_TIMEOUT; |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | void SetZipReaderCache(nsZipReaderCache* cache) { |
michael@0 | 93 | mCache = cache; |
michael@0 | 94 | } |
michael@0 | 95 | |
michael@0 | 96 | protected: |
michael@0 | 97 | typedef nsClassHashtable<nsCStringHashKey, nsJARManifestItem> ManifestDataHashtable; |
michael@0 | 98 | |
michael@0 | 99 | //-- Private data members |
michael@0 | 100 | nsCOMPtr<nsIFile> mZipFile; // The zip/jar file on disk |
michael@0 | 101 | nsCString mOuterZipEntry; // The entry in the zip this zip is reading from |
michael@0 | 102 | nsRefPtr<nsZipArchive> mZip; // The underlying zip archive |
michael@0 | 103 | ManifestDataHashtable mManifestData; // Stores metadata for each entry |
michael@0 | 104 | bool mParsedManifest; // True if manifest has been parsed |
michael@0 | 105 | nsCOMPtr<nsICertificatePrincipal> mPrincipal; // The entity which signed this file |
michael@0 | 106 | int16_t mGlobalStatus; // Global signature verification status |
michael@0 | 107 | PRIntervalTime mReleaseTime; // used by nsZipReaderCache for flushing entries |
michael@0 | 108 | nsZipReaderCache* mCache; // if cached, this points to the cache it's contained in |
michael@0 | 109 | mozilla::Mutex mLock; |
michael@0 | 110 | int64_t mMtime; |
michael@0 | 111 | int32_t mTotalItemsInManifest; |
michael@0 | 112 | bool mOpened; |
michael@0 | 113 | |
michael@0 | 114 | nsresult ParseManifest(); |
michael@0 | 115 | void ReportError(const nsACString &aFilename, int16_t errorCode); |
michael@0 | 116 | nsresult LoadEntry(const nsACString &aFilename, char** aBuf, |
michael@0 | 117 | uint32_t* aBufLen = nullptr); |
michael@0 | 118 | int32_t ReadLine(const char** src); |
michael@0 | 119 | nsresult ParseOneFile(const char* filebuf, int16_t aFileType); |
michael@0 | 120 | nsresult VerifyEntry(nsJARManifestItem* aEntry, const char* aEntryData, |
michael@0 | 121 | uint32_t aLen); |
michael@0 | 122 | |
michael@0 | 123 | nsresult CalculateDigest(const char* aInBuf, uint32_t aInBufLen, |
michael@0 | 124 | nsCString& digest); |
michael@0 | 125 | }; |
michael@0 | 126 | |
michael@0 | 127 | /** |
michael@0 | 128 | * nsJARItem |
michael@0 | 129 | * |
michael@0 | 130 | * An individual JAR entry. A set of nsJARItems matching a |
michael@0 | 131 | * supplied pattern are returned in a nsJAREnumerator. |
michael@0 | 132 | */ |
michael@0 | 133 | class nsJARItem : public nsIZipEntry |
michael@0 | 134 | { |
michael@0 | 135 | public: |
michael@0 | 136 | NS_DECL_THREADSAFE_ISUPPORTS |
michael@0 | 137 | NS_DECL_NSIZIPENTRY |
michael@0 | 138 | |
michael@0 | 139 | nsJARItem(nsZipItem* aZipItem); |
michael@0 | 140 | virtual ~nsJARItem() {} |
michael@0 | 141 | |
michael@0 | 142 | private: |
michael@0 | 143 | uint32_t mSize; /* size in original file */ |
michael@0 | 144 | uint32_t mRealsize; /* inflated size */ |
michael@0 | 145 | uint32_t mCrc32; |
michael@0 | 146 | PRTime mLastModTime; |
michael@0 | 147 | uint16_t mCompression; |
michael@0 | 148 | uint32_t mPermissions; |
michael@0 | 149 | bool mIsDirectory; |
michael@0 | 150 | bool mIsSynthetic; |
michael@0 | 151 | }; |
michael@0 | 152 | |
michael@0 | 153 | /** |
michael@0 | 154 | * nsJAREnumerator |
michael@0 | 155 | * |
michael@0 | 156 | * Enumerates a list of files in a zip archive |
michael@0 | 157 | * (based on a pattern match in its member nsZipFind). |
michael@0 | 158 | */ |
michael@0 | 159 | class nsJAREnumerator MOZ_FINAL : public nsIUTF8StringEnumerator |
michael@0 | 160 | { |
michael@0 | 161 | public: |
michael@0 | 162 | NS_DECL_THREADSAFE_ISUPPORTS |
michael@0 | 163 | NS_DECL_NSIUTF8STRINGENUMERATOR |
michael@0 | 164 | |
michael@0 | 165 | nsJAREnumerator(nsZipFind *aFind) : mFind(aFind), mName(nullptr) { |
michael@0 | 166 | NS_ASSERTION(mFind, "nsJAREnumerator: Missing zipFind."); |
michael@0 | 167 | } |
michael@0 | 168 | |
michael@0 | 169 | private: |
michael@0 | 170 | nsZipFind *mFind; |
michael@0 | 171 | const char* mName; // pointer to an name owned by mArchive -- DON'T delete |
michael@0 | 172 | uint16_t mNameLen; |
michael@0 | 173 | |
michael@0 | 174 | ~nsJAREnumerator() { delete mFind; } |
michael@0 | 175 | }; |
michael@0 | 176 | |
michael@0 | 177 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 178 | |
michael@0 | 179 | #if defined(DEBUG_warren) || defined(DEBUG_jband) |
michael@0 | 180 | #define ZIP_CACHE_HIT_RATE |
michael@0 | 181 | #endif |
michael@0 | 182 | |
michael@0 | 183 | class nsZipReaderCache : public nsIZipReaderCache, public nsIObserver, |
michael@0 | 184 | public nsSupportsWeakReference |
michael@0 | 185 | { |
michael@0 | 186 | public: |
michael@0 | 187 | NS_DECL_THREADSAFE_ISUPPORTS |
michael@0 | 188 | NS_DECL_NSIZIPREADERCACHE |
michael@0 | 189 | NS_DECL_NSIOBSERVER |
michael@0 | 190 | |
michael@0 | 191 | nsZipReaderCache(); |
michael@0 | 192 | virtual ~nsZipReaderCache(); |
michael@0 | 193 | |
michael@0 | 194 | nsresult ReleaseZip(nsJAR* reader); |
michael@0 | 195 | |
michael@0 | 196 | typedef nsRefPtrHashtable<nsCStringHashKey, nsJAR> ZipsHashtable; |
michael@0 | 197 | |
michael@0 | 198 | protected: |
michael@0 | 199 | |
michael@0 | 200 | mozilla::Mutex mLock; |
michael@0 | 201 | uint32_t mCacheSize; |
michael@0 | 202 | ZipsHashtable mZips; |
michael@0 | 203 | |
michael@0 | 204 | #ifdef ZIP_CACHE_HIT_RATE |
michael@0 | 205 | uint32_t mZipCacheLookups; |
michael@0 | 206 | uint32_t mZipCacheHits; |
michael@0 | 207 | uint32_t mZipCacheFlushes; |
michael@0 | 208 | uint32_t mZipSyncMisses; |
michael@0 | 209 | #endif |
michael@0 | 210 | |
michael@0 | 211 | }; |
michael@0 | 212 | |
michael@0 | 213 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 214 | |
michael@0 | 215 | #endif /* nsJAR_h_ */ |