Keine Beschreibung

unzip.c 69KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840
  1. /* unzip.c -- IO for uncompress .zip files using zlib
  2. Version 1.1, February 14h, 2010
  3. part of the MiniZip project
  4. Copyright (C) 1998-2010 Gilles Vollant
  5. http://www.winimage.com/zLibDll/minizip.html
  6. Modifications of Unzip for Zip64
  7. Copyright (C) 2007-2008 Even Rouault
  8. Modifications for Zip64 support on both zip and unzip
  9. Copyright (C) 2009-2010 Mathias Svensson
  10. http://result42.com
  11. Modifications for AES, PKWARE disk spanning
  12. Copyright (C) 2010-2014 Nathan Moinvaziri
  13. This program is distributed under the terms of the same license as zlib.
  14. See the accompanying LICENSE file for the full text of the license.
  15. */
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. /*#ifndef NOUNCRYPT
  20. # define NOUNCRYPT
  21. #endif*/
  22. #include "zlib.h"
  23. #include "unzip.h"
  24. #include "Common.h"
  25. #ifdef STDC
  26. # include <stddef.h>
  27. # include <string.h>
  28. # include <stdlib.h>
  29. #endif
  30. #ifdef NO_ERRNO_H
  31. extern int errno;
  32. #else
  33. # include <errno.h>
  34. #endif
  35. #ifdef HAVE_AES
  36. # define AES_METHOD (99)
  37. # define AES_PWVERIFYSIZE (2)
  38. # define AES_MAXSALTLENGTH (16)
  39. # define AES_AUTHCODESIZE (10)
  40. # define AES_HEADERSIZE (11)
  41. # define AES_KEYSIZE(mode) (64 + (mode * 64))
  42. # include "aes.h"
  43. # include "fileenc.h"
  44. #endif
  45. #ifndef NOUNCRYPT
  46. # include "crypt.h"
  47. #endif
  48. #ifndef local
  49. # define local static
  50. #endif
  51. /* compile with -Dlocal if your debugger can't find static symbols */
  52. #define DISKHEADERMAGIC (0x08074b50)
  53. #define LOCALHEADERMAGIC (0x04034b50)
  54. #define CENTRALHEADERMAGIC (0x02014b50)
  55. #define ENDHEADERMAGIC (0x06054b50)
  56. #define ZIP64ENDHEADERMAGIC (0x06064b50)
  57. #define ZIP64ENDLOCHEADERMAGIC (0x07064b50)
  58. #define SIZECENTRALDIRITEM (0x2e)
  59. #define SIZECENTRALHEADERLOCATOR (0x14) /* 20 */
  60. #define SIZEZIPLOCALHEADER (0x1e)
  61. #ifndef BUFREADCOMMENT
  62. # define BUFREADCOMMENT (0x400)
  63. #endif
  64. #ifndef UNZ_BUFSIZE
  65. # define UNZ_BUFSIZE (64 * 1024)
  66. #endif
  67. #ifndef UNZ_MAXFILENAMEINZIP
  68. # define UNZ_MAXFILENAMEINZIP (256)
  69. #endif
  70. #ifndef ALLOC
  71. # define ALLOC(size) (malloc(size))
  72. #endif
  73. #ifndef TRYFREE
  74. # define TRYFREE(p) {if (p) free(p); }
  75. #endif
  76. const char unz_copyright[] =
  77. " unzip 1.01 Copyright 1998-2004 Gilles Vollant - http://www.winimage.com/zLibDll";
  78. /* unz_file_info_interntal contain internal info about a file in zipfile*/
  79. typedef struct unz_file_info64_internal_s {
  80. ZPOS64_T offset_curfile; /* relative offset of local header 8 bytes */
  81. ZPOS64_T byte_before_the_zipfile; /* byte before the zipfile, (>0 for sfx) */
  82. #ifdef HAVE_AES
  83. uLong aes_encryption_mode;
  84. uLong aes_compression_method;
  85. uLong aes_version;
  86. #endif
  87. } unz_file_info64_internal;
  88. /* file_in_zip_read_info_s contain internal information about a file in zipfile */
  89. typedef struct {
  90. Bytef *read_buffer; /* internal buffer for compressed data */
  91. z_stream stream; /* zLib stream structure for inflate */
  92. #ifdef HAVE_BZIP2
  93. bz_stream bstream; /* bzLib stream structure for bziped */
  94. #endif
  95. #ifdef HAVE_AES
  96. fcrypt_ctx aes_ctx;
  97. #endif
  98. ZPOS64_T pos_in_zipfile; /* position in byte on the zipfile, for fseek */
  99. uLong stream_initialised; /* flag set if stream structure is initialised */
  100. ZPOS64_T offset_local_extrafield; /* offset of the local extra field */
  101. uInt size_local_extrafield; /* size of the local extra field */
  102. ZPOS64_T pos_local_extrafield; /* position in the local extra field in read */
  103. ZPOS64_T total_out_64;
  104. uLong crc32; /* crc32 of all data uncompressed */
  105. uLong crc32_wait; /* crc32 we must obtain after decompress all */
  106. ZPOS64_T rest_read_compressed; /* number of byte to be decompressed */
  107. ZPOS64_T rest_read_uncompressed; /* number of byte to be obtained after decomp */
  108. zlib_filefunc64_32_def z_filefunc;
  109. voidpf filestream; /* io structore of the zipfile */
  110. uLong compression_method; /* compression method (0==store) */
  111. ZPOS64_T byte_before_the_zipfile; /* byte before the zipfile, (>0 for sfx) */
  112. int raw;
  113. } file_in_zip64_read_info_s;
  114. /* unz64_s contain internal information about the zipfile */
  115. typedef struct {
  116. zlib_filefunc64_32_def z_filefunc;
  117. voidpf filestream; /* io structure of the current zipfile */
  118. voidpf filestream_with_CD; /* io structure of the disk with the central directory */
  119. unz_global_info64 gi; /* public global information */
  120. ZPOS64_T byte_before_the_zipfile; /* byte before the zipfile, (>0 for sfx)*/
  121. ZPOS64_T num_file; /* number of the current file in the zipfile*/
  122. ZPOS64_T pos_in_central_dir; /* pos of the current file in the central dir*/
  123. ZPOS64_T current_file_ok; /* flag about the usability of the current file*/
  124. ZPOS64_T central_pos; /* position of the beginning of the central dir*/
  125. uLong number_disk; /* number of the current disk, used for spanning ZIP*/
  126. ZPOS64_T size_central_dir; /* size of the central directory */
  127. ZPOS64_T offset_central_dir; /* offset of start of central directory with
  128. respect to the starting disk number */
  129. unz_file_info64 cur_file_info; /* public info about the current file in zip*/
  130. unz_file_info64_internal cur_file_info_internal;
  131. /* private info about it*/
  132. file_in_zip64_read_info_s *pfile_in_zip_read;
  133. /* structure about the current file if we are decompressing it */
  134. int isZip64; /* is the current file zip64 */
  135. #ifndef NOUNCRYPT
  136. unsigned long keys[3]; /* keys defining the pseudo-random sequence */
  137. const unsigned long *pcrc_32_tab;
  138. #endif
  139. } unz64_s;
  140. /* Translate date/time from Dos format to tm_unz (readable more easily) */
  141. local void unz64local_DosDateToTmuDate(ZPOS64_T ulDosDate, tm_unz *ptm)
  142. {
  143. ZPOS64_T uDate = (ZPOS64_T)(ulDosDate >> 16);
  144. ptm->tm_mday = (uInt)(uDate & 0x1f);
  145. ptm->tm_mon = (uInt)((((uDate) & 0x1E0) / 0x20) - 1);
  146. ptm->tm_year = (uInt)(((uDate & 0x0FE00) / 0x0200) + 1980);
  147. ptm->tm_hour = (uInt)((ulDosDate & 0xF800) / 0x800);
  148. ptm->tm_min = (uInt)((ulDosDate & 0x7E0) / 0x20);
  149. ptm->tm_sec = (uInt)(2 * (ulDosDate & 0x1f));
  150. #define unz64local_in_range(min, max, value) ((min) <= (value) && (value) <= (max))
  151. if (!unz64local_in_range(0, 11, ptm->tm_mon) ||
  152. !unz64local_in_range(1, 31, ptm->tm_mday) ||
  153. !unz64local_in_range(0, 23, ptm->tm_hour) ||
  154. !unz64local_in_range(0, 59, ptm->tm_min) ||
  155. !unz64local_in_range(0, 59, ptm->tm_sec))
  156. /* Invalid date stored, so don't return it. */
  157. memset(ptm, 0, sizeof(tm_unz));
  158. #undef unz64local_in_range
  159. }
  160. /* Read a byte from a gz_stream; Return EOF for end of file. */
  161. local int unz64local_getByte(const zlib_filefunc64_32_def *pzlib_filefunc_def, voidpf filestream, int *pi)
  162. {
  163. unsigned char c;
  164. int err = (int)ZREAD64(*pzlib_filefunc_def, filestream, &c, 1);
  165. if (err == 1) {
  166. *pi = (int)c;
  167. return UNZ_OK;
  168. }
  169. if (ZERROR64(*pzlib_filefunc_def, filestream))
  170. return UNZ_ERRNO;
  171. return UNZ_EOF;
  172. }
  173. local int unz64local_getShort OF((const zlib_filefunc64_32_def * pzlib_filefunc_def, voidpf filestream, uLong * pX));
  174. local int unz64local_getShort(const zlib_filefunc64_32_def *pzlib_filefunc_def, voidpf filestream, uLong *pX)
  175. {
  176. uLong x;
  177. int i = 0;
  178. int err;
  179. err = unz64local_getByte(pzlib_filefunc_def, filestream, &i);
  180. x = (uLong)i;
  181. if (err == UNZ_OK)
  182. err = unz64local_getByte(pzlib_filefunc_def, filestream, &i);
  183. x |= ((uLong)i) << 8;
  184. if (err == UNZ_OK)
  185. *pX = x;
  186. else
  187. *pX = 0;
  188. return err;
  189. }
  190. local int unz64local_getLong OF((const zlib_filefunc64_32_def * pzlib_filefunc_def, voidpf filestream, uLong * pX));
  191. local int unz64local_getLong(const zlib_filefunc64_32_def *pzlib_filefunc_def, voidpf filestream, uLong *pX)
  192. {
  193. uLong x;
  194. int i = 0;
  195. int err;
  196. err = unz64local_getByte(pzlib_filefunc_def, filestream, &i);
  197. x = (uLong)i;
  198. if (err == UNZ_OK)
  199. err = unz64local_getByte(pzlib_filefunc_def, filestream, &i);
  200. x |= ((uLong)i) << 8;
  201. if (err == UNZ_OK)
  202. err = unz64local_getByte(pzlib_filefunc_def, filestream, &i);
  203. x |= ((uLong)i) << 16;
  204. if (err == UNZ_OK)
  205. err = unz64local_getByte(pzlib_filefunc_def, filestream, &i);
  206. x += ((uLong)i) << 24;
  207. if (err == UNZ_OK)
  208. *pX = x;
  209. else
  210. *pX = 0;
  211. return err;
  212. }
  213. local int unz64local_getLong64 OF((const zlib_filefunc64_32_def * pzlib_filefunc_def, voidpf filestream, ZPOS64_T * pX));
  214. local int unz64local_getLong64(const zlib_filefunc64_32_def *pzlib_filefunc_def, voidpf filestream, ZPOS64_T *pX)
  215. {
  216. ZPOS64_T x;
  217. int i = 0;
  218. int err;
  219. err = unz64local_getByte(pzlib_filefunc_def, filestream, &i);
  220. x = (ZPOS64_T)i;
  221. if (err == UNZ_OK)
  222. err = unz64local_getByte(pzlib_filefunc_def, filestream, &i);
  223. x |= ((ZPOS64_T)i) << 8;
  224. if (err == UNZ_OK)
  225. err = unz64local_getByte(pzlib_filefunc_def, filestream, &i);
  226. x |= ((ZPOS64_T)i) << 16;
  227. if (err == UNZ_OK)
  228. err = unz64local_getByte(pzlib_filefunc_def, filestream, &i);
  229. x |= ((ZPOS64_T)i) << 24;
  230. if (err == UNZ_OK)
  231. err = unz64local_getByte(pzlib_filefunc_def, filestream, &i);
  232. x |= ((ZPOS64_T)i) << 32;
  233. if (err == UNZ_OK)
  234. err = unz64local_getByte(pzlib_filefunc_def, filestream, &i);
  235. x |= ((ZPOS64_T)i) << 40;
  236. if (err == UNZ_OK)
  237. err = unz64local_getByte(pzlib_filefunc_def, filestream, &i);
  238. x |= ((ZPOS64_T)i) << 48;
  239. if (err == UNZ_OK)
  240. err = unz64local_getByte(pzlib_filefunc_def, filestream, &i);
  241. x |= ((ZPOS64_T)i) << 56;
  242. if (err == UNZ_OK)
  243. *pX = x;
  244. else
  245. *pX = 0;
  246. return err;
  247. }
  248. /* Locate the Central directory of a zip file (at the end, just before the global comment) */
  249. local ZPOS64_T unz64local_SearchCentralDir OF((const zlib_filefunc64_32_def * pzlib_filefunc_def, voidpf filestream));
  250. local ZPOS64_T unz64local_SearchCentralDir(const zlib_filefunc64_32_def *pzlib_filefunc_def, voidpf filestream)
  251. {
  252. unsigned char *buf;
  253. ZPOS64_T file_size;
  254. ZPOS64_T back_read = 4;
  255. ZPOS64_T max_back = 0xffff; /* maximum size of global comment */
  256. ZPOS64_T pos_found = 0;
  257. uLong read_size;
  258. ZPOS64_T read_pos;
  259. int i;
  260. buf = (unsigned char *)ALLOC(BUFREADCOMMENT + 4);
  261. if (buf == NULL)
  262. return 0;
  263. if (ZSEEK64(*pzlib_filefunc_def, filestream, 0, ZLIB_FILEFUNC_SEEK_END) != 0) {
  264. TRYFREE(buf);
  265. return 0;
  266. }
  267. file_size = ZTELL64(*pzlib_filefunc_def, filestream);
  268. if (max_back > file_size)
  269. max_back = file_size;
  270. while (back_read < max_back) {
  271. if (back_read + BUFREADCOMMENT > max_back)
  272. back_read = max_back;
  273. else
  274. back_read += BUFREADCOMMENT;
  275. read_pos = file_size - back_read;
  276. read_size = ((BUFREADCOMMENT + 4) < (file_size - read_pos)) ?
  277. (BUFREADCOMMENT + 4) : (uLong)(file_size - read_pos);
  278. if (ZSEEK64(*pzlib_filefunc_def, filestream, read_pos, ZLIB_FILEFUNC_SEEK_SET) != 0)
  279. break;
  280. if (ZREAD64(*pzlib_filefunc_def, filestream, buf, read_size) != read_size)
  281. break;
  282. for (i = (int)read_size - 3; (i--) > 0; )
  283. if (((*(buf + i)) == (ENDHEADERMAGIC & 0xff)) &&
  284. ((*(buf + i + 1)) == (ENDHEADERMAGIC >> 8 & 0xff)) &&
  285. ((*(buf + i + 2)) == (ENDHEADERMAGIC >> 16 & 0xff)) &&
  286. ((*(buf + i + 3)) == (ENDHEADERMAGIC >> 24 & 0xff))) {
  287. pos_found = read_pos + i;
  288. break;
  289. }
  290. if (pos_found != 0)
  291. break;
  292. }
  293. TRYFREE(buf);
  294. return pos_found;
  295. }
  296. /* Locate the Central directory 64 of a zipfile (at the end, just before the global comment) */
  297. local ZPOS64_T unz64local_SearchCentralDir64 OF((const zlib_filefunc64_32_def * pzlib_filefunc_def, voidpf filestream,
  298. const ZPOS64_T endcentraloffset));
  299. local ZPOS64_T unz64local_SearchCentralDir64(const zlib_filefunc64_32_def *pzlib_filefunc_def, voidpf filestream,
  300. const ZPOS64_T endcentraloffset)
  301. {
  302. ZPOS64_T offset;
  303. uLong uL;
  304. /* Zip64 end of central directory locator */
  305. if (ZSEEK64(*pzlib_filefunc_def, filestream, endcentraloffset - SIZECENTRALHEADERLOCATOR, ZLIB_FILEFUNC_SEEK_SET) != 0)
  306. return 0;
  307. /* read locator signature */
  308. if (unz64local_getLong(pzlib_filefunc_def, filestream, &uL) != UNZ_OK)
  309. return 0;
  310. if (uL != ZIP64ENDLOCHEADERMAGIC)
  311. return 0;
  312. /* number of the disk with the start of the zip64 end of central directory */
  313. if (unz64local_getLong(pzlib_filefunc_def, filestream, &uL) != UNZ_OK)
  314. return 0;
  315. /* relative offset of the zip64 end of central directory record */
  316. if (unz64local_getLong64(pzlib_filefunc_def, filestream, &offset) != UNZ_OK)
  317. return 0;
  318. /* total number of disks */
  319. if (unz64local_getLong(pzlib_filefunc_def, filestream, &uL) != UNZ_OK)
  320. return 0;
  321. /* Goto end of central directory record */
  322. if (ZSEEK64(*pzlib_filefunc_def, filestream, offset, ZLIB_FILEFUNC_SEEK_SET) != 0)
  323. return 0;
  324. /* the signature */
  325. if (unz64local_getLong(pzlib_filefunc_def, filestream, &uL) != UNZ_OK)
  326. return 0;
  327. if (uL != ZIP64ENDHEADERMAGIC)
  328. return 0;
  329. return offset;
  330. }
  331. local unzFile unzOpenInternal(const void *path, zlib_filefunc64_32_def *pzlib_filefunc64_32_def)
  332. {
  333. unz64_s us;
  334. unz64_s *s;
  335. ZPOS64_T central_pos;
  336. uLong uL;
  337. voidpf filestream = NULL;
  338. ZPOS64_T number_entry_CD;
  339. int err = UNZ_OK;
  340. if (unz_copyright[0] != ' ')
  341. return NULL;
  342. us.filestream = NULL;
  343. us.filestream_with_CD = NULL;
  344. us.z_filefunc.zseek32_file = NULL;
  345. us.z_filefunc.ztell32_file = NULL;
  346. if (pzlib_filefunc64_32_def == NULL)
  347. fill_fopen64_filefunc(&us.z_filefunc.zfile_func64);
  348. else
  349. us.z_filefunc = *pzlib_filefunc64_32_def;
  350. us.filestream = ZOPEN64(us.z_filefunc, path, ZLIB_FILEFUNC_MODE_READ | ZLIB_FILEFUNC_MODE_EXISTING);
  351. if (us.filestream == NULL)
  352. return NULL;
  353. us.filestream_with_CD = us.filestream;
  354. us.isZip64 = 0;
  355. /* Use unz64local_SearchCentralDir first. Only based on the result
  356. is it necessary to locate the unz64local_SearchCentralDir64 */
  357. central_pos = unz64local_SearchCentralDir(&us.z_filefunc, us.filestream);
  358. if (central_pos) {
  359. if (ZSEEK64(us.z_filefunc, us.filestream, central_pos, ZLIB_FILEFUNC_SEEK_SET) != 0)
  360. err = UNZ_ERRNO;
  361. /* the signature, already checked */
  362. if (unz64local_getLong(&us.z_filefunc, us.filestream, &uL) != UNZ_OK)
  363. err = UNZ_ERRNO;
  364. /* number of this disk */
  365. if (unz64local_getShort(&us.z_filefunc, us.filestream, &uL) != UNZ_OK)
  366. err = UNZ_ERRNO;
  367. us.number_disk = uL;
  368. /* number of the disk with the start of the central directory */
  369. if (unz64local_getShort(&us.z_filefunc, us.filestream, &uL) != UNZ_OK)
  370. err = UNZ_ERRNO;
  371. us.gi.number_disk_with_CD = uL;
  372. /* total number of entries in the central directory on this disk */
  373. if (unz64local_getShort(&us.z_filefunc, us.filestream, &uL) != UNZ_OK)
  374. err = UNZ_ERRNO;
  375. us.gi.number_entry = uL;
  376. /* total number of entries in the central directory */
  377. if (unz64local_getShort(&us.z_filefunc, us.filestream, &uL) != UNZ_OK)
  378. err = UNZ_ERRNO;
  379. number_entry_CD = uL;
  380. if (number_entry_CD != us.gi.number_entry)
  381. err = UNZ_BADZIPFILE;
  382. /* size of the central directory */
  383. if (unz64local_getLong(&us.z_filefunc, us.filestream, &uL) != UNZ_OK)
  384. err = UNZ_ERRNO;
  385. us.size_central_dir = uL;
  386. /* offset of start of central directory with respect to the starting disk number */
  387. if (unz64local_getLong(&us.z_filefunc, us.filestream, &uL) != UNZ_OK)
  388. err = UNZ_ERRNO;
  389. us.offset_central_dir = uL;
  390. /* zipfile comment length */
  391. if (unz64local_getShort(&us.z_filefunc, us.filestream, &us.gi.size_comment) != UNZ_OK)
  392. err = UNZ_ERRNO;
  393. if ((err == UNZ_OK) &&
  394. ((us.gi.number_entry == 0xffff) || (us.size_central_dir == 0xffff) || (us.offset_central_dir == 0xffffffff))) {
  395. /* Format should be Zip64, as the central directory or file size is too large */
  396. central_pos = unz64local_SearchCentralDir64(&us.z_filefunc, us.filestream, central_pos);
  397. if (central_pos) {
  398. ZPOS64_T uL64;
  399. us.isZip64 = 1;
  400. if (ZSEEK64(us.z_filefunc, us.filestream, central_pos, ZLIB_FILEFUNC_SEEK_SET) != 0)
  401. err = UNZ_ERRNO;
  402. /* the signature, already checked */
  403. if (unz64local_getLong(&us.z_filefunc, us.filestream, &uL) != UNZ_OK)
  404. err = UNZ_ERRNO;
  405. /* size of zip64 end of central directory record */
  406. if (unz64local_getLong64(&us.z_filefunc, us.filestream, &uL64) != UNZ_OK)
  407. err = UNZ_ERRNO;
  408. /* version made by */
  409. if (unz64local_getShort(&us.z_filefunc, us.filestream, &uL) != UNZ_OK)
  410. err = UNZ_ERRNO;
  411. /* version needed to extract */
  412. if (unz64local_getShort(&us.z_filefunc, us.filestream, &uL) != UNZ_OK)
  413. err = UNZ_ERRNO;
  414. /* number of this disk */
  415. if (unz64local_getLong(&us.z_filefunc, us.filestream, &us.number_disk) != UNZ_OK)
  416. err = UNZ_ERRNO;
  417. /* number of the disk with the start of the central directory */
  418. if (unz64local_getLong(&us.z_filefunc, us.filestream, &us.gi.number_disk_with_CD) != UNZ_OK)
  419. err = UNZ_ERRNO;
  420. /* total number of entries in the central directory on this disk */
  421. if (unz64local_getLong64(&us.z_filefunc, us.filestream, &us.gi.number_entry) != UNZ_OK)
  422. err = UNZ_ERRNO;
  423. /* total number of entries in the central directory */
  424. if (unz64local_getLong64(&us.z_filefunc, us.filestream, &number_entry_CD) != UNZ_OK)
  425. err = UNZ_ERRNO;
  426. if (number_entry_CD != us.gi.number_entry)
  427. err = UNZ_BADZIPFILE;
  428. /* size of the central directory */
  429. if (unz64local_getLong64(&us.z_filefunc, us.filestream, &us.size_central_dir) != UNZ_OK)
  430. err = UNZ_ERRNO;
  431. /* offset of start of central directory with respect to the starting disk number */
  432. if (unz64local_getLong64(&us.z_filefunc, us.filestream, &us.offset_central_dir) != UNZ_OK)
  433. err = UNZ_ERRNO;
  434. } else
  435. err = UNZ_BADZIPFILE;
  436. }
  437. } else
  438. err = UNZ_ERRNO;
  439. if ((err == UNZ_OK) && (central_pos < us.offset_central_dir + us.size_central_dir))
  440. err = UNZ_BADZIPFILE;
  441. if (err != UNZ_OK) {
  442. ZCLOSE64(us.z_filefunc, us.filestream);
  443. return NULL;
  444. }
  445. if (us.gi.number_disk_with_CD == 0) {
  446. /* If there is only one disk open another stream so we don't have to seek between the CD
  447. and the file headers constantly */
  448. filestream = ZOPEN64(us.z_filefunc, path, ZLIB_FILEFUNC_MODE_READ | ZLIB_FILEFUNC_MODE_EXISTING);
  449. if (filestream != NULL)
  450. us.filestream = filestream;
  451. }
  452. /* Hack for zip files that have no respect for zip64
  453. if ((central_pos > 0xffffffff) && (us.offset_central_dir < 0xffffffff))
  454. us.offset_central_dir = central_pos - us.size_central_dir;*/
  455. us.byte_before_the_zipfile = central_pos - (us.offset_central_dir + us.size_central_dir);
  456. us.central_pos = central_pos;
  457. us.pfile_in_zip_read = NULL;
  458. s = (unz64_s *)ALLOC(sizeof(unz64_s));
  459. if (s != NULL) {
  460. *s = us;
  461. unzGoToFirstFile((unzFile)s);
  462. }
  463. return (unzFile)s;
  464. }
  465. extern unzFile ZEXPORT unzOpen2(const char *path, zlib_filefunc_def *pzlib_filefunc32_def)
  466. {
  467. if (pzlib_filefunc32_def != NULL) {
  468. zlib_filefunc64_32_def zlib_filefunc64_32_def_fill;
  469. fill_zlib_filefunc64_32_def_from_filefunc32(&zlib_filefunc64_32_def_fill, pzlib_filefunc32_def);
  470. return unzOpenInternal(path, &zlib_filefunc64_32_def_fill);
  471. }
  472. return unzOpenInternal(path, NULL);
  473. }
  474. extern unzFile ZEXPORT unzOpen2_64(const void *path, zlib_filefunc64_def *pzlib_filefunc_def)
  475. {
  476. if (pzlib_filefunc_def != NULL) {
  477. zlib_filefunc64_32_def zlib_filefunc64_32_def_fill;
  478. zlib_filefunc64_32_def_fill.zfile_func64 = *pzlib_filefunc_def;
  479. zlib_filefunc64_32_def_fill.ztell32_file = NULL;
  480. zlib_filefunc64_32_def_fill.zseek32_file = NULL;
  481. return unzOpenInternal(path, &zlib_filefunc64_32_def_fill);
  482. }
  483. return unzOpenInternal(path, NULL);
  484. }
  485. extern unzFile ZEXPORT unzOpen(const char *path)
  486. {
  487. return unzOpenInternal(path, NULL);
  488. }
  489. extern unzFile ZEXPORT unzOpen64(const void *path)
  490. {
  491. return unzOpenInternal(path, NULL);
  492. }
  493. extern int ZEXPORT unzClose(unzFile file)
  494. {
  495. unz64_s *s;
  496. if (file == NULL)
  497. return UNZ_PARAMERROR;
  498. s = (unz64_s *)file;
  499. if (s->pfile_in_zip_read != NULL)
  500. unzCloseCurrentFile(file);
  501. if ((s->filestream != NULL) && (s->filestream != s->filestream_with_CD))
  502. ZCLOSE64(s->z_filefunc, s->filestream);
  503. if (s->filestream_with_CD != NULL)
  504. ZCLOSE64(s->z_filefunc, s->filestream_with_CD);
  505. s->filestream = NULL;
  506. s->filestream_with_CD = NULL;
  507. TRYFREE(s);
  508. return UNZ_OK;
  509. }
  510. /* Goto to the next available disk for spanned archives */
  511. local int unzGoToNextDisk OF((unzFile file));
  512. local int unzGoToNextDisk(unzFile file)
  513. {
  514. unz64_s *s;
  515. file_in_zip64_read_info_s *pfile_in_zip_read_info;
  516. uLong number_disk_next = 0;
  517. s = (unz64_s *)file;
  518. if (s == NULL)
  519. return UNZ_PARAMERROR;
  520. pfile_in_zip_read_info = s->pfile_in_zip_read;
  521. number_disk_next = s->number_disk;
  522. if ((pfile_in_zip_read_info != NULL) && (pfile_in_zip_read_info->rest_read_uncompressed > 0))
  523. /* We are currently reading a file and we need the next sequential disk */
  524. number_disk_next += 1;
  525. else
  526. /* Goto the disk for the current file */
  527. number_disk_next = s->cur_file_info.disk_num_start;
  528. if (number_disk_next != s->number_disk) {
  529. /* Switch disks */
  530. if ((s->filestream != NULL) && (s->filestream != s->filestream_with_CD))
  531. ZCLOSE64(s->z_filefunc, s->filestream);
  532. if (number_disk_next == s->gi.number_disk_with_CD) {
  533. s->filestream = s->filestream_with_CD;
  534. } else {
  535. s->filestream = ZOPENDISK64(s->z_filefunc, s->filestream_with_CD, (unsigned int)number_disk_next,
  536. ZLIB_FILEFUNC_MODE_READ | ZLIB_FILEFUNC_MODE_EXISTING);
  537. }
  538. if (s->filestream == NULL)
  539. return UNZ_ERRNO;
  540. s->number_disk = number_disk_next;
  541. }
  542. return UNZ_OK;
  543. }
  544. extern int ZEXPORT unzGetGlobalInfo(unzFile file, unz_global_info *pglobal_info32)
  545. {
  546. unz64_s *s;
  547. if (file == NULL)
  548. return UNZ_PARAMERROR;
  549. s = (unz64_s *)file;
  550. /* to do : check if number_entry is not truncated */
  551. pglobal_info32->number_entry = (uLong)s->gi.number_entry;
  552. pglobal_info32->size_comment = s->gi.size_comment;
  553. pglobal_info32->number_disk_with_CD = s->gi.number_disk_with_CD;
  554. return UNZ_OK;
  555. }
  556. extern int ZEXPORT unzGetGlobalInfo64(unzFile file, unz_global_info64 *pglobal_info)
  557. {
  558. unz64_s *s;
  559. if (file == NULL)
  560. return UNZ_PARAMERROR;
  561. s = (unz64_s *)file;
  562. *pglobal_info = s->gi;
  563. return UNZ_OK;
  564. }
  565. extern int ZEXPORT unzGetGlobalComment(unzFile file, char *comment, uLong comment_size)
  566. {
  567. unz64_s *s;
  568. uLong bytes_to_read = comment_size;
  569. if (file == NULL)
  570. return (int)UNZ_PARAMERROR;
  571. s = (unz64_s *)file;
  572. if (bytes_to_read > s->gi.size_comment)
  573. bytes_to_read = s->gi.size_comment;
  574. if (ZSEEK64(s->z_filefunc, s->filestream_with_CD, s->central_pos + 22, ZLIB_FILEFUNC_SEEK_SET) != 0)
  575. return UNZ_ERRNO;
  576. if (bytes_to_read > 0) {
  577. *comment = 0;
  578. if (ZREAD64(s->z_filefunc, s->filestream_with_CD, comment, bytes_to_read) != bytes_to_read)
  579. return UNZ_ERRNO;
  580. }
  581. if ((comment != NULL) && (comment_size > s->gi.size_comment))
  582. *(comment + s->gi.size_comment) = 0;
  583. return (int)bytes_to_read;
  584. }
  585. /* Get Info about the current file in the zipfile, with internal only info */
  586. local int unz64local_GetCurrentFileInfoInternal(unzFile file, unz_file_info64 *pfile_info,
  587. unz_file_info64_internal *pfile_info_internal, char *filename, uLong filename_size, void *extrafield,
  588. uLong extrafield_size, char *comment, uLong comment_size)
  589. {
  590. unz64_s *s;
  591. unz_file_info64 file_info;
  592. unz_file_info64_internal file_info_internal;
  593. ZPOS64_T bytes_to_read;
  594. int err = UNZ_OK;
  595. uLong uMagic;
  596. long lSeek = 0;
  597. ZPOS64_T current_pos = 0;
  598. uLong acc = 0;
  599. uLong uL;
  600. ZPOS64_T uL64;
  601. if (file == NULL)
  602. return UNZ_PARAMERROR;
  603. s = (unz64_s *)file;
  604. if (ZSEEK64(s->z_filefunc, s->filestream_with_CD,
  605. s->pos_in_central_dir + s->byte_before_the_zipfile, ZLIB_FILEFUNC_SEEK_SET) != 0)
  606. err = UNZ_ERRNO;
  607. /* Check the magic */
  608. if (err == UNZ_OK) {
  609. if (unz64local_getLong(&s->z_filefunc, s->filestream_with_CD, &uMagic) != UNZ_OK)
  610. err = UNZ_ERRNO;
  611. else if (uMagic != CENTRALHEADERMAGIC)
  612. err = UNZ_BADZIPFILE;
  613. }
  614. /* Read central directory header */
  615. if (unz64local_getShort(&s->z_filefunc, s->filestream_with_CD, &file_info.version) != UNZ_OK)
  616. err = UNZ_ERRNO;
  617. if (unz64local_getShort(&s->z_filefunc, s->filestream_with_CD, &file_info.version_needed) != UNZ_OK)
  618. err = UNZ_ERRNO;
  619. if (unz64local_getShort(&s->z_filefunc, s->filestream_with_CD, &file_info.flag) != UNZ_OK)
  620. err = UNZ_ERRNO;
  621. if (unz64local_getShort(&s->z_filefunc, s->filestream_with_CD, &file_info.compression_method) != UNZ_OK)
  622. err = UNZ_ERRNO;
  623. if (unz64local_getLong(&s->z_filefunc, s->filestream_with_CD, &file_info.dosDate) != UNZ_OK)
  624. err = UNZ_ERRNO;
  625. unz64local_DosDateToTmuDate(file_info.dosDate, &file_info.tmu_date);
  626. if (unz64local_getLong(&s->z_filefunc, s->filestream_with_CD, &file_info.crc) != UNZ_OK)
  627. err = UNZ_ERRNO;
  628. if (unz64local_getLong(&s->z_filefunc, s->filestream_with_CD, &uL) != UNZ_OK)
  629. err = UNZ_ERRNO;
  630. file_info.compressed_size = uL;
  631. if (unz64local_getLong(&s->z_filefunc, s->filestream_with_CD, &uL) != UNZ_OK)
  632. err = UNZ_ERRNO;
  633. file_info.uncompressed_size = uL;
  634. if (unz64local_getShort(&s->z_filefunc, s->filestream_with_CD, &file_info.size_filename) != UNZ_OK)
  635. err = UNZ_ERRNO;
  636. if (unz64local_getShort(&s->z_filefunc, s->filestream_with_CD, &file_info.size_file_extra) != UNZ_OK)
  637. err = UNZ_ERRNO;
  638. if (unz64local_getShort(&s->z_filefunc, s->filestream_with_CD, &file_info.size_file_comment) != UNZ_OK)
  639. err = UNZ_ERRNO;
  640. if (unz64local_getShort(&s->z_filefunc, s->filestream_with_CD, &file_info.disk_num_start) != UNZ_OK)
  641. err = UNZ_ERRNO;
  642. if (unz64local_getShort(&s->z_filefunc, s->filestream_with_CD, &file_info.internal_fa) != UNZ_OK)
  643. err = UNZ_ERRNO;
  644. if (unz64local_getLong(&s->z_filefunc, s->filestream_with_CD, &file_info.external_fa) != UNZ_OK)
  645. err = UNZ_ERRNO;
  646. /* Relative offset of local header */
  647. if (unz64local_getLong(&s->z_filefunc, s->filestream_with_CD, &uL) != UNZ_OK)
  648. err = UNZ_ERRNO;
  649. file_info.size_file_extra_internal = 0;
  650. file_info.disk_offset = uL;
  651. file_info_internal.offset_curfile = uL;
  652. #ifdef HAVE_AES
  653. file_info_internal.aes_compression_method = 0;
  654. file_info_internal.aes_encryption_mode = 0;
  655. file_info_internal.aes_version = 0;
  656. #endif
  657. lSeek += file_info.size_filename;
  658. if ((err == UNZ_OK) && (filename != NULL)) {
  659. if (file_info.size_filename < filename_size) {
  660. *(filename + file_info.size_filename) = 0;
  661. bytes_to_read = file_info.size_filename;
  662. } else
  663. bytes_to_read = filename_size;
  664. if ((file_info.size_filename > 0) && (filename_size > 0))
  665. if (ZREAD64(s->z_filefunc, s->filestream_with_CD, filename, (uLong)bytes_to_read) != bytes_to_read)
  666. err = UNZ_ERRNO;
  667. lSeek -= (uLong)bytes_to_read;
  668. }
  669. /* Read extrafield */
  670. if ((err == UNZ_OK) && (extrafield != NULL)) {
  671. if (file_info.size_file_extra < extrafield_size)
  672. bytes_to_read = file_info.size_file_extra;
  673. else
  674. bytes_to_read = extrafield_size;
  675. if (lSeek != 0) {
  676. if (ZSEEK64(s->z_filefunc, s->filestream_with_CD, lSeek, ZLIB_FILEFUNC_SEEK_CUR) == 0)
  677. lSeek = 0;
  678. else
  679. err = UNZ_ERRNO;
  680. }
  681. if ((file_info.size_file_extra > 0) && (extrafield_size > 0))
  682. if (ZREAD64(s->z_filefunc, s->filestream_with_CD, extrafield, (uLong)bytes_to_read) != bytes_to_read)
  683. err = UNZ_ERRNO;
  684. lSeek += file_info.size_file_extra - (uLong)bytes_to_read;
  685. } else
  686. lSeek += file_info.size_file_extra;
  687. if ((err == UNZ_OK) && (file_info.size_file_extra != 0)) {
  688. if (lSeek != 0) {
  689. if (ZSEEK64(s->z_filefunc, s->filestream_with_CD, lSeek, ZLIB_FILEFUNC_SEEK_CUR) == 0)
  690. lSeek = 0;
  691. else
  692. err = UNZ_ERRNO;
  693. }
  694. /* We are going to parse the extra field so we need to move back */
  695. current_pos = ZTELL64(s->z_filefunc, s->filestream_with_CD);
  696. if (current_pos < file_info.size_file_extra)
  697. err = UNZ_ERRNO;
  698. current_pos -= file_info.size_file_extra;
  699. if (ZSEEK64(s->z_filefunc, s->filestream_with_CD, current_pos, ZLIB_FILEFUNC_SEEK_SET) != 0)
  700. err = UNZ_ERRNO;
  701. while ((err != UNZ_ERRNO) && (acc < file_info.size_file_extra)) {
  702. uLong headerid;
  703. uLong datasize;
  704. if (unz64local_getShort(&s->z_filefunc, s->filestream_with_CD, &headerid) != UNZ_OK)
  705. err = UNZ_ERRNO;
  706. if (unz64local_getShort(&s->z_filefunc, s->filestream_with_CD, &datasize) != UNZ_OK)
  707. err = UNZ_ERRNO;
  708. /* ZIP64 extra fields */
  709. if (headerid == 0x0001) {
  710. /* Subtract size of ZIP64 field, since ZIP64 is handled internally */
  711. file_info.size_file_extra_internal += 2 + 2 + datasize;
  712. if (file_info.uncompressed_size == 0xffffffff) {
  713. if (unz64local_getLong64(&s->z_filefunc, s->filestream_with_CD, &file_info.uncompressed_size) != UNZ_OK)
  714. err = UNZ_ERRNO;
  715. }
  716. if (file_info.compressed_size == 0xffffffff) {
  717. if (unz64local_getLong64(&s->z_filefunc, s->filestream_with_CD, &file_info.compressed_size) != UNZ_OK)
  718. err = UNZ_ERRNO;
  719. }
  720. if (file_info_internal.offset_curfile == 0xffffffff) {
  721. /* Relative Header offset */
  722. if (unz64local_getLong64(&s->z_filefunc, s->filestream_with_CD, &uL64) != UNZ_OK)
  723. err = UNZ_ERRNO;
  724. file_info_internal.offset_curfile = uL64;
  725. file_info.disk_offset = uL64;
  726. }
  727. if (file_info.disk_num_start == 0xffffffff) {
  728. /* Disk Start Number */
  729. if (unz64local_getLong(&s->z_filefunc, s->filestream_with_CD, &file_info.disk_num_start) != UNZ_OK)
  730. err = UNZ_ERRNO;
  731. }
  732. }
  733. #ifdef HAVE_AES
  734. /* AES header */
  735. else if (headerid == 0x9901) {
  736. /* Subtract size of AES field, since AES is handled internally */
  737. file_info.size_file_extra_internal += 2 + 2 + datasize;
  738. /* Verify version info */
  739. if (unz64local_getShort(&s->z_filefunc, s->filestream_with_CD, &uL) != UNZ_OK)
  740. err = UNZ_ERRNO;
  741. /* Support AE-1 and AE-2 */
  742. if (uL != 1 && uL != 2)
  743. err = UNZ_ERRNO;
  744. file_info_internal.aes_version = uL;
  745. if (unz64local_getByte(&s->z_filefunc, s->filestream_with_CD, (int *)&uL) != UNZ_OK)
  746. err = UNZ_ERRNO;
  747. if ((char)uL != 'A')
  748. err = UNZ_ERRNO;
  749. if (unz64local_getByte(&s->z_filefunc, s->filestream_with_CD, (int *)&uL) != UNZ_OK)
  750. err = UNZ_ERRNO;
  751. if ((char)uL != 'E')
  752. err = UNZ_ERRNO;
  753. /* Get AES encryption strength and actual compression method */
  754. if (unz64local_getByte(&s->z_filefunc, s->filestream_with_CD, (int *)&uL) != UNZ_OK)
  755. err = UNZ_ERRNO;
  756. file_info_internal.aes_encryption_mode = uL;
  757. if (unz64local_getShort(&s->z_filefunc, s->filestream_with_CD, &uL) != UNZ_OK)
  758. err = UNZ_ERRNO;
  759. file_info_internal.aes_compression_method = uL;
  760. }
  761. #endif
  762. else {
  763. if (ZSEEK64(s->z_filefunc, s->filestream_with_CD, datasize, ZLIB_FILEFUNC_SEEK_CUR) != 0)
  764. err = UNZ_ERRNO;
  765. }
  766. acc += 2 + 2 + datasize;
  767. }
  768. }
  769. if (file_info.disk_num_start == s->gi.number_disk_with_CD)
  770. file_info_internal.byte_before_the_zipfile = s->byte_before_the_zipfile;
  771. else
  772. file_info_internal.byte_before_the_zipfile = 0;
  773. if ((err == UNZ_OK) && (comment != NULL)) {
  774. if (file_info.size_file_comment < comment_size) {
  775. *(comment + file_info.size_file_comment) = 0;
  776. bytes_to_read = file_info.size_file_comment;
  777. } else
  778. bytes_to_read = comment_size;
  779. if (lSeek != 0) {
  780. if (ZSEEK64(s->z_filefunc, s->filestream_with_CD, lSeek, ZLIB_FILEFUNC_SEEK_CUR) != 0)
  781. err = UNZ_ERRNO;
  782. }
  783. if ((file_info.size_file_comment > 0) && (comment_size > 0))
  784. if (ZREAD64(s->z_filefunc, s->filestream_with_CD, comment, (uLong)bytes_to_read) != bytes_to_read)
  785. err = UNZ_ERRNO;
  786. lSeek += file_info.size_file_comment - (uLong)bytes_to_read;
  787. } else
  788. lSeek += file_info.size_file_comment;
  789. if ((err == UNZ_OK) && (pfile_info != NULL))
  790. *pfile_info = file_info;
  791. if ((err == UNZ_OK) && (pfile_info_internal != NULL))
  792. *pfile_info_internal = file_info_internal;
  793. return err;
  794. }
  795. extern int ZEXPORT unzGetCurrentFileInfo(unzFile file, unz_file_info *pfile_info, char *filename,
  796. uLong filename_size, void *extrafield, uLong extrafield_size, char *comment, uLong comment_size)
  797. {
  798. unz_file_info64 file_info64;
  799. int err;
  800. err = unz64local_GetCurrentFileInfoInternal(file, &file_info64, NULL, filename, filename_size,
  801. extrafield, extrafield_size, comment, comment_size);
  802. if ((err == UNZ_OK) && (pfile_info != NULL)) {
  803. pfile_info->version = file_info64.version;
  804. pfile_info->version_needed = file_info64.version_needed;
  805. pfile_info->flag = file_info64.flag;
  806. pfile_info->compression_method = file_info64.compression_method;
  807. pfile_info->dosDate = file_info64.dosDate;
  808. pfile_info->crc = file_info64.crc;
  809. pfile_info->size_filename = file_info64.size_filename;
  810. pfile_info->size_file_extra = file_info64.size_file_extra - file_info64.size_file_extra_internal;
  811. pfile_info->size_file_comment = file_info64.size_file_comment;
  812. pfile_info->disk_num_start = file_info64.disk_num_start;
  813. pfile_info->internal_fa = file_info64.internal_fa;
  814. pfile_info->external_fa = file_info64.external_fa;
  815. pfile_info->tmu_date = file_info64.tmu_date,
  816. pfile_info->compressed_size = (uLong)file_info64.compressed_size;
  817. pfile_info->uncompressed_size = (uLong)file_info64.uncompressed_size;
  818. }
  819. return err;
  820. }
  821. extern int ZEXPORT unzGetCurrentFileInfo64(unzFile file, unz_file_info64 *pfile_info, char *filename,
  822. uLong filename_size, void *extrafield, uLong extrafield_size, char *comment, uLong comment_size)
  823. {
  824. return unz64local_GetCurrentFileInfoInternal(file, pfile_info, NULL, filename, filename_size,
  825. extrafield, extrafield_size, comment, comment_size);
  826. }
  827. /* Read the local header of the current zipfile. Check the coherency of the local header and info in the
  828. end of central directory about this file store in *piSizeVar the size of extra info in local header
  829. (filename and size of extra field data) */
  830. local int unz64local_CheckCurrentFileCoherencyHeader(unz64_s *s, uInt *piSizeVar, ZPOS64_T *poffset_local_extrafield,
  831. uInt *psize_local_extrafield)
  832. {
  833. uLong uMagic, uL, uFlags;
  834. uLong size_filename;
  835. uLong size_extra_field;
  836. int err = UNZ_OK;
  837. int compression_method = 0;
  838. *piSizeVar = 0;
  839. *poffset_local_extrafield = 0;
  840. *psize_local_extrafield = 0;
  841. err = unzGoToNextDisk((unzFile)s);
  842. if (err != UNZ_OK)
  843. return err;
  844. if (ZSEEK64(s->z_filefunc, s->filestream, s->cur_file_info_internal.offset_curfile +
  845. s->cur_file_info_internal.byte_before_the_zipfile, ZLIB_FILEFUNC_SEEK_SET) != 0)
  846. return UNZ_ERRNO;
  847. if (err == UNZ_OK) {
  848. if (unz64local_getLong(&s->z_filefunc, s->filestream, &uMagic) != UNZ_OK)
  849. err = UNZ_ERRNO;
  850. else if (uMagic != LOCALHEADERMAGIC)
  851. err = UNZ_BADZIPFILE;
  852. }
  853. if (unz64local_getShort(&s->z_filefunc, s->filestream, &uL) != UNZ_OK)
  854. err = UNZ_ERRNO;
  855. if (unz64local_getShort(&s->z_filefunc, s->filestream, &uFlags) != UNZ_OK)
  856. err = UNZ_ERRNO;
  857. if (unz64local_getShort(&s->z_filefunc, s->filestream, &uL) != UNZ_OK)
  858. err = UNZ_ERRNO;
  859. else if ((err == UNZ_OK) && (uL != s->cur_file_info.compression_method))
  860. err = UNZ_BADZIPFILE;
  861. compression_method = (int)s->cur_file_info.compression_method;
  862. #ifdef HAVE_AES
  863. if (compression_method == AES_METHOD)
  864. compression_method = (int)s->cur_file_info_internal.aes_compression_method;
  865. #endif
  866. if ((err == UNZ_OK) && (compression_method != 0) &&
  867. #ifdef HAVE_BZIP2
  868. (compression_method != Z_BZIP2ED) &&
  869. #endif
  870. (compression_method != Z_DEFLATED))
  871. err = UNZ_BADZIPFILE;
  872. if (unz64local_getLong(&s->z_filefunc, s->filestream, &uL) != UNZ_OK) /* date/time */
  873. err = UNZ_ERRNO;
  874. if (unz64local_getLong(&s->z_filefunc, s->filestream, &uL) != UNZ_OK) /* crc */
  875. err = UNZ_ERRNO;
  876. else if ((err == UNZ_OK) && (uL != s->cur_file_info.crc) && ((uFlags & 8) == 0))
  877. err = UNZ_BADZIPFILE;
  878. if (unz64local_getLong(&s->z_filefunc, s->filestream, &uL) != UNZ_OK) /* size compr */
  879. err = UNZ_ERRNO;
  880. else if ((uL != 0xffffffff) && (err == UNZ_OK) && (uL != s->cur_file_info.compressed_size) && ((uFlags & 8) == 0))
  881. err = UNZ_BADZIPFILE;
  882. if (unz64local_getLong(&s->z_filefunc, s->filestream, &uL) != UNZ_OK) /* size uncompr */
  883. err = UNZ_ERRNO;
  884. else if ((uL != 0xffffffff) && (err == UNZ_OK) && (uL != s->cur_file_info.uncompressed_size) && ((uFlags & 8) == 0))
  885. err = UNZ_BADZIPFILE;
  886. if (unz64local_getShort(&s->z_filefunc, s->filestream, &size_filename) != UNZ_OK)
  887. err = UNZ_ERRNO;
  888. else if ((err == UNZ_OK) && (size_filename != s->cur_file_info.size_filename))
  889. err = UNZ_BADZIPFILE;
  890. *piSizeVar += (uInt)size_filename;
  891. if (unz64local_getShort(&s->z_filefunc, s->filestream, &size_extra_field) != UNZ_OK)
  892. err = UNZ_ERRNO;
  893. *poffset_local_extrafield = s->cur_file_info_internal.offset_curfile + SIZEZIPLOCALHEADER + size_filename;
  894. *psize_local_extrafield = (uInt)size_extra_field;
  895. *piSizeVar += (uInt)size_extra_field;
  896. return err;
  897. }
  898. /*
  899. Open for reading data the current file in the zipfile.
  900. If there is no error and the file is opened, the return value is UNZ_OK.
  901. */
  902. extern int ZEXPORT unzOpenCurrentFile3(unzFile file, int *method, int *level, int raw, const char *password)
  903. {
  904. int err = UNZ_OK;
  905. int compression_method;
  906. uInt iSizeVar;
  907. unz64_s *s;
  908. file_in_zip64_read_info_s *pfile_in_zip_read_info;
  909. ZPOS64_T offset_local_extrafield;
  910. uInt size_local_extrafield;
  911. #ifndef NOUNCRYPT
  912. char source[12];
  913. #else
  914. if (password != NULL)
  915. return UNZ_PARAMERROR;
  916. #endif
  917. if (file == NULL)
  918. return UNZ_PARAMERROR;
  919. s = (unz64_s *)file;
  920. if (!s->current_file_ok)
  921. return UNZ_PARAMERROR;
  922. if (s->pfile_in_zip_read != NULL)
  923. unzCloseCurrentFile(file);
  924. if (unz64local_CheckCurrentFileCoherencyHeader(s, &iSizeVar, &offset_local_extrafield, &size_local_extrafield) != UNZ_OK)
  925. return UNZ_BADZIPFILE;
  926. pfile_in_zip_read_info = (file_in_zip64_read_info_s *)ALLOC(sizeof(file_in_zip64_read_info_s));
  927. if (pfile_in_zip_read_info == NULL)
  928. return UNZ_INTERNALERROR;
  929. pfile_in_zip_read_info->read_buffer = (Bytef *)ALLOC(UNZ_BUFSIZE);
  930. pfile_in_zip_read_info->offset_local_extrafield = offset_local_extrafield;
  931. pfile_in_zip_read_info->size_local_extrafield = size_local_extrafield;
  932. pfile_in_zip_read_info->pos_local_extrafield = 0;
  933. pfile_in_zip_read_info->raw = raw;
  934. if (pfile_in_zip_read_info->read_buffer == NULL) {
  935. TRYFREE(pfile_in_zip_read_info);
  936. return UNZ_INTERNALERROR;
  937. }
  938. pfile_in_zip_read_info->stream_initialised = 0;
  939. compression_method = (int)s->cur_file_info.compression_method;
  940. #ifdef HAVE_AES
  941. if (compression_method == AES_METHOD)
  942. compression_method = (int)s->cur_file_info_internal.aes_compression_method;
  943. #endif
  944. if (method != NULL)
  945. *method = compression_method;
  946. if (level != NULL) {
  947. *level = 6;
  948. switch (s->cur_file_info.flag & 0x06) {
  949. case 6: *level = 1; break;
  950. case 4: *level = 2; break;
  951. case 2: *level = 9; break;
  952. }
  953. }
  954. if ((compression_method != 0) &&
  955. #ifdef HAVE_BZIP2
  956. (compression_method != Z_BZIP2ED) &&
  957. #endif
  958. (compression_method != Z_DEFLATED))
  959. err = UNZ_BADZIPFILE;
  960. pfile_in_zip_read_info->crc32_wait = s->cur_file_info.crc;
  961. pfile_in_zip_read_info->crc32 = 0;
  962. pfile_in_zip_read_info->total_out_64 = 0;
  963. pfile_in_zip_read_info->compression_method = compression_method;
  964. pfile_in_zip_read_info->filestream = s->filestream;
  965. pfile_in_zip_read_info->z_filefunc = s->z_filefunc;
  966. if (s->number_disk == s->gi.number_disk_with_CD)
  967. pfile_in_zip_read_info->byte_before_the_zipfile = s->byte_before_the_zipfile;
  968. else
  969. pfile_in_zip_read_info->byte_before_the_zipfile = 0;
  970. pfile_in_zip_read_info->stream.total_out = 0;
  971. pfile_in_zip_read_info->stream.total_in = 0;
  972. pfile_in_zip_read_info->stream.next_in = NULL;
  973. if (!raw) {
  974. if (compression_method == Z_BZIP2ED) {
  975. #ifdef HAVE_BZIP2
  976. pfile_in_zip_read_info->bstream.bzalloc = (void *(*)(void *, int, int)) 0;
  977. pfile_in_zip_read_info->bstream.bzfree = (free_func)0;
  978. pfile_in_zip_read_info->bstream.opaque = (voidpf)0;
  979. pfile_in_zip_read_info->bstream.state = (voidpf)0;
  980. pfile_in_zip_read_info->stream.zalloc = (alloc_func)0;
  981. pfile_in_zip_read_info->stream.zfree = (free_func)0;
  982. pfile_in_zip_read_info->stream.opaque = (voidpf)0;
  983. pfile_in_zip_read_info->stream.next_in = (voidpf)0;
  984. pfile_in_zip_read_info->stream.avail_in = 0;
  985. err = BZ2_bzDecompressInit(&pfile_in_zip_read_info->bstream, 0, 0);
  986. if (err == Z_OK)
  987. pfile_in_zip_read_info->stream_initialised = Z_BZIP2ED;
  988. else {
  989. TRYFREE(pfile_in_zip_read_info);
  990. return err;
  991. }
  992. #else
  993. pfile_in_zip_read_info->raw = 1;
  994. #endif
  995. } else if (compression_method == Z_DEFLATED) {
  996. pfile_in_zip_read_info->stream.zalloc = (alloc_func)0;
  997. pfile_in_zip_read_info->stream.zfree = (free_func)0;
  998. pfile_in_zip_read_info->stream.opaque = (voidpf)s;
  999. pfile_in_zip_read_info->stream.next_in = 0;
  1000. pfile_in_zip_read_info->stream.avail_in = 0;
  1001. err = inflateInit2(&pfile_in_zip_read_info->stream, -MAX_WBITS);
  1002. if (err == Z_OK)
  1003. pfile_in_zip_read_info->stream_initialised = Z_DEFLATED;
  1004. else {
  1005. TRYFREE(pfile_in_zip_read_info);
  1006. return err;
  1007. }
  1008. /* windowBits is passed < 0 to tell that there is no zlib header.
  1009. * Note that in this case inflate *requires* an extra "dummy" byte
  1010. * after the compressed stream in order to complete decompression and
  1011. * return Z_STREAM_END.
  1012. * In unzip, i don't wait absolutely Z_STREAM_END because I known the
  1013. * size of both compressed and uncompressed data
  1014. */
  1015. }
  1016. }
  1017. pfile_in_zip_read_info->rest_read_compressed = s->cur_file_info.compressed_size;
  1018. pfile_in_zip_read_info->rest_read_uncompressed = s->cur_file_info.uncompressed_size;
  1019. pfile_in_zip_read_info->pos_in_zipfile = s->cur_file_info_internal.offset_curfile + SIZEZIPLOCALHEADER + iSizeVar;
  1020. pfile_in_zip_read_info->stream.avail_in = (uInt)0;
  1021. s->pfile_in_zip_read = pfile_in_zip_read_info;
  1022. #ifndef NOUNCRYPT
  1023. if ((password != NULL) && ((s->cur_file_info.flag & 1) != 0)) {
  1024. if (ZSEEK64(s->z_filefunc, s->filestream,
  1025. s->pfile_in_zip_read->pos_in_zipfile + s->pfile_in_zip_read->byte_before_the_zipfile,
  1026. ZLIB_FILEFUNC_SEEK_SET) != 0)
  1027. return UNZ_INTERNALERROR;
  1028. #ifdef HAVE_AES
  1029. if (s->cur_file_info.compression_method == AES_METHOD) {
  1030. unsigned char passverify[AES_PWVERIFYSIZE];
  1031. unsigned char saltvalue[AES_MAXSALTLENGTH];
  1032. uInt saltlength;
  1033. if ((s->cur_file_info_internal.aes_encryption_mode < 1) ||
  1034. (s->cur_file_info_internal.aes_encryption_mode > 3))
  1035. return UNZ_INTERNALERROR;
  1036. saltlength = SALT_LENGTH(s->cur_file_info_internal.aes_encryption_mode);
  1037. if (ZREAD64(s->z_filefunc, s->filestream, saltvalue, saltlength) != saltlength)
  1038. return UNZ_INTERNALERROR;
  1039. if (ZREAD64(s->z_filefunc, s->filestream, passverify, AES_PWVERIFYSIZE) != AES_PWVERIFYSIZE)
  1040. return UNZ_INTERNALERROR;
  1041. fcrypt_init((int)s->cur_file_info_internal.aes_encryption_mode, (unsigned char *)password, (unsigned int)strlen(password), saltvalue,
  1042. passverify, &s->pfile_in_zip_read->aes_ctx);
  1043. pfile_in_zip_read_info->rest_read_compressed -= saltlength + AES_PWVERIFYSIZE;
  1044. pfile_in_zip_read_info->rest_read_compressed -= AES_AUTHCODESIZE;
  1045. s->pfile_in_zip_read->pos_in_zipfile += saltlength + AES_PWVERIFYSIZE;
  1046. } else
  1047. #endif
  1048. {
  1049. int i;
  1050. s->pcrc_32_tab = (const unsigned long *)get_crc_table();
  1051. init_keys(password, s->keys, s->pcrc_32_tab);
  1052. if (ZREAD64(s->z_filefunc, s->filestream, source, 12) < 12)
  1053. return UNZ_INTERNALERROR;
  1054. for (i = 0; i < 12; i++)
  1055. zdecode(s->keys, s->pcrc_32_tab, source[i]);
  1056. pfile_in_zip_read_info->rest_read_compressed -= 12;
  1057. s->pfile_in_zip_read->pos_in_zipfile += 12;
  1058. }
  1059. }
  1060. #endif
  1061. return UNZ_OK;
  1062. }
  1063. extern int ZEXPORT unzOpenCurrentFile(unzFile file)
  1064. {
  1065. return unzOpenCurrentFile3(file, NULL, NULL, 0, NULL);
  1066. }
  1067. extern int ZEXPORT unzOpenCurrentFilePassword(unzFile file, const char *password)
  1068. {
  1069. return unzOpenCurrentFile3(file, NULL, NULL, 0, password);
  1070. }
  1071. extern int ZEXPORT unzOpenCurrentFile2(unzFile file, int *method, int *level, int raw)
  1072. {
  1073. return unzOpenCurrentFile3(file, method, level, raw, NULL);
  1074. }
  1075. /* Read bytes from the current file.
  1076. buf contain buffer where data must be copied
  1077. len the size of buf.
  1078. return the number of byte copied if some bytes are copied
  1079. return 0 if the end of file was reached
  1080. return <0 with error code if there is an error (UNZ_ERRNO for IO error, or zLib error for uncompress error) */
  1081. extern int ZEXPORT unzReadCurrentFile(unzFile file, voidp buf, unsigned len)
  1082. {
  1083. int err = UNZ_OK;
  1084. uInt read = 0;
  1085. unz64_s *s;
  1086. file_in_zip64_read_info_s *pfile_in_zip_read_info;
  1087. if (file == NULL)
  1088. return UNZ_PARAMERROR;
  1089. s = (unz64_s *)file;
  1090. pfile_in_zip_read_info = s->pfile_in_zip_read;
  1091. if (pfile_in_zip_read_info == NULL)
  1092. return UNZ_PARAMERROR;
  1093. if (pfile_in_zip_read_info->read_buffer == NULL)
  1094. return UNZ_END_OF_LIST_OF_FILE;
  1095. if (len == 0)
  1096. return 0;
  1097. pfile_in_zip_read_info->stream.next_out = (Bytef *)buf;
  1098. pfile_in_zip_read_info->stream.avail_out = (uInt)len;
  1099. if (pfile_in_zip_read_info->raw) {
  1100. if (len > pfile_in_zip_read_info->rest_read_compressed + pfile_in_zip_read_info->stream.avail_in)
  1101. pfile_in_zip_read_info->stream.avail_out = (uInt)pfile_in_zip_read_info->rest_read_compressed +
  1102. pfile_in_zip_read_info->stream.avail_in;
  1103. } else {
  1104. // NOTE:
  1105. // This bit of code seems to try to set the amount of space in the output buffer based on the
  1106. // value stored in the headers stored in the .zip file. However, if those values are incorrect
  1107. // it may result in a loss of data when uncompresssing that file. The compressed data is still
  1108. // legit and will deflate without knowing the uncompressed code so this tidbit is unnecessary and
  1109. // may cause issues for some .zip files.
  1110. //
  1111. // It's removed in here to fix those issues.
  1112. //
  1113. // See: https://github.com/ZipArchive/ziparchive/issues/16
  1114. //
  1115. /*
  1116. FIXME: Upgrading to minizip 1.1 caused issues here, Uncommented the code that was commented before. 11/24/2015
  1117. */
  1118. if (len > pfile_in_zip_read_info->rest_read_uncompressed)
  1119. pfile_in_zip_read_info->stream.avail_out = (uInt)pfile_in_zip_read_info->rest_read_uncompressed;
  1120. }
  1121. while (pfile_in_zip_read_info->stream.avail_out > 0) {
  1122. if (pfile_in_zip_read_info->stream.avail_in == 0) {
  1123. uInt bytes_to_read = UNZ_BUFSIZE;
  1124. uInt bytes_not_read = 0;
  1125. uInt bytes_read = 0;
  1126. uInt total_bytes_read = 0;
  1127. if (pfile_in_zip_read_info->stream.next_in != NULL)
  1128. bytes_not_read = (uInt)(pfile_in_zip_read_info->read_buffer + UNZ_BUFSIZE -
  1129. pfile_in_zip_read_info->stream.next_in);
  1130. bytes_to_read -= bytes_not_read;
  1131. if (bytes_not_read > 0)
  1132. memcpy(pfile_in_zip_read_info->read_buffer, pfile_in_zip_read_info->stream.next_in, bytes_not_read);
  1133. if (pfile_in_zip_read_info->rest_read_compressed < bytes_to_read)
  1134. bytes_to_read = (uInt)pfile_in_zip_read_info->rest_read_compressed;
  1135. while (total_bytes_read != bytes_to_read) {
  1136. if (ZSEEK64(pfile_in_zip_read_info->z_filefunc, pfile_in_zip_read_info->filestream,
  1137. pfile_in_zip_read_info->pos_in_zipfile + pfile_in_zip_read_info->byte_before_the_zipfile,
  1138. ZLIB_FILEFUNC_SEEK_SET) != 0)
  1139. return UNZ_ERRNO;
  1140. bytes_read = (int)ZREAD64(pfile_in_zip_read_info->z_filefunc, pfile_in_zip_read_info->filestream,
  1141. pfile_in_zip_read_info->read_buffer + bytes_not_read + total_bytes_read,
  1142. bytes_to_read - total_bytes_read);
  1143. total_bytes_read += bytes_read;
  1144. pfile_in_zip_read_info->pos_in_zipfile += bytes_read;
  1145. if (bytes_read == 0) {
  1146. if (ZERROR64(pfile_in_zip_read_info->z_filefunc, pfile_in_zip_read_info->filestream))
  1147. return UNZ_ERRNO;
  1148. err = unzGoToNextDisk(file);
  1149. if (err != UNZ_OK)
  1150. return err;
  1151. pfile_in_zip_read_info->pos_in_zipfile = 0;
  1152. pfile_in_zip_read_info->filestream = s->filestream;
  1153. }
  1154. }
  1155. #ifndef NOUNCRYPT
  1156. if ((s->cur_file_info.flag & 1) != 0) {
  1157. #ifdef HAVE_AES
  1158. if (s->cur_file_info.compression_method == AES_METHOD) {
  1159. fcrypt_decrypt(pfile_in_zip_read_info->read_buffer, bytes_to_read, &s->pfile_in_zip_read->aes_ctx);
  1160. } else
  1161. #endif
  1162. {
  1163. uInt i;
  1164. for (i = 0; i < total_bytes_read; i++)
  1165. pfile_in_zip_read_info->read_buffer[i] =
  1166. zdecode(s->keys, s->pcrc_32_tab, pfile_in_zip_read_info->read_buffer[i]);
  1167. }
  1168. }
  1169. #endif
  1170. pfile_in_zip_read_info->rest_read_compressed -= total_bytes_read;
  1171. pfile_in_zip_read_info->stream.next_in = (Bytef *)pfile_in_zip_read_info->read_buffer;
  1172. pfile_in_zip_read_info->stream.avail_in = (uInt)bytes_not_read + total_bytes_read;
  1173. }
  1174. if ((pfile_in_zip_read_info->compression_method == 0) || (pfile_in_zip_read_info->raw)) {
  1175. uInt copy, i;
  1176. if ((pfile_in_zip_read_info->stream.avail_in == 0) &&
  1177. (pfile_in_zip_read_info->rest_read_compressed == 0))
  1178. return (read == 0) ? UNZ_EOF : read;
  1179. if (pfile_in_zip_read_info->stream.avail_out < pfile_in_zip_read_info->stream.avail_in)
  1180. copy = pfile_in_zip_read_info->stream.avail_out;
  1181. else
  1182. copy = pfile_in_zip_read_info->stream.avail_in;
  1183. for (i = 0; i < copy; i++)
  1184. *(pfile_in_zip_read_info->stream.next_out + i) =
  1185. *(pfile_in_zip_read_info->stream.next_in + i);
  1186. pfile_in_zip_read_info->total_out_64 = pfile_in_zip_read_info->total_out_64 + copy;
  1187. pfile_in_zip_read_info->rest_read_uncompressed -= copy;
  1188. pfile_in_zip_read_info->crc32 = crc32(pfile_in_zip_read_info->crc32,
  1189. pfile_in_zip_read_info->stream.next_out, copy);
  1190. pfile_in_zip_read_info->stream.avail_in -= copy;
  1191. pfile_in_zip_read_info->stream.avail_out -= copy;
  1192. pfile_in_zip_read_info->stream.next_out += copy;
  1193. pfile_in_zip_read_info->stream.next_in += copy;
  1194. pfile_in_zip_read_info->stream.total_out += copy;
  1195. read += copy;
  1196. } else if (pfile_in_zip_read_info->compression_method == Z_BZIP2ED) {
  1197. #ifdef HAVE_BZIP2
  1198. uLong total_out_before, total_out_after;
  1199. const Bytef *buf_before;
  1200. uLong out_bytes;
  1201. pfile_in_zip_read_info->bstream.next_in = (char *)pfile_in_zip_read_info->stream.next_in;
  1202. pfile_in_zip_read_info->bstream.avail_in = pfile_in_zip_read_info->stream.avail_in;
  1203. pfile_in_zip_read_info->bstream.total_in_lo32 = pfile_in_zip_read_info->stream.total_in;
  1204. pfile_in_zip_read_info->bstream.total_in_hi32 = 0;
  1205. pfile_in_zip_read_info->bstream.next_out = (char *)pfile_in_zip_read_info->stream.next_out;
  1206. pfile_in_zip_read_info->bstream.avail_out = pfile_in_zip_read_info->stream.avail_out;
  1207. pfile_in_zip_read_info->bstream.total_out_lo32 = pfile_in_zip_read_info->stream.total_out;
  1208. pfile_in_zip_read_info->bstream.total_out_hi32 = 0;
  1209. total_out_before = pfile_in_zip_read_info->bstream.total_out_lo32;
  1210. buf_before = (const Bytef *)pfile_in_zip_read_info->bstream.next_out;
  1211. err = BZ2_bzDecompress(&pfile_in_zip_read_info->bstream);
  1212. total_out_after = pfile_in_zip_read_info->bstream.total_out_lo32;
  1213. out_bytes = total_out_after - total_out_before;
  1214. pfile_in_zip_read_info->total_out_64 = pfile_in_zip_read_info->total_out_64 + out_bytes;
  1215. pfile_in_zip_read_info->rest_read_uncompressed -= out_bytes;
  1216. pfile_in_zip_read_info->crc32 = crc32(pfile_in_zip_read_info->crc32, buf_before, (uInt)(out_bytes));
  1217. read += (uInt)(total_out_after - total_out_before);
  1218. pfile_in_zip_read_info->stream.next_in = (Bytef *)pfile_in_zip_read_info->bstream.next_in;
  1219. pfile_in_zip_read_info->stream.avail_in = pfile_in_zip_read_info->bstream.avail_in;
  1220. pfile_in_zip_read_info->stream.total_in = pfile_in_zip_read_info->bstream.total_in_lo32;
  1221. pfile_in_zip_read_info->stream.next_out = (Bytef *)pfile_in_zip_read_info->bstream.next_out;
  1222. pfile_in_zip_read_info->stream.avail_out = pfile_in_zip_read_info->bstream.avail_out;
  1223. pfile_in_zip_read_info->stream.total_out = pfile_in_zip_read_info->bstream.total_out_lo32;
  1224. if (err == BZ_STREAM_END)
  1225. return (read == 0) ? UNZ_EOF : read;
  1226. if (err != BZ_OK)
  1227. break;
  1228. #endif
  1229. } else {
  1230. ZPOS64_T total_out_before, total_out_after;
  1231. const Bytef *buf_before;
  1232. ZPOS64_T out_bytes;
  1233. int flush = Z_SYNC_FLUSH;
  1234. total_out_before = pfile_in_zip_read_info->stream.total_out;
  1235. buf_before = pfile_in_zip_read_info->stream.next_out;
  1236. /*
  1237. if ((pfile_in_zip_read_info->rest_read_uncompressed ==
  1238. pfile_in_zip_read_info->stream.avail_out) &&
  1239. (pfile_in_zip_read_info->rest_read_compressed == 0))
  1240. flush = Z_FINISH;
  1241. */
  1242. err = inflate(&pfile_in_zip_read_info->stream, flush);
  1243. if ((err >= 0) && (pfile_in_zip_read_info->stream.msg != NULL))
  1244. err = Z_DATA_ERROR;
  1245. total_out_after = pfile_in_zip_read_info->stream.total_out;
  1246. out_bytes = total_out_after - total_out_before;
  1247. pfile_in_zip_read_info->total_out_64 += out_bytes;
  1248. pfile_in_zip_read_info->rest_read_uncompressed -= out_bytes;
  1249. pfile_in_zip_read_info->crc32 =
  1250. crc32(pfile_in_zip_read_info->crc32, buf_before, (uInt)(out_bytes));
  1251. read += (uInt)(total_out_after - total_out_before);
  1252. if (err == Z_STREAM_END)
  1253. return (read == 0) ? UNZ_EOF : read;
  1254. if (err != Z_OK)
  1255. break;
  1256. }
  1257. }
  1258. if (err == Z_OK)
  1259. return read;
  1260. return err;
  1261. }
  1262. extern ZPOS64_T ZEXPORT unzGetCurrentFileZStreamPos64(unzFile file)
  1263. {
  1264. unz64_s *s;
  1265. file_in_zip64_read_info_s *pfile_in_zip_read_info;
  1266. s = (unz64_s *)file;
  1267. if (file == NULL)
  1268. return 0; /* UNZ_PARAMERROR */
  1269. pfile_in_zip_read_info = s->pfile_in_zip_read;
  1270. if (pfile_in_zip_read_info == NULL)
  1271. return 0; /* UNZ_PARAMERROR */
  1272. return pfile_in_zip_read_info->pos_in_zipfile + pfile_in_zip_read_info->byte_before_the_zipfile;
  1273. }
  1274. extern int ZEXPORT unzGetLocalExtrafield(unzFile file, voidp buf, unsigned len)
  1275. {
  1276. unz64_s *s;
  1277. file_in_zip64_read_info_s *pfile_in_zip_read_info;
  1278. uInt read_now;
  1279. ZPOS64_T size_to_read;
  1280. if (file == NULL)
  1281. return UNZ_PARAMERROR;
  1282. s = (unz64_s *)file;
  1283. pfile_in_zip_read_info = s->pfile_in_zip_read;
  1284. if (pfile_in_zip_read_info == NULL)
  1285. return UNZ_PARAMERROR;
  1286. size_to_read = pfile_in_zip_read_info->size_local_extrafield - pfile_in_zip_read_info->pos_local_extrafield;
  1287. if (buf == NULL)
  1288. return (int)size_to_read;
  1289. if (len > size_to_read)
  1290. read_now = (uInt)size_to_read;
  1291. else
  1292. read_now = (uInt)len;
  1293. if (read_now == 0)
  1294. return 0;
  1295. if (ZSEEK64(pfile_in_zip_read_info->z_filefunc, pfile_in_zip_read_info->filestream,
  1296. pfile_in_zip_read_info->offset_local_extrafield + pfile_in_zip_read_info->pos_local_extrafield,
  1297. ZLIB_FILEFUNC_SEEK_SET) != 0)
  1298. return UNZ_ERRNO;
  1299. if (ZREAD64(pfile_in_zip_read_info->z_filefunc, pfile_in_zip_read_info->filestream, buf, read_now) != read_now)
  1300. return UNZ_ERRNO;
  1301. return (int)read_now;
  1302. }
  1303. extern int ZEXPORT unzCloseCurrentFile(unzFile file)
  1304. {
  1305. int err = UNZ_OK;
  1306. unz64_s *s;
  1307. file_in_zip64_read_info_s *pfile_in_zip_read_info;
  1308. if (file == NULL)
  1309. return UNZ_PARAMERROR;
  1310. s = (unz64_s *)file;
  1311. pfile_in_zip_read_info = s->pfile_in_zip_read;
  1312. if (pfile_in_zip_read_info == NULL)
  1313. return UNZ_PARAMERROR;
  1314. #ifdef HAVE_AES
  1315. if (s->cur_file_info.compression_method == AES_METHOD) {
  1316. unsigned char authcode[AES_AUTHCODESIZE];
  1317. unsigned char rauthcode[AES_AUTHCODESIZE];
  1318. if (ZREAD64(s->z_filefunc, s->filestream, authcode, AES_AUTHCODESIZE) != AES_AUTHCODESIZE)
  1319. return UNZ_ERRNO;
  1320. if (fcrypt_end(rauthcode, &s->pfile_in_zip_read->aes_ctx) != AES_AUTHCODESIZE)
  1321. err = UNZ_CRCERROR;
  1322. if (memcmp(authcode, rauthcode, AES_AUTHCODESIZE) != 0)
  1323. err = UNZ_CRCERROR;
  1324. }
  1325. /* AES zip version AE-1 will expect a valid crc as well */
  1326. if ((s->cur_file_info.compression_method != AES_METHOD) ||
  1327. (s->cur_file_info_internal.aes_version == 0x0001))
  1328. #endif
  1329. {
  1330. if ((pfile_in_zip_read_info->rest_read_uncompressed == 0) &&
  1331. (!pfile_in_zip_read_info->raw)) {
  1332. if (pfile_in_zip_read_info->crc32 != pfile_in_zip_read_info->crc32_wait)
  1333. err = UNZ_CRCERROR;
  1334. }
  1335. }
  1336. TRYFREE(pfile_in_zip_read_info->read_buffer);
  1337. pfile_in_zip_read_info->read_buffer = NULL;
  1338. if (pfile_in_zip_read_info->stream_initialised == Z_DEFLATED)
  1339. inflateEnd(&pfile_in_zip_read_info->stream);
  1340. #ifdef HAVE_BZIP2
  1341. else if (pfile_in_zip_read_info->stream_initialised == Z_BZIP2ED)
  1342. BZ2_bzDecompressEnd(&pfile_in_zip_read_info->bstream);
  1343. #endif
  1344. pfile_in_zip_read_info->stream_initialised = 0;
  1345. TRYFREE(pfile_in_zip_read_info);
  1346. s->pfile_in_zip_read = NULL;
  1347. return err;
  1348. }
  1349. extern int ZEXPORT unzGoToFirstFile2(unzFile file, unz_file_info64 *pfile_info, char *filename,
  1350. uLong filename_size, void *extrafield, uLong extrafield_size, char *comment, uLong comment_size)
  1351. {
  1352. int err = UNZ_OK;
  1353. unz64_s *s;
  1354. if (file == NULL)
  1355. return UNZ_PARAMERROR;
  1356. s = (unz64_s *)file;
  1357. s->pos_in_central_dir = s->offset_central_dir;
  1358. s->num_file = 0;
  1359. err = unz64local_GetCurrentFileInfoInternal(file, &s->cur_file_info, &s->cur_file_info_internal,
  1360. filename, filename_size, extrafield, extrafield_size, comment, comment_size);
  1361. s->current_file_ok = (err == UNZ_OK);
  1362. if ((err == UNZ_OK) && (pfile_info != NULL))
  1363. memcpy(pfile_info, &s->cur_file_info, sizeof(unz_file_info64));
  1364. return err;
  1365. }
  1366. extern int ZEXPORT unzGoToFirstFile(unzFile file)
  1367. {
  1368. return unzGoToFirstFile2(file, NULL, NULL, 0, NULL, 0, NULL, 0);
  1369. }
  1370. extern int ZEXPORT unzGoToNextFile2(unzFile file, unz_file_info64 *pfile_info, char *filename,
  1371. uLong filename_size, void *extrafield, uLong extrafield_size, char *comment, uLong comment_size)
  1372. {
  1373. unz64_s *s;
  1374. int err;
  1375. if (file == NULL)
  1376. return UNZ_PARAMERROR;
  1377. s = (unz64_s *)file;
  1378. if (!s->current_file_ok)
  1379. return UNZ_END_OF_LIST_OF_FILE;
  1380. if (s->gi.number_entry != 0xffff) /* 2^16 files overflow hack */
  1381. if (s->num_file + 1 == s->gi.number_entry)
  1382. return UNZ_END_OF_LIST_OF_FILE;
  1383. s->pos_in_central_dir += SIZECENTRALDIRITEM + s->cur_file_info.size_filename +
  1384. s->cur_file_info.size_file_extra + s->cur_file_info.size_file_comment;
  1385. s->num_file++;
  1386. err = unz64local_GetCurrentFileInfoInternal(file, &s->cur_file_info, &s->cur_file_info_internal,
  1387. filename, filename_size, extrafield, extrafield_size, comment, comment_size);
  1388. s->current_file_ok = (err == UNZ_OK);
  1389. if ((err == UNZ_OK) && (pfile_info != NULL))
  1390. memcpy(pfile_info, &s->cur_file_info, sizeof(unz_file_info64));
  1391. return err;
  1392. }
  1393. extern int ZEXPORT unzGoToNextFile(unzFile file)
  1394. {
  1395. return unzGoToNextFile2(file, NULL, NULL, 0, NULL, 0, NULL, 0);
  1396. }
  1397. extern int ZEXPORT unzLocateFile(unzFile file, const char *filename, unzFileNameComparer filename_compare_func)
  1398. {
  1399. unz64_s *s;
  1400. int err;
  1401. unz_file_info64 cur_file_info_saved;
  1402. unz_file_info64_internal cur_file_info_internal_saved;
  1403. ZPOS64_T num_file_saved;
  1404. ZPOS64_T pos_in_central_dir_saved;
  1405. char current_filename[UNZ_MAXFILENAMEINZIP + 1];
  1406. if (file == NULL)
  1407. return UNZ_PARAMERROR;
  1408. if (strlen(filename) >= UNZ_MAXFILENAMEINZIP)
  1409. return UNZ_PARAMERROR;
  1410. s = (unz64_s *)file;
  1411. if (!s->current_file_ok)
  1412. return UNZ_END_OF_LIST_OF_FILE;
  1413. /* Save the current state */
  1414. num_file_saved = s->num_file;
  1415. pos_in_central_dir_saved = s->pos_in_central_dir;
  1416. cur_file_info_saved = s->cur_file_info;
  1417. cur_file_info_internal_saved = s->cur_file_info_internal;
  1418. err = unzGoToFirstFile2(file, NULL, current_filename, sizeof(current_filename) - 1, NULL, 0, NULL, 0);
  1419. while (err == UNZ_OK) {
  1420. if (filename_compare_func != NULL)
  1421. err = filename_compare_func(file, current_filename, filename);
  1422. else
  1423. err = strcmp(current_filename, filename);
  1424. if (err == 0)
  1425. return UNZ_OK;
  1426. err = unzGoToNextFile2(file, NULL, current_filename, sizeof(current_filename) - 1, NULL, 0, NULL, 0);
  1427. }
  1428. /* We failed, so restore the state of the 'current file' to where we were. */
  1429. s->num_file = num_file_saved;
  1430. s->pos_in_central_dir = pos_in_central_dir_saved;
  1431. s->cur_file_info = cur_file_info_saved;
  1432. s->cur_file_info_internal = cur_file_info_internal_saved;
  1433. return err;
  1434. }
  1435. extern int ZEXPORT unzGetFilePos(unzFile file, unz_file_pos *file_pos)
  1436. {
  1437. unz64_file_pos file_pos64;
  1438. int err = unzGetFilePos64(file, &file_pos64);
  1439. if (err == UNZ_OK) {
  1440. file_pos->pos_in_zip_directory = (uLong)file_pos64.pos_in_zip_directory;
  1441. file_pos->num_of_file = (uLong)file_pos64.num_of_file;
  1442. }
  1443. return err;
  1444. }
  1445. extern int ZEXPORT unzGoToFilePos(unzFile file, unz_file_pos *file_pos)
  1446. {
  1447. unz64_file_pos file_pos64;
  1448. if (file_pos == NULL)
  1449. return UNZ_PARAMERROR;
  1450. file_pos64.pos_in_zip_directory = file_pos->pos_in_zip_directory;
  1451. file_pos64.num_of_file = file_pos->num_of_file;
  1452. return unzGoToFilePos64(file, &file_pos64);
  1453. }
  1454. extern int ZEXPORT unzGetFilePos64(unzFile file, unz64_file_pos *file_pos)
  1455. {
  1456. unz64_s *s;
  1457. if (file == NULL || file_pos == NULL)
  1458. return UNZ_PARAMERROR;
  1459. s = (unz64_s *)file;
  1460. if (!s->current_file_ok)
  1461. return UNZ_END_OF_LIST_OF_FILE;
  1462. file_pos->pos_in_zip_directory = s->pos_in_central_dir;
  1463. file_pos->num_of_file = s->num_file;
  1464. return UNZ_OK;
  1465. }
  1466. extern int ZEXPORT unzGoToFilePos64(unzFile file, const unz64_file_pos *file_pos)
  1467. {
  1468. unz64_s *s;
  1469. int err;
  1470. if (file == NULL || file_pos == NULL)
  1471. return UNZ_PARAMERROR;
  1472. s = (unz64_s *)file;
  1473. /* jump to the right spot */
  1474. s->pos_in_central_dir = file_pos->pos_in_zip_directory;
  1475. s->num_file = file_pos->num_of_file;
  1476. /* set the current file */
  1477. err = unz64local_GetCurrentFileInfoInternal(file, &s->cur_file_info, &s->cur_file_info_internal, NULL, 0, NULL, 0, NULL, 0);
  1478. /* return results */
  1479. s->current_file_ok = (err == UNZ_OK);
  1480. return err;
  1481. }
  1482. extern uLong ZEXPORT unzGetOffset(unzFile file)
  1483. {
  1484. ZPOS64_T offset64;
  1485. if (file == NULL)
  1486. return 0; /* UNZ_PARAMERROR; */
  1487. offset64 = unzGetOffset64(file);
  1488. return (uLong)offset64;
  1489. }
  1490. extern ZPOS64_T ZEXPORT unzGetOffset64(unzFile file)
  1491. {
  1492. unz64_s *s;
  1493. if (file == NULL)
  1494. return 0; /* UNZ_PARAMERROR; */
  1495. s = (unz64_s *)file;
  1496. if (!s->current_file_ok)
  1497. return 0;
  1498. if (s->gi.number_entry != 0 && s->gi.number_entry != 0xffff)
  1499. if (s->num_file == s->gi.number_entry)
  1500. return 0;
  1501. return s->pos_in_central_dir;
  1502. }
  1503. extern int ZEXPORT unzSetOffset(unzFile file, uLong pos)
  1504. {
  1505. return unzSetOffset64(file, pos);
  1506. }
  1507. extern int ZEXPORT unzSetOffset64(unzFile file, ZPOS64_T pos)
  1508. {
  1509. unz64_s *s;
  1510. int err;
  1511. if (file == NULL)
  1512. return UNZ_PARAMERROR;
  1513. s = (unz64_s *)file;
  1514. s->pos_in_central_dir = pos;
  1515. s->num_file = s->gi.number_entry; /* hack */
  1516. err = unz64local_GetCurrentFileInfoInternal(file, &s->cur_file_info, &s->cur_file_info_internal, NULL, 0, NULL, 0, NULL, 0);
  1517. s->current_file_ok = (err == UNZ_OK);
  1518. return err;
  1519. }
  1520. extern z_off_t ZEXPORT unztell(unzFile file)
  1521. {
  1522. unz64_s *s;
  1523. file_in_zip64_read_info_s *pfile_in_zip_read_info;
  1524. if (file == NULL)
  1525. return UNZ_PARAMERROR;
  1526. s = (unz64_s *)file;
  1527. pfile_in_zip_read_info = s->pfile_in_zip_read;
  1528. if (pfile_in_zip_read_info == NULL)
  1529. return UNZ_PARAMERROR;
  1530. return (z_off_t)pfile_in_zip_read_info->stream.total_out;
  1531. }
  1532. extern ZPOS64_T ZEXPORT unztell64(unzFile file)
  1533. {
  1534. unz64_s *s;
  1535. file_in_zip64_read_info_s *pfile_in_zip_read_info;
  1536. if (file == NULL)
  1537. return (ZPOS64_T)-1;
  1538. s = (unz64_s *)file;
  1539. pfile_in_zip_read_info = s->pfile_in_zip_read;
  1540. if (pfile_in_zip_read_info == NULL)
  1541. return (ZPOS64_T)-1;
  1542. return pfile_in_zip_read_info->total_out_64;
  1543. }
  1544. extern int ZEXPORT unzeof(unzFile file)
  1545. {
  1546. unz64_s *s;
  1547. file_in_zip64_read_info_s *pfile_in_zip_read_info;
  1548. if (file == NULL)
  1549. return UNZ_PARAMERROR;
  1550. s = (unz64_s *)file;
  1551. pfile_in_zip_read_info = s->pfile_in_zip_read;
  1552. if (pfile_in_zip_read_info == NULL)
  1553. return UNZ_PARAMERROR;
  1554. if (pfile_in_zip_read_info->rest_read_uncompressed == 0)
  1555. return 1;
  1556. return 0;
  1557. }