BugSplat Native Windows DLL  3.6.0.0
BugSplat API reference for native Windows applications.
XZip.h
1 // XZip.h Version 1.1
2 //
3 // Authors: Mark Adler et al. (see below)
4 //
5 // Modified by: Lucian Wischik
6 // lu@wischik.com
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 // hdietrich2@hotmail.com
16 //
17 // Version 1.1: - Added Unicode support to CreateZip() and ZipAdd()
18 // - Changed file names to avoid conflicts with Lucian's files
19 //
21 //
22 // Lucian Wischik's comments:
23 // --------------------------
24 // THIS FILE is almost entirely based upon code by info-zip.
25 // It has been modified by Lucian Wischik.
26 // The original code may be found at http://www.info-zip.org
27 // The original copyright text follows.
28 //
30 //
31 // Original authors' comments:
32 // ---------------------------
33 // This is version 2002-Feb-16 of the Info-ZIP copyright and license. The
34 // definitive version of this document should be available at
35 // ftp://ftp.info-zip.org/pub/infozip/license.html indefinitely.
36 //
37 // Copyright (c) 1990-2002 Info-ZIP. All rights reserved.
38 //
39 // For the purposes of this copyright and license, "Info-ZIP" is defined as
40 // the following set of individuals:
41 //
42 // Mark Adler, John Bush, Karl Davis, Harald Denker, Jean-Michel Dubois,
43 // Jean-loup Gailly, Hunter Goatley, Ian Gorman, Chris Herborth, Dirk Haase,
44 // Greg Hartwig, Robert Heath, Jonathan Hudson, Paul Kienitz,
45 // David Kirschbaum, Johnny Lee, Onno van der Linden, Igor Mandrichenko,
46 // Steve P. Miller, Sergio Monesi, Keith Owens, George Petrov, Greg Roelofs,
47 // Kai Uwe Rommel, Steve Salisbury, Dave Smith, Christian Spieler,
48 // Antoine Verheijen, Paul von Behren, Rich Wales, Mike White
49 //
50 // This software is provided "as is", without warranty of any kind, express
51 // or implied. In no event shall Info-ZIP or its contributors be held liable
52 // for any direct, indirect, incidental, special or consequential damages
53 // arising out of the use of or inability to use this software.
54 //
55 // Permission is granted to anyone to use this software for any purpose,
56 // including commercial applications, and to alter it and redistribute it
57 // freely, subject to the following restrictions:
58 //
59 // 1. Redistributions of source code must retain the above copyright notice,
60 // definition, disclaimer, and this list of conditions.
61 //
62 // 2. Redistributions in binary form (compiled executables) must reproduce
63 // the above copyright notice, definition, disclaimer, and this list of
64 // conditions in documentation and/or other materials provided with the
65 // distribution. The sole exception to this condition is redistribution
66 // of a standard UnZipSFX binary as part of a self-extracting archive;
67 // that is permitted without inclusion of this license, as long as the
68 // normal UnZipSFX banner has not been removed from the binary or disabled.
69 //
70 // 3. Altered versions--including, but not limited to, ports to new
71 // operating systems, existing ports with new graphical interfaces, and
72 // dynamic, shared, or static library versions--must be plainly marked
73 // as such and must not be misrepresented as being the original source.
74 // Such altered versions also must not be misrepresented as being
75 // Info-ZIP releases--including, but not limited to, labeling of the
76 // altered versions with the names "Info-ZIP" (or any variation thereof,
77 // including, but not limited to, different capitalizations),
78 // "Pocket UnZip", "WiZ" or "MacZip" without the explicit permission of
79 // Info-ZIP. Such altered versions are further prohibited from
80 // misrepresentative use of the Zip-Bugs or Info-ZIP e-mail addresses or
81 // of the Info-ZIP URL(s).
82 //
83 // 4. Info-ZIP retains the right to use the names "Info-ZIP", "Zip", "UnZip",
84 // "UnZipSFX", "WiZ", "Pocket UnZip", "Pocket Zip", and "MacZip" for its
85 // own source and binary releases.
86 //
88 
89 #ifndef XZIP_H
90 #define XZIP_H
91 
92 // ZIP functions -- for creating zip files
93 // This file is a repackaged form of the Info-Zip source code available
94 // at www.info-zip.org. The original copyright notice may be found in
95 // zip.cpp. The repackaging was done by Lucian Wischik to simplify its
96 // use in Windows/C++.
97 
98 #ifndef XUNZIP_H
99 DECLARE_HANDLE(HZIP); // An HZIP identifies a zip file that is being created
100 #endif
101 
102 typedef DWORD ZRESULT; // result codes from any of the zip functions. Listed later.
103 
104 // flag values passed to some functions
105 #define ZIP_HANDLE 1
106 #define ZIP_FILENAME 2
107 #define ZIP_MEMORY 3
108 #define ZIP_FOLDER 4
109 
110 
112 //
113 // CreateZip()
114 //
115 // Purpose: Create a zip archive file
116 //
117 // Parameters: z - archive file name if flags is ZIP_FILENAME; for other
118 // uses see below
119 // len - for memory (ZIP_MEMORY) should be the buffer size;
120 // for other uses, should be 0
121 // flags - indicates usage, see below; for files, this will be
122 // ZIP_FILENAME
123 //
124 // Returns: HZIP - non-zero if zip archive created ok, otherwise 0
125 //
126 HZIP CreateZip(void *z, unsigned int len, DWORD flags);
127 // CreateZip - call this to start the creation of a zip file.
128 // As the zip is being created, it will be stored somewhere:
129 // to a pipe: CreateZip(hpipe_write, 0,ZIP_HANDLE);
130 // in a file (by handle): CreateZip(hfile, 0,ZIP_HANDLE);
131 // in a file (by name): CreateZip("c:\\test.zip", 0,ZIP_FILENAME);
132 // in memory: CreateZip(buf, len,ZIP_MEMORY);
133 // or in pagefile memory: CreateZip(0, len,ZIP_MEMORY);
134 // The final case stores it in memory backed by the system paging file,
135 // where the zip may not exceed len bytes. This is a bit friendlier than
136 // allocating memory with new[]: it won't lead to fragmentation, and the
137 // memory won't be touched unless needed.
138 // Note: because pipes don't allow random access, the structure of a zipfile
139 // created into a pipe is slightly different from that created into a file
140 // or memory. In particular, the compressed-size of the item cannot be
141 // stored in the zipfile until after the item itself. (Also, for an item added
142 // itself via a pipe, the uncompressed-size might not either be known until
143 // after.) This is not normally a problem. But if you try to unzip via a pipe
144 // as well, then the unzipper will not know these things about the item until
145 // after it has been unzipped. Therefore: for unzippers which don't just write
146 // each item to disk or to a pipe, but instead pre-allocate memory space into
147 // which to unzip them, then either you have to create the zip not to a pipe,
148 // or you have to add items not from a pipe, or at least when adding items
149 // from a pipe you have to specify the length.
150 
151 
153 //
154 // ZipAdd()
155 //
156 // Purpose: Add a file to a zip archive
157 //
158 // Parameters: hz - handle to an open zip archive
159 // dstzn - name used inside the zip archive to identify the file
160 // src - for a file (ZIP_FILENAME) this specifies the filename
161 // to be added to the archive; for other uses, see below
162 // len - for memory (ZIP_MEMORY) this specifies the buffer
163 // length; for other uses, this should be 0
164 // flags - indicates usage, see below; for files, this will be
165 // ZIP_FILENAME
166 //
167 // Returns: ZRESULT - ZR_OK if success, otherwise some other value
168 //
169 ZRESULT ZipAdd(HZIP hz, const TCHAR *dstzn, void *src, unsigned int len, DWORD flags);
170 // ZipAdd - call this for each file to be added to the zip.
171 // dstzn is the name that the file will be stored as in the zip file.
172 // The file to be added to the zip can come
173 // from a pipe: ZipAdd(hz,"file.dat", hpipe_read,0,ZIP_HANDLE);
174 // from a file: ZipAdd(hz,"file.dat", hfile,0,ZIP_HANDLE);
175 // from a fname: ZipAdd(hz,"file.dat", "c:\\docs\\origfile.dat",0,ZIP_FILENAME);
176 // from memory: ZipAdd(hz,"subdir\\file.dat", buf,len,ZIP_MEMORY);
177 // (folder): ZipAdd(hz,"subdir", 0,0,ZIP_FOLDER);
178 // Note: if adding an item from a pipe, and if also creating the zip file itself
179 // to a pipe, then you might wish to pass a non-zero length to the ZipAdd
180 // function. This will let the zipfile store the items size ahead of the
181 // compressed item itself, which in turn makes it easier when unzipping the
182 // zipfile into a pipe.
183 
184 
186 //
187 // GetZipSize()
188 //
189 // Purpose: Return the file size of the zip archive
190 //
191 // Parameters: hz - handle to an open zip archive
192 //
193 // Returns: Size of file (0L if an error)
194 //
195 DWORD GetZipSize(HZIP hz);
196 
197 
199 //
200 // CloseZip()
201 //
202 // Purpose: Close an open zip archive
203 //
204 // Parameters: hz - handle to an open zip archive
205 //
206 // Returns: ZRESULT - ZR_OK if success, otherwise some other value
207 //
208 ZRESULT CloseZip(HZIP hz);
209 // CloseZip - the zip handle must be closed with this function.
210 
211 
212 ZRESULT ZipGetMemory(HZIP hz, void **buf, unsigned long *len);
213 // ZipGetMemory - If the zip was created in memory, via ZipCreate(0,ZIP_MEMORY),
214 // then this function will return information about that memory block.
215 // buf will receive a pointer to its start, and len its length.
216 // Note: you can't add any more after calling this.
217 
218 
219 unsigned int FormatZipMessage(ZRESULT code, char *buf,unsigned int len);
220 // FormatZipMessage - given an error code, formats it as a string.
221 // It returns the length of the error message. If buf/len points
222 // to a real buffer, then it also writes as much as possible into there.
223 
224 
225 
226 // These are the result codes:
227 #define ZR_OK 0x00000000 // nb. the pseudo-code zr-recent is never returned,
228 #define ZR_RECENT 0x00000001 // but can be passed to FormatZipMessage.
229 // The following come from general system stuff (e.g. files not openable)
230 #define ZR_GENMASK 0x0000FF00
231 #define ZR_NODUPH 0x00000100 // couldn't duplicate the handle
232 #define ZR_NOFILE 0x00000200 // couldn't create/open the file
233 #define ZR_NOALLOC 0x00000300 // failed to allocate some resource
234 #define ZR_WRITE 0x00000400 // a general error writing to the file
235 #define ZR_NOTFOUND 0x00000500 // couldn't find that file in the zip
236 #define ZR_MORE 0x00000600 // there's still more data to be unzipped
237 #define ZR_CORRUPT 0x00000700 // the zipfile is corrupt or not a zipfile
238 #define ZR_READ 0x00000800 // a general error reading the file
239 // The following come from mistakes on the part of the caller
240 #define ZR_CALLERMASK 0x00FF0000
241 #define ZR_ARGS 0x00010000 // general mistake with the arguments
242 #define ZR_NOTMMAP 0x00020000 // tried to ZipGetMemory, but that only works on mmap zipfiles, which yours wasn't
243 #define ZR_MEMSIZE 0x00030000 // the memory size is too small
244 #define ZR_FAILED 0x00040000 // the thing was already failed when you called this function
245 #define ZR_ENDED 0x00050000 // the zip creation has already been closed
246 #define ZR_MISSIZE 0x00060000 // the indicated input file size turned out mistaken
247 #define ZR_PARTIALUNZ 0x00070000 // the file had already been partially unzipped
248 #define ZR_ZMODE 0x00080000 // tried to mix creating/opening a zip
249 // The following come from bugs within the zip library itself
250 #define ZR_BUGMASK 0xFF000000
251 #define ZR_NOTINITED 0x01000000 // initialisation didn't work
252 #define ZR_SEEK 0x02000000 // trying to seek in an unseekable file
253 #define ZR_NOCHANGE 0x04000000 // changed its mind on storage, but not allowed
254 #define ZR_FLATE 0x05000000 // an internal error in the de/inflation code
255 
256 
257 
258 // e.g.
259 //
260 // (1) Traditional use, creating a zipfile from existing files
261 // HZIP hz = CreateZip("c:\\temp.zip",0,ZIP_FILENAME);
262 // ZipAdd(hz,"src1.txt", "c:\\src1.txt",0,ZIP_FILENAME);
263 // ZipAdd(hz,"src2.bmp", "c:\\src2_origfn.bmp",0,ZIP_FILENAME);
264 // CloseZip(hz);
265 //
266 // (2) Memory use, creating an auto-allocated mem-based zip file from various sources
267 // HZIP hz = CreateZip(0,100000,ZIP_MEMORY);
268 // // adding a conventional file...
269 // ZipAdd(hz,"src1.txt", "c:\\src1.txt",0,ZIP_FILENAME);
270 // // adding something from memory...
271 // char buf[1000]; for (int i=0; i<1000; i++) buf[i]=(char)(i&0x7F);
272 // ZipAdd(hz,"file.dat", buf,1000,ZIP_MEMORY);
273 // // adding something from a pipe...
274 // HANDLE hread,hwrite; CreatePipe(&hread,&write,NULL,0);
275 // HANDLE hthread = CreateThread(ThreadFunc,(void*)hwrite);
276 // ZipAdd(hz,"unz3.dat", hread,0,ZIP_HANDLE);
277 // WaitForSingleObject(hthread,INFINITE);
278 // CloseHandle(hthread); CloseHandle(hread);
279 // ... meanwhile DWORD CALLBACK ThreadFunc(void *dat)
280 // { HANDLE hwrite = (HANDLE)dat;
281 // char buf[1000]={17};
282 // DWORD writ; WriteFile(hwrite,buf,1000,&writ,NULL);
283 // CloseHandle(hwrite);
284 // return 0;
285 // }
286 // // and now that the zip is created, let's do something with it:
287 // void *zbuf; unsigned long zlen; ZipGetMemory(hz,&zbuf,&zlen);
288 // HANDLE hfz = CreateFile("test2.zip",GENERIC_WRITE,CREATE_ALWAYS);
289 // DWORD writ; WriteFile(hfz,zbuf,zlen,&writ,NULL);
290 // CloseHandle(hfz);
291 // CloseZip(hz);
292 //
293 // (3) Handle use, for file handles and pipes
294 // HANDLE hzread,hzwrite; CreatePipe(&hzread,&hzwrite);
295 // HANDLE hthread = CreateThread(ZipReceiverThread,(void*)hread);
296 // HZIP hz = ZipCreate(hzwrite,ZIP_HANDLE);
297 // // ... add to it
298 // CloseZip(hz);
299 // CloseHandle(hzwrite);
300 // WaitForSingleObject(hthread,INFINITE);
301 // CloseHandle(hthread);
302 // ... meanwhile DWORD CALLBACK ThreadFunc(void *dat)
303 // { HANDLE hread = (HANDLE)dat;
304 // char buf[1000];
305 // while (true)
306 // { DWORD red; ReadFile(hread,buf,1000,&red,NULL);
307 // // ... and do something with this zip data we're receiving
308 // if (red==0) break;
309 // }
310 // CloseHandle(hread);
311 // return 0;
312 // }
313 //
314 
315 
316 // Now we indulge in a little skullduggery so that the code works whether
317 // the user has included just zip or both zip and unzip.
318 // Idea: if header files for both zip and unzip are present, then presumably
319 // the cpp files for zip and unzip are both present, so we will call
320 // one or the other of them based on a dynamic choice. If the header file
321 // for only one is present, then we will bind to that particular one.
322 HZIP CreateZipZ(void *z,unsigned int len,DWORD flags);
323 ZRESULT CloseZipZ(HZIP hz);
324 unsigned int FormatZipMessageZ(ZRESULT code, char *buf,unsigned int len);
325 bool IsZipHandleZ(HZIP hz);
326 #define CreateZip CreateZipZ
327 
328 #ifdef XUNZIP_H
329 #undef CloseZip
330 #define CloseZip(hz) (IsZipHandleZ(hz)?CloseZipZ(hz):CloseZipU(hz))
331 #else
332 #define CloseZip CloseZipZ
333 #define FormatZipMessage FormatZipMessageZ
334 #endif
335 
336 
337 #endif //XZIP_H