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