2 * Copyright (c) 2003-2007 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 build_stream(struct archive_read *);
59 static int choose_format(struct archive_read *);
60 static struct archive_vtable *archive_read_vtable(void);
61 static int _archive_read_close(struct archive *);
62 static int _archive_read_finish(struct archive *);
64 static struct archive_vtable *
65 archive_read_vtable(void)
67 static struct archive_vtable av;
68 static int inited = 0;
71 av.archive_finish = _archive_read_finish;
72 av.archive_close = _archive_read_close;
78 * Allocate, initialize and return a struct archive object.
81 archive_read_new(void)
83 struct archive_read *a;
85 a = (struct archive_read *)malloc(sizeof(*a));
88 memset(a, 0, sizeof(*a));
89 a->archive.magic = ARCHIVE_READ_MAGIC;
91 a->archive.state = ARCHIVE_STATE_NEW;
92 a->entry = archive_entry_new();
93 a->archive.vtable = archive_read_vtable();
99 * Record the do-not-extract-to file. This belongs in archive_read_extract.c.
102 archive_read_extract_set_skip_file(struct archive *_a, dev_t d, ino_t i)
104 struct archive_read *a = (struct archive_read *)_a;
105 __archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_ANY,
106 "archive_read_extract_set_skip_file");
107 a->skip_file_dev = d;
108 a->skip_file_ino = i;
112 * Set read options for the format.
115 archive_read_set_format_options(struct archive *_a, const char *s)
117 struct archive_read *a;
118 struct archive_format_descriptor *format;
119 char key[64], val[64];
123 a = (struct archive_read *)_a;
125 for (i = 0; i < sizeof(a->formats)/sizeof(a->formats[0]); i++) {
126 format = &a->formats[i];
127 if (format == NULL || format->options == NULL ||
128 format->name == NULL)
129 /* This format does not support option. */
132 while ((len = __archive_parse_options(s, format->name,
133 sizeof(key), key, sizeof(val), val)) > 0) {
135 r = format->options(a, key, NULL);
137 r = format->options(a, key, val);
138 if (r == ARCHIVE_FATAL)
144 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
145 "Illegal format options.");
146 return (ARCHIVE_WARN);
152 * Set read options for the filter.
155 archive_read_set_filter_options(struct archive *_a, const char *s)
157 struct archive_read *a;
158 struct archive_read_filter *filter;
159 struct archive_read_filter_bidder *bidder;
160 char key[64], val[64];
163 a = (struct archive_read *)_a;
166 for (filter = a->filter; filter != NULL; filter = filter->upstream) {
167 bidder = filter->bidder;
170 if (bidder->options == NULL)
171 /* This bidder does not support option */
173 while ((len = __archive_parse_options(s, filter->name,
174 sizeof(key), key, sizeof(val), val)) > 0) {
176 r = bidder->options(bidder, key, NULL);
178 r = bidder->options(bidder, key, val);
179 if (r == ARCHIVE_FATAL)
185 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
186 "Illegal format options.");
187 return (ARCHIVE_WARN);
193 * Set read options for the format and the filter.
196 archive_read_set_options(struct archive *_a, const char *s)
200 r = archive_read_set_format_options(_a, s);
203 r = archive_read_set_filter_options(_a, s);
213 archive_read_open(struct archive *a, void *client_data,
214 archive_open_callback *client_opener, archive_read_callback *client_reader,
215 archive_close_callback *client_closer)
217 /* Old archive_read_open() is just a thin shell around
218 * archive_read_open2. */
219 return archive_read_open2(a, client_data, client_opener,
220 client_reader, NULL, client_closer);
224 client_read_proxy(struct archive_read_filter *self, const void **buff)
227 r = (self->archive->client.reader)(&self->archive->archive,
229 self->archive->archive.raw_position += r;
234 client_skip_proxy(struct archive_read_filter *self, int64_t request)
237 if (self->archive->client.skipper == NULL)
239 r = (self->archive->client.skipper)(&self->archive->archive,
240 self->data, request);
241 self->archive->archive.raw_position += r;
246 client_close_proxy(struct archive_read_filter *self)
250 if (self->archive->client.closer != NULL)
251 r = (self->archive->client.closer)((struct archive *)self->archive,
259 archive_read_open2(struct archive *_a, void *client_data,
260 archive_open_callback *client_opener,
261 archive_read_callback *client_reader,
262 archive_skip_callback *client_skipper,
263 archive_close_callback *client_closer)
265 struct archive_read *a = (struct archive_read *)_a;
266 struct archive_read_filter *filter;
269 __archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
270 "archive_read_open");
272 if (client_reader == NULL)
274 "No reader function provided to archive_read_open");
276 /* Open data source. */
277 if (client_opener != NULL) {
278 e =(client_opener)(&a->archive, client_data);
280 /* If the open failed, call the closer to clean up. */
282 (client_closer)(&a->archive, client_data);
287 /* Save the client functions and mock up the initial source. */
288 a->client.reader = client_reader;
289 a->client.skipper = client_skipper;
290 a->client.closer = client_closer;
292 filter = calloc(1, sizeof(*filter));
294 return (ARCHIVE_FATAL);
295 filter->bidder = NULL;
296 filter->upstream = NULL;
298 filter->data = client_data;
299 filter->read = client_read_proxy;
300 filter->skip = client_skip_proxy;
301 filter->close = client_close_proxy;
302 filter->name = "none";
303 filter->code = ARCHIVE_COMPRESSION_NONE;
306 /* Build out the input pipeline. */
309 a->archive.state = ARCHIVE_STATE_HEADER;
315 * Allow each registered stream transform to bid on whether
316 * it wants to handle this stream. Repeat until we've finished
317 * building the pipeline.
320 build_stream(struct archive_read *a)
322 int number_bidders, i, bid, best_bid;
323 struct archive_read_filter_bidder *bidder, *best_bidder;
324 struct archive_read_filter *filter;
328 number_bidders = sizeof(a->bidders) / sizeof(a->bidders[0]);
334 for (i = 0; i < number_bidders; i++, bidder++) {
335 if (bidder->bid != NULL) {
336 bid = (bidder->bid)(bidder, a->filter);
337 if (bid > best_bid) {
339 best_bidder = bidder;
344 /* If no bidder, we're done. */
345 if (best_bidder == NULL) {
346 a->archive.compression_name = a->filter->name;
347 a->archive.compression_code = a->filter->code;
352 = (struct archive_read_filter *)calloc(1, sizeof(*filter));
354 return (ARCHIVE_FATAL);
355 filter->bidder = best_bidder;
357 filter->upstream = a->filter;
358 r = (best_bidder->init)(filter);
359 if (r != ARCHIVE_OK) {
368 * Read header of next entry.
371 archive_read_next_header(struct archive *_a, struct archive_entry **entryp)
373 struct archive_read *a = (struct archive_read *)_a;
374 struct archive_entry *entry;
377 __archive_check_magic(_a, ARCHIVE_READ_MAGIC,
378 ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
379 "archive_read_next_header");
383 archive_entry_clear(entry);
384 archive_clear_error(&a->archive);
387 * If no format has yet been chosen, choose one.
389 if (a->format == NULL) {
390 slot = choose_format(a);
392 a->archive.state = ARCHIVE_STATE_FATAL;
393 return (ARCHIVE_FATAL);
395 a->format = &(a->formats[slot]);
399 * If client didn't consume entire data, skip any remainder
400 * (This is especially important for GNU incremental directories.)
402 if (a->archive.state == ARCHIVE_STATE_DATA) {
403 ret = archive_read_data_skip(&a->archive);
404 if (ret == ARCHIVE_EOF) {
405 archive_set_error(&a->archive, EIO, "Premature end-of-file.");
406 a->archive.state = ARCHIVE_STATE_FATAL;
407 return (ARCHIVE_FATAL);
409 if (ret != ARCHIVE_OK)
413 /* Record start-of-header. */
414 a->header_position = a->archive.file_position;
416 ret = (a->format->read_header)(a, entry);
419 * EOF and FATAL are persistent at this layer. By
420 * modifying the state, we guarantee that future calls to
421 * read a header or read data will fail.
425 a->archive.state = ARCHIVE_STATE_EOF;
428 a->archive.state = ARCHIVE_STATE_DATA;
431 a->archive.state = ARCHIVE_STATE_DATA;
436 a->archive.state = ARCHIVE_STATE_FATAL;
441 a->read_data_output_offset = 0;
442 a->read_data_remaining = 0;
447 * Allow each registered format to bid on whether it wants to handle
448 * the next entry. Return index of winning bidder.
451 choose_format(struct archive_read *a)
458 slots = sizeof(a->formats) / sizeof(a->formats[0]);
462 /* Set up a->format and a->pformat_data for convenience of bidders. */
463 a->format = &(a->formats[0]);
464 for (i = 0; i < slots; i++, a->format++) {
465 if (a->format->bid) {
466 bid = (a->format->bid)(a);
467 if (bid == ARCHIVE_FATAL)
468 return (ARCHIVE_FATAL);
469 if ((bid > best_bid) || (best_bid_slot < 0)) {
477 * There were no bidders; this is a serious programmer error
478 * and demands a quick and definitive abort.
480 if (best_bid_slot < 0)
481 __archive_errx(1, "No formats were registered; you must "
482 "invoke at least one archive_read_support_format_XXX "
483 "function in order to successfully read an archive.");
486 * There were bidders, but no non-zero bids; this means we
487 * can't support this stream.
490 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
491 "Unrecognized archive format");
492 return (ARCHIVE_FATAL);
495 return (best_bid_slot);
499 * Return the file offset (within the uncompressed data stream) where
500 * the last header started.
503 archive_read_header_position(struct archive *_a)
505 struct archive_read *a = (struct archive_read *)_a;
506 __archive_check_magic(_a, ARCHIVE_READ_MAGIC,
507 ARCHIVE_STATE_ANY, "archive_read_header_position");
508 return (a->header_position);
512 * Read data from an archive entry, using a read(2)-style interface.
513 * This is a convenience routine that just calls
514 * archive_read_data_block and copies the results into the client
515 * buffer, filling any gaps with zero bytes. Clients using this
516 * API can be completely ignorant of sparse-file issues; sparse files
517 * will simply be padded with nulls.
519 * DO NOT intermingle calls to this function and archive_read_data_block
520 * to read a single entry body.
523 archive_read_data(struct archive *_a, void *buff, size_t s)
525 struct archive_read *a = (struct archive_read *)_a;
527 const void *read_buf;
536 if (a->read_data_remaining == 0) {
537 read_buf = a->read_data_block;
538 r = archive_read_data_block(&a->archive, &read_buf,
539 &a->read_data_remaining, &a->read_data_offset);
540 a->read_data_block = read_buf;
541 if (r == ARCHIVE_EOF)
544 * Error codes are all negative, so the status
545 * return here cannot be confused with a valid
546 * byte count. (ARCHIVE_OK is zero.)
552 if (a->read_data_offset < a->read_data_output_offset) {
553 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
554 "Encountered out-of-order sparse blocks");
555 return (ARCHIVE_RETRY);
558 /* Compute the amount of zero padding needed. */
559 if (a->read_data_output_offset + (off_t)s <
560 a->read_data_offset) {
562 } else if (a->read_data_output_offset <
563 a->read_data_offset) {
564 len = a->read_data_offset -
565 a->read_data_output_offset;
570 memset(dest, 0, len);
572 a->read_data_output_offset += len;
576 /* Copy data if there is any space left. */
578 len = a->read_data_remaining;
581 memcpy(dest, a->read_data_block, len);
583 a->read_data_block += len;
584 a->read_data_remaining -= len;
585 a->read_data_output_offset += len;
586 a->read_data_offset += len;
594 #if ARCHIVE_API_VERSION < 3
596 * Obsolete function provided for compatibility only. Note that the API
597 * of this function doesn't allow the caller to detect if the remaining
598 * data from the archive entry is shorter than the buffer provided, or
599 * even if an error occurred while reading data.
602 archive_read_data_into_buffer(struct archive *a, void *d, ssize_t len)
605 archive_read_data(a, d, len);
611 * Skip over all remaining data in this entry.
614 archive_read_data_skip(struct archive *_a)
616 struct archive_read *a = (struct archive_read *)_a;
622 __archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA,
623 "archive_read_data_skip");
625 if (a->format->read_data_skip != NULL)
626 r = (a->format->read_data_skip)(a);
628 while ((r = archive_read_data_block(&a->archive,
629 &buff, &size, &offset))
634 if (r == ARCHIVE_EOF)
637 a->archive.state = ARCHIVE_STATE_HEADER;
642 * Read the next block of entry data from the archive.
643 * This is a zero-copy interface; the client receives a pointer,
644 * size, and file offset of the next available block of data.
646 * Returns ARCHIVE_OK if the operation is successful, ARCHIVE_EOF if
647 * the end of entry is encountered.
650 archive_read_data_block(struct archive *_a,
651 const void **buff, size_t *size, off_t *offset)
653 struct archive_read *a = (struct archive_read *)_a;
654 __archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA,
655 "archive_read_data_block");
657 if (a->format->read_data == NULL) {
658 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
660 "No format_read_data_block function registered");
661 return (ARCHIVE_FATAL);
664 return (a->format->read_data)(a, buff, size, offset);
668 * Close the file and release most resources.
670 * Be careful: client might just call read_new and then read_finish.
671 * Don't assume we actually read anything or performed any non-trivial
675 _archive_read_close(struct archive *_a)
677 struct archive_read *a = (struct archive_read *)_a;
678 int r = ARCHIVE_OK, r1 = ARCHIVE_OK;
681 __archive_check_magic(&a->archive, ARCHIVE_READ_MAGIC,
682 ARCHIVE_STATE_ANY, "archive_read_close");
683 a->archive.state = ARCHIVE_STATE_CLOSED;
685 /* Call cleanup functions registered by optional components. */
686 if (a->cleanup_archive_extract != NULL)
687 r = (a->cleanup_archive_extract)(a);
689 /* TODO: Clean up the formatters. */
691 /* Clean up the filter pipeline. */
692 while (a->filter != NULL) {
693 struct archive_read_filter *t = a->filter->upstream;
694 if (a->filter->close != NULL) {
695 r1 = (a->filter->close)(a->filter);
699 free(a->filter->buffer);
704 /* Release the bidder objects. */
705 n = sizeof(a->bidders)/sizeof(a->bidders[0]);
706 for (i = 0; i < n; i++) {
707 if (a->bidders[i].free != NULL) {
708 r1 = (a->bidders[i].free)(&a->bidders[i]);
718 * Release memory and other resources.
721 _archive_read_finish(struct archive *_a)
723 struct archive_read *a = (struct archive_read *)_a;
728 __archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_ANY,
729 "archive_read_finish");
730 if (a->archive.state != ARCHIVE_STATE_CLOSED)
731 r = archive_read_close(&a->archive);
733 /* Cleanup format-specific data. */
734 slots = sizeof(a->formats) / sizeof(a->formats[0]);
735 for (i = 0; i < slots; i++) {
736 a->format = &(a->formats[i]);
737 if (a->formats[i].cleanup)
738 (a->formats[i].cleanup)(a);
741 archive_string_free(&a->archive.error_string);
743 archive_entry_free(a->entry);
744 a->archive.magic = 0;
746 #if ARCHIVE_API_VERSION > 1
752 * Used internally by read format handlers to register their bid and
753 * initialization functions.
756 __archive_read_register_format(struct archive_read *a,
759 int (*bid)(struct archive_read *),
760 int (*options)(struct archive_read *, const char *, const char *),
761 int (*read_header)(struct archive_read *, struct archive_entry *),
762 int (*read_data)(struct archive_read *, const void **, size_t *, off_t *),
763 int (*read_data_skip)(struct archive_read *),
764 int (*cleanup)(struct archive_read *))
768 __archive_check_magic(&a->archive,
769 ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
770 "__archive_read_register_format");
772 number_slots = sizeof(a->formats) / sizeof(a->formats[0]);
774 for (i = 0; i < number_slots; i++) {
775 if (a->formats[i].bid == bid)
776 return (ARCHIVE_WARN); /* We've already installed */
777 if (a->formats[i].bid == NULL) {
778 a->formats[i].bid = bid;
779 a->formats[i].options = options;
780 a->formats[i].read_header = read_header;
781 a->formats[i].read_data = read_data;
782 a->formats[i].read_data_skip = read_data_skip;
783 a->formats[i].cleanup = cleanup;
784 a->formats[i].data = format_data;
785 a->formats[i].name = name;
790 __archive_errx(1, "Not enough slots for format registration");
791 return (ARCHIVE_FATAL); /* Never actually called. */
795 * Used internally by decompression routines to register their bid and
796 * initialization functions.
798 struct archive_read_filter_bidder *
799 __archive_read_get_bidder(struct archive_read *a)
803 __archive_check_magic(&a->archive,
804 ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
805 "__archive_read_get_bidder");
807 number_slots = sizeof(a->bidders) / sizeof(a->bidders[0]);
809 for (i = 0; i < number_slots; i++) {
810 if (a->bidders[i].bid == NULL) {
811 memset(a->bidders + i, 0, sizeof(a->bidders[0]));
812 return (a->bidders + i);
816 __archive_errx(1, "Not enough slots for compression registration");
817 return (NULL); /* Never actually executed. */
821 * The next three functions comprise the peek/consume internal I/O
822 * system used by archive format readers. This system allows fairly
823 * flexible read-ahead and allows the I/O code to operate in a
824 * zero-copy manner most of the time.
826 * In the ideal case, filters generate blocks of data
827 * and __archive_read_ahead() just returns pointers directly into
828 * those blocks. Then __archive_read_consume() just bumps those
829 * pointers. Only if your request would span blocks does the I/O
830 * layer use a copy buffer to provide you with a contiguous block of
831 * data. The __archive_read_skip() is an optimization; it scans ahead
832 * very quickly (it usually translates into a seek() operation if
833 * you're reading uncompressed disk files).
835 * A couple of useful idioms:
836 * * "I just want some data." Ask for 1 byte and pay attention to
837 * the "number of bytes available" from __archive_read_ahead().
838 * You can consume more than you asked for; you just can't consume
839 * more than is available. If you consume everything that's
840 * immediately available, the next read_ahead() call will pull
842 * * "I want to output a large block of data." As above, ask for 1 byte,
843 * emit all that's available (up to whatever limit you have), then
844 * repeat until you're done.
845 * * "I want to peek ahead by a large amount." Ask for 4k or so, then
846 * double and repeat until you get an error or have enough. Note
847 * that the I/O layer will likely end up expanding its copy buffer
848 * to fit your request, so use this technique cautiously. This
849 * technique is used, for example, by some of the format tasting
850 * code that has uncertain look-ahead needs.
852 * TODO: Someday, provide a more generic __archive_read_seek() for
853 * those cases where it's useful. This is tricky because there are lots
854 * of cases where seek() is not available (reading gzip data from a
855 * network socket, for instance), so there needs to be a good way to
856 * communicate whether seek() is available and users of that interface
857 * need to use non-seeking strategies whenever seek() is not available.
861 * Looks ahead in the input stream:
862 * * If 'avail' pointer is provided, that returns number of bytes available
863 * in the current buffer, which may be much larger than requested.
864 * * If end-of-file, *avail gets set to zero.
865 * * If error, *avail gets error code.
866 * * If request can be met, returns pointer to data, returns NULL
867 * if request is not met.
869 * Note: If you just want "some data", ask for 1 byte and pay attention
870 * to *avail, which will have the actual amount available. If you
871 * know exactly how many bytes you need, just ask for that and treat
872 * a NULL return as an error.
874 * Important: This does NOT move the file pointer. See
875 * __archive_read_consume() below.
879 * This is tricky. We need to provide our clients with pointers to
880 * contiguous blocks of memory but we want to avoid copying whenever
883 * Mostly, this code returns pointers directly into the block of data
884 * provided by the client_read routine. It can do this unless the
885 * request would split across blocks. In that case, we have to copy
886 * into an internal buffer to combine reads.
889 __archive_read_ahead(struct archive_read *a, size_t min, ssize_t *avail)
891 return (__archive_read_filter_ahead(a->filter, min, avail));
895 __archive_read_filter_ahead(struct archive_read_filter *filter,
896 size_t min, ssize_t *avail)
903 *avail = ARCHIVE_FATAL;
908 * Keep pulling more data until we can satisfy the request.
913 * If we can satisfy from the copy buffer, we're done.
915 if (filter->avail >= min) {
917 *avail = filter->avail;
918 return (filter->next);
922 * We can satisfy directly from client buffer if everything
923 * currently in the copy buffer is still in the client buffer.
925 if (filter->client_total >= filter->client_avail + filter->avail
926 && filter->client_avail + filter->avail >= min) {
927 /* "Roll back" to client buffer. */
928 filter->client_avail += filter->avail;
929 filter->client_next -= filter->avail;
930 /* Copy buffer is now empty. */
932 filter->next = filter->buffer;
933 /* Return data from client buffer. */
935 *avail = filter->client_avail;
936 return (filter->client_next);
939 /* Move data forward in copy buffer if necessary. */
940 if (filter->next > filter->buffer &&
941 filter->next + min > filter->buffer + filter->buffer_size) {
942 if (filter->avail > 0)
943 memmove(filter->buffer, filter->next, filter->avail);
944 filter->next = filter->buffer;
947 /* If we've used up the client data, get more. */
948 if (filter->client_avail <= 0) {
949 if (filter->end_of_file) {
954 bytes_read = (filter->read)(filter,
955 &filter->client_buff);
956 if (bytes_read < 0) { /* Read error. */
957 filter->client_total = filter->client_avail = 0;
958 filter->client_next = filter->client_buff = NULL;
961 *avail = ARCHIVE_FATAL;
964 if (bytes_read == 0) { /* Premature end-of-file. */
965 filter->client_total = filter->client_avail = 0;
966 filter->client_next = filter->client_buff = NULL;
967 filter->end_of_file = 1;
968 /* Return whatever we do have. */
970 *avail = filter->avail;
973 filter->position += bytes_read;
974 filter->client_total = bytes_read;
975 filter->client_avail = filter->client_total;
976 filter->client_next = filter->client_buff;
981 * We can't satisfy the request from the copy
982 * buffer or the existing client data, so we
983 * need to copy more client data over to the
987 /* Ensure the buffer is big enough. */
988 if (min > filter->buffer_size) {
992 /* Double the buffer; watch for overflow. */
993 s = t = filter->buffer_size;
998 if (t <= s) { /* Integer overflow! */
1000 &filter->archive->archive,
1002 "Unable to allocate copy buffer");
1005 *avail = ARCHIVE_FATAL;
1010 /* Now s >= min, so allocate a new buffer. */
1011 p = (char *)malloc(s);
1014 &filter->archive->archive,
1016 "Unable to allocate copy buffer");
1019 *avail = ARCHIVE_FATAL;
1022 /* Move data into newly-enlarged buffer. */
1023 if (filter->avail > 0)
1024 memmove(p, filter->next, filter->avail);
1025 free(filter->buffer);
1026 filter->next = filter->buffer = p;
1027 filter->buffer_size = s;
1030 /* We can add client data to copy buffer. */
1031 /* First estimate: copy to fill rest of buffer. */
1032 tocopy = (filter->buffer + filter->buffer_size)
1033 - (filter->next + filter->avail);
1034 /* Don't waste time buffering more than we need to. */
1035 if (tocopy + filter->avail > min)
1036 tocopy = min - filter->avail;
1037 /* Don't copy more than is available. */
1038 if (tocopy > filter->client_avail)
1039 tocopy = filter->client_avail;
1041 memcpy(filter->next + filter->avail, filter->client_next,
1043 /* Remove this data from client buffer. */
1044 filter->client_next += tocopy;
1045 filter->client_avail -= tocopy;
1046 /* add it to copy buffer. */
1047 filter->avail += tocopy;
1053 * Move the file pointer forward. This should be called after
1054 * __archive_read_ahead() returns data to you. Don't try to move
1055 * ahead by more than the amount of data available according to
1056 * __archive_read_ahead().
1059 * Mark the appropriate data as used. Note that the request here will
1060 * often be much smaller than the size of the previous read_ahead
1064 __archive_read_consume(struct archive_read *a, size_t request)
1067 r = __archive_read_filter_consume(a->filter, request);
1068 a->archive.file_position += r;
1073 __archive_read_filter_consume(struct archive_read_filter * filter,
1076 if (filter->avail > 0) {
1077 /* Read came from copy buffer. */
1078 filter->next += request;
1079 filter->avail -= request;
1081 /* Read came from client buffer. */
1082 filter->client_next += request;
1083 filter->client_avail -= request;
1089 * Move the file pointer ahead by an arbitrary amount. If you're
1090 * reading uncompressed data from a disk file, this will actually
1091 * translate into a seek() operation. Even in cases where seek()
1092 * isn't feasible, this at least pushes the read-and-discard loop
1093 * down closer to the data source.
1096 __archive_read_skip(struct archive_read *a, int64_t request)
1098 return (__archive_read_filter_skip(a->filter, request));
1102 __archive_read_filter_skip(struct archive_read_filter *filter, int64_t request)
1104 off_t bytes_skipped, total_bytes_skipped = 0;
1110 * If there is data in the buffers already, use that first.
1112 if (filter->avail > 0) {
1113 min = minimum(request, (off_t)filter->avail);
1114 bytes_skipped = __archive_read_consume(filter->archive, min);
1115 request -= bytes_skipped;
1116 total_bytes_skipped += bytes_skipped;
1118 if (filter->client_avail > 0) {
1119 min = minimum(request, (off_t)filter->client_avail);
1120 bytes_skipped = __archive_read_consume(filter->archive, min);
1121 request -= bytes_skipped;
1122 total_bytes_skipped += bytes_skipped;
1125 return (total_bytes_skipped);
1127 * If a client_skipper was provided, try that first.
1129 #if ARCHIVE_API_VERSION < 2
1130 if ((filter->skip != NULL) && (request < SSIZE_MAX)) {
1132 if (filter->skip != NULL) {
1134 bytes_skipped = (filter->skip)(filter, request);
1135 if (bytes_skipped < 0) { /* error */
1136 filter->client_total = filter->client_avail = 0;
1137 filter->client_next = filter->client_buff = NULL;
1139 return (bytes_skipped);
1141 filter->archive->archive.file_position += bytes_skipped;
1142 total_bytes_skipped += bytes_skipped;
1143 request -= bytes_skipped;
1144 filter->client_next = filter->client_buff;
1145 filter->client_avail = filter->client_total = 0;
1148 * Note that client_skipper will usually not satisfy the
1149 * full request (due to low-level blocking concerns),
1150 * so even if client_skipper is provided, we may still
1151 * have to use ordinary reads to finish out the request.
1153 while (request > 0) {
1154 const void* dummy_buffer;
1156 dummy_buffer = __archive_read_ahead(filter->archive,
1159 return (bytes_read);
1160 if (bytes_read == 0) {
1161 /* We hit EOF before we satisfied the skip request. */
1162 archive_set_error(&filter->archive->archive,
1164 "Truncated input file (need to skip %jd bytes)",
1166 return (ARCHIVE_FATAL);
1168 min = (size_t)(minimum(bytes_read, request));
1169 bytes_read = __archive_read_consume(filter->archive, min);
1170 total_bytes_skipped += bytes_read;
1171 request -= bytes_read;
1173 return (total_bytes_skipped);