b=450088 backing out (new reftest failed)
[wine-gecko.git] / modules / libjar / nsJAR.h
blob8129d97056c522c88de1fa8a0f2e431141ba6ed2
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 PRInt64 GetMtime() {
137 return mMtime;
140 protected:
141 //-- Private data members
142 nsCOMPtr<nsIFile> mZipFile; // The zip/jar file on disk
143 nsZipArchive mZip; // The underlying zip archive
144 nsObjectHashtable mManifestData; // Stores metadata for each entry
145 PRBool mParsedManifest; // True if manifest has been parsed
146 nsCOMPtr<nsIPrincipal> mPrincipal; // The entity which signed this file
147 PRInt16 mGlobalStatus; // Global signature verification status
148 PRIntervalTime mReleaseTime; // used by nsZipReaderCache for flushing entries
149 nsZipReaderCache* mCache; // if cached, this points to the cache it's contained in
150 PRLock* mLock;
151 PRInt64 mMtime;
152 PRInt32 mTotalItemsInManifest;
154 //-- Private functions
155 PRFileDesc* OpenFile();
157 nsresult ParseManifest();
158 void ReportError(const char* aFilename, PRInt16 errorCode);
159 nsresult LoadEntry(const char* aFilename, char** aBuf,
160 PRUint32* aBufLen = nsnull);
161 PRInt32 ReadLine(const char** src);
162 nsresult ParseOneFile(const char* filebuf, PRInt16 aFileType);
163 nsresult VerifyEntry(nsJARManifestItem* aEntry, const char* aEntryData,
164 PRUint32 aLen);
166 nsresult CalculateDigest(const char* aInBuf, PRUint32 aInBufLen,
167 char** digest);
169 //-- Debugging
170 void DumpMetadata(const char* aMessage);
174 * nsJARItem
176 * An individual JAR entry. A set of nsJARItems macthing a
177 * supplied pattern are returned in a nsJAREnumerator.
179 class nsJARItem : public nsIZipEntry
181 public:
182 NS_DECL_ISUPPORTS
183 NS_DECL_NSIZIPENTRY
185 nsJARItem(nsZipItem* aZipItem);
186 virtual ~nsJARItem() {}
188 private:
189 PRUint32 mSize; /* size in original file */
190 PRUint32 mRealsize; /* inflated size */
191 PRUint32 mCrc32;
192 PRUint16 mDate;
193 PRUint16 mTime;
194 PRUint8 mCompression;
195 PRPackedBool mIsDirectory;
196 PRPackedBool mIsSynthetic;
200 * nsJAREnumerator
202 * Enumerates a list of files in a zip archive
203 * (based on a pattern match in its member nsZipFind).
205 class nsJAREnumerator : public nsIUTF8StringEnumerator
207 public:
208 NS_DECL_ISUPPORTS
209 NS_DECL_NSIUTF8STRINGENUMERATOR
211 nsJAREnumerator(nsZipFind *aFind) : mFind(aFind), mCurr(nsnull) {
212 NS_ASSERTION(mFind, "nsJAREnumerator: Missing zipFind.");
215 private:
216 nsZipFind *mFind;
217 const char* mCurr; // pointer to an name owned by mArchive -- DON'T delete
219 ~nsJAREnumerator() { delete mFind; }
222 ////////////////////////////////////////////////////////////////////////////////
224 #if defined(DEBUG_warren) || defined(DEBUG_jband)
225 #define ZIP_CACHE_HIT_RATE
226 #endif
228 class nsZipReaderCache : public nsIZipReaderCache, public nsIObserver,
229 public nsSupportsWeakReference
231 public:
232 NS_DECL_ISUPPORTS
233 NS_DECL_NSIZIPREADERCACHE
234 NS_DECL_NSIOBSERVER
236 nsZipReaderCache();
237 virtual ~nsZipReaderCache();
239 nsresult ReleaseZip(nsJAR* reader);
241 protected:
242 PRLock* mLock;
243 PRInt32 mCacheSize;
244 nsSupportsHashtable mZips;
246 #ifdef ZIP_CACHE_HIT_RATE
247 PRUint32 mZipCacheLookups;
248 PRUint32 mZipCacheHits;
249 PRUint32 mZipCacheFlushes;
250 PRUint32 mZipSyncMisses;
251 #endif
255 ////////////////////////////////////////////////////////////////////////////////
257 #endif /* nsJAR_h__ */