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