2 * Copyright (c) 2003-2011 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 * This file contains the "essential" portions of the read API, that
28 * is, stuff that will probably always be used by any client that
29 * actually needs to read an archive. Optional pieces have been, as
30 * far as possible, separated out into separate files to avoid
31 * needlessly bloating statically-linked clients.
34 #include "archive_platform.h"
35 __FBSDID("$FreeBSD$");
52 #include "archive_entry.h"
53 #include "archive_private.h"
54 #include "archive_read_private.h"
56 #define minimum(a, b) (a < b ? a : b)
58 static int choose_filters(struct archive_read *);
59 static int choose_format(struct archive_read *);
60 static struct archive_vtable *archive_read_vtable(void);
61 static int64_t _archive_filter_bytes(struct archive *, int);
62 static int _archive_filter_code(struct archive *, int);
63 static const char *_archive_filter_name(struct archive *, int);
64 static int _archive_filter_count(struct archive *);
65 static int _archive_read_close(struct archive *);
66 static int _archive_read_data_block(struct archive *,
67 const void **, size_t *, int64_t *);
68 static int _archive_read_free(struct archive *);
69 static int _archive_read_next_header(struct archive *,
70 struct archive_entry **);
71 static int _archive_read_next_header2(struct archive *,
72 struct archive_entry *);
73 static int64_t advance_file_pointer(struct archive_read_filter *, int64_t);
75 static struct archive_vtable *
76 archive_read_vtable(void)
78 static struct archive_vtable av;
79 static int inited = 0;
82 av.archive_filter_bytes = _archive_filter_bytes;
83 av.archive_filter_code = _archive_filter_code;
84 av.archive_filter_name = _archive_filter_name;
85 av.archive_filter_count = _archive_filter_count;
86 av.archive_read_data_block = _archive_read_data_block;
87 av.archive_read_next_header = _archive_read_next_header;
88 av.archive_read_next_header2 = _archive_read_next_header2;
89 av.archive_free = _archive_read_free;
90 av.archive_close = _archive_read_close;
97 * Allocate, initialize and return a struct archive object.
100 archive_read_new(void)
102 struct archive_read *a;
104 a = (struct archive_read *)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;
461 archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
462 "archive_read_open");
463 archive_clear_error(&a->archive);
465 if (a->client.reader == NULL) {
466 archive_set_error(&a->archive, EINVAL,
467 "No reader function provided to archive_read_open");
468 a->archive.state = ARCHIVE_STATE_FATAL;
469 return (ARCHIVE_FATAL);
472 /* Open data source. */
473 if (a->client.opener != NULL) {
474 e = (a->client.opener)(&a->archive, a->client.dataset[0].data);
476 /* If the open failed, call the closer to clean up. */
477 if (a->client.closer) {
478 for (i = 0; i < a->client.nodes; i++)
479 (a->client.closer)(&a->archive,
480 a->client.dataset[i].data);
486 filter = calloc(1, sizeof(*filter));
488 return (ARCHIVE_FATAL);
489 filter->bidder = NULL;
490 filter->upstream = NULL;
492 filter->data = a->client.dataset[0].data;
493 filter->open = client_open_proxy;
494 filter->read = client_read_proxy;
495 filter->skip = client_skip_proxy;
496 filter->seek = client_seek_proxy;
497 filter->close = client_close_proxy;
498 filter->sswitch = client_switch_proxy;
499 filter->name = "none";
500 filter->code = ARCHIVE_FILTER_NONE;
502 a->client.dataset[0].begin_position = 0;
503 if (!a->filter || !a->bypass_filter_bidding)
506 /* Build out the input pipeline. */
507 e = choose_filters(a);
508 if (e < ARCHIVE_WARN) {
509 a->archive.state = ARCHIVE_STATE_FATAL;
510 return (ARCHIVE_FATAL);
515 /* Need to add "NONE" type filter at the end of the filter chain */
517 while (tmp->upstream)
519 tmp->upstream = filter;
524 slot = choose_format(a);
526 __archive_read_close_filters(a);
527 a->archive.state = ARCHIVE_STATE_FATAL;
528 return (ARCHIVE_FATAL);
530 a->format = &(a->formats[slot]);
533 a->archive.state = ARCHIVE_STATE_HEADER;
535 /* Ensure libarchive starts from the first node in a multivolume set */
536 client_switch_proxy(a->filter, 0);
541 * Allow each registered stream transform to bid on whether
542 * it wants to handle this stream. Repeat until we've finished
543 * building the pipeline.
546 choose_filters(struct archive_read *a)
548 int number_bidders, i, bid, best_bid, n;
549 struct archive_read_filter_bidder *bidder, *best_bidder;
550 struct archive_read_filter *filter;
554 for (n = 0; n < 25; ++n) {
555 number_bidders = sizeof(a->bidders) / sizeof(a->bidders[0]);
561 for (i = 0; i < number_bidders; i++, bidder++) {
562 if (bidder->bid != NULL) {
563 bid = (bidder->bid)(bidder, a->filter);
564 if (bid > best_bid) {
566 best_bidder = bidder;
571 /* If no bidder, we're done. */
572 if (best_bidder == NULL) {
573 /* Verify the filter by asking it for some data. */
574 __archive_read_filter_ahead(a->filter, 1, &avail);
576 __archive_read_close_filters(a);
577 __archive_read_free_filters(a);
578 return (ARCHIVE_FATAL);
580 a->archive.compression_name = a->filter->name;
581 a->archive.compression_code = a->filter->code;
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->init)(a->filter);
594 if (r != ARCHIVE_OK) {
595 __archive_read_close_filters(a);
596 __archive_read_free_filters(a);
597 return (ARCHIVE_FATAL);
600 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
601 "Input requires too many filters for decoding");
602 return (ARCHIVE_FATAL);
606 * Read header of next entry.
609 _archive_read_next_header2(struct archive *_a, struct archive_entry *entry)
611 struct archive_read *a = (struct archive_read *)_a;
612 int r1 = ARCHIVE_OK, r2;
614 archive_check_magic(_a, ARCHIVE_READ_MAGIC,
615 ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
616 "archive_read_next_header");
618 archive_entry_clear(entry);
619 archive_clear_error(&a->archive);
622 * If client didn't consume entire data, skip any remainder
623 * (This is especially important for GNU incremental directories.)
625 if (a->archive.state == ARCHIVE_STATE_DATA) {
626 r1 = archive_read_data_skip(&a->archive);
627 if (r1 == ARCHIVE_EOF)
628 archive_set_error(&a->archive, EIO,
629 "Premature end-of-file.");
630 if (r1 == ARCHIVE_EOF || r1 == ARCHIVE_FATAL) {
631 a->archive.state = ARCHIVE_STATE_FATAL;
632 return (ARCHIVE_FATAL);
636 /* Record start-of-header offset in uncompressed stream. */
637 a->header_position = a->filter->position;
640 r2 = (a->format->read_header)(a, entry);
643 * EOF and FATAL are persistent at this layer. By
644 * modifying the state, we guarantee that future calls to
645 * read a header or read data will fail.
649 a->archive.state = ARCHIVE_STATE_EOF;
650 --_a->file_count;/* Revert a file counter. */
653 a->archive.state = ARCHIVE_STATE_DATA;
656 a->archive.state = ARCHIVE_STATE_DATA;
661 a->archive.state = ARCHIVE_STATE_FATAL;
665 a->read_data_output_offset = 0;
666 a->read_data_remaining = 0;
667 a->read_data_is_posix_read = 0;
668 a->read_data_requested = 0;
669 a->data_start_node = a->client.cursor;
670 /* EOF always wins; otherwise return the worst error. */
671 return (r2 < r1 || r2 == ARCHIVE_EOF) ? r2 : r1;
675 _archive_read_next_header(struct archive *_a, struct archive_entry **entryp)
678 struct archive_read *a = (struct archive_read *)_a;
680 ret = _archive_read_next_header2(_a, a->entry);
686 * Allow each registered format to bid on whether it wants to handle
687 * the next entry. Return index of winning bidder.
690 choose_format(struct archive_read *a)
697 slots = sizeof(a->formats) / sizeof(a->formats[0]);
701 /* Set up a->format for convenience of bidders. */
702 a->format = &(a->formats[0]);
703 for (i = 0; i < slots; i++, a->format++) {
704 if (a->format->bid) {
705 bid = (a->format->bid)(a, best_bid);
706 if (bid == ARCHIVE_FATAL)
707 return (ARCHIVE_FATAL);
708 if (a->filter->position != 0)
709 __archive_read_seek(a, 0, SEEK_SET);
710 if ((bid > best_bid) || (best_bid_slot < 0)) {
718 * There were no bidders; this is a serious programmer error
719 * and demands a quick and definitive abort.
721 if (best_bid_slot < 0) {
722 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
723 "No formats registered");
724 return (ARCHIVE_FATAL);
728 * There were bidders, but no non-zero bids; this means we
729 * can't support this stream.
732 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
733 "Unrecognized archive format");
734 return (ARCHIVE_FATAL);
737 return (best_bid_slot);
741 * Return the file offset (within the uncompressed data stream) where
742 * the last header started.
745 archive_read_header_position(struct archive *_a)
747 struct archive_read *a = (struct archive_read *)_a;
748 archive_check_magic(_a, ARCHIVE_READ_MAGIC,
749 ARCHIVE_STATE_ANY, "archive_read_header_position");
750 return (a->header_position);
754 * Read data from an archive entry, using a read(2)-style interface.
755 * This is a convenience routine that just calls
756 * archive_read_data_block and copies the results into the client
757 * buffer, filling any gaps with zero bytes. Clients using this
758 * API can be completely ignorant of sparse-file issues; sparse files
759 * will simply be padded with nulls.
761 * DO NOT intermingle calls to this function and archive_read_data_block
762 * to read a single entry body.
765 archive_read_data(struct archive *_a, void *buff, size_t s)
767 struct archive_read *a = (struct archive_read *)_a;
769 const void *read_buf;
778 if (a->read_data_remaining == 0) {
779 read_buf = a->read_data_block;
780 a->read_data_is_posix_read = 1;
781 a->read_data_requested = s;
782 r = _archive_read_data_block(&a->archive, &read_buf,
783 &a->read_data_remaining, &a->read_data_offset);
784 a->read_data_block = read_buf;
785 if (r == ARCHIVE_EOF)
788 * Error codes are all negative, so the status
789 * return here cannot be confused with a valid
790 * byte count. (ARCHIVE_OK is zero.)
796 if (a->read_data_offset < a->read_data_output_offset) {
797 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
798 "Encountered out-of-order sparse blocks");
799 return (ARCHIVE_RETRY);
802 /* Compute the amount of zero padding needed. */
803 if (a->read_data_output_offset + (int64_t)s <
804 a->read_data_offset) {
806 } else if (a->read_data_output_offset <
807 a->read_data_offset) {
808 len = (size_t)(a->read_data_offset -
809 a->read_data_output_offset);
814 memset(dest, 0, len);
816 a->read_data_output_offset += len;
820 /* Copy data if there is any space left. */
822 len = a->read_data_remaining;
825 memcpy(dest, a->read_data_block, len);
827 a->read_data_block += len;
828 a->read_data_remaining -= len;
829 a->read_data_output_offset += len;
830 a->read_data_offset += len;
835 a->read_data_is_posix_read = 0;
836 a->read_data_requested = 0;
841 * Skip over all remaining data in this entry.
844 archive_read_data_skip(struct archive *_a)
846 struct archive_read *a = (struct archive_read *)_a;
852 archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA,
853 "archive_read_data_skip");
855 if (a->format->read_data_skip != NULL)
856 r = (a->format->read_data_skip)(a);
858 while ((r = archive_read_data_block(&a->archive,
859 &buff, &size, &offset))
864 if (r == ARCHIVE_EOF)
867 a->archive.state = ARCHIVE_STATE_HEADER;
872 archive_seek_data(struct archive *_a, int64_t offset, int whence)
874 struct archive_read *a = (struct archive_read *)_a;
875 archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA,
876 "archive_seek_data_block");
878 if (a->format->seek_data == NULL) {
879 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
881 "No format_seek_data_block function registered");
882 return (ARCHIVE_FATAL);
885 return (a->format->seek_data)(a, offset, whence);
889 * Read the next block of entry data from the archive.
890 * This is a zero-copy interface; the client receives a pointer,
891 * size, and file offset of the next available block of data.
893 * Returns ARCHIVE_OK if the operation is successful, ARCHIVE_EOF if
894 * the end of entry is encountered.
897 _archive_read_data_block(struct archive *_a,
898 const void **buff, size_t *size, int64_t *offset)
900 struct archive_read *a = (struct archive_read *)_a;
901 archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA,
902 "archive_read_data_block");
904 if (a->format->read_data == NULL) {
905 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
907 "No format_read_data_block function registered");
908 return (ARCHIVE_FATAL);
911 return (a->format->read_data)(a, buff, size, offset);
915 __archive_read_close_filters(struct archive_read *a)
917 struct archive_read_filter *f = a->filter;
919 /* Close each filter in the pipeline. */
921 struct archive_read_filter *t = f->upstream;
922 if (!f->closed && f->close != NULL) {
923 int r1 = (f->close)(f);
936 __archive_read_free_filters(struct archive_read *a)
938 while (a->filter != NULL) {
939 struct archive_read_filter *t = a->filter->upstream;
946 * return the count of # of filters in use
949 _archive_filter_count(struct archive *_a)
951 struct archive_read *a = (struct archive_read *)_a;
952 struct archive_read_filter *p = a->filter;
962 * Close the file and all I/O.
965 _archive_read_close(struct archive *_a)
967 struct archive_read *a = (struct archive_read *)_a;
968 int r = ARCHIVE_OK, r1 = ARCHIVE_OK;
970 archive_check_magic(&a->archive, ARCHIVE_READ_MAGIC,
971 ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_read_close");
972 if (a->archive.state == ARCHIVE_STATE_CLOSED)
974 archive_clear_error(&a->archive);
975 a->archive.state = ARCHIVE_STATE_CLOSED;
977 /* TODO: Clean up the formatters. */
979 /* Release the filter objects. */
980 r1 = __archive_read_close_filters(a);
988 * Release memory and other resources.
991 _archive_read_free(struct archive *_a)
993 struct archive_read *a = (struct archive_read *)_a;
1000 archive_check_magic(_a, ARCHIVE_READ_MAGIC,
1001 ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_read_free");
1002 if (a->archive.state != ARCHIVE_STATE_CLOSED
1003 && a->archive.state != ARCHIVE_STATE_FATAL)
1004 r = archive_read_close(&a->archive);
1006 /* Call cleanup functions registered by optional components. */
1007 if (a->cleanup_archive_extract != NULL)
1008 r = (a->cleanup_archive_extract)(a);
1010 /* Cleanup format-specific data. */
1011 slots = sizeof(a->formats) / sizeof(a->formats[0]);
1012 for (i = 0; i < slots; i++) {
1013 a->format = &(a->formats[i]);
1014 if (a->formats[i].cleanup)
1015 (a->formats[i].cleanup)(a);
1018 /* Free the filters */
1019 __archive_read_free_filters(a);
1021 /* Release the bidder objects. */
1022 n = sizeof(a->bidders)/sizeof(a->bidders[0]);
1023 for (i = 0; i < n; i++) {
1024 if (a->bidders[i].free != NULL) {
1025 int r1 = (a->bidders[i].free)(&a->bidders[i]);
1031 archive_string_free(&a->archive.error_string);
1033 archive_entry_free(a->entry);
1034 a->archive.magic = 0;
1035 __archive_clean(&a->archive);
1036 free(a->client.dataset);
1041 static struct archive_read_filter *
1042 get_filter(struct archive *_a, int n)
1044 struct archive_read *a = (struct archive_read *)_a;
1045 struct archive_read_filter *f = a->filter;
1046 /* We use n == -1 for 'the last filter', which is always the
1048 if (n == -1 && f != NULL) {
1049 struct archive_read_filter *last = f;
1059 while (n > 0 && f != NULL) {
1067 _archive_filter_code(struct archive *_a, int n)
1069 struct archive_read_filter *f = get_filter(_a, n);
1070 return f == NULL ? -1 : f->code;
1074 _archive_filter_name(struct archive *_a, int n)
1076 struct archive_read_filter *f = get_filter(_a, n);
1077 return f == NULL ? NULL : f->name;
1081 _archive_filter_bytes(struct archive *_a, int n)
1083 struct archive_read_filter *f = get_filter(_a, n);
1084 return f == NULL ? -1 : f->position;
1088 * Used internally by read format handlers to register their bid and
1089 * initialization functions.
1092 __archive_read_register_format(struct archive_read *a,
1095 int (*bid)(struct archive_read *, int),
1096 int (*options)(struct archive_read *, const char *, const char *),
1097 int (*read_header)(struct archive_read *, struct archive_entry *),
1098 int (*read_data)(struct archive_read *, const void **, size_t *, int64_t *),
1099 int (*read_data_skip)(struct archive_read *),
1100 int64_t (*seek_data)(struct archive_read *, int64_t, int),
1101 int (*cleanup)(struct archive_read *))
1103 int i, number_slots;
1105 archive_check_magic(&a->archive,
1106 ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
1107 "__archive_read_register_format");
1109 number_slots = sizeof(a->formats) / sizeof(a->formats[0]);
1111 for (i = 0; i < number_slots; i++) {
1112 if (a->formats[i].bid == bid)
1113 return (ARCHIVE_WARN); /* We've already installed */
1114 if (a->formats[i].bid == NULL) {
1115 a->formats[i].bid = bid;
1116 a->formats[i].options = options;
1117 a->formats[i].read_header = read_header;
1118 a->formats[i].read_data = read_data;
1119 a->formats[i].read_data_skip = read_data_skip;
1120 a->formats[i].seek_data = seek_data;
1121 a->formats[i].cleanup = cleanup;
1122 a->formats[i].data = format_data;
1123 a->formats[i].name = name;
1124 return (ARCHIVE_OK);
1128 archive_set_error(&a->archive, ENOMEM,
1129 "Not enough slots for format registration");
1130 return (ARCHIVE_FATAL);
1134 * Used internally by decompression routines to register their bid and
1135 * initialization functions.
1138 __archive_read_get_bidder(struct archive_read *a,
1139 struct archive_read_filter_bidder **bidder)
1141 int i, number_slots;
1143 number_slots = sizeof(a->bidders) / sizeof(a->bidders[0]);
1145 for (i = 0; i < number_slots; i++) {
1146 if (a->bidders[i].bid == NULL) {
1147 memset(a->bidders + i, 0, sizeof(a->bidders[0]));
1148 *bidder = (a->bidders + i);
1149 return (ARCHIVE_OK);
1153 archive_set_error(&a->archive, ENOMEM,
1154 "Not enough slots for filter registration");
1155 return (ARCHIVE_FATAL);
1159 * The next section implements the peek/consume internal I/O
1160 * system used by archive readers. This system allows simple
1161 * read-ahead for consumers while preserving zero-copy operation
1164 * The two key operations:
1165 * * The read-ahead function returns a pointer to a block of data
1166 * that satisfies a minimum request.
1167 * * The consume function advances the file pointer.
1169 * In the ideal case, filters generate blocks of data
1170 * and __archive_read_ahead() just returns pointers directly into
1171 * those blocks. Then __archive_read_consume() just bumps those
1172 * pointers. Only if your request would span blocks does the I/O
1173 * layer use a copy buffer to provide you with a contiguous block of
1176 * A couple of useful idioms:
1177 * * "I just want some data." Ask for 1 byte and pay attention to
1178 * the "number of bytes available" from __archive_read_ahead().
1179 * Consume whatever you actually use.
1180 * * "I want to output a large block of data." As above, ask for 1 byte,
1181 * emit all that's available (up to whatever limit you have), consume
1182 * it all, then repeat until you're done. This effectively means that
1183 * you're passing along the blocks that came from your provider.
1184 * * "I want to peek ahead by a large amount." Ask for 4k or so, then
1185 * double and repeat until you get an error or have enough. Note
1186 * that the I/O layer will likely end up expanding its copy buffer
1187 * to fit your request, so use this technique cautiously. This
1188 * technique is used, for example, by some of the format tasting
1189 * code that has uncertain look-ahead needs.
1193 * Looks ahead in the input stream:
1194 * * If 'avail' pointer is provided, that returns number of bytes available
1195 * in the current buffer, which may be much larger than requested.
1196 * * If end-of-file, *avail gets set to zero.
1197 * * If error, *avail gets error code.
1198 * * If request can be met, returns pointer to data.
1199 * * If minimum request cannot be met, returns NULL.
1201 * Note: If you just want "some data", ask for 1 byte and pay attention
1202 * to *avail, which will have the actual amount available. If you
1203 * know exactly how many bytes you need, just ask for that and treat
1204 * a NULL return as an error.
1206 * Important: This does NOT move the file pointer. See
1207 * __archive_read_consume() below.
1210 __archive_read_ahead(struct archive_read *a, size_t min, ssize_t *avail)
1212 return (__archive_read_filter_ahead(a->filter, min, avail));
1216 __archive_read_filter_ahead(struct archive_read_filter *filter,
1217 size_t min, ssize_t *avail)
1222 if (filter->fatal) {
1224 *avail = ARCHIVE_FATAL;
1229 * Keep pulling more data until we can satisfy the request.
1234 * If we can satisfy from the copy buffer (and the
1235 * copy buffer isn't empty), we're done. In particular,
1236 * note that min == 0 is a perfectly well-defined
1239 if (filter->avail >= min && filter->avail > 0) {
1241 *avail = filter->avail;
1242 return (filter->next);
1246 * We can satisfy directly from client buffer if everything
1247 * currently in the copy buffer is still in the client buffer.
1249 if (filter->client_total >= filter->client_avail + filter->avail
1250 && filter->client_avail + filter->avail >= min) {
1251 /* "Roll back" to client buffer. */
1252 filter->client_avail += filter->avail;
1253 filter->client_next -= filter->avail;
1254 /* Copy buffer is now empty. */
1256 filter->next = filter->buffer;
1257 /* Return data from client buffer. */
1259 *avail = filter->client_avail;
1260 return (filter->client_next);
1263 /* Move data forward in copy buffer if necessary. */
1264 if (filter->next > filter->buffer &&
1265 filter->next + min > filter->buffer + filter->buffer_size) {
1266 if (filter->avail > 0)
1267 memmove(filter->buffer, filter->next,
1269 filter->next = filter->buffer;
1272 /* If we've used up the client data, get more. */
1273 if (filter->client_avail <= 0) {
1274 if (filter->end_of_file) {
1279 bytes_read = (filter->read)(filter,
1280 &filter->client_buff);
1281 if (bytes_read < 0) { /* Read error. */
1282 filter->client_total = filter->client_avail = 0;
1283 filter->client_next =
1284 filter->client_buff = NULL;
1287 *avail = ARCHIVE_FATAL;
1290 if (bytes_read == 0) {
1291 /* Check for another client object first */
1292 if (filter->archive->client.cursor !=
1293 filter->archive->client.nodes - 1) {
1294 if (client_switch_proxy(filter,
1295 filter->archive->client.cursor + 1)
1299 /* Premature end-of-file. */
1300 filter->client_total = filter->client_avail = 0;
1301 filter->client_next =
1302 filter->client_buff = NULL;
1303 filter->end_of_file = 1;
1304 /* Return whatever we do have. */
1306 *avail = filter->avail;
1309 filter->client_total = bytes_read;
1310 filter->client_avail = filter->client_total;
1311 filter->client_next = filter->client_buff;
1314 * We can't satisfy the request from the copy
1315 * buffer or the existing client data, so we
1316 * need to copy more client data over to the
1320 /* Ensure the buffer is big enough. */
1321 if (min > filter->buffer_size) {
1325 /* Double the buffer; watch for overflow. */
1326 s = t = filter->buffer_size;
1331 if (t <= s) { /* Integer overflow! */
1333 &filter->archive->archive,
1335 "Unable to allocate copy"
1339 *avail = ARCHIVE_FATAL;
1344 /* Now s >= min, so allocate a new buffer. */
1345 p = (char *)malloc(s);
1348 &filter->archive->archive,
1350 "Unable to allocate copy buffer");
1353 *avail = ARCHIVE_FATAL;
1356 /* Move data into newly-enlarged buffer. */
1357 if (filter->avail > 0)
1358 memmove(p, filter->next, filter->avail);
1359 free(filter->buffer);
1360 filter->next = filter->buffer = p;
1361 filter->buffer_size = s;
1364 /* We can add client data to copy buffer. */
1365 /* First estimate: copy to fill rest of buffer. */
1366 tocopy = (filter->buffer + filter->buffer_size)
1367 - (filter->next + filter->avail);
1368 /* Don't waste time buffering more than we need to. */
1369 if (tocopy + filter->avail > min)
1370 tocopy = min - filter->avail;
1371 /* Don't copy more than is available. */
1372 if (tocopy > filter->client_avail)
1373 tocopy = filter->client_avail;
1375 memcpy(filter->next + filter->avail,
1376 filter->client_next, tocopy);
1377 /* Remove this data from client buffer. */
1378 filter->client_next += tocopy;
1379 filter->client_avail -= tocopy;
1380 /* add it to copy buffer. */
1381 filter->avail += tocopy;
1387 * Move the file pointer forward.
1390 __archive_read_consume(struct archive_read *a, int64_t request)
1392 return (__archive_read_filter_consume(a->filter, request));
1396 __archive_read_filter_consume(struct archive_read_filter * filter,
1402 return ARCHIVE_FATAL;
1406 skipped = advance_file_pointer(filter, request);
1407 if (skipped == request)
1409 /* We hit EOF before we satisfied the skip request. */
1410 if (skipped < 0) /* Map error code to 0 for error message below. */
1412 archive_set_error(&filter->archive->archive,
1414 "Truncated input file (needed %jd bytes, only %jd available)",
1415 (intmax_t)request, (intmax_t)skipped);
1416 return (ARCHIVE_FATAL);
1420 * Advance the file pointer by the amount requested.
1421 * Returns the amount actually advanced, which may be less than the
1422 * request if EOF is encountered first.
1423 * Returns a negative value if there's an I/O error.
1426 advance_file_pointer(struct archive_read_filter *filter, int64_t request)
1428 int64_t bytes_skipped, total_bytes_skipped = 0;
1435 /* Use up the copy buffer first. */
1436 if (filter->avail > 0) {
1437 min = (size_t)minimum(request, (int64_t)filter->avail);
1438 filter->next += min;
1439 filter->avail -= min;
1441 filter->position += min;
1442 total_bytes_skipped += min;
1445 /* Then use up the client buffer. */
1446 if (filter->client_avail > 0) {
1447 min = (size_t)minimum(request, (int64_t)filter->client_avail);
1448 filter->client_next += min;
1449 filter->client_avail -= min;
1451 filter->position += min;
1452 total_bytes_skipped += min;
1455 return (total_bytes_skipped);
1457 /* If there's an optimized skip function, use it. */
1458 if (filter->skip != NULL) {
1459 bytes_skipped = (filter->skip)(filter, request);
1460 if (bytes_skipped < 0) { /* error */
1462 return (bytes_skipped);
1464 filter->position += bytes_skipped;
1465 total_bytes_skipped += bytes_skipped;
1466 request -= bytes_skipped;
1468 return (total_bytes_skipped);
1471 /* Use ordinary reads as necessary to complete the request. */
1473 bytes_read = (filter->read)(filter, &filter->client_buff);
1474 if (bytes_read < 0) {
1475 filter->client_buff = NULL;
1477 return (bytes_read);
1480 if (bytes_read == 0) {
1481 if (filter->archive->client.cursor !=
1482 filter->archive->client.nodes - 1) {
1483 if (client_switch_proxy(filter,
1484 filter->archive->client.cursor + 1)
1488 filter->client_buff = NULL;
1489 filter->end_of_file = 1;
1490 return (total_bytes_skipped);
1493 if (bytes_read >= request) {
1494 filter->client_next =
1495 ((const char *)filter->client_buff) + request;
1496 filter->client_avail = (size_t)(bytes_read - request);
1497 filter->client_total = bytes_read;
1498 total_bytes_skipped += request;
1499 filter->position += request;
1500 return (total_bytes_skipped);
1503 filter->position += bytes_read;
1504 total_bytes_skipped += bytes_read;
1505 request -= bytes_read;
1510 * Returns ARCHIVE_FAILED if seeking isn't supported.
1513 __archive_read_seek(struct archive_read *a, int64_t offset, int whence)
1515 return __archive_read_filter_seek(a->filter, offset, whence);
1519 __archive_read_filter_seek(struct archive_read_filter *filter, int64_t offset,
1522 struct archive_read_client *client;
1524 unsigned int cursor;
1526 if (filter->closed || filter->fatal)
1527 return (ARCHIVE_FATAL);
1528 if (filter->seek == NULL)
1529 return (ARCHIVE_FAILED);
1531 client = &(filter->archive->client);
1534 /* Adjust the offset and use SEEK_SET instead */
1535 offset += filter->position;
1540 if (client->dataset[cursor].begin_position < 0 ||
1541 client->dataset[cursor].total_size < 0 ||
1542 client->dataset[cursor].begin_position +
1543 client->dataset[cursor].total_size - 1 > offset ||
1544 cursor + 1 >= client->nodes)
1546 r = client->dataset[cursor].begin_position +
1547 client->dataset[cursor].total_size;
1548 client->dataset[++cursor].begin_position = r;
1551 r = client_switch_proxy(filter, cursor);
1552 if (r != ARCHIVE_OK)
1554 if ((r = client_seek_proxy(filter, 0, SEEK_END)) < 0)
1556 client->dataset[cursor].total_size = r;
1557 if (client->dataset[cursor].begin_position +
1558 client->dataset[cursor].total_size - 1 > offset ||
1559 cursor + 1 >= client->nodes)
1561 r = client->dataset[cursor].begin_position +
1562 client->dataset[cursor].total_size;
1563 client->dataset[++cursor].begin_position = r;
1565 offset -= client->dataset[cursor].begin_position;
1568 else if (offset > client->dataset[cursor].total_size - 1)
1569 offset = client->dataset[cursor].total_size - 1;
1570 if ((r = client_seek_proxy(filter, offset, SEEK_SET)) < 0)
1577 if (client->dataset[cursor].begin_position < 0 ||
1578 client->dataset[cursor].total_size < 0 ||
1579 cursor + 1 >= client->nodes)
1581 r = client->dataset[cursor].begin_position +
1582 client->dataset[cursor].total_size;
1583 client->dataset[++cursor].begin_position = r;
1586 r = client_switch_proxy(filter, cursor);
1587 if (r != ARCHIVE_OK)
1589 if ((r = client_seek_proxy(filter, 0, SEEK_END)) < 0)
1591 client->dataset[cursor].total_size = r;
1592 r = client->dataset[cursor].begin_position +
1593 client->dataset[cursor].total_size;
1594 if (cursor + 1 >= client->nodes)
1596 client->dataset[++cursor].begin_position = r;
1600 client->dataset[cursor].begin_position)
1602 offset += client->dataset[cursor].total_size;
1606 r = client->dataset[cursor].begin_position +
1607 client->dataset[cursor].total_size;
1609 offset = (r + offset) - client->dataset[cursor].begin_position;
1610 if ((r = client_switch_proxy(filter, cursor)) != ARCHIVE_OK)
1612 r = client_seek_proxy(filter, offset, SEEK_SET);
1618 return (ARCHIVE_FATAL);
1620 r += client->dataset[cursor].begin_position;
1624 * Ouch. Clearing the buffer like this hurts, especially
1625 * at bid time. A lot of our efficiency at bid time comes
1626 * from having bidders reuse the data we've already read.
1628 * TODO: If the seek request is in data we already
1629 * have, then don't call the seek callback.
1631 * TODO: Zip seeks to end-of-file at bid time. If
1632 * other formats also start doing this, we may need to
1633 * find a way for clients to fudge the seek offset to
1636 * Hmmm... If whence was SEEK_END, we know the file
1637 * size is (r - offset). Can we use that to simplify
1638 * the TODO items above?
1640 filter->avail = filter->client_avail = 0;
1641 filter->next = filter->buffer;
1642 filter->position = r;
1643 filter->end_of_file = 0;