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