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