2 * Copyright (c) 2003-2011 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
27 * This file contains the "essential" portions of the read API, that
28 * is, stuff that will probably always be used by any client that
29 * actually needs to read an archive. Optional pieces have been, as
30 * far as possible, separated out into separate files to avoid
31 * needlessly bloating statically-linked clients.
34 #include "archive_platform.h"
35 __FBSDID("$FreeBSD$");
52 #include "archive_entry.h"
53 #include "archive_private.h"
54 #include "archive_read_private.h"
56 #define minimum(a, b) (a < b ? a : b)
58 static int choose_filters(struct archive_read *);
59 static int choose_format(struct archive_read *);
60 static struct archive_vtable *archive_read_vtable(void);
61 static int64_t _archive_filter_bytes(struct archive *, int);
62 static int _archive_filter_code(struct archive *, int);
63 static const char *_archive_filter_name(struct archive *, int);
64 static int _archive_filter_count(struct archive *);
65 static int _archive_read_close(struct archive *);
66 static int _archive_read_data_block(struct archive *,
67 const void **, size_t *, int64_t *);
68 static int _archive_read_free(struct archive *);
69 static int _archive_read_next_header(struct archive *,
70 struct archive_entry **);
71 static int _archive_read_next_header2(struct archive *,
72 struct archive_entry *);
73 static int64_t advance_file_pointer(struct archive_read_filter *, int64_t);
75 static struct archive_vtable *
76 archive_read_vtable(void)
78 static struct archive_vtable av;
79 static int inited = 0;
82 av.archive_filter_bytes = _archive_filter_bytes;
83 av.archive_filter_code = _archive_filter_code;
84 av.archive_filter_name = _archive_filter_name;
85 av.archive_filter_count = _archive_filter_count;
86 av.archive_read_data_block = _archive_read_data_block;
87 av.archive_read_next_header = _archive_read_next_header;
88 av.archive_read_next_header2 = _archive_read_next_header2;
89 av.archive_free = _archive_read_free;
90 av.archive_close = _archive_read_close;
97 * Allocate, initialize and return a struct archive object.
100 archive_read_new(void)
102 struct archive_read *a;
104 a = (struct archive_read *)calloc(1, sizeof(*a));
107 a->archive.magic = ARCHIVE_READ_MAGIC;
109 a->archive.state = ARCHIVE_STATE_NEW;
110 a->entry = archive_entry_new2(&a->archive);
111 a->archive.vtable = archive_read_vtable();
113 a->passphrases.last = &a->passphrases.first;
115 return (&a->archive);
119 * Record the do-not-extract-to file. This belongs in archive_read_extract.c.
122 archive_read_extract_set_skip_file(struct archive *_a, int64_t d, int64_t i)
124 struct archive_read *a = (struct archive_read *)_a;
126 if (ARCHIVE_OK != __archive_check_magic(_a, ARCHIVE_READ_MAGIC,
127 ARCHIVE_STATE_ANY, "archive_read_extract_set_skip_file"))
129 a->skip_file_set = 1;
130 a->skip_file_dev = d;
131 a->skip_file_ino = i;
138 archive_read_open(struct archive *a, void *client_data,
139 archive_open_callback *client_opener, archive_read_callback *client_reader,
140 archive_close_callback *client_closer)
142 /* Old archive_read_open() is just a thin shell around
143 * archive_read_open1. */
144 archive_read_set_open_callback(a, client_opener);
145 archive_read_set_read_callback(a, client_reader);
146 archive_read_set_close_callback(a, client_closer);
147 archive_read_set_callback_data(a, client_data);
148 return archive_read_open1(a);
153 archive_read_open2(struct archive *a, void *client_data,
154 archive_open_callback *client_opener,
155 archive_read_callback *client_reader,
156 archive_skip_callback *client_skipper,
157 archive_close_callback *client_closer)
159 /* Old archive_read_open2() is just a thin shell around
160 * archive_read_open1. */
161 archive_read_set_callback_data(a, client_data);
162 archive_read_set_open_callback(a, client_opener);
163 archive_read_set_read_callback(a, client_reader);
164 archive_read_set_skip_callback(a, client_skipper);
165 archive_read_set_close_callback(a, client_closer);
166 return archive_read_open1(a);
170 client_read_proxy(struct archive_read_filter *self, const void **buff)
173 r = (self->archive->client.reader)(&self->archive->archive,
179 client_skip_proxy(struct archive_read_filter *self, int64_t request)
182 __archive_errx(1, "Negative skip requested.");
186 if (self->archive->client.skipper != NULL) {
187 /* Seek requests over 1GiB are broken down into
188 * multiple seeks. This avoids overflows when the
189 * requests get passed through 32-bit arguments. */
190 int64_t skip_limit = (int64_t)1 << 30;
193 int64_t get, ask = request;
194 if (ask > skip_limit)
196 get = (self->archive->client.skipper)
197 (&self->archive->archive, self->data, ask);
199 if (get == 0 || get == request)
202 return ARCHIVE_FATAL;
205 } else if (self->archive->client.seeker != NULL
206 && request > 64 * 1024) {
207 /* If the client provided a seeker but not a skipper,
208 * we can use the seeker to skip forward.
210 * Note: This isn't always a good idea. The client
211 * skipper is allowed to skip by less than requested
212 * if it needs to maintain block alignment. The
213 * seeker is not allowed to play such games, so using
214 * the seeker here may be a performance loss compared
215 * to just reading and discarding. That's why we
216 * only do this for skips of over 64k.
218 int64_t before = self->position;
219 int64_t after = (self->archive->client.seeker)
220 (&self->archive->archive, self->data, request, SEEK_CUR);
221 if (after != before + request)
222 return ARCHIVE_FATAL;
223 return after - before;
229 client_seek_proxy(struct archive_read_filter *self, int64_t offset, int whence)
231 /* DO NOT use the skipper here! If we transparently handled
232 * forward seek here by using the skipper, that will break
233 * other libarchive code that assumes a successful forward
234 * seek means it can also seek backwards.
236 if (self->archive->client.seeker == NULL) {
237 archive_set_error(&self->archive->archive, ARCHIVE_ERRNO_MISC,
238 "Current client reader does not support seeking a device");
239 return (ARCHIVE_FAILED);
241 return (self->archive->client.seeker)(&self->archive->archive,
242 self->data, offset, whence);
246 client_close_proxy(struct archive_read_filter *self)
248 int r = ARCHIVE_OK, r2;
251 if (self->archive->client.closer == NULL)
253 for (i = 0; i < self->archive->client.nodes; i++)
255 r2 = (self->archive->client.closer)
256 ((struct archive *)self->archive,
257 self->archive->client.dataset[i].data);
265 client_open_proxy(struct archive_read_filter *self)
268 if (self->archive->client.opener != NULL)
269 r = (self->archive->client.opener)(
270 (struct archive *)self->archive, self->data);
275 client_switch_proxy(struct archive_read_filter *self, unsigned int iindex)
277 int r1 = ARCHIVE_OK, r2 = ARCHIVE_OK;
280 /* Don't do anything if already in the specified data node */
281 if (self->archive->client.cursor == iindex)
284 self->archive->client.cursor = iindex;
285 data2 = self->archive->client.dataset[self->archive->client.cursor].data;
286 if (self->archive->client.switcher != NULL)
288 r1 = r2 = (self->archive->client.switcher)
289 ((struct archive *)self->archive, self->data, data2);
294 /* Attempt to call close and open instead */
295 if (self->archive->client.closer != NULL)
296 r1 = (self->archive->client.closer)
297 ((struct archive *)self->archive, self->data);
299 if (self->archive->client.opener != NULL)
300 r2 = (self->archive->client.opener)
301 ((struct archive *)self->archive, self->data);
303 return (r1 < r2) ? r1 : r2;
307 archive_read_set_open_callback(struct archive *_a,
308 archive_open_callback *client_opener)
310 struct archive_read *a = (struct archive_read *)_a;
311 archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
312 "archive_read_set_open_callback");
313 a->client.opener = client_opener;
318 archive_read_set_read_callback(struct archive *_a,
319 archive_read_callback *client_reader)
321 struct archive_read *a = (struct archive_read *)_a;
322 archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
323 "archive_read_set_read_callback");
324 a->client.reader = client_reader;
329 archive_read_set_skip_callback(struct archive *_a,
330 archive_skip_callback *client_skipper)
332 struct archive_read *a = (struct archive_read *)_a;
333 archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
334 "archive_read_set_skip_callback");
335 a->client.skipper = client_skipper;
340 archive_read_set_seek_callback(struct archive *_a,
341 archive_seek_callback *client_seeker)
343 struct archive_read *a = (struct archive_read *)_a;
344 archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
345 "archive_read_set_seek_callback");
346 a->client.seeker = client_seeker;
351 archive_read_set_close_callback(struct archive *_a,
352 archive_close_callback *client_closer)
354 struct archive_read *a = (struct archive_read *)_a;
355 archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
356 "archive_read_set_close_callback");
357 a->client.closer = client_closer;
362 archive_read_set_switch_callback(struct archive *_a,
363 archive_switch_callback *client_switcher)
365 struct archive_read *a = (struct archive_read *)_a;
366 archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
367 "archive_read_set_switch_callback");
368 a->client.switcher = client_switcher;
373 archive_read_set_callback_data(struct archive *_a, void *client_data)
375 return archive_read_set_callback_data2(_a, client_data, 0);
379 archive_read_set_callback_data2(struct archive *_a, void *client_data,
382 struct archive_read *a = (struct archive_read *)_a;
383 archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
384 "archive_read_set_callback_data2");
386 if (a->client.nodes == 0)
388 a->client.dataset = (struct archive_read_data_node *)
389 calloc(1, sizeof(*a->client.dataset));
390 if (a->client.dataset == NULL)
392 archive_set_error(&a->archive, ENOMEM,
394 return ARCHIVE_FATAL;
399 if (iindex > a->client.nodes - 1)
401 archive_set_error(&a->archive, EINVAL,
402 "Invalid index specified.");
403 return ARCHIVE_FATAL;
405 a->client.dataset[iindex].data = client_data;
406 a->client.dataset[iindex].begin_position = -1;
407 a->client.dataset[iindex].total_size = -1;
412 archive_read_add_callback_data(struct archive *_a, void *client_data,
415 struct archive_read *a = (struct archive_read *)_a;
419 archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
420 "archive_read_add_callback_data");
421 if (iindex > a->client.nodes) {
422 archive_set_error(&a->archive, EINVAL,
423 "Invalid index specified.");
424 return ARCHIVE_FATAL;
426 p = realloc(a->client.dataset, sizeof(*a->client.dataset)
427 * (++(a->client.nodes)));
429 archive_set_error(&a->archive, ENOMEM,
431 return ARCHIVE_FATAL;
433 a->client.dataset = (struct archive_read_data_node *)p;
434 for (i = a->client.nodes - 1; i > iindex && i > 0; i--) {
435 a->client.dataset[i].data = a->client.dataset[i-1].data;
436 a->client.dataset[i].begin_position = -1;
437 a->client.dataset[i].total_size = -1;
439 a->client.dataset[iindex].data = client_data;
440 a->client.dataset[iindex].begin_position = -1;
441 a->client.dataset[iindex].total_size = -1;
446 archive_read_append_callback_data(struct archive *_a, void *client_data)
448 struct archive_read *a = (struct archive_read *)_a;
449 return archive_read_add_callback_data(_a, client_data, a->client.nodes);
453 archive_read_prepend_callback_data(struct archive *_a, void *client_data)
455 return archive_read_add_callback_data(_a, client_data, 0);
459 archive_read_open1(struct archive *_a)
461 struct archive_read *a = (struct archive_read *)_a;
462 struct archive_read_filter *filter, *tmp;
463 int slot, e = ARCHIVE_OK;
466 archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
467 "archive_read_open");
468 archive_clear_error(&a->archive);
470 if (a->client.reader == NULL) {
471 archive_set_error(&a->archive, EINVAL,
472 "No reader function provided to archive_read_open");
473 a->archive.state = ARCHIVE_STATE_FATAL;
474 return (ARCHIVE_FATAL);
477 /* Open data source. */
478 if (a->client.opener != NULL) {
479 e = (a->client.opener)(&a->archive, a->client.dataset[0].data);
481 /* If the open failed, call the closer to clean up. */
482 if (a->client.closer) {
483 for (i = 0; i < a->client.nodes; i++)
484 (a->client.closer)(&a->archive,
485 a->client.dataset[i].data);
491 filter = calloc(1, sizeof(*filter));
493 return (ARCHIVE_FATAL);
494 filter->bidder = NULL;
495 filter->upstream = NULL;
497 filter->data = a->client.dataset[0].data;
498 filter->open = client_open_proxy;
499 filter->read = client_read_proxy;
500 filter->skip = client_skip_proxy;
501 filter->seek = client_seek_proxy;
502 filter->close = client_close_proxy;
503 filter->sswitch = client_switch_proxy;
504 filter->name = "none";
505 filter->code = ARCHIVE_FILTER_NONE;
507 a->client.dataset[0].begin_position = 0;
508 if (!a->filter || !a->bypass_filter_bidding)
511 /* Build out the input pipeline. */
512 e = choose_filters(a);
513 if (e < ARCHIVE_WARN) {
514 a->archive.state = ARCHIVE_STATE_FATAL;
515 return (ARCHIVE_FATAL);
520 /* Need to add "NONE" type filter at the end of the filter chain */
522 while (tmp->upstream)
524 tmp->upstream = filter;
529 slot = choose_format(a);
531 __archive_read_close_filters(a);
532 a->archive.state = ARCHIVE_STATE_FATAL;
533 return (ARCHIVE_FATAL);
535 a->format = &(a->formats[slot]);
538 a->archive.state = ARCHIVE_STATE_HEADER;
540 /* Ensure libarchive starts from the first node in a multivolume set */
541 client_switch_proxy(a->filter, 0);
546 * Allow each registered stream transform to bid on whether
547 * it wants to handle this stream. Repeat until we've finished
548 * building the pipeline.
551 /* We won't build a filter pipeline with more stages than this. */
552 #define MAX_NUMBER_FILTERS 25
555 choose_filters(struct archive_read *a)
557 int number_bidders, i, bid, best_bid, number_filters;
558 struct archive_read_filter_bidder *bidder, *best_bidder;
559 struct archive_read_filter *filter;
563 for (number_filters = 0; number_filters < MAX_NUMBER_FILTERS; ++number_filters) {
564 number_bidders = sizeof(a->bidders) / sizeof(a->bidders[0]);
570 for (i = 0; i < number_bidders; i++, bidder++) {
571 if (bidder->bid != NULL) {
572 bid = (bidder->bid)(bidder, a->filter);
573 if (bid > best_bid) {
575 best_bidder = bidder;
580 /* If no bidder, we're done. */
581 if (best_bidder == NULL) {
582 /* Verify the filter by asking it for some data. */
583 __archive_read_filter_ahead(a->filter, 1, &avail);
585 __archive_read_close_filters(a);
586 __archive_read_free_filters(a);
587 return (ARCHIVE_FATAL);
589 a->archive.compression_name = a->filter->name;
590 a->archive.compression_code = a->filter->code;
595 = (struct archive_read_filter *)calloc(1, sizeof(*filter));
597 return (ARCHIVE_FATAL);
598 filter->bidder = best_bidder;
600 filter->upstream = a->filter;
602 r = (best_bidder->init)(a->filter);
603 if (r != ARCHIVE_OK) {
604 __archive_read_close_filters(a);
605 __archive_read_free_filters(a);
606 return (ARCHIVE_FATAL);
609 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
610 "Input requires too many filters for decoding");
611 return (ARCHIVE_FATAL);
615 * Read header of next entry.
618 _archive_read_next_header2(struct archive *_a, struct archive_entry *entry)
620 struct archive_read *a = (struct archive_read *)_a;
621 int r1 = ARCHIVE_OK, r2;
623 archive_check_magic(_a, ARCHIVE_READ_MAGIC,
624 ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
625 "archive_read_next_header");
627 archive_entry_clear(entry);
628 archive_clear_error(&a->archive);
631 * If client didn't consume entire data, skip any remainder
632 * (This is especially important for GNU incremental directories.)
634 if (a->archive.state == ARCHIVE_STATE_DATA) {
635 r1 = archive_read_data_skip(&a->archive);
636 if (r1 == ARCHIVE_EOF)
637 archive_set_error(&a->archive, EIO,
638 "Premature end-of-file.");
639 if (r1 == ARCHIVE_EOF || r1 == ARCHIVE_FATAL) {
640 a->archive.state = ARCHIVE_STATE_FATAL;
641 return (ARCHIVE_FATAL);
645 /* Record start-of-header offset in uncompressed stream. */
646 a->header_position = a->filter->position;
649 r2 = (a->format->read_header)(a, entry);
652 * EOF and FATAL are persistent at this layer. By
653 * modifying the state, we guarantee that future calls to
654 * read a header or read data will fail.
658 a->archive.state = ARCHIVE_STATE_EOF;
659 --_a->file_count;/* Revert a file counter. */
662 a->archive.state = ARCHIVE_STATE_DATA;
665 a->archive.state = ARCHIVE_STATE_DATA;
670 a->archive.state = ARCHIVE_STATE_FATAL;
674 __archive_reset_read_data(&a->archive);
676 a->data_start_node = a->client.cursor;
677 /* EOF always wins; otherwise return the worst error. */
678 return (r2 < r1 || r2 == ARCHIVE_EOF) ? r2 : r1;
682 _archive_read_next_header(struct archive *_a, struct archive_entry **entryp)
685 struct archive_read *a = (struct archive_read *)_a;
687 ret = _archive_read_next_header2(_a, a->entry);
693 * Allow each registered format to bid on whether it wants to handle
694 * the next entry. Return index of winning bidder.
697 choose_format(struct archive_read *a)
704 slots = sizeof(a->formats) / sizeof(a->formats[0]);
708 /* Set up a->format for convenience of bidders. */
709 a->format = &(a->formats[0]);
710 for (i = 0; i < slots; i++, a->format++) {
711 if (a->format->bid) {
712 bid = (a->format->bid)(a, best_bid);
713 if (bid == ARCHIVE_FATAL)
714 return (ARCHIVE_FATAL);
715 if (a->filter->position != 0)
716 __archive_read_seek(a, 0, SEEK_SET);
717 if ((bid > best_bid) || (best_bid_slot < 0)) {
725 * There were no bidders; this is a serious programmer error
726 * and demands a quick and definitive abort.
728 if (best_bid_slot < 0) {
729 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
730 "No formats registered");
731 return (ARCHIVE_FATAL);
735 * There were bidders, but no non-zero bids; this means we
736 * can't support this stream.
739 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
740 "Unrecognized archive format");
741 return (ARCHIVE_FATAL);
744 return (best_bid_slot);
748 * Return the file offset (within the uncompressed data stream) where
749 * the last header started.
752 archive_read_header_position(struct archive *_a)
754 struct archive_read *a = (struct archive_read *)_a;
755 archive_check_magic(_a, ARCHIVE_READ_MAGIC,
756 ARCHIVE_STATE_ANY, "archive_read_header_position");
757 return (a->header_position);
761 * Returns 1 if the archive contains at least one encrypted entry.
762 * If the archive format not support encryption at all
763 * ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED is returned.
764 * If for any other reason (e.g. not enough data read so far)
765 * we cannot say whether there are encrypted entries, then
766 * ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW is returned.
767 * In general, this function will return values below zero when the
768 * reader is uncertain or totally uncapable of encryption support.
769 * When this function returns 0 you can be sure that the reader
770 * supports encryption detection but no encrypted entries have
773 * NOTE: If the metadata/header of an archive is also encrypted, you
774 * cannot rely on the number of encrypted entries. That is why this
775 * function does not return the number of encrypted entries but#
776 * just shows that there are some.
779 archive_read_has_encrypted_entries(struct archive *_a)
781 struct archive_read *a = (struct archive_read *)_a;
782 int format_supports_encryption = archive_read_format_capabilities(_a)
783 & (ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_DATA | ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_METADATA);
785 if (!_a || !format_supports_encryption) {
786 /* Format in general doesn't support encryption */
787 return ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED;
790 /* A reader potentially has read enough data now. */
791 if (a->format && a->format->has_encrypted_entries) {
792 return (a->format->has_encrypted_entries)(a);
795 /* For any other reason we cannot say how many entries are there. */
796 return ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW;
800 * Returns a bitmask of capabilities that are supported by the archive format reader.
801 * If the reader has no special capabilities, ARCHIVE_READ_FORMAT_CAPS_NONE is returned.
804 archive_read_format_capabilities(struct archive *_a)
806 struct archive_read *a = (struct archive_read *)_a;
807 if (a && a->format && a->format->format_capabilties) {
808 return (a->format->format_capabilties)(a);
810 return ARCHIVE_READ_FORMAT_CAPS_NONE;
814 * Read data from an archive entry, using a read(2)-style interface.
815 * This is a convenience routine that just calls
816 * archive_read_data_block and copies the results into the client
817 * buffer, filling any gaps with zero bytes. Clients using this
818 * API can be completely ignorant of sparse-file issues; sparse files
819 * will simply be padded with nulls.
821 * DO NOT intermingle calls to this function and archive_read_data_block
822 * to read a single entry body.
825 archive_read_data(struct archive *_a, void *buff, size_t s)
827 struct archive *a = (struct archive *)_a;
829 const void *read_buf;
838 if (a->read_data_remaining == 0) {
839 read_buf = a->read_data_block;
840 a->read_data_is_posix_read = 1;
841 a->read_data_requested = s;
842 r = archive_read_data_block(a, &read_buf,
843 &a->read_data_remaining, &a->read_data_offset);
844 a->read_data_block = read_buf;
845 if (r == ARCHIVE_EOF)
848 * Error codes are all negative, so the status
849 * return here cannot be confused with a valid
850 * byte count. (ARCHIVE_OK is zero.)
856 if (a->read_data_offset < a->read_data_output_offset) {
857 archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT,
858 "Encountered out-of-order sparse blocks");
859 return (ARCHIVE_RETRY);
862 /* Compute the amount of zero padding needed. */
863 if (a->read_data_output_offset + (int64_t)s <
864 a->read_data_offset) {
866 } else if (a->read_data_output_offset <
867 a->read_data_offset) {
868 len = (size_t)(a->read_data_offset -
869 a->read_data_output_offset);
874 memset(dest, 0, len);
876 a->read_data_output_offset += len;
880 /* Copy data if there is any space left. */
882 len = a->read_data_remaining;
885 memcpy(dest, a->read_data_block, len);
887 a->read_data_block += len;
888 a->read_data_remaining -= len;
889 a->read_data_output_offset += len;
890 a->read_data_offset += len;
895 a->read_data_is_posix_read = 0;
896 a->read_data_requested = 0;
901 * Reset the read_data_* variables, used for starting a new entry.
903 void __archive_reset_read_data(struct archive * a)
905 a->read_data_output_offset = 0;
906 a->read_data_remaining = 0;
907 a->read_data_is_posix_read = 0;
908 a->read_data_requested = 0;
910 /* extra resets, from rar.c */
911 a->read_data_block = NULL;
912 a->read_data_offset = 0;
916 * Skip over all remaining data in this entry.
919 archive_read_data_skip(struct archive *_a)
921 struct archive_read *a = (struct archive_read *)_a;
927 archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA,
928 "archive_read_data_skip");
930 if (a->format->read_data_skip != NULL)
931 r = (a->format->read_data_skip)(a);
933 while ((r = archive_read_data_block(&a->archive,
934 &buff, &size, &offset))
939 if (r == ARCHIVE_EOF)
942 a->archive.state = ARCHIVE_STATE_HEADER;
947 archive_seek_data(struct archive *_a, int64_t offset, int whence)
949 struct archive_read *a = (struct archive_read *)_a;
950 archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA,
951 "archive_seek_data_block");
953 if (a->format->seek_data == NULL) {
954 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
956 "No format_seek_data_block function registered");
957 return (ARCHIVE_FATAL);
960 return (a->format->seek_data)(a, offset, whence);
964 * Read the next block of entry data from the archive.
965 * This is a zero-copy interface; the client receives a pointer,
966 * size, and file offset of the next available block of data.
968 * Returns ARCHIVE_OK if the operation is successful, ARCHIVE_EOF if
969 * the end of entry is encountered.
972 _archive_read_data_block(struct archive *_a,
973 const void **buff, size_t *size, int64_t *offset)
975 struct archive_read *a = (struct archive_read *)_a;
976 archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA,
977 "archive_read_data_block");
979 if (a->format->read_data == NULL) {
980 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
982 "No format->read_data function registered");
983 return (ARCHIVE_FATAL);
986 return (a->format->read_data)(a, buff, size, offset);
990 __archive_read_close_filters(struct archive_read *a)
992 struct archive_read_filter *f = a->filter;
994 /* Close each filter in the pipeline. */
996 struct archive_read_filter *t = f->upstream;
997 if (!f->closed && f->close != NULL) {
998 int r1 = (f->close)(f);
1011 __archive_read_free_filters(struct archive_read *a)
1013 while (a->filter != NULL) {
1014 struct archive_read_filter *t = a->filter->upstream;
1021 * return the count of # of filters in use
1024 _archive_filter_count(struct archive *_a)
1026 struct archive_read *a = (struct archive_read *)_a;
1027 struct archive_read_filter *p = a->filter;
1037 * Close the file and all I/O.
1040 _archive_read_close(struct archive *_a)
1042 struct archive_read *a = (struct archive_read *)_a;
1043 int r = ARCHIVE_OK, r1 = ARCHIVE_OK;
1045 archive_check_magic(&a->archive, ARCHIVE_READ_MAGIC,
1046 ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_read_close");
1047 if (a->archive.state == ARCHIVE_STATE_CLOSED)
1048 return (ARCHIVE_OK);
1049 archive_clear_error(&a->archive);
1050 a->archive.state = ARCHIVE_STATE_CLOSED;
1052 /* TODO: Clean up the formatters. */
1054 /* Release the filter objects. */
1055 r1 = __archive_read_close_filters(a);
1063 * Release memory and other resources.
1066 _archive_read_free(struct archive *_a)
1068 struct archive_read *a = (struct archive_read *)_a;
1069 struct archive_read_passphrase *p;
1075 return (ARCHIVE_OK);
1076 archive_check_magic(_a, ARCHIVE_READ_MAGIC,
1077 ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_read_free");
1078 if (a->archive.state != ARCHIVE_STATE_CLOSED
1079 && a->archive.state != ARCHIVE_STATE_FATAL)
1080 r = archive_read_close(&a->archive);
1082 /* Call cleanup functions registered by optional components. */
1083 if (a->cleanup_archive_extract != NULL)
1084 r = (a->cleanup_archive_extract)(a);
1086 /* Cleanup format-specific data. */
1087 slots = sizeof(a->formats) / sizeof(a->formats[0]);
1088 for (i = 0; i < slots; i++) {
1089 a->format = &(a->formats[i]);
1090 if (a->formats[i].cleanup)
1091 (a->formats[i].cleanup)(a);
1094 /* Free the filters */
1095 __archive_read_free_filters(a);
1097 /* Release the bidder objects. */
1098 n = sizeof(a->bidders)/sizeof(a->bidders[0]);
1099 for (i = 0; i < n; i++) {
1100 if (a->bidders[i].free != NULL) {
1101 int r1 = (a->bidders[i].free)(&a->bidders[i]);
1107 /* Release passphrase list. */
1108 p = a->passphrases.first;
1110 struct archive_read_passphrase *np = p->next;
1112 /* A passphrase should be cleaned. */
1113 memset(p->passphrase, 0, strlen(p->passphrase));
1114 free(p->passphrase);
1119 archive_string_free(&a->archive.error_string);
1120 archive_entry_free(a->entry);
1121 a->archive.magic = 0;
1122 __archive_clean(&a->archive);
1123 free(a->client.dataset);
1128 static struct archive_read_filter *
1129 get_filter(struct archive *_a, int n)
1131 struct archive_read *a = (struct archive_read *)_a;
1132 struct archive_read_filter *f = a->filter;
1133 /* We use n == -1 for 'the last filter', which is always the
1135 if (n == -1 && f != NULL) {
1136 struct archive_read_filter *last = f;
1146 while (n > 0 && f != NULL) {
1154 _archive_filter_code(struct archive *_a, int n)
1156 struct archive_read_filter *f = get_filter(_a, n);
1157 return f == NULL ? -1 : f->code;
1161 _archive_filter_name(struct archive *_a, int n)
1163 struct archive_read_filter *f = get_filter(_a, n);
1164 return f != NULL ? f->name : NULL;
1168 _archive_filter_bytes(struct archive *_a, int n)
1170 struct archive_read_filter *f = get_filter(_a, n);
1171 return f == NULL ? -1 : f->position;
1175 * Used internally by read format handlers to register their bid and
1176 * initialization functions.
1179 __archive_read_register_format(struct archive_read *a,
1182 int (*bid)(struct archive_read *, int),
1183 int (*options)(struct archive_read *, const char *, const char *),
1184 int (*read_header)(struct archive_read *, struct archive_entry *),
1185 int (*read_data)(struct archive_read *, const void **, size_t *, int64_t *),
1186 int (*read_data_skip)(struct archive_read *),
1187 int64_t (*seek_data)(struct archive_read *, int64_t, int),
1188 int (*cleanup)(struct archive_read *),
1189 int (*format_capabilities)(struct archive_read *),
1190 int (*has_encrypted_entries)(struct archive_read *))
1192 int i, number_slots;
1194 archive_check_magic(&a->archive,
1195 ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
1196 "__archive_read_register_format");
1198 number_slots = sizeof(a->formats) / sizeof(a->formats[0]);
1200 for (i = 0; i < number_slots; i++) {
1201 if (a->formats[i].bid == bid)
1202 return (ARCHIVE_WARN); /* We've already installed */
1203 if (a->formats[i].bid == NULL) {
1204 a->formats[i].bid = bid;
1205 a->formats[i].options = options;
1206 a->formats[i].read_header = read_header;
1207 a->formats[i].read_data = read_data;
1208 a->formats[i].read_data_skip = read_data_skip;
1209 a->formats[i].seek_data = seek_data;
1210 a->formats[i].cleanup = cleanup;
1211 a->formats[i].data = format_data;
1212 a->formats[i].name = name;
1213 a->formats[i].format_capabilties = format_capabilities;
1214 a->formats[i].has_encrypted_entries = has_encrypted_entries;
1215 return (ARCHIVE_OK);
1219 archive_set_error(&a->archive, ENOMEM,
1220 "Not enough slots for format registration");
1221 return (ARCHIVE_FATAL);
1225 * Used internally by decompression routines to register their bid and
1226 * initialization functions.
1229 __archive_read_get_bidder(struct archive_read *a,
1230 struct archive_read_filter_bidder **bidder)
1232 int i, number_slots;
1234 number_slots = sizeof(a->bidders) / sizeof(a->bidders[0]);
1236 for (i = 0; i < number_slots; i++) {
1237 if (a->bidders[i].bid == NULL) {
1238 memset(a->bidders + i, 0, sizeof(a->bidders[0]));
1239 *bidder = (a->bidders + i);
1240 return (ARCHIVE_OK);
1244 archive_set_error(&a->archive, ENOMEM,
1245 "Not enough slots for filter registration");
1246 return (ARCHIVE_FATAL);
1250 * The next section implements the peek/consume internal I/O
1251 * system used by archive readers. This system allows simple
1252 * read-ahead for consumers while preserving zero-copy operation
1255 * The two key operations:
1256 * * The read-ahead function returns a pointer to a block of data
1257 * that satisfies a minimum request.
1258 * * The consume function advances the file pointer.
1260 * In the ideal case, filters generate blocks of data
1261 * and __archive_read_ahead() just returns pointers directly into
1262 * those blocks. Then __archive_read_consume() just bumps those
1263 * pointers. Only if your request would span blocks does the I/O
1264 * layer use a copy buffer to provide you with a contiguous block of
1267 * A couple of useful idioms:
1268 * * "I just want some data." Ask for 1 byte and pay attention to
1269 * the "number of bytes available" from __archive_read_ahead().
1270 * Consume whatever you actually use.
1271 * * "I want to output a large block of data." As above, ask for 1 byte,
1272 * emit all that's available (up to whatever limit you have), consume
1273 * it all, then repeat until you're done. This effectively means that
1274 * you're passing along the blocks that came from your provider.
1275 * * "I want to peek ahead by a large amount." Ask for 4k or so, then
1276 * double and repeat until you get an error or have enough. Note
1277 * that the I/O layer will likely end up expanding its copy buffer
1278 * to fit your request, so use this technique cautiously. This
1279 * technique is used, for example, by some of the format tasting
1280 * code that has uncertain look-ahead needs.
1284 * Looks ahead in the input stream:
1285 * * If 'avail' pointer is provided, that returns number of bytes available
1286 * in the current buffer, which may be much larger than requested.
1287 * * If end-of-file, *avail gets set to zero.
1288 * * If error, *avail gets error code.
1289 * * If request can be met, returns pointer to data.
1290 * * If minimum request cannot be met, returns NULL.
1292 * Note: If you just want "some data", ask for 1 byte and pay attention
1293 * to *avail, which will have the actual amount available. If you
1294 * know exactly how many bytes you need, just ask for that and treat
1295 * a NULL return as an error.
1297 * Important: This does NOT move the file pointer. See
1298 * __archive_read_consume() below.
1301 __archive_read_ahead(struct archive_read *a, size_t min, ssize_t *avail)
1303 return (__archive_read_filter_ahead(a->filter, min, avail));
1307 __archive_read_filter_ahead(struct archive_read_filter *filter,
1308 size_t min, ssize_t *avail)
1313 if (filter->fatal) {
1315 *avail = ARCHIVE_FATAL;
1320 * Keep pulling more data until we can satisfy the request.
1325 * If we can satisfy from the copy buffer (and the
1326 * copy buffer isn't empty), we're done. In particular,
1327 * note that min == 0 is a perfectly well-defined
1330 if (filter->avail >= min && filter->avail > 0) {
1332 *avail = filter->avail;
1333 return (filter->next);
1337 * We can satisfy directly from client buffer if everything
1338 * currently in the copy buffer is still in the client buffer.
1340 if (filter->client_total >= filter->client_avail + filter->avail
1341 && filter->client_avail + filter->avail >= min) {
1342 /* "Roll back" to client buffer. */
1343 filter->client_avail += filter->avail;
1344 filter->client_next -= filter->avail;
1345 /* Copy buffer is now empty. */
1347 filter->next = filter->buffer;
1348 /* Return data from client buffer. */
1350 *avail = filter->client_avail;
1351 return (filter->client_next);
1354 /* Move data forward in copy buffer if necessary. */
1355 if (filter->next > filter->buffer &&
1356 filter->next + min > filter->buffer + filter->buffer_size) {
1357 if (filter->avail > 0)
1358 memmove(filter->buffer, filter->next,
1360 filter->next = filter->buffer;
1363 /* If we've used up the client data, get more. */
1364 if (filter->client_avail <= 0) {
1365 if (filter->end_of_file) {
1370 bytes_read = (filter->read)(filter,
1371 &filter->client_buff);
1372 if (bytes_read < 0) { /* Read error. */
1373 filter->client_total = filter->client_avail = 0;
1374 filter->client_next =
1375 filter->client_buff = NULL;
1378 *avail = ARCHIVE_FATAL;
1381 if (bytes_read == 0) {
1382 /* Check for another client object first */
1383 if (filter->archive->client.cursor !=
1384 filter->archive->client.nodes - 1) {
1385 if (client_switch_proxy(filter,
1386 filter->archive->client.cursor + 1)
1390 /* Premature end-of-file. */
1391 filter->client_total = filter->client_avail = 0;
1392 filter->client_next =
1393 filter->client_buff = NULL;
1394 filter->end_of_file = 1;
1395 /* Return whatever we do have. */
1397 *avail = filter->avail;
1400 filter->client_total = bytes_read;
1401 filter->client_avail = filter->client_total;
1402 filter->client_next = filter->client_buff;
1405 * We can't satisfy the request from the copy
1406 * buffer or the existing client data, so we
1407 * need to copy more client data over to the
1411 /* Ensure the buffer is big enough. */
1412 if (min > filter->buffer_size) {
1416 /* Double the buffer; watch for overflow. */
1417 s = t = filter->buffer_size;
1422 if (t <= s) { /* Integer overflow! */
1424 &filter->archive->archive,
1426 "Unable to allocate copy"
1430 *avail = ARCHIVE_FATAL;
1435 /* Now s >= min, so allocate a new buffer. */
1436 p = (char *)malloc(s);
1439 &filter->archive->archive,
1441 "Unable to allocate copy buffer");
1444 *avail = ARCHIVE_FATAL;
1447 /* Move data into newly-enlarged buffer. */
1448 if (filter->avail > 0)
1449 memmove(p, filter->next, filter->avail);
1450 free(filter->buffer);
1451 filter->next = filter->buffer = p;
1452 filter->buffer_size = s;
1455 /* We can add client data to copy buffer. */
1456 /* First estimate: copy to fill rest of buffer. */
1457 tocopy = (filter->buffer + filter->buffer_size)
1458 - (filter->next + filter->avail);
1459 /* Don't waste time buffering more than we need to. */
1460 if (tocopy + filter->avail > min)
1461 tocopy = min - filter->avail;
1462 /* Don't copy more than is available. */
1463 if (tocopy > filter->client_avail)
1464 tocopy = filter->client_avail;
1466 memcpy(filter->next + filter->avail,
1467 filter->client_next, tocopy);
1468 /* Remove this data from client buffer. */
1469 filter->client_next += tocopy;
1470 filter->client_avail -= tocopy;
1471 /* add it to copy buffer. */
1472 filter->avail += tocopy;
1478 * Move the file pointer forward.
1481 __archive_read_consume(struct archive_read *a, int64_t request)
1483 return (__archive_read_filter_consume(a->filter, request));
1487 __archive_read_filter_consume(struct archive_read_filter * filter,
1493 return ARCHIVE_FATAL;
1497 skipped = advance_file_pointer(filter, request);
1498 if (skipped == request)
1500 /* We hit EOF before we satisfied the skip request. */
1501 if (skipped < 0) /* Map error code to 0 for error message below. */
1503 archive_set_error(&filter->archive->archive,
1505 "Truncated input file (needed %jd bytes, only %jd available)",
1506 (intmax_t)request, (intmax_t)skipped);
1507 return (ARCHIVE_FATAL);
1511 * Advance the file pointer by the amount requested.
1512 * Returns the amount actually advanced, which may be less than the
1513 * request if EOF is encountered first.
1514 * Returns a negative value if there's an I/O error.
1517 advance_file_pointer(struct archive_read_filter *filter, int64_t request)
1519 int64_t bytes_skipped, total_bytes_skipped = 0;
1526 /* Use up the copy buffer first. */
1527 if (filter->avail > 0) {
1528 min = (size_t)minimum(request, (int64_t)filter->avail);
1529 filter->next += min;
1530 filter->avail -= min;
1532 filter->position += min;
1533 total_bytes_skipped += min;
1536 /* Then use up the client buffer. */
1537 if (filter->client_avail > 0) {
1538 min = (size_t)minimum(request, (int64_t)filter->client_avail);
1539 filter->client_next += min;
1540 filter->client_avail -= min;
1542 filter->position += min;
1543 total_bytes_skipped += min;
1546 return (total_bytes_skipped);
1548 /* If there's an optimized skip function, use it. */
1549 if (filter->skip != NULL) {
1550 bytes_skipped = (filter->skip)(filter, request);
1551 if (bytes_skipped < 0) { /* error */
1553 return (bytes_skipped);
1555 filter->position += bytes_skipped;
1556 total_bytes_skipped += bytes_skipped;
1557 request -= bytes_skipped;
1559 return (total_bytes_skipped);
1562 /* Use ordinary reads as necessary to complete the request. */
1564 bytes_read = (filter->read)(filter, &filter->client_buff);
1565 if (bytes_read < 0) {
1566 filter->client_buff = NULL;
1568 return (bytes_read);
1571 if (bytes_read == 0) {
1572 if (filter->archive->client.cursor !=
1573 filter->archive->client.nodes - 1) {
1574 if (client_switch_proxy(filter,
1575 filter->archive->client.cursor + 1)
1579 filter->client_buff = NULL;
1580 filter->end_of_file = 1;
1581 return (total_bytes_skipped);
1584 if (bytes_read >= request) {
1585 filter->client_next =
1586 ((const char *)filter->client_buff) + request;
1587 filter->client_avail = (size_t)(bytes_read - request);
1588 filter->client_total = bytes_read;
1589 total_bytes_skipped += request;
1590 filter->position += request;
1591 return (total_bytes_skipped);
1594 filter->position += bytes_read;
1595 total_bytes_skipped += bytes_read;
1596 request -= bytes_read;
1601 * Returns ARCHIVE_FAILED if seeking isn't supported.
1604 __archive_read_seek(struct archive_read *a, int64_t offset, int whence)
1606 return __archive_read_filter_seek(a->filter, offset, whence);
1610 __archive_read_filter_seek(struct archive_read_filter *filter, int64_t offset,
1613 struct archive_read_client *client;
1615 unsigned int cursor;
1617 if (filter->closed || filter->fatal)
1618 return (ARCHIVE_FATAL);
1619 if (filter->seek == NULL)
1620 return (ARCHIVE_FAILED);
1622 client = &(filter->archive->client);
1625 /* Adjust the offset and use SEEK_SET instead */
1626 offset += filter->position;
1631 if (client->dataset[cursor].begin_position < 0 ||
1632 client->dataset[cursor].total_size < 0 ||
1633 client->dataset[cursor].begin_position +
1634 client->dataset[cursor].total_size - 1 > offset ||
1635 cursor + 1 >= client->nodes)
1637 r = client->dataset[cursor].begin_position +
1638 client->dataset[cursor].total_size;
1639 client->dataset[++cursor].begin_position = r;
1642 r = client_switch_proxy(filter, cursor);
1643 if (r != ARCHIVE_OK)
1645 if ((r = client_seek_proxy(filter, 0, SEEK_END)) < 0)
1647 client->dataset[cursor].total_size = r;
1648 if (client->dataset[cursor].begin_position +
1649 client->dataset[cursor].total_size - 1 > offset ||
1650 cursor + 1 >= client->nodes)
1652 r = client->dataset[cursor].begin_position +
1653 client->dataset[cursor].total_size;
1654 client->dataset[++cursor].begin_position = r;
1656 offset -= client->dataset[cursor].begin_position;
1658 || offset > client->dataset[cursor].total_size)
1659 return ARCHIVE_FATAL;
1660 if ((r = client_seek_proxy(filter, offset, SEEK_SET)) < 0)
1667 if (client->dataset[cursor].begin_position < 0 ||
1668 client->dataset[cursor].total_size < 0 ||
1669 cursor + 1 >= client->nodes)
1671 r = client->dataset[cursor].begin_position +
1672 client->dataset[cursor].total_size;
1673 client->dataset[++cursor].begin_position = r;
1676 r = client_switch_proxy(filter, cursor);
1677 if (r != ARCHIVE_OK)
1679 if ((r = client_seek_proxy(filter, 0, SEEK_END)) < 0)
1681 client->dataset[cursor].total_size = r;
1682 r = client->dataset[cursor].begin_position +
1683 client->dataset[cursor].total_size;
1684 if (cursor + 1 >= client->nodes)
1686 client->dataset[++cursor].begin_position = r;
1690 client->dataset[cursor].begin_position)
1692 offset += client->dataset[cursor].total_size;
1696 r = client->dataset[cursor].begin_position +
1697 client->dataset[cursor].total_size;
1699 offset = (r + offset) - client->dataset[cursor].begin_position;
1700 if ((r = client_switch_proxy(filter, cursor)) != ARCHIVE_OK)
1702 r = client_seek_proxy(filter, offset, SEEK_SET);
1708 return (ARCHIVE_FATAL);
1710 r += client->dataset[cursor].begin_position;
1714 * Ouch. Clearing the buffer like this hurts, especially
1715 * at bid time. A lot of our efficiency at bid time comes
1716 * from having bidders reuse the data we've already read.
1718 * TODO: If the seek request is in data we already
1719 * have, then don't call the seek callback.
1721 * TODO: Zip seeks to end-of-file at bid time. If
1722 * other formats also start doing this, we may need to
1723 * find a way for clients to fudge the seek offset to
1726 * Hmmm... If whence was SEEK_END, we know the file
1727 * size is (r - offset). Can we use that to simplify
1728 * the TODO items above?
1730 filter->avail = filter->client_avail = 0;
1731 filter->next = filter->buffer;
1732 filter->position = r;
1733 filter->end_of_file = 0;