Backout bug 449422.
[wine-gecko.git] / modules / libjar / nsJAR.h
blobf515a34f283d9fdc918a808bc26baac12f406944
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
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/
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.
15 * The Original Code is Mozilla Communicator client code, released
16 * March 31, 1998.
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.
23 * Contributor(s):
24 * Don Bragg <dbragg@netscape.com>
25 * Samir Gehani <sgehani@netscape.com>
26 * Mitch Stoltz <mstoltz@netscape.com>
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.
40 * ***** END LICENSE BLOCK ***** */
43 #ifndef nsJAR_h__
44 #define nsJAR_h__
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"
55 #include "nsIComponentManager.h"
56 #include "nsCOMPtr.h"
57 #include "nsString.h"
58 #include "nsIFile.h"
59 #include "nsStringEnumerator.h"
60 #include "nsHashtable.h"
61 #include "nsAutoLock.h"
62 #include "nsIZipReader.h"
63 #include "nsIJAR.h"
64 #include "nsZipArchive.h"
65 #include "zipfile.h"
66 #include "nsIPrincipal.h"
67 #include "nsISignatureVerifier.h"
68 #include "nsIObserverService.h"
69 #include "nsWeakReference.h"
70 #include "nsIObserver.h"
72 class nsIInputStream;
73 class nsJARManifestItem;
74 class nsZipReaderCache;
76 /* For mManifestStatus */
77 typedef enum
79 JAR_MANIFEST_NOT_PARSED = 0,
80 JAR_VALID_MANIFEST = 1,
81 JAR_INVALID_SIG = 2,
82 JAR_INVALID_UNKNOWN_CA = 3,
83 JAR_INVALID_MANIFEST = 4,
84 JAR_INVALID_ENTRY = 5,
85 JAR_NO_MANIFEST = 6,
86 JAR_NOT_SIGNED = 7
87 } JARManifestStatusType;
89 PRTime GetModTime(PRUint16 aDate, PRUint16 aTime);
91 /*-------------------------------------------------------------------------
92 * Class nsJAR declaration.
93 * nsJAR serves as an XPCOM wrapper for nsZipArchive with the addition of
94 * JAR manifest file parsing.
95 *------------------------------------------------------------------------*/
96 class nsJAR : public nsIZipReader, public nsIJAR
98 // Allows nsJARInputStream to call the verification functions
99 friend class nsJARInputStream;
101 public:
103 nsJAR();
104 virtual ~nsJAR();
106 NS_DEFINE_STATIC_CID_ACCESSOR( NS_ZIPREADER_CID )
108 NS_DECL_ISUPPORTS
110 NS_DECL_NSIZIPREADER
112 NS_DECL_NSIJAR
114 nsresult GetJarPath(nsACString& aResult);
116 PRIntervalTime GetReleaseTime() {
117 return mReleaseTime;
120 PRBool IsReleased() {
121 return mReleaseTime != PR_INTERVAL_NO_TIMEOUT;
124 void SetReleaseTime() {
125 mReleaseTime = PR_IntervalNow();
128 void ClearReleaseTime() {
129 mReleaseTime = PR_INTERVAL_NO_TIMEOUT;
132 void SetZipReaderCache(nsZipReaderCache* cache) {
133 mCache = cache;
136 protected:
137 //-- Private data members
138 nsCOMPtr<nsIFile> mZipFile; // The zip/jar file on disk
139 nsZipArchive mZip; // The underlying zip archive
140 nsObjectHashtable mManifestData; // Stores metadata for each entry
141 PRBool mParsedManifest; // True if manifest has been parsed
142 nsCOMPtr<nsIPrincipal> mPrincipal; // The entity which signed this file
143 PRInt16 mGlobalStatus; // Global signature verification status
144 PRIntervalTime mReleaseTime; // used by nsZipReaderCache for flushing entries
145 nsZipReaderCache* mCache; // if cached, this points to the cache it's contained in
146 PRLock* mLock;
147 PRInt32 mTotalItemsInManifest;
149 //-- Private functions
150 PRFileDesc* OpenFile();
152 nsresult ParseManifest(nsISignatureVerifier* verifier);
153 void ReportError(const char* aFilename, PRInt16 errorCode);
154 nsresult LoadEntry(const char* aFilename, char** aBuf,
155 PRUint32* aBufLen = nsnull);
156 PRInt32 ReadLine(const char** src);
157 nsresult ParseOneFile(const char* filebuf, PRInt16 aFileType);
158 nsresult VerifyEntry(nsJARManifestItem* aEntry, const char* aEntryData,
159 PRUint32 aLen);
161 nsresult CalculateDigest(const char* aInBuf, PRUint32 aInBufLen,
162 char** digest);
164 //-- Debugging
165 void DumpMetadata(const char* aMessage);
169 * nsJARItem
171 * An individual JAR entry. A set of nsJARItems macthing a
172 * supplied pattern are returned in a nsJAREnumerator.
174 class nsJARItem : public nsIZipEntry
176 public:
177 NS_DECL_ISUPPORTS
178 NS_DECL_NSIZIPENTRY
180 nsJARItem(nsZipItem* aZipItem);
181 virtual ~nsJARItem() {}
183 private:
184 PRUint32 mSize; /* size in original file */
185 PRUint32 mRealsize; /* inflated size */
186 PRUint32 mCrc32;
187 PRUint16 mDate;
188 PRUint16 mTime;
189 PRUint8 mCompression;
190 PRPackedBool mIsDirectory;
191 PRPackedBool mIsSynthetic;
195 * nsJAREnumerator
197 * Enumerates a list of files in a zip archive
198 * (based on a pattern match in its member nsZipFind).
200 class nsJAREnumerator : public nsIUTF8StringEnumerator
202 public:
203 NS_DECL_ISUPPORTS
204 NS_DECL_NSIUTF8STRINGENUMERATOR
206 nsJAREnumerator(nsZipFind *aFind) : mFind(aFind), mCurr(nsnull) {
207 NS_ASSERTION(mFind, "nsJAREnumerator: Missing zipFind.");
210 private:
211 nsZipFind *mFind;
212 const char* mCurr; // pointer to an name owned by mArchive -- DON'T delete
214 ~nsJAREnumerator() { delete mFind; }
217 ////////////////////////////////////////////////////////////////////////////////
219 #if defined(DEBUG_warren) || defined(DEBUG_jband)
220 #define ZIP_CACHE_HIT_RATE
221 #endif
223 class nsZipReaderCache : public nsIZipReaderCache, public nsIObserver,
224 public nsSupportsWeakReference
226 public:
227 NS_DECL_ISUPPORTS
228 NS_DECL_NSIZIPREADERCACHE
229 NS_DECL_NSIOBSERVER
231 nsZipReaderCache();
232 virtual ~nsZipReaderCache();
234 nsresult ReleaseZip(nsJAR* reader);
236 protected:
237 PRLock* mLock;
238 PRInt32 mCacheSize;
239 nsSupportsHashtable mZips;
241 #ifdef ZIP_CACHE_HIT_RATE
242 PRUint32 mZipCacheLookups;
243 PRUint32 mZipCacheHits;
244 PRUint32 mZipCacheFlushes;
245 PRUint32 mZipSyncMisses;
246 #endif
250 ////////////////////////////////////////////////////////////////////////////////
252 #endif /* nsJAR_h__ */