1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is Mozilla Communicator client code, released
18 * The Initial Developer of the Original Code is
19 * Netscape Communications Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 1998-1999
21 * the Initial Developer. All Rights Reserved.
24 * Daniel Veditz <dveditz@netscape.com>
25 * Samir Gehani <sgehani@netscape.com>
26 * Mitch Stoltz <mstoltz@netscape.com>
28 * Alternatively, the contents of this file may be used under the terms of
29 * either the GNU General Public License Version 2 or later (the "GPL"), or
30 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
31 * in which case the provisions of the GPL or the LGPL are applicable instead
32 * of those above. If you wish to allow use of your version of this file only
33 * under the terms of either the GPL or the LGPL, and not to allow others to
34 * use your version of this file under the terms of the MPL, indicate your
35 * decision by deleting the provisions above and replace them with the notice
36 * and other provisions required by the GPL or the LGPL. If you do not delete
37 * the provisions above, a recipient may use your version of this file under
38 * the terms of any one of the MPL, the GPL or the LGPL.
40 * ***** END LICENSE BLOCK ***** */
42 #ifndef nsZipArchive_h_
43 #define nsZipArchive_h_
45 #define ZIP_MAGIC 0x5A49505FL /* "ZIP_" */
46 #define ZIPFIND_MAGIC 0x5A495046L /* "ZIPF" */
47 #define ZIP_TABSIZE 256
48 // Keep this odd. The -1 is significant.
49 #define ZIP_BUFLEN (4 * 1024 - 1)
52 #define nsZipArchive nsZipArchiveStandalone
54 #define ZIP_Seek(fd,p,m) (fseek((fd),(p),(m))==0)
58 #define PL_ARENA_CONST_ALIGN_MASK 7
60 #define ZIP_Seek(fd,p,m) (PR_Seek((fd),((PROffset32)p),(m))==((PROffset32)p))
68 class nsZipItemMetadata
;
75 * This file defines some of the basic structures used by libjar to
76 * read Zip files. It makes use of zlib in order to do the decompression.
78 * A few notes on the classes/structs:
79 * nsZipArchive represents a single Zip file, and maintains an index
80 * of all the items in the file.
81 * nsZipItem represents a single item (file) in the Zip archive.
82 * nsZipFind represents the metadata involved in doing a search,
83 * and current state of the iteration of found objects.
85 * There is a lot of #ifdef STANDALONE here, and that is so that these
86 * basic structures can be reused in a standalone static
87 * library. In order for the code to be reused, these structures
88 * should never use anything from XPCOM, including such obvious things
89 * as NS_ASSERTION(). Instead, use the basic NSPR equivalents.
91 * There is one key difference in the way that this code behaves in
92 * STANDALONE mode. The nsZipArchive owns a single file descriptor and
93 * that is used to read the ZIP file index, and for 'Test' and 'Extract'.
94 * Since there is only one nsZipArchive per file, you can only Test/Extract
95 * one file at a time from the Zip file.
96 * 'MT''safe' reading from the zipfile is performed through JARInputStream,
97 * which maintains its own file descriptor, allowing for multiple reads
98 * concurrently from the same zip file.
102 * nsZipItem -- a helper struct for nsZipArchive
104 * each nsZipItem represents one file in the archive and all the
105 * information needed to manipulate it.
111 PRUint32 headerOffset
;
113 PRUint32 size
; /* size in original file */
114 PRUint32 realsize
; /* inflated size */
118 * Keep small items together, to avoid overhead.
124 PRPackedBool hasDataOffset
: 1;
125 PRPackedBool isDirectory
: 1;
126 PRPackedBool isSynthetic
: 1; /* whether item is an actual zip entry or was
127 generated as part of a real entry's path,
128 e.g. foo/ in a zip containing only foo/a.txt
129 and no foo/ entry is synthetic */
130 #if defined(XP_UNIX) || defined(XP_BEOS)
131 PRPackedBool isSymlink
: 1;
134 char name
[1]; // actually, bigger than 1
138 * nsZipArchive -- a class for reading the PKZIP file format.
143 friend class nsZipFind
;
147 /** cookie used to validate supposed objects passed from C code */
148 const PRInt32 kMagic
;
151 /** constructing does not open the archive. See OpenArchive() */
154 /** destructing the object closes the archive */
160 * It's an error to call this more than once on the same nsZipArchive
161 * object. If we were allowed to use exceptions this would have been
162 * part of the constructor
164 * @param fd File descriptor of file to open
165 * @return status code
167 nsresult
OpenArchive(PRFileDesc
* fd
);
170 * Test the integrity of items in this archive by running
171 * a CRC check after extracting each item into a memory
172 * buffer. If an entry name is supplied only the
173 * specified item is tested. Else, if null is supplied
174 * then all the items in the archive are tested.
176 * @return status code
178 nsresult
Test(const char *aEntryName
);
181 * Closes an open archive.
183 nsresult
CloseArchive();
187 * @param aEntryName Name of file in the archive
188 * @return pointer to nsZipItem
190 nsZipItem
* GetItem(const char * aEntryName
);
195 * @param zipEntry Name of file in archive to extract
196 * @param outFD Filedescriptor to write contents to
197 * @param outname Name of file to write to
198 * @return status code
200 nsresult
ExtractFile(nsZipItem
* zipEntry
, const char *outname
, PRFileDesc
* outFD
);
205 * Initializes a search for files in the archive. FindNext() returns
206 * the actual matches. The nsZipFind must be deleted when you're done
208 * @param aPattern a string or RegExp pattern to search for
209 * (may be NULL to find all files in archive)
210 * @param aFind a pointer to a pointer to a structure used
211 * in FindNext. In the case of an error this
212 * will be set to NULL.
213 * @return status code
215 PRInt32
FindInit(const char * aPattern
, nsZipFind
** aFind
);
218 * Moves the filepointer aFd to the start of data of the aItem.
219 * @param aItem Pointer to nsZipItem
220 * @param aFd The filepointer to move
222 nsresult
SeekToItem(nsZipItem
* aItem
, PRFileDesc
* aFd
);
225 //--- private members ---
227 nsZipItem
* mFiles
[ZIP_TABSIZE
];
232 // Used for central directory reading, and for Test and Extract
235 // Whether we synthesized the directory entries
236 PRPackedBool mBuiltSynthetics
;
238 //--- private methods ---
240 nsZipArchive
& operator=(const nsZipArchive
& rhs
); // prevent assignments
241 nsZipArchive(const nsZipArchive
& rhs
); // prevent copies
243 nsZipItem
* CreateZipItem(PRUint16 namelen
);
244 nsresult
BuildFileList();
245 nsresult
BuildSynthetics();
247 nsresult
CopyItemToDisk(PRUint32 size
, PRUint32 crc
, PRFileDesc
* outFD
);
248 nsresult
InflateItem(const nsZipItem
* aItem
, PRFileDesc
* outFD
);
255 * a helper class for nsZipArchive, representing a search
261 const PRInt32 kMagic
;
264 nsZipFind(nsZipArchive
* aZip
, char* aPattern
, PRBool regExp
);
267 nsresult
FindNext(const char ** aResult
);
270 nsZipArchive
* mArchive
;
274 PRPackedBool mRegExp
;
276 //-- prevent copies and assignments
277 nsZipFind
& operator=(const nsZipFind
& rhs
);
278 nsZipFind(const nsZipFind
& rhs
);
281 nsresult
gZlibInit(z_stream
*zs
);
283 #endif /* nsZipArchive_h_ */