2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla 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/MPL/
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* The Original Code is Mozilla Communicator client code, released
|
|
|
|
* March 31, 1998.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Daniel Veditz <dveditz@netscape.com>
|
|
|
|
* Samir Gehani <sgehani@netscape.com>
|
|
|
|
* Mitch Stoltz <mstoltz@netscape.com>
|
|
|
|
* Jeroen Dobbelaere <jeroen.dobbelaere@acunia.com>
|
|
|
|
* Jeff Walden <jwalden+code@mit.edu>
|
2009-08-12 13:50:12 -07:00
|
|
|
* Taras Glek <tglek@mozilla.com>
|
2007-03-22 10:30:00 -07:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This module implements a simple archive extractor for the PKZIP format.
|
|
|
|
*
|
|
|
|
* The underlying nsZipArchive is NOT thread-safe. Do not pass references
|
|
|
|
* or pointers to it across thread boundaries.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define READTYPE PRInt32
|
|
|
|
#include "zlib.h"
|
|
|
|
#include "nsISupportsUtils.h"
|
|
|
|
#include "nsRecyclingAllocator.h"
|
2009-08-12 13:50:12 -07:00
|
|
|
#include "prio.h"
|
|
|
|
#include "plstr.h"
|
|
|
|
#include "prlog.h"
|
|
|
|
#include "stdlib.h"
|
|
|
|
#include "nsWildCard.h"
|
|
|
|
#include "nsZipArchive.h"
|
2010-08-18 10:34:07 -07:00
|
|
|
#include "nsString.h"
|
2010-05-19 16:22:19 -07:00
|
|
|
#include "mozilla/FunctionTimer.h"
|
2010-08-18 10:34:07 -07:00
|
|
|
#include "prenv.h"
|
2010-11-11 12:13:48 -08:00
|
|
|
#if defined(XP_WIN)
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
2010-05-19 16:22:19 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/**
|
|
|
|
* Global allocator used with zlib. Destroyed in module shutdown.
|
|
|
|
*/
|
|
|
|
#define NBUCKETS 6
|
|
|
|
nsRecyclingAllocator *gZlibAllocator = NULL;
|
|
|
|
|
|
|
|
// For placement new used for arena allocations of zip file list
|
|
|
|
#include NEW_H
|
|
|
|
#define ZIP_ARENABLOCKSIZE (1*1024)
|
|
|
|
|
|
|
|
#ifdef XP_UNIX
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#elif defined(XP_WIN) || defined(XP_OS2)
|
|
|
|
#include <io.h>
|
|
|
|
#endif
|
|
|
|
|
2009-06-25 22:43:15 -07:00
|
|
|
#ifdef __SYMBIAN32__
|
|
|
|
#include <sys/syslimits.h>
|
|
|
|
#endif /*__SYMBIAN32__*/
|
|
|
|
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#ifndef XP_UNIX /* we need some constants defined in limits.h and unistd.h */
|
|
|
|
# ifndef S_IFMT
|
|
|
|
# define S_IFMT 0170000
|
|
|
|
# endif
|
|
|
|
# ifndef S_IFLNK
|
|
|
|
# define S_IFLNK 0120000
|
|
|
|
# endif
|
|
|
|
# ifndef PATH_MAX
|
|
|
|
# define PATH_MAX 1024
|
|
|
|
# endif
|
|
|
|
#endif /* XP_UNIX */
|
|
|
|
|
2009-10-17 08:54:54 -07:00
|
|
|
|
2010-08-18 10:34:07 -07:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2009-10-17 08:54:54 -07:00
|
|
|
static const PRUint32 kMaxNameLength = PATH_MAX; /* Maximum name length */
|
|
|
|
// For synthetic zip entries. Date/time corresponds to 1980-01-01 00:00.
|
|
|
|
static const PRUint16 kSyntheticTime = 0;
|
|
|
|
static const PRUint16 kSyntheticDate = (1 + (1 << 5) + (0 << 9));
|
|
|
|
|
2009-12-15 15:01:08 -08:00
|
|
|
static PRUint16 xtoint(const PRUint8 *ii);
|
|
|
|
static PRUint32 xtolong(const PRUint8 *ll);
|
2009-10-17 08:54:54 -07:00
|
|
|
static PRUint32 HashName(const char* aName, PRUint16 nameLen);
|
Bug 627277 - Remove (broken) BeOS support. r=biesi,dwitte,gavin,joe,jorendorff,josh,khuey,mfinkle,neil,Pike,roc,shaver,smontagu,taras
2011-02-19 11:10:24 -08:00
|
|
|
#ifdef XP_UNIX
|
2007-03-22 10:30:00 -07:00
|
|
|
static nsresult ResolveSymlink(const char *path);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//***********************************************************
|
|
|
|
// Allocators for use with zlib
|
|
|
|
//
|
2009-10-09 05:30:45 -07:00
|
|
|
// Use a recycling allocator, for re-use of of the zlib buffers.
|
|
|
|
// For every inflation the following allocations are done:
|
|
|
|
// zlibAlloc(1, 9520)
|
|
|
|
// zlibAlloc(32768, 1)
|
2007-03-22 10:30:00 -07:00
|
|
|
//***********************************************************
|
|
|
|
|
2008-10-10 08:04:34 -07:00
|
|
|
static void *
|
2007-03-22 10:30:00 -07:00
|
|
|
zlibAlloc(void *opaque, uInt items, uInt size)
|
|
|
|
{
|
|
|
|
nsRecyclingAllocator *zallocator = (nsRecyclingAllocator *)opaque;
|
2009-10-21 02:58:49 -07:00
|
|
|
if (zallocator) {
|
|
|
|
return gZlibAllocator->Malloc(items * size);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2009-10-21 02:58:49 -07:00
|
|
|
return malloc(items * size);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-10-10 08:04:34 -07:00
|
|
|
static void
|
2007-03-22 10:30:00 -07:00
|
|
|
zlibFree(void *opaque, void *ptr)
|
|
|
|
{
|
|
|
|
nsRecyclingAllocator *zallocator = (nsRecyclingAllocator *)opaque;
|
|
|
|
if (zallocator)
|
|
|
|
zallocator->Free(ptr);
|
|
|
|
else
|
|
|
|
free(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult gZlibInit(z_stream *zs)
|
|
|
|
{
|
|
|
|
memset(zs, 0, sizeof(z_stream));
|
|
|
|
//-- ensure we have our zlib allocator for better performance
|
|
|
|
if (!gZlibAllocator) {
|
|
|
|
gZlibAllocator = new nsRecyclingAllocator(NBUCKETS, NS_DEFAULT_RECYCLE_TIMEOUT, "libjar");
|
|
|
|
}
|
|
|
|
if (gZlibAllocator) {
|
|
|
|
zs->zalloc = zlibAlloc;
|
|
|
|
zs->zfree = zlibFree;
|
|
|
|
zs->opaque = gZlibAllocator;
|
|
|
|
}
|
|
|
|
int zerr = inflateInit2(zs, -MAX_WBITS);
|
2009-10-08 07:24:22 -07:00
|
|
|
if (zerr != Z_OK) return NS_ERROR_OUT_OF_MEMORY;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-10-08 07:24:22 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-08-12 13:50:12 -07:00
|
|
|
nsZipHandle::nsZipHandle()
|
2010-01-23 08:40:21 -08:00
|
|
|
: mFileData(nsnull)
|
2009-08-12 13:50:12 -07:00
|
|
|
, mLen(0)
|
|
|
|
, mMap(nsnull)
|
|
|
|
, mRefCnt(0)
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(nsZipHandle);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_THREADSAFE_ADDREF(nsZipHandle)
|
|
|
|
NS_IMPL_THREADSAFE_RELEASE(nsZipHandle)
|
|
|
|
|
2010-09-08 20:37:34 -07:00
|
|
|
nsresult nsZipHandle::Init(nsILocalFile *file, nsZipHandle **ret)
|
2009-08-12 13:50:12 -07:00
|
|
|
{
|
2010-09-08 20:37:34 -07:00
|
|
|
mozilla::AutoFDClose fd;
|
|
|
|
nsresult rv = file->OpenNSPRFileDesc(PR_RDONLY, 0000, &fd);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2009-08-12 13:50:12 -07:00
|
|
|
PRInt64 size = PR_Available64(fd);
|
|
|
|
if (size >= PR_INT32_MAX)
|
|
|
|
return NS_ERROR_FILE_TOO_BIG;
|
|
|
|
|
|
|
|
PRFileMap *map = PR_CreateFileMap(fd, size, PR_PROT_READONLY);
|
|
|
|
if (!map)
|
|
|
|
return NS_ERROR_FAILURE;
|
2009-11-07 07:19:20 -08:00
|
|
|
|
|
|
|
PRUint8 *buf = (PRUint8*) PR_MemMap(map, 0, (PRUint32) size);
|
|
|
|
// Bug 525755: PR_MemMap fails when fd points at something other than a normal file.
|
|
|
|
if (!buf) {
|
|
|
|
PR_CloseFileMap(map);
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2009-08-12 13:50:12 -07:00
|
|
|
|
2010-09-08 20:37:34 -07:00
|
|
|
nsRefPtr<nsZipHandle> handle = new nsZipHandle();
|
2009-10-04 10:20:45 -07:00
|
|
|
if (!handle) {
|
2010-11-11 12:13:48 -08:00
|
|
|
PR_MemUnmap(buf, (PRUint32) size);
|
2009-10-04 10:20:45 -07:00
|
|
|
PR_CloseFileMap(map);
|
2009-08-12 13:50:12 -07:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2009-10-04 10:20:45 -07:00
|
|
|
}
|
2009-08-12 13:50:12 -07:00
|
|
|
|
|
|
|
handle->mMap = map;
|
2010-09-08 20:37:34 -07:00
|
|
|
handle->mFile = file;
|
2009-08-12 13:50:12 -07:00
|
|
|
handle->mLen = (PRUint32) size;
|
2009-11-07 07:19:20 -08:00
|
|
|
handle->mFileData = buf;
|
2010-09-08 20:37:34 -07:00
|
|
|
*ret = handle.forget().get();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsZipHandle::Init(nsZipArchive *zip, const char *entry,
|
|
|
|
nsZipHandle **ret)
|
|
|
|
{
|
|
|
|
nsRefPtr<nsZipHandle> handle = new nsZipHandle();
|
|
|
|
if (!handle)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
handle->mBuf = new nsZipItemPtr<PRUint8>(zip, entry);
|
|
|
|
if (!handle->mBuf)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2010-09-20 12:58:40 -07:00
|
|
|
if (!handle->mBuf->Buffer())
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
2010-09-08 20:37:34 -07:00
|
|
|
handle->mMap = nsnull;
|
|
|
|
handle->mLen = handle->mBuf->Length();
|
|
|
|
handle->mFileData = handle->mBuf->Buffer();
|
|
|
|
*ret = handle.forget().get();
|
2009-08-12 13:50:12 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsZipHandle::~nsZipHandle()
|
|
|
|
{
|
2010-09-08 20:37:34 -07:00
|
|
|
if (mMap) {
|
|
|
|
PR_MemUnmap((void *)mFileData, mLen);
|
2009-08-12 13:50:12 -07:00
|
|
|
PR_CloseFileMap(mMap);
|
2009-10-04 10:20:45 -07:00
|
|
|
}
|
2010-09-08 20:37:34 -07:00
|
|
|
mFileData = nsnull;
|
|
|
|
mMap = nsnull;
|
|
|
|
mBuf = nsnull;
|
2009-08-12 13:50:12 -07:00
|
|
|
MOZ_COUNT_DTOR(nsZipHandle);
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
//***********************************************************
|
|
|
|
// nsZipArchive -- public methods
|
|
|
|
//***********************************************************
|
|
|
|
|
|
|
|
//---------------------------------------------
|
|
|
|
// nsZipArchive::OpenArchive
|
|
|
|
//---------------------------------------------
|
2010-09-08 20:37:34 -07:00
|
|
|
nsresult nsZipArchive::OpenArchive(nsZipHandle *aZipHandle)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-09-08 20:37:34 -07:00
|
|
|
mFd = aZipHandle;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Initialize our arena
|
|
|
|
PL_INIT_ARENA_POOL(&mArena, "ZipArena", ZIP_ARENABLOCKSIZE);
|
|
|
|
|
|
|
|
//-- get table of contents for archive
|
2010-09-08 20:37:34 -07:00
|
|
|
nsresult rv = BuildFileList();
|
2010-08-18 10:34:07 -07:00
|
|
|
char *env = PR_GetEnv("MOZ_JAR_LOG_DIR");
|
2010-09-08 20:37:34 -07:00
|
|
|
if (env && NS_SUCCEEDED(rv) && aZipHandle->mFile) {
|
2010-08-18 10:34:07 -07:00
|
|
|
nsCOMPtr<nsILocalFile> logFile;
|
|
|
|
nsresult rv2 = NS_NewLocalFile(NS_ConvertUTF8toUTF16(env), PR_FALSE, getter_AddRefs(logFile));
|
|
|
|
|
|
|
|
if (!NS_SUCCEEDED(rv2))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
// Create a directory for the log (in case it doesn't exist)
|
|
|
|
logFile->Create(nsIFile::DIRECTORY_TYPE, 0700);
|
|
|
|
|
|
|
|
nsAutoString name;
|
2010-09-08 20:37:34 -07:00
|
|
|
aZipHandle->mFile->GetLeafName(name);
|
2010-08-18 10:34:07 -07:00
|
|
|
name.Append(NS_LITERAL_STRING(".log"));
|
|
|
|
logFile->Append(name);
|
|
|
|
|
2010-09-08 20:37:34 -07:00
|
|
|
PRFileDesc* fd;
|
2010-08-18 10:34:07 -07:00
|
|
|
rv2 = logFile->OpenNSPRFileDesc(PR_WRONLY|PR_CREATE_FILE|PR_APPEND, 0644, &fd);
|
|
|
|
if (NS_SUCCEEDED(rv2))
|
|
|
|
mLog = fd;
|
|
|
|
}
|
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-09-08 20:37:34 -07:00
|
|
|
nsresult nsZipArchive::OpenArchive(nsIFile *aFile)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsILocalFile> localFile = do_QueryInterface(aFile, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsRefPtr<nsZipHandle> handle;
|
|
|
|
rv = nsZipHandle::Init(localFile, getter_AddRefs(handle));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
return OpenArchive(handle);
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
//---------------------------------------------
|
|
|
|
// nsZipArchive::Test
|
|
|
|
//---------------------------------------------
|
|
|
|
nsresult nsZipArchive::Test(const char *aEntryName)
|
|
|
|
{
|
|
|
|
nsZipItem* currItem;
|
|
|
|
|
|
|
|
if (aEntryName) // only test specified item
|
|
|
|
{
|
|
|
|
currItem = GetItem(aEntryName);
|
|
|
|
if (!currItem)
|
2009-10-08 07:24:22 -07:00
|
|
|
return NS_ERROR_FILE_TARGET_DOES_NOT_EXIST;
|
2007-03-22 10:30:00 -07:00
|
|
|
//-- don't test (synthetic) directory items
|
2009-10-17 08:54:54 -07:00
|
|
|
if (currItem->IsDirectory())
|
2009-10-08 07:24:22 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
return ExtractFile(currItem, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// test all items in archive
|
|
|
|
for (int i = 0; i < ZIP_TABSIZE; i++) {
|
|
|
|
for (currItem = mFiles[i]; currItem; currItem = currItem->next) {
|
|
|
|
//-- don't test (synthetic) directory items
|
2009-10-17 08:54:54 -07:00
|
|
|
if (currItem->IsDirectory())
|
2007-03-22 10:30:00 -07:00
|
|
|
continue;
|
|
|
|
nsresult rv = ExtractFile(currItem, 0, 0);
|
2009-10-08 07:24:22 -07:00
|
|
|
if (rv != NS_OK)
|
2007-03-22 10:30:00 -07:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-08 07:24:22 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------
|
|
|
|
// nsZipArchive::CloseArchive
|
|
|
|
//---------------------------------------------
|
|
|
|
nsresult nsZipArchive::CloseArchive()
|
|
|
|
{
|
|
|
|
if (mFd) {
|
|
|
|
PL_FinishArenaPool(&mArena);
|
2009-10-04 10:20:45 -07:00
|
|
|
mFd = NULL;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// CAUTION:
|
|
|
|
// We don't need to delete each of the nsZipItem as the memory for
|
|
|
|
// the zip item and the filename it holds are both allocated from the Arena.
|
|
|
|
// Hence, destroying the Arena is like destroying all the memory
|
|
|
|
// for all the nsZipItem in one shot. But if the ~nsZipItem is doing
|
|
|
|
// anything more than cleaning up memory, we should start calling it.
|
|
|
|
// Let us also cleanup the mFiles table for re-use on the next 'open' call
|
2009-10-04 10:20:45 -07:00
|
|
|
memset(mFiles, 0, sizeof(mFiles));
|
|
|
|
mBuiltSynthetics = false;
|
2009-10-08 07:24:22 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------
|
|
|
|
// nsZipArchive::GetItem
|
|
|
|
//---------------------------------------------
|
|
|
|
nsZipItem* nsZipArchive::GetItem(const char * aEntryName)
|
|
|
|
{
|
|
|
|
if (aEntryName) {
|
2009-10-17 08:54:54 -07:00
|
|
|
PRUint32 len = strlen(aEntryName);
|
2007-03-22 10:30:00 -07:00
|
|
|
//-- If the request is for a directory, make sure that synthetic entries
|
|
|
|
//-- are created for the directories without their own entry.
|
|
|
|
if (!mBuiltSynthetics) {
|
|
|
|
if ((len > 0) && (aEntryName[len-1] == '/')) {
|
2009-10-08 07:24:22 -07:00
|
|
|
if (BuildSynthetics() != NS_OK)
|
2007-03-22 10:30:00 -07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2010-11-11 12:13:48 -08:00
|
|
|
MOZ_WIN_MEM_TRY_BEGIN
|
2009-10-17 08:54:54 -07:00
|
|
|
nsZipItem* item = mFiles[ HashName(aEntryName, len) ];
|
2007-03-22 10:30:00 -07:00
|
|
|
while (item) {
|
2009-10-17 08:54:54 -07:00
|
|
|
if ((len == item->nameLength) &&
|
2010-08-18 10:34:07 -07:00
|
|
|
(!memcmp(aEntryName, item->Name(), len))) {
|
|
|
|
|
|
|
|
if (mLog) {
|
|
|
|
// Successful GetItem() is a good indicator that the file is about to be read
|
|
|
|
char *tmp = PL_strdup(aEntryName);
|
|
|
|
tmp[len]='\n';
|
|
|
|
PR_Write(mLog, tmp, len+1);
|
|
|
|
PL_strfree(tmp);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
return item; //-- found it
|
2010-08-18 10:34:07 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
item = item->next;
|
|
|
|
}
|
2010-11-11 12:13:48 -08:00
|
|
|
MOZ_WIN_MEM_TRY_CATCH(return nsnull)
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2010-11-11 12:13:48 -08:00
|
|
|
return nsnull;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------
|
|
|
|
// nsZipArchive::ExtractFile
|
|
|
|
// This extracts the item to the filehandle provided.
|
|
|
|
// If 'aFd' is null, it only tests the extraction.
|
|
|
|
// On extraction error(s) it removes the file.
|
|
|
|
// When needed, it also resolves the symlink.
|
|
|
|
//---------------------------------------------
|
|
|
|
nsresult nsZipArchive::ExtractFile(nsZipItem *item, const char *outname,
|
|
|
|
PRFileDesc* aFd)
|
|
|
|
{
|
|
|
|
if (!item)
|
2009-10-08 07:24:22 -07:00
|
|
|
return NS_ERROR_ILLEGAL_VALUE;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!mFd)
|
2009-10-08 07:24:22 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Directory extraction is handled in nsJAR::Extract,
|
|
|
|
// so the item to be extracted should never be a directory
|
2009-10-17 08:54:54 -07:00
|
|
|
PR_ASSERT(!item->IsDirectory());
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-06-04 14:10:23 -07:00
|
|
|
Bytef outbuf[ZIP_BUFLEN];
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-06-04 14:10:23 -07:00
|
|
|
nsZipCursor cursor(item, this, outbuf, ZIP_BUFLEN, true);
|
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-06-04 14:10:23 -07:00
|
|
|
while (true) {
|
|
|
|
PRUint32 count = 0;
|
|
|
|
PRUint8* buf = cursor.Read(&count);
|
|
|
|
if (!buf) {
|
|
|
|
rv = NS_ERROR_FILE_CORRUPTED;
|
|
|
|
break;
|
|
|
|
} else if (count == 0) {
|
2010-06-02 16:14:30 -07:00
|
|
|
break;
|
2010-06-04 14:10:23 -07:00
|
|
|
}
|
2010-06-02 17:05:49 -07:00
|
|
|
|
2010-06-04 14:10:23 -07:00
|
|
|
if (aFd && PR_Write(aFd, buf, count) < (READTYPE)count) {
|
|
|
|
rv = NS_ERROR_FILE_DISK_FULL;
|
|
|
|
break;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//-- delete the file on errors, or resolve symlink if needed
|
|
|
|
if (aFd) {
|
|
|
|
PR_Close(aFd);
|
2009-10-08 07:24:22 -07:00
|
|
|
if (rv != NS_OK)
|
2007-03-22 10:30:00 -07:00
|
|
|
PR_Delete(outname);
|
Bug 627277 - Remove (broken) BeOS support. r=biesi,dwitte,gavin,joe,jorendorff,josh,khuey,mfinkle,neil,Pike,roc,shaver,smontagu,taras
2011-02-19 11:10:24 -08:00
|
|
|
#ifdef XP_UNIX
|
2009-10-17 08:54:54 -07:00
|
|
|
else if (item->IsSymlink())
|
2007-03-22 10:30:00 -07:00
|
|
|
rv = ResolveSymlink(outname);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------
|
|
|
|
// nsZipArchive::FindInit
|
|
|
|
//---------------------------------------------
|
|
|
|
PRInt32
|
|
|
|
nsZipArchive::FindInit(const char * aPattern, nsZipFind **aFind)
|
|
|
|
{
|
|
|
|
if (!aFind)
|
2009-10-08 07:24:22 -07:00
|
|
|
return NS_ERROR_ILLEGAL_VALUE;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// null out param in case an error happens
|
|
|
|
*aFind = NULL;
|
|
|
|
|
|
|
|
PRBool regExp = PR_FALSE;
|
|
|
|
char* pattern = 0;
|
|
|
|
|
|
|
|
// Create synthetic directory entries on demand
|
|
|
|
nsresult rv = BuildSynthetics();
|
2009-10-08 07:24:22 -07:00
|
|
|
if (rv != NS_OK)
|
2007-03-22 10:30:00 -07:00
|
|
|
return rv;
|
|
|
|
|
|
|
|
// validate the pattern
|
|
|
|
if (aPattern)
|
|
|
|
{
|
|
|
|
switch (NS_WildCardValid((char*)aPattern))
|
|
|
|
{
|
|
|
|
case INVALID_SXP:
|
2009-10-08 07:24:22 -07:00
|
|
|
return NS_ERROR_ILLEGAL_VALUE;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
case NON_SXP:
|
|
|
|
regExp = PR_FALSE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VALID_SXP:
|
|
|
|
regExp = PR_TRUE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
// undocumented return value from RegExpValid!
|
|
|
|
PR_ASSERT(PR_FALSE);
|
2009-10-08 07:24:22 -07:00
|
|
|
return NS_ERROR_ILLEGAL_VALUE;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pattern = PL_strdup(aPattern);
|
|
|
|
if (!pattern)
|
2009-10-08 07:24:22 -07:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
*aFind = new nsZipFind(this, pattern, regExp);
|
|
|
|
if (!*aFind) {
|
2009-04-07 02:24:58 -07:00
|
|
|
PL_strfree(pattern);
|
2009-10-08 07:24:22 -07:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-10-08 07:24:22 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------
|
|
|
|
// nsZipFind::FindNext
|
|
|
|
//---------------------------------------------
|
2009-10-17 08:54:54 -07:00
|
|
|
nsresult nsZipFind::FindNext(const char ** aResult, PRUint16 *aNameLen)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-10-17 08:54:54 -07:00
|
|
|
if (!mArchive || !aResult || !aNameLen)
|
2009-10-08 07:24:22 -07:00
|
|
|
return NS_ERROR_ILLEGAL_VALUE;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
*aResult = 0;
|
2009-10-17 08:54:54 -07:00
|
|
|
*aNameLen = 0;
|
2010-11-11 12:13:48 -08:00
|
|
|
MOZ_WIN_MEM_TRY_BEGIN
|
2007-03-22 10:30:00 -07:00
|
|
|
// we start from last match, look for next
|
|
|
|
while (mSlot < ZIP_TABSIZE)
|
|
|
|
{
|
|
|
|
// move to next in current chain, or move to new slot
|
|
|
|
mItem = mItem ? mItem->next : mArchive->mFiles[mSlot];
|
|
|
|
|
|
|
|
PRBool found = PR_FALSE;
|
|
|
|
if (!mItem)
|
|
|
|
++mSlot; // no more in this chain, move to next slot
|
|
|
|
else if (!mPattern)
|
|
|
|
found = PR_TRUE; // always match
|
|
|
|
else if (mRegExp)
|
2009-10-17 08:54:54 -07:00
|
|
|
{
|
|
|
|
char buf[kMaxNameLength+1];
|
|
|
|
memcpy(buf, mItem->Name(), mItem->nameLength);
|
|
|
|
buf[mItem->nameLength]='\0';
|
|
|
|
found = (NS_WildCardMatch(buf, mPattern, PR_FALSE) == MATCH);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
else
|
2009-10-17 08:54:54 -07:00
|
|
|
found = ((mItem->nameLength == strlen(mPattern)) &&
|
|
|
|
(memcmp(mItem->Name(), mPattern, mItem->nameLength) == 0));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (found) {
|
2009-10-17 08:54:54 -07:00
|
|
|
// Need also to return the name length, as it is NOT zero-terminatdd...
|
|
|
|
*aResult = mItem->Name();
|
|
|
|
*aNameLen = mItem->nameLength;
|
2009-10-08 07:24:22 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
2010-11-11 12:13:48 -08:00
|
|
|
MOZ_WIN_MEM_TRY_CATCH(return NS_ERROR_FAILURE)
|
2009-10-08 07:24:22 -07:00
|
|
|
return NS_ERROR_FILE_TARGET_DOES_NOT_EXIST;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
Bug 627277 - Remove (broken) BeOS support. r=biesi,dwitte,gavin,joe,jorendorff,josh,khuey,mfinkle,neil,Pike,roc,shaver,smontagu,taras
2011-02-19 11:10:24 -08:00
|
|
|
#ifdef XP_UNIX
|
2007-03-22 10:30:00 -07:00
|
|
|
//---------------------------------------------
|
|
|
|
// ResolveSymlink
|
|
|
|
//---------------------------------------------
|
|
|
|
static nsresult ResolveSymlink(const char *path)
|
|
|
|
{
|
|
|
|
PRFileDesc * fIn = PR_Open(path, PR_RDONLY, 0000);
|
|
|
|
if (!fIn)
|
2009-10-08 07:24:22 -07:00
|
|
|
return NS_ERROR_FILE_DISK_FULL;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
char buf[PATH_MAX+1];
|
|
|
|
PRInt32 length = PR_Read(fIn, (void*)buf, PATH_MAX);
|
|
|
|
PR_Close(fIn);
|
|
|
|
|
|
|
|
if ( (length <= 0)
|
|
|
|
|| ((buf[length] = 0, PR_Delete(path)) != 0)
|
|
|
|
|| (symlink(buf, path) != 0))
|
|
|
|
{
|
2009-10-08 07:24:22 -07:00
|
|
|
return NS_ERROR_FILE_DISK_FULL;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2009-10-08 07:24:22 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//***********************************************************
|
|
|
|
// nsZipArchive -- private implementation
|
|
|
|
//***********************************************************
|
|
|
|
|
|
|
|
//---------------------------------------------
|
|
|
|
// nsZipArchive::CreateZipItem
|
|
|
|
//---------------------------------------------
|
2009-10-17 08:54:54 -07:00
|
|
|
nsZipItem* nsZipArchive::CreateZipItem()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// Arena allocate the nsZipItem
|
|
|
|
void *mem;
|
2009-10-17 08:54:54 -07:00
|
|
|
PL_ARENA_ALLOCATE(mem, &mArena, sizeof(nsZipItem));
|
2007-03-22 10:30:00 -07:00
|
|
|
return (nsZipItem*)mem;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------
|
|
|
|
// nsZipArchive::BuildFileList
|
|
|
|
//---------------------------------------------
|
|
|
|
nsresult nsZipArchive::BuildFileList()
|
|
|
|
{
|
2010-11-11 12:13:48 -08:00
|
|
|
#ifndef XP_WIN
|
2010-05-19 16:22:19 -07:00
|
|
|
NS_TIME_FUNCTION;
|
2010-11-11 12:13:48 -08:00
|
|
|
#endif
|
2009-08-24 15:12:02 -07:00
|
|
|
// Get archive size using end pos
|
2010-09-08 20:37:34 -07:00
|
|
|
const PRUint8* buf;
|
|
|
|
const PRUint8* startp = mFd->mFileData;
|
|
|
|
const PRUint8* endp = startp + mFd->mLen;
|
2010-11-11 12:13:48 -08:00
|
|
|
MOZ_WIN_MEM_TRY_BEGIN
|
2010-09-10 09:30:07 -07:00
|
|
|
PRUint32 centralOffset = 4;
|
2010-09-20 12:58:40 -07:00
|
|
|
if (mFd->mLen > ZIPCENTRAL_SIZE && xtolong(startp + centralOffset) == CENTRALSIG) {
|
2010-08-18 10:34:07 -07:00
|
|
|
// Success means optimized jar layout from bug 559961 is in effect
|
|
|
|
} else {
|
|
|
|
for (buf = endp - ZIPEND_SIZE; buf > startp; buf--)
|
|
|
|
{
|
|
|
|
if (xtolong(buf) == ENDSIG) {
|
|
|
|
centralOffset = xtolong(((ZipEnd *)buf)->offset_central_dir);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-08-24 15:12:02 -07:00
|
|
|
}
|
2010-03-26 10:19:53 -07:00
|
|
|
|
|
|
|
if (!centralOffset)
|
|
|
|
return NS_ERROR_FILE_CORRUPTED;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-08-24 15:12:02 -07:00
|
|
|
//-- Read the central directory headers
|
2009-10-17 08:54:54 -07:00
|
|
|
buf = startp + centralOffset;
|
2010-01-28 01:52:35 -08:00
|
|
|
if (endp - buf < PRInt32(sizeof(PRUint32)))
|
2010-01-25 01:21:49 -08:00
|
|
|
return NS_ERROR_FILE_CORRUPTED;
|
2007-03-22 10:30:00 -07:00
|
|
|
PRUint32 sig = xtolong(buf);
|
|
|
|
while (sig == CENTRALSIG) {
|
2009-08-24 15:12:02 -07:00
|
|
|
// Make sure there is enough data available.
|
|
|
|
if (endp - buf < ZIPCENTRAL_SIZE)
|
2009-10-08 07:24:22 -07:00
|
|
|
return NS_ERROR_FILE_CORRUPTED;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-08-24 15:12:02 -07:00
|
|
|
// Read the fixed-size data.
|
|
|
|
ZipCentral* central = (ZipCentral*)buf;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
PRUint16 namelen = xtoint(central->filename_len);
|
|
|
|
PRUint16 extralen = xtoint(central->extrafield_len);
|
|
|
|
PRUint16 commentlen = xtoint(central->commentfield_len);
|
|
|
|
|
2009-10-17 08:54:54 -07:00
|
|
|
// Point to the next item at the top of loop
|
|
|
|
buf += ZIPCENTRAL_SIZE + namelen + extralen + commentlen;
|
|
|
|
|
2009-08-24 15:12:02 -07:00
|
|
|
// Sanity check variable sizes and refuse to deal with
|
|
|
|
// anything too big: it's likely a corrupt archive.
|
2009-10-17 08:54:54 -07:00
|
|
|
if (namelen > kMaxNameLength || buf >= endp)
|
2009-10-08 07:24:22 -07:00
|
|
|
return NS_ERROR_FILE_CORRUPTED;
|
2008-03-19 12:28:38 -07:00
|
|
|
|
2009-10-17 08:54:54 -07:00
|
|
|
nsZipItem* item = CreateZipItem();
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!item)
|
2009-10-08 07:24:22 -07:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-10-17 08:54:54 -07:00
|
|
|
item->central = central;
|
|
|
|
item->nameLength = namelen;
|
|
|
|
item->isSynthetic = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-08-24 15:12:02 -07:00
|
|
|
// Add item to file table
|
2009-10-17 08:54:54 -07:00
|
|
|
PRUint32 hash = HashName(item->Name(), namelen);
|
2007-03-22 10:30:00 -07:00
|
|
|
item->next = mFiles[hash];
|
|
|
|
mFiles[hash] = item;
|
|
|
|
|
2009-08-24 15:12:02 -07:00
|
|
|
sig = xtolong(buf);
|
2007-03-22 10:30:00 -07:00
|
|
|
} /* while reading central directory records */
|
|
|
|
|
|
|
|
if (sig != ENDSIG)
|
2009-10-08 07:24:22 -07:00
|
|
|
return NS_ERROR_FILE_CORRUPTED;
|
2010-11-11 12:13:48 -08:00
|
|
|
|
|
|
|
MOZ_WIN_MEM_TRY_CATCH(return NS_ERROR_FAILURE)
|
2009-10-08 07:24:22 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------
|
|
|
|
// nsZipArchive::BuildSynthetics
|
|
|
|
//---------------------------------------------
|
|
|
|
nsresult nsZipArchive::BuildSynthetics()
|
|
|
|
{
|
|
|
|
if (mBuiltSynthetics)
|
2009-10-08 07:24:22 -07:00
|
|
|
return NS_OK;
|
2009-10-04 10:20:45 -07:00
|
|
|
mBuiltSynthetics = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-11-11 12:13:48 -08:00
|
|
|
MOZ_WIN_MEM_TRY_BEGIN
|
2007-03-22 10:30:00 -07:00
|
|
|
// Create synthetic entries for any missing directories.
|
|
|
|
// Do this when all ziptable has scanned to prevent double entries.
|
|
|
|
for (int i = 0; i < ZIP_TABSIZE; ++i)
|
|
|
|
{
|
|
|
|
for (nsZipItem* item = mFiles[i]; item != 0; item = item->next)
|
|
|
|
{
|
|
|
|
if (item->isSynthetic)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
//-- add entries for directories in the current item's path
|
|
|
|
//-- go from end to beginning, because then we can stop trying
|
|
|
|
//-- to create diritems if we find that the diritem we want to
|
|
|
|
//-- create already exists
|
|
|
|
//-- start just before the last char so as to not add the item
|
|
|
|
//-- twice if it's a directory
|
2009-10-17 08:54:54 -07:00
|
|
|
PRUint16 namelen = item->nameLength;
|
|
|
|
const char *name = item->Name();
|
|
|
|
for (PRUint16 dirlen = namelen - 1; dirlen > 0; dirlen--)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-10-17 08:54:54 -07:00
|
|
|
if (name[dirlen-1] != '/')
|
2007-03-22 10:30:00 -07:00
|
|
|
continue;
|
|
|
|
|
2009-10-17 08:54:54 -07:00
|
|
|
// Is the directory already in the file table?
|
|
|
|
PRUint32 hash = HashName(item->Name(), dirlen);
|
2007-03-22 10:30:00 -07:00
|
|
|
PRBool found = PR_FALSE;
|
|
|
|
for (nsZipItem* zi = mFiles[hash]; zi != NULL; zi = zi->next)
|
|
|
|
{
|
2009-10-17 08:54:54 -07:00
|
|
|
if ((dirlen == zi->nameLength) &&
|
|
|
|
(0 == memcmp(item->Name(), zi->Name(), dirlen)))
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// we've already added this dir and all its parents
|
|
|
|
found = PR_TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// if the directory was found, break out of the directory
|
|
|
|
// creation loop now that we know all implicit directories
|
|
|
|
// are there -- otherwise, start creating the zip item
|
|
|
|
if (found)
|
|
|
|
break;
|
|
|
|
|
2009-10-17 08:54:54 -07:00
|
|
|
nsZipItem* diritem = CreateZipItem();
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!diritem)
|
2009-10-08 07:24:22 -07:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-10-17 08:54:54 -07:00
|
|
|
// Point to the central record of the original item for the name part.
|
|
|
|
diritem->central = item->central;
|
|
|
|
diritem->nameLength = dirlen;
|
|
|
|
diritem->isSynthetic = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// add diritem to the file table
|
|
|
|
diritem->next = mFiles[hash];
|
|
|
|
mFiles[hash] = diritem;
|
|
|
|
} /* end processing of dirs in item's name */
|
|
|
|
}
|
|
|
|
}
|
2010-11-11 12:13:48 -08:00
|
|
|
MOZ_WIN_MEM_TRY_CATCH(return NS_ERROR_FAILURE)
|
2009-10-08 07:24:22 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-10-04 10:20:45 -07:00
|
|
|
nsZipHandle* nsZipArchive::GetFD()
|
2009-08-12 13:50:12 -07:00
|
|
|
{
|
2009-10-04 10:20:45 -07:00
|
|
|
if (!mFd)
|
2009-08-12 13:50:12 -07:00
|
|
|
return NULL;
|
|
|
|
return mFd.get();
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
//---------------------------------------------
|
2009-10-04 10:20:45 -07:00
|
|
|
// nsZipArchive::GetData
|
2007-03-22 10:30:00 -07:00
|
|
|
//---------------------------------------------
|
2010-09-08 20:37:34 -07:00
|
|
|
const PRUint8* nsZipArchive::GetData(nsZipItem* aItem)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
PR_ASSERT (aItem);
|
2010-11-11 12:13:48 -08:00
|
|
|
MOZ_WIN_MEM_TRY_BEGIN
|
2009-10-04 10:20:45 -07:00
|
|
|
//-- read local header to get variable length values and calculate
|
|
|
|
//-- the real data offset
|
2009-10-17 08:54:54 -07:00
|
|
|
PRUint32 len = mFd->mLen;
|
2010-09-08 20:37:34 -07:00
|
|
|
const PRUint8* data = mFd->mFileData;
|
2009-10-17 08:54:54 -07:00
|
|
|
PRUint32 offset = aItem->LocalOffset();
|
|
|
|
if (offset + ZIPLOCAL_SIZE > len)
|
2009-10-04 10:20:45 -07:00
|
|
|
return nsnull;
|
2009-09-01 12:47:00 -07:00
|
|
|
|
2009-10-04 10:20:45 -07:00
|
|
|
// -- check signature before using the structure, in case the zip file is corrupt
|
2009-10-17 08:54:54 -07:00
|
|
|
ZipLocal* Local = (ZipLocal*)(data + offset);
|
2009-10-04 10:20:45 -07:00
|
|
|
if ((xtolong(Local->signature) != LOCALSIG))
|
|
|
|
return nsnull;
|
|
|
|
|
|
|
|
//-- NOTE: extralen is different in central header and local header
|
|
|
|
//-- for archives created using the Unix "zip" utility. To set
|
|
|
|
//-- the offset accurately we need the _local_ extralen.
|
2009-10-17 08:54:54 -07:00
|
|
|
offset += ZIPLOCAL_SIZE +
|
|
|
|
xtoint(Local->filename_len) +
|
|
|
|
xtoint(Local->extrafield_len);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-10-04 10:20:45 -07:00
|
|
|
// -- check if there is enough source data in the file
|
2009-10-17 08:54:54 -07:00
|
|
|
if (offset + aItem->Size() > len)
|
2009-10-04 10:20:45 -07:00
|
|
|
return nsnull;
|
|
|
|
|
2009-10-17 08:54:54 -07:00
|
|
|
return data + offset;
|
2010-11-11 12:13:48 -08:00
|
|
|
MOZ_WIN_MEM_TRY_CATCH(return nsnull)
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------
|
|
|
|
// nsZipArchive constructor and destructor
|
|
|
|
//------------------------------------------
|
|
|
|
|
|
|
|
nsZipArchive::nsZipArchive() :
|
2009-10-04 10:20:45 -07:00
|
|
|
mBuiltSynthetics(false)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(nsZipArchive);
|
|
|
|
|
|
|
|
// initialize the table to NULL
|
2009-10-04 10:20:45 -07:00
|
|
|
memset(mFiles, 0, sizeof(mFiles));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsZipArchive::~nsZipArchive()
|
|
|
|
{
|
|
|
|
CloseArchive();
|
|
|
|
|
|
|
|
MOZ_COUNT_DTOR(nsZipArchive);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//------------------------------------------
|
|
|
|
// nsZipFind constructor and destructor
|
|
|
|
//------------------------------------------
|
|
|
|
|
|
|
|
nsZipFind::nsZipFind(nsZipArchive* aZip, char* aPattern, PRBool aRegExp) :
|
|
|
|
mArchive(aZip),
|
|
|
|
mPattern(aPattern),
|
|
|
|
mItem(0),
|
|
|
|
mSlot(0),
|
|
|
|
mRegExp(aRegExp)
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(nsZipFind);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsZipFind::~nsZipFind()
|
|
|
|
{
|
2009-04-07 02:24:58 -07:00
|
|
|
PL_strfree(mPattern);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
MOZ_COUNT_DTOR(nsZipFind);
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------
|
|
|
|
// helper functions
|
|
|
|
//------------------------------------------
|
|
|
|
|
|
|
|
/*
|
|
|
|
* HashName
|
|
|
|
*
|
|
|
|
* returns a hash key for the entry name
|
|
|
|
*/
|
2009-10-17 08:54:54 -07:00
|
|
|
static PRUint32 HashName(const char* aName, PRUint16 len)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
PR_ASSERT(aName != 0);
|
|
|
|
|
2009-10-17 08:54:54 -07:00
|
|
|
const PRUint8* p = (const PRUint8*)aName;
|
|
|
|
const PRUint8* endp = p + len;
|
2007-03-22 10:30:00 -07:00
|
|
|
PRUint32 val = 0;
|
2009-10-17 08:54:54 -07:00
|
|
|
while (p != endp) {
|
|
|
|
val = val*37 + *p++;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return (val % ZIP_TABSIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* x t o i n t
|
|
|
|
*
|
|
|
|
* Converts a two byte ugly endianed integer
|
|
|
|
* to our platform's integer.
|
|
|
|
*/
|
2009-12-15 15:01:08 -08:00
|
|
|
static PRUint16 xtoint (const PRUint8 *ii)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
return (PRUint16) ((ii [0]) | (ii [1] << 8));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* x t o l o n g
|
|
|
|
*
|
|
|
|
* Converts a four byte ugly endianed integer
|
|
|
|
* to our platform's integer.
|
|
|
|
*/
|
2009-12-15 15:01:08 -08:00
|
|
|
static PRUint32 xtolong (const PRUint8 *ll)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
return (PRUint32)( (ll [0] << 0) |
|
|
|
|
(ll [1] << 8) |
|
|
|
|
(ll [2] << 16) |
|
|
|
|
(ll [3] << 24) );
|
|
|
|
}
|
|
|
|
|
2009-12-15 15:01:08 -08:00
|
|
|
/*
|
|
|
|
* GetModTime
|
|
|
|
*
|
|
|
|
* returns last modification time in microseconds
|
|
|
|
*/
|
|
|
|
static PRTime GetModTime(PRUint16 aDate, PRUint16 aTime)
|
|
|
|
{
|
|
|
|
// Note that on DST shift we can't handle correctly the hour that is valid
|
|
|
|
// in both DST zones
|
|
|
|
PRExplodedTime time;
|
|
|
|
|
|
|
|
time.tm_usec = 0;
|
|
|
|
|
|
|
|
time.tm_hour = (aTime >> 11) & 0x1F;
|
|
|
|
time.tm_min = (aTime >> 5) & 0x3F;
|
|
|
|
time.tm_sec = (aTime & 0x1F) * 2;
|
|
|
|
|
|
|
|
time.tm_year = (aDate >> 9) + 1980;
|
|
|
|
time.tm_month = ((aDate >> 5) & 0x0F) - 1;
|
|
|
|
time.tm_mday = aDate & 0x1F;
|
|
|
|
|
|
|
|
time.tm_params.tp_gmt_offset = 0;
|
|
|
|
time.tm_params.tp_dst_offset = 0;
|
|
|
|
|
|
|
|
PR_NormalizeTime(&time, PR_GMTParameters);
|
|
|
|
time.tm_params.tp_gmt_offset = PR_LocalTimeParameters(&time).tp_gmt_offset;
|
|
|
|
PR_NormalizeTime(&time, PR_GMTParameters);
|
|
|
|
time.tm_params.tp_dst_offset = PR_LocalTimeParameters(&time).tp_dst_offset;
|
|
|
|
|
|
|
|
return PR_ImplodeTime(&time);
|
|
|
|
}
|
|
|
|
|
|
|
|
PRUint32 nsZipItem::LocalOffset()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-10-17 08:54:54 -07:00
|
|
|
return xtolong(central->localhdr_offset);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-12-15 15:01:08 -08:00
|
|
|
PRUint32 nsZipItem::Size()
|
2009-10-17 08:54:54 -07:00
|
|
|
{
|
|
|
|
return isSynthetic ? 0 : xtolong(central->size);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-12-15 15:01:08 -08:00
|
|
|
PRUint32 nsZipItem::RealSize()
|
2009-10-17 08:54:54 -07:00
|
|
|
{
|
|
|
|
return isSynthetic ? 0 : xtolong(central->orglen);
|
|
|
|
}
|
|
|
|
|
2009-12-15 15:01:08 -08:00
|
|
|
PRUint32 nsZipItem::CRC32()
|
2009-10-17 08:54:54 -07:00
|
|
|
{
|
|
|
|
return isSynthetic ? 0 : xtolong(central->crc32);
|
|
|
|
}
|
|
|
|
|
2009-12-15 15:01:08 -08:00
|
|
|
PRUint16 nsZipItem::Date()
|
2009-10-17 08:54:54 -07:00
|
|
|
{
|
|
|
|
return isSynthetic ? kSyntheticDate : xtoint(central->date);
|
|
|
|
}
|
|
|
|
|
2009-12-15 15:01:08 -08:00
|
|
|
PRUint16 nsZipItem::Time()
|
2009-10-17 08:54:54 -07:00
|
|
|
{
|
|
|
|
return isSynthetic ? kSyntheticTime : xtoint(central->time);
|
|
|
|
}
|
|
|
|
|
2009-12-15 15:01:08 -08:00
|
|
|
PRUint16 nsZipItem::Compression()
|
2009-10-17 08:54:54 -07:00
|
|
|
{
|
|
|
|
return isSynthetic ? STORED : xtoint(central->method);
|
|
|
|
}
|
|
|
|
|
2009-12-15 15:01:08 -08:00
|
|
|
bool nsZipItem::IsDirectory()
|
2009-10-17 08:54:54 -07:00
|
|
|
{
|
|
|
|
return isSynthetic || ((nameLength > 0) && ('/' == Name()[nameLength - 1]));
|
|
|
|
}
|
|
|
|
|
2009-12-15 15:01:08 -08:00
|
|
|
PRUint16 nsZipItem::Mode()
|
2009-10-17 08:54:54 -07:00
|
|
|
{
|
|
|
|
if (isSynthetic) return 0755;
|
|
|
|
return ((PRUint16)(central->external_attributes[2]) | 0x100);
|
|
|
|
}
|
|
|
|
|
2009-12-15 15:01:08 -08:00
|
|
|
const PRUint8 * nsZipItem::GetExtraField(PRUint16 aTag, PRUint16 *aBlockSize)
|
|
|
|
{
|
2010-11-11 12:13:48 -08:00
|
|
|
if (isSynthetic) return nsnull;
|
|
|
|
MOZ_WIN_MEM_TRY_BEGIN
|
2009-12-15 15:01:08 -08:00
|
|
|
const unsigned char *buf = ((const unsigned char*)central) + ZIPCENTRAL_SIZE +
|
|
|
|
nameLength;
|
|
|
|
PRUint32 buflen = (PRUint32)xtoint(central->extrafield_len);
|
|
|
|
PRUint32 pos = 0;
|
|
|
|
PRUint16 tag, blocksize;
|
|
|
|
|
|
|
|
while (buf && (pos + 4) <= buflen) {
|
|
|
|
tag = xtoint(buf + pos);
|
|
|
|
blocksize = xtoint(buf + pos + 2);
|
|
|
|
|
|
|
|
if (aTag == tag && (pos + 4 + blocksize) <= buflen) {
|
|
|
|
*aBlockSize = blocksize;
|
|
|
|
return buf + pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
pos += blocksize + 4;
|
|
|
|
}
|
|
|
|
|
2010-11-11 12:13:48 -08:00
|
|
|
MOZ_WIN_MEM_TRY_CATCH(return nsnull)
|
|
|
|
return nsnull;
|
2009-12-15 15:01:08 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PRTime nsZipItem::LastModTime()
|
|
|
|
{
|
|
|
|
if (isSynthetic) return GetModTime(kSyntheticDate, kSyntheticTime);
|
|
|
|
|
|
|
|
// Try to read timestamp from extra field
|
|
|
|
PRUint16 blocksize;
|
|
|
|
const PRUint8 *tsField = GetExtraField(EXTENDED_TIMESTAMP_FIELD, &blocksize);
|
|
|
|
if (tsField && blocksize >= 5 && tsField[4] & EXTENDED_TIMESTAMP_MODTIME) {
|
|
|
|
return (PRTime)(xtolong(tsField + 5)) * PR_USEC_PER_SEC;
|
|
|
|
}
|
|
|
|
|
|
|
|
return GetModTime(Date(), Time());
|
|
|
|
}
|
|
|
|
|
Bug 627277 - Remove (broken) BeOS support. r=biesi,dwitte,gavin,joe,jorendorff,josh,khuey,mfinkle,neil,Pike,roc,shaver,smontagu,taras
2011-02-19 11:10:24 -08:00
|
|
|
#ifdef XP_UNIX
|
2009-12-15 15:01:08 -08:00
|
|
|
bool nsZipItem::IsSymlink()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-10-17 08:54:54 -07:00
|
|
|
if (isSynthetic) return false;
|
|
|
|
return (xtoint(central->external_attributes+2) & S_IFMT) == S_IFLNK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
#endif
|
2009-12-15 15:01:08 -08:00
|
|
|
|
2010-06-04 14:10:23 -07:00
|
|
|
nsZipCursor::nsZipCursor(nsZipItem *item, nsZipArchive *aZip, PRUint8* aBuf, PRUint32 aBufSize, bool doCRC) :
|
|
|
|
mItem(item),
|
|
|
|
mBuf(aBuf),
|
|
|
|
mBufSize(aBufSize),
|
|
|
|
mDoCRC(doCRC)
|
|
|
|
{
|
|
|
|
if (mItem->Compression() == DEFLATED) {
|
2010-07-11 05:49:52 -07:00
|
|
|
#ifdef DEBUG
|
|
|
|
nsresult status =
|
|
|
|
#endif
|
|
|
|
gZlibInit(&mZs);
|
2010-06-04 14:10:23 -07:00
|
|
|
NS_ASSERTION(status == NS_OK, "Zlib failed to initialize");
|
|
|
|
NS_ASSERTION(aBuf, "Must pass in a buffer for DEFLATED nsZipItem");
|
|
|
|
}
|
|
|
|
|
|
|
|
mZs.avail_in = item->Size();
|
|
|
|
mZs.next_in = (Bytef*)aZip->GetData(item);
|
|
|
|
|
|
|
|
if (doCRC)
|
|
|
|
mCRC = crc32(0L, Z_NULL, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsZipCursor::~nsZipCursor()
|
|
|
|
{
|
|
|
|
if (mItem->Compression() == DEFLATED) {
|
|
|
|
inflateEnd(&mZs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PRUint8* nsZipCursor::Read(PRUint32 *aBytesRead) {
|
|
|
|
int zerr;
|
|
|
|
PRUint8 *buf = nsnull;
|
|
|
|
bool verifyCRC = true;
|
|
|
|
|
|
|
|
if (!mZs.next_in)
|
|
|
|
return nsnull;
|
2010-11-11 12:13:48 -08:00
|
|
|
MOZ_WIN_MEM_TRY_BEGIN
|
2010-06-04 14:10:23 -07:00
|
|
|
switch (mItem->Compression()) {
|
|
|
|
case STORED:
|
|
|
|
*aBytesRead = mZs.avail_in;
|
|
|
|
buf = mZs.next_in;
|
|
|
|
mZs.next_in += mZs.avail_in;
|
|
|
|
mZs.avail_in = 0;
|
|
|
|
break;
|
|
|
|
case DEFLATED:
|
|
|
|
buf = mBuf;
|
|
|
|
mZs.next_out = buf;
|
|
|
|
mZs.avail_out = mBufSize;
|
|
|
|
|
|
|
|
zerr = inflate(&mZs, Z_PARTIAL_FLUSH);
|
|
|
|
if (zerr != Z_OK && zerr != Z_STREAM_END)
|
|
|
|
return nsnull;
|
|
|
|
|
|
|
|
*aBytesRead = mZs.next_out - buf;
|
|
|
|
verifyCRC = (zerr == Z_STREAM_END);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mDoCRC) {
|
|
|
|
mCRC = crc32(mCRC, (const unsigned char*)buf, *aBytesRead);
|
|
|
|
if (verifyCRC && mCRC != mItem->CRC32())
|
|
|
|
return nsnull;
|
|
|
|
}
|
2010-11-11 12:13:48 -08:00
|
|
|
MOZ_WIN_MEM_TRY_CATCH(return nsnull)
|
2010-06-04 14:10:23 -07:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsZipItemPtr_base::nsZipItemPtr_base(nsZipArchive *aZip, const char * aEntryName, bool doCRC) :
|
|
|
|
mReturnBuf(nsnull)
|
|
|
|
{
|
|
|
|
// make sure the ziparchive hangs around
|
|
|
|
mZipHandle = aZip->GetFD();
|
2010-11-11 12:13:48 -08:00
|
|
|
|
2010-06-04 14:10:23 -07:00
|
|
|
nsZipItem* item = aZip->GetItem(aEntryName);
|
|
|
|
if (!item)
|
|
|
|
return;
|
|
|
|
|
|
|
|
PRUint32 size = 0;
|
|
|
|
if (item->Compression() == DEFLATED) {
|
|
|
|
size = item->RealSize();
|
|
|
|
mAutoBuf = new PRUint8[size];
|
|
|
|
}
|
|
|
|
|
|
|
|
nsZipCursor cursor(item, aZip, mAutoBuf, size, doCRC);
|
|
|
|
mReturnBuf = cursor.Read(&mReadlen);
|
|
|
|
if (!mReturnBuf)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (mReadlen != item->RealSize()) {
|
|
|
|
NS_ASSERTION(mReadlen == item->RealSize(), "nsZipCursor underflow");
|
|
|
|
mReturnBuf = nsnull;
|
|
|
|
}
|
|
|
|
}
|