zip.c 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956
  1. /* zip.c -- IO on .zip files using zlib
  2. Version 1.1, February 14h, 2010
  3. part of the MiniZip project - ( http://www.winimage.com/zLibDll/minizip.html )
  4. Copyright (C) 1998-2010 Gilles Vollant (minizip) ( http://www.winimage.com/zLibDll/minizip.html )
  5. Modifications for Zip64 support
  6. Copyright (C) 2009-2010 Mathias Svensson ( http://result42.com )
  7. For more info read MiniZip_info.txt
  8. Changes
  9. Oct-2009 - Mathias Svensson - Remove old C style function prototypes
  10. Oct-2009 - Mathias Svensson - Added Zip64 Support when creating new file archives
  11. Oct-2009 - Mathias Svensson - Did some code cleanup and refactoring to get better overview of some functions.
  12. Oct-2009 - Mathias Svensson - Added zipRemoveExtraInfoBlock to strip extra field data from its ZIP64 data
  13. It is used when recreating zip archive with RAW when deleting items from a zip.
  14. ZIP64 data is automatically added to items that needs it, and existing ZIP64 data need to be removed.
  15. Oct-2009 - Mathias Svensson - Added support for BZIP2 as compression mode (bzip2 lib is required)
  16. Jan-2010 - back to unzip and minizip 1.0 name scheme, with compatibility layer
  17. */
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <stdint.h>
  22. #include <time.h>
  23. #include "zlib.h"
  24. #include "zip.h"
  25. #ifdef STDC
  26. # include <stddef.h>
  27. #endif
  28. #ifdef NO_ERRNO_H
  29. extern int errno;
  30. #else
  31. # include <errno.h>
  32. #endif
  33. #ifndef local
  34. # define local static
  35. #endif
  36. /* compile with -Dlocal if your debugger can't find static symbols */
  37. #ifndef VERSIONMADEBY
  38. # define VERSIONMADEBY (0x0) /* platform dependent */
  39. #endif
  40. #ifndef Z_BUFSIZE
  41. #define Z_BUFSIZE (64*1024) //(16384)
  42. #endif
  43. #ifndef Z_MAXFILENAMEINZIP
  44. #define Z_MAXFILENAMEINZIP (256)
  45. #endif
  46. #ifndef ALLOC
  47. # define ALLOC(size) (malloc(size))
  48. #endif
  49. /*
  50. #define SIZECENTRALDIRITEM (0x2e)
  51. #define SIZEZIPLOCALHEADER (0x1e)
  52. */
  53. /* I've found an old Unix (a SunOS 4.1.3_U1) without all SEEK_* defined.... */
  54. // NOT sure that this work on ALL platform
  55. #define MAKEULONG64(a, b) ((ZPOS64_T)(((unsigned long)(a)) | ((ZPOS64_T)((unsigned long)(b))) << 32))
  56. #ifndef SEEK_CUR
  57. #define SEEK_CUR 1
  58. #endif
  59. #ifndef SEEK_END
  60. #define SEEK_END 2
  61. #endif
  62. #ifndef SEEK_SET
  63. #define SEEK_SET 0
  64. #endif
  65. #ifndef DEF_MEM_LEVEL
  66. #if MAX_MEM_LEVEL >= 8
  67. # define DEF_MEM_LEVEL 8
  68. #else
  69. # define DEF_MEM_LEVEL MAX_MEM_LEVEL
  70. #endif
  71. #endif
  72. const char zip_copyright[] =" zip 1.01 Copyright 1998-2004 Gilles Vollant - http://www.winimage.com/zLibDll";
  73. #define SIZEDATA_INDATABLOCK (4096-(4*4))
  74. #define LOCALHEADERMAGIC (0x04034b50)
  75. #define CENTRALHEADERMAGIC (0x02014b50)
  76. #define ENDHEADERMAGIC (0x06054b50)
  77. #define ZIP64ENDHEADERMAGIC (0x6064b50)
  78. #define ZIP64ENDLOCHEADERMAGIC (0x7064b50)
  79. #define FLAG_LOCALHEADER_OFFSET (0x06)
  80. #define CRC_LOCALHEADER_OFFSET (0x0e)
  81. #define SIZECENTRALHEADER (0x2e) /* 46 */
  82. typedef struct linkedlist_datablock_internal_s
  83. {
  84. struct linkedlist_datablock_internal_s* next_datablock;
  85. uLong avail_in_this_block;
  86. uLong filled_in_this_block;
  87. uLong unused; /* for future use and alignment */
  88. unsigned char data[SIZEDATA_INDATABLOCK];
  89. } linkedlist_datablock_internal;
  90. typedef struct linkedlist_data_s
  91. {
  92. linkedlist_datablock_internal* first_block;
  93. linkedlist_datablock_internal* last_block;
  94. } linkedlist_data;
  95. typedef struct
  96. {
  97. z_stream stream; /* zLib stream structure for inflate */
  98. #ifdef HAVE_BZIP2
  99. bz_stream bstream; /* bzLib stream structure for bziped */
  100. #endif
  101. int stream_initialised; /* 1 is stream is initialised */
  102. uInt pos_in_buffered_data; /* last written byte in buffered_data */
  103. ZPOS64_T pos_local_header; /* offset of the local header of the file
  104. currently writing */
  105. char* central_header; /* central header data for the current file */
  106. uLong size_centralExtra;
  107. uLong size_centralheader; /* size of the central header for cur file */
  108. uLong size_centralExtraFree; /* Extra bytes allocated to the centralheader but that are not used */
  109. uLong flag; /* flag of the file currently writing */
  110. int method; /* compression method of file currently wr.*/
  111. int raw; /* 1 for directly writing raw data */
  112. Byte buffered_data[Z_BUFSIZE];/* buffer contain compressed data to be writ*/
  113. uLong dosDate;
  114. uLong crc32;
  115. int encrypt;
  116. int zip64; /* Add ZIP64 extended information in the extra field */
  117. ZPOS64_T pos_zip64extrainfo;
  118. ZPOS64_T totalCompressedData;
  119. ZPOS64_T totalUncompressedData;
  120. #ifndef NOCRYPT
  121. unsigned long keys[3]; /* keys defining the pseudo-random sequence */
  122. const z_crc_t* pcrc_32_tab;
  123. unsigned crypt_header_size;
  124. #endif
  125. } curfile64_info;
  126. typedef struct
  127. {
  128. zlib_filefunc64_32_def z_filefunc;
  129. voidpf filestream; /* io structure of the zipfile */
  130. linkedlist_data central_dir;/* datablock with central dir in construction*/
  131. int in_opened_file_inzip; /* 1 if a file in the zip is currently writ.*/
  132. curfile64_info ci; /* info on the file currently writing */
  133. ZPOS64_T begin_pos; /* position of the beginning of the zipfile */
  134. ZPOS64_T add_position_when_writing_offset;
  135. ZPOS64_T number_entry;
  136. #ifndef NO_ADDFILEINEXISTINGZIP
  137. char *globalcomment;
  138. #endif
  139. } zip64_internal;
  140. #ifndef NOCRYPT
  141. #define INCLUDECRYPTINGCODE_IFCRYPTALLOWED
  142. #include "crypt.h"
  143. #endif
  144. local linkedlist_datablock_internal* allocate_new_datablock(void) {
  145. linkedlist_datablock_internal* ldi;
  146. ldi = (linkedlist_datablock_internal*)
  147. ALLOC(sizeof(linkedlist_datablock_internal));
  148. if (ldi!=NULL)
  149. {
  150. ldi->next_datablock = NULL ;
  151. ldi->filled_in_this_block = 0 ;
  152. ldi->avail_in_this_block = SIZEDATA_INDATABLOCK ;
  153. }
  154. return ldi;
  155. }
  156. local void free_datablock(linkedlist_datablock_internal* ldi) {
  157. while (ldi!=NULL)
  158. {
  159. linkedlist_datablock_internal* ldinext = ldi->next_datablock;
  160. free(ldi);
  161. ldi = ldinext;
  162. }
  163. }
  164. local void init_linkedlist(linkedlist_data* ll) {
  165. ll->first_block = ll->last_block = NULL;
  166. }
  167. local void free_linkedlist(linkedlist_data* ll) {
  168. free_datablock(ll->first_block);
  169. ll->first_block = ll->last_block = NULL;
  170. }
  171. local int add_data_in_datablock(linkedlist_data* ll, const void* buf, uLong len) {
  172. linkedlist_datablock_internal* ldi;
  173. const unsigned char* from_copy;
  174. if (ll==NULL)
  175. return ZIP_INTERNALERROR;
  176. if (ll->last_block == NULL)
  177. {
  178. ll->first_block = ll->last_block = allocate_new_datablock();
  179. if (ll->first_block == NULL)
  180. return ZIP_INTERNALERROR;
  181. }
  182. ldi = ll->last_block;
  183. from_copy = (const unsigned char*)buf;
  184. while (len>0)
  185. {
  186. uInt copy_this;
  187. uInt i;
  188. unsigned char* to_copy;
  189. if (ldi->avail_in_this_block==0)
  190. {
  191. ldi->next_datablock = allocate_new_datablock();
  192. if (ldi->next_datablock == NULL)
  193. return ZIP_INTERNALERROR;
  194. ldi = ldi->next_datablock ;
  195. ll->last_block = ldi;
  196. }
  197. if (ldi->avail_in_this_block < len)
  198. copy_this = (uInt)ldi->avail_in_this_block;
  199. else
  200. copy_this = (uInt)len;
  201. to_copy = &(ldi->data[ldi->filled_in_this_block]);
  202. for (i=0;i<copy_this;i++)
  203. *(to_copy+i)=*(from_copy+i);
  204. ldi->filled_in_this_block += copy_this;
  205. ldi->avail_in_this_block -= copy_this;
  206. from_copy += copy_this ;
  207. len -= copy_this;
  208. }
  209. return ZIP_OK;
  210. }
  211. /****************************************************************************/
  212. #ifndef NO_ADDFILEINEXISTINGZIP
  213. /* ===========================================================================
  214. Inputs a long in LSB order to the given file
  215. nbByte == 1, 2 ,4 or 8 (byte, short or long, ZPOS64_T)
  216. */
  217. local int zip64local_putValue(const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, ZPOS64_T x, int nbByte) {
  218. unsigned char buf[8];
  219. int n;
  220. for (n = 0; n < nbByte; n++)
  221. {
  222. buf[n] = (unsigned char)(x & 0xff);
  223. x >>= 8;
  224. }
  225. if (x != 0)
  226. { /* data overflow - hack for ZIP64 (X Roche) */
  227. for (n = 0; n < nbByte; n++)
  228. {
  229. buf[n] = 0xff;
  230. }
  231. }
  232. if (ZWRITE64(*pzlib_filefunc_def,filestream,buf,(uLong)nbByte)!=(uLong)nbByte)
  233. return ZIP_ERRNO;
  234. else
  235. return ZIP_OK;
  236. }
  237. local void zip64local_putValue_inmemory (void* dest, ZPOS64_T x, int nbByte) {
  238. unsigned char* buf=(unsigned char*)dest;
  239. int n;
  240. for (n = 0; n < nbByte; n++) {
  241. buf[n] = (unsigned char)(x & 0xff);
  242. x >>= 8;
  243. }
  244. if (x != 0)
  245. { /* data overflow - hack for ZIP64 */
  246. for (n = 0; n < nbByte; n++)
  247. {
  248. buf[n] = 0xff;
  249. }
  250. }
  251. }
  252. /****************************************************************************/
  253. local uLong zip64local_TmzDateToDosDate(const tm_zip* ptm) {
  254. uLong year = (uLong)ptm->tm_year;
  255. if (year>=1980)
  256. year-=1980;
  257. else if (year>=80)
  258. year-=80;
  259. return
  260. (uLong) (((uLong)(ptm->tm_mday) + (32 * (uLong)(ptm->tm_mon+1)) + (512 * year)) << 16) |
  261. (((uLong)ptm->tm_sec/2) + (32 * (uLong)ptm->tm_min) + (2048 * (uLong)ptm->tm_hour));
  262. }
  263. /****************************************************************************/
  264. local int zip64local_getByte(const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, int* pi) {
  265. unsigned char c;
  266. int err = (int)ZREAD64(*pzlib_filefunc_def,filestream,&c,1);
  267. if (err==1)
  268. {
  269. *pi = (int)c;
  270. return ZIP_OK;
  271. }
  272. else
  273. {
  274. if (ZERROR64(*pzlib_filefunc_def,filestream))
  275. return ZIP_ERRNO;
  276. else
  277. return ZIP_EOF;
  278. }
  279. }
  280. /* ===========================================================================
  281. Reads a long in LSB order from the given gz_stream. Sets
  282. */
  283. local int zip64local_getShort(const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, uLong* pX) {
  284. uLong x ;
  285. int i = 0;
  286. int err;
  287. err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
  288. x = (uLong)i;
  289. if (err==ZIP_OK)
  290. err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
  291. x += ((uLong)i)<<8;
  292. if (err==ZIP_OK)
  293. *pX = x;
  294. else
  295. *pX = 0;
  296. return err;
  297. }
  298. local int zip64local_getLong(const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, uLong* pX) {
  299. uLong x ;
  300. int i = 0;
  301. int err;
  302. err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
  303. x = (uLong)i;
  304. if (err==ZIP_OK)
  305. err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
  306. x += ((uLong)i)<<8;
  307. if (err==ZIP_OK)
  308. err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
  309. x += ((uLong)i)<<16;
  310. if (err==ZIP_OK)
  311. err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
  312. x += ((uLong)i)<<24;
  313. if (err==ZIP_OK)
  314. *pX = x;
  315. else
  316. *pX = 0;
  317. return err;
  318. }
  319. local int zip64local_getLong64(const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream, ZPOS64_T *pX) {
  320. ZPOS64_T x;
  321. int i = 0;
  322. int err;
  323. err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
  324. x = (ZPOS64_T)i;
  325. if (err==ZIP_OK)
  326. err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
  327. x += ((ZPOS64_T)i)<<8;
  328. if (err==ZIP_OK)
  329. err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
  330. x += ((ZPOS64_T)i)<<16;
  331. if (err==ZIP_OK)
  332. err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
  333. x += ((ZPOS64_T)i)<<24;
  334. if (err==ZIP_OK)
  335. err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
  336. x += ((ZPOS64_T)i)<<32;
  337. if (err==ZIP_OK)
  338. err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
  339. x += ((ZPOS64_T)i)<<40;
  340. if (err==ZIP_OK)
  341. err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
  342. x += ((ZPOS64_T)i)<<48;
  343. if (err==ZIP_OK)
  344. err = zip64local_getByte(pzlib_filefunc_def,filestream,&i);
  345. x += ((ZPOS64_T)i)<<56;
  346. if (err==ZIP_OK)
  347. *pX = x;
  348. else
  349. *pX = 0;
  350. return err;
  351. }
  352. #ifndef BUFREADCOMMENT
  353. #define BUFREADCOMMENT (0x400)
  354. #endif
  355. /*
  356. Locate the Central directory of a zipfile (at the end, just before
  357. the global comment)
  358. */
  359. local ZPOS64_T zip64local_SearchCentralDir(const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream) {
  360. unsigned char* buf;
  361. ZPOS64_T uSizeFile;
  362. ZPOS64_T uBackRead;
  363. ZPOS64_T uMaxBack=0xffff; /* maximum size of global comment */
  364. ZPOS64_T uPosFound=0;
  365. if (ZSEEK64(*pzlib_filefunc_def,filestream,0,ZLIB_FILEFUNC_SEEK_END) != 0)
  366. return 0;
  367. uSizeFile = ZTELL64(*pzlib_filefunc_def,filestream);
  368. if (uMaxBack>uSizeFile)
  369. uMaxBack = uSizeFile;
  370. buf = (unsigned char*)ALLOC(BUFREADCOMMENT+4);
  371. if (buf==NULL)
  372. return 0;
  373. uBackRead = 4;
  374. while (uBackRead<uMaxBack)
  375. {
  376. uLong uReadSize;
  377. ZPOS64_T uReadPos ;
  378. int i;
  379. if (uBackRead+BUFREADCOMMENT>uMaxBack)
  380. uBackRead = uMaxBack;
  381. else
  382. uBackRead+=BUFREADCOMMENT;
  383. uReadPos = uSizeFile-uBackRead ;
  384. uReadSize = ((BUFREADCOMMENT+4) < (uSizeFile-uReadPos)) ?
  385. (BUFREADCOMMENT+4) : (uLong)(uSizeFile-uReadPos);
  386. if (ZSEEK64(*pzlib_filefunc_def,filestream,uReadPos,ZLIB_FILEFUNC_SEEK_SET)!=0)
  387. break;
  388. if (ZREAD64(*pzlib_filefunc_def,filestream,buf,uReadSize)!=uReadSize)
  389. break;
  390. for (i=(int)uReadSize-3; (i--)>0;)
  391. if (((*(buf+i))==0x50) && ((*(buf+i+1))==0x4b) &&
  392. ((*(buf+i+2))==0x05) && ((*(buf+i+3))==0x06))
  393. {
  394. uPosFound = uReadPos+(unsigned)i;
  395. break;
  396. }
  397. if (uPosFound!=0)
  398. break;
  399. }
  400. free(buf);
  401. return uPosFound;
  402. }
  403. /*
  404. Locate the End of Zip64 Central directory locator and from there find the CD of a zipfile (at the end, just before
  405. the global comment)
  406. */
  407. local ZPOS64_T zip64local_SearchCentralDir64(const zlib_filefunc64_32_def* pzlib_filefunc_def, voidpf filestream) {
  408. unsigned char* buf;
  409. ZPOS64_T uSizeFile;
  410. ZPOS64_T uBackRead;
  411. ZPOS64_T uMaxBack=0xffff; /* maximum size of global comment */
  412. ZPOS64_T uPosFound=0;
  413. uLong uL;
  414. ZPOS64_T relativeOffset;
  415. if (ZSEEK64(*pzlib_filefunc_def,filestream,0,ZLIB_FILEFUNC_SEEK_END) != 0)
  416. return 0;
  417. uSizeFile = ZTELL64(*pzlib_filefunc_def,filestream);
  418. if (uMaxBack>uSizeFile)
  419. uMaxBack = uSizeFile;
  420. buf = (unsigned char*)ALLOC(BUFREADCOMMENT+4);
  421. if (buf==NULL)
  422. return 0;
  423. uBackRead = 4;
  424. while (uBackRead<uMaxBack)
  425. {
  426. uLong uReadSize;
  427. ZPOS64_T uReadPos;
  428. int i;
  429. if (uBackRead+BUFREADCOMMENT>uMaxBack)
  430. uBackRead = uMaxBack;
  431. else
  432. uBackRead+=BUFREADCOMMENT;
  433. uReadPos = uSizeFile-uBackRead ;
  434. uReadSize = ((BUFREADCOMMENT+4) < (uSizeFile-uReadPos)) ?
  435. (BUFREADCOMMENT+4) : (uLong)(uSizeFile-uReadPos);
  436. if (ZSEEK64(*pzlib_filefunc_def,filestream,uReadPos,ZLIB_FILEFUNC_SEEK_SET)!=0)
  437. break;
  438. if (ZREAD64(*pzlib_filefunc_def,filestream,buf,uReadSize)!=uReadSize)
  439. break;
  440. for (i=(int)uReadSize-3; (i--)>0;)
  441. {
  442. // Signature "0x07064b50" Zip64 end of central directory locater
  443. if (((*(buf+i))==0x50) && ((*(buf+i+1))==0x4b) && ((*(buf+i+2))==0x06) && ((*(buf+i+3))==0x07))
  444. {
  445. uPosFound = uReadPos+(unsigned)i;
  446. break;
  447. }
  448. }
  449. if (uPosFound!=0)
  450. break;
  451. }
  452. free(buf);
  453. if (uPosFound == 0)
  454. return 0;
  455. /* Zip64 end of central directory locator */
  456. if (ZSEEK64(*pzlib_filefunc_def,filestream, uPosFound,ZLIB_FILEFUNC_SEEK_SET)!=0)
  457. return 0;
  458. /* the signature, already checked */
  459. if (zip64local_getLong(pzlib_filefunc_def,filestream,&uL)!=ZIP_OK)
  460. return 0;
  461. /* number of the disk with the start of the zip64 end of central directory */
  462. if (zip64local_getLong(pzlib_filefunc_def,filestream,&uL)!=ZIP_OK)
  463. return 0;
  464. if (uL != 0)
  465. return 0;
  466. /* relative offset of the zip64 end of central directory record */
  467. if (zip64local_getLong64(pzlib_filefunc_def,filestream,&relativeOffset)!=ZIP_OK)
  468. return 0;
  469. /* total number of disks */
  470. if (zip64local_getLong(pzlib_filefunc_def,filestream,&uL)!=ZIP_OK)
  471. return 0;
  472. if (uL != 1)
  473. return 0;
  474. /* Goto Zip64 end of central directory record */
  475. if (ZSEEK64(*pzlib_filefunc_def,filestream, relativeOffset,ZLIB_FILEFUNC_SEEK_SET)!=0)
  476. return 0;
  477. /* the signature */
  478. if (zip64local_getLong(pzlib_filefunc_def,filestream,&uL)!=ZIP_OK)
  479. return 0;
  480. if (uL != 0x06064b50) // signature of 'Zip64 end of central directory'
  481. return 0;
  482. return relativeOffset;
  483. }
  484. local int LoadCentralDirectoryRecord(zip64_internal* pziinit) {
  485. int err=ZIP_OK;
  486. ZPOS64_T byte_before_the_zipfile;/* byte before the zipfile, (>0 for sfx)*/
  487. ZPOS64_T size_central_dir; /* size of the central directory */
  488. ZPOS64_T offset_central_dir; /* offset of start of central directory */
  489. ZPOS64_T central_pos;
  490. uLong uL;
  491. uLong number_disk; /* number of the current disk, used for
  492. spanning ZIP, unsupported, always 0*/
  493. uLong number_disk_with_CD; /* number of the disk with central dir, used
  494. for spanning ZIP, unsupported, always 0*/
  495. ZPOS64_T number_entry;
  496. ZPOS64_T number_entry_CD; /* total number of entries in
  497. the central dir
  498. (same than number_entry on nospan) */
  499. uLong VersionMadeBy;
  500. uLong VersionNeeded;
  501. uLong size_comment;
  502. int hasZIP64Record = 0;
  503. // check first if we find a ZIP64 record
  504. central_pos = zip64local_SearchCentralDir64(&pziinit->z_filefunc,pziinit->filestream);
  505. if(central_pos > 0)
  506. {
  507. hasZIP64Record = 1;
  508. }
  509. else if(central_pos == 0)
  510. {
  511. central_pos = zip64local_SearchCentralDir(&pziinit->z_filefunc,pziinit->filestream);
  512. }
  513. /* disable to allow appending to empty ZIP archive
  514. if (central_pos==0)
  515. err=ZIP_ERRNO;
  516. */
  517. if(hasZIP64Record)
  518. {
  519. ZPOS64_T sizeEndOfCentralDirectory;
  520. if (ZSEEK64(pziinit->z_filefunc, pziinit->filestream, central_pos, ZLIB_FILEFUNC_SEEK_SET) != 0)
  521. err=ZIP_ERRNO;
  522. /* the signature, already checked */
  523. if (zip64local_getLong(&pziinit->z_filefunc, pziinit->filestream,&uL)!=ZIP_OK)
  524. err=ZIP_ERRNO;
  525. /* size of zip64 end of central directory record */
  526. if (zip64local_getLong64(&pziinit->z_filefunc, pziinit->filestream, &sizeEndOfCentralDirectory)!=ZIP_OK)
  527. err=ZIP_ERRNO;
  528. /* version made by */
  529. if (zip64local_getShort(&pziinit->z_filefunc, pziinit->filestream, &VersionMadeBy)!=ZIP_OK)
  530. err=ZIP_ERRNO;
  531. /* version needed to extract */
  532. if (zip64local_getShort(&pziinit->z_filefunc, pziinit->filestream, &VersionNeeded)!=ZIP_OK)
  533. err=ZIP_ERRNO;
  534. /* number of this disk */
  535. if (zip64local_getLong(&pziinit->z_filefunc, pziinit->filestream,&number_disk)!=ZIP_OK)
  536. err=ZIP_ERRNO;
  537. /* number of the disk with the start of the central directory */
  538. if (zip64local_getLong(&pziinit->z_filefunc, pziinit->filestream,&number_disk_with_CD)!=ZIP_OK)
  539. err=ZIP_ERRNO;
  540. /* total number of entries in the central directory on this disk */
  541. if (zip64local_getLong64(&pziinit->z_filefunc, pziinit->filestream, &number_entry)!=ZIP_OK)
  542. err=ZIP_ERRNO;
  543. /* total number of entries in the central directory */
  544. if (zip64local_getLong64(&pziinit->z_filefunc, pziinit->filestream,&number_entry_CD)!=ZIP_OK)
  545. err=ZIP_ERRNO;
  546. if ((number_entry_CD!=number_entry) || (number_disk_with_CD!=0) || (number_disk!=0))
  547. err=ZIP_BADZIPFILE;
  548. /* size of the central directory */
  549. if (zip64local_getLong64(&pziinit->z_filefunc, pziinit->filestream,&size_central_dir)!=ZIP_OK)
  550. err=ZIP_ERRNO;
  551. /* offset of start of central directory with respect to the
  552. starting disk number */
  553. if (zip64local_getLong64(&pziinit->z_filefunc, pziinit->filestream,&offset_central_dir)!=ZIP_OK)
  554. err=ZIP_ERRNO;
  555. // TODO..
  556. // read the comment from the standard central header.
  557. size_comment = 0;
  558. }
  559. else
  560. {
  561. // Read End of central Directory info
  562. if (ZSEEK64(pziinit->z_filefunc, pziinit->filestream, central_pos,ZLIB_FILEFUNC_SEEK_SET)!=0)
  563. err=ZIP_ERRNO;
  564. /* the signature, already checked */
  565. if (zip64local_getLong(&pziinit->z_filefunc, pziinit->filestream,&uL)!=ZIP_OK)
  566. err=ZIP_ERRNO;
  567. /* number of this disk */
  568. if (zip64local_getShort(&pziinit->z_filefunc, pziinit->filestream,&number_disk)!=ZIP_OK)
  569. err=ZIP_ERRNO;
  570. /* number of the disk with the start of the central directory */
  571. if (zip64local_getShort(&pziinit->z_filefunc, pziinit->filestream,&number_disk_with_CD)!=ZIP_OK)
  572. err=ZIP_ERRNO;
  573. /* total number of entries in the central dir on this disk */
  574. number_entry = 0;
  575. if (zip64local_getShort(&pziinit->z_filefunc, pziinit->filestream, &uL)!=ZIP_OK)
  576. err=ZIP_ERRNO;
  577. else
  578. number_entry = uL;
  579. /* total number of entries in the central dir */
  580. number_entry_CD = 0;
  581. if (zip64local_getShort(&pziinit->z_filefunc, pziinit->filestream, &uL)!=ZIP_OK)
  582. err=ZIP_ERRNO;
  583. else
  584. number_entry_CD = uL;
  585. if ((number_entry_CD!=number_entry) || (number_disk_with_CD!=0) || (number_disk!=0))
  586. err=ZIP_BADZIPFILE;
  587. /* size of the central directory */
  588. size_central_dir = 0;
  589. if (zip64local_getLong(&pziinit->z_filefunc, pziinit->filestream, &uL)!=ZIP_OK)
  590. err=ZIP_ERRNO;
  591. else
  592. size_central_dir = uL;
  593. /* offset of start of central directory with respect to the starting disk number */
  594. offset_central_dir = 0;
  595. if (zip64local_getLong(&pziinit->z_filefunc, pziinit->filestream, &uL)!=ZIP_OK)
  596. err=ZIP_ERRNO;
  597. else
  598. offset_central_dir = uL;
  599. /* zipfile global comment length */
  600. if (zip64local_getShort(&pziinit->z_filefunc, pziinit->filestream, &size_comment)!=ZIP_OK)
  601. err=ZIP_ERRNO;
  602. }
  603. if ((central_pos<offset_central_dir+size_central_dir) &&
  604. (err==ZIP_OK))
  605. err=ZIP_BADZIPFILE;
  606. if (err!=ZIP_OK)
  607. {
  608. ZCLOSE64(pziinit->z_filefunc, pziinit->filestream);
  609. return ZIP_ERRNO;
  610. }
  611. if (size_comment>0)
  612. {
  613. pziinit->globalcomment = (char*)ALLOC(size_comment+1);
  614. if (pziinit->globalcomment)
  615. {
  616. size_comment = ZREAD64(pziinit->z_filefunc, pziinit->filestream, pziinit->globalcomment,size_comment);
  617. pziinit->globalcomment[size_comment]=0;
  618. }
  619. }
  620. byte_before_the_zipfile = central_pos - (offset_central_dir+size_central_dir);
  621. pziinit->add_position_when_writing_offset = byte_before_the_zipfile;
  622. {
  623. ZPOS64_T size_central_dir_to_read = size_central_dir;
  624. size_t buf_size = SIZEDATA_INDATABLOCK;
  625. void* buf_read = (void*)ALLOC(buf_size);
  626. if (ZSEEK64(pziinit->z_filefunc, pziinit->filestream, offset_central_dir + byte_before_the_zipfile, ZLIB_FILEFUNC_SEEK_SET) != 0)
  627. err=ZIP_ERRNO;
  628. while ((size_central_dir_to_read>0) && (err==ZIP_OK))
  629. {
  630. ZPOS64_T read_this = SIZEDATA_INDATABLOCK;
  631. if (read_this > size_central_dir_to_read)
  632. read_this = size_central_dir_to_read;
  633. if (ZREAD64(pziinit->z_filefunc, pziinit->filestream,buf_read,(uLong)read_this) != read_this)
  634. err=ZIP_ERRNO;
  635. if (err==ZIP_OK)
  636. err = add_data_in_datablock(&pziinit->central_dir,buf_read, (uLong)read_this);
  637. size_central_dir_to_read-=read_this;
  638. }
  639. free(buf_read);
  640. }
  641. pziinit->begin_pos = byte_before_the_zipfile;
  642. pziinit->number_entry = number_entry_CD;
  643. if (ZSEEK64(pziinit->z_filefunc, pziinit->filestream, offset_central_dir+byte_before_the_zipfile,ZLIB_FILEFUNC_SEEK_SET) != 0)
  644. err=ZIP_ERRNO;
  645. return err;
  646. }
  647. #endif /* !NO_ADDFILEINEXISTINGZIP*/
  648. /************************************************************/
  649. extern zipFile ZEXPORT zipOpen3(const void *pathname, int append, zipcharpc* globalcomment, zlib_filefunc64_32_def* pzlib_filefunc64_32_def) {
  650. zip64_internal ziinit;
  651. zip64_internal* zi;
  652. int err=ZIP_OK;
  653. ziinit.z_filefunc.zseek32_file = NULL;
  654. ziinit.z_filefunc.ztell32_file = NULL;
  655. if (pzlib_filefunc64_32_def==NULL) {
  656. //fill_fopen64_filefunc(&ziinit.z_filefunc.zfile_func64);
  657. } else
  658. ziinit.z_filefunc = *pzlib_filefunc64_32_def;
  659. ziinit.filestream = ZOPEN64(ziinit.z_filefunc,
  660. pathname,
  661. (append == APPEND_STATUS_CREATE) ?
  662. (ZLIB_FILEFUNC_MODE_READ | ZLIB_FILEFUNC_MODE_WRITE | ZLIB_FILEFUNC_MODE_CREATE) :
  663. (ZLIB_FILEFUNC_MODE_READ | ZLIB_FILEFUNC_MODE_WRITE | ZLIB_FILEFUNC_MODE_EXISTING));
  664. if (ziinit.filestream == NULL)
  665. return NULL;
  666. if (append == APPEND_STATUS_CREATEAFTER)
  667. ZSEEK64(ziinit.z_filefunc,ziinit.filestream,0,SEEK_END);
  668. ziinit.begin_pos = ZTELL64(ziinit.z_filefunc,ziinit.filestream);
  669. ziinit.in_opened_file_inzip = 0;
  670. ziinit.ci.stream_initialised = 0;
  671. ziinit.number_entry = 0;
  672. ziinit.add_position_when_writing_offset = 0;
  673. init_linkedlist(&(ziinit.central_dir));
  674. zi = (zip64_internal*)ALLOC(sizeof(zip64_internal));
  675. if (zi==NULL)
  676. {
  677. ZCLOSE64(ziinit.z_filefunc,ziinit.filestream);
  678. return NULL;
  679. }
  680. /* now we add file in a zipfile */
  681. # ifndef NO_ADDFILEINEXISTINGZIP
  682. ziinit.globalcomment = NULL;
  683. if (append == APPEND_STATUS_ADDINZIP)
  684. {
  685. // Read and Cache Central Directory Records
  686. err = LoadCentralDirectoryRecord(&ziinit);
  687. }
  688. if (globalcomment)
  689. {
  690. *globalcomment = ziinit.globalcomment;
  691. }
  692. # endif /* !NO_ADDFILEINEXISTINGZIP*/
  693. if (err != ZIP_OK)
  694. {
  695. # ifndef NO_ADDFILEINEXISTINGZIP
  696. free(ziinit.globalcomment);
  697. # endif /* !NO_ADDFILEINEXISTINGZIP*/
  698. free(zi);
  699. return NULL;
  700. }
  701. else
  702. {
  703. *zi = ziinit;
  704. return (zipFile)zi;
  705. }
  706. }
  707. extern zipFile ZEXPORT zipOpen2(const char *pathname, int append, zipcharpc* globalcomment, zlib_filefunc_def* pzlib_filefunc32_def) {
  708. if (pzlib_filefunc32_def != NULL)
  709. {
  710. zlib_filefunc64_32_def zlib_filefunc64_32_def_fill;
  711. fill_zlib_filefunc64_32_def_from_filefunc32(&zlib_filefunc64_32_def_fill,pzlib_filefunc32_def);
  712. return zipOpen3(pathname, append, globalcomment, &zlib_filefunc64_32_def_fill);
  713. }
  714. else
  715. return zipOpen3(pathname, append, globalcomment, NULL);
  716. }
  717. extern zipFile ZEXPORT zipOpen2_64(const void *pathname, int append, zipcharpc* globalcomment, zlib_filefunc64_def* pzlib_filefunc_def) {
  718. if (pzlib_filefunc_def != NULL)
  719. {
  720. zlib_filefunc64_32_def zlib_filefunc64_32_def_fill;
  721. zlib_filefunc64_32_def_fill.zfile_func64 = *pzlib_filefunc_def;
  722. zlib_filefunc64_32_def_fill.ztell32_file = NULL;
  723. zlib_filefunc64_32_def_fill.zseek32_file = NULL;
  724. return zipOpen3(pathname, append, globalcomment, &zlib_filefunc64_32_def_fill);
  725. }
  726. else
  727. return zipOpen3(pathname, append, globalcomment, NULL);
  728. }
  729. extern zipFile ZEXPORT zipOpen(const char* pathname, int append) {
  730. return zipOpen3((const void*)pathname,append,NULL,NULL);
  731. }
  732. extern zipFile ZEXPORT zipOpen64(const void* pathname, int append) {
  733. return zipOpen3(pathname,append,NULL,NULL);
  734. }
  735. local int Write_LocalFileHeader(zip64_internal* zi, const char* filename, uInt size_extrafield_local, const void* extrafield_local) {
  736. /* write the local header */
  737. int err;
  738. uInt size_filename = (uInt)strlen(filename);
  739. uInt size_extrafield = size_extrafield_local;
  740. err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)LOCALHEADERMAGIC, 4);
  741. if (err==ZIP_OK)
  742. {
  743. if(zi->ci.zip64)
  744. err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)45,2);/* version needed to extract */
  745. else
  746. err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)20,2);/* version needed to extract */
  747. }
  748. if (err==ZIP_OK)
  749. err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->ci.flag,2);
  750. if (err==ZIP_OK)
  751. err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->ci.method,2);
  752. if (err==ZIP_OK)
  753. err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->ci.dosDate,4);
  754. // CRC / Compressed size / Uncompressed size will be filled in later and rewritten later
  755. if (err==ZIP_OK)
  756. err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4); /* crc 32, unknown */
  757. if (err==ZIP_OK)
  758. {
  759. if(zi->ci.zip64)
  760. err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0xFFFFFFFF,4); /* compressed size, unknown */
  761. else
  762. err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4); /* compressed size, unknown */
  763. }
  764. if (err==ZIP_OK)
  765. {
  766. if(zi->ci.zip64)
  767. err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0xFFFFFFFF,4); /* uncompressed size, unknown */
  768. else
  769. err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4); /* uncompressed size, unknown */
  770. }
  771. if (err==ZIP_OK)
  772. err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)size_filename,2);
  773. if(zi->ci.zip64)
  774. {
  775. size_extrafield += 20;
  776. }
  777. if (err==ZIP_OK)
  778. err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)size_extrafield,2);
  779. if ((err==ZIP_OK) && (size_filename > 0))
  780. {
  781. if (ZWRITE64(zi->z_filefunc,zi->filestream,filename,size_filename)!=size_filename)
  782. err = ZIP_ERRNO;
  783. }
  784. if ((err==ZIP_OK) && (size_extrafield_local > 0))
  785. {
  786. if (ZWRITE64(zi->z_filefunc, zi->filestream, extrafield_local, size_extrafield_local) != size_extrafield_local)
  787. err = ZIP_ERRNO;
  788. }
  789. if ((err==ZIP_OK) && (zi->ci.zip64))
  790. {
  791. // write the Zip64 extended info
  792. short HeaderID = 1;
  793. short DataSize = 16;
  794. ZPOS64_T CompressedSize = 0;
  795. ZPOS64_T UncompressedSize = 0;
  796. // Remember position of Zip64 extended info for the local file header. (needed when we update size after done with file)
  797. zi->ci.pos_zip64extrainfo = ZTELL64(zi->z_filefunc,zi->filestream);
  798. err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (ZPOS64_T)HeaderID,2);
  799. err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (ZPOS64_T)DataSize,2);
  800. err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (ZPOS64_T)UncompressedSize,8);
  801. err = zip64local_putValue(&zi->z_filefunc, zi->filestream, (ZPOS64_T)CompressedSize,8);
  802. }
  803. return err;
  804. }
  805. /*
  806. NOTE.
  807. When writing RAW the ZIP64 extended information in extrafield_local and extrafield_global needs to be stripped
  808. before calling this function it can be done with zipRemoveExtraInfoBlock
  809. It is not done here because then we need to realloc a new buffer since parameters are 'const' and I want to minimize
  810. unnecessary allocations.
  811. */
  812. extern int ZEXPORT zipOpenNewFileInZip4_64(zipFile file, const char* filename, const zip_fileinfo* zipfi,
  813. const void* extrafield_local, uInt size_extrafield_local,
  814. const void* extrafield_global, uInt size_extrafield_global,
  815. const char* comment, int method, int level, int raw,
  816. int windowBits,int memLevel, int strategy,
  817. const char* password, uLong crcForCrypting,
  818. uLong versionMadeBy, uLong flagBase, int zip64) {
  819. zip64_internal* zi;
  820. uInt size_filename;
  821. uInt size_comment;
  822. uInt i;
  823. int err = ZIP_OK;
  824. # ifdef NOCRYPT
  825. (crcForCrypting);
  826. if (password != NULL)
  827. return ZIP_PARAMERROR;
  828. # endif
  829. if (file == NULL)
  830. return ZIP_PARAMERROR;
  831. #ifdef HAVE_BZIP2
  832. if ((method!=0) && (method!=Z_DEFLATED) && (method!=Z_BZIP2ED))
  833. return ZIP_PARAMERROR;
  834. #else
  835. if ((method!=0) && (method!=Z_DEFLATED))
  836. return ZIP_PARAMERROR;
  837. #endif
  838. // The filename and comment length must fit in 16 bits.
  839. if ((filename!=NULL) && (strlen(filename)>0xffff))
  840. return ZIP_PARAMERROR;
  841. if ((comment!=NULL) && (strlen(comment)>0xffff))
  842. return ZIP_PARAMERROR;
  843. // The extra field length must fit in 16 bits. If the member also requires
  844. // a Zip64 extra block, that will also need to fit within that 16-bit
  845. // length, but that will be checked for later.
  846. if ((size_extrafield_local>0xffff) || (size_extrafield_global>0xffff))
  847. return ZIP_PARAMERROR;
  848. zi = (zip64_internal*)file;
  849. if (zi->in_opened_file_inzip == 1)
  850. {
  851. err = zipCloseFileInZip (file);
  852. if (err != ZIP_OK)
  853. return err;
  854. }
  855. if (filename==NULL)
  856. filename="-";
  857. if (comment==NULL)
  858. size_comment = 0;
  859. else
  860. size_comment = (uInt)strlen(comment);
  861. size_filename = (uInt)strlen(filename);
  862. if (zipfi == NULL)
  863. zi->ci.dosDate = 0;
  864. else
  865. {
  866. if (zipfi->dosDate != 0)
  867. zi->ci.dosDate = zipfi->dosDate;
  868. else
  869. zi->ci.dosDate = zip64local_TmzDateToDosDate(&zipfi->tmz_date);
  870. }
  871. zi->ci.flag = flagBase;
  872. if ((level==8) || (level==9))
  873. zi->ci.flag |= 2;
  874. if (level==2)
  875. zi->ci.flag |= 4;
  876. if (level==1)
  877. zi->ci.flag |= 6;
  878. if (password != NULL)
  879. zi->ci.flag |= 1;
  880. zi->ci.crc32 = 0;
  881. zi->ci.method = method;
  882. zi->ci.encrypt = 0;
  883. zi->ci.stream_initialised = 0;
  884. zi->ci.pos_in_buffered_data = 0;
  885. zi->ci.raw = raw;
  886. zi->ci.pos_local_header = ZTELL64(zi->z_filefunc,zi->filestream);
  887. zi->ci.size_centralheader = SIZECENTRALHEADER + size_filename + size_extrafield_global + size_comment;
  888. zi->ci.size_centralExtraFree = 32; // Extra space we have reserved in case we need to add ZIP64 extra info data
  889. zi->ci.central_header = (char*)ALLOC((uInt)zi->ci.size_centralheader + zi->ci.size_centralExtraFree);
  890. zi->ci.size_centralExtra = size_extrafield_global;
  891. zip64local_putValue_inmemory(zi->ci.central_header,(uLong)CENTRALHEADERMAGIC,4);
  892. /* version info */
  893. zip64local_putValue_inmemory(zi->ci.central_header+4,(uLong)versionMadeBy,2);
  894. zip64local_putValue_inmemory(zi->ci.central_header+6,(uLong)20,2);
  895. zip64local_putValue_inmemory(zi->ci.central_header+8,(uLong)zi->ci.flag,2);
  896. zip64local_putValue_inmemory(zi->ci.central_header+10,(uLong)zi->ci.method,2);
  897. zip64local_putValue_inmemory(zi->ci.central_header+12,(uLong)zi->ci.dosDate,4);
  898. zip64local_putValue_inmemory(zi->ci.central_header+16,(uLong)0,4); /*crc*/
  899. zip64local_putValue_inmemory(zi->ci.central_header+20,(uLong)0,4); /*compr size*/
  900. zip64local_putValue_inmemory(zi->ci.central_header+24,(uLong)0,4); /*uncompr size*/
  901. zip64local_putValue_inmemory(zi->ci.central_header+28,(uLong)size_filename,2);
  902. zip64local_putValue_inmemory(zi->ci.central_header+30,(uLong)size_extrafield_global,2);
  903. zip64local_putValue_inmemory(zi->ci.central_header+32,(uLong)size_comment,2);
  904. zip64local_putValue_inmemory(zi->ci.central_header+34,(uLong)0,2); /*disk nm start*/
  905. if (zipfi==NULL)
  906. zip64local_putValue_inmemory(zi->ci.central_header+36,(uLong)0,2);
  907. else
  908. zip64local_putValue_inmemory(zi->ci.central_header+36,(uLong)zipfi->internal_fa,2);
  909. if (zipfi==NULL)
  910. zip64local_putValue_inmemory(zi->ci.central_header+38,(uLong)0,4);
  911. else
  912. zip64local_putValue_inmemory(zi->ci.central_header+38,(uLong)zipfi->external_fa,4);
  913. if(zi->ci.pos_local_header >= 0xffffffff)
  914. zip64local_putValue_inmemory(zi->ci.central_header+42,(uLong)0xffffffff,4);
  915. else
  916. zip64local_putValue_inmemory(zi->ci.central_header+42,(uLong)zi->ci.pos_local_header - zi->add_position_when_writing_offset,4);
  917. for (i=0;i<size_filename;i++)
  918. *(zi->ci.central_header+SIZECENTRALHEADER+i) = *(filename+i);
  919. for (i=0;i<size_extrafield_global;i++)
  920. *(zi->ci.central_header+SIZECENTRALHEADER+size_filename+i) =
  921. *(((const char*)extrafield_global)+i);
  922. for (i=0;i<size_comment;i++)
  923. *(zi->ci.central_header+SIZECENTRALHEADER+size_filename+
  924. size_extrafield_global+i) = *(comment+i);
  925. if (zi->ci.central_header == NULL)
  926. return ZIP_INTERNALERROR;
  927. zi->ci.zip64 = zip64;
  928. zi->ci.totalCompressedData = 0;
  929. zi->ci.totalUncompressedData = 0;
  930. zi->ci.pos_zip64extrainfo = 0;
  931. err = Write_LocalFileHeader(zi, filename, size_extrafield_local, extrafield_local);
  932. #ifdef HAVE_BZIP2
  933. zi->ci.bstream.avail_in = (uInt)0;
  934. zi->ci.bstream.avail_out = (uInt)Z_BUFSIZE;
  935. zi->ci.bstream.next_out = (char*)zi->ci.buffered_data;
  936. zi->ci.bstream.total_in_hi32 = 0;
  937. zi->ci.bstream.total_in_lo32 = 0;
  938. zi->ci.bstream.total_out_hi32 = 0;
  939. zi->ci.bstream.total_out_lo32 = 0;
  940. #endif
  941. zi->ci.stream.avail_in = (uInt)0;
  942. zi->ci.stream.avail_out = (uInt)Z_BUFSIZE;
  943. zi->ci.stream.next_out = zi->ci.buffered_data;
  944. zi->ci.stream.total_in = 0;
  945. zi->ci.stream.total_out = 0;
  946. zi->ci.stream.data_type = Z_BINARY;
  947. #ifdef HAVE_BZIP2
  948. if ((err==ZIP_OK) && (zi->ci.method == Z_DEFLATED || zi->ci.method == Z_BZIP2ED) && (!zi->ci.raw))
  949. #else
  950. if ((err==ZIP_OK) && (zi->ci.method == Z_DEFLATED) && (!zi->ci.raw))
  951. #endif
  952. {
  953. if(zi->ci.method == Z_DEFLATED)
  954. {
  955. zi->ci.stream.zalloc = zi->z_filefunc.zfile_func64.alloc_mem;
  956. zi->ci.stream.zfree = zi->z_filefunc.zfile_func64.free_mem;
  957. zi->ci.stream.opaque = (voidpf)0;
  958. if (windowBits>0)
  959. windowBits = -windowBits;
  960. err = deflateInit2(&zi->ci.stream, level, Z_DEFLATED, windowBits, memLevel, strategy);
  961. if (err==Z_OK)
  962. zi->ci.stream_initialised = Z_DEFLATED;
  963. }
  964. else if(zi->ci.method == Z_BZIP2ED)
  965. {
  966. #ifdef HAVE_BZIP2
  967. // Init BZip stuff here
  968. zi->ci.bstream.bzalloc = 0;
  969. zi->ci.bstream.bzfree = 0;
  970. zi->ci.bstream.opaque = (voidpf)0;
  971. err = BZ2_bzCompressInit(&zi->ci.bstream, level, 0,35);
  972. if(err == BZ_OK)
  973. zi->ci.stream_initialised = Z_BZIP2ED;
  974. #endif
  975. }
  976. }
  977. # ifndef NOCRYPT
  978. zi->ci.crypt_header_size = 0;
  979. if ((err==Z_OK) && (password != NULL))
  980. {
  981. unsigned char bufHead[RAND_HEAD_LEN];
  982. unsigned int sizeHead;
  983. zi->ci.encrypt = 1;
  984. zi->ci.pcrc_32_tab = get_crc_table();
  985. /*init_keys(password,zi->ci.keys,zi->ci.pcrc_32_tab);*/
  986. sizeHead=crypthead(password,bufHead,RAND_HEAD_LEN,zi->ci.keys,zi->ci.pcrc_32_tab,crcForCrypting);
  987. zi->ci.crypt_header_size = sizeHead;
  988. if (ZWRITE64(zi->z_filefunc,zi->filestream,bufHead,sizeHead) != sizeHead)
  989. err = ZIP_ERRNO;
  990. }
  991. # endif
  992. if (err==Z_OK)
  993. zi->in_opened_file_inzip = 1;
  994. return err;
  995. }
  996. extern int ZEXPORT zipOpenNewFileInZip4(zipFile file, const char* filename, const zip_fileinfo* zipfi,
  997. const void* extrafield_local, uInt size_extrafield_local,
  998. const void* extrafield_global, uInt size_extrafield_global,
  999. const char* comment, int method, int level, int raw,
  1000. int windowBits,int memLevel, int strategy,
  1001. const char* password, uLong crcForCrypting,
  1002. uLong versionMadeBy, uLong flagBase) {
  1003. return zipOpenNewFileInZip4_64(file, filename, zipfi,
  1004. extrafield_local, size_extrafield_local,
  1005. extrafield_global, size_extrafield_global,
  1006. comment, method, level, raw,
  1007. windowBits, memLevel, strategy,
  1008. password, crcForCrypting, versionMadeBy, flagBase, 0);
  1009. }
  1010. extern int ZEXPORT zipOpenNewFileInZip3(zipFile file, const char* filename, const zip_fileinfo* zipfi,
  1011. const void* extrafield_local, uInt size_extrafield_local,
  1012. const void* extrafield_global, uInt size_extrafield_global,
  1013. const char* comment, int method, int level, int raw,
  1014. int windowBits,int memLevel, int strategy,
  1015. const char* password, uLong crcForCrypting) {
  1016. return zipOpenNewFileInZip4_64(file, filename, zipfi,
  1017. extrafield_local, size_extrafield_local,
  1018. extrafield_global, size_extrafield_global,
  1019. comment, method, level, raw,
  1020. windowBits, memLevel, strategy,
  1021. password, crcForCrypting, VERSIONMADEBY, 0, 0);
  1022. }
  1023. extern int ZEXPORT zipOpenNewFileInZip3_64(zipFile file, const char* filename, const zip_fileinfo* zipfi,
  1024. const void* extrafield_local, uInt size_extrafield_local,
  1025. const void* extrafield_global, uInt size_extrafield_global,
  1026. const char* comment, int method, int level, int raw,
  1027. int windowBits,int memLevel, int strategy,
  1028. const char* password, uLong crcForCrypting, int zip64) {
  1029. return zipOpenNewFileInZip4_64(file, filename, zipfi,
  1030. extrafield_local, size_extrafield_local,
  1031. extrafield_global, size_extrafield_global,
  1032. comment, method, level, raw,
  1033. windowBits, memLevel, strategy,
  1034. password, crcForCrypting, VERSIONMADEBY, 0, zip64);
  1035. }
  1036. extern int ZEXPORT zipOpenNewFileInZip2(zipFile file, const char* filename, const zip_fileinfo* zipfi,
  1037. const void* extrafield_local, uInt size_extrafield_local,
  1038. const void* extrafield_global, uInt size_extrafield_global,
  1039. const char* comment, int method, int level, int raw) {
  1040. return zipOpenNewFileInZip4_64(file, filename, zipfi,
  1041. extrafield_local, size_extrafield_local,
  1042. extrafield_global, size_extrafield_global,
  1043. comment, method, level, raw,
  1044. -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,
  1045. NULL, 0, VERSIONMADEBY, 0, 0);
  1046. }
  1047. extern int ZEXPORT zipOpenNewFileInZip2_64(zipFile file, const char* filename, const zip_fileinfo* zipfi,
  1048. const void* extrafield_local, uInt size_extrafield_local,
  1049. const void* extrafield_global, uInt size_extrafield_global,
  1050. const char* comment, int method, int level, int raw, int zip64) {
  1051. return zipOpenNewFileInZip4_64(file, filename, zipfi,
  1052. extrafield_local, size_extrafield_local,
  1053. extrafield_global, size_extrafield_global,
  1054. comment, method, level, raw,
  1055. -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,
  1056. NULL, 0, VERSIONMADEBY, 0, zip64);
  1057. }
  1058. extern int ZEXPORT zipOpenNewFileInZip64(zipFile file, const char* filename, const zip_fileinfo* zipfi,
  1059. const void* extrafield_local, uInt size_extrafield_local,
  1060. const void*extrafield_global, uInt size_extrafield_global,
  1061. const char* comment, int method, int level, int zip64) {
  1062. return zipOpenNewFileInZip4_64(file, filename, zipfi,
  1063. extrafield_local, size_extrafield_local,
  1064. extrafield_global, size_extrafield_global,
  1065. comment, method, level, 0,
  1066. -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,
  1067. NULL, 0, VERSIONMADEBY, 0, zip64);
  1068. }
  1069. extern int ZEXPORT zipOpenNewFileInZip(zipFile file, const char* filename, const zip_fileinfo* zipfi,
  1070. const void* extrafield_local, uInt size_extrafield_local,
  1071. const void*extrafield_global, uInt size_extrafield_global,
  1072. const char* comment, int method, int level) {
  1073. return zipOpenNewFileInZip4_64(file, filename, zipfi,
  1074. extrafield_local, size_extrafield_local,
  1075. extrafield_global, size_extrafield_global,
  1076. comment, method, level, 0,
  1077. -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,
  1078. NULL, 0, VERSIONMADEBY, 0, 0);
  1079. }
  1080. local int zip64FlushWriteBuffer(zip64_internal* zi) {
  1081. int err=ZIP_OK;
  1082. if (zi->ci.encrypt != 0)
  1083. {
  1084. #ifndef NOCRYPT
  1085. uInt i;
  1086. int t;
  1087. for (i=0;i<zi->ci.pos_in_buffered_data;i++)
  1088. zi->ci.buffered_data[i] = zencode(zi->ci.keys, zi->ci.pcrc_32_tab, zi->ci.buffered_data[i],t);
  1089. #endif
  1090. }
  1091. if (ZWRITE64(zi->z_filefunc,zi->filestream,zi->ci.buffered_data,zi->ci.pos_in_buffered_data) != zi->ci.pos_in_buffered_data)
  1092. err = ZIP_ERRNO;
  1093. zi->ci.totalCompressedData += zi->ci.pos_in_buffered_data;
  1094. #ifdef HAVE_BZIP2
  1095. if(zi->ci.method == Z_BZIP2ED)
  1096. {
  1097. zi->ci.totalUncompressedData += zi->ci.bstream.total_in_lo32;
  1098. zi->ci.bstream.total_in_lo32 = 0;
  1099. zi->ci.bstream.total_in_hi32 = 0;
  1100. }
  1101. else
  1102. #endif
  1103. {
  1104. zi->ci.totalUncompressedData += zi->ci.stream.total_in;
  1105. zi->ci.stream.total_in = 0;
  1106. }
  1107. zi->ci.pos_in_buffered_data = 0;
  1108. return err;
  1109. }
  1110. extern int ZEXPORT zipWriteInFileInZip(zipFile file, const void* buf, unsigned int len) {
  1111. zip64_internal* zi;
  1112. int err=ZIP_OK;
  1113. if (file == NULL)
  1114. return ZIP_PARAMERROR;
  1115. zi = (zip64_internal*)file;
  1116. if (zi->in_opened_file_inzip == 0)
  1117. return ZIP_PARAMERROR;
  1118. zi->ci.crc32 = crc32(zi->ci.crc32,buf,(uInt)len);
  1119. #ifdef HAVE_BZIP2
  1120. if(zi->ci.method == Z_BZIP2ED && (!zi->ci.raw))
  1121. {
  1122. zi->ci.bstream.next_in = (void*)buf;
  1123. zi->ci.bstream.avail_in = len;
  1124. err = BZ_RUN_OK;
  1125. while ((err==BZ_RUN_OK) && (zi->ci.bstream.avail_in>0))
  1126. {
  1127. if (zi->ci.bstream.avail_out == 0)
  1128. {
  1129. if (zip64FlushWriteBuffer(zi) == ZIP_ERRNO)
  1130. err = ZIP_ERRNO;
  1131. zi->ci.bstream.avail_out = (uInt)Z_BUFSIZE;
  1132. zi->ci.bstream.next_out = (char*)zi->ci.buffered_data;
  1133. }
  1134. if(err != BZ_RUN_OK)
  1135. break;
  1136. if ((zi->ci.method == Z_BZIP2ED) && (!zi->ci.raw))
  1137. {
  1138. uLong uTotalOutBefore_lo = zi->ci.bstream.total_out_lo32;
  1139. // uLong uTotalOutBefore_hi = zi->ci.bstream.total_out_hi32;
  1140. err=BZ2_bzCompress(&zi->ci.bstream, BZ_RUN);
  1141. zi->ci.pos_in_buffered_data += (uInt)(zi->ci.bstream.total_out_lo32 - uTotalOutBefore_lo) ;
  1142. }
  1143. }
  1144. if(err == BZ_RUN_OK)
  1145. err = ZIP_OK;
  1146. }
  1147. else
  1148. #endif
  1149. {
  1150. zi->ci.stream.next_in = (Bytef*)(uintptr_t)buf;
  1151. zi->ci.stream.avail_in = len;
  1152. while ((err==ZIP_OK) && (zi->ci.stream.avail_in>0))
  1153. {
  1154. if (zi->ci.stream.avail_out == 0)
  1155. {
  1156. if (zip64FlushWriteBuffer(zi) == ZIP_ERRNO)
  1157. err = ZIP_ERRNO;
  1158. zi->ci.stream.avail_out = (uInt)Z_BUFSIZE;
  1159. zi->ci.stream.next_out = zi->ci.buffered_data;
  1160. }
  1161. if(err != ZIP_OK)
  1162. break;
  1163. if ((zi->ci.method == Z_DEFLATED) && (!zi->ci.raw))
  1164. {
  1165. uLong uTotalOutBefore = zi->ci.stream.total_out;
  1166. err=deflate(&zi->ci.stream, Z_NO_FLUSH);
  1167. zi->ci.pos_in_buffered_data += (uInt)(zi->ci.stream.total_out - uTotalOutBefore) ;
  1168. }
  1169. else
  1170. {
  1171. uInt copy_this,i;
  1172. if (zi->ci.stream.avail_in < zi->ci.stream.avail_out)
  1173. copy_this = zi->ci.stream.avail_in;
  1174. else
  1175. copy_this = zi->ci.stream.avail_out;
  1176. for (i = 0; i < copy_this; i++)
  1177. *(((char*)zi->ci.stream.next_out)+i) =
  1178. *(((const char*)zi->ci.stream.next_in)+i);
  1179. {
  1180. zi->ci.stream.avail_in -= copy_this;
  1181. zi->ci.stream.avail_out-= copy_this;
  1182. zi->ci.stream.next_in+= copy_this;
  1183. zi->ci.stream.next_out+= copy_this;
  1184. zi->ci.stream.total_in+= copy_this;
  1185. zi->ci.stream.total_out+= copy_this;
  1186. zi->ci.pos_in_buffered_data += copy_this;
  1187. }
  1188. }
  1189. }// while(...)
  1190. }
  1191. return err;
  1192. }
  1193. extern int ZEXPORT zipCloseFileInZipRaw(zipFile file, uLong uncompressed_size, uLong crc32) {
  1194. return zipCloseFileInZipRaw64 (file, uncompressed_size, crc32);
  1195. }
  1196. extern int ZEXPORT zipCloseFileInZipRaw64(zipFile file, ZPOS64_T uncompressed_size, uLong crc32) {
  1197. zip64_internal* zi;
  1198. ZPOS64_T compressed_size;
  1199. uLong invalidValue = 0xffffffff;
  1200. unsigned datasize = 0;
  1201. int err=ZIP_OK;
  1202. if (file == NULL)
  1203. return ZIP_PARAMERROR;
  1204. zi = (zip64_internal*)file;
  1205. if (zi->in_opened_file_inzip == 0)
  1206. return ZIP_PARAMERROR;
  1207. zi->ci.stream.avail_in = 0;
  1208. if ((zi->ci.method == Z_DEFLATED) && (!zi->ci.raw))
  1209. {
  1210. while (err==ZIP_OK)
  1211. {
  1212. uLong uTotalOutBefore;
  1213. if (zi->ci.stream.avail_out == 0)
  1214. {
  1215. if (zip64FlushWriteBuffer(zi) == ZIP_ERRNO)
  1216. err = ZIP_ERRNO;
  1217. zi->ci.stream.avail_out = (uInt)Z_BUFSIZE;
  1218. zi->ci.stream.next_out = zi->ci.buffered_data;
  1219. }
  1220. uTotalOutBefore = zi->ci.stream.total_out;
  1221. err=deflate(&zi->ci.stream, Z_FINISH);
  1222. zi->ci.pos_in_buffered_data += (uInt)(zi->ci.stream.total_out - uTotalOutBefore) ;
  1223. }
  1224. }
  1225. else if ((zi->ci.method == Z_BZIP2ED) && (!zi->ci.raw))
  1226. {
  1227. #ifdef HAVE_BZIP2
  1228. err = BZ_FINISH_OK;
  1229. while (err==BZ_FINISH_OK)
  1230. {
  1231. uLong uTotalOutBefore;
  1232. if (zi->ci.bstream.avail_out == 0)
  1233. {
  1234. if (zip64FlushWriteBuffer(zi) == ZIP_ERRNO)
  1235. err = ZIP_ERRNO;
  1236. zi->ci.bstream.avail_out = (uInt)Z_BUFSIZE;
  1237. zi->ci.bstream.next_out = (char*)zi->ci.buffered_data;
  1238. }
  1239. uTotalOutBefore = zi->ci.bstream.total_out_lo32;
  1240. err=BZ2_bzCompress(&zi->ci.bstream, BZ_FINISH);
  1241. if(err == BZ_STREAM_END)
  1242. err = Z_STREAM_END;
  1243. zi->ci.pos_in_buffered_data += (uInt)(zi->ci.bstream.total_out_lo32 - uTotalOutBefore);
  1244. }
  1245. if(err == BZ_FINISH_OK)
  1246. err = ZIP_OK;
  1247. #endif
  1248. }
  1249. if (err==Z_STREAM_END)
  1250. err=ZIP_OK; /* this is normal */
  1251. if ((zi->ci.pos_in_buffered_data>0) && (err==ZIP_OK))
  1252. {
  1253. if (zip64FlushWriteBuffer(zi)==ZIP_ERRNO)
  1254. err = ZIP_ERRNO;
  1255. }
  1256. if ((zi->ci.method == Z_DEFLATED) && (!zi->ci.raw))
  1257. {
  1258. int tmp_err = deflateEnd(&zi->ci.stream);
  1259. if (err == ZIP_OK)
  1260. err = tmp_err;
  1261. zi->ci.stream_initialised = 0;
  1262. }
  1263. #ifdef HAVE_BZIP2
  1264. else if((zi->ci.method == Z_BZIP2ED) && (!zi->ci.raw))
  1265. {
  1266. int tmperr = BZ2_bzCompressEnd(&zi->ci.bstream);
  1267. if (err==ZIP_OK)
  1268. err = tmperr;
  1269. zi->ci.stream_initialised = 0;
  1270. }
  1271. #endif
  1272. if (!zi->ci.raw)
  1273. {
  1274. crc32 = (uLong)zi->ci.crc32;
  1275. uncompressed_size = zi->ci.totalUncompressedData;
  1276. }
  1277. compressed_size = zi->ci.totalCompressedData;
  1278. # ifndef NOCRYPT
  1279. compressed_size += zi->ci.crypt_header_size;
  1280. # endif
  1281. // update Current Item crc and sizes,
  1282. if(compressed_size >= 0xffffffff || uncompressed_size >= 0xffffffff || zi->ci.pos_local_header >= 0xffffffff)
  1283. {
  1284. /*version Made by*/
  1285. zip64local_putValue_inmemory(zi->ci.central_header+4,(uLong)45,2);
  1286. /*version needed*/
  1287. zip64local_putValue_inmemory(zi->ci.central_header+6,(uLong)45,2);
  1288. }
  1289. zip64local_putValue_inmemory(zi->ci.central_header+16,crc32,4); /*crc*/
  1290. if(compressed_size >= 0xffffffff)
  1291. zip64local_putValue_inmemory(zi->ci.central_header+20, invalidValue,4); /*compr size*/
  1292. else
  1293. zip64local_putValue_inmemory(zi->ci.central_header+20, compressed_size,4); /*compr size*/
  1294. /// set internal file attributes field
  1295. if (zi->ci.stream.data_type == Z_ASCII)
  1296. zip64local_putValue_inmemory(zi->ci.central_header+36,(uLong)Z_ASCII,2);
  1297. if(uncompressed_size >= 0xffffffff)
  1298. zip64local_putValue_inmemory(zi->ci.central_header+24, invalidValue,4); /*uncompr size*/
  1299. else
  1300. zip64local_putValue_inmemory(zi->ci.central_header+24, uncompressed_size,4); /*uncompr size*/
  1301. // Add ZIP64 extra info field for uncompressed size
  1302. if(uncompressed_size >= 0xffffffff)
  1303. datasize += 8;
  1304. // Add ZIP64 extra info field for compressed size
  1305. if(compressed_size >= 0xffffffff)
  1306. datasize += 8;
  1307. // Add ZIP64 extra info field for relative offset to local file header of current file
  1308. if(zi->ci.pos_local_header >= 0xffffffff)
  1309. datasize += 8;
  1310. if(datasize > 0)
  1311. {
  1312. char* p = NULL;
  1313. if((uLong)(datasize + 4) > zi->ci.size_centralExtraFree)
  1314. {
  1315. // we cannot write more data to the buffer that we have room for.
  1316. return ZIP_BADZIPFILE;
  1317. }
  1318. p = zi->ci.central_header + zi->ci.size_centralheader;
  1319. // Add Extra Information Header for 'ZIP64 information'
  1320. zip64local_putValue_inmemory(p, 0x0001, 2); // HeaderID
  1321. p += 2;
  1322. zip64local_putValue_inmemory(p, datasize, 2); // DataSize
  1323. p += 2;
  1324. if(uncompressed_size >= 0xffffffff)
  1325. {
  1326. zip64local_putValue_inmemory(p, uncompressed_size, 8);
  1327. p += 8;
  1328. }
  1329. if(compressed_size >= 0xffffffff)
  1330. {
  1331. zip64local_putValue_inmemory(p, compressed_size, 8);
  1332. p += 8;
  1333. }
  1334. if(zi->ci.pos_local_header >= 0xffffffff)
  1335. {
  1336. zip64local_putValue_inmemory(p, zi->ci.pos_local_header, 8);
  1337. p += 8;
  1338. }
  1339. // Update how much extra free space we got in the memory buffer
  1340. // and increase the centralheader size so the new ZIP64 fields are included
  1341. // ( 4 below is the size of HeaderID and DataSize field )
  1342. zi->ci.size_centralExtraFree -= datasize + 4;
  1343. zi->ci.size_centralheader += datasize + 4;
  1344. // Update the extra info size field
  1345. zi->ci.size_centralExtra += datasize + 4;
  1346. zip64local_putValue_inmemory(zi->ci.central_header+30,(uLong)zi->ci.size_centralExtra,2);
  1347. }
  1348. if (err==ZIP_OK)
  1349. err = add_data_in_datablock(&zi->central_dir, zi->ci.central_header, (uLong)zi->ci.size_centralheader);
  1350. free(zi->ci.central_header);
  1351. if (err==ZIP_OK)
  1352. {
  1353. // Update the LocalFileHeader with the new values.
  1354. ZPOS64_T cur_pos_inzip = ZTELL64(zi->z_filefunc,zi->filestream);
  1355. if (ZSEEK64(zi->z_filefunc,zi->filestream, zi->ci.pos_local_header + 14,ZLIB_FILEFUNC_SEEK_SET)!=0)
  1356. err = ZIP_ERRNO;
  1357. if (err==ZIP_OK)
  1358. err = zip64local_putValue(&zi->z_filefunc,zi->filestream,crc32,4); /* crc 32, unknown */
  1359. if(uncompressed_size >= 0xffffffff || compressed_size >= 0xffffffff )
  1360. {
  1361. if(zi->ci.pos_zip64extrainfo > 0)
  1362. {
  1363. // Update the size in the ZIP64 extended field.
  1364. if (ZSEEK64(zi->z_filefunc,zi->filestream, zi->ci.pos_zip64extrainfo + 4,ZLIB_FILEFUNC_SEEK_SET)!=0)
  1365. err = ZIP_ERRNO;
  1366. if (err==ZIP_OK) /* compressed size, unknown */
  1367. err = zip64local_putValue(&zi->z_filefunc, zi->filestream, uncompressed_size, 8);
  1368. if (err==ZIP_OK) /* uncompressed size, unknown */
  1369. err = zip64local_putValue(&zi->z_filefunc, zi->filestream, compressed_size, 8);
  1370. }
  1371. else
  1372. err = ZIP_BADZIPFILE; // Caller passed zip64 = 0, so no room for zip64 info -> fatal
  1373. }
  1374. else
  1375. {
  1376. if (err==ZIP_OK) /* compressed size, unknown */
  1377. err = zip64local_putValue(&zi->z_filefunc,zi->filestream,compressed_size,4);
  1378. if (err==ZIP_OK) /* uncompressed size, unknown */
  1379. err = zip64local_putValue(&zi->z_filefunc,zi->filestream,uncompressed_size,4);
  1380. }
  1381. if (ZSEEK64(zi->z_filefunc,zi->filestream, cur_pos_inzip,ZLIB_FILEFUNC_SEEK_SET)!=0)
  1382. err = ZIP_ERRNO;
  1383. }
  1384. zi->number_entry ++;
  1385. zi->in_opened_file_inzip = 0;
  1386. return err;
  1387. }
  1388. extern int ZEXPORT zipCloseFileInZip(zipFile file) {
  1389. return zipCloseFileInZipRaw (file,0,0);
  1390. }
  1391. local int Write_Zip64EndOfCentralDirectoryLocator(zip64_internal* zi, ZPOS64_T zip64eocd_pos_inzip) {
  1392. int err = ZIP_OK;
  1393. ZPOS64_T pos = zip64eocd_pos_inzip - zi->add_position_when_writing_offset;
  1394. err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)ZIP64ENDLOCHEADERMAGIC,4);
  1395. /*num disks*/
  1396. if (err==ZIP_OK) /* number of the disk with the start of the central directory */
  1397. err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4);
  1398. /*relative offset*/
  1399. if (err==ZIP_OK) /* Relative offset to the Zip64EndOfCentralDirectory */
  1400. err = zip64local_putValue(&zi->z_filefunc,zi->filestream, pos,8);
  1401. /*total disks*/ /* Do not support spawning of disk so always say 1 here*/
  1402. if (err==ZIP_OK) /* number of the disk with the start of the central directory */
  1403. err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)1,4);
  1404. return err;
  1405. }
  1406. local int Write_Zip64EndOfCentralDirectoryRecord(zip64_internal* zi, uLong size_centraldir, ZPOS64_T centraldir_pos_inzip) {
  1407. int err = ZIP_OK;
  1408. uLong Zip64DataSize = 44;
  1409. err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)ZIP64ENDHEADERMAGIC,4);
  1410. if (err==ZIP_OK) /* size of this 'zip64 end of central directory' */
  1411. err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(ZPOS64_T)Zip64DataSize,8); // why ZPOS64_T of this ?
  1412. if (err==ZIP_OK) /* version made by */
  1413. err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)45,2);
  1414. if (err==ZIP_OK) /* version needed */
  1415. err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)45,2);
  1416. if (err==ZIP_OK) /* number of this disk */
  1417. err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4);
  1418. if (err==ZIP_OK) /* number of the disk with the start of the central directory */
  1419. err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,4);
  1420. if (err==ZIP_OK) /* total number of entries in the central dir on this disk */
  1421. err = zip64local_putValue(&zi->z_filefunc, zi->filestream, zi->number_entry, 8);
  1422. if (err==ZIP_OK) /* total number of entries in the central dir */
  1423. err = zip64local_putValue(&zi->z_filefunc, zi->filestream, zi->number_entry, 8);
  1424. if (err==ZIP_OK) /* size of the central directory */
  1425. err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(ZPOS64_T)size_centraldir,8);
  1426. if (err==ZIP_OK) /* offset of start of central directory with respect to the starting disk number */
  1427. {
  1428. ZPOS64_T pos = centraldir_pos_inzip - zi->add_position_when_writing_offset;
  1429. err = zip64local_putValue(&zi->z_filefunc,zi->filestream, (ZPOS64_T)pos,8);
  1430. }
  1431. return err;
  1432. }
  1433. local int Write_EndOfCentralDirectoryRecord(zip64_internal* zi, uLong size_centraldir, ZPOS64_T centraldir_pos_inzip) {
  1434. int err = ZIP_OK;
  1435. /*signature*/
  1436. err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)ENDHEADERMAGIC,4);
  1437. if (err==ZIP_OK) /* number of this disk */
  1438. err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,2);
  1439. if (err==ZIP_OK) /* number of the disk with the start of the central directory */
  1440. err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0,2);
  1441. if (err==ZIP_OK) /* total number of entries in the central dir on this disk */
  1442. {
  1443. {
  1444. if(zi->number_entry >= 0xFFFF)
  1445. err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0xffff,2); // use value in ZIP64 record
  1446. else
  1447. err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->number_entry,2);
  1448. }
  1449. }
  1450. if (err==ZIP_OK) /* total number of entries in the central dir */
  1451. {
  1452. if(zi->number_entry >= 0xFFFF)
  1453. err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)0xffff,2); // use value in ZIP64 record
  1454. else
  1455. err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)zi->number_entry,2);
  1456. }
  1457. if (err==ZIP_OK) /* size of the central directory */
  1458. err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)size_centraldir,4);
  1459. if (err==ZIP_OK) /* offset of start of central directory with respect to the starting disk number */
  1460. {
  1461. ZPOS64_T pos = centraldir_pos_inzip - zi->add_position_when_writing_offset;
  1462. if(pos >= 0xffffffff)
  1463. {
  1464. err = zip64local_putValue(&zi->z_filefunc,zi->filestream, (uLong)0xffffffff,4);
  1465. }
  1466. else
  1467. err = zip64local_putValue(&zi->z_filefunc,zi->filestream, (uLong)(centraldir_pos_inzip - zi->add_position_when_writing_offset),4);
  1468. }
  1469. return err;
  1470. }
  1471. local int Write_GlobalComment(zip64_internal* zi, const char* global_comment) {
  1472. int err = ZIP_OK;
  1473. uInt size_global_comment = 0;
  1474. if(global_comment != NULL)
  1475. size_global_comment = (uInt)strlen(global_comment);
  1476. err = zip64local_putValue(&zi->z_filefunc,zi->filestream,(uLong)size_global_comment,2);
  1477. if (err == ZIP_OK && size_global_comment > 0)
  1478. {
  1479. if (ZWRITE64(zi->z_filefunc,zi->filestream, global_comment, size_global_comment) != size_global_comment)
  1480. err = ZIP_ERRNO;
  1481. }
  1482. return err;
  1483. }
  1484. extern int ZEXPORT zipClose(zipFile file, const char* global_comment) {
  1485. zip64_internal* zi;
  1486. int err = 0;
  1487. uLong size_centraldir = 0;
  1488. ZPOS64_T centraldir_pos_inzip;
  1489. ZPOS64_T pos;
  1490. if (file == NULL)
  1491. return ZIP_PARAMERROR;
  1492. zi = (zip64_internal*)file;
  1493. if (zi->in_opened_file_inzip == 1)
  1494. {
  1495. err = zipCloseFileInZip (file);
  1496. }
  1497. #ifndef NO_ADDFILEINEXISTINGZIP
  1498. if (global_comment==NULL)
  1499. global_comment = zi->globalcomment;
  1500. #endif
  1501. centraldir_pos_inzip = ZTELL64(zi->z_filefunc,zi->filestream);
  1502. if (err==ZIP_OK)
  1503. {
  1504. linkedlist_datablock_internal* ldi = zi->central_dir.first_block;
  1505. while (ldi!=NULL)
  1506. {
  1507. if ((err==ZIP_OK) && (ldi->filled_in_this_block>0))
  1508. {
  1509. if (ZWRITE64(zi->z_filefunc,zi->filestream, ldi->data, ldi->filled_in_this_block) != ldi->filled_in_this_block)
  1510. err = ZIP_ERRNO;
  1511. }
  1512. size_centraldir += ldi->filled_in_this_block;
  1513. ldi = ldi->next_datablock;
  1514. }
  1515. }
  1516. free_linkedlist(&(zi->central_dir));
  1517. pos = centraldir_pos_inzip - zi->add_position_when_writing_offset;
  1518. if(pos >= 0xffffffff || zi->number_entry >= 0xFFFF)
  1519. {
  1520. ZPOS64_T Zip64EOCDpos = ZTELL64(zi->z_filefunc,zi->filestream);
  1521. Write_Zip64EndOfCentralDirectoryRecord(zi, size_centraldir, centraldir_pos_inzip);
  1522. Write_Zip64EndOfCentralDirectoryLocator(zi, Zip64EOCDpos);
  1523. }
  1524. if (err==ZIP_OK)
  1525. err = Write_EndOfCentralDirectoryRecord(zi, size_centraldir, centraldir_pos_inzip);
  1526. if(err == ZIP_OK)
  1527. err = Write_GlobalComment(zi, global_comment);
  1528. if (ZCLOSE64(zi->z_filefunc,zi->filestream) != 0)
  1529. if (err == ZIP_OK)
  1530. err = ZIP_ERRNO;
  1531. #ifndef NO_ADDFILEINEXISTINGZIP
  1532. free(zi->globalcomment);
  1533. #endif
  1534. free(zi);
  1535. return err;
  1536. }
  1537. extern int ZEXPORT zipRemoveExtraInfoBlock(char* pData, int* dataLen, short sHeader) {
  1538. char* p = pData;
  1539. int size = 0;
  1540. char* pNewHeader;
  1541. char* pTmp;
  1542. short header;
  1543. short dataSize;
  1544. int retVal = ZIP_OK;
  1545. if(pData == NULL || dataLen == NULL || *dataLen < 4)
  1546. return ZIP_PARAMERROR;
  1547. pNewHeader = (char*)ALLOC((unsigned)*dataLen);
  1548. pTmp = pNewHeader;
  1549. while(p < (pData + *dataLen))
  1550. {
  1551. header = *(short*)p;
  1552. dataSize = *(((short*)p)+1);
  1553. if( header == sHeader ) // Header found.
  1554. {
  1555. p += dataSize + 4; // skip it. do not copy to temp buffer
  1556. }
  1557. else
  1558. {
  1559. // Extra Info block should not be removed, So copy it to the temp buffer.
  1560. memcpy(pTmp, p, dataSize + 4);
  1561. p += dataSize + 4;
  1562. size += dataSize + 4;
  1563. }
  1564. }
  1565. if(size < *dataLen)
  1566. {
  1567. // clean old extra info block.
  1568. memset(pData,0, *dataLen);
  1569. // copy the new extra info block over the old
  1570. if(size > 0)
  1571. memcpy(pData, pNewHeader, size);
  1572. // set the new extra info size
  1573. *dataLen = size;
  1574. retVal = ZIP_OK;
  1575. }
  1576. else
  1577. retVal = ZIP_ERRNO;
  1578. free(pNewHeader);
  1579. return retVal;
  1580. }