Debian Codegraph @

bypy/windows/XUnzip.h

raw
1 // XUnzip.h Version 1.3
2 //
3 // Authors: Mark Adler et al. (see below)
4 //
5 // Modified by: Lucian Wischik
6 // [email protected]
7 //
8 // Version 1.0 - Turned C files into just a single CPP file
9 // - Made them compile cleanly as C++ files
10 // - Gave them simpler APIs
11 // - Added the ability to zip/unzip directly in memory without
12 // any intermediate files
13 //
14 // Modified by: Hans Dietrich
15 // [email protected]
16 //
17 ///////////////////////////////////////////////////////////////////////////////
18 //
19 // Lucian Wischik's comments:
20 // --------------------------
21 // THIS FILE is almost entirely based upon code by info-zip.
22 // It has been modified by Lucian Wischik.
23 // The original code may be found at http://www.info-zip.org
24 // The original copyright text follows.
25 //
26 ///////////////////////////////////////////////////////////////////////////////
27 //
28 // Original authors' comments:
29 // ---------------------------
30 // This is version 2002-Feb-16 of the Info-ZIP copyright and license. The
31 // definitive version of this document should be available at
32 // ftp://ftp.info-zip.org/pub/infozip/license.html indefinitely.
33 //
34 // Copyright (c) 1990-2002 Info-ZIP. All rights reserved.
35 //
36 // For the purposes of this copyright and license, "Info-ZIP" is defined as
37 // the following set of individuals:
38 //
39 // Mark Adler, John Bush, Karl Davis, Harald Denker, Jean-Michel Dubois,
40 // Jean-loup Gailly, Hunter Goatley, Ian Gorman, Chris Herborth, Dirk Haase,
41 // Greg Hartwig, Robert Heath, Jonathan Hudson, Paul Kienitz,
42 // David Kirschbaum, Johnny Lee, Onno van der Linden, Igor Mandrichenko,
43 // Steve P. Miller, Sergio Monesi, Keith Owens, George Petrov, Greg Roelofs,
44 // Kai Uwe Rommel, Steve Salisbury, Dave Smith, Christian Spieler,
45 // Antoine Verheijen, Paul von Behren, Rich Wales, Mike White
46 //
47 // This software is provided "as is", without warranty of any kind, express
48 // or implied. In no event shall Info-ZIP or its contributors be held liable
49 // for any direct, indirect, incidental, special or consequential damages
50 // arising out of the use of or inability to use this software.
51 //
52 // Permission is granted to anyone to use this software for any purpose,
53 // including commercial applications, and to alter it and redistribute it
54 // freely, subject to the following restrictions:
55 //
56 // 1. Redistributions of source code must retain the above copyright notice,
57 // definition, disclaimer, and this list of conditions.
58 //
59 // 2. Redistributions in binary form (compiled executables) must reproduce
60 // the above copyright notice, definition, disclaimer, and this list of
61 // conditions in documentation and/or other materials provided with the
62 // distribution. The sole exception to this condition is redistribution
63 // of a standard UnZipSFX binary as part of a self-extracting archive;
64 // that is permitted without inclusion of this license, as long as the
65 // normal UnZipSFX banner has not been removed from the binary or disabled.
66 //
67 // 3. Altered versions--including, but not limited to, ports to new
68 // operating systems, existing ports with new graphical interfaces, and
69 // dynamic, shared, or static library versions--must be plainly marked
70 // as such and must not be misrepresented as being the original source.
71 // Such altered versions also must not be misrepresented as being
72 // Info-ZIP releases--including, but not limited to, labeling of the
73 // altered versions with the names "Info-ZIP" (or any variation thereof,
74 // including, but not limited to, different capitalizations),
75 // "Pocket UnZip", "WiZ" or "MacZip" without the explicit permission of
76 // Info-ZIP. Such altered versions are further prohibited from
77 // misrepresentative use of the Zip-Bugs or Info-ZIP e-mail addresses or
78 // of the Info-ZIP URL(s).
79 //
80 // 4. Info-ZIP retains the right to use the names "Info-ZIP", "Zip", "UnZip",
81 // "UnZipSFX", "WiZ", "Pocket UnZip", "Pocket Zip", and "MacZip" for its
82 // own source and binary releases.
83 //
84 ///////////////////////////////////////////////////////////////////////////////
85
86 #ifndef XUNZIP_H
87 #define XUNZIP_H
88
89
90 #ifndef XZIP_H
91 DECLARE_HANDLE(HZIP); // An HZIP identifies a zip file that has been opened
92 #endif
93
94 typedef DWORD ZRESULT;
95 // return codes from any of the zip functions. Listed later.
96
97 #define ZIP_HANDLE 1
98 #define ZIP_FILENAME 2
99 #define ZIP_MEMORY 3
100
101 typedef struct {
102 int index; // index of this file within the zip
103 char name[MAX_PATH]; // filename within the zip
104 DWORD attr; // attributes, as in GetFileAttributes.
105 FILETIME atime, ctime, mtime; // access, create, modify filetimes
106 long comp_size; // sizes of item, compressed and uncompressed. These
107 long unc_size; // may be -1 if not yet known (e.g. being streamed in)
108 } ZIPENTRY;
109
110 typedef struct {
111 int index; // index of this file within the zip
112 TCHAR name[MAX_PATH]; // filename within the zip
113 DWORD attr; // attributes, as in GetFileAttributes.
114 FILETIME atime, ctime, mtime; // access, create, modify filetimes
115 long comp_size; // sizes of item, compressed and uncompressed. These
116 long unc_size; // may be -1 if not yet known (e.g. being streamed in)
117 } ZIPENTRYW;
118
119
120 ///////////////////////////////////////////////////////////////////////////////
121 //
122 // OpenZip()
123 //
124 // Purpose: Open an existing zip archive file
125 //
126 // Parameters: z - archive file name if flags is ZIP_FILENAME; for other
127 // uses see below
128 // len - for memory (ZIP_MEMORY) should be the buffer size;
129 // for other uses, should be 0
130 // flags - indicates usage, see below; for files, this will be
131 // ZIP_FILENAME
132 //
133 // Returns: HZIP - non-zero if zip archive opened ok, otherwise 0
134 //
135 HZIP OpenZip(void *z, unsigned int len, DWORD flags);
136 // OpenZip - opens a zip file and returns a handle with which you can
137 // subsequently examine its contents. You can open a zip file from:
138 // from a pipe: OpenZip(hpipe_read,0, ZIP_HANDLE);
139 // from a file (by handle): OpenZip(hfile,0, ZIP_HANDLE);
140 // from a file (by name): OpenZip("c:\\test.zip",0, ZIP_FILENAME);
141 // from a memory block: OpenZip(bufstart, buflen, ZIP_MEMORY);
142 // If the file is opened through a pipe, then items may only be
143 // accessed in increasing order, and an item may only be unzipped once,
144 // although GetZipItem can be called immediately before and after unzipping
145 // it. If it's opened i n any other way, then full random access is possible.
146 // Note: pipe input is not yet implemented.
147
148
149 ///////////////////////////////////////////////////////////////////////////////
150 //
151 // GetZipItem()
152 //
153 // Purpose: Get information about an item in an open zip archive
154 //
155 // Parameters: hz - handle of open zip archive
156 // index - index number (0 based) of item in zip
157 // ze - pointer to a ZIPENTRY (if ANSI) or ZIPENTRYW struct
158 // (if Unicode)
159 //
160 // Returns: ZRESULT - ZR_OK if success, otherwise some other value
161 //
162
163 #ifdef _UNICODE
164 #define GetZipItem GetZipItemW
165 #else
166 #define GetZipItem GetZipItemA
167 #endif
168
169 ZRESULT GetZipItemA(HZIP hz, int index, ZIPENTRY *ze);
170 ZRESULT GetZipItemW(HZIP hz, int index, ZIPENTRYW *ze);
171 // GetZipItem - call this to get information about an item in the zip.
172 // If index is -1 and the file wasn't opened through a pipe,
173 // then it returns information about the whole zipfile
174 // (and in particular ze.index returns the number of index items).
175 // Note: the item might be a directory (ze.attr & FILE_ATTRIBUTE_DIRECTORY)
176 // See below for notes on what happens when you unzip such an item.
177 // Note: if you are opening the zip through a pipe, then random access
178 // is not possible and GetZipItem(-1) fails and you can't discover the number
179 // of items except by calling GetZipItem on each one of them in turn,
180 // starting at 0, until eventually the call fails. Also, in the event that
181 // you are opening through a pipe and the zip was itself created into a pipe,
182 // then then comp_size and sometimes unc_size as well may not be known until
183 // after the item has been unzipped.
184
185
186 ///////////////////////////////////////////////////////////////////////////////
187 //
188 // FindZipItem()
189 //
190 // Purpose: Find item by name and return information about it
191 //
192 // Parameters: hz - handle of open zip archive
193 // name - name of file to look for inside zip archive
194 // ic - TRUE = case insensitive
195 // index - pointer to index number returned, or -1
196 // ze - pointer to a ZIPENTRY (if ANSI) or ZIPENTRYW struct
197 // (if Unicode)
198 //
199 // Returns: ZRESULT - ZR_OK if success, otherwise some other value
200 //
201
202 #ifdef _UNICODE
203 #define FindZipItem FindZipItemW
204 #else
205 #define FindZipItem FindZipItemA
206 #endif
207
208 ZRESULT FindZipItemA(HZIP hz, const TCHAR *name, bool ic, int *index, ZIPENTRY *ze);
209 ZRESULT FindZipItemW(HZIP hz, const TCHAR *name, bool ic, int *index, ZIPENTRYW *ze);
210 // FindZipItem - finds an item by name. ic means 'insensitive to case'.
211 // It returns the index of the item, and returns information about it.
212 // If nothing was found, then index is set to -1 and the function returns
213 // an error code.
214
215
216 ///////////////////////////////////////////////////////////////////////////////
217 //
218 // UnzipItem()
219 //
220 // Purpose: Find item by index and unzip it
221 //
222 // Parameters: hz - handle of open zip archive
223 // index - index number of file to unzip
224 // dst - target file name of unzipped file
225 // len - for memory (ZIP_MEMORY. length of buffer;
226 // otherwise 0
227 // flags - indicates usage, see below; for files, this will be
228 // ZIP_FILENAME
229 //
230 // Returns: ZRESULT - ZR_OK if success, otherwise some other value
231 //
232
233 ZRESULT UnzipItem(HZIP hz, int index, void *dst, unsigned int len, DWORD flags);
234 // UnzipItem - given an index to an item, unzips it. You can unzip to:
235 // to a pipe: UnzipItem(hz,i, hpipe_write,0,ZIP_HANDLE);
236 // to a file (by handle): UnzipItem(hz,i, hfile,0,ZIP_HANDLE);
237 // to a file (by name): UnzipItem(hz,i, ze.name,0,ZIP_FILENAME);
238 // to a memory block: UnzipItem(hz,i, buf,buflen,ZIP_MEMORY);
239 // In the final case, if the buffer isn't large enough to hold it all,
240 // then the return code indicates that more is yet to come. If it was
241 // large enough, and you want to know precisely how big, GetZipItem.
242 // Note: zip files are normally stored with relative pathnames. If you
243 // unzip with ZIP_FILENAME a relative pathname then the item gets created
244 // relative to the current directory - it first ensures that all necessary
245 // subdirectories have been created. Also, the item may itself be a directory.
246 // If you unzip a directory with ZIP_FILENAME, then the directory gets created.
247 // If you unzip it to a handle or a memory block, then nothing gets created
248 // and it emits 0 bytes.
249
250
251 ///////////////////////////////////////////////////////////////////////////////
252 //
253 // CloseZip()
254 //
255 // Purpose: Close an open zip archive
256 //
257 // Parameters: hz - handle to an open zip archive
258 //
259 // Returns: ZRESULT - ZR_OK if success, otherwise some other value
260 //
261 ZRESULT CloseZip(HZIP hz);
262 // CloseZip - the zip handle must be closed with this function.
263
264 unsigned int FormatZipMessage(ZRESULT code, char *buf, unsigned int len);
265 // FormatZipMessage - given an error code, formats it as a string.
266 // It returns the length of the error message. If buf/len points
267 // to a real buffer, then it also writes as much as possible into there.
268
269
270 // These are the result codes:
271 #define ZR_OK 0x00000000 // nb. the pseudo-code zr-recent is never returned,
272 #define ZR_RECENT 0x00000001 // but can be passed to FormatZipMessage.
273 // The following come from general system stuff (e.g. files not openable)
274 #define ZR_GENMASK 0x0000FF00
275 #define ZR_NODUPH 0x00000100 // couldn't duplicate the handle
276 #define ZR_NOFILE 0x00000200 // couldn't create/open the file
277 #define ZR_NOALLOC 0x00000300 // failed to allocate some resource
278 #define ZR_WRITE 0x00000400 // a general error writing to the file
279 #define ZR_NOTFOUND 0x00000500 // couldn't find that file in the zip
280 #define ZR_MORE 0x00000600 // there's still more data to be unzipped
281 #define ZR_CORRUPT 0x00000700 // the zipfile is corrupt or not a zipfile
282 #define ZR_READ 0x00000800 // a general error reading the file
283 // The following come from mistakes on the part of the caller
284 #define ZR_CALLERMASK 0x00FF0000
285 #define ZR_ARGS 0x00010000 // general mistake with the arguments
286 #define ZR_NOTMMAP 0x00020000 // tried to ZipGetMemory, but that only works on mmap zipfiles, which yours wasn't
287 #define ZR_MEMSIZE 0x00030000 // the memory size is too small
288 #define ZR_FAILED 0x00040000 // the thing was already failed when you called this function
289 #define ZR_ENDED 0x00050000 // the zip creation has already been closed
290 #define ZR_MISSIZE 0x00060000 // the indicated input file size turned out mistaken
291 #define ZR_PARTIALUNZ 0x00070000 // the file had already been partially unzipped
292 #define ZR_ZMODE 0x00080000 // tried to mix creating/opening a zip
293 // The following come from bugs within the zip library itself
294 #define ZR_BUGMASK 0xFF000000
295 #define ZR_NOTINITED 0x01000000 // initialisation didn't work
296 #define ZR_SEEK 0x02000000 // trying to seek in an unseekable file
297 #define ZR_NOCHANGE 0x04000000 // changed its mind on storage, but not allowed
298 #define ZR_FLATE 0x05000000 // an internal error in the de/inflation code
299
300
301 // e.g.
302 //
303 // SetCurrentDirectory("c:\\docs\\stuff");
304 // HZIP hz = OpenZip("c:\\stuff.zip",0,ZIP_FILENAME);
305 // ZIPENTRY ze; GetZipItem(hz,-1,&ze); int numitems=ze.index;
306 // for (int i=0; i<numitems; i++)
307 // { GetZipItem(hz,i,&ze);
308 // UnzipItem(hz,i,ze.name,0,ZIP_FILENAME);
309 // }
310 // CloseZip(hz);
311 //
312 //
313 // HRSRC hrsrc = FindResource(hInstance,MAKEINTRESOURCE(1),RT_RCDATA);
314 // HANDLE hglob = LoadResource(hInstance,hrsrc);
315 // void *zipbuf=LockResource(hglob);
316 // unsigned int ziplen=SizeofResource(hInstance,hrsrc);
317 // HZIP hz = OpenZip(zipbuf, ziplen, ZIP_MEMORY);
318 // - unzip to a membuffer -
319 // ZIPENTRY ze; int i; FindZipItem(hz,"file.dat",&i,&ze);
320 // char *ibuf = new char[ze.unc_size];
321 // UnzipItem(hz,i, ibuf, ze.unc_size,ZIP_MEMORY);
322 // delete[] buf;
323 // - unzip to a fixed membuff -
324 // ZIPENTRY ze; int i; FindZipItem(hz,"file.dat",&i,&ze);
325 // char ibuf[1024]; ZIPRESULT zr=ZR_MORE; unsigned long totsize=0;
326 // while (zr==ZR_MORE)
327 // { zr = UnzipItem(hz,i, ibuf,1024,ZIP_MEMORY);
328 // unsigned long bufsize=1024; if (zr==ZR_OK) bufsize=ze.unc_size-totsize;
329 // totsize+=bufsize;
330 // }
331 // - unzip to a pipe -
332 // HANDLE hthread=CreateWavReaderThread(&hread,&hwrite);
333 // FindZipItem(hz,"sound.wav",&i,&ze);
334 // UnzipItem(hz,i, hwrite,0,ZIP_HANDLE);
335 // CloseHandle(hwrite);
336 // WaitForSingleObject(hthread,INFINITE);
337 // CloseHandle(hread); CloseHandle(hthread);
338 // - finished -
339 // CloseZip(hz);
340 // // note: no need to free resources obtained through Find/Load/LockResource
341 //
342 //
343 // SetCurrentDirectory("c:\\docs\\pipedzipstuff");
344 // HANDLE hread,hwrite; CreatePipe(&hread,&hwrite);
345 // CreateZipWriterThread(hwrite);
346 // HZIP hz = OpenZip(hread,0,ZIP_HANDLE);
347 // for (int i=0; ; i++)
348 // { ZIPENTRY ze; ZRESULT res = GetZipItem(hz,i,&ze);
349 // if (res!=ZE_OK) break; // no more
350 // UnzipItem(hz,i, ze.name,0,ZIP_FILENAME);
351 // }
352 // CloseZip(hz);
353 //
354
355
356 // Now we indulge in a little skullduggery so that the code works whether
357 // the user has included just zip or both zip and unzip.
358 // Idea: if header files for both zip and unzip are present, then presumably
359 // the cpp files for zip and unzip are both present, so we will call
360 // one or the other of them based on a dynamic choice. If the header file
361 // for only one is present, then we will bind to that particular one.
362 HZIP OpenZipU(void *z, unsigned int len, DWORD flags);
363 ZRESULT CloseZipU(HZIP hz);
364 unsigned int FormatZipMessageU(ZRESULT code, char *buf, unsigned int len);
365 bool IsZipHandleU(HZIP hz);
366 #define OpenZip OpenZipU
367
368 #ifdef XZIP_H
369 #undef CloseZip
370 #define CloseZip(hz) (IsZipHandleU(hz) ? CloseZipU(hz) : CloseZipZ(hz))
371 #else
372 #define CloseZip CloseZipU
373 #define FormatZipMessage FormatZipMessageU
374 #endif
375
376
377 #endif // XUNZIP_H