Back to index

lightning-sunbird  0.9+nobinonly
nsJAR.h
Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Mozilla Communicator client code, released
00016  * March 31, 1998.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications Corporation.
00020  * Portions created by the Initial Developer are Copyright (C) 1998-2000
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   Don Bragg <dbragg@netscape.com>
00025  *   Samir Gehani <sgehani@netscape.com>
00026  *   Mitch Stoltz <mstoltz@netscape.com>
00027  *
00028  * Alternatively, the contents of this file may be used under the terms of
00029  * either the GNU General Public License Version 2 or later (the "GPL"), or
00030  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00031  * in which case the provisions of the GPL or the LGPL are applicable instead
00032  * of those above. If you wish to allow use of your version of this file only
00033  * under the terms of either the GPL or the LGPL, and not to allow others to
00034  * use your version of this file under the terms of the MPL, indicate your
00035  * decision by deleting the provisions above and replace them with the notice
00036  * and other provisions required by the GPL or the LGPL. If you do not delete
00037  * the provisions above, a recipient may use your version of this file under
00038  * the terms of any one of the MPL, the GPL or the LGPL.
00039  *
00040  * ***** END LICENSE BLOCK ***** */
00041 
00042 
00043 #ifndef nsJAR_h__
00044 #define nsJAR_h__
00045 
00046 #include "nscore.h"
00047 #include "pratom.h"
00048 #include "prmem.h"
00049 #include "prio.h"
00050 #include "plstr.h"
00051 #include "prlog.h"
00052 #include "prtypes.h"
00053 #include "prinrval.h"
00054 
00055 #include "nsIComponentManager.h"
00056 #include "nsCOMPtr.h"
00057 #include "nsString.h"
00058 #include "nsIFile.h"
00059 #include "nsIEnumerator.h"
00060 #include "nsVoidArray.h"
00061 #include "nsHashtable.h"
00062 #include "nsAutoLock.h"
00063 #include "nsIZipReader.h"
00064 #include "nsIJAR.h"
00065 #include "nsZipArchive.h"
00066 #include "zipfile.h"
00067 #include "nsIPrincipal.h"
00068 #include "nsISignatureVerifier.h"
00069 #include "nsIObserverService.h"
00070 #include "nsWeakReference.h"
00071 #include "nsIObserver.h"
00072 
00073 class nsIInputStream;
00074 class nsJARManifestItem;
00075 class nsZipReaderCache;
00076 
00077 /*-------------------------------------------------------------------------
00078  * Class nsJAR declaration. 
00079  * nsJAR serves as an XPCOM wrapper for nsZipArchive with the addition of 
00080  * JAR manifest file parsing. 
00081  *------------------------------------------------------------------------*/
00082 class nsJAR : public nsIZipReader, public nsIJAR
00083 {
00084   // Allows nsJARInputStream to call the verification functions
00085   friend class nsJARInputStream;
00086 
00087   public:
00088 
00089     nsJAR();
00090     virtual ~nsJAR();
00091     
00092     NS_DEFINE_STATIC_CID_ACCESSOR( NS_ZIPREADER_CID );
00093   
00094     NS_DECL_ISUPPORTS
00095 
00096     NS_DECL_NSIZIPREADER
00097 
00098     NS_DECL_NSIJAR
00099 
00100     PRIntervalTime GetReleaseTime() {
00101         return mReleaseTime;
00102     }
00103     
00104     PRBool IsReleased() {
00105         return mReleaseTime != PR_INTERVAL_NO_TIMEOUT;
00106     }
00107 
00108     void SetReleaseTime() {
00109       mReleaseTime = PR_IntervalNow();
00110     }
00111     
00112     void ClearReleaseTime() {
00113       mReleaseTime = PR_INTERVAL_NO_TIMEOUT;
00114     }
00115     
00116     void SetZipReaderCache(nsZipReaderCache* cache) {
00117       mCache = cache;
00118     }
00119 
00120     PRFileDesc* OpenFile();
00121   protected:
00122     //-- Private data members
00123     nsCOMPtr<nsIFile>        mZipFile;        // The zip/jar file on disk
00124     nsZipArchive             mZip;            // The underlying zip archive
00125     nsObjectHashtable        mManifestData;   // Stores metadata for each entry
00126     PRBool                   mParsedManifest; // True if manifest has been parsed
00127     nsCOMPtr<nsIPrincipal>   mPrincipal;      // The entity which signed this file
00128     PRInt16                  mGlobalStatus;   // Global signature verification status
00129     PRIntervalTime           mReleaseTime;    // used by nsZipReaderCache for flushing entries
00130     nsZipReaderCache*        mCache;          // if cached, this points to the cache it's contained in
00131        PRLock*                                    mLock;       
00132     PRInt32                  mTotalItemsInManifest;
00133     PRFileDesc*              mFd;
00134     
00135     //-- Private functions
00136     nsresult ParseManifest(nsISignatureVerifier* verifier);
00137     void     ReportError(const char* aFilename, PRInt16 errorCode);
00138     nsresult LoadEntry(const char* aFilename, char** aBuf, 
00139                        PRUint32* aBufLen = nsnull);
00140     PRInt32  ReadLine(const char** src); 
00141     nsresult ParseOneFile(nsISignatureVerifier* verifier,
00142                           const char* filebuf, PRInt16 aFileType);
00143     nsresult VerifyEntry(nsISignatureVerifier* verifier,
00144                          nsJARManifestItem* aEntry, const char* aEntryData, 
00145                          PRUint32 aLen);
00146 
00147     nsresult CalculateDigest(const char* aInBuf, PRUint32 aInBufLen,
00148                              char** digest);
00149 
00150     //-- Debugging
00151     void DumpMetadata(const char* aMessage);
00152 };
00153 
00160 class nsJARItem : public nsIZipEntry
00161 {
00162 public:
00163     NS_DECL_ISUPPORTS
00164     NS_DECL_NSIZIPENTRY
00165     
00166     void Init(nsZipItem* aZipItem);
00167 
00168     nsJARItem();
00169     virtual ~nsJARItem();
00170 
00171     private:
00172     nsZipItem* mZipItem;
00173 };
00174 
00181 class nsJAREnumerator : public nsISimpleEnumerator
00182 {
00183 public:
00184     NS_DECL_ISUPPORTS
00185     NS_DECL_NSISIMPLEENUMERATOR
00186 
00187     nsJAREnumerator(nsZipFind *aFind);
00188     virtual ~nsJAREnumerator();
00189 
00190 protected:
00191     nsZipArchive *mArchive; // pointer extracted from mFind for efficiency
00192     nsZipFind    *mFind;
00193     nsZipItem    *mCurr;    // raw pointer to an nsZipItem owned by mArchive -- DON'T delete
00194     PRBool        mIsCurrStale;
00195 };
00196 
00198 
00199 #if defined(DEBUG_warren) || defined(DEBUG_jband)
00200 #define ZIP_CACHE_HIT_RATE
00201 #endif
00202 
00203 class nsZipReaderCache : public nsIZipReaderCache, public nsIObserver,
00204                          public nsSupportsWeakReference
00205 {
00206 public:
00207   NS_DECL_ISUPPORTS
00208   NS_DECL_NSIZIPREADERCACHE
00209   NS_DECL_NSIOBSERVER
00210 
00211   nsZipReaderCache();
00212   virtual ~nsZipReaderCache();
00213 
00214   nsresult ReleaseZip(nsJAR* reader);
00215 
00216 protected:
00217   PRLock*               mLock;
00218   PRInt32               mCacheSize;
00219   nsSupportsHashtable   mZips;
00220 
00221 #ifdef ZIP_CACHE_HIT_RATE
00222   PRUint32              mZipCacheLookups;
00223   PRUint32              mZipCacheHits;
00224   PRUint32              mZipCacheFlushes;
00225   PRUint32              mZipSyncMisses;
00226 #endif
00227 
00228 };
00229 
00231 
00232 #endif /* nsJAR_h__ */
00233