zip_close.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. /*
  2. zip_close.c -- close zip archive and update changes
  3. Copyright (C) 1999-2021 Dieter Baron and Thomas Klausner
  4. This file is part of libzip, a library to manipulate ZIP archives.
  5. The authors can be contacted at <libzip@nih.at>
  6. Redistribution and use in source and binary forms, with or without
  7. modification, are permitted provided that the following conditions
  8. are met:
  9. 1. Redistributions of source code must retain the above copyright
  10. notice, this list of conditions and the following disclaimer.
  11. 2. Redistributions in binary form must reproduce the above copyright
  12. notice, this list of conditions and the following disclaimer in
  13. the documentation and/or other materials provided with the
  14. distribution.
  15. 3. The names of the authors may not be used to endorse or promote
  16. products derived from this software without specific prior
  17. written permission.
  18. THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
  19. OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
  22. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  24. GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  25. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  26. IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  27. OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  28. IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include "zipint.h"
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #ifdef _WIN32
  34. #include <fcntl.h>
  35. #include <io.h>
  36. #endif
  37. static int add_data(zip_t *, zip_source_t *, zip_dirent_t *, zip_uint32_t);
  38. static int copy_data(zip_t *, zip_uint64_t);
  39. static int copy_source(zip_t *, zip_source_t *, zip_int64_t);
  40. static int write_cdir(zip_t *, const zip_filelist_t *, zip_uint64_t);
  41. static int write_data_descriptor(zip_t *za, const zip_dirent_t *dirent, int is_zip64);
  42. ZIP_EXTERN int
  43. zip_close(zip_t *za) {
  44. zip_uint64_t i, j, survivors, unchanged_offset;
  45. zip_int64_t off;
  46. int error;
  47. zip_filelist_t *filelist;
  48. int changed;
  49. if (za == NULL)
  50. return -1;
  51. changed = _zip_changed(za, &survivors);
  52. /* don't create zip files with no entries */
  53. if (survivors == 0) {
  54. if ((za->open_flags & ZIP_TRUNCATE) || changed) {
  55. if (zip_source_remove(za->src) < 0) {
  56. if (!((zip_error_code_zip(zip_source_error(za->src)) == ZIP_ER_REMOVE) && (zip_error_code_system(zip_source_error(za->src)) == ENOENT))) {
  57. _zip_error_set_from_source(&za->error, za->src);
  58. return -1;
  59. }
  60. }
  61. }
  62. zip_discard(za);
  63. return 0;
  64. }
  65. if (!changed) {
  66. zip_discard(za);
  67. return 0;
  68. }
  69. if (survivors > za->nentry) {
  70. zip_error_set(&za->error, ZIP_ER_INTERNAL, 0);
  71. return -1;
  72. }
  73. if ((filelist = (zip_filelist_t *)malloc(sizeof(filelist[0]) * (size_t)survivors)) == NULL)
  74. return -1;
  75. unchanged_offset = ZIP_UINT64_MAX;
  76. /* create list of files with index into original archive */
  77. for (i = j = 0; i < za->nentry; i++) {
  78. if (za->entry[i].orig != NULL && ZIP_ENTRY_HAS_CHANGES(&za->entry[i])) {
  79. unchanged_offset = ZIP_MIN(unchanged_offset, za->entry[i].orig->offset);
  80. }
  81. if (za->entry[i].deleted) {
  82. continue;
  83. }
  84. if (j >= survivors) {
  85. free(filelist);
  86. zip_error_set(&za->error, ZIP_ER_INTERNAL, 0);
  87. return -1;
  88. }
  89. filelist[j].idx = i;
  90. j++;
  91. }
  92. if (j < survivors) {
  93. free(filelist);
  94. zip_error_set(&za->error, ZIP_ER_INTERNAL, 0);
  95. return -1;
  96. }
  97. if ((zip_source_supports(za->src) & ZIP_SOURCE_MAKE_COMMAND_BITMASK(ZIP_SOURCE_BEGIN_WRITE_CLONING)) == 0) {
  98. unchanged_offset = 0;
  99. }
  100. else {
  101. if (unchanged_offset == ZIP_UINT64_MAX) {
  102. /* we're keeping all file data, find the end of the last one */
  103. zip_uint64_t last_index = ZIP_UINT64_MAX;
  104. unchanged_offset = 0;
  105. for (i = 0; i < za->nentry; i++) {
  106. if (za->entry[i].orig != NULL) {
  107. if (za->entry[i].orig->offset >= unchanged_offset) {
  108. unchanged_offset = za->entry[i].orig->offset;
  109. last_index = i;
  110. }
  111. }
  112. }
  113. if (last_index != ZIP_UINT64_MAX) {
  114. if ((unchanged_offset = _zip_file_get_end(za, last_index, &za->error)) == 0) {
  115. free(filelist);
  116. return -1;
  117. }
  118. }
  119. }
  120. if (unchanged_offset > 0) {
  121. if (zip_source_begin_write_cloning(za->src, unchanged_offset) < 0) {
  122. /* cloning not supported, need to copy everything */
  123. unchanged_offset = 0;
  124. }
  125. }
  126. }
  127. if (unchanged_offset == 0) {
  128. if (zip_source_begin_write(za->src) < 0) {
  129. _zip_error_set_from_source(&za->error, za->src);
  130. free(filelist);
  131. return -1;
  132. }
  133. }
  134. if (_zip_progress_start(za->progress) != 0) {
  135. zip_error_set(&za->error, ZIP_ER_CANCELLED, 0);
  136. zip_source_rollback_write(za->src);
  137. free(filelist);
  138. return -1;
  139. }
  140. error = 0;
  141. for (j = 0; j < survivors; j++) {
  142. int new_data;
  143. zip_entry_t *entry;
  144. zip_dirent_t *de;
  145. if (_zip_progress_subrange(za->progress, (double)j / (double)survivors, (double)(j + 1) / (double)survivors) != 0) {
  146. zip_error_set(&za->error, ZIP_ER_CANCELLED, 0);
  147. error = 1;
  148. break;
  149. }
  150. i = filelist[j].idx;
  151. entry = za->entry + i;
  152. if (entry->orig != NULL && entry->orig->offset < unchanged_offset) {
  153. /* already implicitly copied by cloning */
  154. continue;
  155. }
  156. new_data = (ZIP_ENTRY_DATA_CHANGED(entry) || ZIP_ENTRY_CHANGED(entry, ZIP_DIRENT_COMP_METHOD) || ZIP_ENTRY_CHANGED(entry, ZIP_DIRENT_ENCRYPTION_METHOD));
  157. /* create new local directory entry */
  158. if (entry->changes == NULL) {
  159. if ((entry->changes = _zip_dirent_clone(entry->orig)) == NULL) {
  160. zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
  161. error = 1;
  162. break;
  163. }
  164. }
  165. de = entry->changes;
  166. if (_zip_read_local_ef(za, i) < 0) {
  167. error = 1;
  168. break;
  169. }
  170. if ((off = zip_source_tell_write(za->src)) < 0) {
  171. _zip_error_set_from_source(&za->error, za->src);
  172. error = 1;
  173. break;
  174. }
  175. de->offset = (zip_uint64_t)off;
  176. if (new_data) {
  177. zip_source_t *zs;
  178. zs = NULL;
  179. if (!ZIP_ENTRY_DATA_CHANGED(entry)) {
  180. if ((zs = _zip_source_zip_new(za, i, ZIP_FL_UNCHANGED, 0, 0, NULL, &za->error)) == NULL) {
  181. error = 1;
  182. break;
  183. }
  184. }
  185. /* add_data writes dirent */
  186. if (add_data(za, zs ? zs : entry->source, de, entry->changes ? entry->changes->changed : 0) < 0) {
  187. error = 1;
  188. if (zs)
  189. zip_source_free(zs);
  190. break;
  191. }
  192. if (zs)
  193. zip_source_free(zs);
  194. }
  195. else {
  196. zip_uint64_t offset;
  197. if (de->encryption_method != ZIP_EM_TRAD_PKWARE) {
  198. /* when copying data, all sizes are known -> no data descriptor needed */
  199. /* except for PKWare encryption, where removing the data descriptor breaks password validation */
  200. de->bitflags &= (zip_uint16_t)~ZIP_GPBF_DATA_DESCRIPTOR;
  201. }
  202. if (_zip_dirent_write(za, de, ZIP_FL_LOCAL) < 0) {
  203. error = 1;
  204. break;
  205. }
  206. if ((offset = _zip_file_get_offset(za, i, &za->error)) == 0) {
  207. error = 1;
  208. break;
  209. }
  210. if (zip_source_seek(za->src, (zip_int64_t)offset, SEEK_SET) < 0) {
  211. _zip_error_set_from_source(&za->error, za->src);
  212. error = 1;
  213. break;
  214. }
  215. if (copy_data(za, de->comp_size) < 0) {
  216. error = 1;
  217. break;
  218. }
  219. if (de->bitflags & ZIP_GPBF_DATA_DESCRIPTOR) {
  220. if (write_data_descriptor(za, de, _zip_dirent_needs_zip64(de, 0)) < 0) {
  221. error = 1;
  222. break;
  223. }
  224. }
  225. }
  226. }
  227. if (!error) {
  228. if (write_cdir(za, filelist, survivors) < 0)
  229. error = 1;
  230. }
  231. free(filelist);
  232. if (!error) {
  233. if (zip_source_commit_write(za->src) != 0) {
  234. _zip_error_set_from_source(&za->error, za->src);
  235. error = 1;
  236. }
  237. _zip_progress_end(za->progress);
  238. }
  239. if (error) {
  240. zip_source_rollback_write(za->src);
  241. return -1;
  242. }
  243. zip_discard(za);
  244. return 0;
  245. }
  246. static int
  247. add_data(zip_t *za, zip_source_t *src, zip_dirent_t *de, zip_uint32_t changed) {
  248. zip_int64_t offstart, offdata, offend, data_length;
  249. zip_stat_t st;
  250. zip_file_attributes_t attributes;
  251. zip_source_t *src_final, *src_tmp;
  252. int ret;
  253. int is_zip64;
  254. zip_flags_t flags;
  255. bool needs_recompress, needs_decompress, needs_crc, needs_compress, needs_reencrypt, needs_decrypt, needs_encrypt;
  256. if (zip_source_stat(src, &st) < 0) {
  257. _zip_error_set_from_source(&za->error, src);
  258. return -1;
  259. }
  260. if ((st.valid & ZIP_STAT_COMP_METHOD) == 0) {
  261. st.valid |= ZIP_STAT_COMP_METHOD;
  262. st.comp_method = ZIP_CM_STORE;
  263. }
  264. if (ZIP_CM_IS_DEFAULT(de->comp_method) && st.comp_method != ZIP_CM_STORE)
  265. de->comp_method = st.comp_method;
  266. else if (de->comp_method == ZIP_CM_STORE && (st.valid & ZIP_STAT_SIZE)) {
  267. st.valid |= ZIP_STAT_COMP_SIZE;
  268. st.comp_size = st.size;
  269. }
  270. else {
  271. /* we'll recompress */
  272. st.valid &= ~ZIP_STAT_COMP_SIZE;
  273. }
  274. if ((st.valid & ZIP_STAT_ENCRYPTION_METHOD) == 0) {
  275. st.valid |= ZIP_STAT_ENCRYPTION_METHOD;
  276. st.encryption_method = ZIP_EM_NONE;
  277. }
  278. flags = ZIP_EF_LOCAL;
  279. if ((st.valid & ZIP_STAT_SIZE) == 0) {
  280. flags |= ZIP_FL_FORCE_ZIP64;
  281. data_length = -1;
  282. }
  283. else {
  284. de->uncomp_size = st.size;
  285. /* this is technically incorrect (copy_source counts compressed data), but it's the best we have */
  286. data_length = (zip_int64_t)st.size;
  287. if ((st.valid & ZIP_STAT_COMP_SIZE) == 0) {
  288. zip_uint64_t max_compressed_size;
  289. zip_uint16_t compression_method = ZIP_CM_ACTUAL(de->comp_method);
  290. if (compression_method == ZIP_CM_STORE) {
  291. max_compressed_size = st.size;
  292. }
  293. else {
  294. zip_compression_algorithm_t *algorithm = _zip_get_compression_algorithm(compression_method, true);
  295. if (algorithm == NULL) {
  296. max_compressed_size = ZIP_UINT64_MAX;
  297. }
  298. else {
  299. max_compressed_size = algorithm->maximum_compressed_size(st.size);
  300. }
  301. }
  302. if (max_compressed_size > 0xffffffffu) {
  303. flags |= ZIP_FL_FORCE_ZIP64;
  304. }
  305. }
  306. else {
  307. de->comp_size = st.comp_size;
  308. data_length = (zip_int64_t)st.comp_size;
  309. }
  310. }
  311. if ((offstart = zip_source_tell_write(za->src)) < 0) {
  312. _zip_error_set_from_source(&za->error, za->src);
  313. return -1;
  314. }
  315. /* as long as we don't support non-seekable output, clear data descriptor bit */
  316. de->bitflags &= (zip_uint16_t)~ZIP_GPBF_DATA_DESCRIPTOR;
  317. if ((is_zip64 = _zip_dirent_write(za, de, flags)) < 0) {
  318. return -1;
  319. }
  320. needs_recompress = st.comp_method != ZIP_CM_ACTUAL(de->comp_method);
  321. needs_decompress = needs_recompress && (st.comp_method != ZIP_CM_STORE);
  322. /* in these cases we can compute the CRC ourselves, so we do */
  323. needs_crc = (st.comp_method == ZIP_CM_STORE) || needs_decompress;
  324. needs_compress = needs_recompress && (de->comp_method != ZIP_CM_STORE);
  325. needs_reencrypt = needs_recompress || (de->changed & ZIP_DIRENT_PASSWORD) || (de->encryption_method != st.encryption_method);
  326. needs_decrypt = needs_reencrypt && (st.encryption_method != ZIP_EM_NONE);
  327. needs_encrypt = needs_reencrypt && (de->encryption_method != ZIP_EM_NONE);
  328. src_final = src;
  329. zip_source_keep(src_final);
  330. if (needs_decrypt) {
  331. zip_encryption_implementation impl;
  332. if ((impl = _zip_get_encryption_implementation(st.encryption_method, ZIP_CODEC_DECODE)) == NULL) {
  333. zip_error_set(&za->error, ZIP_ER_ENCRNOTSUPP, 0);
  334. zip_source_free(src_final);
  335. return -1;
  336. }
  337. if ((src_tmp = impl(za, src_final, st.encryption_method, ZIP_CODEC_DECODE, za->default_password)) == NULL) {
  338. /* error set by impl */
  339. zip_source_free(src_final);
  340. return -1;
  341. }
  342. zip_source_free(src_final);
  343. src_final = src_tmp;
  344. }
  345. if (needs_decompress) {
  346. if ((src_tmp = zip_source_decompress(za, src_final, st.comp_method)) == NULL) {
  347. zip_source_free(src_final);
  348. return -1;
  349. }
  350. zip_source_free(src_final);
  351. src_final = src_tmp;
  352. }
  353. if (needs_crc) {
  354. if ((src_tmp = zip_source_crc_create(src_final, 0, &za->error)) == NULL) {
  355. zip_source_free(src_final);
  356. return -1;
  357. }
  358. zip_source_free(src_final);
  359. src_final = src_tmp;
  360. }
  361. if (needs_compress) {
  362. if ((src_tmp = zip_source_compress(za, src_final, de->comp_method, de->compression_level)) == NULL) {
  363. zip_source_free(src_final);
  364. return -1;
  365. }
  366. zip_source_free(src_final);
  367. src_final = src_tmp;
  368. }
  369. if (needs_encrypt) {
  370. zip_encryption_implementation impl;
  371. const char *password = NULL;
  372. if (de->password) {
  373. password = de->password;
  374. }
  375. else if (za->default_password) {
  376. password = za->default_password;
  377. }
  378. if ((impl = _zip_get_encryption_implementation(de->encryption_method, ZIP_CODEC_ENCODE)) == NULL) {
  379. zip_error_set(&za->error, ZIP_ER_ENCRNOTSUPP, 0);
  380. zip_source_free(src_final);
  381. return -1;
  382. }
  383. if ((src_tmp = impl(za, src_final, de->encryption_method, ZIP_CODEC_ENCODE, password)) == NULL) {
  384. /* error set by impl */
  385. zip_source_free(src_final);
  386. return -1;
  387. }
  388. if (de->encryption_method == ZIP_EM_TRAD_PKWARE) {
  389. de->bitflags |= ZIP_GPBF_DATA_DESCRIPTOR;
  390. }
  391. zip_source_free(src_final);
  392. src_final = src_tmp;
  393. }
  394. if ((offdata = zip_source_tell_write(za->src)) < 0) {
  395. _zip_error_set_from_source(&za->error, za->src);
  396. return -1;
  397. }
  398. ret = copy_source(za, src_final, data_length);
  399. if (zip_source_stat(src_final, &st) < 0) {
  400. _zip_error_set_from_source(&za->error, src_final);
  401. ret = -1;
  402. }
  403. if (zip_source_get_file_attributes(src_final, &attributes) != 0) {
  404. _zip_error_set_from_source(&za->error, src_final);
  405. ret = -1;
  406. }
  407. zip_source_free(src_final);
  408. if (ret < 0) {
  409. return -1;
  410. }
  411. if ((offend = zip_source_tell_write(za->src)) < 0) {
  412. _zip_error_set_from_source(&za->error, za->src);
  413. return -1;
  414. }
  415. if (zip_source_seek_write(za->src, offstart, SEEK_SET) < 0) {
  416. _zip_error_set_from_source(&za->error, za->src);
  417. return -1;
  418. }
  419. if ((st.valid & (ZIP_STAT_COMP_METHOD | ZIP_STAT_CRC | ZIP_STAT_SIZE)) != (ZIP_STAT_COMP_METHOD | ZIP_STAT_CRC | ZIP_STAT_SIZE)) {
  420. zip_error_set(&za->error, ZIP_ER_INTERNAL, 0);
  421. return -1;
  422. }
  423. if ((de->changed & ZIP_DIRENT_LAST_MOD) == 0) {
  424. if (st.valid & ZIP_STAT_MTIME)
  425. de->last_mod = st.mtime;
  426. else
  427. time(&de->last_mod);
  428. }
  429. de->comp_method = st.comp_method;
  430. de->crc = st.crc;
  431. de->uncomp_size = st.size;
  432. de->comp_size = (zip_uint64_t)(offend - offdata);
  433. _zip_dirent_apply_attributes(de, &attributes, (flags & ZIP_FL_FORCE_ZIP64) != 0, changed);
  434. if ((ret = _zip_dirent_write(za, de, flags)) < 0)
  435. return -1;
  436. if (is_zip64 != ret) {
  437. /* Zip64 mismatch between preliminary file header written before data and final file header written afterwards */
  438. zip_error_set(&za->error, ZIP_ER_INTERNAL, 0);
  439. return -1;
  440. }
  441. if (zip_source_seek_write(za->src, offend, SEEK_SET) < 0) {
  442. _zip_error_set_from_source(&za->error, za->src);
  443. return -1;
  444. }
  445. if (de->bitflags & ZIP_GPBF_DATA_DESCRIPTOR) {
  446. if (write_data_descriptor(za, de, is_zip64) < 0) {
  447. return -1;
  448. }
  449. }
  450. return 0;
  451. }
  452. static int
  453. copy_data(zip_t *za, zip_uint64_t len) {
  454. DEFINE_BYTE_ARRAY(buf, BUFSIZE);
  455. size_t n;
  456. double total = (double)len;
  457. if (!byte_array_init(buf, BUFSIZE)) {
  458. zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
  459. return -1;
  460. }
  461. while (len > 0) {
  462. n = len > BUFSIZE ? BUFSIZE : len;
  463. if (_zip_read(za->src, buf, n, &za->error) < 0) {
  464. byte_array_fini(buf);
  465. return -1;
  466. }
  467. if (_zip_write(za, buf, n) < 0) {
  468. byte_array_fini(buf);
  469. return -1;
  470. }
  471. len -= n;
  472. if (_zip_progress_update(za->progress, (total - (double)len) / total) != 0) {
  473. zip_error_set(&za->error, ZIP_ER_CANCELLED, 0);
  474. return -1;
  475. }
  476. }
  477. byte_array_fini(buf);
  478. return 0;
  479. }
  480. static int
  481. copy_source(zip_t *za, zip_source_t *src, zip_int64_t data_length) {
  482. DEFINE_BYTE_ARRAY(buf, BUFSIZE);
  483. zip_int64_t n, current;
  484. int ret;
  485. if (zip_source_open(src) < 0) {
  486. _zip_error_set_from_source(&za->error, src);
  487. return -1;
  488. }
  489. if (!byte_array_init(buf, BUFSIZE)) {
  490. zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
  491. return -1;
  492. }
  493. ret = 0;
  494. current = 0;
  495. while ((n = zip_source_read(src, buf, BUFSIZE)) > 0) {
  496. if (_zip_write(za, buf, (zip_uint64_t)n) < 0) {
  497. ret = -1;
  498. break;
  499. }
  500. if (n == BUFSIZE && za->progress && data_length > 0) {
  501. current += n;
  502. if (_zip_progress_update(za->progress, (double)current / (double)data_length) != 0) {
  503. zip_error_set(&za->error, ZIP_ER_CANCELLED, 0);
  504. ret = -1;
  505. break;
  506. }
  507. }
  508. }
  509. if (n < 0) {
  510. _zip_error_set_from_source(&za->error, src);
  511. ret = -1;
  512. }
  513. byte_array_fini(buf);
  514. zip_source_close(src);
  515. return ret;
  516. }
  517. static int
  518. write_cdir(zip_t *za, const zip_filelist_t *filelist, zip_uint64_t survivors) {
  519. if (zip_source_tell_write(za->src) < 0) {
  520. return -1;
  521. }
  522. if (_zip_cdir_write(za, filelist, survivors) < 0) {
  523. return -1;
  524. }
  525. if (zip_source_tell_write(za->src) < 0) {
  526. return -1;
  527. }
  528. return 0;
  529. }
  530. int
  531. _zip_changed(const zip_t *za, zip_uint64_t *survivorsp) {
  532. int changed;
  533. zip_uint64_t i, survivors;
  534. changed = 0;
  535. survivors = 0;
  536. if (za->comment_changed || za->ch_flags != za->flags) {
  537. changed = 1;
  538. }
  539. for (i = 0; i < za->nentry; i++) {
  540. if (ZIP_ENTRY_HAS_CHANGES(&za->entry[i])) {
  541. changed = 1;
  542. }
  543. if (!za->entry[i].deleted) {
  544. survivors++;
  545. }
  546. }
  547. if (survivorsp) {
  548. *survivorsp = survivors;
  549. }
  550. return changed;
  551. }
  552. static int
  553. write_data_descriptor(zip_t *za, const zip_dirent_t *de, int is_zip64) {
  554. zip_buffer_t *buffer = _zip_buffer_new(NULL, MAX_DATA_DESCRIPTOR_LENGTH);
  555. int ret = 0;
  556. if (buffer == NULL) {
  557. zip_error_set(&za->error, ZIP_ER_MEMORY, 0);
  558. return -1;
  559. }
  560. _zip_buffer_put(buffer, DATADES_MAGIC, 4);
  561. _zip_buffer_put_32(buffer, de->crc);
  562. if (is_zip64) {
  563. _zip_buffer_put_64(buffer, de->comp_size);
  564. _zip_buffer_put_64(buffer, de->uncomp_size);
  565. }
  566. else {
  567. _zip_buffer_put_32(buffer, (zip_uint32_t)de->comp_size);
  568. _zip_buffer_put_32(buffer, (zip_uint32_t)de->uncomp_size);
  569. }
  570. if (!_zip_buffer_ok(buffer)) {
  571. zip_error_set(&za->error, ZIP_ER_INTERNAL, 0);
  572. ret = -1;
  573. }
  574. else {
  575. ret = _zip_write(za, _zip_buffer_data(buffer), _zip_buffer_offset(buffer));
  576. }
  577. _zip_buffer_free(buffer);
  578. return ret;
  579. }