archive_read_support_format_zip.c 25 KB

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