Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

342 lines
15 KiB

  1. // XZip.h Version 1.1
  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. // Version 1.1: - Added Unicode support to CreateZip() and ZipAdd()
  18. // - Changed file names to avoid conflicts with Lucian's files
  19. //
  20. ///////////////////////////////////////////////////////////////////////////////
  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. //
  29. ///////////////////////////////////////////////////////////////////////////////
  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. //
  87. ///////////////////////////////////////////////////////////////////////////////
  88. #ifndef XZIP_H
  89. #define XZIP_H
  90. // ZIP functions -- for creating zip files
  91. // This file is a repackaged form of the Info-Zip source code available
  92. // at www.info-zip.org. The original copyright notice may be found in
  93. // zip.cpp. The repackaging was done by Lucian Wischik to simplify its
  94. // use in Windows/C++.
  95. #if !defined( DWORD )
  96. #ifdef _WIN32
  97. typedef unsigned long DWORD;
  98. #else
  99. typedef unsigned int DWORD;
  100. #endif
  101. #endif
  102. #if !defined( TCHAR )
  103. typedef char TCHAR;
  104. #endif
  105. #ifndef XUNZIP_H
  106. #if !defined(DECLARE_HANDLE)
  107. #if !defined(HANDLE)
  108. typedef void * HANDLE;
  109. #endif
  110. #define DECLARE_HANDLE(name) typedef struct name##__ { int unused; } *name
  111. #endif
  112. DECLARE_HANDLE(HZIP); // An HZIP identifies a zip file that is being created
  113. #endif
  114. typedef DWORD ZRESULT; // result codes from any of the zip functions. Listed later.
  115. // flag values passed to some functions
  116. #define ZIP_HANDLE 1
  117. #define ZIP_FILENAME 2
  118. #define ZIP_MEMORY 3
  119. #define ZIP_FOLDER 4
  120. ///////////////////////////////////////////////////////////////////////////////
  121. //
  122. // CreateZip()
  123. //
  124. // Purpose: Create a 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 created ok, otherwise 0
  134. //
  135. HZIP CreateZip(void *z, unsigned int len, DWORD flags);
  136. // CreateZip - call this to start the creation of a zip file.
  137. // As the zip is being created, it will be stored somewhere:
  138. // to a pipe: CreateZip(hpipe_write, 0,ZIP_HANDLE);
  139. // in a file (by handle): CreateZip(hfile, 0,ZIP_HANDLE);
  140. // in a file (by name): CreateZip("c:\\test.zip", 0,ZIP_FILENAME);
  141. // in memory: CreateZip(buf, len,ZIP_MEMORY);
  142. // or in pagefile memory: CreateZip(0, len,ZIP_MEMORY);
  143. // The final case stores it in memory backed by the system paging file,
  144. // where the zip may not exceed len bytes. This is a bit friendlier than
  145. // allocating memory with new[]: it won't lead to fragmentation, and the
  146. // memory won't be touched unless needed.
  147. // Note: because pipes don't allow random access, the structure of a zipfile
  148. // created into a pipe is slightly different from that created into a file
  149. // or memory. In particular, the compressed-size of the item cannot be
  150. // stored in the zipfile until after the item itself. (Also, for an item added
  151. // itself via a pipe, the uncompressed-size might not either be known until
  152. // after.) This is not normally a problem. But if you try to unzip via a pipe
  153. // as well, then the unzipper will not know these things about the item until
  154. // after it has been unzipped. Therefore: for unzippers which don't just write
  155. // each item to disk or to a pipe, but instead pre-allocate memory space into
  156. // which to unzip them, then either you have to create the zip not to a pipe,
  157. // or you have to add items not from a pipe, or at least when adding items
  158. // from a pipe you have to specify the length.
  159. ///////////////////////////////////////////////////////////////////////////////
  160. //
  161. // ZipAdd()
  162. //
  163. // Purpose: Add a file to a zip archive
  164. //
  165. // Parameters: hz - handle to an open zip archive
  166. // dstzn - name used inside the zip archive to identify the file
  167. // src - for a file (ZIP_FILENAME) this specifies the filename
  168. // to be added to the archive; for other uses, see below
  169. // len - for memory (ZIP_MEMORY) this specifies the buffer
  170. // length; for other uses, this should be 0
  171. // flags - indicates usage, see below; for files, this will be
  172. // ZIP_FILENAME
  173. //
  174. // Returns: ZRESULT - ZR_OK if success, otherwise some other value
  175. //
  176. ZRESULT ZipAdd(HZIP hz, const TCHAR *dstzn, void *src, unsigned int len, DWORD flags);
  177. // ZipAdd - call this for each file to be added to the zip.
  178. // dstzn is the name that the file will be stored as in the zip file.
  179. // The file to be added to the zip can come
  180. // from a pipe: ZipAdd(hz,"file.dat", hpipe_read,0,ZIP_HANDLE);
  181. // from a file: ZipAdd(hz,"file.dat", hfile,0,ZIP_HANDLE);
  182. // from a fname: ZipAdd(hz,"file.dat", "c:\\docs\\origfile.dat",0,ZIP_FILENAME);
  183. // from memory: ZipAdd(hz,"subdir\\file.dat", buf,len,ZIP_MEMORY);
  184. // (folder): ZipAdd(hz,"subdir", 0,0,ZIP_FOLDER);
  185. // Note: if adding an item from a pipe, and if also creating the zip file itself
  186. // to a pipe, then you might wish to pass a non-zero length to the ZipAdd
  187. // function. This will let the zipfile store the items size ahead of the
  188. // compressed item itself, which in turn makes it easier when unzipping the
  189. // zipfile into a pipe.
  190. ///////////////////////////////////////////////////////////////////////////////
  191. //
  192. // CloseZip()
  193. //
  194. // Purpose: Close an open zip archive
  195. //
  196. // Parameters: hz - handle to an open zip archive
  197. //
  198. // Returns: ZRESULT - ZR_OK if success, otherwise some other value
  199. //
  200. ZRESULT CloseZip(HZIP hz);
  201. // CloseZip - the zip handle must be closed with this function.
  202. ZRESULT ZipGetMemory(HZIP hz, void **buf, unsigned long *len);
  203. // ZipGetMemory - If the zip was created in memory, via ZipCreate(0,ZIP_MEMORY),
  204. // then this function will return information about that memory block.
  205. // buf will receive a pointer to its start, and len its length.
  206. // Note: you can't add any more after calling this.
  207. unsigned int FormatZipMessage(ZRESULT code, char *buf,unsigned int len);
  208. // FormatZipMessage - given an error code, formats it as a string.
  209. // It returns the length of the error message. If buf/len points
  210. // to a real buffer, then it also writes as much as possible into there.
  211. // These are the result codes:
  212. #define ZR_OK 0x00000000 // nb. the pseudo-code zr-recent is never returned,
  213. #define ZR_RECENT 0x00000001 // but can be passed to FormatZipMessage.
  214. // The following come from general system stuff (e.g. files not openable)
  215. #define ZR_GENMASK 0x0000FF00
  216. #define ZR_NODUPH 0x00000100 // couldn't duplicate the handle
  217. #define ZR_NOFILE 0x00000200 // couldn't create/open the file
  218. #define ZR_NOALLOC 0x00000300 // failed to allocate some resource
  219. #define ZR_WRITE 0x00000400 // a general error writing to the file
  220. #define ZR_NOTFOUND 0x00000500 // couldn't find that file in the zip
  221. #define ZR_MORE 0x00000600 // there's still more data to be unzipped
  222. #define ZR_CORRUPT 0x00000700 // the zipfile is corrupt or not a zipfile
  223. #define ZR_READ 0x00000800 // a general error reading the file
  224. // The following come from mistakes on the part of the caller
  225. #define ZR_CALLERMASK 0x00FF0000
  226. #define ZR_ARGS 0x00010000 // general mistake with the arguments
  227. #define ZR_NOTMMAP 0x00020000 // tried to ZipGetMemory, but that only works on mmap zipfiles, which yours wasn't
  228. #define ZR_MEMSIZE 0x00030000 // the memory size is too small
  229. #define ZR_FAILED 0x00040000 // the thing was already failed when you called this function
  230. #define ZR_ENDED 0x00050000 // the zip creation has already been closed
  231. #define ZR_MISSIZE 0x00060000 // the indicated input file size turned out mistaken
  232. #define ZR_PARTIALUNZ 0x00070000 // the file had already been partially unzipped
  233. #define ZR_ZMODE 0x00080000 // tried to mix creating/opening a zip
  234. // The following come from bugs within the zip library itself
  235. #define ZR_BUGMASK 0xFF000000
  236. #define ZR_NOTINITED 0x01000000 // initialisation didn't work
  237. #define ZR_SEEK 0x02000000 // trying to seek in an unseekable file
  238. #define ZR_NOCHANGE 0x04000000 // changed its mind on storage, but not allowed
  239. #define ZR_FLATE 0x05000000 // an internal error in the de/inflation code
  240. // e.g.
  241. //
  242. // (1) Traditional use, creating a zipfile from existing files
  243. // HZIP hz = CreateZip("c:\\temp.zip",0,ZIP_FILENAME);
  244. // ZipAdd(hz,"src1.txt", "c:\\src1.txt",0,ZIP_FILENAME);
  245. // ZipAdd(hz,"src2.bmp", "c:\\src2_origfn.bmp",0,ZIP_FILENAME);
  246. // CloseZip(hz);
  247. //
  248. // (2) Memory use, creating an auto-allocated mem-based zip file from various sources
  249. // HZIP hz = CreateZip(0,100000,ZIP_MEMORY);
  250. // // adding a conventional file...
  251. // ZipAdd(hz,"src1.txt", "c:\\src1.txt",0,ZIP_FILENAME);
  252. // // adding something from memory...
  253. // char buf[1000]; for (int i=0; i<1000; i++) buf[i]=(char)(i&0x7F);
  254. // ZipAdd(hz,"file.dat", buf,1000,ZIP_MEMORY);
  255. // // adding something from a pipe...
  256. // HANDLE hread,hwrite; CreatePipe(&hread,&write,NULL,0);
  257. // HANDLE hthread = CreateThread(ThreadFunc,(void*)hwrite);
  258. // ZipAdd(hz,"unz3.dat", hread,0,ZIP_HANDLE);
  259. // WaitForSingleObject(hthread,INFINITE);
  260. // CloseHandle(hthread); CloseHandle(hread);
  261. // ... meanwhile DWORD CALLBACK ThreadFunc(void *dat)
  262. // { HANDLE hwrite = (HANDLE)dat;
  263. // char buf[1000]={17};
  264. // DWORD writ; WriteFile(hwrite,buf,1000,&writ,NULL);
  265. // CloseHandle(hwrite);
  266. // return 0;
  267. // }
  268. // // and now that the zip is created, let's do something with it:
  269. // void *zbuf; unsigned long zlen; ZipGetMemory(hz,&zbuf,&zlen);
  270. // HANDLE hfz = CreateFile("test2.zip",GENERIC_WRITE,CREATE_ALWAYS);
  271. // DWORD writ; WriteFile(hfz,zbuf,zlen,&writ,NULL);
  272. // CloseHandle(hfz);
  273. // CloseZip(hz);
  274. //
  275. // (3) Handle use, for file handles and pipes
  276. // HANDLE hzread,hzwrite; CreatePipe(&hzread,&hzwrite);
  277. // HANDLE hthread = CreateThread(ZipReceiverThread,(void*)hread);
  278. // HZIP hz = ZipCreate(hzwrite,ZIP_HANDLE);
  279. // // ... add to it
  280. // CloseZip(hz);
  281. // CloseHandle(hzwrite);
  282. // WaitForSingleObject(hthread,INFINITE);
  283. // CloseHandle(hthread);
  284. // ... meanwhile DWORD CALLBACK ThreadFunc(void *dat)
  285. // { HANDLE hread = (HANDLE)dat;
  286. // char buf[1000];
  287. // while (true)
  288. // { DWORD red; ReadFile(hread,buf,1000,&red,NULL);
  289. // // ... and do something with this zip data we're receiving
  290. // if (red==0) break;
  291. // }
  292. // CloseHandle(hread);
  293. // return 0;
  294. // }
  295. //
  296. // Now we indulge in a little skullduggery so that the code works whether
  297. // the user has included just zip or both zip and unzip.
  298. // Idea: if header files for both zip and unzip are present, then presumably
  299. // the cpp files for zip and unzip are both present, so we will call
  300. // one or the other of them based on a dynamic choice. If the header file
  301. // for only one is present, then we will bind to that particular one.
  302. HZIP CreateZipZ(void *z,unsigned int len,DWORD flags);
  303. ZRESULT CloseZipZ(HZIP hz);
  304. unsigned int FormatZipMessageZ(ZRESULT code, char *buf,unsigned int len);
  305. bool IsZipHandleZ(HZIP hz);
  306. #define CreateZip CreateZipZ
  307. #ifdef XUNZIP_H
  308. #undef CloseZip
  309. #define CloseZip(hz) (IsZipHandleZ(hz)?CloseZipZ(hz):CloseZipU(hz))
  310. #else
  311. #define CloseZip CloseZipZ
  312. #define FormatZipMessage FormatZipMessageZ
  313. #endif
  314. #endif //XZIP_H