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];
124 if (s == NULL || *s == '\0')
126 a = (struct archive_read *)_a;
127 __archive_check_magic(&a->archive, ARCHIVE_READ_MAGIC,
128 ARCHIVE_STATE_NEW, "archive_read_set_format_options");
130 for (i = 0; i < sizeof(a->formats)/sizeof(a->formats[0]); i++) {
131 format = &a->formats[i];
132 if (format == NULL || format->options == NULL ||
133 format->name == NULL)
134 /* This format does not support option. */
137 while ((len = __archive_parse_options(s, format->name,
138 sizeof(key), key, sizeof(val), val)) > 0) {
139 valp = val[0] == '\0' ? NULL : val;
141 r = format->options(a, key, valp);
143 if (r == ARCHIVE_FATAL)
149 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
150 "Illegal format options.");
151 return (ARCHIVE_WARN);
157 * Set read options for the filter.
160 archive_read_set_filter_options(struct archive *_a, const char *s)
162 struct archive_read *a;
163 struct archive_read_filter *filter;
164 struct archive_read_filter_bidder *bidder;
165 char key[64], val[64];
168 if (s == NULL || *s == '\0')
170 a = (struct archive_read *)_a;
171 __archive_check_magic(&a->archive, ARCHIVE_READ_MAGIC,
172 ARCHIVE_STATE_NEW, "archive_read_set_filter_options");
175 for (filter = a->filter; filter != NULL; filter = filter->upstream) {
176 bidder = filter->bidder;
179 if (bidder->options == NULL)
180 /* This bidder does not support option */
182 while ((len = __archive_parse_options(s, filter->name,
183 sizeof(key), key, sizeof(val), val)) > 0) {
185 r = bidder->options(bidder, key, NULL);
187 r = bidder->options(bidder, key, val);
188 if (r == ARCHIVE_FATAL)
194 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
195 "Illegal format options.");
196 return (ARCHIVE_WARN);
202 * Set read options for the format and the filter.
205 archive_read_set_options(struct archive *_a, const char *s)
209 r = archive_read_set_format_options(_a, s);
212 r = archive_read_set_filter_options(_a, s);
222 archive_read_open(struct archive *a, void *client_data,
223 archive_open_callback *client_opener, archive_read_callback *client_reader,
224 archive_close_callback *client_closer)
226 /* Old archive_read_open() is just a thin shell around
227 * archive_read_open2. */
228 return archive_read_open2(a, client_data, client_opener,
229 client_reader, NULL, client_closer);
233 client_read_proxy(struct archive_read_filter *self, const void **buff)
236 r = (self->archive->client.reader)(&self->archive->archive,
238 self->archive->archive.raw_position += r;
243 client_skip_proxy(struct archive_read_filter *self, int64_t request)
246 if (self->archive->client.skipper == NULL)
248 r = (self->archive->client.skipper)(&self->archive->archive,
249 self->data, request);
250 self->archive->archive.raw_position += r;
255 client_close_proxy(struct archive_read_filter *self)
259 if (self->archive->client.closer != NULL)
260 r = (self->archive->client.closer)((struct archive *)self->archive,
268 archive_read_open2(struct archive *_a, void *client_data,
269 archive_open_callback *client_opener,
270 archive_read_callback *client_reader,
271 archive_skip_callback *client_skipper,
272 archive_close_callback *client_closer)
274 struct archive_read *a = (struct archive_read *)_a;
275 struct archive_read_filter *filter;
278 __archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
279 "archive_read_open");
281 if (client_reader == NULL)
283 "No reader function provided to archive_read_open");
285 /* Open data source. */
286 if (client_opener != NULL) {
287 e =(client_opener)(&a->archive, client_data);
289 /* If the open failed, call the closer to clean up. */
291 (client_closer)(&a->archive, client_data);
296 /* Save the client functions and mock up the initial source. */
297 a->client.reader = client_reader;
298 a->client.skipper = client_skipper;
299 a->client.closer = client_closer;
301 filter = calloc(1, sizeof(*filter));
303 return (ARCHIVE_FATAL);
304 filter->bidder = NULL;
305 filter->upstream = NULL;
307 filter->data = client_data;
308 filter->read = client_read_proxy;
309 filter->skip = client_skip_proxy;
310 filter->close = client_close_proxy;
311 filter->name = "none";
312 filter->code = ARCHIVE_COMPRESSION_NONE;
315 /* Build out the input pipeline. */
318 a->archive.state = ARCHIVE_STATE_HEADER;
324 * Allow each registered stream transform to bid on whether
325 * it wants to handle this stream. Repeat until we've finished
326 * building the pipeline.
329 build_stream(struct archive_read *a)
331 int number_bidders, i, bid, best_bid;
332 struct archive_read_filter_bidder *bidder, *best_bidder;
333 struct archive_read_filter *filter;
337 number_bidders = sizeof(a->bidders) / sizeof(a->bidders[0]);
343 for (i = 0; i < number_bidders; i++, bidder++) {
344 if (bidder->bid != NULL) {
345 bid = (bidder->bid)(bidder, a->filter);
346 if (bid > best_bid) {
348 best_bidder = bidder;
353 /* If no bidder, we're done. */
354 if (best_bidder == NULL) {
355 a->archive.compression_name = a->filter->name;
356 a->archive.compression_code = a->filter->code;
361 = (struct archive_read_filter *)calloc(1, sizeof(*filter));
363 return (ARCHIVE_FATAL);
364 filter->bidder = best_bidder;
366 filter->upstream = a->filter;
367 r = (best_bidder->init)(filter);
368 if (r != ARCHIVE_OK) {
377 * Read header of next entry.
380 archive_read_next_header2(struct archive *_a, struct archive_entry *entry)
382 struct archive_read *a = (struct archive_read *)_a;
385 __archive_check_magic(_a, ARCHIVE_READ_MAGIC,
386 ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
387 "archive_read_next_header");
389 archive_entry_clear(entry);
390 archive_clear_error(&a->archive);
393 * If no format has yet been chosen, choose one.
395 if (a->format == NULL) {
396 slot = choose_format(a);
398 a->archive.state = ARCHIVE_STATE_FATAL;
399 return (ARCHIVE_FATAL);
401 a->format = &(a->formats[slot]);
405 * If client didn't consume entire data, skip any remainder
406 * (This is especially important for GNU incremental directories.)
408 if (a->archive.state == ARCHIVE_STATE_DATA) {
409 ret = archive_read_data_skip(&a->archive);
410 if (ret == ARCHIVE_EOF) {
411 archive_set_error(&a->archive, EIO, "Premature end-of-file.");
412 a->archive.state = ARCHIVE_STATE_FATAL;
413 return (ARCHIVE_FATAL);
415 if (ret != ARCHIVE_OK)
419 /* Record start-of-header. */
420 a->header_position = a->archive.file_position;
422 ret = (a->format->read_header)(a, entry);
425 * EOF and FATAL are persistent at this layer. By
426 * modifying the state, we guarantee that future calls to
427 * read a header or read data will fail.
431 a->archive.state = ARCHIVE_STATE_EOF;
434 a->archive.state = ARCHIVE_STATE_DATA;
437 a->archive.state = ARCHIVE_STATE_DATA;
442 a->archive.state = ARCHIVE_STATE_FATAL;
446 a->read_data_output_offset = 0;
447 a->read_data_remaining = 0;
452 archive_read_next_header(struct archive *_a, struct archive_entry **entryp)
455 struct archive_read *a = (struct archive_read *)_a;
457 ret = archive_read_next_header2(_a, a->entry);
463 * Allow each registered format to bid on whether it wants to handle
464 * the next entry. Return index of winning bidder.
467 choose_format(struct archive_read *a)
474 slots = sizeof(a->formats) / sizeof(a->formats[0]);
478 /* Set up a->format and a->pformat_data for convenience of bidders. */
479 a->format = &(a->formats[0]);
480 for (i = 0; i < slots; i++, a->format++) {
481 if (a->format->bid) {
482 bid = (a->format->bid)(a);
483 if (bid == ARCHIVE_FATAL)
484 return (ARCHIVE_FATAL);
485 if ((bid > best_bid) || (best_bid_slot < 0)) {
493 * There were no bidders; this is a serious programmer error
494 * and demands a quick and definitive abort.
496 if (best_bid_slot < 0)
497 __archive_errx(1, "No formats were registered; you must "
498 "invoke at least one archive_read_support_format_XXX "
499 "function in order to successfully read an archive.");
502 * There were bidders, but no non-zero bids; this means we
503 * can't support this stream.
506 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
507 "Unrecognized archive format");
508 return (ARCHIVE_FATAL);
511 return (best_bid_slot);
515 * Return the file offset (within the uncompressed data stream) where
516 * the last header started.
519 archive_read_header_position(struct archive *_a)
521 struct archive_read *a = (struct archive_read *)_a;
522 __archive_check_magic(_a, ARCHIVE_READ_MAGIC,
523 ARCHIVE_STATE_ANY, "archive_read_header_position");
524 return (a->header_position);
528 * Read data from an archive entry, using a read(2)-style interface.
529 * This is a convenience routine that just calls
530 * archive_read_data_block and copies the results into the client
531 * buffer, filling any gaps with zero bytes. Clients using this
532 * API can be completely ignorant of sparse-file issues; sparse files
533 * will simply be padded with nulls.
535 * DO NOT intermingle calls to this function and archive_read_data_block
536 * to read a single entry body.
539 archive_read_data(struct archive *_a, void *buff, size_t s)
541 struct archive_read *a = (struct archive_read *)_a;
543 const void *read_buf;
552 if (a->read_data_remaining == 0) {
553 read_buf = a->read_data_block;
554 r = archive_read_data_block(&a->archive, &read_buf,
555 &a->read_data_remaining, &a->read_data_offset);
556 a->read_data_block = read_buf;
557 if (r == ARCHIVE_EOF)
560 * Error codes are all negative, so the status
561 * return here cannot be confused with a valid
562 * byte count. (ARCHIVE_OK is zero.)
568 if (a->read_data_offset < a->read_data_output_offset) {
569 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
570 "Encountered out-of-order sparse blocks");
571 return (ARCHIVE_RETRY);
574 /* Compute the amount of zero padding needed. */
575 if (a->read_data_output_offset + (off_t)s <
576 a->read_data_offset) {
578 } else if (a->read_data_output_offset <
579 a->read_data_offset) {
580 len = a->read_data_offset -
581 a->read_data_output_offset;
586 memset(dest, 0, len);
588 a->read_data_output_offset += len;
592 /* Copy data if there is any space left. */
594 len = a->read_data_remaining;
597 memcpy(dest, a->read_data_block, len);
599 a->read_data_block += len;
600 a->read_data_remaining -= len;
601 a->read_data_output_offset += len;
602 a->read_data_offset += len;
610 #if ARCHIVE_API_VERSION < 3
612 * Obsolete function provided for compatibility only. Note that the API
613 * of this function doesn't allow the caller to detect if the remaining
614 * data from the archive entry is shorter than the buffer provided, or
615 * even if an error occurred while reading data.
618 archive_read_data_into_buffer(struct archive *a, void *d, ssize_t len)
621 archive_read_data(a, d, len);
627 * Skip over all remaining data in this entry.
630 archive_read_data_skip(struct archive *_a)
632 struct archive_read *a = (struct archive_read *)_a;
638 __archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA,
639 "archive_read_data_skip");
641 if (a->format->read_data_skip != NULL)
642 r = (a->format->read_data_skip)(a);
644 while ((r = archive_read_data_block(&a->archive,
645 &buff, &size, &offset))
650 if (r == ARCHIVE_EOF)
653 a->archive.state = ARCHIVE_STATE_HEADER;
658 * Read the next block of entry data from the archive.
659 * This is a zero-copy interface; the client receives a pointer,
660 * size, and file offset of the next available block of data.
662 * Returns ARCHIVE_OK if the operation is successful, ARCHIVE_EOF if
663 * the end of entry is encountered.
666 archive_read_data_block(struct archive *_a,
667 const void **buff, size_t *size, off_t *offset)
669 struct archive_read *a = (struct archive_read *)_a;
670 __archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA,
671 "archive_read_data_block");
673 if (a->format->read_data == NULL) {
674 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
676 "No format_read_data_block function registered");
677 return (ARCHIVE_FATAL);
680 return (a->format->read_data)(a, buff, size, offset);
684 * Close the file and release most resources.
686 * Be careful: client might just call read_new and then read_finish.
687 * Don't assume we actually read anything or performed any non-trivial
691 _archive_read_close(struct archive *_a)
693 struct archive_read *a = (struct archive_read *)_a;
694 int r = ARCHIVE_OK, r1 = ARCHIVE_OK;
697 __archive_check_magic(&a->archive, ARCHIVE_READ_MAGIC,
698 ARCHIVE_STATE_ANY, "archive_read_close");
699 archive_clear_error(&a->archive);
700 a->archive.state = ARCHIVE_STATE_CLOSED;
703 /* Call cleanup functions registered by optional components. */
704 if (a->cleanup_archive_extract != NULL)
705 r = (a->cleanup_archive_extract)(a);
707 /* TODO: Clean up the formatters. */
709 /* Clean up the filter pipeline. */
710 while (a->filter != NULL) {
711 struct archive_read_filter *t = a->filter->upstream;
712 if (a->filter->close != NULL) {
713 r1 = (a->filter->close)(a->filter);
717 free(a->filter->buffer);
722 /* Release the bidder objects. */
723 n = sizeof(a->bidders)/sizeof(a->bidders[0]);
724 for (i = 0; i < n; i++) {
725 if (a->bidders[i].free != NULL) {
726 r1 = (a->bidders[i].free)(&a->bidders[i]);
736 * Release memory and other resources.
739 _archive_read_finish(struct archive *_a)
741 struct archive_read *a = (struct archive_read *)_a;
746 __archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_ANY,
747 "archive_read_finish");
748 if (a->archive.state != ARCHIVE_STATE_CLOSED)
749 r = archive_read_close(&a->archive);
751 /* Cleanup format-specific data. */
752 slots = sizeof(a->formats) / sizeof(a->formats[0]);
753 for (i = 0; i < slots; i++) {
754 a->format = &(a->formats[i]);
755 if (a->formats[i].cleanup)
756 (a->formats[i].cleanup)(a);
759 archive_string_free(&a->archive.error_string);
761 archive_entry_free(a->entry);
762 a->archive.magic = 0;
764 #if ARCHIVE_API_VERSION > 1
770 * Used internally by read format handlers to register their bid and
771 * initialization functions.
774 __archive_read_register_format(struct archive_read *a,
777 int (*bid)(struct archive_read *),
778 int (*options)(struct archive_read *, const char *, const char *),
779 int (*read_header)(struct archive_read *, struct archive_entry *),
780 int (*read_data)(struct archive_read *, const void **, size_t *, off_t *),
781 int (*read_data_skip)(struct archive_read *),
782 int (*cleanup)(struct archive_read *))
786 __archive_check_magic(&a->archive,
787 ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
788 "__archive_read_register_format");
790 number_slots = sizeof(a->formats) / sizeof(a->formats[0]);
792 for (i = 0; i < number_slots; i++) {
793 if (a->formats[i].bid == bid)
794 return (ARCHIVE_WARN); /* We've already installed */
795 if (a->formats[i].bid == NULL) {
796 a->formats[i].bid = bid;
797 a->formats[i].options = options;
798 a->formats[i].read_header = read_header;
799 a->formats[i].read_data = read_data;
800 a->formats[i].read_data_skip = read_data_skip;
801 a->formats[i].cleanup = cleanup;
802 a->formats[i].data = format_data;
803 a->formats[i].name = name;
808 __archive_errx(1, "Not enough slots for format registration");
809 return (ARCHIVE_FATAL); /* Never actually called. */
813 * Used internally by decompression routines to register their bid and
814 * initialization functions.
816 struct archive_read_filter_bidder *
817 __archive_read_get_bidder(struct archive_read *a)
821 __archive_check_magic(&a->archive,
822 ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
823 "__archive_read_get_bidder");
825 number_slots = sizeof(a->bidders) / sizeof(a->bidders[0]);
827 for (i = 0; i < number_slots; i++) {
828 if (a->bidders[i].bid == NULL) {
829 memset(a->bidders + i, 0, sizeof(a->bidders[0]));
830 return (a->bidders + i);
834 __archive_errx(1, "Not enough slots for compression registration");
835 return (NULL); /* Never actually executed. */
839 * The next three functions comprise the peek/consume internal I/O
840 * system used by archive format readers. This system allows fairly
841 * flexible read-ahead and allows the I/O code to operate in a
842 * zero-copy manner most of the time.
844 * In the ideal case, filters generate blocks of data
845 * and __archive_read_ahead() just returns pointers directly into
846 * those blocks. Then __archive_read_consume() just bumps those
847 * pointers. Only if your request would span blocks does the I/O
848 * layer use a copy buffer to provide you with a contiguous block of
849 * data. The __archive_read_skip() is an optimization; it scans ahead
850 * very quickly (it usually translates into a seek() operation if
851 * you're reading uncompressed disk files).
853 * A couple of useful idioms:
854 * * "I just want some data." Ask for 1 byte and pay attention to
855 * the "number of bytes available" from __archive_read_ahead().
856 * You can consume more than you asked for; you just can't consume
857 * more than is available. If you consume everything that's
858 * immediately available, the next read_ahead() call will pull
860 * * "I want to output a large block of data." As above, ask for 1 byte,
861 * emit all that's available (up to whatever limit you have), then
862 * repeat until you're done.
863 * * "I want to peek ahead by a large amount." Ask for 4k or so, then
864 * double and repeat until you get an error or have enough. Note
865 * that the I/O layer will likely end up expanding its copy buffer
866 * to fit your request, so use this technique cautiously. This
867 * technique is used, for example, by some of the format tasting
868 * code that has uncertain look-ahead needs.
870 * TODO: Someday, provide a more generic __archive_read_seek() for
871 * those cases where it's useful. This is tricky because there are lots
872 * of cases where seek() is not available (reading gzip data from a
873 * network socket, for instance), so there needs to be a good way to
874 * communicate whether seek() is available and users of that interface
875 * need to use non-seeking strategies whenever seek() is not available.
879 * Looks ahead in the input stream:
880 * * If 'avail' pointer is provided, that returns number of bytes available
881 * in the current buffer, which may be much larger than requested.
882 * * If end-of-file, *avail gets set to zero.
883 * * If error, *avail gets error code.
884 * * If request can be met, returns pointer to data, returns NULL
885 * if request is not met.
887 * Note: If you just want "some data", ask for 1 byte and pay attention
888 * to *avail, which will have the actual amount available. If you
889 * know exactly how many bytes you need, just ask for that and treat
890 * a NULL return as an error.
892 * Important: This does NOT move the file pointer. See
893 * __archive_read_consume() below.
897 * This is tricky. We need to provide our clients with pointers to
898 * contiguous blocks of memory but we want to avoid copying whenever
901 * Mostly, this code returns pointers directly into the block of data
902 * provided by the client_read routine. It can do this unless the
903 * request would split across blocks. In that case, we have to copy
904 * into an internal buffer to combine reads.
907 __archive_read_ahead(struct archive_read *a, size_t min, ssize_t *avail)
909 return (__archive_read_filter_ahead(a->filter, min, avail));
913 __archive_read_filter_ahead(struct archive_read_filter *filter,
914 size_t min, ssize_t *avail)
921 *avail = ARCHIVE_FATAL;
926 * Keep pulling more data until we can satisfy the request.
931 * If we can satisfy from the copy buffer (and the
932 * copy buffer isn't empty), we're done. In particular,
933 * note that min == 0 is a perfectly well-defined
936 if (filter->avail >= min && filter->avail > 0) {
938 *avail = filter->avail;
939 return (filter->next);
943 * We can satisfy directly from client buffer if everything
944 * currently in the copy buffer is still in the client buffer.
946 if (filter->client_total >= filter->client_avail + filter->avail
947 && filter->client_avail + filter->avail >= min) {
948 /* "Roll back" to client buffer. */
949 filter->client_avail += filter->avail;
950 filter->client_next -= filter->avail;
951 /* Copy buffer is now empty. */
953 filter->next = filter->buffer;
954 /* Return data from client buffer. */
956 *avail = filter->client_avail;
957 return (filter->client_next);
960 /* Move data forward in copy buffer if necessary. */
961 if (filter->next > filter->buffer &&
962 filter->next + min > filter->buffer + filter->buffer_size) {
963 if (filter->avail > 0)
964 memmove(filter->buffer, filter->next, filter->avail);
965 filter->next = filter->buffer;
968 /* If we've used up the client data, get more. */
969 if (filter->client_avail <= 0) {
970 if (filter->end_of_file) {
975 bytes_read = (filter->read)(filter,
976 &filter->client_buff);
977 if (bytes_read < 0) { /* Read error. */
978 filter->client_total = filter->client_avail = 0;
979 filter->client_next = filter->client_buff = NULL;
982 *avail = ARCHIVE_FATAL;
985 if (bytes_read == 0) { /* Premature end-of-file. */
986 filter->client_total = filter->client_avail = 0;
987 filter->client_next = filter->client_buff = NULL;
988 filter->end_of_file = 1;
989 /* Return whatever we do have. */
991 *avail = filter->avail;
994 filter->position += bytes_read;
995 filter->client_total = bytes_read;
996 filter->client_avail = filter->client_total;
997 filter->client_next = filter->client_buff;
1002 * We can't satisfy the request from the copy
1003 * buffer or the existing client data, so we
1004 * need to copy more client data over to the
1008 /* Ensure the buffer is big enough. */
1009 if (min > filter->buffer_size) {
1013 /* Double the buffer; watch for overflow. */
1014 s = t = filter->buffer_size;
1019 if (t <= s) { /* Integer overflow! */
1021 &filter->archive->archive,
1023 "Unable to allocate copy buffer");
1026 *avail = ARCHIVE_FATAL;
1031 /* Now s >= min, so allocate a new buffer. */
1032 p = (char *)malloc(s);
1035 &filter->archive->archive,
1037 "Unable to allocate copy buffer");
1040 *avail = ARCHIVE_FATAL;
1043 /* Move data into newly-enlarged buffer. */
1044 if (filter->avail > 0)
1045 memmove(p, filter->next, filter->avail);
1046 free(filter->buffer);
1047 filter->next = filter->buffer = p;
1048 filter->buffer_size = s;
1051 /* We can add client data to copy buffer. */
1052 /* First estimate: copy to fill rest of buffer. */
1053 tocopy = (filter->buffer + filter->buffer_size)
1054 - (filter->next + filter->avail);
1055 /* Don't waste time buffering more than we need to. */
1056 if (tocopy + filter->avail > min)
1057 tocopy = min - filter->avail;
1058 /* Don't copy more than is available. */
1059 if (tocopy > filter->client_avail)
1060 tocopy = filter->client_avail;
1062 memcpy(filter->next + filter->avail, filter->client_next,
1064 /* Remove this data from client buffer. */
1065 filter->client_next += tocopy;
1066 filter->client_avail -= tocopy;
1067 /* add it to copy buffer. */
1068 filter->avail += tocopy;
1074 * Move the file pointer forward. This should be called after
1075 * __archive_read_ahead() returns data to you. Don't try to move
1076 * ahead by more than the amount of data available according to
1077 * __archive_read_ahead().
1080 * Mark the appropriate data as used. Note that the request here will
1081 * often be much smaller than the size of the previous read_ahead
1085 __archive_read_consume(struct archive_read *a, size_t request)
1088 r = __archive_read_filter_consume(a->filter, request);
1089 a->archive.file_position += r;
1094 __archive_read_filter_consume(struct archive_read_filter * filter,
1097 if (filter->avail > 0) {
1098 /* Read came from copy buffer. */
1099 filter->next += request;
1100 filter->avail -= request;
1102 /* Read came from client buffer. */
1103 filter->client_next += request;
1104 filter->client_avail -= request;
1110 * Move the file pointer ahead by an arbitrary amount. If you're
1111 * reading uncompressed data from a disk file, this will actually
1112 * translate into a seek() operation. Even in cases where seek()
1113 * isn't feasible, this at least pushes the read-and-discard loop
1114 * down closer to the data source.
1117 __archive_read_skip(struct archive_read *a, int64_t request)
1119 int64_t skipped = __archive_read_skip_lenient(a, request);
1120 if (skipped == request)
1122 /* We hit EOF before we satisfied the skip request. */
1123 archive_set_error(&a->archive,
1125 "Truncated input file (needed %jd bytes, only %jd available)",
1126 (intmax_t)request, (intmax_t)skipped);
1127 return (ARCHIVE_FATAL);
1131 __archive_read_skip_lenient(struct archive_read *a, int64_t request)
1133 int64_t skipped = __archive_read_filter_skip(a->filter, request);
1135 a->archive.file_position += skipped;
1140 __archive_read_filter_skip(struct archive_read_filter *filter, int64_t request)
1142 off_t bytes_skipped, total_bytes_skipped = 0;
1148 * If there is data in the buffers already, use that first.
1150 if (filter->avail > 0) {
1151 min = minimum(request, (off_t)filter->avail);
1152 bytes_skipped = __archive_read_filter_consume(filter, min);
1153 request -= bytes_skipped;
1154 total_bytes_skipped += bytes_skipped;
1156 if (filter->client_avail > 0) {
1157 min = minimum(request, (off_t)filter->client_avail);
1158 bytes_skipped = __archive_read_filter_consume(filter, min);
1159 request -= bytes_skipped;
1160 total_bytes_skipped += bytes_skipped;
1163 return (total_bytes_skipped);
1165 * If a client_skipper was provided, try that first.
1167 #if ARCHIVE_API_VERSION < 2
1168 if ((filter->skip != NULL) && (request < SSIZE_MAX)) {
1170 if (filter->skip != NULL) {
1172 bytes_skipped = (filter->skip)(filter, request);
1173 if (bytes_skipped < 0) { /* error */
1174 filter->client_total = filter->client_avail = 0;
1175 filter->client_next = filter->client_buff = NULL;
1177 return (bytes_skipped);
1179 total_bytes_skipped += bytes_skipped;
1180 request -= bytes_skipped;
1181 filter->client_next = filter->client_buff;
1182 filter->client_avail = filter->client_total = 0;
1185 * Note that client_skipper will usually not satisfy the
1186 * full request (due to low-level blocking concerns),
1187 * so even if client_skipper is provided, we may still
1188 * have to use ordinary reads to finish out the request.
1190 while (request > 0) {
1191 const void* dummy_buffer;
1193 dummy_buffer = __archive_read_filter_ahead(filter,
1196 return (bytes_read);
1197 if (bytes_read == 0) {
1198 return (total_bytes_skipped);
1200 min = (size_t)(minimum(bytes_read, request));
1201 bytes_read = __archive_read_filter_consume(filter, min);
1202 total_bytes_skipped += bytes_read;
1203 request -= bytes_read;
1205 return (total_bytes_skipped);