archive_read_support_compression_bzip2.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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_bzip2.c,v 1.19 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. #ifdef HAVE_STRING_H
  35. #include <string.h>
  36. #endif
  37. #ifdef HAVE_UNISTD_H
  38. #include <unistd.h>
  39. #endif
  40. #ifdef HAVE_BZLIB_H
  41. #include <bzlib.h>
  42. #endif
  43. #include "archive.h"
  44. #include "archive_private.h"
  45. #include "archive_read_private.h"
  46. #if HAVE_BZLIB_H
  47. struct private_data {
  48. bz_stream stream;
  49. char *out_block;
  50. size_t out_block_size;
  51. char valid; /* True = decompressor is initialized */
  52. char eof; /* True = found end of compressed data. */
  53. };
  54. /* Bzip2 filter */
  55. static ssize_t bzip2_filter_read(struct archive_read_filter *, const void **);
  56. static int bzip2_filter_close(struct archive_read_filter *);
  57. #endif
  58. /*
  59. * Note that we can detect bzip2 archives even if we can't decompress
  60. * them. (In fact, we like detecting them because we can give better
  61. * error messages.) So the bid framework here gets compiled even
  62. * if bzlib is unavailable.
  63. */
  64. static int bzip2_reader_bid(struct archive_read_filter_bidder *, struct archive_read_filter *);
  65. static int bzip2_reader_init(struct archive_read_filter *);
  66. static int bzip2_reader_free(struct archive_read_filter_bidder *);
  67. int
  68. archive_read_support_compression_bzip2(struct archive *_a)
  69. {
  70. struct archive_read *a = (struct archive_read *)_a;
  71. struct archive_read_filter_bidder *reader = __archive_read_get_bidder(a);
  72. if (reader == NULL)
  73. return (ARCHIVE_FATAL);
  74. reader->data = NULL;
  75. reader->bid = bzip2_reader_bid;
  76. reader->init = bzip2_reader_init;
  77. reader->options = NULL;
  78. reader->free = bzip2_reader_free;
  79. #if HAVE_BZLIB_H
  80. return (ARCHIVE_OK);
  81. #else
  82. archive_set_error(_a, ARCHIVE_ERRNO_MISC,
  83. "Using external bunzip2 program");
  84. return (ARCHIVE_WARN);
  85. #endif
  86. }
  87. static int
  88. bzip2_reader_free(struct archive_read_filter_bidder *self){
  89. (void)self; /* UNUSED */
  90. return (ARCHIVE_OK);
  91. }
  92. /*
  93. * Test whether we can handle this data.
  94. *
  95. * This logic returns zero if any part of the signature fails. It
  96. * also tries to Do The Right Thing if a very short buffer prevents us
  97. * from verifying as much as we would like.
  98. */
  99. static int
  100. bzip2_reader_bid(struct archive_read_filter_bidder *self, struct archive_read_filter *filter)
  101. {
  102. const unsigned char *buffer;
  103. ssize_t avail;
  104. int bits_checked;
  105. (void)self; /* UNUSED */
  106. /* Minimal bzip2 archive is 14 bytes. */
  107. buffer = __archive_read_filter_ahead(filter, 14, &avail);
  108. if (buffer == NULL)
  109. return (0);
  110. /* First three bytes must be "BZh" */
  111. bits_checked = 0;
  112. if (buffer[0] != 'B' || buffer[1] != 'Z' || buffer[2] != 'h')
  113. return (0);
  114. bits_checked += 24;
  115. /* Next follows a compression flag which must be an ASCII digit. */
  116. if (buffer[3] < '1' || buffer[3] > '9')
  117. return (0);
  118. bits_checked += 5;
  119. /* After BZh[1-9], there must be either a data block
  120. * which begins with 0x314159265359 or an end-of-data
  121. * marker of 0x177245385090. */
  122. if (memcmp(buffer + 4, "\x31\x41\x59\x26\x53\x59", 6) == 0)
  123. bits_checked += 48;
  124. else if (memcmp(buffer + 4, "\x17\x72\x45\x38\x50\x90", 6) == 0)
  125. bits_checked += 48;
  126. else
  127. return (0);
  128. return (bits_checked);
  129. }
  130. #ifndef HAVE_BZLIB_H
  131. /*
  132. * If we don't have the library on this system, we can't actually do the
  133. * decompression. We can, however, still detect compressed archives
  134. * and emit a useful message.
  135. */
  136. static int
  137. bzip2_reader_init(struct archive_read_filter *self)
  138. {
  139. int r;
  140. r = __archive_read_program(self, "bunzip2");
  141. /* Note: We set the format here even if __archive_read_program()
  142. * above fails. We do, after all, know what the format is
  143. * even if we weren't able to read it. */
  144. self->code = ARCHIVE_COMPRESSION_BZIP2;
  145. self->name = "bzip2";
  146. return (r);
  147. }
  148. #else
  149. /*
  150. * Setup the callbacks.
  151. */
  152. static int
  153. bzip2_reader_init(struct archive_read_filter *self)
  154. {
  155. static const size_t out_block_size = 64 * 1024;
  156. void *out_block;
  157. struct private_data *state;
  158. self->code = ARCHIVE_COMPRESSION_BZIP2;
  159. self->name = "bzip2";
  160. state = (struct private_data *)calloc(1, sizeof(*state));
  161. out_block = (unsigned char *)malloc(out_block_size);
  162. if (state == NULL || out_block == NULL) {
  163. archive_set_error(&self->archive->archive, ENOMEM,
  164. "Can't allocate data for bzip2 decompression");
  165. free(out_block);
  166. free(state);
  167. return (ARCHIVE_FATAL);
  168. }
  169. self->data = state;
  170. state->out_block_size = out_block_size;
  171. state->out_block = out_block;
  172. self->read = bzip2_filter_read;
  173. self->skip = NULL; /* not supported */
  174. self->close = bzip2_filter_close;
  175. return (ARCHIVE_OK);
  176. }
  177. /*
  178. * Return the next block of decompressed data.
  179. */
  180. static ssize_t
  181. bzip2_filter_read(struct archive_read_filter *self, const void **p)
  182. {
  183. struct private_data *state;
  184. size_t decompressed;
  185. const char *read_buf;
  186. ssize_t ret;
  187. state = (struct private_data *)self->data;
  188. if (state->eof) {
  189. *p = NULL;
  190. return (0);
  191. }
  192. /* Empty our output buffer. */
  193. state->stream.next_out = state->out_block;
  194. state->stream.avail_out = state->out_block_size;
  195. /* Try to fill the output buffer. */
  196. for (;;) {
  197. if (!state->valid) {
  198. if (bzip2_reader_bid(self->bidder, self->upstream) == 0) {
  199. state->eof = 1;
  200. *p = state->out_block;
  201. decompressed = state->stream.next_out
  202. - state->out_block;
  203. return (decompressed);
  204. }
  205. /* Initialize compression library. */
  206. ret = BZ2_bzDecompressInit(&(state->stream),
  207. 0 /* library verbosity */,
  208. 0 /* don't use low-mem algorithm */);
  209. /* If init fails, try low-memory algorithm instead. */
  210. if (ret == BZ_MEM_ERROR)
  211. ret = BZ2_bzDecompressInit(&(state->stream),
  212. 0 /* library verbosity */,
  213. 1 /* do use low-mem algo */);
  214. if (ret != BZ_OK) {
  215. const char *detail = NULL;
  216. int err = ARCHIVE_ERRNO_MISC;
  217. switch (ret) {
  218. case BZ_PARAM_ERROR:
  219. detail = "invalid setup parameter";
  220. break;
  221. case BZ_MEM_ERROR:
  222. err = ENOMEM;
  223. detail = "out of memory";
  224. break;
  225. case BZ_CONFIG_ERROR:
  226. detail = "mis-compiled library";
  227. break;
  228. }
  229. archive_set_error(&self->archive->archive, err,
  230. "Internal error initializing decompressor%s%s",
  231. detail == NULL ? "" : ": ",
  232. detail);
  233. return (ARCHIVE_FATAL);
  234. }
  235. state->valid = 1;
  236. }
  237. /* stream.next_in is really const, but bzlib
  238. * doesn't declare it so. <sigh> */
  239. read_buf =
  240. __archive_read_filter_ahead(self->upstream, 1, &ret);
  241. if (read_buf == NULL)
  242. return (ARCHIVE_FATAL);
  243. state->stream.next_in = (char *)(uintptr_t)read_buf;
  244. state->stream.avail_in = ret;
  245. /* There is no more data, return whatever we have. */
  246. if (ret == 0) {
  247. state->eof = 1;
  248. *p = state->out_block;
  249. decompressed = state->stream.next_out
  250. - state->out_block;
  251. return (decompressed);
  252. }
  253. /* Decompress as much as we can in one pass. */
  254. ret = BZ2_bzDecompress(&(state->stream));
  255. __archive_read_filter_consume(self->upstream,
  256. state->stream.next_in - read_buf);
  257. switch (ret) {
  258. case BZ_STREAM_END: /* Found end of stream. */
  259. switch (BZ2_bzDecompressEnd(&(state->stream))) {
  260. case BZ_OK:
  261. break;
  262. default:
  263. archive_set_error(&(self->archive->archive),
  264. ARCHIVE_ERRNO_MISC,
  265. "Failed to clean up decompressor");
  266. return (ARCHIVE_FATAL);
  267. }
  268. state->valid = 0;
  269. /* FALLTHROUGH */
  270. case BZ_OK: /* Decompressor made some progress. */
  271. /* If we filled our buffer, update stats and return. */
  272. if (state->stream.avail_out == 0) {
  273. *p = state->out_block;
  274. decompressed = state->stream.next_out
  275. - state->out_block;
  276. return (decompressed);
  277. }
  278. break;
  279. default: /* Return an error. */
  280. archive_set_error(&self->archive->archive,
  281. ARCHIVE_ERRNO_MISC, "bzip decompression failed");
  282. return (ARCHIVE_FATAL);
  283. }
  284. }
  285. }
  286. /*
  287. * Clean up the decompressor.
  288. */
  289. static int
  290. bzip2_filter_close(struct archive_read_filter *self)
  291. {
  292. struct private_data *state;
  293. int ret = ARCHIVE_OK;
  294. state = (struct private_data *)self->data;
  295. if (state->valid) {
  296. switch (BZ2_bzDecompressEnd(&state->stream)) {
  297. case BZ_OK:
  298. break;
  299. default:
  300. archive_set_error(&self->archive->archive,
  301. ARCHIVE_ERRNO_MISC,
  302. "Failed to clean up decompressor");
  303. ret = ARCHIVE_FATAL;
  304. }
  305. state->valid = 0;
  306. }
  307. free(state->out_block);
  308. free(state);
  309. return (ret);
  310. }
  311. #endif /* HAVE_BZLIB_H */