Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2125 lines
70 KiB

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