Counter Strike : Global Offensive Source Code
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.

424 lines
17 KiB

  1. // XUnzip.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 XUNZIP_H
  89. #define XUNZIP_H
  90. #if !defined( DWORD )
  91. #if defined( _WIN32 ) || defined( _PS3 )
  92. typedef unsigned long DWORD;
  93. #else
  94. typedef unsigned int DWORD;
  95. #endif
  96. #endif
  97. #if !defined( TCHAR )
  98. typedef char TCHAR;
  99. #endif
  100. #if defined(POSIX) && !defined(MAX_PATH)
  101. #include <limits.h>
  102. #define MAX_PATH PATH_MAX
  103. typedef bool BOOL;
  104. #endif
  105. #ifndef XZIP_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. #if defined(_WIN32) && !defined(_WINBASE_) && !defined(_FILETIME_)
  115. #define _FILETIME_
  116. typedef struct _FILETIME
  117. {
  118. DWORD dwLowDateTime;
  119. DWORD dwHighDateTime;
  120. } FILETIME, * LPFILETIME, *PFILETIME;
  121. #endif
  122. #if defined(POSIX) && !defined( _PS3 )
  123. typedef time_t FILETIME;
  124. #endif
  125. typedef DWORD ZRESULT;
  126. // return codes from any of the zip functions. Listed later.
  127. #define ZIP_HANDLE 1
  128. #define ZIP_FILENAME 2
  129. #define ZIP_MEMORY 3
  130. typedef struct
  131. { int index; // index of this file within the zip
  132. char name[MAX_PATH]; // filename within the zip
  133. DWORD attr; // attributes, as in GetFileAttributes.
  134. FILETIME atime,ctime,mtime;// access, create, modify filetimes
  135. long comp_size; // sizes of item, compressed and uncompressed. These
  136. long unc_size; // may be -1 if not yet known (e.g. being streamed in)
  137. } ZIPENTRY;
  138. typedef struct
  139. { int index; // index of this file within the zip
  140. TCHAR name[MAX_PATH]; // filename within the zip
  141. DWORD attr; // attributes, as in GetFileAttributes.
  142. FILETIME atime,ctime,mtime;// access, create, modify filetimes
  143. long comp_size; // sizes of item, compressed and uncompressed. These
  144. long unc_size; // may be -1 if not yet known (e.g. being streamed in)
  145. } ZIPENTRYW;
  146. ///////////////////////////////////////////////////////////////////////////////
  147. //
  148. // OpenZip()
  149. //
  150. // Purpose: Open an existing zip archive file
  151. //
  152. // Parameters: z - archive file name if flags is ZIP_FILENAME; for other
  153. // uses see below
  154. // len - for memory (ZIP_MEMORY) should be the buffer size;
  155. // for other uses, should be 0
  156. // flags - indicates usage, see below; for files, this will be
  157. // ZIP_FILENAME
  158. //
  159. // Returns: HZIP - non-zero if zip archive opened ok, otherwise 0
  160. //
  161. HZIP OpenZip(void *z, unsigned int len, DWORD flags);
  162. // OpenZip - opens a zip file and returns a handle with which you can
  163. // subsequently examine its contents. You can open a zip file from:
  164. // from a pipe: OpenZip(hpipe_read,0, ZIP_HANDLE);
  165. // from a file (by handle): OpenZip(hfile,0, ZIP_HANDLE);
  166. // from a file (by name): OpenZip("c:\\test.zip",0, ZIP_FILENAME);
  167. // from a memory block: OpenZip(bufstart, buflen, ZIP_MEMORY);
  168. // If the file is opened through a pipe, then items may only be
  169. // accessed in increasing order, and an item may only be unzipped once,
  170. // although GetZipItem can be called immediately before and after unzipping
  171. // it. If it's opened i n any other way, then full random access is possible.
  172. // Note: pipe input is not yet implemented.
  173. ///////////////////////////////////////////////////////////////////////////////
  174. //
  175. // GetZipItem()
  176. //
  177. // Purpose: Get information about an item in an open zip archive
  178. //
  179. // Parameters: hz - handle of open zip archive
  180. // index - index number (0 based) of item in zip
  181. // ze - pointer to a ZIPENTRY (if ANSI) or ZIPENTRYW struct
  182. // (if Unicode)
  183. //
  184. // Returns: ZRESULT - ZR_OK if success, otherwise some other value
  185. //
  186. #ifdef _UNICODE
  187. #define GetZipItem GetZipItemW
  188. #else
  189. #define GetZipItem GetZipItemA
  190. #endif
  191. ZRESULT GetZipItemA(HZIP hz, int index, ZIPENTRY *ze);
  192. ZRESULT GetZipItemW(HZIP hz, int index, ZIPENTRYW *ze);
  193. // GetZipItem - call this to get information about an item in the zip.
  194. // If index is -1 and the file wasn't opened through a pipe,
  195. // then it returns information about the whole zipfile
  196. // (and in particular ze.index returns the number of index items).
  197. // Note: the item might be a directory (ze.attr & FILE_ATTRIBUTE_DIRECTORY)
  198. // See below for notes on what happens when you unzip such an item.
  199. // Note: if you are opening the zip through a pipe, then random access
  200. // is not possible and GetZipItem(-1) fails and you can't discover the number
  201. // of items except by calling GetZipItem on each one of them in turn,
  202. // starting at 0, until eventually the call fails. Also, in the event that
  203. // you are opening through a pipe and the zip was itself created into a pipe,
  204. // then then comp_size and sometimes unc_size as well may not be known until
  205. // after the item has been unzipped.
  206. ///////////////////////////////////////////////////////////////////////////////
  207. //
  208. // FindZipItem()
  209. //
  210. // Purpose: Find item by name and return information about it
  211. //
  212. // Parameters: hz - handle of open zip archive
  213. // name - name of file to look for inside zip archive
  214. // ic - TRUE = case insensitive
  215. // index - pointer to index number returned, or -1
  216. // ze - pointer to a ZIPENTRY (if ANSI) or ZIPENTRYW struct
  217. // (if Unicode)
  218. //
  219. // Returns: ZRESULT - ZR_OK if success, otherwise some other value
  220. //
  221. #ifdef _UNICODE
  222. #define FindZipItem FindZipItemW
  223. #else
  224. #define FindZipItem FindZipItemA
  225. #endif
  226. ZRESULT FindZipItemA(HZIP hz, const TCHAR *name, bool ic, int *index, ZIPENTRY *ze);
  227. ZRESULT FindZipItemW(HZIP hz, const TCHAR *name, bool ic, int *index, ZIPENTRYW *ze);
  228. // FindZipItem - finds an item by name. ic means 'insensitive to case'.
  229. // It returns the index of the item, and returns information about it.
  230. // If nothing was found, then index is set to -1 and the function returns
  231. // an error code.
  232. ///////////////////////////////////////////////////////////////////////////////
  233. //
  234. // UnzipItem()
  235. //
  236. // Purpose: Find item by index and unzip it
  237. //
  238. // Parameters: hz - handle of open zip archive
  239. // index - index number of file to unzip
  240. // dst - target file name of unzipped file
  241. // len - for memory (ZIP_MEMORY. length of buffer;
  242. // otherwise 0
  243. // flags - indicates usage, see below; for files, this will be
  244. // ZIP_FILENAME
  245. //
  246. // Returns: ZRESULT - ZR_OK if success, otherwise some other value
  247. //
  248. ZRESULT UnzipItem(HZIP hz, int index, void *dst, unsigned int len, DWORD flags);
  249. // UnzipItem - given an index to an item, unzips it. You can unzip to:
  250. // to a pipe: UnzipItem(hz,i, hpipe_write,0,ZIP_HANDLE);
  251. // to a file (by handle): UnzipItem(hz,i, hfile,0,ZIP_HANDLE);
  252. // to a file (by name): UnzipItem(hz,i, ze.name,0,ZIP_FILENAME);
  253. // to a memory block: UnzipItem(hz,i, buf,buflen,ZIP_MEMORY);
  254. // In the final case, if the buffer isn't large enough to hold it all,
  255. // then the return code indicates that more is yet to come. If it was
  256. // large enough, and you want to know precisely how big, GetZipItem.
  257. // Note: zip files are normally stored with relative pathnames. If you
  258. // unzip with ZIP_FILENAME a relative pathname then the item gets created
  259. // relative to the current directory - it first ensures that all necessary
  260. // subdirectories have been created. Also, the item may itself be a directory.
  261. // If you unzip a directory with ZIP_FILENAME, then the directory gets created.
  262. // If you unzip it to a handle or a memory block, then nothing gets created
  263. // and it emits 0 bytes.
  264. ///////////////////////////////////////////////////////////////////////////////
  265. //
  266. // CloseZip()
  267. //
  268. // Purpose: Close an open zip archive
  269. //
  270. // Parameters: hz - handle to an open zip archive
  271. //
  272. // Returns: ZRESULT - ZR_OK if success, otherwise some other value
  273. //
  274. ZRESULT CloseZip(HZIP hz);
  275. // CloseZip - the zip handle must be closed with this function.
  276. unsigned int FormatZipMessage(ZRESULT code, char *buf,unsigned int len);
  277. // FormatZipMessage - given an error code, formats it as a string.
  278. // It returns the length of the error message. If buf/len points
  279. // to a real buffer, then it also writes as much as possible into there.
  280. // These are the result codes:
  281. #define ZR_OK 0x00000000 // nb. the pseudo-code zr-recent is never returned,
  282. #define ZR_RECENT 0x00000001 // but can be passed to FormatZipMessage.
  283. // The following come from general system stuff (e.g. files not openable)
  284. #define ZR_GENMASK 0x0000FF00
  285. #define ZR_NODUPH 0x00000100 // couldn't duplicate the handle
  286. #define ZR_NOFILE 0x00000200 // couldn't create/open the file
  287. #define ZR_NOALLOC 0x00000300 // failed to allocate some resource
  288. #define ZR_WRITE 0x00000400 // a general error writing to the file
  289. #define ZR_NOTFOUND 0x00000500 // couldn't find that file in the zip
  290. #define ZR_MORE 0x00000600 // there's still more data to be unzipped
  291. #define ZR_CORRUPT 0x00000700 // the zipfile is corrupt or not a zipfile
  292. #define ZR_READ 0x00000800 // a general error reading the file
  293. // The following come from mistakes on the part of the caller
  294. #define ZR_CALLERMASK 0x00FF0000
  295. #define ZR_ARGS 0x00010000 // general mistake with the arguments
  296. #define ZR_NOTMMAP 0x00020000 // tried to ZipGetMemory, but that only works on mmap zipfiles, which yours wasn't
  297. #define ZR_MEMSIZE 0x00030000 // the memory size is too small
  298. #define ZR_FAILED 0x00040000 // the thing was already failed when you called this function
  299. #define ZR_ENDED 0x00050000 // the zip creation has already been closed
  300. #define ZR_MISSIZE 0x00060000 // the indicated input file size turned out mistaken
  301. #define ZR_PARTIALUNZ 0x00070000 // the file had already been partially unzipped
  302. #define ZR_ZMODE 0x00080000 // tried to mix creating/opening a zip
  303. // The following come from bugs within the zip library itself
  304. #define ZR_BUGMASK 0xFF000000
  305. #define ZR_NOTINITED 0x01000000 // initialisation didn't work
  306. #define ZR_SEEK 0x02000000 // trying to seek in an unseekable file
  307. #define ZR_NOCHANGE 0x04000000 // changed its mind on storage, but not allowed
  308. #define ZR_FLATE 0x05000000 // an internal error in the de/inflation code
  309. // e.g.
  310. //
  311. // SetCurrentDirectory("c:\\docs\\stuff");
  312. // HZIP hz = OpenZip("c:\\stuff.zip",0,ZIP_FILENAME);
  313. // ZIPENTRY ze; GetZipItem(hz,-1,&ze); int numitems=ze.index;
  314. // for (int i=0; i<numitems; i++)
  315. // { GetZipItem(hz,i,&ze);
  316. // UnzipItem(hz,i,ze.name,0,ZIP_FILENAME);
  317. // }
  318. // CloseZip(hz);
  319. //
  320. //
  321. // HRSRC hrsrc = FindResource(hInstance,MAKEINTRESOURCE(1),RT_RCDATA);
  322. // HANDLE hglob = LoadResource(hInstance,hrsrc);
  323. // void *zipbuf=LockResource(hglob);
  324. // unsigned int ziplen=SizeofResource(hInstance,hrsrc);
  325. // HZIP hz = OpenZip(zipbuf, ziplen, ZIP_MEMORY);
  326. // - unzip to a membuffer -
  327. // ZIPENTRY ze; int i; FindZipItem(hz,"file.dat",&i,&ze);
  328. // char *ibuf = new char[ze.unc_size];
  329. // UnzipItem(hz,i, ibuf, ze.unc_size,ZIP_MEMORY);
  330. // delete[] buf;
  331. // - unzip to a fixed membuff -
  332. // ZIPENTRY ze; int i; FindZipItem(hz,"file.dat",&i,&ze);
  333. // char ibuf[1024]; ZIPRESULT zr=ZR_MORE; unsigned long totsize=0;
  334. // while (zr==ZR_MORE)
  335. // { zr = UnzipItem(hz,i, ibuf,1024,ZIP_MEMORY);
  336. // unsigned long bufsize=1024; if (zr==ZR_OK) bufsize=ze.unc_size-totsize;
  337. // totsize+=bufsize;
  338. // }
  339. // - unzip to a pipe -
  340. // HANDLE hthread=CreateWavReaderThread(&hread,&hwrite);
  341. // FindZipItem(hz,"sound.wav",&i,&ze);
  342. // UnzipItem(hz,i, hwrite,0,ZIP_HANDLE);
  343. // CloseHandle(hwrite);
  344. // WaitForSingleObject(hthread,INFINITE);
  345. // CloseHandle(hread); CloseHandle(hthread);
  346. // - finished -
  347. // CloseZip(hz);
  348. // // note: no need to free resources obtained through Find/Load/LockResource
  349. //
  350. //
  351. // SetCurrentDirectory("c:\\docs\\pipedzipstuff");
  352. // HANDLE hread,hwrite; CreatePipe(&hread,&hwrite);
  353. // CreateZipWriterThread(hwrite);
  354. // HZIP hz = OpenZip(hread,0,ZIP_HANDLE);
  355. // for (int i=0; ; i++)
  356. // { ZIPENTRY ze; ZRESULT res = GetZipItem(hz,i,&ze);
  357. // if (res!=ZE_OK) break; // no more
  358. // UnzipItem(hz,i, ze.name,0,ZIP_FILENAME);
  359. // }
  360. // CloseZip(hz);
  361. //
  362. // Now we indulge in a little skullduggery so that the code works whether
  363. // the user has included just zip or both zip and unzip.
  364. // Idea: if header files for both zip and unzip are present, then presumably
  365. // the cpp files for zip and unzip are both present, so we will call
  366. // one or the other of them based on a dynamic choice. If the header file
  367. // for only one is present, then we will bind to that particular one.
  368. HZIP OpenZipU(void *z,unsigned int len,DWORD flags);
  369. ZRESULT CloseZipU(HZIP hz);
  370. unsigned int FormatZipMessageU(ZRESULT code, char *buf,unsigned int len);
  371. bool IsZipHandleU(HZIP hz);
  372. #define OpenZip OpenZipU
  373. #ifdef XZIP_H
  374. #undef CloseZip
  375. #define CloseZip(hz) (IsZipHandleU(hz)?CloseZipU(hz):CloseZipZ(hz))
  376. #else
  377. #define CloseZip CloseZipU
  378. #define FormatZipMessage FormatZipMessageU
  379. #endif
  380. // safe defintion of unzip, catches exceptions
  381. bool SafeUnzipMemory( const void *pvZipped, int cubZipped, void *pvDest, int cubDest /* should be the exact expected unzipped size */ );
  382. #endif //XUNZIP_H