1999-03-09 05:13:56 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2004-04-19 02:01:16 +04:00
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
1999-03-09 05:13:56 +03:00
|
|
|
*
|
2004-04-19 02:01:16 +04:00
|
|
|
* 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/
|
1999-03-09 05:13:56 +03:00
|
|
|
*
|
2004-04-19 02:01:16 +04: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-03-09 05:13:56 +03:00
|
|
|
*
|
2004-04-19 02:01:16 +04:00
|
|
|
* The Original Code is Mozilla Communicator client code, released
|
|
|
|
* March 31, 1998.
|
1999-03-09 05:13:56 +03:00
|
|
|
*
|
2004-04-19 02:01:16 +04:00
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998-1999
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Daniel Veditz <dveditz@netscape.com>
|
|
|
|
* Samir Gehani <sgehani@netscape.com>
|
|
|
|
* Mitch Stoltz <mstoltz@netscape.com>
|
2009-08-13 00:50:12 +04:00
|
|
|
* Taras Glek <tglek@mozilla.com>
|
2004-04-19 02:01:16 +04: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 ***** */
|
1999-03-09 05:13:56 +03:00
|
|
|
|
1999-06-23 10:16:28 +04:00
|
|
|
#ifndef nsZipArchive_h_
|
|
|
|
#define nsZipArchive_h_
|
1999-03-09 05:13:56 +03:00
|
|
|
|
1999-05-19 07:21:03 +04:00
|
|
|
#define ZIP_TABSIZE 256
|
2009-10-04 21:20:45 +04:00
|
|
|
#define ZIP_BUFLEN (4*1024) /* Used as output buffer when deflating items to a file */
|
1999-05-19 07:21:03 +04:00
|
|
|
|
2009-10-17 19:54:54 +04:00
|
|
|
#define PL_ARENA_CONST_ALIGN_MASK (sizeof(void*)-1)
|
2002-03-11 23:18:35 +03:00
|
|
|
#include "plarena.h"
|
2003-03-14 21:59:51 +03:00
|
|
|
|
|
|
|
#include "zlib.h"
|
2009-10-17 19:54:54 +04:00
|
|
|
#include "zipstruct.h"
|
2009-08-13 00:50:12 +04:00
|
|
|
#include "nsAutoPtr.h"
|
2010-01-23 19:40:21 +03:00
|
|
|
#include "nsILocalFile.h"
|
2003-03-14 21:59:51 +03:00
|
|
|
|
1999-05-19 07:21:03 +04:00
|
|
|
class nsZipFind;
|
1999-03-09 05:13:56 +03:00
|
|
|
|
2003-03-14 21:59:51 +03:00
|
|
|
struct PRFileDesc;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This file defines some of the basic structures used by libjar to
|
|
|
|
* read Zip files. It makes use of zlib in order to do the decompression.
|
|
|
|
*
|
2006-05-02 23:33:09 +04:00
|
|
|
* A few notes on the classes/structs:
|
2003-03-14 21:59:51 +03:00
|
|
|
* nsZipArchive represents a single Zip file, and maintains an index
|
|
|
|
* of all the items in the file.
|
|
|
|
* nsZipItem represents a single item (file) in the Zip archive.
|
|
|
|
* nsZipFind represents the metadata involved in doing a search,
|
|
|
|
* and current state of the iteration of found objects.
|
2006-05-02 23:33:09 +04:00
|
|
|
* 'MT''safe' reading from the zipfile is performed through JARInputStream,
|
|
|
|
* which maintains its own file descriptor, allowing for multiple reads
|
|
|
|
* concurrently from the same zip file.
|
2003-03-14 21:59:51 +03:00
|
|
|
*/
|
|
|
|
|
1999-04-28 04:34:30 +04:00
|
|
|
/**
|
2006-05-02 23:33:09 +04:00
|
|
|
* nsZipItem -- a helper struct for nsZipArchive
|
1999-04-28 04:34:30 +04:00
|
|
|
*
|
|
|
|
* each nsZipItem represents one file in the archive and all the
|
|
|
|
* information needed to manipulate it.
|
|
|
|
*/
|
2009-10-17 19:54:54 +04:00
|
|
|
class nsZipItem
|
1999-04-28 04:34:30 +04:00
|
|
|
{
|
2009-10-17 19:54:54 +04:00
|
|
|
public:
|
|
|
|
const char* Name() { return ((const char*)central) + ZIPCENTRAL_SIZE; }
|
|
|
|
|
2009-12-16 02:01:08 +03:00
|
|
|
PRUint32 LocalOffset();
|
|
|
|
PRUint32 Size();
|
|
|
|
PRUint32 RealSize();
|
|
|
|
PRUint32 CRC32();
|
|
|
|
PRUint16 Date();
|
|
|
|
PRUint16 Time();
|
|
|
|
PRUint16 Compression();
|
|
|
|
bool IsDirectory();
|
|
|
|
PRUint16 Mode();
|
|
|
|
const PRUint8* GetExtraField(PRUint16 aTag, PRUint16 *aBlockSize);
|
|
|
|
PRTime LastModTime();
|
1999-04-28 04:34:30 +04:00
|
|
|
|
2006-05-02 23:33:09 +04:00
|
|
|
#if defined(XP_UNIX) || defined(XP_BEOS)
|
2009-12-16 02:01:08 +03:00
|
|
|
bool IsSymlink();
|
2003-07-16 02:18:04 +04:00
|
|
|
#endif
|
2000-03-28 07:38:06 +04:00
|
|
|
|
2009-10-17 19:54:54 +04:00
|
|
|
nsZipItem* next;
|
|
|
|
const ZipCentral* central;
|
|
|
|
PRUint16 nameLength;
|
|
|
|
bool isSynthetic;
|
1999-04-28 04:34:30 +04:00
|
|
|
};
|
|
|
|
|
2009-08-13 00:50:12 +04:00
|
|
|
class nsZipHandle;
|
2009-10-04 21:20:45 +04:00
|
|
|
|
1999-04-28 04:34:30 +04:00
|
|
|
/**
|
|
|
|
* nsZipArchive -- a class for reading the PKZIP file format.
|
|
|
|
*
|
|
|
|
*/
|
1999-03-09 05:13:56 +03:00
|
|
|
class nsZipArchive
|
|
|
|
{
|
2006-05-02 23:33:09 +04:00
|
|
|
friend class nsZipFind;
|
|
|
|
|
1999-03-09 05:13:56 +03:00
|
|
|
public:
|
1999-04-28 04:34:30 +04:00
|
|
|
/** constructing does not open the archive. See OpenArchive() */
|
1999-03-09 05:13:56 +03:00
|
|
|
nsZipArchive();
|
|
|
|
|
|
|
|
/** destructing the object closes the archive */
|
|
|
|
~nsZipArchive();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* OpenArchive
|
|
|
|
*
|
|
|
|
* It's an error to call this more than once on the same nsZipArchive
|
|
|
|
* object. If we were allowed to use exceptions this would have been
|
|
|
|
* part of the constructor
|
|
|
|
*
|
2006-05-02 23:33:09 +04:00
|
|
|
* @param fd File descriptor of file to open
|
1999-03-09 05:13:56 +03:00
|
|
|
* @return status code
|
|
|
|
*/
|
2010-01-23 19:40:21 +03:00
|
|
|
nsresult OpenArchive(nsIFile *aZipFile);
|
1999-11-12 09:13:13 +03:00
|
|
|
|
2000-12-27 10:05:55 +03:00
|
|
|
/**
|
|
|
|
* Test the integrity of items in this archive by running
|
|
|
|
* a CRC check after extracting each item into a memory
|
|
|
|
* buffer. If an entry name is supplied only the
|
|
|
|
* specified item is tested. Else, if null is supplied
|
|
|
|
* then all the items in the archive are tested.
|
|
|
|
*
|
|
|
|
* @return status code
|
|
|
|
*/
|
2006-05-02 23:33:09 +04:00
|
|
|
nsresult Test(const char *aEntryName);
|
2000-12-27 10:05:55 +03:00
|
|
|
|
1999-11-12 09:13:13 +03:00
|
|
|
/**
|
|
|
|
* Closes an open archive.
|
|
|
|
*/
|
2006-05-02 23:33:09 +04:00
|
|
|
nsresult CloseArchive();
|
1999-11-12 09:13:13 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* GetItem
|
2006-05-02 23:33:09 +04:00
|
|
|
* @param aEntryName Name of file in the archive
|
|
|
|
* @return pointer to nsZipItem
|
2001-02-23 03:15:04 +03:00
|
|
|
*/
|
2006-05-02 23:33:09 +04:00
|
|
|
nsZipItem* GetItem(const char * aEntryName);
|
1999-11-12 09:13:13 +03:00
|
|
|
|
1999-10-26 23:43:26 +04:00
|
|
|
/**
|
2006-05-02 23:33:09 +04:00
|
|
|
* ExtractFile
|
1999-10-26 23:43:26 +04:00
|
|
|
*
|
2006-05-02 23:33:09 +04:00
|
|
|
* @param zipEntry Name of file in archive to extract
|
|
|
|
* @param outFD Filedescriptor to write contents to
|
|
|
|
* @param outname Name of file to write to
|
1999-10-26 23:43:26 +04:00
|
|
|
* @return status code
|
|
|
|
*/
|
2006-05-02 23:33:09 +04:00
|
|
|
nsresult ExtractFile(nsZipItem * zipEntry, const char *outname, PRFileDesc * outFD);
|
1999-03-09 05:13:56 +03:00
|
|
|
|
1999-05-19 07:21:03 +04:00
|
|
|
/**
|
|
|
|
* FindInit
|
|
|
|
*
|
|
|
|
* Initializes a search for files in the archive. FindNext() returns
|
2006-05-02 23:33:09 +04:00
|
|
|
* the actual matches. The nsZipFind must be deleted when you're done
|
1999-05-19 07:21:03 +04:00
|
|
|
*
|
|
|
|
* @param aPattern a string or RegExp pattern to search for
|
|
|
|
* (may be NULL to find all files in archive)
|
2006-03-30 02:10:37 +04:00
|
|
|
* @param aFind a pointer to a pointer to a structure used
|
|
|
|
* in FindNext. In the case of an error this
|
|
|
|
* will be set to NULL.
|
|
|
|
* @return status code
|
1999-05-19 07:21:03 +04:00
|
|
|
*/
|
2006-03-30 02:10:37 +04:00
|
|
|
PRInt32 FindInit(const char * aPattern, nsZipFind** aFind);
|
1999-05-19 07:21:03 +04:00
|
|
|
|
2009-10-04 21:20:45 +04:00
|
|
|
/*
|
|
|
|
* Gets an undependent handle to the mapped file.
|
|
|
|
*/
|
|
|
|
nsZipHandle* GetFD();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get pointer to the data of the item.
|
|
|
|
* @param aItem Pointer to nsZipItem
|
|
|
|
* reutrns null when zip file is corrupt.
|
1999-05-19 07:21:03 +04:00
|
|
|
*/
|
2009-10-04 21:20:45 +04:00
|
|
|
PRUint8* GetData(nsZipItem* aItem);
|
2001-09-21 03:33:23 +04:00
|
|
|
|
1999-03-09 05:13:56 +03:00
|
|
|
private:
|
|
|
|
//--- private members ---
|
2001-12-15 09:31:49 +03:00
|
|
|
|
1999-04-28 04:34:30 +04:00
|
|
|
nsZipItem* mFiles[ZIP_TABSIZE];
|
2002-03-11 23:18:35 +03:00
|
|
|
PLArenaPool mArena;
|
1999-11-03 02:46:09 +03:00
|
|
|
|
2006-09-24 19:22:36 +04:00
|
|
|
// Whether we synthesized the directory entries
|
2009-10-04 21:20:45 +04:00
|
|
|
bool mBuiltSynthetics;
|
2006-09-24 19:22:36 +04:00
|
|
|
|
2009-08-13 00:50:12 +04:00
|
|
|
// file handle
|
|
|
|
nsRefPtr<nsZipHandle> mFd;
|
1999-03-09 05:13:56 +03:00
|
|
|
//--- private methods ---
|
|
|
|
|
1999-04-28 04:34:30 +04:00
|
|
|
nsZipArchive& operator=(const nsZipArchive& rhs); // prevent assignments
|
|
|
|
nsZipArchive(const nsZipArchive& rhs); // prevent copies
|
1999-03-09 05:13:56 +03:00
|
|
|
|
2009-10-17 19:54:54 +04:00
|
|
|
nsZipItem* CreateZipItem();
|
2006-05-02 23:33:09 +04:00
|
|
|
nsresult BuildFileList();
|
2006-09-24 19:22:36 +04:00
|
|
|
nsresult BuildSynthetics();
|
2010-06-03 04:05:49 +04:00
|
|
|
|
|
|
|
nsresult CopyItemToDisk(nsZipItem* item, PRFileDesc* outFD);
|
|
|
|
nsresult InflateItem(nsZipItem* item, PRFileDesc* outFD);
|
2009-08-13 00:50:12 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
class nsZipHandle {
|
|
|
|
friend class nsZipArchive;
|
|
|
|
public:
|
|
|
|
static nsresult Init(PRFileDesc *fd, nsZipHandle **ret NS_OUTPARAM);
|
|
|
|
|
|
|
|
NS_METHOD_(nsrefcnt) AddRef(void);
|
|
|
|
NS_METHOD_(nsrefcnt) Release(void);
|
|
|
|
|
|
|
|
protected:
|
2009-10-04 21:20:45 +04:00
|
|
|
PRUint8 * mFileData; /* pointer to mmaped file */
|
|
|
|
PRUint32 mLen; /* length of file and memory mapped area */
|
2009-08-13 00:50:12 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
nsZipHandle();
|
|
|
|
~nsZipHandle();
|
|
|
|
|
2009-10-04 21:20:45 +04:00
|
|
|
PRFileMap * mMap; /* nspr datastructure for mmap */
|
|
|
|
nsrefcnt mRefCnt; /* ref count */
|
1999-03-09 05:13:56 +03:00
|
|
|
};
|
|
|
|
|
1999-05-19 07:21:03 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* nsZipFind
|
|
|
|
*
|
|
|
|
* a helper class for nsZipArchive, representing a search
|
|
|
|
*/
|
|
|
|
class nsZipFind
|
|
|
|
{
|
|
|
|
public:
|
2003-03-14 21:59:51 +03:00
|
|
|
nsZipFind(nsZipArchive* aZip, char* aPattern, PRBool regExp);
|
1999-05-19 07:21:03 +04:00
|
|
|
~nsZipFind();
|
|
|
|
|
2009-10-17 19:54:54 +04:00
|
|
|
nsresult FindNext(const char** aResult, PRUint16* aNameLen);
|
1999-06-23 10:16:28 +04:00
|
|
|
|
1999-05-19 07:21:03 +04:00
|
|
|
private:
|
|
|
|
nsZipArchive* mArchive;
|
|
|
|
char* mPattern;
|
|
|
|
nsZipItem* mItem;
|
2006-05-02 23:33:09 +04:00
|
|
|
PRUint16 mSlot;
|
|
|
|
PRPackedBool mRegExp;
|
1999-05-19 07:21:03 +04:00
|
|
|
|
|
|
|
//-- prevent copies and assignments
|
|
|
|
nsZipFind& operator=(const nsZipFind& rhs);
|
|
|
|
nsZipFind(const nsZipFind& rhs);
|
|
|
|
};
|
1999-06-23 10:16:28 +04:00
|
|
|
|
2006-05-02 23:33:09 +04:00
|
|
|
nsresult gZlibInit(z_stream *zs);
|
|
|
|
|
1999-06-24 07:13:10 +04:00
|
|
|
#endif /* nsZipArchive_h_ */
|