unzip.c 37 KB

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