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/. */
16 #include "mozilla/Mutex.h"
17 #include "nsIComponentManager.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"
33 class nsJARManifestItem
;
34 class nsZipReaderCache
;
36 /* For mManifestStatus */
39 JAR_MANIFEST_NOT_PARSED
= 0,
40 JAR_VALID_MANIFEST
= 1,
42 JAR_INVALID_UNKNOWN_CA
= 3,
43 JAR_INVALID_MANIFEST
= 4,
44 JAR_INVALID_ENTRY
= 5,
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
;
66 NS_DEFINE_STATIC_CID_ACCESSOR( NS_ZIPREADER_CID
)
68 NS_DECL_THREADSAFE_ISUPPORTS
72 nsresult
GetJarPath(nsACString
& aResult
);
74 PRIntervalTime
GetReleaseTime() {
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
) {
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
;
107 int32_t mTotalItemsInManifest
;
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
,
119 nsresult
CalculateDigest(const char* aInBuf
, uint32_t aInBufLen
,
126 * An individual JAR entry. A set of nsJARItems macthing a
127 * supplied pattern are returned in a nsJAREnumerator.
129 class nsJARItem
: public nsIZipEntry
132 NS_DECL_THREADSAFE_ISUPPORTS
135 nsJARItem(nsZipItem
* aZipItem
);
136 virtual ~nsJARItem() {}
139 uint32_t mSize
; /* size in original file */
140 uint32_t mRealsize
; /* inflated size */
143 uint16_t mCompression
;
144 uint32_t mPermissions
;
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
158 NS_DECL_THREADSAFE_ISUPPORTS
159 NS_DECL_NSIUTF8STRINGENUMERATOR
161 nsJAREnumerator(nsZipFind
*aFind
) : mFind(aFind
), mName(nullptr) {
162 NS_ASSERTION(mFind
, "nsJAREnumerator: Missing zipFind.");
167 const char* mName
; // pointer to an name owned by mArchive -- DON'T delete
170 ~nsJAREnumerator() { delete mFind
; }
173 ////////////////////////////////////////////////////////////////////////////////
175 #if defined(DEBUG_warren) || defined(DEBUG_jband)
176 #define ZIP_CACHE_HIT_RATE
179 class nsZipReaderCache
: public nsIZipReaderCache
, public nsIObserver
,
180 public nsSupportsWeakReference
183 NS_DECL_THREADSAFE_ISUPPORTS
184 NS_DECL_NSIZIPREADERCACHE
188 virtual ~nsZipReaderCache();
190 nsresult
ReleaseZip(nsJAR
* reader
);
193 mozilla::Mutex mLock
;
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
;
206 ////////////////////////////////////////////////////////////////////////////////
208 #endif /* nsJAR_h__ */