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