archive_read.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241
  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. /*
  26. * This file contains the "essential" portions of the read API, that
  27. * is, stuff that will probably always be used by any client that
  28. * actually needs to read an archive. Optional pieces have been, as
  29. * far as possible, separated out into separate files to avoid
  30. * needlessly bloating statically-linked clients.
  31. */
  32. #include "archive_platform.h"
  33. __FBSDID("$FreeBSD: src/lib/libarchive/archive_read.c,v 1.39 2008/12/06 06:45:15 kientzle Exp $");
  34. #ifdef HAVE_ERRNO_H
  35. #include <errno.h>
  36. #endif
  37. #include <stdio.h>
  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_UNISTD_H
  45. #include <unistd.h>
  46. #endif
  47. #include "archive.h"
  48. #include "archive_entry.h"
  49. #include "archive_private.h"
  50. #include "archive_read_private.h"
  51. #define minimum(a, b) (a < b ? a : b)
  52. static int build_stream(struct archive_read *);
  53. static int choose_format(struct archive_read *);
  54. static struct archive_vtable *archive_read_vtable(void);
  55. static int _archive_read_close(struct archive *);
  56. static int _archive_read_finish(struct archive *);
  57. static struct archive_vtable *
  58. archive_read_vtable(void)
  59. {
  60. static struct archive_vtable av;
  61. static int inited = 0;
  62. if (!inited) {
  63. av.archive_finish = _archive_read_finish;
  64. av.archive_close = _archive_read_close;
  65. }
  66. return (&av);
  67. }
  68. /*
  69. * Allocate, initialize and return a struct archive object.
  70. */
  71. struct archive *
  72. archive_read_new(void)
  73. {
  74. struct archive_read *a;
  75. a = (struct archive_read *)malloc(sizeof(*a));
  76. if (a == NULL)
  77. return (NULL);
  78. memset(a, 0, sizeof(*a));
  79. a->archive.magic = ARCHIVE_READ_MAGIC;
  80. a->archive.state = ARCHIVE_STATE_NEW;
  81. a->entry = archive_entry_new();
  82. a->archive.vtable = archive_read_vtable();
  83. return (&a->archive);
  84. }
  85. /*
  86. * Record the do-not-extract-to file. This belongs in archive_read_extract.c.
  87. */
  88. void
  89. archive_read_extract_set_skip_file(struct archive *_a, dev_t d, ino_t i)
  90. {
  91. struct archive_read *a = (struct archive_read *)_a;
  92. __archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_ANY,
  93. "archive_read_extract_set_skip_file");
  94. a->skip_file_dev = d;
  95. a->skip_file_ino = i;
  96. }
  97. /*
  98. * Set read options for the format.
  99. */
  100. int
  101. archive_read_set_format_options(struct archive *_a, const char *s)
  102. {
  103. struct archive_read *a;
  104. struct archive_format_descriptor *format;
  105. char key[64], val[64];
  106. char *valp;
  107. size_t i;
  108. int len, r;
  109. __archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
  110. "archive_read_set_format_options");
  111. if (s == NULL || *s == '\0')
  112. return (ARCHIVE_OK);
  113. a = (struct archive_read *)_a;
  114. __archive_check_magic(&a->archive, ARCHIVE_READ_MAGIC,
  115. ARCHIVE_STATE_NEW, "archive_read_set_format_options");
  116. len = 0;
  117. for (i = 0; i < sizeof(a->formats)/sizeof(a->formats[0]); i++) {
  118. format = &a->formats[i];
  119. if (format == NULL || format->options == NULL ||
  120. format->name == NULL)
  121. /* This format does not support option. */
  122. continue;
  123. while ((len = __archive_parse_options(s, format->name,
  124. sizeof(key), key, sizeof(val), val)) > 0) {
  125. valp = val[0] == '\0' ? NULL : val;
  126. a->format = format;
  127. r = format->options(a, key, valp);
  128. a->format = NULL;
  129. if (r == ARCHIVE_FATAL)
  130. return (r);
  131. s += len;
  132. }
  133. }
  134. if (len < 0) {
  135. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  136. "Illegal format options.");
  137. return (ARCHIVE_WARN);
  138. }
  139. return (ARCHIVE_OK);
  140. }
  141. /*
  142. * Set read options for the filter.
  143. */
  144. int
  145. archive_read_set_filter_options(struct archive *_a, const char *s)
  146. {
  147. struct archive_read *a;
  148. struct archive_read_filter *filter;
  149. struct archive_read_filter_bidder *bidder;
  150. char key[64], val[64];
  151. int len, r;
  152. __archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
  153. "archive_read_set_filter_options");
  154. if (s == NULL || *s == '\0')
  155. return (ARCHIVE_OK);
  156. a = (struct archive_read *)_a;
  157. __archive_check_magic(&a->archive, ARCHIVE_READ_MAGIC,
  158. ARCHIVE_STATE_NEW, "archive_read_set_filter_options");
  159. filter = a->filter;
  160. len = 0;
  161. for (filter = a->filter; filter != NULL; filter = filter->upstream) {
  162. bidder = filter->bidder;
  163. if (bidder == NULL)
  164. continue;
  165. if (bidder->options == NULL)
  166. /* This bidder does not support option */
  167. continue;
  168. while ((len = __archive_parse_options(s, filter->name,
  169. sizeof(key), key, sizeof(val), val)) > 0) {
  170. if (val[0] == '\0')
  171. r = bidder->options(bidder, key, NULL);
  172. else
  173. r = bidder->options(bidder, key, val);
  174. if (r == ARCHIVE_FATAL)
  175. return (r);
  176. s += len;
  177. }
  178. }
  179. if (len < 0) {
  180. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  181. "Illegal format options.");
  182. return (ARCHIVE_WARN);
  183. }
  184. return (ARCHIVE_OK);
  185. }
  186. /*
  187. * Set read options for the format and the filter.
  188. */
  189. int
  190. archive_read_set_options(struct archive *_a, const char *s)
  191. {
  192. int r;
  193. __archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
  194. "archive_read_set_options");
  195. archive_clear_error(_a);
  196. r = archive_read_set_format_options(_a, s);
  197. if (r != ARCHIVE_OK)
  198. return (r);
  199. r = archive_read_set_filter_options(_a, s);
  200. if (r != ARCHIVE_OK)
  201. return (r);
  202. return (ARCHIVE_OK);
  203. }
  204. /*
  205. * Open the archive
  206. */
  207. int
  208. archive_read_open(struct archive *a, void *client_data,
  209. archive_open_callback *client_opener, archive_read_callback *client_reader,
  210. archive_close_callback *client_closer)
  211. {
  212. /* Old archive_read_open() is just a thin shell around
  213. * archive_read_open2. */
  214. return archive_read_open2(a, client_data, client_opener,
  215. client_reader, NULL, client_closer);
  216. }
  217. static ssize_t
  218. client_read_proxy(struct archive_read_filter *self, const void **buff)
  219. {
  220. ssize_t r;
  221. r = (self->archive->client.reader)(&self->archive->archive,
  222. self->data, buff);
  223. self->archive->archive.raw_position += r;
  224. return (r);
  225. }
  226. static int64_t
  227. client_skip_proxy(struct archive_read_filter *self, int64_t request)
  228. {
  229. int64_t ask, get, total;
  230. /* Limit our maximum seek request to 1GB on platforms
  231. * with 32-bit off_t (such as Windows). */
  232. int64_t skip_limit = ((int64_t)1) << (sizeof(off_t) * 8 - 2);
  233. if (self->archive->client.skipper == NULL)
  234. return (0);
  235. total = 0;
  236. for (;;) {
  237. ask = request;
  238. if (ask > skip_limit)
  239. ask = skip_limit;
  240. get = (self->archive->client.skipper)(&self->archive->archive,
  241. self->data, ask);
  242. if (get == 0)
  243. return (total);
  244. request -= get;
  245. self->archive->archive.raw_position += get;
  246. total += get;
  247. }
  248. }
  249. static int
  250. client_close_proxy(struct archive_read_filter *self)
  251. {
  252. int r = ARCHIVE_OK;
  253. if (self->archive->client.closer != NULL)
  254. r = (self->archive->client.closer)((struct archive *)self->archive,
  255. self->data);
  256. self->data = NULL;
  257. return (r);
  258. }
  259. int
  260. archive_read_open2(struct archive *_a, void *client_data,
  261. archive_open_callback *client_opener,
  262. archive_read_callback *client_reader,
  263. archive_skip_callback *client_skipper,
  264. archive_close_callback *client_closer)
  265. {
  266. struct archive_read *a = (struct archive_read *)_a;
  267. struct archive_read_filter *filter;
  268. int e;
  269. __archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
  270. "archive_read_open");
  271. archive_clear_error(&a->archive);
  272. if (client_reader == NULL)
  273. __archive_errx(1,
  274. "No reader function provided to archive_read_open");
  275. /* Open data source. */
  276. if (client_opener != NULL) {
  277. e =(client_opener)(&a->archive, client_data);
  278. if (e != 0) {
  279. /* If the open failed, call the closer to clean up. */
  280. if (client_closer)
  281. (client_closer)(&a->archive, client_data);
  282. return (e);
  283. }
  284. }
  285. /* Save the client functions and mock up the initial source. */
  286. a->client.reader = client_reader;
  287. a->client.skipper = client_skipper;
  288. a->client.closer = client_closer;
  289. filter = calloc(1, sizeof(*filter));
  290. if (filter == NULL)
  291. return (ARCHIVE_FATAL);
  292. filter->bidder = NULL;
  293. filter->upstream = NULL;
  294. filter->archive = a;
  295. filter->data = client_data;
  296. filter->read = client_read_proxy;
  297. filter->skip = client_skip_proxy;
  298. filter->close = client_close_proxy;
  299. filter->name = "none";
  300. filter->code = ARCHIVE_COMPRESSION_NONE;
  301. a->filter = filter;
  302. /* Build out the input pipeline. */
  303. e = build_stream(a);
  304. if (e == ARCHIVE_OK)
  305. a->archive.state = ARCHIVE_STATE_HEADER;
  306. return (e);
  307. }
  308. /*
  309. * Allow each registered stream transform to bid on whether
  310. * it wants to handle this stream. Repeat until we've finished
  311. * building the pipeline.
  312. */
  313. static int
  314. build_stream(struct archive_read *a)
  315. {
  316. int number_bidders, i, bid, best_bid;
  317. struct archive_read_filter_bidder *bidder, *best_bidder;
  318. struct archive_read_filter *filter;
  319. ssize_t avail;
  320. int r;
  321. for (;;) {
  322. number_bidders = sizeof(a->bidders) / sizeof(a->bidders[0]);
  323. best_bid = 0;
  324. best_bidder = NULL;
  325. bidder = a->bidders;
  326. for (i = 0; i < number_bidders; i++, bidder++) {
  327. if (bidder->bid != NULL) {
  328. bid = (bidder->bid)(bidder, a->filter);
  329. if (bid > best_bid) {
  330. best_bid = bid;
  331. best_bidder = bidder;
  332. }
  333. }
  334. }
  335. /* If no bidder, we're done. */
  336. if (best_bidder == NULL) {
  337. a->archive.compression_name = a->filter->name;
  338. a->archive.compression_code = a->filter->code;
  339. return (ARCHIVE_OK);
  340. }
  341. filter
  342. = (struct archive_read_filter *)calloc(1, sizeof(*filter));
  343. if (filter == NULL)
  344. return (ARCHIVE_FATAL);
  345. filter->bidder = best_bidder;
  346. filter->archive = a;
  347. filter->upstream = a->filter;
  348. r = (best_bidder->init)(filter);
  349. if (r != ARCHIVE_OK) {
  350. free(filter);
  351. return (r);
  352. }
  353. /* Verify the filter by asking it for some data. */
  354. __archive_read_filter_ahead(filter, 1, &avail);
  355. if (avail < 0) {
  356. /* If the read failed, bail out now. */
  357. free(filter);
  358. return (avail);
  359. }
  360. a->filter = filter;
  361. }
  362. }
  363. /*
  364. * Read header of next entry.
  365. */
  366. int
  367. archive_read_next_header2(struct archive *_a, struct archive_entry *entry)
  368. {
  369. struct archive_read *a = (struct archive_read *)_a;
  370. int slot, ret;
  371. __archive_check_magic(_a, ARCHIVE_READ_MAGIC,
  372. ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
  373. "archive_read_next_header");
  374. ++_a->file_count;
  375. archive_entry_clear(entry);
  376. archive_clear_error(&a->archive);
  377. /*
  378. * If no format has yet been chosen, choose one.
  379. */
  380. if (a->format == NULL) {
  381. slot = choose_format(a);
  382. if (slot < 0) {
  383. a->archive.state = ARCHIVE_STATE_FATAL;
  384. return (ARCHIVE_FATAL);
  385. }
  386. a->format = &(a->formats[slot]);
  387. }
  388. /*
  389. * If client didn't consume entire data, skip any remainder
  390. * (This is especially important for GNU incremental directories.)
  391. */
  392. if (a->archive.state == ARCHIVE_STATE_DATA) {
  393. ret = archive_read_data_skip(&a->archive);
  394. if (ret == ARCHIVE_EOF) {
  395. archive_set_error(&a->archive, EIO, "Premature end-of-file.");
  396. a->archive.state = ARCHIVE_STATE_FATAL;
  397. return (ARCHIVE_FATAL);
  398. }
  399. if (ret != ARCHIVE_OK)
  400. return (ret);
  401. }
  402. /* Record start-of-header. */
  403. a->header_position = a->archive.file_position;
  404. ret = (a->format->read_header)(a, entry);
  405. /*
  406. * EOF and FATAL are persistent at this layer. By
  407. * modifying the state, we guarantee that future calls to
  408. * read a header or read data will fail.
  409. */
  410. switch (ret) {
  411. case ARCHIVE_EOF:
  412. a->archive.state = ARCHIVE_STATE_EOF;
  413. break;
  414. case ARCHIVE_OK:
  415. a->archive.state = ARCHIVE_STATE_DATA;
  416. break;
  417. case ARCHIVE_WARN:
  418. a->archive.state = ARCHIVE_STATE_DATA;
  419. break;
  420. case ARCHIVE_RETRY:
  421. break;
  422. case ARCHIVE_FATAL:
  423. a->archive.state = ARCHIVE_STATE_FATAL;
  424. break;
  425. }
  426. a->read_data_output_offset = 0;
  427. a->read_data_remaining = 0;
  428. return (ret);
  429. }
  430. int
  431. archive_read_next_header(struct archive *_a, struct archive_entry **entryp)
  432. {
  433. int ret;
  434. struct archive_read *a = (struct archive_read *)_a;
  435. *entryp = NULL;
  436. ret = archive_read_next_header2(_a, a->entry);
  437. *entryp = a->entry;
  438. return ret;
  439. }
  440. /*
  441. * Allow each registered format to bid on whether it wants to handle
  442. * the next entry. Return index of winning bidder.
  443. */
  444. static int
  445. choose_format(struct archive_read *a)
  446. {
  447. int slots;
  448. int i;
  449. int bid, best_bid;
  450. int best_bid_slot;
  451. slots = sizeof(a->formats) / sizeof(a->formats[0]);
  452. best_bid = -1;
  453. best_bid_slot = -1;
  454. /* Set up a->format and a->pformat_data for convenience of bidders. */
  455. a->format = &(a->formats[0]);
  456. for (i = 0; i < slots; i++, a->format++) {
  457. if (a->format->bid) {
  458. bid = (a->format->bid)(a);
  459. if (bid == ARCHIVE_FATAL)
  460. return (ARCHIVE_FATAL);
  461. if ((bid > best_bid) || (best_bid_slot < 0)) {
  462. best_bid = bid;
  463. best_bid_slot = i;
  464. }
  465. }
  466. }
  467. /*
  468. * There were no bidders; this is a serious programmer error
  469. * and demands a quick and definitive abort.
  470. */
  471. if (best_bid_slot < 0)
  472. __archive_errx(1, "No formats were registered; you must "
  473. "invoke at least one archive_read_support_format_XXX "
  474. "function in order to successfully read an archive.");
  475. /*
  476. * There were bidders, but no non-zero bids; this means we
  477. * can't support this stream.
  478. */
  479. if (best_bid < 1) {
  480. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  481. "Unrecognized archive format");
  482. return (ARCHIVE_FATAL);
  483. }
  484. return (best_bid_slot);
  485. }
  486. /*
  487. * Return the file offset (within the uncompressed data stream) where
  488. * the last header started.
  489. */
  490. int64_t
  491. archive_read_header_position(struct archive *_a)
  492. {
  493. struct archive_read *a = (struct archive_read *)_a;
  494. __archive_check_magic(_a, ARCHIVE_READ_MAGIC,
  495. ARCHIVE_STATE_ANY, "archive_read_header_position");
  496. return (a->header_position);
  497. }
  498. /*
  499. * Read data from an archive entry, using a read(2)-style interface.
  500. * This is a convenience routine that just calls
  501. * archive_read_data_block and copies the results into the client
  502. * buffer, filling any gaps with zero bytes. Clients using this
  503. * API can be completely ignorant of sparse-file issues; sparse files
  504. * will simply be padded with nulls.
  505. *
  506. * DO NOT intermingle calls to this function and archive_read_data_block
  507. * to read a single entry body.
  508. */
  509. ssize_t
  510. archive_read_data(struct archive *_a, void *buff, size_t s)
  511. {
  512. struct archive_read *a = (struct archive_read *)_a;
  513. char *dest;
  514. const void *read_buf;
  515. size_t bytes_read;
  516. size_t len;
  517. int r;
  518. bytes_read = 0;
  519. dest = (char *)buff;
  520. while (s > 0) {
  521. if (a->read_data_remaining == 0) {
  522. read_buf = a->read_data_block;
  523. r = archive_read_data_block(&a->archive, &read_buf,
  524. &a->read_data_remaining, &a->read_data_offset);
  525. a->read_data_block = read_buf;
  526. if (r == ARCHIVE_EOF)
  527. return (bytes_read);
  528. /*
  529. * Error codes are all negative, so the status
  530. * return here cannot be confused with a valid
  531. * byte count. (ARCHIVE_OK is zero.)
  532. */
  533. if (r < ARCHIVE_OK)
  534. return (r);
  535. }
  536. if (a->read_data_offset < a->read_data_output_offset) {
  537. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  538. "Encountered out-of-order sparse blocks");
  539. return (ARCHIVE_RETRY);
  540. }
  541. /* Compute the amount of zero padding needed. */
  542. if (a->read_data_output_offset + (off_t)s <
  543. a->read_data_offset) {
  544. len = s;
  545. } else if (a->read_data_output_offset <
  546. a->read_data_offset) {
  547. len = a->read_data_offset -
  548. a->read_data_output_offset;
  549. } else
  550. len = 0;
  551. /* Add zeroes. */
  552. memset(dest, 0, len);
  553. s -= len;
  554. a->read_data_output_offset += len;
  555. dest += len;
  556. bytes_read += len;
  557. /* Copy data if there is any space left. */
  558. if (s > 0) {
  559. len = a->read_data_remaining;
  560. if (len > s)
  561. len = s;
  562. memcpy(dest, a->read_data_block, len);
  563. s -= len;
  564. a->read_data_block += len;
  565. a->read_data_remaining -= len;
  566. a->read_data_output_offset += len;
  567. a->read_data_offset += len;
  568. dest += len;
  569. bytes_read += len;
  570. }
  571. }
  572. return (bytes_read);
  573. }
  574. #if ARCHIVE_API_VERSION < 3
  575. /*
  576. * Obsolete function provided for compatibility only. Note that the API
  577. * of this function doesn't allow the caller to detect if the remaining
  578. * data from the archive entry is shorter than the buffer provided, or
  579. * even if an error occurred while reading data.
  580. */
  581. int
  582. archive_read_data_into_buffer(struct archive *a, void *d, ssize_t len)
  583. {
  584. archive_read_data(a, d, len);
  585. return (ARCHIVE_OK);
  586. }
  587. #endif
  588. /*
  589. * Skip over all remaining data in this entry.
  590. */
  591. int
  592. archive_read_data_skip(struct archive *_a)
  593. {
  594. struct archive_read *a = (struct archive_read *)_a;
  595. int r;
  596. const void *buff;
  597. size_t size;
  598. off_t offset;
  599. __archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA,
  600. "archive_read_data_skip");
  601. if (a->format->read_data_skip != NULL)
  602. r = (a->format->read_data_skip)(a);
  603. else {
  604. while ((r = archive_read_data_block(&a->archive,
  605. &buff, &size, &offset))
  606. == ARCHIVE_OK)
  607. ;
  608. }
  609. if (r == ARCHIVE_EOF)
  610. r = ARCHIVE_OK;
  611. a->archive.state = ARCHIVE_STATE_HEADER;
  612. return (r);
  613. }
  614. /*
  615. * Read the next block of entry data from the archive.
  616. * This is a zero-copy interface; the client receives a pointer,
  617. * size, and file offset of the next available block of data.
  618. *
  619. * Returns ARCHIVE_OK if the operation is successful, ARCHIVE_EOF if
  620. * the end of entry is encountered.
  621. */
  622. int
  623. archive_read_data_block(struct archive *_a,
  624. const void **buff, size_t *size, off_t *offset)
  625. {
  626. struct archive_read *a = (struct archive_read *)_a;
  627. __archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA,
  628. "archive_read_data_block");
  629. if (a->format->read_data == NULL) {
  630. archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
  631. "Internal error: "
  632. "No format_read_data_block function registered");
  633. return (ARCHIVE_FATAL);
  634. }
  635. return (a->format->read_data)(a, buff, size, offset);
  636. }
  637. /*
  638. * Close the file and release most resources.
  639. *
  640. * Be careful: client might just call read_new and then read_finish.
  641. * Don't assume we actually read anything or performed any non-trivial
  642. * initialization.
  643. */
  644. static int
  645. _archive_read_close(struct archive *_a)
  646. {
  647. struct archive_read *a = (struct archive_read *)_a;
  648. int r = ARCHIVE_OK, r1 = ARCHIVE_OK;
  649. size_t i, n;
  650. __archive_check_magic(&a->archive, ARCHIVE_READ_MAGIC,
  651. ARCHIVE_STATE_ANY, "archive_read_close");
  652. archive_clear_error(&a->archive);
  653. a->archive.state = ARCHIVE_STATE_CLOSED;
  654. /* Call cleanup functions registered by optional components. */
  655. if (a->cleanup_archive_extract != NULL)
  656. r = (a->cleanup_archive_extract)(a);
  657. /* TODO: Clean up the formatters. */
  658. /* Clean up the filter pipeline. */
  659. while (a->filter != NULL) {
  660. struct archive_read_filter *t = a->filter->upstream;
  661. if (a->filter->close != NULL) {
  662. r1 = (a->filter->close)(a->filter);
  663. if (r1 < r)
  664. r = r1;
  665. }
  666. free(a->filter->buffer);
  667. free(a->filter);
  668. a->filter = t;
  669. }
  670. /* Release the bidder objects. */
  671. n = sizeof(a->bidders)/sizeof(a->bidders[0]);
  672. for (i = 0; i < n; i++) {
  673. if (a->bidders[i].free != NULL) {
  674. r1 = (a->bidders[i].free)(&a->bidders[i]);
  675. if (r1 < r)
  676. r = r1;
  677. }
  678. }
  679. return (r);
  680. }
  681. /*
  682. * Release memory and other resources.
  683. */
  684. static int
  685. _archive_read_finish(struct archive *_a)
  686. {
  687. struct archive_read *a = (struct archive_read *)_a;
  688. int i;
  689. int slots;
  690. int r = ARCHIVE_OK;
  691. __archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_ANY,
  692. "archive_read_finish");
  693. if (a->archive.state != ARCHIVE_STATE_CLOSED)
  694. r = archive_read_close(&a->archive);
  695. /* Cleanup format-specific data. */
  696. slots = sizeof(a->formats) / sizeof(a->formats[0]);
  697. for (i = 0; i < slots; i++) {
  698. a->format = &(a->formats[i]);
  699. if (a->formats[i].cleanup)
  700. (a->formats[i].cleanup)(a);
  701. }
  702. archive_string_free(&a->archive.error_string);
  703. if (a->entry)
  704. archive_entry_free(a->entry);
  705. a->archive.magic = 0;
  706. free(a);
  707. #if ARCHIVE_API_VERSION > 1
  708. return (r);
  709. #endif
  710. }
  711. /*
  712. * Used internally by read format handlers to register their bid and
  713. * initialization functions.
  714. */
  715. int
  716. __archive_read_register_format(struct archive_read *a,
  717. void *format_data,
  718. const char *name,
  719. int (*bid)(struct archive_read *),
  720. int (*options)(struct archive_read *, const char *, const char *),
  721. int (*read_header)(struct archive_read *, struct archive_entry *),
  722. int (*read_data)(struct archive_read *, const void **, size_t *, off_t *),
  723. int (*read_data_skip)(struct archive_read *),
  724. int (*cleanup)(struct archive_read *))
  725. {
  726. int i, number_slots;
  727. __archive_check_magic(&a->archive,
  728. ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
  729. "__archive_read_register_format");
  730. number_slots = sizeof(a->formats) / sizeof(a->formats[0]);
  731. for (i = 0; i < number_slots; i++) {
  732. if (a->formats[i].bid == bid)
  733. return (ARCHIVE_WARN); /* We've already installed */
  734. if (a->formats[i].bid == NULL) {
  735. a->formats[i].bid = bid;
  736. a->formats[i].options = options;
  737. a->formats[i].read_header = read_header;
  738. a->formats[i].read_data = read_data;
  739. a->formats[i].read_data_skip = read_data_skip;
  740. a->formats[i].cleanup = cleanup;
  741. a->formats[i].data = format_data;
  742. a->formats[i].name = name;
  743. return (ARCHIVE_OK);
  744. }
  745. }
  746. __archive_errx(1, "Not enough slots for format registration");
  747. return (ARCHIVE_FATAL); /* Never actually called. */
  748. }
  749. /*
  750. * Used internally by decompression routines to register their bid and
  751. * initialization functions.
  752. */
  753. struct archive_read_filter_bidder *
  754. __archive_read_get_bidder(struct archive_read *a)
  755. {
  756. int i, number_slots;
  757. __archive_check_magic(&a->archive,
  758. ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
  759. "__archive_read_get_bidder");
  760. number_slots = sizeof(a->bidders) / sizeof(a->bidders[0]);
  761. for (i = 0; i < number_slots; i++) {
  762. if (a->bidders[i].bid == NULL) {
  763. memset(a->bidders + i, 0, sizeof(a->bidders[0]));
  764. return (a->bidders + i);
  765. }
  766. }
  767. __archive_errx(1, "Not enough slots for compression registration");
  768. return (NULL); /* Never actually executed. */
  769. }
  770. /*
  771. * The next three functions comprise the peek/consume internal I/O
  772. * system used by archive format readers. This system allows fairly
  773. * flexible read-ahead and allows the I/O code to operate in a
  774. * zero-copy manner most of the time.
  775. *
  776. * In the ideal case, filters generate blocks of data
  777. * and __archive_read_ahead() just returns pointers directly into
  778. * those blocks. Then __archive_read_consume() just bumps those
  779. * pointers. Only if your request would span blocks does the I/O
  780. * layer use a copy buffer to provide you with a contiguous block of
  781. * data. The __archive_read_skip() is an optimization; it scans ahead
  782. * very quickly (it usually translates into a seek() operation if
  783. * you're reading uncompressed disk files).
  784. *
  785. * A couple of useful idioms:
  786. * * "I just want some data." Ask for 1 byte and pay attention to
  787. * the "number of bytes available" from __archive_read_ahead().
  788. * You can consume more than you asked for; you just can't consume
  789. * more than is available. If you consume everything that's
  790. * immediately available, the next read_ahead() call will pull
  791. * the next block.
  792. * * "I want to output a large block of data." As above, ask for 1 byte,
  793. * emit all that's available (up to whatever limit you have), then
  794. * repeat until you're done.
  795. * * "I want to peek ahead by a large amount." Ask for 4k or so, then
  796. * double and repeat until you get an error or have enough. Note
  797. * that the I/O layer will likely end up expanding its copy buffer
  798. * to fit your request, so use this technique cautiously. This
  799. * technique is used, for example, by some of the format tasting
  800. * code that has uncertain look-ahead needs.
  801. *
  802. * TODO: Someday, provide a more generic __archive_read_seek() for
  803. * those cases where it's useful. This is tricky because there are lots
  804. * of cases where seek() is not available (reading gzip data from a
  805. * network socket, for instance), so there needs to be a good way to
  806. * communicate whether seek() is available and users of that interface
  807. * need to use non-seeking strategies whenever seek() is not available.
  808. */
  809. /*
  810. * Looks ahead in the input stream:
  811. * * If 'avail' pointer is provided, that returns number of bytes available
  812. * in the current buffer, which may be much larger than requested.
  813. * * If end-of-file, *avail gets set to zero.
  814. * * If error, *avail gets error code.
  815. * * If request can be met, returns pointer to data, returns NULL
  816. * if request is not met.
  817. *
  818. * Note: If you just want "some data", ask for 1 byte and pay attention
  819. * to *avail, which will have the actual amount available. If you
  820. * know exactly how many bytes you need, just ask for that and treat
  821. * a NULL return as an error.
  822. *
  823. * Important: This does NOT move the file pointer. See
  824. * __archive_read_consume() below.
  825. */
  826. /*
  827. * This is tricky. We need to provide our clients with pointers to
  828. * contiguous blocks of memory but we want to avoid copying whenever
  829. * possible.
  830. *
  831. * Mostly, this code returns pointers directly into the block of data
  832. * provided by the client_read routine. It can do this unless the
  833. * request would split across blocks. In that case, we have to copy
  834. * into an internal buffer to combine reads.
  835. */
  836. const void *
  837. __archive_read_ahead(struct archive_read *a, size_t min, ssize_t *avail)
  838. {
  839. return (__archive_read_filter_ahead(a->filter, min, avail));
  840. }
  841. const void *
  842. __archive_read_filter_ahead(struct archive_read_filter *filter,
  843. size_t min, ssize_t *avail)
  844. {
  845. ssize_t bytes_read;
  846. size_t tocopy;
  847. if (filter->fatal) {
  848. if (avail)
  849. *avail = ARCHIVE_FATAL;
  850. return (NULL);
  851. }
  852. /*
  853. * Keep pulling more data until we can satisfy the request.
  854. */
  855. for (;;) {
  856. /*
  857. * If we can satisfy from the copy buffer (and the
  858. * copy buffer isn't empty), we're done. In particular,
  859. * note that min == 0 is a perfectly well-defined
  860. * request.
  861. */
  862. if (filter->avail >= min && filter->avail > 0) {
  863. if (avail != NULL)
  864. *avail = filter->avail;
  865. return (filter->next);
  866. }
  867. /*
  868. * We can satisfy directly from client buffer if everything
  869. * currently in the copy buffer is still in the client buffer.
  870. */
  871. if (filter->client_total >= filter->client_avail + filter->avail
  872. && filter->client_avail + filter->avail >= min) {
  873. /* "Roll back" to client buffer. */
  874. filter->client_avail += filter->avail;
  875. filter->client_next -= filter->avail;
  876. /* Copy buffer is now empty. */
  877. filter->avail = 0;
  878. filter->next = filter->buffer;
  879. /* Return data from client buffer. */
  880. if (avail != NULL)
  881. *avail = filter->client_avail;
  882. return (filter->client_next);
  883. }
  884. /* Move data forward in copy buffer if necessary. */
  885. if (filter->next > filter->buffer &&
  886. filter->next + min > filter->buffer + filter->buffer_size) {
  887. if (filter->avail > 0)
  888. memmove(filter->buffer, filter->next, filter->avail);
  889. filter->next = filter->buffer;
  890. }
  891. /* If we've used up the client data, get more. */
  892. if (filter->client_avail <= 0) {
  893. if (filter->end_of_file) {
  894. if (avail != NULL)
  895. *avail = 0;
  896. return (NULL);
  897. }
  898. bytes_read = (filter->read)(filter,
  899. &filter->client_buff);
  900. if (bytes_read < 0) { /* Read error. */
  901. filter->client_total = filter->client_avail = 0;
  902. filter->client_next = filter->client_buff = NULL;
  903. filter->fatal = 1;
  904. if (avail != NULL)
  905. *avail = ARCHIVE_FATAL;
  906. return (NULL);
  907. }
  908. if (bytes_read == 0) { /* Premature end-of-file. */
  909. filter->client_total = filter->client_avail = 0;
  910. filter->client_next = filter->client_buff = NULL;
  911. filter->end_of_file = 1;
  912. /* Return whatever we do have. */
  913. if (avail != NULL)
  914. *avail = filter->avail;
  915. return (NULL);
  916. }
  917. filter->position += bytes_read;
  918. filter->client_total = bytes_read;
  919. filter->client_avail = filter->client_total;
  920. filter->client_next = filter->client_buff;
  921. }
  922. else
  923. {
  924. /*
  925. * We can't satisfy the request from the copy
  926. * buffer or the existing client data, so we
  927. * need to copy more client data over to the
  928. * copy buffer.
  929. */
  930. /* Ensure the buffer is big enough. */
  931. if (min > filter->buffer_size) {
  932. size_t s, t;
  933. char *p;
  934. /* Double the buffer; watch for overflow. */
  935. s = t = filter->buffer_size;
  936. if (s == 0)
  937. s = min;
  938. while (s < min) {
  939. t *= 2;
  940. if (t <= s) { /* Integer overflow! */
  941. archive_set_error(
  942. &filter->archive->archive,
  943. ENOMEM,
  944. "Unable to allocate copy buffer");
  945. filter->fatal = 1;
  946. if (avail != NULL)
  947. *avail = ARCHIVE_FATAL;
  948. return (NULL);
  949. }
  950. s = t;
  951. }
  952. /* Now s >= min, so allocate a new buffer. */
  953. p = (char *)malloc(s);
  954. if (p == NULL) {
  955. archive_set_error(
  956. &filter->archive->archive,
  957. ENOMEM,
  958. "Unable to allocate copy buffer");
  959. filter->fatal = 1;
  960. if (avail != NULL)
  961. *avail = ARCHIVE_FATAL;
  962. return (NULL);
  963. }
  964. /* Move data into newly-enlarged buffer. */
  965. if (filter->avail > 0)
  966. memmove(p, filter->next, filter->avail);
  967. free(filter->buffer);
  968. filter->next = filter->buffer = p;
  969. filter->buffer_size = s;
  970. }
  971. /* We can add client data to copy buffer. */
  972. /* First estimate: copy to fill rest of buffer. */
  973. tocopy = (filter->buffer + filter->buffer_size)
  974. - (filter->next + filter->avail);
  975. /* Don't waste time buffering more than we need to. */
  976. if (tocopy + filter->avail > min)
  977. tocopy = min - filter->avail;
  978. /* Don't copy more than is available. */
  979. if (tocopy > filter->client_avail)
  980. tocopy = filter->client_avail;
  981. memcpy(filter->next + filter->avail, filter->client_next,
  982. tocopy);
  983. /* Remove this data from client buffer. */
  984. filter->client_next += tocopy;
  985. filter->client_avail -= tocopy;
  986. /* add it to copy buffer. */
  987. filter->avail += tocopy;
  988. }
  989. }
  990. }
  991. /*
  992. * Move the file pointer forward. This should be called after
  993. * __archive_read_ahead() returns data to you. Don't try to move
  994. * ahead by more than the amount of data available according to
  995. * __archive_read_ahead().
  996. */
  997. /*
  998. * Mark the appropriate data as used. Note that the request here will
  999. * often be much smaller than the size of the previous read_ahead
  1000. * request.
  1001. */
  1002. ssize_t
  1003. __archive_read_consume(struct archive_read *a, size_t request)
  1004. {
  1005. ssize_t r;
  1006. r = __archive_read_filter_consume(a->filter, request);
  1007. a->archive.file_position += r;
  1008. return (r);
  1009. }
  1010. ssize_t
  1011. __archive_read_filter_consume(struct archive_read_filter * filter,
  1012. size_t request)
  1013. {
  1014. if (filter->avail > 0) {
  1015. /* Read came from copy buffer. */
  1016. filter->next += request;
  1017. filter->avail -= request;
  1018. } else {
  1019. /* Read came from client buffer. */
  1020. filter->client_next += request;
  1021. filter->client_avail -= request;
  1022. }
  1023. return (request);
  1024. }
  1025. /*
  1026. * Move the file pointer ahead by an arbitrary amount. If you're
  1027. * reading uncompressed data from a disk file, this will actually
  1028. * translate into a seek() operation. Even in cases where seek()
  1029. * isn't feasible, this at least pushes the read-and-discard loop
  1030. * down closer to the data source.
  1031. */
  1032. int64_t
  1033. __archive_read_skip(struct archive_read *a, int64_t request)
  1034. {
  1035. int64_t skipped = __archive_read_skip_lenient(a, request);
  1036. if (skipped == request)
  1037. return (skipped);
  1038. /* We hit EOF before we satisfied the skip request. */
  1039. if (skipped < 0) /* Map error code to 0 for error message below. */
  1040. skipped = 0;
  1041. archive_set_error(&a->archive,
  1042. ARCHIVE_ERRNO_MISC,
  1043. "Truncated input file (needed %jd bytes, only %jd available)",
  1044. (intmax_t)request, (intmax_t)skipped);
  1045. return (ARCHIVE_FATAL);
  1046. }
  1047. int64_t
  1048. __archive_read_skip_lenient(struct archive_read *a, int64_t request)
  1049. {
  1050. int64_t skipped = __archive_read_filter_skip(a->filter, request);
  1051. if (skipped > 0)
  1052. a->archive.file_position += skipped;
  1053. return (skipped);
  1054. }
  1055. int64_t
  1056. __archive_read_filter_skip(struct archive_read_filter *filter, int64_t request)
  1057. {
  1058. int64_t bytes_skipped, total_bytes_skipped = 0;
  1059. size_t min;
  1060. if (filter->fatal)
  1061. return (-1);
  1062. /*
  1063. * If there is data in the buffers already, use that first.
  1064. */
  1065. if (filter->avail > 0) {
  1066. min = minimum(request, (off_t)filter->avail);
  1067. bytes_skipped = __archive_read_filter_consume(filter, min);
  1068. request -= bytes_skipped;
  1069. total_bytes_skipped += bytes_skipped;
  1070. }
  1071. if (filter->client_avail > 0) {
  1072. min = minimum(request, (int64_t)filter->client_avail);
  1073. bytes_skipped = __archive_read_filter_consume(filter, min);
  1074. request -= bytes_skipped;
  1075. total_bytes_skipped += bytes_skipped;
  1076. }
  1077. if (request == 0)
  1078. return (total_bytes_skipped);
  1079. /*
  1080. * If a client_skipper was provided, try that first.
  1081. */
  1082. #if ARCHIVE_API_VERSION < 2
  1083. if ((filter->skip != NULL) && (request < SSIZE_MAX)) {
  1084. #else
  1085. if (filter->skip != NULL) {
  1086. #endif
  1087. bytes_skipped = (filter->skip)(filter, request);
  1088. if (bytes_skipped < 0) { /* error */
  1089. filter->client_total = filter->client_avail = 0;
  1090. filter->client_next = filter->client_buff = NULL;
  1091. filter->fatal = 1;
  1092. return (bytes_skipped);
  1093. }
  1094. total_bytes_skipped += bytes_skipped;
  1095. request -= bytes_skipped;
  1096. filter->client_next = filter->client_buff;
  1097. filter->client_avail = filter->client_total = 0;
  1098. }
  1099. /*
  1100. * Note that client_skipper will usually not satisfy the
  1101. * full request (due to low-level blocking concerns),
  1102. * so even if client_skipper is provided, we may still
  1103. * have to use ordinary reads to finish out the request.
  1104. */
  1105. while (request > 0) {
  1106. const void* dummy_buffer;
  1107. ssize_t bytes_read;
  1108. dummy_buffer = __archive_read_filter_ahead(filter,
  1109. 1, &bytes_read);
  1110. if (bytes_read < 0)
  1111. return (bytes_read);
  1112. if (bytes_read == 0) {
  1113. return (total_bytes_skipped);
  1114. }
  1115. min = (size_t)(minimum(bytes_read, request));
  1116. bytes_read = __archive_read_filter_consume(filter, min);
  1117. total_bytes_skipped += bytes_read;
  1118. request -= bytes_read;
  1119. }
  1120. return (total_bytes_skipped);
  1121. }