unzip.c 72 KB

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