unzip.c 73 KB

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