XUnzip.h 17 KB

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