]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - contrib/libarchive/libarchive/archive_read.c
MFH r328332:
[FreeBSD/stable/10.git] / contrib / libarchive / libarchive / archive_read.c
1 /*-
2  * Copyright (c) 2003-2011 Tim Kientzle
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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.
24  */
25
26 /*
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.
32  */
33
34 #include "archive_platform.h"
35 __FBSDID("$FreeBSD$");
36
37 #ifdef HAVE_ERRNO_H
38 #include <errno.h>
39 #endif
40 #include <stdio.h>
41 #ifdef HAVE_STDLIB_H
42 #include <stdlib.h>
43 #endif
44 #ifdef HAVE_STRING_H
45 #include <string.h>
46 #endif
47 #ifdef HAVE_UNISTD_H
48 #include <unistd.h>
49 #endif
50
51 #include "archive.h"
52 #include "archive_entry.h"
53 #include "archive_private.h"
54 #include "archive_read_private.h"
55
56 #define minimum(a, b) (a < b ? a : b)
57
58 static int      choose_filters(struct archive_read *);
59 static int      choose_format(struct archive_read *);
60 static int      close_filters(struct archive_read *);
61 static struct archive_vtable *archive_read_vtable(void);
62 static int64_t  _archive_filter_bytes(struct archive *, int);
63 static int      _archive_filter_code(struct archive *, int);
64 static const char *_archive_filter_name(struct archive *, int);
65 static int  _archive_filter_count(struct archive *);
66 static int      _archive_read_close(struct archive *);
67 static int      _archive_read_data_block(struct archive *,
68                     const void **, size_t *, int64_t *);
69 static int      _archive_read_free(struct archive *);
70 static int      _archive_read_next_header(struct archive *,
71                     struct archive_entry **);
72 static int      _archive_read_next_header2(struct archive *,
73                     struct archive_entry *);
74 static int64_t  advance_file_pointer(struct archive_read_filter *, int64_t);
75
76 static struct archive_vtable *
77 archive_read_vtable(void)
78 {
79         static struct archive_vtable av;
80         static int inited = 0;
81
82         if (!inited) {
83                 av.archive_filter_bytes = _archive_filter_bytes;
84                 av.archive_filter_code = _archive_filter_code;
85                 av.archive_filter_name = _archive_filter_name;
86                 av.archive_filter_count = _archive_filter_count;
87                 av.archive_read_data_block = _archive_read_data_block;
88                 av.archive_read_next_header = _archive_read_next_header;
89                 av.archive_read_next_header2 = _archive_read_next_header2;
90                 av.archive_free = _archive_read_free;
91                 av.archive_close = _archive_read_close;
92                 inited = 1;
93         }
94         return (&av);
95 }
96
97 /*
98  * Allocate, initialize and return a struct archive object.
99  */
100 struct archive *
101 archive_read_new(void)
102 {
103         struct archive_read *a;
104
105         a = (struct archive_read *)calloc(1, sizeof(*a));
106         if (a == NULL)
107                 return (NULL);
108         a->archive.magic = ARCHIVE_READ_MAGIC;
109
110         a->archive.state = ARCHIVE_STATE_NEW;
111         a->entry = archive_entry_new2(&a->archive);
112         a->archive.vtable = archive_read_vtable();
113
114         a->passphrases.last = &a->passphrases.first;
115
116         return (&a->archive);
117 }
118
119 /*
120  * Record the do-not-extract-to file. This belongs in archive_read_extract.c.
121  */
122 void
123 archive_read_extract_set_skip_file(struct archive *_a, la_int64_t d,
124     la_int64_t i)
125 {
126         struct archive_read *a = (struct archive_read *)_a;
127
128         if (ARCHIVE_OK != __archive_check_magic(_a, ARCHIVE_READ_MAGIC,
129                 ARCHIVE_STATE_ANY, "archive_read_extract_set_skip_file"))
130                 return;
131         a->skip_file_set = 1;
132         a->skip_file_dev = d;
133         a->skip_file_ino = i;
134 }
135
136 /*
137  * Open the archive
138  */
139 int
140 archive_read_open(struct archive *a, void *client_data,
141     archive_open_callback *client_opener, archive_read_callback *client_reader,
142     archive_close_callback *client_closer)
143 {
144         /* Old archive_read_open() is just a thin shell around
145          * archive_read_open1. */
146         archive_read_set_open_callback(a, client_opener);
147         archive_read_set_read_callback(a, client_reader);
148         archive_read_set_close_callback(a, client_closer);
149         archive_read_set_callback_data(a, client_data);
150         return archive_read_open1(a);
151 }
152
153
154 int
155 archive_read_open2(struct archive *a, void *client_data,
156     archive_open_callback *client_opener,
157     archive_read_callback *client_reader,
158     archive_skip_callback *client_skipper,
159     archive_close_callback *client_closer)
160 {
161         /* Old archive_read_open2() is just a thin shell around
162          * archive_read_open1. */
163         archive_read_set_callback_data(a, client_data);
164         archive_read_set_open_callback(a, client_opener);
165         archive_read_set_read_callback(a, client_reader);
166         archive_read_set_skip_callback(a, client_skipper);
167         archive_read_set_close_callback(a, client_closer);
168         return archive_read_open1(a);
169 }
170
171 static ssize_t
172 client_read_proxy(struct archive_read_filter *self, const void **buff)
173 {
174         ssize_t r;
175         r = (self->archive->client.reader)(&self->archive->archive,
176             self->data, buff);
177         return (r);
178 }
179
180 static int64_t
181 client_skip_proxy(struct archive_read_filter *self, int64_t request)
182 {
183         if (request < 0)
184                 __archive_errx(1, "Negative skip requested.");
185         if (request == 0)
186                 return 0;
187
188         if (self->archive->client.skipper != NULL) {
189                 /* Seek requests over 1GiB are broken down into
190                  * multiple seeks.  This avoids overflows when the
191                  * requests get passed through 32-bit arguments. */
192                 int64_t skip_limit = (int64_t)1 << 30;
193                 int64_t total = 0;
194                 for (;;) {
195                         int64_t get, ask = request;
196                         if (ask > skip_limit)
197                                 ask = skip_limit;
198                         get = (self->archive->client.skipper)
199                                 (&self->archive->archive, self->data, ask);
200                         total += get;
201                         if (get == 0 || get == request)
202                                 return (total);
203                         if (get > request)
204                                 return ARCHIVE_FATAL;
205                         request -= get;
206                 }
207         } else if (self->archive->client.seeker != NULL
208                 && request > 64 * 1024) {
209                 /* If the client provided a seeker but not a skipper,
210                  * we can use the seeker to skip forward.
211                  *
212                  * Note: This isn't always a good idea.  The client
213                  * skipper is allowed to skip by less than requested
214                  * if it needs to maintain block alignment.  The
215                  * seeker is not allowed to play such games, so using
216                  * the seeker here may be a performance loss compared
217                  * to just reading and discarding.  That's why we
218                  * only do this for skips of over 64k.
219                  */
220                 int64_t before = self->position;
221                 int64_t after = (self->archive->client.seeker)
222                     (&self->archive->archive, self->data, request, SEEK_CUR);
223                 if (after != before + request)
224                         return ARCHIVE_FATAL;
225                 return after - before;
226         }
227         return 0;
228 }
229
230 static int64_t
231 client_seek_proxy(struct archive_read_filter *self, int64_t offset, int whence)
232 {
233         /* DO NOT use the skipper here!  If we transparently handled
234          * forward seek here by using the skipper, that will break
235          * other libarchive code that assumes a successful forward
236          * seek means it can also seek backwards.
237          */
238         if (self->archive->client.seeker == NULL) {
239                 archive_set_error(&self->archive->archive, ARCHIVE_ERRNO_MISC,
240                     "Current client reader does not support seeking a device");
241                 return (ARCHIVE_FAILED);
242         }
243         return (self->archive->client.seeker)(&self->archive->archive,
244             self->data, offset, whence);
245 }
246
247 static int
248 client_close_proxy(struct archive_read_filter *self)
249 {
250         int r = ARCHIVE_OK, r2;
251         unsigned int i;
252
253         if (self->archive->client.closer == NULL)
254                 return (r);
255         for (i = 0; i < self->archive->client.nodes; i++)
256         {
257                 r2 = (self->archive->client.closer)
258                         ((struct archive *)self->archive,
259                                 self->archive->client.dataset[i].data);
260                 if (r > r2)
261                         r = r2;
262         }
263         return (r);
264 }
265
266 static int
267 client_open_proxy(struct archive_read_filter *self)
268 {
269   int r = ARCHIVE_OK;
270         if (self->archive->client.opener != NULL)
271                 r = (self->archive->client.opener)(
272                     (struct archive *)self->archive, self->data);
273         return (r);
274 }
275
276 static int
277 client_switch_proxy(struct archive_read_filter *self, unsigned int iindex)
278 {
279   int r1 = ARCHIVE_OK, r2 = ARCHIVE_OK;
280         void *data2 = NULL;
281
282         /* Don't do anything if already in the specified data node */
283         if (self->archive->client.cursor == iindex)
284                 return (ARCHIVE_OK);
285
286         self->archive->client.cursor = iindex;
287         data2 = self->archive->client.dataset[self->archive->client.cursor].data;
288         if (self->archive->client.switcher != NULL)
289         {
290                 r1 = r2 = (self->archive->client.switcher)
291                         ((struct archive *)self->archive, self->data, data2);
292                 self->data = data2;
293         }
294         else
295         {
296                 /* Attempt to call close and open instead */
297                 if (self->archive->client.closer != NULL)
298                         r1 = (self->archive->client.closer)
299                                 ((struct archive *)self->archive, self->data);
300                 self->data = data2;
301                 if (self->archive->client.opener != NULL)
302                         r2 = (self->archive->client.opener)
303                                 ((struct archive *)self->archive, self->data);
304         }
305         return (r1 < r2) ? r1 : r2;
306 }
307
308 int
309 archive_read_set_open_callback(struct archive *_a,
310     archive_open_callback *client_opener)
311 {
312         struct archive_read *a = (struct archive_read *)_a;
313         archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
314             "archive_read_set_open_callback");
315         a->client.opener = client_opener;
316         return ARCHIVE_OK;
317 }
318
319 int
320 archive_read_set_read_callback(struct archive *_a,
321     archive_read_callback *client_reader)
322 {
323         struct archive_read *a = (struct archive_read *)_a;
324         archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
325             "archive_read_set_read_callback");
326         a->client.reader = client_reader;
327         return ARCHIVE_OK;
328 }
329
330 int
331 archive_read_set_skip_callback(struct archive *_a,
332     archive_skip_callback *client_skipper)
333 {
334         struct archive_read *a = (struct archive_read *)_a;
335         archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
336             "archive_read_set_skip_callback");
337         a->client.skipper = client_skipper;
338         return ARCHIVE_OK;
339 }
340
341 int
342 archive_read_set_seek_callback(struct archive *_a,
343     archive_seek_callback *client_seeker)
344 {
345         struct archive_read *a = (struct archive_read *)_a;
346         archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
347             "archive_read_set_seek_callback");
348         a->client.seeker = client_seeker;
349         return ARCHIVE_OK;
350 }
351
352 int
353 archive_read_set_close_callback(struct archive *_a,
354     archive_close_callback *client_closer)
355 {
356         struct archive_read *a = (struct archive_read *)_a;
357         archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
358             "archive_read_set_close_callback");
359         a->client.closer = client_closer;
360         return ARCHIVE_OK;
361 }
362
363 int
364 archive_read_set_switch_callback(struct archive *_a,
365     archive_switch_callback *client_switcher)
366 {
367         struct archive_read *a = (struct archive_read *)_a;
368         archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
369             "archive_read_set_switch_callback");
370         a->client.switcher = client_switcher;
371         return ARCHIVE_OK;
372 }
373
374 int
375 archive_read_set_callback_data(struct archive *_a, void *client_data)
376 {
377         return archive_read_set_callback_data2(_a, client_data, 0);
378 }
379
380 int
381 archive_read_set_callback_data2(struct archive *_a, void *client_data,
382     unsigned int iindex)
383 {
384         struct archive_read *a = (struct archive_read *)_a;
385         archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
386             "archive_read_set_callback_data2");
387
388         if (a->client.nodes == 0)
389         {
390                 a->client.dataset = (struct archive_read_data_node *)
391                     calloc(1, sizeof(*a->client.dataset));
392                 if (a->client.dataset == NULL)
393                 {
394                         archive_set_error(&a->archive, ENOMEM,
395                                 "No memory.");
396                         return ARCHIVE_FATAL;
397                 }
398                 a->client.nodes = 1;
399         }
400
401         if (iindex > a->client.nodes - 1)
402         {
403                 archive_set_error(&a->archive, EINVAL,
404                         "Invalid index specified.");
405                 return ARCHIVE_FATAL;
406         }
407         a->client.dataset[iindex].data = client_data;
408         a->client.dataset[iindex].begin_position = -1;
409         a->client.dataset[iindex].total_size = -1;
410         return ARCHIVE_OK;
411 }
412
413 int
414 archive_read_add_callback_data(struct archive *_a, void *client_data,
415     unsigned int iindex)
416 {
417         struct archive_read *a = (struct archive_read *)_a;
418         void *p;
419         unsigned int i;
420
421         archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
422             "archive_read_add_callback_data");
423         if (iindex > a->client.nodes) {
424                 archive_set_error(&a->archive, EINVAL,
425                         "Invalid index specified.");
426                 return ARCHIVE_FATAL;
427         }
428         p = realloc(a->client.dataset, sizeof(*a->client.dataset)
429                 * (++(a->client.nodes)));
430         if (p == NULL) {
431                 archive_set_error(&a->archive, ENOMEM,
432                         "No memory.");
433                 return ARCHIVE_FATAL;
434         }
435         a->client.dataset = (struct archive_read_data_node *)p;
436         for (i = a->client.nodes - 1; i > iindex && i > 0; i--) {
437                 a->client.dataset[i].data = a->client.dataset[i-1].data;
438                 a->client.dataset[i].begin_position = -1;
439                 a->client.dataset[i].total_size = -1;
440         }
441         a->client.dataset[iindex].data = client_data;
442         a->client.dataset[iindex].begin_position = -1;
443         a->client.dataset[iindex].total_size = -1;
444         return ARCHIVE_OK;
445 }
446
447 int
448 archive_read_append_callback_data(struct archive *_a, void *client_data)
449 {
450         struct archive_read *a = (struct archive_read *)_a;
451         return archive_read_add_callback_data(_a, client_data, a->client.nodes);
452 }
453
454 int
455 archive_read_prepend_callback_data(struct archive *_a, void *client_data)
456 {
457         return archive_read_add_callback_data(_a, client_data, 0);
458 }
459
460 int
461 archive_read_open1(struct archive *_a)
462 {
463         struct archive_read *a = (struct archive_read *)_a;
464         struct archive_read_filter *filter, *tmp;
465         int slot, e = ARCHIVE_OK;
466         unsigned int i;
467
468         archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
469             "archive_read_open");
470         archive_clear_error(&a->archive);
471
472         if (a->client.reader == NULL) {
473                 archive_set_error(&a->archive, EINVAL,
474                     "No reader function provided to archive_read_open");
475                 a->archive.state = ARCHIVE_STATE_FATAL;
476                 return (ARCHIVE_FATAL);
477         }
478
479         /* Open data source. */
480         if (a->client.opener != NULL) {
481                 e = (a->client.opener)(&a->archive, a->client.dataset[0].data);
482                 if (e != 0) {
483                         /* If the open failed, call the closer to clean up. */
484                         if (a->client.closer) {
485                                 for (i = 0; i < a->client.nodes; i++)
486                                         (a->client.closer)(&a->archive,
487                                             a->client.dataset[i].data);
488                         }
489                         return (e);
490                 }
491         }
492
493         filter = calloc(1, sizeof(*filter));
494         if (filter == NULL)
495                 return (ARCHIVE_FATAL);
496         filter->bidder = NULL;
497         filter->upstream = NULL;
498         filter->archive = a;
499         filter->data = a->client.dataset[0].data;
500         filter->open = client_open_proxy;
501         filter->read = client_read_proxy;
502         filter->skip = client_skip_proxy;
503         filter->seek = client_seek_proxy;
504         filter->close = client_close_proxy;
505         filter->sswitch = client_switch_proxy;
506         filter->name = "none";
507         filter->code = ARCHIVE_FILTER_NONE;
508
509         a->client.dataset[0].begin_position = 0;
510         if (!a->filter || !a->bypass_filter_bidding)
511         {
512                 a->filter = filter;
513                 /* Build out the input pipeline. */
514                 e = choose_filters(a);
515                 if (e < ARCHIVE_WARN) {
516                         a->archive.state = ARCHIVE_STATE_FATAL;
517                         return (ARCHIVE_FATAL);
518                 }
519         }
520         else
521         {
522                 /* Need to add "NONE" type filter at the end of the filter chain */
523                 tmp = a->filter;
524                 while (tmp->upstream)
525                         tmp = tmp->upstream;
526                 tmp->upstream = filter;
527         }
528
529         if (!a->format)
530         {
531                 slot = choose_format(a);
532                 if (slot < 0) {
533                         close_filters(a);
534                         a->archive.state = ARCHIVE_STATE_FATAL;
535                         return (ARCHIVE_FATAL);
536                 }
537                 a->format = &(a->formats[slot]);
538         }
539
540         a->archive.state = ARCHIVE_STATE_HEADER;
541
542         /* Ensure libarchive starts from the first node in a multivolume set */
543         client_switch_proxy(a->filter, 0);
544         return (e);
545 }
546
547 /*
548  * Allow each registered stream transform to bid on whether
549  * it wants to handle this stream.  Repeat until we've finished
550  * building the pipeline.
551  */
552
553 /* We won't build a filter pipeline with more stages than this. */
554 #define MAX_NUMBER_FILTERS 25
555
556 static int
557 choose_filters(struct archive_read *a)
558 {
559         int number_bidders, i, bid, best_bid, number_filters;
560         struct archive_read_filter_bidder *bidder, *best_bidder;
561         struct archive_read_filter *filter;
562         ssize_t avail;
563         int r;
564
565         for (number_filters = 0; number_filters < MAX_NUMBER_FILTERS; ++number_filters) {
566                 number_bidders = sizeof(a->bidders) / sizeof(a->bidders[0]);
567
568                 best_bid = 0;
569                 best_bidder = NULL;
570
571                 bidder = a->bidders;
572                 for (i = 0; i < number_bidders; i++, bidder++) {
573                         if (bidder->bid != NULL) {
574                                 bid = (bidder->bid)(bidder, a->filter);
575                                 if (bid > best_bid) {
576                                         best_bid = bid;
577                                         best_bidder = bidder;
578                                 }
579                         }
580                 }
581
582                 /* If no bidder, we're done. */
583                 if (best_bidder == NULL) {
584                         /* Verify the filter by asking it for some data. */
585                         __archive_read_filter_ahead(a->filter, 1, &avail);
586                         if (avail < 0) {
587                                 __archive_read_free_filters(a);
588                                 return (ARCHIVE_FATAL);
589                         }
590                         a->archive.compression_name = a->filter->name;
591                         a->archive.compression_code = a->filter->code;
592                         return (ARCHIVE_OK);
593                 }
594
595                 filter
596                     = (struct archive_read_filter *)calloc(1, sizeof(*filter));
597                 if (filter == NULL)
598                         return (ARCHIVE_FATAL);
599                 filter->bidder = best_bidder;
600                 filter->archive = a;
601                 filter->upstream = a->filter;
602                 a->filter = filter;
603                 r = (best_bidder->init)(a->filter);
604                 if (r != ARCHIVE_OK) {
605                         __archive_read_free_filters(a);
606                         return (ARCHIVE_FATAL);
607                 }
608         }
609         archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
610             "Input requires too many filters for decoding");
611         return (ARCHIVE_FATAL);
612 }
613
614 /*
615  * Read header of next entry.
616  */
617 static int
618 _archive_read_next_header2(struct archive *_a, struct archive_entry *entry)
619 {
620         struct archive_read *a = (struct archive_read *)_a;
621         int r1 = ARCHIVE_OK, r2;
622
623         archive_check_magic(_a, ARCHIVE_READ_MAGIC,
624             ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
625             "archive_read_next_header");
626
627         archive_entry_clear(entry);
628         archive_clear_error(&a->archive);
629
630         /*
631          * If client didn't consume entire data, skip any remainder
632          * (This is especially important for GNU incremental directories.)
633          */
634         if (a->archive.state == ARCHIVE_STATE_DATA) {
635                 r1 = archive_read_data_skip(&a->archive);
636                 if (r1 == ARCHIVE_EOF)
637                         archive_set_error(&a->archive, EIO,
638                             "Premature end-of-file.");
639                 if (r1 == ARCHIVE_EOF || r1 == ARCHIVE_FATAL) {
640                         a->archive.state = ARCHIVE_STATE_FATAL;
641                         return (ARCHIVE_FATAL);
642                 }
643         }
644
645         /* Record start-of-header offset in uncompressed stream. */
646         a->header_position = a->filter->position;
647
648         ++_a->file_count;
649         r2 = (a->format->read_header)(a, entry);
650
651         /*
652          * EOF and FATAL are persistent at this layer.  By
653          * modifying the state, we guarantee that future calls to
654          * read a header or read data will fail.
655          */
656         switch (r2) {
657         case ARCHIVE_EOF:
658                 a->archive.state = ARCHIVE_STATE_EOF;
659                 --_a->file_count;/* Revert a file counter. */
660                 break;
661         case ARCHIVE_OK:
662                 a->archive.state = ARCHIVE_STATE_DATA;
663                 break;
664         case ARCHIVE_WARN:
665                 a->archive.state = ARCHIVE_STATE_DATA;
666                 break;
667         case ARCHIVE_RETRY:
668                 break;
669         case ARCHIVE_FATAL:
670                 a->archive.state = ARCHIVE_STATE_FATAL;
671                 break;
672         }
673
674         __archive_reset_read_data(&a->archive);
675
676         a->data_start_node = a->client.cursor;
677         /* EOF always wins; otherwise return the worst error. */
678         return (r2 < r1 || r2 == ARCHIVE_EOF) ? r2 : r1;
679 }
680
681 static int
682 _archive_read_next_header(struct archive *_a, struct archive_entry **entryp)
683 {
684         int ret;
685         struct archive_read *a = (struct archive_read *)_a;
686         *entryp = NULL;
687         ret = _archive_read_next_header2(_a, a->entry);
688         *entryp = a->entry;
689         return ret;
690 }
691
692 /*
693  * Allow each registered format to bid on whether it wants to handle
694  * the next entry.  Return index of winning bidder.
695  */
696 static int
697 choose_format(struct archive_read *a)
698 {
699         int slots;
700         int i;
701         int bid, best_bid;
702         int best_bid_slot;
703
704         slots = sizeof(a->formats) / sizeof(a->formats[0]);
705         best_bid = -1;
706         best_bid_slot = -1;
707
708         /* Set up a->format for convenience of bidders. */
709         a->format = &(a->formats[0]);
710         for (i = 0; i < slots; i++, a->format++) {
711                 if (a->format->bid) {
712                         bid = (a->format->bid)(a, best_bid);
713                         if (bid == ARCHIVE_FATAL)
714                                 return (ARCHIVE_FATAL);
715                         if (a->filter->position != 0)
716                                 __archive_read_seek(a, 0, SEEK_SET);
717                         if ((bid > best_bid) || (best_bid_slot < 0)) {
718                                 best_bid = bid;
719                                 best_bid_slot = i;
720                         }
721                 }
722         }
723
724         /*
725          * There were no bidders; this is a serious programmer error
726          * and demands a quick and definitive abort.
727          */
728         if (best_bid_slot < 0) {
729                 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
730                     "No formats registered");
731                 return (ARCHIVE_FATAL);
732         }
733
734         /*
735          * There were bidders, but no non-zero bids; this means we
736          * can't support this stream.
737          */
738         if (best_bid < 1) {
739                 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
740                     "Unrecognized archive format");
741                 return (ARCHIVE_FATAL);
742         }
743
744         return (best_bid_slot);
745 }
746
747 /*
748  * Return the file offset (within the uncompressed data stream) where
749  * the last header started.
750  */
751 la_int64_t
752 archive_read_header_position(struct archive *_a)
753 {
754         struct archive_read *a = (struct archive_read *)_a;
755         archive_check_magic(_a, ARCHIVE_READ_MAGIC,
756             ARCHIVE_STATE_ANY, "archive_read_header_position");
757         return (a->header_position);
758 }
759
760 /*
761  * Returns 1 if the archive contains at least one encrypted entry.
762  * If the archive format not support encryption at all
763  * ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED is returned.
764  * If for any other reason (e.g. not enough data read so far)
765  * we cannot say whether there are encrypted entries, then
766  * ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW is returned.
767  * In general, this function will return values below zero when the
768  * reader is uncertain or totally incapable of encryption support.
769  * When this function returns 0 you can be sure that the reader
770  * supports encryption detection but no encrypted entries have
771  * been found yet.
772  *
773  * NOTE: If the metadata/header of an archive is also encrypted, you
774  * cannot rely on the number of encrypted entries. That is why this
775  * function does not return the number of encrypted entries but#
776  * just shows that there are some.
777  */
778 int
779 archive_read_has_encrypted_entries(struct archive *_a)
780 {
781         struct archive_read *a = (struct archive_read *)_a;
782         int format_supports_encryption = archive_read_format_capabilities(_a)
783                         & (ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_DATA | ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_METADATA);
784
785         if (!_a || !format_supports_encryption) {
786                 /* Format in general doesn't support encryption */
787                 return ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED;
788         }
789
790         /* A reader potentially has read enough data now. */
791         if (a->format && a->format->has_encrypted_entries) {
792                 return (a->format->has_encrypted_entries)(a);
793         }
794
795         /* For any other reason we cannot say how many entries are there. */
796         return ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW;
797 }
798
799 /*
800  * Returns a bitmask of capabilities that are supported by the archive format reader.
801  * If the reader has no special capabilities, ARCHIVE_READ_FORMAT_CAPS_NONE is returned.
802  */
803 int
804 archive_read_format_capabilities(struct archive *_a)
805 {
806         struct archive_read *a = (struct archive_read *)_a;
807         if (a && a->format && a->format->format_capabilties) {
808                 return (a->format->format_capabilties)(a);
809         }
810         return ARCHIVE_READ_FORMAT_CAPS_NONE;
811 }
812
813 /*
814  * Read data from an archive entry, using a read(2)-style interface.
815  * This is a convenience routine that just calls
816  * archive_read_data_block and copies the results into the client
817  * buffer, filling any gaps with zero bytes.  Clients using this
818  * API can be completely ignorant of sparse-file issues; sparse files
819  * will simply be padded with nulls.
820  *
821  * DO NOT intermingle calls to this function and archive_read_data_block
822  * to read a single entry body.
823  */
824 ssize_t
825 archive_read_data(struct archive *_a, void *buff, size_t s)
826 {
827         struct archive *a = (struct archive *)_a;
828         char    *dest;
829         const void *read_buf;
830         size_t   bytes_read;
831         size_t   len;
832         int      r;
833
834         bytes_read = 0;
835         dest = (char *)buff;
836
837         while (s > 0) {
838                 if (a->read_data_remaining == 0) {
839                         read_buf = a->read_data_block;
840                         a->read_data_is_posix_read = 1;
841                         a->read_data_requested = s;
842                         r = archive_read_data_block(a, &read_buf,
843                             &a->read_data_remaining, &a->read_data_offset);
844                         a->read_data_block = read_buf;
845                         if (r == ARCHIVE_EOF)
846                                 return (bytes_read);
847                         /*
848                          * Error codes are all negative, so the status
849                          * return here cannot be confused with a valid
850                          * byte count.  (ARCHIVE_OK is zero.)
851                          */
852                         if (r < ARCHIVE_OK)
853                                 return (r);
854                 }
855
856                 if (a->read_data_offset < a->read_data_output_offset) {
857                         archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT,
858                             "Encountered out-of-order sparse blocks");
859                         return (ARCHIVE_RETRY);
860                 }
861
862                 /* Compute the amount of zero padding needed. */
863                 if (a->read_data_output_offset + (int64_t)s <
864                     a->read_data_offset) {
865                         len = s;
866                 } else if (a->read_data_output_offset <
867                     a->read_data_offset) {
868                         len = (size_t)(a->read_data_offset -
869                             a->read_data_output_offset);
870                 } else
871                         len = 0;
872
873                 /* Add zeroes. */
874                 memset(dest, 0, len);
875                 s -= len;
876                 a->read_data_output_offset += len;
877                 dest += len;
878                 bytes_read += len;
879
880                 /* Copy data if there is any space left. */
881                 if (s > 0) {
882                         len = a->read_data_remaining;
883                         if (len > s)
884                                 len = s;
885                         if (len)
886                                 memcpy(dest, a->read_data_block, len);
887                         s -= len;
888                         a->read_data_block += len;
889                         a->read_data_remaining -= len;
890                         a->read_data_output_offset += len;
891                         a->read_data_offset += len;
892                         dest += len;
893                         bytes_read += len;
894                 }
895         }
896         a->read_data_is_posix_read = 0;
897         a->read_data_requested = 0;
898         return (bytes_read);
899 }
900
901 /*
902  * Reset the read_data_* variables, used for starting a new entry.
903  */
904 void __archive_reset_read_data(struct archive * a)
905 {
906         a->read_data_output_offset = 0;
907         a->read_data_remaining = 0;
908         a->read_data_is_posix_read = 0;
909         a->read_data_requested = 0;
910
911    /* extra resets, from rar.c */
912    a->read_data_block = NULL;
913    a->read_data_offset = 0;
914 }
915
916 /*
917  * Skip over all remaining data in this entry.
918  */
919 int
920 archive_read_data_skip(struct archive *_a)
921 {
922         struct archive_read *a = (struct archive_read *)_a;
923         int r;
924         const void *buff;
925         size_t size;
926         int64_t offset;
927
928         archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA,
929             "archive_read_data_skip");
930
931         if (a->format->read_data_skip != NULL)
932                 r = (a->format->read_data_skip)(a);
933         else {
934                 while ((r = archive_read_data_block(&a->archive,
935                             &buff, &size, &offset))
936                     == ARCHIVE_OK)
937                         ;
938         }
939
940         if (r == ARCHIVE_EOF)
941                 r = ARCHIVE_OK;
942
943         a->archive.state = ARCHIVE_STATE_HEADER;
944         return (r);
945 }
946
947 la_int64_t
948 archive_seek_data(struct archive *_a, int64_t offset, int whence)
949 {
950         struct archive_read *a = (struct archive_read *)_a;
951         archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA,
952             "archive_seek_data_block");
953
954         if (a->format->seek_data == NULL) {
955                 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
956                     "Internal error: "
957                     "No format_seek_data_block function registered");
958                 return (ARCHIVE_FATAL);
959         }
960
961         return (a->format->seek_data)(a, offset, whence);
962 }
963
964 /*
965  * Read the next block of entry data from the archive.
966  * This is a zero-copy interface; the client receives a pointer,
967  * size, and file offset of the next available block of data.
968  *
969  * Returns ARCHIVE_OK if the operation is successful, ARCHIVE_EOF if
970  * the end of entry is encountered.
971  */
972 static int
973 _archive_read_data_block(struct archive *_a,
974     const void **buff, size_t *size, int64_t *offset)
975 {
976         struct archive_read *a = (struct archive_read *)_a;
977         archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA,
978             "archive_read_data_block");
979
980         if (a->format->read_data == NULL) {
981                 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
982                     "Internal error: "
983                     "No format->read_data function registered");
984                 return (ARCHIVE_FATAL);
985         }
986
987         return (a->format->read_data)(a, buff, size, offset);
988 }
989
990 static int
991 close_filters(struct archive_read *a)
992 {
993         struct archive_read_filter *f = a->filter;
994         int r = ARCHIVE_OK;
995         /* Close each filter in the pipeline. */
996         while (f != NULL) {
997                 struct archive_read_filter *t = f->upstream;
998                 if (!f->closed && f->close != NULL) {
999                         int r1 = (f->close)(f);
1000                         f->closed = 1;
1001                         if (r1 < r)
1002                                 r = r1;
1003                 }
1004                 free(f->buffer);
1005                 f->buffer = NULL;
1006                 f = t;
1007         }
1008         return r;
1009 }
1010
1011 void
1012 __archive_read_free_filters(struct archive_read *a)
1013 {
1014         /* Make sure filters are closed and their buffers are freed */
1015         close_filters(a);
1016
1017         while (a->filter != NULL) {
1018                 struct archive_read_filter *t = a->filter->upstream;
1019                 free(a->filter);
1020                 a->filter = t;
1021         }
1022 }
1023
1024 /*
1025  * return the count of # of filters in use
1026  */
1027 static int
1028 _archive_filter_count(struct archive *_a)
1029 {
1030         struct archive_read *a = (struct archive_read *)_a;
1031         struct archive_read_filter *p = a->filter;
1032         int count = 0;
1033         while(p) {
1034                 count++;
1035                 p = p->upstream;
1036         }
1037         return count;
1038 }
1039
1040 /*
1041  * Close the file and all I/O.
1042  */
1043 static int
1044 _archive_read_close(struct archive *_a)
1045 {
1046         struct archive_read *a = (struct archive_read *)_a;
1047         int r = ARCHIVE_OK, r1 = ARCHIVE_OK;
1048
1049         archive_check_magic(&a->archive, ARCHIVE_READ_MAGIC,
1050             ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_read_close");
1051         if (a->archive.state == ARCHIVE_STATE_CLOSED)
1052                 return (ARCHIVE_OK);
1053         archive_clear_error(&a->archive);
1054         a->archive.state = ARCHIVE_STATE_CLOSED;
1055
1056         /* TODO: Clean up the formatters. */
1057
1058         /* Release the filter objects. */
1059         r1 = close_filters(a);
1060         if (r1 < r)
1061                 r = r1;
1062
1063         return (r);
1064 }
1065
1066 /*
1067  * Release memory and other resources.
1068  */
1069 static int
1070 _archive_read_free(struct archive *_a)
1071 {
1072         struct archive_read *a = (struct archive_read *)_a;
1073         struct archive_read_passphrase *p;
1074         int i, n;
1075         int slots;
1076         int r = ARCHIVE_OK;
1077
1078         if (_a == NULL)
1079                 return (ARCHIVE_OK);
1080         archive_check_magic(_a, ARCHIVE_READ_MAGIC,
1081             ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_read_free");
1082         if (a->archive.state != ARCHIVE_STATE_CLOSED
1083             && a->archive.state != ARCHIVE_STATE_FATAL)
1084                 r = archive_read_close(&a->archive);
1085
1086         /* Call cleanup functions registered by optional components. */
1087         if (a->cleanup_archive_extract != NULL)
1088                 r = (a->cleanup_archive_extract)(a);
1089
1090         /* Cleanup format-specific data. */
1091         slots = sizeof(a->formats) / sizeof(a->formats[0]);
1092         for (i = 0; i < slots; i++) {
1093                 a->format = &(a->formats[i]);
1094                 if (a->formats[i].cleanup)
1095                         (a->formats[i].cleanup)(a);
1096         }
1097
1098         /* Free the filters */
1099         __archive_read_free_filters(a);
1100
1101         /* Release the bidder objects. */
1102         n = sizeof(a->bidders)/sizeof(a->bidders[0]);
1103         for (i = 0; i < n; i++) {
1104                 if (a->bidders[i].free != NULL) {
1105                         int r1 = (a->bidders[i].free)(&a->bidders[i]);
1106                         if (r1 < r)
1107                                 r = r1;
1108                 }
1109         }
1110
1111         /* Release passphrase list. */
1112         p = a->passphrases.first;
1113         while (p != NULL) {
1114                 struct archive_read_passphrase *np = p->next;
1115
1116                 /* A passphrase should be cleaned. */
1117                 memset(p->passphrase, 0, strlen(p->passphrase));
1118                 free(p->passphrase);
1119                 free(p);
1120                 p = np;
1121         }
1122
1123         archive_string_free(&a->archive.error_string);
1124         archive_entry_free(a->entry);
1125         a->archive.magic = 0;
1126         __archive_clean(&a->archive);
1127         free(a->client.dataset);
1128         free(a);
1129         return (r);
1130 }
1131
1132 static struct archive_read_filter *
1133 get_filter(struct archive *_a, int n)
1134 {
1135         struct archive_read *a = (struct archive_read *)_a;
1136         struct archive_read_filter *f = a->filter;
1137         /* We use n == -1 for 'the last filter', which is always the
1138          * client proxy. */
1139         if (n == -1 && f != NULL) {
1140                 struct archive_read_filter *last = f;
1141                 f = f->upstream;
1142                 while (f != NULL) {
1143                         last = f;
1144                         f = f->upstream;
1145                 }
1146                 return (last);
1147         }
1148         if (n < 0)
1149                 return NULL;
1150         while (n > 0 && f != NULL) {
1151                 f = f->upstream;
1152                 --n;
1153         }
1154         return (f);
1155 }
1156
1157 static int
1158 _archive_filter_code(struct archive *_a, int n)
1159 {
1160         struct archive_read_filter *f = get_filter(_a, n);
1161         return f == NULL ? -1 : f->code;
1162 }
1163
1164 static const char *
1165 _archive_filter_name(struct archive *_a, int n)
1166 {
1167         struct archive_read_filter *f = get_filter(_a, n);
1168         return f != NULL ? f->name : NULL;
1169 }
1170
1171 static int64_t
1172 _archive_filter_bytes(struct archive *_a, int n)
1173 {
1174         struct archive_read_filter *f = get_filter(_a, n);
1175         return f == NULL ? -1 : f->position;
1176 }
1177
1178 /*
1179  * Used internally by read format handlers to register their bid and
1180  * initialization functions.
1181  */
1182 int
1183 __archive_read_register_format(struct archive_read *a,
1184     void *format_data,
1185     const char *name,
1186     int (*bid)(struct archive_read *, int),
1187     int (*options)(struct archive_read *, const char *, const char *),
1188     int (*read_header)(struct archive_read *, struct archive_entry *),
1189     int (*read_data)(struct archive_read *, const void **, size_t *, int64_t *),
1190     int (*read_data_skip)(struct archive_read *),
1191     int64_t (*seek_data)(struct archive_read *, int64_t, int),
1192     int (*cleanup)(struct archive_read *),
1193     int (*format_capabilities)(struct archive_read *),
1194     int (*has_encrypted_entries)(struct archive_read *))
1195 {
1196         int i, number_slots;
1197
1198         archive_check_magic(&a->archive,
1199             ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
1200             "__archive_read_register_format");
1201
1202         number_slots = sizeof(a->formats) / sizeof(a->formats[0]);
1203
1204         for (i = 0; i < number_slots; i++) {
1205                 if (a->formats[i].bid == bid)
1206                         return (ARCHIVE_WARN); /* We've already installed */
1207                 if (a->formats[i].bid == NULL) {
1208                         a->formats[i].bid = bid;
1209                         a->formats[i].options = options;
1210                         a->formats[i].read_header = read_header;
1211                         a->formats[i].read_data = read_data;
1212                         a->formats[i].read_data_skip = read_data_skip;
1213                         a->formats[i].seek_data = seek_data;
1214                         a->formats[i].cleanup = cleanup;
1215                         a->formats[i].data = format_data;
1216                         a->formats[i].name = name;
1217                         a->formats[i].format_capabilties = format_capabilities;
1218                         a->formats[i].has_encrypted_entries = has_encrypted_entries;
1219                         return (ARCHIVE_OK);
1220                 }
1221         }
1222
1223         archive_set_error(&a->archive, ENOMEM,
1224             "Not enough slots for format registration");
1225         return (ARCHIVE_FATAL);
1226 }
1227
1228 /*
1229  * Used internally by decompression routines to register their bid and
1230  * initialization functions.
1231  */
1232 int
1233 __archive_read_get_bidder(struct archive_read *a,
1234     struct archive_read_filter_bidder **bidder)
1235 {
1236         int i, number_slots;
1237
1238         number_slots = sizeof(a->bidders) / sizeof(a->bidders[0]);
1239
1240         for (i = 0; i < number_slots; i++) {
1241                 if (a->bidders[i].bid == NULL) {
1242                         memset(a->bidders + i, 0, sizeof(a->bidders[0]));
1243                         *bidder = (a->bidders + i);
1244                         return (ARCHIVE_OK);
1245                 }
1246         }
1247
1248         archive_set_error(&a->archive, ENOMEM,
1249             "Not enough slots for filter registration");
1250         return (ARCHIVE_FATAL);
1251 }
1252
1253 /*
1254  * The next section implements the peek/consume internal I/O
1255  * system used by archive readers.  This system allows simple
1256  * read-ahead for consumers while preserving zero-copy operation
1257  * most of the time.
1258  *
1259  * The two key operations:
1260  *  * The read-ahead function returns a pointer to a block of data
1261  *    that satisfies a minimum request.
1262  *  * The consume function advances the file pointer.
1263  *
1264  * In the ideal case, filters generate blocks of data
1265  * and __archive_read_ahead() just returns pointers directly into
1266  * those blocks.  Then __archive_read_consume() just bumps those
1267  * pointers.  Only if your request would span blocks does the I/O
1268  * layer use a copy buffer to provide you with a contiguous block of
1269  * data.
1270  *
1271  * A couple of useful idioms:
1272  *  * "I just want some data."  Ask for 1 byte and pay attention to
1273  *    the "number of bytes available" from __archive_read_ahead().
1274  *    Consume whatever you actually use.
1275  *  * "I want to output a large block of data."  As above, ask for 1 byte,
1276  *    emit all that's available (up to whatever limit you have), consume
1277  *    it all, then repeat until you're done.  This effectively means that
1278  *    you're passing along the blocks that came from your provider.
1279  *  * "I want to peek ahead by a large amount."  Ask for 4k or so, then
1280  *    double and repeat until you get an error or have enough.  Note
1281  *    that the I/O layer will likely end up expanding its copy buffer
1282  *    to fit your request, so use this technique cautiously.  This
1283  *    technique is used, for example, by some of the format tasting
1284  *    code that has uncertain look-ahead needs.
1285  */
1286
1287 /*
1288  * Looks ahead in the input stream:
1289  *  * If 'avail' pointer is provided, that returns number of bytes available
1290  *    in the current buffer, which may be much larger than requested.
1291  *  * If end-of-file, *avail gets set to zero.
1292  *  * If error, *avail gets error code.
1293  *  * If request can be met, returns pointer to data.
1294  *  * If minimum request cannot be met, returns NULL.
1295  *
1296  * Note: If you just want "some data", ask for 1 byte and pay attention
1297  * to *avail, which will have the actual amount available.  If you
1298  * know exactly how many bytes you need, just ask for that and treat
1299  * a NULL return as an error.
1300  *
1301  * Important:  This does NOT move the file pointer.  See
1302  * __archive_read_consume() below.
1303  */
1304 const void *
1305 __archive_read_ahead(struct archive_read *a, size_t min, ssize_t *avail)
1306 {
1307         return (__archive_read_filter_ahead(a->filter, min, avail));
1308 }
1309
1310 const void *
1311 __archive_read_filter_ahead(struct archive_read_filter *filter,
1312     size_t min, ssize_t *avail)
1313 {
1314         ssize_t bytes_read;
1315         size_t tocopy;
1316
1317         if (filter->fatal) {
1318                 if (avail)
1319                         *avail = ARCHIVE_FATAL;
1320                 return (NULL);
1321         }
1322
1323         /*
1324          * Keep pulling more data until we can satisfy the request.
1325          */
1326         for (;;) {
1327
1328                 /*
1329                  * If we can satisfy from the copy buffer (and the
1330                  * copy buffer isn't empty), we're done.  In particular,
1331                  * note that min == 0 is a perfectly well-defined
1332                  * request.
1333                  */
1334                 if (filter->avail >= min && filter->avail > 0) {
1335                         if (avail != NULL)
1336                                 *avail = filter->avail;
1337                         return (filter->next);
1338                 }
1339
1340                 /*
1341                  * We can satisfy directly from client buffer if everything
1342                  * currently in the copy buffer is still in the client buffer.
1343                  */
1344                 if (filter->client_total >= filter->client_avail + filter->avail
1345                     && filter->client_avail + filter->avail >= min) {
1346                         /* "Roll back" to client buffer. */
1347                         filter->client_avail += filter->avail;
1348                         filter->client_next -= filter->avail;
1349                         /* Copy buffer is now empty. */
1350                         filter->avail = 0;
1351                         filter->next = filter->buffer;
1352                         /* Return data from client buffer. */
1353                         if (avail != NULL)
1354                                 *avail = filter->client_avail;
1355                         return (filter->client_next);
1356                 }
1357
1358                 /* Move data forward in copy buffer if necessary. */
1359                 if (filter->next > filter->buffer &&
1360                     filter->next + min > filter->buffer + filter->buffer_size) {
1361                         if (filter->avail > 0)
1362                                 memmove(filter->buffer, filter->next,
1363                                     filter->avail);
1364                         filter->next = filter->buffer;
1365                 }
1366
1367                 /* If we've used up the client data, get more. */
1368                 if (filter->client_avail <= 0) {
1369                         if (filter->end_of_file) {
1370                                 if (avail != NULL)
1371                                         *avail = 0;
1372                                 return (NULL);
1373                         }
1374                         bytes_read = (filter->read)(filter,
1375                             &filter->client_buff);
1376                         if (bytes_read < 0) {           /* Read error. */
1377                                 filter->client_total = filter->client_avail = 0;
1378                                 filter->client_next =
1379                                     filter->client_buff = NULL;
1380                                 filter->fatal = 1;
1381                                 if (avail != NULL)
1382                                         *avail = ARCHIVE_FATAL;
1383                                 return (NULL);
1384                         }
1385                         if (bytes_read == 0) {
1386                                 /* Check for another client object first */
1387                                 if (filter->archive->client.cursor !=
1388                                       filter->archive->client.nodes - 1) {
1389                                         if (client_switch_proxy(filter,
1390                                             filter->archive->client.cursor + 1)
1391                                             == ARCHIVE_OK)
1392                                                 continue;
1393                                 }
1394                                 /* Premature end-of-file. */
1395                                 filter->client_total = filter->client_avail = 0;
1396                                 filter->client_next =
1397                                     filter->client_buff = NULL;
1398                                 filter->end_of_file = 1;
1399                                 /* Return whatever we do have. */
1400                                 if (avail != NULL)
1401                                         *avail = filter->avail;
1402                                 return (NULL);
1403                         }
1404                         filter->client_total = bytes_read;
1405                         filter->client_avail = filter->client_total;
1406                         filter->client_next = filter->client_buff;
1407                 } else {
1408                         /*
1409                          * We can't satisfy the request from the copy
1410                          * buffer or the existing client data, so we
1411                          * need to copy more client data over to the
1412                          * copy buffer.
1413                          */
1414
1415                         /* Ensure the buffer is big enough. */
1416                         if (min > filter->buffer_size) {
1417                                 size_t s, t;
1418                                 char *p;
1419
1420                                 /* Double the buffer; watch for overflow. */
1421                                 s = t = filter->buffer_size;
1422                                 if (s == 0)
1423                                         s = min;
1424                                 while (s < min) {
1425                                         t *= 2;
1426                                         if (t <= s) { /* Integer overflow! */
1427                                                 archive_set_error(
1428                                                     &filter->archive->archive,
1429                                                     ENOMEM,
1430                                                     "Unable to allocate copy"
1431                                                     " buffer");
1432                                                 filter->fatal = 1;
1433                                                 if (avail != NULL)
1434                                                         *avail = ARCHIVE_FATAL;
1435                                                 return (NULL);
1436                                         }
1437                                         s = t;
1438                                 }
1439                                 /* Now s >= min, so allocate a new buffer. */
1440                                 p = (char *)malloc(s);
1441                                 if (p == NULL) {
1442                                         archive_set_error(
1443                                                 &filter->archive->archive,
1444                                                 ENOMEM,
1445                                             "Unable to allocate copy buffer");
1446                                         filter->fatal = 1;
1447                                         if (avail != NULL)
1448                                                 *avail = ARCHIVE_FATAL;
1449                                         return (NULL);
1450                                 }
1451                                 /* Move data into newly-enlarged buffer. */
1452                                 if (filter->avail > 0)
1453                                         memmove(p, filter->next, filter->avail);
1454                                 free(filter->buffer);
1455                                 filter->next = filter->buffer = p;
1456                                 filter->buffer_size = s;
1457                         }
1458
1459                         /* We can add client data to copy buffer. */
1460                         /* First estimate: copy to fill rest of buffer. */
1461                         tocopy = (filter->buffer + filter->buffer_size)
1462                             - (filter->next + filter->avail);
1463                         /* Don't waste time buffering more than we need to. */
1464                         if (tocopy + filter->avail > min)
1465                                 tocopy = min - filter->avail;
1466                         /* Don't copy more than is available. */
1467                         if (tocopy > filter->client_avail)
1468                                 tocopy = filter->client_avail;
1469
1470                         memcpy(filter->next + filter->avail,
1471                             filter->client_next, tocopy);
1472                         /* Remove this data from client buffer. */
1473                         filter->client_next += tocopy;
1474                         filter->client_avail -= tocopy;
1475                         /* add it to copy buffer. */
1476                         filter->avail += tocopy;
1477                 }
1478         }
1479 }
1480
1481 /*
1482  * Move the file pointer forward.
1483  */
1484 int64_t
1485 __archive_read_consume(struct archive_read *a, int64_t request)
1486 {
1487         return (__archive_read_filter_consume(a->filter, request));
1488 }
1489
1490 int64_t
1491 __archive_read_filter_consume(struct archive_read_filter * filter,
1492     int64_t request)
1493 {
1494         int64_t skipped;
1495
1496         if (request < 0)
1497                 return ARCHIVE_FATAL;
1498         if (request == 0)
1499                 return 0;
1500
1501         skipped = advance_file_pointer(filter, request);
1502         if (skipped == request)
1503                 return (skipped);
1504         /* We hit EOF before we satisfied the skip request. */
1505         if (skipped < 0)  /* Map error code to 0 for error message below. */
1506                 skipped = 0;
1507         archive_set_error(&filter->archive->archive,
1508             ARCHIVE_ERRNO_MISC,
1509             "Truncated input file (needed %jd bytes, only %jd available)",
1510             (intmax_t)request, (intmax_t)skipped);
1511         return (ARCHIVE_FATAL);
1512 }
1513
1514 /*
1515  * Advance the file pointer by the amount requested.
1516  * Returns the amount actually advanced, which may be less than the
1517  * request if EOF is encountered first.
1518  * Returns a negative value if there's an I/O error.
1519  */
1520 static int64_t
1521 advance_file_pointer(struct archive_read_filter *filter, int64_t request)
1522 {
1523         int64_t bytes_skipped, total_bytes_skipped = 0;
1524         ssize_t bytes_read;
1525         size_t min;
1526
1527         if (filter->fatal)
1528                 return (-1);
1529
1530         /* Use up the copy buffer first. */
1531         if (filter->avail > 0) {
1532                 min = (size_t)minimum(request, (int64_t)filter->avail);
1533                 filter->next += min;
1534                 filter->avail -= min;
1535                 request -= min;
1536                 filter->position += min;
1537                 total_bytes_skipped += min;
1538         }
1539
1540         /* Then use up the client buffer. */
1541         if (filter->client_avail > 0) {
1542                 min = (size_t)minimum(request, (int64_t)filter->client_avail);
1543                 filter->client_next += min;
1544                 filter->client_avail -= min;
1545                 request -= min;
1546                 filter->position += min;
1547                 total_bytes_skipped += min;
1548         }
1549         if (request == 0)
1550                 return (total_bytes_skipped);
1551
1552         /* If there's an optimized skip function, use it. */
1553         if (filter->skip != NULL) {
1554                 bytes_skipped = (filter->skip)(filter, request);
1555                 if (bytes_skipped < 0) {        /* error */
1556                         filter->fatal = 1;
1557                         return (bytes_skipped);
1558                 }
1559                 filter->position += bytes_skipped;
1560                 total_bytes_skipped += bytes_skipped;
1561                 request -= bytes_skipped;
1562                 if (request == 0)
1563                         return (total_bytes_skipped);
1564         }
1565
1566         /* Use ordinary reads as necessary to complete the request. */
1567         for (;;) {
1568                 bytes_read = (filter->read)(filter, &filter->client_buff);
1569                 if (bytes_read < 0) {
1570                         filter->client_buff = NULL;
1571                         filter->fatal = 1;
1572                         return (bytes_read);
1573                 }
1574
1575                 if (bytes_read == 0) {
1576                         if (filter->archive->client.cursor !=
1577                               filter->archive->client.nodes - 1) {
1578                                 if (client_switch_proxy(filter,
1579                                     filter->archive->client.cursor + 1)
1580                                     == ARCHIVE_OK)
1581                                         continue;
1582                         }
1583                         filter->client_buff = NULL;
1584                         filter->end_of_file = 1;
1585                         return (total_bytes_skipped);
1586                 }
1587
1588                 if (bytes_read >= request) {
1589                         filter->client_next =
1590                             ((const char *)filter->client_buff) + request;
1591                         filter->client_avail = (size_t)(bytes_read - request);
1592                         filter->client_total = bytes_read;
1593                         total_bytes_skipped += request;
1594                         filter->position += request;
1595                         return (total_bytes_skipped);
1596                 }
1597
1598                 filter->position += bytes_read;
1599                 total_bytes_skipped += bytes_read;
1600                 request -= bytes_read;
1601         }
1602 }
1603
1604 /**
1605  * Returns ARCHIVE_FAILED if seeking isn't supported.
1606  */
1607 int64_t
1608 __archive_read_seek(struct archive_read *a, int64_t offset, int whence)
1609 {
1610         return __archive_read_filter_seek(a->filter, offset, whence);
1611 }
1612
1613 int64_t
1614 __archive_read_filter_seek(struct archive_read_filter *filter, int64_t offset,
1615     int whence)
1616 {
1617         struct archive_read_client *client;
1618         int64_t r;
1619         unsigned int cursor;
1620
1621         if (filter->closed || filter->fatal)
1622                 return (ARCHIVE_FATAL);
1623         if (filter->seek == NULL)
1624                 return (ARCHIVE_FAILED);
1625
1626         client = &(filter->archive->client);
1627         switch (whence) {
1628         case SEEK_CUR:
1629                 /* Adjust the offset and use SEEK_SET instead */
1630                 offset += filter->position;
1631                 __LA_FALLTHROUGH;
1632         case SEEK_SET:
1633                 cursor = 0;
1634                 while (1)
1635                 {
1636                         if (client->dataset[cursor].begin_position < 0 ||
1637                             client->dataset[cursor].total_size < 0 ||
1638                             client->dataset[cursor].begin_position +
1639                               client->dataset[cursor].total_size - 1 > offset ||
1640                             cursor + 1 >= client->nodes)
1641                                 break;
1642                         r = client->dataset[cursor].begin_position +
1643                                 client->dataset[cursor].total_size;
1644                         client->dataset[++cursor].begin_position = r;
1645                 }
1646                 while (1) {
1647                         r = client_switch_proxy(filter, cursor);
1648                         if (r != ARCHIVE_OK)
1649                                 return r;
1650                         if ((r = client_seek_proxy(filter, 0, SEEK_END)) < 0)
1651                                 return r;
1652                         client->dataset[cursor].total_size = r;
1653                         if (client->dataset[cursor].begin_position +
1654                             client->dataset[cursor].total_size - 1 > offset ||
1655                             cursor + 1 >= client->nodes)
1656                                 break;
1657                         r = client->dataset[cursor].begin_position +
1658                                 client->dataset[cursor].total_size;
1659                         client->dataset[++cursor].begin_position = r;
1660                 }
1661                 offset -= client->dataset[cursor].begin_position;
1662                 if (offset < 0
1663                     || offset > client->dataset[cursor].total_size)
1664                         return ARCHIVE_FATAL;
1665                 if ((r = client_seek_proxy(filter, offset, SEEK_SET)) < 0)
1666                         return r;
1667                 break;
1668
1669         case SEEK_END:
1670                 cursor = 0;
1671                 while (1) {
1672                         if (client->dataset[cursor].begin_position < 0 ||
1673                             client->dataset[cursor].total_size < 0 ||
1674                             cursor + 1 >= client->nodes)
1675                                 break;
1676                         r = client->dataset[cursor].begin_position +
1677                                 client->dataset[cursor].total_size;
1678                         client->dataset[++cursor].begin_position = r;
1679                 }
1680                 while (1) {
1681                         r = client_switch_proxy(filter, cursor);
1682                         if (r != ARCHIVE_OK)
1683                                 return r;
1684                         if ((r = client_seek_proxy(filter, 0, SEEK_END)) < 0)
1685                                 return r;
1686                         client->dataset[cursor].total_size = r;
1687                         r = client->dataset[cursor].begin_position +
1688                                 client->dataset[cursor].total_size;
1689                         if (cursor + 1 >= client->nodes)
1690                                 break;
1691                         client->dataset[++cursor].begin_position = r;
1692                 }
1693                 while (1) {
1694                         if (r + offset >=
1695                             client->dataset[cursor].begin_position)
1696                                 break;
1697                         offset += client->dataset[cursor].total_size;
1698                         if (cursor == 0)
1699                                 break;
1700                         cursor--;
1701                         r = client->dataset[cursor].begin_position +
1702                                 client->dataset[cursor].total_size;
1703                 }
1704                 offset = (r + offset) - client->dataset[cursor].begin_position;
1705                 if ((r = client_switch_proxy(filter, cursor)) != ARCHIVE_OK)
1706                         return r;
1707                 r = client_seek_proxy(filter, offset, SEEK_SET);
1708                 if (r < ARCHIVE_OK)
1709                         return r;
1710                 break;
1711
1712         default:
1713                 return (ARCHIVE_FATAL);
1714         }
1715         r += client->dataset[cursor].begin_position;
1716
1717         if (r >= 0) {
1718                 /*
1719                  * Ouch.  Clearing the buffer like this hurts, especially
1720                  * at bid time.  A lot of our efficiency at bid time comes
1721                  * from having bidders reuse the data we've already read.
1722                  *
1723                  * TODO: If the seek request is in data we already
1724                  * have, then don't call the seek callback.
1725                  *
1726                  * TODO: Zip seeks to end-of-file at bid time.  If
1727                  * other formats also start doing this, we may need to
1728                  * find a way for clients to fudge the seek offset to
1729                  * a block boundary.
1730                  *
1731                  * Hmmm... If whence was SEEK_END, we know the file
1732                  * size is (r - offset).  Can we use that to simplify
1733                  * the TODO items above?
1734                  */
1735                 filter->avail = filter->client_avail = 0;
1736                 filter->next = filter->buffer;
1737                 filter->position = r;
1738                 filter->end_of_file = 0;
1739         }
1740         return r;
1741 }