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
15 * The Original Code is Mozilla Communicator client code, released
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.
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 ***** */
55 #include "nsIComponentManager.h"
59 #include "nsStringEnumerator.h"
60 #include "nsHashtable.h"
61 #include "nsAutoLock.h"
62 #include "nsIZipReader.h"
64 #include "nsZipArchive.h"
66 #include "nsIPrincipal.h"
67 #include "nsISignatureVerifier.h"
68 #include "nsIObserverService.h"
69 #include "nsWeakReference.h"
70 #include "nsIObserver.h"
73 class nsJARManifestItem
;
74 class nsZipReaderCache
;
76 /* For mManifestStatus */
79 JAR_MANIFEST_NOT_PARSED
= 0,
80 JAR_VALID_MANIFEST
= 1,
82 JAR_INVALID_UNKNOWN_CA
= 3,
83 JAR_INVALID_MANIFEST
= 4,
84 JAR_INVALID_ENTRY
= 5,
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
;
106 NS_DEFINE_STATIC_CID_ACCESSOR( NS_ZIPREADER_CID
)
114 nsresult
GetJarPath(nsACString
& aResult
);
116 PRIntervalTime
GetReleaseTime() {
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
) {
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
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
,
166 nsresult
CalculateDigest(const char* aInBuf
, PRUint32 aInBufLen
,
170 void DumpMetadata(const char* aMessage
);
176 * An individual JAR entry. A set of nsJARItems macthing a
177 * supplied pattern are returned in a nsJAREnumerator.
179 class nsJARItem
: public nsIZipEntry
185 nsJARItem(nsZipItem
* aZipItem
);
186 virtual ~nsJARItem() {}
189 PRUint32 mSize
; /* size in original file */
190 PRUint32 mRealsize
; /* inflated size */
194 PRUint8 mCompression
;
195 PRPackedBool mIsDirectory
;
196 PRPackedBool mIsSynthetic
;
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
209 NS_DECL_NSIUTF8STRINGENUMERATOR
211 nsJAREnumerator(nsZipFind
*aFind
) : mFind(aFind
), mCurr(nsnull
) {
212 NS_ASSERTION(mFind
, "nsJAREnumerator: Missing zipFind.");
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
228 class nsZipReaderCache
: public nsIZipReaderCache
, public nsIObserver
,
229 public nsSupportsWeakReference
233 NS_DECL_NSIZIPREADERCACHE
237 virtual ~nsZipReaderCache();
239 nsresult
ReleaseZip(nsJAR
* reader
);
244 nsSupportsHashtable mZips
;
246 #ifdef ZIP_CACHE_HIT_RATE
247 PRUint32 mZipCacheLookups
;
248 PRUint32 mZipCacheHits
;
249 PRUint32 mZipCacheFlushes
;
250 PRUint32 mZipSyncMisses
;
255 ////////////////////////////////////////////////////////////////////////////////
257 #endif /* nsJAR_h__ */