zip.c 64 KB

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