archive_write_set_format_zip.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  1. /*-
  2. * Copyright (c) 2008 Anselm Strauss
  3. * Copyright (c) 2009 Joerg Sonnenberger
  4. * Copyright (c) 2011-2012 Michihiro NAKAJIMA
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  18. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  19. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  21. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  25. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. /*
  28. * Development supported by Google Summer of Code 2008.
  29. */
  30. #include "archive_platform.h"
  31. __FBSDID("$FreeBSD: head/lib/libarchive/archive_write_set_format_zip.c 201168 2009-12-29 06:15:32Z kientzle $");
  32. #ifdef HAVE_ERRNO_H
  33. #include <errno.h>
  34. #endif
  35. #ifdef HAVE_LANGINFO_H
  36. #include <langinfo.h>
  37. #endif
  38. #ifdef HAVE_STDLIB_H
  39. #include <stdlib.h>
  40. #endif
  41. #ifdef HAVE_STRING_H
  42. #include <string.h>
  43. #endif
  44. #ifdef HAVE_ZLIB_H
  45. #include <cm_zlib.h>
  46. #endif
  47. #include "archive.h"
  48. #include "archive_endian.h"
  49. #include "archive_entry.h"
  50. #include "archive_entry_locale.h"
  51. #include "archive_private.h"
  52. #include "archive_write_private.h"
  53. #ifndef HAVE_ZLIB_H
  54. #include "archive_crc32.h"
  55. #endif
  56. #define ZIP_ENTRY_FLAG_LENGTH_AT_END (1<<3)
  57. #define ZIP_ENTRY_FLAG_UTF8_NAME (1 << 11)
  58. enum compression {
  59. COMPRESSION_UNSPECIFIED = -1,
  60. COMPRESSION_STORE = 0,
  61. COMPRESSION_DEFLATE = 8
  62. };
  63. #ifdef HAVE_ZLIB_H
  64. #define COMPRESSION_DEFAULT COMPRESSION_DEFLATE
  65. #else
  66. #define COMPRESSION_DEFAULT COMPRESSION_STORE
  67. #endif
  68. struct cd_segment {
  69. struct cd_segment *next;
  70. size_t buff_size;
  71. unsigned char *buff;
  72. unsigned char *p;
  73. };
  74. /* Bits used to enable/disable certain experimental features. */
  75. #define EXPERIMENT_LA 1
  76. #define EXPERIMENTS_ALL 0xffff
  77. struct zip {
  78. int64_t entry_offset;
  79. int64_t entry_compressed_size;
  80. int64_t entry_uncompressed_size;
  81. int64_t entry_compressed_written;
  82. int64_t entry_uncompressed_written;
  83. int64_t entry_uncompressed_limit;
  84. struct archive_entry *entry;
  85. uint32_t entry_crc32;
  86. enum compression entry_compression;
  87. int entry_flags;
  88. int entry_uses_zip64;
  89. int experiments;
  90. unsigned char *file_header;
  91. size_t file_header_extra_offset;
  92. unsigned long (*crc32func)(unsigned long crc, const void *buff, size_t len);
  93. struct cd_segment *central_directory;
  94. struct cd_segment *central_directory_last;
  95. size_t central_directory_bytes;
  96. size_t central_directory_entries;
  97. int64_t written_bytes; /* Overall position in file. */
  98. struct archive_string_conv *opt_sconv;
  99. struct archive_string_conv *sconv_default;
  100. enum compression requested_compression;
  101. int init_default_conversion;
  102. #define ZIP_FLAG_AVOID_ZIP64 1
  103. #define ZIP_FLAG_FORCE_ZIP64 2
  104. #define ZIP_FLAG_EXPERIMENT_EL 4
  105. int flags;
  106. #ifdef HAVE_ZLIB_H
  107. z_stream stream;
  108. size_t len_buf;
  109. unsigned char *buf;
  110. #endif
  111. };
  112. /* Don't call this min or MIN, since those are already defined
  113. on lots of platforms (but not all). */
  114. #define zipmin(a, b) ((a) > (b) ? (b) : (a))
  115. static ssize_t archive_write_zip_data(struct archive_write *,
  116. const void *buff, size_t s);
  117. static int archive_write_zip_close(struct archive_write *);
  118. static int archive_write_zip_free(struct archive_write *);
  119. static int archive_write_zip_finish_entry(struct archive_write *);
  120. static int archive_write_zip_header(struct archive_write *,
  121. struct archive_entry *);
  122. static int archive_write_zip_options(struct archive_write *,
  123. const char *, const char *);
  124. static unsigned int dos_time(const time_t);
  125. static size_t path_length(struct archive_entry *);
  126. static int write_path(struct archive_entry *, struct archive_write *);
  127. static void copy_path(struct archive_entry *, unsigned char *);
  128. static struct archive_string_conv *get_sconv(struct archive_write *, struct zip *);
  129. static unsigned char *
  130. cd_alloc(struct zip *zip, size_t length)
  131. {
  132. unsigned char *p;
  133. if (zip->central_directory == NULL
  134. || (zip->central_directory_last->p + length
  135. > zip->central_directory_last->buff + zip->central_directory_last->buff_size)) {
  136. struct cd_segment *segment = calloc(1, sizeof(*segment));
  137. if (segment == NULL)
  138. return NULL;
  139. segment->buff_size = 64 * 1024;
  140. segment->buff = malloc(segment->buff_size);
  141. if (segment->buff == NULL) {
  142. free(segment);
  143. return NULL;
  144. }
  145. segment->p = segment->buff;
  146. if (zip->central_directory == NULL) {
  147. zip->central_directory
  148. = zip->central_directory_last
  149. = segment;
  150. } else {
  151. zip->central_directory_last->next = segment;
  152. zip->central_directory_last = segment;
  153. }
  154. }
  155. p = zip->central_directory_last->p;
  156. zip->central_directory_last->p += length;
  157. zip->central_directory_bytes += length;
  158. return (p);
  159. }
  160. static unsigned long
  161. real_crc32(unsigned long crc, const void *buff, size_t len)
  162. {
  163. return crc32(crc, buff, len);
  164. }
  165. static unsigned long
  166. fake_crc32(unsigned long crc, const void *buff, size_t len)
  167. {
  168. (void)crc; /* UNUSED */
  169. (void)buff; /* UNUSED */
  170. (void)len; /* UNUSED */
  171. return 0;
  172. }
  173. static int
  174. archive_write_zip_options(struct archive_write *a, const char *key,
  175. const char *val)
  176. {
  177. struct zip *zip = a->format_data;
  178. int ret = ARCHIVE_FAILED;
  179. if (strcmp(key, "compression") == 0) {
  180. /*
  181. * Set compression to use on all future entries.
  182. * This only affects regular files.
  183. */
  184. if (val == NULL || val[0] == 0) {
  185. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  186. "%s: compression option needs a compression name",
  187. a->format_name);
  188. } else if (strcmp(val, "deflate") == 0) {
  189. #ifdef HAVE_ZLIB_H
  190. zip->requested_compression = COMPRESSION_DEFLATE;
  191. ret = ARCHIVE_OK;
  192. #else
  193. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  194. "deflate compression not supported");
  195. #endif
  196. } else if (strcmp(val, "store") == 0) {
  197. zip->requested_compression = COMPRESSION_STORE;
  198. ret = ARCHIVE_OK;
  199. }
  200. return (ret);
  201. } else if (strcmp(key, "experimental") == 0) {
  202. if (val == NULL || val[0] == 0) {
  203. zip->flags &= ~ ZIP_FLAG_EXPERIMENT_EL;
  204. } else {
  205. zip->flags |= ZIP_FLAG_EXPERIMENT_EL;
  206. }
  207. return (ARCHIVE_OK);
  208. } else if (strcmp(key, "fakecrc32") == 0) {
  209. /*
  210. * FOR TESTING ONLY: disable CRC calculation to speed up
  211. * certain complex tests.
  212. */
  213. if (val == NULL || val[0] == 0) {
  214. zip->crc32func = real_crc32;
  215. } else {
  216. zip->crc32func = fake_crc32;
  217. }
  218. return (ARCHIVE_OK);
  219. } else if (strcmp(key, "hdrcharset") == 0) {
  220. /*
  221. * Set the character set used in translating filenames.
  222. */
  223. if (val == NULL || val[0] == 0) {
  224. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  225. "%s: hdrcharset option needs a character-set name",
  226. a->format_name);
  227. } else {
  228. zip->opt_sconv = archive_string_conversion_to_charset(
  229. &a->archive, val, 0);
  230. if (zip->opt_sconv != NULL)
  231. ret = ARCHIVE_OK;
  232. else
  233. ret = ARCHIVE_FATAL;
  234. }
  235. return (ret);
  236. } else if (strcmp(key, "zip64") == 0) {
  237. /*
  238. * Bias decisions about Zip64: force them to be
  239. * generated in certain cases where they are not
  240. * forbidden or avoid them in certain cases where they
  241. * are not strictly required.
  242. */
  243. if (val != NULL && *val != '\0') {
  244. zip->flags |= ZIP_FLAG_FORCE_ZIP64;
  245. zip->flags &= ~ZIP_FLAG_AVOID_ZIP64;
  246. } else {
  247. zip->flags &= ~ZIP_FLAG_FORCE_ZIP64;
  248. zip->flags |= ZIP_FLAG_AVOID_ZIP64;
  249. }
  250. return (ARCHIVE_OK);
  251. }
  252. /* Note: The "warn" return is just to inform the options
  253. * supervisor that we didn't handle it. It will generate
  254. * a suitable error if no one used this option. */
  255. return (ARCHIVE_WARN);
  256. }
  257. int
  258. archive_write_zip_set_compression_deflate(struct archive *_a)
  259. {
  260. struct archive_write *a = (struct archive_write *)_a;
  261. int ret = ARCHIVE_FAILED;
  262. archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
  263. ARCHIVE_STATE_NEW | ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
  264. "archive_write_zip_set_compression_deflate");
  265. if (a->archive.archive_format != ARCHIVE_FORMAT_ZIP) {
  266. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  267. "Can only use archive_write_zip_set_compression_deflate"
  268. " with zip format");
  269. ret = ARCHIVE_FATAL;
  270. } else {
  271. #ifdef HAVE_ZLIB_H
  272. struct zip *zip = a->format_data;
  273. zip->requested_compression = COMPRESSION_DEFLATE;
  274. ret = ARCHIVE_OK;
  275. #else
  276. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  277. "deflate compression not supported");
  278. ret = ARCHIVE_FAILED;
  279. #endif
  280. }
  281. return (ret);
  282. }
  283. int
  284. archive_write_zip_set_compression_store(struct archive *_a)
  285. {
  286. struct archive_write *a = (struct archive_write *)_a;
  287. struct zip *zip = a->format_data;
  288. int ret = ARCHIVE_FAILED;
  289. archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
  290. ARCHIVE_STATE_NEW | ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
  291. "archive_write_zip_set_compression_deflate");
  292. if (a->archive.archive_format != ARCHIVE_FORMAT_ZIP) {
  293. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  294. "Can only use archive_write_zip_set_compression_store"
  295. " with zip format");
  296. ret = ARCHIVE_FATAL;
  297. } else {
  298. zip->requested_compression = COMPRESSION_STORE;
  299. ret = ARCHIVE_OK;
  300. }
  301. return (ret);
  302. }
  303. int
  304. archive_write_set_format_zip(struct archive *_a)
  305. {
  306. struct archive_write *a = (struct archive_write *)_a;
  307. struct zip *zip;
  308. archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
  309. ARCHIVE_STATE_NEW, "archive_write_set_format_zip");
  310. /* If another format was already registered, unregister it. */
  311. if (a->format_free != NULL)
  312. (a->format_free)(a);
  313. zip = (struct zip *) calloc(1, sizeof(*zip));
  314. if (zip == NULL) {
  315. archive_set_error(&a->archive, ENOMEM,
  316. "Can't allocate zip data");
  317. return (ARCHIVE_FATAL);
  318. }
  319. /* "Unspecified" lets us choose the appropriate compression. */
  320. zip->requested_compression = COMPRESSION_UNSPECIFIED;
  321. zip->crc32func = real_crc32;
  322. #ifdef HAVE_ZLIB_H
  323. zip->len_buf = 65536;
  324. zip->buf = malloc(zip->len_buf);
  325. if (zip->buf == NULL) {
  326. free(zip);
  327. archive_set_error(&a->archive, ENOMEM,
  328. "Can't allocate compression buffer");
  329. return (ARCHIVE_FATAL);
  330. }
  331. #endif
  332. a->format_data = zip;
  333. a->format_name = "zip";
  334. a->format_options = archive_write_zip_options;
  335. a->format_write_header = archive_write_zip_header;
  336. a->format_write_data = archive_write_zip_data;
  337. a->format_finish_entry = archive_write_zip_finish_entry;
  338. a->format_close = archive_write_zip_close;
  339. a->format_free = archive_write_zip_free;
  340. a->archive.archive_format = ARCHIVE_FORMAT_ZIP;
  341. a->archive.archive_format_name = "ZIP";
  342. return (ARCHIVE_OK);
  343. }
  344. static int
  345. is_all_ascii(const char *p)
  346. {
  347. const unsigned char *pp = (const unsigned char *)p;
  348. while (*pp) {
  349. if (*pp++ > 127)
  350. return (0);
  351. }
  352. return (1);
  353. }
  354. static int
  355. archive_write_zip_header(struct archive_write *a, struct archive_entry *entry)
  356. {
  357. unsigned char local_header[32];
  358. unsigned char local_extra[128];
  359. struct zip *zip = a->format_data;
  360. unsigned char *e;
  361. unsigned char *cd_extra;
  362. size_t filename_length;
  363. const char *symlink = NULL;
  364. size_t symlink_size = 0;
  365. struct archive_string_conv *sconv = get_sconv(a, zip);
  366. int ret, ret2 = ARCHIVE_OK;
  367. int64_t size;
  368. mode_t type;
  369. int version_needed = 10;
  370. /* Ignore types of entries that we don't support. */
  371. type = archive_entry_filetype(entry);
  372. if (type != AE_IFREG && type != AE_IFDIR && type != AE_IFLNK) {
  373. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  374. "Filetype not supported");
  375. return ARCHIVE_FAILED;
  376. };
  377. /* If we're not using Zip64, reject large files. */
  378. if (zip->flags & ZIP_FLAG_AVOID_ZIP64) {
  379. /* Reject entries over 4GB. */
  380. if (archive_entry_size_is_set(entry)
  381. && (archive_entry_size(entry) > 0xffffffff)) {
  382. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  383. "Files > 4GB require Zip64 extensions");
  384. return ARCHIVE_FAILED;
  385. }
  386. /* Reject entries if archive is > 4GB. */
  387. if (zip->written_bytes > 0xffffffff) {
  388. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  389. "Archives > 4GB require Zip64 extensions");
  390. return ARCHIVE_FAILED;
  391. }
  392. }
  393. /* Only regular files can have size > 0. */
  394. if (type != AE_IFREG)
  395. archive_entry_set_size(entry, 0);
  396. /* Reset information from last entry. */
  397. zip->entry_offset = zip->written_bytes;
  398. zip->entry_uncompressed_limit = INT64_MAX;
  399. zip->entry_compressed_size = 0;
  400. zip->entry_uncompressed_size = 0;
  401. zip->entry_compressed_written = 0;
  402. zip->entry_uncompressed_written = 0;
  403. zip->entry_flags = 0;
  404. zip->entry_uses_zip64 = 0;
  405. zip->entry_crc32 = zip->crc32func(0, NULL, 0);
  406. if (zip->entry != NULL) {
  407. archive_entry_free(zip->entry);
  408. zip->entry = NULL;
  409. }
  410. #if defined(_WIN32) && !defined(__CYGWIN__)
  411. /* Make sure the path separators in pahtname, hardlink and symlink
  412. * are all slash '/', not the Windows path separator '\'. */
  413. zip->entry = __la_win_entry_in_posix_pathseparator(entry);
  414. if (zip->entry == entry)
  415. zip->entry = archive_entry_clone(entry);
  416. #else
  417. zip->entry = archive_entry_clone(entry);
  418. #endif
  419. if (zip->entry == NULL) {
  420. archive_set_error(&a->archive, ENOMEM,
  421. "Can't allocate zip header data");
  422. return (ARCHIVE_FATAL);
  423. }
  424. if (sconv != NULL) {
  425. const char *p;
  426. size_t len;
  427. if (archive_entry_pathname_l(entry, &p, &len, sconv) != 0) {
  428. if (errno == ENOMEM) {
  429. archive_set_error(&a->archive, ENOMEM,
  430. "Can't allocate memory for Pathname");
  431. return (ARCHIVE_FATAL);
  432. }
  433. archive_set_error(&a->archive,
  434. ARCHIVE_ERRNO_FILE_FORMAT,
  435. "Can't translate Pathname '%s' to %s",
  436. archive_entry_pathname(entry),
  437. archive_string_conversion_charset_name(sconv));
  438. ret2 = ARCHIVE_WARN;
  439. }
  440. if (len > 0)
  441. archive_entry_set_pathname(zip->entry, p);
  442. /*
  443. * There is no standard for symlink handling; we convert
  444. * it using the same character-set translation that we use
  445. * for filename.
  446. */
  447. if (type == AE_IFLNK) {
  448. if (archive_entry_symlink_l(entry, &p, &len, sconv)) {
  449. if (errno == ENOMEM) {
  450. archive_set_error(&a->archive, ENOMEM,
  451. "Can't allocate memory "
  452. " for Symlink");
  453. return (ARCHIVE_FATAL);
  454. }
  455. /* No error if we can't convert. */
  456. } else if (len > 0)
  457. archive_entry_set_symlink(zip->entry, p);
  458. }
  459. }
  460. /* If filename isn't ASCII and we can use UTF-8, set the UTF-8 flag. */
  461. if (!is_all_ascii(archive_entry_pathname(zip->entry))) {
  462. if (zip->opt_sconv != NULL) {
  463. if (strcmp(archive_string_conversion_charset_name(
  464. zip->opt_sconv), "UTF-8") == 0)
  465. zip->entry_flags |= ZIP_ENTRY_FLAG_UTF8_NAME;
  466. #if HAVE_NL_LANGINFO
  467. } else if (strcmp(nl_langinfo(CODESET), "UTF-8") == 0) {
  468. zip->entry_flags |= ZIP_ENTRY_FLAG_UTF8_NAME;
  469. #endif
  470. }
  471. }
  472. filename_length = path_length(zip->entry);
  473. /* Determine appropriate compression and size for this entry. */
  474. if (type == AE_IFLNK) {
  475. symlink = archive_entry_symlink(zip->entry);
  476. if (symlink != NULL)
  477. symlink_size = strlen(symlink);
  478. else
  479. symlink_size = 0;
  480. zip->entry_uncompressed_limit = symlink_size;
  481. zip->entry_compressed_size = symlink_size;
  482. zip->entry_uncompressed_size = symlink_size;
  483. zip->entry_crc32 = zip->crc32func(zip->entry_crc32,
  484. (const unsigned char *)symlink, symlink_size);
  485. zip->entry_compression = COMPRESSION_STORE;
  486. version_needed = 20;
  487. } else if (type != AE_IFREG) {
  488. zip->entry_compression = COMPRESSION_STORE;
  489. zip->entry_uncompressed_limit = 0;
  490. size = 0;
  491. version_needed = 20;
  492. } else if (archive_entry_size_is_set(zip->entry)) {
  493. size = archive_entry_size(zip->entry);
  494. zip->entry_uncompressed_limit = size;
  495. zip->entry_compression = zip->requested_compression;
  496. if (zip->entry_compression == COMPRESSION_UNSPECIFIED) {
  497. zip->entry_compression = COMPRESSION_DEFAULT;
  498. }
  499. if (zip->entry_compression == COMPRESSION_STORE) {
  500. zip->entry_compressed_size = size;
  501. zip->entry_uncompressed_size = size;
  502. version_needed = 10;
  503. } else {
  504. zip->entry_uncompressed_size = size;
  505. version_needed = 20;
  506. }
  507. if ((zip->flags & ZIP_FLAG_FORCE_ZIP64) /* User asked. */
  508. || (zip->entry_uncompressed_size > ARCHIVE_LITERAL_LL(0xffffffff))) { /* Large entry. */
  509. zip->entry_uses_zip64 = 1;
  510. version_needed = 45;
  511. }
  512. /* We may know the size, but never the CRC. */
  513. zip->entry_flags |= ZIP_ENTRY_FLAG_LENGTH_AT_END;
  514. } else {
  515. /* Prefer deflate if it's available, because deflate
  516. * has a clear end-of-data marker that makes
  517. * length-at-end more reliable. */
  518. zip->entry_compression = COMPRESSION_DEFAULT;
  519. zip->entry_flags |= ZIP_ENTRY_FLAG_LENGTH_AT_END;
  520. if ((zip->flags & ZIP_FLAG_AVOID_ZIP64) == 0) {
  521. zip->entry_uses_zip64 = 1;
  522. version_needed = 45;
  523. } else if (zip->entry_compression == COMPRESSION_STORE) {
  524. version_needed = 10;
  525. } else {
  526. version_needed = 20;
  527. }
  528. }
  529. /* Format the local header. */
  530. memset(local_header, 0, sizeof(local_header));
  531. memcpy(local_header, "PK\003\004", 4);
  532. archive_le16enc(local_header + 4, version_needed);
  533. archive_le16enc(local_header + 6, zip->entry_flags);
  534. archive_le16enc(local_header + 8, zip->entry_compression);
  535. archive_le32enc(local_header + 10, dos_time(archive_entry_mtime(zip->entry)));
  536. archive_le32enc(local_header + 14, zip->entry_crc32);
  537. if (zip->entry_uses_zip64) {
  538. /* Zip64 data in the local header "must" include both
  539. * compressed and uncompressed sizes AND those fields
  540. * are included only if these are 0xffffffff;
  541. * THEREFORE these must be set this way, even if we
  542. * know one of them is smaller. */
  543. archive_le32enc(local_header + 18, ARCHIVE_LITERAL_LL(0xffffffff));
  544. archive_le32enc(local_header + 22, ARCHIVE_LITERAL_LL(0xffffffff));
  545. } else {
  546. archive_le32enc(local_header + 18, zip->entry_compressed_size);
  547. archive_le32enc(local_header + 22, zip->entry_uncompressed_size);
  548. }
  549. archive_le16enc(local_header + 26, filename_length);
  550. /* Format as much of central directory file header as we can: */
  551. zip->file_header = cd_alloc(zip, 46);
  552. /* If (zip->file_header == NULL) XXXX */
  553. ++zip->central_directory_entries;
  554. memset(zip->file_header, 0, 46);
  555. memcpy(zip->file_header, "PK\001\002", 4);
  556. /* "Made by PKZip 2.0 on Unix." */
  557. archive_le16enc(zip->file_header + 4, 3 * 256 + version_needed);
  558. archive_le16enc(zip->file_header + 6, version_needed);
  559. archive_le16enc(zip->file_header + 8, zip->entry_flags);
  560. archive_le16enc(zip->file_header + 10, zip->entry_compression);
  561. archive_le32enc(zip->file_header + 12, dos_time(archive_entry_mtime(zip->entry)));
  562. archive_le16enc(zip->file_header + 28, filename_length);
  563. /* Following Info-Zip, store mode in the "external attributes" field. */
  564. archive_le32enc(zip->file_header + 38,
  565. ((uint32_t)archive_entry_mode(zip->entry)) << 16);
  566. e = cd_alloc(zip, filename_length);
  567. /* If (e == NULL) XXXX */
  568. copy_path(zip->entry, e);
  569. /* Format extra data. */
  570. memset(local_extra, 0, sizeof(local_extra));
  571. e = local_extra;
  572. /* First, extra blocks that are the same between
  573. * the local file header and the central directory.
  574. * We format them once and then duplicate them. */
  575. /* UT timestamp, length depends on what timestamps are set. */
  576. memcpy(e, "UT", 2);
  577. archive_le16enc(e + 2,
  578. 1
  579. + (archive_entry_mtime_is_set(entry) ? 4 : 0)
  580. + (archive_entry_atime_is_set(entry) ? 4 : 0)
  581. + (archive_entry_ctime_is_set(entry) ? 4 : 0));
  582. e += 4;
  583. *e++ =
  584. (archive_entry_mtime_is_set(entry) ? 1 : 0)
  585. | (archive_entry_atime_is_set(entry) ? 2 : 0)
  586. | (archive_entry_ctime_is_set(entry) ? 4 : 0);
  587. if (archive_entry_mtime_is_set(entry)) {
  588. archive_le32enc(e, (uint32_t)archive_entry_mtime(entry));
  589. e += 4;
  590. }
  591. if (archive_entry_atime_is_set(entry)) {
  592. archive_le32enc(e, (uint32_t)archive_entry_atime(entry));
  593. e += 4;
  594. }
  595. if (archive_entry_ctime_is_set(entry)) {
  596. archive_le32enc(e, (uint32_t)archive_entry_ctime(entry));
  597. e += 4;
  598. }
  599. /* ux Unix extra data, length 11, version 1 */
  600. /* TODO: If uid < 64k, use 2 bytes, ditto for gid. */
  601. memcpy(e, "ux\013\000\001", 5);
  602. e += 5;
  603. *e++ = 4; /* Length of following UID */
  604. archive_le32enc(e, (uint32_t)archive_entry_uid(entry));
  605. e += 4;
  606. *e++ = 4; /* Length of following GID */
  607. archive_le32enc(e, (uint32_t)archive_entry_gid(entry));
  608. e += 4;
  609. /* Copy UT and ux into central directory as well. */
  610. zip->file_header_extra_offset = zip->central_directory_bytes;
  611. cd_extra = cd_alloc(zip, e - local_extra);
  612. memcpy(cd_extra, local_extra, e - local_extra);
  613. /*
  614. * Following extra blocks vary between local header and
  615. * central directory. These are the local header versions.
  616. * Central directory versions get formatted in
  617. * archive_write_zip_finish_entry() below.
  618. */
  619. /* "[Zip64 entry] in the local header MUST include BOTH
  620. * original [uncompressed] and compressed size fields." */
  621. if (zip->entry_uses_zip64) {
  622. unsigned char *zip64_start = e;
  623. memcpy(e, "\001\000\020\000", 4);
  624. e += 4;
  625. archive_le64enc(e, zip->entry_uncompressed_size);
  626. e += 8;
  627. archive_le64enc(e, zip->entry_compressed_size);
  628. e += 8;
  629. archive_le16enc(zip64_start + 2, e - (zip64_start + 4));
  630. }
  631. if (zip->flags & ZIP_FLAG_EXPERIMENT_EL) {
  632. /* Experimental 'el' extension to improve streaming. */
  633. unsigned char *external_info = e;
  634. int included = 7;
  635. memcpy(e, "el\000\000", 4); // 0x6c65 + 2-byte length
  636. e += 4;
  637. e[0] = included; /* bitmap of included fields */
  638. e += 1;
  639. if (included & 1) {
  640. archive_le16enc(e, /* "Version created by" */
  641. 3 * 256 + version_needed);
  642. e += 2;
  643. }
  644. if (included & 2) {
  645. archive_le16enc(e, 0); /* internal file attributes */
  646. e += 2;
  647. }
  648. if (included & 4) {
  649. archive_le32enc(e, /* external file attributes */
  650. ((uint32_t)archive_entry_mode(zip->entry)) << 16);
  651. e += 4;
  652. }
  653. if (included & 8) {
  654. // Libarchive does not currently support file comments.
  655. }
  656. archive_le16enc(external_info + 2, e - (external_info + 4));
  657. }
  658. /* Update local header with size of extra data and write it all out: */
  659. archive_le16enc(local_header + 28, e - local_extra);
  660. ret = __archive_write_output(a, local_header, 30);
  661. if (ret != ARCHIVE_OK)
  662. return (ARCHIVE_FATAL);
  663. zip->written_bytes += 30;
  664. ret = write_path(zip->entry, a);
  665. if (ret <= ARCHIVE_OK)
  666. return (ARCHIVE_FATAL);
  667. zip->written_bytes += ret;
  668. ret = __archive_write_output(a, local_extra, e - local_extra);
  669. if (ret != ARCHIVE_OK)
  670. return (ARCHIVE_FATAL);
  671. zip->written_bytes += e - local_extra;
  672. /* For symlinks, write the body now. */
  673. if (symlink != NULL) {
  674. ret = __archive_write_output(a, symlink, (size_t)symlink_size);
  675. if (ret != ARCHIVE_OK)
  676. return (ARCHIVE_FATAL);
  677. zip->entry_compressed_written += symlink_size;
  678. zip->entry_uncompressed_written += symlink_size;
  679. zip->written_bytes += symlink_size;
  680. }
  681. #ifdef HAVE_ZLIB_H
  682. if (zip->entry_compression == COMPRESSION_DEFLATE) {
  683. zip->stream.zalloc = Z_NULL;
  684. zip->stream.zfree = Z_NULL;
  685. zip->stream.opaque = Z_NULL;
  686. zip->stream.next_out = zip->buf;
  687. zip->stream.avail_out = (uInt)zip->len_buf;
  688. if (deflateInit2(&zip->stream, Z_DEFAULT_COMPRESSION,
  689. Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY) != Z_OK) {
  690. archive_set_error(&a->archive, ENOMEM,
  691. "Can't init deflate compressor");
  692. return (ARCHIVE_FATAL);
  693. }
  694. }
  695. #endif
  696. return (ret2);
  697. }
  698. static ssize_t
  699. archive_write_zip_data(struct archive_write *a, const void *buff, size_t s)
  700. {
  701. int ret;
  702. struct zip *zip = a->format_data;
  703. if ((int64_t)s > zip->entry_uncompressed_limit)
  704. s = (size_t)zip->entry_uncompressed_limit;
  705. zip->entry_uncompressed_written += s;
  706. if (s == 0) return 0;
  707. switch (zip->entry_compression) {
  708. case COMPRESSION_STORE:
  709. ret = __archive_write_output(a, buff, s);
  710. if (ret != ARCHIVE_OK)
  711. return (ret);
  712. zip->written_bytes += s;
  713. zip->entry_compressed_written += s;
  714. break;
  715. #if HAVE_ZLIB_H
  716. case COMPRESSION_DEFLATE:
  717. zip->stream.next_in = (unsigned char*)(uintptr_t)buff;
  718. zip->stream.avail_in = (uInt)s;
  719. do {
  720. ret = deflate(&zip->stream, Z_NO_FLUSH);
  721. if (ret == Z_STREAM_ERROR)
  722. return (ARCHIVE_FATAL);
  723. if (zip->stream.avail_out == 0) {
  724. ret = __archive_write_output(a, zip->buf,
  725. zip->len_buf);
  726. if (ret != ARCHIVE_OK)
  727. return (ret);
  728. zip->entry_compressed_written += zip->len_buf;
  729. zip->written_bytes += zip->len_buf;
  730. zip->stream.next_out = zip->buf;
  731. zip->stream.avail_out = (uInt)zip->len_buf;
  732. }
  733. } while (zip->stream.avail_in != 0);
  734. break;
  735. #endif
  736. default:
  737. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  738. "Invalid ZIP compression type");
  739. return ARCHIVE_FATAL;
  740. }
  741. zip->entry_uncompressed_limit -= s;
  742. zip->entry_crc32 = zip->crc32func(zip->entry_crc32, buff, (unsigned)s);
  743. return (s);
  744. }
  745. static int
  746. archive_write_zip_finish_entry(struct archive_write *a)
  747. {
  748. struct zip *zip = a->format_data;
  749. int ret;
  750. #if HAVE_ZLIB_H
  751. if (zip->entry_compression == COMPRESSION_DEFLATE) {
  752. for (;;) {
  753. size_t remainder;
  754. ret = deflate(&zip->stream, Z_FINISH);
  755. if (ret == Z_STREAM_ERROR)
  756. return (ARCHIVE_FATAL);
  757. remainder = zip->len_buf - zip->stream.avail_out;
  758. ret = __archive_write_output(a, zip->buf, remainder);
  759. if (ret != ARCHIVE_OK)
  760. return (ret);
  761. zip->entry_compressed_written += remainder;
  762. zip->written_bytes += remainder;
  763. zip->stream.next_out = zip->buf;
  764. if (zip->stream.avail_out != 0)
  765. break;
  766. zip->stream.avail_out = (uInt)zip->len_buf;
  767. }
  768. deflateEnd(&zip->stream);
  769. }
  770. #endif
  771. /* Write trailing data descriptor. */
  772. if ((zip->entry_flags & ZIP_ENTRY_FLAG_LENGTH_AT_END) != 0) {
  773. char d[24];
  774. memcpy(d, "PK\007\010", 4);
  775. archive_le32enc(d + 4, zip->entry_crc32);
  776. if (zip->entry_uses_zip64) {
  777. archive_le64enc(d + 8, (uint64_t)zip->entry_compressed_written);
  778. archive_le64enc(d + 16, (uint64_t)zip->entry_uncompressed_written);
  779. ret = __archive_write_output(a, d, 24);
  780. zip->written_bytes += 24;
  781. } else {
  782. archive_le32enc(d + 8, (uint32_t)zip->entry_compressed_written);
  783. archive_le32enc(d + 12, (uint32_t)zip->entry_uncompressed_written);
  784. ret = __archive_write_output(a, d, 16);
  785. zip->written_bytes += 16;
  786. }
  787. if (ret != ARCHIVE_OK)
  788. return (ARCHIVE_FATAL);
  789. }
  790. /* Append Zip64 extra data to central directory information. */
  791. if (zip->entry_compressed_written > ARCHIVE_LITERAL_LL(0xffffffff)
  792. || zip->entry_uncompressed_written > ARCHIVE_LITERAL_LL(0xffffffff)
  793. || zip->entry_offset > ARCHIVE_LITERAL_LL(0xffffffff)) {
  794. unsigned char zip64[32];
  795. unsigned char *z = zip64, *zd;
  796. memcpy(z, "\001\000\000\000", 4);
  797. z += 4;
  798. if (zip->entry_uncompressed_written >= ARCHIVE_LITERAL_LL(0xffffffff)) {
  799. archive_le64enc(z, zip->entry_uncompressed_written);
  800. z += 8;
  801. }
  802. if (zip->entry_compressed_written >= ARCHIVE_LITERAL_LL(0xffffffff)) {
  803. archive_le64enc(z, zip->entry_compressed_written);
  804. z += 8;
  805. }
  806. if (zip->entry_offset >= ARCHIVE_LITERAL_LL(0xffffffff)) {
  807. archive_le64enc(z, zip->entry_offset);
  808. z += 8;
  809. }
  810. archive_le16enc(zip64 + 2, z - (zip64 + 4));
  811. zd = cd_alloc(zip, z - zip64);
  812. if (zd == NULL) {
  813. archive_set_error(&a->archive, ENOMEM,
  814. "Can't allocate zip data");
  815. return (ARCHIVE_FATAL);
  816. }
  817. memcpy(zd, zip64, z - zip64);
  818. /* Zip64 means version needs to be set to at least 4.5 */
  819. if (archive_le16dec(zip->file_header + 6) < 45)
  820. archive_le16enc(zip->file_header + 6, 45);
  821. }
  822. /* Fix up central directory file header. */
  823. archive_le32enc(zip->file_header + 16, zip->entry_crc32);
  824. archive_le32enc(zip->file_header + 20,
  825. zipmin(zip->entry_compressed_written, ARCHIVE_LITERAL_LL(0xffffffff)));
  826. archive_le32enc(zip->file_header + 24,
  827. zipmin(zip->entry_uncompressed_written, ARCHIVE_LITERAL_LL(0xffffffff)));
  828. archive_le16enc(zip->file_header + 30,
  829. zip->central_directory_bytes - zip->file_header_extra_offset);
  830. archive_le32enc(zip->file_header + 42,
  831. zipmin(zip->entry_offset, ARCHIVE_LITERAL_LL(0xffffffff)));
  832. return (ARCHIVE_OK);
  833. }
  834. static int
  835. archive_write_zip_close(struct archive_write *a)
  836. {
  837. uint8_t buff[64];
  838. int64_t offset_start, offset_end;
  839. struct zip *zip = a->format_data;
  840. struct cd_segment *segment;
  841. int ret;
  842. offset_start = zip->written_bytes;
  843. segment = zip->central_directory;
  844. while (segment != NULL) {
  845. ret = __archive_write_output(a,
  846. segment->buff, segment->p - segment->buff);
  847. if (ret != ARCHIVE_OK)
  848. return (ARCHIVE_FATAL);
  849. zip->written_bytes += segment->p - segment->buff;
  850. segment = segment->next;
  851. }
  852. offset_end = zip->written_bytes;
  853. /* If central dir info is too large, write Zip64 end-of-cd */
  854. if (offset_end - offset_start > ARCHIVE_LITERAL_LL(0xffffffff)
  855. || offset_start > ARCHIVE_LITERAL_LL(0xffffffff)
  856. || zip->central_directory_entries > 0xffffUL
  857. || (zip->flags & ZIP_FLAG_FORCE_ZIP64)) {
  858. /* Zip64 end-of-cd record */
  859. memset(buff, 0, 56);
  860. memcpy(buff, "PK\006\006", 4);
  861. archive_le64enc(buff + 4, 44);
  862. archive_le16enc(buff + 12, 45);
  863. archive_le16enc(buff + 14, 45);
  864. /* This is disk 0 of 0. */
  865. archive_le64enc(buff + 24, zip->central_directory_entries);
  866. archive_le64enc(buff + 32, zip->central_directory_entries);
  867. archive_le64enc(buff + 40, offset_end - offset_start);
  868. archive_le64enc(buff + 48, offset_start);
  869. ret = __archive_write_output(a, buff, 56);
  870. if (ret != ARCHIVE_OK)
  871. return (ARCHIVE_FATAL);
  872. zip->written_bytes += 56;
  873. /* Zip64 end-of-cd locator record. */
  874. memset(buff, 0, 20);
  875. memcpy(buff, "PK\006\007", 4);
  876. archive_le32enc(buff + 4, 0);
  877. archive_le64enc(buff + 8, offset_end);
  878. archive_le32enc(buff + 16, 1);
  879. ret = __archive_write_output(a, buff, 20);
  880. if (ret != ARCHIVE_OK)
  881. return (ARCHIVE_FATAL);
  882. zip->written_bytes += 20;
  883. }
  884. /* Format and write end of central directory. */
  885. memset(buff, 0, sizeof(buff));
  886. memcpy(buff, "PK\005\006", 4);
  887. archive_le16enc(buff + 8, zipmin(0xffffU, zip->central_directory_entries));
  888. archive_le16enc(buff + 10, zipmin(0xffffU, zip->central_directory_entries));
  889. archive_le32enc(buff + 12, (uint32_t)zipmin(ARCHIVE_LITERAL_LL(0xffffffff), (offset_end - offset_start)));
  890. archive_le32enc(buff + 16, (uint32_t)zipmin(ARCHIVE_LITERAL_LL(0xffffffff), offset_start));
  891. ret = __archive_write_output(a, buff, 22);
  892. if (ret != ARCHIVE_OK)
  893. return (ARCHIVE_FATAL);
  894. zip->written_bytes += 22;
  895. return (ARCHIVE_OK);
  896. }
  897. static int
  898. archive_write_zip_free(struct archive_write *a)
  899. {
  900. struct zip *zip;
  901. struct cd_segment *segment;
  902. zip = a->format_data;
  903. while (zip->central_directory != NULL) {
  904. segment = zip->central_directory;
  905. zip->central_directory = segment->next;
  906. free(segment->buff);
  907. free(segment);
  908. }
  909. #ifdef HAVE_ZLIB_H
  910. free(zip->buf);
  911. #endif
  912. archive_entry_free(zip->entry);
  913. /* TODO: Free opt_sconv, sconv_default */
  914. free(zip);
  915. a->format_data = NULL;
  916. return (ARCHIVE_OK);
  917. }
  918. /* Convert into MSDOS-style date/time. */
  919. static unsigned int
  920. dos_time(const time_t unix_time)
  921. {
  922. struct tm *t;
  923. unsigned int dt;
  924. /* This will not preserve time when creating/extracting the archive
  925. * on two systems with different time zones. */
  926. t = localtime(&unix_time);
  927. /* MSDOS-style date/time is only between 1980-01-01 and 2107-12-31 */
  928. if (t->tm_year < 1980 - 1900)
  929. /* Set minimum date/time '1980-01-01 00:00:00'. */
  930. dt = 0x00210000U;
  931. else if (t->tm_year > 2107 - 1900)
  932. /* Set maximum date/time '2107-12-31 23:59:58'. */
  933. dt = 0xff9fbf7dU;
  934. else {
  935. dt = 0;
  936. dt += ((t->tm_year - 80) & 0x7f) << 9;
  937. dt += ((t->tm_mon + 1) & 0x0f) << 5;
  938. dt += (t->tm_mday & 0x1f);
  939. dt <<= 16;
  940. dt += (t->tm_hour & 0x1f) << 11;
  941. dt += (t->tm_min & 0x3f) << 5;
  942. dt += (t->tm_sec & 0x3e) >> 1; /* Only counting every 2 seconds. */
  943. }
  944. return dt;
  945. }
  946. static size_t
  947. path_length(struct archive_entry *entry)
  948. {
  949. mode_t type;
  950. const char *path;
  951. type = archive_entry_filetype(entry);
  952. path = archive_entry_pathname(entry);
  953. if (path == NULL)
  954. return (0);
  955. if (type == AE_IFDIR &&
  956. (path[0] == '\0' || path[strlen(path) - 1] != '/')) {
  957. return strlen(path) + 1;
  958. } else {
  959. return strlen(path);
  960. }
  961. }
  962. static int
  963. write_path(struct archive_entry *entry, struct archive_write *archive)
  964. {
  965. int ret;
  966. const char *path;
  967. mode_t type;
  968. size_t written_bytes;
  969. path = archive_entry_pathname(entry);
  970. type = archive_entry_filetype(entry);
  971. written_bytes = 0;
  972. ret = __archive_write_output(archive, path, strlen(path));
  973. if (ret != ARCHIVE_OK)
  974. return (ARCHIVE_FATAL);
  975. written_bytes += strlen(path);
  976. /* Folders are recognized by a trailing slash. */
  977. if ((type == AE_IFDIR) & (path[strlen(path) - 1] != '/')) {
  978. ret = __archive_write_output(archive, "/", 1);
  979. if (ret != ARCHIVE_OK)
  980. return (ARCHIVE_FATAL);
  981. written_bytes += 1;
  982. }
  983. return ((int)written_bytes);
  984. }
  985. static void
  986. copy_path(struct archive_entry *entry, unsigned char *p)
  987. {
  988. const char *path;
  989. size_t pathlen;
  990. mode_t type;
  991. path = archive_entry_pathname(entry);
  992. pathlen = strlen(path);
  993. type = archive_entry_filetype(entry);
  994. memcpy(p, path, pathlen);
  995. /* Folders are recognized by a trailing slash. */
  996. if ((type == AE_IFDIR) & (path[pathlen - 1] != '/')) {
  997. p[pathlen] = '/';
  998. p[pathlen + 1] = '\0';
  999. }
  1000. }
  1001. static struct archive_string_conv *
  1002. get_sconv(struct archive_write *a, struct zip *zip)
  1003. {
  1004. if (zip->opt_sconv != NULL)
  1005. return (zip->opt_sconv);
  1006. if (!zip->init_default_conversion) {
  1007. zip->sconv_default =
  1008. archive_string_default_conversion_for_write(&(a->archive));
  1009. zip->init_default_conversion = 1;
  1010. }
  1011. return (zip->sconv_default);
  1012. }