unzip.c 36 KB

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