2000-02-14 01:57:01 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
1999-06-01 21:08:32 +00:00
|
|
|
/*
|
1999-11-06 03:43:54 +00:00
|
|
|
* The contents of this file are subject to the Netscape Public
|
|
|
|
* License Version 1.1 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at http://www.mozilla.org/NPL/
|
1999-06-01 21:08:32 +00:00
|
|
|
*
|
1999-11-06 03:43:54 +00:00
|
|
|
* Software distributed under the License is distributed on an "AS
|
|
|
|
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
|
|
|
* implied. See the License for the specific language governing
|
|
|
|
* rights and limitations under the License.
|
1999-06-01 21:08:32 +00:00
|
|
|
*
|
|
|
|
* The Original Code is Mozilla Communicator client code,
|
|
|
|
* released March 31, 1998.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Netscape Communications
|
1999-11-06 03:43:54 +00:00
|
|
|
* Corporation. Portions created by Netscape are
|
|
|
|
* Copyright (C) 1998 Netscape Communications Corporation. All
|
|
|
|
* Rights Reserved.
|
1999-06-01 21:08:32 +00:00
|
|
|
*
|
1999-11-06 03:43:54 +00:00
|
|
|
* Contributor(s):
|
1999-06-01 21:08:32 +00:00
|
|
|
* Daniel Veditz <dveditz@netscape.com>
|
1999-06-23 06:16:28 +00:00
|
|
|
* Samir Gehani <sgehani@netscape.com>
|
1999-10-26 19:43:26 +00:00
|
|
|
* Mitch Stoltz <mstoltz@netsape.com>
|
2000-01-11 20:49:15 +00:00
|
|
|
* Pierre Phaneuf <pp@ludusdesign.com>
|
1999-06-01 21:08:32 +00:00
|
|
|
*/
|
|
|
|
#include <string.h>
|
2000-01-24 21:28:28 +00:00
|
|
|
#include "nsILocalFile.h"
|
2000-01-29 00:03:57 +00:00
|
|
|
#include "nsJARInputStream.h"
|
|
|
|
#include "nsJAR.h"
|
|
|
|
#include "nsXPIDLString.h"
|
2000-03-21 04:21:28 +00:00
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "plbase64.h"
|
2000-04-26 03:50:07 +00:00
|
|
|
#include "nsIConsoleService.h"
|
2001-02-23 00:15:04 +00:00
|
|
|
#include "nscore.h"
|
2000-03-21 04:21:28 +00:00
|
|
|
|
2000-02-21 20:19:16 +00:00
|
|
|
#ifdef XP_UNIX
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#elif defined (XP_PC)
|
|
|
|
#include <io.h>
|
|
|
|
#endif
|
|
|
|
|
2000-02-14 01:57:01 +00:00
|
|
|
//----------------------------------------------
|
|
|
|
// Errors and other utility definitions
|
|
|
|
//----------------------------------------------
|
1999-11-12 06:13:13 +00:00
|
|
|
#ifndef __gen_nsIFile_h__
|
2000-03-28 03:38:06 +00:00
|
|
|
#define NS_ERROR_FILE_UNRECOGNIZED_PATH NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_FILES, 1)
|
1999-11-12 06:13:13 +00:00
|
|
|
#define NS_ERROR_FILE_UNRESOLVABLE_SYMLINK NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_FILES, 2)
|
|
|
|
#define NS_ERROR_FILE_EXECUTION_FAILED NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_FILES, 3)
|
|
|
|
#define NS_ERROR_FILE_UNKNOWN_TYPE NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_FILES, 4)
|
|
|
|
#define NS_ERROR_FILE_DESTINATION_NOT_DIR NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_FILES, 5)
|
|
|
|
#define NS_ERROR_FILE_TARGET_DOES_NOT_EXIST NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_FILES, 6)
|
|
|
|
#define NS_ERROR_FILE_COPY_OR_MOVE_FAILED NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_FILES, 7)
|
|
|
|
#define NS_ERROR_FILE_ALREADY_EXISTS NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_FILES, 8)
|
|
|
|
#define NS_ERROR_FILE_INVALID_PATH NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_FILES, 9)
|
|
|
|
#define NS_ERROR_FILE_DISK_FULL NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_FILES, 10)
|
|
|
|
#define NS_ERROR_FILE_CORRUPTED NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_FILES, 11)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static nsresult
|
|
|
|
ziperr2nsresult(PRInt32 ziperr)
|
|
|
|
{
|
|
|
|
switch (ziperr) {
|
|
|
|
case ZIP_OK: return NS_OK;
|
|
|
|
case ZIP_ERR_MEMORY: return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
case ZIP_ERR_DISK: return NS_ERROR_FILE_DISK_FULL;
|
|
|
|
case ZIP_ERR_CORRUPT: return NS_ERROR_FILE_CORRUPTED;
|
|
|
|
case ZIP_ERR_PARAM: return NS_ERROR_ILLEGAL_VALUE;
|
|
|
|
case ZIP_ERR_FNF: return NS_ERROR_FILE_TARGET_DOES_NOT_EXIST;
|
|
|
|
case ZIP_ERR_UNSUPPORTED: return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
default: return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
1999-06-01 21:08:32 +00:00
|
|
|
|
2000-01-29 00:03:57 +00:00
|
|
|
//-- PR_Free doesn't null the pointer.
|
|
|
|
// This macro takes care of that.
|
|
|
|
#define JAR_NULLFREE(_ptr) \
|
|
|
|
{ \
|
|
|
|
PR_FREEIF(_ptr); \
|
2000-02-02 01:58:37 +00:00
|
|
|
_ptr = nsnull; \
|
2000-01-29 00:03:57 +00:00
|
|
|
}
|
|
|
|
|
2000-02-14 01:57:01 +00: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 04:21:28 +00:00
|
|
|
JAR_INTERNAL = 2,
|
|
|
|
JAR_EXTERNAL = 3
|
2000-02-14 01:57:01 +00:00
|
|
|
} JARManifestItemType;
|
|
|
|
|
|
|
|
class nsJARManifestItem
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
JARManifestItemType mType;
|
|
|
|
|
2000-03-21 04:21:28 +00:00
|
|
|
// True if the second step of verification (VerifyEntry)
|
2000-02-14 01:57:01 +00:00
|
|
|
// has taken place:
|
2000-07-12 03:10:33 +00:00
|
|
|
PRBool entryVerified;
|
2000-02-14 01:57:01 +00:00
|
|
|
|
2000-03-21 04:21:28 +00:00
|
|
|
// Not signed, valid, or failure code
|
|
|
|
PRInt16 status;
|
2000-02-14 01:57:01 +00:00
|
|
|
|
|
|
|
// Internal storage of digests
|
|
|
|
char* calculatedSectionDigest;
|
|
|
|
char* storedEntryDigest;
|
|
|
|
|
|
|
|
nsJARManifestItem();
|
|
|
|
virtual ~nsJARManifestItem();
|
|
|
|
};
|
|
|
|
|
|
|
|
//-------------------------------------------------
|
|
|
|
// nsJARManifestItem constructors and destructor
|
|
|
|
//-------------------------------------------------
|
|
|
|
nsJARManifestItem::nsJARManifestItem(): mType(JAR_INTERNAL),
|
2000-07-12 03:10:33 +00:00
|
|
|
entryVerified(PR_FALSE),
|
2001-02-23 00:15:04 +00:00
|
|
|
status(nsIJAR::NOT_SIGNED),
|
2000-02-14 01:57:01 +00:00
|
|
|
calculatedSectionDigest(nsnull),
|
|
|
|
storedEntryDigest(nsnull)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsJARManifestItem::~nsJARManifestItem()
|
|
|
|
{
|
|
|
|
// Delete digests if necessary
|
|
|
|
PR_FREEIF(calculatedSectionDigest);
|
|
|
|
PR_FREEIF(storedEntryDigest);
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------
|
|
|
|
// nsJAR constructor/destructor
|
|
|
|
//----------------------------------------------
|
2000-01-29 00:03:57 +00:00
|
|
|
PR_STATIC_CALLBACK(PRBool)
|
|
|
|
DeleteManifestEntry(nsHashKey* aKey, void* aData, void* closure)
|
1999-06-01 21:08:32 +00:00
|
|
|
{
|
2000-02-14 01:57:01 +00:00
|
|
|
//-- deletes an entry in mManifestData.
|
2000-01-29 00:03:57 +00:00
|
|
|
PR_FREEIF(aData);
|
|
|
|
return PR_TRUE;
|
1999-06-01 21:08:32 +00:00
|
|
|
}
|
|
|
|
|
2000-04-26 03:50:07 +00:00
|
|
|
// The following initialization makes a guess of 10 entries per jarfile.
|
|
|
|
nsJAR::nsJAR(): mManifestData(nsnull, nsnull, DeleteManifestEntry, nsnull, 10),
|
2001-02-23 00:15:04 +00:00
|
|
|
mParsedManifest(PR_FALSE), mGlobalStatus(nsIJAR::NOT_SIGNED),
|
2000-10-31 22:44:20 +00:00
|
|
|
mReleaseTime(PR_INTERVAL_NO_TIMEOUT), mCache(nsnull), mLock(nsnull)
|
1999-11-12 06:13:13 +00:00
|
|
|
{
|
|
|
|
NS_INIT_REFCNT();
|
|
|
|
}
|
1999-06-01 21:08:32 +00:00
|
|
|
|
|
|
|
nsJAR::~nsJAR()
|
2000-06-07 09:05:22 +00:00
|
|
|
{
|
|
|
|
Close();
|
2000-09-15 21:56:20 +00:00
|
|
|
if (mLock)
|
|
|
|
PR_DestroyLock(mLock);
|
1999-06-01 21:08:32 +00:00
|
|
|
}
|
|
|
|
|
2001-02-23 00:15:04 +00:00
|
|
|
NS_IMPL_THREADSAFE_QUERY_INTERFACE2(nsJAR, nsIZipReader, nsIJAR)
|
2000-08-23 03:18:53 +00:00
|
|
|
NS_IMPL_THREADSAFE_ADDREF(nsJAR)
|
|
|
|
|
|
|
|
// Custom Release method works with nsZipReaderCache...
|
|
|
|
nsrefcnt nsJAR::Release(void)
|
|
|
|
{
|
|
|
|
nsrefcnt count;
|
|
|
|
NS_PRECONDITION(0 != mRefCnt, "dup release");
|
|
|
|
count = PR_AtomicDecrement((PRInt32 *)&mRefCnt);
|
|
|
|
NS_LOG_RELEASE(this, count, "nsJAR");
|
|
|
|
if (0 == count) {
|
|
|
|
mRefCnt = 1; /* stabilize */
|
|
|
|
/* enable this to find non-threadsafe destructors: */
|
|
|
|
/* NS_ASSERT_OWNINGTHREAD(_class); */
|
|
|
|
NS_DELETEXPCOM(this);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if (1 == count && mCache) {
|
|
|
|
nsresult rv = mCache->ReleaseZip(this);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "failed to release zip file");
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
1999-06-01 21:08:32 +00:00
|
|
|
|
2000-02-14 01:57:01 +00:00
|
|
|
//----------------------------------------------
|
2001-02-23 00:15:04 +00:00
|
|
|
// nsIZipReader implementation
|
2000-02-14 01:57:01 +00:00
|
|
|
//----------------------------------------------
|
2000-04-12 07:58:24 +00:00
|
|
|
|
|
|
|
NS_METHOD
|
|
|
|
nsJAR::Create(nsISupports *aOuter, REFNSIID aIID, void **aResult)
|
|
|
|
{
|
|
|
|
if (aOuter)
|
|
|
|
return NS_ERROR_NO_AGGREGATION;
|
|
|
|
|
|
|
|
nsJAR* jar = new nsJAR();
|
|
|
|
if (jar == nsnull)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
NS_ADDREF(jar);
|
|
|
|
nsresult rv = jar->QueryInterface(aIID, aResult);
|
|
|
|
NS_RELEASE(jar);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
1999-06-01 21:08:32 +00:00
|
|
|
NS_IMETHODIMP
|
2000-01-24 21:28:28 +00:00
|
|
|
nsJAR::Init(nsIFile* zipFile)
|
1999-06-01 21:08:32 +00:00
|
|
|
{
|
1999-11-12 06:13:13 +00:00
|
|
|
mZipFile = zipFile;
|
2000-09-15 21:56:20 +00:00
|
|
|
mLock = PR_NewLock();
|
|
|
|
return mLock ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
|
1999-06-01 21:08:32 +00:00
|
|
|
}
|
|
|
|
|
2000-04-12 07:58:24 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsJAR::GetFile(nsIFile* *result)
|
|
|
|
{
|
|
|
|
*result = mZipFile;
|
|
|
|
NS_ADDREF(*result);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-06-01 21:08:32 +00:00
|
|
|
NS_IMETHODIMP
|
1999-11-12 06:13:13 +00:00
|
|
|
nsJAR::Open()
|
|
|
|
{
|
2000-01-24 21:28:28 +00:00
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsILocalFile> localFile = do_QueryInterface(mZipFile, &rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
PRFileDesc* fd;
|
|
|
|
rv = localFile->OpenNSPRFileDesc(PR_RDONLY, 0664, &fd);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
PRInt32 err = mZip.OpenArchiveWithFileDesc(fd);
|
2000-01-25 20:41:47 +00:00
|
|
|
|
1999-11-12 06:13:13 +00:00
|
|
|
return ziperr2nsresult(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsJAR::Close()
|
|
|
|
{
|
|
|
|
PRInt32 err = mZip.CloseArchive();
|
|
|
|
return ziperr2nsresult(err);
|
|
|
|
}
|
|
|
|
|
2000-12-27 07:05:55 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsJAR::Test(const char *aEntryName)
|
|
|
|
{
|
|
|
|
PRInt32 err = mZip.Test(aEntryName);
|
|
|
|
return ziperr2nsresult(err);
|
|
|
|
}
|
|
|
|
|
1999-11-12 06:13:13 +00:00
|
|
|
NS_IMETHODIMP
|
2000-01-24 21:28:28 +00:00
|
|
|
nsJAR::Extract(const char *zipEntry, nsIFile* outFile)
|
1999-06-01 21:08:32 +00:00
|
|
|
{
|
2000-09-15 21:56:20 +00:00
|
|
|
// nsZipArchive and zlib are not thread safe
|
|
|
|
// we need to use a lock to prevent bug #51267
|
|
|
|
nsAutoLock lock(mLock);
|
|
|
|
|
2000-01-24 21:28:28 +00:00
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsILocalFile> localFile = do_QueryInterface(outFile, &rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
PRFileDesc* fd;
|
2000-01-25 20:41:47 +00:00
|
|
|
rv = localFile->OpenNSPRFileDesc(PR_RDWR | PR_CREATE_FILE, 0664, &fd);
|
2001-03-14 05:15:55 +00:00
|
|
|
if (NS_FAILED(rv)) return NS_ERROR_FILE_ACCESS_DENIED;
|
2000-01-24 21:28:28 +00:00
|
|
|
|
2000-03-28 03:38:06 +00:00
|
|
|
nsZipItem *item = 0;
|
|
|
|
PRInt32 err = mZip.ExtractFileToFileDesc(zipEntry, fd, &item);
|
|
|
|
PR_Close(fd);
|
2000-02-21 20:19:16 +00:00
|
|
|
|
|
|
|
if (err != ZIP_OK)
|
|
|
|
outFile->Delete(PR_FALSE);
|
|
|
|
else
|
|
|
|
{
|
2000-03-28 03:38:06 +00:00
|
|
|
#if defined(XP_UNIX)
|
2000-02-21 20:19:16 +00:00
|
|
|
char *path;
|
|
|
|
|
|
|
|
rv = outFile->GetPath(&path);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
2000-03-28 03:38:06 +00:00
|
|
|
chmod(path, item->mode);
|
2000-02-21 20:19:16 +00:00
|
|
|
#endif
|
|
|
|
|
2000-03-28 03:38:06 +00:00
|
|
|
RestoreModTime(item, outFile); // non-fatal if this fails, ignore errors
|
|
|
|
}
|
|
|
|
|
1999-11-12 06:13:13 +00:00
|
|
|
return ziperr2nsresult(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsJAR::GetEntry(const char *zipEntry, nsIZipEntry* *result)
|
|
|
|
{
|
2000-01-29 00:03:57 +00:00
|
|
|
nsZipItem* zipItem;
|
|
|
|
PRInt32 err = mZip.GetItem(zipEntry, &zipItem);
|
|
|
|
if (err != ZIP_OK) return ziperr2nsresult(err);
|
1999-11-12 06:13:13 +00:00
|
|
|
|
|
|
|
nsJARItem* jarItem = new nsJARItem();
|
|
|
|
if (jarItem == nsnull)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
NS_ADDREF(jarItem);
|
2000-01-29 00:03:57 +00:00
|
|
|
jarItem->Init(zipItem);
|
1999-11-12 06:13:13 +00:00
|
|
|
*result = jarItem;
|
1999-06-01 21:08:32 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-06-23 06:16:28 +00:00
|
|
|
NS_IMETHODIMP
|
2000-02-14 01:57:01 +00:00
|
|
|
nsJAR::FindEntries(const char *aPattern, nsISimpleEnumerator **result)
|
1999-06-23 06:16:28 +00:00
|
|
|
{
|
2000-02-14 01:57:01 +00:00
|
|
|
if (!result)
|
1999-11-12 06:13:13 +00:00
|
|
|
return NS_ERROR_INVALID_POINTER;
|
1999-06-23 06:16:28 +00:00
|
|
|
|
1999-11-12 06:13:13 +00:00
|
|
|
nsZipFind *find = mZip.FindInit(aPattern);
|
|
|
|
if (!find)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
1999-06-23 06:16:28 +00:00
|
|
|
|
1999-11-12 06:13:13 +00:00
|
|
|
nsISimpleEnumerator *zipEnum = new nsJAREnumerator(find);
|
|
|
|
if (!zipEnum)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
NS_ADDREF( zipEnum );
|
1999-06-23 06:16:28 +00:00
|
|
|
|
2000-02-14 01:57:01 +00:00
|
|
|
*result = zipEnum;
|
1999-11-12 06:13:13 +00:00
|
|
|
return NS_OK;
|
1999-06-23 06:16:28 +00:00
|
|
|
}
|
|
|
|
|
1999-10-26 19:43:26 +00:00
|
|
|
NS_IMETHODIMP
|
2000-07-12 03:10:33 +00:00
|
|
|
nsJAR::GetInputStream(const char* aFilename, nsIInputStream** result)
|
1999-10-26 19:43:26 +00:00
|
|
|
{
|
2000-09-15 21:56:20 +00:00
|
|
|
// nsZipArchive and zlib are not thread safe
|
|
|
|
// we need to use a lock to prevent bug #51267
|
|
|
|
nsAutoLock lock(mLock);
|
|
|
|
|
2000-07-12 03:10:33 +00:00
|
|
|
NS_ENSURE_ARG_POINTER(result);
|
|
|
|
nsresult rv;
|
|
|
|
nsJARInputStream* jis = nsnull;
|
|
|
|
rv = nsJARInputStream::Create(nsnull, NS_GET_IID(nsIInputStream), (void**)&jis);
|
|
|
|
if (!jis) return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
rv = jis->Init(this, aFilename);
|
|
|
|
if (NS_FAILED(rv)) return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
*result = (nsIInputStream*)jis;
|
|
|
|
return NS_OK;
|
2000-01-29 00:03:57 +00:00
|
|
|
}
|
|
|
|
|
2001-02-23 00:15:04 +00:00
|
|
|
//----------------------------------------------
|
|
|
|
// nsIJAR implementation
|
|
|
|
//----------------------------------------------
|
|
|
|
|
2000-04-26 03:50:07 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsJAR::GetCertificatePrincipal(const char* aFilename, nsIPrincipal** aPrincipal)
|
2000-01-29 00:03:57 +00:00
|
|
|
{
|
2000-02-14 01:57:01 +00:00
|
|
|
//-- Parameter check
|
2000-03-21 04:21:28 +00:00
|
|
|
if (!aPrincipal)
|
2000-01-29 00:03:57 +00:00
|
|
|
return NS_ERROR_NULL_POINTER;
|
2000-03-21 04:21:28 +00:00
|
|
|
*aPrincipal = nsnull;
|
2000-01-29 00:03:57 +00:00
|
|
|
|
2000-07-12 03:10:33 +00:00
|
|
|
//-- Get the signature verifier service
|
|
|
|
nsresult rv;
|
2000-09-13 23:57:52 +00:00
|
|
|
NS_WITH_SERVICE(nsISignatureVerifier, verifier, SIGNATURE_VERIFIER_CONTRACTID, &rv);
|
2000-07-12 03:10:33 +00:00
|
|
|
if (NS_FAILED(rv)) // No signature verifier available
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
//-- Parse the manifest
|
|
|
|
rv = ParseManifest(verifier);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2001-02-23 00:15:04 +00:00
|
|
|
if (mGlobalStatus == nsIJAR::NO_MANIFEST)
|
2000-07-12 03:10:33 +00:00
|
|
|
return NS_OK;
|
|
|
|
|
2000-04-26 03:50:07 +00:00
|
|
|
PRInt16 requestedStatus;
|
|
|
|
if (aFilename)
|
2000-01-29 00:03:57 +00:00
|
|
|
{
|
2000-04-26 03:50:07 +00:00
|
|
|
//-- Find the item
|
2000-08-10 06:19:37 +00:00
|
|
|
nsCStringKey key(aFilename);
|
2001-02-23 00:15:04 +00:00
|
|
|
nsJARManifestItem* manItem = NS_STATIC_CAST(nsJARManifestItem*, mManifestData.Get(&key));
|
2000-04-26 03:50:07 +00:00
|
|
|
if (!manItem)
|
|
|
|
return NS_OK;
|
2000-07-12 03:10:33 +00:00
|
|
|
//-- Verify the item against the manifest
|
|
|
|
if (!manItem->entryVerified)
|
2000-04-26 03:50:07 +00:00
|
|
|
{
|
2000-07-12 03:10:33 +00:00
|
|
|
nsXPIDLCString entryData;
|
|
|
|
PRUint32 entryDataLen;
|
|
|
|
rv = LoadEntry(aFilename, getter_Copies(entryData), &entryDataLen);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
rv = VerifyEntry(verifier, manItem, entryData, entryDataLen);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2000-04-26 03:50:07 +00:00
|
|
|
}
|
|
|
|
requestedStatus = manItem->status;
|
2000-01-29 00:03:57 +00:00
|
|
|
}
|
2000-04-26 03:50:07 +00:00
|
|
|
else // User wants identity of signer w/o verifying any entries
|
|
|
|
requestedStatus = mGlobalStatus;
|
2000-01-29 00:03:57 +00:00
|
|
|
|
2001-02-23 00:15:04 +00:00
|
|
|
if (requestedStatus != nsIJAR::VALID)
|
2000-04-26 03:50:07 +00:00
|
|
|
ReportError(aFilename, requestedStatus);
|
|
|
|
else // Valid signature
|
2000-01-29 00:03:57 +00:00
|
|
|
{
|
2000-04-26 03:50:07 +00:00
|
|
|
*aPrincipal = mPrincipal;
|
2000-03-21 04:21:28 +00:00
|
|
|
NS_IF_ADDREF(*aPrincipal);
|
2000-02-14 01:57:01 +00:00
|
|
|
}
|
2000-03-21 04:21:28 +00:00
|
|
|
return NS_OK;
|
2000-05-10 01:49:33 +00:00
|
|
|
}
|
|
|
|
|
2000-01-29 00:03:57 +00:00
|
|
|
//----------------------------------------------
|
|
|
|
// nsJAR private implementation
|
|
|
|
//----------------------------------------------
|
2000-02-14 01:57:01 +00:00
|
|
|
nsresult
|
2000-03-30 08:09:45 +00:00
|
|
|
nsJAR::LoadEntry(const char* aFilename, char** aBuf, PRUint32* aBufLen)
|
2000-01-29 00:03:57 +00:00
|
|
|
{
|
2000-07-12 03:10:33 +00:00
|
|
|
//-- Get a stream for reading the file
|
2000-01-29 00:03:57 +00:00
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIInputStream> manifestStream;
|
2000-07-12 03:10:33 +00:00
|
|
|
rv = GetInputStream(aFilename, getter_AddRefs(manifestStream));
|
2000-01-29 00:03:57 +00:00
|
|
|
if (NS_FAILED(rv)) return NS_ERROR_FILE_TARGET_DOES_NOT_EXIST;
|
|
|
|
|
|
|
|
//-- Read the manifest file into memory
|
|
|
|
char* buf;
|
|
|
|
PRUint32 len;
|
|
|
|
rv = manifestStream->Available(&len);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2000-02-14 01:57:01 +00:00
|
|
|
buf = (char*)PR_MALLOC(len+1);
|
2000-01-29 00:03:57 +00:00
|
|
|
if (!buf) return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
PRUint32 bytesRead;
|
|
|
|
rv = manifestStream->Read(buf, len, &bytesRead);
|
|
|
|
if (bytesRead != len)
|
|
|
|
rv = NS_ERROR_FILE_CORRUPTED;
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2000-02-14 01:57:01 +00:00
|
|
|
buf[len] = '\0'; //Null-terminate the buffer
|
2000-01-29 00:03:57 +00:00
|
|
|
*aBuf = buf;
|
|
|
|
if (aBufLen)
|
|
|
|
*aBufLen = len;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-02-14 01:57:01 +00:00
|
|
|
PRInt32
|
|
|
|
nsJAR::ReadLine(const char** src)
|
2000-01-29 00:03:57 +00:00
|
|
|
{
|
|
|
|
//--Moves pointer to beginning of next line and returns line length
|
|
|
|
// not including CR/LF.
|
|
|
|
PRInt32 length;
|
|
|
|
char* eol = PL_strpbrk(*src, "\r\n");
|
|
|
|
|
|
|
|
if (eol == nsnull) // Probably reached end of file before newline
|
|
|
|
{
|
|
|
|
length = PL_strlen(*src);
|
|
|
|
if (length == 0) // immediate end-of-file
|
|
|
|
*src = nsnull;
|
|
|
|
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 03:50:07 +00: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
|
2000-05-10 01:49:33 +00:00
|
|
|
nsJAR::ParseManifest(nsISignatureVerifier* verifier)
|
2000-04-26 03:50:07 +00:00
|
|
|
{
|
|
|
|
//-- Verification Step 1
|
|
|
|
if (mParsedManifest)
|
|
|
|
return NS_OK;
|
|
|
|
//-- (1)Manifest (MF) file
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsISimpleEnumerator> files;
|
|
|
|
rv = FindEntries(JAR_MF_SEARCH_STRING, getter_AddRefs(files));
|
|
|
|
if (!files) rv = NS_ERROR_FAILURE;
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
//-- Load the file into memory
|
|
|
|
nsCOMPtr<nsJARItem> file;
|
|
|
|
rv = files->GetNext(getter_AddRefs(file));
|
2000-07-12 03:10:33 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
if (!file)
|
|
|
|
{
|
2001-02-23 00:15:04 +00:00
|
|
|
mGlobalStatus = nsIJAR::NO_MANIFEST;
|
2000-07-12 03:10:33 +00:00
|
|
|
mParsedManifest = PR_TRUE;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2000-04-26 03:50:07 +00:00
|
|
|
PRBool more;
|
|
|
|
rv = files->HasMoreElements(&more);
|
2000-07-12 03:10:33 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
if (more)
|
|
|
|
{
|
|
|
|
mParsedManifest = PR_TRUE;
|
|
|
|
return NS_ERROR_FILE_CORRUPTED; // More than one MF file
|
|
|
|
}
|
2000-04-26 03:50:07 +00:00
|
|
|
nsXPIDLCString manifestFilename;
|
|
|
|
rv = file->GetName(getter_Copies(manifestFilename));
|
|
|
|
if (!manifestFilename || NS_FAILED(rv)) return rv;
|
|
|
|
nsXPIDLCString manifestBuffer;
|
|
|
|
rv = LoadEntry(manifestFilename, getter_Copies(manifestBuffer));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
//-- Parse it
|
2000-05-10 01:49:33 +00:00
|
|
|
rv = ParseOneFile(verifier, manifestBuffer, JAR_MF);
|
2000-04-26 03:50:07 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
//-- (2)Signature (SF) file
|
|
|
|
// If there are multiple signatures, we select one.
|
|
|
|
rv = FindEntries(JAR_SF_SEARCH_STRING, getter_AddRefs(files));
|
|
|
|
if (!files) rv = NS_ERROR_FAILURE;
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
//-- Get an SF file
|
|
|
|
rv = files->GetNext(getter_AddRefs(file));
|
2000-07-12 03:10:33 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
if (!file)
|
|
|
|
{
|
2001-02-23 00:15:04 +00:00
|
|
|
mGlobalStatus = nsIJAR::NO_MANIFEST;
|
2000-07-12 03:10:33 +00:00
|
|
|
mParsedManifest = PR_TRUE;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2000-04-26 03:50:07 +00:00
|
|
|
rv = file->GetName(getter_Copies(manifestFilename));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
PRUint32 manifestLen;
|
|
|
|
rv = LoadEntry(manifestFilename, getter_Copies(manifestBuffer), &manifestLen);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
//-- Get its corresponding signature file
|
2000-05-13 00:22:38 +00:00
|
|
|
nsCAutoString sigFilename( NS_STATIC_CAST(const char*, manifestFilename) );
|
2000-04-26 03:50:07 +00:00
|
|
|
PRInt32 extension = sigFilename.RFindChar('.') + 1;
|
|
|
|
NS_ASSERTION(extension != 0, "Manifest Parser: Missing file extension.");
|
|
|
|
(void)sigFilename.Cut(extension, 2);
|
|
|
|
nsXPIDLCString sigBuffer;
|
|
|
|
PRUint32 sigLen;
|
2000-05-12 07:53:02 +00:00
|
|
|
{
|
|
|
|
nsCAutoString tempFilename(sigFilename); tempFilename.Append("rsa", 3);
|
|
|
|
rv = LoadEntry(tempFilename, getter_Copies(sigBuffer), &sigLen);
|
|
|
|
}
|
2000-04-26 03:50:07 +00:00
|
|
|
if (NS_FAILED(rv))
|
2000-05-12 07:53:02 +00:00
|
|
|
{
|
|
|
|
nsCAutoString tempFilename(sigFilename); tempFilename.Append("RSA", 3);
|
|
|
|
rv = LoadEntry(tempFilename, getter_Copies(sigBuffer), &sigLen);
|
|
|
|
}
|
2000-07-12 03:10:33 +00:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
{
|
2001-02-23 00:15:04 +00:00
|
|
|
mGlobalStatus = nsIJAR::NO_MANIFEST;
|
2000-07-12 03:10:33 +00:00
|
|
|
mParsedManifest = PR_TRUE;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2000-04-26 03:50:07 +00:00
|
|
|
|
|
|
|
//-- Verify that the signature file is a valid signature of the SF file
|
2000-05-10 01:49:33 +00:00
|
|
|
PRInt32 verifyError;
|
|
|
|
rv = verifier->VerifySignature(sigBuffer, sigLen, manifestBuffer, manifestLen,
|
|
|
|
&verifyError, getter_AddRefs(mPrincipal));
|
2000-04-26 03:50:07 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2000-05-10 01:49:33 +00:00
|
|
|
if (mPrincipal)
|
2001-02-23 00:15:04 +00:00
|
|
|
mGlobalStatus = nsIJAR::VALID;
|
2000-05-10 01:49:33 +00:00
|
|
|
else if (verifyError == nsISignatureVerifier::VERIFY_ERROR_UNKNOWN_CA)
|
2001-02-23 00:15:04 +00:00
|
|
|
mGlobalStatus = nsIJAR::INVALID_UNKNOWN_CA;
|
2000-05-10 01:49:33 +00:00
|
|
|
else
|
2001-02-23 00:15:04 +00:00
|
|
|
mGlobalStatus = nsIJAR::INVALID_SIG;
|
2000-05-10 01:49:33 +00:00
|
|
|
|
2000-04-26 03:50:07 +00: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.
|
2000-05-10 01:49:33 +00:00
|
|
|
ParseOneFile(verifier, manifestBuffer, JAR_SF);
|
2000-07-12 03:10:33 +00:00
|
|
|
mParsedManifest = PR_TRUE;
|
2000-04-26 03:50:07 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2000-02-14 01:57:01 +00:00
|
|
|
nsresult
|
2000-05-10 01:49:33 +00:00
|
|
|
nsJAR::ParseOneFile(nsISignatureVerifier* verifier,
|
|
|
|
const char* filebuf, PRInt16 aFileType)
|
2000-01-29 00:03:57 +00:00
|
|
|
{
|
2000-03-21 04:21:28 +00:00
|
|
|
//-- Check file header
|
2000-01-29 00:03:57 +00:00
|
|
|
const char* nextLineStart = filebuf;
|
|
|
|
nsCAutoString curLine;
|
|
|
|
PRInt32 linelen;
|
|
|
|
linelen = ReadLine(&nextLineStart);
|
2000-03-21 04:21:28 +00:00
|
|
|
curLine.Assign(filebuf, linelen);
|
2000-09-03 05:54:31 +00:00
|
|
|
|
|
|
|
if ( ((aFileType == JAR_MF) && !curLine.Equals(JAR_MF_HEADER) ) ||
|
|
|
|
((aFileType == JAR_SF) && !curLine.Equals(JAR_SF_HEADER) ) )
|
2000-01-29 00:03:57 +00:00
|
|
|
return NS_ERROR_FILE_CORRUPTED;
|
|
|
|
|
2000-03-21 04:21:28 +00:00
|
|
|
//-- Skip header section
|
|
|
|
do {
|
|
|
|
linelen = ReadLine(&nextLineStart);
|
|
|
|
} while (linelen > 0);
|
|
|
|
|
|
|
|
//-- Set up parsing variables
|
|
|
|
const char* curPos;
|
|
|
|
const char* sectionStart = nextLineStart;
|
|
|
|
|
|
|
|
nsJARManifestItem* curItemMF;
|
|
|
|
PRBool foundName = PR_FALSE;
|
|
|
|
if (aFileType == JAR_MF)
|
|
|
|
curItemMF = new nsJARManifestItem();
|
|
|
|
nsCAutoString curItemName;
|
|
|
|
nsCAutoString storedSectionDigest;
|
|
|
|
|
2000-01-29 00:03:57 +00: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)
|
|
|
|
{
|
2000-03-21 04:21:28 +00:00
|
|
|
if (curItemMF->mType != JAR_INVALID)
|
2000-01-29 00:03:57 +00:00
|
|
|
{
|
2000-02-14 01:57:01 +00:00
|
|
|
//-- Did this section have a name: line?
|
2000-01-29 00:03:57 +00:00
|
|
|
if(!foundName)
|
2000-03-21 04:21:28 +00:00
|
|
|
curItemMF->mType = JAR_INVALID;
|
2000-01-29 00:03:57 +00:00
|
|
|
else
|
|
|
|
{
|
2000-03-21 04:21:28 +00:00
|
|
|
if (curItemMF->mType == JAR_INTERNAL)
|
|
|
|
{
|
2000-01-29 00:03:57 +00:00
|
|
|
//-- If it's an internal item, it must correspond
|
|
|
|
// to a valid jar entry
|
|
|
|
nsIZipEntry* entry;
|
|
|
|
PRInt32 result = GetEntry(curItemName, &entry);
|
|
|
|
if (result != ZIP_OK || !entry)
|
2000-03-21 04:21:28 +00:00
|
|
|
curItemMF->mType = JAR_INVALID;
|
2000-01-29 00:03:57 +00:00
|
|
|
}
|
|
|
|
//-- Check for duplicates
|
2000-08-10 06:19:37 +00:00
|
|
|
nsCStringKey key(curItemName);
|
2000-01-29 00:03:57 +00:00
|
|
|
if (mManifestData.Exists(&key))
|
2000-03-21 04:21:28 +00:00
|
|
|
curItemMF->mType = JAR_INVALID;
|
2000-01-29 00:03:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-03-21 04:21:28 +00:00
|
|
|
if (curItemMF->mType == JAR_INVALID)
|
|
|
|
delete curItemMF;
|
2000-02-14 01:57:01 +00:00
|
|
|
else //-- calculate section digest
|
2000-01-29 00:03:57 +00:00
|
|
|
{
|
|
|
|
PRUint32 sectionLength = curPos - sectionStart;
|
2000-05-10 01:49:33 +00:00
|
|
|
CalculateDigest(verifier, sectionStart, sectionLength,
|
2000-03-21 04:21:28 +00:00
|
|
|
&(curItemMF->calculatedSectionDigest));
|
2000-01-29 00:03:57 +00:00
|
|
|
//-- Save item in the hashtable
|
2000-08-10 06:19:37 +00:00
|
|
|
nsCStringKey itemKey(curItemName);
|
2000-03-21 04:21:28 +00:00
|
|
|
mManifestData.Put(&itemKey, (void*)curItemMF);
|
2000-01-29 00:03:57 +00:00
|
|
|
}
|
|
|
|
if (nextLineStart == nsnull) // end-of-file
|
|
|
|
break;
|
|
|
|
|
|
|
|
sectionStart = nextLineStart;
|
2000-03-21 04:21:28 +00:00
|
|
|
curItemMF = new nsJARManifestItem();
|
2000-01-29 00:03:57 +00:00
|
|
|
} // (aFileType == JAR_MF)
|
|
|
|
else
|
|
|
|
//-- file type is SF, compare digest with calculated
|
|
|
|
// section digests from MF file.
|
|
|
|
{
|
|
|
|
if (foundName)
|
|
|
|
{
|
2000-03-21 04:21:28 +00:00
|
|
|
nsJARManifestItem* curItemSF;
|
2000-08-10 06:19:37 +00:00
|
|
|
nsCStringKey key(curItemName);
|
2000-03-21 04:21:28 +00:00
|
|
|
curItemSF = (nsJARManifestItem*)mManifestData.Get(&key);
|
|
|
|
if(curItemSF)
|
2000-01-29 00:03:57 +00:00
|
|
|
{
|
2000-03-21 04:21:28 +00:00
|
|
|
NS_ASSERTION(curItemSF->status == nsJAR::NOT_SIGNED,
|
|
|
|
"SECURITY ERROR: nsJARManifestItem not correctly initialized");
|
2000-04-26 03:50:07 +00:00
|
|
|
curItemSF->status = mGlobalStatus;
|
2001-02-23 00:15:04 +00:00
|
|
|
if (curItemSF->status == nsIJAR::VALID)
|
2000-03-21 04:21:28 +00:00
|
|
|
{ // Compare digests
|
|
|
|
if (storedSectionDigest.Length() == 0)
|
2001-02-23 00:15:04 +00:00
|
|
|
curItemSF->status = nsIJAR::NOT_SIGNED;
|
2000-03-21 04:21:28 +00:00
|
|
|
else
|
2000-01-29 00:03:57 +00:00
|
|
|
{
|
2000-09-03 05:54:31 +00:00
|
|
|
if (!storedSectionDigest.Equals((const char*)curItemSF->calculatedSectionDigest))
|
2001-02-23 00:15:04 +00:00
|
|
|
curItemSF->status = nsIJAR::INVALID_MANIFEST;
|
2000-03-21 04:21:28 +00:00
|
|
|
JAR_NULLFREE(curItemSF->calculatedSectionDigest)
|
2000-02-14 01:57:01 +00:00
|
|
|
storedSectionDigest = "";
|
2000-01-29 00:03:57 +00:00
|
|
|
}
|
2000-02-14 01:57:01 +00:00
|
|
|
} // (aPrincipal != nsnull)
|
2000-03-21 04:21:28 +00:00
|
|
|
} // if(curItemSF)
|
2000-01-29 00:03:57 +00:00
|
|
|
} // if(foundName)
|
|
|
|
|
|
|
|
if(nextLineStart == nsnull) // end-of-file
|
|
|
|
break;
|
|
|
|
} // aFileType == JAR_SF
|
|
|
|
foundName = PR_FALSE;
|
|
|
|
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;
|
|
|
|
PRInt32 continuationLen = ReadLine(&nextLineStart) - 1;
|
2000-02-14 01:57:01 +00:00
|
|
|
nsCAutoString continuation(curPos+1, continuationLen);
|
2000-01-29 00:03:57 +00:00
|
|
|
curLine += continuation;
|
|
|
|
linelen += continuationLen;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-- Find colon in current line, this separates name from value
|
|
|
|
PRInt32 colonPos = curLine.FindChar(':');
|
|
|
|
if (colonPos == -1) // No colon on line, ignore line
|
|
|
|
continue;
|
|
|
|
//-- Break down the line
|
|
|
|
nsCAutoString lineName;
|
|
|
|
curLine.Left(lineName, colonPos);
|
|
|
|
nsCAutoString lineData;
|
|
|
|
curLine.Mid(lineData, colonPos+2, linelen - (colonPos+2));
|
|
|
|
|
|
|
|
//-- Lines to look for:
|
2000-02-14 01:57:01 +00:00
|
|
|
// (1) Digest:
|
2000-04-27 19:49:53 +00:00
|
|
|
if (lineName.CompareWithConversion("SHA1-Digest",PR_TRUE) == 0)
|
2000-01-29 00:03:57 +00:00
|
|
|
//-- This is a digest line, save the data in the appropriate place
|
|
|
|
{
|
|
|
|
if(aFileType == JAR_MF)
|
|
|
|
{
|
2000-03-21 04:21:28 +00:00
|
|
|
curItemMF->storedEntryDigest = (char*)PR_MALLOC(lineData.Length()+1);
|
|
|
|
if (!(curItemMF->storedEntryDigest))
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
PL_strcpy(curItemMF->storedEntryDigest, lineData);
|
2000-01-29 00:03:57 +00:00
|
|
|
}
|
|
|
|
else
|
2000-02-14 01:57:01 +00:00
|
|
|
storedSectionDigest = lineData;
|
2000-01-29 00:03:57 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// (2) Name: associates this manifest section with a file in the jar.
|
2000-04-27 19:49:53 +00:00
|
|
|
if (!foundName && lineName.CompareWithConversion("Name", PR_TRUE) == 0)
|
2000-01-29 00:03:57 +00:00
|
|
|
{
|
2000-03-21 04:21:28 +00:00
|
|
|
curItemName = lineData;
|
2000-01-29 00:03:57 +00:00
|
|
|
foundName = PR_TRUE;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// (3) Magic: this may be an inline Javascript.
|
|
|
|
// We can't do any other kind of magic.
|
|
|
|
if ( aFileType == JAR_MF &&
|
2000-04-27 19:49:53 +00:00
|
|
|
lineName.CompareWithConversion("Magic", PR_TRUE) == 0)
|
2000-01-29 00:03:57 +00:00
|
|
|
{
|
2000-04-27 19:49:53 +00:00
|
|
|
if(lineData.CompareWithConversion("javascript", PR_TRUE) == 0)
|
2000-03-21 04:21:28 +00:00
|
|
|
curItemMF->mType = JAR_EXTERNAL;
|
2000-01-29 00:03:57 +00:00
|
|
|
else
|
2000-03-21 04:21:28 +00:00
|
|
|
curItemMF->mType = JAR_INVALID;
|
2000-01-29 00:03:57 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // for (;;)
|
1999-10-26 19:43:26 +00:00
|
|
|
return NS_OK;
|
2000-01-29 00:03:57 +00:00
|
|
|
} //ParseOneFile()
|
|
|
|
|
2000-02-14 01:57:01 +00:00
|
|
|
nsresult
|
2000-07-12 03:10:33 +00:00
|
|
|
nsJAR::VerifyEntry(nsISignatureVerifier* verifier,
|
|
|
|
nsJARManifestItem* aManItem, const char* aEntryData,
|
2000-02-14 01:57:01 +00:00
|
|
|
PRUint32 aLen)
|
|
|
|
{
|
2001-02-23 00:15:04 +00:00
|
|
|
if (aManItem->status == nsIJAR::VALID)
|
2000-02-14 01:57:01 +00:00
|
|
|
{
|
2000-07-12 03:10:33 +00:00
|
|
|
if(!aManItem->storedEntryDigest)
|
2000-03-21 04:21:28 +00:00
|
|
|
// No entry digests in manifest file. Entry is unsigned.
|
2001-02-23 00:15:04 +00:00
|
|
|
aManItem->status = nsIJAR::NOT_SIGNED;
|
2000-02-14 01:57:01 +00:00
|
|
|
else
|
|
|
|
{ //-- Calculate and compare digests
|
|
|
|
char* calculatedEntryDigest;
|
2000-07-12 03:10:33 +00:00
|
|
|
nsresult rv = CalculateDigest(verifier, aEntryData, aLen, &calculatedEntryDigest);
|
2000-05-10 01:49:33 +00:00
|
|
|
if (NS_FAILED(rv)) return NS_ERROR_FAILURE;
|
2000-07-12 03:10:33 +00:00
|
|
|
if (PL_strcmp(aManItem->storedEntryDigest, calculatedEntryDigest) != 0)
|
2001-02-23 00:15:04 +00:00
|
|
|
aManItem->status = nsIJAR::INVALID_ENTRY;
|
2000-02-14 01:57:01 +00:00
|
|
|
JAR_NULLFREE(calculatedEntryDigest)
|
2000-07-12 03:10:33 +00:00
|
|
|
JAR_NULLFREE(aManItem->storedEntryDigest)
|
2000-02-14 01:57:01 +00:00
|
|
|
}
|
|
|
|
}
|
2000-07-12 03:10:33 +00:00
|
|
|
aManItem->entryVerified = PR_TRUE;
|
|
|
|
return NS_OK;
|
2000-03-28 03:38:06 +00:00
|
|
|
}
|
|
|
|
|
2000-04-26 03:50:07 +00:00
|
|
|
void nsJAR::ReportError(const char* aFilename, PRInt16 errorCode)
|
|
|
|
{
|
|
|
|
//-- Generate error message
|
2000-05-10 01:49:33 +00:00
|
|
|
nsAutoString message;
|
|
|
|
message.AssignWithConversion("Signature Verification Error: the signature on ");
|
2000-04-26 03:50:07 +00:00
|
|
|
if (aFilename)
|
2000-04-27 19:49:53 +00:00
|
|
|
message.AppendWithConversion(aFilename);
|
2000-04-26 03:50:07 +00:00
|
|
|
else
|
2000-04-27 19:49:53 +00:00
|
|
|
message.AppendWithConversion("this .jar archive");
|
|
|
|
message.AppendWithConversion(" is invalid because ");
|
2000-04-26 03:50:07 +00:00
|
|
|
switch(errorCode)
|
|
|
|
{
|
2001-02-23 00:15:04 +00:00
|
|
|
case nsIJAR::NOT_SIGNED:
|
2000-04-27 19:49:53 +00:00
|
|
|
message.AppendWithConversion("the archive did not contain a valid PKCS7 signature.");
|
2000-04-26 03:50:07 +00:00
|
|
|
break;
|
2001-02-23 00:15:04 +00:00
|
|
|
case nsIJAR::INVALID_SIG:
|
2000-05-10 01:49:33 +00:00
|
|
|
message.AppendWithConversion("the digital signature (*.RSA) file is not a valid signature of the signature instruction file (*.SF).");
|
2000-04-26 03:50:07 +00:00
|
|
|
break;
|
2001-02-23 00:15:04 +00:00
|
|
|
case nsIJAR::INVALID_UNKNOWN_CA:
|
2000-04-27 19:49:53 +00:00
|
|
|
message.AppendWithConversion("the certificate used to sign this file has an unrecognized issuer.");
|
2000-04-26 03:50:07 +00:00
|
|
|
break;
|
2001-02-23 00:15:04 +00:00
|
|
|
case nsIJAR::INVALID_MANIFEST:
|
2000-05-10 01:49:33 +00:00
|
|
|
message.AppendWithConversion("the signature instruction file (*.SF) does not contain a valid hash of the MANIFEST.MF file.");
|
2000-04-26 03:50:07 +00:00
|
|
|
break;
|
2001-02-23 00:15:04 +00:00
|
|
|
case nsIJAR::INVALID_ENTRY:
|
2000-05-10 01:49:33 +00:00
|
|
|
message.AppendWithConversion("the MANIFEST.MF file does not contain a valid hash of the file being verified.");
|
2000-04-26 03:50:07 +00:00
|
|
|
break;
|
|
|
|
default:
|
2000-04-27 19:49:53 +00:00
|
|
|
message.AppendWithConversion("of an unknown problem.");
|
2000-04-26 03:50:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Report error in JS console
|
2000-09-14 23:20:49 +00:00
|
|
|
nsCOMPtr<nsIConsoleService> console(do_GetService("@mozilla.org/consoleservice;1"));
|
2000-04-26 03:50:07 +00:00
|
|
|
if (console)
|
|
|
|
{
|
|
|
|
PRUnichar* messageUni = message.ToNewUnicode();
|
|
|
|
if (!messageUni) return;
|
|
|
|
console->LogStringMessage(messageUni);
|
2000-06-03 09:46:12 +00:00
|
|
|
nsMemory::Free(messageUni);
|
2000-04-26 03:50:07 +00:00
|
|
|
}
|
2001-05-15 06:43:03 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
char* messageCstr = message.ToNewCString();
|
|
|
|
if (!messageCstr) return;
|
|
|
|
fprintf(stderr, "%s\n", messageCstr);
|
|
|
|
nsMemory::Free(messageCstr);
|
2000-05-10 01:49:33 +00:00
|
|
|
#endif
|
2000-04-26 03:50:07 +00:00
|
|
|
}
|
|
|
|
|
2000-03-28 03:38:06 +00:00
|
|
|
nsresult
|
|
|
|
nsJAR::RestoreModTime(nsZipItem *aItem, nsIFile *aExtractedFile)
|
|
|
|
{
|
|
|
|
if (!aItem || !aExtractedFile)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
char *timestr;
|
|
|
|
PRTime prtime;
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
timestr = aItem->GetModTime();
|
|
|
|
if (timestr)
|
|
|
|
{
|
|
|
|
if (PR_SUCCESS == PR_ParseTimeString(timestr, PR_FALSE, &prtime))
|
2000-05-09 19:59:06 +00:00
|
|
|
{
|
|
|
|
PRTime conversion = LL_ZERO;
|
|
|
|
PRTime newTime = LL_ZERO;
|
2000-05-16 21:53:34 +00:00
|
|
|
LL_I2L(conversion, PR_USEC_PER_MSEC);
|
2000-05-09 19:59:06 +00:00
|
|
|
LL_DIV(newTime, prtime, conversion);
|
2000-05-09 19:11:14 +00:00
|
|
|
// nsIFile needs usecs.
|
2000-05-09 19:59:06 +00:00
|
|
|
rv = aExtractedFile->SetLastModificationDate(newTime);
|
|
|
|
}
|
2000-03-28 03:38:06 +00:00
|
|
|
|
|
|
|
JAR_NULLFREE(timestr);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
2000-03-21 04:21:28 +00:00
|
|
|
}
|
|
|
|
|
2000-05-10 01:49:33 +00:00
|
|
|
nsresult nsJAR::CalculateDigest(nsISignatureVerifier* verifier,
|
|
|
|
const char* aInBuf, PRUint32 aLen,
|
|
|
|
char** digest)
|
2000-03-21 04:21:28 +00:00
|
|
|
{
|
|
|
|
*digest = nsnull;
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
//-- Calculate the digest
|
2001-05-23 23:22:25 +00:00
|
|
|
HASHContextStr* id;
|
2000-05-10 01:49:33 +00:00
|
|
|
rv = verifier->HashBegin(nsISignatureVerifier::SHA1, &id);
|
2000-03-21 04:21:28 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2000-05-10 01:49:33 +00:00
|
|
|
rv = verifier->HashUpdate(id, aInBuf, aLen);
|
2000-03-21 04:21:28 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
PRUint32 len;
|
2000-05-10 01:49:33 +00:00
|
|
|
unsigned char* rawDigest = (unsigned char*)PR_MALLOC(nsISignatureVerifier::SHA1_LENGTH);
|
2000-03-21 04:21:28 +00:00
|
|
|
if (rawDigest == nsnull) return NS_ERROR_OUT_OF_MEMORY;
|
2000-05-10 01:49:33 +00:00
|
|
|
rv = verifier->HashEnd(id, &rawDigest, &len, nsISignatureVerifier::SHA1_LENGTH);
|
2000-03-21 04:21:28 +00:00
|
|
|
if (NS_FAILED(rv)) { PR_FREEIF(rawDigest); return rv; }
|
|
|
|
|
|
|
|
//-- Encode the digest in base64
|
2000-05-10 01:49:33 +00:00
|
|
|
*digest = PL_Base64Encode((char*)rawDigest, len, *digest);
|
2000-03-21 04:21:28 +00:00
|
|
|
if (!(*digest)) { PR_FREEIF(rawDigest); return NS_ERROR_OUT_OF_MEMORY; }
|
|
|
|
|
|
|
|
PR_FREEIF(rawDigest);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2000-01-29 00:03:57 +00:00
|
|
|
//----------------------------------------------
|
|
|
|
// Debugging functions
|
|
|
|
//----------------------------------------------
|
|
|
|
#if 0
|
|
|
|
PR_STATIC_CALLBACK(PRBool)
|
|
|
|
PrintManItem(nsHashKey* aKey, void* aData, void* closure)
|
|
|
|
{
|
|
|
|
nsJARManifestItem* manItem = (nsJARManifestItem*)aData;
|
|
|
|
if (manItem)
|
|
|
|
{
|
2000-08-10 06:19:37 +00:00
|
|
|
nsCStringKey* key2 = (nsCStringKey*)aKey;
|
2000-02-14 01:57:01 +00:00
|
|
|
char* name = key2->GetString().ToNewCString();
|
2000-07-12 03:10:33 +00:00
|
|
|
if (!(PL_strcmp(name, "") == 0))
|
2000-10-28 22:17:53 +00:00
|
|
|
printf("%s s=%i\n",name, manItem->status);
|
2000-01-29 00:03:57 +00:00
|
|
|
}
|
|
|
|
return PR_TRUE;
|
1999-10-26 19:43:26 +00:00
|
|
|
}
|
2000-01-29 00:03:57 +00:00
|
|
|
#endif
|
|
|
|
|
2000-02-14 01:57:01 +00:00
|
|
|
void nsJAR::DumpMetadata(const char* aMessage)
|
2000-01-29 00:03:57 +00:00
|
|
|
{
|
|
|
|
#if 0
|
2000-10-28 22:17:53 +00:00
|
|
|
printf("### nsJAR::DumpMetadata at %s ###\n", aMessage);
|
2000-07-12 03:10:33 +00:00
|
|
|
if (mPrincipal)
|
|
|
|
{
|
|
|
|
char* toStr;
|
|
|
|
mPrincipal->ToString(&toStr);
|
2000-10-28 22:17:53 +00:00
|
|
|
printf("Principal: %s.\n", toStr);
|
2000-07-12 03:10:33 +00:00
|
|
|
PR_FREEIF(toStr);
|
|
|
|
}
|
|
|
|
else
|
2000-10-28 22:17:53 +00:00
|
|
|
printf("No Principal. \n");
|
2000-01-29 00:03:57 +00:00
|
|
|
mManifestData.Enumerate(PrintManItem);
|
2000-10-28 22:17:53 +00:00
|
|
|
printf("\n");
|
2000-01-29 00:03:57 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
1999-06-23 06:16:28 +00:00
|
|
|
//----------------------------------------------
|
|
|
|
// nsJAREnumerator constructor and destructor
|
|
|
|
//----------------------------------------------
|
|
|
|
nsJAREnumerator::nsJAREnumerator(nsZipFind *aFind)
|
|
|
|
: mFind(aFind),
|
|
|
|
mCurr(nsnull),
|
|
|
|
mIsCurrStale(PR_TRUE)
|
|
|
|
{
|
|
|
|
mArchive = mFind->GetArchive();
|
|
|
|
NS_INIT_REFCNT();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsJAREnumerator::~nsJAREnumerator()
|
|
|
|
{
|
|
|
|
mArchive->FindFree(mFind);
|
|
|
|
}
|
|
|
|
|
2000-01-11 20:49:15 +00:00
|
|
|
NS_IMPL_ISUPPORTS(nsJAREnumerator, NS_GET_IID(nsISimpleEnumerator));
|
1999-06-23 06:16:28 +00:00
|
|
|
|
|
|
|
//----------------------------------------------
|
|
|
|
// nsJAREnumerator::HasMoreElements
|
|
|
|
//----------------------------------------------
|
1999-06-01 21:08:32 +00:00
|
|
|
NS_IMETHODIMP
|
1999-06-23 06:16:28 +00:00
|
|
|
nsJAREnumerator::HasMoreElements(PRBool* aResult)
|
1999-06-01 21:08:32 +00:00
|
|
|
{
|
1999-06-23 06:16:28 +00:00
|
|
|
PRInt32 err;
|
|
|
|
|
|
|
|
if (!mFind)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
|
|
|
// try to get the next element
|
|
|
|
if (mIsCurrStale)
|
|
|
|
{
|
|
|
|
err = mArchive->FindNext( mFind, &mCurr );
|
|
|
|
if (err == ZIP_ERR_FNF)
|
|
|
|
{
|
|
|
|
*aResult = PR_FALSE;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
if (err != ZIP_OK)
|
|
|
|
return NS_ERROR_FAILURE; // no error translation
|
|
|
|
|
|
|
|
mIsCurrStale = PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aResult = PR_TRUE;
|
|
|
|
return NS_OK;
|
1999-06-01 21:08:32 +00:00
|
|
|
}
|
|
|
|
|
1999-06-23 06:16:28 +00:00
|
|
|
//----------------------------------------------
|
|
|
|
// nsJAREnumerator::GetNext
|
|
|
|
//----------------------------------------------
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsJAREnumerator::GetNext(nsISupports** aResult)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
PRBool bMore;
|
|
|
|
|
|
|
|
// check if the current item is "stale"
|
|
|
|
if (mIsCurrStale)
|
|
|
|
{
|
|
|
|
rv = HasMoreElements( &bMore );
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
if (bMore == PR_FALSE)
|
|
|
|
{
|
|
|
|
*aResult = nsnull; // null return value indicates no more elements
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// pack into an nsIJARItem
|
1999-11-12 06:13:13 +00:00
|
|
|
nsJARItem* jarItem = new nsJARItem();
|
1999-07-15 23:06:52 +00:00
|
|
|
if(jarItem)
|
|
|
|
{
|
1999-11-12 06:13:13 +00:00
|
|
|
NS_ADDREF(jarItem);
|
2000-01-29 00:03:57 +00:00
|
|
|
jarItem->Init(mCurr);
|
1999-07-15 23:06:52 +00:00
|
|
|
*aResult = jarItem;
|
|
|
|
mIsCurrStale = PR_TRUE; // we just gave this one away
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
1999-06-23 06:16:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------
|
|
|
|
// nsJARItem constructors and destructor
|
|
|
|
//-------------------------------------------------
|
|
|
|
nsJARItem::nsJARItem()
|
|
|
|
{
|
1999-06-29 23:23:23 +00:00
|
|
|
NS_INIT_ISUPPORTS();
|
1999-06-23 06:16:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsJARItem::~nsJARItem()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
1999-11-12 06:13:13 +00:00
|
|
|
NS_IMPL_ISUPPORTS1(nsJARItem, nsIZipEntry);
|
1999-06-23 06:16:28 +00:00
|
|
|
|
2000-01-29 00:03:57 +00:00
|
|
|
void nsJARItem::Init(nsZipItem* aZipItem)
|
|
|
|
{
|
|
|
|
mZipItem = aZipItem;
|
|
|
|
}
|
|
|
|
|
1999-06-23 06:16:28 +00:00
|
|
|
//------------------------------------------
|
|
|
|
// nsJARItem::GetName
|
|
|
|
//------------------------------------------
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsJARItem::GetName(char * *aName)
|
|
|
|
{
|
|
|
|
char *namedup;
|
|
|
|
|
|
|
|
if ( !aName )
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
2000-01-29 00:03:57 +00:00
|
|
|
if ( !mZipItem->name )
|
1999-06-23 06:16:28 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2000-01-29 00:03:57 +00:00
|
|
|
namedup = PL_strndup( mZipItem->name, mZipItem->namelen );
|
1999-06-23 06:16:28 +00:00
|
|
|
if ( !namedup )
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
*aName = namedup;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------
|
|
|
|
// nsJARItem::GetCompression
|
|
|
|
//------------------------------------------
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsJARItem::GetCompression(PRUint16 *aCompression)
|
|
|
|
{
|
|
|
|
if (!aCompression)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
2000-01-29 00:03:57 +00:00
|
|
|
if (!mZipItem->compression)
|
1999-06-23 06:16:28 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2000-01-29 00:03:57 +00:00
|
|
|
*aCompression = mZipItem->compression;
|
1999-06-23 06:16:28 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------
|
|
|
|
// nsJARItem::GetSize
|
|
|
|
//------------------------------------------
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsJARItem::GetSize(PRUint32 *aSize)
|
|
|
|
{
|
|
|
|
if (!aSize)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
2000-01-29 00:03:57 +00:00
|
|
|
if (!mZipItem->size)
|
1999-06-23 06:16:28 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2000-01-29 00:03:57 +00:00
|
|
|
*aSize = mZipItem->size;
|
1999-06-23 06:16:28 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------
|
|
|
|
// nsJARItem::GetRealSize
|
|
|
|
//------------------------------------------
|
|
|
|
NS_IMETHODIMP
|
1999-11-12 06:13:13 +00:00
|
|
|
nsJARItem::GetRealSize(PRUint32 *aRealsize)
|
1999-06-23 06:16:28 +00:00
|
|
|
{
|
|
|
|
if (!aRealsize)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
2000-01-29 00:03:57 +00:00
|
|
|
if (!mZipItem->realsize)
|
1999-06-23 06:16:28 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2000-01-29 00:03:57 +00:00
|
|
|
*aRealsize = mZipItem->realsize;
|
1999-06-23 06:16:28 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------
|
|
|
|
// nsJARItem::GetCrc32
|
|
|
|
//------------------------------------------
|
|
|
|
NS_IMETHODIMP
|
1999-11-12 06:13:13 +00:00
|
|
|
nsJARItem::GetCRC32(PRUint32 *aCrc32)
|
1999-06-23 06:16:28 +00:00
|
|
|
{
|
|
|
|
if (!aCrc32)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
2000-01-29 00:03:57 +00:00
|
|
|
if (!mZipItem->crc32)
|
1999-06-23 06:16:28 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2000-01-29 00:03:57 +00:00
|
|
|
*aCrc32 = mZipItem->crc32;
|
1999-06-23 06:16:28 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2000-01-29 00:03:57 +00:00
|
|
|
|
2000-04-12 07:58:24 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// nsIZipReaderCache
|
|
|
|
|
2000-10-31 22:44:20 +00:00
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS3(nsZipReaderCache, nsIZipReaderCache, nsIObserver, nsISupportsWeakReference)
|
2000-04-12 07:58:24 +00:00
|
|
|
|
|
|
|
nsZipReaderCache::nsZipReaderCache()
|
2000-04-26 21:27:52 +00:00
|
|
|
: mLock(nsnull),
|
2000-10-31 22:44:20 +00:00
|
|
|
mZips(16)
|
2000-08-24 07:38:41 +00:00
|
|
|
#ifdef ZIP_CACHE_HIT_RATE
|
2000-10-31 22:44:20 +00:00
|
|
|
,
|
2000-08-24 07:38:41 +00:00
|
|
|
mZipCacheLookups(0),
|
|
|
|
mZipCacheHits(0),
|
|
|
|
mZipCacheFlushes(0),
|
2000-10-31 22:44:20 +00:00
|
|
|
mZipSyncMisses(0)
|
2000-08-24 07:38:41 +00:00
|
|
|
#endif
|
2000-04-12 07:58:24 +00:00
|
|
|
{
|
|
|
|
NS_INIT_REFCNT();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsZipReaderCache::Init(PRUint32 cacheSize)
|
|
|
|
{
|
2000-10-31 22:44:20 +00:00
|
|
|
nsresult rv;
|
2000-09-20 19:35:24 +00:00
|
|
|
#ifdef xDEBUG_warren
|
2000-05-25 08:30:29 +00:00
|
|
|
mCacheSize = 1;//cacheSize; // XXX hack
|
|
|
|
#else
|
2000-10-31 22:44:20 +00:00
|
|
|
mCacheSize = cacheSize;
|
2000-05-25 08:30:29 +00:00
|
|
|
#endif
|
2000-10-31 22:44:20 +00:00
|
|
|
|
|
|
|
// Register as a memory pressure observer
|
|
|
|
NS_WITH_SERVICE(nsIObserverService, os, NS_OBSERVERSERVICE_CONTRACTID, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
{
|
|
|
|
rv = os->AddObserver(this, NS_MEMORY_PRESSURE_TOPIC);
|
|
|
|
}
|
|
|
|
// ignore failure of the observer registration.
|
|
|
|
|
2000-04-12 07:58:24 +00:00
|
|
|
mLock = PR_NewLock();
|
|
|
|
return mLock ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2000-08-23 20:39:17 +00:00
|
|
|
static PRBool PR_CALLBACK
|
2000-08-23 03:18:53 +00:00
|
|
|
DropZipReaderCache(nsHashKey *aKey, void *aData, void* closure)
|
|
|
|
{
|
|
|
|
nsJAR* zip = (nsJAR*)aData;
|
|
|
|
zip->SetZipReaderCache(nsnull);
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
2000-04-12 07:58:24 +00:00
|
|
|
nsZipReaderCache::~nsZipReaderCache()
|
|
|
|
{
|
|
|
|
if (mLock)
|
|
|
|
PR_DestroyLock(mLock);
|
2000-08-23 03:18:53 +00:00
|
|
|
mZips.Enumerate(DropZipReaderCache, nsnull);
|
2000-08-24 07:38:41 +00:00
|
|
|
|
|
|
|
#ifdef ZIP_CACHE_HIT_RATE
|
2000-10-31 22:44:20 +00: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 07:38:41 +00:00
|
|
|
#endif
|
2000-04-12 07:58:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_METHOD
|
|
|
|
nsZipReaderCache::Create(nsISupports *aOuter, REFNSIID aIID, void **aResult)
|
|
|
|
{
|
2000-06-07 09:05:22 +00:00
|
|
|
if (aOuter)
|
|
|
|
return NS_ERROR_NO_AGGREGATION;
|
2000-04-12 07:58:24 +00:00
|
|
|
|
2000-06-07 09:05:22 +00:00
|
|
|
nsZipReaderCache* cache = new nsZipReaderCache();
|
|
|
|
if (cache == nsnull)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
NS_ADDREF(cache);
|
|
|
|
nsresult rv = cache->QueryInterface(aIID, aResult);
|
|
|
|
NS_RELEASE(cache);
|
|
|
|
return rv;
|
2000-04-12 07:58:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsZipReaderCache::GetZip(nsIFile* zipFile, nsIZipReader* *result)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsAutoLock lock(mLock);
|
|
|
|
|
2000-08-24 07:38:41 +00:00
|
|
|
#ifdef ZIP_CACHE_HIT_RATE
|
|
|
|
mZipCacheLookups++;
|
|
|
|
#endif
|
|
|
|
|
2000-05-25 08:30:29 +00:00
|
|
|
nsXPIDLCString path;
|
|
|
|
rv = zipFile->GetPath(getter_Copies(path));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2000-08-10 06:19:37 +00:00
|
|
|
nsCStringKey key(path);
|
2001-02-23 00:15:04 +00:00
|
|
|
nsJAR* zip = NS_STATIC_CAST(nsJAR*, NS_STATIC_CAST(nsIZipReader*,mZips.Get(&key))); // AddRefs
|
2000-08-23 03:18:53 +00:00
|
|
|
if (zip) {
|
2000-08-24 07:38:41 +00:00
|
|
|
#ifdef ZIP_CACHE_HIT_RATE
|
|
|
|
mZipCacheHits++;
|
|
|
|
#endif
|
2000-10-31 22:44:20 +00:00
|
|
|
zip->ClearReleaseTime();
|
2000-04-12 07:58:24 +00:00
|
|
|
}
|
2000-08-23 03:18:53 +00:00
|
|
|
else {
|
|
|
|
zip = new nsJAR();
|
|
|
|
if (zip == nsnull)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
NS_ADDREF(zip);
|
|
|
|
zip->SetZipReaderCache(this);
|
2000-04-12 07:58:24 +00:00
|
|
|
|
2000-08-23 03:18:53 +00:00
|
|
|
rv = zip->Init(zipFile);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_RELEASE(zip);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
rv = zip->Open();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_RELEASE(zip);
|
|
|
|
return rv;
|
|
|
|
}
|
2000-04-12 07:58:24 +00:00
|
|
|
|
2001-02-23 00:15:04 +00:00
|
|
|
PRBool collision = mZips.Put(&key, NS_STATIC_CAST(nsIZipReader*, zip)); // AddRefs to 2
|
2000-08-23 03:18:53 +00:00
|
|
|
NS_ASSERTION(!collision, "horked");
|
2000-04-12 07:58:24 +00:00
|
|
|
}
|
|
|
|
*result = zip;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2000-08-23 20:39:17 +00:00
|
|
|
static PRBool PR_CALLBACK
|
2000-08-23 03:18:53 +00: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) {
|
|
|
|
if (oldest == nsnull ||
|
|
|
|
currentReleaseTime < oldest->GetReleaseTime()) {
|
|
|
|
*oldestPtr = current;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
2000-10-31 22:44:20 +00:00
|
|
|
struct ZipFindData {nsJAR* zip; PRBool found;};
|
|
|
|
|
|
|
|
static PRBool PR_CALLBACK
|
|
|
|
FindZip(nsHashKey *aKey, void *aData, void* closure)
|
|
|
|
{
|
|
|
|
ZipFindData* find_data = (ZipFindData*)closure;
|
|
|
|
|
|
|
|
if (find_data->zip == (nsJAR*)aData) {
|
|
|
|
find_data->found = PR_TRUE;
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
2000-08-23 03:18:53 +00:00
|
|
|
nsresult
|
|
|
|
nsZipReaderCache::ReleaseZip(nsJAR* zip)
|
2000-04-12 07:58:24 +00:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsAutoLock lock(mLock);
|
|
|
|
|
2000-10-31 22:44:20 +00: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
|
|
|
|
// second thread then Releases the zip and the ref coutn goes to one. It
|
|
|
|
// Then tries to enter this ReleaseZip method and blocks while the first
|
|
|
|
// 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!
|
|
|
|
//
|
|
|
|
// So, we are going to try safegaurding here by searching our hashtable while
|
|
|
|
// locked here for the zip. We return fast if it is not found.
|
|
|
|
|
|
|
|
ZipFindData find_data = {zip, PR_FALSE};
|
|
|
|
mZips.Enumerate(FindZip, &find_data);
|
|
|
|
if (!find_data.found) {
|
|
|
|
#ifdef ZIP_CACHE_HIT_RATE
|
|
|
|
mZipSyncMisses++;
|
|
|
|
#endif
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2000-09-18 07:08:22 +00:00
|
|
|
zip->SetReleaseTime();
|
|
|
|
|
2000-08-23 03:18:53 +00:00
|
|
|
if (mZips.Count() <= mCacheSize)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
nsJAR* oldest = nsnull;
|
2000-10-31 22:44:20 +00: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 03:18:53 +00:00
|
|
|
|
2000-08-24 07:38:41 +00:00
|
|
|
#ifdef ZIP_CACHE_HIT_RATE
|
|
|
|
mZipCacheFlushes++;
|
|
|
|
#endif
|
|
|
|
|
2000-10-31 22:44:20 +00:00
|
|
|
// Clear the cache pointer in case we gave out this oldest guy while
|
|
|
|
// his Release call was being made. Otherwise we could nest on ReleaseZip
|
|
|
|
// when the second owner calls Release and we are still here in this lock.
|
|
|
|
oldest->SetZipReaderCache(nsnull);
|
|
|
|
|
2000-08-23 03:18:53 +00:00
|
|
|
// remove from hashtable
|
2000-04-12 07:58:24 +00:00
|
|
|
nsCOMPtr<nsIFile> zipFile;
|
2000-08-23 03:18:53 +00:00
|
|
|
rv = oldest->GetFile(getter_AddRefs(zipFile));
|
2000-04-12 07:58:24 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2000-05-25 08:30:29 +00:00
|
|
|
nsXPIDLCString path;
|
|
|
|
rv = zipFile->GetPath(getter_Copies(path));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2000-08-10 06:19:37 +00:00
|
|
|
nsCStringKey key(path);
|
2000-08-23 03:18:53 +00:00
|
|
|
PRBool removed = mZips.Remove(&key); // Releases
|
|
|
|
NS_ASSERTION(removed, "botched");
|
2000-04-12 07:58:24 +00:00
|
|
|
|
2000-10-31 22:44:20 +00:00
|
|
|
#ifdef xDEBUG_jband
|
|
|
|
printf("dumped %s from the jar cache\n", (const char*) path);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PRBool PR_CALLBACK
|
|
|
|
FindFlushableZip(nsHashKey *aKey, void *aData, void* closure)
|
|
|
|
{
|
|
|
|
nsHashKey** flushableKeyPtr = (nsHashKey**)closure;
|
|
|
|
nsJAR* current = (nsJAR*)aData;
|
|
|
|
|
|
|
|
if (current->GetReleaseTime() != PR_INTERVAL_NO_TIMEOUT) {
|
|
|
|
*flushableKeyPtr = aKey;
|
|
|
|
current->SetZipReaderCache(nsnull);
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsZipReaderCache::Observe(nsISupports *aSubject,
|
|
|
|
const PRUnichar *aTopic,
|
|
|
|
const PRUnichar *aSomeData)
|
|
|
|
{
|
|
|
|
if (nsCRT::strcmp(aTopic, NS_MEMORY_PRESSURE_TOPIC) == 0) {
|
|
|
|
nsAutoLock lock(mLock);
|
|
|
|
while (PR_TRUE) {
|
|
|
|
nsHashKey* flushable = nsnull;
|
|
|
|
mZips.Enumerate(FindFlushableZip, &flushable);
|
|
|
|
if ( ! flushable )
|
|
|
|
break;
|
|
|
|
PRBool removed = mZips.Remove(flushable); // Releases
|
|
|
|
NS_ASSERTION(removed, "botched");
|
|
|
|
|
|
|
|
#ifdef xDEBUG_jband
|
|
|
|
printf("flushed something from the jar cache\n");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
2000-04-12 07:58:24 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|