2
0

unzip.c 71 KB

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