zip.c 64 KB

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