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>
|
|
|
|
*
|
|
|
|
* 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_MAGIC 0x5A49505FL /* "ZIP_" */
|
|
|
|
#define ZIPFIND_MAGIC 0x5A495046L /* "ZIPF" */
|
|
|
|
#define ZIP_TABSIZE 256
|
2001-12-01 03:50:13 +03:00
|
|
|
// Keep this odd. The -1 is significant.
|
|
|
|
#define ZIP_BUFLEN (4 * 1024 - 1)
|
1999-05-19 07:21:03 +04:00
|
|
|
|
2000-08-19 02:09:26 +04:00
|
|
|
#ifdef STANDALONE
|
|
|
|
#define nsZipArchive nsZipArchiveStandalone
|
2001-04-03 10:35:13 +04:00
|
|
|
|
|
|
|
#define ZIP_Seek(fd,p,m) (fseek((fd),(p),(m))==0)
|
2003-03-14 21:59:51 +03:00
|
|
|
|
2001-04-03 10:35:13 +04:00
|
|
|
#else
|
2002-03-11 23:18:35 +03:00
|
|
|
|
|
|
|
#define PL_ARENA_CONST_ALIGN_MASK 7
|
|
|
|
#include "plarena.h"
|
2001-12-15 09:31:49 +03:00
|
|
|
#define ZIP_Seek(fd,p,m) (PR_Seek((fd),((PROffset32)p),(m))==((PROffset32)p))
|
2003-03-14 21:59:51 +03:00
|
|
|
|
2000-08-19 02:09:26 +04:00
|
|
|
#endif
|
1999-05-19 07:21:03 +04:00
|
|
|
|
2003-03-14 21:59:51 +03:00
|
|
|
#include "zlib.h"
|
|
|
|
|
1999-05-19 07:21:03 +04:00
|
|
|
class nsZipFind;
|
2003-03-14 21:59:51 +03:00
|
|
|
class nsZipReadState;
|
2000-01-29 03:03:57 +03:00
|
|
|
class nsZipItemMetadata;
|
1999-03-09 05:13:56 +03:00
|
|
|
|
2003-03-14 21:59:51 +03:00
|
|
|
#ifndef STANDALONE
|
|
|
|
struct PRFileDesc;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* There is a lot of #ifdef STANDALONE here, and that is so that these
|
|
|
|
* basic structures can be reused in a standalone static
|
|
|
|
* library. In order for the code to be reused, these structures
|
|
|
|
* should never use anything from XPCOM, including such obvious things
|
|
|
|
* as NS_ASSERTION(). Instead, use the basic NSPR equivalents.
|
|
|
|
*
|
|
|
|
* There is one key difference in the way that this code behaves in
|
2006-05-02 23:33:09 +04:00
|
|
|
* STANDALONE mode. The nsZipArchive owns a single file descriptor and
|
|
|
|
* that is used to read the ZIP file index, and for 'Test' and 'Extract'.
|
|
|
|
* Since there is only one nsZipArchive per file, you can only Test/Extract
|
|
|
|
* one file at a time from the Zip file.
|
|
|
|
* '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.
|
|
|
|
*/
|
2006-05-02 23:33:09 +04:00
|
|
|
struct nsZipItem
|
1999-04-28 04:34:30 +04:00
|
|
|
{
|
2001-12-07 04:16:20 +03:00
|
|
|
nsZipItem* next;
|
|
|
|
|
2006-05-02 23:33:09 +04:00
|
|
|
PRUint32 offset;
|
|
|
|
PRUint32 size; /* size in original file */
|
|
|
|
PRUint32 realsize; /* inflated size */
|
|
|
|
PRUint32 crc32;
|
1999-04-28 04:34:30 +04:00
|
|
|
|
2001-12-07 04:16:20 +03:00
|
|
|
/*
|
|
|
|
* Keep small items together, to avoid overhead.
|
|
|
|
*/
|
2006-05-02 23:33:09 +04:00
|
|
|
PRUint16 time;
|
|
|
|
PRUint16 date;
|
|
|
|
PRUint16 mode;
|
2001-12-07 04:16:20 +03:00
|
|
|
PRUint8 compression;
|
2006-05-02 23:33:09 +04:00
|
|
|
PRPackedBool hasDataOffset : 1;
|
|
|
|
PRPackedBool isDirectory : 1;
|
|
|
|
PRPackedBool isSynthetic : 1; /* whether item is an actual zip entry or was
|
|
|
|
generated as part of a real entry's path,
|
|
|
|
e.g. foo/ in a zip containing only foo/a.txt
|
|
|
|
and no foo/ entry is synthetic */
|
|
|
|
#if defined(XP_UNIX) || defined(XP_BEOS)
|
|
|
|
PRPackedBool isSymlink : 1;
|
2003-07-16 02:18:04 +04:00
|
|
|
#endif
|
2000-03-28 07:38:06 +04:00
|
|
|
|
2006-05-02 23:33:09 +04:00
|
|
|
char name[1]; // actually, bigger than 1
|
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:
|
2006-05-02 23:33:09 +04:00
|
|
|
#ifdef STANDALONE
|
1999-03-09 05:13:56 +03:00
|
|
|
/** cookie used to validate supposed objects passed from C code */
|
|
|
|
const PRInt32 kMagic;
|
2006-05-02 23:33:09 +04:00
|
|
|
#endif
|
2002-03-11 23:18:35 +03:00
|
|
|
|
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
|
|
|
|
*/
|
2006-05-02 23:33:09 +04:00
|
|
|
nsresult OpenArchive(PRFileDesc* fd);
|
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
|
|
|
|
|
|
|
/**
|
2006-05-02 23:33:09 +04:00
|
|
|
* Moves the filepointer aFd to the start of data of the aItem.
|
|
|
|
* @param aItem Pointer to nsZipItem
|
|
|
|
* @param aFd The filepointer to move
|
1999-05-19 07:21:03 +04:00
|
|
|
*/
|
2006-05-02 23:33:09 +04:00
|
|
|
nsresult SeekToItem(nsZipItem* aItem, PRFileDesc* aFd);
|
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
|
|
|
#ifndef STANDALONE
|
|
|
|
PLArenaPool mArena;
|
|
|
|
#endif
|
1999-11-03 02:46:09 +03:00
|
|
|
|
2006-05-02 23:33:09 +04:00
|
|
|
// Used for central directory reading, and for Test and Extract
|
|
|
|
PRFileDesc *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
|
|
|
|
2006-05-02 23:33:09 +04:00
|
|
|
nsZipItem* CreateZipItem(PRUint16 namelen);
|
|
|
|
nsresult BuildFileList();
|
2002-03-11 23:18:35 +03:00
|
|
|
|
2006-05-02 23:33:09 +04:00
|
|
|
nsresult CopyItemToDisk(PRUint32 size, PRUint32 crc, PRFileDesc* outFD);
|
|
|
|
nsresult InflateItem(const nsZipItem* aItem, PRFileDesc* outFD);
|
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:
|
2006-05-02 23:33:09 +04:00
|
|
|
#ifdef STANDALONE
|
1999-05-19 07:21:03 +04:00
|
|
|
const PRInt32 kMagic;
|
2006-05-02 23:33:09 +04:00
|
|
|
#endif
|
1999-05-19 07:21:03 +04:00
|
|
|
|
2003-03-14 21:59:51 +03:00
|
|
|
nsZipFind(nsZipArchive* aZip, char* aPattern, PRBool regExp);
|
1999-05-19 07:21:03 +04:00
|
|
|
~nsZipFind();
|
|
|
|
|
2006-05-02 23:33:09 +04:00
|
|
|
nsresult FindNext(const char ** aResult);
|
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_ */
|