Backed out changeset c40e797b011f (bug 765285) for ASAN bustage. r=whyunsetc-nanyway
[gecko.git] / modules / libjar / nsJAR.h
blob8eda159f5b445fd28b5b6df2ebf53a1efbb81243
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef nsJAR_h__
8 #define nsJAR_h__
10 #include "nscore.h"
11 #include "prio.h"
12 #include "plstr.h"
13 #include "prlog.h"
14 #include "prinrval.h"
16 #include "mozilla/Mutex.h"
17 #include "nsIComponentManager.h"
18 #include "nsCOMPtr.h"
19 #include "nsString.h"
20 #include "nsIFile.h"
21 #include "nsStringEnumerator.h"
22 #include "nsHashtable.h"
23 #include "nsIZipReader.h"
24 #include "nsZipArchive.h"
25 #include "nsICertificatePrincipal.h"
26 #include "nsISignatureVerifier.h"
27 #include "nsIObserverService.h"
28 #include "nsWeakReference.h"
29 #include "nsIObserver.h"
30 #include "mozilla/Attributes.h"
32 class nsIInputStream;
33 class nsJARManifestItem;
34 class nsZipReaderCache;
36 /* For mManifestStatus */
37 typedef enum
39 JAR_MANIFEST_NOT_PARSED = 0,
40 JAR_VALID_MANIFEST = 1,
41 JAR_INVALID_SIG = 2,
42 JAR_INVALID_UNKNOWN_CA = 3,
43 JAR_INVALID_MANIFEST = 4,
44 JAR_INVALID_ENTRY = 5,
45 JAR_NO_MANIFEST = 6,
46 JAR_NOT_SIGNED = 7
47 } JARManifestStatusType;
49 /*-------------------------------------------------------------------------
50 * Class nsJAR declaration.
51 * nsJAR serves as an XPCOM wrapper for nsZipArchive with the addition of
52 * JAR manifest file parsing.
53 *------------------------------------------------------------------------*/
54 class nsJAR : public nsIZipReader
56 // Allows nsJARInputStream to call the verification functions
57 friend class nsJARInputStream;
58 // Allows nsZipReaderCache to access mOuterZipEntry
59 friend class nsZipReaderCache;
61 public:
63 nsJAR();
64 virtual ~nsJAR();
66 NS_DEFINE_STATIC_CID_ACCESSOR( NS_ZIPREADER_CID )
68 NS_DECL_THREADSAFE_ISUPPORTS
70 NS_DECL_NSIZIPREADER
72 nsresult GetJarPath(nsACString& aResult);
74 PRIntervalTime GetReleaseTime() {
75 return mReleaseTime;
78 bool IsReleased() {
79 return mReleaseTime != PR_INTERVAL_NO_TIMEOUT;
82 void SetReleaseTime() {
83 mReleaseTime = PR_IntervalNow();
86 void ClearReleaseTime() {
87 mReleaseTime = PR_INTERVAL_NO_TIMEOUT;
90 void SetZipReaderCache(nsZipReaderCache* cache) {
91 mCache = cache;
94 protected:
95 //-- Private data members
96 nsCOMPtr<nsIFile> mZipFile; // The zip/jar file on disk
97 nsCString mOuterZipEntry; // The entry in the zip this zip is reading from
98 nsRefPtr<nsZipArchive> mZip; // The underlying zip archive
99 nsObjectHashtable mManifestData; // Stores metadata for each entry
100 bool mParsedManifest; // True if manifest has been parsed
101 nsCOMPtr<nsICertificatePrincipal> mPrincipal; // The entity which signed this file
102 int16_t mGlobalStatus; // Global signature verification status
103 PRIntervalTime mReleaseTime; // used by nsZipReaderCache for flushing entries
104 nsZipReaderCache* mCache; // if cached, this points to the cache it's contained in
105 mozilla::Mutex mLock;
106 int64_t mMtime;
107 int32_t mTotalItemsInManifest;
108 bool mOpened;
110 nsresult ParseManifest();
111 void ReportError(const nsACString &aFilename, int16_t errorCode);
112 nsresult LoadEntry(const nsACString &aFilename, char** aBuf,
113 uint32_t* aBufLen = nullptr);
114 int32_t ReadLine(const char** src);
115 nsresult ParseOneFile(const char* filebuf, int16_t aFileType);
116 nsresult VerifyEntry(nsJARManifestItem* aEntry, const char* aEntryData,
117 uint32_t aLen);
119 nsresult CalculateDigest(const char* aInBuf, uint32_t aInBufLen,
120 nsCString& digest);
124 * nsJARItem
126 * An individual JAR entry. A set of nsJARItems macthing a
127 * supplied pattern are returned in a nsJAREnumerator.
129 class nsJARItem : public nsIZipEntry
131 public:
132 NS_DECL_THREADSAFE_ISUPPORTS
133 NS_DECL_NSIZIPENTRY
135 nsJARItem(nsZipItem* aZipItem);
136 virtual ~nsJARItem() {}
138 private:
139 uint32_t mSize; /* size in original file */
140 uint32_t mRealsize; /* inflated size */
141 uint32_t mCrc32;
142 PRTime mLastModTime;
143 uint16_t mCompression;
144 uint32_t mPermissions;
145 bool mIsDirectory;
146 bool mIsSynthetic;
150 * nsJAREnumerator
152 * Enumerates a list of files in a zip archive
153 * (based on a pattern match in its member nsZipFind).
155 class nsJAREnumerator MOZ_FINAL : public nsIUTF8StringEnumerator
157 public:
158 NS_DECL_THREADSAFE_ISUPPORTS
159 NS_DECL_NSIUTF8STRINGENUMERATOR
161 nsJAREnumerator(nsZipFind *aFind) : mFind(aFind), mName(nullptr) {
162 NS_ASSERTION(mFind, "nsJAREnumerator: Missing zipFind.");
165 private:
166 nsZipFind *mFind;
167 const char* mName; // pointer to an name owned by mArchive -- DON'T delete
168 uint16_t mNameLen;
170 ~nsJAREnumerator() { delete mFind; }
173 ////////////////////////////////////////////////////////////////////////////////
175 #if defined(DEBUG_warren) || defined(DEBUG_jband)
176 #define ZIP_CACHE_HIT_RATE
177 #endif
179 class nsZipReaderCache : public nsIZipReaderCache, public nsIObserver,
180 public nsSupportsWeakReference
182 public:
183 NS_DECL_THREADSAFE_ISUPPORTS
184 NS_DECL_NSIZIPREADERCACHE
185 NS_DECL_NSIOBSERVER
187 nsZipReaderCache();
188 virtual ~nsZipReaderCache();
190 nsresult ReleaseZip(nsJAR* reader);
192 protected:
193 mozilla::Mutex mLock;
194 int32_t mCacheSize;
195 nsSupportsHashtable mZips;
197 #ifdef ZIP_CACHE_HIT_RATE
198 uint32_t mZipCacheLookups;
199 uint32_t mZipCacheHits;
200 uint32_t mZipCacheFlushes;
201 uint32_t mZipSyncMisses;
202 #endif
206 ////////////////////////////////////////////////////////////////////////////////
208 #endif /* nsJAR_h__ */