zip.c 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982
  1. /* zip.c -- IO on .zip files using zlib
  2. Version 1.2.0, September 16th, 2017
  3. part of the MiniZip project
  4. Copyright (C) 2010-2017 Nathan Moinvaziri
  5. Modifications for AES, PKWARE disk spanning
  6. https://github.com/nmoinvaz/minizip
  7. Copyright (C) 2009-2010 Mathias Svensson
  8. Modifications for Zip64 support
  9. http://result42.com
  10. Copyright (C) 1998-2010 Gilles Vollant
  11. http://www.winimage.com/zLibDll/minizip.html
  12. This program is distributed under the terms of the same license as zlib.
  13. See the accompanying LICENSE file for the full text of the license.
  14. */
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <stdint.h>
  18. #include <string.h>
  19. #include <errno.h>
  20. #include "zlib.h"
  21. #include "zip.h"
  22. #ifdef HAVE_AES
  23. # define AES_METHOD (99)
  24. # define AES_PWVERIFYSIZE (2)
  25. # define AES_AUTHCODESIZE (10)
  26. # define AES_MAXSALTLENGTH (16)
  27. # define AES_VERSION (0x0001)
  28. # define AES_ENCRYPTIONMODE (0x03)
  29. # include "aes/aes.h"
  30. # include "aes/fileenc.h"
  31. # include "aes/prng.h"
  32. #endif
  33. #ifdef HAVE_APPLE_COMPRESSION
  34. # include <compression.h>
  35. #endif
  36. #ifndef NOCRYPT
  37. # include "crypt.h"
  38. #endif
  39. #define SIZEDATA_INDATABLOCK (4096-(4*4))
  40. #define DISKHEADERMAGIC (0x08074b50)
  41. #define LOCALHEADERMAGIC (0x04034b50)
  42. #define CENTRALHEADERMAGIC (0x02014b50)
  43. #define ENDHEADERMAGIC (0x06054b50)
  44. #define ZIP64ENDHEADERMAGIC (0x06064b50)
  45. #define ZIP64ENDLOCHEADERMAGIC (0x07064b50)
  46. #define DATADESCRIPTORMAGIC (0x08074b50)
  47. #define FLAG_LOCALHEADER_OFFSET (0x06)
  48. #define CRC_LOCALHEADER_OFFSET (0x0e)
  49. #define SIZECENTRALHEADER (0x2e) /* 46 */
  50. #define SIZECENTRALHEADERLOCATOR (0x14) /* 20 */
  51. #define SIZECENTRALDIRITEM (0x2e)
  52. #define SIZEZIPLOCALHEADER (0x1e)
  53. #ifndef BUFREADCOMMENT
  54. # define BUFREADCOMMENT (0x400)
  55. #endif
  56. #ifndef VERSIONMADEBY
  57. # define VERSIONMADEBY (0x0) /* platform dependent */
  58. #endif
  59. #ifndef Z_BUFSIZE
  60. # define Z_BUFSIZE (UINT16_MAX)
  61. #endif
  62. #ifndef ALLOC
  63. # define ALLOC(size) (malloc(size))
  64. #endif
  65. #ifndef TRYFREE
  66. # define TRYFREE(p) {if (p) free(p);}
  67. #endif
  68. /* NOT sure that this work on ALL platform */
  69. #define MAKEULONG64(a, b) ((uint64_t)(((unsigned long)(a)) | ((uint64_t)((unsigned long)(b))) << 32))
  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. typedef struct linkedlist_datablock_internal_s
  79. {
  80. struct linkedlist_datablock_internal_s *next_datablock;
  81. uint32_t avail_in_this_block;
  82. uint32_t filled_in_this_block;
  83. uint32_t unused; /* for future use and alignment */
  84. uint8_t data[SIZEDATA_INDATABLOCK];
  85. } linkedlist_datablock_internal;
  86. typedef struct linkedlist_data_s
  87. {
  88. linkedlist_datablock_internal *first_block;
  89. linkedlist_datablock_internal *last_block;
  90. } linkedlist_data;
  91. typedef struct
  92. {
  93. z_stream stream; /* zLib stream structure for inflate */
  94. #ifdef HAVE_BZIP2
  95. bz_stream bstream; /* bzLib stream structure for bziped */
  96. #endif
  97. #ifdef HAVE_APPLE_COMPRESSION
  98. compression_stream astream; /* libcompression stream structure */
  99. #endif
  100. #ifdef HAVE_AES
  101. fcrypt_ctx aes_ctx;
  102. prng_ctx aes_rng[1];
  103. #endif
  104. int stream_initialised; /* 1 is stream is initialized */
  105. uint32_t pos_in_buffered_data; /* last written byte in buffered_data */
  106. uint64_t pos_local_header; /* offset of the local header of the file currently writing */
  107. char *central_header; /* central header data for the current file */
  108. uint16_t size_centralextra;
  109. uint16_t size_centralheader; /* size of the central header for cur file */
  110. uint16_t size_centralextrafree; /* Extra bytes allocated to the central header but that are not used */
  111. uint16_t size_comment;
  112. uint16_t flag; /* flag of the file currently writing */
  113. uint16_t method; /* compression method written to file.*/
  114. uint16_t compression_method; /* compression method to use */
  115. int raw; /* 1 for directly writing raw data */
  116. uint8_t buffered_data[Z_BUFSIZE]; /* buffer contain compressed data to be writ*/
  117. uint32_t dos_date;
  118. uint32_t crc32;
  119. int zip64; /* add ZIP64 extended information in the extra field */
  120. uint32_t number_disk; /* number of current disk used for spanning ZIP */
  121. uint64_t total_compressed;
  122. uint64_t total_uncompressed;
  123. #ifndef NOCRYPT
  124. uint32_t keys[3]; /* keys defining the pseudo-random sequence */
  125. const z_crc_t *pcrc_32_tab;
  126. #endif
  127. } curfile64_info;
  128. typedef struct
  129. {
  130. zlib_filefunc64_32_def z_filefunc;
  131. voidpf filestream; /* io structure of the zipfile */
  132. voidpf filestream_with_CD; /* io structure of the zipfile with the central dir */
  133. linkedlist_data central_dir; /* datablock with central dir in construction*/
  134. int in_opened_file_inzip; /* 1 if a file in the zip is currently writ.*/
  135. int append; /* append mode */
  136. curfile64_info ci; /* info on the file currently writing */
  137. uint64_t add_position_when_writting_offset;
  138. uint64_t number_entry;
  139. uint64_t disk_size; /* size of each disk */
  140. uint32_t number_disk; /* number of the current disk, used for spanning ZIP */
  141. uint32_t number_disk_with_CD; /* number the the disk with central dir, used for spanning ZIP */
  142. #ifndef NO_ADDFILEINEXISTINGZIP
  143. char *globalcomment;
  144. #endif
  145. } zip64_internal;
  146. /* Allocate a new data block */
  147. static linkedlist_datablock_internal *allocate_new_datablock(void)
  148. {
  149. linkedlist_datablock_internal *ldi = NULL;
  150. ldi = (linkedlist_datablock_internal*)ALLOC(sizeof(linkedlist_datablock_internal));
  151. if (ldi != NULL)
  152. {
  153. ldi->next_datablock = NULL;
  154. ldi->filled_in_this_block = 0;
  155. ldi->avail_in_this_block = SIZEDATA_INDATABLOCK;
  156. }
  157. return ldi;
  158. }
  159. /* Free data block in linked list */
  160. static void free_datablock(linkedlist_datablock_internal *ldi)
  161. {
  162. while (ldi != NULL)
  163. {
  164. linkedlist_datablock_internal *ldinext = ldi->next_datablock;
  165. TRYFREE(ldi);
  166. ldi = ldinext;
  167. }
  168. }
  169. /* Initialize linked list */
  170. static void init_linkedlist(linkedlist_data *ll)
  171. {
  172. ll->first_block = ll->last_block = NULL;
  173. }
  174. /* Free entire linked list and all data blocks */
  175. static void free_linkedlist(linkedlist_data *ll)
  176. {
  177. free_datablock(ll->first_block);
  178. ll->first_block = ll->last_block = NULL;
  179. }
  180. /* Add data to linked list data block */
  181. static int add_data_in_datablock(linkedlist_data *ll, const void *buf, uint32_t len)
  182. {
  183. linkedlist_datablock_internal *ldi = NULL;
  184. const unsigned char *from_copy = NULL;
  185. if (ll == NULL)
  186. return ZIP_INTERNALERROR;
  187. if (ll->last_block == NULL)
  188. {
  189. ll->first_block = ll->last_block = allocate_new_datablock();
  190. if (ll->first_block == NULL)
  191. return ZIP_INTERNALERROR;
  192. }
  193. ldi = ll->last_block;
  194. from_copy = (unsigned char*)buf;
  195. while (len > 0)
  196. {
  197. uint32_t copy_this = 0;
  198. uint32_t i = 0;
  199. unsigned char *to_copy = NULL;
  200. if (ldi->avail_in_this_block == 0)
  201. {
  202. ldi->next_datablock = allocate_new_datablock();
  203. if (ldi->next_datablock == NULL)
  204. return ZIP_INTERNALERROR;
  205. ldi = ldi->next_datablock ;
  206. ll->last_block = ldi;
  207. }
  208. if (ldi->avail_in_this_block < len)
  209. copy_this = ldi->avail_in_this_block;
  210. else
  211. copy_this = len;
  212. to_copy = &(ldi->data[ldi->filled_in_this_block]);
  213. for (i = 0; i < copy_this; i++)
  214. *(to_copy+i) = *(from_copy+i);
  215. ldi->filled_in_this_block += copy_this;
  216. ldi->avail_in_this_block -= copy_this;
  217. from_copy += copy_this;
  218. len -= copy_this;
  219. }
  220. return ZIP_OK;
  221. }
  222. /* Inputs a long in LSB order to the given file: nbByte == 1, 2 ,4 or 8 (byte, short or long, uint64_t) */
  223. static int zipWriteValue(const zlib_filefunc64_32_def *pzlib_filefunc_def, voidpf filestream,
  224. uint64_t x, uint32_t len)
  225. {
  226. unsigned char buf[8];
  227. uint32_t n = 0;
  228. for (n = 0; n < len; n++)
  229. {
  230. buf[n] = (unsigned char)(x & 0xff);
  231. x >>= 8;
  232. }
  233. if (x != 0)
  234. {
  235. /* Data overflow - hack for ZIP64 (X Roche) */
  236. for (n = 0; n < len; n++)
  237. {
  238. buf[n] = 0xff;
  239. }
  240. }
  241. if (ZWRITE64(*pzlib_filefunc_def, filestream, buf, len) != len)
  242. return ZIP_ERRNO;
  243. return ZIP_OK;
  244. }
  245. static void zipWriteValueToMemory(void* dest, uint64_t x, uint32_t len)
  246. {
  247. unsigned char *buf = (unsigned char*)dest;
  248. uint32_t n = 0;
  249. for (n = 0; n < len; n++)
  250. {
  251. buf[n] = (unsigned char)(x & 0xff);
  252. x >>= 8;
  253. }
  254. if (x != 0)
  255. {
  256. /* data overflow - hack for ZIP64 */
  257. for (n = 0; n < len; n++)
  258. {
  259. buf[n] = 0xff;
  260. }
  261. }
  262. }
  263. static void zipWriteValueToMemoryAndMove(unsigned char **dest_ptr, uint64_t x, uint32_t len)
  264. {
  265. zipWriteValueToMemory(*dest_ptr, x, len);
  266. *dest_ptr += len;
  267. }
  268. static int zipReadUInt8(const zlib_filefunc64_32_def *pzlib_filefunc_def, voidpf filestream, uint8_t *value)
  269. {
  270. uint8_t c = 0;
  271. if (ZREAD64(*pzlib_filefunc_def, filestream, &c, 1) == 1)
  272. {
  273. *value = (uint8_t)c;
  274. return ZIP_OK;
  275. }
  276. if (ZERROR64(*pzlib_filefunc_def, filestream))
  277. return ZIP_ERRNO;
  278. return ZIP_EOF;
  279. }
  280. static int zipReadUInt16(const zlib_filefunc64_32_def *pzlib_filefunc_def, voidpf filestream, uint16_t *value)
  281. {
  282. uint16_t x = 0;
  283. uint8_t c = 0;
  284. int err = ZIP_OK;
  285. err = zipReadUInt8(pzlib_filefunc_def, filestream, &c);
  286. x = (uint16_t)c;
  287. if (err == ZIP_OK)
  288. err = zipReadUInt8(pzlib_filefunc_def, filestream, &c);
  289. x += ((uint16_t)c) << 8;
  290. if (err == ZIP_OK)
  291. *value = x;
  292. else
  293. *value = 0;
  294. return err;
  295. }
  296. static int zipReadUInt32(const zlib_filefunc64_32_def *pzlib_filefunc_def, voidpf filestream, uint32_t *value)
  297. {
  298. uint32_t x = 0;
  299. uint8_t c = 0;
  300. int err = ZIP_OK;
  301. err = zipReadUInt8(pzlib_filefunc_def, filestream, &c);
  302. x = (uint32_t)c;
  303. if (err == ZIP_OK)
  304. err = zipReadUInt8(pzlib_filefunc_def, filestream, &c);
  305. x += ((uint32_t)c) << 8;
  306. if (err == ZIP_OK)
  307. err = zipReadUInt8(pzlib_filefunc_def, filestream, &c);
  308. x += ((uint32_t)c) << 16;
  309. if (err == ZIP_OK)
  310. err = zipReadUInt8(pzlib_filefunc_def, filestream, &c);
  311. x += ((uint32_t)c) << 24;
  312. if (err == ZIP_OK)
  313. *value = x;
  314. else
  315. *value = 0;
  316. return err;
  317. }
  318. static int zipReadUInt64(const zlib_filefunc64_32_def *pzlib_filefunc_def, voidpf filestream, uint64_t *value)
  319. {
  320. uint64_t x = 0;
  321. uint8_t c = 0;
  322. int err = ZIP_OK;
  323. err = zipReadUInt8(pzlib_filefunc_def, filestream, &c);
  324. x = (uint64_t)c;
  325. if (err == ZIP_OK)
  326. err = zipReadUInt8(pzlib_filefunc_def, filestream, &c);
  327. x += ((uint64_t)c) << 8;
  328. if (err == ZIP_OK)
  329. err = zipReadUInt8(pzlib_filefunc_def, filestream, &c);
  330. x += ((uint64_t)c) << 16;
  331. if (err == ZIP_OK)
  332. err = zipReadUInt8(pzlib_filefunc_def, filestream, &c);
  333. x += ((uint64_t)c) << 24;
  334. if (err == ZIP_OK)
  335. err = zipReadUInt8(pzlib_filefunc_def, filestream, &c);
  336. x += ((uint64_t)c) << 32;
  337. if (err == ZIP_OK)
  338. err = zipReadUInt8(pzlib_filefunc_def, filestream, &c);
  339. x += ((uint64_t)c) << 40;
  340. if (err == ZIP_OK)
  341. err = zipReadUInt8(pzlib_filefunc_def, filestream, &c);
  342. x += ((uint64_t)c) << 48;
  343. if (err == ZIP_OK)
  344. err = zipReadUInt8(pzlib_filefunc_def, filestream, &c);
  345. x += ((uint64_t)c) << 56;
  346. if (err == ZIP_OK)
  347. *value = x;
  348. else
  349. *value = 0;
  350. return err;
  351. }
  352. /* Gets the amount of bytes left to write to the current disk for spanning archives */
  353. static void zipGetDiskSizeAvailable(zipFile file, uint64_t *size_available)
  354. {
  355. zip64_internal *zi = NULL;
  356. uint64_t current_disk_size = 0;
  357. zi = (zip64_internal*)file;
  358. ZSEEK64(zi->z_filefunc, zi->filestream, 0, ZLIB_FILEFUNC_SEEK_END);
  359. current_disk_size = ZTELL64(zi->z_filefunc, zi->filestream);
  360. *size_available = zi->disk_size - current_disk_size;
  361. }
  362. /* Goes to a specific disk number for spanning archives */
  363. static int zipGoToSpecificDisk(zipFile file, uint32_t number_disk, int open_existing)
  364. {
  365. zip64_internal *zi = NULL;
  366. int err = ZIP_OK;
  367. zi = (zip64_internal*)file;
  368. if (zi->disk_size == 0)
  369. return err;
  370. if ((zi->filestream != NULL) && (zi->filestream != zi->filestream_with_CD))
  371. ZCLOSE64(zi->z_filefunc, zi->filestream);
  372. zi->filestream = ZOPENDISK64(zi->z_filefunc, zi->filestream_with_CD, number_disk, (open_existing == 1) ?
  373. (ZLIB_FILEFUNC_MODE_READ | ZLIB_FILEFUNC_MODE_WRITE | ZLIB_FILEFUNC_MODE_EXISTING) :
  374. (ZLIB_FILEFUNC_MODE_READ | ZLIB_FILEFUNC_MODE_WRITE | ZLIB_FILEFUNC_MODE_CREATE));
  375. if (zi->filestream == NULL)
  376. err = ZIP_ERRNO;
  377. return err;
  378. }
  379. /* Goes to the first disk in a spanned archive */
  380. static int zipGoToFirstDisk(zipFile file)
  381. {
  382. zip64_internal *zi = NULL;
  383. uint32_t number_disk_next = 0;
  384. int err = ZIP_OK;
  385. zi = (zip64_internal*)file;
  386. if (zi->disk_size == 0)
  387. return err;
  388. number_disk_next = 0;
  389. if (zi->number_disk_with_CD > 0)
  390. number_disk_next = zi->number_disk_with_CD - 1;
  391. err = zipGoToSpecificDisk(file, number_disk_next, (zi->append == APPEND_STATUS_ADDINZIP));
  392. if ((err == ZIP_ERRNO) && (zi->append == APPEND_STATUS_ADDINZIP))
  393. err = zipGoToSpecificDisk(file, number_disk_next, 0);
  394. if (err == ZIP_OK)
  395. zi->number_disk = number_disk_next;
  396. ZSEEK64(zi->z_filefunc, zi->filestream, 0, ZLIB_FILEFUNC_SEEK_END);
  397. return err;
  398. }
  399. /* Goes to the next disk in a spanned archive */
  400. static int zipGoToNextDisk(zipFile file)
  401. {
  402. zip64_internal *zi = NULL;
  403. uint64_t size_available_in_disk = 0;
  404. uint32_t number_disk_next = 0;
  405. int err = ZIP_OK;
  406. zi = (zip64_internal*)file;
  407. if (zi->disk_size == 0)
  408. return err;
  409. number_disk_next = zi->number_disk + 1;
  410. do
  411. {
  412. err = zipGoToSpecificDisk(file, number_disk_next, (zi->append == APPEND_STATUS_ADDINZIP));
  413. if ((err == ZIP_ERRNO) && (zi->append == APPEND_STATUS_ADDINZIP))
  414. err = zipGoToSpecificDisk(file, number_disk_next, 0);
  415. if (err != ZIP_OK)
  416. break;
  417. zipGetDiskSizeAvailable(file, &size_available_in_disk);
  418. zi->number_disk = number_disk_next;
  419. zi->number_disk_with_CD = zi->number_disk + 1;
  420. number_disk_next += 1;
  421. }
  422. while (size_available_in_disk <= 0);
  423. return err;
  424. }
  425. /* Locate the Central directory of a zipfile (at the end, just before the global comment) */
  426. static uint64_t zipSearchCentralDir(const zlib_filefunc64_32_def *pzlib_filefunc_def, voidpf filestream)
  427. {
  428. unsigned char *buf = NULL;
  429. uint64_t file_size = 0;
  430. uint64_t back_read = 4;
  431. uint64_t max_back = UINT16_MAX; /* maximum size of global comment */
  432. uint64_t pos_found = 0;
  433. uint32_t read_size = 0;
  434. uint64_t read_pos = 0;
  435. uint32_t i = 0;
  436. buf = (unsigned char*)ALLOC(BUFREADCOMMENT+4);
  437. if (buf == NULL)
  438. return 0;
  439. if (ZSEEK64(*pzlib_filefunc_def, filestream, 0, ZLIB_FILEFUNC_SEEK_END) != 0)
  440. {
  441. TRYFREE(buf);
  442. return 0;
  443. }
  444. file_size = ZTELL64(*pzlib_filefunc_def, filestream);
  445. if (max_back > file_size)
  446. max_back = file_size;
  447. while (back_read < max_back)
  448. {
  449. if (back_read + BUFREADCOMMENT > max_back)
  450. back_read = max_back;
  451. else
  452. back_read += BUFREADCOMMENT;
  453. read_pos = file_size-back_read;
  454. read_size = ((BUFREADCOMMENT+4) < (file_size - read_pos)) ?
  455. (BUFREADCOMMENT+4) : (uint32_t)(file_size - read_pos);
  456. if (ZSEEK64(*pzlib_filefunc_def, filestream, read_pos, ZLIB_FILEFUNC_SEEK_SET) != 0)
  457. break;
  458. if (ZREAD64(*pzlib_filefunc_def, filestream, buf, read_size) != read_size)
  459. break;
  460. for (i = read_size-3; (i--) > 0;)
  461. if ((*(buf+i)) == (ENDHEADERMAGIC & 0xff) &&
  462. (*(buf+i+1)) == (ENDHEADERMAGIC >> 8 & 0xff) &&
  463. (*(buf+i+2)) == (ENDHEADERMAGIC >> 16 & 0xff) &&
  464. (*(buf+i+3)) == (ENDHEADERMAGIC >> 24 & 0xff))
  465. {
  466. pos_found = read_pos+i;
  467. break;
  468. }
  469. if (pos_found != 0)
  470. break;
  471. }
  472. TRYFREE(buf);
  473. return pos_found;
  474. }
  475. /* Locate the Central directory 64 of a zipfile (at the end, just before the global comment) */
  476. static uint64_t zipSearchCentralDir64(const zlib_filefunc64_32_def *pzlib_filefunc_def, voidpf filestream,
  477. const uint64_t endcentraloffset)
  478. {
  479. uint64_t offset = 0;
  480. uint32_t value32 = 0;
  481. /* Zip64 end of central directory locator */
  482. if (ZSEEK64(*pzlib_filefunc_def, filestream, endcentraloffset - SIZECENTRALHEADERLOCATOR, ZLIB_FILEFUNC_SEEK_SET) != 0)
  483. return 0;
  484. /* Read locator signature */
  485. if (zipReadUInt32(pzlib_filefunc_def, filestream, &value32) != ZIP_OK)
  486. return 0;
  487. if (value32 != ZIP64ENDLOCHEADERMAGIC)
  488. return 0;
  489. /* Number of the disk with the start of the zip64 end of central directory */
  490. if (zipReadUInt32(pzlib_filefunc_def, filestream, &value32) != ZIP_OK)
  491. return 0;
  492. /* Relative offset of the zip64 end of central directory record */
  493. if (zipReadUInt64(pzlib_filefunc_def, filestream, &offset) != ZIP_OK)
  494. return 0;
  495. /* Total number of disks */
  496. if (zipReadUInt32(pzlib_filefunc_def, filestream, &value32) != ZIP_OK)
  497. return 0;
  498. /* Goto end of central directory record */
  499. if (ZSEEK64(*pzlib_filefunc_def,filestream, offset, ZLIB_FILEFUNC_SEEK_SET) != 0)
  500. return 0;
  501. /* The signature */
  502. if (zipReadUInt32(pzlib_filefunc_def, filestream, &value32) != ZIP_OK)
  503. return 0;
  504. if (value32 != ZIP64ENDHEADERMAGIC)
  505. return 0;
  506. return offset;
  507. }
  508. extern zipFile ZEXPORT zipOpen4(const void *path, int append, uint64_t disk_size, const char **globalcomment,
  509. zlib_filefunc64_32_def *pzlib_filefunc64_32_def)
  510. {
  511. zip64_internal ziinit;
  512. zip64_internal *zi = NULL;
  513. #ifndef NO_ADDFILEINEXISTINGZIP
  514. uint64_t byte_before_the_zipfile = 0; /* byte before the zipfile, (>0 for sfx)*/
  515. uint64_t size_central_dir = 0; /* size of the central directory */
  516. uint64_t offset_central_dir = 0; /* offset of start of central directory */
  517. uint64_t number_entry_CD = 0; /* total number of entries in the central dir */
  518. uint64_t number_entry = 0;
  519. uint64_t central_pos = 0;
  520. uint64_t size_central_dir_to_read = 0;
  521. uint16_t value16 = 0;
  522. uint32_t value32 = 0;
  523. uint16_t size_comment = 0;
  524. size_t buf_size = SIZEDATA_INDATABLOCK;
  525. void *buf_read = NULL;
  526. #endif
  527. int err = ZIP_OK;
  528. int mode = 0;
  529. ziinit.z_filefunc.zseek32_file = NULL;
  530. ziinit.z_filefunc.ztell32_file = NULL;
  531. if (pzlib_filefunc64_32_def == NULL)
  532. fill_fopen64_filefunc(&ziinit.z_filefunc.zfile_func64);
  533. else
  534. ziinit.z_filefunc = *pzlib_filefunc64_32_def;
  535. if (append == APPEND_STATUS_CREATE)
  536. mode = (ZLIB_FILEFUNC_MODE_READ | ZLIB_FILEFUNC_MODE_WRITE | ZLIB_FILEFUNC_MODE_CREATE);
  537. else
  538. mode = (ZLIB_FILEFUNC_MODE_READ | ZLIB_FILEFUNC_MODE_WRITE | ZLIB_FILEFUNC_MODE_EXISTING);
  539. ziinit.filestream = ZOPEN64(ziinit.z_filefunc, path, mode);
  540. if (ziinit.filestream == NULL)
  541. return NULL;
  542. if (append == APPEND_STATUS_CREATEAFTER)
  543. {
  544. /* Don't support spanning ZIP with APPEND_STATUS_CREATEAFTER */
  545. if (disk_size > 0)
  546. return NULL;
  547. ZSEEK64(ziinit.z_filefunc,ziinit.filestream,0,SEEK_END);
  548. }
  549. ziinit.filestream_with_CD = ziinit.filestream;
  550. ziinit.append = append;
  551. ziinit.number_disk = 0;
  552. ziinit.number_disk_with_CD = 0;
  553. ziinit.disk_size = disk_size;
  554. ziinit.in_opened_file_inzip = 0;
  555. ziinit.ci.stream_initialised = 0;
  556. ziinit.number_entry = 0;
  557. ziinit.add_position_when_writting_offset = 0;
  558. init_linkedlist(&(ziinit.central_dir));
  559. zi = (zip64_internal*)ALLOC(sizeof(zip64_internal));
  560. if (zi == NULL)
  561. {
  562. ZCLOSE64(ziinit.z_filefunc,ziinit.filestream);
  563. return NULL;
  564. }
  565. #ifndef NO_ADDFILEINEXISTINGZIP
  566. /* Add file in a zipfile */
  567. ziinit.globalcomment = NULL;
  568. if (append == APPEND_STATUS_ADDINZIP)
  569. {
  570. /* Read and Cache Central Directory Records */
  571. central_pos = zipSearchCentralDir(&ziinit.z_filefunc,ziinit.filestream);
  572. /* Disable to allow appending to empty ZIP archive (must be standard zip, not zip64)
  573. if (central_pos == 0)
  574. err = ZIP_ERRNO;
  575. */
  576. if (err == ZIP_OK)
  577. {
  578. /* Read end of central directory info */
  579. if (ZSEEK64(ziinit.z_filefunc, ziinit.filestream, central_pos,ZLIB_FILEFUNC_SEEK_SET) != 0)
  580. err = ZIP_ERRNO;
  581. /* The signature, already checked */
  582. if (zipReadUInt32(&ziinit.z_filefunc, ziinit.filestream, &value32) != ZIP_OK)
  583. err = ZIP_ERRNO;
  584. /* Number of this disk */
  585. if (zipReadUInt16(&ziinit.z_filefunc, ziinit.filestream, &value16) != ZIP_OK)
  586. err = ZIP_ERRNO;
  587. ziinit.number_disk = value16;
  588. /* Number of the disk with the start of the central directory */
  589. if (zipReadUInt16(&ziinit.z_filefunc, ziinit.filestream, &value16) != ZIP_OK)
  590. err = ZIP_ERRNO;
  591. ziinit.number_disk_with_CD = value16;
  592. /* Total number of entries in the central dir on this disk */
  593. number_entry = 0;
  594. if (zipReadUInt16(&ziinit.z_filefunc, ziinit.filestream, &value16) != ZIP_OK)
  595. err = ZIP_ERRNO;
  596. else
  597. number_entry = value16;
  598. /* Total number of entries in the central dir */
  599. number_entry_CD = 0;
  600. if (zipReadUInt16(&ziinit.z_filefunc, ziinit.filestream, &value16) != ZIP_OK)
  601. err = ZIP_ERRNO;
  602. else
  603. number_entry_CD = value16;
  604. if (number_entry_CD!=number_entry)
  605. err = ZIP_BADZIPFILE;
  606. /* Size of the central directory */
  607. size_central_dir = 0;
  608. if (zipReadUInt32(&ziinit.z_filefunc, ziinit.filestream, &value32) != ZIP_OK)
  609. err = ZIP_ERRNO;
  610. else
  611. size_central_dir = value32;
  612. /* Offset of start of central directory with respect to the starting disk number */
  613. offset_central_dir = 0;
  614. if (zipReadUInt32(&ziinit.z_filefunc, ziinit.filestream, &value32) != ZIP_OK)
  615. err = ZIP_ERRNO;
  616. else
  617. offset_central_dir = value32;
  618. /* Zipfile global comment length */
  619. if (zipReadUInt16(&ziinit.z_filefunc, ziinit.filestream, &size_comment) != ZIP_OK)
  620. err = ZIP_ERRNO;
  621. if ((err == ZIP_OK) && ((number_entry_CD == UINT16_MAX) || (offset_central_dir == UINT32_MAX)))
  622. {
  623. /* Format should be Zip64, as the central directory or file size is too large */
  624. central_pos = zipSearchCentralDir64(&ziinit.z_filefunc, ziinit.filestream, central_pos);
  625. if (central_pos)
  626. {
  627. uint64_t sizeEndOfCentralDirectory;
  628. if (ZSEEK64(ziinit.z_filefunc, ziinit.filestream, central_pos, ZLIB_FILEFUNC_SEEK_SET) != 0)
  629. err = ZIP_ERRNO;
  630. /* The signature, already checked */
  631. if (zipReadUInt32(&ziinit.z_filefunc, ziinit.filestream, &value32) != ZIP_OK)
  632. err = ZIP_ERRNO;
  633. /* Size of zip64 end of central directory record */
  634. if (zipReadUInt64(&ziinit.z_filefunc, ziinit.filestream, &sizeEndOfCentralDirectory) != ZIP_OK)
  635. err = ZIP_ERRNO;
  636. /* Version made by */
  637. if (zipReadUInt16(&ziinit.z_filefunc, ziinit.filestream, &value16) != ZIP_OK)
  638. err = ZIP_ERRNO;
  639. /* Version needed to extract */
  640. if (zipReadUInt16(&ziinit.z_filefunc, ziinit.filestream, &value16) != ZIP_OK)
  641. err = ZIP_ERRNO;
  642. /* Number of this disk */
  643. if (zipReadUInt32(&ziinit.z_filefunc, ziinit.filestream, &ziinit.number_disk) != ZIP_OK)
  644. err = ZIP_ERRNO;
  645. /* Number of the disk with the start of the central directory */
  646. if (zipReadUInt32(&ziinit.z_filefunc, ziinit.filestream, &ziinit.number_disk_with_CD) != ZIP_OK)
  647. err = ZIP_ERRNO;
  648. /* Total number of entries in the central directory on this disk */
  649. if (zipReadUInt64(&ziinit.z_filefunc, ziinit.filestream, &number_entry) != ZIP_OK)
  650. err = ZIP_ERRNO;
  651. /* Total number of entries in the central directory */
  652. if (zipReadUInt64(&ziinit.z_filefunc, ziinit.filestream, &number_entry_CD) != ZIP_OK)
  653. err = ZIP_ERRNO;
  654. if (number_entry_CD!=number_entry)
  655. err = ZIP_BADZIPFILE;
  656. /* Size of the central directory */
  657. if (zipReadUInt64(&ziinit.z_filefunc, ziinit.filestream, &size_central_dir) != ZIP_OK)
  658. err = ZIP_ERRNO;
  659. /* Offset of start of central directory with respect to the starting disk number */
  660. if (zipReadUInt64(&ziinit.z_filefunc, ziinit.filestream, &offset_central_dir) != ZIP_OK)
  661. err = ZIP_ERRNO;
  662. }
  663. else
  664. err = ZIP_BADZIPFILE;
  665. }
  666. }
  667. if ((err == ZIP_OK) && (central_pos < offset_central_dir + size_central_dir))
  668. err = ZIP_BADZIPFILE;
  669. if ((err == ZIP_OK) && (size_comment > 0))
  670. {
  671. ziinit.globalcomment = (char*)ALLOC(size_comment+1);
  672. if (ziinit.globalcomment)
  673. {
  674. if (ZREAD64(ziinit.z_filefunc, ziinit.filestream, ziinit.globalcomment, size_comment) != size_comment)
  675. err = ZIP_ERRNO;
  676. else
  677. ziinit.globalcomment[size_comment] = 0;
  678. }
  679. }
  680. if (err != ZIP_OK)
  681. {
  682. ZCLOSE64(ziinit.z_filefunc, ziinit.filestream);
  683. TRYFREE(ziinit.globalcomment);
  684. TRYFREE(zi);
  685. return NULL;
  686. }
  687. byte_before_the_zipfile = central_pos - (offset_central_dir+size_central_dir);
  688. ziinit.add_position_when_writting_offset = byte_before_the_zipfile;
  689. /* Store central directory in memory */
  690. size_central_dir_to_read = size_central_dir;
  691. buf_size = SIZEDATA_INDATABLOCK;
  692. buf_read = (void*)ALLOC(buf_size);
  693. if (ZSEEK64(ziinit.z_filefunc, ziinit.filestream,
  694. offset_central_dir + byte_before_the_zipfile, ZLIB_FILEFUNC_SEEK_SET) != 0)
  695. err = ZIP_ERRNO;
  696. while ((size_central_dir_to_read > 0) && (err == ZIP_OK))
  697. {
  698. uint64_t read_this = SIZEDATA_INDATABLOCK;
  699. if (read_this > size_central_dir_to_read)
  700. read_this = size_central_dir_to_read;
  701. if (ZREAD64(ziinit.z_filefunc, ziinit.filestream, buf_read, (uint32_t)read_this) != read_this)
  702. err = ZIP_ERRNO;
  703. if (err == ZIP_OK)
  704. err = add_data_in_datablock(&ziinit.central_dir, buf_read, (uint32_t)read_this);
  705. size_central_dir_to_read -= read_this;
  706. }
  707. TRYFREE(buf_read);
  708. ziinit.number_entry = number_entry_CD;
  709. if (ZSEEK64(ziinit.z_filefunc, ziinit.filestream,
  710. offset_central_dir+byte_before_the_zipfile, ZLIB_FILEFUNC_SEEK_SET) != 0)
  711. err = ZIP_ERRNO;
  712. }
  713. if (globalcomment)
  714. *globalcomment = ziinit.globalcomment;
  715. #endif
  716. if (err != ZIP_OK)
  717. {
  718. #ifndef NO_ADDFILEINEXISTINGZIP
  719. TRYFREE(ziinit.globalcomment);
  720. #endif
  721. TRYFREE(zi);
  722. return NULL;
  723. }
  724. *zi = ziinit;
  725. zipGoToFirstDisk((zipFile)zi);
  726. return(zipFile)zi;
  727. }
  728. extern zipFile ZEXPORT zipOpen2(const char *path, int append, const char **globalcomment,
  729. zlib_filefunc_def *pzlib_filefunc32_def)
  730. {
  731. if (pzlib_filefunc32_def != NULL)
  732. {
  733. zlib_filefunc64_32_def zlib_filefunc64_32_def_fill;
  734. fill_zlib_filefunc64_32_def_from_filefunc32(&zlib_filefunc64_32_def_fill,pzlib_filefunc32_def);
  735. return zipOpen4(path, append, 0, globalcomment, &zlib_filefunc64_32_def_fill);
  736. }
  737. return zipOpen4(path, append, 0, globalcomment, NULL);
  738. }
  739. extern zipFile ZEXPORT zipOpen2_64(const void *path, int append, const char **globalcomment,
  740. zlib_filefunc64_def *pzlib_filefunc_def)
  741. {
  742. if (pzlib_filefunc_def != NULL)
  743. {
  744. zlib_filefunc64_32_def zlib_filefunc64_32_def_fill;
  745. zlib_filefunc64_32_def_fill.zfile_func64 = *pzlib_filefunc_def;
  746. zlib_filefunc64_32_def_fill.ztell32_file = NULL;
  747. zlib_filefunc64_32_def_fill.zseek32_file = NULL;
  748. return zipOpen4(path, append, 0, globalcomment, &zlib_filefunc64_32_def_fill);
  749. }
  750. return zipOpen4(path, append, 0, globalcomment, NULL);
  751. }
  752. extern zipFile ZEXPORT zipOpen3(const char *path, int append, uint64_t disk_size, const char **globalcomment,
  753. zlib_filefunc_def *pzlib_filefunc32_def)
  754. {
  755. if (pzlib_filefunc32_def != NULL)
  756. {
  757. zlib_filefunc64_32_def zlib_filefunc64_32_def_fill;
  758. fill_zlib_filefunc64_32_def_from_filefunc32(&zlib_filefunc64_32_def_fill,pzlib_filefunc32_def);
  759. return zipOpen4(path, append, disk_size, globalcomment, &zlib_filefunc64_32_def_fill);
  760. }
  761. return zipOpen4(path, append, disk_size, globalcomment, NULL);
  762. }
  763. extern zipFile ZEXPORT zipOpen3_64(const void *path, int append, uint64_t disk_size, const char **globalcomment,
  764. zlib_filefunc64_def *pzlib_filefunc_def)
  765. {
  766. if (pzlib_filefunc_def != NULL)
  767. {
  768. zlib_filefunc64_32_def zlib_filefunc64_32_def_fill;
  769. zlib_filefunc64_32_def_fill.zfile_func64 = *pzlib_filefunc_def;
  770. zlib_filefunc64_32_def_fill.ztell32_file = NULL;
  771. zlib_filefunc64_32_def_fill.zseek32_file = NULL;
  772. return zipOpen4(path, append, disk_size, globalcomment, &zlib_filefunc64_32_def_fill);
  773. }
  774. return zipOpen4(path, append, disk_size, globalcomment, NULL);
  775. }
  776. extern zipFile ZEXPORT zipOpen(const char *path, int append)
  777. {
  778. return zipOpen3((const void*)path, append, 0, NULL, NULL);
  779. }
  780. extern zipFile ZEXPORT zipOpen64(const void *path, int append)
  781. {
  782. return zipOpen3(path, append, 0, NULL, NULL);
  783. }
  784. extern int ZEXPORT zipOpenNewFileInZip4_64(zipFile file, const char *filename, const zip_fileinfo *zipfi,
  785. const void *extrafield_local, uint16_t size_extrafield_local, const void *extrafield_global,
  786. uint16_t size_extrafield_global, const char *comment, uint16_t method, int level, int raw, int windowBits, int memLevel,
  787. int strategy, const char *password, ZIP_UNUSED uint32_t crc_for_crypting, uint16_t version_madeby, uint16_t flag_base, int zip64)
  788. {
  789. zip64_internal *zi = NULL;
  790. uint64_t size_available = 0;
  791. uint64_t size_needed = 0;
  792. uint16_t size_filename = 0;
  793. uint16_t size_comment = 0;
  794. uint16_t i = 0;
  795. unsigned char *central_dir = NULL;
  796. int err = ZIP_OK;
  797. #ifdef NOCRYPT
  798. if (password != NULL)
  799. return ZIP_PARAMERROR;
  800. #endif
  801. if (file == NULL)
  802. return ZIP_PARAMERROR;
  803. if ((method != 0) &&
  804. #ifdef HAVE_BZIP2
  805. (method != Z_BZIP2ED) &&
  806. #endif
  807. (method != Z_DEFLATED))
  808. return ZIP_PARAMERROR;
  809. zi = (zip64_internal*)file;
  810. if (zi->in_opened_file_inzip == 1)
  811. {
  812. err = zipCloseFileInZip (file);
  813. if (err != ZIP_OK)
  814. return err;
  815. }
  816. if (filename == NULL)
  817. filename = "-";
  818. if (comment != NULL)
  819. size_comment = (uint16_t)strlen(comment);
  820. size_filename = (uint16_t)strlen(filename);
  821. if (zipfi == NULL)
  822. zi->ci.dos_date = 0;
  823. else
  824. {
  825. if (zipfi->dos_date != 0)
  826. zi->ci.dos_date = zipfi->dos_date;
  827. }
  828. zi->ci.method = method;
  829. zi->ci.compression_method = method;
  830. zi->ci.raw = raw;
  831. zi->ci.flag = flag_base | 8;
  832. if ((level == 8) || (level == 9))
  833. zi->ci.flag |= 2;
  834. if (level == 2)
  835. zi->ci.flag |= 4;
  836. if (level == 1)
  837. zi->ci.flag |= 6;
  838. if (password != NULL)
  839. {
  840. zi->ci.flag |= 1;
  841. #ifdef HAVE_AES
  842. zi->ci.method = AES_METHOD;
  843. #endif
  844. }
  845. else
  846. {
  847. zi->ci.flag &= ~1;
  848. }
  849. if (zi->disk_size > 0)
  850. {
  851. if ((zi->number_disk == 0) && (zi->number_entry == 0))
  852. err = zipWriteValue(&zi->z_filefunc, zi->filestream, (uint32_t)DISKHEADERMAGIC, 4);
  853. /* Make sure enough space available on current disk for local header */
  854. zipGetDiskSizeAvailable((zipFile)zi, &size_available);
  855. size_needed = 30 + size_filename + size_extrafield_local;
  856. #ifdef HAVE_AES
  857. if (zi->ci.method == AES_METHOD)
  858. size_needed += 11;
  859. #endif
  860. if (size_available < size_needed)
  861. zipGoToNextDisk((zipFile)zi);
  862. }
  863. zi->ci.zip64 = zip64;
  864. zi->ci.pos_local_header = ZTELL64(zi->z_filefunc, zi->filestream);
  865. if (zi->ci.pos_local_header >= UINT32_MAX)
  866. zi->ci.zip64 = 1;
  867. zi->ci.size_comment = size_comment;
  868. zi->ci.size_centralheader = SIZECENTRALHEADER + size_filename + size_extrafield_global;
  869. zi->ci.size_centralextra = size_extrafield_global;
  870. zi->ci.size_centralextrafree = 32; /* Extra space reserved for ZIP64 extra info */
  871. #ifdef HAVE_AES
  872. if (zi->ci.method == AES_METHOD)
  873. zi->ci.size_centralextrafree += 11; /* Extra space reserved for AES extra info */
  874. #endif
  875. zi->ci.central_header = (char*)ALLOC((uint32_t)zi->ci.size_centralheader + zi->ci.size_centralextrafree + size_comment);
  876. zi->ci.number_disk = zi->number_disk;
  877. /* Write central directory header */
  878. central_dir = (unsigned char*)zi->ci.central_header;
  879. zipWriteValueToMemoryAndMove(&central_dir, (uint32_t)CENTRALHEADERMAGIC, 4);
  880. zipWriteValueToMemoryAndMove(&central_dir, version_madeby, 2);
  881. if (zi->ci.zip64)
  882. zipWriteValueToMemoryAndMove(&central_dir, (uint16_t)45, 2);
  883. else
  884. zipWriteValueToMemoryAndMove(&central_dir, (uint16_t)20, 2);
  885. zipWriteValueToMemoryAndMove(&central_dir, zi->ci.flag, 2);
  886. zipWriteValueToMemoryAndMove(&central_dir, zi->ci.method, 2);
  887. zipWriteValueToMemoryAndMove(&central_dir, zi->ci.dos_date, 4);
  888. zipWriteValueToMemoryAndMove(&central_dir, (uint32_t)0, 4); /*crc*/
  889. zipWriteValueToMemoryAndMove(&central_dir, (uint32_t)0, 4); /*compr size*/
  890. zipWriteValueToMemoryAndMove(&central_dir, (uint32_t)0, 4); /*uncompr size*/
  891. zipWriteValueToMemoryAndMove(&central_dir, size_filename, 2);
  892. zipWriteValueToMemoryAndMove(&central_dir, size_extrafield_global, 2);
  893. zipWriteValueToMemoryAndMove(&central_dir, size_comment, 2);
  894. zipWriteValueToMemoryAndMove(&central_dir, (uint16_t)zi->ci.number_disk, 2); /*disk nm start*/
  895. if (zipfi == NULL)
  896. zipWriteValueToMemoryAndMove(&central_dir, (uint16_t)0, 2);
  897. else
  898. zipWriteValueToMemoryAndMove(&central_dir, zipfi->internal_fa, 2);
  899. if (zipfi == NULL)
  900. zipWriteValueToMemoryAndMove(&central_dir, (uint32_t)0, 4);
  901. else
  902. zipWriteValueToMemoryAndMove(&central_dir, zipfi->external_fa, 4);
  903. if (zi->ci.pos_local_header >= UINT32_MAX)
  904. zipWriteValueToMemoryAndMove(&central_dir, UINT32_MAX, 4);
  905. else
  906. zipWriteValueToMemoryAndMove(&central_dir,
  907. (uint32_t)(zi->ci.pos_local_header - zi->add_position_when_writting_offset), 4);
  908. for (i = 0; i < size_filename; i++)
  909. zi->ci.central_header[SIZECENTRALHEADER+i] = filename[i];
  910. for (i = 0; i < size_extrafield_global; i++)
  911. zi->ci.central_header[SIZECENTRALHEADER+size_filename+i] =
  912. ((const char*)extrafield_global)[i];
  913. /* Store comment at the end for later repositioning */
  914. for (i = 0; i < size_comment; i++)
  915. zi->ci.central_header[zi->ci.size_centralheader+
  916. zi->ci.size_centralextrafree+i] = comment[i];
  917. if (zi->ci.central_header == NULL)
  918. return ZIP_INTERNALERROR;
  919. /* Write the local header */
  920. if (err == ZIP_OK)
  921. err = zipWriteValue(&zi->z_filefunc, zi->filestream, (uint32_t)LOCALHEADERMAGIC, 4);
  922. if (err == ZIP_OK)
  923. {
  924. if (zi->ci.zip64)
  925. err = zipWriteValue(&zi->z_filefunc, zi->filestream, (uint16_t)45, 2); /* version needed to extract */
  926. else
  927. err = zipWriteValue(&zi->z_filefunc, zi->filestream, (uint16_t)20, 2); /* version needed to extract */
  928. }
  929. if (err == ZIP_OK)
  930. err = zipWriteValue(&zi->z_filefunc, zi->filestream, zi->ci.flag, 2);
  931. if (err == ZIP_OK)
  932. err = zipWriteValue(&zi->z_filefunc, zi->filestream, zi->ci.method, 2);
  933. if (err == ZIP_OK)
  934. err = zipWriteValue(&zi->z_filefunc, zi->filestream, zi->ci.dos_date, 4);
  935. /* CRC & compressed size & uncompressed size is in data descriptor */
  936. if (err == ZIP_OK)
  937. err = zipWriteValue(&zi->z_filefunc, zi->filestream, (uint32_t)0, 4); /* crc 32, unknown */
  938. if (err == ZIP_OK)
  939. err = zipWriteValue(&zi->z_filefunc, zi->filestream, (uint32_t)0, 4); /* compressed size, unknown */
  940. if (err == ZIP_OK)
  941. err = zipWriteValue(&zi->z_filefunc, zi->filestream, (uint32_t)0, 4); /* uncompressed size, unknown */
  942. if (err == ZIP_OK)
  943. err = zipWriteValue(&zi->z_filefunc, zi->filestream, size_filename, 2);
  944. if (err == ZIP_OK)
  945. {
  946. uint64_t size_extrafield = size_extrafield_local;
  947. #ifdef HAVE_AES
  948. if (zi->ci.method == AES_METHOD)
  949. size_extrafield += 11;
  950. #endif
  951. err = zipWriteValue(&zi->z_filefunc, zi->filestream, (uint16_t)size_extrafield, 2);
  952. }
  953. if ((err == ZIP_OK) && (size_filename > 0))
  954. {
  955. if (ZWRITE64(zi->z_filefunc, zi->filestream, filename, size_filename) != size_filename)
  956. err = ZIP_ERRNO;
  957. }
  958. if ((err == ZIP_OK) && (size_extrafield_local > 0))
  959. {
  960. if (ZWRITE64(zi->z_filefunc, zi->filestream, extrafield_local, size_extrafield_local) != size_extrafield_local)
  961. err = ZIP_ERRNO;
  962. }
  963. #ifdef HAVE_AES
  964. /* Write the AES extended info */
  965. if ((err == ZIP_OK) && (zi->ci.method == AES_METHOD))
  966. {
  967. int headerid = 0x9901;
  968. short datasize = 7;
  969. err = zipWriteValue(&zi->z_filefunc, zi->filestream, headerid, 2);
  970. if (err == ZIP_OK)
  971. err = zipWriteValue(&zi->z_filefunc, zi->filestream, datasize, 2);
  972. if (err == ZIP_OK)
  973. err = zipWriteValue(&zi->z_filefunc, zi->filestream, AES_VERSION, 2);
  974. if (err == ZIP_OK)
  975. err = zipWriteValue(&zi->z_filefunc, zi->filestream, 'A', 1);
  976. if (err == ZIP_OK)
  977. err = zipWriteValue(&zi->z_filefunc, zi->filestream, 'E', 1);
  978. if (err == ZIP_OK)
  979. err = zipWriteValue(&zi->z_filefunc, zi->filestream, AES_ENCRYPTIONMODE, 1);
  980. if (err == ZIP_OK)
  981. err = zipWriteValue(&zi->z_filefunc, zi->filestream, zi->ci.compression_method, 2);
  982. }
  983. #endif
  984. zi->ci.crc32 = 0;
  985. zi->ci.stream_initialised = 0;
  986. zi->ci.pos_in_buffered_data = 0;
  987. zi->ci.total_compressed = 0;
  988. zi->ci.total_uncompressed = 0;
  989. #ifdef HAVE_BZIP2
  990. zi->ci.bstream.avail_in = (uint16_t)0;
  991. zi->ci.bstream.avail_out = (uint16_t)Z_BUFSIZE;
  992. zi->ci.bstream.next_out = (char*)zi->ci.buffered_data;
  993. zi->ci.bstream.total_in_hi32 = 0;
  994. zi->ci.bstream.total_in_lo32 = 0;
  995. zi->ci.bstream.total_out_hi32 = 0;
  996. zi->ci.bstream.total_out_lo32 = 0;
  997. #endif
  998. zi->ci.stream.avail_in = (uint16_t)0;
  999. zi->ci.stream.avail_out = Z_BUFSIZE;
  1000. zi->ci.stream.next_out = zi->ci.buffered_data;
  1001. zi->ci.stream.total_in = 0;
  1002. zi->ci.stream.total_out = 0;
  1003. zi->ci.stream.data_type = Z_BINARY;
  1004. if ((err == ZIP_OK) && (!zi->ci.raw))
  1005. {
  1006. if (method == Z_DEFLATED)
  1007. {
  1008. zi->ci.stream.zalloc = (alloc_func)0;
  1009. zi->ci.stream.zfree = (free_func)0;
  1010. zi->ci.stream.opaque = (voidpf)zi;
  1011. if (windowBits > 0)
  1012. windowBits = -windowBits;
  1013. #ifdef HAVE_APPLE_COMPRESSION
  1014. err = compression_stream_init(&zi->ci.astream, COMPRESSION_STREAM_ENCODE, COMPRESSION_ZLIB);
  1015. if (err == COMPRESSION_STATUS_ERROR)
  1016. err = Z_ERRNO;
  1017. else
  1018. err = Z_OK;
  1019. #else
  1020. err = deflateInit2(&zi->ci.stream, level, Z_DEFLATED, windowBits, memLevel, strategy);
  1021. #endif
  1022. if (err == Z_OK)
  1023. zi->ci.stream_initialised = Z_DEFLATED;
  1024. }
  1025. else if (method == Z_BZIP2ED)
  1026. {
  1027. #ifdef HAVE_BZIP2
  1028. zi->ci.bstream.bzalloc = 0;
  1029. zi->ci.bstream.bzfree = 0;
  1030. zi->ci.bstream.opaque = (voidpf)0;
  1031. err = BZ2_bzCompressInit(&zi->ci.bstream, level, 0, 35);
  1032. if (err == BZ_OK)
  1033. zi->ci.stream_initialised = Z_BZIP2ED;
  1034. #endif
  1035. }
  1036. }
  1037. #ifndef NOCRYPT
  1038. if ((err == Z_OK) && (password != NULL))
  1039. {
  1040. #ifdef HAVE_AES
  1041. if (zi->ci.method == AES_METHOD)
  1042. {
  1043. unsigned char passverify[AES_PWVERIFYSIZE];
  1044. unsigned char saltvalue[AES_MAXSALTLENGTH];
  1045. uint16_t saltlength = 0;
  1046. if ((AES_ENCRYPTIONMODE < 1) || (AES_ENCRYPTIONMODE > 3))
  1047. return Z_ERRNO;
  1048. saltlength = SALT_LENGTH(AES_ENCRYPTIONMODE);
  1049. prng_init(cryptrand, zi->ci.aes_rng);
  1050. prng_rand(saltvalue, saltlength, zi->ci.aes_rng);
  1051. prng_end(zi->ci.aes_rng);
  1052. fcrypt_init(AES_ENCRYPTIONMODE, (uint8_t *)password, (uint32_t)strlen(password), saltvalue, passverify, &zi->ci.aes_ctx);
  1053. if (ZWRITE64(zi->z_filefunc, zi->filestream, saltvalue, saltlength) != saltlength)
  1054. err = ZIP_ERRNO;
  1055. if (ZWRITE64(zi->z_filefunc, zi->filestream, passverify, AES_PWVERIFYSIZE) != AES_PWVERIFYSIZE)
  1056. err = ZIP_ERRNO;
  1057. zi->ci.total_compressed += saltlength + AES_PWVERIFYSIZE + AES_AUTHCODESIZE;
  1058. }
  1059. else
  1060. #endif
  1061. {
  1062. unsigned char buf_head[RAND_HEAD_LEN];
  1063. uint32_t size_head = 0;
  1064. uint8_t verify1 = 0;
  1065. uint8_t verify2 = 0;
  1066. zi->ci.pcrc_32_tab = get_crc_table();
  1067. /*
  1068. Info-ZIP modification to ZipCrypto format:
  1069. If bit 3 of the general purpose bit flag is set, it uses high byte of 16-bit File Time.
  1070. */
  1071. verify1 = (uint8_t)((zi->ci.dos_date >> 16) & 0xff);
  1072. verify2 = (uint8_t)((zi->ci.dos_date >> 8) & 0xff);
  1073. size_head = crypthead(password, buf_head, RAND_HEAD_LEN, zi->ci.keys, zi->ci.pcrc_32_tab, verify1, verify2);
  1074. zi->ci.total_compressed += size_head;
  1075. if (ZWRITE64(zi->z_filefunc, zi->filestream, buf_head, size_head) != size_head)
  1076. err = ZIP_ERRNO;
  1077. }
  1078. }
  1079. #endif
  1080. if (err == Z_OK)
  1081. zi->in_opened_file_inzip = 1;
  1082. return err;
  1083. }
  1084. extern int ZEXPORT zipOpenNewFileInZip4(zipFile file, const char *filename, const zip_fileinfo *zipfi,
  1085. const void *extrafield_local, uint16_t size_extrafield_local, const void *extrafield_global,
  1086. uint16_t size_extrafield_global, const char *comment, uint16_t method, int level, int raw, int windowBits,
  1087. int memLevel, int strategy, const char *password, ZIP_UNUSED uint32_t crc_for_crypting, uint16_t version_madeby, uint16_t flag_base)
  1088. {
  1089. return zipOpenNewFileInZip4_64(file, filename, zipfi, extrafield_local, size_extrafield_local,
  1090. extrafield_global, size_extrafield_global, comment, method, level, raw, windowBits, memLevel,
  1091. strategy, password, crc_for_crypting, version_madeby, flag_base, 0);
  1092. }
  1093. extern int ZEXPORT zipOpenNewFileInZip3(zipFile file, const char *filename, const zip_fileinfo *zipfi,
  1094. const void *extrafield_local, uint16_t size_extrafield_local, const void *extrafield_global,
  1095. uint16_t size_extrafield_global, const char *comment, uint16_t method, int level, int raw, int windowBits,
  1096. int memLevel, int strategy, const char *password, ZIP_UNUSED uint32_t crc_for_crypting)
  1097. {
  1098. return zipOpenNewFileInZip4_64(file, filename, zipfi, extrafield_local, size_extrafield_local,
  1099. extrafield_global, size_extrafield_global, comment, method, level, raw, windowBits, memLevel,
  1100. strategy, password, crc_for_crypting, VERSIONMADEBY, 0, 0);
  1101. }
  1102. extern int ZEXPORT zipOpenNewFileInZip3_64(zipFile file, const char *filename, const zip_fileinfo *zipfi,
  1103. const void *extrafield_local, uint16_t size_extrafield_local, const void *extrafield_global,
  1104. uint16_t size_extrafield_global, const char *comment, uint16_t method, int level, int raw, int windowBits,
  1105. int memLevel, int strategy, const char *password, ZIP_UNUSED uint32_t crc_for_crypting, int zip64)
  1106. {
  1107. return zipOpenNewFileInZip4_64(file, filename, zipfi, extrafield_local, size_extrafield_local,
  1108. extrafield_global, size_extrafield_global, comment, method, level, raw, windowBits, memLevel, strategy,
  1109. password, crc_for_crypting, VERSIONMADEBY, 0, zip64);
  1110. }
  1111. extern int ZEXPORT zipOpenNewFileInZip2(zipFile file, const char *filename, const zip_fileinfo *zipfi,
  1112. const void *extrafield_local, uint16_t size_extrafield_local, const void *extrafield_global,
  1113. uint16_t size_extrafield_global, const char *comment, uint16_t method, int level, int raw)
  1114. {
  1115. return zipOpenNewFileInZip4_64(file, filename, zipfi, extrafield_local, size_extrafield_local,
  1116. extrafield_global, size_extrafield_global, comment, method, level, raw, -MAX_WBITS, DEF_MEM_LEVEL,
  1117. Z_DEFAULT_STRATEGY, NULL, 0, VERSIONMADEBY, 0, 0);
  1118. }
  1119. extern int ZEXPORT zipOpenNewFileInZip2_64(zipFile file, const char *filename, const zip_fileinfo *zipfi,
  1120. const void *extrafield_local, uint16_t size_extrafield_local, const void *extrafield_global,
  1121. uint16_t size_extrafield_global, const char *comment, uint16_t method, int level, int raw, int zip64)
  1122. {
  1123. return zipOpenNewFileInZip4_64(file, filename, zipfi, extrafield_local, size_extrafield_local,
  1124. extrafield_global, size_extrafield_global, comment, method, level, raw, -MAX_WBITS, DEF_MEM_LEVEL,
  1125. Z_DEFAULT_STRATEGY, NULL, 0, VERSIONMADEBY, 0, zip64);
  1126. }
  1127. extern int ZEXPORT zipOpenNewFileInZip64(zipFile file, const char *filename, const zip_fileinfo *zipfi,
  1128. const void *extrafield_local, uint16_t size_extrafield_local, const void *extrafield_global,
  1129. uint16_t size_extrafield_global, const char *comment, uint16_t method, int level, int zip64)
  1130. {
  1131. return zipOpenNewFileInZip4_64(file, filename, zipfi, extrafield_local, size_extrafield_local,
  1132. extrafield_global, size_extrafield_global, comment, method, level, 0, -MAX_WBITS, DEF_MEM_LEVEL,
  1133. Z_DEFAULT_STRATEGY, NULL, 0, VERSIONMADEBY, 0, zip64);
  1134. }
  1135. extern int ZEXPORT zipOpenNewFileInZip(zipFile file, const char *filename, const zip_fileinfo *zipfi,
  1136. const void *extrafield_local, uint16_t size_extrafield_local, const void *extrafield_global,
  1137. uint16_t size_extrafield_global, const char *comment, uint16_t method, int level)
  1138. {
  1139. return zipOpenNewFileInZip4_64(file, filename, zipfi, extrafield_local, size_extrafield_local,
  1140. extrafield_global, size_extrafield_global, comment, method, level, 0, -MAX_WBITS, DEF_MEM_LEVEL,
  1141. Z_DEFAULT_STRATEGY, NULL, 0, VERSIONMADEBY, 0, 0);
  1142. }
  1143. /* Flushes the write buffer to disk */
  1144. static int zipFlushWriteBuffer(zip64_internal *zi)
  1145. {
  1146. uint64_t size_available = 0;
  1147. uint32_t written = 0;
  1148. uint32_t total_written = 0;
  1149. uint32_t write = 0;
  1150. uint32_t max_write = 0;
  1151. int err = ZIP_OK;
  1152. if ((zi->ci.flag & 1) != 0)
  1153. {
  1154. #ifndef NOCRYPT
  1155. #ifdef HAVE_AES
  1156. if (zi->ci.method == AES_METHOD)
  1157. {
  1158. fcrypt_encrypt(zi->ci.buffered_data, zi->ci.pos_in_buffered_data, &zi->ci.aes_ctx);
  1159. }
  1160. else
  1161. #endif
  1162. {
  1163. uint32_t i = 0;
  1164. uint8_t t = 0;
  1165. for (i = 0; i < zi->ci.pos_in_buffered_data; i++)
  1166. zi->ci.buffered_data[i] = (uint8_t)zencode(zi->ci.keys, zi->ci.pcrc_32_tab, zi->ci.buffered_data[i], t);
  1167. }
  1168. #endif
  1169. }
  1170. write = zi->ci.pos_in_buffered_data;
  1171. do
  1172. {
  1173. max_write = write;
  1174. if (zi->disk_size > 0)
  1175. {
  1176. zipGetDiskSizeAvailable((zipFile)zi, &size_available);
  1177. if (size_available == 0)
  1178. {
  1179. err = zipGoToNextDisk((zipFile)zi);
  1180. if (err != ZIP_OK)
  1181. return err;
  1182. }
  1183. if (size_available < (uint64_t)max_write)
  1184. max_write = (uint32_t)size_available;
  1185. }
  1186. written = ZWRITE64(zi->z_filefunc, zi->filestream, zi->ci.buffered_data + total_written, max_write);
  1187. if (written != max_write)
  1188. {
  1189. err = ZIP_ERRNO;
  1190. break;
  1191. }
  1192. total_written += written;
  1193. write -= written;
  1194. }
  1195. while (write > 0);
  1196. zi->ci.total_compressed += zi->ci.pos_in_buffered_data;
  1197. #ifdef HAVE_BZIP2
  1198. if (zi->ci.compression_method == Z_BZIP2ED)
  1199. {
  1200. zi->ci.total_uncompressed += zi->ci.bstream.total_in_lo32;
  1201. zi->ci.bstream.total_in_lo32 = 0;
  1202. zi->ci.bstream.total_in_hi32 = 0;
  1203. }
  1204. else
  1205. #endif
  1206. {
  1207. zi->ci.total_uncompressed += zi->ci.stream.total_in;
  1208. zi->ci.stream.total_in = 0;
  1209. }
  1210. zi->ci.pos_in_buffered_data = 0;
  1211. return err;
  1212. }
  1213. extern int ZEXPORT zipWriteInFileInZip(zipFile file, const void *buf, uint32_t len)
  1214. {
  1215. zip64_internal *zi = NULL;
  1216. int err = ZIP_OK;
  1217. if (file == NULL)
  1218. return ZIP_PARAMERROR;
  1219. zi = (zip64_internal*)file;
  1220. if (zi->in_opened_file_inzip == 0)
  1221. return ZIP_PARAMERROR;
  1222. zi->ci.crc32 = (uint32_t)crc32(zi->ci.crc32, buf, len);
  1223. #ifdef HAVE_BZIP2
  1224. if ((zi->ci.compression_method == Z_BZIP2ED) && (!zi->ci.raw))
  1225. {
  1226. zi->ci.bstream.next_in = (void*)buf;
  1227. zi->ci.bstream.avail_in = len;
  1228. err = BZ_RUN_OK;
  1229. while ((err == BZ_RUN_OK) && (zi->ci.bstream.avail_in > 0))
  1230. {
  1231. if (zi->ci.bstream.avail_out == 0)
  1232. {
  1233. err = zipFlushWriteBuffer(zi);
  1234. zi->ci.bstream.avail_out = (uint16_t)Z_BUFSIZE;
  1235. zi->ci.bstream.next_out = (char*)zi->ci.buffered_data;
  1236. }
  1237. else
  1238. {
  1239. uint32_t total_out_before_lo = zi->ci.bstream.total_out_lo32;
  1240. uint32_t total_out_before_hi = zi->ci.bstream.total_out_hi32;
  1241. err = BZ2_bzCompress(&zi->ci.bstream, BZ_RUN);
  1242. zi->ci.pos_in_buffered_data += (uint16_t)(zi->ci.bstream.total_out_lo32 - total_out_before_lo);
  1243. }
  1244. }
  1245. if (err == BZ_RUN_OK)
  1246. err = ZIP_OK;
  1247. }
  1248. else
  1249. #endif
  1250. {
  1251. zi->ci.stream.next_in = (uint8_t*)buf;
  1252. zi->ci.stream.avail_in = len;
  1253. while ((err == ZIP_OK) && (zi->ci.stream.avail_in > 0))
  1254. {
  1255. if (zi->ci.stream.avail_out == 0)
  1256. {
  1257. err = zipFlushWriteBuffer(zi);
  1258. zi->ci.stream.avail_out = Z_BUFSIZE;
  1259. zi->ci.stream.next_out = zi->ci.buffered_data;
  1260. }
  1261. if (err != ZIP_OK)
  1262. break;
  1263. if ((zi->ci.compression_method == Z_DEFLATED) && (!zi->ci.raw))
  1264. {
  1265. #ifdef HAVE_APPLE_COMPRESSION
  1266. uLong total_out_before = zi->ci.stream.total_out;
  1267. zi->ci.astream.src_ptr = zi->ci.stream.next_in;
  1268. zi->ci.astream.src_size = zi->ci.stream.avail_in;
  1269. zi->ci.astream.dst_ptr = zi->ci.stream.next_out;
  1270. zi->ci.astream.dst_size = zi->ci.stream.avail_out;
  1271. compression_status status = 0;
  1272. compression_stream_flags flags = 0;
  1273. status = compression_stream_process(&zi->ci.astream, flags);
  1274. uLong total_out_after = len - zi->ci.astream.src_size;
  1275. zi->ci.stream.next_in = zi->ci.astream.src_ptr;
  1276. zi->ci.stream.avail_in = zi->ci.astream.src_size;
  1277. zi->ci.stream.next_out = zi->ci.astream.dst_ptr;
  1278. zi->ci.stream.avail_out = zi->ci.astream.dst_size;
  1279. zi->ci.stream.total_in += total_out_after;
  1280. //zi->ci.stream.total_out += copy_this;
  1281. zi->ci.pos_in_buffered_data += total_out_after;
  1282. if (status == COMPRESSION_STATUS_ERROR)
  1283. err = ZIP_INTERNALERROR;
  1284. #else
  1285. uint32_t total_out_before = (uint32_t)zi->ci.stream.total_out;
  1286. err = deflate(&zi->ci.stream, Z_NO_FLUSH);
  1287. zi->ci.pos_in_buffered_data += (uint32_t)(zi->ci.stream.total_out - total_out_before);
  1288. #endif
  1289. }
  1290. else
  1291. {
  1292. uint32_t copy_this = 0;
  1293. uint32_t i = 0;
  1294. if (zi->ci.stream.avail_in < zi->ci.stream.avail_out)
  1295. copy_this = zi->ci.stream.avail_in;
  1296. else
  1297. copy_this = zi->ci.stream.avail_out;
  1298. for (i = 0; i < copy_this; i++)
  1299. *(((char*)zi->ci.stream.next_out)+i) =
  1300. *(((const char*)zi->ci.stream.next_in)+i);
  1301. zi->ci.stream.avail_in -= copy_this;
  1302. zi->ci.stream.avail_out -= copy_this;
  1303. zi->ci.stream.next_in += copy_this;
  1304. zi->ci.stream.next_out += copy_this;
  1305. zi->ci.stream.total_in += copy_this;
  1306. zi->ci.stream.total_out += copy_this;
  1307. zi->ci.pos_in_buffered_data += copy_this;
  1308. }
  1309. }
  1310. }
  1311. return err;
  1312. }
  1313. extern int ZEXPORT zipCloseFileInZipRaw64(zipFile file, uint64_t uncompressed_size, uint32_t crc32)
  1314. {
  1315. zip64_internal *zi = NULL;
  1316. uint16_t extra_data_size = 0;
  1317. uint32_t i = 0;
  1318. unsigned char *extra_info = NULL;
  1319. int err = ZIP_OK;
  1320. if (file == NULL)
  1321. return ZIP_PARAMERROR;
  1322. zi = (zip64_internal*)file;
  1323. if (zi->in_opened_file_inzip == 0)
  1324. return ZIP_PARAMERROR;
  1325. zi->ci.stream.avail_in = 0;
  1326. if (!zi->ci.raw)
  1327. {
  1328. if (zi->ci.compression_method == Z_DEFLATED)
  1329. {
  1330. while (err == ZIP_OK)
  1331. {
  1332. uint32_t total_out_before = 0;
  1333. if (zi->ci.stream.avail_out == 0)
  1334. {
  1335. err = zipFlushWriteBuffer(zi);
  1336. zi->ci.stream.avail_out = Z_BUFSIZE;
  1337. zi->ci.stream.next_out = zi->ci.buffered_data;
  1338. }
  1339. if (err != ZIP_OK)
  1340. break;
  1341. #ifdef HAVE_APPLE_COMPRESSION
  1342. total_out_before = zi->ci.stream.total_out;
  1343. zi->ci.astream.src_ptr = zi->ci.stream.next_in;
  1344. zi->ci.astream.src_size = zi->ci.stream.avail_in;
  1345. zi->ci.astream.dst_ptr = zi->ci.stream.next_out;
  1346. zi->ci.astream.dst_size = zi->ci.stream.avail_out;
  1347. compression_status status = 0;
  1348. status = compression_stream_process(&zi->ci.astream, COMPRESSION_STREAM_FINALIZE);
  1349. uint32_t total_out_after = Z_BUFSIZE - zi->ci.astream.dst_size;
  1350. zi->ci.stream.next_in = zi->ci.astream.src_ptr;
  1351. zi->ci.stream.avail_in = zi->ci.astream.src_size;
  1352. zi->ci.stream.next_out = zi->ci.astream.dst_ptr;
  1353. zi->ci.stream.avail_out = zi->ci.astream.dst_size;
  1354. //zi->ci.stream.total_in += total_out_after;
  1355. //zi->ci.stream.total_out += copy_this;
  1356. zi->ci.pos_in_buffered_data += total_out_after;
  1357. if (status == COMPRESSION_STATUS_ERROR)
  1358. {
  1359. err = ZIP_INTERNALERROR;
  1360. }
  1361. else if (status == COMPRESSION_STATUS_END)
  1362. {
  1363. err = Z_STREAM_END;
  1364. }
  1365. #else
  1366. total_out_before = (uint32_t)zi->ci.stream.total_out;
  1367. err = deflate(&zi->ci.stream, Z_FINISH);
  1368. zi->ci.pos_in_buffered_data += (uint16_t)(zi->ci.stream.total_out - total_out_before);
  1369. #endif
  1370. }
  1371. }
  1372. else if (zi->ci.compression_method == Z_BZIP2ED)
  1373. {
  1374. #ifdef HAVE_BZIP2
  1375. err = BZ_FINISH_OK;
  1376. while (err == BZ_FINISH_OK)
  1377. {
  1378. uint32_t total_out_before = 0;
  1379. if (zi->ci.bstream.avail_out == 0)
  1380. {
  1381. err = zipFlushWriteBuffer(zi);
  1382. zi->ci.bstream.avail_out = (uint16_t)Z_BUFSIZE;
  1383. zi->ci.bstream.next_out = (char*)zi->ci.buffered_data;
  1384. }
  1385. total_out_before = zi->ci.bstream.total_out_lo32;
  1386. err = BZ2_bzCompress(&zi->ci.bstream, BZ_FINISH);
  1387. if (err == BZ_STREAM_END)
  1388. err = Z_STREAM_END;
  1389. zi->ci.pos_in_buffered_data += (uint16_t)(zi->ci.bstream.total_out_lo32 - total_out_before);
  1390. }
  1391. if (err == BZ_FINISH_OK)
  1392. err = ZIP_OK;
  1393. #endif
  1394. }
  1395. }
  1396. if (err == Z_STREAM_END)
  1397. err = ZIP_OK; /* this is normal */
  1398. if ((zi->ci.pos_in_buffered_data > 0) && (err == ZIP_OK))
  1399. {
  1400. err = zipFlushWriteBuffer(zi);
  1401. }
  1402. #ifdef HAVE_AES
  1403. if (zi->ci.method == AES_METHOD)
  1404. {
  1405. unsigned char authcode[AES_AUTHCODESIZE];
  1406. fcrypt_end(authcode, &zi->ci.aes_ctx);
  1407. if (ZWRITE64(zi->z_filefunc, zi->filestream, authcode, AES_AUTHCODESIZE) != AES_AUTHCODESIZE)
  1408. err = ZIP_ERRNO;
  1409. }
  1410. #endif
  1411. if (!zi->ci.raw)
  1412. {
  1413. if (zi->ci.compression_method == Z_DEFLATED)
  1414. {
  1415. int tmp_err = 0;
  1416. #ifdef HAVE_APPLE_COMPRESSION
  1417. tmp_err = compression_stream_destroy(&zi->ci.astream);
  1418. #else
  1419. tmp_err = deflateEnd(&zi->ci.stream);
  1420. #endif
  1421. if (err == ZIP_OK)
  1422. err = tmp_err;
  1423. zi->ci.stream_initialised = 0;
  1424. }
  1425. #ifdef HAVE_BZIP2
  1426. else if (zi->ci.compression_method == Z_BZIP2ED)
  1427. {
  1428. int tmperr = BZ2_bzCompressEnd(&zi->ci.bstream);
  1429. if (err == ZIP_OK)
  1430. err = tmperr;
  1431. zi->ci.stream_initialised = 0;
  1432. }
  1433. #endif
  1434. crc32 = zi->ci.crc32;
  1435. uncompressed_size = zi->ci.total_uncompressed;
  1436. }
  1437. /* Write data descriptor */
  1438. if (err == ZIP_OK)
  1439. err = zipWriteValue(&zi->z_filefunc, zi->filestream, (uint32_t)DATADESCRIPTORMAGIC, 4);
  1440. if (err == ZIP_OK)
  1441. err = zipWriteValue(&zi->z_filefunc, zi->filestream, crc32, 4);
  1442. if (err == ZIP_OK)
  1443. {
  1444. if (zi->ci.zip64)
  1445. err = zipWriteValue(&zi->z_filefunc, zi->filestream, zi->ci.total_compressed, 8);
  1446. else
  1447. err = zipWriteValue(&zi->z_filefunc, zi->filestream, (uint32_t)zi->ci.total_compressed, 4);
  1448. }
  1449. if (err == ZIP_OK)
  1450. {
  1451. if (zi->ci.zip64)
  1452. err = zipWriteValue(&zi->z_filefunc, zi->filestream, uncompressed_size, 8);
  1453. else
  1454. err = zipWriteValue(&zi->z_filefunc, zi->filestream, (uint32_t)uncompressed_size, 4);
  1455. }
  1456. /* Update crc and sizes to central directory */
  1457. zipWriteValueToMemory(zi->ci.central_header + 16, crc32, 4); /* crc */
  1458. if (zi->ci.total_compressed >= UINT32_MAX)
  1459. zipWriteValueToMemory(zi->ci.central_header + 20, UINT32_MAX, 4); /* compr size */
  1460. else
  1461. zipWriteValueToMemory(zi->ci.central_header + 20, zi->ci.total_compressed, 4); /* compr size */
  1462. if (uncompressed_size >= UINT32_MAX)
  1463. zipWriteValueToMemory(zi->ci.central_header + 24, UINT32_MAX, 4); /* uncompr size */
  1464. else
  1465. zipWriteValueToMemory(zi->ci.central_header + 24, uncompressed_size, 4); /* uncompr size */
  1466. if (zi->ci.stream.data_type == Z_ASCII)
  1467. zipWriteValueToMemory(zi->ci.central_header + 36, (uint16_t)Z_ASCII, 2); /* internal file attrib */
  1468. /* Add ZIP64 extra info field for uncompressed size */
  1469. if (uncompressed_size >= UINT32_MAX)
  1470. extra_data_size += 8;
  1471. /* Add ZIP64 extra info field for compressed size */
  1472. if (zi->ci.total_compressed >= UINT32_MAX)
  1473. extra_data_size += 8;
  1474. /* Add ZIP64 extra info field for relative offset to local file header of current file */
  1475. if (zi->ci.pos_local_header >= UINT32_MAX)
  1476. extra_data_size += 8;
  1477. /* Add ZIP64 extra info header to central directory */
  1478. if (extra_data_size > 0)
  1479. {
  1480. if ((uint32_t)(extra_data_size + 4) > zi->ci.size_centralextrafree)
  1481. return ZIP_BADZIPFILE;
  1482. extra_info = (unsigned char*)zi->ci.central_header + zi->ci.size_centralheader;
  1483. zipWriteValueToMemoryAndMove(&extra_info, 0x0001, 2);
  1484. zipWriteValueToMemoryAndMove(&extra_info, extra_data_size, 2);
  1485. if (uncompressed_size >= UINT32_MAX)
  1486. zipWriteValueToMemoryAndMove(&extra_info, uncompressed_size, 8);
  1487. if (zi->ci.total_compressed >= UINT32_MAX)
  1488. zipWriteValueToMemoryAndMove(&extra_info, zi->ci.total_compressed, 8);
  1489. if (zi->ci.pos_local_header >= UINT32_MAX)
  1490. zipWriteValueToMemoryAndMove(&extra_info, zi->ci.pos_local_header, 8);
  1491. zi->ci.size_centralextrafree -= extra_data_size + 4;
  1492. zi->ci.size_centralheader += extra_data_size + 4;
  1493. zi->ci.size_centralextra += extra_data_size + 4;
  1494. zipWriteValueToMemory(zi->ci.central_header + 30, zi->ci.size_centralextra, 2);
  1495. }
  1496. #ifdef HAVE_AES
  1497. /* Write AES extra info header to central directory */
  1498. if (zi->ci.method == AES_METHOD)
  1499. {
  1500. extra_info = (unsigned char*)zi->ci.central_header + zi->ci.size_centralheader;
  1501. extra_data_size = 7;
  1502. if ((uint32_t)(extra_data_size + 4) > zi->ci.size_centralextrafree)
  1503. return ZIP_BADZIPFILE;
  1504. zipWriteValueToMemoryAndMove(&extra_info, 0x9901, 2);
  1505. zipWriteValueToMemoryAndMove(&extra_info, extra_data_size, 2);
  1506. zipWriteValueToMemoryAndMove(&extra_info, AES_VERSION, 2);
  1507. zipWriteValueToMemoryAndMove(&extra_info, 'A', 1);
  1508. zipWriteValueToMemoryAndMove(&extra_info, 'E', 1);
  1509. zipWriteValueToMemoryAndMove(&extra_info, AES_ENCRYPTIONMODE, 1);
  1510. zipWriteValueToMemoryAndMove(&extra_info, zi->ci.compression_method, 2);
  1511. zi->ci.size_centralextrafree -= extra_data_size + 4;
  1512. zi->ci.size_centralheader += extra_data_size + 4;
  1513. zi->ci.size_centralextra += extra_data_size + 4;
  1514. zipWriteValueToMemory(zi->ci.central_header + 30, zi->ci.size_centralextra, 2);
  1515. }
  1516. #endif
  1517. /* Restore comment to correct position */
  1518. for (i = 0; i < zi->ci.size_comment; i++)
  1519. zi->ci.central_header[zi->ci.size_centralheader+i] =
  1520. zi->ci.central_header[zi->ci.size_centralheader+zi->ci.size_centralextrafree+i];
  1521. zi->ci.size_centralheader += zi->ci.size_comment;
  1522. if (err == ZIP_OK)
  1523. err = add_data_in_datablock(&zi->central_dir, zi->ci.central_header, zi->ci.size_centralheader);
  1524. free(zi->ci.central_header);
  1525. zi->number_entry++;
  1526. zi->in_opened_file_inzip = 0;
  1527. return err;
  1528. }
  1529. extern int ZEXPORT zipCloseFileInZipRaw(zipFile file, uint32_t uncompressed_size, uint32_t crc32)
  1530. {
  1531. return zipCloseFileInZipRaw64(file, uncompressed_size, crc32);
  1532. }
  1533. extern int ZEXPORT zipCloseFileInZip(zipFile file)
  1534. {
  1535. return zipCloseFileInZipRaw(file, 0, 0);
  1536. }
  1537. extern int ZEXPORT zipClose(zipFile file, const char *global_comment)
  1538. {
  1539. return zipClose_64(file, global_comment);
  1540. }
  1541. extern int ZEXPORT zipClose_64(zipFile file, const char *global_comment)
  1542. {
  1543. return zipClose2_64(file, global_comment, VERSIONMADEBY);
  1544. }
  1545. extern int ZEXPORT zipClose2_64(zipFile file, const char *global_comment, uint16_t version_madeby)
  1546. {
  1547. zip64_internal *zi = NULL;
  1548. uint32_t size_centraldir = 0;
  1549. uint16_t size_global_comment = 0;
  1550. uint64_t centraldir_pos_inzip = 0;
  1551. uint64_t pos = 0;
  1552. uint64_t cd_pos = 0;
  1553. uint32_t write = 0;
  1554. int err = ZIP_OK;
  1555. if (file == NULL)
  1556. return ZIP_PARAMERROR;
  1557. zi = (zip64_internal*)file;
  1558. if (zi->in_opened_file_inzip == 1)
  1559. err = zipCloseFileInZip(file);
  1560. #ifndef NO_ADDFILEINEXISTINGZIP
  1561. if (global_comment == NULL)
  1562. global_comment = zi->globalcomment;
  1563. #endif
  1564. if (zi->filestream != zi->filestream_with_CD)
  1565. {
  1566. if (ZCLOSE64(zi->z_filefunc, zi->filestream) != 0)
  1567. if (err == ZIP_OK)
  1568. err = ZIP_ERRNO;
  1569. if (zi->disk_size > 0)
  1570. zi->number_disk_with_CD = zi->number_disk + 1;
  1571. zi->filestream = zi->filestream_with_CD;
  1572. }
  1573. centraldir_pos_inzip = ZTELL64(zi->z_filefunc, zi->filestream);
  1574. if (err == ZIP_OK)
  1575. {
  1576. linkedlist_datablock_internal *ldi = zi->central_dir.first_block;
  1577. while (ldi != NULL)
  1578. {
  1579. if ((err == ZIP_OK) && (ldi->filled_in_this_block > 0))
  1580. {
  1581. write = ZWRITE64(zi->z_filefunc, zi->filestream, ldi->data, ldi->filled_in_this_block);
  1582. if (write != ldi->filled_in_this_block)
  1583. err = ZIP_ERRNO;
  1584. }
  1585. size_centraldir += ldi->filled_in_this_block;
  1586. ldi = ldi->next_datablock;
  1587. }
  1588. }
  1589. free_linkedlist(&(zi->central_dir));
  1590. pos = centraldir_pos_inzip - zi->add_position_when_writting_offset;
  1591. /* Write the ZIP64 central directory header */
  1592. if (pos >= UINT32_MAX || zi->number_entry > UINT32_MAX)
  1593. {
  1594. uint64_t zip64_eocd_pos_inzip = ZTELL64(zi->z_filefunc, zi->filestream);
  1595. uint32_t zip64_datasize = 44;
  1596. err = zipWriteValue(&zi->z_filefunc, zi->filestream, (uint32_t)ZIP64ENDHEADERMAGIC, 4);
  1597. /* Size of this 'zip64 end of central directory' */
  1598. if (err == ZIP_OK)
  1599. err = zipWriteValue(&zi->z_filefunc, zi->filestream, (uint64_t)zip64_datasize, 8);
  1600. /* Version made by */
  1601. if (err == ZIP_OK)
  1602. err = zipWriteValue(&zi->z_filefunc, zi->filestream, version_madeby, 2);
  1603. /* version needed */
  1604. if (err == ZIP_OK)
  1605. err = zipWriteValue(&zi->z_filefunc, zi->filestream, (uint16_t)45, 2);
  1606. /* Number of this disk */
  1607. if (err == ZIP_OK)
  1608. err = zipWriteValue(&zi->z_filefunc, zi->filestream, zi->number_disk_with_CD, 4);
  1609. /* Number of the disk with the start of the central directory */
  1610. if (err == ZIP_OK)
  1611. err = zipWriteValue(&zi->z_filefunc, zi->filestream, zi->number_disk_with_CD, 4);
  1612. /* Total number of entries in the central dir on this disk */
  1613. if (err == ZIP_OK)
  1614. err = zipWriteValue(&zi->z_filefunc, zi->filestream, zi->number_entry, 8);
  1615. /* Total number of entries in the central dir */
  1616. if (err == ZIP_OK)
  1617. err = zipWriteValue(&zi->z_filefunc, zi->filestream, zi->number_entry, 8);
  1618. /* Size of the central directory */
  1619. if (err == ZIP_OK)
  1620. err = zipWriteValue(&zi->z_filefunc, zi->filestream, (uint64_t)size_centraldir, 8);
  1621. if (err == ZIP_OK)
  1622. {
  1623. /* Offset of start of central directory with respect to the starting disk number */
  1624. cd_pos = centraldir_pos_inzip - zi->add_position_when_writting_offset;
  1625. err = zipWriteValue(&zi->z_filefunc, zi->filestream, cd_pos, 8);
  1626. }
  1627. if (err == ZIP_OK)
  1628. err = zipWriteValue(&zi->z_filefunc, zi->filestream, (uint32_t)ZIP64ENDLOCHEADERMAGIC, 4);
  1629. /* Number of the disk with the start of the central directory */
  1630. if (err == ZIP_OK)
  1631. err = zipWriteValue(&zi->z_filefunc, zi->filestream, zi->number_disk_with_CD, 4);
  1632. /* Relative offset to the Zip64EndOfCentralDirectory */
  1633. if (err == ZIP_OK)
  1634. {
  1635. cd_pos = zip64_eocd_pos_inzip - zi->add_position_when_writting_offset;
  1636. err = zipWriteValue(&zi->z_filefunc, zi->filestream, cd_pos, 8);
  1637. }
  1638. /* Number of the disk with the start of the central directory */
  1639. if (err == ZIP_OK)
  1640. err = zipWriteValue(&zi->z_filefunc, zi->filestream, zi->number_disk_with_CD + 1, 4);
  1641. }
  1642. /* Write the central directory header */
  1643. /* Signature */
  1644. if (err == ZIP_OK)
  1645. err = zipWriteValue(&zi->z_filefunc, zi->filestream, (uint32_t)ENDHEADERMAGIC, 4);
  1646. /* Number of this disk */
  1647. if (err == ZIP_OK)
  1648. err = zipWriteValue(&zi->z_filefunc, zi->filestream, (uint16_t)zi->number_disk_with_CD, 2);
  1649. /* Number of the disk with the start of the central directory */
  1650. if (err == ZIP_OK)
  1651. err = zipWriteValue(&zi->z_filefunc, zi->filestream, (uint16_t)zi->number_disk_with_CD, 2);
  1652. /* Total number of entries in the central dir on this disk */
  1653. if (err == ZIP_OK)
  1654. {
  1655. if (zi->number_entry >= UINT16_MAX)
  1656. err = zipWriteValue(&zi->z_filefunc, zi->filestream, UINT16_MAX, 2); /* use value in ZIP64 record */
  1657. else
  1658. err = zipWriteValue(&zi->z_filefunc, zi->filestream, (uint16_t)zi->number_entry, 2);
  1659. }
  1660. /* Total number of entries in the central dir */
  1661. if (err == ZIP_OK)
  1662. {
  1663. if (zi->number_entry >= UINT16_MAX)
  1664. err = zipWriteValue(&zi->z_filefunc, zi->filestream, UINT16_MAX, 2); /* use value in ZIP64 record */
  1665. else
  1666. err = zipWriteValue(&zi->z_filefunc, zi->filestream, (uint16_t)zi->number_entry, 2);
  1667. }
  1668. /* Size of the central directory */
  1669. if (err == ZIP_OK)
  1670. err = zipWriteValue(&zi->z_filefunc, zi->filestream, size_centraldir, 4);
  1671. /* Offset of start of central directory with respect to the starting disk number */
  1672. if (err == ZIP_OK)
  1673. {
  1674. cd_pos = centraldir_pos_inzip - zi->add_position_when_writting_offset;
  1675. if (pos >= UINT32_MAX)
  1676. err = zipWriteValue(&zi->z_filefunc, zi->filestream, UINT32_MAX, 4);
  1677. else
  1678. err = zipWriteValue(&zi->z_filefunc, zi->filestream, (uint32_t)cd_pos, 4);
  1679. }
  1680. /* Write global comment */
  1681. if (global_comment != NULL)
  1682. size_global_comment = (uint16_t)strlen(global_comment);
  1683. if (err == ZIP_OK)
  1684. err = zipWriteValue(&zi->z_filefunc, zi->filestream, size_global_comment, 2);
  1685. if (err == ZIP_OK && size_global_comment > 0)
  1686. {
  1687. if (ZWRITE64(zi->z_filefunc, zi->filestream, global_comment, size_global_comment) != size_global_comment)
  1688. err = ZIP_ERRNO;
  1689. }
  1690. if ((ZCLOSE64(zi->z_filefunc, zi->filestream) != 0) && (err == ZIP_OK))
  1691. err = ZIP_ERRNO;
  1692. #ifndef NO_ADDFILEINEXISTINGZIP
  1693. TRYFREE(zi->globalcomment);
  1694. #endif
  1695. TRYFREE(zi);
  1696. return err;
  1697. }