unzip.cpp 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171
  1. /* unzip.c -- IO for uncompress .zip files using zlib
  2. Version 1.1, February 14h, 2010
  3. part of the MiniZip project - ( http://www.winimage.com/zLibDll/minizip.html )
  4. Copyright (C) 1998-2010 Gilles Vollant (minizip) ( http://www.winimage.com/zLibDll/minizip.html )
  5. Modifications of Unzip for Zip64
  6. Copyright (C) 2007-2008 Even Rouault
  7. Modifications for Zip64 support on both zip and unzip
  8. Copyright (C) 2009-2010 Mathias Svensson ( http://result42.com )
  9. For more info read MiniZip_info.txt
  10. ------------------------------------------------------------------------------------
  11. Decryption code comes from crypt.c by Info-ZIP but has been greatly reduced in terms of
  12. compatibility with older software. The following is from the original crypt.c.
  13. Code woven in by Terry Thorsen 1/2003.
  14. Copyright (c) 1990-2000 Info-ZIP. All rights reserved.
  15. See the accompanying file LICENSE, version 2000-Apr-09 or later
  16. (the contents of which are also included in zip.h) for terms of use.
  17. If, for some reason, all these files are missing, the Info-ZIP license
  18. also may be found at: ftp://ftp.info-zip.org/pub/infozip/license.html
  19. crypt.c (full version) by Info-ZIP. Last revised: [see crypt.h]
  20. The encryption/decryption parts of this source code (as opposed to the
  21. non-echoing password parts) were originally written in Europe. The
  22. whole source package can be freely distributed, including from the USA.
  23. (Prior to January 2000, re-export from the US was a violation of US law.)
  24. This encryption code is a direct transcription of the algorithm from
  25. Roger Schlafly, described by Phil Katz in the file appnote.txt. This
  26. file (appnote.txt) is distributed with the PKZIP program (even in the
  27. version without encryption capabilities).
  28. ------------------------------------------------------------------------------------
  29. Changes in unzip.c
  30. 2007-2008 - Even Rouault - Addition of cpl_unzGetCurrentFileZStreamPos
  31. 2007-2008 - Even Rouault - Decoration of symbol names unz* -> cpl_unz*
  32. 2007-2008 - Even Rouault - Remove old C style function prototypes
  33. 2007-2008 - Even Rouault - Add unzip support for ZIP64
  34. Copyright (C) 2007-2008 Even Rouault
  35. Oct-2009 - Mathias Svensson - Removed cpl_* from symbol names (Even Rouault added them but since this is now moved to a new project (minizip64) I renamed them again).
  36. Oct-2009 - Mathias Svensson - Fixed problem if uncompressed size was > 4G and compressed size was <4G
  37. should only read the compressed/uncompressed size from the Zip64 format if
  38. the size from normal header was 0xFFFFFFFF
  39. Oct-2009 - Mathias Svensson - Applied some bug fixes from patches received from Gilles Vollant
  40. Oct-2009 - Mathias Svensson - Applied support to unzip files with compression method BZIP2 (bzip2 lib is required)
  41. Patch created by Daniel Borca
  42. Jan-2010 - back to unzip and minizip 1.0 name scheme, with compatibility layer
  43. Copyright (C) 1998 - 2010 Gilles Vollant, Even Rouault, Mathias Svensson
  44. */
  45. #include <stdio.h>
  46. #include <stdlib.h>
  47. #include <string.h>
  48. #ifndef NOUNCRYPT
  49. #define NOUNCRYPT
  50. #endif
  51. #include "zlib.h"
  52. #include "unzip.h"
  53. #include "ioapi_mem.h"
  54. #ifdef STDC
  55. # include <stddef.h>
  56. # include <string.h>
  57. # include <stdlib.h>
  58. #endif
  59. #ifdef NO_ERRNO_H
  60. extern int errno;
  61. #else
  62. # include <errno.h>
  63. #endif
  64. namespace cocos2d {
  65. #ifndef local
  66. # define local static
  67. #endif
  68. /* compile with -Dlocal if your debugger can't find static symbols */
  69. #ifndef CASESENSITIVITYDEFAULT_NO
  70. # if !defined(unix) && !defined(CASESENSITIVITYDEFAULT_YES)
  71. # define CASESENSITIVITYDEFAULT_NO
  72. # endif
  73. #endif
  74. #ifndef UNZ_BUFSIZE
  75. #define UNZ_BUFSIZE (16384)
  76. #endif
  77. #ifndef UNZ_MAXFILENAMEINZIP
  78. #define UNZ_MAXFILENAMEINZIP (256)
  79. #endif
  80. #ifndef ALLOC
  81. # define ALLOC(size) (malloc(size))
  82. #endif
  83. #ifndef TRYFREE
  84. # define TRYFREE(p) {if (p) free(p);}
  85. #endif
  86. #define SIZECENTRALDIRITEM (0x2e)
  87. #define SIZEZIPLOCALHEADER (0x1e)
  88. const char unz_copyright[] =
  89. " unzip 1.01 Copyright 1998-2004 Gilles Vollant - http://www.winimage.com/zLibDll";
  90. /* unz_file_info_interntal contain internal info about a file in zipfile*/
  91. typedef struct unz_file_info64_internal_s
  92. {
  93. ZPOS64_T offset_curfile;/* relative offset of local header 8 bytes */
  94. } unz_file_info64_internal;
  95. /* file_in_zip_read_info_s contain internal information about a file in zipfile,
  96. when reading and decompress it */
  97. typedef struct
  98. {
  99. char *read_buffer; /* internal buffer for compressed data */
  100. z_stream stream; /* zLib stream structure for inflate */
  101. #ifdef HAVE_BZIP2
  102. bz_stream bstream; /* bzLib stream structure for bziped */
  103. #endif
  104. ZPOS64_T pos_in_zipfile; /* position in byte on the zipfile, for fseek*/
  105. uLong stream_initialised; /* flag set if stream structure is initialized*/
  106. ZPOS64_T offset_local_extrafield;/* offset of the local extra field */
  107. uInt size_local_extrafield;/* size of the local extra field */
  108. ZPOS64_T pos_local_extrafield; /* position in the local extra field in read*/
  109. ZPOS64_T total_out_64;
  110. uLong crc32; /* crc32 of all data uncompressed */
  111. uLong crc32_wait; /* crc32 we must obtain after decompress all */
  112. ZPOS64_T rest_read_compressed; /* number of byte to be decompressed */
  113. ZPOS64_T rest_read_uncompressed;/*number of byte to be obtained after decomp*/
  114. zlib_filefunc64_32_def z_filefunc;
  115. voidpf filestream; /* io structure of the zipfile */
  116. uLong compression_method; /* compression method (0==store) */
  117. ZPOS64_T byte_before_the_zipfile;/* byte before the zipfile, (>0 for sfx)*/
  118. int raw;
  119. } file_in_zip64_read_info_s;
  120. /* unz64_s contain internal information about the zipfile
  121. */
  122. typedef struct
  123. {
  124. zlib_filefunc64_32_def z_filefunc;
  125. int is64bitOpenFunction;
  126. voidpf filestream; /* io structure of the zipfile */
  127. unz_global_info64 gi; /* public global information */
  128. ZPOS64_T byte_before_the_zipfile;/* byte before the zipfile, (>0 for sfx)*/
  129. ZPOS64_T num_file; /* number of the current file in the zipfile*/
  130. ZPOS64_T pos_in_central_dir; /* pos of the current file in the central dir*/
  131. ZPOS64_T current_file_ok; /* flag about the usability of the current file*/
  132. ZPOS64_T central_pos; /* position of the beginning of the central dir*/
  133. ZPOS64_T size_central_dir; /* size of the central directory */
  134. ZPOS64_T offset_central_dir; /* offset of start of central directory with
  135. respect to the starting disk number */
  136. unz_file_info64 cur_file_info; /* public info about the current file in zip*/
  137. unz_file_info64_internal cur_file_info_internal; /* private info about it*/
  138. file_in_zip64_read_info_s* pfile_in_zip_read; /* structure about the current
  139. file if we are decompressing it */
  140. int encrypted;
  141. int isZip64;
  142. # ifndef NOUNCRYPT
  143. unsigned long keys[3]; /* keys defining the pseudo-random sequence */
  144. const unsigned long* pcrc_32_tab;
  145. # endif
  146. } unz64_s;
  147. #ifndef NOUNCRYPT
  148. #include "crypt.h"
  149. #endif
  150. /* ===========================================================================
  151. Read a byte from a gz_stream; update next_in and avail_in. Return EOF
  152. for end of file.
  153. IN assertion: the stream s has been successfully opened for reading.
  154. */
  155. local int unz64local_getByte OF((
  156. const zlib_filefunc64_32_def* pzlib_filefunc_def,
  157. voidpf filestream,
  158. int *pi));
  159. local int unz64local_getByte(const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, int *pi)
  160. {
  161. unsigned char c;
  162. int err = (int)ZREAD64(*pzlib_filefunc_def,filestream,&c,1);
  163. if (err==1)
  164. {
  165. *pi = (int)c;
  166. return UNZ_OK;
  167. }
  168. else
  169. {
  170. if (ZERROR64(*pzlib_filefunc_def,filestream))
  171. return UNZ_ERRNO;
  172. else
  173. return UNZ_EOF;
  174. }
  175. }
  176. /* ===========================================================================
  177. Reads a long in LSB order from the given gz_stream. Sets
  178. */
  179. local int unz64local_getShort OF((
  180. const zlib_filefunc64_32_def* pzlib_filefunc_def,
  181. voidpf filestream,
  182. uLong *pX));
  183. local int unz64local_getShort (const zlib_filefunc64_32_def* pzlib_filefunc_def,
  184. voidpf filestream,
  185. uLong *pX)
  186. {
  187. uLong x ;
  188. int i = 0;
  189. int err;
  190. err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
  191. x = (uLong)i;
  192. if (err==UNZ_OK)
  193. err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
  194. x |= ((uLong)i)<<8;
  195. if (err==UNZ_OK)
  196. *pX = x;
  197. else
  198. *pX = 0;
  199. return err;
  200. }
  201. local int unz64local_getLong OF((
  202. const zlib_filefunc64_32_def* pzlib_filefunc_def,
  203. voidpf filestream,
  204. uLong *pX));
  205. local int unz64local_getLong (const zlib_filefunc64_32_def* pzlib_filefunc_def,
  206. voidpf filestream,
  207. uLong *pX)
  208. {
  209. uLong x ;
  210. int i = 0;
  211. int err;
  212. err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
  213. x = (uLong)i;
  214. if (err==UNZ_OK)
  215. err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
  216. x |= ((uLong)i)<<8;
  217. if (err==UNZ_OK)
  218. err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
  219. x |= ((uLong)i)<<16;
  220. if (err==UNZ_OK)
  221. err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
  222. x += ((uLong)i)<<24;
  223. if (err==UNZ_OK)
  224. *pX = x;
  225. else
  226. *pX = 0;
  227. return err;
  228. }
  229. local int unz64local_getLong64 OF((
  230. const zlib_filefunc64_32_def* pzlib_filefunc_def,
  231. voidpf filestream,
  232. ZPOS64_T *pX));
  233. local int unz64local_getLong64 (const zlib_filefunc64_32_def* pzlib_filefunc_def,
  234. voidpf filestream,
  235. ZPOS64_T *pX)
  236. {
  237. ZPOS64_T x ;
  238. int i = 0;
  239. int err;
  240. err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
  241. x = (ZPOS64_T)i;
  242. if (err==UNZ_OK)
  243. err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
  244. x |= ((ZPOS64_T)i)<<8;
  245. if (err==UNZ_OK)
  246. err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
  247. x |= ((ZPOS64_T)i)<<16;
  248. if (err==UNZ_OK)
  249. err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
  250. x |= ((ZPOS64_T)i)<<24;
  251. if (err==UNZ_OK)
  252. err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
  253. x |= ((ZPOS64_T)i)<<32;
  254. if (err==UNZ_OK)
  255. err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
  256. x |= ((ZPOS64_T)i)<<40;
  257. if (err==UNZ_OK)
  258. err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
  259. x |= ((ZPOS64_T)i)<<48;
  260. if (err==UNZ_OK)
  261. err = unz64local_getByte(pzlib_filefunc_def,filestream,&i);
  262. x |= ((ZPOS64_T)i)<<56;
  263. if (err==UNZ_OK)
  264. *pX = x;
  265. else
  266. *pX = 0;
  267. return err;
  268. }
  269. /* My own strcmpi / strcasecmp */
  270. local int strcmpcasenosensitive_internal (const char* fileName1, const char* fileName2)
  271. {
  272. for (;;)
  273. {
  274. char c1=*(fileName1++);
  275. char c2=*(fileName2++);
  276. if ((c1>='a') && (c1<='z'))
  277. c1 -= 0x20;
  278. if ((c2>='a') && (c2<='z'))
  279. c2 -= 0x20;
  280. if (c1=='\0')
  281. return ((c2=='\0') ? 0 : -1);
  282. if (c2=='\0')
  283. return 1;
  284. if (c1<c2)
  285. return -1;
  286. if (c1>c2)
  287. return 1;
  288. }
  289. }
  290. #ifdef CASESENSITIVITYDEFAULT_NO
  291. #define CASESENSITIVITYDEFAULTVALUE 2
  292. #else
  293. #define CASESENSITIVITYDEFAULTVALUE 1
  294. #endif
  295. #ifndef STRCMPCASENOSENTIVEFUNCTION
  296. #define STRCMPCASENOSENTIVEFUNCTION strcmpcasenosensitive_internal
  297. #endif
  298. /*
  299. Compare two filename (fileName1,fileName2).
  300. If iCaseSenisivity = 1, comparison is case sensitivity (like strcmp)
  301. If iCaseSenisivity = 2, comparison is not case sensitivity (like strcmpi
  302. or strcasecmp)
  303. If iCaseSenisivity = 0, case sensitivity is default of your operating system
  304. (like 1 on Unix, 2 on Windows)
  305. */
  306. int ZEXPORT unzStringFileNameCompare (const char* fileName1,
  307. const char* fileName2,
  308. int iCaseSensitivity)
  309. {
  310. if (iCaseSensitivity==0)
  311. iCaseSensitivity=CASESENSITIVITYDEFAULTVALUE;
  312. if (iCaseSensitivity==1)
  313. return strcmp(fileName1,fileName2);
  314. return STRCMPCASENOSENTIVEFUNCTION(fileName1,fileName2);
  315. }
  316. #ifndef BUFREADCOMMENT
  317. #define BUFREADCOMMENT (0x400)
  318. #endif
  319. /*
  320. Locate the Central directory of a zipfile (at the end, just before
  321. the global comment)
  322. */
  323. local ZPOS64_T unz64local_SearchCentralDir OF((const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream));
  324. local ZPOS64_T unz64local_SearchCentralDir(const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream)
  325. {
  326. unsigned char* buf;
  327. ZPOS64_T uSizeFile;
  328. ZPOS64_T uBackRead;
  329. ZPOS64_T uMaxBack=0xffff; /* maximum size of global comment */
  330. ZPOS64_T uPosFound=0;
  331. if (ZSEEK64(*pzlib_filefunc_def,filestream,0,ZLIB_FILEFUNC_SEEK_END) != 0)
  332. return 0;
  333. uSizeFile = ZTELL64(*pzlib_filefunc_def,filestream);
  334. if (uMaxBack>uSizeFile)
  335. uMaxBack = uSizeFile;
  336. buf = (unsigned char*)ALLOC(BUFREADCOMMENT+4);
  337. if (buf==NULL)
  338. return 0;
  339. uBackRead = 4;
  340. while (uBackRead<uMaxBack)
  341. {
  342. uLong uReadSize;
  343. ZPOS64_T uReadPos ;
  344. int i;
  345. if (uBackRead+BUFREADCOMMENT>uMaxBack)
  346. uBackRead = uMaxBack;
  347. else
  348. uBackRead+=BUFREADCOMMENT;
  349. uReadPos = uSizeFile-uBackRead ;
  350. uReadSize = ((BUFREADCOMMENT+4) < (uSizeFile-uReadPos)) ?
  351. (BUFREADCOMMENT+4) : (uLong)(uSizeFile-uReadPos);
  352. if (ZSEEK64(*pzlib_filefunc_def,filestream,uReadPos,ZLIB_FILEFUNC_SEEK_SET)!=0)
  353. break;
  354. if (ZREAD64(*pzlib_filefunc_def,filestream,buf,uReadSize)!=uReadSize)
  355. break;
  356. for (i=(int)uReadSize-3; (i--)>0;)
  357. if (((*(buf+i))==0x50) && ((*(buf+i+1))==0x4b) &&
  358. ((*(buf+i+2))==0x05) && ((*(buf+i+3))==0x06))
  359. {
  360. uPosFound = uReadPos+i;
  361. break;
  362. }
  363. if (uPosFound!=0)
  364. break;
  365. }
  366. TRYFREE(buf);
  367. return uPosFound;
  368. }
  369. /*
  370. Locate the Central directory 64 of a zipfile (at the end, just before
  371. the global comment)
  372. */
  373. local ZPOS64_T unz64local_SearchCentralDir64 OF((
  374. const zlib_filefunc64_32_def* pzlib_filefunc_def,
  375. voidpf filestream));
  376. local ZPOS64_T unz64local_SearchCentralDir64(const zlib_filefunc64_32_def* pzlib_filefunc_def,
  377. voidpf filestream)
  378. {
  379. unsigned char* buf;
  380. ZPOS64_T uSizeFile;
  381. ZPOS64_T uBackRead;
  382. ZPOS64_T uMaxBack=0xffff; /* maximum size of global comment */
  383. ZPOS64_T uPosFound=0;
  384. uLong uL;
  385. ZPOS64_T relativeOffset;
  386. if (ZSEEK64(*pzlib_filefunc_def,filestream,0,ZLIB_FILEFUNC_SEEK_END) != 0)
  387. return 0;
  388. uSizeFile = ZTELL64(*pzlib_filefunc_def,filestream);
  389. if (uMaxBack>uSizeFile)
  390. uMaxBack = uSizeFile;
  391. buf = (unsigned char*)ALLOC(BUFREADCOMMENT+4);
  392. if (buf==NULL)
  393. return 0;
  394. uBackRead = 4;
  395. while (uBackRead<uMaxBack)
  396. {
  397. uLong uReadSize;
  398. ZPOS64_T uReadPos;
  399. int i;
  400. if (uBackRead+BUFREADCOMMENT>uMaxBack)
  401. uBackRead = uMaxBack;
  402. else
  403. uBackRead+=BUFREADCOMMENT;
  404. uReadPos = uSizeFile-uBackRead ;
  405. uReadSize = ((BUFREADCOMMENT+4) < (uSizeFile-uReadPos)) ?
  406. (BUFREADCOMMENT+4) : (uLong)(uSizeFile-uReadPos);
  407. if (ZSEEK64(*pzlib_filefunc_def,filestream,uReadPos,ZLIB_FILEFUNC_SEEK_SET)!=0)
  408. break;
  409. if (ZREAD64(*pzlib_filefunc_def,filestream,buf,uReadSize)!=uReadSize)
  410. break;
  411. for (i=(int)uReadSize-3; (i--)>0;)
  412. if (((*(buf+i))==0x50) && ((*(buf+i+1))==0x4b) &&
  413. ((*(buf+i+2))==0x06) && ((*(buf+i+3))==0x07))
  414. {
  415. uPosFound = uReadPos+i;
  416. break;
  417. }
  418. if (uPosFound!=0)
  419. break;
  420. }
  421. TRYFREE(buf);
  422. if (uPosFound == 0)
  423. return 0;
  424. /* Zip64 end of central directory locator */
  425. if (ZSEEK64(*pzlib_filefunc_def,filestream, uPosFound,ZLIB_FILEFUNC_SEEK_SET)!=0)
  426. return 0;
  427. /* the signature, already checked */
  428. if (unz64local_getLong(pzlib_filefunc_def,filestream,&uL)!=UNZ_OK)
  429. return 0;
  430. /* number of the disk with the start of the zip64 end of central directory */
  431. if (unz64local_getLong(pzlib_filefunc_def,filestream,&uL)!=UNZ_OK)
  432. return 0;
  433. if (uL != 0)
  434. return 0;
  435. /* relative offset of the zip64 end of central directory record */
  436. if (unz64local_getLong64(pzlib_filefunc_def,filestream,&relativeOffset)!=UNZ_OK)
  437. return 0;
  438. /* total number of disks */
  439. if (unz64local_getLong(pzlib_filefunc_def,filestream,&uL)!=UNZ_OK)
  440. return 0;
  441. if (uL != 1)
  442. return 0;
  443. /* Goto end of central directory record */
  444. if (ZSEEK64(*pzlib_filefunc_def,filestream, relativeOffset,ZLIB_FILEFUNC_SEEK_SET)!=0)
  445. return 0;
  446. /* the signature */
  447. if (unz64local_getLong(pzlib_filefunc_def,filestream,&uL)!=UNZ_OK)
  448. return 0;
  449. if (uL != 0x06064b50)
  450. return 0;
  451. return relativeOffset;
  452. }
  453. /*
  454. Open a Zip file. path contain the full pathname (by example,
  455. on a Windows NT computer "c:\\test\\zlib114.zip" or on an Unix computer
  456. "zlib/zlib114.zip".
  457. If the zipfile cannot be opened (file doesn't exist or in not valid), the
  458. return value is NULL.
  459. Else, the return value is a unzFile Handle, usable with other function
  460. of this unzip package.
  461. */
  462. local unzFile unzOpenInternal (const void *path,
  463. zlib_filefunc64_32_def* pzlib_filefunc64_32_def,
  464. int is64bitOpenFunction)
  465. {
  466. unz64_s us;
  467. unz64_s *s;
  468. ZPOS64_T central_pos;
  469. uLong uL;
  470. uLong number_disk; /* number of the current dist, used for
  471. spanning ZIP, unsupported, always 0*/
  472. uLong number_disk_with_CD; /* number the the disk with central dir, used
  473. for spanning ZIP, unsupported, always 0*/
  474. ZPOS64_T number_entry_CD; /* total number of entries in
  475. the central dir
  476. (same than number_entry on nospan) */
  477. int err=UNZ_OK;
  478. if (unz_copyright[0]!=' ')
  479. return NULL;
  480. us.z_filefunc.zseek32_file = NULL;
  481. us.z_filefunc.ztell32_file = NULL;
  482. if (pzlib_filefunc64_32_def==NULL)
  483. fill_fopen64_filefunc(&us.z_filefunc.zfile_func64);
  484. else
  485. us.z_filefunc = *pzlib_filefunc64_32_def;
  486. us.is64bitOpenFunction = is64bitOpenFunction;
  487. us.filestream = ZOPEN64(us.z_filefunc,
  488. path,
  489. ZLIB_FILEFUNC_MODE_READ |
  490. ZLIB_FILEFUNC_MODE_EXISTING);
  491. if (us.filestream==NULL)
  492. return NULL;
  493. central_pos = unz64local_SearchCentralDir64(&us.z_filefunc,us.filestream);
  494. if (central_pos)
  495. {
  496. uLong uS;
  497. ZPOS64_T uL64;
  498. us.isZip64 = 1;
  499. if (ZSEEK64(us.z_filefunc, us.filestream,
  500. central_pos,ZLIB_FILEFUNC_SEEK_SET)!=0)
  501. err=UNZ_ERRNO;
  502. /* the signature, already checked */
  503. if (unz64local_getLong(&us.z_filefunc, us.filestream,&uL)!=UNZ_OK)
  504. err=UNZ_ERRNO;
  505. /* size of zip64 end of central directory record */
  506. if (unz64local_getLong64(&us.z_filefunc, us.filestream,&uL64)!=UNZ_OK)
  507. err=UNZ_ERRNO;
  508. /* version made by */
  509. if (unz64local_getShort(&us.z_filefunc, us.filestream,&uS)!=UNZ_OK)
  510. err=UNZ_ERRNO;
  511. /* version needed to extract */
  512. if (unz64local_getShort(&us.z_filefunc, us.filestream,&uS)!=UNZ_OK)
  513. err=UNZ_ERRNO;
  514. /* number of this disk */
  515. if (unz64local_getLong(&us.z_filefunc, us.filestream,&number_disk)!=UNZ_OK)
  516. err=UNZ_ERRNO;
  517. /* number of the disk with the start of the central directory */
  518. if (unz64local_getLong(&us.z_filefunc, us.filestream,&number_disk_with_CD)!=UNZ_OK)
  519. err=UNZ_ERRNO;
  520. /* total number of entries in the central directory on this disk */
  521. if (unz64local_getLong64(&us.z_filefunc, us.filestream,&us.gi.number_entry)!=UNZ_OK)
  522. err=UNZ_ERRNO;
  523. /* total number of entries in the central directory */
  524. if (unz64local_getLong64(&us.z_filefunc, us.filestream,&number_entry_CD)!=UNZ_OK)
  525. err=UNZ_ERRNO;
  526. if ((number_entry_CD!=us.gi.number_entry) ||
  527. (number_disk_with_CD!=0) ||
  528. (number_disk!=0))
  529. err=UNZ_BADZIPFILE;
  530. /* size of the central directory */
  531. if (unz64local_getLong64(&us.z_filefunc, us.filestream,&us.size_central_dir)!=UNZ_OK)
  532. err=UNZ_ERRNO;
  533. /* offset of start of central directory with respect to the
  534. starting disk number */
  535. if (unz64local_getLong64(&us.z_filefunc, us.filestream,&us.offset_central_dir)!=UNZ_OK)
  536. err=UNZ_ERRNO;
  537. us.gi.size_comment = 0;
  538. }
  539. else
  540. {
  541. central_pos = unz64local_SearchCentralDir(&us.z_filefunc,us.filestream);
  542. if (central_pos==0)
  543. err=UNZ_ERRNO;
  544. us.isZip64 = 0;
  545. if (ZSEEK64(us.z_filefunc, us.filestream,
  546. central_pos,ZLIB_FILEFUNC_SEEK_SET)!=0)
  547. err=UNZ_ERRNO;
  548. /* the signature, already checked */
  549. if (unz64local_getLong(&us.z_filefunc, us.filestream,&uL)!=UNZ_OK)
  550. err=UNZ_ERRNO;
  551. /* number of this disk */
  552. if (unz64local_getShort(&us.z_filefunc, us.filestream,&number_disk)!=UNZ_OK)
  553. err=UNZ_ERRNO;
  554. /* number of the disk with the start of the central directory */
  555. if (unz64local_getShort(&us.z_filefunc, us.filestream,&number_disk_with_CD)!=UNZ_OK)
  556. err=UNZ_ERRNO;
  557. /* total number of entries in the central dir on this disk */
  558. if (unz64local_getShort(&us.z_filefunc, us.filestream,&uL)!=UNZ_OK)
  559. err=UNZ_ERRNO;
  560. us.gi.number_entry = uL;
  561. /* total number of entries in the central dir */
  562. if (unz64local_getShort(&us.z_filefunc, us.filestream,&uL)!=UNZ_OK)
  563. err=UNZ_ERRNO;
  564. number_entry_CD = uL;
  565. if ((number_entry_CD!=us.gi.number_entry) ||
  566. (number_disk_with_CD!=0) ||
  567. (number_disk!=0))
  568. err=UNZ_BADZIPFILE;
  569. /* size of the central directory */
  570. if (unz64local_getLong(&us.z_filefunc, us.filestream,&uL)!=UNZ_OK)
  571. err=UNZ_ERRNO;
  572. us.size_central_dir = uL;
  573. /* offset of start of central directory with respect to the
  574. starting disk number */
  575. if (unz64local_getLong(&us.z_filefunc, us.filestream,&uL)!=UNZ_OK)
  576. err=UNZ_ERRNO;
  577. us.offset_central_dir = uL;
  578. /* zipfile comment length */
  579. if (unz64local_getShort(&us.z_filefunc, us.filestream,&us.gi.size_comment)!=UNZ_OK)
  580. err=UNZ_ERRNO;
  581. }
  582. if ((central_pos<us.offset_central_dir+us.size_central_dir) &&
  583. (err==UNZ_OK))
  584. err=UNZ_BADZIPFILE;
  585. if (err!=UNZ_OK)
  586. {
  587. ZCLOSE64(us.z_filefunc, us.filestream);
  588. return NULL;
  589. }
  590. us.byte_before_the_zipfile = central_pos -
  591. (us.offset_central_dir+us.size_central_dir);
  592. us.central_pos = central_pos;
  593. us.pfile_in_zip_read = NULL;
  594. us.encrypted = 0;
  595. s=(unz64_s*)ALLOC(sizeof(unz64_s));
  596. if( s != NULL)
  597. {
  598. *s=us;
  599. unzGoToFirstFile((unzFile)s);
  600. }
  601. return (unzFile)s;
  602. }
  603. unzFile ZEXPORT unzOpen2 (const char *path,
  604. zlib_filefunc_def* pzlib_filefunc32_def)
  605. {
  606. if (pzlib_filefunc32_def != NULL)
  607. {
  608. zlib_filefunc64_32_def zlib_filefunc64_32_def_fill;
  609. fill_zlib_filefunc64_32_def_from_filefunc32(&zlib_filefunc64_32_def_fill,pzlib_filefunc32_def);
  610. return unzOpenInternal(path, &zlib_filefunc64_32_def_fill, 0);
  611. }
  612. else
  613. return unzOpenInternal(path, NULL, 0);
  614. }
  615. unzFile ZEXPORT unzOpen2_64 (const void *path,
  616. zlib_filefunc64_def* pzlib_filefunc_def)
  617. {
  618. if (pzlib_filefunc_def != NULL)
  619. {
  620. zlib_filefunc64_32_def zlib_filefunc64_32_def_fill;
  621. zlib_filefunc64_32_def_fill.zfile_func64 = *pzlib_filefunc_def;
  622. zlib_filefunc64_32_def_fill.ztell32_file = NULL;
  623. zlib_filefunc64_32_def_fill.zseek32_file = NULL;
  624. return unzOpenInternal(path, &zlib_filefunc64_32_def_fill, 1);
  625. }
  626. else
  627. return unzOpenInternal(path, NULL, 1);
  628. }
  629. unzFile ZEXPORT unzOpen (const char *path)
  630. {
  631. return unzOpenInternal(path, NULL, 0);
  632. }
  633. unzFile ZEXPORT unzOpen64 (const void *path)
  634. {
  635. return unzOpenInternal(path, NULL, 1);
  636. }
  637. unzFile ZEXPORT unzOpenBuffer (const void* buffer, uLong size)
  638. {
  639. char path[48] = {0};
  640. zlib_filefunc64_32_def memory_file;
  641. sprintf(path, "%llx %lx", (unsigned long long)buffer, (unsigned long)size);
  642. fill_memory_filefunc64_32(&memory_file);
  643. return unzOpenInternal(path, &memory_file, 0);
  644. }
  645. /*
  646. Close a ZipFile opened with unzipOpen.
  647. If there is files inside the .Zip opened with unzipOpenCurrentFile (see later),
  648. these files MUST be closed with unzipCloseCurrentFile before call unzipClose.
  649. return UNZ_OK if there is no problem. */
  650. int ZEXPORT unzClose (unzFile file)
  651. {
  652. unz64_s* s;
  653. if (file==NULL)
  654. return UNZ_PARAMERROR;
  655. s=(unz64_s*)file;
  656. if (s->pfile_in_zip_read!=NULL)
  657. unzCloseCurrentFile(file);
  658. ZCLOSE64(s->z_filefunc, s->filestream);
  659. TRYFREE(s);
  660. return UNZ_OK;
  661. }
  662. /*
  663. Write info about the ZipFile in the *pglobal_info structure.
  664. No preparation of the structure is needed
  665. return UNZ_OK if there is no problem. */
  666. int ZEXPORT unzGetGlobalInfo64 (unzFile file, unz_global_info64* pglobal_info)
  667. {
  668. unz64_s* s;
  669. if (file==NULL)
  670. return UNZ_PARAMERROR;
  671. s=(unz64_s*)file;
  672. *pglobal_info=s->gi;
  673. return UNZ_OK;
  674. }
  675. int ZEXPORT unzGetGlobalInfo (unzFile file, unz_global_info* pglobal_info32)
  676. {
  677. unz64_s* s;
  678. if (file==NULL)
  679. return UNZ_PARAMERROR;
  680. s=(unz64_s*)file;
  681. /* to do : check if number_entry is not truncated */
  682. pglobal_info32->number_entry = (uLong)s->gi.number_entry;
  683. pglobal_info32->size_comment = s->gi.size_comment;
  684. return UNZ_OK;
  685. }
  686. /*
  687. Translate date/time from Dos format to tm_unz (readable more easily)
  688. */
  689. local void unz64local_DosDateToTmuDate (ZPOS64_T ulDosDate, tm_unz* ptm)
  690. {
  691. ZPOS64_T uDate;
  692. uDate = (ZPOS64_T)(ulDosDate>>16);
  693. ptm->tm_mday = (uInt)(uDate&0x1f) ;
  694. ptm->tm_mon = (uInt)((((uDate)&0x1E0)/0x20)-1) ;
  695. ptm->tm_year = (uInt)(((uDate&0x0FE00)/0x0200)+1980) ;
  696. ptm->tm_hour = (uInt) ((ulDosDate &0xF800)/0x800);
  697. ptm->tm_min = (uInt) ((ulDosDate&0x7E0)/0x20) ;
  698. ptm->tm_sec = (uInt) (2*(ulDosDate&0x1f)) ;
  699. }
  700. /*
  701. Get Info about the current file in the zipfile, with internal only info
  702. */
  703. local int unz64local_GetCurrentFileInfoInternal OF((unzFile file,
  704. unz_file_info64 *pfile_info,
  705. unz_file_info64_internal
  706. *pfile_info_internal,
  707. char *szFileName,
  708. uLong fileNameBufferSize,
  709. void *extraField,
  710. uLong extraFieldBufferSize,
  711. char *szComment,
  712. uLong commentBufferSize));
  713. local int unz64local_GetCurrentFileInfoInternal (unzFile file,
  714. unz_file_info64 *pfile_info,
  715. unz_file_info64_internal
  716. *pfile_info_internal,
  717. char *szFileName,
  718. uLong fileNameBufferSize,
  719. void *extraField,
  720. uLong extraFieldBufferSize,
  721. char *szComment,
  722. uLong commentBufferSize)
  723. {
  724. unz64_s* s;
  725. unz_file_info64 file_info;
  726. unz_file_info64_internal file_info_internal;
  727. int err=UNZ_OK;
  728. uLong uMagic;
  729. long lSeek=0;
  730. uLong uL;
  731. if (file==NULL)
  732. return UNZ_PARAMERROR;
  733. s=(unz64_s*)file;
  734. if (ZSEEK64(s->z_filefunc, s->filestream,
  735. s->pos_in_central_dir+s->byte_before_the_zipfile,
  736. ZLIB_FILEFUNC_SEEK_SET)!=0)
  737. err=UNZ_ERRNO;
  738. /* we check the magic */
  739. if (err==UNZ_OK)
  740. {
  741. if (unz64local_getLong(&s->z_filefunc, s->filestream,&uMagic) != UNZ_OK)
  742. err=UNZ_ERRNO;
  743. else if (uMagic!=0x02014b50)
  744. err=UNZ_BADZIPFILE;
  745. }
  746. if (unz64local_getShort(&s->z_filefunc, s->filestream,&file_info.version) != UNZ_OK)
  747. err=UNZ_ERRNO;
  748. if (unz64local_getShort(&s->z_filefunc, s->filestream,&file_info.version_needed) != UNZ_OK)
  749. err=UNZ_ERRNO;
  750. if (unz64local_getShort(&s->z_filefunc, s->filestream,&file_info.flag) != UNZ_OK)
  751. err=UNZ_ERRNO;
  752. if (unz64local_getShort(&s->z_filefunc, s->filestream,&file_info.compression_method) != UNZ_OK)
  753. err=UNZ_ERRNO;
  754. if (unz64local_getLong(&s->z_filefunc, s->filestream,&file_info.dosDate) != UNZ_OK)
  755. err=UNZ_ERRNO;
  756. unz64local_DosDateToTmuDate(file_info.dosDate,&file_info.tmu_date);
  757. if (unz64local_getLong(&s->z_filefunc, s->filestream,&file_info.crc) != UNZ_OK)
  758. err=UNZ_ERRNO;
  759. if (unz64local_getLong(&s->z_filefunc, s->filestream,&uL) != UNZ_OK)
  760. err=UNZ_ERRNO;
  761. file_info.compressed_size = uL;
  762. if (unz64local_getLong(&s->z_filefunc, s->filestream,&uL) != UNZ_OK)
  763. err=UNZ_ERRNO;
  764. file_info.uncompressed_size = uL;
  765. if (unz64local_getShort(&s->z_filefunc, s->filestream,&file_info.size_filename) != UNZ_OK)
  766. err=UNZ_ERRNO;
  767. if (unz64local_getShort(&s->z_filefunc, s->filestream,&file_info.size_file_extra) != UNZ_OK)
  768. err=UNZ_ERRNO;
  769. if (unz64local_getShort(&s->z_filefunc, s->filestream,&file_info.size_file_comment) != UNZ_OK)
  770. err=UNZ_ERRNO;
  771. if (unz64local_getShort(&s->z_filefunc, s->filestream,&file_info.disk_num_start) != UNZ_OK)
  772. err=UNZ_ERRNO;
  773. if (unz64local_getShort(&s->z_filefunc, s->filestream,&file_info.internal_fa) != UNZ_OK)
  774. err=UNZ_ERRNO;
  775. if (unz64local_getLong(&s->z_filefunc, s->filestream,&file_info.external_fa) != UNZ_OK)
  776. err=UNZ_ERRNO;
  777. // relative offset of local header
  778. if (unz64local_getLong(&s->z_filefunc, s->filestream,&uL) != UNZ_OK)
  779. err=UNZ_ERRNO;
  780. file_info_internal.offset_curfile = uL;
  781. lSeek+=file_info.size_filename;
  782. if ((err==UNZ_OK) && (szFileName!=NULL))
  783. {
  784. uLong uSizeRead ;
  785. if (file_info.size_filename<fileNameBufferSize)
  786. {
  787. *(szFileName+file_info.size_filename)='\0';
  788. uSizeRead = file_info.size_filename;
  789. }
  790. else
  791. uSizeRead = fileNameBufferSize;
  792. if ((file_info.size_filename>0) && (fileNameBufferSize>0))
  793. if (ZREAD64(s->z_filefunc, s->filestream,szFileName,uSizeRead)!=uSizeRead)
  794. err=UNZ_ERRNO;
  795. lSeek -= uSizeRead;
  796. }
  797. // Read extrafield
  798. if ((err==UNZ_OK) && (extraField!=NULL))
  799. {
  800. ZPOS64_T uSizeRead ;
  801. if (file_info.size_file_extra<extraFieldBufferSize)
  802. uSizeRead = file_info.size_file_extra;
  803. else
  804. uSizeRead = extraFieldBufferSize;
  805. if (lSeek!=0)
  806. {
  807. if (ZSEEK64(s->z_filefunc, s->filestream,lSeek,ZLIB_FILEFUNC_SEEK_CUR)==0)
  808. lSeek=0;
  809. else
  810. err=UNZ_ERRNO;
  811. }
  812. if ((file_info.size_file_extra>0) && (extraFieldBufferSize>0))
  813. if (ZREAD64(s->z_filefunc, s->filestream,extraField,(uLong)uSizeRead)!=uSizeRead)
  814. err=UNZ_ERRNO;
  815. lSeek += file_info.size_file_extra - (uLong)uSizeRead;
  816. }
  817. else
  818. lSeek += file_info.size_file_extra;
  819. if ((err==UNZ_OK) && (file_info.size_file_extra != 0))
  820. {
  821. uLong acc = 0;
  822. // since lSeek now points to after the extra field we need to move back
  823. lSeek -= file_info.size_file_extra;
  824. if (lSeek!=0)
  825. {
  826. if (ZSEEK64(s->z_filefunc, s->filestream,lSeek,ZLIB_FILEFUNC_SEEK_CUR)==0)
  827. lSeek=0;
  828. else
  829. err=UNZ_ERRNO;
  830. }
  831. while(acc < file_info.size_file_extra)
  832. {
  833. uLong headerId;
  834. uLong dataSize;
  835. if (unz64local_getShort(&s->z_filefunc, s->filestream,&headerId) != UNZ_OK)
  836. err=UNZ_ERRNO;
  837. if (unz64local_getShort(&s->z_filefunc, s->filestream,&dataSize) != UNZ_OK)
  838. err=UNZ_ERRNO;
  839. /* ZIP64 extra fields */
  840. if (headerId == 0x0001)
  841. {
  842. uLong uL2;
  843. if (file_info.uncompressed_size == (ZPOS64_T)(unsigned long)-1)
  844. {
  845. if (unz64local_getLong64(&s->z_filefunc, s->filestream,&file_info.uncompressed_size) != UNZ_OK)
  846. err=UNZ_ERRNO;
  847. }
  848. if (file_info.compressed_size == (ZPOS64_T)(unsigned long)-1)
  849. {
  850. if (unz64local_getLong64(&s->z_filefunc, s->filestream,&file_info.compressed_size) != UNZ_OK)
  851. err=UNZ_ERRNO;
  852. }
  853. if (file_info_internal.offset_curfile == (ZPOS64_T)(unsigned long)-1)
  854. {
  855. /* Relative Header offset */
  856. if (unz64local_getLong64(&s->z_filefunc, s->filestream,&file_info_internal.offset_curfile) != UNZ_OK)
  857. err=UNZ_ERRNO;
  858. }
  859. if(file_info.disk_num_start == (unsigned long)-1)
  860. {
  861. /* Disk Start Number */
  862. if (unz64local_getLong(&s->z_filefunc, s->filestream, &uL2) != UNZ_OK)
  863. err=UNZ_ERRNO;
  864. }
  865. }
  866. else
  867. {
  868. if (ZSEEK64(s->z_filefunc, s->filestream,dataSize,ZLIB_FILEFUNC_SEEK_CUR)!=0)
  869. err=UNZ_ERRNO;
  870. }
  871. acc += 2 + 2 + dataSize;
  872. }
  873. }
  874. if ((err==UNZ_OK) && (szComment!=NULL))
  875. {
  876. uLong uSizeRead ;
  877. if (file_info.size_file_comment<commentBufferSize)
  878. {
  879. *(szComment+file_info.size_file_comment)='\0';
  880. uSizeRead = file_info.size_file_comment;
  881. }
  882. else
  883. uSizeRead = commentBufferSize;
  884. if (lSeek!=0)
  885. {
  886. if (ZSEEK64(s->z_filefunc, s->filestream,lSeek,ZLIB_FILEFUNC_SEEK_CUR)==0) {
  887. //lSeek=0;
  888. }
  889. else
  890. err=UNZ_ERRNO;
  891. }
  892. if ((file_info.size_file_comment>0) && (commentBufferSize>0))
  893. if (ZREAD64(s->z_filefunc, s->filestream,szComment,uSizeRead)!=uSizeRead)
  894. err=UNZ_ERRNO;
  895. //lSeek+=file_info.size_file_comment - uSizeRead;
  896. }
  897. //else
  898. // lSeek+=file_info.size_file_comment;
  899. if ((err==UNZ_OK) && (pfile_info!=NULL))
  900. *pfile_info=file_info;
  901. if ((err==UNZ_OK) && (pfile_info_internal!=NULL))
  902. *pfile_info_internal=file_info_internal;
  903. return err;
  904. }
  905. /*
  906. Write info about the ZipFile in the *pglobal_info structure.
  907. No preparation of the structure is needed
  908. return UNZ_OK if there is no problem.
  909. */
  910. int ZEXPORT unzGetCurrentFileInfo64 (unzFile file,
  911. unz_file_info64 * pfile_info,
  912. char * szFileName, uLong fileNameBufferSize,
  913. void *extraField, uLong extraFieldBufferSize,
  914. char* szComment, uLong commentBufferSize)
  915. {
  916. return unz64local_GetCurrentFileInfoInternal(file,pfile_info,NULL,
  917. szFileName,fileNameBufferSize,
  918. extraField,extraFieldBufferSize,
  919. szComment,commentBufferSize);
  920. }
  921. int ZEXPORT unzGetCurrentFileInfo (unzFile file,
  922. unz_file_info * pfile_info,
  923. char * szFileName, uLong fileNameBufferSize,
  924. void *extraField, uLong extraFieldBufferSize,
  925. char* szComment, uLong commentBufferSize)
  926. {
  927. int err;
  928. unz_file_info64 file_info64;
  929. err = unz64local_GetCurrentFileInfoInternal(file,&file_info64,NULL,
  930. szFileName,fileNameBufferSize,
  931. extraField,extraFieldBufferSize,
  932. szComment,commentBufferSize);
  933. if (err==UNZ_OK)
  934. {
  935. pfile_info->version = file_info64.version;
  936. pfile_info->version_needed = file_info64.version_needed;
  937. pfile_info->flag = file_info64.flag;
  938. pfile_info->compression_method = file_info64.compression_method;
  939. pfile_info->dosDate = file_info64.dosDate;
  940. pfile_info->crc = file_info64.crc;
  941. pfile_info->size_filename = file_info64.size_filename;
  942. pfile_info->size_file_extra = file_info64.size_file_extra;
  943. pfile_info->size_file_comment = file_info64.size_file_comment;
  944. pfile_info->disk_num_start = file_info64.disk_num_start;
  945. pfile_info->internal_fa = file_info64.internal_fa;
  946. pfile_info->external_fa = file_info64.external_fa;
  947. pfile_info->tmu_date = file_info64.tmu_date,
  948. pfile_info->compressed_size = (uLong)file_info64.compressed_size;
  949. pfile_info->uncompressed_size = (uLong)file_info64.uncompressed_size;
  950. }
  951. return err;
  952. }
  953. /*
  954. Set the current file of the zipfile to the first file
  955. with retrieving an information about the file.
  956. return UNZ_OK if there is no problem
  957. */
  958. int ZEXPORT unzGoToFirstFile64 (unzFile file,
  959. unz_file_info64 *pfile_info,
  960. char *szFileName,
  961. uLong fileNameBufferSize)
  962. {
  963. int err=UNZ_OK;
  964. unz64_s* s;
  965. if (file==NULL)
  966. return UNZ_PARAMERROR;
  967. s=(unz64_s*)file;
  968. s->pos_in_central_dir=s->offset_central_dir;
  969. s->num_file=0;
  970. err=unz64local_GetCurrentFileInfoInternal(file,&s->cur_file_info,
  971. &s->cur_file_info_internal,
  972. szFileName,fileNameBufferSize,NULL,0,NULL,0);
  973. s->current_file_ok = (err == UNZ_OK);
  974. if (pfile_info)
  975. *pfile_info = s->cur_file_info;
  976. return err;
  977. }
  978. /*
  979. Set the current file of the zipfile to the first file.
  980. return UNZ_OK if there is no problem
  981. */
  982. int ZEXPORT unzGoToFirstFile (unzFile file)
  983. {
  984. return unzGoToFirstFile64(file, NULL, NULL, 0);
  985. }
  986. /*
  987. Set the current file of the zipfile to the next file
  988. with retrieving an information about the file.
  989. return UNZ_OK if there is no problem
  990. return UNZ_END_OF_LIST_OF_FILE if the actual file was the latest.
  991. */
  992. int ZEXPORT unzGoToNextFile64 (unzFile file,
  993. unz_file_info64 *pfile_info,
  994. char *szFileName,
  995. uLong fileNameBufferSize)
  996. {
  997. unz64_s* s;
  998. int err;
  999. if (file==NULL)
  1000. return UNZ_PARAMERROR;
  1001. s=(unz64_s*)file;
  1002. if (!s->current_file_ok)
  1003. return UNZ_END_OF_LIST_OF_FILE;
  1004. if (s->gi.number_entry != 0xffff) /* 2^16 files overflow hack */
  1005. if (s->num_file+1==s->gi.number_entry)
  1006. return UNZ_END_OF_LIST_OF_FILE;
  1007. s->pos_in_central_dir += SIZECENTRALDIRITEM + s->cur_file_info.size_filename +
  1008. s->cur_file_info.size_file_extra + s->cur_file_info.size_file_comment ;
  1009. s->num_file++;
  1010. err = unz64local_GetCurrentFileInfoInternal(file,&s->cur_file_info,
  1011. &s->cur_file_info_internal,
  1012. szFileName,fileNameBufferSize,NULL,0,NULL,0);
  1013. s->current_file_ok = (err == UNZ_OK);
  1014. if (pfile_info)
  1015. *pfile_info = s->cur_file_info;
  1016. return err;
  1017. }
  1018. /*
  1019. Set the current file of the zipfile to the next file.
  1020. return UNZ_OK if there is no problem
  1021. return UNZ_END_OF_LIST_OF_FILE if the actual file was the latest.
  1022. */
  1023. int ZEXPORT unzGoToNextFile (unzFile file)
  1024. {
  1025. return unzGoToNextFile64(file, NULL, NULL, 0);
  1026. }
  1027. /*
  1028. Try locate the file szFileName in the zipfile.
  1029. For the iCaseSensitivity signification, see unzipStringFileNameCompare
  1030. return value :
  1031. UNZ_OK if the file is found. It becomes the current file.
  1032. UNZ_END_OF_LIST_OF_FILE if the file is not found
  1033. */
  1034. int ZEXPORT unzLocateFile (unzFile file, const char *szFileName, int iCaseSensitivity)
  1035. {
  1036. unz64_s* s;
  1037. int err;
  1038. /* We remember the 'current' position in the file so that we can jump
  1039. * back there if we fail.
  1040. */
  1041. unz_file_info64 cur_file_infoSaved;
  1042. unz_file_info64_internal cur_file_info_internalSaved;
  1043. ZPOS64_T num_fileSaved;
  1044. ZPOS64_T pos_in_central_dirSaved;
  1045. if (file==NULL)
  1046. return UNZ_PARAMERROR;
  1047. if (strlen(szFileName)>=UNZ_MAXFILENAMEINZIP)
  1048. return UNZ_PARAMERROR;
  1049. s=(unz64_s*)file;
  1050. if (!s->current_file_ok)
  1051. return UNZ_END_OF_LIST_OF_FILE;
  1052. /* Save the current state */
  1053. num_fileSaved = s->num_file;
  1054. pos_in_central_dirSaved = s->pos_in_central_dir;
  1055. cur_file_infoSaved = s->cur_file_info;
  1056. cur_file_info_internalSaved = s->cur_file_info_internal;
  1057. err = unzGoToFirstFile(file);
  1058. while (err == UNZ_OK)
  1059. {
  1060. char szCurrentFileName[UNZ_MAXFILENAMEINZIP+1];
  1061. err = unzGetCurrentFileInfo64(file,NULL,
  1062. szCurrentFileName,sizeof(szCurrentFileName)-1,
  1063. NULL,0,NULL,0);
  1064. if (err == UNZ_OK)
  1065. {
  1066. if (unzStringFileNameCompare(szCurrentFileName,
  1067. szFileName,iCaseSensitivity)==0)
  1068. return UNZ_OK;
  1069. err = unzGoToNextFile(file);
  1070. }
  1071. }
  1072. /* We failed, so restore the state of the 'current file' to where we
  1073. * were.
  1074. */
  1075. s->num_file = num_fileSaved ;
  1076. s->pos_in_central_dir = pos_in_central_dirSaved ;
  1077. s->cur_file_info = cur_file_infoSaved;
  1078. s->cur_file_info_internal = cur_file_info_internalSaved;
  1079. return err;
  1080. }
  1081. /*
  1082. ///////////////////////////////////////////
  1083. // Contributed by Ryan Haksi (mailto://cryogen@infoserve.net)
  1084. // I need random access
  1085. //
  1086. // Further optimization could be realized by adding an ability
  1087. // to cache the directory in memory. The goal being a single
  1088. // comprehensive file read to put the file I need in a memory.
  1089. */
  1090. /*
  1091. typedef struct unz_file_pos_s
  1092. {
  1093. ZPOS64_T pos_in_zip_directory; // offset in file
  1094. ZPOS64_T num_of_file; // # of file
  1095. } unz_file_pos;
  1096. */
  1097. int ZEXPORT unzGetFilePos64(unzFile file, unz64_file_pos* file_pos)
  1098. {
  1099. unz64_s* s;
  1100. if (file==NULL || file_pos==NULL)
  1101. return UNZ_PARAMERROR;
  1102. s=(unz64_s*)file;
  1103. if (!s->current_file_ok)
  1104. return UNZ_END_OF_LIST_OF_FILE;
  1105. file_pos->pos_in_zip_directory = s->pos_in_central_dir;
  1106. file_pos->num_of_file = s->num_file;
  1107. return UNZ_OK;
  1108. }
  1109. int ZEXPORT unzGetFilePos(
  1110. unzFile file,
  1111. unz_file_pos* file_pos)
  1112. {
  1113. unz64_file_pos file_pos64;
  1114. int err = unzGetFilePos64(file,&file_pos64);
  1115. if (err==UNZ_OK)
  1116. {
  1117. file_pos->pos_in_zip_directory = (uLong)file_pos64.pos_in_zip_directory;
  1118. file_pos->num_of_file = (uLong)file_pos64.num_of_file;
  1119. }
  1120. return err;
  1121. }
  1122. int ZEXPORT unzGoToFilePos64(unzFile file, const unz64_file_pos* file_pos)
  1123. {
  1124. unz64_s* s;
  1125. int err;
  1126. if (file==NULL || file_pos==NULL)
  1127. return UNZ_PARAMERROR;
  1128. s=(unz64_s*)file;
  1129. /* jump to the right spot */
  1130. s->pos_in_central_dir = file_pos->pos_in_zip_directory;
  1131. s->num_file = file_pos->num_of_file;
  1132. /* set the current file */
  1133. err = unz64local_GetCurrentFileInfoInternal(file,&s->cur_file_info,
  1134. &s->cur_file_info_internal,
  1135. NULL,0,NULL,0,NULL,0);
  1136. /* return results */
  1137. s->current_file_ok = (err == UNZ_OK);
  1138. return err;
  1139. }
  1140. int ZEXPORT unzGoToFilePos(
  1141. unzFile file,
  1142. unz_file_pos* file_pos)
  1143. {
  1144. unz64_file_pos file_pos64;
  1145. if (file_pos == NULL)
  1146. return UNZ_PARAMERROR;
  1147. file_pos64.pos_in_zip_directory = file_pos->pos_in_zip_directory;
  1148. file_pos64.num_of_file = file_pos->num_of_file;
  1149. return unzGoToFilePos64(file,&file_pos64);
  1150. }
  1151. /*
  1152. // Unzip Helper Functions - should be here?
  1153. ///////////////////////////////////////////
  1154. */
  1155. /*
  1156. Read the local header of the current zipfile
  1157. Check the coherency of the local header and info in the end of central
  1158. directory about this file
  1159. store in *piSizeVar the size of extra info in local header
  1160. (filename and size of extra field data)
  1161. */
  1162. local int unz64local_CheckCurrentFileCoherencyHeader (unz64_s* s, uInt* piSizeVar,
  1163. ZPOS64_T * poffset_local_extrafield,
  1164. uInt * psize_local_extrafield)
  1165. {
  1166. uLong uMagic,uData,uFlags;
  1167. uLong size_filename;
  1168. uLong size_extra_field;
  1169. int err=UNZ_OK;
  1170. *piSizeVar = 0;
  1171. *poffset_local_extrafield = 0;
  1172. *psize_local_extrafield = 0;
  1173. if (ZSEEK64(s->z_filefunc, s->filestream,s->cur_file_info_internal.offset_curfile +
  1174. s->byte_before_the_zipfile,ZLIB_FILEFUNC_SEEK_SET)!=0)
  1175. return UNZ_ERRNO;
  1176. if (err==UNZ_OK)
  1177. {
  1178. if (unz64local_getLong(&s->z_filefunc, s->filestream,&uMagic) != UNZ_OK)
  1179. err=UNZ_ERRNO;
  1180. else if (uMagic!=0x04034b50)
  1181. err=UNZ_BADZIPFILE;
  1182. }
  1183. if (unz64local_getShort(&s->z_filefunc, s->filestream,&uData) != UNZ_OK)
  1184. err=UNZ_ERRNO;
  1185. /*
  1186. else if ((err==UNZ_OK) && (uData!=s->cur_file_info.wVersion))
  1187. err=UNZ_BADZIPFILE;
  1188. */
  1189. if (unz64local_getShort(&s->z_filefunc, s->filestream,&uFlags) != UNZ_OK)
  1190. err=UNZ_ERRNO;
  1191. if (unz64local_getShort(&s->z_filefunc, s->filestream,&uData) != UNZ_OK)
  1192. err=UNZ_ERRNO;
  1193. else if ((err==UNZ_OK) && (uData!=s->cur_file_info.compression_method))
  1194. err=UNZ_BADZIPFILE;
  1195. if ((err==UNZ_OK) && (s->cur_file_info.compression_method!=0) &&
  1196. /* #ifdef HAVE_BZIP2 */
  1197. (s->cur_file_info.compression_method!=Z_BZIP2ED) &&
  1198. /* #endif */
  1199. (s->cur_file_info.compression_method!=Z_DEFLATED))
  1200. err=UNZ_BADZIPFILE;
  1201. if (unz64local_getLong(&s->z_filefunc, s->filestream,&uData) != UNZ_OK) /* date/time */
  1202. err=UNZ_ERRNO;
  1203. if (unz64local_getLong(&s->z_filefunc, s->filestream,&uData) != UNZ_OK) /* crc */
  1204. err=UNZ_ERRNO;
  1205. else if ((err==UNZ_OK) && (uData!=s->cur_file_info.crc) && ((uFlags & 8)==0))
  1206. err=UNZ_BADZIPFILE;
  1207. if (unz64local_getLong(&s->z_filefunc, s->filestream,&uData) != UNZ_OK) /* size compr */
  1208. err=UNZ_ERRNO;
  1209. else if (uData != 0xFFFFFFFF && (err==UNZ_OK) && (uData!=s->cur_file_info.compressed_size) && ((uFlags & 8)==0))
  1210. err=UNZ_BADZIPFILE;
  1211. if (unz64local_getLong(&s->z_filefunc, s->filestream,&uData) != UNZ_OK) /* size uncompr */
  1212. err=UNZ_ERRNO;
  1213. else if (uData != 0xFFFFFFFF && (err==UNZ_OK) && (uData!=s->cur_file_info.uncompressed_size) && ((uFlags & 8)==0))
  1214. err=UNZ_BADZIPFILE;
  1215. if (unz64local_getShort(&s->z_filefunc, s->filestream,&size_filename) != UNZ_OK)
  1216. err=UNZ_ERRNO;
  1217. else if ((err==UNZ_OK) && (size_filename!=s->cur_file_info.size_filename))
  1218. err=UNZ_BADZIPFILE;
  1219. *piSizeVar += (uInt)size_filename;
  1220. if (unz64local_getShort(&s->z_filefunc, s->filestream,&size_extra_field) != UNZ_OK)
  1221. err=UNZ_ERRNO;
  1222. *poffset_local_extrafield= s->cur_file_info_internal.offset_curfile +
  1223. SIZEZIPLOCALHEADER + size_filename;
  1224. *psize_local_extrafield = (uInt)size_extra_field;
  1225. *piSizeVar += (uInt)size_extra_field;
  1226. return err;
  1227. }
  1228. /*
  1229. Open for reading data the current file in the zipfile.
  1230. If there is no error and the file is opened, the return value is UNZ_OK.
  1231. */
  1232. int ZEXPORT unzOpenCurrentFile3 (unzFile file, int* method,
  1233. int* level, int raw, const char* password)
  1234. {
  1235. int err=UNZ_OK;
  1236. uInt iSizeVar;
  1237. unz64_s* s;
  1238. file_in_zip64_read_info_s* pfile_in_zip_read_info;
  1239. ZPOS64_T offset_local_extrafield; /* offset of the local extra field */
  1240. uInt size_local_extrafield; /* size of the local extra field */
  1241. # ifndef NOUNCRYPT
  1242. char source[12];
  1243. # else
  1244. if (password != NULL)
  1245. return UNZ_PARAMERROR;
  1246. # endif
  1247. if (file==NULL)
  1248. return UNZ_PARAMERROR;
  1249. s=(unz64_s*)file;
  1250. if (!s->current_file_ok)
  1251. return UNZ_PARAMERROR;
  1252. if (s->pfile_in_zip_read != NULL)
  1253. unzCloseCurrentFile(file);
  1254. if (unz64local_CheckCurrentFileCoherencyHeader(s,&iSizeVar, &offset_local_extrafield,&size_local_extrafield)!=UNZ_OK)
  1255. return UNZ_BADZIPFILE;
  1256. pfile_in_zip_read_info = (file_in_zip64_read_info_s*)ALLOC(sizeof(file_in_zip64_read_info_s));
  1257. if (pfile_in_zip_read_info==NULL)
  1258. return UNZ_INTERNALERROR;
  1259. pfile_in_zip_read_info->read_buffer=(char*)ALLOC(UNZ_BUFSIZE);
  1260. pfile_in_zip_read_info->offset_local_extrafield = offset_local_extrafield;
  1261. pfile_in_zip_read_info->size_local_extrafield = size_local_extrafield;
  1262. pfile_in_zip_read_info->pos_local_extrafield=0;
  1263. pfile_in_zip_read_info->raw=raw;
  1264. if (pfile_in_zip_read_info->read_buffer==NULL)
  1265. {
  1266. TRYFREE(pfile_in_zip_read_info);
  1267. return UNZ_INTERNALERROR;
  1268. }
  1269. pfile_in_zip_read_info->stream_initialised=0;
  1270. if (method!=NULL)
  1271. *method = (int)s->cur_file_info.compression_method;
  1272. if (level!=NULL)
  1273. {
  1274. *level = 6;
  1275. switch (s->cur_file_info.flag & 0x06)
  1276. {
  1277. case 6 : *level = 1; break;
  1278. case 4 : *level = 2; break;
  1279. case 2 : *level = 9; break;
  1280. }
  1281. }
  1282. if ((s->cur_file_info.compression_method!=0) &&
  1283. /* #ifdef HAVE_BZIP2 */
  1284. (s->cur_file_info.compression_method!=Z_BZIP2ED) &&
  1285. /* #endif */
  1286. (s->cur_file_info.compression_method!=Z_DEFLATED)) {
  1287. //err=UNZ_BADZIPFILE;
  1288. }
  1289. pfile_in_zip_read_info->crc32_wait=s->cur_file_info.crc;
  1290. pfile_in_zip_read_info->crc32=0;
  1291. pfile_in_zip_read_info->total_out_64=0;
  1292. pfile_in_zip_read_info->compression_method = s->cur_file_info.compression_method;
  1293. pfile_in_zip_read_info->filestream=s->filestream;
  1294. pfile_in_zip_read_info->z_filefunc=s->z_filefunc;
  1295. pfile_in_zip_read_info->byte_before_the_zipfile=s->byte_before_the_zipfile;
  1296. pfile_in_zip_read_info->stream.total_out = 0;
  1297. if ((s->cur_file_info.compression_method==Z_BZIP2ED) && (!raw))
  1298. {
  1299. #ifdef HAVE_BZIP2
  1300. pfile_in_zip_read_info->bstream.bzalloc = (void *(*) (void *, int, int))0;
  1301. pfile_in_zip_read_info->bstream.bzfree = (free_func)0;
  1302. pfile_in_zip_read_info->bstream.opaque = (voidpf)0;
  1303. pfile_in_zip_read_info->bstream.state = (voidpf)0;
  1304. pfile_in_zip_read_info->stream.zalloc = (alloc_func)0;
  1305. pfile_in_zip_read_info->stream.zfree = (free_func)0;
  1306. pfile_in_zip_read_info->stream.opaque = (voidpf)0;
  1307. pfile_in_zip_read_info->stream.next_in = (voidpf)0;
  1308. pfile_in_zip_read_info->stream.avail_in = 0;
  1309. err=BZ2_bzDecompressInit(&pfile_in_zip_read_info->bstream, 0, 0);
  1310. if (err == Z_OK)
  1311. pfile_in_zip_read_info->stream_initialised=Z_BZIP2ED;
  1312. else
  1313. {
  1314. TRYFREE(pfile_in_zip_read_info);
  1315. return err;
  1316. }
  1317. #else
  1318. pfile_in_zip_read_info->raw=1;
  1319. #endif
  1320. }
  1321. else if ((s->cur_file_info.compression_method==Z_DEFLATED) && (!raw))
  1322. {
  1323. pfile_in_zip_read_info->stream.zalloc = (alloc_func)0;
  1324. pfile_in_zip_read_info->stream.zfree = (free_func)0;
  1325. pfile_in_zip_read_info->stream.opaque = (voidpf)0;
  1326. pfile_in_zip_read_info->stream.next_in = 0;
  1327. pfile_in_zip_read_info->stream.avail_in = 0;
  1328. err=inflateInit2(&pfile_in_zip_read_info->stream, -MAX_WBITS);
  1329. if (err == Z_OK)
  1330. pfile_in_zip_read_info->stream_initialised=Z_DEFLATED;
  1331. else
  1332. {
  1333. TRYFREE(pfile_in_zip_read_info);
  1334. return err;
  1335. }
  1336. /* windowBits is passed < 0 to tell that there is no zlib header.
  1337. * Note that in this case inflate *requires* an extra "dummy" byte
  1338. * after the compressed stream in order to complete decompression and
  1339. * return Z_STREAM_END.
  1340. * In unzip, i don't wait absolutely Z_STREAM_END because I known the
  1341. * size of both compressed and uncompressed data
  1342. */
  1343. }
  1344. pfile_in_zip_read_info->rest_read_compressed =
  1345. s->cur_file_info.compressed_size ;
  1346. pfile_in_zip_read_info->rest_read_uncompressed =
  1347. s->cur_file_info.uncompressed_size ;
  1348. pfile_in_zip_read_info->pos_in_zipfile =
  1349. s->cur_file_info_internal.offset_curfile + SIZEZIPLOCALHEADER +
  1350. iSizeVar;
  1351. pfile_in_zip_read_info->stream.avail_in = (uInt)0;
  1352. s->pfile_in_zip_read = pfile_in_zip_read_info;
  1353. s->encrypted = 0;
  1354. # ifndef NOUNCRYPT
  1355. if (password != NULL)
  1356. {
  1357. int i;
  1358. s->pcrc_32_tab = get_crc_table();
  1359. init_keys(password,s->keys,s->pcrc_32_tab);
  1360. if (ZSEEK64(s->z_filefunc, s->filestream,
  1361. s->pfile_in_zip_read->pos_in_zipfile +
  1362. s->pfile_in_zip_read->byte_before_the_zipfile,
  1363. SEEK_SET)!=0)
  1364. return UNZ_INTERNALERROR;
  1365. if(ZREAD64(s->z_filefunc, s->filestream,source, 12)<12)
  1366. return UNZ_INTERNALERROR;
  1367. for (i = 0; i<12; i++)
  1368. zdecode(s->keys,s->pcrc_32_tab,source[i]);
  1369. s->pfile_in_zip_read->pos_in_zipfile+=12;
  1370. s->encrypted=1;
  1371. }
  1372. # endif
  1373. return UNZ_OK;
  1374. }
  1375. int ZEXPORT unzOpenCurrentFile (unzFile file)
  1376. {
  1377. return unzOpenCurrentFile3(file, NULL, NULL, 0, NULL);
  1378. }
  1379. int ZEXPORT unzOpenCurrentFilePassword (unzFile file, const char* password)
  1380. {
  1381. return unzOpenCurrentFile3(file, NULL, NULL, 0, password);
  1382. }
  1383. int ZEXPORT unzOpenCurrentFile2 (unzFile file, int* method, int* level, int raw)
  1384. {
  1385. return unzOpenCurrentFile3(file, method, level, raw, NULL);
  1386. }
  1387. /** Addition for GDAL : START */
  1388. ZPOS64_T ZEXPORT unzGetCurrentFileZStreamPos64( unzFile file)
  1389. {
  1390. unz64_s* s;
  1391. file_in_zip64_read_info_s* pfile_in_zip_read_info;
  1392. s=(unz64_s*)file;
  1393. if (file==NULL)
  1394. return 0; //UNZ_PARAMERROR;
  1395. pfile_in_zip_read_info=s->pfile_in_zip_read;
  1396. if (pfile_in_zip_read_info==NULL)
  1397. return 0; //UNZ_PARAMERROR;
  1398. return pfile_in_zip_read_info->pos_in_zipfile +
  1399. pfile_in_zip_read_info->byte_before_the_zipfile;
  1400. }
  1401. /** Addition for GDAL : END */
  1402. /*
  1403. Read bytes from the current file.
  1404. buf contain buffer where data must be copied
  1405. len the size of buf.
  1406. return the number of byte copied if some bytes are copied
  1407. return 0 if the end of file was reached
  1408. return <0 with error code if there is an error
  1409. (UNZ_ERRNO for IO error, or zLib error for uncompress error)
  1410. */
  1411. int ZEXPORT unzReadCurrentFile (unzFile file, voidp buf, unsigned len)
  1412. {
  1413. int err=UNZ_OK;
  1414. uInt iRead = 0;
  1415. unz64_s* s;
  1416. file_in_zip64_read_info_s* pfile_in_zip_read_info;
  1417. if (file==NULL)
  1418. return UNZ_PARAMERROR;
  1419. s=(unz64_s*)file;
  1420. pfile_in_zip_read_info=s->pfile_in_zip_read;
  1421. if (pfile_in_zip_read_info==NULL)
  1422. return UNZ_PARAMERROR;
  1423. if (pfile_in_zip_read_info->read_buffer == NULL)
  1424. return UNZ_END_OF_LIST_OF_FILE;
  1425. if (len==0)
  1426. return 0;
  1427. pfile_in_zip_read_info->stream.next_out = (Bytef*)buf;
  1428. pfile_in_zip_read_info->stream.avail_out = (uInt)len;
  1429. if ((len>pfile_in_zip_read_info->rest_read_uncompressed) &&
  1430. (!(pfile_in_zip_read_info->raw)))
  1431. pfile_in_zip_read_info->stream.avail_out =
  1432. (uInt)pfile_in_zip_read_info->rest_read_uncompressed;
  1433. if ((len>pfile_in_zip_read_info->rest_read_compressed+
  1434. pfile_in_zip_read_info->stream.avail_in) &&
  1435. (pfile_in_zip_read_info->raw))
  1436. pfile_in_zip_read_info->stream.avail_out =
  1437. (uInt)pfile_in_zip_read_info->rest_read_compressed+
  1438. pfile_in_zip_read_info->stream.avail_in;
  1439. while (pfile_in_zip_read_info->stream.avail_out>0)
  1440. {
  1441. if ((pfile_in_zip_read_info->stream.avail_in==0) &&
  1442. (pfile_in_zip_read_info->rest_read_compressed>0))
  1443. {
  1444. uInt uReadThis = UNZ_BUFSIZE;
  1445. if (pfile_in_zip_read_info->rest_read_compressed<uReadThis)
  1446. uReadThis = (uInt)pfile_in_zip_read_info->rest_read_compressed;
  1447. if (uReadThis == 0)
  1448. return UNZ_EOF;
  1449. if (ZSEEK64(pfile_in_zip_read_info->z_filefunc,
  1450. pfile_in_zip_read_info->filestream,
  1451. pfile_in_zip_read_info->pos_in_zipfile +
  1452. pfile_in_zip_read_info->byte_before_the_zipfile,
  1453. ZLIB_FILEFUNC_SEEK_SET)!=0)
  1454. return UNZ_ERRNO;
  1455. if (ZREAD64(pfile_in_zip_read_info->z_filefunc,
  1456. pfile_in_zip_read_info->filestream,
  1457. pfile_in_zip_read_info->read_buffer,
  1458. uReadThis)!=uReadThis)
  1459. return UNZ_ERRNO;
  1460. # ifndef NOUNCRYPT
  1461. if(s->encrypted)
  1462. {
  1463. uInt i;
  1464. for(i=0;i<uReadThis;i++)
  1465. pfile_in_zip_read_info->read_buffer[i] =
  1466. zdecode(s->keys,s->pcrc_32_tab,
  1467. pfile_in_zip_read_info->read_buffer[i]);
  1468. }
  1469. # endif
  1470. pfile_in_zip_read_info->pos_in_zipfile += uReadThis;
  1471. pfile_in_zip_read_info->rest_read_compressed-=uReadThis;
  1472. pfile_in_zip_read_info->stream.next_in =
  1473. (Bytef*)pfile_in_zip_read_info->read_buffer;
  1474. pfile_in_zip_read_info->stream.avail_in = (uInt)uReadThis;
  1475. }
  1476. if ((pfile_in_zip_read_info->compression_method==0) || (pfile_in_zip_read_info->raw))
  1477. {
  1478. uInt uDoCopy,i ;
  1479. if ((pfile_in_zip_read_info->stream.avail_in == 0) &&
  1480. (pfile_in_zip_read_info->rest_read_compressed == 0))
  1481. return (iRead==0) ? UNZ_EOF : iRead;
  1482. if (pfile_in_zip_read_info->stream.avail_out <
  1483. pfile_in_zip_read_info->stream.avail_in)
  1484. uDoCopy = pfile_in_zip_read_info->stream.avail_out ;
  1485. else
  1486. uDoCopy = pfile_in_zip_read_info->stream.avail_in ;
  1487. for (i=0;i<uDoCopy;i++)
  1488. *(pfile_in_zip_read_info->stream.next_out+i) =
  1489. *(pfile_in_zip_read_info->stream.next_in+i);
  1490. pfile_in_zip_read_info->total_out_64 = pfile_in_zip_read_info->total_out_64 + uDoCopy;
  1491. pfile_in_zip_read_info->crc32 = crc32(pfile_in_zip_read_info->crc32,
  1492. pfile_in_zip_read_info->stream.next_out,
  1493. uDoCopy);
  1494. pfile_in_zip_read_info->rest_read_uncompressed-=uDoCopy;
  1495. pfile_in_zip_read_info->stream.avail_in -= uDoCopy;
  1496. pfile_in_zip_read_info->stream.avail_out -= uDoCopy;
  1497. pfile_in_zip_read_info->stream.next_out += uDoCopy;
  1498. pfile_in_zip_read_info->stream.next_in += uDoCopy;
  1499. pfile_in_zip_read_info->stream.total_out += uDoCopy;
  1500. iRead += uDoCopy;
  1501. }
  1502. else if (pfile_in_zip_read_info->compression_method==Z_BZIP2ED)
  1503. {
  1504. #ifdef HAVE_BZIP2
  1505. uLong uTotalOutBefore,uTotalOutAfter;
  1506. const Bytef *bufBefore;
  1507. uLong uOutThis;
  1508. pfile_in_zip_read_info->bstream.next_in = (char*)pfile_in_zip_read_info->stream.next_in;
  1509. pfile_in_zip_read_info->bstream.avail_in = pfile_in_zip_read_info->stream.avail_in;
  1510. pfile_in_zip_read_info->bstream.total_in_lo32 = pfile_in_zip_read_info->stream.total_in;
  1511. pfile_in_zip_read_info->bstream.total_in_hi32 = 0;
  1512. pfile_in_zip_read_info->bstream.next_out = (char*)pfile_in_zip_read_info->stream.next_out;
  1513. pfile_in_zip_read_info->bstream.avail_out = pfile_in_zip_read_info->stream.avail_out;
  1514. pfile_in_zip_read_info->bstream.total_out_lo32 = pfile_in_zip_read_info->stream.total_out;
  1515. pfile_in_zip_read_info->bstream.total_out_hi32 = 0;
  1516. uTotalOutBefore = pfile_in_zip_read_info->bstream.total_out_lo32;
  1517. bufBefore = (const Bytef *)pfile_in_zip_read_info->bstream.next_out;
  1518. err=BZ2_bzDecompress(&pfile_in_zip_read_info->bstream);
  1519. uTotalOutAfter = pfile_in_zip_read_info->bstream.total_out_lo32;
  1520. uOutThis = uTotalOutAfter-uTotalOutBefore;
  1521. pfile_in_zip_read_info->total_out_64 = pfile_in_zip_read_info->total_out_64 + uOutThis;
  1522. pfile_in_zip_read_info->crc32 = crc32(pfile_in_zip_read_info->crc32,bufBefore, (uInt)(uOutThis));
  1523. pfile_in_zip_read_info->rest_read_uncompressed -= uOutThis;
  1524. iRead += (uInt)(uTotalOutAfter - uTotalOutBefore);
  1525. pfile_in_zip_read_info->stream.next_in = (Bytef*)pfile_in_zip_read_info->bstream.next_in;
  1526. pfile_in_zip_read_info->stream.avail_in = pfile_in_zip_read_info->bstream.avail_in;
  1527. pfile_in_zip_read_info->stream.total_in = pfile_in_zip_read_info->bstream.total_in_lo32;
  1528. pfile_in_zip_read_info->stream.next_out = (Bytef*)pfile_in_zip_read_info->bstream.next_out;
  1529. pfile_in_zip_read_info->stream.avail_out = pfile_in_zip_read_info->bstream.avail_out;
  1530. pfile_in_zip_read_info->stream.total_out = pfile_in_zip_read_info->bstream.total_out_lo32;
  1531. if (err==BZ_STREAM_END)
  1532. return (iRead==0) ? UNZ_EOF : iRead;
  1533. if (err!=BZ_OK)
  1534. break;
  1535. #endif
  1536. } // end Z_BZIP2ED
  1537. else
  1538. {
  1539. ZPOS64_T uTotalOutBefore,uTotalOutAfter;
  1540. const Bytef *bufBefore;
  1541. ZPOS64_T uOutThis;
  1542. int flush=Z_SYNC_FLUSH;
  1543. uTotalOutBefore = pfile_in_zip_read_info->stream.total_out;
  1544. bufBefore = pfile_in_zip_read_info->stream.next_out;
  1545. /*
  1546. if ((pfile_in_zip_read_info->rest_read_uncompressed ==
  1547. pfile_in_zip_read_info->stream.avail_out) &&
  1548. (pfile_in_zip_read_info->rest_read_compressed == 0))
  1549. flush = Z_FINISH;
  1550. */
  1551. err=inflate(&pfile_in_zip_read_info->stream,flush);
  1552. if ((err>=0) && (pfile_in_zip_read_info->stream.msg!=NULL))
  1553. err = Z_DATA_ERROR;
  1554. uTotalOutAfter = pfile_in_zip_read_info->stream.total_out;
  1555. uOutThis = uTotalOutAfter-uTotalOutBefore;
  1556. pfile_in_zip_read_info->total_out_64 = pfile_in_zip_read_info->total_out_64 + uOutThis;
  1557. pfile_in_zip_read_info->crc32 =
  1558. crc32(pfile_in_zip_read_info->crc32,bufBefore,
  1559. (uInt)(uOutThis));
  1560. pfile_in_zip_read_info->rest_read_uncompressed -=
  1561. uOutThis;
  1562. iRead += (uInt)(uTotalOutAfter - uTotalOutBefore);
  1563. if (err==Z_STREAM_END)
  1564. return (iRead==0) ? UNZ_EOF : iRead;
  1565. if (err!=Z_OK)
  1566. break;
  1567. }
  1568. }
  1569. if (err==Z_OK)
  1570. return iRead;
  1571. return err;
  1572. }
  1573. /*
  1574. Give the current position in uncompressed data
  1575. */
  1576. z_off_t ZEXPORT unztell (unzFile file)
  1577. {
  1578. unz64_s* s;
  1579. file_in_zip64_read_info_s* pfile_in_zip_read_info;
  1580. if (file==NULL)
  1581. return UNZ_PARAMERROR;
  1582. s=(unz64_s*)file;
  1583. pfile_in_zip_read_info=s->pfile_in_zip_read;
  1584. if (pfile_in_zip_read_info==NULL)
  1585. return UNZ_PARAMERROR;
  1586. return (z_off_t)pfile_in_zip_read_info->stream.total_out;
  1587. }
  1588. ZPOS64_T ZEXPORT unztell64 (unzFile file)
  1589. {
  1590. unz64_s* s;
  1591. file_in_zip64_read_info_s* pfile_in_zip_read_info;
  1592. if (file==NULL)
  1593. return (ZPOS64_T)-1;
  1594. s=(unz64_s*)file;
  1595. pfile_in_zip_read_info=s->pfile_in_zip_read;
  1596. if (pfile_in_zip_read_info==NULL)
  1597. return (ZPOS64_T)-1;
  1598. return pfile_in_zip_read_info->total_out_64;
  1599. }
  1600. /*
  1601. return 1 if the end of file was reached, 0 elsewhere
  1602. */
  1603. int ZEXPORT unzeof (unzFile file)
  1604. {
  1605. unz64_s* s;
  1606. file_in_zip64_read_info_s* pfile_in_zip_read_info;
  1607. if (file==NULL)
  1608. return UNZ_PARAMERROR;
  1609. s=(unz64_s*)file;
  1610. pfile_in_zip_read_info=s->pfile_in_zip_read;
  1611. if (pfile_in_zip_read_info==NULL)
  1612. return UNZ_PARAMERROR;
  1613. if (pfile_in_zip_read_info->rest_read_uncompressed == 0)
  1614. return 1;
  1615. else
  1616. return 0;
  1617. }
  1618. /*
  1619. Read extra field from the current file (opened by unzOpenCurrentFile)
  1620. This is the local-header version of the extra field (sometimes, there is
  1621. more info in the local-header version than in the central-header)
  1622. if buf==NULL, it return the size of the local extra field that can be read
  1623. if buf!=NULL, len is the size of the buffer, the extra header is copied in
  1624. buf.
  1625. the return value is the number of bytes copied in buf, or (if <0)
  1626. the error code
  1627. */
  1628. int ZEXPORT unzGetLocalExtrafield (unzFile file, voidp buf, unsigned len)
  1629. {
  1630. unz64_s* s;
  1631. file_in_zip64_read_info_s* pfile_in_zip_read_info;
  1632. uInt read_now;
  1633. ZPOS64_T size_to_read;
  1634. if (file==NULL)
  1635. return UNZ_PARAMERROR;
  1636. s=(unz64_s*)file;
  1637. pfile_in_zip_read_info=s->pfile_in_zip_read;
  1638. if (pfile_in_zip_read_info==NULL)
  1639. return UNZ_PARAMERROR;
  1640. size_to_read = (pfile_in_zip_read_info->size_local_extrafield -
  1641. pfile_in_zip_read_info->pos_local_extrafield);
  1642. if (buf==NULL)
  1643. return (int)size_to_read;
  1644. if (len>size_to_read)
  1645. read_now = (uInt)size_to_read;
  1646. else
  1647. read_now = (uInt)len ;
  1648. if (read_now==0)
  1649. return 0;
  1650. if (ZSEEK64(pfile_in_zip_read_info->z_filefunc,
  1651. pfile_in_zip_read_info->filestream,
  1652. pfile_in_zip_read_info->offset_local_extrafield +
  1653. pfile_in_zip_read_info->pos_local_extrafield,
  1654. ZLIB_FILEFUNC_SEEK_SET)!=0)
  1655. return UNZ_ERRNO;
  1656. if (ZREAD64(pfile_in_zip_read_info->z_filefunc,
  1657. pfile_in_zip_read_info->filestream,
  1658. buf,read_now)!=read_now)
  1659. return UNZ_ERRNO;
  1660. return (int)read_now;
  1661. }
  1662. /*
  1663. Close the file in zip opened with unzipOpenCurrentFile
  1664. Return UNZ_CRCERROR if all the file was read but the CRC is not good
  1665. */
  1666. int ZEXPORT unzCloseCurrentFile (unzFile file)
  1667. {
  1668. int err=UNZ_OK;
  1669. unz64_s* s;
  1670. file_in_zip64_read_info_s* pfile_in_zip_read_info;
  1671. if (file==NULL)
  1672. return UNZ_PARAMERROR;
  1673. s=(unz64_s*)file;
  1674. pfile_in_zip_read_info=s->pfile_in_zip_read;
  1675. if (pfile_in_zip_read_info==NULL)
  1676. return UNZ_PARAMERROR;
  1677. if ((pfile_in_zip_read_info->rest_read_uncompressed == 0) &&
  1678. (!pfile_in_zip_read_info->raw))
  1679. {
  1680. if (pfile_in_zip_read_info->crc32 != pfile_in_zip_read_info->crc32_wait)
  1681. err=UNZ_CRCERROR;
  1682. }
  1683. TRYFREE(pfile_in_zip_read_info->read_buffer);
  1684. pfile_in_zip_read_info->read_buffer = NULL;
  1685. if (pfile_in_zip_read_info->stream_initialised == Z_DEFLATED)
  1686. inflateEnd(&pfile_in_zip_read_info->stream);
  1687. #ifdef HAVE_BZIP2
  1688. else if (pfile_in_zip_read_info->stream_initialised == Z_BZIP2ED)
  1689. BZ2_bzDecompressEnd(&pfile_in_zip_read_info->bstream);
  1690. #endif
  1691. pfile_in_zip_read_info->stream_initialised = 0;
  1692. TRYFREE(pfile_in_zip_read_info);
  1693. s->pfile_in_zip_read=NULL;
  1694. return err;
  1695. }
  1696. /*
  1697. Get the global comment string of the ZipFile, in the szComment buffer.
  1698. uSizeBuf is the size of the szComment buffer.
  1699. return the number of byte copied or an error code <0
  1700. */
  1701. int ZEXPORT unzGetGlobalComment (unzFile file, char * szComment, uLong uSizeBuf)
  1702. {
  1703. unz64_s* s;
  1704. uLong uReadThis ;
  1705. if (file==NULL)
  1706. return (int)UNZ_PARAMERROR;
  1707. s=(unz64_s*)file;
  1708. uReadThis = uSizeBuf;
  1709. if (uReadThis>s->gi.size_comment)
  1710. uReadThis = s->gi.size_comment;
  1711. if (ZSEEK64(s->z_filefunc,s->filestream,s->central_pos+22,ZLIB_FILEFUNC_SEEK_SET)!=0)
  1712. return UNZ_ERRNO;
  1713. if (uReadThis>0)
  1714. {
  1715. *szComment='\0';
  1716. if (ZREAD64(s->z_filefunc,s->filestream,szComment,uReadThis)!=uReadThis)
  1717. return UNZ_ERRNO;
  1718. }
  1719. if ((szComment != NULL) && (uSizeBuf > s->gi.size_comment))
  1720. *(szComment+s->gi.size_comment)='\0';
  1721. return (int)uReadThis;
  1722. }
  1723. /* Additions by RX '2004 */
  1724. ZPOS64_T ZEXPORT unzGetOffset64(unzFile file)
  1725. {
  1726. unz64_s* s;
  1727. if (file==NULL)
  1728. return 0; //UNZ_PARAMERROR;
  1729. s=(unz64_s*)file;
  1730. if (!s->current_file_ok)
  1731. return 0;
  1732. if (s->gi.number_entry != 0 && s->gi.number_entry != 0xffff)
  1733. if (s->num_file==s->gi.number_entry)
  1734. return 0;
  1735. return s->pos_in_central_dir;
  1736. }
  1737. uLong ZEXPORT unzGetOffset (unzFile file)
  1738. {
  1739. ZPOS64_T offset64;
  1740. if (file==NULL)
  1741. return 0; //UNZ_PARAMERROR;
  1742. offset64 = unzGetOffset64(file);
  1743. return (uLong)offset64;
  1744. }
  1745. int ZEXPORT unzSetOffset64(unzFile file, ZPOS64_T pos)
  1746. {
  1747. unz64_s* s;
  1748. int err;
  1749. if (file==NULL)
  1750. return UNZ_PARAMERROR;
  1751. s=(unz64_s*)file;
  1752. s->pos_in_central_dir = pos;
  1753. s->num_file = s->gi.number_entry; /* hack */
  1754. err = unz64local_GetCurrentFileInfoInternal(file,&s->cur_file_info,
  1755. &s->cur_file_info_internal,
  1756. NULL,0,NULL,0,NULL,0);
  1757. s->current_file_ok = (err == UNZ_OK);
  1758. return err;
  1759. }
  1760. int ZEXPORT unzSetOffset (unzFile file, uLong pos)
  1761. {
  1762. return unzSetOffset64(file,pos);
  1763. }
  1764. } // end of namespace cocos2d