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 void free_filters(struct archive_read *);
61 static int close_filters(struct archive_read *);
62 static struct archive_vtable *archive_read_vtable(void);
63 static int64_t _archive_filter_bytes(struct archive *, int);
64 static int _archive_filter_code(struct archive *, int);
65 static const char *_archive_filter_name(struct archive *, int);
66 static int _archive_filter_count(struct archive *);
67 static int _archive_read_close(struct archive *);
68 static int _archive_read_data_block(struct archive *,
69 const void **, size_t *, int64_t *);
70 static int _archive_read_free(struct archive *);
71 static int _archive_read_next_header(struct archive *,
72 struct archive_entry **);
73 static int _archive_read_next_header2(struct archive *,
74 struct archive_entry *);
75 static int64_t advance_file_pointer(struct archive_read_filter *, int64_t);
77 static struct archive_vtable *
78 archive_read_vtable(void)
80 static struct archive_vtable av;
81 static int inited = 0;
84 av.archive_filter_bytes = _archive_filter_bytes;
85 av.archive_filter_code = _archive_filter_code;
86 av.archive_filter_name = _archive_filter_name;
87 av.archive_filter_count = _archive_filter_count;
88 av.archive_read_data_block = _archive_read_data_block;
89 av.archive_read_next_header = _archive_read_next_header;
90 av.archive_read_next_header2 = _archive_read_next_header2;
91 av.archive_free = _archive_read_free;
92 av.archive_close = _archive_read_close;
99 * Allocate, initialize and return a struct archive object.
102 archive_read_new(void)
104 struct archive_read *a;
106 a = (struct archive_read *)malloc(sizeof(*a));
109 memset(a, 0, sizeof(*a));
110 a->archive.magic = ARCHIVE_READ_MAGIC;
112 a->archive.state = ARCHIVE_STATE_NEW;
113 a->entry = archive_entry_new2(&a->archive);
114 a->archive.vtable = archive_read_vtable();
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)(&self->archive->archive,
205 } else if (self->archive->client.seeker != NULL
206 && request > 64 * 1024) {
207 /* If the client provided a seeker but not a skipper,
208 * we can use the seeker to skip forward.
210 * Note: This isn't always a good idea. The client
211 * skipper is allowed to skip by less than requested
212 * if it needs to maintain block alignment. The
213 * seeker is not allowed to play such games, so using
214 * the seeker here may be a performance loss compared
215 * to just reading and discarding. That's why we
216 * only do this for skips of over 64k.
218 int64_t before = self->position;
219 int64_t after = (self->archive->client.seeker)(&self->archive->archive,
220 self->data, request, SEEK_CUR);
221 if (after != before + request)
222 return ARCHIVE_FATAL;
223 return after - before;
229 client_seek_proxy(struct archive_read_filter *self, int64_t offset, int whence)
231 /* DO NOT use the skipper here! If we transparently handled
232 * forward seek here by using the skipper, that will break
233 * other libarchive code that assumes a successful forward
234 * seek means it can also seek backwards.
236 if (self->archive->client.seeker == NULL)
237 return (ARCHIVE_FAILED);
238 return (self->archive->client.seeker)(&self->archive->archive,
239 self->data, offset, whence);
243 client_close_proxy(struct archive_read_filter *self)
247 if (self->archive->client.closer != NULL)
248 r = (self->archive->client.closer)((struct archive *)self->archive,
254 archive_read_set_open_callback(struct archive *_a,
255 archive_open_callback *client_opener)
257 struct archive_read *a = (struct archive_read *)_a;
258 archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
259 "archive_read_set_open_callback");
260 a->client.opener = client_opener;
265 archive_read_set_read_callback(struct archive *_a,
266 archive_read_callback *client_reader)
268 struct archive_read *a = (struct archive_read *)_a;
269 archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
270 "archive_read_set_read_callback");
271 a->client.reader = client_reader;
276 archive_read_set_skip_callback(struct archive *_a,
277 archive_skip_callback *client_skipper)
279 struct archive_read *a = (struct archive_read *)_a;
280 archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
281 "archive_read_set_skip_callback");
282 a->client.skipper = client_skipper;
287 archive_read_set_seek_callback(struct archive *_a,
288 archive_seek_callback *client_seeker)
290 struct archive_read *a = (struct archive_read *)_a;
291 archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
292 "archive_read_set_seek_callback");
293 a->client.seeker = client_seeker;
298 archive_read_set_close_callback(struct archive *_a,
299 archive_close_callback *client_closer)
301 struct archive_read *a = (struct archive_read *)_a;
302 archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
303 "archive_read_set_close_callback");
304 a->client.closer = client_closer;
309 archive_read_set_callback_data(struct archive *_a, void *client_data)
311 struct archive_read *a = (struct archive_read *)_a;
312 archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
313 "archive_read_set_callback_data");
314 a->client.data = client_data;
319 archive_read_open1(struct archive *_a)
321 struct archive_read *a = (struct archive_read *)_a;
322 struct archive_read_filter *filter;
325 archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
326 "archive_read_open");
327 archive_clear_error(&a->archive);
329 if (a->client.reader == NULL) {
330 archive_set_error(&a->archive, EINVAL,
331 "No reader function provided to archive_read_open");
332 a->archive.state = ARCHIVE_STATE_FATAL;
333 return (ARCHIVE_FATAL);
336 /* Open data source. */
337 if (a->client.opener != NULL) {
338 e =(a->client.opener)(&a->archive, a->client.data);
340 /* If the open failed, call the closer to clean up. */
341 if (a->client.closer)
342 (a->client.closer)(&a->archive, a->client.data);
347 filter = calloc(1, sizeof(*filter));
349 return (ARCHIVE_FATAL);
350 filter->bidder = NULL;
351 filter->upstream = NULL;
353 filter->data = a->client.data;
354 filter->read = client_read_proxy;
355 filter->skip = client_skip_proxy;
356 filter->seek = client_seek_proxy;
357 filter->close = client_close_proxy;
358 filter->name = "none";
359 filter->code = ARCHIVE_COMPRESSION_NONE;
362 /* Build out the input pipeline. */
363 e = choose_filters(a);
364 if (e < ARCHIVE_WARN) {
365 a->archive.state = ARCHIVE_STATE_FATAL;
366 return (ARCHIVE_FATAL);
369 slot = choose_format(a);
372 a->archive.state = ARCHIVE_STATE_FATAL;
373 return (ARCHIVE_FATAL);
375 a->format = &(a->formats[slot]);
377 a->archive.state = ARCHIVE_STATE_HEADER;
382 * Allow each registered stream transform to bid on whether
383 * it wants to handle this stream. Repeat until we've finished
384 * building the pipeline.
387 choose_filters(struct archive_read *a)
389 int number_bidders, i, bid, best_bid;
390 struct archive_read_filter_bidder *bidder, *best_bidder;
391 struct archive_read_filter *filter;
396 number_bidders = sizeof(a->bidders) / sizeof(a->bidders[0]);
402 for (i = 0; i < number_bidders; i++, bidder++) {
403 if (bidder->bid != NULL) {
404 bid = (bidder->bid)(bidder, a->filter);
405 if (bid > best_bid) {
407 best_bidder = bidder;
412 /* If no bidder, we're done. */
413 if (best_bidder == NULL) {
414 /* Verify the filter by asking it for some data. */
415 __archive_read_filter_ahead(a->filter, 1, &avail);
419 return (ARCHIVE_FATAL);
421 a->archive.compression_name = a->filter->name;
422 a->archive.compression_code = a->filter->code;
427 = (struct archive_read_filter *)calloc(1, sizeof(*filter));
429 return (ARCHIVE_FATAL);
430 filter->bidder = best_bidder;
432 filter->upstream = a->filter;
434 r = (best_bidder->init)(a->filter);
435 if (r != ARCHIVE_OK) {
438 return (ARCHIVE_FATAL);
444 * Read header of next entry.
447 _archive_read_next_header2(struct archive *_a, struct archive_entry *entry)
449 struct archive_read *a = (struct archive_read *)_a;
450 int r1 = ARCHIVE_OK, r2;
452 archive_check_magic(_a, ARCHIVE_READ_MAGIC,
453 ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
454 "archive_read_next_header");
456 archive_entry_clear(entry);
457 archive_clear_error(&a->archive);
460 * If client didn't consume entire data, skip any remainder
461 * (This is especially important for GNU incremental directories.)
463 if (a->archive.state == ARCHIVE_STATE_DATA) {
464 r1 = archive_read_data_skip(&a->archive);
465 if (r1 == ARCHIVE_EOF)
466 archive_set_error(&a->archive, EIO,
467 "Premature end-of-file.");
468 if (r1 == ARCHIVE_EOF || r1 == ARCHIVE_FATAL) {
469 a->archive.state = ARCHIVE_STATE_FATAL;
470 return (ARCHIVE_FATAL);
474 /* Record start-of-header offset in uncompressed stream. */
475 a->header_position = a->filter->position;
478 r2 = (a->format->read_header)(a, entry);
481 * EOF and FATAL are persistent at this layer. By
482 * modifying the state, we guarantee that future calls to
483 * read a header or read data will fail.
487 a->archive.state = ARCHIVE_STATE_EOF;
488 --_a->file_count;/* Revert a file counter. */
491 a->archive.state = ARCHIVE_STATE_DATA;
494 a->archive.state = ARCHIVE_STATE_DATA;
499 a->archive.state = ARCHIVE_STATE_FATAL;
503 a->read_data_output_offset = 0;
504 a->read_data_remaining = 0;
505 /* EOF always wins; otherwise return the worst error. */
506 return (r2 < r1 || r2 == ARCHIVE_EOF) ? r2 : r1;
510 _archive_read_next_header(struct archive *_a, struct archive_entry **entryp)
513 struct archive_read *a = (struct archive_read *)_a;
515 ret = _archive_read_next_header2(_a, a->entry);
521 * Allow each registered format to bid on whether it wants to handle
522 * the next entry. Return index of winning bidder.
525 choose_format(struct archive_read *a)
532 slots = sizeof(a->formats) / sizeof(a->formats[0]);
536 /* Set up a->format for convenience of bidders. */
537 a->format = &(a->formats[0]);
538 for (i = 0; i < slots; i++, a->format++) {
539 if (a->format->bid) {
540 bid = (a->format->bid)(a, best_bid);
541 if (bid == ARCHIVE_FATAL)
542 return (ARCHIVE_FATAL);
543 if (a->filter->position != 0)
544 __archive_read_seek(a, 0, SEEK_SET);
545 if ((bid > best_bid) || (best_bid_slot < 0)) {
553 * There were no bidders; this is a serious programmer error
554 * and demands a quick and definitive abort.
556 if (best_bid_slot < 0) {
557 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
558 "No formats registered");
559 return (ARCHIVE_FATAL);
563 * There were bidders, but no non-zero bids; this means we
564 * can't support this stream.
567 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
568 "Unrecognized archive format");
569 return (ARCHIVE_FATAL);
572 return (best_bid_slot);
576 * Return the file offset (within the uncompressed data stream) where
577 * the last header started.
580 archive_read_header_position(struct archive *_a)
582 struct archive_read *a = (struct archive_read *)_a;
583 archive_check_magic(_a, ARCHIVE_READ_MAGIC,
584 ARCHIVE_STATE_ANY, "archive_read_header_position");
585 return (a->header_position);
589 * Read data from an archive entry, using a read(2)-style interface.
590 * This is a convenience routine that just calls
591 * archive_read_data_block and copies the results into the client
592 * buffer, filling any gaps with zero bytes. Clients using this
593 * API can be completely ignorant of sparse-file issues; sparse files
594 * will simply be padded with nulls.
596 * DO NOT intermingle calls to this function and archive_read_data_block
597 * to read a single entry body.
600 archive_read_data(struct archive *_a, void *buff, size_t s)
602 struct archive_read *a = (struct archive_read *)_a;
604 const void *read_buf;
613 if (a->read_data_remaining == 0) {
614 read_buf = a->read_data_block;
615 r = _archive_read_data_block(&a->archive, &read_buf,
616 &a->read_data_remaining, &a->read_data_offset);
617 a->read_data_block = read_buf;
618 if (r == ARCHIVE_EOF)
621 * Error codes are all negative, so the status
622 * return here cannot be confused with a valid
623 * byte count. (ARCHIVE_OK is zero.)
629 if (a->read_data_offset < a->read_data_output_offset) {
630 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
631 "Encountered out-of-order sparse blocks");
632 return (ARCHIVE_RETRY);
635 /* Compute the amount of zero padding needed. */
636 if (a->read_data_output_offset + s <
637 a->read_data_offset) {
639 } else if (a->read_data_output_offset <
640 a->read_data_offset) {
641 len = a->read_data_offset -
642 a->read_data_output_offset;
647 memset(dest, 0, len);
649 a->read_data_output_offset += len;
653 /* Copy data if there is any space left. */
655 len = a->read_data_remaining;
658 memcpy(dest, a->read_data_block, len);
660 a->read_data_block += len;
661 a->read_data_remaining -= len;
662 a->read_data_output_offset += len;
663 a->read_data_offset += len;
672 * Skip over all remaining data in this entry.
675 archive_read_data_skip(struct archive *_a)
677 struct archive_read *a = (struct archive_read *)_a;
683 archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA,
684 "archive_read_data_skip");
686 if (a->format->read_data_skip != NULL)
687 r = (a->format->read_data_skip)(a);
689 while ((r = archive_read_data_block(&a->archive,
690 &buff, &size, &offset))
695 if (r == ARCHIVE_EOF)
698 a->archive.state = ARCHIVE_STATE_HEADER;
703 * Read the next block of entry data from the archive.
704 * This is a zero-copy interface; the client receives a pointer,
705 * size, and file offset of the next available block of data.
707 * Returns ARCHIVE_OK if the operation is successful, ARCHIVE_EOF if
708 * the end of entry is encountered.
711 _archive_read_data_block(struct archive *_a,
712 const void **buff, size_t *size, int64_t *offset)
714 struct archive_read *a = (struct archive_read *)_a;
715 archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA,
716 "archive_read_data_block");
718 if (a->format->read_data == NULL) {
719 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
721 "No format_read_data_block function registered");
722 return (ARCHIVE_FATAL);
725 return (a->format->read_data)(a, buff, size, offset);
729 close_filters(struct archive_read *a)
731 struct archive_read_filter *f = a->filter;
733 /* Close each filter in the pipeline. */
735 struct archive_read_filter *t = f->upstream;
736 if (!f->closed && f->close != NULL) {
737 int r1 = (f->close)(f);
750 free_filters(struct archive_read *a)
752 while (a->filter != NULL) {
753 struct archive_read_filter *t = a->filter->upstream;
760 * return the count of # of filters in use
763 _archive_filter_count(struct archive *_a)
765 struct archive_read *a = (struct archive_read *)_a;
766 struct archive_read_filter *p = a->filter;
776 * Close the file and all I/O.
779 _archive_read_close(struct archive *_a)
781 struct archive_read *a = (struct archive_read *)_a;
782 int r = ARCHIVE_OK, r1 = ARCHIVE_OK;
784 archive_check_magic(&a->archive, ARCHIVE_READ_MAGIC,
785 ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_read_close");
786 if (a->archive.state == ARCHIVE_STATE_CLOSED)
788 archive_clear_error(&a->archive);
789 a->archive.state = ARCHIVE_STATE_CLOSED;
791 /* TODO: Clean up the formatters. */
793 /* Release the filter objects. */
794 r1 = close_filters(a);
802 * Release memory and other resources.
805 _archive_read_free(struct archive *_a)
807 struct archive_read *a = (struct archive_read *)_a;
814 archive_check_magic(_a, ARCHIVE_READ_MAGIC,
815 ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_read_free");
816 if (a->archive.state != ARCHIVE_STATE_CLOSED
817 && a->archive.state != ARCHIVE_STATE_FATAL)
818 r = archive_read_close(&a->archive);
820 /* Call cleanup functions registered by optional components. */
821 if (a->cleanup_archive_extract != NULL)
822 r = (a->cleanup_archive_extract)(a);
824 /* Cleanup format-specific data. */
825 slots = sizeof(a->formats) / sizeof(a->formats[0]);
826 for (i = 0; i < slots; i++) {
827 a->format = &(a->formats[i]);
828 if (a->formats[i].cleanup)
829 (a->formats[i].cleanup)(a);
832 /* Free the filters */
835 /* Release the bidder objects. */
836 n = sizeof(a->bidders)/sizeof(a->bidders[0]);
837 for (i = 0; i < n; i++) {
838 if (a->bidders[i].free != NULL) {
839 int r1 = (a->bidders[i].free)(&a->bidders[i]);
845 archive_string_free(&a->archive.error_string);
847 archive_entry_free(a->entry);
848 a->archive.magic = 0;
849 __archive_clean(&a->archive);
854 static struct archive_read_filter *
855 get_filter(struct archive *_a, int n)
857 struct archive_read *a = (struct archive_read *)_a;
858 struct archive_read_filter *f = a->filter;
859 /* We use n == -1 for 'the last filter', which is always the client proxy. */
860 if (n == -1 && f != NULL) {
861 struct archive_read_filter *last = f;
871 while (n > 0 && f != NULL) {
879 _archive_filter_code(struct archive *_a, int n)
881 struct archive_read_filter *f = get_filter(_a, n);
882 return f == NULL ? -1 : f->code;
886 _archive_filter_name(struct archive *_a, int n)
888 struct archive_read_filter *f = get_filter(_a, n);
889 return f == NULL ? NULL : f->name;
893 _archive_filter_bytes(struct archive *_a, int n)
895 struct archive_read_filter *f = get_filter(_a, n);
896 return f == NULL ? -1 : f->position;
900 * Used internally by read format handlers to register their bid and
901 * initialization functions.
904 __archive_read_register_format(struct archive_read *a,
907 int (*bid)(struct archive_read *, int),
908 int (*options)(struct archive_read *, const char *, const char *),
909 int (*read_header)(struct archive_read *, struct archive_entry *),
910 int (*read_data)(struct archive_read *, const void **, size_t *, int64_t *),
911 int (*read_data_skip)(struct archive_read *),
912 int (*cleanup)(struct archive_read *))
916 archive_check_magic(&a->archive,
917 ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
918 "__archive_read_register_format");
920 number_slots = sizeof(a->formats) / sizeof(a->formats[0]);
922 for (i = 0; i < number_slots; i++) {
923 if (a->formats[i].bid == bid)
924 return (ARCHIVE_WARN); /* We've already installed */
925 if (a->formats[i].bid == NULL) {
926 a->formats[i].bid = bid;
927 a->formats[i].options = options;
928 a->formats[i].read_header = read_header;
929 a->formats[i].read_data = read_data;
930 a->formats[i].read_data_skip = read_data_skip;
931 a->formats[i].cleanup = cleanup;
932 a->formats[i].data = format_data;
933 a->formats[i].name = name;
938 archive_set_error(&a->archive, ENOMEM,
939 "Not enough slots for format registration");
940 return (ARCHIVE_FATAL);
944 * Used internally by decompression routines to register their bid and
945 * initialization functions.
948 __archive_read_get_bidder(struct archive_read *a,
949 struct archive_read_filter_bidder **bidder)
953 number_slots = sizeof(a->bidders) / sizeof(a->bidders[0]);
955 for (i = 0; i < number_slots; i++) {
956 if (a->bidders[i].bid == NULL) {
957 memset(a->bidders + i, 0, sizeof(a->bidders[0]));
958 *bidder = (a->bidders + i);
963 archive_set_error(&a->archive, ENOMEM,
964 "Not enough slots for filter registration");
965 return (ARCHIVE_FATAL);
969 * The next section implements the peek/consume internal I/O
970 * system used by archive readers. This system allows simple
971 * read-ahead for consumers while preserving zero-copy operation
974 * The two key operations:
975 * * The read-ahead function returns a pointer to a block of data
976 * that satisfies a minimum request.
977 * * The consume function advances the file pointer.
979 * In the ideal case, filters generate blocks of data
980 * and __archive_read_ahead() just returns pointers directly into
981 * those blocks. Then __archive_read_consume() just bumps those
982 * pointers. Only if your request would span blocks does the I/O
983 * layer use a copy buffer to provide you with a contiguous block of
986 * A couple of useful idioms:
987 * * "I just want some data." Ask for 1 byte and pay attention to
988 * the "number of bytes available" from __archive_read_ahead().
989 * Consume whatever you actually use.
990 * * "I want to output a large block of data." As above, ask for 1 byte,
991 * emit all that's available (up to whatever limit you have), consume
992 * it all, then repeat until you're done. This effectively means that
993 * you're passing along the blocks that came from your provider.
994 * * "I want to peek ahead by a large amount." Ask for 4k or so, then
995 * double and repeat until you get an error or have enough. Note
996 * that the I/O layer will likely end up expanding its copy buffer
997 * to fit your request, so use this technique cautiously. This
998 * technique is used, for example, by some of the format tasting
999 * code that has uncertain look-ahead needs.
1003 * Looks ahead in the input stream:
1004 * * If 'avail' pointer is provided, that returns number of bytes available
1005 * in the current buffer, which may be much larger than requested.
1006 * * If end-of-file, *avail gets set to zero.
1007 * * If error, *avail gets error code.
1008 * * If request can be met, returns pointer to data.
1009 * * If minimum request cannot be met, returns NULL.
1011 * Note: If you just want "some data", ask for 1 byte and pay attention
1012 * to *avail, which will have the actual amount available. If you
1013 * know exactly how many bytes you need, just ask for that and treat
1014 * a NULL return as an error.
1016 * Important: This does NOT move the file pointer. See
1017 * __archive_read_consume() below.
1020 __archive_read_ahead(struct archive_read *a, size_t min, ssize_t *avail)
1022 return (__archive_read_filter_ahead(a->filter, min, avail));
1026 __archive_read_filter_ahead(struct archive_read_filter *filter,
1027 size_t min, ssize_t *avail)
1032 if (filter->fatal) {
1034 *avail = ARCHIVE_FATAL;
1039 * Keep pulling more data until we can satisfy the request.
1044 * If we can satisfy from the copy buffer (and the
1045 * copy buffer isn't empty), we're done. In particular,
1046 * note that min == 0 is a perfectly well-defined
1049 if (filter->avail >= min && filter->avail > 0) {
1051 *avail = filter->avail;
1052 return (filter->next);
1056 * We can satisfy directly from client buffer if everything
1057 * currently in the copy buffer is still in the client buffer.
1059 if (filter->client_total >= filter->client_avail + filter->avail
1060 && filter->client_avail + filter->avail >= min) {
1061 /* "Roll back" to client buffer. */
1062 filter->client_avail += filter->avail;
1063 filter->client_next -= filter->avail;
1064 /* Copy buffer is now empty. */
1066 filter->next = filter->buffer;
1067 /* Return data from client buffer. */
1069 *avail = filter->client_avail;
1070 return (filter->client_next);
1073 /* Move data forward in copy buffer if necessary. */
1074 if (filter->next > filter->buffer &&
1075 filter->next + min > filter->buffer + filter->buffer_size) {
1076 if (filter->avail > 0)
1077 memmove(filter->buffer, filter->next, filter->avail);
1078 filter->next = filter->buffer;
1081 /* If we've used up the client data, get more. */
1082 if (filter->client_avail <= 0) {
1083 if (filter->end_of_file) {
1088 bytes_read = (filter->read)(filter,
1089 &filter->client_buff);
1090 if (bytes_read < 0) { /* Read error. */
1091 filter->client_total = filter->client_avail = 0;
1092 filter->client_next = filter->client_buff = NULL;
1095 *avail = ARCHIVE_FATAL;
1098 if (bytes_read == 0) { /* Premature end-of-file. */
1099 filter->client_total = filter->client_avail = 0;
1100 filter->client_next = filter->client_buff = NULL;
1101 filter->end_of_file = 1;
1102 /* Return whatever we do have. */
1104 *avail = filter->avail;
1107 filter->client_total = bytes_read;
1108 filter->client_avail = filter->client_total;
1109 filter->client_next = filter->client_buff;
1114 * We can't satisfy the request from the copy
1115 * buffer or the existing client data, so we
1116 * need to copy more client data over to the
1120 /* Ensure the buffer is big enough. */
1121 if (min > filter->buffer_size) {
1125 /* Double the buffer; watch for overflow. */
1126 s = t = filter->buffer_size;
1131 if (t <= s) { /* Integer overflow! */
1133 &filter->archive->archive,
1135 "Unable to allocate copy buffer");
1138 *avail = ARCHIVE_FATAL;
1143 /* Now s >= min, so allocate a new buffer. */
1144 p = (char *)malloc(s);
1147 &filter->archive->archive,
1149 "Unable to allocate copy buffer");
1152 *avail = ARCHIVE_FATAL;
1155 /* Move data into newly-enlarged buffer. */
1156 if (filter->avail > 0)
1157 memmove(p, filter->next, filter->avail);
1158 free(filter->buffer);
1159 filter->next = filter->buffer = p;
1160 filter->buffer_size = s;
1163 /* We can add client data to copy buffer. */
1164 /* First estimate: copy to fill rest of buffer. */
1165 tocopy = (filter->buffer + filter->buffer_size)
1166 - (filter->next + filter->avail);
1167 /* Don't waste time buffering more than we need to. */
1168 if (tocopy + filter->avail > min)
1169 tocopy = min - filter->avail;
1170 /* Don't copy more than is available. */
1171 if (tocopy > filter->client_avail)
1172 tocopy = filter->client_avail;
1174 memcpy(filter->next + filter->avail, filter->client_next,
1176 /* Remove this data from client buffer. */
1177 filter->client_next += tocopy;
1178 filter->client_avail -= tocopy;
1179 /* add it to copy buffer. */
1180 filter->avail += tocopy;
1186 * Move the file pointer forward.
1189 __archive_read_consume(struct archive_read *a, int64_t request)
1191 return (__archive_read_filter_consume(a->filter, request));
1195 __archive_read_filter_consume(struct archive_read_filter * filter,
1203 skipped = advance_file_pointer(filter, request);
1204 if (skipped == request)
1206 /* We hit EOF before we satisfied the skip request. */
1207 if (skipped < 0) /* Map error code to 0 for error message below. */
1209 archive_set_error(&filter->archive->archive,
1211 "Truncated input file (needed %jd bytes, only %jd available)",
1212 (intmax_t)request, (intmax_t)skipped);
1213 return (ARCHIVE_FATAL);
1217 * Advance the file pointer by the amount requested.
1218 * Returns the amount actually advanced, which may be less than the
1219 * request if EOF is encountered first.
1220 * Returns a negative value if there's an I/O error.
1223 advance_file_pointer(struct archive_read_filter *filter, int64_t request)
1225 int64_t bytes_skipped, total_bytes_skipped = 0;
1232 /* Use up the copy buffer first. */
1233 if (filter->avail > 0) {
1234 min = minimum(request, (int64_t)filter->avail);
1235 filter->next += min;
1236 filter->avail -= min;
1238 filter->position += min;
1239 total_bytes_skipped += min;
1242 /* Then use up the client buffer. */
1243 if (filter->client_avail > 0) {
1244 min = minimum(request, (int64_t)filter->client_avail);
1245 filter->client_next += min;
1246 filter->client_avail -= min;
1248 filter->position += min;
1249 total_bytes_skipped += min;
1252 return (total_bytes_skipped);
1254 /* If there's an optimized skip function, use it. */
1255 if (filter->skip != NULL) {
1256 bytes_skipped = (filter->skip)(filter, request);
1257 if (bytes_skipped < 0) { /* error */
1259 return (bytes_skipped);
1261 filter->position += bytes_skipped;
1262 total_bytes_skipped += bytes_skipped;
1263 request -= bytes_skipped;
1265 return (total_bytes_skipped);
1268 /* Use ordinary reads as necessary to complete the request. */
1270 bytes_read = (filter->read)(filter, &filter->client_buff);
1271 if (bytes_read < 0) {
1272 filter->client_buff = NULL;
1274 return (bytes_read);
1277 if (bytes_read == 0) {
1278 filter->client_buff = NULL;
1279 filter->end_of_file = 1;
1280 return (total_bytes_skipped);
1283 if (bytes_read >= request) {
1284 filter->client_next =
1285 ((const char *)filter->client_buff) + request;
1286 filter->client_avail = bytes_read - request;
1287 filter->client_total = bytes_read;
1288 total_bytes_skipped += request;
1289 filter->position += request;
1290 return (total_bytes_skipped);
1293 filter->position += bytes_read;
1294 total_bytes_skipped += bytes_read;
1295 request -= bytes_read;
1300 * Returns ARCHIVE_FAILED if seeking isn't supported.
1303 __archive_read_seek(struct archive_read *a, int64_t offset, int whence)
1305 return __archive_read_filter_seek(a->filter, offset, whence);
1309 __archive_read_filter_seek(struct archive_read_filter *filter, int64_t offset, int whence)
1313 if (filter->closed || filter->fatal)
1314 return (ARCHIVE_FATAL);
1315 if (filter->seek == NULL)
1316 return (ARCHIVE_FAILED);
1317 r = filter->seek(filter, offset, whence);
1320 * Ouch. Clearing the buffer like this hurts, especially
1321 * at bid time. A lot of our efficiency at bid time comes
1322 * from having bidders reuse the data we've already read.
1324 * TODO: If the seek request is in data we already
1325 * have, then don't call the seek callback.
1327 * TODO: Zip seeks to end-of-file at bid time. If
1328 * other formats also start doing this, we may need to
1329 * find a way for clients to fudge the seek offset to
1332 * Hmmm... If whence was SEEK_END, we know the file
1333 * size is (r - offset). Can we use that to simplify
1334 * the TODO items above?
1336 filter->avail = filter->client_avail = 0;
1337 filter->next = filter->buffer;
1338 filter->position = r;
1339 filter->end_of_file = 0;