unzip.cpp 71 KB

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