unzip.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319
  1. /* unzip.c -- IO on .zip files using zlib
  2. Version 0.15 beta, Mar 19th, 1998,
  3. Read unzip.h for more info
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include "zlib.h"
  9. #include "unzip.h"
  10. #define __PHYSICSFS_INTERNAL__
  11. #include "physfs_internal.h"
  12. #if (!defined PHYSFS_SUPPORTS_ZIP)
  13. #error PHYSFS_SUPPORTS_ZIP must be defined.
  14. #endif
  15. #ifdef STDC
  16. # include <stddef.h>
  17. # include <string.h>
  18. # include <stdlib.h>
  19. #endif
  20. #ifdef NO_ERRNO_H
  21. extern int errno;
  22. #else
  23. # include <errno.h>
  24. #endif
  25. #ifndef local
  26. # define local static
  27. #endif
  28. /* compile with -Dlocal if your debugger can't find static symbols */
  29. #if !defined(unix) && !defined(CASESENSITIVITYDEFAULT_YES) && \
  30. !defined(CASESENSITIVITYDEFAULT_NO)
  31. #define CASESENSITIVITYDEFAULT_NO
  32. #endif
  33. #ifndef UNZ_BUFSIZE
  34. #define UNZ_BUFSIZE (16384)
  35. #endif
  36. #ifndef UNZ_MAXFILENAMEINZIP
  37. #define UNZ_MAXFILENAMEINZIP (256)
  38. #endif
  39. #ifndef ALLOC
  40. # define ALLOC(size) (malloc(size))
  41. #endif
  42. #ifndef TRYFREE
  43. # define TRYFREE(p) {if (p) free(p);}
  44. #endif
  45. #define SIZECENTRALDIRITEM (0x2e)
  46. #define SIZEZIPLOCALHEADER (0x1e)
  47. /* I've found an old Unix (a SunOS 4.1.3_U1) without all SEEK_* defined.... */
  48. #ifndef SEEK_CUR
  49. #define SEEK_CUR 1
  50. #endif
  51. #ifndef SEEK_END
  52. #define SEEK_END 2
  53. #endif
  54. #ifndef SEEK_SET
  55. #define SEEK_SET 0
  56. #endif
  57. const char unz_copyright[] =
  58. " unzip 0.15 Copyright 1998 Gilles Vollant ";
  59. /* unz_file_info_interntal contain internal info about a file in zipfile*/
  60. typedef struct unz_file_info_internal_s
  61. {
  62. uLong offset_curfile;/* relative offset of local header 4 bytes */
  63. } unz_file_info_internal;
  64. /* file_in_zip_read_info_s contain internal information about a file in zipfile,
  65. when reading and decompress it */
  66. typedef struct
  67. {
  68. char *read_buffer; /* internal buffer for compressed data */
  69. z_stream stream; /* zLib stream structure for inflate */
  70. uLong pos_in_zipfile; /* position in byte on the zipfile, for seek*/
  71. uLong stream_initialised; /* flag set if stream structure is initialised*/
  72. uLong offset_local_extrafield;/* offset of the local extra field */
  73. uInt size_local_extrafield;/* size of the local extra field */
  74. uLong pos_local_extrafield; /* position in the local extra field in read*/
  75. uLong crc32; /* crc32 of all data uncompressed */
  76. uLong crc32_wait; /* crc32 we must obtain after decompress all */
  77. uLong rest_read_compressed; /* number of byte to be decompressed */
  78. uLong rest_read_uncompressed;/*number of byte to be obtained after decomp*/
  79. void* file; /* io structore of the zipfile */
  80. uLong compression_method; /* compression method (0==store) */
  81. uLong byte_before_the_zipfile;/* byte before the zipfile, (>0 for sfx)*/
  82. } file_in_zip_read_info_s;
  83. /* unz_s contain internal information about the zipfile
  84. */
  85. typedef struct
  86. {
  87. void* file; /* io structore of the zipfile */
  88. unz_global_info gi; /* public global information */
  89. uLong byte_before_the_zipfile;/* byte before the zipfile, (>0 for sfx)*/
  90. uLong num_file; /* number of the current file in the zipfile*/
  91. uLong pos_in_central_dir; /* pos of the current file in the central dir*/
  92. uLong current_file_ok; /* flag about the usability of the current file*/
  93. uLong central_pos; /* position of the beginning of the central dir*/
  94. uLong size_central_dir; /* size of the central directory */
  95. uLong offset_central_dir; /* offset of start of central directory with
  96. respect to the starting disk number */
  97. unz_file_info cur_file_info; /* public info about the current file in zip*/
  98. unz_file_info_internal cur_file_info_internal; /* private info about it*/
  99. file_in_zip_read_info_s* pfile_in_zip_read; /* structure about the current
  100. file if we are decompressing it */
  101. } unz_s;
  102. /* ===========================================================================
  103. Read a byte from a gz_stream; update next_in and avail_in. Return EOF
  104. for end of file.
  105. IN assertion: the stream s has been sucessfully opened for reading.
  106. */
  107. #if 0 /* not actually used anymore, at this point. */
  108. local int unzlocal_getByte(fin,pi)
  109. void *fin;
  110. int *pi;
  111. {
  112. PHYSFS_uint8 c;
  113. PHYSFS_sint64 err = __PHYSFS_platformRead(fin, &c, sizeof (c), 1);
  114. if (err==1)
  115. {
  116. *pi = (int)c;
  117. return UNZ_OK;
  118. }
  119. else
  120. {
  121. if (__PHYSFS_platformEOF(fin))
  122. return UNZ_EOF;
  123. else
  124. return UNZ_ERRNO;
  125. }
  126. }
  127. #endif
  128. /* ===========================================================================
  129. Reads a long in LSB order from the given gz_stream. Sets
  130. */
  131. local int unzlocal_getShort (fin,pX)
  132. void* fin;
  133. uLong *pX;
  134. {
  135. PHYSFS_uint16 val;
  136. PHYSFS_sint64 err = __PHYSFS_platformRead(fin, &val, sizeof (val), 1);
  137. if (err==1)
  138. {
  139. *pX = (uLong) PHYSFS_swapULE16(val);
  140. return UNZ_OK;
  141. }
  142. else
  143. {
  144. *pX = 0;
  145. if (__PHYSFS_platformEOF(fin))
  146. return UNZ_EOF;
  147. else
  148. return UNZ_ERRNO;
  149. }
  150. return(UNZ_ERRNO); /* shouldn't ever hit this. */
  151. }
  152. local int unzlocal_getLong (fin,pX)
  153. void* fin;
  154. uLong *pX;
  155. {
  156. PHYSFS_uint32 val;
  157. PHYSFS_sint64 err = __PHYSFS_platformRead(fin, &val, sizeof (val), 1);
  158. if (err==1)
  159. {
  160. *pX = (uLong) PHYSFS_swapULE32(val);
  161. return UNZ_OK;
  162. }
  163. else
  164. {
  165. *pX = 0;
  166. if (__PHYSFS_platformEOF(fin))
  167. return UNZ_EOF;
  168. else
  169. return UNZ_ERRNO;
  170. }
  171. return(UNZ_ERRNO); /* shouldn't ever hit this. */
  172. }
  173. /* My own strcmpi / strcasecmp */
  174. #if 1
  175. #define strcmpcasenosensitive_internal(x,y) __PHYSFS_platformStricmp(x,y)
  176. #else
  177. local int strcmpcasenosensitive_internal (fileName1,fileName2)
  178. const char* fileName1;
  179. const char* fileName2;
  180. {
  181. for (;;)
  182. {
  183. char c1=*(fileName1++);
  184. char c2=*(fileName2++);
  185. if ((c1>='a') && (c1<='z'))
  186. c1 -= 0x20;
  187. if ((c2>='a') && (c2<='z'))
  188. c2 -= 0x20;
  189. if (c1=='\0')
  190. return ((c2=='\0') ? 0 : -1);
  191. if (c2=='\0')
  192. return 1;
  193. if (c1<c2)
  194. return -1;
  195. if (c1>c2)
  196. return 1;
  197. }
  198. }
  199. #endif
  200. #ifdef CASESENSITIVITYDEFAULT_NO
  201. #define CASESENSITIVITYDEFAULTVALUE 2
  202. #else
  203. #define CASESENSITIVITYDEFAULTVALUE 1
  204. #endif
  205. #ifndef STRCMPCASENOSENTIVEFUNCTION
  206. #define STRCMPCASENOSENTIVEFUNCTION strcmpcasenosensitive_internal
  207. #endif
  208. /*
  209. Compare two filename (fileName1,fileName2).
  210. If iCaseSenisivity = 1, comparision is case sensitivity (like strcmp)
  211. If iCaseSenisivity = 2, comparision is not case sensitivity (like strcmpi
  212. or strcasecmp)
  213. If iCaseSenisivity = 0, case sensitivity is defaut of your operating system
  214. (like 1 on Unix, 2 on Windows)
  215. */
  216. extern int ZEXPORT unzStringFileNameCompare (fileName1,fileName2,iCaseSensitivity)
  217. const char* fileName1;
  218. const char* fileName2;
  219. int iCaseSensitivity;
  220. {
  221. if (iCaseSensitivity==0)
  222. iCaseSensitivity=CASESENSITIVITYDEFAULTVALUE;
  223. if (iCaseSensitivity==1)
  224. return strcmp(fileName1,fileName2);
  225. return STRCMPCASENOSENTIVEFUNCTION(fileName1,fileName2);
  226. }
  227. #define BUFREADCOMMENT (0x400)
  228. /*
  229. Locate the Central directory of a zipfile (at the end, just before
  230. the global comment)
  231. */
  232. local uLong unzlocal_SearchCentralDir(fin)
  233. void *fin;
  234. {
  235. unsigned char* buf;
  236. uLong uSizeFile;
  237. uLong uBackRead;
  238. uLong uMaxBack=0xffff; /* maximum size of global comment */
  239. uLong uPosFound=0;
  240. uSizeFile = (uLong) __PHYSFS_platformFileLength( fin );
  241. if (uMaxBack>uSizeFile)
  242. uMaxBack = uSizeFile;
  243. buf = (unsigned char*)ALLOC(BUFREADCOMMENT+4);
  244. if (buf==NULL)
  245. return 0;
  246. uBackRead = 4;
  247. while (uBackRead<uMaxBack)
  248. {
  249. uLong uReadSize,uReadPos ;
  250. int i;
  251. if (uBackRead+BUFREADCOMMENT>uMaxBack)
  252. uBackRead = uMaxBack;
  253. else
  254. uBackRead+=BUFREADCOMMENT;
  255. uReadPos = uSizeFile-uBackRead ;
  256. uReadSize = ((BUFREADCOMMENT+4) < (uSizeFile-uReadPos)) ?
  257. (BUFREADCOMMENT+4) : (uSizeFile-uReadPos);
  258. if (!__PHYSFS_platformSeek(fin,uReadPos))
  259. break;
  260. if (__PHYSFS_platformRead(fin,buf,(uInt)uReadSize,1)!=1)
  261. break;
  262. for (i=(int)uReadSize-3; (i--)>0;)
  263. {
  264. if (((*(buf+i))==0x50) && ((*(buf+i+1))==0x4b) &&
  265. ((*(buf+i+2))==0x05) && ((*(buf+i+3))==0x06))
  266. {
  267. uPosFound = uReadPos+i;
  268. break;
  269. }
  270. }
  271. if (uPosFound!=0)
  272. break;
  273. }
  274. TRYFREE(buf);
  275. return uPosFound;
  276. }
  277. /*
  278. Open a Zip file. path contain the full pathname (by example,
  279. on a Windows NT computer "c:\\test\\zlib109.zip" or on an Unix computer
  280. "zlib/zlib109.zip".
  281. If the zipfile cannot be opened (file don't exist or in not valid), the
  282. return value is NULL.
  283. Else, the return value is a unzFile Handle, usable with other function
  284. of this unzip package.
  285. */
  286. extern unzFile ZEXPORT unzOpen (path)
  287. const char *path;
  288. {
  289. unz_s us;
  290. unz_s *s;
  291. uLong central_pos,uL;
  292. void* fin ;
  293. uLong number_disk; /* number of the current dist, used for
  294. spaning ZIP, unsupported, always 0*/
  295. uLong number_disk_with_CD; /* number the the disk with central dir, used
  296. for spaning ZIP, unsupported, always 0*/
  297. uLong number_entry_CD; /* total number of entries in
  298. the central dir
  299. (same than number_entry on nospan) */
  300. int err=UNZ_OK;
  301. if (unz_copyright[0]!=' ')
  302. return NULL;
  303. fin=__PHYSFS_platformOpenRead(path);
  304. if (fin==NULL)
  305. return NULL;
  306. central_pos = unzlocal_SearchCentralDir(fin);
  307. if (central_pos==0)
  308. err=UNZ_ERRNO;
  309. if (!__PHYSFS_platformSeek(fin,central_pos))
  310. err=UNZ_ERRNO;
  311. /* the signature, already checked */
  312. if (unzlocal_getLong(fin,&uL)!=UNZ_OK)
  313. err=UNZ_ERRNO;
  314. /* number of this disk */
  315. if (unzlocal_getShort(fin,&number_disk)!=UNZ_OK)
  316. err=UNZ_ERRNO;
  317. /* number of the disk with the start of the central directory */
  318. if (unzlocal_getShort(fin,&number_disk_with_CD)!=UNZ_OK)
  319. err=UNZ_ERRNO;
  320. /* total number of entries in the central dir on this disk */
  321. if (unzlocal_getShort(fin,&us.gi.number_entry)!=UNZ_OK)
  322. err=UNZ_ERRNO;
  323. /* total number of entries in the central dir */
  324. if (unzlocal_getShort(fin,&number_entry_CD)!=UNZ_OK)
  325. err=UNZ_ERRNO;
  326. if ((number_entry_CD!=us.gi.number_entry) ||
  327. (number_disk_with_CD!=0) ||
  328. (number_disk!=0))
  329. err=UNZ_BADZIPFILE;
  330. /* size of the central directory */
  331. if (unzlocal_getLong(fin,&us.size_central_dir)!=UNZ_OK)
  332. err=UNZ_ERRNO;
  333. /* offset of start of central directory with respect to the
  334. starting disk number */
  335. if (unzlocal_getLong(fin,&us.offset_central_dir)!=UNZ_OK)
  336. err=UNZ_ERRNO;
  337. /* zipfile comment length */
  338. if (unzlocal_getShort(fin,&us.gi.size_comment)!=UNZ_OK)
  339. err=UNZ_ERRNO;
  340. if ((central_pos<us.offset_central_dir+us.size_central_dir) &&
  341. (err==UNZ_OK))
  342. err=UNZ_BADZIPFILE;
  343. if (err!=UNZ_OK)
  344. {
  345. __PHYSFS_platformClose(fin);
  346. return NULL;
  347. }
  348. us.file=fin;
  349. us.byte_before_the_zipfile = central_pos -
  350. (us.offset_central_dir+us.size_central_dir);
  351. us.central_pos = central_pos;
  352. us.pfile_in_zip_read = NULL;
  353. s=(unz_s*)ALLOC(sizeof(unz_s));
  354. *s=us;
  355. unzGoToFirstFile((unzFile)s);
  356. return (unzFile)s;
  357. }
  358. /*
  359. Close a ZipFile opened with unzipOpen.
  360. If there is files inside the .Zip opened with unzipOpenCurrentFile (see later),
  361. these files MUST be closed with unzipCloseCurrentFile before call unzipClose.
  362. return UNZ_OK if there is no problem. */
  363. extern int ZEXPORT unzClose (file)
  364. unzFile file;
  365. {
  366. unz_s* s;
  367. if (file==NULL)
  368. return UNZ_PARAMERROR;
  369. s=(unz_s*)file;
  370. if (s->pfile_in_zip_read!=NULL)
  371. unzCloseCurrentFile(file);
  372. __PHYSFS_platformClose(s->file);
  373. TRYFREE(s);
  374. return UNZ_OK;
  375. }
  376. /*
  377. Write info about the ZipFile in the *pglobal_info structure.
  378. No preparation of the structure is needed
  379. return UNZ_OK if there is no problem. */
  380. extern int ZEXPORT unzGetGlobalInfo (file,pglobal_info)
  381. unzFile file;
  382. unz_global_info *pglobal_info;
  383. {
  384. unz_s* s;
  385. if (file==NULL)
  386. return UNZ_PARAMERROR;
  387. s=(unz_s*)file;
  388. *pglobal_info=s->gi;
  389. return UNZ_OK;
  390. }
  391. /*
  392. Translate date/time from Dos format to tm_unz (readable more easilty)
  393. */
  394. local void unzlocal_DosDateToTmuDate (ulDosDate, ptm)
  395. uLong ulDosDate;
  396. tm_unz* ptm;
  397. {
  398. uLong uDate;
  399. uDate = (uLong)(ulDosDate>>16);
  400. ptm->tm_mday = (uInt)(uDate&0x1f) ;
  401. ptm->tm_mon = (uInt)((((uDate)&0x1E0)/0x20)-1) ;
  402. ptm->tm_year = (uInt)(((uDate&0x0FE00)/0x0200)+1980) ;
  403. ptm->tm_hour = (uInt) ((ulDosDate &0xF800)/0x800);
  404. ptm->tm_min = (uInt) ((ulDosDate&0x7E0)/0x20) ;
  405. ptm->tm_sec = (uInt) (2*(ulDosDate&0x1f)) ;
  406. }
  407. /*
  408. Get Info about the current file in the zipfile, with internal only info
  409. */
  410. local int unzlocal_GetCurrentFileInfoInternal OF((unzFile file,
  411. unz_file_info *pfile_info,
  412. unz_file_info_internal
  413. *pfile_info_internal,
  414. char *szFileName,
  415. uLong fileNameBufferSize,
  416. void *extraField,
  417. uLong extraFieldBufferSize,
  418. char *szComment,
  419. uLong commentBufferSize));
  420. local int unzlocal_GetCurrentFileInfoInternal (file,
  421. pfile_info,
  422. pfile_info_internal,
  423. szFileName, fileNameBufferSize,
  424. extraField, extraFieldBufferSize,
  425. szComment, commentBufferSize)
  426. unzFile file;
  427. unz_file_info *pfile_info;
  428. unz_file_info_internal *pfile_info_internal;
  429. char *szFileName;
  430. uLong fileNameBufferSize;
  431. void *extraField;
  432. uLong extraFieldBufferSize;
  433. char *szComment;
  434. uLong commentBufferSize;
  435. {
  436. unz_s* s;
  437. unz_file_info file_info;
  438. unz_file_info_internal file_info_internal;
  439. int err=UNZ_OK;
  440. uLong uMagic;
  441. long lSeek=0;
  442. if (file==NULL)
  443. return UNZ_PARAMERROR;
  444. s=(unz_s*)file;
  445. if (!__PHYSFS_platformSeek(s->file,s->pos_in_central_dir+s->byte_before_the_zipfile))
  446. err=UNZ_ERRNO;
  447. /* we check the magic */
  448. if (err==UNZ_OK)
  449. {
  450. if (unzlocal_getLong(s->file,&uMagic) != UNZ_OK)
  451. err=UNZ_ERRNO;
  452. else if (uMagic!=0x02014b50)
  453. err=UNZ_BADZIPFILE;
  454. }
  455. if (unzlocal_getShort(s->file,&file_info.version) != UNZ_OK)
  456. err=UNZ_ERRNO;
  457. if (unzlocal_getShort(s->file,&file_info.version_needed) != UNZ_OK)
  458. err=UNZ_ERRNO;
  459. if (unzlocal_getShort(s->file,&file_info.flag) != UNZ_OK)
  460. err=UNZ_ERRNO;
  461. if (unzlocal_getShort(s->file,&file_info.compression_method) != UNZ_OK)
  462. err=UNZ_ERRNO;
  463. if (unzlocal_getLong(s->file,&file_info.dosDate) != UNZ_OK)
  464. err=UNZ_ERRNO;
  465. unzlocal_DosDateToTmuDate(file_info.dosDate,&file_info.tmu_date);
  466. if (unzlocal_getLong(s->file,&file_info.crc) != UNZ_OK)
  467. err=UNZ_ERRNO;
  468. if (unzlocal_getLong(s->file,&file_info.compressed_size) != UNZ_OK)
  469. err=UNZ_ERRNO;
  470. if (unzlocal_getLong(s->file,&file_info.uncompressed_size) != UNZ_OK)
  471. err=UNZ_ERRNO;
  472. if (unzlocal_getShort(s->file,&file_info.size_filename) != UNZ_OK)
  473. err=UNZ_ERRNO;
  474. if (unzlocal_getShort(s->file,&file_info.size_file_extra) != UNZ_OK)
  475. err=UNZ_ERRNO;
  476. if (unzlocal_getShort(s->file,&file_info.size_file_comment) != UNZ_OK)
  477. err=UNZ_ERRNO;
  478. if (unzlocal_getShort(s->file,&file_info.disk_num_start) != UNZ_OK)
  479. err=UNZ_ERRNO;
  480. if (unzlocal_getShort(s->file,&file_info.internal_fa) != UNZ_OK)
  481. err=UNZ_ERRNO;
  482. if (unzlocal_getLong(s->file,&file_info.external_fa) != UNZ_OK)
  483. err=UNZ_ERRNO;
  484. if (unzlocal_getLong(s->file,&file_info_internal.offset_curfile) != UNZ_OK)
  485. err=UNZ_ERRNO;
  486. lSeek+=file_info.size_filename;
  487. if ((err==UNZ_OK) && (szFileName!=NULL))
  488. {
  489. uLong uSizeRead ;
  490. if (file_info.size_filename<fileNameBufferSize)
  491. {
  492. *(szFileName+file_info.size_filename)='\0';
  493. uSizeRead = file_info.size_filename;
  494. }
  495. else
  496. uSizeRead = fileNameBufferSize;
  497. if ((file_info.size_filename>0) && (fileNameBufferSize>0))
  498. {
  499. if (__PHYSFS_platformRead(s->file,szFileName,(uInt)uSizeRead,1)!=1)
  500. err=UNZ_ERRNO;
  501. }
  502. lSeek -= uSizeRead;
  503. }
  504. if ((err==UNZ_OK) && (extraField!=NULL))
  505. {
  506. uLong uSizeRead ;
  507. if (file_info.size_file_extra<extraFieldBufferSize)
  508. uSizeRead = file_info.size_file_extra;
  509. else
  510. uSizeRead = extraFieldBufferSize;
  511. if (lSeek!=0)
  512. {
  513. PHYSFS_sint64 curpos = __PHYSFS_platformTell(s->file);
  514. if ((curpos >= 0) && (__PHYSFS_platformSeek(s->file,lSeek+curpos)))
  515. lSeek=0;
  516. else
  517. err=UNZ_ERRNO;
  518. }
  519. if ((file_info.size_file_extra>0) && (extraFieldBufferSize>0))
  520. {
  521. if (__PHYSFS_platformRead(s->file,extraField,(uInt)uSizeRead,1)!=1)
  522. err=UNZ_ERRNO;
  523. }
  524. lSeek += file_info.size_file_extra - uSizeRead;
  525. }
  526. else
  527. lSeek+=file_info.size_file_extra;
  528. if ((err==UNZ_OK) && (szComment!=NULL))
  529. {
  530. uLong uSizeRead ;
  531. if (file_info.size_file_comment<commentBufferSize)
  532. {
  533. *(szComment+file_info.size_file_comment)='\0';
  534. uSizeRead = file_info.size_file_comment;
  535. }
  536. else
  537. uSizeRead = commentBufferSize;
  538. if (lSeek!=0)
  539. {
  540. PHYSFS_sint64 curpos = __PHYSFS_platformTell(s->file);
  541. if (__PHYSFS_platformSeek(s->file,lSeek+curpos))
  542. lSeek=0;
  543. else
  544. err=UNZ_ERRNO;
  545. }
  546. if ((file_info.size_file_comment>0) && (commentBufferSize>0))
  547. {
  548. if (__PHYSFS_platformRead(s->file,szComment,(uInt)uSizeRead,1)!=1)
  549. err=UNZ_ERRNO;
  550. }
  551. lSeek+=file_info.size_file_comment - uSizeRead;
  552. }
  553. else
  554. lSeek+=file_info.size_file_comment;
  555. if ((err==UNZ_OK) && (pfile_info!=NULL))
  556. *pfile_info=file_info;
  557. if ((err==UNZ_OK) && (pfile_info_internal!=NULL))
  558. *pfile_info_internal=file_info_internal;
  559. return err;
  560. }
  561. /*
  562. Write info about the ZipFile in the *pglobal_info structure.
  563. No preparation of the structure is needed
  564. return UNZ_OK if there is no problem.
  565. */
  566. extern int ZEXPORT unzGetCurrentFileInfo (file,
  567. pfile_info,
  568. szFileName, fileNameBufferSize,
  569. extraField, extraFieldBufferSize,
  570. szComment, commentBufferSize)
  571. unzFile file;
  572. unz_file_info *pfile_info;
  573. char *szFileName;
  574. uLong fileNameBufferSize;
  575. void *extraField;
  576. uLong extraFieldBufferSize;
  577. char *szComment;
  578. uLong commentBufferSize;
  579. {
  580. return unzlocal_GetCurrentFileInfoInternal(file,pfile_info,NULL,
  581. szFileName,fileNameBufferSize,
  582. extraField,extraFieldBufferSize,
  583. szComment,commentBufferSize);
  584. }
  585. /*
  586. Set the current file of the zipfile to the first file.
  587. return UNZ_OK if there is no problem
  588. */
  589. extern int ZEXPORT unzGoToFirstFile (file)
  590. unzFile file;
  591. {
  592. int err=UNZ_OK;
  593. unz_s* s;
  594. if (file==NULL)
  595. return UNZ_PARAMERROR;
  596. s=(unz_s*)file;
  597. s->pos_in_central_dir=s->offset_central_dir;
  598. s->num_file=0;
  599. err=unzlocal_GetCurrentFileInfoInternal(file,&s->cur_file_info,
  600. &s->cur_file_info_internal,
  601. NULL,0,NULL,0,NULL,0);
  602. s->current_file_ok = (err == UNZ_OK);
  603. return err;
  604. }
  605. /*
  606. Set the current file of the zipfile to the next file.
  607. return UNZ_OK if there is no problem
  608. return UNZ_END_OF_LIST_OF_FILE if the actual file was the latest.
  609. */
  610. extern int ZEXPORT unzGoToNextFile (file)
  611. unzFile file;
  612. {
  613. unz_s* s;
  614. int err;
  615. if (file==NULL)
  616. return UNZ_PARAMERROR;
  617. s=(unz_s*)file;
  618. if (!s->current_file_ok)
  619. return UNZ_END_OF_LIST_OF_FILE;
  620. if (s->num_file+1==s->gi.number_entry)
  621. return UNZ_END_OF_LIST_OF_FILE;
  622. s->pos_in_central_dir += SIZECENTRALDIRITEM + s->cur_file_info.size_filename +
  623. s->cur_file_info.size_file_extra + s->cur_file_info.size_file_comment ;
  624. s->num_file++;
  625. err = unzlocal_GetCurrentFileInfoInternal(file,&s->cur_file_info,
  626. &s->cur_file_info_internal,
  627. NULL,0,NULL,0,NULL,0);
  628. s->current_file_ok = (err == UNZ_OK);
  629. return err;
  630. }
  631. /*
  632. Try locate the file szFileName in the zipfile.
  633. For the iCaseSensitivity signification, see unzipStringFileNameCompare
  634. return value :
  635. UNZ_OK if the file is found. It becomes the current file.
  636. UNZ_END_OF_LIST_OF_FILE if the file is not found
  637. */
  638. extern int ZEXPORT unzLocateFile (file, szFileName, iCaseSensitivity)
  639. unzFile file;
  640. const char *szFileName;
  641. int iCaseSensitivity;
  642. {
  643. unz_s* s;
  644. int err;
  645. uLong num_fileSaved;
  646. uLong pos_in_central_dirSaved;
  647. if (file==NULL)
  648. return UNZ_PARAMERROR;
  649. if (strlen(szFileName)>=UNZ_MAXFILENAMEINZIP)
  650. return UNZ_PARAMERROR;
  651. s=(unz_s*)file;
  652. if (!s->current_file_ok)
  653. return UNZ_END_OF_LIST_OF_FILE;
  654. num_fileSaved = s->num_file;
  655. pos_in_central_dirSaved = s->pos_in_central_dir;
  656. err = unzGoToFirstFile(file);
  657. while (err == UNZ_OK)
  658. {
  659. char szCurrentFileName[UNZ_MAXFILENAMEINZIP+1];
  660. unzGetCurrentFileInfo(file,NULL,
  661. szCurrentFileName,sizeof(szCurrentFileName)-1,
  662. NULL,0,NULL,0);
  663. if (unzStringFileNameCompare(szCurrentFileName,
  664. szFileName,iCaseSensitivity)==0)
  665. return UNZ_OK;
  666. err = unzGoToNextFile(file);
  667. }
  668. s->num_file = num_fileSaved ;
  669. s->pos_in_central_dir = pos_in_central_dirSaved ;
  670. return err;
  671. }
  672. /*
  673. Read the local header of the current zipfile
  674. Check the coherency of the local header and info in the end of central
  675. directory about this file
  676. store in *piSizeVar the size of extra info in local header
  677. (filename and size of extra field data)
  678. */
  679. local int unzlocal_CheckCurrentFileCoherencyHeader (s,piSizeVar,
  680. poffset_local_extrafield,
  681. psize_local_extrafield)
  682. unz_s* s;
  683. uInt* piSizeVar;
  684. uLong *poffset_local_extrafield;
  685. uInt *psize_local_extrafield;
  686. {
  687. uLong uMagic,uData,uFlags;
  688. uLong size_filename;
  689. uLong size_extra_field;
  690. int err=UNZ_OK;
  691. *piSizeVar = 0;
  692. *poffset_local_extrafield = 0;
  693. *psize_local_extrafield = 0;
  694. if (!__PHYSFS_platformSeek(s->file,
  695. s->cur_file_info_internal.offset_curfile +
  696. s->byte_before_the_zipfile))
  697. {
  698. return UNZ_ERRNO;
  699. }
  700. if (err==UNZ_OK)
  701. {
  702. if (unzlocal_getLong(s->file,&uMagic) != UNZ_OK)
  703. err=UNZ_ERRNO;
  704. else if (uMagic!=0x04034b50)
  705. err=UNZ_BADZIPFILE;
  706. }
  707. if (unzlocal_getShort(s->file,&uData) != UNZ_OK)
  708. err=UNZ_ERRNO;
  709. /*
  710. else if ((err==UNZ_OK) && (uData!=s->cur_file_info.wVersion))
  711. err=UNZ_BADZIPFILE;
  712. */
  713. if (unzlocal_getShort(s->file,&uFlags) != UNZ_OK)
  714. err=UNZ_ERRNO;
  715. if (unzlocal_getShort(s->file,&uData) != UNZ_OK)
  716. err=UNZ_ERRNO;
  717. else if ((err==UNZ_OK) && (uData!=s->cur_file_info.compression_method))
  718. err=UNZ_BADZIPFILE;
  719. if ((err==UNZ_OK) && (s->cur_file_info.compression_method!=0) &&
  720. (s->cur_file_info.compression_method!=Z_DEFLATED))
  721. err=UNZ_BADZIPFILE;
  722. if (unzlocal_getLong(s->file,&uData) != UNZ_OK) /* date/time */
  723. err=UNZ_ERRNO;
  724. if (unzlocal_getLong(s->file,&uData) != UNZ_OK) /* crc */
  725. err=UNZ_ERRNO;
  726. else if ((err==UNZ_OK) && (uData!=s->cur_file_info.crc) &&
  727. ((uFlags & 8)==0))
  728. err=UNZ_BADZIPFILE;
  729. if (unzlocal_getLong(s->file,&uData) != UNZ_OK) /* size compr */
  730. err=UNZ_ERRNO;
  731. else if ((err==UNZ_OK) && (uData!=s->cur_file_info.compressed_size) &&
  732. ((uFlags & 8)==0))
  733. err=UNZ_BADZIPFILE;
  734. if (unzlocal_getLong(s->file,&uData) != UNZ_OK) /* size uncompr */
  735. err=UNZ_ERRNO;
  736. else if ((err==UNZ_OK) && (uData!=s->cur_file_info.uncompressed_size) &&
  737. ((uFlags & 8)==0))
  738. err=UNZ_BADZIPFILE;
  739. if (unzlocal_getShort(s->file,&size_filename) != UNZ_OK)
  740. err=UNZ_ERRNO;
  741. else if ((err==UNZ_OK) && (size_filename!=s->cur_file_info.size_filename))
  742. err=UNZ_BADZIPFILE;
  743. *piSizeVar += (uInt)size_filename;
  744. if (unzlocal_getShort(s->file,&size_extra_field) != UNZ_OK)
  745. err=UNZ_ERRNO;
  746. *poffset_local_extrafield= s->cur_file_info_internal.offset_curfile +
  747. SIZEZIPLOCALHEADER + size_filename;
  748. *psize_local_extrafield = (uInt)size_extra_field;
  749. *piSizeVar += (uInt)size_extra_field;
  750. return err;
  751. }
  752. /*
  753. Open for reading data the current file in the zipfile.
  754. If there is no error and the file is opened, the return value is UNZ_OK.
  755. */
  756. extern int ZEXPORT unzOpenCurrentFile (file)
  757. unzFile file;
  758. {
  759. int err=UNZ_OK;
  760. int Store;
  761. uInt iSizeVar;
  762. unz_s* s;
  763. file_in_zip_read_info_s* pfile_in_zip_read_info;
  764. uLong offset_local_extrafield; /* offset of the local extra field */
  765. uInt size_local_extrafield; /* size of the local extra field */
  766. if (file==NULL)
  767. return UNZ_PARAMERROR;
  768. s=(unz_s*)file;
  769. if (!s->current_file_ok)
  770. return UNZ_PARAMERROR;
  771. if (s->pfile_in_zip_read != NULL)
  772. unzCloseCurrentFile(file);
  773. if (unzlocal_CheckCurrentFileCoherencyHeader(s,&iSizeVar,
  774. &offset_local_extrafield,&size_local_extrafield)!=UNZ_OK)
  775. return UNZ_BADZIPFILE;
  776. pfile_in_zip_read_info = (file_in_zip_read_info_s*)
  777. ALLOC(sizeof(file_in_zip_read_info_s));
  778. if (pfile_in_zip_read_info==NULL)
  779. return UNZ_INTERNALERROR;
  780. pfile_in_zip_read_info->read_buffer=(char*)ALLOC(UNZ_BUFSIZE);
  781. pfile_in_zip_read_info->offset_local_extrafield = offset_local_extrafield;
  782. pfile_in_zip_read_info->size_local_extrafield = size_local_extrafield;
  783. pfile_in_zip_read_info->pos_local_extrafield=0;
  784. if (pfile_in_zip_read_info->read_buffer==NULL)
  785. {
  786. TRYFREE(pfile_in_zip_read_info);
  787. return UNZ_INTERNALERROR;
  788. }
  789. pfile_in_zip_read_info->stream_initialised=0;
  790. if ((s->cur_file_info.compression_method!=0) &&
  791. (s->cur_file_info.compression_method!=Z_DEFLATED))
  792. err=UNZ_BADZIPFILE;
  793. Store = s->cur_file_info.compression_method==0;
  794. pfile_in_zip_read_info->crc32_wait=s->cur_file_info.crc;
  795. pfile_in_zip_read_info->crc32=0;
  796. pfile_in_zip_read_info->compression_method =
  797. s->cur_file_info.compression_method;
  798. pfile_in_zip_read_info->file=s->file;
  799. pfile_in_zip_read_info->byte_before_the_zipfile=s->byte_before_the_zipfile;
  800. pfile_in_zip_read_info->stream.total_out = 0;
  801. if (!Store)
  802. {
  803. pfile_in_zip_read_info->stream.zalloc = (alloc_func)0;
  804. pfile_in_zip_read_info->stream.zfree = (free_func)0;
  805. pfile_in_zip_read_info->stream.opaque = (voidpf)0;
  806. err=inflateInit2(&pfile_in_zip_read_info->stream, -MAX_WBITS);
  807. if (err == Z_OK)
  808. pfile_in_zip_read_info->stream_initialised=1;
  809. /* windowBits is passed < 0 to tell that there is no zlib header.
  810. * Note that in this case inflate *requires* an extra "dummy" byte
  811. * after the compressed stream in order to complete decompression and
  812. * return Z_STREAM_END.
  813. * In unzip, i don't wait absolutely Z_STREAM_END because I known the
  814. * size of both compressed and uncompressed data
  815. */
  816. }
  817. pfile_in_zip_read_info->rest_read_compressed =
  818. s->cur_file_info.compressed_size ;
  819. pfile_in_zip_read_info->rest_read_uncompressed =
  820. s->cur_file_info.uncompressed_size ;
  821. pfile_in_zip_read_info->pos_in_zipfile =
  822. s->cur_file_info_internal.offset_curfile + SIZEZIPLOCALHEADER +
  823. iSizeVar;
  824. pfile_in_zip_read_info->stream.avail_in = (uInt)0;
  825. s->pfile_in_zip_read = pfile_in_zip_read_info;
  826. return UNZ_OK;
  827. }
  828. /*
  829. Read bytes from the current file.
  830. buf contain buffer where data must be copied
  831. len the size of buf.
  832. return the number of byte copied if somes bytes are copied
  833. return 0 if the end of file was reached
  834. return <0 with error code if there is an error
  835. (UNZ_ERRNO for IO error, or zLib error for uncompress error)
  836. */
  837. extern int ZEXPORT unzReadCurrentFile (file, buf, len)
  838. unzFile file;
  839. voidp buf;
  840. unsigned len;
  841. {
  842. int err=UNZ_OK;
  843. uInt iRead = 0;
  844. unz_s* s;
  845. file_in_zip_read_info_s* pfile_in_zip_read_info;
  846. if (file==NULL)
  847. return UNZ_PARAMERROR;
  848. s=(unz_s*)file;
  849. pfile_in_zip_read_info=s->pfile_in_zip_read;
  850. if (pfile_in_zip_read_info==NULL)
  851. return UNZ_PARAMERROR;
  852. if ((pfile_in_zip_read_info->read_buffer == NULL))
  853. return UNZ_END_OF_LIST_OF_FILE;
  854. if (len==0)
  855. return 0;
  856. pfile_in_zip_read_info->stream.next_out = (Bytef*)buf;
  857. pfile_in_zip_read_info->stream.avail_out = (uInt)len;
  858. if (len>pfile_in_zip_read_info->rest_read_uncompressed)
  859. pfile_in_zip_read_info->stream.avail_out =
  860. (uInt)pfile_in_zip_read_info->rest_read_uncompressed;
  861. while (pfile_in_zip_read_info->stream.avail_out>0)
  862. {
  863. if ((pfile_in_zip_read_info->stream.avail_in==0) &&
  864. (pfile_in_zip_read_info->rest_read_compressed>0))
  865. {
  866. uInt uReadThis = UNZ_BUFSIZE;
  867. if (pfile_in_zip_read_info->rest_read_compressed<uReadThis)
  868. uReadThis = (uInt)pfile_in_zip_read_info->rest_read_compressed;
  869. if (uReadThis == 0)
  870. return UNZ_EOF;
  871. if (!__PHYSFS_platformSeek(pfile_in_zip_read_info->file,
  872. pfile_in_zip_read_info->pos_in_zipfile +
  873. pfile_in_zip_read_info->byte_before_the_zipfile))
  874. return UNZ_ERRNO;
  875. if (__PHYSFS_platformRead(pfile_in_zip_read_info->file,
  876. pfile_in_zip_read_info->read_buffer,
  877. uReadThis,1)!=1)
  878. return UNZ_ERRNO;
  879. pfile_in_zip_read_info->pos_in_zipfile += uReadThis;
  880. pfile_in_zip_read_info->rest_read_compressed-=uReadThis;
  881. pfile_in_zip_read_info->stream.next_in =
  882. (Bytef*)pfile_in_zip_read_info->read_buffer;
  883. pfile_in_zip_read_info->stream.avail_in = (uInt)uReadThis;
  884. }
  885. if (pfile_in_zip_read_info->compression_method==0)
  886. {
  887. uInt uDoCopy,i ;
  888. if (pfile_in_zip_read_info->stream.avail_out <
  889. pfile_in_zip_read_info->stream.avail_in)
  890. uDoCopy = pfile_in_zip_read_info->stream.avail_out ;
  891. else
  892. uDoCopy = pfile_in_zip_read_info->stream.avail_in ;
  893. for (i=0;i<uDoCopy;i++)
  894. *(pfile_in_zip_read_info->stream.next_out+i) =
  895. *(pfile_in_zip_read_info->stream.next_in+i);
  896. pfile_in_zip_read_info->crc32 = crc32(pfile_in_zip_read_info->crc32,
  897. pfile_in_zip_read_info->stream.next_out,
  898. uDoCopy);
  899. pfile_in_zip_read_info->rest_read_uncompressed-=uDoCopy;
  900. pfile_in_zip_read_info->stream.avail_in -= uDoCopy;
  901. pfile_in_zip_read_info->stream.avail_out -= uDoCopy;
  902. pfile_in_zip_read_info->stream.next_out += uDoCopy;
  903. pfile_in_zip_read_info->stream.next_in += uDoCopy;
  904. pfile_in_zip_read_info->stream.total_out += uDoCopy;
  905. iRead += uDoCopy;
  906. }
  907. else
  908. {
  909. uLong uTotalOutBefore,uTotalOutAfter;
  910. const Bytef *bufBefore;
  911. uLong uOutThis;
  912. int flush=Z_SYNC_FLUSH;
  913. uTotalOutBefore = pfile_in_zip_read_info->stream.total_out;
  914. bufBefore = pfile_in_zip_read_info->stream.next_out;
  915. /*
  916. if ((pfile_in_zip_read_info->rest_read_uncompressed ==
  917. pfile_in_zip_read_info->stream.avail_out) &&
  918. (pfile_in_zip_read_info->rest_read_compressed == 0))
  919. flush = Z_FINISH;
  920. */
  921. err=inflate(&pfile_in_zip_read_info->stream,flush);
  922. uTotalOutAfter = pfile_in_zip_read_info->stream.total_out;
  923. uOutThis = uTotalOutAfter-uTotalOutBefore;
  924. pfile_in_zip_read_info->crc32 =
  925. crc32(pfile_in_zip_read_info->crc32,bufBefore,
  926. (uInt)(uOutThis));
  927. pfile_in_zip_read_info->rest_read_uncompressed -=
  928. uOutThis;
  929. iRead += (uInt)(uTotalOutAfter - uTotalOutBefore);
  930. if (err==Z_STREAM_END)
  931. return (iRead==0) ? UNZ_EOF : iRead;
  932. if (err!=Z_OK)
  933. break;
  934. }
  935. }
  936. if (err==Z_OK)
  937. return iRead;
  938. return err;
  939. }
  940. /*
  941. Give the current position in uncompressed data
  942. */
  943. extern z_off_t ZEXPORT unztell (file)
  944. unzFile file;
  945. {
  946. unz_s* s;
  947. file_in_zip_read_info_s* pfile_in_zip_read_info;
  948. if (file==NULL)
  949. return UNZ_PARAMERROR;
  950. s=(unz_s*)file;
  951. pfile_in_zip_read_info=s->pfile_in_zip_read;
  952. if (pfile_in_zip_read_info==NULL)
  953. return UNZ_PARAMERROR;
  954. return (z_off_t)pfile_in_zip_read_info->stream.total_out;
  955. }
  956. /*
  957. return 1 if the end of file was reached, 0 elsewhere
  958. */
  959. extern int ZEXPORT unzeof (file)
  960. unzFile file;
  961. {
  962. unz_s* s;
  963. file_in_zip_read_info_s* pfile_in_zip_read_info;
  964. if (file==NULL)
  965. return UNZ_PARAMERROR;
  966. s=(unz_s*)file;
  967. pfile_in_zip_read_info=s->pfile_in_zip_read;
  968. if (pfile_in_zip_read_info==NULL)
  969. return UNZ_PARAMERROR;
  970. if (pfile_in_zip_read_info->rest_read_uncompressed == 0)
  971. return 1;
  972. else
  973. return 0;
  974. }
  975. /*
  976. Read extra field from the current file (opened by unzOpenCurrentFile)
  977. This is the local-header version of the extra field (sometimes, there is
  978. more info in the local-header version than in the central-header)
  979. if buf==NULL, it return the size of the local extra field that can be read
  980. if buf!=NULL, len is the size of the buffer, the extra header is copied in
  981. buf.
  982. the return value is the number of bytes copied in buf, or (if <0)
  983. the error code
  984. */
  985. extern int ZEXPORT unzGetLocalExtrafield (file,buf,len)
  986. unzFile file;
  987. voidp buf;
  988. unsigned len;
  989. {
  990. unz_s* s;
  991. file_in_zip_read_info_s* pfile_in_zip_read_info;
  992. uInt read_now;
  993. uLong size_to_read;
  994. if (file==NULL)
  995. return UNZ_PARAMERROR;
  996. s=(unz_s*)file;
  997. pfile_in_zip_read_info=s->pfile_in_zip_read;
  998. if (pfile_in_zip_read_info==NULL)
  999. return UNZ_PARAMERROR;
  1000. size_to_read = (pfile_in_zip_read_info->size_local_extrafield -
  1001. pfile_in_zip_read_info->pos_local_extrafield);
  1002. if (buf==NULL)
  1003. return (int)size_to_read;
  1004. if (len>size_to_read)
  1005. read_now = (uInt)size_to_read;
  1006. else
  1007. read_now = (uInt)len ;
  1008. if (read_now==0)
  1009. return 0;
  1010. if (!__PHYSFS_platformSeek(pfile_in_zip_read_info->file,
  1011. pfile_in_zip_read_info->offset_local_extrafield +
  1012. pfile_in_zip_read_info->pos_local_extrafield))
  1013. return UNZ_ERRNO;
  1014. if (__PHYSFS_platformRead(pfile_in_zip_read_info->file,buf,(uInt)size_to_read,1)!=1)
  1015. return UNZ_ERRNO;
  1016. return (int)read_now;
  1017. }
  1018. /*
  1019. Close the file in zip opened with unzipOpenCurrentFile
  1020. Return UNZ_CRCERROR if all the file was read but the CRC is not good
  1021. */
  1022. extern int ZEXPORT unzCloseCurrentFile (file)
  1023. unzFile file;
  1024. {
  1025. int err=UNZ_OK;
  1026. unz_s* s;
  1027. file_in_zip_read_info_s* pfile_in_zip_read_info;
  1028. if (file==NULL)
  1029. return UNZ_PARAMERROR;
  1030. s=(unz_s*)file;
  1031. pfile_in_zip_read_info=s->pfile_in_zip_read;
  1032. if (pfile_in_zip_read_info==NULL)
  1033. return UNZ_PARAMERROR;
  1034. if (pfile_in_zip_read_info->rest_read_uncompressed == 0)
  1035. {
  1036. if (pfile_in_zip_read_info->crc32 != pfile_in_zip_read_info->crc32_wait)
  1037. err=UNZ_CRCERROR;
  1038. }
  1039. TRYFREE(pfile_in_zip_read_info->read_buffer);
  1040. pfile_in_zip_read_info->read_buffer = NULL;
  1041. if (pfile_in_zip_read_info->stream_initialised)
  1042. inflateEnd(&pfile_in_zip_read_info->stream);
  1043. pfile_in_zip_read_info->stream_initialised = 0;
  1044. TRYFREE(pfile_in_zip_read_info);
  1045. s->pfile_in_zip_read=NULL;
  1046. return err;
  1047. }
  1048. /*
  1049. Get the global comment string of the ZipFile, in the szComment buffer.
  1050. uSizeBuf is the size of the szComment buffer.
  1051. return the number of byte copied or an error code <0
  1052. */
  1053. extern int ZEXPORT unzGetGlobalComment (file, szComment, uSizeBuf)
  1054. unzFile file;
  1055. char *szComment;
  1056. uLong uSizeBuf;
  1057. {
  1058. /*int err=UNZ_OK;*/
  1059. unz_s* s;
  1060. uLong uReadThis ;
  1061. if (file==NULL)
  1062. return UNZ_PARAMERROR;
  1063. s=(unz_s*)file;
  1064. uReadThis = uSizeBuf;
  1065. if (uReadThis>s->gi.size_comment)
  1066. uReadThis = s->gi.size_comment;
  1067. if (!__PHYSFS_platformSeek(s->file,s->central_pos+22))
  1068. return UNZ_ERRNO;
  1069. if (uReadThis>0)
  1070. {
  1071. *szComment='\0';
  1072. if (__PHYSFS_platformRead(s->file,szComment,(uInt)uReadThis,1)!=1)
  1073. return UNZ_ERRNO;
  1074. }
  1075. if ((szComment != NULL) && (uSizeBuf > s->gi.size_comment))
  1076. *(szComment+s->gi.size_comment)='\0';
  1077. return (int)uReadThis;
  1078. }