archive_read_support_compression_gzip.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /*-
  2. * Copyright (c) 2003-2007 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_compression_gzip.c,v 1.17 2008/12/06 06:45:15 kientzle Exp $");
  27. #ifdef HAVE_ERRNO_H
  28. #include <errno.h>
  29. #endif
  30. #ifdef HAVE_STDLIB_H
  31. #include <stdlib.h>
  32. #endif
  33. #ifdef HAVE_STRING_H
  34. #include <string.h>
  35. #endif
  36. #ifdef HAVE_UNISTD_H
  37. #include <unistd.h>
  38. #endif
  39. #ifdef HAVE_ZLIB_H
  40. #include <zlib.h>
  41. #endif
  42. #include "archive.h"
  43. #include "archive_private.h"
  44. #include "archive_read_private.h"
  45. #ifdef HAVE_ZLIB_H
  46. struct private_data {
  47. z_stream stream;
  48. char in_stream;
  49. unsigned char *out_block;
  50. size_t out_block_size;
  51. int64_t total_out;
  52. unsigned long crc;
  53. char eof; /* True = found end of compressed data. */
  54. };
  55. /* Gzip Filter. */
  56. static ssize_t gzip_filter_read(struct archive_read_filter *, const void **);
  57. static int gzip_filter_close(struct archive_read_filter *);
  58. #endif
  59. /*
  60. * Note that we can detect gzip archives even if we can't decompress
  61. * them. (In fact, we like detecting them because we can give better
  62. * error messages.) So the bid framework here gets compiled even
  63. * if zlib is unavailable.
  64. *
  65. * TODO: If zlib is unavailable, gzip_bidder_init() should
  66. * use the compress_program framework to try to fire up an external
  67. * gunzip program.
  68. */
  69. static int gzip_bidder_bid(struct archive_read_filter_bidder *,
  70. struct archive_read_filter *);
  71. static int gzip_bidder_init(struct archive_read_filter *);
  72. int
  73. archive_read_support_compression_gzip(struct archive *_a)
  74. {
  75. struct archive_read *a = (struct archive_read *)_a;
  76. struct archive_read_filter_bidder *bidder = __archive_read_get_bidder(a);
  77. if (bidder == NULL)
  78. return (ARCHIVE_FATAL);
  79. bidder->data = NULL;
  80. bidder->bid = gzip_bidder_bid;
  81. bidder->init = gzip_bidder_init;
  82. bidder->options = NULL;
  83. bidder->free = NULL; /* No data, so no cleanup necessary. */
  84. /* Signal the extent of gzip support with the return value here. */
  85. #if HAVE_ZLIB_H
  86. return (ARCHIVE_OK);
  87. #else
  88. archive_set_error(_a, ARCHIVE_ERRNO_MISC,
  89. "Using external gunzip program");
  90. return (ARCHIVE_WARN);
  91. #endif
  92. }
  93. /*
  94. * Read and verify the header.
  95. *
  96. * Returns zero if the header couldn't be validated, else returns
  97. * number of bytes in header. If pbits is non-NULL, it receives a
  98. * count of bits verified, suitable for use by bidder.
  99. */
  100. static int
  101. peek_at_header(struct archive_read_filter *filter, int *pbits)
  102. {
  103. const unsigned char *p;
  104. ssize_t avail, len;
  105. int bits = 0;
  106. int header_flags;
  107. /* Start by looking at the first ten bytes of the header, which
  108. * is all fixed layout. */
  109. len = 10;
  110. p = __archive_read_filter_ahead(filter, len, &avail);
  111. if (p == NULL || avail == 0)
  112. return (0);
  113. if (p[0] != 037)
  114. return (0);
  115. bits += 8;
  116. if (p[1] != 0213)
  117. return (0);
  118. bits += 8;
  119. if (p[2] != 8) /* We only support deflation. */
  120. return (0);
  121. bits += 8;
  122. if ((p[3] & 0xE0)!= 0) /* No reserved flags set. */
  123. return (0);
  124. bits += 3;
  125. header_flags = p[3];
  126. /* Bytes 4-7 are mod time. */
  127. /* Byte 8 is deflate flags. */
  128. /* XXXX TODO: return deflate flags back to consume_header for use
  129. in initializing the decompressor. */
  130. /* Byte 9 is OS. */
  131. /* Optional extra data: 2 byte length plus variable body. */
  132. if (header_flags & 4) {
  133. p = __archive_read_filter_ahead(filter, len + 2, &avail);
  134. if (p == NULL)
  135. return (0);
  136. len += ((int)p[len + 1] << 8) | (int)p[len];
  137. }
  138. /* Null-terminated optional filename. */
  139. if (header_flags & 8) {
  140. do {
  141. ++len;
  142. if (avail < len)
  143. p = __archive_read_filter_ahead(filter,
  144. len, &avail);
  145. if (p == NULL)
  146. return (0);
  147. } while (p[len - 1] != 0);
  148. }
  149. /* Null-terminated optional comment. */
  150. if (header_flags & 16) {
  151. do {
  152. ++len;
  153. if (avail < len)
  154. p = __archive_read_filter_ahead(filter,
  155. len, &avail);
  156. if (p == NULL)
  157. return (0);
  158. } while (p[len - 1] != 0);
  159. }
  160. /* Optional header CRC */
  161. if ((header_flags & 2)) {
  162. p = __archive_read_filter_ahead(filter, len + 2, &avail);
  163. if (p == NULL)
  164. return (0);
  165. #if 0
  166. int hcrc = ((int)p[len + 1] << 8) | (int)p[len];
  167. int crc = /* XXX TODO: Compute header CRC. */;
  168. if (crc != hcrc)
  169. return (0);
  170. bits += 16;
  171. #endif
  172. len += 2;
  173. }
  174. if (pbits != NULL)
  175. *pbits = bits;
  176. return (len);
  177. }
  178. /*
  179. * Bidder just verifies the header and returns the number of verified bits.
  180. */
  181. static int
  182. gzip_bidder_bid(struct archive_read_filter_bidder *self,
  183. struct archive_read_filter *filter)
  184. {
  185. int bits_checked;
  186. (void)self; /* UNUSED */
  187. if (peek_at_header(filter, &bits_checked))
  188. return (bits_checked);
  189. return (0);
  190. }
  191. #ifndef HAVE_ZLIB_H
  192. /*
  193. * If we don't have the library on this system, we can't do the
  194. * decompression directly. We can, however, try to run gunzip
  195. * in case that's available.
  196. */
  197. static int
  198. gzip_bidder_init(struct archive_read_filter *self)
  199. {
  200. int r;
  201. r = __archive_read_program(self, "gunzip");
  202. /* Note: We set the format here even if __archive_read_program()
  203. * above fails. We do, after all, know what the format is
  204. * even if we weren't able to read it. */
  205. self->code = ARCHIVE_COMPRESSION_GZIP;
  206. self->name = "gzip";
  207. return (r);
  208. }
  209. #else
  210. /*
  211. * Initialize the filter object.
  212. */
  213. static int
  214. gzip_bidder_init(struct archive_read_filter *self)
  215. {
  216. struct private_data *state;
  217. static const size_t out_block_size = 64 * 1024;
  218. void *out_block;
  219. self->code = ARCHIVE_COMPRESSION_GZIP;
  220. self->name = "gzip";
  221. state = (struct private_data *)calloc(1, sizeof(*state));
  222. out_block = (unsigned char *)malloc(out_block_size);
  223. if (state == NULL || out_block == NULL) {
  224. free(out_block);
  225. free(state);
  226. archive_set_error(&self->archive->archive, ENOMEM,
  227. "Can't allocate data for gzip decompression");
  228. return (ARCHIVE_FATAL);
  229. }
  230. self->data = state;
  231. state->out_block_size = out_block_size;
  232. state->out_block = out_block;
  233. self->read = gzip_filter_read;
  234. self->skip = NULL; /* not supported */
  235. self->close = gzip_filter_close;
  236. state->in_stream = 0; /* We're not actually within a stream yet. */
  237. return (ARCHIVE_OK);
  238. }
  239. static int
  240. consume_header(struct archive_read_filter *self)
  241. {
  242. struct private_data *state;
  243. ssize_t avail;
  244. size_t len;
  245. int ret;
  246. state = (struct private_data *)self->data;
  247. /* If this is a real header, consume it. */
  248. len = peek_at_header(self->upstream, NULL);
  249. if (len == 0)
  250. return (ARCHIVE_EOF);
  251. __archive_read_filter_consume(self->upstream, len);
  252. /* Initialize CRC accumulator. */
  253. state->crc = crc32(0L, NULL, 0);
  254. /* Initialize compression library. */
  255. state->stream.next_in = (unsigned char *)(uintptr_t)
  256. __archive_read_filter_ahead(self->upstream, 1, &avail);
  257. state->stream.avail_in = avail;
  258. ret = inflateInit2(&(state->stream),
  259. -15 /* Don't check for zlib header */);
  260. /* Decipher the error code. */
  261. switch (ret) {
  262. case Z_OK:
  263. state->in_stream = 1;
  264. return (ARCHIVE_OK);
  265. case Z_STREAM_ERROR:
  266. archive_set_error(&self->archive->archive,
  267. ARCHIVE_ERRNO_MISC,
  268. "Internal error initializing compression library: "
  269. "invalid setup parameter");
  270. break;
  271. case Z_MEM_ERROR:
  272. archive_set_error(&self->archive->archive, ENOMEM,
  273. "Internal error initializing compression library: "
  274. "out of memory");
  275. break;
  276. case Z_VERSION_ERROR:
  277. archive_set_error(&self->archive->archive,
  278. ARCHIVE_ERRNO_MISC,
  279. "Internal error initializing compression library: "
  280. "invalid library version");
  281. break;
  282. default:
  283. archive_set_error(&self->archive->archive,
  284. ARCHIVE_ERRNO_MISC,
  285. "Internal error initializing compression library: "
  286. " Zlib error %d", ret);
  287. break;
  288. }
  289. return (ARCHIVE_FATAL);
  290. }
  291. static int
  292. consume_trailer(struct archive_read_filter *self)
  293. {
  294. struct private_data *state;
  295. const unsigned char *p;
  296. ssize_t avail;
  297. state = (struct private_data *)self->data;
  298. state->in_stream = 0;
  299. switch (inflateEnd(&(state->stream))) {
  300. case Z_OK:
  301. break;
  302. default:
  303. archive_set_error(&self->archive->archive,
  304. ARCHIVE_ERRNO_MISC,
  305. "Failed to clean up gzip decompressor");
  306. return (ARCHIVE_FATAL);
  307. }
  308. /* GZip trailer is a fixed 8 byte structure. */
  309. p = __archive_read_filter_ahead(self->upstream, 8, &avail);
  310. if (p == NULL || avail == 0)
  311. return (ARCHIVE_FATAL);
  312. /* XXX TODO: Verify the length and CRC. */
  313. /* We've verified the trailer, so consume it now. */
  314. __archive_read_filter_consume(self->upstream, 8);
  315. return (ARCHIVE_OK);
  316. }
  317. static ssize_t
  318. gzip_filter_read(struct archive_read_filter *self, const void **p)
  319. {
  320. struct private_data *state;
  321. size_t decompressed;
  322. ssize_t avail_in;
  323. int ret;
  324. state = (struct private_data *)self->data;
  325. /* Empty our output buffer. */
  326. state->stream.next_out = state->out_block;
  327. state->stream.avail_out = state->out_block_size;
  328. /* Try to fill the output buffer. */
  329. while (state->stream.avail_out > 0 && !state->eof) {
  330. /* If we're not in a stream, read a header
  331. * and initialize the decompression library. */
  332. if (!state->in_stream) {
  333. ret = consume_header(self);
  334. if (ret == ARCHIVE_EOF) {
  335. state->eof = 1;
  336. break;
  337. }
  338. if (ret < ARCHIVE_OK)
  339. return (ret);
  340. }
  341. /* Peek at the next available data. */
  342. /* ZLib treats stream.next_in as const but doesn't declare
  343. * it so, hence this ugly cast. */
  344. state->stream.next_in = (unsigned char *)(uintptr_t)
  345. __archive_read_filter_ahead(self->upstream, 1, &avail_in);
  346. if (state->stream.next_in == NULL)
  347. return (ARCHIVE_FATAL);
  348. state->stream.avail_in = avail_in;
  349. /* Decompress and consume some of that data. */
  350. ret = inflate(&(state->stream), 0);
  351. switch (ret) {
  352. case Z_OK: /* Decompressor made some progress. */
  353. __archive_read_filter_consume(self->upstream,
  354. avail_in - state->stream.avail_in);
  355. break;
  356. case Z_STREAM_END: /* Found end of stream. */
  357. __archive_read_filter_consume(self->upstream,
  358. avail_in - state->stream.avail_in);
  359. /* Consume the stream trailer; release the
  360. * decompression library. */
  361. ret = consume_trailer(self);
  362. break;
  363. default:
  364. /* Return an error. */
  365. archive_set_error(&self->archive->archive,
  366. ARCHIVE_ERRNO_MISC,
  367. "gzip decompression failed");
  368. return (ARCHIVE_FATAL);
  369. }
  370. }
  371. /* We've read as much as we can. */
  372. decompressed = state->stream.next_out - state->out_block;
  373. state->total_out += decompressed;
  374. if (decompressed == 0)
  375. *p = NULL;
  376. else
  377. *p = state->out_block;
  378. return (decompressed);
  379. }
  380. /*
  381. * Clean up the decompressor.
  382. */
  383. static int
  384. gzip_filter_close(struct archive_read_filter *self)
  385. {
  386. struct private_data *state;
  387. int ret;
  388. state = (struct private_data *)self->data;
  389. ret = ARCHIVE_OK;
  390. if (state->in_stream) {
  391. switch (inflateEnd(&(state->stream))) {
  392. case Z_OK:
  393. break;
  394. default:
  395. archive_set_error(&(self->archive->archive),
  396. ARCHIVE_ERRNO_MISC,
  397. "Failed to clean up gzip compressor");
  398. ret = ARCHIVE_FATAL;
  399. }
  400. }
  401. free(state->out_block);
  402. free(state);
  403. return (ret);
  404. }
  405. #endif /* HAVE_ZLIB_H */