Unzip.cpp 33 KB

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