unzip.c 70 KB

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