unzip.c 70 KB

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