zip.c 64 KB

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