archive_read_support_format_zip.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  1. /*-
  2. * Copyright (c) 2004 Tim Kientzle
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  16. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  17. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  19. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  21. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "archive_platform.h"
  26. __FBSDID("$FreeBSD: src/lib/libarchive/archive_read_support_format_zip.c,v 1.28 2008/12/06 06:45:15 kientzle Exp $");
  27. #ifdef HAVE_ERRNO_H
  28. #include <errno.h>
  29. #endif
  30. #include <stdio.h>
  31. #ifdef HAVE_STDLIB_H
  32. #include <stdlib.h>
  33. #endif
  34. #include <time.h>
  35. #ifdef HAVE_ZLIB_H
  36. #include <cm_zlib.h>
  37. #endif
  38. #include "archive.h"
  39. #include "archive_entry.h"
  40. #include "archive_private.h"
  41. #include "archive_read_private.h"
  42. #include "archive_endian.h"
  43. #ifndef HAVE_ZLIB_H
  44. #include "archive_crc32.h"
  45. #endif
  46. struct zip {
  47. /* entry_bytes_remaining is the number of bytes we expect. */
  48. int64_t entry_bytes_remaining;
  49. int64_t entry_offset;
  50. /* These count the number of bytes actually read for the entry. */
  51. int64_t entry_compressed_bytes_read;
  52. int64_t entry_uncompressed_bytes_read;
  53. /* Running CRC32 of the decompressed data */
  54. unsigned long entry_crc32;
  55. unsigned version;
  56. unsigned system;
  57. unsigned flags;
  58. unsigned compression;
  59. const char * compression_name;
  60. time_t mtime;
  61. time_t ctime;
  62. time_t atime;
  63. mode_t mode;
  64. uid_t uid;
  65. gid_t gid;
  66. /* Flags to mark progress of decompression. */
  67. char decompress_init;
  68. char end_of_entry;
  69. unsigned long crc32;
  70. ssize_t filename_length;
  71. ssize_t extra_length;
  72. int64_t uncompressed_size;
  73. int64_t compressed_size;
  74. unsigned char *uncompressed_buffer;
  75. size_t uncompressed_buffer_size;
  76. #ifdef HAVE_ZLIB_H
  77. z_stream stream;
  78. char stream_valid;
  79. #endif
  80. struct archive_string pathname;
  81. struct archive_string extra;
  82. char format_name[64];
  83. };
  84. #define ZIP_LENGTH_AT_END 8
  85. struct zip_file_header {
  86. char signature[4];
  87. char version[2];
  88. char flags[2];
  89. char compression[2];
  90. char timedate[4];
  91. char crc32[4];
  92. char compressed_size[4];
  93. char uncompressed_size[4];
  94. char filename_length[2];
  95. char extra_length[2];
  96. };
  97. static const char *compression_names[] = {
  98. "uncompressed",
  99. "shrinking",
  100. "reduced-1",
  101. "reduced-2",
  102. "reduced-3",
  103. "reduced-4",
  104. "imploded",
  105. "reserved",
  106. "deflation"
  107. };
  108. static int archive_read_format_zip_bid(struct archive_read *);
  109. static int archive_read_format_zip_cleanup(struct archive_read *);
  110. static int archive_read_format_zip_read_data(struct archive_read *,
  111. const void **, size_t *, off_t *);
  112. static int archive_read_format_zip_read_data_skip(struct archive_read *a);
  113. static int archive_read_format_zip_read_header(struct archive_read *,
  114. struct archive_entry *);
  115. static int zip_read_data_deflate(struct archive_read *a, const void **buff,
  116. size_t *size, off_t *offset);
  117. static int zip_read_data_none(struct archive_read *a, const void **buff,
  118. size_t *size, off_t *offset);
  119. static int zip_read_file_header(struct archive_read *a,
  120. struct archive_entry *entry, struct zip *zip);
  121. static time_t zip_time(const char *);
  122. static void process_extra(const void* extra, struct zip* zip);
  123. int
  124. archive_read_support_format_zip(struct archive *_a)
  125. {
  126. struct archive_read *a = (struct archive_read *)_a;
  127. struct zip *zip;
  128. int r;
  129. zip = (struct zip *)malloc(sizeof(*zip));
  130. if (zip == NULL) {
  131. archive_set_error(&a->archive, ENOMEM, "Can't allocate zip data");
  132. return (ARCHIVE_FATAL);
  133. }
  134. memset(zip, 0, sizeof(*zip));
  135. r = __archive_read_register_format(a,
  136. zip,
  137. "zip",
  138. archive_read_format_zip_bid,
  139. NULL,
  140. archive_read_format_zip_read_header,
  141. archive_read_format_zip_read_data,
  142. archive_read_format_zip_read_data_skip,
  143. archive_read_format_zip_cleanup);
  144. if (r != ARCHIVE_OK)
  145. free(zip);
  146. return (ARCHIVE_OK);
  147. }
  148. static int
  149. archive_read_format_zip_bid(struct archive_read *a)
  150. {
  151. const char *p;
  152. const void *buff;
  153. ssize_t bytes_avail, offset;
  154. if ((p = __archive_read_ahead(a, 4, NULL)) == NULL)
  155. return (-1);
  156. /*
  157. * Bid of 30 here is: 16 bits for "PK",
  158. * next 16-bit field has four options (-2 bits).
  159. * 16 + 16-2 = 30.
  160. */
  161. if (p[0] == 'P' && p[1] == 'K') {
  162. if ((p[2] == '\001' && p[3] == '\002')
  163. || (p[2] == '\003' && p[3] == '\004')
  164. || (p[2] == '\005' && p[3] == '\006')
  165. || (p[2] == '\007' && p[3] == '\010')
  166. || (p[2] == '0' && p[3] == '0'))
  167. return (30);
  168. }
  169. /*
  170. * Attempt to handle self-extracting archives
  171. * by noting a PE header and searching forward
  172. * up to 128k for a 'PK\003\004' marker.
  173. */
  174. if (p[0] == 'M' && p[1] == 'Z') {
  175. /*
  176. * TODO: Optimize by initializing 'offset' to an
  177. * estimate of the likely start of the archive data
  178. * based on values in the PE header. Note that we
  179. * don't need to be exact, but we mustn't skip too
  180. * far. The search below will compensate if we
  181. * undershoot.
  182. */
  183. offset = 0;
  184. while (offset < 124000) {
  185. /* Get 4k of data beyond where we stopped. */
  186. buff = __archive_read_ahead(a, offset + 4096,
  187. &bytes_avail);
  188. if (bytes_avail < offset + 1)
  189. break;
  190. p = (const char *)buff + offset;
  191. while (p + 9 < (const char *)buff + bytes_avail) {
  192. if (p[0] == 'P' && p[1] == 'K' /* signature */
  193. && p[2] == 3 && p[3] == 4 /* File entry */
  194. && p[8] == 8 /* compression == deflate */
  195. && p[9] == 0 /* High byte of compression */
  196. )
  197. {
  198. return (30);
  199. }
  200. ++p;
  201. }
  202. offset = p - (const char *)buff;
  203. }
  204. }
  205. return (0);
  206. }
  207. /*
  208. * Search forward for a "PK\003\004" file header. This handles the
  209. * case of self-extracting archives, where there is an executable
  210. * prepended to the ZIP archive.
  211. */
  212. static int
  213. skip_sfx(struct archive_read *a)
  214. {
  215. const void *h;
  216. const char *p, *q;
  217. size_t skip;
  218. ssize_t bytes;
  219. /*
  220. * TODO: We should be able to skip forward by a bunch
  221. * by lifting some values from the PE header. We don't
  222. * need to be exact (we're still going to search forward
  223. * to find the header), but it will speed things up and
  224. * reduce the chance of a false positive.
  225. */
  226. for (;;) {
  227. h = __archive_read_ahead(a, 4, &bytes);
  228. if (bytes < 4)
  229. return (ARCHIVE_FATAL);
  230. p = h;
  231. q = p + bytes;
  232. /*
  233. * Scan ahead until we find something that looks
  234. * like the zip header.
  235. */
  236. while (p + 4 < q) {
  237. switch (p[3]) {
  238. case '\004':
  239. /* TODO: Additional verification here. */
  240. if (memcmp("PK\003\004", p, 4) == 0) {
  241. skip = p - (const char *)h;
  242. __archive_read_consume(a, skip);
  243. return (ARCHIVE_OK);
  244. }
  245. p += 4;
  246. break;
  247. case '\003': p += 1; break;
  248. case 'K': p += 2; break;
  249. case 'P': p += 3; break;
  250. default: p += 4; break;
  251. }
  252. }
  253. skip = p - (const char *)h;
  254. __archive_read_consume(a, skip);
  255. }
  256. }
  257. static int
  258. archive_read_format_zip_read_header(struct archive_read *a,
  259. struct archive_entry *entry)
  260. {
  261. const void *h;
  262. const char *signature;
  263. struct zip *zip;
  264. int r = ARCHIVE_OK, r1;
  265. a->archive.archive_format = ARCHIVE_FORMAT_ZIP;
  266. if (a->archive.archive_format_name == NULL)
  267. a->archive.archive_format_name = "ZIP";
  268. zip = (struct zip *)(a->format->data);
  269. zip->decompress_init = 0;
  270. zip->end_of_entry = 0;
  271. zip->entry_uncompressed_bytes_read = 0;
  272. zip->entry_compressed_bytes_read = 0;
  273. zip->entry_crc32 = crc32(0, NULL, 0);
  274. if ((h = __archive_read_ahead(a, 4, NULL)) == NULL)
  275. return (ARCHIVE_FATAL);
  276. signature = (const char *)h;
  277. if (signature[0] == 'M' && signature[1] == 'Z') {
  278. /* This is an executable? Must be self-extracting... */
  279. r = skip_sfx(a);
  280. if (r < ARCHIVE_WARN)
  281. return (r);
  282. if ((h = __archive_read_ahead(a, 4, NULL)) == NULL)
  283. return (ARCHIVE_FATAL);
  284. signature = (const char *)h;
  285. }
  286. if (signature[0] != 'P' || signature[1] != 'K') {
  287. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  288. "Bad ZIP file");
  289. return (ARCHIVE_FATAL);
  290. }
  291. /*
  292. * "PK00" signature is used for "split" archives that
  293. * only have a single segment. This means we can just
  294. * skip the PK00; the first real file header should follow.
  295. */
  296. if (signature[2] == '0' && signature[3] == '0') {
  297. __archive_read_consume(a, 4);
  298. if ((h = __archive_read_ahead(a, 4, NULL)) == NULL)
  299. return (ARCHIVE_FATAL);
  300. signature = (const char *)h;
  301. if (signature[0] != 'P' || signature[1] != 'K') {
  302. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  303. "Bad ZIP file");
  304. return (ARCHIVE_FATAL);
  305. }
  306. }
  307. if (signature[2] == '\001' && signature[3] == '\002') {
  308. /* Beginning of central directory. */
  309. return (ARCHIVE_EOF);
  310. }
  311. if (signature[2] == '\003' && signature[3] == '\004') {
  312. /* Regular file entry. */
  313. r1 = zip_read_file_header(a, entry, zip);
  314. if (r1 != ARCHIVE_OK)
  315. return (r1);
  316. return (r);
  317. }
  318. if (signature[2] == '\005' && signature[3] == '\006') {
  319. /* End-of-archive record. */
  320. return (ARCHIVE_EOF);
  321. }
  322. if (signature[2] == '\007' && signature[3] == '\010') {
  323. /*
  324. * We should never encounter this record here;
  325. * see ZIP_LENGTH_AT_END handling below for details.
  326. */
  327. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  328. "Bad ZIP file: Unexpected end-of-entry record");
  329. return (ARCHIVE_FATAL);
  330. }
  331. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  332. "Damaged ZIP file or unsupported format variant (%d,%d)",
  333. signature[2], signature[3]);
  334. return (ARCHIVE_FATAL);
  335. }
  336. static int
  337. zip_read_file_header(struct archive_read *a, struct archive_entry *entry,
  338. struct zip *zip)
  339. {
  340. const struct zip_file_header *p;
  341. const void *h;
  342. if ((p = __archive_read_ahead(a, sizeof *p, NULL)) == NULL) {
  343. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  344. "Truncated ZIP file header");
  345. return (ARCHIVE_FATAL);
  346. }
  347. zip->version = p->version[0];
  348. zip->system = p->version[1];
  349. zip->flags = archive_le16dec(p->flags);
  350. zip->compression = archive_le16dec(p->compression);
  351. if (zip->compression <
  352. sizeof(compression_names)/sizeof(compression_names[0]))
  353. zip->compression_name = compression_names[zip->compression];
  354. else
  355. zip->compression_name = "??";
  356. zip->mtime = zip_time(p->timedate);
  357. zip->ctime = 0;
  358. zip->atime = 0;
  359. zip->mode = 0;
  360. zip->uid = 0;
  361. zip->gid = 0;
  362. zip->crc32 = archive_le32dec(p->crc32);
  363. zip->filename_length = archive_le16dec(p->filename_length);
  364. zip->extra_length = archive_le16dec(p->extra_length);
  365. zip->uncompressed_size = archive_le32dec(p->uncompressed_size);
  366. zip->compressed_size = archive_le32dec(p->compressed_size);
  367. __archive_read_consume(a, sizeof(struct zip_file_header));
  368. /* Read the filename. */
  369. if ((h = __archive_read_ahead(a, zip->filename_length, NULL)) == NULL) {
  370. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  371. "Truncated ZIP file header");
  372. return (ARCHIVE_FATAL);
  373. }
  374. if (archive_string_ensure(&zip->pathname, zip->filename_length) == NULL)
  375. __archive_errx(1, "Out of memory");
  376. archive_strncpy(&zip->pathname, h, zip->filename_length);
  377. __archive_read_consume(a, zip->filename_length);
  378. archive_entry_set_pathname(entry, zip->pathname.s);
  379. if (zip->pathname.s[archive_strlen(&zip->pathname) - 1] == '/')
  380. zip->mode = AE_IFDIR | 0777;
  381. else
  382. zip->mode = AE_IFREG | 0777;
  383. /* Read the extra data. */
  384. if ((h = __archive_read_ahead(a, zip->extra_length, NULL)) == NULL) {
  385. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  386. "Truncated ZIP file header");
  387. return (ARCHIVE_FATAL);
  388. }
  389. process_extra(h, zip);
  390. __archive_read_consume(a, zip->extra_length);
  391. /* Populate some additional entry fields: */
  392. archive_entry_set_mode(entry, zip->mode);
  393. archive_entry_set_uid(entry, zip->uid);
  394. archive_entry_set_gid(entry, zip->gid);
  395. archive_entry_set_mtime(entry, zip->mtime, 0);
  396. archive_entry_set_ctime(entry, zip->ctime, 0);
  397. archive_entry_set_atime(entry, zip->atime, 0);
  398. /* Set the size only if it's meaningful. */
  399. if (0 == (zip->flags & ZIP_LENGTH_AT_END))
  400. archive_entry_set_size(entry, zip->uncompressed_size);
  401. zip->entry_bytes_remaining = zip->compressed_size;
  402. zip->entry_offset = 0;
  403. /* If there's no body, force read_data() to return EOF immediately. */
  404. if (0 == (zip->flags & ZIP_LENGTH_AT_END)
  405. && zip->entry_bytes_remaining < 1)
  406. zip->end_of_entry = 1;
  407. /* Set up a more descriptive format name. */
  408. sprintf(zip->format_name, "ZIP %d.%d (%s)",
  409. zip->version / 10, zip->version % 10,
  410. zip->compression_name);
  411. a->archive.archive_format_name = zip->format_name;
  412. return (ARCHIVE_OK);
  413. }
  414. /* Convert an MSDOS-style date/time into Unix-style time. */
  415. static time_t
  416. zip_time(const char *p)
  417. {
  418. int msTime, msDate;
  419. struct tm ts;
  420. msTime = (0xff & (unsigned)p[0]) + 256 * (0xff & (unsigned)p[1]);
  421. msDate = (0xff & (unsigned)p[2]) + 256 * (0xff & (unsigned)p[3]);
  422. memset(&ts, 0, sizeof(ts));
  423. ts.tm_year = ((msDate >> 9) & 0x7f) + 80; /* Years since 1900. */
  424. ts.tm_mon = ((msDate >> 5) & 0x0f) - 1; /* Month number. */
  425. ts.tm_mday = msDate & 0x1f; /* Day of month. */
  426. ts.tm_hour = (msTime >> 11) & 0x1f;
  427. ts.tm_min = (msTime >> 5) & 0x3f;
  428. ts.tm_sec = (msTime << 1) & 0x3e;
  429. ts.tm_isdst = -1;
  430. return mktime(&ts);
  431. }
  432. static int
  433. archive_read_format_zip_read_data(struct archive_read *a,
  434. const void **buff, size_t *size, off_t *offset)
  435. {
  436. int r;
  437. struct zip *zip;
  438. zip = (struct zip *)(a->format->data);
  439. /*
  440. * If we hit end-of-entry last time, clean up and return
  441. * ARCHIVE_EOF this time.
  442. */
  443. if (zip->end_of_entry) {
  444. *offset = zip->entry_uncompressed_bytes_read;
  445. *size = 0;
  446. *buff = NULL;
  447. return (ARCHIVE_EOF);
  448. }
  449. switch(zip->compression) {
  450. case 0: /* No compression. */
  451. r = zip_read_data_none(a, buff, size, offset);
  452. break;
  453. case 8: /* Deflate compression. */
  454. r = zip_read_data_deflate(a, buff, size, offset);
  455. break;
  456. default: /* Unsupported compression. */
  457. *buff = NULL;
  458. *size = 0;
  459. *offset = 0;
  460. /* Return a warning. */
  461. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  462. "Unsupported ZIP compression method (%s)",
  463. zip->compression_name);
  464. if (zip->flags & ZIP_LENGTH_AT_END) {
  465. /*
  466. * ZIP_LENGTH_AT_END requires us to
  467. * decompress the entry in order to
  468. * skip it, but we don't know this
  469. * compression method, so we give up.
  470. */
  471. r = ARCHIVE_FATAL;
  472. } else {
  473. /* We can't decompress this entry, but we will
  474. * be able to skip() it and try the next entry. */
  475. r = ARCHIVE_WARN;
  476. }
  477. break;
  478. }
  479. if (r != ARCHIVE_OK)
  480. return (r);
  481. /* Update checksum */
  482. if (*size)
  483. zip->entry_crc32 = crc32(zip->entry_crc32, *buff, *size);
  484. /* If we hit the end, swallow any end-of-data marker. */
  485. if (zip->end_of_entry) {
  486. if (zip->flags & ZIP_LENGTH_AT_END) {
  487. const char *p;
  488. if ((p = __archive_read_ahead(a, 16, NULL)) == NULL) {
  489. archive_set_error(&a->archive,
  490. ARCHIVE_ERRNO_FILE_FORMAT,
  491. "Truncated ZIP end-of-file record");
  492. return (ARCHIVE_FATAL);
  493. }
  494. zip->crc32 = archive_le32dec(p + 4);
  495. zip->compressed_size = archive_le32dec(p + 8);
  496. zip->uncompressed_size = archive_le32dec(p + 12);
  497. __archive_read_consume(a, 16);
  498. }
  499. /* Check file size, CRC against these values. */
  500. if (zip->compressed_size != zip->entry_compressed_bytes_read) {
  501. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  502. "ZIP compressed data is wrong size");
  503. return (ARCHIVE_WARN);
  504. }
  505. /* Size field only stores the lower 32 bits of the actual size. */
  506. if ((zip->uncompressed_size & UINT32_MAX)
  507. != (zip->entry_uncompressed_bytes_read & UINT32_MAX)) {
  508. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  509. "ZIP uncompressed data is wrong size");
  510. return (ARCHIVE_WARN);
  511. }
  512. /* Check computed CRC against header */
  513. if (zip->crc32 != zip->entry_crc32) {
  514. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  515. "ZIP bad CRC: 0x%lx should be 0x%lx",
  516. zip->entry_crc32, zip->crc32);
  517. return (ARCHIVE_WARN);
  518. }
  519. }
  520. /* Return EOF immediately if this is a non-regular file. */
  521. if (AE_IFREG != (zip->mode & AE_IFMT))
  522. return (ARCHIVE_EOF);
  523. return (ARCHIVE_OK);
  524. }
  525. /*
  526. * Read "uncompressed" data. According to the current specification,
  527. * if ZIP_LENGTH_AT_END is specified, then the size fields in the
  528. * initial file header are supposed to be set to zero. This would, of
  529. * course, make it impossible for us to read the archive, since we
  530. * couldn't determine the end of the file data. Info-ZIP seems to
  531. * include the real size fields both before and after the data in this
  532. * case (the CRC only appears afterwards), so this works as you would
  533. * expect.
  534. *
  535. * Returns ARCHIVE_OK if successful, ARCHIVE_FATAL otherwise, sets
  536. * zip->end_of_entry if it consumes all of the data.
  537. */
  538. static int
  539. zip_read_data_none(struct archive_read *a, const void **buff,
  540. size_t *size, off_t *offset)
  541. {
  542. struct zip *zip;
  543. ssize_t bytes_avail;
  544. zip = (struct zip *)(a->format->data);
  545. if (zip->entry_bytes_remaining == 0) {
  546. *buff = NULL;
  547. *size = 0;
  548. *offset = zip->entry_offset;
  549. zip->end_of_entry = 1;
  550. return (ARCHIVE_OK);
  551. }
  552. /*
  553. * Note: '1' here is a performance optimization.
  554. * Recall that the decompression layer returns a count of
  555. * available bytes; asking for more than that forces the
  556. * decompressor to combine reads by copying data.
  557. */
  558. *buff = __archive_read_ahead(a, 1, &bytes_avail);
  559. if (bytes_avail <= 0) {
  560. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  561. "Truncated ZIP file data");
  562. return (ARCHIVE_FATAL);
  563. }
  564. if (bytes_avail > zip->entry_bytes_remaining)
  565. bytes_avail = zip->entry_bytes_remaining;
  566. __archive_read_consume(a, bytes_avail);
  567. *size = bytes_avail;
  568. *offset = zip->entry_offset;
  569. zip->entry_offset += *size;
  570. zip->entry_bytes_remaining -= *size;
  571. zip->entry_uncompressed_bytes_read += *size;
  572. zip->entry_compressed_bytes_read += *size;
  573. return (ARCHIVE_OK);
  574. }
  575. #ifdef HAVE_ZLIB_H
  576. static int
  577. zip_read_data_deflate(struct archive_read *a, const void **buff,
  578. size_t *size, off_t *offset)
  579. {
  580. struct zip *zip;
  581. ssize_t bytes_avail;
  582. const void *compressed_buff;
  583. int r;
  584. zip = (struct zip *)(a->format->data);
  585. /* If the buffer hasn't been allocated, allocate it now. */
  586. if (zip->uncompressed_buffer == NULL) {
  587. zip->uncompressed_buffer_size = 32 * 1024;
  588. zip->uncompressed_buffer
  589. = (unsigned char *)malloc(zip->uncompressed_buffer_size);
  590. if (zip->uncompressed_buffer == NULL) {
  591. archive_set_error(&a->archive, ENOMEM,
  592. "No memory for ZIP decompression");
  593. return (ARCHIVE_FATAL);
  594. }
  595. }
  596. /* If we haven't yet read any data, initialize the decompressor. */
  597. if (!zip->decompress_init) {
  598. if (zip->stream_valid)
  599. r = inflateReset(&zip->stream);
  600. else
  601. r = inflateInit2(&zip->stream,
  602. -15 /* Don't check for zlib header */);
  603. if (r != Z_OK) {
  604. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  605. "Can't initialize ZIP decompression.");
  606. return (ARCHIVE_FATAL);
  607. }
  608. /* Stream structure has been set up. */
  609. zip->stream_valid = 1;
  610. /* We've initialized decompression for this stream. */
  611. zip->decompress_init = 1;
  612. }
  613. /*
  614. * Note: '1' here is a performance optimization.
  615. * Recall that the decompression layer returns a count of
  616. * available bytes; asking for more than that forces the
  617. * decompressor to combine reads by copying data.
  618. */
  619. compressed_buff = __archive_read_ahead(a, 1, &bytes_avail);
  620. if (bytes_avail <= 0) {
  621. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  622. "Truncated ZIP file body");
  623. return (ARCHIVE_FATAL);
  624. }
  625. /*
  626. * A bug in zlib.h: stream.next_in should be marked 'const'
  627. * but isn't (the library never alters data through the
  628. * next_in pointer, only reads it). The result: this ugly
  629. * cast to remove 'const'.
  630. */
  631. zip->stream.next_in = (Bytef *)(uintptr_t)(const void *)compressed_buff;
  632. zip->stream.avail_in = bytes_avail;
  633. zip->stream.total_in = 0;
  634. zip->stream.next_out = zip->uncompressed_buffer;
  635. zip->stream.avail_out = zip->uncompressed_buffer_size;
  636. zip->stream.total_out = 0;
  637. r = inflate(&zip->stream, 0);
  638. switch (r) {
  639. case Z_OK:
  640. break;
  641. case Z_STREAM_END:
  642. zip->end_of_entry = 1;
  643. break;
  644. case Z_MEM_ERROR:
  645. archive_set_error(&a->archive, ENOMEM,
  646. "Out of memory for ZIP decompression");
  647. return (ARCHIVE_FATAL);
  648. default:
  649. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  650. "ZIP decompression failed (%d)", r);
  651. return (ARCHIVE_FATAL);
  652. }
  653. /* Consume as much as the compressor actually used. */
  654. bytes_avail = zip->stream.total_in;
  655. __archive_read_consume(a, bytes_avail);
  656. zip->entry_bytes_remaining -= bytes_avail;
  657. zip->entry_compressed_bytes_read += bytes_avail;
  658. *offset = zip->entry_offset;
  659. *size = zip->stream.total_out;
  660. zip->entry_uncompressed_bytes_read += *size;
  661. *buff = zip->uncompressed_buffer;
  662. zip->entry_offset += *size;
  663. return (ARCHIVE_OK);
  664. }
  665. #else
  666. static int
  667. zip_read_data_deflate(struct archive_read *a, const void **buff,
  668. size_t *size, off_t *offset)
  669. {
  670. *buff = NULL;
  671. *size = 0;
  672. *offset = 0;
  673. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  674. "libarchive compiled without deflate support (no libz)");
  675. return (ARCHIVE_FATAL);
  676. }
  677. #endif
  678. static int
  679. archive_read_format_zip_read_data_skip(struct archive_read *a)
  680. {
  681. struct zip *zip;
  682. const void *buff = NULL;
  683. off_t bytes_skipped;
  684. zip = (struct zip *)(a->format->data);
  685. /* If we've already read to end of data, we're done. */
  686. if (zip->end_of_entry)
  687. return (ARCHIVE_OK);
  688. /*
  689. * If the length is at the end, we have no choice but
  690. * to decompress all the data to find the end marker.
  691. */
  692. if (zip->flags & ZIP_LENGTH_AT_END) {
  693. size_t size;
  694. off_t offset;
  695. int r;
  696. do {
  697. r = archive_read_format_zip_read_data(a, &buff,
  698. &size, &offset);
  699. } while (r == ARCHIVE_OK);
  700. return (r);
  701. }
  702. /*
  703. * If the length is at the beginning, we can skip the
  704. * compressed data much more quickly.
  705. */
  706. bytes_skipped = __archive_read_skip(a, zip->entry_bytes_remaining);
  707. if (bytes_skipped < 0)
  708. return (ARCHIVE_FATAL);
  709. /* This entry is finished and done. */
  710. zip->end_of_entry = 1;
  711. return (ARCHIVE_OK);
  712. }
  713. static int
  714. archive_read_format_zip_cleanup(struct archive_read *a)
  715. {
  716. struct zip *zip;
  717. zip = (struct zip *)(a->format->data);
  718. #ifdef HAVE_ZLIB_H
  719. if (zip->stream_valid)
  720. inflateEnd(&zip->stream);
  721. #endif
  722. free(zip->uncompressed_buffer);
  723. archive_string_free(&(zip->pathname));
  724. archive_string_free(&(zip->extra));
  725. free(zip);
  726. (a->format->data) = NULL;
  727. return (ARCHIVE_OK);
  728. }
  729. /*
  730. * The extra data is stored as a list of
  731. * id1+size1+data1 + id2+size2+data2 ...
  732. * triplets. id and size are 2 bytes each.
  733. */
  734. static void
  735. process_extra(const void* extra, struct zip* zip)
  736. {
  737. int offset = 0;
  738. const char *p = (const char *)extra;
  739. while (offset < zip->extra_length - 4)
  740. {
  741. unsigned short headerid = archive_le16dec(p + offset);
  742. unsigned short datasize = archive_le16dec(p + offset + 2);
  743. offset += 4;
  744. if (offset + datasize > zip->extra_length)
  745. break;
  746. #ifdef DEBUG
  747. fprintf(stderr, "Header id 0x%04x, length %d\n",
  748. headerid, datasize);
  749. #endif
  750. switch (headerid) {
  751. case 0x0001:
  752. /* Zip64 extended information extra field. */
  753. if (datasize >= 8)
  754. zip->uncompressed_size = archive_le64dec(p + offset);
  755. if (datasize >= 16)
  756. zip->compressed_size = archive_le64dec(p + offset + 8);
  757. break;
  758. case 0x5455:
  759. {
  760. /* Extended time field "UT". */
  761. int flags = p[offset];
  762. offset++;
  763. datasize--;
  764. /* Flag bits indicate which dates are present. */
  765. if (flags & 0x01)
  766. {
  767. #ifdef DEBUG
  768. fprintf(stderr, "mtime: %lld -> %d\n",
  769. (long long)zip->mtime,
  770. archive_le32dec(p + offset));
  771. #endif
  772. if (datasize < 4)
  773. break;
  774. zip->mtime = archive_le32dec(p + offset);
  775. offset += 4;
  776. datasize -= 4;
  777. }
  778. if (flags & 0x02)
  779. {
  780. if (datasize < 4)
  781. break;
  782. zip->atime = archive_le32dec(p + offset);
  783. offset += 4;
  784. datasize -= 4;
  785. }
  786. if (flags & 0x04)
  787. {
  788. if (datasize < 4)
  789. break;
  790. zip->ctime = archive_le32dec(p + offset);
  791. offset += 4;
  792. datasize -= 4;
  793. }
  794. break;
  795. }
  796. case 0x7855:
  797. /* Info-ZIP Unix Extra Field (type 2) "Ux". */
  798. #ifdef DEBUG
  799. fprintf(stderr, "uid %d gid %d\n",
  800. archive_le16dec(p + offset),
  801. archive_le16dec(p + offset + 2));
  802. #endif
  803. if (datasize >= 2)
  804. zip->uid = archive_le16dec(p + offset);
  805. if (datasize >= 4)
  806. zip->gid = archive_le16dec(p + offset + 2);
  807. break;
  808. default:
  809. break;
  810. }
  811. offset += datasize;
  812. }
  813. #ifdef DEBUG
  814. if (offset != zip->extra_length)
  815. {
  816. fprintf(stderr,
  817. "Extra data field contents do not match reported size!");
  818. }
  819. #endif
  820. }