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: 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 build_stream(struct archive_read *);
59
static int choose_format(struct archive_read *);
60
static int cleanup_filters(struct archive_read *);
61
static struct archive_vtable *archive_read_vtable(void);
62
static int _archive_read_close(struct archive *);
63
static int _archive_read_finish(struct archive *);
65
static struct archive_vtable *
66
archive_read_vtable(void)
68
static struct archive_vtable av;
69
static int inited = 0;
72
av.archive_finish = _archive_read_finish;
73
av.archive_close = _archive_read_close;
79
* Allocate, initialize and return a struct archive object.
82
archive_read_new(void)
84
struct archive_read *a;
86
a = (struct archive_read *)malloc(sizeof(*a));
89
memset(a, 0, sizeof(*a));
90
a->archive.magic = ARCHIVE_READ_MAGIC;
92
a->archive.state = ARCHIVE_STATE_NEW;
93
a->entry = archive_entry_new();
94
a->archive.vtable = archive_read_vtable();
100
* Record the do-not-extract-to file. This belongs in archive_read_extract.c.
103
archive_read_extract_set_skip_file(struct archive *_a, dev_t d, ino_t i)
105
struct archive_read *a = (struct archive_read *)_a;
106
__archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_ANY,
107
"archive_read_extract_set_skip_file");
108
a->skip_file_dev = d;
109
a->skip_file_ino = i;
113
* Set read options for the format.
116
archive_read_set_format_options(struct archive *_a, const char *s)
118
struct archive_read *a;
119
struct archive_format_descriptor *format;
120
char key[64], val[64];
125
__archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
126
"archive_read_set_format_options");
128
if (s == NULL || *s == '\0')
130
a = (struct archive_read *)_a;
131
__archive_check_magic(&a->archive, ARCHIVE_READ_MAGIC,
132
ARCHIVE_STATE_NEW, "archive_read_set_format_options");
134
for (i = 0; i < sizeof(a->formats)/sizeof(a->formats[0]); i++) {
135
format = &a->formats[i];
136
if (format == NULL || format->options == NULL ||
137
format->name == NULL)
138
/* This format does not support option. */
141
while ((len = __archive_parse_options(s, format->name,
142
sizeof(key), key, sizeof(val), val)) > 0) {
143
valp = val[0] == '\0' ? NULL : val;
145
r = format->options(a, key, valp);
147
if (r == ARCHIVE_FATAL)
153
archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
154
"Illegal format options.");
155
return (ARCHIVE_WARN);
161
* Set read options for the filter.
164
archive_read_set_filter_options(struct archive *_a, const char *s)
166
struct archive_read *a;
167
struct archive_read_filter *filter;
168
struct archive_read_filter_bidder *bidder;
169
char key[64], val[64];
172
__archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
173
"archive_read_set_filter_options");
175
if (s == NULL || *s == '\0')
177
a = (struct archive_read *)_a;
178
__archive_check_magic(&a->archive, ARCHIVE_READ_MAGIC,
179
ARCHIVE_STATE_NEW, "archive_read_set_filter_options");
181
for (filter = a->filter; filter != NULL; filter = filter->upstream) {
182
bidder = filter->bidder;
185
if (bidder->options == NULL)
186
/* This bidder does not support option */
188
while ((len = __archive_parse_options(s, filter->name,
189
sizeof(key), key, sizeof(val), val)) > 0) {
191
r = bidder->options(bidder, key, NULL);
193
r = bidder->options(bidder, key, val);
194
if (r == ARCHIVE_FATAL)
200
archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
201
"Illegal format options.");
202
return (ARCHIVE_WARN);
208
* Set read options for the format and the filter.
211
archive_read_set_options(struct archive *_a, const char *s)
215
__archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
216
"archive_read_set_options");
217
archive_clear_error(_a);
219
r = archive_read_set_format_options(_a, s);
222
r = archive_read_set_filter_options(_a, s);
232
archive_read_open(struct archive *a, void *client_data,
233
archive_open_callback *client_opener, archive_read_callback *client_reader,
234
archive_close_callback *client_closer)
236
/* Old archive_read_open() is just a thin shell around
237
* archive_read_open2. */
238
return archive_read_open2(a, client_data, client_opener,
239
client_reader, NULL, client_closer);
243
client_read_proxy(struct archive_read_filter *self, const void **buff)
246
r = (self->archive->client.reader)(&self->archive->archive,
248
self->archive->archive.raw_position += r;
253
client_skip_proxy(struct archive_read_filter *self, int64_t request)
255
int64_t ask, get, total;
256
/* Limit our maximum seek request to 1GB on platforms
257
* with 32-bit off_t (such as Windows). */
258
int64_t skip_limit = ((int64_t)1) << (sizeof(off_t) * 8 - 2);
260
if (self->archive->client.skipper == NULL)
265
if (ask > skip_limit)
267
get = (self->archive->client.skipper)(&self->archive->archive,
272
self->archive->archive.raw_position += get;
278
client_close_proxy(struct archive_read_filter *self)
282
if (self->archive->client.closer != NULL)
283
r = (self->archive->client.closer)((struct archive *)self->archive,
291
archive_read_open2(struct archive *_a, void *client_data,
292
archive_open_callback *client_opener,
293
archive_read_callback *client_reader,
294
archive_skip_callback *client_skipper,
295
archive_close_callback *client_closer)
297
struct archive_read *a = (struct archive_read *)_a;
298
struct archive_read_filter *filter;
301
__archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
302
"archive_read_open");
303
archive_clear_error(&a->archive);
305
if (client_reader == NULL)
307
"No reader function provided to archive_read_open");
309
/* Open data source. */
310
if (client_opener != NULL) {
311
e =(client_opener)(&a->archive, client_data);
313
/* If the open failed, call the closer to clean up. */
315
(client_closer)(&a->archive, client_data);
320
/* Save the client functions and mock up the initial source. */
321
a->client.reader = client_reader;
322
a->client.skipper = client_skipper;
323
a->client.closer = client_closer;
325
filter = calloc(1, sizeof(*filter));
327
return (ARCHIVE_FATAL);
328
filter->bidder = NULL;
329
filter->upstream = NULL;
331
filter->data = client_data;
332
filter->read = client_read_proxy;
333
filter->skip = client_skip_proxy;
334
filter->close = client_close_proxy;
335
filter->name = "none";
336
filter->code = ARCHIVE_COMPRESSION_NONE;
339
/* Build out the input pipeline. */
342
a->archive.state = ARCHIVE_STATE_HEADER;
348
* Allow each registered stream transform to bid on whether
349
* it wants to handle this stream. Repeat until we've finished
350
* building the pipeline.
353
build_stream(struct archive_read *a)
355
int number_bidders, i, bid, best_bid;
356
struct archive_read_filter_bidder *bidder, *best_bidder;
357
struct archive_read_filter *filter;
362
number_bidders = sizeof(a->bidders) / sizeof(a->bidders[0]);
368
for (i = 0; i < number_bidders; i++, bidder++) {
369
if (bidder->bid != NULL) {
370
bid = (bidder->bid)(bidder, a->filter);
371
if (bid > best_bid) {
373
best_bidder = bidder;
378
/* If no bidder, we're done. */
379
if (best_bidder == NULL) {
380
a->archive.compression_name = a->filter->name;
381
a->archive.compression_code = a->filter->code;
386
= (struct archive_read_filter *)calloc(1, sizeof(*filter));
388
return (ARCHIVE_FATAL);
389
filter->bidder = best_bidder;
391
filter->upstream = a->filter;
392
r = (best_bidder->init)(filter);
393
if (r != ARCHIVE_OK) {
398
/* Verify the filter by asking it for some data. */
399
__archive_read_filter_ahead(filter, 1, &avail);
402
return (ARCHIVE_FATAL);
408
* Read header of next entry.
411
archive_read_next_header2(struct archive *_a, struct archive_entry *entry)
413
struct archive_read *a = (struct archive_read *)_a;
416
__archive_check_magic(_a, ARCHIVE_READ_MAGIC,
417
ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
418
"archive_read_next_header");
421
archive_entry_clear(entry);
422
archive_clear_error(&a->archive);
425
* If no format has yet been chosen, choose one.
427
if (a->format == NULL) {
428
slot = choose_format(a);
430
a->archive.state = ARCHIVE_STATE_FATAL;
431
return (ARCHIVE_FATAL);
433
a->format = &(a->formats[slot]);
437
* If client didn't consume entire data, skip any remainder
438
* (This is especially important for GNU incremental directories.)
440
if (a->archive.state == ARCHIVE_STATE_DATA) {
441
ret = archive_read_data_skip(&a->archive);
442
if (ret == ARCHIVE_EOF) {
443
archive_set_error(&a->archive, EIO, "Premature end-of-file.");
444
a->archive.state = ARCHIVE_STATE_FATAL;
445
return (ARCHIVE_FATAL);
447
if (ret != ARCHIVE_OK)
451
/* Record start-of-header. */
452
a->header_position = a->archive.file_position;
454
ret = (a->format->read_header)(a, entry);
457
* EOF and FATAL are persistent at this layer. By
458
* modifying the state, we guarantee that future calls to
459
* read a header or read data will fail.
463
a->archive.state = ARCHIVE_STATE_EOF;
466
a->archive.state = ARCHIVE_STATE_DATA;
469
a->archive.state = ARCHIVE_STATE_DATA;
474
a->archive.state = ARCHIVE_STATE_FATAL;
478
a->read_data_output_offset = 0;
479
a->read_data_remaining = 0;
484
archive_read_next_header(struct archive *_a, struct archive_entry **entryp)
487
struct archive_read *a = (struct archive_read *)_a;
489
ret = archive_read_next_header2(_a, a->entry);
495
* Allow each registered format to bid on whether it wants to handle
496
* the next entry. Return index of winning bidder.
499
choose_format(struct archive_read *a)
506
slots = sizeof(a->formats) / sizeof(a->formats[0]);
510
/* Set up a->format and a->pformat_data for convenience of bidders. */
511
a->format = &(a->formats[0]);
512
for (i = 0; i < slots; i++, a->format++) {
513
if (a->format->bid) {
514
bid = (a->format->bid)(a);
515
if (bid == ARCHIVE_FATAL)
516
return (ARCHIVE_FATAL);
517
if ((bid > best_bid) || (best_bid_slot < 0)) {
525
* There were no bidders; this is a serious programmer error
526
* and demands a quick and definitive abort.
528
if (best_bid_slot < 0)
529
__archive_errx(1, "No formats were registered; you must "
530
"invoke at least one archive_read_support_format_XXX "
531
"function in order to successfully read an archive.");
534
* There were bidders, but no non-zero bids; this means we
535
* can't support this stream.
538
archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
539
"Unrecognized archive format");
540
return (ARCHIVE_FATAL);
543
return (best_bid_slot);
547
* Return the file offset (within the uncompressed data stream) where
548
* the last header started.
551
archive_read_header_position(struct archive *_a)
553
struct archive_read *a = (struct archive_read *)_a;
554
__archive_check_magic(_a, ARCHIVE_READ_MAGIC,
555
ARCHIVE_STATE_ANY, "archive_read_header_position");
556
return (a->header_position);
560
* Read data from an archive entry, using a read(2)-style interface.
561
* This is a convenience routine that just calls
562
* archive_read_data_block and copies the results into the client
563
* buffer, filling any gaps with zero bytes. Clients using this
564
* API can be completely ignorant of sparse-file issues; sparse files
565
* will simply be padded with nulls.
567
* DO NOT intermingle calls to this function and archive_read_data_block
568
* to read a single entry body.
571
archive_read_data(struct archive *_a, void *buff, size_t s)
573
struct archive_read *a = (struct archive_read *)_a;
575
const void *read_buf;
584
if (a->read_data_remaining == 0) {
585
read_buf = a->read_data_block;
586
r = archive_read_data_block(&a->archive, &read_buf,
587
&a->read_data_remaining, &a->read_data_offset);
588
a->read_data_block = read_buf;
589
if (r == ARCHIVE_EOF)
592
* Error codes are all negative, so the status
593
* return here cannot be confused with a valid
594
* byte count. (ARCHIVE_OK is zero.)
600
if (a->read_data_offset < a->read_data_output_offset) {
601
archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
602
"Encountered out-of-order sparse blocks");
603
return (ARCHIVE_RETRY);
606
/* Compute the amount of zero padding needed. */
607
if (a->read_data_output_offset + (off_t)s <
608
a->read_data_offset) {
610
} else if (a->read_data_output_offset <
611
a->read_data_offset) {
612
len = a->read_data_offset -
613
a->read_data_output_offset;
618
memset(dest, 0, len);
620
a->read_data_output_offset += len;
624
/* Copy data if there is any space left. */
626
len = a->read_data_remaining;
629
memcpy(dest, a->read_data_block, len);
631
a->read_data_block += len;
632
a->read_data_remaining -= len;
633
a->read_data_output_offset += len;
634
a->read_data_offset += len;
642
#if ARCHIVE_API_VERSION < 3
644
* Obsolete function provided for compatibility only. Note that the API
645
* of this function doesn't allow the caller to detect if the remaining
646
* data from the archive entry is shorter than the buffer provided, or
647
* even if an error occurred while reading data.
650
archive_read_data_into_buffer(struct archive *a, void *d, ssize_t len)
653
archive_read_data(a, d, len);
659
* Skip over all remaining data in this entry.
662
archive_read_data_skip(struct archive *_a)
664
struct archive_read *a = (struct archive_read *)_a;
670
__archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA,
671
"archive_read_data_skip");
673
if (a->format->read_data_skip != NULL)
674
r = (a->format->read_data_skip)(a);
676
while ((r = archive_read_data_block(&a->archive,
677
&buff, &size, &offset))
682
if (r == ARCHIVE_EOF)
685
a->archive.state = ARCHIVE_STATE_HEADER;
690
* Read the next block of entry data from the archive.
691
* This is a zero-copy interface; the client receives a pointer,
692
* size, and file offset of the next available block of data.
694
* Returns ARCHIVE_OK if the operation is successful, ARCHIVE_EOF if
695
* the end of entry is encountered.
698
archive_read_data_block(struct archive *_a,
699
const void **buff, size_t *size, off_t *offset)
701
struct archive_read *a = (struct archive_read *)_a;
702
__archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA,
703
"archive_read_data_block");
705
if (a->format->read_data == NULL) {
706
archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
708
"No format_read_data_block function registered");
709
return (ARCHIVE_FATAL);
712
return (a->format->read_data)(a, buff, size, offset);
716
* Close the file and release most resources.
718
* Be careful: client might just call read_new and then read_finish.
719
* Don't assume we actually read anything or performed any non-trivial
723
_archive_read_close(struct archive *_a)
725
struct archive_read *a = (struct archive_read *)_a;
726
int r = ARCHIVE_OK, r1 = ARCHIVE_OK;
729
__archive_check_magic(&a->archive, ARCHIVE_READ_MAGIC,
730
ARCHIVE_STATE_ANY, "archive_read_close");
731
archive_clear_error(&a->archive);
732
a->archive.state = ARCHIVE_STATE_CLOSED;
735
/* Call cleanup functions registered by optional components. */
736
if (a->cleanup_archive_extract != NULL)
737
r = (a->cleanup_archive_extract)(a);
739
/* TODO: Clean up the formatters. */
741
/* Release the filter objects. */
742
r1 = cleanup_filters(a);
746
/* Release the bidder objects. */
747
n = sizeof(a->bidders)/sizeof(a->bidders[0]);
748
for (i = 0; i < n; i++) {
749
if (a->bidders[i].free != NULL) {
750
r1 = (a->bidders[i].free)(&a->bidders[i]);
760
cleanup_filters(struct archive_read *a)
763
/* Clean up the filter pipeline. */
764
while (a->filter != NULL) {
765
struct archive_read_filter *t = a->filter->upstream;
766
if (a->filter->close != NULL) {
767
int r1 = (a->filter->close)(a->filter);
771
free(a->filter->buffer);
779
* Release memory and other resources.
782
_archive_read_finish(struct archive *_a)
784
struct archive_read *a = (struct archive_read *)_a;
789
__archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_ANY,
790
"archive_read_finish");
791
if (a->archive.state != ARCHIVE_STATE_CLOSED)
792
r = archive_read_close(&a->archive);
794
/* Cleanup format-specific data. */
795
slots = sizeof(a->formats) / sizeof(a->formats[0]);
796
for (i = 0; i < slots; i++) {
797
a->format = &(a->formats[i]);
798
if (a->formats[i].cleanup)
799
(a->formats[i].cleanup)(a);
802
archive_string_free(&a->archive.error_string);
804
archive_entry_free(a->entry);
805
a->archive.magic = 0;
807
#if ARCHIVE_API_VERSION > 1
813
* Used internally by read format handlers to register their bid and
814
* initialization functions.
817
__archive_read_register_format(struct archive_read *a,
820
int (*bid)(struct archive_read *),
821
int (*options)(struct archive_read *, const char *, const char *),
822
int (*read_header)(struct archive_read *, struct archive_entry *),
823
int (*read_data)(struct archive_read *, const void **, size_t *, off_t *),
824
int (*read_data_skip)(struct archive_read *),
825
int (*cleanup)(struct archive_read *))
829
__archive_check_magic(&a->archive,
830
ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
831
"__archive_read_register_format");
833
number_slots = sizeof(a->formats) / sizeof(a->formats[0]);
835
for (i = 0; i < number_slots; i++) {
836
if (a->formats[i].bid == bid)
837
return (ARCHIVE_WARN); /* We've already installed */
838
if (a->formats[i].bid == NULL) {
839
a->formats[i].bid = bid;
840
a->formats[i].options = options;
841
a->formats[i].read_header = read_header;
842
a->formats[i].read_data = read_data;
843
a->formats[i].read_data_skip = read_data_skip;
844
a->formats[i].cleanup = cleanup;
845
a->formats[i].data = format_data;
846
a->formats[i].name = name;
851
__archive_errx(1, "Not enough slots for format registration");
852
return (ARCHIVE_FATAL); /* Never actually called. */
856
* Used internally by decompression routines to register their bid and
857
* initialization functions.
859
struct archive_read_filter_bidder *
860
__archive_read_get_bidder(struct archive_read *a)
864
__archive_check_magic(&a->archive,
865
ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
866
"__archive_read_get_bidder");
868
number_slots = sizeof(a->bidders) / sizeof(a->bidders[0]);
870
for (i = 0; i < number_slots; i++) {
871
if (a->bidders[i].bid == NULL) {
872
memset(a->bidders + i, 0, sizeof(a->bidders[0]));
873
return (a->bidders + i);
877
__archive_errx(1, "Not enough slots for compression registration");
878
return (NULL); /* Never actually executed. */
882
* The next three functions comprise the peek/consume internal I/O
883
* system used by archive format readers. This system allows fairly
884
* flexible read-ahead and allows the I/O code to operate in a
885
* zero-copy manner most of the time.
887
* In the ideal case, filters generate blocks of data
888
* and __archive_read_ahead() just returns pointers directly into
889
* those blocks. Then __archive_read_consume() just bumps those
890
* pointers. Only if your request would span blocks does the I/O
891
* layer use a copy buffer to provide you with a contiguous block of
892
* data. The __archive_read_skip() is an optimization; it scans ahead
893
* very quickly (it usually translates into a seek() operation if
894
* you're reading uncompressed disk files).
896
* A couple of useful idioms:
897
* * "I just want some data." Ask for 1 byte and pay attention to
898
* the "number of bytes available" from __archive_read_ahead().
899
* You can consume more than you asked for; you just can't consume
900
* more than is available. If you consume everything that's
901
* immediately available, the next read_ahead() call will pull
903
* * "I want to output a large block of data." As above, ask for 1 byte,
904
* emit all that's available (up to whatever limit you have), then
905
* repeat until you're done.
906
* * "I want to peek ahead by a large amount." Ask for 4k or so, then
907
* double and repeat until you get an error or have enough. Note
908
* that the I/O layer will likely end up expanding its copy buffer
909
* to fit your request, so use this technique cautiously. This
910
* technique is used, for example, by some of the format tasting
911
* code that has uncertain look-ahead needs.
913
* TODO: Someday, provide a more generic __archive_read_seek() for
914
* those cases where it's useful. This is tricky because there are lots
915
* of cases where seek() is not available (reading gzip data from a
916
* network socket, for instance), so there needs to be a good way to
917
* communicate whether seek() is available and users of that interface
918
* need to use non-seeking strategies whenever seek() is not available.
922
* Looks ahead in the input stream:
923
* * If 'avail' pointer is provided, that returns number of bytes available
924
* in the current buffer, which may be much larger than requested.
925
* * If end-of-file, *avail gets set to zero.
926
* * If error, *avail gets error code.
927
* * If request can be met, returns pointer to data, returns NULL
928
* if request is not met.
930
* Note: If you just want "some data", ask for 1 byte and pay attention
931
* to *avail, which will have the actual amount available. If you
932
* know exactly how many bytes you need, just ask for that and treat
933
* a NULL return as an error.
935
* Important: This does NOT move the file pointer. See
936
* __archive_read_consume() below.
940
* This is tricky. We need to provide our clients with pointers to
941
* contiguous blocks of memory but we want to avoid copying whenever
944
* Mostly, this code returns pointers directly into the block of data
945
* provided by the client_read routine. It can do this unless the
946
* request would split across blocks. In that case, we have to copy
947
* into an internal buffer to combine reads.
950
__archive_read_ahead(struct archive_read *a, size_t min, ssize_t *avail)
952
return (__archive_read_filter_ahead(a->filter, min, avail));
956
__archive_read_filter_ahead(struct archive_read_filter *filter,
957
size_t min, ssize_t *avail)
964
*avail = ARCHIVE_FATAL;
969
* Keep pulling more data until we can satisfy the request.
974
* If we can satisfy from the copy buffer (and the
975
* copy buffer isn't empty), we're done. In particular,
976
* note that min == 0 is a perfectly well-defined
979
if (filter->avail >= min && filter->avail > 0) {
981
*avail = filter->avail;
982
return (filter->next);
986
* We can satisfy directly from client buffer if everything
987
* currently in the copy buffer is still in the client buffer.
989
if (filter->client_total >= filter->client_avail + filter->avail
990
&& filter->client_avail + filter->avail >= min) {
991
/* "Roll back" to client buffer. */
992
filter->client_avail += filter->avail;
993
filter->client_next -= filter->avail;
994
/* Copy buffer is now empty. */
996
filter->next = filter->buffer;
997
/* Return data from client buffer. */
999
*avail = filter->client_avail;
1000
return (filter->client_next);
1003
/* Move data forward in copy buffer if necessary. */
1004
if (filter->next > filter->buffer &&
1005
filter->next + min > filter->buffer + filter->buffer_size) {
1006
if (filter->avail > 0)
1007
memmove(filter->buffer, filter->next, filter->avail);
1008
filter->next = filter->buffer;
1011
/* If we've used up the client data, get more. */
1012
if (filter->client_avail <= 0) {
1013
if (filter->end_of_file) {
1018
bytes_read = (filter->read)(filter,
1019
&filter->client_buff);
1020
if (bytes_read < 0) { /* Read error. */
1021
filter->client_total = filter->client_avail = 0;
1022
filter->client_next = filter->client_buff = NULL;
1025
*avail = ARCHIVE_FATAL;
1028
if (bytes_read == 0) { /* Premature end-of-file. */
1029
filter->client_total = filter->client_avail = 0;
1030
filter->client_next = filter->client_buff = NULL;
1031
filter->end_of_file = 1;
1032
/* Return whatever we do have. */
1034
*avail = filter->avail;
1037
filter->position += bytes_read;
1038
filter->client_total = bytes_read;
1039
filter->client_avail = filter->client_total;
1040
filter->client_next = filter->client_buff;
1045
* We can't satisfy the request from the copy
1046
* buffer or the existing client data, so we
1047
* need to copy more client data over to the
1051
/* Ensure the buffer is big enough. */
1052
if (min > filter->buffer_size) {
1056
/* Double the buffer; watch for overflow. */
1057
s = t = filter->buffer_size;
1062
if (t <= s) { /* Integer overflow! */
1064
&filter->archive->archive,
1066
"Unable to allocate copy buffer");
1069
*avail = ARCHIVE_FATAL;
1074
/* Now s >= min, so allocate a new buffer. */
1075
p = (char *)malloc(s);
1078
&filter->archive->archive,
1080
"Unable to allocate copy buffer");
1083
*avail = ARCHIVE_FATAL;
1086
/* Move data into newly-enlarged buffer. */
1087
if (filter->avail > 0)
1088
memmove(p, filter->next, filter->avail);
1089
free(filter->buffer);
1090
filter->next = filter->buffer = p;
1091
filter->buffer_size = s;
1094
/* We can add client data to copy buffer. */
1095
/* First estimate: copy to fill rest of buffer. */
1096
tocopy = (filter->buffer + filter->buffer_size)
1097
- (filter->next + filter->avail);
1098
/* Don't waste time buffering more than we need to. */
1099
if (tocopy + filter->avail > min)
1100
tocopy = min - filter->avail;
1101
/* Don't copy more than is available. */
1102
if (tocopy > filter->client_avail)
1103
tocopy = filter->client_avail;
1105
memcpy(filter->next + filter->avail, filter->client_next,
1107
/* Remove this data from client buffer. */
1108
filter->client_next += tocopy;
1109
filter->client_avail -= tocopy;
1110
/* add it to copy buffer. */
1111
filter->avail += tocopy;
1117
* Move the file pointer forward. This should be called after
1118
* __archive_read_ahead() returns data to you. Don't try to move
1119
* ahead by more than the amount of data available according to
1120
* __archive_read_ahead().
1123
* Mark the appropriate data as used. Note that the request here will
1124
* often be much smaller than the size of the previous read_ahead
1128
__archive_read_consume(struct archive_read *a, size_t request)
1131
r = __archive_read_filter_consume(a->filter, request);
1132
a->archive.file_position += r;
1137
__archive_read_filter_consume(struct archive_read_filter * filter,
1140
if (filter->avail > 0) {
1141
/* Read came from copy buffer. */
1142
filter->next += request;
1143
filter->avail -= request;
1145
/* Read came from client buffer. */
1146
filter->client_next += request;
1147
filter->client_avail -= request;
1153
* Move the file pointer ahead by an arbitrary amount. If you're
1154
* reading uncompressed data from a disk file, this will actually
1155
* translate into a seek() operation. Even in cases where seek()
1156
* isn't feasible, this at least pushes the read-and-discard loop
1157
* down closer to the data source.
1160
__archive_read_skip(struct archive_read *a, int64_t request)
1162
int64_t skipped = __archive_read_skip_lenient(a, request);
1163
if (skipped == request)
1165
/* We hit EOF before we satisfied the skip request. */
1166
if (skipped < 0) // Map error code to 0 for error message below.
1168
archive_set_error(&a->archive,
1170
"Truncated input file (needed %jd bytes, only %jd available)",
1171
(intmax_t)request, (intmax_t)skipped);
1172
return (ARCHIVE_FATAL);
1176
__archive_read_skip_lenient(struct archive_read *a, int64_t request)
1178
int64_t skipped = __archive_read_filter_skip(a->filter, request);
1180
a->archive.file_position += skipped;
1185
__archive_read_filter_skip(struct archive_read_filter *filter, int64_t request)
1187
int64_t bytes_skipped, total_bytes_skipped = 0;
1193
* If there is data in the buffers already, use that first.
1195
if (filter->avail > 0) {
1196
min = minimum(request, (off_t)filter->avail);
1197
bytes_skipped = __archive_read_filter_consume(filter, min);
1198
request -= bytes_skipped;
1199
total_bytes_skipped += bytes_skipped;
1201
if (filter->client_avail > 0) {
1202
min = minimum(request, (int64_t)filter->client_avail);
1203
bytes_skipped = __archive_read_filter_consume(filter, min);
1204
request -= bytes_skipped;
1205
total_bytes_skipped += bytes_skipped;
1208
return (total_bytes_skipped);
1210
* If a client_skipper was provided, try that first.
1212
#if ARCHIVE_API_VERSION < 2
1213
if ((filter->skip != NULL) && (request < SSIZE_MAX)) {
1215
if (filter->skip != NULL) {
1217
bytes_skipped = (filter->skip)(filter, request);
1218
if (bytes_skipped < 0) { /* error */
1219
filter->client_total = filter->client_avail = 0;
1220
filter->client_next = filter->client_buff = NULL;
1222
return (bytes_skipped);
1224
total_bytes_skipped += bytes_skipped;
1225
request -= bytes_skipped;
1226
filter->client_next = filter->client_buff;
1227
filter->client_avail = filter->client_total = 0;
1230
* Note that client_skipper will usually not satisfy the
1231
* full request (due to low-level blocking concerns),
1232
* so even if client_skipper is provided, we may still
1233
* have to use ordinary reads to finish out the request.
1235
while (request > 0) {
1237
(void)__archive_read_filter_ahead(filter, 1, &bytes_read);
1239
return (bytes_read);
1240
if (bytes_read == 0) {
1241
return (total_bytes_skipped);
1243
min = (size_t)(minimum(bytes_read, request));
1244
bytes_read = __archive_read_filter_consume(filter, min);
1245
total_bytes_skipped += bytes_read;
1246
request -= bytes_read;
1248
return (total_bytes_skipped);