zip.c 64 KB

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