archive_read.c 34 KB

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