1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/modules/libjar/nsJAR.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,215 @@ 1.4 +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#ifndef nsJAR_h_ 1.10 +#define nsJAR_h_ 1.11 + 1.12 +#include "nscore.h" 1.13 +#include "prio.h" 1.14 +#include "plstr.h" 1.15 +#include "prlog.h" 1.16 +#include "prinrval.h" 1.17 + 1.18 +#include "mozilla/Mutex.h" 1.19 +#include "nsIComponentManager.h" 1.20 +#include "nsCOMPtr.h" 1.21 +#include "nsClassHashtable.h" 1.22 +#include "nsString.h" 1.23 +#include "nsIFile.h" 1.24 +#include "nsStringEnumerator.h" 1.25 +#include "nsHashKeys.h" 1.26 +#include "nsRefPtrHashtable.h" 1.27 +#include "nsTHashtable.h" 1.28 +#include "nsIZipReader.h" 1.29 +#include "nsZipArchive.h" 1.30 +#include "nsICertificatePrincipal.h" 1.31 +#include "nsISignatureVerifier.h" 1.32 +#include "nsIObserverService.h" 1.33 +#include "nsWeakReference.h" 1.34 +#include "nsIObserver.h" 1.35 +#include "mozilla/Attributes.h" 1.36 + 1.37 +class nsIInputStream; 1.38 +class nsJARManifestItem; 1.39 +class nsZipReaderCache; 1.40 + 1.41 +/* For mManifestStatus */ 1.42 +typedef enum 1.43 +{ 1.44 + JAR_MANIFEST_NOT_PARSED = 0, 1.45 + JAR_VALID_MANIFEST = 1, 1.46 + JAR_INVALID_SIG = 2, 1.47 + JAR_INVALID_UNKNOWN_CA = 3, 1.48 + JAR_INVALID_MANIFEST = 4, 1.49 + JAR_INVALID_ENTRY = 5, 1.50 + JAR_NO_MANIFEST = 6, 1.51 + JAR_NOT_SIGNED = 7 1.52 +} JARManifestStatusType; 1.53 + 1.54 +/*------------------------------------------------------------------------- 1.55 + * Class nsJAR declaration. 1.56 + * nsJAR serves as an XPCOM wrapper for nsZipArchive with the addition of 1.57 + * JAR manifest file parsing. 1.58 + *------------------------------------------------------------------------*/ 1.59 +class nsJAR : public nsIZipReader 1.60 +{ 1.61 + // Allows nsJARInputStream to call the verification functions 1.62 + friend class nsJARInputStream; 1.63 + // Allows nsZipReaderCache to access mOuterZipEntry 1.64 + friend class nsZipReaderCache; 1.65 + 1.66 + public: 1.67 + 1.68 + nsJAR(); 1.69 + virtual ~nsJAR(); 1.70 + 1.71 + NS_DEFINE_STATIC_CID_ACCESSOR( NS_ZIPREADER_CID ) 1.72 + 1.73 + NS_DECL_THREADSAFE_ISUPPORTS 1.74 + 1.75 + NS_DECL_NSIZIPREADER 1.76 + 1.77 + nsresult GetJarPath(nsACString& aResult); 1.78 + 1.79 + PRIntervalTime GetReleaseTime() { 1.80 + return mReleaseTime; 1.81 + } 1.82 + 1.83 + bool IsReleased() { 1.84 + return mReleaseTime != PR_INTERVAL_NO_TIMEOUT; 1.85 + } 1.86 + 1.87 + void SetReleaseTime() { 1.88 + mReleaseTime = PR_IntervalNow(); 1.89 + } 1.90 + 1.91 + void ClearReleaseTime() { 1.92 + mReleaseTime = PR_INTERVAL_NO_TIMEOUT; 1.93 + } 1.94 + 1.95 + void SetZipReaderCache(nsZipReaderCache* cache) { 1.96 + mCache = cache; 1.97 + } 1.98 + 1.99 + protected: 1.100 + typedef nsClassHashtable<nsCStringHashKey, nsJARManifestItem> ManifestDataHashtable; 1.101 + 1.102 + //-- Private data members 1.103 + nsCOMPtr<nsIFile> mZipFile; // The zip/jar file on disk 1.104 + nsCString mOuterZipEntry; // The entry in the zip this zip is reading from 1.105 + nsRefPtr<nsZipArchive> mZip; // The underlying zip archive 1.106 + ManifestDataHashtable mManifestData; // Stores metadata for each entry 1.107 + bool mParsedManifest; // True if manifest has been parsed 1.108 + nsCOMPtr<nsICertificatePrincipal> mPrincipal; // The entity which signed this file 1.109 + int16_t mGlobalStatus; // Global signature verification status 1.110 + PRIntervalTime mReleaseTime; // used by nsZipReaderCache for flushing entries 1.111 + nsZipReaderCache* mCache; // if cached, this points to the cache it's contained in 1.112 + mozilla::Mutex mLock; 1.113 + int64_t mMtime; 1.114 + int32_t mTotalItemsInManifest; 1.115 + bool mOpened; 1.116 + 1.117 + nsresult ParseManifest(); 1.118 + void ReportError(const nsACString &aFilename, int16_t errorCode); 1.119 + nsresult LoadEntry(const nsACString &aFilename, char** aBuf, 1.120 + uint32_t* aBufLen = nullptr); 1.121 + int32_t ReadLine(const char** src); 1.122 + nsresult ParseOneFile(const char* filebuf, int16_t aFileType); 1.123 + nsresult VerifyEntry(nsJARManifestItem* aEntry, const char* aEntryData, 1.124 + uint32_t aLen); 1.125 + 1.126 + nsresult CalculateDigest(const char* aInBuf, uint32_t aInBufLen, 1.127 + nsCString& digest); 1.128 +}; 1.129 + 1.130 +/** 1.131 + * nsJARItem 1.132 + * 1.133 + * An individual JAR entry. A set of nsJARItems matching a 1.134 + * supplied pattern are returned in a nsJAREnumerator. 1.135 + */ 1.136 +class nsJARItem : public nsIZipEntry 1.137 +{ 1.138 +public: 1.139 + NS_DECL_THREADSAFE_ISUPPORTS 1.140 + NS_DECL_NSIZIPENTRY 1.141 + 1.142 + nsJARItem(nsZipItem* aZipItem); 1.143 + virtual ~nsJARItem() {} 1.144 + 1.145 +private: 1.146 + uint32_t mSize; /* size in original file */ 1.147 + uint32_t mRealsize; /* inflated size */ 1.148 + uint32_t mCrc32; 1.149 + PRTime mLastModTime; 1.150 + uint16_t mCompression; 1.151 + uint32_t mPermissions; 1.152 + bool mIsDirectory; 1.153 + bool mIsSynthetic; 1.154 +}; 1.155 + 1.156 +/** 1.157 + * nsJAREnumerator 1.158 + * 1.159 + * Enumerates a list of files in a zip archive 1.160 + * (based on a pattern match in its member nsZipFind). 1.161 + */ 1.162 +class nsJAREnumerator MOZ_FINAL : public nsIUTF8StringEnumerator 1.163 +{ 1.164 +public: 1.165 + NS_DECL_THREADSAFE_ISUPPORTS 1.166 + NS_DECL_NSIUTF8STRINGENUMERATOR 1.167 + 1.168 + nsJAREnumerator(nsZipFind *aFind) : mFind(aFind), mName(nullptr) { 1.169 + NS_ASSERTION(mFind, "nsJAREnumerator: Missing zipFind."); 1.170 + } 1.171 + 1.172 +private: 1.173 + nsZipFind *mFind; 1.174 + const char* mName; // pointer to an name owned by mArchive -- DON'T delete 1.175 + uint16_t mNameLen; 1.176 + 1.177 + ~nsJAREnumerator() { delete mFind; } 1.178 +}; 1.179 + 1.180 +//////////////////////////////////////////////////////////////////////////////// 1.181 + 1.182 +#if defined(DEBUG_warren) || defined(DEBUG_jband) 1.183 +#define ZIP_CACHE_HIT_RATE 1.184 +#endif 1.185 + 1.186 +class nsZipReaderCache : public nsIZipReaderCache, public nsIObserver, 1.187 + public nsSupportsWeakReference 1.188 +{ 1.189 +public: 1.190 + NS_DECL_THREADSAFE_ISUPPORTS 1.191 + NS_DECL_NSIZIPREADERCACHE 1.192 + NS_DECL_NSIOBSERVER 1.193 + 1.194 + nsZipReaderCache(); 1.195 + virtual ~nsZipReaderCache(); 1.196 + 1.197 + nsresult ReleaseZip(nsJAR* reader); 1.198 + 1.199 + typedef nsRefPtrHashtable<nsCStringHashKey, nsJAR> ZipsHashtable; 1.200 + 1.201 +protected: 1.202 + 1.203 + mozilla::Mutex mLock; 1.204 + uint32_t mCacheSize; 1.205 + ZipsHashtable mZips; 1.206 + 1.207 +#ifdef ZIP_CACHE_HIT_RATE 1.208 + uint32_t mZipCacheLookups; 1.209 + uint32_t mZipCacheHits; 1.210 + uint32_t mZipCacheFlushes; 1.211 + uint32_t mZipSyncMisses; 1.212 +#endif 1.213 + 1.214 +}; 1.215 + 1.216 +//////////////////////////////////////////////////////////////////////////////// 1.217 + 1.218 +#endif /* nsJAR_h_ */