2000-02-14 04:57:01 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
1999-06-02 01:08:32 +04:00
|
|
|
#include <string.h>
|
2000-01-29 03:03:57 +03:00
|
|
|
#include "nsJARInputStream.h"
|
|
|
|
#include "nsJAR.h"
|
2012-06-06 06:08:30 +04:00
|
|
|
#include "nsIFile.h"
|
2000-04-26 07:50:07 +04:00
|
|
|
#include "nsIConsoleService.h"
|
2005-06-01 20:06:53 +04:00
|
|
|
#include "nsICryptoHash.h"
|
2006-05-02 23:33:09 +04:00
|
|
|
#include "prprf.h"
|
2010-09-10 01:01:00 +04:00
|
|
|
#include "mozilla/Omnijar.h"
|
2000-03-21 07:21:28 +03:00
|
|
|
|
2000-02-21 23:19:16 +03:00
|
|
|
#ifdef XP_UNIX
|
|
|
|
#include <sys/stat.h>
|
2003-04-03 02:45:08 +04:00
|
|
|
#elif defined (XP_WIN) || defined(XP_OS2)
|
2000-02-21 23:19:16 +03:00
|
|
|
#include <io.h>
|
|
|
|
#endif
|
|
|
|
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 08:29:02 +04:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2000-02-14 04:57:01 +03:00
|
|
|
//----------------------------------------------
|
|
|
|
// nsJARManifestItem declaration
|
|
|
|
//----------------------------------------------
|
|
|
|
/*
|
|
|
|
* nsJARManifestItem contains meta-information pertaining
|
|
|
|
* to an individual JAR entry, taken from the
|
|
|
|
* META-INF/MANIFEST.MF and META-INF/ *.SF files.
|
|
|
|
* This is security-critical information, defined here so it is not
|
|
|
|
* accessible from anywhere else.
|
|
|
|
*/
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
JAR_INVALID = 1,
|
2000-03-21 07:21:28 +03:00
|
|
|
JAR_INTERNAL = 2,
|
|
|
|
JAR_EXTERNAL = 3
|
2000-02-14 04:57:01 +03:00
|
|
|
} JARManifestItemType;
|
|
|
|
|
|
|
|
class nsJARManifestItem
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
JARManifestItemType mType;
|
|
|
|
|
2000-03-21 07:21:28 +03:00
|
|
|
// True if the second step of verification (VerifyEntry)
|
2000-02-14 04:57:01 +03:00
|
|
|
// has taken place:
|
2011-09-29 10:19:26 +04:00
|
|
|
bool entryVerified;
|
2000-02-14 04:57:01 +03:00
|
|
|
|
2000-03-21 07:21:28 +03:00
|
|
|
// Not signed, valid, or failure code
|
2012-08-22 19:56:38 +04:00
|
|
|
int16_t status;
|
2000-02-14 04:57:01 +03:00
|
|
|
|
|
|
|
// Internal storage of digests
|
2009-04-07 13:24:58 +04:00
|
|
|
nsCString calculatedSectionDigest;
|
|
|
|
nsCString storedEntryDigest;
|
2000-02-14 04:57:01 +03:00
|
|
|
|
|
|
|
nsJARManifestItem();
|
|
|
|
virtual ~nsJARManifestItem();
|
|
|
|
};
|
|
|
|
|
|
|
|
//-------------------------------------------------
|
|
|
|
// nsJARManifestItem constructors and destructor
|
|
|
|
//-------------------------------------------------
|
|
|
|
nsJARManifestItem::nsJARManifestItem(): mType(JAR_INTERNAL),
|
2011-10-17 18:59:28 +04:00
|
|
|
entryVerified(false),
|
2009-04-07 13:24:58 +04:00
|
|
|
status(JAR_NOT_SIGNED)
|
2000-02-14 04:57:01 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsJARManifestItem::~nsJARManifestItem()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------
|
|
|
|
// nsJAR constructor/destructor
|
|
|
|
//----------------------------------------------
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool
|
2000-01-29 03:03:57 +03:00
|
|
|
DeleteManifestEntry(nsHashKey* aKey, void* aData, void* closure)
|
1999-06-02 01:08:32 +04:00
|
|
|
{
|
2000-02-14 04:57:01 +03:00
|
|
|
//-- deletes an entry in mManifestData.
|
2005-06-14 14:29:45 +04:00
|
|
|
delete (nsJARManifestItem*)aData;
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
1999-06-02 01:08:32 +04:00
|
|
|
}
|
|
|
|
|
2000-04-26 07:50:07 +04:00
|
|
|
// The following initialization makes a guess of 10 entries per jarfile.
|
2010-09-10 01:01:00 +04:00
|
|
|
nsJAR::nsJAR(): mZip(new nsZipArchive()),
|
2012-07-30 18:20:58 +04:00
|
|
|
mManifestData(nullptr, nullptr, DeleteManifestEntry, nullptr, 10),
|
2011-10-17 18:59:28 +04:00
|
|
|
mParsedManifest(false),
|
2006-05-02 23:33:09 +04:00
|
|
|
mGlobalStatus(JAR_MANIFEST_NOT_PARSED),
|
2003-06-25 05:13:36 +04:00
|
|
|
mReleaseTime(PR_INTERVAL_NO_TIMEOUT),
|
2012-07-30 18:20:58 +04:00
|
|
|
mCache(nullptr),
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 08:29:02 +04:00
|
|
|
mLock("nsJAR::mLock"),
|
|
|
|
mTotalItemsInManifest(0),
|
2011-10-17 18:59:28 +04:00
|
|
|
mOpened(false)
|
1999-11-12 09:13:13 +03:00
|
|
|
{
|
|
|
|
}
|
1999-06-02 01:08:32 +04:00
|
|
|
|
|
|
|
nsJAR::~nsJAR()
|
2000-06-07 13:05:22 +04:00
|
|
|
{
|
|
|
|
Close();
|
1999-06-02 01:08:32 +04:00
|
|
|
}
|
|
|
|
|
2010-03-07 16:56:45 +03:00
|
|
|
NS_IMPL_THREADSAFE_QUERY_INTERFACE1(nsJAR, nsIZipReader)
|
2000-08-23 07:18:53 +04:00
|
|
|
NS_IMPL_THREADSAFE_ADDREF(nsJAR)
|
|
|
|
|
|
|
|
// Custom Release method works with nsZipReaderCache...
|
|
|
|
nsrefcnt nsJAR::Release(void)
|
|
|
|
{
|
|
|
|
nsrefcnt count;
|
|
|
|
NS_PRECONDITION(0 != mRefCnt, "dup release");
|
2011-03-28 23:58:49 +04:00
|
|
|
count = NS_AtomicDecrementRefcnt(mRefCnt);
|
2000-08-23 07:18:53 +04:00
|
|
|
NS_LOG_RELEASE(this, count, "nsJAR");
|
|
|
|
if (0 == count) {
|
|
|
|
mRefCnt = 1; /* stabilize */
|
|
|
|
/* enable this to find non-threadsafe destructors: */
|
2005-01-12 22:20:01 +03:00
|
|
|
/* NS_ASSERT_OWNINGTHREAD(nsJAR); */
|
2010-07-05 13:42:18 +04:00
|
|
|
delete this;
|
2000-08-23 07:18:53 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if (1 == count && mCache) {
|
2010-07-11 16:49:52 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
nsresult rv =
|
|
|
|
#endif
|
|
|
|
mCache->ReleaseZip(this);
|
2000-08-23 07:18:53 +04:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "failed to release zip file");
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
1999-06-02 01:08:32 +04:00
|
|
|
|
2000-02-14 04:57:01 +03:00
|
|
|
//----------------------------------------------
|
2001-02-23 03:15:04 +03:00
|
|
|
// nsIZipReader implementation
|
2000-02-14 04:57:01 +03:00
|
|
|
//----------------------------------------------
|
2000-04-12 11:58:24 +04:00
|
|
|
|
1999-06-02 01:08:32 +04:00
|
|
|
NS_IMETHODIMP
|
2006-05-02 23:33:09 +04:00
|
|
|
nsJAR::Open(nsIFile* zipFile)
|
1999-06-02 01:08:32 +04:00
|
|
|
{
|
2009-01-22 18:12:00 +03:00
|
|
|
NS_ENSURE_ARG_POINTER(zipFile);
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 08:29:02 +04:00
|
|
|
if (mOpened) return NS_ERROR_FAILURE; // Already open!
|
2006-05-02 23:33:09 +04:00
|
|
|
|
1999-11-12 09:13:13 +03:00
|
|
|
mZipFile = zipFile;
|
2011-03-31 23:01:12 +04:00
|
|
|
mOuterZipEntry.Truncate();
|
2011-10-17 18:59:28 +04:00
|
|
|
mOpened = true;
|
2010-09-10 01:01:00 +04:00
|
|
|
|
|
|
|
// The omnijar is special, it is opened early on and closed late
|
|
|
|
// this avoids reopening it
|
2011-12-08 14:03:36 +04:00
|
|
|
nsRefPtr<nsZipArchive> zip = mozilla::Omnijar::GetReader(zipFile);
|
2011-02-25 14:53:36 +03:00
|
|
|
if (zip) {
|
|
|
|
mZip = zip;
|
2010-09-10 01:01:00 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
return mZip->OpenArchive(zipFile);
|
1999-06-02 01:08:32 +04:00
|
|
|
}
|
|
|
|
|
2010-09-09 07:38:34 +04:00
|
|
|
NS_IMETHODIMP
|
2011-09-29 03:14:45 +04:00
|
|
|
nsJAR::OpenInner(nsIZipReader *aZipReader, const nsACString &aZipEntry)
|
2010-09-09 07:38:34 +04:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aZipReader);
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 08:29:02 +04:00
|
|
|
if (mOpened) return NS_ERROR_FAILURE; // Already open!
|
2010-09-09 07:38:34 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool exist;
|
2011-09-29 03:14:45 +04:00
|
|
|
nsresult rv = aZipReader->HasEntry(aZipEntry, &exist);
|
2010-09-20 23:58:40 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
NS_ENSURE_TRUE(exist, NS_ERROR_FILE_NOT_FOUND);
|
|
|
|
|
|
|
|
rv = aZipReader->GetFile(getter_AddRefs(mZipFile));
|
2010-09-09 07:38:34 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mOpened = true;
|
2010-09-09 07:38:34 +04:00
|
|
|
|
|
|
|
mOuterZipEntry.Assign(aZipEntry);
|
|
|
|
|
|
|
|
nsRefPtr<nsZipHandle> handle;
|
2011-09-29 03:14:45 +04:00
|
|
|
rv = nsZipHandle::Init(static_cast<nsJAR*>(aZipReader)->mZip.get(), PromiseFlatCString(aZipEntry).get(),
|
2010-09-09 07:38:34 +04:00
|
|
|
getter_AddRefs(handle));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2010-09-10 01:01:00 +04:00
|
|
|
return mZip->OpenArchive(handle);
|
2010-09-09 07:38:34 +04:00
|
|
|
}
|
|
|
|
|
2000-04-12 11:58:24 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsJAR::GetFile(nsIFile* *result)
|
|
|
|
{
|
|
|
|
*result = mZipFile;
|
2003-09-24 05:48:09 +04:00
|
|
|
NS_IF_ADDREF(*result);
|
2000-04-12 11:58:24 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-06-02 01:08:32 +04:00
|
|
|
NS_IMETHODIMP
|
2006-05-02 23:33:09 +04:00
|
|
|
nsJAR::Close()
|
1999-11-12 09:13:13 +03:00
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
mOpened = false;
|
|
|
|
mParsedManifest = false;
|
2008-11-13 18:38:16 +03:00
|
|
|
mManifestData.Reset();
|
2006-05-02 23:33:09 +04:00
|
|
|
mGlobalStatus = JAR_MANIFEST_NOT_PARSED;
|
|
|
|
mTotalItemsInManifest = 0;
|
1999-11-12 09:13:13 +03:00
|
|
|
|
2011-12-08 14:03:36 +04:00
|
|
|
nsRefPtr<nsZipArchive> greOmni = mozilla::Omnijar::GetReader(mozilla::Omnijar::GRE);
|
|
|
|
nsRefPtr<nsZipArchive> appOmni = mozilla::Omnijar::GetReader(mozilla::Omnijar::APP);
|
|
|
|
|
|
|
|
if (mZip == greOmni || mZip == appOmni) {
|
2010-09-10 01:01:00 +04:00
|
|
|
mZip = new nsZipArchive();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
return mZip->CloseArchive();
|
1999-11-12 09:13:13 +03:00
|
|
|
}
|
|
|
|
|
2000-12-27 10:05:55 +03:00
|
|
|
NS_IMETHODIMP
|
2011-09-29 03:14:45 +04:00
|
|
|
nsJAR::Test(const nsACString &aEntryName)
|
2000-12-27 10:05:55 +03:00
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
return mZip->Test(aEntryName.IsEmpty()? nullptr : PromiseFlatCString(aEntryName).get());
|
2000-12-27 10:05:55 +03:00
|
|
|
}
|
|
|
|
|
1999-11-12 09:13:13 +03:00
|
|
|
NS_IMETHODIMP
|
2011-09-29 03:14:45 +04:00
|
|
|
nsJAR::Extract(const nsACString &aEntryName, nsIFile* outFile)
|
1999-06-02 01:08:32 +04:00
|
|
|
{
|
2000-09-16 01:56:20 +04:00
|
|
|
// nsZipArchive and zlib are not thread safe
|
|
|
|
// we need to use a lock to prevent bug #51267
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 08:29:02 +04:00
|
|
|
MutexAutoLock lock(mLock);
|
2000-09-16 01:56:20 +04:00
|
|
|
|
2011-09-29 03:14:45 +04:00
|
|
|
nsZipItem *item = mZip->GetItem(PromiseFlatCString(aEntryName).get());
|
2006-05-02 23:33:09 +04:00
|
|
|
NS_ENSURE_TRUE(item, NS_ERROR_FILE_TARGET_DOES_NOT_EXIST);
|
2004-08-17 04:12:04 +04:00
|
|
|
|
2006-03-30 02:10:37 +04:00
|
|
|
// Remove existing file or directory so we set permissions correctly.
|
|
|
|
// If it's a directory that already exists and contains files, throw
|
|
|
|
// an exception and return.
|
|
|
|
|
|
|
|
//XXX Bug 332139:
|
|
|
|
//XXX If we guarantee that rv in the case of a non-empty directory
|
|
|
|
//XXX is always FILE_DIR_NOT_EMPTY, we can remove
|
|
|
|
//XXX |rv == NS_ERROR_FAILURE| - bug 322183 needs to be completely
|
|
|
|
//XXX fixed before that can happen
|
2012-06-06 06:08:30 +04:00
|
|
|
nsresult rv = outFile->Remove(false);
|
2006-03-30 02:10:37 +04:00
|
|
|
if (rv == NS_ERROR_FILE_DIR_NOT_EMPTY ||
|
|
|
|
rv == NS_ERROR_FAILURE)
|
|
|
|
return rv;
|
2004-08-17 04:12:04 +04:00
|
|
|
|
2009-10-17 19:54:54 +04:00
|
|
|
if (item->IsDirectory())
|
2006-03-30 02:10:37 +04:00
|
|
|
{
|
2012-06-06 06:08:30 +04:00
|
|
|
rv = outFile->Create(nsIFile::DIRECTORY_TYPE, item->Mode());
|
2006-03-30 02:10:37 +04:00
|
|
|
//XXX Do this in nsZipArchive? It would be nice to keep extraction
|
|
|
|
//XXX code completely there, but that would require a way to get a
|
2012-06-06 06:08:30 +04:00
|
|
|
//XXX PRDir from outFile.
|
2006-03-30 02:10:37 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PRFileDesc* fd;
|
2012-06-06 06:08:30 +04:00
|
|
|
rv = outFile->OpenNSPRFileDesc(PR_WRONLY | PR_CREATE_FILE, item->Mode(), &fd);
|
2006-03-30 02:10:37 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2000-01-25 00:28:28 +03:00
|
|
|
|
2006-05-02 23:33:09 +04:00
|
|
|
// ExtractFile also closes the fd handle and resolves the symlink if needed
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString path;
|
2006-05-02 23:33:09 +04:00
|
|
|
rv = outFile->GetNativePath(path);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2000-02-21 23:19:16 +03:00
|
|
|
|
2010-09-10 01:01:00 +04:00
|
|
|
rv = mZip->ExtractFile(item, path.get(), fd);
|
2000-03-28 07:38:06 +04:00
|
|
|
}
|
2006-05-02 23:33:09 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2000-03-28 07:38:06 +04:00
|
|
|
|
2006-05-02 23:33:09 +04:00
|
|
|
// nsIFile needs milliseconds, while prtime is in microseconds.
|
|
|
|
// non-fatal if this fails, ignore errors
|
2009-12-16 02:01:08 +03:00
|
|
|
outFile->SetLastModifiedTime(item->LastModTime() / PR_USEC_PER_MSEC);
|
2006-05-02 23:33:09 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
1999-11-12 09:13:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 03:14:45 +04:00
|
|
|
nsJAR::GetEntry(const nsACString &aEntryName, nsIZipEntry* *result)
|
1999-11-12 09:13:13 +03:00
|
|
|
{
|
2011-09-29 03:14:45 +04:00
|
|
|
nsZipItem* zipItem = mZip->GetItem(PromiseFlatCString(aEntryName).get());
|
2006-05-02 23:33:09 +04:00
|
|
|
NS_ENSURE_TRUE(zipItem, NS_ERROR_FILE_TARGET_DOES_NOT_EXIST);
|
1999-11-12 09:13:13 +03:00
|
|
|
|
2006-05-02 23:33:09 +04:00
|
|
|
nsJARItem* jarItem = new nsJARItem(zipItem);
|
|
|
|
NS_ENSURE_TRUE(jarItem, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
|
|
|
NS_ADDREF(*result = jarItem);
|
1999-06-02 01:08:32 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2006-05-02 23:33:09 +04:00
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsJAR::HasEntry(const nsACString &aEntryName, bool *result)
|
2006-05-02 23:33:09 +04:00
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
*result = mZip->GetItem(PromiseFlatCString(aEntryName).get()) != nullptr;
|
2006-05-02 23:33:09 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 03:14:45 +04:00
|
|
|
nsJAR::FindEntries(const nsACString &aPattern, nsIUTF8StringEnumerator **result)
|
1999-06-23 10:16:28 +04:00
|
|
|
{
|
2006-05-02 23:33:09 +04:00
|
|
|
NS_ENSURE_ARG_POINTER(result);
|
2006-03-30 02:10:37 +04:00
|
|
|
|
|
|
|
nsZipFind *find;
|
2012-07-30 18:20:58 +04:00
|
|
|
nsresult rv = mZip->FindInit(aPattern.IsEmpty()? nullptr : PromiseFlatCString(aPattern).get(), &find);
|
2006-05-02 23:33:09 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
1999-06-23 10:16:28 +04:00
|
|
|
|
2006-05-02 23:33:09 +04:00
|
|
|
nsIUTF8StringEnumerator *zipEnum = new nsJAREnumerator(find);
|
|
|
|
if (!zipEnum) {
|
|
|
|
delete find;
|
1999-11-12 09:13:13 +03:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2006-05-02 23:33:09 +04:00
|
|
|
}
|
1999-06-23 10:16:28 +04:00
|
|
|
|
2006-05-02 23:33:09 +04:00
|
|
|
NS_ADDREF(*result = zipEnum);
|
1999-11-12 09:13:13 +03:00
|
|
|
return NS_OK;
|
1999-06-23 10:16:28 +04:00
|
|
|
}
|
|
|
|
|
1999-10-26 23:43:26 +04:00
|
|
|
NS_IMETHODIMP
|
2011-09-29 03:14:45 +04:00
|
|
|
nsJAR::GetInputStream(const nsACString &aFilename, nsIInputStream** result)
|
1999-10-26 23:43:26 +04:00
|
|
|
{
|
2006-10-10 17:24:57 +04:00
|
|
|
return GetInputStreamWithSpec(EmptyCString(), aFilename, result);
|
|
|
|
}
|
2006-09-24 20:23:31 +04:00
|
|
|
|
2006-10-10 17:24:57 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsJAR::GetInputStreamWithSpec(const nsACString& aJarDirSpec,
|
2011-09-29 03:14:45 +04:00
|
|
|
const nsACString &aEntryName, nsIInputStream** result)
|
2006-10-10 17:24:57 +04:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(result);
|
2006-09-24 20:23:31 +04:00
|
|
|
|
2006-10-10 17:24:57 +04:00
|
|
|
// Watch out for the jar:foo.zip!/ (aDir is empty) top-level special case!
|
2012-07-30 18:20:58 +04:00
|
|
|
nsZipItem *item = nullptr;
|
2011-09-29 03:14:45 +04:00
|
|
|
const char *entry = PromiseFlatCString(aEntryName).get();
|
|
|
|
if (*entry) {
|
2006-10-10 17:24:57 +04:00
|
|
|
// First check if item exists in jar
|
2011-09-29 03:14:45 +04:00
|
|
|
item = mZip->GetItem(entry);
|
2006-10-10 17:24:57 +04:00
|
|
|
if (!item) return NS_ERROR_FILE_TARGET_DOES_NOT_EXIST;
|
|
|
|
}
|
2003-03-14 21:59:51 +03:00
|
|
|
nsJARInputStream* jis = new nsJARInputStream();
|
2006-10-10 17:24:57 +04:00
|
|
|
// addref now so we can call InitFile/InitDirectory()
|
|
|
|
NS_ENSURE_TRUE(jis, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
NS_ADDREF(*result = jis);
|
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
2009-10-17 19:54:54 +04:00
|
|
|
if (!item || item->IsDirectory()) {
|
2011-09-29 03:14:45 +04:00
|
|
|
rv = jis->InitDirectory(this, aJarDirSpec, entry);
|
2006-10-10 17:24:57 +04:00
|
|
|
} else {
|
2009-10-04 21:20:45 +04:00
|
|
|
rv = jis->InitFile(this, item);
|
2006-10-10 17:24:57 +04:00
|
|
|
}
|
2003-03-14 21:59:51 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_RELEASE(*result);
|
|
|
|
}
|
2006-10-10 17:24:57 +04:00
|
|
|
return rv;
|
2000-01-29 03:03:57 +03:00
|
|
|
}
|
|
|
|
|
2000-04-26 07:50:07 +04:00
|
|
|
NS_IMETHODIMP
|
2012-10-22 10:29:56 +04:00
|
|
|
nsJAR::GetCertificatePrincipal(const nsACString &aFilename, nsICertificatePrincipal** aPrincipal)
|
2000-01-29 03:03:57 +03:00
|
|
|
{
|
2000-02-14 04:57:01 +03:00
|
|
|
//-- Parameter check
|
2000-03-21 07:21:28 +03:00
|
|
|
if (!aPrincipal)
|
2000-01-29 03:03:57 +03:00
|
|
|
return NS_ERROR_NULL_POINTER;
|
2012-07-30 18:20:58 +04:00
|
|
|
*aPrincipal = nullptr;
|
2000-01-29 03:03:57 +03:00
|
|
|
|
2010-11-24 05:14:52 +03:00
|
|
|
// Don't check signatures in the omnijar - this is only
|
|
|
|
// interesting for extensions/XPIs.
|
2011-12-08 14:03:36 +04:00
|
|
|
nsRefPtr<nsZipArchive> greOmni = mozilla::Omnijar::GetReader(mozilla::Omnijar::GRE);
|
|
|
|
nsRefPtr<nsZipArchive> appOmni = mozilla::Omnijar::GetReader(mozilla::Omnijar::APP);
|
|
|
|
|
|
|
|
if (mZip == greOmni || mZip == appOmni)
|
2010-11-24 05:14:52 +03:00
|
|
|
return NS_OK;
|
|
|
|
|
2000-07-12 07:10:33 +04:00
|
|
|
//-- Parse the manifest
|
2008-11-18 22:11:35 +03:00
|
|
|
nsresult rv = ParseManifest();
|
2000-07-12 07:10:33 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2006-05-02 23:33:09 +04:00
|
|
|
if (mGlobalStatus == JAR_NO_MANIFEST)
|
2000-07-12 07:10:33 +04:00
|
|
|
return NS_OK;
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int16_t requestedStatus;
|
2011-10-28 17:43:01 +04:00
|
|
|
if (!aFilename.IsEmpty())
|
2000-01-29 03:03:57 +03:00
|
|
|
{
|
2000-04-26 07:50:07 +04:00
|
|
|
//-- Find the item
|
2011-10-28 17:43:01 +04:00
|
|
|
nsCStringKey key(aFilename);
|
2007-07-08 11:08:04 +04:00
|
|
|
nsJARManifestItem* manItem = static_cast<nsJARManifestItem*>(mManifestData.Get(&key));
|
2000-04-26 07:50:07 +04:00
|
|
|
if (!manItem)
|
|
|
|
return NS_OK;
|
2000-07-12 07:10:33 +04:00
|
|
|
//-- Verify the item against the manifest
|
|
|
|
if (!manItem->entryVerified)
|
2000-04-26 07:50:07 +04:00
|
|
|
{
|
2000-07-12 07:10:33 +04:00
|
|
|
nsXPIDLCString entryData;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t entryDataLen;
|
2000-07-12 07:10:33 +04:00
|
|
|
rv = LoadEntry(aFilename, getter_Copies(entryData), &entryDataLen);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2006-05-02 23:33:09 +04:00
|
|
|
rv = VerifyEntry(manItem, entryData, entryDataLen);
|
2000-07-12 07:10:33 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2000-04-26 07:50:07 +04:00
|
|
|
}
|
|
|
|
requestedStatus = manItem->status;
|
2000-01-29 03:03:57 +03:00
|
|
|
}
|
2000-04-26 07:50:07 +04:00
|
|
|
else // User wants identity of signer w/o verifying any entries
|
|
|
|
requestedStatus = mGlobalStatus;
|
2000-01-29 03:03:57 +03:00
|
|
|
|
2006-05-02 23:33:09 +04:00
|
|
|
if (requestedStatus != JAR_VALID_MANIFEST)
|
2011-10-28 17:43:01 +04:00
|
|
|
ReportError(aFilename, requestedStatus);
|
2000-04-26 07:50:07 +04:00
|
|
|
else // Valid signature
|
2000-01-29 03:03:57 +03:00
|
|
|
{
|
2000-04-26 07:50:07 +04:00
|
|
|
*aPrincipal = mPrincipal;
|
2000-03-21 07:21:28 +03:00
|
|
|
NS_IF_ADDREF(*aPrincipal);
|
2000-02-14 04:57:01 +03:00
|
|
|
}
|
2000-03-21 07:21:28 +03:00
|
|
|
return NS_OK;
|
2000-05-10 05:49:33 +04:00
|
|
|
}
|
|
|
|
|
2002-12-14 01:24:12 +03:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsJAR::GetManifestEntriesCount(uint32_t* count)
|
2002-12-14 01:24:12 +03:00
|
|
|
{
|
|
|
|
*count = mTotalItemsInManifest;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2006-05-02 23:33:09 +04:00
|
|
|
nsresult
|
|
|
|
nsJAR::GetJarPath(nsACString& aResult)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(mZipFile);
|
|
|
|
|
|
|
|
return mZipFile->GetNativePath(aResult);
|
|
|
|
}
|
|
|
|
|
2000-01-29 03:03:57 +03:00
|
|
|
//----------------------------------------------
|
|
|
|
// nsJAR private implementation
|
|
|
|
//----------------------------------------------
|
2000-02-14 04:57:01 +03:00
|
|
|
nsresult
|
2012-08-22 19:56:38 +04:00
|
|
|
nsJAR::LoadEntry(const nsACString &aFilename, char** aBuf, uint32_t* aBufLen)
|
2000-01-29 03:03:57 +03:00
|
|
|
{
|
2000-07-12 07:10:33 +04:00
|
|
|
//-- Get a stream for reading the file
|
2000-01-29 03:03:57 +03:00
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIInputStream> manifestStream;
|
2000-07-12 07:10:33 +04:00
|
|
|
rv = GetInputStream(aFilename, getter_AddRefs(manifestStream));
|
2000-01-29 03:03:57 +03:00
|
|
|
if (NS_FAILED(rv)) return NS_ERROR_FILE_TARGET_DOES_NOT_EXIST;
|
|
|
|
|
|
|
|
//-- Read the manifest file into memory
|
|
|
|
char* buf;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint64_t len64;
|
2012-08-11 06:44:11 +04:00
|
|
|
rv = manifestStream->Available(&len64);
|
2000-01-29 03:03:57 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2012-09-28 10:57:33 +04:00
|
|
|
NS_ENSURE_TRUE(len64 < UINT32_MAX, NS_ERROR_FILE_CORRUPTED); // bug 164695
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t len = (uint32_t)len64;
|
2010-05-19 04:13:05 +04:00
|
|
|
buf = (char*)malloc(len+1);
|
2000-01-29 03:03:57 +03:00
|
|
|
if (!buf) return NS_ERROR_OUT_OF_MEMORY;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t bytesRead;
|
2000-01-29 03:03:57 +03:00
|
|
|
rv = manifestStream->Read(buf, len, &bytesRead);
|
|
|
|
if (bytesRead != len)
|
|
|
|
rv = NS_ERROR_FILE_CORRUPTED;
|
2002-10-31 09:23:52 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
2010-05-19 04:13:05 +04:00
|
|
|
free(buf);
|
2002-10-31 09:23:52 +03:00
|
|
|
return rv;
|
|
|
|
}
|
2000-02-14 04:57:01 +03:00
|
|
|
buf[len] = '\0'; //Null-terminate the buffer
|
2000-01-29 03:03:57 +03:00
|
|
|
*aBuf = buf;
|
|
|
|
if (aBufLen)
|
|
|
|
*aBufLen = len;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t
|
2000-02-14 04:57:01 +03:00
|
|
|
nsJAR::ReadLine(const char** src)
|
2000-01-29 03:03:57 +03:00
|
|
|
{
|
|
|
|
//--Moves pointer to beginning of next line and returns line length
|
|
|
|
// not including CR/LF.
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t length;
|
2000-01-29 03:03:57 +03:00
|
|
|
char* eol = PL_strpbrk(*src, "\r\n");
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
if (eol == nullptr) // Probably reached end of file before newline
|
2000-01-29 03:03:57 +03:00
|
|
|
{
|
|
|
|
length = PL_strlen(*src);
|
|
|
|
if (length == 0) // immediate end-of-file
|
2012-07-30 18:20:58 +04:00
|
|
|
*src = nullptr;
|
2000-01-29 03:03:57 +03:00
|
|
|
else // some data left on this line
|
|
|
|
*src += length;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
length = eol - *src;
|
|
|
|
if (eol[0] == '\r' && eol[1] == '\n') // CR LF, so skip 2
|
|
|
|
*src = eol+2;
|
|
|
|
else // Either CR or LF, so skip 1
|
|
|
|
*src = eol+1;
|
|
|
|
}
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2000-04-26 07:50:07 +04:00
|
|
|
//-- The following #defines are used by ParseManifest()
|
|
|
|
// and ParseOneFile(). The header strings are defined in the JAR specification.
|
|
|
|
#define JAR_MF 1
|
|
|
|
#define JAR_SF 2
|
|
|
|
#define JAR_MF_SEARCH_STRING "(M|/M)ETA-INF/(M|m)(ANIFEST|anifest).(MF|mf)$"
|
|
|
|
#define JAR_SF_SEARCH_STRING "(M|/M)ETA-INF/*.(SF|sf)$"
|
|
|
|
#define JAR_MF_HEADER (const char*)"Manifest-Version: 1.0"
|
|
|
|
#define JAR_SF_HEADER (const char*)"Signature-Version: 1.0"
|
|
|
|
|
|
|
|
nsresult
|
2008-11-18 22:11:35 +03:00
|
|
|
nsJAR::ParseManifest()
|
2000-04-26 07:50:07 +04:00
|
|
|
{
|
|
|
|
//-- Verification Step 1
|
|
|
|
if (mParsedManifest)
|
|
|
|
return NS_OK;
|
|
|
|
//-- (1)Manifest (MF) file
|
2006-05-02 23:33:09 +04:00
|
|
|
nsCOMPtr<nsIUTF8StringEnumerator> files;
|
2011-09-29 03:14:45 +04:00
|
|
|
nsresult rv = FindEntries(nsDependentCString(JAR_MF_SEARCH_STRING), getter_AddRefs(files));
|
2000-04-26 07:50:07 +04:00
|
|
|
if (!files) rv = NS_ERROR_FAILURE;
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
//-- Load the file into memory
|
2011-09-29 10:19:26 +04:00
|
|
|
bool more;
|
2006-05-02 23:33:09 +04:00
|
|
|
rv = files->HasMore(&more);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (!more)
|
2000-07-12 07:10:33 +04:00
|
|
|
{
|
2006-05-02 23:33:09 +04:00
|
|
|
mGlobalStatus = JAR_NO_MANIFEST;
|
2011-10-17 18:59:28 +04:00
|
|
|
mParsedManifest = true;
|
2000-07-12 07:10:33 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2006-05-02 23:33:09 +04:00
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString manifestFilename;
|
2006-05-02 23:33:09 +04:00
|
|
|
rv = files->GetNext(manifestFilename);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Check if there is more than one manifest, if so then error!
|
|
|
|
rv = files->HasMore(&more);
|
2000-07-12 07:10:33 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
if (more)
|
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
mParsedManifest = true;
|
2000-07-12 07:10:33 +04:00
|
|
|
return NS_ERROR_FILE_CORRUPTED; // More than one MF file
|
|
|
|
}
|
2006-05-02 23:33:09 +04:00
|
|
|
|
2000-04-26 07:50:07 +04:00
|
|
|
nsXPIDLCString manifestBuffer;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t manifestLen;
|
2011-09-29 03:14:45 +04:00
|
|
|
rv = LoadEntry(manifestFilename, getter_Copies(manifestBuffer), &manifestLen);
|
2000-04-26 07:50:07 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
//-- Parse it
|
2006-05-02 23:33:09 +04:00
|
|
|
rv = ParseOneFile(manifestBuffer, JAR_MF);
|
2000-04-26 07:50:07 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
//-- (2)Signature (SF) file
|
|
|
|
// If there are multiple signatures, we select one.
|
2011-09-29 03:14:45 +04:00
|
|
|
rv = FindEntries(nsDependentCString(JAR_SF_SEARCH_STRING), getter_AddRefs(files));
|
2000-04-26 07:50:07 +04:00
|
|
|
if (!files) rv = NS_ERROR_FAILURE;
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
//-- Get an SF file
|
2006-05-02 23:33:09 +04:00
|
|
|
rv = files->HasMore(&more);
|
2000-07-12 07:10:33 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2006-05-02 23:33:09 +04:00
|
|
|
if (!more)
|
2000-07-12 07:10:33 +04:00
|
|
|
{
|
2006-05-02 23:33:09 +04:00
|
|
|
mGlobalStatus = JAR_NO_MANIFEST;
|
2011-10-17 18:59:28 +04:00
|
|
|
mParsedManifest = true;
|
2000-07-12 07:10:33 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2006-05-02 23:33:09 +04:00
|
|
|
rv = files->GetNext(manifestFilename);
|
2000-04-26 07:50:07 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2011-09-29 03:14:45 +04:00
|
|
|
rv = LoadEntry(manifestFilename, getter_Copies(manifestBuffer), &manifestLen);
|
2000-04-26 07:50:07 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
//-- Get its corresponding signature file
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString sigFilename(manifestFilename);
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t extension = sigFilename.RFindChar('.') + 1;
|
2000-04-26 07:50:07 +04:00
|
|
|
NS_ASSERTION(extension != 0, "Manifest Parser: Missing file extension.");
|
|
|
|
(void)sigFilename.Cut(extension, 2);
|
|
|
|
nsXPIDLCString sigBuffer;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t sigLen;
|
2000-05-12 11:53:02 +04:00
|
|
|
{
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString tempFilename(sigFilename); tempFilename.Append("rsa", 3);
|
2011-09-29 03:14:45 +04:00
|
|
|
rv = LoadEntry(tempFilename, getter_Copies(sigBuffer), &sigLen);
|
2000-05-12 11:53:02 +04:00
|
|
|
}
|
2000-04-26 07:50:07 +04:00
|
|
|
if (NS_FAILED(rv))
|
Bit checkin for bug 68045, r/sr=waterson&shaver, second attempt. It all works
for me on optimized and debug gcc2.96, rh7.1.
- Better failure codes from nsXULPrototypeScript::Deserialize.
- Call nsXULDocument::AbortFastLoads after nsXULPrototypeScript::Serialize
failure, instead of just nulling the FastLoad service's output stream.
- Expose nsXULDocument::AbortFastLoads via nsIXULPrototypeCache, for use from
nsChromeProtocolHandler.cpp. AbortFastLoads flushes the XUL cache now, for
good measure.
- The needless "Current" adjective in nsIFastLoadService attribute and method
names is no more.
- Add a do_GetFastLoadService() helper, to use CID instead of contractid, and
to let the compiler consolidate the static inline CID.
- Add "nglayout.debug.checksum_xul_fastload_file" pref so people can do without
the checksum verification step when reading a FastLoad file.
- Verify the FastLoad file checksum, by default. Also, cache it in the FastLoad
service so we don't recompute it when re-opening the FastLoad file as mailnews
and other top-levels start up. Fill the checksum cache in EndFastLoad, when
the last pseudo-concurrent top-level finishes loading.
My hope to compute the checksum while writing the FastLoad file ran afoul of
misordered writes. The old code to checksum the in-memory nsFastLoadHeader
also was broken on little endian platforms. Now all checksumming is done via
a separate read pass over the complete file, save for the header's checksum
field, which is summed as if it contained zero.
- Track and check FastLoad file dependencies. This required groveling with a
bunch of Necko interfaces in nsChromeProtocolHandler::NewChannel -- read it
and weep. Dependency checking, as well as checksum access and computation,
use better-factored nsIFastLoad{File,Read,Write}Control interfaces.
- nsBufferedStream::Seek wasn't flushing the buffer when seeking backward
within the buffer, but it must, because mCursor bounds the amount to write
if the buffer contains the end of file.
- Add an unbufferedStream readonly attribute to nsIStreamBufferAccess, so we
don't have to screw around with the bufferying layer when checksumming. Also
implement nsIStreamBufferAccess in nsBufferedOutputStream.
- nsISeekableOutputStream was bogus, based on a bad state I had put the
nsBufferedOutputStream code in on its way from being completely broken when
you seek backwards outside of the buffer. Removing this interface required
using nsIFastLoadFileIO in nsFastLoadFileWriter, and it also required careful
ordering of Close calls (the Reader must close after the Writer or Updater,
so that the Reader's underlying, unbuffered input stream can be read by
nsFastLoadFileWriter::Close to compute the checksum.
- Miscellaneous tab/indentation, comment typo, bracing, if( => if ( style,
nsnull vs. 0, useless variable elimination, tortured control flow,
AutoString instead of String, and gratuitous ; after nsISupportsUtils.h
macro call cleanups.
2001-08-22 00:51:34 +04:00
|
|
|
{
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString tempFilename(sigFilename); tempFilename.Append("RSA", 3);
|
2011-09-29 03:14:45 +04:00
|
|
|
rv = LoadEntry(tempFilename, getter_Copies(sigBuffer), &sigLen);
|
Bit checkin for bug 68045, r/sr=waterson&shaver, second attempt. It all works
for me on optimized and debug gcc2.96, rh7.1.
- Better failure codes from nsXULPrototypeScript::Deserialize.
- Call nsXULDocument::AbortFastLoads after nsXULPrototypeScript::Serialize
failure, instead of just nulling the FastLoad service's output stream.
- Expose nsXULDocument::AbortFastLoads via nsIXULPrototypeCache, for use from
nsChromeProtocolHandler.cpp. AbortFastLoads flushes the XUL cache now, for
good measure.
- The needless "Current" adjective in nsIFastLoadService attribute and method
names is no more.
- Add a do_GetFastLoadService() helper, to use CID instead of contractid, and
to let the compiler consolidate the static inline CID.
- Add "nglayout.debug.checksum_xul_fastload_file" pref so people can do without
the checksum verification step when reading a FastLoad file.
- Verify the FastLoad file checksum, by default. Also, cache it in the FastLoad
service so we don't recompute it when re-opening the FastLoad file as mailnews
and other top-levels start up. Fill the checksum cache in EndFastLoad, when
the last pseudo-concurrent top-level finishes loading.
My hope to compute the checksum while writing the FastLoad file ran afoul of
misordered writes. The old code to checksum the in-memory nsFastLoadHeader
also was broken on little endian platforms. Now all checksumming is done via
a separate read pass over the complete file, save for the header's checksum
field, which is summed as if it contained zero.
- Track and check FastLoad file dependencies. This required groveling with a
bunch of Necko interfaces in nsChromeProtocolHandler::NewChannel -- read it
and weep. Dependency checking, as well as checksum access and computation,
use better-factored nsIFastLoad{File,Read,Write}Control interfaces.
- nsBufferedStream::Seek wasn't flushing the buffer when seeking backward
within the buffer, but it must, because mCursor bounds the amount to write
if the buffer contains the end of file.
- Add an unbufferedStream readonly attribute to nsIStreamBufferAccess, so we
don't have to screw around with the bufferying layer when checksumming. Also
implement nsIStreamBufferAccess in nsBufferedOutputStream.
- nsISeekableOutputStream was bogus, based on a bad state I had put the
nsBufferedOutputStream code in on its way from being completely broken when
you seek backwards outside of the buffer. Removing this interface required
using nsIFastLoadFileIO in nsFastLoadFileWriter, and it also required careful
ordering of Close calls (the Reader must close after the Writer or Updater,
so that the Reader's underlying, unbuffered input stream can be read by
nsFastLoadFileWriter::Close to compute the checksum.
- Miscellaneous tab/indentation, comment typo, bracing, if( => if ( style,
nsnull vs. 0, useless variable elimination, tortured control flow,
AutoString instead of String, and gratuitous ; after nsISupportsUtils.h
macro call cleanups.
2001-08-22 00:51:34 +04:00
|
|
|
}
|
2000-07-12 07:10:33 +04:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
{
|
2006-05-02 23:33:09 +04:00
|
|
|
mGlobalStatus = JAR_NO_MANIFEST;
|
2011-10-17 18:59:28 +04:00
|
|
|
mParsedManifest = true;
|
2000-07-12 07:10:33 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2002-12-14 01:24:12 +03:00
|
|
|
|
2008-11-18 22:11:35 +03:00
|
|
|
//-- Get the signature verifier service
|
|
|
|
nsCOMPtr<nsISignatureVerifier> verifier =
|
|
|
|
do_GetService(SIGNATURE_VERIFIER_CONTRACTID, &rv);
|
|
|
|
if (NS_FAILED(rv)) // No signature verifier available
|
|
|
|
{
|
|
|
|
mGlobalStatus = JAR_NO_MANIFEST;
|
2011-10-17 18:59:28 +04:00
|
|
|
mParsedManifest = true;
|
2008-11-18 22:11:35 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2000-04-26 07:50:07 +04:00
|
|
|
//-- Verify that the signature file is a valid signature of the SF file
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t verifyError;
|
2000-05-10 05:49:33 +04:00
|
|
|
rv = verifier->VerifySignature(sigBuffer, sigLen, manifestBuffer, manifestLen,
|
|
|
|
&verifyError, getter_AddRefs(mPrincipal));
|
2000-04-26 07:50:07 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2002-12-14 01:24:12 +03:00
|
|
|
if (mPrincipal && verifyError == 0)
|
2006-05-02 23:33:09 +04:00
|
|
|
mGlobalStatus = JAR_VALID_MANIFEST;
|
2000-05-10 05:49:33 +04:00
|
|
|
else if (verifyError == nsISignatureVerifier::VERIFY_ERROR_UNKNOWN_CA)
|
2006-05-02 23:33:09 +04:00
|
|
|
mGlobalStatus = JAR_INVALID_UNKNOWN_CA;
|
2000-05-10 05:49:33 +04:00
|
|
|
else
|
2006-05-02 23:33:09 +04:00
|
|
|
mGlobalStatus = JAR_INVALID_SIG;
|
2000-05-10 05:49:33 +04:00
|
|
|
|
2000-04-26 07:50:07 +04:00
|
|
|
//-- Parse the SF file. If the verification above failed, principal
|
|
|
|
// is null, and ParseOneFile will mark the relevant entries as invalid.
|
|
|
|
// if ParseOneFile fails, then it has no effect, and we can safely
|
|
|
|
// continue to the next SF file, or return.
|
2006-05-02 23:33:09 +04:00
|
|
|
ParseOneFile(manifestBuffer, JAR_SF);
|
2011-10-17 18:59:28 +04:00
|
|
|
mParsedManifest = true;
|
2000-04-26 07:50:07 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2000-02-14 04:57:01 +03:00
|
|
|
nsresult
|
2012-08-22 19:56:38 +04:00
|
|
|
nsJAR::ParseOneFile(const char* filebuf, int16_t aFileType)
|
2000-01-29 03:03:57 +03:00
|
|
|
{
|
2000-03-21 07:21:28 +03:00
|
|
|
//-- Check file header
|
2000-01-29 03:03:57 +03:00
|
|
|
const char* nextLineStart = filebuf;
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString curLine;
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t linelen;
|
2000-01-29 03:03:57 +03:00
|
|
|
linelen = ReadLine(&nextLineStart);
|
2000-03-21 07:21:28 +03:00
|
|
|
curLine.Assign(filebuf, linelen);
|
2000-09-03 09:54:31 +04:00
|
|
|
|
|
|
|
if ( ((aFileType == JAR_MF) && !curLine.Equals(JAR_MF_HEADER) ) ||
|
|
|
|
((aFileType == JAR_SF) && !curLine.Equals(JAR_SF_HEADER) ) )
|
2000-01-29 03:03:57 +03:00
|
|
|
return NS_ERROR_FILE_CORRUPTED;
|
|
|
|
|
2000-03-21 07:21:28 +03:00
|
|
|
//-- Skip header section
|
|
|
|
do {
|
|
|
|
linelen = ReadLine(&nextLineStart);
|
|
|
|
} while (linelen > 0);
|
|
|
|
|
|
|
|
//-- Set up parsing variables
|
|
|
|
const char* curPos;
|
|
|
|
const char* sectionStart = nextLineStart;
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
nsJARManifestItem* curItemMF = nullptr;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool foundName = false;
|
2000-03-21 07:21:28 +03:00
|
|
|
if (aFileType == JAR_MF)
|
2003-11-17 23:44:14 +03:00
|
|
|
if (!(curItemMF = new nsJARManifestItem()))
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString curItemName;
|
|
|
|
nsAutoCString storedSectionDigest;
|
2000-03-21 07:21:28 +03:00
|
|
|
|
2000-01-29 03:03:57 +03:00
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
curPos = nextLineStart;
|
|
|
|
linelen = ReadLine(&nextLineStart);
|
|
|
|
curLine.Assign(curPos, linelen);
|
|
|
|
if (linelen == 0)
|
|
|
|
// end of section (blank line or end-of-file)
|
|
|
|
{
|
|
|
|
if (aFileType == JAR_MF)
|
|
|
|
{
|
2002-12-14 01:24:12 +03:00
|
|
|
mTotalItemsInManifest++;
|
2000-03-21 07:21:28 +03:00
|
|
|
if (curItemMF->mType != JAR_INVALID)
|
2000-01-29 03:03:57 +03:00
|
|
|
{
|
2000-02-14 04:57:01 +03:00
|
|
|
//-- Did this section have a name: line?
|
2000-01-29 03:03:57 +03:00
|
|
|
if(!foundName)
|
2000-03-21 07:21:28 +03:00
|
|
|
curItemMF->mType = JAR_INVALID;
|
2000-01-29 03:03:57 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
//-- If it's an internal item, it must correspond
|
|
|
|
// to a valid jar entry
|
2006-05-02 23:33:09 +04:00
|
|
|
if (curItemMF->mType == JAR_INTERNAL)
|
|
|
|
{
|
2011-09-29 10:19:26 +04:00
|
|
|
bool exists;
|
2009-10-08 18:24:22 +04:00
|
|
|
nsresult rv = HasEntry(curItemName, &exists);
|
|
|
|
if (NS_FAILED(rv) || !exists)
|
2000-03-21 07:21:28 +03:00
|
|
|
curItemMF->mType = JAR_INVALID;
|
2000-01-29 03:03:57 +03:00
|
|
|
}
|
|
|
|
//-- Check for duplicates
|
2000-08-10 10:19:37 +04:00
|
|
|
nsCStringKey key(curItemName);
|
2000-01-29 03:03:57 +03:00
|
|
|
if (mManifestData.Exists(&key))
|
2000-03-21 07:21:28 +03:00
|
|
|
curItemMF->mType = JAR_INVALID;
|
2000-01-29 03:03:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-03-21 07:21:28 +03:00
|
|
|
if (curItemMF->mType == JAR_INVALID)
|
|
|
|
delete curItemMF;
|
2000-02-14 04:57:01 +03:00
|
|
|
else //-- calculate section digest
|
2000-01-29 03:03:57 +03:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t sectionLength = curPos - sectionStart;
|
2005-06-01 20:06:53 +04:00
|
|
|
CalculateDigest(sectionStart, sectionLength,
|
2009-04-07 13:24:58 +04:00
|
|
|
curItemMF->calculatedSectionDigest);
|
2000-01-29 03:03:57 +03:00
|
|
|
//-- Save item in the hashtable
|
2000-08-10 10:19:37 +04:00
|
|
|
nsCStringKey itemKey(curItemName);
|
2000-03-21 07:21:28 +03:00
|
|
|
mManifestData.Put(&itemKey, (void*)curItemMF);
|
2000-01-29 03:03:57 +03:00
|
|
|
}
|
2012-07-30 18:20:58 +04:00
|
|
|
if (nextLineStart == nullptr) // end-of-file
|
2000-01-29 03:03:57 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
sectionStart = nextLineStart;
|
2003-11-17 23:44:14 +03:00
|
|
|
if (!(curItemMF = new nsJARManifestItem()))
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2000-01-29 03:03:57 +03:00
|
|
|
} // (aFileType == JAR_MF)
|
|
|
|
else
|
|
|
|
//-- file type is SF, compare digest with calculated
|
|
|
|
// section digests from MF file.
|
|
|
|
{
|
|
|
|
if (foundName)
|
|
|
|
{
|
2000-03-21 07:21:28 +03:00
|
|
|
nsJARManifestItem* curItemSF;
|
2000-08-10 10:19:37 +04:00
|
|
|
nsCStringKey key(curItemName);
|
2000-03-21 07:21:28 +03:00
|
|
|
curItemSF = (nsJARManifestItem*)mManifestData.Get(&key);
|
|
|
|
if(curItemSF)
|
2000-01-29 03:03:57 +03:00
|
|
|
{
|
2006-05-02 23:33:09 +04:00
|
|
|
NS_ASSERTION(curItemSF->status == JAR_NOT_SIGNED,
|
2000-03-21 07:21:28 +03:00
|
|
|
"SECURITY ERROR: nsJARManifestItem not correctly initialized");
|
2000-04-26 07:50:07 +04:00
|
|
|
curItemSF->status = mGlobalStatus;
|
2006-05-02 23:33:09 +04:00
|
|
|
if (curItemSF->status == JAR_VALID_MANIFEST)
|
2000-03-21 07:21:28 +03:00
|
|
|
{ // Compare digests
|
2003-05-24 01:34:47 +04:00
|
|
|
if (storedSectionDigest.IsEmpty())
|
2006-05-02 23:33:09 +04:00
|
|
|
curItemSF->status = JAR_NOT_SIGNED;
|
2000-03-21 07:21:28 +03:00
|
|
|
else
|
2000-01-29 03:03:57 +03:00
|
|
|
{
|
2009-04-07 13:24:58 +04:00
|
|
|
if (!storedSectionDigest.Equals(curItemSF->calculatedSectionDigest))
|
2006-05-02 23:33:09 +04:00
|
|
|
curItemSF->status = JAR_INVALID_MANIFEST;
|
2009-04-07 13:24:58 +04:00
|
|
|
curItemSF->calculatedSectionDigest.Truncate();
|
|
|
|
storedSectionDigest.Truncate();
|
2000-01-29 03:03:57 +03:00
|
|
|
}
|
2012-07-30 18:20:58 +04:00
|
|
|
} // (aPrincipal != nullptr)
|
2000-03-21 07:21:28 +03:00
|
|
|
} // if(curItemSF)
|
2000-01-29 03:03:57 +03:00
|
|
|
} // if(foundName)
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
if(nextLineStart == nullptr) // end-of-file
|
2000-01-29 03:03:57 +03:00
|
|
|
break;
|
|
|
|
} // aFileType == JAR_SF
|
2011-10-17 18:59:28 +04:00
|
|
|
foundName = false;
|
2000-01-29 03:03:57 +03:00
|
|
|
continue;
|
|
|
|
} // if(linelen == 0)
|
|
|
|
|
|
|
|
//-- Look for continuations (beginning with a space) on subsequent lines
|
|
|
|
// and append them to the current line.
|
|
|
|
while(*nextLineStart == ' ')
|
|
|
|
{
|
|
|
|
curPos = nextLineStart;
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t continuationLen = ReadLine(&nextLineStart) - 1;
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString continuation(curPos+1, continuationLen);
|
2000-01-29 03:03:57 +03:00
|
|
|
curLine += continuation;
|
|
|
|
linelen += continuationLen;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-- Find colon in current line, this separates name from value
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t colonPos = curLine.FindChar(':');
|
2000-01-29 03:03:57 +03:00
|
|
|
if (colonPos == -1) // No colon on line, ignore line
|
|
|
|
continue;
|
|
|
|
//-- Break down the line
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString lineName;
|
2000-01-29 03:03:57 +03:00
|
|
|
curLine.Left(lineName, colonPos);
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString lineData;
|
2000-01-29 03:03:57 +03:00
|
|
|
curLine.Mid(lineData, colonPos+2, linelen - (colonPos+2));
|
|
|
|
|
|
|
|
//-- Lines to look for:
|
2000-02-14 04:57:01 +03:00
|
|
|
// (1) Digest:
|
2009-04-07 13:24:58 +04:00
|
|
|
if (lineName.LowerCaseEqualsLiteral("sha1-digest"))
|
2000-01-29 03:03:57 +03:00
|
|
|
//-- This is a digest line, save the data in the appropriate place
|
|
|
|
{
|
|
|
|
if(aFileType == JAR_MF)
|
2009-04-07 13:24:58 +04:00
|
|
|
curItemMF->storedEntryDigest = lineData;
|
2000-01-29 03:03:57 +03:00
|
|
|
else
|
2000-02-14 04:57:01 +03:00
|
|
|
storedSectionDigest = lineData;
|
2000-01-29 03:03:57 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// (2) Name: associates this manifest section with a file in the jar.
|
2009-04-07 13:24:58 +04:00
|
|
|
if (!foundName && lineName.LowerCaseEqualsLiteral("name"))
|
2000-01-29 03:03:57 +03:00
|
|
|
{
|
2000-03-21 07:21:28 +03:00
|
|
|
curItemName = lineData;
|
2011-10-17 18:59:28 +04:00
|
|
|
foundName = true;
|
2000-01-29 03:03:57 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// (3) Magic: this may be an inline Javascript.
|
|
|
|
// We can't do any other kind of magic.
|
2009-04-07 13:24:58 +04:00
|
|
|
if (aFileType == JAR_MF && lineName.LowerCaseEqualsLiteral("magic"))
|
2000-01-29 03:03:57 +03:00
|
|
|
{
|
2009-04-07 13:24:58 +04:00
|
|
|
if (lineData.LowerCaseEqualsLiteral("javascript"))
|
2000-03-21 07:21:28 +03:00
|
|
|
curItemMF->mType = JAR_EXTERNAL;
|
2000-01-29 03:03:57 +03:00
|
|
|
else
|
2000-03-21 07:21:28 +03:00
|
|
|
curItemMF->mType = JAR_INVALID;
|
2000-01-29 03:03:57 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // for (;;)
|
1999-10-26 23:43:26 +04:00
|
|
|
return NS_OK;
|
2000-01-29 03:03:57 +03:00
|
|
|
} //ParseOneFile()
|
|
|
|
|
2000-02-14 04:57:01 +03:00
|
|
|
nsresult
|
2006-05-02 23:33:09 +04:00
|
|
|
nsJAR::VerifyEntry(nsJARManifestItem* aManItem, const char* aEntryData,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aLen)
|
2000-02-14 04:57:01 +03:00
|
|
|
{
|
2006-05-02 23:33:09 +04:00
|
|
|
if (aManItem->status == JAR_VALID_MANIFEST)
|
2000-02-14 04:57:01 +03:00
|
|
|
{
|
2009-04-07 13:24:58 +04:00
|
|
|
if (aManItem->storedEntryDigest.IsEmpty())
|
2000-03-21 07:21:28 +03:00
|
|
|
// No entry digests in manifest file. Entry is unsigned.
|
2006-05-02 23:33:09 +04:00
|
|
|
aManItem->status = JAR_NOT_SIGNED;
|
2000-02-14 04:57:01 +03:00
|
|
|
else
|
|
|
|
{ //-- Calculate and compare digests
|
2009-04-07 13:24:58 +04:00
|
|
|
nsCString calculatedEntryDigest;
|
|
|
|
nsresult rv = CalculateDigest(aEntryData, aLen, calculatedEntryDigest);
|
2000-05-10 05:49:33 +04:00
|
|
|
if (NS_FAILED(rv)) return NS_ERROR_FAILURE;
|
2009-04-07 13:24:58 +04:00
|
|
|
if (!aManItem->storedEntryDigest.Equals(calculatedEntryDigest))
|
2006-05-02 23:33:09 +04:00
|
|
|
aManItem->status = JAR_INVALID_ENTRY;
|
2009-04-07 13:24:58 +04:00
|
|
|
aManItem->storedEntryDigest.Truncate();
|
2000-02-14 04:57:01 +03:00
|
|
|
}
|
|
|
|
}
|
2011-10-17 18:59:28 +04:00
|
|
|
aManItem->entryVerified = true;
|
2000-07-12 07:10:33 +04:00
|
|
|
return NS_OK;
|
2000-03-28 07:38:06 +04:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
void nsJAR::ReportError(const nsACString &aFilename, int16_t errorCode)
|
2000-04-26 07:50:07 +04:00
|
|
|
{
|
|
|
|
//-- Generate error message
|
2000-05-10 05:49:33 +04:00
|
|
|
nsAutoString message;
|
2004-06-17 04:13:25 +04:00
|
|
|
message.AssignLiteral("Signature Verification Error: the signature on ");
|
2011-10-28 17:43:01 +04:00
|
|
|
if (!aFilename.IsEmpty())
|
2012-07-06 13:43:28 +04:00
|
|
|
AppendASCIItoUTF16(aFilename, message);
|
2000-04-26 07:50:07 +04:00
|
|
|
else
|
2004-06-17 04:13:25 +04:00
|
|
|
message.AppendLiteral("this .jar archive");
|
|
|
|
message.AppendLiteral(" is invalid because ");
|
2000-04-26 07:50:07 +04:00
|
|
|
switch(errorCode)
|
|
|
|
{
|
2006-05-02 23:33:09 +04:00
|
|
|
case JAR_NOT_SIGNED:
|
2004-06-17 04:13:25 +04:00
|
|
|
message.AppendLiteral("the archive did not contain a valid PKCS7 signature.");
|
2000-04-26 07:50:07 +04:00
|
|
|
break;
|
2006-05-02 23:33:09 +04:00
|
|
|
case JAR_INVALID_SIG:
|
|
|
|
message.AppendLiteral("the digital signature (*.RSA) file is not a valid signature of the signature instruction file (*.SF).");
|
2000-04-26 07:50:07 +04:00
|
|
|
break;
|
2006-05-02 23:33:09 +04:00
|
|
|
case JAR_INVALID_UNKNOWN_CA:
|
2004-06-17 04:13:25 +04:00
|
|
|
message.AppendLiteral("the certificate used to sign this file has an unrecognized issuer.");
|
2000-04-26 07:50:07 +04:00
|
|
|
break;
|
2006-05-02 23:33:09 +04:00
|
|
|
case JAR_INVALID_MANIFEST:
|
|
|
|
message.AppendLiteral("the signature instruction file (*.SF) does not contain a valid hash of the MANIFEST.MF file.");
|
2000-04-26 07:50:07 +04:00
|
|
|
break;
|
2006-05-02 23:33:09 +04:00
|
|
|
case JAR_INVALID_ENTRY:
|
2004-06-17 04:13:25 +04:00
|
|
|
message.AppendLiteral("the MANIFEST.MF file does not contain a valid hash of the file being verified.");
|
2000-04-26 07:50:07 +04:00
|
|
|
break;
|
2006-05-02 23:33:09 +04:00
|
|
|
case JAR_NO_MANIFEST:
|
|
|
|
message.AppendLiteral("the archive did not contain a manifest.");
|
|
|
|
break;
|
2000-04-26 07:50:07 +04:00
|
|
|
default:
|
2004-06-17 04:13:25 +04:00
|
|
|
message.AppendLiteral("of an unknown problem.");
|
2000-04-26 07:50:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Report error in JS console
|
2000-09-15 03:20:49 +04:00
|
|
|
nsCOMPtr<nsIConsoleService> console(do_GetService("@mozilla.org/consoleservice;1"));
|
2000-04-26 07:50:07 +04:00
|
|
|
if (console)
|
|
|
|
{
|
2001-09-29 12:28:41 +04:00
|
|
|
console->LogStringMessage(message.get());
|
2000-04-26 07:50:07 +04:00
|
|
|
}
|
2001-05-15 10:43:03 +04:00
|
|
|
#ifdef DEBUG
|
2001-09-29 12:28:41 +04:00
|
|
|
char* messageCstr = ToNewCString(message);
|
2001-05-15 10:43:03 +04:00
|
|
|
if (!messageCstr) return;
|
|
|
|
fprintf(stderr, "%s\n", messageCstr);
|
|
|
|
nsMemory::Free(messageCstr);
|
2000-05-10 05:49:33 +04:00
|
|
|
#endif
|
2000-04-26 07:50:07 +04:00
|
|
|
}
|
|
|
|
|
2000-03-21 07:21:28 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
nsresult nsJAR::CalculateDigest(const char* aInBuf, uint32_t aLen,
|
2009-04-07 13:24:58 +04:00
|
|
|
nsCString& digest)
|
2000-03-21 07:21:28 +03:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2005-06-01 20:06:53 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsICryptoHash> hasher = do_CreateInstance("@mozilla.org/security/hash;1", &rv);
|
2000-03-21 07:21:28 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2005-06-01 20:06:53 +04:00
|
|
|
rv = hasher->Init(nsICryptoHash::SHA1);
|
2000-03-21 07:21:28 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2005-06-01 20:06:53 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
rv = hasher->Update((const uint8_t*) aInBuf, aLen);
|
2005-06-01 20:06:53 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return hasher->Finish(true, digest);
|
2000-03-21 07:21:28 +03:00
|
|
|
}
|
|
|
|
|
2006-05-02 23:33:09 +04:00
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS1(nsJAREnumerator, nsIUTF8StringEnumerator)
|
|
|
|
|
2000-01-29 03:03:57 +03:00
|
|
|
//----------------------------------------------
|
2006-05-02 23:33:09 +04:00
|
|
|
// nsJAREnumerator::HasMore
|
1999-06-23 10:16:28 +04:00
|
|
|
//----------------------------------------------
|
1999-06-02 01:08:32 +04:00
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsJAREnumerator::HasMore(bool* aResult)
|
1999-06-02 01:08:32 +04:00
|
|
|
{
|
1999-06-23 10:16:28 +04:00
|
|
|
// try to get the next element
|
2009-10-17 19:54:54 +04:00
|
|
|
if (!mName) {
|
2006-05-02 23:33:09 +04:00
|
|
|
NS_ASSERTION(mFind, "nsJAREnumerator: Missing zipFind.");
|
2009-10-17 19:54:54 +04:00
|
|
|
nsresult rv = mFind->FindNext( &mName, &mNameLen );
|
2006-05-02 23:33:09 +04:00
|
|
|
if (rv == NS_ERROR_FILE_TARGET_DOES_NOT_EXIST) {
|
2011-10-17 18:59:28 +04:00
|
|
|
*aResult = false; // No more matches available
|
1999-06-23 10:16:28 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2006-05-02 23:33:09 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE); // no error translation
|
1999-06-23 10:16:28 +04:00
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
*aResult = true;
|
1999-06-23 10:16:28 +04:00
|
|
|
return NS_OK;
|
1999-06-02 01:08:32 +04:00
|
|
|
}
|
|
|
|
|
1999-06-23 10:16:28 +04:00
|
|
|
//----------------------------------------------
|
|
|
|
// nsJAREnumerator::GetNext
|
|
|
|
//----------------------------------------------
|
|
|
|
NS_IMETHODIMP
|
2006-05-02 23:33:09 +04:00
|
|
|
nsJAREnumerator::GetNext(nsACString& aResult)
|
1999-06-23 10:16:28 +04:00
|
|
|
{
|
|
|
|
// check if the current item is "stale"
|
2009-10-17 19:54:54 +04:00
|
|
|
if (!mName) {
|
2011-09-29 10:19:26 +04:00
|
|
|
bool bMore;
|
2006-05-02 23:33:09 +04:00
|
|
|
nsresult rv = HasMore(&bMore);
|
|
|
|
if (NS_FAILED(rv) || !bMore)
|
|
|
|
return NS_ERROR_FAILURE; // no error translation
|
1999-07-16 03:06:52 +04:00
|
|
|
}
|
2009-10-17 19:54:54 +04:00
|
|
|
aResult.Assign(mName, mNameLen);
|
|
|
|
mName = 0; // we just gave this one away
|
2006-05-02 23:33:09 +04:00
|
|
|
return NS_OK;
|
1999-06-23 10:16:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-03-30 02:10:37 +04:00
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS1(nsJARItem, nsIZipEntry)
|
1999-06-23 10:16:28 +04:00
|
|
|
|
2006-05-02 23:33:09 +04:00
|
|
|
nsJARItem::nsJARItem(nsZipItem* aZipItem)
|
2009-10-17 19:54:54 +04:00
|
|
|
: mSize(aZipItem->Size()),
|
|
|
|
mRealsize(aZipItem->RealSize()),
|
|
|
|
mCrc32(aZipItem->CRC32()),
|
2009-12-16 02:01:08 +03:00
|
|
|
mLastModTime(aZipItem->LastModTime()),
|
2009-10-17 19:54:54 +04:00
|
|
|
mCompression(aZipItem->Compression()),
|
|
|
|
mIsDirectory(aZipItem->IsDirectory()),
|
2006-05-02 23:33:09 +04:00
|
|
|
mIsSynthetic(aZipItem->isSynthetic)
|
2000-01-29 03:03:57 +03:00
|
|
|
{
|
1999-06-23 10:16:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------
|
|
|
|
// nsJARItem::GetCompression
|
|
|
|
//------------------------------------------
|
2006-03-30 02:10:37 +04:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsJARItem::GetCompression(uint16_t *aCompression)
|
1999-06-23 10:16:28 +04:00
|
|
|
{
|
2006-05-02 23:33:09 +04:00
|
|
|
NS_ENSURE_ARG_POINTER(aCompression);
|
1999-06-23 10:16:28 +04:00
|
|
|
|
2006-05-02 23:33:09 +04:00
|
|
|
*aCompression = mCompression;
|
1999-06-23 10:16:28 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------
|
|
|
|
// nsJARItem::GetSize
|
|
|
|
//------------------------------------------
|
2006-03-30 02:10:37 +04:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsJARItem::GetSize(uint32_t *aSize)
|
1999-06-23 10:16:28 +04:00
|
|
|
{
|
2006-05-02 23:33:09 +04:00
|
|
|
NS_ENSURE_ARG_POINTER(aSize);
|
1999-06-23 10:16:28 +04:00
|
|
|
|
2006-05-02 23:33:09 +04:00
|
|
|
*aSize = mSize;
|
1999-06-23 10:16:28 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------
|
|
|
|
// nsJARItem::GetRealSize
|
|
|
|
//------------------------------------------
|
2006-03-30 02:10:37 +04:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsJARItem::GetRealSize(uint32_t *aRealsize)
|
1999-06-23 10:16:28 +04:00
|
|
|
{
|
2006-05-02 23:33:09 +04:00
|
|
|
NS_ENSURE_ARG_POINTER(aRealsize);
|
1999-06-23 10:16:28 +04:00
|
|
|
|
2006-05-02 23:33:09 +04:00
|
|
|
*aRealsize = mRealsize;
|
1999-06-23 10:16:28 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------
|
|
|
|
// nsJARItem::GetCrc32
|
|
|
|
//------------------------------------------
|
2006-03-30 02:10:37 +04:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsJARItem::GetCRC32(uint32_t *aCrc32)
|
1999-06-23 10:16:28 +04:00
|
|
|
{
|
2006-05-02 23:33:09 +04:00
|
|
|
NS_ENSURE_ARG_POINTER(aCrc32);
|
1999-06-23 10:16:28 +04:00
|
|
|
|
2006-05-02 23:33:09 +04:00
|
|
|
*aCrc32 = mCrc32;
|
1999-06-23 10:16:28 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2000-01-29 03:03:57 +03:00
|
|
|
|
2006-03-30 02:10:37 +04:00
|
|
|
//------------------------------------------
|
|
|
|
// nsJARItem::GetIsDirectory
|
|
|
|
//------------------------------------------
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsJARItem::GetIsDirectory(bool *aIsDirectory)
|
2006-03-30 02:10:37 +04:00
|
|
|
{
|
2006-05-02 23:33:09 +04:00
|
|
|
NS_ENSURE_ARG_POINTER(aIsDirectory);
|
2006-03-30 02:10:37 +04:00
|
|
|
|
2006-05-02 23:33:09 +04:00
|
|
|
*aIsDirectory = mIsDirectory;
|
2006-03-30 02:10:37 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------
|
|
|
|
// nsJARItem::GetIsSynthetic
|
|
|
|
//------------------------------------------
|
2006-05-02 23:33:09 +04:00
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsJARItem::GetIsSynthetic(bool *aIsSynthetic)
|
2006-03-30 02:10:37 +04:00
|
|
|
{
|
2006-05-02 23:33:09 +04:00
|
|
|
NS_ENSURE_ARG_POINTER(aIsSynthetic);
|
2006-03-30 02:10:37 +04:00
|
|
|
|
2006-05-02 23:33:09 +04:00
|
|
|
*aIsSynthetic = mIsSynthetic;
|
2006-03-30 02:10:37 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------
|
|
|
|
// nsJARItem::GetLastModifiedTime
|
|
|
|
//------------------------------------------
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsJARItem::GetLastModifiedTime(PRTime* aLastModTime)
|
|
|
|
{
|
2006-05-02 23:33:09 +04:00
|
|
|
NS_ENSURE_ARG_POINTER(aLastModTime);
|
2006-03-30 02:10:37 +04:00
|
|
|
|
2009-12-16 02:01:08 +03:00
|
|
|
*aLastModTime = mLastModTime;
|
2006-03-30 02:10:37 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2000-04-12 11:58:24 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// nsIZipReaderCache
|
|
|
|
|
2000-11-01 01:44:20 +03:00
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS3(nsZipReaderCache, nsIZipReaderCache, nsIObserver, nsISupportsWeakReference)
|
2000-04-12 11:58:24 +04:00
|
|
|
|
|
|
|
nsZipReaderCache::nsZipReaderCache()
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 08:29:02 +04:00
|
|
|
: mLock("nsZipReaderCache.mLock")
|
2010-09-09 07:38:34 +04:00
|
|
|
, mZips(16)
|
2000-08-24 11:38:41 +04:00
|
|
|
#ifdef ZIP_CACHE_HIT_RATE
|
2000-11-01 01:44:20 +03:00
|
|
|
,
|
2000-08-24 11:38:41 +04:00
|
|
|
mZipCacheLookups(0),
|
|
|
|
mZipCacheHits(0),
|
|
|
|
mZipCacheFlushes(0),
|
2000-11-01 01:44:20 +03:00
|
|
|
mZipSyncMisses(0)
|
2000-08-24 11:38:41 +04:00
|
|
|
#endif
|
2000-04-12 11:58:24 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsZipReaderCache::Init(uint32_t cacheSize)
|
2000-04-12 11:58:24 +04:00
|
|
|
{
|
2000-11-01 01:44:20 +03:00
|
|
|
mCacheSize = cacheSize;
|
|
|
|
|
|
|
|
// Register as a memory pressure observer
|
2001-07-25 11:54:28 +04:00
|
|
|
nsCOMPtr<nsIObserverService> os =
|
2004-11-27 20:25:25 +03:00
|
|
|
do_GetService("@mozilla.org/observer-service;1");
|
|
|
|
if (os)
|
2000-11-01 01:44:20 +03:00
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
os->AddObserver(this, "memory-pressure", true);
|
|
|
|
os->AddObserver(this, "chrome-flush-caches", true);
|
|
|
|
os->AddObserver(this, "flush-cache-entry", true);
|
2000-11-01 01:44:20 +03:00
|
|
|
}
|
|
|
|
// ignore failure of the observer registration.
|
|
|
|
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 08:29:02 +04:00
|
|
|
return NS_OK;
|
2000-04-12 11:58:24 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool
|
2000-08-23 07:18:53 +04:00
|
|
|
DropZipReaderCache(nsHashKey *aKey, void *aData, void* closure)
|
|
|
|
{
|
|
|
|
nsJAR* zip = (nsJAR*)aData;
|
2012-07-30 18:20:58 +04:00
|
|
|
zip->SetZipReaderCache(nullptr);
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2000-08-23 07:18:53 +04:00
|
|
|
}
|
|
|
|
|
2000-04-12 11:58:24 +04:00
|
|
|
nsZipReaderCache::~nsZipReaderCache()
|
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
mZips.Enumerate(DropZipReaderCache, nullptr);
|
2000-08-24 11:38:41 +04:00
|
|
|
|
|
|
|
#ifdef ZIP_CACHE_HIT_RATE
|
2000-11-01 01:44:20 +03:00
|
|
|
printf("nsZipReaderCache size=%d hits=%d lookups=%d rate=%f%% flushes=%d missed %d\n",
|
|
|
|
mCacheSize, mZipCacheHits, mZipCacheLookups,
|
|
|
|
(float)mZipCacheHits / mZipCacheLookups,
|
|
|
|
mZipCacheFlushes, mZipSyncMisses);
|
2000-08-24 11:38:41 +04:00
|
|
|
#endif
|
2000-04-12 11:58:24 +04:00
|
|
|
}
|
|
|
|
|
2012-12-22 17:56:21 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsZipReaderCache::IsCached(nsIFile* zipFile, bool* aResult)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(zipFile);
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIZipReader> antiLockZipGrip;
|
|
|
|
MutexAutoLock lock(mLock);
|
|
|
|
|
|
|
|
nsAutoCString uri;
|
|
|
|
rv = zipFile->GetNativePath(uri);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
uri.Insert(NS_LITERAL_CSTRING("file:"), 0);
|
|
|
|
|
|
|
|
nsCStringKey key(uri);
|
|
|
|
|
|
|
|
*aResult = mZips.Exists(&key);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2000-04-12 11:58:24 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsZipReaderCache::GetZip(nsIFile* zipFile, nsIZipReader* *result)
|
|
|
|
{
|
2008-09-06 19:06:47 +04:00
|
|
|
NS_ENSURE_ARG_POINTER(zipFile);
|
2000-04-12 11:58:24 +04:00
|
|
|
nsresult rv;
|
2010-03-07 16:56:45 +03:00
|
|
|
nsCOMPtr<nsIZipReader> antiLockZipGrip;
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 08:29:02 +04:00
|
|
|
MutexAutoLock lock(mLock);
|
2000-04-12 11:58:24 +04:00
|
|
|
|
2000-08-24 11:38:41 +04:00
|
|
|
#ifdef ZIP_CACHE_HIT_RATE
|
|
|
|
mZipCacheLookups++;
|
|
|
|
#endif
|
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString uri;
|
2010-09-09 07:38:34 +04:00
|
|
|
rv = zipFile->GetNativePath(uri);
|
2000-05-25 12:30:29 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2010-09-09 07:38:34 +04:00
|
|
|
uri.Insert(NS_LITERAL_CSTRING("file:"), 0);
|
|
|
|
|
|
|
|
nsCStringKey key(uri);
|
2007-07-08 11:08:04 +04:00
|
|
|
nsJAR* zip = static_cast<nsJAR*>(static_cast<nsIZipReader*>(mZips.Get(&key))); // AddRefs
|
2009-08-13 00:50:12 +04:00
|
|
|
if (zip) {
|
2000-08-24 11:38:41 +04:00
|
|
|
#ifdef ZIP_CACHE_HIT_RATE
|
|
|
|
mZipCacheHits++;
|
|
|
|
#endif
|
2000-11-01 01:44:20 +03:00
|
|
|
zip->ClearReleaseTime();
|
2000-04-12 11:58:24 +04:00
|
|
|
}
|
2000-08-23 07:18:53 +04:00
|
|
|
else {
|
|
|
|
zip = new nsJAR();
|
2012-07-30 18:20:58 +04:00
|
|
|
if (zip == nullptr)
|
2000-08-23 07:18:53 +04:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
NS_ADDREF(zip);
|
|
|
|
zip->SetZipReaderCache(this);
|
2000-04-12 11:58:24 +04:00
|
|
|
|
2006-05-02 23:33:09 +04:00
|
|
|
rv = zip->Open(zipFile);
|
2000-08-23 07:18:53 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_RELEASE(zip);
|
|
|
|
return rv;
|
|
|
|
}
|
2000-04-12 11:58:24 +04:00
|
|
|
|
2010-07-11 16:49:52 +04:00
|
|
|
#ifdef DEBUG
|
2011-09-29 10:19:26 +04:00
|
|
|
bool collision =
|
2010-07-11 16:49:52 +04:00
|
|
|
#endif
|
|
|
|
mZips.Put(&key, static_cast<nsIZipReader*>(zip)); // AddRefs to 2
|
2000-08-23 07:18:53 +04:00
|
|
|
NS_ASSERTION(!collision, "horked");
|
2000-04-12 11:58:24 +04:00
|
|
|
}
|
|
|
|
*result = zip;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2010-09-09 07:38:34 +04:00
|
|
|
NS_IMETHODIMP
|
2011-09-29 03:14:45 +04:00
|
|
|
nsZipReaderCache::GetInnerZip(nsIFile* zipFile, const nsACString &entry,
|
2010-09-09 07:38:34 +04:00
|
|
|
nsIZipReader* *result)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(zipFile);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIZipReader> outerZipReader;
|
|
|
|
nsresult rv = GetZip(zipFile, getter_AddRefs(outerZipReader));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
#ifdef ZIP_CACHE_HIT_RATE
|
|
|
|
mZipCacheLookups++;
|
|
|
|
#endif
|
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString uri;
|
2010-09-09 07:38:34 +04:00
|
|
|
rv = zipFile->GetNativePath(uri);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
uri.Insert(NS_LITERAL_CSTRING("jar:"), 0);
|
|
|
|
uri.AppendLiteral("!/");
|
|
|
|
uri.Append(entry);
|
|
|
|
|
|
|
|
nsCStringKey key(uri);
|
|
|
|
nsJAR* zip = static_cast<nsJAR*>(static_cast<nsIZipReader*>(mZips.Get(&key))); // AddRefs
|
|
|
|
if (zip) {
|
|
|
|
#ifdef ZIP_CACHE_HIT_RATE
|
|
|
|
mZipCacheHits++;
|
|
|
|
#endif
|
|
|
|
zip->ClearReleaseTime();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
zip = new nsJAR();
|
|
|
|
NS_ADDREF(zip);
|
|
|
|
zip->SetZipReaderCache(this);
|
|
|
|
|
|
|
|
rv = zip->OpenInner(outerZipReader, entry);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_RELEASE(zip);
|
|
|
|
return rv;
|
|
|
|
}
|
2011-02-08 22:16:37 +03:00
|
|
|
#ifdef DEBUG
|
2011-09-29 10:19:26 +04:00
|
|
|
bool collision =
|
2011-02-08 22:16:37 +03:00
|
|
|
#endif
|
|
|
|
mZips.Put(&key, static_cast<nsIZipReader*>(zip)); // AddRefs to 2
|
2010-09-09 07:38:34 +04:00
|
|
|
NS_ASSERTION(!collision, "horked");
|
|
|
|
}
|
|
|
|
*result = zip;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool
|
2000-08-23 07:18:53 +04:00
|
|
|
FindOldestZip(nsHashKey *aKey, void *aData, void* closure)
|
|
|
|
{
|
|
|
|
nsJAR** oldestPtr = (nsJAR**)closure;
|
|
|
|
nsJAR* oldest = *oldestPtr;
|
|
|
|
nsJAR* current = (nsJAR*)aData;
|
|
|
|
PRIntervalTime currentReleaseTime = current->GetReleaseTime();
|
|
|
|
if (currentReleaseTime != PR_INTERVAL_NO_TIMEOUT) {
|
2012-07-30 18:20:58 +04:00
|
|
|
if (oldest == nullptr ||
|
2000-08-23 07:18:53 +04:00
|
|
|
currentReleaseTime < oldest->GetReleaseTime()) {
|
|
|
|
*oldestPtr = current;
|
|
|
|
}
|
|
|
|
}
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2000-08-23 07:18:53 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
struct ZipFindData {nsJAR* zip; bool found;};
|
2000-11-01 01:44:20 +03:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool
|
2000-11-01 01:44:20 +03:00
|
|
|
FindZip(nsHashKey *aKey, void *aData, void* closure)
|
|
|
|
{
|
|
|
|
ZipFindData* find_data = (ZipFindData*)closure;
|
|
|
|
|
|
|
|
if (find_data->zip == (nsJAR*)aData) {
|
2011-10-17 18:59:28 +04:00
|
|
|
find_data->found = true;
|
|
|
|
return false;
|
2000-11-01 01:44:20 +03:00
|
|
|
}
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2000-11-01 01:44:20 +03:00
|
|
|
}
|
|
|
|
|
2000-08-23 07:18:53 +04:00
|
|
|
nsresult
|
|
|
|
nsZipReaderCache::ReleaseZip(nsJAR* zip)
|
2000-04-12 11:58:24 +04:00
|
|
|
{
|
|
|
|
nsresult rv;
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 08:29:02 +04:00
|
|
|
MutexAutoLock lock(mLock);
|
2000-04-12 11:58:24 +04:00
|
|
|
|
2000-11-01 01:44:20 +03:00
|
|
|
// It is possible that two thread compete for this zip. The dangerous
|
|
|
|
// case is where one thread Releases the zip and discovers that the ref
|
|
|
|
// count has gone to one. Before it can call this ReleaseZip method
|
|
|
|
// another thread calls our GetZip method. The ref count goes to two. That
|
2006-03-30 02:10:37 +04:00
|
|
|
// second thread then Releases the zip and the ref count goes to one. It
|
|
|
|
// then tries to enter this ReleaseZip method and blocks while the first
|
2000-11-01 01:44:20 +03:00
|
|
|
// thread is still here. The first thread continues and remove the zip from
|
|
|
|
// the cache and calls its Release method sending the ref count to 0 and
|
|
|
|
// deleting the zip. However, the second thread is still blocked at the
|
|
|
|
// start of ReleaseZip, but the 'zip' param now hold a reference to a
|
|
|
|
// deleted zip!
|
|
|
|
//
|
2006-03-30 02:10:37 +04:00
|
|
|
// So, we are going to try safeguarding here by searching our hashtable while
|
2000-11-01 01:44:20 +03:00
|
|
|
// locked here for the zip. We return fast if it is not found.
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
ZipFindData find_data = {zip, false};
|
2000-11-01 01:44:20 +03:00
|
|
|
mZips.Enumerate(FindZip, &find_data);
|
|
|
|
if (!find_data.found) {
|
|
|
|
#ifdef ZIP_CACHE_HIT_RATE
|
|
|
|
mZipSyncMisses++;
|
|
|
|
#endif
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2000-09-18 11:08:22 +04:00
|
|
|
zip->SetReleaseTime();
|
|
|
|
|
2000-08-23 07:18:53 +04:00
|
|
|
if (mZips.Count() <= mCacheSize)
|
|
|
|
return NS_OK;
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
nsJAR* oldest = nullptr;
|
2000-11-01 01:44:20 +03:00
|
|
|
mZips.Enumerate(FindOldestZip, &oldest);
|
|
|
|
|
|
|
|
// Because of the craziness above it is possible that there is no zip that
|
|
|
|
// needs removing.
|
|
|
|
if (!oldest)
|
|
|
|
return NS_OK;
|
2000-08-23 07:18:53 +04:00
|
|
|
|
2000-08-24 11:38:41 +04:00
|
|
|
#ifdef ZIP_CACHE_HIT_RATE
|
|
|
|
mZipCacheFlushes++;
|
|
|
|
#endif
|
|
|
|
|
2000-08-23 07:18:53 +04:00
|
|
|
// remove from hashtable
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString uri;
|
2010-09-09 07:38:34 +04:00
|
|
|
rv = oldest->GetJarPath(uri);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2011-03-31 23:01:12 +04:00
|
|
|
if (oldest->mOuterZipEntry.IsEmpty()) {
|
2010-09-09 07:38:34 +04:00
|
|
|
uri.Insert(NS_LITERAL_CSTRING("file:"), 0);
|
|
|
|
} else {
|
|
|
|
uri.Insert(NS_LITERAL_CSTRING("jar:"), 0);
|
|
|
|
uri.AppendLiteral("!/");
|
2011-03-31 23:01:12 +04:00
|
|
|
uri.Append(oldest->mOuterZipEntry);
|
2010-09-09 07:38:34 +04:00
|
|
|
}
|
2000-05-25 12:30:29 +04:00
|
|
|
|
2010-09-09 07:38:34 +04:00
|
|
|
nsCStringKey key(uri);
|
2011-03-31 23:01:12 +04:00
|
|
|
nsRefPtr<nsJAR> removed;
|
|
|
|
mZips.Remove(&key, (nsISupports **)removed.StartAssignment());
|
2000-08-23 07:18:53 +04:00
|
|
|
NS_ASSERTION(removed, "botched");
|
2011-03-31 23:01:12 +04:00
|
|
|
NS_ASSERTION(oldest == removed, "removed wrong entry");
|
|
|
|
|
|
|
|
if (removed)
|
2012-07-30 18:20:58 +04:00
|
|
|
removed->SetZipReaderCache(nullptr);
|
2000-04-12 11:58:24 +04:00
|
|
|
|
2000-11-01 01:44:20 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool
|
2000-11-01 01:44:20 +03:00
|
|
|
FindFlushableZip(nsHashKey *aKey, void *aData, void* closure)
|
|
|
|
{
|
|
|
|
nsHashKey** flushableKeyPtr = (nsHashKey**)closure;
|
|
|
|
nsJAR* current = (nsJAR*)aData;
|
|
|
|
|
|
|
|
if (current->GetReleaseTime() != PR_INTERVAL_NO_TIMEOUT) {
|
|
|
|
*flushableKeyPtr = aKey;
|
2012-07-30 18:20:58 +04:00
|
|
|
current->SetZipReaderCache(nullptr);
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2000-11-01 01:44:20 +03:00
|
|
|
}
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2000-11-01 01:44:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsZipReaderCache::Observe(nsISupports *aSubject,
|
2001-10-20 00:52:59 +04:00
|
|
|
const char *aTopic,
|
2000-11-01 01:44:20 +03:00
|
|
|
const PRUnichar *aSomeData)
|
|
|
|
{
|
2004-11-27 20:25:25 +03:00
|
|
|
if (strcmp(aTopic, "memory-pressure") == 0) {
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 08:29:02 +04:00
|
|
|
MutexAutoLock lock(mLock);
|
2011-10-17 18:59:28 +04:00
|
|
|
while (true) {
|
2012-07-30 18:20:58 +04:00
|
|
|
nsHashKey* flushable = nullptr;
|
2000-11-01 01:44:20 +03:00
|
|
|
mZips.Enumerate(FindFlushableZip, &flushable);
|
|
|
|
if ( ! flushable )
|
|
|
|
break;
|
2010-07-11 16:49:52 +04:00
|
|
|
#ifdef DEBUG
|
2011-09-29 10:19:26 +04:00
|
|
|
bool removed =
|
2010-07-11 16:49:52 +04:00
|
|
|
#endif
|
|
|
|
mZips.Remove(flushable); // Releases
|
2000-11-01 01:44:20 +03:00
|
|
|
NS_ASSERTION(removed, "botched");
|
|
|
|
|
|
|
|
#ifdef xDEBUG_jband
|
|
|
|
printf("flushed something from the jar cache\n");
|
|
|
|
#endif
|
|
|
|
}
|
2004-11-27 20:25:25 +03:00
|
|
|
}
|
|
|
|
else if (strcmp(aTopic, "chrome-flush-caches") == 0) {
|
2012-07-30 18:20:58 +04:00
|
|
|
mZips.Enumerate(DropZipReaderCache, nullptr);
|
2004-11-27 20:25:25 +03:00
|
|
|
mZips.Reset();
|
|
|
|
}
|
2010-09-09 07:39:19 +04:00
|
|
|
else if (strcmp(aTopic, "flush-cache-entry") == 0) {
|
|
|
|
nsCOMPtr<nsIFile> file = do_QueryInterface(aSubject);
|
|
|
|
if (!file)
|
|
|
|
return NS_OK;
|
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString uri;
|
2010-09-09 07:39:19 +04:00
|
|
|
if (NS_FAILED(file->GetNativePath(uri)))
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
uri.Insert(NS_LITERAL_CSTRING("file:"), 0);
|
|
|
|
nsCStringKey key(uri);
|
|
|
|
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 08:29:02 +04:00
|
|
|
MutexAutoLock lock(mLock);
|
2010-09-09 07:39:19 +04:00
|
|
|
nsJAR* zip = static_cast<nsJAR*>(static_cast<nsIZipReader*>(mZips.Get(&key)));
|
|
|
|
if (!zip)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
#ifdef ZIP_CACHE_HIT_RATE
|
|
|
|
mZipCacheFlushes++;
|
|
|
|
#endif
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
zip->SetZipReaderCache(nullptr);
|
2010-09-09 07:39:19 +04:00
|
|
|
|
|
|
|
mZips.Remove(&key);
|
|
|
|
NS_RELEASE(zip);
|
|
|
|
}
|
2000-04-12 11:58:24 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|