unzip.c 35 KB

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