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 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 *)malloc(sizeof(*a));
107 memset(a, 0, 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 return (&a->archive);
118 * Record the do-not-extract-to file. This belongs in archive_read_extract.c.
121 archive_read_extract_set_skip_file(struct archive *_a, int64_t d, int64_t i)
123 struct archive_read *a = (struct archive_read *)_a;
125 if (ARCHIVE_OK != __archive_check_magic(_a, ARCHIVE_READ_MAGIC,
126 ARCHIVE_STATE_ANY, "archive_read_extract_set_skip_file"))
128 a->skip_file_set = 1;
129 a->skip_file_dev = d;
130 a->skip_file_ino = i;
137 archive_read_open(struct archive *a, void *client_data,
138 archive_open_callback *client_opener, archive_read_callback *client_reader,
139 archive_close_callback *client_closer)
141 /* Old archive_read_open() is just a thin shell around
142 * archive_read_open1. */
143 archive_read_set_open_callback(a, client_opener);
144 archive_read_set_read_callback(a, client_reader);
145 archive_read_set_close_callback(a, client_closer);
146 archive_read_set_callback_data(a, client_data);
147 return archive_read_open1(a);
152 archive_read_open2(struct archive *a, void *client_data,
153 archive_open_callback *client_opener,
154 archive_read_callback *client_reader,
155 archive_skip_callback *client_skipper,
156 archive_close_callback *client_closer)
158 /* Old archive_read_open2() is just a thin shell around
159 * archive_read_open1. */
160 archive_read_set_callback_data(a, client_data);
161 archive_read_set_open_callback(a, client_opener);
162 archive_read_set_read_callback(a, client_reader);
163 archive_read_set_skip_callback(a, client_skipper);
164 archive_read_set_close_callback(a, client_closer);
165 return archive_read_open1(a);
169 client_read_proxy(struct archive_read_filter *self, const void **buff)
172 r = (self->archive->client.reader)(&self->archive->archive,
178 client_skip_proxy(struct archive_read_filter *self, int64_t request)
181 __archive_errx(1, "Negative skip requested.");
185 if (self->archive->client.skipper != NULL) {
186 /* Seek requests over 1GiB are broken down into
187 * multiple seeks. This avoids overflows when the
188 * requests get passed through 32-bit arguments. */
189 int64_t skip_limit = (int64_t)1 << 30;
192 int64_t get, ask = request;
193 if (ask > skip_limit)
195 get = (self->archive->client.skipper)
196 (&self->archive->archive, self->data, ask);
202 } else if (self->archive->client.seeker != NULL
203 && request > 64 * 1024) {
204 /* If the client provided a seeker but not a skipper,
205 * we can use the seeker to skip forward.
207 * Note: This isn't always a good idea. The client
208 * skipper is allowed to skip by less than requested
209 * if it needs to maintain block alignment. The
210 * seeker is not allowed to play such games, so using
211 * the seeker here may be a performance loss compared
212 * to just reading and discarding. That's why we
213 * only do this for skips of over 64k.
215 int64_t before = self->position;
216 int64_t after = (self->archive->client.seeker)
217 (&self->archive->archive, self->data, request, SEEK_CUR);
218 if (after != before + request)
219 return ARCHIVE_FATAL;
220 return after - before;
226 client_seek_proxy(struct archive_read_filter *self, int64_t offset, int whence)
228 /* DO NOT use the skipper here! If we transparently handled
229 * forward seek here by using the skipper, that will break
230 * other libarchive code that assumes a successful forward
231 * seek means it can also seek backwards.
233 if (self->archive->client.seeker == NULL)
234 return (ARCHIVE_FAILED);
235 return (self->archive->client.seeker)(&self->archive->archive,
236 self->data, offset, whence);
240 client_close_proxy(struct archive_read_filter *self)
242 int r = ARCHIVE_OK, r2;
245 if (self->archive->client.closer == NULL)
247 for (i = 0; i < self->archive->client.nodes; i++)
249 r2 = (self->archive->client.closer)
250 ((struct archive *)self->archive,
251 self->archive->client.dataset[i].data);
259 client_open_proxy(struct archive_read_filter *self)
262 if (self->archive->client.opener != NULL)
263 r = (self->archive->client.opener)(
264 (struct archive *)self->archive, self->data);
269 client_switch_proxy(struct archive_read_filter *self, unsigned int iindex)
271 int r1 = ARCHIVE_OK, r2 = ARCHIVE_OK;
274 /* Don't do anything if already in the specified data node */
275 if (self->archive->client.cursor == iindex)
278 self->archive->client.cursor = iindex;
279 data2 = self->archive->client.dataset[self->archive->client.cursor].data;
280 if (self->archive->client.switcher != NULL)
282 r1 = r2 = (self->archive->client.switcher)
283 ((struct archive *)self->archive, self->data, data2);
288 /* Attempt to call close and open instead */
289 if (self->archive->client.closer != NULL)
290 r1 = (self->archive->client.closer)
291 ((struct archive *)self->archive, self->data);
293 if (self->archive->client.opener != NULL)
294 r2 = (self->archive->client.opener)
295 ((struct archive *)self->archive, self->data);
297 return (r1 < r2) ? r1 : r2;
301 archive_read_set_open_callback(struct archive *_a,
302 archive_open_callback *client_opener)
304 struct archive_read *a = (struct archive_read *)_a;
305 archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
306 "archive_read_set_open_callback");
307 a->client.opener = client_opener;
312 archive_read_set_read_callback(struct archive *_a,
313 archive_read_callback *client_reader)
315 struct archive_read *a = (struct archive_read *)_a;
316 archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
317 "archive_read_set_read_callback");
318 a->client.reader = client_reader;
323 archive_read_set_skip_callback(struct archive *_a,
324 archive_skip_callback *client_skipper)
326 struct archive_read *a = (struct archive_read *)_a;
327 archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
328 "archive_read_set_skip_callback");
329 a->client.skipper = client_skipper;
334 archive_read_set_seek_callback(struct archive *_a,
335 archive_seek_callback *client_seeker)
337 struct archive_read *a = (struct archive_read *)_a;
338 archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
339 "archive_read_set_seek_callback");
340 a->client.seeker = client_seeker;
345 archive_read_set_close_callback(struct archive *_a,
346 archive_close_callback *client_closer)
348 struct archive_read *a = (struct archive_read *)_a;
349 archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
350 "archive_read_set_close_callback");
351 a->client.closer = client_closer;
356 archive_read_set_switch_callback(struct archive *_a,
357 archive_switch_callback *client_switcher)
359 struct archive_read *a = (struct archive_read *)_a;
360 archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
361 "archive_read_set_switch_callback");
362 a->client.switcher = client_switcher;
367 archive_read_set_callback_data(struct archive *_a, void *client_data)
369 return archive_read_set_callback_data2(_a, client_data, 0);
373 archive_read_set_callback_data2(struct archive *_a, void *client_data,
376 struct archive_read *a = (struct archive_read *)_a;
377 archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
378 "archive_read_set_callback_data2");
380 if (a->client.nodes == 0)
382 a->client.dataset = (struct archive_read_data_node *)
383 calloc(1, sizeof(*a->client.dataset));
384 if (a->client.dataset == NULL)
386 archive_set_error(&a->archive, ENOMEM,
388 return ARCHIVE_FATAL;
393 if (iindex > a->client.nodes - 1)
395 archive_set_error(&a->archive, EINVAL,
396 "Invalid index specified.");
397 return ARCHIVE_FATAL;
399 a->client.dataset[iindex].data = client_data;
400 a->client.dataset[iindex].begin_position = -1;
401 a->client.dataset[iindex].total_size = -1;
406 archive_read_add_callback_data(struct archive *_a, void *client_data,
409 struct archive_read *a = (struct archive_read *)_a;
413 archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
414 "archive_read_add_callback_data");
415 if (iindex > a->client.nodes) {
416 archive_set_error(&a->archive, EINVAL,
417 "Invalid index specified.");
418 return ARCHIVE_FATAL;
420 p = realloc(a->client.dataset, sizeof(*a->client.dataset)
421 * (++(a->client.nodes)));
423 archive_set_error(&a->archive, ENOMEM,
425 return ARCHIVE_FATAL;
427 a->client.dataset = (struct archive_read_data_node *)p;
428 for (i = a->client.nodes - 1; i > iindex && i > 0; i--) {
429 a->client.dataset[i].data = a->client.dataset[i-1].data;
430 a->client.dataset[i].begin_position = -1;
431 a->client.dataset[i].total_size = -1;
433 a->client.dataset[iindex].data = client_data;
434 a->client.dataset[iindex].begin_position = -1;
435 a->client.dataset[iindex].total_size = -1;
440 archive_read_append_callback_data(struct archive *_a, void *client_data)
442 struct archive_read *a = (struct archive_read *)_a;
443 return archive_read_add_callback_data(_a, client_data, a->client.nodes);
447 archive_read_prepend_callback_data(struct archive *_a, void *client_data)
449 return archive_read_add_callback_data(_a, client_data, 0);
453 archive_read_open1(struct archive *_a)
455 struct archive_read *a = (struct archive_read *)_a;
456 struct archive_read_filter *filter, *tmp;
460 archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
461 "archive_read_open");
462 archive_clear_error(&a->archive);
464 if (a->client.reader == NULL) {
465 archive_set_error(&a->archive, EINVAL,
466 "No reader function provided to archive_read_open");
467 a->archive.state = ARCHIVE_STATE_FATAL;
468 return (ARCHIVE_FATAL);
471 /* Open data source. */
472 if (a->client.opener != NULL) {
473 e = (a->client.opener)(&a->archive, a->client.dataset[0].data);
475 /* If the open failed, call the closer to clean up. */
476 if (a->client.closer) {
477 for (i = 0; i < a->client.nodes; i++)
478 (a->client.closer)(&a->archive,
479 a->client.dataset[i].data);
485 filter = calloc(1, sizeof(*filter));
487 return (ARCHIVE_FATAL);
488 filter->bidder = NULL;
489 filter->upstream = NULL;
491 filter->data = a->client.dataset[0].data;
492 filter->open = client_open_proxy;
493 filter->read = client_read_proxy;
494 filter->skip = client_skip_proxy;
495 filter->seek = client_seek_proxy;
496 filter->close = client_close_proxy;
497 filter->sswitch = client_switch_proxy;
498 filter->name = "none";
499 filter->code = ARCHIVE_FILTER_NONE;
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);
525 __archive_read_close_filters(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 choose_filters(struct archive_read *a)
547 int number_bidders, i, bid, best_bid;
548 struct archive_read_filter_bidder *bidder, *best_bidder;
549 struct archive_read_filter *filter;
554 number_bidders = sizeof(a->bidders) / sizeof(a->bidders[0]);
560 for (i = 0; i < number_bidders; i++, bidder++) {
561 if (bidder->bid != NULL) {
562 bid = (bidder->bid)(bidder, a->filter);
563 if (bid > best_bid) {
565 best_bidder = bidder;
570 /* If no bidder, we're done. */
571 if (best_bidder == NULL) {
572 /* Verify the filter by asking it for some data. */
573 __archive_read_filter_ahead(a->filter, 1, &avail);
575 __archive_read_close_filters(a);
576 __archive_read_free_filters(a);
577 return (ARCHIVE_FATAL);
579 a->archive.compression_name = a->filter->name;
580 a->archive.compression_code = a->filter->code;
585 = (struct archive_read_filter *)calloc(1, sizeof(*filter));
587 return (ARCHIVE_FATAL);
588 filter->bidder = best_bidder;
590 filter->upstream = a->filter;
592 r = (best_bidder->init)(a->filter);
593 if (r != ARCHIVE_OK) {
594 __archive_read_close_filters(a);
595 __archive_read_free_filters(a);
596 return (ARCHIVE_FATAL);
602 * Read header of next entry.
605 _archive_read_next_header2(struct archive *_a, struct archive_entry *entry)
607 struct archive_read *a = (struct archive_read *)_a;
608 int r1 = ARCHIVE_OK, r2;
610 archive_check_magic(_a, ARCHIVE_READ_MAGIC,
611 ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
612 "archive_read_next_header");
614 archive_entry_clear(entry);
615 archive_clear_error(&a->archive);
618 * If client didn't consume entire data, skip any remainder
619 * (This is especially important for GNU incremental directories.)
621 if (a->archive.state == ARCHIVE_STATE_DATA) {
622 r1 = archive_read_data_skip(&a->archive);
623 if (r1 == ARCHIVE_EOF)
624 archive_set_error(&a->archive, EIO,
625 "Premature end-of-file.");
626 if (r1 == ARCHIVE_EOF || r1 == ARCHIVE_FATAL) {
627 a->archive.state = ARCHIVE_STATE_FATAL;
628 return (ARCHIVE_FATAL);
632 /* Record start-of-header offset in uncompressed stream. */
633 a->header_position = a->filter->position;
636 r2 = (a->format->read_header)(a, entry);
639 * EOF and FATAL are persistent at this layer. By
640 * modifying the state, we guarantee that future calls to
641 * read a header or read data will fail.
645 a->archive.state = ARCHIVE_STATE_EOF;
646 --_a->file_count;/* Revert a file counter. */
649 a->archive.state = ARCHIVE_STATE_DATA;
652 a->archive.state = ARCHIVE_STATE_DATA;
657 a->archive.state = ARCHIVE_STATE_FATAL;
661 a->read_data_output_offset = 0;
662 a->read_data_remaining = 0;
663 a->read_data_is_posix_read = 0;
664 a->read_data_requested = 0;
665 a->data_start_node = a->client.cursor;
666 /* EOF always wins; otherwise return the worst error. */
667 return (r2 < r1 || r2 == ARCHIVE_EOF) ? r2 : r1;
671 _archive_read_next_header(struct archive *_a, struct archive_entry **entryp)
674 struct archive_read *a = (struct archive_read *)_a;
676 ret = _archive_read_next_header2(_a, a->entry);
682 * Allow each registered format to bid on whether it wants to handle
683 * the next entry. Return index of winning bidder.
686 choose_format(struct archive_read *a)
693 slots = sizeof(a->formats) / sizeof(a->formats[0]);
697 /* Set up a->format for convenience of bidders. */
698 a->format = &(a->formats[0]);
699 for (i = 0; i < slots; i++, a->format++) {
700 if (a->format->bid) {
701 bid = (a->format->bid)(a, best_bid);
702 if (bid == ARCHIVE_FATAL)
703 return (ARCHIVE_FATAL);
704 if (a->filter->position != 0)
705 __archive_read_seek(a, 0, SEEK_SET);
706 if ((bid > best_bid) || (best_bid_slot < 0)) {
714 * There were no bidders; this is a serious programmer error
715 * and demands a quick and definitive abort.
717 if (best_bid_slot < 0) {
718 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
719 "No formats registered");
720 return (ARCHIVE_FATAL);
724 * There were bidders, but no non-zero bids; this means we
725 * can't support this stream.
728 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
729 "Unrecognized archive format");
730 return (ARCHIVE_FATAL);
733 return (best_bid_slot);
737 * Return the file offset (within the uncompressed data stream) where
738 * the last header started.
741 archive_read_header_position(struct archive *_a)
743 struct archive_read *a = (struct archive_read *)_a;
744 archive_check_magic(_a, ARCHIVE_READ_MAGIC,
745 ARCHIVE_STATE_ANY, "archive_read_header_position");
746 return (a->header_position);
750 * Read data from an archive entry, using a read(2)-style interface.
751 * This is a convenience routine that just calls
752 * archive_read_data_block and copies the results into the client
753 * buffer, filling any gaps with zero bytes. Clients using this
754 * API can be completely ignorant of sparse-file issues; sparse files
755 * will simply be padded with nulls.
757 * DO NOT intermingle calls to this function and archive_read_data_block
758 * to read a single entry body.
761 archive_read_data(struct archive *_a, void *buff, size_t s)
763 struct archive_read *a = (struct archive_read *)_a;
765 const void *read_buf;
774 if (a->read_data_remaining == 0) {
775 read_buf = a->read_data_block;
776 a->read_data_is_posix_read = 1;
777 a->read_data_requested = s;
778 r = _archive_read_data_block(&a->archive, &read_buf,
779 &a->read_data_remaining, &a->read_data_offset);
780 a->read_data_block = read_buf;
781 if (r == ARCHIVE_EOF)
784 * Error codes are all negative, so the status
785 * return here cannot be confused with a valid
786 * byte count. (ARCHIVE_OK is zero.)
792 if (a->read_data_offset < a->read_data_output_offset) {
793 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
794 "Encountered out-of-order sparse blocks");
795 return (ARCHIVE_RETRY);
798 /* Compute the amount of zero padding needed. */
799 if (a->read_data_output_offset + (int64_t)s <
800 a->read_data_offset) {
802 } else if (a->read_data_output_offset <
803 a->read_data_offset) {
804 len = (size_t)(a->read_data_offset -
805 a->read_data_output_offset);
810 memset(dest, 0, len);
812 a->read_data_output_offset += len;
816 /* Copy data if there is any space left. */
818 len = a->read_data_remaining;
821 memcpy(dest, a->read_data_block, len);
823 a->read_data_block += len;
824 a->read_data_remaining -= len;
825 a->read_data_output_offset += len;
826 a->read_data_offset += len;
831 a->read_data_is_posix_read = 0;
832 a->read_data_requested = 0;
837 * Skip over all remaining data in this entry.
840 archive_read_data_skip(struct archive *_a)
842 struct archive_read *a = (struct archive_read *)_a;
848 archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA,
849 "archive_read_data_skip");
851 if (a->format->read_data_skip != NULL)
852 r = (a->format->read_data_skip)(a);
854 while ((r = archive_read_data_block(&a->archive,
855 &buff, &size, &offset))
860 if (r == ARCHIVE_EOF)
863 a->archive.state = ARCHIVE_STATE_HEADER;
868 archive_seek_data(struct archive *_a, int64_t offset, int whence)
870 struct archive_read *a = (struct archive_read *)_a;
871 archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA,
872 "archive_seek_data_block");
874 if (a->format->seek_data == NULL) {
875 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
877 "No format_seek_data_block function registered");
878 return (ARCHIVE_FATAL);
881 return (a->format->seek_data)(a, offset, whence);
885 * Read the next block of entry data from the archive.
886 * This is a zero-copy interface; the client receives a pointer,
887 * size, and file offset of the next available block of data.
889 * Returns ARCHIVE_OK if the operation is successful, ARCHIVE_EOF if
890 * the end of entry is encountered.
893 _archive_read_data_block(struct archive *_a,
894 const void **buff, size_t *size, int64_t *offset)
896 struct archive_read *a = (struct archive_read *)_a;
897 archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA,
898 "archive_read_data_block");
900 if (a->format->read_data == NULL) {
901 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
903 "No format_read_data_block function registered");
904 return (ARCHIVE_FATAL);
907 return (a->format->read_data)(a, buff, size, offset);
911 __archive_read_close_filters(struct archive_read *a)
913 struct archive_read_filter *f = a->filter;
915 /* Close each filter in the pipeline. */
917 struct archive_read_filter *t = f->upstream;
918 if (!f->closed && f->close != NULL) {
919 int r1 = (f->close)(f);
932 __archive_read_free_filters(struct archive_read *a)
934 while (a->filter != NULL) {
935 struct archive_read_filter *t = a->filter->upstream;
942 * return the count of # of filters in use
945 _archive_filter_count(struct archive *_a)
947 struct archive_read *a = (struct archive_read *)_a;
948 struct archive_read_filter *p = a->filter;
958 * Close the file and all I/O.
961 _archive_read_close(struct archive *_a)
963 struct archive_read *a = (struct archive_read *)_a;
964 int r = ARCHIVE_OK, r1 = ARCHIVE_OK;
966 archive_check_magic(&a->archive, ARCHIVE_READ_MAGIC,
967 ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_read_close");
968 if (a->archive.state == ARCHIVE_STATE_CLOSED)
970 archive_clear_error(&a->archive);
971 a->archive.state = ARCHIVE_STATE_CLOSED;
973 /* TODO: Clean up the formatters. */
975 /* Release the filter objects. */
976 r1 = __archive_read_close_filters(a);
984 * Release memory and other resources.
987 _archive_read_free(struct archive *_a)
989 struct archive_read *a = (struct archive_read *)_a;
996 archive_check_magic(_a, ARCHIVE_READ_MAGIC,
997 ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_read_free");
998 if (a->archive.state != ARCHIVE_STATE_CLOSED
999 && a->archive.state != ARCHIVE_STATE_FATAL)
1000 r = archive_read_close(&a->archive);
1002 /* Call cleanup functions registered by optional components. */
1003 if (a->cleanup_archive_extract != NULL)
1004 r = (a->cleanup_archive_extract)(a);
1006 /* Cleanup format-specific data. */
1007 slots = sizeof(a->formats) / sizeof(a->formats[0]);
1008 for (i = 0; i < slots; i++) {
1009 a->format = &(a->formats[i]);
1010 if (a->formats[i].cleanup)
1011 (a->formats[i].cleanup)(a);
1014 /* Free the filters */
1015 __archive_read_free_filters(a);
1017 /* Release the bidder objects. */
1018 n = sizeof(a->bidders)/sizeof(a->bidders[0]);
1019 for (i = 0; i < n; i++) {
1020 if (a->bidders[i].free != NULL) {
1021 int r1 = (a->bidders[i].free)(&a->bidders[i]);
1027 archive_string_free(&a->archive.error_string);
1029 archive_entry_free(a->entry);
1030 a->archive.magic = 0;
1031 __archive_clean(&a->archive);
1032 free(a->client.dataset);
1037 static struct archive_read_filter *
1038 get_filter(struct archive *_a, int n)
1040 struct archive_read *a = (struct archive_read *)_a;
1041 struct archive_read_filter *f = a->filter;
1042 /* We use n == -1 for 'the last filter', which is always the
1044 if (n == -1 && f != NULL) {
1045 struct archive_read_filter *last = f;
1055 while (n > 0 && f != NULL) {
1063 _archive_filter_code(struct archive *_a, int n)
1065 struct archive_read_filter *f = get_filter(_a, n);
1066 return f == NULL ? -1 : f->code;
1070 _archive_filter_name(struct archive *_a, int n)
1072 struct archive_read_filter *f = get_filter(_a, n);
1073 return f == NULL ? NULL : f->name;
1077 _archive_filter_bytes(struct archive *_a, int n)
1079 struct archive_read_filter *f = get_filter(_a, n);
1080 return f == NULL ? -1 : f->position;
1084 * Used internally by read format handlers to register their bid and
1085 * initialization functions.
1088 __archive_read_register_format(struct archive_read *a,
1091 int (*bid)(struct archive_read *, int),
1092 int (*options)(struct archive_read *, const char *, const char *),
1093 int (*read_header)(struct archive_read *, struct archive_entry *),
1094 int (*read_data)(struct archive_read *, const void **, size_t *, int64_t *),
1095 int (*read_data_skip)(struct archive_read *),
1096 int64_t (*seek_data)(struct archive_read *, int64_t, int),
1097 int (*cleanup)(struct archive_read *))
1099 int i, number_slots;
1101 archive_check_magic(&a->archive,
1102 ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
1103 "__archive_read_register_format");
1105 number_slots = sizeof(a->formats) / sizeof(a->formats[0]);
1107 for (i = 0; i < number_slots; i++) {
1108 if (a->formats[i].bid == bid)
1109 return (ARCHIVE_WARN); /* We've already installed */
1110 if (a->formats[i].bid == NULL) {
1111 a->formats[i].bid = bid;
1112 a->formats[i].options = options;
1113 a->formats[i].read_header = read_header;
1114 a->formats[i].read_data = read_data;
1115 a->formats[i].read_data_skip = read_data_skip;
1116 a->formats[i].seek_data = seek_data;
1117 a->formats[i].cleanup = cleanup;
1118 a->formats[i].data = format_data;
1119 a->formats[i].name = name;
1120 return (ARCHIVE_OK);
1124 archive_set_error(&a->archive, ENOMEM,
1125 "Not enough slots for format registration");
1126 return (ARCHIVE_FATAL);
1130 * Used internally by decompression routines to register their bid and
1131 * initialization functions.
1134 __archive_read_get_bidder(struct archive_read *a,
1135 struct archive_read_filter_bidder **bidder)
1137 int i, number_slots;
1139 number_slots = sizeof(a->bidders) / sizeof(a->bidders[0]);
1141 for (i = 0; i < number_slots; i++) {
1142 if (a->bidders[i].bid == NULL) {
1143 memset(a->bidders + i, 0, sizeof(a->bidders[0]));
1144 *bidder = (a->bidders + i);
1145 return (ARCHIVE_OK);
1149 archive_set_error(&a->archive, ENOMEM,
1150 "Not enough slots for filter registration");
1151 return (ARCHIVE_FATAL);
1155 * The next section implements the peek/consume internal I/O
1156 * system used by archive readers. This system allows simple
1157 * read-ahead for consumers while preserving zero-copy operation
1160 * The two key operations:
1161 * * The read-ahead function returns a pointer to a block of data
1162 * that satisfies a minimum request.
1163 * * The consume function advances the file pointer.
1165 * In the ideal case, filters generate blocks of data
1166 * and __archive_read_ahead() just returns pointers directly into
1167 * those blocks. Then __archive_read_consume() just bumps those
1168 * pointers. Only if your request would span blocks does the I/O
1169 * layer use a copy buffer to provide you with a contiguous block of
1172 * A couple of useful idioms:
1173 * * "I just want some data." Ask for 1 byte and pay attention to
1174 * the "number of bytes available" from __archive_read_ahead().
1175 * Consume whatever you actually use.
1176 * * "I want to output a large block of data." As above, ask for 1 byte,
1177 * emit all that's available (up to whatever limit you have), consume
1178 * it all, then repeat until you're done. This effectively means that
1179 * you're passing along the blocks that came from your provider.
1180 * * "I want to peek ahead by a large amount." Ask for 4k or so, then
1181 * double and repeat until you get an error or have enough. Note
1182 * that the I/O layer will likely end up expanding its copy buffer
1183 * to fit your request, so use this technique cautiously. This
1184 * technique is used, for example, by some of the format tasting
1185 * code that has uncertain look-ahead needs.
1189 * Looks ahead in the input stream:
1190 * * If 'avail' pointer is provided, that returns number of bytes available
1191 * in the current buffer, which may be much larger than requested.
1192 * * If end-of-file, *avail gets set to zero.
1193 * * If error, *avail gets error code.
1194 * * If request can be met, returns pointer to data.
1195 * * If minimum request cannot be met, returns NULL.
1197 * Note: If you just want "some data", ask for 1 byte and pay attention
1198 * to *avail, which will have the actual amount available. If you
1199 * know exactly how many bytes you need, just ask for that and treat
1200 * a NULL return as an error.
1202 * Important: This does NOT move the file pointer. See
1203 * __archive_read_consume() below.
1206 __archive_read_ahead(struct archive_read *a, size_t min, ssize_t *avail)
1208 return (__archive_read_filter_ahead(a->filter, min, avail));
1212 __archive_read_filter_ahead(struct archive_read_filter *filter,
1213 size_t min, ssize_t *avail)
1218 if (filter->fatal) {
1220 *avail = ARCHIVE_FATAL;
1225 * Keep pulling more data until we can satisfy the request.
1230 * If we can satisfy from the copy buffer (and the
1231 * copy buffer isn't empty), we're done. In particular,
1232 * note that min == 0 is a perfectly well-defined
1235 if (filter->avail >= min && filter->avail > 0) {
1237 *avail = filter->avail;
1238 return (filter->next);
1242 * We can satisfy directly from client buffer if everything
1243 * currently in the copy buffer is still in the client buffer.
1245 if (filter->client_total >= filter->client_avail + filter->avail
1246 && filter->client_avail + filter->avail >= min) {
1247 /* "Roll back" to client buffer. */
1248 filter->client_avail += filter->avail;
1249 filter->client_next -= filter->avail;
1250 /* Copy buffer is now empty. */
1252 filter->next = filter->buffer;
1253 /* Return data from client buffer. */
1255 *avail = filter->client_avail;
1256 return (filter->client_next);
1259 /* Move data forward in copy buffer if necessary. */
1260 if (filter->next > filter->buffer &&
1261 filter->next + min > filter->buffer + filter->buffer_size) {
1262 if (filter->avail > 0)
1263 memmove(filter->buffer, filter->next,
1265 filter->next = filter->buffer;
1268 /* If we've used up the client data, get more. */
1269 if (filter->client_avail <= 0) {
1270 if (filter->end_of_file) {
1275 bytes_read = (filter->read)(filter,
1276 &filter->client_buff);
1277 if (bytes_read < 0) { /* Read error. */
1278 filter->client_total = filter->client_avail = 0;
1279 filter->client_next =
1280 filter->client_buff = NULL;
1283 *avail = ARCHIVE_FATAL;
1286 if (bytes_read == 0) {
1287 /* Check for another client object first */
1288 if (filter->archive->client.cursor !=
1289 filter->archive->client.nodes - 1) {
1290 if (client_switch_proxy(filter,
1291 filter->archive->client.cursor + 1)
1295 /* Premature end-of-file. */
1296 filter->client_total = filter->client_avail = 0;
1297 filter->client_next =
1298 filter->client_buff = NULL;
1299 filter->end_of_file = 1;
1300 /* Return whatever we do have. */
1302 *avail = filter->avail;
1305 filter->client_total = bytes_read;
1306 filter->client_avail = filter->client_total;
1307 filter->client_next = filter->client_buff;
1310 * We can't satisfy the request from the copy
1311 * buffer or the existing client data, so we
1312 * need to copy more client data over to the
1316 /* Ensure the buffer is big enough. */
1317 if (min > filter->buffer_size) {
1321 /* Double the buffer; watch for overflow. */
1322 s = t = filter->buffer_size;
1327 if (t <= s) { /* Integer overflow! */
1329 &filter->archive->archive,
1331 "Unable to allocate copy"
1335 *avail = ARCHIVE_FATAL;
1340 /* Now s >= min, so allocate a new buffer. */
1341 p = (char *)malloc(s);
1344 &filter->archive->archive,
1346 "Unable to allocate copy buffer");
1349 *avail = ARCHIVE_FATAL;
1352 /* Move data into newly-enlarged buffer. */
1353 if (filter->avail > 0)
1354 memmove(p, filter->next, filter->avail);
1355 free(filter->buffer);
1356 filter->next = filter->buffer = p;
1357 filter->buffer_size = s;
1360 /* We can add client data to copy buffer. */
1361 /* First estimate: copy to fill rest of buffer. */
1362 tocopy = (filter->buffer + filter->buffer_size)
1363 - (filter->next + filter->avail);
1364 /* Don't waste time buffering more than we need to. */
1365 if (tocopy + filter->avail > min)
1366 tocopy = min - filter->avail;
1367 /* Don't copy more than is available. */
1368 if (tocopy > filter->client_avail)
1369 tocopy = filter->client_avail;
1371 memcpy(filter->next + filter->avail,
1372 filter->client_next, tocopy);
1373 /* Remove this data from client buffer. */
1374 filter->client_next += tocopy;
1375 filter->client_avail -= tocopy;
1376 /* add it to copy buffer. */
1377 filter->avail += tocopy;
1383 * Move the file pointer forward.
1386 __archive_read_consume(struct archive_read *a, int64_t request)
1388 return (__archive_read_filter_consume(a->filter, request));
1392 __archive_read_filter_consume(struct archive_read_filter * filter,
1400 skipped = advance_file_pointer(filter, request);
1401 if (skipped == request)
1403 /* We hit EOF before we satisfied the skip request. */
1404 if (skipped < 0) /* Map error code to 0 for error message below. */
1406 archive_set_error(&filter->archive->archive,
1408 "Truncated input file (needed %jd bytes, only %jd available)",
1409 (intmax_t)request, (intmax_t)skipped);
1410 return (ARCHIVE_FATAL);
1414 * Advance the file pointer by the amount requested.
1415 * Returns the amount actually advanced, which may be less than the
1416 * request if EOF is encountered first.
1417 * Returns a negative value if there's an I/O error.
1420 advance_file_pointer(struct archive_read_filter *filter, int64_t request)
1422 int64_t bytes_skipped, total_bytes_skipped = 0;
1429 /* Use up the copy buffer first. */
1430 if (filter->avail > 0) {
1431 min = (size_t)minimum(request, (int64_t)filter->avail);
1432 filter->next += min;
1433 filter->avail -= min;
1435 filter->position += min;
1436 total_bytes_skipped += min;
1439 /* Then use up the client buffer. */
1440 if (filter->client_avail > 0) {
1441 min = (size_t)minimum(request, (int64_t)filter->client_avail);
1442 filter->client_next += min;
1443 filter->client_avail -= min;
1445 filter->position += min;
1446 total_bytes_skipped += min;
1449 return (total_bytes_skipped);
1451 /* If there's an optimized skip function, use it. */
1452 if (filter->skip != NULL) {
1453 bytes_skipped = (filter->skip)(filter, request);
1454 if (bytes_skipped < 0) { /* error */
1456 return (bytes_skipped);
1458 filter->position += bytes_skipped;
1459 total_bytes_skipped += bytes_skipped;
1460 request -= bytes_skipped;
1462 return (total_bytes_skipped);
1465 /* Use ordinary reads as necessary to complete the request. */
1467 bytes_read = (filter->read)(filter, &filter->client_buff);
1468 if (bytes_read < 0) {
1469 filter->client_buff = NULL;
1471 return (bytes_read);
1474 if (bytes_read == 0) {
1475 if (filter->archive->client.cursor !=
1476 filter->archive->client.nodes - 1) {
1477 if (client_switch_proxy(filter,
1478 filter->archive->client.cursor + 1)
1482 filter->client_buff = NULL;
1483 filter->end_of_file = 1;
1484 return (total_bytes_skipped);
1487 if (bytes_read >= request) {
1488 filter->client_next =
1489 ((const char *)filter->client_buff) + request;
1490 filter->client_avail = (size_t)(bytes_read - request);
1491 filter->client_total = bytes_read;
1492 total_bytes_skipped += request;
1493 filter->position += request;
1494 return (total_bytes_skipped);
1497 filter->position += bytes_read;
1498 total_bytes_skipped += bytes_read;
1499 request -= bytes_read;
1504 * Returns ARCHIVE_FAILED if seeking isn't supported.
1507 __archive_read_seek(struct archive_read *a, int64_t offset, int whence)
1509 return __archive_read_filter_seek(a->filter, offset, whence);
1513 __archive_read_filter_seek(struct archive_read_filter *filter, int64_t offset,
1516 struct archive_read_client *client;
1518 unsigned int cursor;
1520 if (filter->closed || filter->fatal)
1521 return (ARCHIVE_FATAL);
1522 if (filter->seek == NULL)
1523 return (ARCHIVE_FAILED);
1525 client = &(filter->archive->client);
1528 /* Adjust the offset and use SEEK_SET instead */
1529 offset += filter->position;
1534 if (client->dataset[cursor].begin_position < 0 ||
1535 client->dataset[cursor].total_size < 0 ||
1536 client->dataset[cursor].begin_position +
1537 client->dataset[cursor].total_size - 1 > offset ||
1538 cursor + 1 >= client->nodes)
1540 r = client->dataset[cursor].begin_position +
1541 client->dataset[cursor].total_size;
1542 client->dataset[++cursor].begin_position = r;
1545 r = client_switch_proxy(filter, cursor);
1546 if (r != ARCHIVE_OK)
1548 if ((r = client_seek_proxy(filter, 0, SEEK_END)) < 0)
1550 client->dataset[cursor].total_size = r;
1551 if (client->dataset[cursor].begin_position +
1552 client->dataset[cursor].total_size - 1 > offset ||
1553 cursor + 1 >= client->nodes)
1555 r = client->dataset[cursor].begin_position +
1556 client->dataset[cursor].total_size;
1557 client->dataset[++cursor].begin_position = r;
1559 offset -= client->dataset[cursor].begin_position;
1562 else if (offset > client->dataset[cursor].total_size - 1)
1563 offset = client->dataset[cursor].total_size - 1;
1564 if ((r = client_seek_proxy(filter, offset, SEEK_SET)) < 0)
1571 if (client->dataset[cursor].begin_position < 0 ||
1572 client->dataset[cursor].total_size < 0 ||
1573 cursor + 1 >= client->nodes)
1575 r = client->dataset[cursor].begin_position +
1576 client->dataset[cursor].total_size;
1577 client->dataset[++cursor].begin_position = r;
1580 r = client_switch_proxy(filter, cursor);
1581 if (r != ARCHIVE_OK)
1583 if ((r = client_seek_proxy(filter, 0, SEEK_END)) < 0)
1585 client->dataset[cursor].total_size = r;
1586 r = client->dataset[cursor].begin_position +
1587 client->dataset[cursor].total_size;
1588 if (cursor + 1 >= client->nodes)
1590 client->dataset[++cursor].begin_position = r;
1594 client->dataset[cursor].begin_position)
1596 offset += client->dataset[cursor].total_size;
1600 r = client->dataset[cursor].begin_position +
1601 client->dataset[cursor].total_size;
1603 offset = (r + offset) - client->dataset[cursor].begin_position;
1604 if ((r = client_switch_proxy(filter, cursor)) != ARCHIVE_OK)
1606 r = client_seek_proxy(filter, offset, SEEK_SET);
1612 return (ARCHIVE_FATAL);
1614 r += client->dataset[cursor].begin_position;
1618 * Ouch. Clearing the buffer like this hurts, especially
1619 * at bid time. A lot of our efficiency at bid time comes
1620 * from having bidders reuse the data we've already read.
1622 * TODO: If the seek request is in data we already
1623 * have, then don't call the seek callback.
1625 * TODO: Zip seeks to end-of-file at bid time. If
1626 * other formats also start doing this, we may need to
1627 * find a way for clients to fudge the seek offset to
1630 * Hmmm... If whence was SEEK_END, we know the file
1631 * size is (r - offset). Can we use that to simplify
1632 * the TODO items above?
1634 filter->avail = filter->client_avail = 0;
1635 filter->next = filter->buffer;
1636 filter->position = r;
1637 filter->end_of_file = 0;