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 int close_filters(struct archive_read *);
61 static struct archive_vtable *archive_read_vtable(void);
62 static int64_t _archive_filter_bytes(struct archive *, int);
63 static int _archive_filter_code(struct archive *, int);
64 static const char *_archive_filter_name(struct archive *, int);
65 static int _archive_filter_count(struct archive *);
66 static int _archive_read_close(struct archive *);
67 static int _archive_read_data_block(struct archive *,
68 const void **, size_t *, int64_t *);
69 static int _archive_read_free(struct archive *);
70 static int _archive_read_next_header(struct archive *,
71 struct archive_entry **);
72 static int _archive_read_next_header2(struct archive *,
73 struct archive_entry *);
74 static int64_t advance_file_pointer(struct archive_read_filter *, int64_t);
76 static struct archive_vtable *
77 archive_read_vtable(void)
79 static struct archive_vtable av;
80 static int inited = 0;
83 av.archive_filter_bytes = _archive_filter_bytes;
84 av.archive_filter_code = _archive_filter_code;
85 av.archive_filter_name = _archive_filter_name;
86 av.archive_filter_count = _archive_filter_count;
87 av.archive_read_data_block = _archive_read_data_block;
88 av.archive_read_next_header = _archive_read_next_header;
89 av.archive_read_next_header2 = _archive_read_next_header2;
90 av.archive_free = _archive_read_free;
91 av.archive_close = _archive_read_close;
98 * Allocate, initialize and return a struct archive object.
101 archive_read_new(void)
103 struct archive_read *a;
105 a = (struct archive_read *)calloc(1, sizeof(*a));
108 a->archive.magic = ARCHIVE_READ_MAGIC;
110 a->archive.state = ARCHIVE_STATE_NEW;
111 a->entry = archive_entry_new2(&a->archive);
112 a->archive.vtable = archive_read_vtable();
114 a->passphrases.last = &a->passphrases.first;
116 return (&a->archive);
120 * Record the do-not-extract-to file. This belongs in archive_read_extract.c.
123 archive_read_extract_set_skip_file(struct archive *_a, int64_t d, int64_t i)
125 struct archive_read *a = (struct archive_read *)_a;
127 if (ARCHIVE_OK != __archive_check_magic(_a, ARCHIVE_READ_MAGIC,
128 ARCHIVE_STATE_ANY, "archive_read_extract_set_skip_file"))
130 a->skip_file_set = 1;
131 a->skip_file_dev = d;
132 a->skip_file_ino = i;
139 archive_read_open(struct archive *a, void *client_data,
140 archive_open_callback *client_opener, archive_read_callback *client_reader,
141 archive_close_callback *client_closer)
143 /* Old archive_read_open() is just a thin shell around
144 * archive_read_open1. */
145 archive_read_set_open_callback(a, client_opener);
146 archive_read_set_read_callback(a, client_reader);
147 archive_read_set_close_callback(a, client_closer);
148 archive_read_set_callback_data(a, client_data);
149 return archive_read_open1(a);
154 archive_read_open2(struct archive *a, void *client_data,
155 archive_open_callback *client_opener,
156 archive_read_callback *client_reader,
157 archive_skip_callback *client_skipper,
158 archive_close_callback *client_closer)
160 /* Old archive_read_open2() is just a thin shell around
161 * archive_read_open1. */
162 archive_read_set_callback_data(a, client_data);
163 archive_read_set_open_callback(a, client_opener);
164 archive_read_set_read_callback(a, client_reader);
165 archive_read_set_skip_callback(a, client_skipper);
166 archive_read_set_close_callback(a, client_closer);
167 return archive_read_open1(a);
171 client_read_proxy(struct archive_read_filter *self, const void **buff)
174 r = (self->archive->client.reader)(&self->archive->archive,
180 client_skip_proxy(struct archive_read_filter *self, int64_t request)
183 __archive_errx(1, "Negative skip requested.");
187 if (self->archive->client.skipper != NULL) {
188 /* Seek requests over 1GiB are broken down into
189 * multiple seeks. This avoids overflows when the
190 * requests get passed through 32-bit arguments. */
191 int64_t skip_limit = (int64_t)1 << 30;
194 int64_t get, ask = request;
195 if (ask > skip_limit)
197 get = (self->archive->client.skipper)
198 (&self->archive->archive, self->data, ask);
200 if (get == 0 || get == request)
203 return ARCHIVE_FATAL;
206 } else if (self->archive->client.seeker != NULL
207 && request > 64 * 1024) {
208 /* If the client provided a seeker but not a skipper,
209 * we can use the seeker to skip forward.
211 * Note: This isn't always a good idea. The client
212 * skipper is allowed to skip by less than requested
213 * if it needs to maintain block alignment. The
214 * seeker is not allowed to play such games, so using
215 * the seeker here may be a performance loss compared
216 * to just reading and discarding. That's why we
217 * only do this for skips of over 64k.
219 int64_t before = self->position;
220 int64_t after = (self->archive->client.seeker)
221 (&self->archive->archive, self->data, request, SEEK_CUR);
222 if (after != before + request)
223 return ARCHIVE_FATAL;
224 return after - before;
230 client_seek_proxy(struct archive_read_filter *self, int64_t offset, int whence)
232 /* DO NOT use the skipper here! If we transparently handled
233 * forward seek here by using the skipper, that will break
234 * other libarchive code that assumes a successful forward
235 * seek means it can also seek backwards.
237 if (self->archive->client.seeker == NULL) {
238 archive_set_error(&self->archive->archive, ARCHIVE_ERRNO_MISC,
239 "Current client reader does not support seeking a device");
240 return (ARCHIVE_FAILED);
242 return (self->archive->client.seeker)(&self->archive->archive,
243 self->data, offset, whence);
247 client_close_proxy(struct archive_read_filter *self)
249 int r = ARCHIVE_OK, r2;
252 if (self->archive->client.closer == NULL)
254 for (i = 0; i < self->archive->client.nodes; i++)
256 r2 = (self->archive->client.closer)
257 ((struct archive *)self->archive,
258 self->archive->client.dataset[i].data);
266 client_open_proxy(struct archive_read_filter *self)
269 if (self->archive->client.opener != NULL)
270 r = (self->archive->client.opener)(
271 (struct archive *)self->archive, self->data);
276 client_switch_proxy(struct archive_read_filter *self, unsigned int iindex)
278 int r1 = ARCHIVE_OK, r2 = ARCHIVE_OK;
281 /* Don't do anything if already in the specified data node */
282 if (self->archive->client.cursor == iindex)
285 self->archive->client.cursor = iindex;
286 data2 = self->archive->client.dataset[self->archive->client.cursor].data;
287 if (self->archive->client.switcher != NULL)
289 r1 = r2 = (self->archive->client.switcher)
290 ((struct archive *)self->archive, self->data, data2);
295 /* Attempt to call close and open instead */
296 if (self->archive->client.closer != NULL)
297 r1 = (self->archive->client.closer)
298 ((struct archive *)self->archive, self->data);
300 if (self->archive->client.opener != NULL)
301 r2 = (self->archive->client.opener)
302 ((struct archive *)self->archive, self->data);
304 return (r1 < r2) ? r1 : r2;
308 archive_read_set_open_callback(struct archive *_a,
309 archive_open_callback *client_opener)
311 struct archive_read *a = (struct archive_read *)_a;
312 archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
313 "archive_read_set_open_callback");
314 a->client.opener = client_opener;
319 archive_read_set_read_callback(struct archive *_a,
320 archive_read_callback *client_reader)
322 struct archive_read *a = (struct archive_read *)_a;
323 archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
324 "archive_read_set_read_callback");
325 a->client.reader = client_reader;
330 archive_read_set_skip_callback(struct archive *_a,
331 archive_skip_callback *client_skipper)
333 struct archive_read *a = (struct archive_read *)_a;
334 archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
335 "archive_read_set_skip_callback");
336 a->client.skipper = client_skipper;
341 archive_read_set_seek_callback(struct archive *_a,
342 archive_seek_callback *client_seeker)
344 struct archive_read *a = (struct archive_read *)_a;
345 archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
346 "archive_read_set_seek_callback");
347 a->client.seeker = client_seeker;
352 archive_read_set_close_callback(struct archive *_a,
353 archive_close_callback *client_closer)
355 struct archive_read *a = (struct archive_read *)_a;
356 archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
357 "archive_read_set_close_callback");
358 a->client.closer = client_closer;
363 archive_read_set_switch_callback(struct archive *_a,
364 archive_switch_callback *client_switcher)
366 struct archive_read *a = (struct archive_read *)_a;
367 archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
368 "archive_read_set_switch_callback");
369 a->client.switcher = client_switcher;
374 archive_read_set_callback_data(struct archive *_a, void *client_data)
376 return archive_read_set_callback_data2(_a, client_data, 0);
380 archive_read_set_callback_data2(struct archive *_a, void *client_data,
383 struct archive_read *a = (struct archive_read *)_a;
384 archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
385 "archive_read_set_callback_data2");
387 if (a->client.nodes == 0)
389 a->client.dataset = (struct archive_read_data_node *)
390 calloc(1, sizeof(*a->client.dataset));
391 if (a->client.dataset == NULL)
393 archive_set_error(&a->archive, ENOMEM,
395 return ARCHIVE_FATAL;
400 if (iindex > a->client.nodes - 1)
402 archive_set_error(&a->archive, EINVAL,
403 "Invalid index specified.");
404 return ARCHIVE_FATAL;
406 a->client.dataset[iindex].data = client_data;
407 a->client.dataset[iindex].begin_position = -1;
408 a->client.dataset[iindex].total_size = -1;
413 archive_read_add_callback_data(struct archive *_a, void *client_data,
416 struct archive_read *a = (struct archive_read *)_a;
420 archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
421 "archive_read_add_callback_data");
422 if (iindex > a->client.nodes) {
423 archive_set_error(&a->archive, EINVAL,
424 "Invalid index specified.");
425 return ARCHIVE_FATAL;
427 p = realloc(a->client.dataset, sizeof(*a->client.dataset)
428 * (++(a->client.nodes)));
430 archive_set_error(&a->archive, ENOMEM,
432 return ARCHIVE_FATAL;
434 a->client.dataset = (struct archive_read_data_node *)p;
435 for (i = a->client.nodes - 1; i > iindex && i > 0; i--) {
436 a->client.dataset[i].data = a->client.dataset[i-1].data;
437 a->client.dataset[i].begin_position = -1;
438 a->client.dataset[i].total_size = -1;
440 a->client.dataset[iindex].data = client_data;
441 a->client.dataset[iindex].begin_position = -1;
442 a->client.dataset[iindex].total_size = -1;
447 archive_read_append_callback_data(struct archive *_a, void *client_data)
449 struct archive_read *a = (struct archive_read *)_a;
450 return archive_read_add_callback_data(_a, client_data, a->client.nodes);
454 archive_read_prepend_callback_data(struct archive *_a, void *client_data)
456 return archive_read_add_callback_data(_a, client_data, 0);
460 archive_read_open1(struct archive *_a)
462 struct archive_read *a = (struct archive_read *)_a;
463 struct archive_read_filter *filter, *tmp;
464 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 if (a->client.closer) {
484 for (i = 0; i < a->client.nodes; i++)
485 (a->client.closer)(&a->archive,
486 a->client.dataset[i].data);
492 filter = calloc(1, sizeof(*filter));
494 return (ARCHIVE_FATAL);
495 filter->bidder = NULL;
496 filter->upstream = NULL;
498 filter->data = a->client.dataset[0].data;
499 filter->open = client_open_proxy;
500 filter->read = client_read_proxy;
501 filter->skip = client_skip_proxy;
502 filter->seek = client_seek_proxy;
503 filter->close = client_close_proxy;
504 filter->sswitch = client_switch_proxy;
505 filter->name = "none";
506 filter->code = ARCHIVE_FILTER_NONE;
508 a->client.dataset[0].begin_position = 0;
509 if (!a->filter || !a->bypass_filter_bidding)
512 /* Build out the input pipeline. */
513 e = choose_filters(a);
514 if (e < ARCHIVE_WARN) {
515 a->archive.state = ARCHIVE_STATE_FATAL;
516 return (ARCHIVE_FATAL);
521 /* Need to add "NONE" type filter at the end of the filter chain */
523 while (tmp->upstream)
525 tmp->upstream = filter;
530 slot = choose_format(a);
533 a->archive.state = ARCHIVE_STATE_FATAL;
534 return (ARCHIVE_FATAL);
536 a->format = &(a->formats[slot]);
539 a->archive.state = ARCHIVE_STATE_HEADER;
541 /* Ensure libarchive starts from the first node in a multivolume set */
542 client_switch_proxy(a->filter, 0);
547 * Allow each registered stream transform to bid on whether
548 * it wants to handle this stream. Repeat until we've finished
549 * building the pipeline.
552 /* We won't build a filter pipeline with more stages than this. */
553 #define MAX_NUMBER_FILTERS 25
556 choose_filters(struct archive_read *a)
558 int number_bidders, i, bid, best_bid, number_filters;
559 struct archive_read_filter_bidder *bidder, *best_bidder;
560 struct archive_read_filter *filter;
564 for (number_filters = 0; number_filters < MAX_NUMBER_FILTERS; ++number_filters) {
565 number_bidders = sizeof(a->bidders) / sizeof(a->bidders[0]);
571 for (i = 0; i < number_bidders; i++, bidder++) {
572 if (bidder->bid != NULL) {
573 bid = (bidder->bid)(bidder, a->filter);
574 if (bid > best_bid) {
576 best_bidder = bidder;
581 /* If no bidder, we're done. */
582 if (best_bidder == NULL) {
583 /* Verify the filter by asking it for some data. */
584 __archive_read_filter_ahead(a->filter, 1, &avail);
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_free_filters(a);
605 return (ARCHIVE_FATAL);
608 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
609 "Input requires too many filters for decoding");
610 return (ARCHIVE_FATAL);
614 * Read header of next entry.
617 _archive_read_next_header2(struct archive *_a, struct archive_entry *entry)
619 struct archive_read *a = (struct archive_read *)_a;
620 int r1 = ARCHIVE_OK, r2;
622 archive_check_magic(_a, ARCHIVE_READ_MAGIC,
623 ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
624 "archive_read_next_header");
626 archive_entry_clear(entry);
627 archive_clear_error(&a->archive);
630 * If client didn't consume entire data, skip any remainder
631 * (This is especially important for GNU incremental directories.)
633 if (a->archive.state == ARCHIVE_STATE_DATA) {
634 r1 = archive_read_data_skip(&a->archive);
635 if (r1 == ARCHIVE_EOF)
636 archive_set_error(&a->archive, EIO,
637 "Premature end-of-file.");
638 if (r1 == ARCHIVE_EOF || r1 == ARCHIVE_FATAL) {
639 a->archive.state = ARCHIVE_STATE_FATAL;
640 return (ARCHIVE_FATAL);
644 /* Record start-of-header offset in uncompressed stream. */
645 a->header_position = a->filter->position;
648 r2 = (a->format->read_header)(a, entry);
651 * EOF and FATAL are persistent at this layer. By
652 * modifying the state, we guarantee that future calls to
653 * read a header or read data will fail.
657 a->archive.state = ARCHIVE_STATE_EOF;
658 --_a->file_count;/* Revert a file counter. */
661 a->archive.state = ARCHIVE_STATE_DATA;
664 a->archive.state = ARCHIVE_STATE_DATA;
669 a->archive.state = ARCHIVE_STATE_FATAL;
673 __archive_reset_read_data(&a->archive);
675 a->data_start_node = a->client.cursor;
676 /* EOF always wins; otherwise return the worst error. */
677 return (r2 < r1 || r2 == ARCHIVE_EOF) ? r2 : r1;
681 _archive_read_next_header(struct archive *_a, struct archive_entry **entryp)
684 struct archive_read *a = (struct archive_read *)_a;
686 ret = _archive_read_next_header2(_a, a->entry);
692 * Allow each registered format to bid on whether it wants to handle
693 * the next entry. Return index of winning bidder.
696 choose_format(struct archive_read *a)
703 slots = sizeof(a->formats) / sizeof(a->formats[0]);
707 /* Set up a->format for convenience of bidders. */
708 a->format = &(a->formats[0]);
709 for (i = 0; i < slots; i++, a->format++) {
710 if (a->format->bid) {
711 bid = (a->format->bid)(a, best_bid);
712 if (bid == ARCHIVE_FATAL)
713 return (ARCHIVE_FATAL);
714 if (a->filter->position != 0)
715 __archive_read_seek(a, 0, SEEK_SET);
716 if ((bid > best_bid) || (best_bid_slot < 0)) {
724 * There were no bidders; this is a serious programmer error
725 * and demands a quick and definitive abort.
727 if (best_bid_slot < 0) {
728 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
729 "No formats registered");
730 return (ARCHIVE_FATAL);
734 * There were bidders, but no non-zero bids; this means we
735 * can't support this stream.
738 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
739 "Unrecognized archive format");
740 return (ARCHIVE_FATAL);
743 return (best_bid_slot);
747 * Return the file offset (within the uncompressed data stream) where
748 * the last header started.
751 archive_read_header_position(struct archive *_a)
753 struct archive_read *a = (struct archive_read *)_a;
754 archive_check_magic(_a, ARCHIVE_READ_MAGIC,
755 ARCHIVE_STATE_ANY, "archive_read_header_position");
756 return (a->header_position);
760 * Returns 1 if the archive contains at least one encrypted entry.
761 * If the archive format not support encryption at all
762 * ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED is returned.
763 * If for any other reason (e.g. not enough data read so far)
764 * we cannot say whether there are encrypted entries, then
765 * ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW is returned.
766 * In general, this function will return values below zero when the
767 * reader is uncertain or totally incapable of encryption support.
768 * When this function returns 0 you can be sure that the reader
769 * supports encryption detection but no encrypted entries have
772 * NOTE: If the metadata/header of an archive is also encrypted, you
773 * cannot rely on the number of encrypted entries. That is why this
774 * function does not return the number of encrypted entries but#
775 * just shows that there are some.
778 archive_read_has_encrypted_entries(struct archive *_a)
780 struct archive_read *a = (struct archive_read *)_a;
781 int format_supports_encryption = archive_read_format_capabilities(_a)
782 & (ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_DATA | ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_METADATA);
784 if (!_a || !format_supports_encryption) {
785 /* Format in general doesn't support encryption */
786 return ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED;
789 /* A reader potentially has read enough data now. */
790 if (a->format && a->format->has_encrypted_entries) {
791 return (a->format->has_encrypted_entries)(a);
794 /* For any other reason we cannot say how many entries are there. */
795 return ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW;
799 * Returns a bitmask of capabilities that are supported by the archive format reader.
800 * If the reader has no special capabilities, ARCHIVE_READ_FORMAT_CAPS_NONE is returned.
803 archive_read_format_capabilities(struct archive *_a)
805 struct archive_read *a = (struct archive_read *)_a;
806 if (a && a->format && a->format->format_capabilties) {
807 return (a->format->format_capabilties)(a);
809 return ARCHIVE_READ_FORMAT_CAPS_NONE;
813 * Read data from an archive entry, using a read(2)-style interface.
814 * This is a convenience routine that just calls
815 * archive_read_data_block and copies the results into the client
816 * buffer, filling any gaps with zero bytes. Clients using this
817 * API can be completely ignorant of sparse-file issues; sparse files
818 * will simply be padded with nulls.
820 * DO NOT intermingle calls to this function and archive_read_data_block
821 * to read a single entry body.
824 archive_read_data(struct archive *_a, void *buff, size_t s)
826 struct archive *a = (struct archive *)_a;
828 const void *read_buf;
837 if (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;
884 memcpy(dest, a->read_data_block, len);
886 a->read_data_block += len;
887 a->read_data_remaining -= len;
888 a->read_data_output_offset += len;
889 a->read_data_offset += len;
894 a->read_data_is_posix_read = 0;
895 a->read_data_requested = 0;
900 * Reset the read_data_* variables, used for starting a new entry.
902 void __archive_reset_read_data(struct archive * a)
904 a->read_data_output_offset = 0;
905 a->read_data_remaining = 0;
906 a->read_data_is_posix_read = 0;
907 a->read_data_requested = 0;
909 /* extra resets, from rar.c */
910 a->read_data_block = NULL;
911 a->read_data_offset = 0;
915 * Skip over all remaining data in this entry.
918 archive_read_data_skip(struct archive *_a)
920 struct archive_read *a = (struct archive_read *)_a;
926 archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA,
927 "archive_read_data_skip");
929 if (a->format->read_data_skip != NULL)
930 r = (a->format->read_data_skip)(a);
932 while ((r = archive_read_data_block(&a->archive,
933 &buff, &size, &offset))
938 if (r == ARCHIVE_EOF)
941 a->archive.state = ARCHIVE_STATE_HEADER;
946 archive_seek_data(struct archive *_a, int64_t offset, int whence)
948 struct archive_read *a = (struct archive_read *)_a;
949 archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA,
950 "archive_seek_data_block");
952 if (a->format->seek_data == NULL) {
953 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
955 "No format_seek_data_block function registered");
956 return (ARCHIVE_FATAL);
959 return (a->format->seek_data)(a, offset, whence);
963 * Read the next block of entry data from the archive.
964 * This is a zero-copy interface; the client receives a pointer,
965 * size, and file offset of the next available block of data.
967 * Returns ARCHIVE_OK if the operation is successful, ARCHIVE_EOF if
968 * the end of entry is encountered.
971 _archive_read_data_block(struct archive *_a,
972 const void **buff, size_t *size, int64_t *offset)
974 struct archive_read *a = (struct archive_read *)_a;
975 archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA,
976 "archive_read_data_block");
978 if (a->format->read_data == NULL) {
979 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
981 "No format->read_data function registered");
982 return (ARCHIVE_FATAL);
985 return (a->format->read_data)(a, buff, size, offset);
989 close_filters(struct archive_read *a)
991 struct archive_read_filter *f = a->filter;
993 /* Close each filter in the pipeline. */
995 struct archive_read_filter *t = f->upstream;
996 if (!f->closed && f->close != NULL) {
997 int r1 = (f->close)(f);
1010 __archive_read_free_filters(struct archive_read *a)
1012 /* Make sure filters are closed and their buffers are freed */
1015 while (a->filter != NULL) {
1016 struct archive_read_filter *t = a->filter->upstream;
1023 * return the count of # of filters in use
1026 _archive_filter_count(struct archive *_a)
1028 struct archive_read *a = (struct archive_read *)_a;
1029 struct archive_read_filter *p = a->filter;
1039 * Close the file and all I/O.
1042 _archive_read_close(struct archive *_a)
1044 struct archive_read *a = (struct archive_read *)_a;
1045 int r = ARCHIVE_OK, r1 = ARCHIVE_OK;
1047 archive_check_magic(&a->archive, ARCHIVE_READ_MAGIC,
1048 ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_read_close");
1049 if (a->archive.state == ARCHIVE_STATE_CLOSED)
1050 return (ARCHIVE_OK);
1051 archive_clear_error(&a->archive);
1052 a->archive.state = ARCHIVE_STATE_CLOSED;
1054 /* TODO: Clean up the formatters. */
1056 /* Release the filter objects. */
1057 r1 = close_filters(a);
1065 * Release memory and other resources.
1068 _archive_read_free(struct archive *_a)
1070 struct archive_read *a = (struct archive_read *)_a;
1071 struct archive_read_passphrase *p;
1077 return (ARCHIVE_OK);
1078 archive_check_magic(_a, ARCHIVE_READ_MAGIC,
1079 ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_read_free");
1080 if (a->archive.state != ARCHIVE_STATE_CLOSED
1081 && a->archive.state != ARCHIVE_STATE_FATAL)
1082 r = archive_read_close(&a->archive);
1084 /* Call cleanup functions registered by optional components. */
1085 if (a->cleanup_archive_extract != NULL)
1086 r = (a->cleanup_archive_extract)(a);
1088 /* Cleanup format-specific data. */
1089 slots = sizeof(a->formats) / sizeof(a->formats[0]);
1090 for (i = 0; i < slots; i++) {
1091 a->format = &(a->formats[i]);
1092 if (a->formats[i].cleanup)
1093 (a->formats[i].cleanup)(a);
1096 /* Free the filters */
1097 __archive_read_free_filters(a);
1099 /* Release the bidder objects. */
1100 n = sizeof(a->bidders)/sizeof(a->bidders[0]);
1101 for (i = 0; i < n; i++) {
1102 if (a->bidders[i].free != NULL) {
1103 int r1 = (a->bidders[i].free)(&a->bidders[i]);
1109 /* Release passphrase list. */
1110 p = a->passphrases.first;
1112 struct archive_read_passphrase *np = p->next;
1114 /* A passphrase should be cleaned. */
1115 memset(p->passphrase, 0, strlen(p->passphrase));
1116 free(p->passphrase);
1121 archive_string_free(&a->archive.error_string);
1122 archive_entry_free(a->entry);
1123 a->archive.magic = 0;
1124 __archive_clean(&a->archive);
1125 free(a->client.dataset);
1130 static struct archive_read_filter *
1131 get_filter(struct archive *_a, int n)
1133 struct archive_read *a = (struct archive_read *)_a;
1134 struct archive_read_filter *f = a->filter;
1135 /* We use n == -1 for 'the last filter', which is always the
1137 if (n == -1 && f != NULL) {
1138 struct archive_read_filter *last = f;
1148 while (n > 0 && f != NULL) {
1156 _archive_filter_code(struct archive *_a, int n)
1158 struct archive_read_filter *f = get_filter(_a, n);
1159 return f == NULL ? -1 : f->code;
1163 _archive_filter_name(struct archive *_a, int n)
1165 struct archive_read_filter *f = get_filter(_a, n);
1166 return f != NULL ? f->name : NULL;
1170 _archive_filter_bytes(struct archive *_a, int n)
1172 struct archive_read_filter *f = get_filter(_a, n);
1173 return f == NULL ? -1 : f->position;
1177 * Used internally by read format handlers to register their bid and
1178 * initialization functions.
1181 __archive_read_register_format(struct archive_read *a,
1184 int (*bid)(struct archive_read *, int),
1185 int (*options)(struct archive_read *, const char *, const char *),
1186 int (*read_header)(struct archive_read *, struct archive_entry *),
1187 int (*read_data)(struct archive_read *, const void **, size_t *, int64_t *),
1188 int (*read_data_skip)(struct archive_read *),
1189 int64_t (*seek_data)(struct archive_read *, int64_t, int),
1190 int (*cleanup)(struct archive_read *),
1191 int (*format_capabilities)(struct archive_read *),
1192 int (*has_encrypted_entries)(struct archive_read *))
1194 int i, number_slots;
1196 archive_check_magic(&a->archive,
1197 ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
1198 "__archive_read_register_format");
1200 number_slots = sizeof(a->formats) / sizeof(a->formats[0]);
1202 for (i = 0; i < number_slots; i++) {
1203 if (a->formats[i].bid == bid)
1204 return (ARCHIVE_WARN); /* We've already installed */
1205 if (a->formats[i].bid == NULL) {
1206 a->formats[i].bid = bid;
1207 a->formats[i].options = options;
1208 a->formats[i].read_header = read_header;
1209 a->formats[i].read_data = read_data;
1210 a->formats[i].read_data_skip = read_data_skip;
1211 a->formats[i].seek_data = seek_data;
1212 a->formats[i].cleanup = cleanup;
1213 a->formats[i].data = format_data;
1214 a->formats[i].name = name;
1215 a->formats[i].format_capabilties = format_capabilities;
1216 a->formats[i].has_encrypted_entries = has_encrypted_entries;
1217 return (ARCHIVE_OK);
1221 archive_set_error(&a->archive, ENOMEM,
1222 "Not enough slots for format registration");
1223 return (ARCHIVE_FATAL);
1227 * Used internally by decompression routines to register their bid and
1228 * initialization functions.
1231 __archive_read_get_bidder(struct archive_read *a,
1232 struct archive_read_filter_bidder **bidder)
1234 int i, number_slots;
1236 number_slots = sizeof(a->bidders) / sizeof(a->bidders[0]);
1238 for (i = 0; i < number_slots; i++) {
1239 if (a->bidders[i].bid == NULL) {
1240 memset(a->bidders + i, 0, sizeof(a->bidders[0]));
1241 *bidder = (a->bidders + i);
1242 return (ARCHIVE_OK);
1246 archive_set_error(&a->archive, ENOMEM,
1247 "Not enough slots for filter registration");
1248 return (ARCHIVE_FATAL);
1252 * The next section implements the peek/consume internal I/O
1253 * system used by archive readers. This system allows simple
1254 * read-ahead for consumers while preserving zero-copy operation
1257 * The two key operations:
1258 * * The read-ahead function returns a pointer to a block of data
1259 * that satisfies a minimum request.
1260 * * The consume function advances the file pointer.
1262 * In the ideal case, filters generate blocks of data
1263 * and __archive_read_ahead() just returns pointers directly into
1264 * those blocks. Then __archive_read_consume() just bumps those
1265 * pointers. Only if your request would span blocks does the I/O
1266 * layer use a copy buffer to provide you with a contiguous block of
1269 * A couple of useful idioms:
1270 * * "I just want some data." Ask for 1 byte and pay attention to
1271 * the "number of bytes available" from __archive_read_ahead().
1272 * Consume whatever you actually use.
1273 * * "I want to output a large block of data." As above, ask for 1 byte,
1274 * emit all that's available (up to whatever limit you have), consume
1275 * it all, then repeat until you're done. This effectively means that
1276 * you're passing along the blocks that came from your provider.
1277 * * "I want to peek ahead by a large amount." Ask for 4k or so, then
1278 * double and repeat until you get an error or have enough. Note
1279 * that the I/O layer will likely end up expanding its copy buffer
1280 * to fit your request, so use this technique cautiously. This
1281 * technique is used, for example, by some of the format tasting
1282 * code that has uncertain look-ahead needs.
1286 * Looks ahead in the input stream:
1287 * * If 'avail' pointer is provided, that returns number of bytes available
1288 * in the current buffer, which may be much larger than requested.
1289 * * If end-of-file, *avail gets set to zero.
1290 * * If error, *avail gets error code.
1291 * * If request can be met, returns pointer to data.
1292 * * If minimum request cannot be met, returns NULL.
1294 * Note: If you just want "some data", ask for 1 byte and pay attention
1295 * to *avail, which will have the actual amount available. If you
1296 * know exactly how many bytes you need, just ask for that and treat
1297 * a NULL return as an error.
1299 * Important: This does NOT move the file pointer. See
1300 * __archive_read_consume() below.
1303 __archive_read_ahead(struct archive_read *a, size_t min, ssize_t *avail)
1305 return (__archive_read_filter_ahead(a->filter, min, avail));
1309 __archive_read_filter_ahead(struct archive_read_filter *filter,
1310 size_t min, ssize_t *avail)
1315 if (filter->fatal) {
1317 *avail = ARCHIVE_FATAL;
1322 * Keep pulling more data until we can satisfy the request.
1327 * If we can satisfy from the copy buffer (and the
1328 * copy buffer isn't empty), we're done. In particular,
1329 * note that min == 0 is a perfectly well-defined
1332 if (filter->avail >= min && filter->avail > 0) {
1334 *avail = filter->avail;
1335 return (filter->next);
1339 * We can satisfy directly from client buffer if everything
1340 * currently in the copy buffer is still in the client buffer.
1342 if (filter->client_total >= filter->client_avail + filter->avail
1343 && filter->client_avail + filter->avail >= min) {
1344 /* "Roll back" to client buffer. */
1345 filter->client_avail += filter->avail;
1346 filter->client_next -= filter->avail;
1347 /* Copy buffer is now empty. */
1349 filter->next = filter->buffer;
1350 /* Return data from client buffer. */
1352 *avail = filter->client_avail;
1353 return (filter->client_next);
1356 /* Move data forward in copy buffer if necessary. */
1357 if (filter->next > filter->buffer &&
1358 filter->next + min > filter->buffer + filter->buffer_size) {
1359 if (filter->avail > 0)
1360 memmove(filter->buffer, filter->next,
1362 filter->next = filter->buffer;
1365 /* If we've used up the client data, get more. */
1366 if (filter->client_avail <= 0) {
1367 if (filter->end_of_file) {
1372 bytes_read = (filter->read)(filter,
1373 &filter->client_buff);
1374 if (bytes_read < 0) { /* Read error. */
1375 filter->client_total = filter->client_avail = 0;
1376 filter->client_next =
1377 filter->client_buff = NULL;
1380 *avail = ARCHIVE_FATAL;
1383 if (bytes_read == 0) {
1384 /* Check for another client object first */
1385 if (filter->archive->client.cursor !=
1386 filter->archive->client.nodes - 1) {
1387 if (client_switch_proxy(filter,
1388 filter->archive->client.cursor + 1)
1392 /* Premature end-of-file. */
1393 filter->client_total = filter->client_avail = 0;
1394 filter->client_next =
1395 filter->client_buff = NULL;
1396 filter->end_of_file = 1;
1397 /* Return whatever we do have. */
1399 *avail = filter->avail;
1402 filter->client_total = bytes_read;
1403 filter->client_avail = filter->client_total;
1404 filter->client_next = filter->client_buff;
1407 * We can't satisfy the request from the copy
1408 * buffer or the existing client data, so we
1409 * need to copy more client data over to the
1413 /* Ensure the buffer is big enough. */
1414 if (min > filter->buffer_size) {
1418 /* Double the buffer; watch for overflow. */
1419 s = t = filter->buffer_size;
1424 if (t <= s) { /* Integer overflow! */
1426 &filter->archive->archive,
1428 "Unable to allocate copy"
1432 *avail = ARCHIVE_FATAL;
1437 /* Now s >= min, so allocate a new buffer. */
1438 p = (char *)malloc(s);
1441 &filter->archive->archive,
1443 "Unable to allocate copy buffer");
1446 *avail = ARCHIVE_FATAL;
1449 /* Move data into newly-enlarged buffer. */
1450 if (filter->avail > 0)
1451 memmove(p, filter->next, filter->avail);
1452 free(filter->buffer);
1453 filter->next = filter->buffer = p;
1454 filter->buffer_size = s;
1457 /* We can add client data to copy buffer. */
1458 /* First estimate: copy to fill rest of buffer. */
1459 tocopy = (filter->buffer + filter->buffer_size)
1460 - (filter->next + filter->avail);
1461 /* Don't waste time buffering more than we need to. */
1462 if (tocopy + filter->avail > min)
1463 tocopy = min - filter->avail;
1464 /* Don't copy more than is available. */
1465 if (tocopy > filter->client_avail)
1466 tocopy = filter->client_avail;
1468 memcpy(filter->next + filter->avail,
1469 filter->client_next, tocopy);
1470 /* Remove this data from client buffer. */
1471 filter->client_next += tocopy;
1472 filter->client_avail -= tocopy;
1473 /* add it to copy buffer. */
1474 filter->avail += tocopy;
1480 * Move the file pointer forward.
1483 __archive_read_consume(struct archive_read *a, int64_t request)
1485 return (__archive_read_filter_consume(a->filter, request));
1489 __archive_read_filter_consume(struct archive_read_filter * filter,
1495 return ARCHIVE_FATAL;
1499 skipped = advance_file_pointer(filter, request);
1500 if (skipped == request)
1502 /* We hit EOF before we satisfied the skip request. */
1503 if (skipped < 0) /* Map error code to 0 for error message below. */
1505 archive_set_error(&filter->archive->archive,
1507 "Truncated input file (needed %jd bytes, only %jd available)",
1508 (intmax_t)request, (intmax_t)skipped);
1509 return (ARCHIVE_FATAL);
1513 * Advance the file pointer by the amount requested.
1514 * Returns the amount actually advanced, which may be less than the
1515 * request if EOF is encountered first.
1516 * Returns a negative value if there's an I/O error.
1519 advance_file_pointer(struct archive_read_filter *filter, int64_t request)
1521 int64_t bytes_skipped, total_bytes_skipped = 0;
1528 /* Use up the copy buffer first. */
1529 if (filter->avail > 0) {
1530 min = (size_t)minimum(request, (int64_t)filter->avail);
1531 filter->next += min;
1532 filter->avail -= min;
1534 filter->position += min;
1535 total_bytes_skipped += min;
1538 /* Then use up the client buffer. */
1539 if (filter->client_avail > 0) {
1540 min = (size_t)minimum(request, (int64_t)filter->client_avail);
1541 filter->client_next += min;
1542 filter->client_avail -= min;
1544 filter->position += min;
1545 total_bytes_skipped += min;
1548 return (total_bytes_skipped);
1550 /* If there's an optimized skip function, use it. */
1551 if (filter->skip != NULL) {
1552 bytes_skipped = (filter->skip)(filter, request);
1553 if (bytes_skipped < 0) { /* error */
1555 return (bytes_skipped);
1557 filter->position += bytes_skipped;
1558 total_bytes_skipped += bytes_skipped;
1559 request -= bytes_skipped;
1561 return (total_bytes_skipped);
1564 /* Use ordinary reads as necessary to complete the request. */
1566 bytes_read = (filter->read)(filter, &filter->client_buff);
1567 if (bytes_read < 0) {
1568 filter->client_buff = NULL;
1570 return (bytes_read);
1573 if (bytes_read == 0) {
1574 if (filter->archive->client.cursor !=
1575 filter->archive->client.nodes - 1) {
1576 if (client_switch_proxy(filter,
1577 filter->archive->client.cursor + 1)
1581 filter->client_buff = NULL;
1582 filter->end_of_file = 1;
1583 return (total_bytes_skipped);
1586 if (bytes_read >= request) {
1587 filter->client_next =
1588 ((const char *)filter->client_buff) + request;
1589 filter->client_avail = (size_t)(bytes_read - request);
1590 filter->client_total = bytes_read;
1591 total_bytes_skipped += request;
1592 filter->position += request;
1593 return (total_bytes_skipped);
1596 filter->position += bytes_read;
1597 total_bytes_skipped += bytes_read;
1598 request -= bytes_read;
1603 * Returns ARCHIVE_FAILED if seeking isn't supported.
1606 __archive_read_seek(struct archive_read *a, int64_t offset, int whence)
1608 return __archive_read_filter_seek(a->filter, offset, whence);
1612 __archive_read_filter_seek(struct archive_read_filter *filter, int64_t offset,
1615 struct archive_read_client *client;
1617 unsigned int cursor;
1619 if (filter->closed || filter->fatal)
1620 return (ARCHIVE_FATAL);
1621 if (filter->seek == NULL)
1622 return (ARCHIVE_FAILED);
1624 client = &(filter->archive->client);
1627 /* Adjust the offset and use SEEK_SET instead */
1628 offset += filter->position;
1633 if (client->dataset[cursor].begin_position < 0 ||
1634 client->dataset[cursor].total_size < 0 ||
1635 client->dataset[cursor].begin_position +
1636 client->dataset[cursor].total_size - 1 > offset ||
1637 cursor + 1 >= client->nodes)
1639 r = client->dataset[cursor].begin_position +
1640 client->dataset[cursor].total_size;
1641 client->dataset[++cursor].begin_position = r;
1644 r = client_switch_proxy(filter, cursor);
1645 if (r != ARCHIVE_OK)
1647 if ((r = client_seek_proxy(filter, 0, SEEK_END)) < 0)
1649 client->dataset[cursor].total_size = r;
1650 if (client->dataset[cursor].begin_position +
1651 client->dataset[cursor].total_size - 1 > offset ||
1652 cursor + 1 >= client->nodes)
1654 r = client->dataset[cursor].begin_position +
1655 client->dataset[cursor].total_size;
1656 client->dataset[++cursor].begin_position = r;
1658 offset -= client->dataset[cursor].begin_position;
1660 || offset > client->dataset[cursor].total_size)
1661 return ARCHIVE_FATAL;
1662 if ((r = client_seek_proxy(filter, offset, SEEK_SET)) < 0)
1669 if (client->dataset[cursor].begin_position < 0 ||
1670 client->dataset[cursor].total_size < 0 ||
1671 cursor + 1 >= client->nodes)
1673 r = client->dataset[cursor].begin_position +
1674 client->dataset[cursor].total_size;
1675 client->dataset[++cursor].begin_position = r;
1678 r = client_switch_proxy(filter, cursor);
1679 if (r != ARCHIVE_OK)
1681 if ((r = client_seek_proxy(filter, 0, SEEK_END)) < 0)
1683 client->dataset[cursor].total_size = r;
1684 r = client->dataset[cursor].begin_position +
1685 client->dataset[cursor].total_size;
1686 if (cursor + 1 >= client->nodes)
1688 client->dataset[++cursor].begin_position = r;
1692 client->dataset[cursor].begin_position)
1694 offset += client->dataset[cursor].total_size;
1698 r = client->dataset[cursor].begin_position +
1699 client->dataset[cursor].total_size;
1701 offset = (r + offset) - client->dataset[cursor].begin_position;
1702 if ((r = client_switch_proxy(filter, cursor)) != ARCHIVE_OK)
1704 r = client_seek_proxy(filter, offset, SEEK_SET);
1710 return (ARCHIVE_FATAL);
1712 r += client->dataset[cursor].begin_position;
1716 * Ouch. Clearing the buffer like this hurts, especially
1717 * at bid time. A lot of our efficiency at bid time comes
1718 * from having bidders reuse the data we've already read.
1720 * TODO: If the seek request is in data we already
1721 * have, then don't call the seek callback.
1723 * TODO: Zip seeks to end-of-file at bid time. If
1724 * other formats also start doing this, we may need to
1725 * find a way for clients to fudge the seek offset to
1728 * Hmmm... If whence was SEEK_END, we know the file
1729 * size is (r - offset). Can we use that to simplify
1730 * the TODO items above?
1732 filter->avail = filter->client_avail = 0;
1733 filter->next = filter->buffer;
1734 filter->position = r;
1735 filter->end_of_file = 0;