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