1 : /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is Mozilla Communicator client code, released
16 : * March 31, 1998.
17 : *
18 : * The Initial Developer of the Original Code is
19 : * Netscape Communications Corporation.
20 : * Portions created by the Initial Developer are Copyright (C) 1998-2000
21 : * the Initial Developer. All Rights Reserved.
22 : *
23 : * Contributor(s):
24 : * Don Bragg <dbragg@netscape.com>
25 : * Samir Gehani <sgehani@netscape.com>
26 : * Mitch Stoltz <mstoltz@netscape.com>
27 : *
28 : * Alternatively, the contents of this file may be used under the terms of
29 : * either the GNU General Public License Version 2 or later (the "GPL"), or
30 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
31 : * in which case the provisions of the GPL or the LGPL are applicable instead
32 : * of those above. If you wish to allow use of your version of this file only
33 : * under the terms of either the GPL or the LGPL, and not to allow others to
34 : * use your version of this file under the terms of the MPL, indicate your
35 : * decision by deleting the provisions above and replace them with the notice
36 : * and other provisions required by the GPL or the LGPL. If you do not delete
37 : * the provisions above, a recipient may use your version of this file under
38 : * the terms of any one of the MPL, the GPL or the LGPL.
39 : *
40 : * ***** END LICENSE BLOCK ***** */
41 :
42 :
43 : #ifndef nsJAR_h__
44 : #define nsJAR_h__
45 :
46 : #include "nscore.h"
47 : #include "pratom.h"
48 : #include "prmem.h"
49 : #include "prio.h"
50 : #include "plstr.h"
51 : #include "prlog.h"
52 : #include "prtypes.h"
53 : #include "prinrval.h"
54 :
55 : #include "mozilla/Mutex.h"
56 : #include "nsIComponentManager.h"
57 : #include "nsCOMPtr.h"
58 : #include "nsString.h"
59 : #include "nsIFile.h"
60 : #include "nsStringEnumerator.h"
61 : #include "nsHashtable.h"
62 : #include "nsIZipReader.h"
63 : #include "nsZipArchive.h"
64 : #include "nsIPrincipal.h"
65 : #include "nsISignatureVerifier.h"
66 : #include "nsIObserverService.h"
67 : #include "nsWeakReference.h"
68 : #include "nsIObserver.h"
69 :
70 : class nsIInputStream;
71 : class nsJARManifestItem;
72 : class nsZipReaderCache;
73 :
74 : /* For mManifestStatus */
75 : typedef enum
76 : {
77 : JAR_MANIFEST_NOT_PARSED = 0,
78 : JAR_VALID_MANIFEST = 1,
79 : JAR_INVALID_SIG = 2,
80 : JAR_INVALID_UNKNOWN_CA = 3,
81 : JAR_INVALID_MANIFEST = 4,
82 : JAR_INVALID_ENTRY = 5,
83 : JAR_NO_MANIFEST = 6,
84 : JAR_NOT_SIGNED = 7
85 : } JARManifestStatusType;
86 :
87 : /*-------------------------------------------------------------------------
88 : * Class nsJAR declaration.
89 : * nsJAR serves as an XPCOM wrapper for nsZipArchive with the addition of
90 : * JAR manifest file parsing.
91 : *------------------------------------------------------------------------*/
92 : class nsJAR : public nsIZipReader
93 : {
94 : // Allows nsJARInputStream to call the verification functions
95 : friend class nsJARInputStream;
96 : // Allows nsZipReaderCache to access mOuterZipEntry
97 : friend class nsZipReaderCache;
98 :
99 : public:
100 :
101 : nsJAR();
102 : virtual ~nsJAR();
103 :
104 : NS_DEFINE_STATIC_CID_ACCESSOR( NS_ZIPREADER_CID )
105 :
106 : NS_DECL_ISUPPORTS
107 :
108 : NS_DECL_NSIZIPREADER
109 :
110 : nsresult GetJarPath(nsACString& aResult);
111 :
112 567 : PRIntervalTime GetReleaseTime() {
113 567 : return mReleaseTime;
114 : }
115 :
116 : bool IsReleased() {
117 : return mReleaseTime != PR_INTERVAL_NO_TIMEOUT;
118 : }
119 :
120 236 : void SetReleaseTime() {
121 236 : mReleaseTime = PR_IntervalNow();
122 236 : }
123 :
124 112 : void ClearReleaseTime() {
125 112 : mReleaseTime = PR_INTERVAL_NO_TIMEOUT;
126 112 : }
127 :
128 252 : void SetZipReaderCache(nsZipReaderCache* cache) {
129 252 : mCache = cache;
130 252 : }
131 :
132 : protected:
133 : //-- Private data members
134 : nsCOMPtr<nsIFile> mZipFile; // The zip/jar file on disk
135 : nsCString mOuterZipEntry; // The entry in the zip this zip is reading from
136 : nsRefPtr<nsZipArchive> mZip; // The underlying zip archive
137 : nsObjectHashtable mManifestData; // Stores metadata for each entry
138 : bool mParsedManifest; // True if manifest has been parsed
139 : nsCOMPtr<nsIPrincipal> mPrincipal; // The entity which signed this file
140 : PRInt16 mGlobalStatus; // Global signature verification status
141 : PRIntervalTime mReleaseTime; // used by nsZipReaderCache for flushing entries
142 : nsZipReaderCache* mCache; // if cached, this points to the cache it's contained in
143 : mozilla::Mutex mLock;
144 : PRInt64 mMtime;
145 : PRInt32 mTotalItemsInManifest;
146 : bool mOpened;
147 :
148 : nsresult ParseManifest();
149 : void ReportError(const nsACString &aFilename, PRInt16 errorCode);
150 : nsresult LoadEntry(const nsACString &aFilename, char** aBuf,
151 : PRUint32* aBufLen = nsnull);
152 : PRInt32 ReadLine(const char** src);
153 : nsresult ParseOneFile(const char* filebuf, PRInt16 aFileType);
154 : nsresult VerifyEntry(nsJARManifestItem* aEntry, const char* aEntryData,
155 : PRUint32 aLen);
156 :
157 : nsresult CalculateDigest(const char* aInBuf, PRUint32 aInBufLen,
158 : nsCString& digest);
159 : };
160 :
161 : /**
162 : * nsJARItem
163 : *
164 : * An individual JAR entry. A set of nsJARItems macthing a
165 : * supplied pattern are returned in a nsJAREnumerator.
166 : */
167 : class nsJARItem : public nsIZipEntry
168 : {
169 : public:
170 : NS_DECL_ISUPPORTS
171 : NS_DECL_NSIZIPENTRY
172 :
173 : nsJARItem(nsZipItem* aZipItem);
174 6000 : virtual ~nsJARItem() {}
175 :
176 : private:
177 : PRUint32 mSize; /* size in original file */
178 : PRUint32 mRealsize; /* inflated size */
179 : PRUint32 mCrc32;
180 : PRTime mLastModTime;
181 : PRUint16 mCompression;
182 : bool mIsDirectory;
183 : bool mIsSynthetic;
184 : };
185 :
186 : /**
187 : * nsJAREnumerator
188 : *
189 : * Enumerates a list of files in a zip archive
190 : * (based on a pattern match in its member nsZipFind).
191 : */
192 : class nsJAREnumerator : public nsIUTF8StringEnumerator
193 : {
194 : public:
195 : NS_DECL_ISUPPORTS
196 : NS_DECL_NSIUTF8STRINGENUMERATOR
197 :
198 2143 : nsJAREnumerator(nsZipFind *aFind) : mFind(aFind), mName(nsnull) {
199 2143 : NS_ASSERTION(mFind, "nsJAREnumerator: Missing zipFind.");
200 2143 : }
201 :
202 : private:
203 : nsZipFind *mFind;
204 : const char* mName; // pointer to an name owned by mArchive -- DON'T delete
205 : PRUint16 mNameLen;
206 :
207 4286 : ~nsJAREnumerator() { delete mFind; }
208 : };
209 :
210 : ////////////////////////////////////////////////////////////////////////////////
211 :
212 : #if defined(DEBUG_warren) || defined(DEBUG_jband)
213 : #define ZIP_CACHE_HIT_RATE
214 : #endif
215 :
216 : class nsZipReaderCache : public nsIZipReaderCache, public nsIObserver,
217 : public nsSupportsWeakReference
218 : {
219 : public:
220 : NS_DECL_ISUPPORTS
221 : NS_DECL_NSIZIPREADERCACHE
222 : NS_DECL_NSIOBSERVER
223 :
224 : nsZipReaderCache();
225 : virtual ~nsZipReaderCache();
226 :
227 : nsresult ReleaseZip(nsJAR* reader);
228 :
229 : protected:
230 : mozilla::Mutex mLock;
231 : PRInt32 mCacheSize;
232 : nsSupportsHashtable mZips;
233 :
234 : #ifdef ZIP_CACHE_HIT_RATE
235 : PRUint32 mZipCacheLookups;
236 : PRUint32 mZipCacheHits;
237 : PRUint32 mZipCacheFlushes;
238 : PRUint32 mZipSyncMisses;
239 : #endif
240 :
241 : };
242 :
243 : ////////////////////////////////////////////////////////////////////////////////
244 :
245 : #endif /* nsJAR_h__ */
|