unzip.c 35 KB

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