archive_read.c revision 318482
1228753Smm/*-
2232153Smm * Copyright (c) 2003-2011 Tim Kientzle
3228753Smm * All rights reserved.
4228753Smm *
5228753Smm * Redistribution and use in source and binary forms, with or without
6228753Smm * modification, are permitted provided that the following conditions
7228753Smm * are met:
8228753Smm * 1. Redistributions of source code must retain the above copyright
9228753Smm *    notice, this list of conditions and the following disclaimer.
10228753Smm * 2. Redistributions in binary form must reproduce the above copyright
11228753Smm *    notice, this list of conditions and the following disclaimer in the
12228753Smm *    documentation and/or other materials provided with the distribution.
13228753Smm *
14228753Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15228753Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16228753Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17228753Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18228753Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19228753Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20228753Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21228753Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22228753Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23228753Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24228753Smm */
25228753Smm
26228753Smm/*
27228753Smm * This file contains the "essential" portions of the read API, that
28228753Smm * is, stuff that will probably always be used by any client that
29228753Smm * actually needs to read an archive.  Optional pieces have been, as
30228753Smm * far as possible, separated out into separate files to avoid
31228753Smm * needlessly bloating statically-linked clients.
32228753Smm */
33228753Smm
34228753Smm#include "archive_platform.h"
35228763Smm__FBSDID("$FreeBSD: stable/11/contrib/libarchive/libarchive/archive_read.c 318482 2017-05-18 19:47:43Z mm $");
36228753Smm
37228753Smm#ifdef HAVE_ERRNO_H
38228753Smm#include <errno.h>
39228753Smm#endif
40228753Smm#include <stdio.h>
41228753Smm#ifdef HAVE_STDLIB_H
42228753Smm#include <stdlib.h>
43228753Smm#endif
44228753Smm#ifdef HAVE_STRING_H
45228753Smm#include <string.h>
46228753Smm#endif
47228753Smm#ifdef HAVE_UNISTD_H
48228753Smm#include <unistd.h>
49228753Smm#endif
50228753Smm
51228753Smm#include "archive.h"
52228753Smm#include "archive_entry.h"
53228753Smm#include "archive_private.h"
54228753Smm#include "archive_read_private.h"
55228753Smm
56228753Smm#define minimum(a, b) (a < b ? a : b)
57228753Smm
58232153Smmstatic int	choose_filters(struct archive_read *);
59228753Smmstatic int	choose_format(struct archive_read *);
60311041Smmstatic int	close_filters(struct archive_read *);
61228753Smmstatic struct archive_vtable *archive_read_vtable(void);
62232153Smmstatic int64_t	_archive_filter_bytes(struct archive *, int);
63232153Smmstatic int	_archive_filter_code(struct archive *, int);
64232153Smmstatic const char *_archive_filter_name(struct archive *, int);
65232153Smmstatic int  _archive_filter_count(struct archive *);
66228753Smmstatic int	_archive_read_close(struct archive *);
67232153Smmstatic int	_archive_read_data_block(struct archive *,
68232153Smm		    const void **, size_t *, int64_t *);
69228773Smmstatic int	_archive_read_free(struct archive *);
70232153Smmstatic int	_archive_read_next_header(struct archive *,
71232153Smm		    struct archive_entry **);
72232153Smmstatic int	_archive_read_next_header2(struct archive *,
73232153Smm		    struct archive_entry *);
74232153Smmstatic int64_t  advance_file_pointer(struct archive_read_filter *, int64_t);
75228753Smm
76228753Smmstatic struct archive_vtable *
77228753Smmarchive_read_vtable(void)
78228753Smm{
79228753Smm	static struct archive_vtable av;
80228753Smm	static int inited = 0;
81228753Smm
82228753Smm	if (!inited) {
83232153Smm		av.archive_filter_bytes = _archive_filter_bytes;
84232153Smm		av.archive_filter_code = _archive_filter_code;
85232153Smm		av.archive_filter_name = _archive_filter_name;
86232153Smm		av.archive_filter_count = _archive_filter_count;
87232153Smm		av.archive_read_data_block = _archive_read_data_block;
88232153Smm		av.archive_read_next_header = _archive_read_next_header;
89232153Smm		av.archive_read_next_header2 = _archive_read_next_header2;
90228773Smm		av.archive_free = _archive_read_free;
91228753Smm		av.archive_close = _archive_read_close;
92232153Smm		inited = 1;
93228753Smm	}
94228753Smm	return (&av);
95228753Smm}
96228753Smm
97228753Smm/*
98228753Smm * Allocate, initialize and return a struct archive object.
99228753Smm */
100228753Smmstruct archive *
101228753Smmarchive_read_new(void)
102228753Smm{
103228753Smm	struct archive_read *a;
104228753Smm
105299529Smm	a = (struct archive_read *)calloc(1, sizeof(*a));
106228753Smm	if (a == NULL)
107228753Smm		return (NULL);
108228753Smm	a->archive.magic = ARCHIVE_READ_MAGIC;
109228753Smm
110228753Smm	a->archive.state = ARCHIVE_STATE_NEW;
111232153Smm	a->entry = archive_entry_new2(&a->archive);
112228753Smm	a->archive.vtable = archive_read_vtable();
113228753Smm
114299529Smm	a->passphrases.last = &a->passphrases.first;
115299529Smm
116228753Smm	return (&a->archive);
117228753Smm}
118228753Smm
119228753Smm/*
120228753Smm * Record the do-not-extract-to file. This belongs in archive_read_extract.c.
121228753Smm */
122228753Smmvoid
123232153Smmarchive_read_extract_set_skip_file(struct archive *_a, int64_t d, int64_t i)
124228753Smm{
125228753Smm	struct archive_read *a = (struct archive_read *)_a;
126232153Smm
127232153Smm	if (ARCHIVE_OK != __archive_check_magic(_a, ARCHIVE_READ_MAGIC,
128232153Smm		ARCHIVE_STATE_ANY, "archive_read_extract_set_skip_file"))
129232153Smm		return;
130232153Smm	a->skip_file_set = 1;
131228753Smm	a->skip_file_dev = d;
132228753Smm	a->skip_file_ino = i;
133228753Smm}
134228753Smm
135228753Smm/*
136232153Smm * Open the archive
137228753Smm */
138228753Smmint
139232153Smmarchive_read_open(struct archive *a, void *client_data,
140232153Smm    archive_open_callback *client_opener, archive_read_callback *client_reader,
141232153Smm    archive_close_callback *client_closer)
142228753Smm{
143232153Smm	/* Old archive_read_open() is just a thin shell around
144232153Smm	 * archive_read_open1. */
145232153Smm	archive_read_set_open_callback(a, client_opener);
146232153Smm	archive_read_set_read_callback(a, client_reader);
147232153Smm	archive_read_set_close_callback(a, client_closer);
148232153Smm	archive_read_set_callback_data(a, client_data);
149232153Smm	return archive_read_open1(a);
150228753Smm}
151228753Smm
152228753Smm
153228753Smmint
154232153Smmarchive_read_open2(struct archive *a, void *client_data,
155232153Smm    archive_open_callback *client_opener,
156232153Smm    archive_read_callback *client_reader,
157232153Smm    archive_skip_callback *client_skipper,
158228753Smm    archive_close_callback *client_closer)
159228753Smm{
160232153Smm	/* Old archive_read_open2() is just a thin shell around
161232153Smm	 * archive_read_open1. */
162232153Smm	archive_read_set_callback_data(a, client_data);
163232153Smm	archive_read_set_open_callback(a, client_opener);
164232153Smm	archive_read_set_read_callback(a, client_reader);
165232153Smm	archive_read_set_skip_callback(a, client_skipper);
166232153Smm	archive_read_set_close_callback(a, client_closer);
167232153Smm	return archive_read_open1(a);
168228753Smm}
169228753Smm
170228753Smmstatic ssize_t
171228753Smmclient_read_proxy(struct archive_read_filter *self, const void **buff)
172228753Smm{
173228753Smm	ssize_t r;
174228753Smm	r = (self->archive->client.reader)(&self->archive->archive,
175228753Smm	    self->data, buff);
176228753Smm	return (r);
177228753Smm}
178228753Smm
179228753Smmstatic int64_t
180228753Smmclient_skip_proxy(struct archive_read_filter *self, int64_t request)
181228753Smm{
182232153Smm	if (request < 0)
183232153Smm		__archive_errx(1, "Negative skip requested.");
184232153Smm	if (request == 0)
185232153Smm		return 0;
186228753Smm
187232153Smm	if (self->archive->client.skipper != NULL) {
188232153Smm		/* Seek requests over 1GiB are broken down into
189232153Smm		 * multiple seeks.  This avoids overflows when the
190232153Smm		 * requests get passed through 32-bit arguments. */
191232153Smm		int64_t skip_limit = (int64_t)1 << 30;
192232153Smm		int64_t total = 0;
193232153Smm		for (;;) {
194232153Smm			int64_t get, ask = request;
195232153Smm			if (ask > skip_limit)
196232153Smm				ask = skip_limit;
197248616Smm			get = (self->archive->client.skipper)
198248616Smm				(&self->archive->archive, self->data, ask);
199299529Smm			total += get;
200299529Smm			if (get == 0 || get == request)
201232153Smm				return (total);
202299529Smm			if (get > request)
203299529Smm				return ARCHIVE_FATAL;
204232153Smm			request -= get;
205232153Smm		}
206232153Smm	} else if (self->archive->client.seeker != NULL
207232153Smm		&& request > 64 * 1024) {
208232153Smm		/* If the client provided a seeker but not a skipper,
209232153Smm		 * we can use the seeker to skip forward.
210232153Smm		 *
211232153Smm		 * Note: This isn't always a good idea.  The client
212232153Smm		 * skipper is allowed to skip by less than requested
213232153Smm		 * if it needs to maintain block alignment.  The
214232153Smm		 * seeker is not allowed to play such games, so using
215232153Smm		 * the seeker here may be a performance loss compared
216232153Smm		 * to just reading and discarding.  That's why we
217232153Smm		 * only do this for skips of over 64k.
218232153Smm		 */
219232153Smm		int64_t before = self->position;
220248616Smm		int64_t after = (self->archive->client.seeker)
221248616Smm		    (&self->archive->archive, self->data, request, SEEK_CUR);
222232153Smm		if (after != before + request)
223232153Smm			return ARCHIVE_FATAL;
224232153Smm		return after - before;
225228753Smm	}
226232153Smm	return 0;
227228753Smm}
228228753Smm
229232153Smmstatic int64_t
230232153Smmclient_seek_proxy(struct archive_read_filter *self, int64_t offset, int whence)
231232153Smm{
232232153Smm	/* DO NOT use the skipper here!  If we transparently handled
233232153Smm	 * forward seek here by using the skipper, that will break
234232153Smm	 * other libarchive code that assumes a successful forward
235232153Smm	 * seek means it can also seek backwards.
236232153Smm	 */
237299529Smm	if (self->archive->client.seeker == NULL) {
238299529Smm		archive_set_error(&self->archive->archive, ARCHIVE_ERRNO_MISC,
239299529Smm		    "Current client reader does not support seeking a device");
240232153Smm		return (ARCHIVE_FAILED);
241299529Smm	}
242232153Smm	return (self->archive->client.seeker)(&self->archive->archive,
243232153Smm	    self->data, offset, whence);
244232153Smm}
245232153Smm
246228753Smmstatic int
247228753Smmclient_close_proxy(struct archive_read_filter *self)
248228753Smm{
249248616Smm	int r = ARCHIVE_OK, r2;
250248616Smm	unsigned int i;
251228753Smm
252248616Smm	if (self->archive->client.closer == NULL)
253248616Smm		return (r);
254248616Smm	for (i = 0; i < self->archive->client.nodes; i++)
255248616Smm	{
256248616Smm		r2 = (self->archive->client.closer)
257248616Smm			((struct archive *)self->archive,
258248616Smm				self->archive->client.dataset[i].data);
259248616Smm		if (r > r2)
260248616Smm			r = r2;
261248616Smm	}
262228753Smm	return (r);
263228753Smm}
264228753Smm
265248616Smmstatic int
266248616Smmclient_open_proxy(struct archive_read_filter *self)
267248616Smm{
268248616Smm  int r = ARCHIVE_OK;
269248616Smm	if (self->archive->client.opener != NULL)
270248616Smm		r = (self->archive->client.opener)(
271248616Smm		    (struct archive *)self->archive, self->data);
272248616Smm	return (r);
273248616Smm}
274248616Smm
275248616Smmstatic int
276248616Smmclient_switch_proxy(struct archive_read_filter *self, unsigned int iindex)
277248616Smm{
278248616Smm  int r1 = ARCHIVE_OK, r2 = ARCHIVE_OK;
279248616Smm	void *data2 = NULL;
280248616Smm
281248616Smm	/* Don't do anything if already in the specified data node */
282248616Smm	if (self->archive->client.cursor == iindex)
283248616Smm		return (ARCHIVE_OK);
284248616Smm
285248616Smm	self->archive->client.cursor = iindex;
286248616Smm	data2 = self->archive->client.dataset[self->archive->client.cursor].data;
287248616Smm	if (self->archive->client.switcher != NULL)
288248616Smm	{
289248616Smm		r1 = r2 = (self->archive->client.switcher)
290248616Smm			((struct archive *)self->archive, self->data, data2);
291248616Smm		self->data = data2;
292248616Smm	}
293248616Smm	else
294248616Smm	{
295248616Smm		/* Attempt to call close and open instead */
296248616Smm		if (self->archive->client.closer != NULL)
297248616Smm			r1 = (self->archive->client.closer)
298248616Smm				((struct archive *)self->archive, self->data);
299248616Smm		self->data = data2;
300248616Smm		if (self->archive->client.opener != NULL)
301248616Smm			r2 = (self->archive->client.opener)
302248616Smm				((struct archive *)self->archive, self->data);
303248616Smm	}
304248616Smm	return (r1 < r2) ? r1 : r2;
305248616Smm}
306248616Smm
307232153Smmint
308232153Smmarchive_read_set_open_callback(struct archive *_a,
309232153Smm    archive_open_callback *client_opener)
310232153Smm{
311232153Smm	struct archive_read *a = (struct archive_read *)_a;
312232153Smm	archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
313232153Smm	    "archive_read_set_open_callback");
314232153Smm	a->client.opener = client_opener;
315232153Smm	return ARCHIVE_OK;
316232153Smm}
317228753Smm
318228753Smmint
319232153Smmarchive_read_set_read_callback(struct archive *_a,
320232153Smm    archive_read_callback *client_reader)
321232153Smm{
322232153Smm	struct archive_read *a = (struct archive_read *)_a;
323232153Smm	archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
324232153Smm	    "archive_read_set_read_callback");
325232153Smm	a->client.reader = client_reader;
326232153Smm	return ARCHIVE_OK;
327232153Smm}
328232153Smm
329232153Smmint
330232153Smmarchive_read_set_skip_callback(struct archive *_a,
331232153Smm    archive_skip_callback *client_skipper)
332232153Smm{
333232153Smm	struct archive_read *a = (struct archive_read *)_a;
334232153Smm	archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
335232153Smm	    "archive_read_set_skip_callback");
336232153Smm	a->client.skipper = client_skipper;
337232153Smm	return ARCHIVE_OK;
338232153Smm}
339232153Smm
340232153Smmint
341232153Smmarchive_read_set_seek_callback(struct archive *_a,
342232153Smm    archive_seek_callback *client_seeker)
343232153Smm{
344232153Smm	struct archive_read *a = (struct archive_read *)_a;
345232153Smm	archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
346232153Smm	    "archive_read_set_seek_callback");
347232153Smm	a->client.seeker = client_seeker;
348232153Smm	return ARCHIVE_OK;
349232153Smm}
350232153Smm
351232153Smmint
352232153Smmarchive_read_set_close_callback(struct archive *_a,
353228753Smm    archive_close_callback *client_closer)
354228753Smm{
355228753Smm	struct archive_read *a = (struct archive_read *)_a;
356232153Smm	archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
357232153Smm	    "archive_read_set_close_callback");
358232153Smm	a->client.closer = client_closer;
359232153Smm	return ARCHIVE_OK;
360232153Smm}
361232153Smm
362232153Smmint
363248616Smmarchive_read_set_switch_callback(struct archive *_a,
364248616Smm    archive_switch_callback *client_switcher)
365248616Smm{
366248616Smm	struct archive_read *a = (struct archive_read *)_a;
367248616Smm	archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
368248616Smm	    "archive_read_set_switch_callback");
369248616Smm	a->client.switcher = client_switcher;
370248616Smm	return ARCHIVE_OK;
371248616Smm}
372248616Smm
373248616Smmint
374232153Smmarchive_read_set_callback_data(struct archive *_a, void *client_data)
375232153Smm{
376248616Smm	return archive_read_set_callback_data2(_a, client_data, 0);
377248616Smm}
378248616Smm
379248616Smmint
380248616Smmarchive_read_set_callback_data2(struct archive *_a, void *client_data,
381248616Smm    unsigned int iindex)
382248616Smm{
383232153Smm	struct archive_read *a = (struct archive_read *)_a;
384232153Smm	archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
385248616Smm	    "archive_read_set_callback_data2");
386248616Smm
387248616Smm	if (a->client.nodes == 0)
388248616Smm	{
389248616Smm		a->client.dataset = (struct archive_read_data_node *)
390248616Smm		    calloc(1, sizeof(*a->client.dataset));
391248616Smm		if (a->client.dataset == NULL)
392248616Smm		{
393248616Smm			archive_set_error(&a->archive, ENOMEM,
394248616Smm				"No memory.");
395248616Smm			return ARCHIVE_FATAL;
396248616Smm		}
397248616Smm		a->client.nodes = 1;
398248616Smm	}
399248616Smm
400248616Smm	if (iindex > a->client.nodes - 1)
401248616Smm	{
402248616Smm		archive_set_error(&a->archive, EINVAL,
403248616Smm			"Invalid index specified.");
404248616Smm		return ARCHIVE_FATAL;
405248616Smm	}
406248616Smm	a->client.dataset[iindex].data = client_data;
407248616Smm	a->client.dataset[iindex].begin_position = -1;
408248616Smm	a->client.dataset[iindex].total_size = -1;
409232153Smm	return ARCHIVE_OK;
410232153Smm}
411232153Smm
412232153Smmint
413248616Smmarchive_read_add_callback_data(struct archive *_a, void *client_data,
414248616Smm    unsigned int iindex)
415248616Smm{
416248616Smm	struct archive_read *a = (struct archive_read *)_a;
417248616Smm	void *p;
418248616Smm	unsigned int i;
419248616Smm
420248616Smm	archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
421248616Smm	    "archive_read_add_callback_data");
422248616Smm	if (iindex > a->client.nodes) {
423248616Smm		archive_set_error(&a->archive, EINVAL,
424248616Smm			"Invalid index specified.");
425248616Smm		return ARCHIVE_FATAL;
426248616Smm	}
427248616Smm	p = realloc(a->client.dataset, sizeof(*a->client.dataset)
428248616Smm		* (++(a->client.nodes)));
429248616Smm	if (p == NULL) {
430248616Smm		archive_set_error(&a->archive, ENOMEM,
431248616Smm			"No memory.");
432248616Smm		return ARCHIVE_FATAL;
433248616Smm	}
434248616Smm	a->client.dataset = (struct archive_read_data_node *)p;
435248616Smm	for (i = a->client.nodes - 1; i > iindex && i > 0; i--) {
436248616Smm		a->client.dataset[i].data = a->client.dataset[i-1].data;
437248616Smm		a->client.dataset[i].begin_position = -1;
438248616Smm		a->client.dataset[i].total_size = -1;
439248616Smm	}
440248616Smm	a->client.dataset[iindex].data = client_data;
441248616Smm	a->client.dataset[iindex].begin_position = -1;
442248616Smm	a->client.dataset[iindex].total_size = -1;
443248616Smm	return ARCHIVE_OK;
444248616Smm}
445248616Smm
446248616Smmint
447248616Smmarchive_read_append_callback_data(struct archive *_a, void *client_data)
448248616Smm{
449248616Smm	struct archive_read *a = (struct archive_read *)_a;
450248616Smm	return archive_read_add_callback_data(_a, client_data, a->client.nodes);
451248616Smm}
452248616Smm
453248616Smmint
454248616Smmarchive_read_prepend_callback_data(struct archive *_a, void *client_data)
455248616Smm{
456248616Smm	return archive_read_add_callback_data(_a, client_data, 0);
457248616Smm}
458248616Smm
459248616Smmint
460232153Smmarchive_read_open1(struct archive *_a)
461232153Smm{
462232153Smm	struct archive_read *a = (struct archive_read *)_a;
463248616Smm	struct archive_read_filter *filter, *tmp;
464299529Smm	int slot, e = ARCHIVE_OK;
465248616Smm	unsigned int i;
466228753Smm
467232153Smm	archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
468228753Smm	    "archive_read_open");
469228753Smm	archive_clear_error(&a->archive);
470228753Smm
471232153Smm	if (a->client.reader == NULL) {
472232153Smm		archive_set_error(&a->archive, EINVAL,
473228753Smm		    "No reader function provided to archive_read_open");
474232153Smm		a->archive.state = ARCHIVE_STATE_FATAL;
475232153Smm		return (ARCHIVE_FATAL);
476232153Smm	}
477228753Smm
478228753Smm	/* Open data source. */
479232153Smm	if (a->client.opener != NULL) {
480248616Smm		e = (a->client.opener)(&a->archive, a->client.dataset[0].data);
481228753Smm		if (e != 0) {
482228753Smm			/* If the open failed, call the closer to clean up. */
483248616Smm			if (a->client.closer) {
484248616Smm				for (i = 0; i < a->client.nodes; i++)
485248616Smm					(a->client.closer)(&a->archive,
486248616Smm					    a->client.dataset[i].data);
487248616Smm			}
488228753Smm			return (e);
489228753Smm		}
490228753Smm	}
491228753Smm
492228753Smm	filter = calloc(1, sizeof(*filter));
493228753Smm	if (filter == NULL)
494228753Smm		return (ARCHIVE_FATAL);
495228753Smm	filter->bidder = NULL;
496228753Smm	filter->upstream = NULL;
497228753Smm	filter->archive = a;
498248616Smm	filter->data = a->client.dataset[0].data;
499248616Smm	filter->open = client_open_proxy;
500228753Smm	filter->read = client_read_proxy;
501228753Smm	filter->skip = client_skip_proxy;
502232153Smm	filter->seek = client_seek_proxy;
503228753Smm	filter->close = client_close_proxy;
504248616Smm	filter->sswitch = client_switch_proxy;
505228753Smm	filter->name = "none";
506248616Smm	filter->code = ARCHIVE_FILTER_NONE;
507228753Smm
508248616Smm	a->client.dataset[0].begin_position = 0;
509248616Smm	if (!a->filter || !a->bypass_filter_bidding)
510248616Smm	{
511248616Smm		a->filter = filter;
512248616Smm		/* Build out the input pipeline. */
513248616Smm		e = choose_filters(a);
514248616Smm		if (e < ARCHIVE_WARN) {
515248616Smm			a->archive.state = ARCHIVE_STATE_FATAL;
516248616Smm			return (ARCHIVE_FATAL);
517248616Smm		}
518232153Smm	}
519248616Smm	else
520248616Smm	{
521248616Smm		/* Need to add "NONE" type filter at the end of the filter chain */
522248616Smm		tmp = a->filter;
523248616Smm		while (tmp->upstream)
524248616Smm			tmp = tmp->upstream;
525248616Smm		tmp->upstream = filter;
526248616Smm	}
527228753Smm
528248616Smm	if (!a->format)
529248616Smm	{
530248616Smm		slot = choose_format(a);
531248616Smm		if (slot < 0) {
532311041Smm			close_filters(a);
533248616Smm			a->archive.state = ARCHIVE_STATE_FATAL;
534248616Smm			return (ARCHIVE_FATAL);
535248616Smm		}
536248616Smm		a->format = &(a->formats[slot]);
537232153Smm	}
538232153Smm
539232153Smm	a->archive.state = ARCHIVE_STATE_HEADER;
540248616Smm
541248616Smm	/* Ensure libarchive starts from the first node in a multivolume set */
542248616Smm	client_switch_proxy(a->filter, 0);
543228753Smm	return (e);
544228753Smm}
545228753Smm
546228753Smm/*
547228753Smm * Allow each registered stream transform to bid on whether
548228753Smm * it wants to handle this stream.  Repeat until we've finished
549228753Smm * building the pipeline.
550228753Smm */
551299529Smm
552299529Smm/* We won't build a filter pipeline with more stages than this. */
553299529Smm#define MAX_NUMBER_FILTERS 25
554299529Smm
555228753Smmstatic int
556232153Smmchoose_filters(struct archive_read *a)
557228753Smm{
558299529Smm	int number_bidders, i, bid, best_bid, number_filters;
559228753Smm	struct archive_read_filter_bidder *bidder, *best_bidder;
560228753Smm	struct archive_read_filter *filter;
561228753Smm	ssize_t avail;
562228753Smm	int r;
563228753Smm
564299529Smm	for (number_filters = 0; number_filters < MAX_NUMBER_FILTERS; ++number_filters) {
565228753Smm		number_bidders = sizeof(a->bidders) / sizeof(a->bidders[0]);
566228753Smm
567228753Smm		best_bid = 0;
568228753Smm		best_bidder = NULL;
569228753Smm
570228753Smm		bidder = a->bidders;
571228753Smm		for (i = 0; i < number_bidders; i++, bidder++) {
572228753Smm			if (bidder->bid != NULL) {
573228753Smm				bid = (bidder->bid)(bidder, a->filter);
574228753Smm				if (bid > best_bid) {
575228753Smm					best_bid = bid;
576228753Smm					best_bidder = bidder;
577228753Smm				}
578228753Smm			}
579228753Smm		}
580228753Smm
581228753Smm		/* If no bidder, we're done. */
582228753Smm		if (best_bidder == NULL) {
583232153Smm			/* Verify the filter by asking it for some data. */
584228753Smm			__archive_read_filter_ahead(a->filter, 1, &avail);
585228753Smm			if (avail < 0) {
586248616Smm				__archive_read_free_filters(a);
587228753Smm				return (ARCHIVE_FATAL);
588228753Smm			}
589228753Smm			a->archive.compression_name = a->filter->name;
590228753Smm			a->archive.compression_code = a->filter->code;
591228753Smm			return (ARCHIVE_OK);
592228753Smm		}
593228753Smm
594228753Smm		filter
595228753Smm		    = (struct archive_read_filter *)calloc(1, sizeof(*filter));
596228753Smm		if (filter == NULL)
597228753Smm			return (ARCHIVE_FATAL);
598228753Smm		filter->bidder = best_bidder;
599228753Smm		filter->archive = a;
600228753Smm		filter->upstream = a->filter;
601228753Smm		a->filter = filter;
602228753Smm		r = (best_bidder->init)(a->filter);
603228753Smm		if (r != ARCHIVE_OK) {
604248616Smm			__archive_read_free_filters(a);
605232153Smm			return (ARCHIVE_FATAL);
606228753Smm		}
607228753Smm	}
608295914Sdelphij	archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
609295914Sdelphij	    "Input requires too many filters for decoding");
610295914Sdelphij	return (ARCHIVE_FATAL);
611228753Smm}
612228753Smm
613228753Smm/*
614228753Smm * Read header of next entry.
615228753Smm */
616232153Smmstatic int
617232153Smm_archive_read_next_header2(struct archive *_a, struct archive_entry *entry)
618228753Smm{
619228753Smm	struct archive_read *a = (struct archive_read *)_a;
620232153Smm	int r1 = ARCHIVE_OK, r2;
621228753Smm
622232153Smm	archive_check_magic(_a, ARCHIVE_READ_MAGIC,
623228753Smm	    ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
624228753Smm	    "archive_read_next_header");
625228753Smm
626228753Smm	archive_entry_clear(entry);
627228753Smm	archive_clear_error(&a->archive);
628228753Smm
629228753Smm	/*
630228753Smm	 * If client didn't consume entire data, skip any remainder
631228753Smm	 * (This is especially important for GNU incremental directories.)
632228753Smm	 */
633228753Smm	if (a->archive.state == ARCHIVE_STATE_DATA) {
634232153Smm		r1 = archive_read_data_skip(&a->archive);
635232153Smm		if (r1 == ARCHIVE_EOF)
636232153Smm			archive_set_error(&a->archive, EIO,
637232153Smm			    "Premature end-of-file.");
638232153Smm		if (r1 == ARCHIVE_EOF || r1 == ARCHIVE_FATAL) {
639228753Smm			a->archive.state = ARCHIVE_STATE_FATAL;
640228753Smm			return (ARCHIVE_FATAL);
641228753Smm		}
642228753Smm	}
643228753Smm
644232153Smm	/* Record start-of-header offset in uncompressed stream. */
645232153Smm	a->header_position = a->filter->position;
646228753Smm
647232153Smm	++_a->file_count;
648232153Smm	r2 = (a->format->read_header)(a, entry);
649228753Smm
650228753Smm	/*
651228753Smm	 * EOF and FATAL are persistent at this layer.  By
652228753Smm	 * modifying the state, we guarantee that future calls to
653228753Smm	 * read a header or read data will fail.
654228753Smm	 */
655232153Smm	switch (r2) {
656228753Smm	case ARCHIVE_EOF:
657228753Smm		a->archive.state = ARCHIVE_STATE_EOF;
658232153Smm		--_a->file_count;/* Revert a file counter. */
659228753Smm		break;
660228753Smm	case ARCHIVE_OK:
661228753Smm		a->archive.state = ARCHIVE_STATE_DATA;
662228753Smm		break;
663228753Smm	case ARCHIVE_WARN:
664228753Smm		a->archive.state = ARCHIVE_STATE_DATA;
665228753Smm		break;
666228753Smm	case ARCHIVE_RETRY:
667228753Smm		break;
668228753Smm	case ARCHIVE_FATAL:
669228753Smm		a->archive.state = ARCHIVE_STATE_FATAL;
670228753Smm		break;
671228753Smm	}
672228753Smm
673299529Smm	__archive_reset_read_data(&a->archive);
674299529Smm
675248616Smm	a->data_start_node = a->client.cursor;
676232153Smm	/* EOF always wins; otherwise return the worst error. */
677232153Smm	return (r2 < r1 || r2 == ARCHIVE_EOF) ? r2 : r1;
678228753Smm}
679228753Smm
680299529Smmstatic int
681232153Smm_archive_read_next_header(struct archive *_a, struct archive_entry **entryp)
682228753Smm{
683228753Smm	int ret;
684228753Smm	struct archive_read *a = (struct archive_read *)_a;
685228753Smm	*entryp = NULL;
686232153Smm	ret = _archive_read_next_header2(_a, a->entry);
687228753Smm	*entryp = a->entry;
688228753Smm	return ret;
689228753Smm}
690228753Smm
691228753Smm/*
692228753Smm * Allow each registered format to bid on whether it wants to handle
693228753Smm * the next entry.  Return index of winning bidder.
694228753Smm */
695228753Smmstatic int
696228753Smmchoose_format(struct archive_read *a)
697228753Smm{
698228753Smm	int slots;
699228753Smm	int i;
700228753Smm	int bid, best_bid;
701228753Smm	int best_bid_slot;
702228753Smm
703228753Smm	slots = sizeof(a->formats) / sizeof(a->formats[0]);
704228753Smm	best_bid = -1;
705228753Smm	best_bid_slot = -1;
706228753Smm
707232153Smm	/* Set up a->format for convenience of bidders. */
708228753Smm	a->format = &(a->formats[0]);
709228753Smm	for (i = 0; i < slots; i++, a->format++) {
710228753Smm		if (a->format->bid) {
711232153Smm			bid = (a->format->bid)(a, best_bid);
712228753Smm			if (bid == ARCHIVE_FATAL)
713228753Smm				return (ARCHIVE_FATAL);
714232153Smm			if (a->filter->position != 0)
715232153Smm				__archive_read_seek(a, 0, SEEK_SET);
716228753Smm			if ((bid > best_bid) || (best_bid_slot < 0)) {
717228753Smm				best_bid = bid;
718228753Smm				best_bid_slot = i;
719228753Smm			}
720228753Smm		}
721228753Smm	}
722228753Smm
723228753Smm	/*
724228753Smm	 * There were no bidders; this is a serious programmer error
725228753Smm	 * and demands a quick and definitive abort.
726228753Smm	 */
727232153Smm	if (best_bid_slot < 0) {
728232153Smm		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
729232153Smm		    "No formats registered");
730232153Smm		return (ARCHIVE_FATAL);
731232153Smm	}
732228753Smm
733228753Smm	/*
734228753Smm	 * There were bidders, but no non-zero bids; this means we
735228753Smm	 * can't support this stream.
736228753Smm	 */
737228753Smm	if (best_bid < 1) {
738228753Smm		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
739228753Smm		    "Unrecognized archive format");
740228753Smm		return (ARCHIVE_FATAL);
741228753Smm	}
742228753Smm
743228753Smm	return (best_bid_slot);
744228753Smm}
745228753Smm
746228753Smm/*
747228753Smm * Return the file offset (within the uncompressed data stream) where
748228753Smm * the last header started.
749228753Smm */
750228753Smmint64_t
751228753Smmarchive_read_header_position(struct archive *_a)
752228753Smm{
753228753Smm	struct archive_read *a = (struct archive_read *)_a;
754232153Smm	archive_check_magic(_a, ARCHIVE_READ_MAGIC,
755228753Smm	    ARCHIVE_STATE_ANY, "archive_read_header_position");
756228753Smm	return (a->header_position);
757228753Smm}
758228753Smm
759228753Smm/*
760299529Smm * Returns 1 if the archive contains at least one encrypted entry.
761299529Smm * If the archive format not support encryption at all
762299529Smm * ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED is returned.
763299529Smm * If for any other reason (e.g. not enough data read so far)
764299529Smm * we cannot say whether there are encrypted entries, then
765299529Smm * ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW is returned.
766299529Smm * In general, this function will return values below zero when the
767311041Smm * reader is uncertain or totally incapable of encryption support.
768299529Smm * When this function returns 0 you can be sure that the reader
769299529Smm * supports encryption detection but no encrypted entries have
770299529Smm * been found yet.
771299529Smm *
772299529Smm * NOTE: If the metadata/header of an archive is also encrypted, you
773299529Smm * cannot rely on the number of encrypted entries. That is why this
774299529Smm * function does not return the number of encrypted entries but#
775299529Smm * just shows that there are some.
776299529Smm */
777299529Smmint
778299529Smmarchive_read_has_encrypted_entries(struct archive *_a)
779299529Smm{
780299529Smm	struct archive_read *a = (struct archive_read *)_a;
781299529Smm	int format_supports_encryption = archive_read_format_capabilities(_a)
782299529Smm			& (ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_DATA | ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_METADATA);
783299529Smm
784299529Smm	if (!_a || !format_supports_encryption) {
785299529Smm		/* Format in general doesn't support encryption */
786299529Smm		return ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED;
787299529Smm	}
788299529Smm
789299529Smm	/* A reader potentially has read enough data now. */
790299529Smm	if (a->format && a->format->has_encrypted_entries) {
791299529Smm		return (a->format->has_encrypted_entries)(a);
792299529Smm	}
793299529Smm
794299529Smm	/* For any other reason we cannot say how many entries are there. */
795299529Smm	return ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW;
796299529Smm}
797299529Smm
798299529Smm/*
799299529Smm * Returns a bitmask of capabilities that are supported by the archive format reader.
800299529Smm * If the reader has no special capabilities, ARCHIVE_READ_FORMAT_CAPS_NONE is returned.
801299529Smm */
802299529Smmint
803299529Smmarchive_read_format_capabilities(struct archive *_a)
804299529Smm{
805299529Smm	struct archive_read *a = (struct archive_read *)_a;
806299529Smm	if (a && a->format && a->format->format_capabilties) {
807299529Smm		return (a->format->format_capabilties)(a);
808299529Smm	}
809299529Smm	return ARCHIVE_READ_FORMAT_CAPS_NONE;
810299529Smm}
811299529Smm
812299529Smm/*
813228753Smm * Read data from an archive entry, using a read(2)-style interface.
814228753Smm * This is a convenience routine that just calls
815228753Smm * archive_read_data_block and copies the results into the client
816228753Smm * buffer, filling any gaps with zero bytes.  Clients using this
817228753Smm * API can be completely ignorant of sparse-file issues; sparse files
818228753Smm * will simply be padded with nulls.
819228753Smm *
820228753Smm * DO NOT intermingle calls to this function and archive_read_data_block
821228753Smm * to read a single entry body.
822228753Smm */
823228753Smmssize_t
824228753Smmarchive_read_data(struct archive *_a, void *buff, size_t s)
825228753Smm{
826299529Smm	struct archive *a = (struct archive *)_a;
827228753Smm	char	*dest;
828228753Smm	const void *read_buf;
829228753Smm	size_t	 bytes_read;
830228753Smm	size_t	 len;
831228753Smm	int	 r;
832228753Smm
833228753Smm	bytes_read = 0;
834228753Smm	dest = (char *)buff;
835228753Smm
836228753Smm	while (s > 0) {
837228753Smm		if (a->read_data_remaining == 0) {
838228753Smm			read_buf = a->read_data_block;
839248616Smm			a->read_data_is_posix_read = 1;
840248616Smm			a->read_data_requested = s;
841299529Smm			r = archive_read_data_block(a, &read_buf,
842228753Smm			    &a->read_data_remaining, &a->read_data_offset);
843228753Smm			a->read_data_block = read_buf;
844228753Smm			if (r == ARCHIVE_EOF)
845228753Smm				return (bytes_read);
846228753Smm			/*
847228753Smm			 * Error codes are all negative, so the status
848228753Smm			 * return here cannot be confused with a valid
849228753Smm			 * byte count.  (ARCHIVE_OK is zero.)
850228753Smm			 */
851228753Smm			if (r < ARCHIVE_OK)
852228753Smm				return (r);
853228753Smm		}
854228753Smm
855228753Smm		if (a->read_data_offset < a->read_data_output_offset) {
856299529Smm			archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT,
857228753Smm			    "Encountered out-of-order sparse blocks");
858228753Smm			return (ARCHIVE_RETRY);
859228753Smm		}
860228753Smm
861228753Smm		/* Compute the amount of zero padding needed. */
862232153Smm		if (a->read_data_output_offset + (int64_t)s <
863228753Smm		    a->read_data_offset) {
864228753Smm			len = s;
865228753Smm		} else if (a->read_data_output_offset <
866228753Smm		    a->read_data_offset) {
867238856Smm			len = (size_t)(a->read_data_offset -
868238856Smm			    a->read_data_output_offset);
869228753Smm		} else
870228753Smm			len = 0;
871228753Smm
872228753Smm		/* Add zeroes. */
873228753Smm		memset(dest, 0, len);
874228753Smm		s -= len;
875228753Smm		a->read_data_output_offset += len;
876228753Smm		dest += len;
877228753Smm		bytes_read += len;
878228753Smm
879228753Smm		/* Copy data if there is any space left. */
880228753Smm		if (s > 0) {
881228753Smm			len = a->read_data_remaining;
882228753Smm			if (len > s)
883228753Smm				len = s;
884318482Smm			if (len)
885318482Smm				memcpy(dest, a->read_data_block, len);
886228753Smm			s -= len;
887228753Smm			a->read_data_block += len;
888228753Smm			a->read_data_remaining -= len;
889228753Smm			a->read_data_output_offset += len;
890228753Smm			a->read_data_offset += len;
891228753Smm			dest += len;
892228753Smm			bytes_read += len;
893228753Smm		}
894228753Smm	}
895248616Smm	a->read_data_is_posix_read = 0;
896248616Smm	a->read_data_requested = 0;
897228753Smm	return (bytes_read);
898228753Smm}
899228753Smm
900228753Smm/*
901299529Smm * Reset the read_data_* variables, used for starting a new entry.
902299529Smm */
903299529Smmvoid __archive_reset_read_data(struct archive * a)
904299529Smm{
905299529Smm	a->read_data_output_offset = 0;
906299529Smm	a->read_data_remaining = 0;
907299529Smm	a->read_data_is_posix_read = 0;
908299529Smm	a->read_data_requested = 0;
909299529Smm
910299529Smm   /* extra resets, from rar.c */
911299529Smm   a->read_data_block = NULL;
912299529Smm   a->read_data_offset = 0;
913299529Smm}
914299529Smm
915299529Smm/*
916228753Smm * Skip over all remaining data in this entry.
917228753Smm */
918228753Smmint
919228753Smmarchive_read_data_skip(struct archive *_a)
920228753Smm{
921228753Smm	struct archive_read *a = (struct archive_read *)_a;
922228753Smm	int r;
923228753Smm	const void *buff;
924228753Smm	size_t size;
925232153Smm	int64_t offset;
926228753Smm
927232153Smm	archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA,
928228753Smm	    "archive_read_data_skip");
929228753Smm
930228753Smm	if (a->format->read_data_skip != NULL)
931228753Smm		r = (a->format->read_data_skip)(a);
932228753Smm	else {
933228753Smm		while ((r = archive_read_data_block(&a->archive,
934228753Smm			    &buff, &size, &offset))
935228753Smm		    == ARCHIVE_OK)
936228753Smm			;
937228753Smm	}
938228753Smm
939228753Smm	if (r == ARCHIVE_EOF)
940228753Smm		r = ARCHIVE_OK;
941228753Smm
942228753Smm	a->archive.state = ARCHIVE_STATE_HEADER;
943228753Smm	return (r);
944228753Smm}
945228753Smm
946248616Smmint64_t
947248616Smmarchive_seek_data(struct archive *_a, int64_t offset, int whence)
948248616Smm{
949248616Smm	struct archive_read *a = (struct archive_read *)_a;
950248616Smm	archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA,
951248616Smm	    "archive_seek_data_block");
952248616Smm
953248616Smm	if (a->format->seek_data == NULL) {
954248616Smm		archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
955248616Smm		    "Internal error: "
956248616Smm		    "No format_seek_data_block function registered");
957248616Smm		return (ARCHIVE_FATAL);
958248616Smm	}
959248616Smm
960248616Smm	return (a->format->seek_data)(a, offset, whence);
961248616Smm}
962248616Smm
963228753Smm/*
964228753Smm * Read the next block of entry data from the archive.
965228753Smm * This is a zero-copy interface; the client receives a pointer,
966228753Smm * size, and file offset of the next available block of data.
967228753Smm *
968228753Smm * Returns ARCHIVE_OK if the operation is successful, ARCHIVE_EOF if
969228753Smm * the end of entry is encountered.
970228753Smm */
971232153Smmstatic int
972232153Smm_archive_read_data_block(struct archive *_a,
973232153Smm    const void **buff, size_t *size, int64_t *offset)
974228753Smm{
975228753Smm	struct archive_read *a = (struct archive_read *)_a;
976232153Smm	archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA,
977228753Smm	    "archive_read_data_block");
978228753Smm
979228753Smm	if (a->format->read_data == NULL) {
980228753Smm		archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
981228753Smm		    "Internal error: "
982299529Smm		    "No format->read_data function registered");
983228753Smm		return (ARCHIVE_FATAL);
984228753Smm	}
985228753Smm
986228753Smm	return (a->format->read_data)(a, buff, size, offset);
987228753Smm}
988228753Smm
989311041Smmstatic int
990311041Smmclose_filters(struct archive_read *a)
991232153Smm{
992232153Smm	struct archive_read_filter *f = a->filter;
993232153Smm	int r = ARCHIVE_OK;
994232153Smm	/* Close each filter in the pipeline. */
995232153Smm	while (f != NULL) {
996232153Smm		struct archive_read_filter *t = f->upstream;
997232153Smm		if (!f->closed && f->close != NULL) {
998232153Smm			int r1 = (f->close)(f);
999232153Smm			f->closed = 1;
1000232153Smm			if (r1 < r)
1001232153Smm				r = r1;
1002232153Smm		}
1003232153Smm		free(f->buffer);
1004232153Smm		f->buffer = NULL;
1005232153Smm		f = t;
1006232153Smm	}
1007232153Smm	return r;
1008232153Smm}
1009232153Smm
1010248616Smmvoid
1011248616Smm__archive_read_free_filters(struct archive_read *a)
1012232153Smm{
1013311041Smm	/* Make sure filters are closed and their buffers are freed */
1014311041Smm	close_filters(a);
1015311041Smm
1016232153Smm	while (a->filter != NULL) {
1017232153Smm		struct archive_read_filter *t = a->filter->upstream;
1018232153Smm		free(a->filter);
1019232153Smm		a->filter = t;
1020232153Smm	}
1021232153Smm}
1022232153Smm
1023228753Smm/*
1024232153Smm * return the count of # of filters in use
1025228753Smm */
1026228753Smmstatic int
1027232153Smm_archive_filter_count(struct archive *_a)
1028232153Smm{
1029232153Smm	struct archive_read *a = (struct archive_read *)_a;
1030232153Smm	struct archive_read_filter *p = a->filter;
1031232153Smm	int count = 0;
1032232153Smm	while(p) {
1033232153Smm		count++;
1034232153Smm		p = p->upstream;
1035232153Smm	}
1036232153Smm	return count;
1037232153Smm}
1038232153Smm
1039232153Smm/*
1040232153Smm * Close the file and all I/O.
1041232153Smm */
1042232153Smmstatic int
1043228753Smm_archive_read_close(struct archive *_a)
1044228753Smm{
1045228753Smm	struct archive_read *a = (struct archive_read *)_a;
1046228753Smm	int r = ARCHIVE_OK, r1 = ARCHIVE_OK;
1047228753Smm
1048232153Smm	archive_check_magic(&a->archive, ARCHIVE_READ_MAGIC,
1049232153Smm	    ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_read_close");
1050232153Smm	if (a->archive.state == ARCHIVE_STATE_CLOSED)
1051232153Smm		return (ARCHIVE_OK);
1052228753Smm	archive_clear_error(&a->archive);
1053228753Smm	a->archive.state = ARCHIVE_STATE_CLOSED;
1054228753Smm
1055228753Smm	/* TODO: Clean up the formatters. */
1056228753Smm
1057228753Smm	/* Release the filter objects. */
1058311041Smm	r1 = close_filters(a);
1059228753Smm	if (r1 < r)
1060228753Smm		r = r1;
1061228753Smm
1062228753Smm	return (r);
1063228753Smm}
1064228753Smm
1065228753Smm/*
1066228753Smm * Release memory and other resources.
1067228753Smm */
1068228753Smmstatic int
1069228773Smm_archive_read_free(struct archive *_a)
1070228753Smm{
1071228753Smm	struct archive_read *a = (struct archive_read *)_a;
1072299529Smm	struct archive_read_passphrase *p;
1073232153Smm	int i, n;
1074228753Smm	int slots;
1075228753Smm	int r = ARCHIVE_OK;
1076228753Smm
1077232153Smm	if (_a == NULL)
1078232153Smm		return (ARCHIVE_OK);
1079232153Smm	archive_check_magic(_a, ARCHIVE_READ_MAGIC,
1080232153Smm	    ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_read_free");
1081232153Smm	if (a->archive.state != ARCHIVE_STATE_CLOSED
1082232153Smm	    && a->archive.state != ARCHIVE_STATE_FATAL)
1083228753Smm		r = archive_read_close(&a->archive);
1084228753Smm
1085232153Smm	/* Call cleanup functions registered by optional components. */
1086232153Smm	if (a->cleanup_archive_extract != NULL)
1087232153Smm		r = (a->cleanup_archive_extract)(a);
1088232153Smm
1089228753Smm	/* Cleanup format-specific data. */
1090228753Smm	slots = sizeof(a->formats) / sizeof(a->formats[0]);
1091228753Smm	for (i = 0; i < slots; i++) {
1092228753Smm		a->format = &(a->formats[i]);
1093228753Smm		if (a->formats[i].cleanup)
1094228753Smm			(a->formats[i].cleanup)(a);
1095228753Smm	}
1096228753Smm
1097232153Smm	/* Free the filters */
1098248616Smm	__archive_read_free_filters(a);
1099232153Smm
1100232153Smm	/* Release the bidder objects. */
1101232153Smm	n = sizeof(a->bidders)/sizeof(a->bidders[0]);
1102232153Smm	for (i = 0; i < n; i++) {
1103232153Smm		if (a->bidders[i].free != NULL) {
1104232153Smm			int r1 = (a->bidders[i].free)(&a->bidders[i]);
1105232153Smm			if (r1 < r)
1106232153Smm				r = r1;
1107232153Smm		}
1108232153Smm	}
1109232153Smm
1110299529Smm	/* Release passphrase list. */
1111299529Smm	p = a->passphrases.first;
1112299529Smm	while (p != NULL) {
1113299529Smm		struct archive_read_passphrase *np = p->next;
1114299529Smm
1115299529Smm		/* A passphrase should be cleaned. */
1116299529Smm		memset(p->passphrase, 0, strlen(p->passphrase));
1117299529Smm		free(p->passphrase);
1118299529Smm		free(p);
1119299529Smm		p = np;
1120299529Smm	}
1121299529Smm
1122228753Smm	archive_string_free(&a->archive.error_string);
1123299529Smm	archive_entry_free(a->entry);
1124228753Smm	a->archive.magic = 0;
1125232153Smm	__archive_clean(&a->archive);
1126248616Smm	free(a->client.dataset);
1127228753Smm	free(a);
1128228753Smm	return (r);
1129228753Smm}
1130228753Smm
1131232153Smmstatic struct archive_read_filter *
1132232153Smmget_filter(struct archive *_a, int n)
1133232153Smm{
1134232153Smm	struct archive_read *a = (struct archive_read *)_a;
1135232153Smm	struct archive_read_filter *f = a->filter;
1136248616Smm	/* We use n == -1 for 'the last filter', which is always the
1137248616Smm	 * client proxy. */
1138232153Smm	if (n == -1 && f != NULL) {
1139232153Smm		struct archive_read_filter *last = f;
1140232153Smm		f = f->upstream;
1141232153Smm		while (f != NULL) {
1142232153Smm			last = f;
1143232153Smm			f = f->upstream;
1144232153Smm		}
1145232153Smm		return (last);
1146232153Smm	}
1147232153Smm	if (n < 0)
1148232153Smm		return NULL;
1149232153Smm	while (n > 0 && f != NULL) {
1150232153Smm		f = f->upstream;
1151232153Smm		--n;
1152232153Smm	}
1153232153Smm	return (f);
1154232153Smm}
1155232153Smm
1156232153Smmstatic int
1157232153Smm_archive_filter_code(struct archive *_a, int n)
1158232153Smm{
1159232153Smm	struct archive_read_filter *f = get_filter(_a, n);
1160232153Smm	return f == NULL ? -1 : f->code;
1161232153Smm}
1162232153Smm
1163232153Smmstatic const char *
1164232153Smm_archive_filter_name(struct archive *_a, int n)
1165232153Smm{
1166232153Smm	struct archive_read_filter *f = get_filter(_a, n);
1167299529Smm	return f != NULL ? f->name : NULL;
1168232153Smm}
1169232153Smm
1170232153Smmstatic int64_t
1171232153Smm_archive_filter_bytes(struct archive *_a, int n)
1172232153Smm{
1173232153Smm	struct archive_read_filter *f = get_filter(_a, n);
1174232153Smm	return f == NULL ? -1 : f->position;
1175232153Smm}
1176232153Smm
1177228753Smm/*
1178228753Smm * Used internally by read format handlers to register their bid and
1179228753Smm * initialization functions.
1180228753Smm */
1181228753Smmint
1182228753Smm__archive_read_register_format(struct archive_read *a,
1183228753Smm    void *format_data,
1184228753Smm    const char *name,
1185232153Smm    int (*bid)(struct archive_read *, int),
1186228753Smm    int (*options)(struct archive_read *, const char *, const char *),
1187228753Smm    int (*read_header)(struct archive_read *, struct archive_entry *),
1188232153Smm    int (*read_data)(struct archive_read *, const void **, size_t *, int64_t *),
1189228753Smm    int (*read_data_skip)(struct archive_read *),
1190248616Smm    int64_t (*seek_data)(struct archive_read *, int64_t, int),
1191299529Smm    int (*cleanup)(struct archive_read *),
1192299529Smm    int (*format_capabilities)(struct archive_read *),
1193299529Smm    int (*has_encrypted_entries)(struct archive_read *))
1194228753Smm{
1195228753Smm	int i, number_slots;
1196228753Smm
1197232153Smm	archive_check_magic(&a->archive,
1198228753Smm	    ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
1199228753Smm	    "__archive_read_register_format");
1200228753Smm
1201228753Smm	number_slots = sizeof(a->formats) / sizeof(a->formats[0]);
1202228753Smm
1203228753Smm	for (i = 0; i < number_slots; i++) {
1204228753Smm		if (a->formats[i].bid == bid)
1205228753Smm			return (ARCHIVE_WARN); /* We've already installed */
1206228753Smm		if (a->formats[i].bid == NULL) {
1207228753Smm			a->formats[i].bid = bid;
1208228753Smm			a->formats[i].options = options;
1209228753Smm			a->formats[i].read_header = read_header;
1210228753Smm			a->formats[i].read_data = read_data;
1211228753Smm			a->formats[i].read_data_skip = read_data_skip;
1212248616Smm			a->formats[i].seek_data = seek_data;
1213228753Smm			a->formats[i].cleanup = cleanup;
1214228753Smm			a->formats[i].data = format_data;
1215228753Smm			a->formats[i].name = name;
1216299529Smm			a->formats[i].format_capabilties = format_capabilities;
1217299529Smm			a->formats[i].has_encrypted_entries = has_encrypted_entries;
1218228753Smm			return (ARCHIVE_OK);
1219228753Smm		}
1220228753Smm	}
1221228753Smm
1222232153Smm	archive_set_error(&a->archive, ENOMEM,
1223232153Smm	    "Not enough slots for format registration");
1224232153Smm	return (ARCHIVE_FATAL);
1225228753Smm}
1226228753Smm
1227228753Smm/*
1228228753Smm * Used internally by decompression routines to register their bid and
1229228753Smm * initialization functions.
1230228753Smm */
1231232153Smmint
1232232153Smm__archive_read_get_bidder(struct archive_read *a,
1233232153Smm    struct archive_read_filter_bidder **bidder)
1234228753Smm{
1235228753Smm	int i, number_slots;
1236228753Smm
1237228753Smm	number_slots = sizeof(a->bidders) / sizeof(a->bidders[0]);
1238228753Smm
1239228753Smm	for (i = 0; i < number_slots; i++) {
1240228753Smm		if (a->bidders[i].bid == NULL) {
1241228753Smm			memset(a->bidders + i, 0, sizeof(a->bidders[0]));
1242232153Smm			*bidder = (a->bidders + i);
1243232153Smm			return (ARCHIVE_OK);
1244228753Smm		}
1245228753Smm	}
1246228753Smm
1247232153Smm	archive_set_error(&a->archive, ENOMEM,
1248232153Smm	    "Not enough slots for filter registration");
1249232153Smm	return (ARCHIVE_FATAL);
1250228753Smm}
1251228753Smm
1252228753Smm/*
1253232153Smm * The next section implements the peek/consume internal I/O
1254232153Smm * system used by archive readers.  This system allows simple
1255232153Smm * read-ahead for consumers while preserving zero-copy operation
1256232153Smm * most of the time.
1257228753Smm *
1258232153Smm * The two key operations:
1259232153Smm *  * The read-ahead function returns a pointer to a block of data
1260232153Smm *    that satisfies a minimum request.
1261232153Smm *  * The consume function advances the file pointer.
1262232153Smm *
1263228753Smm * In the ideal case, filters generate blocks of data
1264228753Smm * and __archive_read_ahead() just returns pointers directly into
1265228753Smm * those blocks.  Then __archive_read_consume() just bumps those
1266228753Smm * pointers.  Only if your request would span blocks does the I/O
1267228753Smm * layer use a copy buffer to provide you with a contiguous block of
1268232153Smm * data.
1269228753Smm *
1270228753Smm * A couple of useful idioms:
1271228753Smm *  * "I just want some data."  Ask for 1 byte and pay attention to
1272228753Smm *    the "number of bytes available" from __archive_read_ahead().
1273232153Smm *    Consume whatever you actually use.
1274228753Smm *  * "I want to output a large block of data."  As above, ask for 1 byte,
1275232153Smm *    emit all that's available (up to whatever limit you have), consume
1276232153Smm *    it all, then repeat until you're done.  This effectively means that
1277232153Smm *    you're passing along the blocks that came from your provider.
1278228753Smm *  * "I want to peek ahead by a large amount."  Ask for 4k or so, then
1279228753Smm *    double and repeat until you get an error or have enough.  Note
1280228753Smm *    that the I/O layer will likely end up expanding its copy buffer
1281228753Smm *    to fit your request, so use this technique cautiously.  This
1282228753Smm *    technique is used, for example, by some of the format tasting
1283228753Smm *    code that has uncertain look-ahead needs.
1284228753Smm */
1285228753Smm
1286228753Smm/*
1287228753Smm * Looks ahead in the input stream:
1288228753Smm *  * If 'avail' pointer is provided, that returns number of bytes available
1289228753Smm *    in the current buffer, which may be much larger than requested.
1290228753Smm *  * If end-of-file, *avail gets set to zero.
1291228753Smm *  * If error, *avail gets error code.
1292232153Smm *  * If request can be met, returns pointer to data.
1293232153Smm *  * If minimum request cannot be met, returns NULL.
1294228753Smm *
1295228753Smm * Note: If you just want "some data", ask for 1 byte and pay attention
1296228753Smm * to *avail, which will have the actual amount available.  If you
1297228753Smm * know exactly how many bytes you need, just ask for that and treat
1298228753Smm * a NULL return as an error.
1299228753Smm *
1300228753Smm * Important:  This does NOT move the file pointer.  See
1301228753Smm * __archive_read_consume() below.
1302228753Smm */
1303228753Smmconst void *
1304228753Smm__archive_read_ahead(struct archive_read *a, size_t min, ssize_t *avail)
1305228753Smm{
1306228753Smm	return (__archive_read_filter_ahead(a->filter, min, avail));
1307228753Smm}
1308228753Smm
1309228753Smmconst void *
1310228753Smm__archive_read_filter_ahead(struct archive_read_filter *filter,
1311228753Smm    size_t min, ssize_t *avail)
1312228753Smm{
1313228753Smm	ssize_t bytes_read;
1314228753Smm	size_t tocopy;
1315228753Smm
1316228753Smm	if (filter->fatal) {
1317228753Smm		if (avail)
1318228753Smm			*avail = ARCHIVE_FATAL;
1319228753Smm		return (NULL);
1320228753Smm	}
1321228753Smm
1322228753Smm	/*
1323228753Smm	 * Keep pulling more data until we can satisfy the request.
1324228753Smm	 */
1325228753Smm	for (;;) {
1326228753Smm
1327228753Smm		/*
1328228753Smm		 * If we can satisfy from the copy buffer (and the
1329228753Smm		 * copy buffer isn't empty), we're done.  In particular,
1330228753Smm		 * note that min == 0 is a perfectly well-defined
1331228753Smm		 * request.
1332228753Smm		 */
1333228753Smm		if (filter->avail >= min && filter->avail > 0) {
1334228753Smm			if (avail != NULL)
1335228753Smm				*avail = filter->avail;
1336228753Smm			return (filter->next);
1337228753Smm		}
1338228753Smm
1339228753Smm		/*
1340228753Smm		 * We can satisfy directly from client buffer if everything
1341228753Smm		 * currently in the copy buffer is still in the client buffer.
1342228753Smm		 */
1343228753Smm		if (filter->client_total >= filter->client_avail + filter->avail
1344228753Smm		    && filter->client_avail + filter->avail >= min) {
1345228753Smm			/* "Roll back" to client buffer. */
1346228753Smm			filter->client_avail += filter->avail;
1347228753Smm			filter->client_next -= filter->avail;
1348228753Smm			/* Copy buffer is now empty. */
1349228753Smm			filter->avail = 0;
1350228753Smm			filter->next = filter->buffer;
1351228753Smm			/* Return data from client buffer. */
1352228753Smm			if (avail != NULL)
1353228753Smm				*avail = filter->client_avail;
1354228753Smm			return (filter->client_next);
1355228753Smm		}
1356228753Smm
1357228753Smm		/* Move data forward in copy buffer if necessary. */
1358228753Smm		if (filter->next > filter->buffer &&
1359228753Smm		    filter->next + min > filter->buffer + filter->buffer_size) {
1360228753Smm			if (filter->avail > 0)
1361248616Smm				memmove(filter->buffer, filter->next,
1362248616Smm				    filter->avail);
1363228753Smm			filter->next = filter->buffer;
1364228753Smm		}
1365228753Smm
1366228753Smm		/* If we've used up the client data, get more. */
1367228753Smm		if (filter->client_avail <= 0) {
1368228753Smm			if (filter->end_of_file) {
1369228753Smm				if (avail != NULL)
1370228753Smm					*avail = 0;
1371228753Smm				return (NULL);
1372228753Smm			}
1373228753Smm			bytes_read = (filter->read)(filter,
1374228753Smm			    &filter->client_buff);
1375228753Smm			if (bytes_read < 0) {		/* Read error. */
1376228753Smm				filter->client_total = filter->client_avail = 0;
1377248616Smm				filter->client_next =
1378248616Smm				    filter->client_buff = NULL;
1379228753Smm				filter->fatal = 1;
1380228753Smm				if (avail != NULL)
1381228753Smm					*avail = ARCHIVE_FATAL;
1382228753Smm				return (NULL);
1383228753Smm			}
1384248616Smm			if (bytes_read == 0) {
1385248616Smm				/* Check for another client object first */
1386248616Smm				if (filter->archive->client.cursor !=
1387248616Smm				      filter->archive->client.nodes - 1) {
1388248616Smm					if (client_switch_proxy(filter,
1389248616Smm					    filter->archive->client.cursor + 1)
1390248616Smm					    == ARCHIVE_OK)
1391248616Smm						continue;
1392248616Smm				}
1393248616Smm				/* Premature end-of-file. */
1394228753Smm				filter->client_total = filter->client_avail = 0;
1395248616Smm				filter->client_next =
1396248616Smm				    filter->client_buff = NULL;
1397228753Smm				filter->end_of_file = 1;
1398228753Smm				/* Return whatever we do have. */
1399228753Smm				if (avail != NULL)
1400228753Smm					*avail = filter->avail;
1401228753Smm				return (NULL);
1402228753Smm			}
1403228753Smm			filter->client_total = bytes_read;
1404228753Smm			filter->client_avail = filter->client_total;
1405228753Smm			filter->client_next = filter->client_buff;
1406248616Smm		} else {
1407228753Smm			/*
1408228753Smm			 * We can't satisfy the request from the copy
1409228753Smm			 * buffer or the existing client data, so we
1410228753Smm			 * need to copy more client data over to the
1411228753Smm			 * copy buffer.
1412228753Smm			 */
1413228753Smm
1414228753Smm			/* Ensure the buffer is big enough. */
1415228753Smm			if (min > filter->buffer_size) {
1416228753Smm				size_t s, t;
1417228753Smm				char *p;
1418228753Smm
1419228753Smm				/* Double the buffer; watch for overflow. */
1420228753Smm				s = t = filter->buffer_size;
1421228753Smm				if (s == 0)
1422228753Smm					s = min;
1423228753Smm				while (s < min) {
1424228753Smm					t *= 2;
1425228753Smm					if (t <= s) { /* Integer overflow! */
1426228753Smm						archive_set_error(
1427248616Smm						    &filter->archive->archive,
1428248616Smm						    ENOMEM,
1429248616Smm						    "Unable to allocate copy"
1430248616Smm						    " buffer");
1431228753Smm						filter->fatal = 1;
1432228753Smm						if (avail != NULL)
1433228753Smm							*avail = ARCHIVE_FATAL;
1434228753Smm						return (NULL);
1435228753Smm					}
1436228753Smm					s = t;
1437228753Smm				}
1438228753Smm				/* Now s >= min, so allocate a new buffer. */
1439228753Smm				p = (char *)malloc(s);
1440228753Smm				if (p == NULL) {
1441228753Smm					archive_set_error(
1442228753Smm						&filter->archive->archive,
1443228753Smm						ENOMEM,
1444228753Smm					    "Unable to allocate copy buffer");
1445228753Smm					filter->fatal = 1;
1446228753Smm					if (avail != NULL)
1447228753Smm						*avail = ARCHIVE_FATAL;
1448228753Smm					return (NULL);
1449228753Smm				}
1450228753Smm				/* Move data into newly-enlarged buffer. */
1451228753Smm				if (filter->avail > 0)
1452228753Smm					memmove(p, filter->next, filter->avail);
1453228753Smm				free(filter->buffer);
1454228753Smm				filter->next = filter->buffer = p;
1455228753Smm				filter->buffer_size = s;
1456228753Smm			}
1457228753Smm
1458228753Smm			/* We can add client data to copy buffer. */
1459228753Smm			/* First estimate: copy to fill rest of buffer. */
1460228753Smm			tocopy = (filter->buffer + filter->buffer_size)
1461228753Smm			    - (filter->next + filter->avail);
1462228753Smm			/* Don't waste time buffering more than we need to. */
1463228753Smm			if (tocopy + filter->avail > min)
1464228753Smm				tocopy = min - filter->avail;
1465228753Smm			/* Don't copy more than is available. */
1466228753Smm			if (tocopy > filter->client_avail)
1467228753Smm				tocopy = filter->client_avail;
1468228753Smm
1469248616Smm			memcpy(filter->next + filter->avail,
1470248616Smm			    filter->client_next, tocopy);
1471228753Smm			/* Remove this data from client buffer. */
1472228753Smm			filter->client_next += tocopy;
1473228753Smm			filter->client_avail -= tocopy;
1474228753Smm			/* add it to copy buffer. */
1475228753Smm			filter->avail += tocopy;
1476228753Smm		}
1477228753Smm	}
1478228753Smm}
1479228753Smm
1480228753Smm/*
1481232153Smm * Move the file pointer forward.
1482228753Smm */
1483232153Smmint64_t
1484232153Smm__archive_read_consume(struct archive_read *a, int64_t request)
1485228753Smm{
1486232153Smm	return (__archive_read_filter_consume(a->filter, request));
1487228753Smm}
1488228753Smm
1489232153Smmint64_t
1490228753Smm__archive_read_filter_consume(struct archive_read_filter * filter,
1491232153Smm    int64_t request)
1492228753Smm{
1493232153Smm	int64_t skipped;
1494228753Smm
1495282932Sdelphij	if (request < 0)
1496282932Sdelphij		return ARCHIVE_FATAL;
1497232153Smm	if (request == 0)
1498232153Smm		return 0;
1499232153Smm
1500232153Smm	skipped = advance_file_pointer(filter, request);
1501228753Smm	if (skipped == request)
1502228753Smm		return (skipped);
1503228753Smm	/* We hit EOF before we satisfied the skip request. */
1504232153Smm	if (skipped < 0)  /* Map error code to 0 for error message below. */
1505228753Smm		skipped = 0;
1506232153Smm	archive_set_error(&filter->archive->archive,
1507228753Smm	    ARCHIVE_ERRNO_MISC,
1508228753Smm	    "Truncated input file (needed %jd bytes, only %jd available)",
1509228753Smm	    (intmax_t)request, (intmax_t)skipped);
1510228753Smm	return (ARCHIVE_FATAL);
1511228753Smm}
1512228753Smm
1513232153Smm/*
1514232153Smm * Advance the file pointer by the amount requested.
1515232153Smm * Returns the amount actually advanced, which may be less than the
1516232153Smm * request if EOF is encountered first.
1517232153Smm * Returns a negative value if there's an I/O error.
1518232153Smm */
1519232153Smmstatic int64_t
1520232153Smmadvance_file_pointer(struct archive_read_filter *filter, int64_t request)
1521228753Smm{
1522228753Smm	int64_t bytes_skipped, total_bytes_skipped = 0;
1523232153Smm	ssize_t bytes_read;
1524228753Smm	size_t min;
1525228753Smm
1526228753Smm	if (filter->fatal)
1527228753Smm		return (-1);
1528232153Smm
1529232153Smm	/* Use up the copy buffer first. */
1530228753Smm	if (filter->avail > 0) {
1531238856Smm		min = (size_t)minimum(request, (int64_t)filter->avail);
1532232153Smm		filter->next += min;
1533232153Smm		filter->avail -= min;
1534232153Smm		request -= min;
1535232153Smm		filter->position += min;
1536232153Smm		total_bytes_skipped += min;
1537228753Smm	}
1538232153Smm
1539232153Smm	/* Then use up the client buffer. */
1540228753Smm	if (filter->client_avail > 0) {
1541238856Smm		min = (size_t)minimum(request, (int64_t)filter->client_avail);
1542232153Smm		filter->client_next += min;
1543232153Smm		filter->client_avail -= min;
1544232153Smm		request -= min;
1545232153Smm		filter->position += min;
1546232153Smm		total_bytes_skipped += min;
1547228753Smm	}
1548228753Smm	if (request == 0)
1549228753Smm		return (total_bytes_skipped);
1550232153Smm
1551232153Smm	/* If there's an optimized skip function, use it. */
1552228753Smm	if (filter->skip != NULL) {
1553228753Smm		bytes_skipped = (filter->skip)(filter, request);
1554228753Smm		if (bytes_skipped < 0) {	/* error */
1555228753Smm			filter->fatal = 1;
1556228753Smm			return (bytes_skipped);
1557228753Smm		}
1558232153Smm		filter->position += bytes_skipped;
1559228753Smm		total_bytes_skipped += bytes_skipped;
1560228753Smm		request -= bytes_skipped;
1561232153Smm		if (request == 0)
1562232153Smm			return (total_bytes_skipped);
1563228753Smm	}
1564232153Smm
1565232153Smm	/* Use ordinary reads as necessary to complete the request. */
1566232153Smm	for (;;) {
1567232153Smm		bytes_read = (filter->read)(filter, &filter->client_buff);
1568232153Smm		if (bytes_read < 0) {
1569232153Smm			filter->client_buff = NULL;
1570232153Smm			filter->fatal = 1;
1571228753Smm			return (bytes_read);
1572232153Smm		}
1573232153Smm
1574228753Smm		if (bytes_read == 0) {
1575248616Smm			if (filter->archive->client.cursor !=
1576248616Smm			      filter->archive->client.nodes - 1) {
1577248616Smm				if (client_switch_proxy(filter,
1578248616Smm				    filter->archive->client.cursor + 1)
1579248616Smm				    == ARCHIVE_OK)
1580248616Smm					continue;
1581248616Smm			}
1582232153Smm			filter->client_buff = NULL;
1583232153Smm			filter->end_of_file = 1;
1584228753Smm			return (total_bytes_skipped);
1585228753Smm		}
1586232153Smm
1587232153Smm		if (bytes_read >= request) {
1588232153Smm			filter->client_next =
1589232153Smm			    ((const char *)filter->client_buff) + request;
1590238856Smm			filter->client_avail = (size_t)(bytes_read - request);
1591232153Smm			filter->client_total = bytes_read;
1592232153Smm			total_bytes_skipped += request;
1593232153Smm			filter->position += request;
1594232153Smm			return (total_bytes_skipped);
1595232153Smm		}
1596232153Smm
1597232153Smm		filter->position += bytes_read;
1598228753Smm		total_bytes_skipped += bytes_read;
1599228753Smm		request -= bytes_read;
1600228753Smm	}
1601228753Smm}
1602232153Smm
1603232153Smm/**
1604232153Smm * Returns ARCHIVE_FAILED if seeking isn't supported.
1605232153Smm */
1606232153Smmint64_t
1607232153Smm__archive_read_seek(struct archive_read *a, int64_t offset, int whence)
1608232153Smm{
1609232153Smm	return __archive_read_filter_seek(a->filter, offset, whence);
1610232153Smm}
1611232153Smm
1612232153Smmint64_t
1613248616Smm__archive_read_filter_seek(struct archive_read_filter *filter, int64_t offset,
1614248616Smm    int whence)
1615232153Smm{
1616248616Smm	struct archive_read_client *client;
1617232153Smm	int64_t r;
1618248616Smm	unsigned int cursor;
1619232153Smm
1620232153Smm	if (filter->closed || filter->fatal)
1621232153Smm		return (ARCHIVE_FATAL);
1622232153Smm	if (filter->seek == NULL)
1623232153Smm		return (ARCHIVE_FAILED);
1624248616Smm
1625248616Smm	client = &(filter->archive->client);
1626248616Smm	switch (whence) {
1627248616Smm	case SEEK_CUR:
1628248616Smm		/* Adjust the offset and use SEEK_SET instead */
1629248616Smm		offset += filter->position;
1630248616Smm	case SEEK_SET:
1631248616Smm		cursor = 0;
1632248616Smm		while (1)
1633248616Smm		{
1634248616Smm			if (client->dataset[cursor].begin_position < 0 ||
1635248616Smm			    client->dataset[cursor].total_size < 0 ||
1636248616Smm			    client->dataset[cursor].begin_position +
1637248616Smm			      client->dataset[cursor].total_size - 1 > offset ||
1638248616Smm			    cursor + 1 >= client->nodes)
1639248616Smm				break;
1640248616Smm			r = client->dataset[cursor].begin_position +
1641248616Smm				client->dataset[cursor].total_size;
1642248616Smm			client->dataset[++cursor].begin_position = r;
1643248616Smm		}
1644248616Smm		while (1) {
1645248616Smm			r = client_switch_proxy(filter, cursor);
1646248616Smm			if (r != ARCHIVE_OK)
1647248616Smm				return r;
1648248616Smm			if ((r = client_seek_proxy(filter, 0, SEEK_END)) < 0)
1649248616Smm				return r;
1650248616Smm			client->dataset[cursor].total_size = r;
1651248616Smm			if (client->dataset[cursor].begin_position +
1652248616Smm			    client->dataset[cursor].total_size - 1 > offset ||
1653248616Smm			    cursor + 1 >= client->nodes)
1654248616Smm				break;
1655248616Smm			r = client->dataset[cursor].begin_position +
1656248616Smm				client->dataset[cursor].total_size;
1657248616Smm			client->dataset[++cursor].begin_position = r;
1658248616Smm		}
1659248616Smm		offset -= client->dataset[cursor].begin_position;
1660299529Smm		if (offset < 0
1661299529Smm		    || offset > client->dataset[cursor].total_size)
1662299529Smm			return ARCHIVE_FATAL;
1663248616Smm		if ((r = client_seek_proxy(filter, offset, SEEK_SET)) < 0)
1664248616Smm			return r;
1665248616Smm		break;
1666248616Smm
1667248616Smm	case SEEK_END:
1668248616Smm		cursor = 0;
1669248616Smm		while (1) {
1670248616Smm			if (client->dataset[cursor].begin_position < 0 ||
1671248616Smm			    client->dataset[cursor].total_size < 0 ||
1672248616Smm			    cursor + 1 >= client->nodes)
1673248616Smm				break;
1674248616Smm			r = client->dataset[cursor].begin_position +
1675248616Smm				client->dataset[cursor].total_size;
1676248616Smm			client->dataset[++cursor].begin_position = r;
1677248616Smm		}
1678248616Smm		while (1) {
1679248616Smm			r = client_switch_proxy(filter, cursor);
1680248616Smm			if (r != ARCHIVE_OK)
1681248616Smm				return r;
1682248616Smm			if ((r = client_seek_proxy(filter, 0, SEEK_END)) < 0)
1683248616Smm				return r;
1684248616Smm			client->dataset[cursor].total_size = r;
1685248616Smm			r = client->dataset[cursor].begin_position +
1686248616Smm				client->dataset[cursor].total_size;
1687248616Smm			if (cursor + 1 >= client->nodes)
1688248616Smm				break;
1689248616Smm			client->dataset[++cursor].begin_position = r;
1690248616Smm		}
1691248616Smm		while (1) {
1692248616Smm			if (r + offset >=
1693248616Smm			    client->dataset[cursor].begin_position)
1694248616Smm				break;
1695248616Smm			offset += client->dataset[cursor].total_size;
1696248616Smm			if (cursor == 0)
1697248616Smm				break;
1698248616Smm			cursor--;
1699248616Smm			r = client->dataset[cursor].begin_position +
1700248616Smm				client->dataset[cursor].total_size;
1701248616Smm		}
1702248616Smm		offset = (r + offset) - client->dataset[cursor].begin_position;
1703248616Smm		if ((r = client_switch_proxy(filter, cursor)) != ARCHIVE_OK)
1704248616Smm			return r;
1705248616Smm		r = client_seek_proxy(filter, offset, SEEK_SET);
1706248616Smm		if (r < ARCHIVE_OK)
1707248616Smm			return r;
1708248616Smm		break;
1709248616Smm
1710248616Smm	default:
1711248616Smm		return (ARCHIVE_FATAL);
1712248616Smm	}
1713248616Smm	r += client->dataset[cursor].begin_position;
1714248616Smm
1715232153Smm	if (r >= 0) {
1716232153Smm		/*
1717232153Smm		 * Ouch.  Clearing the buffer like this hurts, especially
1718232153Smm		 * at bid time.  A lot of our efficiency at bid time comes
1719232153Smm		 * from having bidders reuse the data we've already read.
1720232153Smm		 *
1721232153Smm		 * TODO: If the seek request is in data we already
1722232153Smm		 * have, then don't call the seek callback.
1723232153Smm		 *
1724232153Smm		 * TODO: Zip seeks to end-of-file at bid time.  If
1725232153Smm		 * other formats also start doing this, we may need to
1726232153Smm		 * find a way for clients to fudge the seek offset to
1727232153Smm		 * a block boundary.
1728232153Smm		 *
1729232153Smm		 * Hmmm... If whence was SEEK_END, we know the file
1730232153Smm		 * size is (r - offset).  Can we use that to simplify
1731232153Smm		 * the TODO items above?
1732232153Smm		 */
1733232153Smm		filter->avail = filter->client_avail = 0;
1734232153Smm		filter->next = filter->buffer;
1735232153Smm		filter->position = r;
1736232153Smm		filter->end_of_file = 0;
1737232153Smm	}
1738232153Smm	return r;
1739232153Smm}
1740