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: head/lib/libarchive/archive_read.c 201157 2009-12-29 05:30:23Z kientzle $");
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 int close_filters(struct archive_read *);
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 const struct archive_vtable
76 archive_read_vtable = {
77 .archive_filter_bytes = _archive_filter_bytes,
78 .archive_filter_code = _archive_filter_code,
79 .archive_filter_name = _archive_filter_name,
80 .archive_filter_count = _archive_filter_count,
81 .archive_read_data_block = _archive_read_data_block,
82 .archive_read_next_header = _archive_read_next_header,
83 .archive_read_next_header2 = _archive_read_next_header2,
84 .archive_free = _archive_read_free,
85 .archive_close = _archive_read_close,
89 * Allocate, initialize and return a struct archive object.
92 archive_read_new(void)
94 struct archive_read *a;
96 a = (struct archive_read *)calloc(1, sizeof(*a));
99 a->archive.magic = ARCHIVE_READ_MAGIC;
101 a->archive.state = ARCHIVE_STATE_NEW;
102 a->entry = archive_entry_new2(&a->archive);
103 a->archive.vtable = &archive_read_vtable;
105 a->passphrases.last = &a->passphrases.first;
107 return (&a->archive);
111 * Record the do-not-extract-to file. This belongs in archive_read_extract.c.
114 archive_read_extract_set_skip_file(struct archive *_a, la_int64_t d,
117 struct archive_read *a = (struct archive_read *)_a;
119 if (ARCHIVE_OK != __archive_check_magic(_a, ARCHIVE_READ_MAGIC,
120 ARCHIVE_STATE_ANY, "archive_read_extract_set_skip_file"))
122 a->skip_file_set = 1;
123 a->skip_file_dev = d;
124 a->skip_file_ino = i;
131 archive_read_open(struct archive *a, void *client_data,
132 archive_open_callback *client_opener, archive_read_callback *client_reader,
133 archive_close_callback *client_closer)
135 /* Old archive_read_open() is just a thin shell around
136 * archive_read_open1. */
137 archive_read_set_open_callback(a, client_opener);
138 archive_read_set_read_callback(a, client_reader);
139 archive_read_set_close_callback(a, client_closer);
140 archive_read_set_callback_data(a, client_data);
141 return archive_read_open1(a);
146 archive_read_open2(struct archive *a, void *client_data,
147 archive_open_callback *client_opener,
148 archive_read_callback *client_reader,
149 archive_skip_callback *client_skipper,
150 archive_close_callback *client_closer)
152 /* Old archive_read_open2() is just a thin shell around
153 * archive_read_open1. */
154 archive_read_set_callback_data(a, client_data);
155 archive_read_set_open_callback(a, client_opener);
156 archive_read_set_read_callback(a, client_reader);
157 archive_read_set_skip_callback(a, client_skipper);
158 archive_read_set_close_callback(a, client_closer);
159 return archive_read_open1(a);
163 client_read_proxy(struct archive_read_filter *self, const void **buff)
166 r = (self->archive->client.reader)(&self->archive->archive,
172 client_skip_proxy(struct archive_read_filter *self, int64_t request)
175 __archive_errx(1, "Negative skip requested.");
179 if (self->archive->client.skipper != NULL) {
180 /* Seek requests over 1GiB are broken down into
181 * multiple seeks. This avoids overflows when the
182 * requests get passed through 32-bit arguments. */
183 int64_t skip_limit = (int64_t)1 << 30;
186 int64_t get, ask = request;
187 if (ask > skip_limit)
189 get = (self->archive->client.skipper)
190 (&self->archive->archive, self->data, ask);
192 if (get == 0 || get == request)
195 return ARCHIVE_FATAL;
198 } else if (self->archive->client.seeker != NULL
199 && request > 64 * 1024) {
200 /* If the client provided a seeker but not a skipper,
201 * we can use the seeker to skip forward.
203 * Note: This isn't always a good idea. The client
204 * skipper is allowed to skip by less than requested
205 * if it needs to maintain block alignment. The
206 * seeker is not allowed to play such games, so using
207 * the seeker here may be a performance loss compared
208 * to just reading and discarding. That's why we
209 * only do this for skips of over 64k.
211 int64_t before = self->position;
212 int64_t after = (self->archive->client.seeker)
213 (&self->archive->archive, self->data, request, SEEK_CUR);
214 if (after != before + request)
215 return ARCHIVE_FATAL;
216 return after - before;
222 client_seek_proxy(struct archive_read_filter *self, int64_t offset, int whence)
224 /* DO NOT use the skipper here! If we transparently handled
225 * forward seek here by using the skipper, that will break
226 * other libarchive code that assumes a successful forward
227 * seek means it can also seek backwards.
229 if (self->archive->client.seeker == NULL) {
230 archive_set_error(&self->archive->archive, ARCHIVE_ERRNO_MISC,
231 "Current client reader does not support seeking a device");
232 return (ARCHIVE_FAILED);
234 return (self->archive->client.seeker)(&self->archive->archive,
235 self->data, offset, whence);
239 read_client_close_proxy(struct archive_read *a)
241 int r = ARCHIVE_OK, r2;
244 if (a->client.closer == NULL)
246 for (i = 0; i < a->client.nodes; i++)
248 r2 = (a->client.closer)
249 ((struct archive *)a, a->client.dataset[i].data);
257 client_close_proxy(struct archive_read_filter *self)
259 return read_client_close_proxy(self->archive);
263 client_open_proxy(struct archive_read_filter *self)
266 if (self->archive->client.opener != NULL)
267 r = (self->archive->client.opener)(
268 (struct archive *)self->archive, self->data);
273 client_switch_proxy(struct archive_read_filter *self, unsigned int iindex)
275 int r1 = ARCHIVE_OK, r2 = ARCHIVE_OK;
278 /* Don't do anything if already in the specified data node */
279 if (self->archive->client.cursor == iindex)
282 self->archive->client.cursor = iindex;
283 data2 = self->archive->client.dataset[self->archive->client.cursor].data;
284 if (self->archive->client.switcher != NULL)
286 r1 = r2 = (self->archive->client.switcher)
287 ((struct archive *)self->archive, self->data, data2);
292 /* Attempt to call close and open instead */
293 if (self->archive->client.closer != NULL)
294 r1 = (self->archive->client.closer)
295 ((struct archive *)self->archive, self->data);
297 r2 = client_open_proxy(self);
299 return (r1 < r2) ? r1 : r2;
303 archive_read_set_open_callback(struct archive *_a,
304 archive_open_callback *client_opener)
306 struct archive_read *a = (struct archive_read *)_a;
307 archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
308 "archive_read_set_open_callback");
309 a->client.opener = client_opener;
314 archive_read_set_read_callback(struct archive *_a,
315 archive_read_callback *client_reader)
317 struct archive_read *a = (struct archive_read *)_a;
318 archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
319 "archive_read_set_read_callback");
320 a->client.reader = client_reader;
325 archive_read_set_skip_callback(struct archive *_a,
326 archive_skip_callback *client_skipper)
328 struct archive_read *a = (struct archive_read *)_a;
329 archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
330 "archive_read_set_skip_callback");
331 a->client.skipper = client_skipper;
336 archive_read_set_seek_callback(struct archive *_a,
337 archive_seek_callback *client_seeker)
339 struct archive_read *a = (struct archive_read *)_a;
340 archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
341 "archive_read_set_seek_callback");
342 a->client.seeker = client_seeker;
347 archive_read_set_close_callback(struct archive *_a,
348 archive_close_callback *client_closer)
350 struct archive_read *a = (struct archive_read *)_a;
351 archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
352 "archive_read_set_close_callback");
353 a->client.closer = client_closer;
358 archive_read_set_switch_callback(struct archive *_a,
359 archive_switch_callback *client_switcher)
361 struct archive_read *a = (struct archive_read *)_a;
362 archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
363 "archive_read_set_switch_callback");
364 a->client.switcher = client_switcher;
369 archive_read_set_callback_data(struct archive *_a, void *client_data)
371 return archive_read_set_callback_data2(_a, client_data, 0);
375 archive_read_set_callback_data2(struct archive *_a, void *client_data,
378 struct archive_read *a = (struct archive_read *)_a;
379 archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
380 "archive_read_set_callback_data2");
382 if (a->client.nodes == 0)
384 a->client.dataset = (struct archive_read_data_node *)
385 calloc(1, sizeof(*a->client.dataset));
386 if (a->client.dataset == NULL)
388 archive_set_error(&a->archive, ENOMEM,
390 return ARCHIVE_FATAL;
395 if (iindex > a->client.nodes - 1)
397 archive_set_error(&a->archive, EINVAL,
398 "Invalid index specified.");
399 return ARCHIVE_FATAL;
401 a->client.dataset[iindex].data = client_data;
402 a->client.dataset[iindex].begin_position = -1;
403 a->client.dataset[iindex].total_size = -1;
408 archive_read_add_callback_data(struct archive *_a, void *client_data,
411 struct archive_read *a = (struct archive_read *)_a;
415 archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
416 "archive_read_add_callback_data");
417 if (iindex > a->client.nodes) {
418 archive_set_error(&a->archive, EINVAL,
419 "Invalid index specified.");
420 return ARCHIVE_FATAL;
422 p = realloc(a->client.dataset, sizeof(*a->client.dataset)
423 * (++(a->client.nodes)));
425 archive_set_error(&a->archive, ENOMEM,
427 return ARCHIVE_FATAL;
429 a->client.dataset = (struct archive_read_data_node *)p;
430 for (i = a->client.nodes - 1; i > iindex; i--) {
431 a->client.dataset[i].data = a->client.dataset[i-1].data;
432 a->client.dataset[i].begin_position = -1;
433 a->client.dataset[i].total_size = -1;
435 a->client.dataset[iindex].data = client_data;
436 a->client.dataset[iindex].begin_position = -1;
437 a->client.dataset[iindex].total_size = -1;
442 archive_read_append_callback_data(struct archive *_a, void *client_data)
444 struct archive_read *a = (struct archive_read *)_a;
445 return archive_read_add_callback_data(_a, client_data, a->client.nodes);
449 archive_read_prepend_callback_data(struct archive *_a, void *client_data)
451 return archive_read_add_callback_data(_a, client_data, 0);
454 static const struct archive_read_filter_vtable
455 none_reader_vtable = {
456 .read = client_read_proxy,
457 .close = client_close_proxy,
461 archive_read_open1(struct archive *_a)
463 struct archive_read *a = (struct archive_read *)_a;
464 struct archive_read_filter *filter, *tmp;
465 int slot, e = ARCHIVE_OK;
467 archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
468 "archive_read_open");
469 archive_clear_error(&a->archive);
471 if (a->client.reader == NULL) {
472 archive_set_error(&a->archive, EINVAL,
473 "No reader function provided to archive_read_open");
474 a->archive.state = ARCHIVE_STATE_FATAL;
475 return (ARCHIVE_FATAL);
478 /* Open data source. */
479 if (a->client.opener != NULL) {
480 e = (a->client.opener)(&a->archive, a->client.dataset[0].data);
482 /* If the open failed, call the closer to clean up. */
483 read_client_close_proxy(a);
488 filter = calloc(1, sizeof(*filter));
490 return (ARCHIVE_FATAL);
491 filter->bidder = NULL;
492 filter->upstream = NULL;
494 filter->data = a->client.dataset[0].data;
495 filter->vtable = &none_reader_vtable;
496 filter->name = "none";
497 filter->code = ARCHIVE_FILTER_NONE;
498 filter->can_skip = 1;
499 filter->can_seek = 1;
501 a->client.dataset[0].begin_position = 0;
502 if (!a->filter || !a->bypass_filter_bidding)
505 /* Build out the input pipeline. */
506 e = choose_filters(a);
507 if (e < ARCHIVE_WARN) {
508 a->archive.state = ARCHIVE_STATE_FATAL;
509 return (ARCHIVE_FATAL);
514 /* Need to add "NONE" type filter at the end of the filter chain */
516 while (tmp->upstream)
518 tmp->upstream = filter;
523 slot = choose_format(a);
526 a->archive.state = ARCHIVE_STATE_FATAL;
527 return (ARCHIVE_FATAL);
529 a->format = &(a->formats[slot]);
532 a->archive.state = ARCHIVE_STATE_HEADER;
534 /* Ensure libarchive starts from the first node in a multivolume set */
535 client_switch_proxy(a->filter, 0);
540 * Allow each registered stream transform to bid on whether
541 * it wants to handle this stream. Repeat until we've finished
542 * building the pipeline.
545 /* We won't build a filter pipeline with more stages than this. */
546 #define MAX_NUMBER_FILTERS 25
549 choose_filters(struct archive_read *a)
551 int number_bidders, i, bid, best_bid, number_filters;
552 struct archive_read_filter_bidder *bidder, *best_bidder;
553 struct archive_read_filter *filter;
557 for (number_filters = 0; number_filters < MAX_NUMBER_FILTERS; ++number_filters) {
558 number_bidders = sizeof(a->bidders) / sizeof(a->bidders[0]);
564 for (i = 0; i < number_bidders; i++, bidder++) {
565 if (bidder->vtable == NULL)
567 bid = (bidder->vtable->bid)(bidder, a->filter);
568 if (bid > best_bid) {
570 best_bidder = bidder;
574 /* If no bidder, we're done. */
575 if (best_bidder == NULL) {
576 /* Verify the filter by asking it for some data. */
577 __archive_read_filter_ahead(a->filter, 1, &avail);
579 __archive_read_free_filters(a);
580 return (ARCHIVE_FATAL);
586 = (struct archive_read_filter *)calloc(1, sizeof(*filter));
588 return (ARCHIVE_FATAL);
589 filter->bidder = best_bidder;
591 filter->upstream = a->filter;
593 r = (best_bidder->vtable->init)(a->filter);
594 if (r != ARCHIVE_OK) {
595 __archive_read_free_filters(a);
596 return (ARCHIVE_FATAL);
599 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
600 "Input requires too many filters for decoding");
601 return (ARCHIVE_FATAL);
605 __archive_read_header(struct archive_read *a, struct archive_entry *entry)
607 if (!a->filter->vtable->read_header)
609 return a->filter->vtable->read_header(a->filter, entry);
613 * Read header of next entry.
616 _archive_read_next_header2(struct archive *_a, struct archive_entry *entry)
618 struct archive_read *a = (struct archive_read *)_a;
619 int r1 = ARCHIVE_OK, r2;
621 archive_check_magic(_a, ARCHIVE_READ_MAGIC,
622 ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
623 "archive_read_next_header");
625 archive_entry_clear(entry);
626 archive_clear_error(&a->archive);
629 * If client didn't consume entire data, skip any remainder
630 * (This is especially important for GNU incremental directories.)
632 if (a->archive.state == ARCHIVE_STATE_DATA) {
633 r1 = archive_read_data_skip(&a->archive);
634 if (r1 == ARCHIVE_EOF)
635 archive_set_error(&a->archive, EIO,
636 "Premature end-of-file.");
637 if (r1 == ARCHIVE_EOF || r1 == ARCHIVE_FATAL) {
638 a->archive.state = ARCHIVE_STATE_FATAL;
639 return (ARCHIVE_FATAL);
643 /* Record start-of-header offset in uncompressed stream. */
644 a->header_position = a->filter->position;
647 r2 = (a->format->read_header)(a, entry);
650 * EOF and FATAL are persistent at this layer. By
651 * modifying the state, we guarantee that future calls to
652 * read a header or read data will fail.
656 a->archive.state = ARCHIVE_STATE_EOF;
657 --_a->file_count;/* Revert a file counter. */
660 a->archive.state = ARCHIVE_STATE_DATA;
663 a->archive.state = ARCHIVE_STATE_DATA;
668 a->archive.state = ARCHIVE_STATE_FATAL;
672 __archive_reset_read_data(&a->archive);
674 a->data_start_node = a->client.cursor;
675 /* EOF always wins; otherwise return the worst error. */
676 return (r2 < r1 || r2 == ARCHIVE_EOF) ? r2 : r1;
680 _archive_read_next_header(struct archive *_a, struct archive_entry **entryp)
683 struct archive_read *a = (struct archive_read *)_a;
685 ret = _archive_read_next_header2(_a, a->entry);
691 * Allow each registered format to bid on whether it wants to handle
692 * the next entry. Return index of winning bidder.
695 choose_format(struct archive_read *a)
702 slots = sizeof(a->formats) / sizeof(a->formats[0]);
706 /* Set up a->format for convenience of bidders. */
707 a->format = &(a->formats[0]);
708 for (i = 0; i < slots; i++, a->format++) {
709 if (a->format->bid) {
710 bid = (a->format->bid)(a, best_bid);
711 if (bid == ARCHIVE_FATAL)
712 return (ARCHIVE_FATAL);
713 if (a->filter->position != 0)
714 __archive_read_seek(a, 0, SEEK_SET);
715 if ((bid > best_bid) || (best_bid_slot < 0)) {
723 * There were no bidders; this is a serious programmer error
724 * and demands a quick and definitive abort.
726 if (best_bid_slot < 0) {
727 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
728 "No formats registered");
729 return (ARCHIVE_FATAL);
733 * There were bidders, but no non-zero bids; this means we
734 * can't support this stream.
737 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
738 "Unrecognized archive format");
739 return (ARCHIVE_FATAL);
742 return (best_bid_slot);
746 * Return the file offset (within the uncompressed data stream) where
747 * the last header started.
750 archive_read_header_position(struct archive *_a)
752 struct archive_read *a = (struct archive_read *)_a;
753 archive_check_magic(_a, ARCHIVE_READ_MAGIC,
754 ARCHIVE_STATE_ANY, "archive_read_header_position");
755 return (a->header_position);
759 * Returns 1 if the archive contains at least one encrypted entry.
760 * If the archive format not support encryption at all
761 * ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED is returned.
762 * If for any other reason (e.g. not enough data read so far)
763 * we cannot say whether there are encrypted entries, then
764 * ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW is returned.
765 * In general, this function will return values below zero when the
766 * reader is uncertain or totally incapable of encryption support.
767 * When this function returns 0 you can be sure that the reader
768 * supports encryption detection but no encrypted entries have
771 * NOTE: If the metadata/header of an archive is also encrypted, you
772 * cannot rely on the number of encrypted entries. That is why this
773 * function does not return the number of encrypted entries but#
774 * just shows that there are some.
777 archive_read_has_encrypted_entries(struct archive *_a)
779 struct archive_read *a = (struct archive_read *)_a;
780 int format_supports_encryption = archive_read_format_capabilities(_a)
781 & (ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_DATA | ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_METADATA);
783 if (!_a || !format_supports_encryption) {
784 /* Format in general doesn't support encryption */
785 return ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED;
788 /* A reader potentially has read enough data now. */
789 if (a->format && a->format->has_encrypted_entries) {
790 return (a->format->has_encrypted_entries)(a);
793 /* For any other reason we cannot say how many entries are there. */
794 return ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW;
798 * Returns a bitmask of capabilities that are supported by the archive format reader.
799 * If the reader has no special capabilities, ARCHIVE_READ_FORMAT_CAPS_NONE is returned.
802 archive_read_format_capabilities(struct archive *_a)
804 struct archive_read *a = (struct archive_read *)_a;
805 if (a && a->format && a->format->format_capabilties) {
806 return (a->format->format_capabilties)(a);
808 return ARCHIVE_READ_FORMAT_CAPS_NONE;
812 * Read data from an archive entry, using a read(2)-style interface.
813 * This is a convenience routine that just calls
814 * archive_read_data_block and copies the results into the client
815 * buffer, filling any gaps with zero bytes. Clients using this
816 * API can be completely ignorant of sparse-file issues; sparse files
817 * will simply be padded with nulls.
819 * DO NOT intermingle calls to this function and archive_read_data_block
820 * to read a single entry body.
823 archive_read_data(struct archive *_a, void *buff, size_t s)
825 struct archive *a = (struct archive *)_a;
827 const void *read_buf;
836 if (a->read_data_offset == a->read_data_output_offset &&
837 a->read_data_remaining == 0) {
838 read_buf = a->read_data_block;
839 a->read_data_is_posix_read = 1;
840 a->read_data_requested = s;
841 r = archive_read_data_block(a, &read_buf,
842 &a->read_data_remaining, &a->read_data_offset);
843 a->read_data_block = read_buf;
844 if (r == ARCHIVE_EOF)
847 * Error codes are all negative, so the status
848 * return here cannot be confused with a valid
849 * byte count. (ARCHIVE_OK is zero.)
855 if (a->read_data_offset < a->read_data_output_offset) {
856 archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT,
857 "Encountered out-of-order sparse blocks");
858 return (ARCHIVE_RETRY);
861 /* Compute the amount of zero padding needed. */
862 if (a->read_data_output_offset + (int64_t)s <
863 a->read_data_offset) {
865 } else if (a->read_data_output_offset <
866 a->read_data_offset) {
867 len = (size_t)(a->read_data_offset -
868 a->read_data_output_offset);
873 memset(dest, 0, len);
875 a->read_data_output_offset += len;
879 /* Copy data if there is any space left. */
881 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;
896 a->read_data_is_posix_read = 0;
897 a->read_data_requested = 0;
902 * Reset the read_data_* variables, used for starting a new entry.
904 void __archive_reset_read_data(struct archive * a)
906 a->read_data_output_offset = 0;
907 a->read_data_remaining = 0;
908 a->read_data_is_posix_read = 0;
909 a->read_data_requested = 0;
911 /* extra resets, from rar.c */
912 a->read_data_block = NULL;
913 a->read_data_offset = 0;
917 * Skip over all remaining data in this entry.
920 archive_read_data_skip(struct archive *_a)
922 struct archive_read *a = (struct archive_read *)_a;
928 archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA,
929 "archive_read_data_skip");
931 if (a->format->read_data_skip != NULL)
932 r = (a->format->read_data_skip)(a);
934 while ((r = archive_read_data_block(&a->archive,
935 &buff, &size, &offset))
940 if (r == ARCHIVE_EOF)
943 a->archive.state = ARCHIVE_STATE_HEADER;
948 archive_seek_data(struct archive *_a, int64_t offset, int whence)
950 struct archive_read *a = (struct archive_read *)_a;
951 archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA,
952 "archive_seek_data_block");
954 if (a->format->seek_data == NULL) {
955 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
957 "No format_seek_data_block function registered");
958 return (ARCHIVE_FATAL);
961 return (a->format->seek_data)(a, offset, whence);
965 * Read the next block of entry data from the archive.
966 * This is a zero-copy interface; the client receives a pointer,
967 * size, and file offset of the next available block of data.
969 * Returns ARCHIVE_OK if the operation is successful, ARCHIVE_EOF if
970 * the end of entry is encountered.
973 _archive_read_data_block(struct archive *_a,
974 const void **buff, size_t *size, int64_t *offset)
976 struct archive_read *a = (struct archive_read *)_a;
977 archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA,
978 "archive_read_data_block");
980 if (a->format->read_data == NULL) {
981 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
983 "No format->read_data function registered");
984 return (ARCHIVE_FATAL);
987 return (a->format->read_data)(a, buff, size, offset);
991 close_filters(struct archive_read *a)
993 struct archive_read_filter *f = a->filter;
995 /* Close each filter in the pipeline. */
997 struct archive_read_filter *t = f->upstream;
998 if (!f->closed && f->vtable != NULL) {
999 int r1 = (f->vtable->close)(f);
1012 __archive_read_free_filters(struct archive_read *a)
1014 /* Make sure filters are closed and their buffers are freed */
1017 while (a->filter != NULL) {
1018 struct archive_read_filter *t = a->filter->upstream;
1025 * return the count of # of filters in use
1028 _archive_filter_count(struct archive *_a)
1030 struct archive_read *a = (struct archive_read *)_a;
1031 struct archive_read_filter *p = a->filter;
1041 * Close the file and all I/O.
1044 _archive_read_close(struct archive *_a)
1046 struct archive_read *a = (struct archive_read *)_a;
1047 int r = ARCHIVE_OK, r1 = ARCHIVE_OK;
1049 archive_check_magic(&a->archive, ARCHIVE_READ_MAGIC,
1050 ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_read_close");
1051 if (a->archive.state == ARCHIVE_STATE_CLOSED)
1052 return (ARCHIVE_OK);
1053 archive_clear_error(&a->archive);
1054 a->archive.state = ARCHIVE_STATE_CLOSED;
1056 /* TODO: Clean up the formatters. */
1058 /* Release the filter objects. */
1059 r1 = close_filters(a);
1067 * Release memory and other resources.
1070 _archive_read_free(struct archive *_a)
1072 struct archive_read *a = (struct archive_read *)_a;
1073 struct archive_read_passphrase *p;
1079 return (ARCHIVE_OK);
1080 archive_check_magic(_a, ARCHIVE_READ_MAGIC,
1081 ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_read_free");
1082 if (a->archive.state != ARCHIVE_STATE_CLOSED
1083 && a->archive.state != ARCHIVE_STATE_FATAL)
1084 r = archive_read_close(&a->archive);
1086 /* Call cleanup functions registered by optional components. */
1087 if (a->cleanup_archive_extract != NULL)
1088 r = (a->cleanup_archive_extract)(a);
1090 /* Cleanup format-specific data. */
1091 slots = sizeof(a->formats) / sizeof(a->formats[0]);
1092 for (i = 0; i < slots; i++) {
1093 a->format = &(a->formats[i]);
1094 if (a->formats[i].cleanup)
1095 (a->formats[i].cleanup)(a);
1098 /* Free the filters */
1099 __archive_read_free_filters(a);
1101 /* Release the bidder objects. */
1102 n = sizeof(a->bidders)/sizeof(a->bidders[0]);
1103 for (i = 0; i < n; i++) {
1104 if (a->bidders[i].vtable == NULL ||
1105 a->bidders[i].vtable->free == NULL)
1107 (a->bidders[i].vtable->free)(&a->bidders[i]);
1110 /* Release passphrase list. */
1111 p = a->passphrases.first;
1113 struct archive_read_passphrase *np = p->next;
1115 /* A passphrase should be cleaned. */
1116 memset(p->passphrase, 0, strlen(p->passphrase));
1117 free(p->passphrase);
1122 archive_string_free(&a->archive.error_string);
1123 archive_entry_free(a->entry);
1124 a->archive.magic = 0;
1125 __archive_clean(&a->archive);
1126 free(a->client.dataset);
1131 static struct archive_read_filter *
1132 get_filter(struct archive *_a, int n)
1134 struct archive_read *a = (struct archive_read *)_a;
1135 struct archive_read_filter *f = a->filter;
1136 /* We use n == -1 for 'the last filter', which is always the
1138 if (n == -1 && f != NULL) {
1139 struct archive_read_filter *last = f;
1149 while (n > 0 && f != NULL) {
1157 _archive_filter_code(struct archive *_a, int n)
1159 struct archive_read_filter *f = get_filter(_a, n);
1160 return f == NULL ? -1 : f->code;
1164 _archive_filter_name(struct archive *_a, int n)
1166 struct archive_read_filter *f = get_filter(_a, n);
1167 return f != NULL ? f->name : NULL;
1171 _archive_filter_bytes(struct archive *_a, int n)
1173 struct archive_read_filter *f = get_filter(_a, n);
1174 return f == NULL ? -1 : f->position;
1178 * Used internally by read format handlers to register their bid and
1179 * initialization functions.
1182 __archive_read_register_format(struct archive_read *a,
1185 int (*bid)(struct archive_read *, int),
1186 int (*options)(struct archive_read *, const char *, const char *),
1187 int (*read_header)(struct archive_read *, struct archive_entry *),
1188 int (*read_data)(struct archive_read *, const void **, size_t *, int64_t *),
1189 int (*read_data_skip)(struct archive_read *),
1190 int64_t (*seek_data)(struct archive_read *, int64_t, int),
1191 int (*cleanup)(struct archive_read *),
1192 int (*format_capabilities)(struct archive_read *),
1193 int (*has_encrypted_entries)(struct archive_read *))
1195 int i, number_slots;
1197 archive_check_magic(&a->archive,
1198 ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
1199 "__archive_read_register_format");
1201 number_slots = sizeof(a->formats) / sizeof(a->formats[0]);
1203 for (i = 0; i < number_slots; i++) {
1204 if (a->formats[i].bid == bid)
1205 return (ARCHIVE_WARN); /* We've already installed */
1206 if (a->formats[i].bid == NULL) {
1207 a->formats[i].bid = bid;
1208 a->formats[i].options = options;
1209 a->formats[i].read_header = read_header;
1210 a->formats[i].read_data = read_data;
1211 a->formats[i].read_data_skip = read_data_skip;
1212 a->formats[i].seek_data = seek_data;
1213 a->formats[i].cleanup = cleanup;
1214 a->formats[i].data = format_data;
1215 a->formats[i].name = name;
1216 a->formats[i].format_capabilties = format_capabilities;
1217 a->formats[i].has_encrypted_entries = has_encrypted_entries;
1218 return (ARCHIVE_OK);
1222 archive_set_error(&a->archive, ENOMEM,
1223 "Not enough slots for format registration");
1224 return (ARCHIVE_FATAL);
1228 * Used internally by decompression routines to register their bid and
1229 * initialization functions.
1232 __archive_read_register_bidder(struct archive_read *a,
1235 const struct archive_read_filter_bidder_vtable *vtable)
1237 struct archive_read_filter_bidder *bidder;
1238 int i, number_slots;
1240 archive_check_magic(&a->archive, ARCHIVE_READ_MAGIC,
1241 ARCHIVE_STATE_NEW, "__archive_read_register_bidder");
1243 number_slots = sizeof(a->bidders) / sizeof(a->bidders[0]);
1245 for (i = 0; i < number_slots; i++) {
1246 if (a->bidders[i].vtable != NULL)
1248 memset(a->bidders + i, 0, sizeof(a->bidders[0]));
1249 bidder = (a->bidders + i);
1250 bidder->data = bidder_data;
1251 bidder->name = name;
1252 bidder->vtable = vtable;
1253 if (bidder->vtable->bid == NULL || bidder->vtable->init == NULL) {
1254 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
1256 "no bid/init for filter bidder");
1257 return (ARCHIVE_FATAL);
1260 return (ARCHIVE_OK);
1263 archive_set_error(&a->archive, ENOMEM,
1264 "Not enough slots for filter registration");
1265 return (ARCHIVE_FATAL);
1269 * The next section implements the peek/consume internal I/O
1270 * system used by archive readers. This system allows simple
1271 * read-ahead for consumers while preserving zero-copy operation
1274 * The two key operations:
1275 * * The read-ahead function returns a pointer to a block of data
1276 * that satisfies a minimum request.
1277 * * The consume function advances the file pointer.
1279 * In the ideal case, filters generate blocks of data
1280 * and __archive_read_ahead() just returns pointers directly into
1281 * those blocks. Then __archive_read_consume() just bumps those
1282 * pointers. Only if your request would span blocks does the I/O
1283 * layer use a copy buffer to provide you with a contiguous block of
1286 * A couple of useful idioms:
1287 * * "I just want some data." Ask for 1 byte and pay attention to
1288 * the "number of bytes available" from __archive_read_ahead().
1289 * Consume whatever you actually use.
1290 * * "I want to output a large block of data." As above, ask for 1 byte,
1291 * emit all that's available (up to whatever limit you have), consume
1292 * it all, then repeat until you're done. This effectively means that
1293 * you're passing along the blocks that came from your provider.
1294 * * "I want to peek ahead by a large amount." Ask for 4k or so, then
1295 * double and repeat until you get an error or have enough. Note
1296 * that the I/O layer will likely end up expanding its copy buffer
1297 * to fit your request, so use this technique cautiously. This
1298 * technique is used, for example, by some of the format tasting
1299 * code that has uncertain look-ahead needs.
1303 * Looks ahead in the input stream:
1304 * * If 'avail' pointer is provided, that returns number of bytes available
1305 * in the current buffer, which may be much larger than requested.
1306 * * If end-of-file, *avail gets set to zero.
1307 * * If error, *avail gets error code.
1308 * * If request can be met, returns pointer to data.
1309 * * If minimum request cannot be met, returns NULL.
1311 * Note: If you just want "some data", ask for 1 byte and pay attention
1312 * to *avail, which will have the actual amount available. If you
1313 * know exactly how many bytes you need, just ask for that and treat
1314 * a NULL return as an error.
1316 * Important: This does NOT move the file pointer. See
1317 * __archive_read_consume() below.
1320 __archive_read_ahead(struct archive_read *a, size_t min, ssize_t *avail)
1322 return (__archive_read_filter_ahead(a->filter, min, avail));
1326 __archive_read_filter_ahead(struct archive_read_filter *filter,
1327 size_t min, ssize_t *avail)
1332 if (filter->fatal) {
1334 *avail = ARCHIVE_FATAL;
1339 * Keep pulling more data until we can satisfy the request.
1344 * If we can satisfy from the copy buffer (and the
1345 * copy buffer isn't empty), we're done. In particular,
1346 * note that min == 0 is a perfectly well-defined
1349 if (filter->avail >= min && filter->avail > 0) {
1351 *avail = filter->avail;
1352 return (filter->next);
1356 * We can satisfy directly from client buffer if everything
1357 * currently in the copy buffer is still in the client buffer.
1359 if (filter->client_total >= filter->client_avail + filter->avail
1360 && filter->client_avail + filter->avail >= min) {
1361 /* "Roll back" to client buffer. */
1362 filter->client_avail += filter->avail;
1363 filter->client_next -= filter->avail;
1364 /* Copy buffer is now empty. */
1366 filter->next = filter->buffer;
1367 /* Return data from client buffer. */
1369 *avail = filter->client_avail;
1370 return (filter->client_next);
1373 /* Move data forward in copy buffer if necessary. */
1374 if (filter->next > filter->buffer &&
1375 filter->next + min > filter->buffer + filter->buffer_size) {
1376 if (filter->avail > 0)
1377 memmove(filter->buffer, filter->next,
1379 filter->next = filter->buffer;
1382 /* If we've used up the client data, get more. */
1383 if (filter->client_avail <= 0) {
1384 if (filter->end_of_file) {
1389 bytes_read = (filter->vtable->read)(filter,
1390 &filter->client_buff);
1391 if (bytes_read < 0) { /* Read error. */
1392 filter->client_total = filter->client_avail = 0;
1393 filter->client_next =
1394 filter->client_buff = NULL;
1397 *avail = ARCHIVE_FATAL;
1400 if (bytes_read == 0) {
1401 /* Check for another client object first */
1402 if (filter->archive->client.cursor !=
1403 filter->archive->client.nodes - 1) {
1404 if (client_switch_proxy(filter,
1405 filter->archive->client.cursor + 1)
1409 /* Premature end-of-file. */
1410 filter->client_total = filter->client_avail = 0;
1411 filter->client_next =
1412 filter->client_buff = NULL;
1413 filter->end_of_file = 1;
1414 /* Return whatever we do have. */
1416 *avail = filter->avail;
1419 filter->client_total = bytes_read;
1420 filter->client_avail = filter->client_total;
1421 filter->client_next = filter->client_buff;
1424 * We can't satisfy the request from the copy
1425 * buffer or the existing client data, so we
1426 * need to copy more client data over to the
1430 /* Ensure the buffer is big enough. */
1431 if (min > filter->buffer_size) {
1435 /* Double the buffer; watch for overflow. */
1436 s = t = filter->buffer_size;
1441 if (t <= s) { /* Integer overflow! */
1443 &filter->archive->archive,
1445 "Unable to allocate copy"
1449 *avail = ARCHIVE_FATAL;
1454 /* Now s >= min, so allocate a new buffer. */
1455 p = (char *)malloc(s);
1458 &filter->archive->archive,
1460 "Unable to allocate copy buffer");
1463 *avail = ARCHIVE_FATAL;
1466 /* Move data into newly-enlarged buffer. */
1467 if (filter->avail > 0)
1468 memmove(p, filter->next, filter->avail);
1469 free(filter->buffer);
1470 filter->next = filter->buffer = p;
1471 filter->buffer_size = s;
1474 /* We can add client data to copy buffer. */
1475 /* First estimate: copy to fill rest of buffer. */
1476 tocopy = (filter->buffer + filter->buffer_size)
1477 - (filter->next + filter->avail);
1478 /* Don't waste time buffering more than we need to. */
1479 if (tocopy + filter->avail > min)
1480 tocopy = min - filter->avail;
1481 /* Don't copy more than is available. */
1482 if (tocopy > filter->client_avail)
1483 tocopy = filter->client_avail;
1485 memcpy(filter->next + filter->avail,
1486 filter->client_next, tocopy);
1487 /* Remove this data from client buffer. */
1488 filter->client_next += tocopy;
1489 filter->client_avail -= tocopy;
1490 /* add it to copy buffer. */
1491 filter->avail += tocopy;
1497 * Move the file pointer forward.
1500 __archive_read_consume(struct archive_read *a, int64_t request)
1502 return (__archive_read_filter_consume(a->filter, request));
1506 __archive_read_filter_consume(struct archive_read_filter * filter,
1512 return ARCHIVE_FATAL;
1516 skipped = advance_file_pointer(filter, request);
1517 if (skipped == request)
1519 /* We hit EOF before we satisfied the skip request. */
1520 if (skipped < 0) /* Map error code to 0 for error message below. */
1522 archive_set_error(&filter->archive->archive,
1524 "Truncated input file (needed %jd bytes, only %jd available)",
1525 (intmax_t)request, (intmax_t)skipped);
1526 return (ARCHIVE_FATAL);
1530 * Advance the file pointer by the amount requested.
1531 * Returns the amount actually advanced, which may be less than the
1532 * request if EOF is encountered first.
1533 * Returns a negative value if there's an I/O error.
1536 advance_file_pointer(struct archive_read_filter *filter, int64_t request)
1538 int64_t bytes_skipped, total_bytes_skipped = 0;
1545 /* Use up the copy buffer first. */
1546 if (filter->avail > 0) {
1547 min = (size_t)minimum(request, (int64_t)filter->avail);
1548 filter->next += min;
1549 filter->avail -= min;
1551 filter->position += min;
1552 total_bytes_skipped += min;
1555 /* Then use up the client buffer. */
1556 if (filter->client_avail > 0) {
1557 min = (size_t)minimum(request, (int64_t)filter->client_avail);
1558 filter->client_next += min;
1559 filter->client_avail -= min;
1561 filter->position += min;
1562 total_bytes_skipped += min;
1565 return (total_bytes_skipped);
1567 /* If there's an optimized skip function, use it. */
1568 if (filter->can_skip != 0) {
1569 bytes_skipped = client_skip_proxy(filter, request);
1570 if (bytes_skipped < 0) { /* error */
1572 return (bytes_skipped);
1574 filter->position += bytes_skipped;
1575 total_bytes_skipped += bytes_skipped;
1576 request -= bytes_skipped;
1578 return (total_bytes_skipped);
1581 /* Use ordinary reads as necessary to complete the request. */
1583 bytes_read = (filter->vtable->read)(filter, &filter->client_buff);
1584 if (bytes_read < 0) {
1585 filter->client_buff = NULL;
1587 return (bytes_read);
1590 if (bytes_read == 0) {
1591 if (filter->archive->client.cursor !=
1592 filter->archive->client.nodes - 1) {
1593 if (client_switch_proxy(filter,
1594 filter->archive->client.cursor + 1)
1598 filter->client_buff = NULL;
1599 filter->end_of_file = 1;
1600 return (total_bytes_skipped);
1603 if (bytes_read >= request) {
1604 filter->client_next =
1605 ((const char *)filter->client_buff) + request;
1606 filter->client_avail = (size_t)(bytes_read - request);
1607 filter->client_total = bytes_read;
1608 total_bytes_skipped += request;
1609 filter->position += request;
1610 return (total_bytes_skipped);
1613 filter->position += bytes_read;
1614 total_bytes_skipped += bytes_read;
1615 request -= bytes_read;
1620 * Returns ARCHIVE_FAILED if seeking isn't supported.
1623 __archive_read_seek(struct archive_read *a, int64_t offset, int whence)
1625 return __archive_read_filter_seek(a->filter, offset, whence);
1629 __archive_read_filter_seek(struct archive_read_filter *filter, int64_t offset,
1632 struct archive_read_client *client;
1634 unsigned int cursor;
1636 if (filter->closed || filter->fatal)
1637 return (ARCHIVE_FATAL);
1638 if (filter->can_seek == 0)
1639 return (ARCHIVE_FAILED);
1641 client = &(filter->archive->client);
1644 /* Adjust the offset and use SEEK_SET instead */
1645 offset += filter->position;
1651 if (client->dataset[cursor].begin_position < 0 ||
1652 client->dataset[cursor].total_size < 0 ||
1653 client->dataset[cursor].begin_position +
1654 client->dataset[cursor].total_size - 1 > offset ||
1655 cursor + 1 >= client->nodes)
1657 r = client->dataset[cursor].begin_position +
1658 client->dataset[cursor].total_size;
1659 client->dataset[++cursor].begin_position = r;
1662 r = client_switch_proxy(filter, cursor);
1663 if (r != ARCHIVE_OK)
1665 if ((r = client_seek_proxy(filter, 0, SEEK_END)) < 0)
1667 client->dataset[cursor].total_size = r;
1668 if (client->dataset[cursor].begin_position +
1669 client->dataset[cursor].total_size - 1 > offset ||
1670 cursor + 1 >= client->nodes)
1672 r = client->dataset[cursor].begin_position +
1673 client->dataset[cursor].total_size;
1674 client->dataset[++cursor].begin_position = r;
1676 offset -= client->dataset[cursor].begin_position;
1678 || offset > client->dataset[cursor].total_size)
1679 return ARCHIVE_FATAL;
1680 if ((r = client_seek_proxy(filter, offset, SEEK_SET)) < 0)
1687 if (client->dataset[cursor].begin_position < 0 ||
1688 client->dataset[cursor].total_size < 0 ||
1689 cursor + 1 >= client->nodes)
1691 r = client->dataset[cursor].begin_position +
1692 client->dataset[cursor].total_size;
1693 client->dataset[++cursor].begin_position = r;
1696 r = client_switch_proxy(filter, cursor);
1697 if (r != ARCHIVE_OK)
1699 if ((r = client_seek_proxy(filter, 0, SEEK_END)) < 0)
1701 client->dataset[cursor].total_size = r;
1702 r = client->dataset[cursor].begin_position +
1703 client->dataset[cursor].total_size;
1704 if (cursor + 1 >= client->nodes)
1706 client->dataset[++cursor].begin_position = r;
1710 client->dataset[cursor].begin_position)
1712 offset += client->dataset[cursor].total_size;
1716 r = client->dataset[cursor].begin_position +
1717 client->dataset[cursor].total_size;
1719 offset = (r + offset) - client->dataset[cursor].begin_position;
1720 if ((r = client_switch_proxy(filter, cursor)) != ARCHIVE_OK)
1722 r = client_seek_proxy(filter, offset, SEEK_SET);
1728 return (ARCHIVE_FATAL);
1730 r += client->dataset[cursor].begin_position;
1734 * Ouch. Clearing the buffer like this hurts, especially
1735 * at bid time. A lot of our efficiency at bid time comes
1736 * from having bidders reuse the data we've already read.
1738 * TODO: If the seek request is in data we already
1739 * have, then don't call the seek callback.
1741 * TODO: Zip seeks to end-of-file at bid time. If
1742 * other formats also start doing this, we may need to
1743 * find a way for clients to fudge the seek offset to
1746 * Hmmm... If whence was SEEK_END, we know the file
1747 * size is (r - offset). Can we use that to simplify
1748 * the TODO items above?
1750 filter->avail = filter->client_avail = 0;
1751 filter->next = filter->buffer;
1752 filter->position = r;
1753 filter->end_of_file = 0;