archive_read.c revision 328827
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 328827 2018-02-03 02:17:04Z 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
123328827Smmarchive_read_extract_set_skip_file(struct archive *_a, la_int64_t d,
124328827Smm    la_int64_t i)
125228753Smm{
126228753Smm	struct archive_read *a = (struct archive_read *)_a;
127232153Smm
128232153Smm	if (ARCHIVE_OK != __archive_check_magic(_a, ARCHIVE_READ_MAGIC,
129232153Smm		ARCHIVE_STATE_ANY, "archive_read_extract_set_skip_file"))
130232153Smm		return;
131232153Smm	a->skip_file_set = 1;
132228753Smm	a->skip_file_dev = d;
133228753Smm	a->skip_file_ino = i;
134228753Smm}
135228753Smm
136228753Smm/*
137232153Smm * Open the archive
138228753Smm */
139228753Smmint
140232153Smmarchive_read_open(struct archive *a, void *client_data,
141232153Smm    archive_open_callback *client_opener, archive_read_callback *client_reader,
142232153Smm    archive_close_callback *client_closer)
143228753Smm{
144232153Smm	/* Old archive_read_open() is just a thin shell around
145232153Smm	 * archive_read_open1. */
146232153Smm	archive_read_set_open_callback(a, client_opener);
147232153Smm	archive_read_set_read_callback(a, client_reader);
148232153Smm	archive_read_set_close_callback(a, client_closer);
149232153Smm	archive_read_set_callback_data(a, client_data);
150232153Smm	return archive_read_open1(a);
151228753Smm}
152228753Smm
153228753Smm
154228753Smmint
155232153Smmarchive_read_open2(struct archive *a, void *client_data,
156232153Smm    archive_open_callback *client_opener,
157232153Smm    archive_read_callback *client_reader,
158232153Smm    archive_skip_callback *client_skipper,
159228753Smm    archive_close_callback *client_closer)
160228753Smm{
161232153Smm	/* Old archive_read_open2() is just a thin shell around
162232153Smm	 * archive_read_open1. */
163232153Smm	archive_read_set_callback_data(a, client_data);
164232153Smm	archive_read_set_open_callback(a, client_opener);
165232153Smm	archive_read_set_read_callback(a, client_reader);
166232153Smm	archive_read_set_skip_callback(a, client_skipper);
167232153Smm	archive_read_set_close_callback(a, client_closer);
168232153Smm	return archive_read_open1(a);
169228753Smm}
170228753Smm
171228753Smmstatic ssize_t
172228753Smmclient_read_proxy(struct archive_read_filter *self, const void **buff)
173228753Smm{
174228753Smm	ssize_t r;
175228753Smm	r = (self->archive->client.reader)(&self->archive->archive,
176228753Smm	    self->data, buff);
177228753Smm	return (r);
178228753Smm}
179228753Smm
180228753Smmstatic int64_t
181228753Smmclient_skip_proxy(struct archive_read_filter *self, int64_t request)
182228753Smm{
183232153Smm	if (request < 0)
184232153Smm		__archive_errx(1, "Negative skip requested.");
185232153Smm	if (request == 0)
186232153Smm		return 0;
187228753Smm
188232153Smm	if (self->archive->client.skipper != NULL) {
189232153Smm		/* Seek requests over 1GiB are broken down into
190232153Smm		 * multiple seeks.  This avoids overflows when the
191232153Smm		 * requests get passed through 32-bit arguments. */
192232153Smm		int64_t skip_limit = (int64_t)1 << 30;
193232153Smm		int64_t total = 0;
194232153Smm		for (;;) {
195232153Smm			int64_t get, ask = request;
196232153Smm			if (ask > skip_limit)
197232153Smm				ask = skip_limit;
198248616Smm			get = (self->archive->client.skipper)
199248616Smm				(&self->archive->archive, self->data, ask);
200299529Smm			total += get;
201299529Smm			if (get == 0 || get == request)
202232153Smm				return (total);
203299529Smm			if (get > request)
204299529Smm				return ARCHIVE_FATAL;
205232153Smm			request -= get;
206232153Smm		}
207232153Smm	} else if (self->archive->client.seeker != NULL
208232153Smm		&& request > 64 * 1024) {
209232153Smm		/* If the client provided a seeker but not a skipper,
210232153Smm		 * we can use the seeker to skip forward.
211232153Smm		 *
212232153Smm		 * Note: This isn't always a good idea.  The client
213232153Smm		 * skipper is allowed to skip by less than requested
214232153Smm		 * if it needs to maintain block alignment.  The
215232153Smm		 * seeker is not allowed to play such games, so using
216232153Smm		 * the seeker here may be a performance loss compared
217232153Smm		 * to just reading and discarding.  That's why we
218232153Smm		 * only do this for skips of over 64k.
219232153Smm		 */
220232153Smm		int64_t before = self->position;
221248616Smm		int64_t after = (self->archive->client.seeker)
222248616Smm		    (&self->archive->archive, self->data, request, SEEK_CUR);
223232153Smm		if (after != before + request)
224232153Smm			return ARCHIVE_FATAL;
225232153Smm		return after - before;
226228753Smm	}
227232153Smm	return 0;
228228753Smm}
229228753Smm
230232153Smmstatic int64_t
231232153Smmclient_seek_proxy(struct archive_read_filter *self, int64_t offset, int whence)
232232153Smm{
233232153Smm	/* DO NOT use the skipper here!  If we transparently handled
234232153Smm	 * forward seek here by using the skipper, that will break
235232153Smm	 * other libarchive code that assumes a successful forward
236232153Smm	 * seek means it can also seek backwards.
237232153Smm	 */
238299529Smm	if (self->archive->client.seeker == NULL) {
239299529Smm		archive_set_error(&self->archive->archive, ARCHIVE_ERRNO_MISC,
240299529Smm		    "Current client reader does not support seeking a device");
241232153Smm		return (ARCHIVE_FAILED);
242299529Smm	}
243232153Smm	return (self->archive->client.seeker)(&self->archive->archive,
244232153Smm	    self->data, offset, whence);
245232153Smm}
246232153Smm
247228753Smmstatic int
248228753Smmclient_close_proxy(struct archive_read_filter *self)
249228753Smm{
250248616Smm	int r = ARCHIVE_OK, r2;
251248616Smm	unsigned int i;
252228753Smm
253248616Smm	if (self->archive->client.closer == NULL)
254248616Smm		return (r);
255248616Smm	for (i = 0; i < self->archive->client.nodes; i++)
256248616Smm	{
257248616Smm		r2 = (self->archive->client.closer)
258248616Smm			((struct archive *)self->archive,
259248616Smm				self->archive->client.dataset[i].data);
260248616Smm		if (r > r2)
261248616Smm			r = r2;
262248616Smm	}
263228753Smm	return (r);
264228753Smm}
265228753Smm
266248616Smmstatic int
267248616Smmclient_open_proxy(struct archive_read_filter *self)
268248616Smm{
269248616Smm  int r = ARCHIVE_OK;
270248616Smm	if (self->archive->client.opener != NULL)
271248616Smm		r = (self->archive->client.opener)(
272248616Smm		    (struct archive *)self->archive, self->data);
273248616Smm	return (r);
274248616Smm}
275248616Smm
276248616Smmstatic int
277248616Smmclient_switch_proxy(struct archive_read_filter *self, unsigned int iindex)
278248616Smm{
279248616Smm  int r1 = ARCHIVE_OK, r2 = ARCHIVE_OK;
280248616Smm	void *data2 = NULL;
281248616Smm
282248616Smm	/* Don't do anything if already in the specified data node */
283248616Smm	if (self->archive->client.cursor == iindex)
284248616Smm		return (ARCHIVE_OK);
285248616Smm
286248616Smm	self->archive->client.cursor = iindex;
287248616Smm	data2 = self->archive->client.dataset[self->archive->client.cursor].data;
288248616Smm	if (self->archive->client.switcher != NULL)
289248616Smm	{
290248616Smm		r1 = r2 = (self->archive->client.switcher)
291248616Smm			((struct archive *)self->archive, self->data, data2);
292248616Smm		self->data = data2;
293248616Smm	}
294248616Smm	else
295248616Smm	{
296248616Smm		/* Attempt to call close and open instead */
297248616Smm		if (self->archive->client.closer != NULL)
298248616Smm			r1 = (self->archive->client.closer)
299248616Smm				((struct archive *)self->archive, self->data);
300248616Smm		self->data = data2;
301248616Smm		if (self->archive->client.opener != NULL)
302248616Smm			r2 = (self->archive->client.opener)
303248616Smm				((struct archive *)self->archive, self->data);
304248616Smm	}
305248616Smm	return (r1 < r2) ? r1 : r2;
306248616Smm}
307248616Smm
308232153Smmint
309232153Smmarchive_read_set_open_callback(struct archive *_a,
310232153Smm    archive_open_callback *client_opener)
311232153Smm{
312232153Smm	struct archive_read *a = (struct archive_read *)_a;
313232153Smm	archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
314232153Smm	    "archive_read_set_open_callback");
315232153Smm	a->client.opener = client_opener;
316232153Smm	return ARCHIVE_OK;
317232153Smm}
318228753Smm
319228753Smmint
320232153Smmarchive_read_set_read_callback(struct archive *_a,
321232153Smm    archive_read_callback *client_reader)
322232153Smm{
323232153Smm	struct archive_read *a = (struct archive_read *)_a;
324232153Smm	archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
325232153Smm	    "archive_read_set_read_callback");
326232153Smm	a->client.reader = client_reader;
327232153Smm	return ARCHIVE_OK;
328232153Smm}
329232153Smm
330232153Smmint
331232153Smmarchive_read_set_skip_callback(struct archive *_a,
332232153Smm    archive_skip_callback *client_skipper)
333232153Smm{
334232153Smm	struct archive_read *a = (struct archive_read *)_a;
335232153Smm	archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
336232153Smm	    "archive_read_set_skip_callback");
337232153Smm	a->client.skipper = client_skipper;
338232153Smm	return ARCHIVE_OK;
339232153Smm}
340232153Smm
341232153Smmint
342232153Smmarchive_read_set_seek_callback(struct archive *_a,
343232153Smm    archive_seek_callback *client_seeker)
344232153Smm{
345232153Smm	struct archive_read *a = (struct archive_read *)_a;
346232153Smm	archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
347232153Smm	    "archive_read_set_seek_callback");
348232153Smm	a->client.seeker = client_seeker;
349232153Smm	return ARCHIVE_OK;
350232153Smm}
351232153Smm
352232153Smmint
353232153Smmarchive_read_set_close_callback(struct archive *_a,
354228753Smm    archive_close_callback *client_closer)
355228753Smm{
356228753Smm	struct archive_read *a = (struct archive_read *)_a;
357232153Smm	archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
358232153Smm	    "archive_read_set_close_callback");
359232153Smm	a->client.closer = client_closer;
360232153Smm	return ARCHIVE_OK;
361232153Smm}
362232153Smm
363232153Smmint
364248616Smmarchive_read_set_switch_callback(struct archive *_a,
365248616Smm    archive_switch_callback *client_switcher)
366248616Smm{
367248616Smm	struct archive_read *a = (struct archive_read *)_a;
368248616Smm	archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
369248616Smm	    "archive_read_set_switch_callback");
370248616Smm	a->client.switcher = client_switcher;
371248616Smm	return ARCHIVE_OK;
372248616Smm}
373248616Smm
374248616Smmint
375232153Smmarchive_read_set_callback_data(struct archive *_a, void *client_data)
376232153Smm{
377248616Smm	return archive_read_set_callback_data2(_a, client_data, 0);
378248616Smm}
379248616Smm
380248616Smmint
381248616Smmarchive_read_set_callback_data2(struct archive *_a, void *client_data,
382248616Smm    unsigned int iindex)
383248616Smm{
384232153Smm	struct archive_read *a = (struct archive_read *)_a;
385232153Smm	archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
386248616Smm	    "archive_read_set_callback_data2");
387248616Smm
388248616Smm	if (a->client.nodes == 0)
389248616Smm	{
390248616Smm		a->client.dataset = (struct archive_read_data_node *)
391248616Smm		    calloc(1, sizeof(*a->client.dataset));
392248616Smm		if (a->client.dataset == NULL)
393248616Smm		{
394248616Smm			archive_set_error(&a->archive, ENOMEM,
395248616Smm				"No memory.");
396248616Smm			return ARCHIVE_FATAL;
397248616Smm		}
398248616Smm		a->client.nodes = 1;
399248616Smm	}
400248616Smm
401248616Smm	if (iindex > a->client.nodes - 1)
402248616Smm	{
403248616Smm		archive_set_error(&a->archive, EINVAL,
404248616Smm			"Invalid index specified.");
405248616Smm		return ARCHIVE_FATAL;
406248616Smm	}
407248616Smm	a->client.dataset[iindex].data = client_data;
408248616Smm	a->client.dataset[iindex].begin_position = -1;
409248616Smm	a->client.dataset[iindex].total_size = -1;
410232153Smm	return ARCHIVE_OK;
411232153Smm}
412232153Smm
413232153Smmint
414248616Smmarchive_read_add_callback_data(struct archive *_a, void *client_data,
415248616Smm    unsigned int iindex)
416248616Smm{
417248616Smm	struct archive_read *a = (struct archive_read *)_a;
418248616Smm	void *p;
419248616Smm	unsigned int i;
420248616Smm
421248616Smm	archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
422248616Smm	    "archive_read_add_callback_data");
423248616Smm	if (iindex > a->client.nodes) {
424248616Smm		archive_set_error(&a->archive, EINVAL,
425248616Smm			"Invalid index specified.");
426248616Smm		return ARCHIVE_FATAL;
427248616Smm	}
428248616Smm	p = realloc(a->client.dataset, sizeof(*a->client.dataset)
429248616Smm		* (++(a->client.nodes)));
430248616Smm	if (p == NULL) {
431248616Smm		archive_set_error(&a->archive, ENOMEM,
432248616Smm			"No memory.");
433248616Smm		return ARCHIVE_FATAL;
434248616Smm	}
435248616Smm	a->client.dataset = (struct archive_read_data_node *)p;
436248616Smm	for (i = a->client.nodes - 1; i > iindex && i > 0; i--) {
437248616Smm		a->client.dataset[i].data = a->client.dataset[i-1].data;
438248616Smm		a->client.dataset[i].begin_position = -1;
439248616Smm		a->client.dataset[i].total_size = -1;
440248616Smm	}
441248616Smm	a->client.dataset[iindex].data = client_data;
442248616Smm	a->client.dataset[iindex].begin_position = -1;
443248616Smm	a->client.dataset[iindex].total_size = -1;
444248616Smm	return ARCHIVE_OK;
445248616Smm}
446248616Smm
447248616Smmint
448248616Smmarchive_read_append_callback_data(struct archive *_a, void *client_data)
449248616Smm{
450248616Smm	struct archive_read *a = (struct archive_read *)_a;
451248616Smm	return archive_read_add_callback_data(_a, client_data, a->client.nodes);
452248616Smm}
453248616Smm
454248616Smmint
455248616Smmarchive_read_prepend_callback_data(struct archive *_a, void *client_data)
456248616Smm{
457248616Smm	return archive_read_add_callback_data(_a, client_data, 0);
458248616Smm}
459248616Smm
460248616Smmint
461232153Smmarchive_read_open1(struct archive *_a)
462232153Smm{
463232153Smm	struct archive_read *a = (struct archive_read *)_a;
464248616Smm	struct archive_read_filter *filter, *tmp;
465299529Smm	int slot, e = ARCHIVE_OK;
466248616Smm	unsigned int i;
467228753Smm
468232153Smm	archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
469228753Smm	    "archive_read_open");
470228753Smm	archive_clear_error(&a->archive);
471228753Smm
472232153Smm	if (a->client.reader == NULL) {
473232153Smm		archive_set_error(&a->archive, EINVAL,
474228753Smm		    "No reader function provided to archive_read_open");
475232153Smm		a->archive.state = ARCHIVE_STATE_FATAL;
476232153Smm		return (ARCHIVE_FATAL);
477232153Smm	}
478228753Smm
479228753Smm	/* Open data source. */
480232153Smm	if (a->client.opener != NULL) {
481248616Smm		e = (a->client.opener)(&a->archive, a->client.dataset[0].data);
482228753Smm		if (e != 0) {
483228753Smm			/* If the open failed, call the closer to clean up. */
484248616Smm			if (a->client.closer) {
485248616Smm				for (i = 0; i < a->client.nodes; i++)
486248616Smm					(a->client.closer)(&a->archive,
487248616Smm					    a->client.dataset[i].data);
488248616Smm			}
489228753Smm			return (e);
490228753Smm		}
491228753Smm	}
492228753Smm
493228753Smm	filter = calloc(1, sizeof(*filter));
494228753Smm	if (filter == NULL)
495228753Smm		return (ARCHIVE_FATAL);
496228753Smm	filter->bidder = NULL;
497228753Smm	filter->upstream = NULL;
498228753Smm	filter->archive = a;
499248616Smm	filter->data = a->client.dataset[0].data;
500248616Smm	filter->open = client_open_proxy;
501228753Smm	filter->read = client_read_proxy;
502228753Smm	filter->skip = client_skip_proxy;
503232153Smm	filter->seek = client_seek_proxy;
504228753Smm	filter->close = client_close_proxy;
505248616Smm	filter->sswitch = client_switch_proxy;
506228753Smm	filter->name = "none";
507248616Smm	filter->code = ARCHIVE_FILTER_NONE;
508228753Smm
509248616Smm	a->client.dataset[0].begin_position = 0;
510248616Smm	if (!a->filter || !a->bypass_filter_bidding)
511248616Smm	{
512248616Smm		a->filter = filter;
513248616Smm		/* Build out the input pipeline. */
514248616Smm		e = choose_filters(a);
515248616Smm		if (e < ARCHIVE_WARN) {
516248616Smm			a->archive.state = ARCHIVE_STATE_FATAL;
517248616Smm			return (ARCHIVE_FATAL);
518248616Smm		}
519232153Smm	}
520248616Smm	else
521248616Smm	{
522248616Smm		/* Need to add "NONE" type filter at the end of the filter chain */
523248616Smm		tmp = a->filter;
524248616Smm		while (tmp->upstream)
525248616Smm			tmp = tmp->upstream;
526248616Smm		tmp->upstream = filter;
527248616Smm	}
528228753Smm
529248616Smm	if (!a->format)
530248616Smm	{
531248616Smm		slot = choose_format(a);
532248616Smm		if (slot < 0) {
533311041Smm			close_filters(a);
534248616Smm			a->archive.state = ARCHIVE_STATE_FATAL;
535248616Smm			return (ARCHIVE_FATAL);
536248616Smm		}
537248616Smm		a->format = &(a->formats[slot]);
538232153Smm	}
539232153Smm
540232153Smm	a->archive.state = ARCHIVE_STATE_HEADER;
541248616Smm
542248616Smm	/* Ensure libarchive starts from the first node in a multivolume set */
543248616Smm	client_switch_proxy(a->filter, 0);
544228753Smm	return (e);
545228753Smm}
546228753Smm
547228753Smm/*
548228753Smm * Allow each registered stream transform to bid on whether
549228753Smm * it wants to handle this stream.  Repeat until we've finished
550228753Smm * building the pipeline.
551228753Smm */
552299529Smm
553299529Smm/* We won't build a filter pipeline with more stages than this. */
554299529Smm#define MAX_NUMBER_FILTERS 25
555299529Smm
556228753Smmstatic int
557232153Smmchoose_filters(struct archive_read *a)
558228753Smm{
559299529Smm	int number_bidders, i, bid, best_bid, number_filters;
560228753Smm	struct archive_read_filter_bidder *bidder, *best_bidder;
561228753Smm	struct archive_read_filter *filter;
562228753Smm	ssize_t avail;
563228753Smm	int r;
564228753Smm
565299529Smm	for (number_filters = 0; number_filters < MAX_NUMBER_FILTERS; ++number_filters) {
566228753Smm		number_bidders = sizeof(a->bidders) / sizeof(a->bidders[0]);
567228753Smm
568228753Smm		best_bid = 0;
569228753Smm		best_bidder = NULL;
570228753Smm
571228753Smm		bidder = a->bidders;
572228753Smm		for (i = 0; i < number_bidders; i++, bidder++) {
573228753Smm			if (bidder->bid != NULL) {
574228753Smm				bid = (bidder->bid)(bidder, a->filter);
575228753Smm				if (bid > best_bid) {
576228753Smm					best_bid = bid;
577228753Smm					best_bidder = bidder;
578228753Smm				}
579228753Smm			}
580228753Smm		}
581228753Smm
582228753Smm		/* If no bidder, we're done. */
583228753Smm		if (best_bidder == NULL) {
584232153Smm			/* Verify the filter by asking it for some data. */
585228753Smm			__archive_read_filter_ahead(a->filter, 1, &avail);
586228753Smm			if (avail < 0) {
587248616Smm				__archive_read_free_filters(a);
588228753Smm				return (ARCHIVE_FATAL);
589228753Smm			}
590228753Smm			a->archive.compression_name = a->filter->name;
591228753Smm			a->archive.compression_code = a->filter->code;
592228753Smm			return (ARCHIVE_OK);
593228753Smm		}
594228753Smm
595228753Smm		filter
596228753Smm		    = (struct archive_read_filter *)calloc(1, sizeof(*filter));
597228753Smm		if (filter == NULL)
598228753Smm			return (ARCHIVE_FATAL);
599228753Smm		filter->bidder = best_bidder;
600228753Smm		filter->archive = a;
601228753Smm		filter->upstream = a->filter;
602228753Smm		a->filter = filter;
603228753Smm		r = (best_bidder->init)(a->filter);
604228753Smm		if (r != ARCHIVE_OK) {
605248616Smm			__archive_read_free_filters(a);
606232153Smm			return (ARCHIVE_FATAL);
607228753Smm		}
608228753Smm	}
609295914Sdelphij	archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
610295914Sdelphij	    "Input requires too many filters for decoding");
611295914Sdelphij	return (ARCHIVE_FATAL);
612228753Smm}
613228753Smm
614228753Smm/*
615228753Smm * Read header of next entry.
616228753Smm */
617232153Smmstatic int
618232153Smm_archive_read_next_header2(struct archive *_a, struct archive_entry *entry)
619228753Smm{
620228753Smm	struct archive_read *a = (struct archive_read *)_a;
621232153Smm	int r1 = ARCHIVE_OK, r2;
622228753Smm
623232153Smm	archive_check_magic(_a, ARCHIVE_READ_MAGIC,
624228753Smm	    ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
625228753Smm	    "archive_read_next_header");
626228753Smm
627228753Smm	archive_entry_clear(entry);
628228753Smm	archive_clear_error(&a->archive);
629228753Smm
630228753Smm	/*
631228753Smm	 * If client didn't consume entire data, skip any remainder
632228753Smm	 * (This is especially important for GNU incremental directories.)
633228753Smm	 */
634228753Smm	if (a->archive.state == ARCHIVE_STATE_DATA) {
635232153Smm		r1 = archive_read_data_skip(&a->archive);
636232153Smm		if (r1 == ARCHIVE_EOF)
637232153Smm			archive_set_error(&a->archive, EIO,
638232153Smm			    "Premature end-of-file.");
639232153Smm		if (r1 == ARCHIVE_EOF || r1 == ARCHIVE_FATAL) {
640228753Smm			a->archive.state = ARCHIVE_STATE_FATAL;
641228753Smm			return (ARCHIVE_FATAL);
642228753Smm		}
643228753Smm	}
644228753Smm
645232153Smm	/* Record start-of-header offset in uncompressed stream. */
646232153Smm	a->header_position = a->filter->position;
647228753Smm
648232153Smm	++_a->file_count;
649232153Smm	r2 = (a->format->read_header)(a, entry);
650228753Smm
651228753Smm	/*
652228753Smm	 * EOF and FATAL are persistent at this layer.  By
653228753Smm	 * modifying the state, we guarantee that future calls to
654228753Smm	 * read a header or read data will fail.
655228753Smm	 */
656232153Smm	switch (r2) {
657228753Smm	case ARCHIVE_EOF:
658228753Smm		a->archive.state = ARCHIVE_STATE_EOF;
659232153Smm		--_a->file_count;/* Revert a file counter. */
660228753Smm		break;
661228753Smm	case ARCHIVE_OK:
662228753Smm		a->archive.state = ARCHIVE_STATE_DATA;
663228753Smm		break;
664228753Smm	case ARCHIVE_WARN:
665228753Smm		a->archive.state = ARCHIVE_STATE_DATA;
666228753Smm		break;
667228753Smm	case ARCHIVE_RETRY:
668228753Smm		break;
669228753Smm	case ARCHIVE_FATAL:
670228753Smm		a->archive.state = ARCHIVE_STATE_FATAL;
671228753Smm		break;
672228753Smm	}
673228753Smm
674299529Smm	__archive_reset_read_data(&a->archive);
675299529Smm
676248616Smm	a->data_start_node = a->client.cursor;
677232153Smm	/* EOF always wins; otherwise return the worst error. */
678232153Smm	return (r2 < r1 || r2 == ARCHIVE_EOF) ? r2 : r1;
679228753Smm}
680228753Smm
681299529Smmstatic int
682232153Smm_archive_read_next_header(struct archive *_a, struct archive_entry **entryp)
683228753Smm{
684228753Smm	int ret;
685228753Smm	struct archive_read *a = (struct archive_read *)_a;
686228753Smm	*entryp = NULL;
687232153Smm	ret = _archive_read_next_header2(_a, a->entry);
688228753Smm	*entryp = a->entry;
689228753Smm	return ret;
690228753Smm}
691228753Smm
692228753Smm/*
693228753Smm * Allow each registered format to bid on whether it wants to handle
694228753Smm * the next entry.  Return index of winning bidder.
695228753Smm */
696228753Smmstatic int
697228753Smmchoose_format(struct archive_read *a)
698228753Smm{
699228753Smm	int slots;
700228753Smm	int i;
701228753Smm	int bid, best_bid;
702228753Smm	int best_bid_slot;
703228753Smm
704228753Smm	slots = sizeof(a->formats) / sizeof(a->formats[0]);
705228753Smm	best_bid = -1;
706228753Smm	best_bid_slot = -1;
707228753Smm
708232153Smm	/* Set up a->format for convenience of bidders. */
709228753Smm	a->format = &(a->formats[0]);
710228753Smm	for (i = 0; i < slots; i++, a->format++) {
711228753Smm		if (a->format->bid) {
712232153Smm			bid = (a->format->bid)(a, best_bid);
713228753Smm			if (bid == ARCHIVE_FATAL)
714228753Smm				return (ARCHIVE_FATAL);
715232153Smm			if (a->filter->position != 0)
716232153Smm				__archive_read_seek(a, 0, SEEK_SET);
717228753Smm			if ((bid > best_bid) || (best_bid_slot < 0)) {
718228753Smm				best_bid = bid;
719228753Smm				best_bid_slot = i;
720228753Smm			}
721228753Smm		}
722228753Smm	}
723228753Smm
724228753Smm	/*
725228753Smm	 * There were no bidders; this is a serious programmer error
726228753Smm	 * and demands a quick and definitive abort.
727228753Smm	 */
728232153Smm	if (best_bid_slot < 0) {
729232153Smm		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
730232153Smm		    "No formats registered");
731232153Smm		return (ARCHIVE_FATAL);
732232153Smm	}
733228753Smm
734228753Smm	/*
735228753Smm	 * There were bidders, but no non-zero bids; this means we
736228753Smm	 * can't support this stream.
737228753Smm	 */
738228753Smm	if (best_bid < 1) {
739228753Smm		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
740228753Smm		    "Unrecognized archive format");
741228753Smm		return (ARCHIVE_FATAL);
742228753Smm	}
743228753Smm
744228753Smm	return (best_bid_slot);
745228753Smm}
746228753Smm
747228753Smm/*
748228753Smm * Return the file offset (within the uncompressed data stream) where
749228753Smm * the last header started.
750228753Smm */
751328827Smmla_int64_t
752228753Smmarchive_read_header_position(struct archive *_a)
753228753Smm{
754228753Smm	struct archive_read *a = (struct archive_read *)_a;
755232153Smm	archive_check_magic(_a, ARCHIVE_READ_MAGIC,
756228753Smm	    ARCHIVE_STATE_ANY, "archive_read_header_position");
757228753Smm	return (a->header_position);
758228753Smm}
759228753Smm
760228753Smm/*
761299529Smm * Returns 1 if the archive contains at least one encrypted entry.
762299529Smm * If the archive format not support encryption at all
763299529Smm * ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED is returned.
764299529Smm * If for any other reason (e.g. not enough data read so far)
765299529Smm * we cannot say whether there are encrypted entries, then
766299529Smm * ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW is returned.
767299529Smm * In general, this function will return values below zero when the
768311041Smm * reader is uncertain or totally incapable of encryption support.
769299529Smm * When this function returns 0 you can be sure that the reader
770299529Smm * supports encryption detection but no encrypted entries have
771299529Smm * been found yet.
772299529Smm *
773299529Smm * NOTE: If the metadata/header of an archive is also encrypted, you
774299529Smm * cannot rely on the number of encrypted entries. That is why this
775299529Smm * function does not return the number of encrypted entries but#
776299529Smm * just shows that there are some.
777299529Smm */
778299529Smmint
779299529Smmarchive_read_has_encrypted_entries(struct archive *_a)
780299529Smm{
781299529Smm	struct archive_read *a = (struct archive_read *)_a;
782299529Smm	int format_supports_encryption = archive_read_format_capabilities(_a)
783299529Smm			& (ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_DATA | ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_METADATA);
784299529Smm
785299529Smm	if (!_a || !format_supports_encryption) {
786299529Smm		/* Format in general doesn't support encryption */
787299529Smm		return ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED;
788299529Smm	}
789299529Smm
790299529Smm	/* A reader potentially has read enough data now. */
791299529Smm	if (a->format && a->format->has_encrypted_entries) {
792299529Smm		return (a->format->has_encrypted_entries)(a);
793299529Smm	}
794299529Smm
795299529Smm	/* For any other reason we cannot say how many entries are there. */
796299529Smm	return ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW;
797299529Smm}
798299529Smm
799299529Smm/*
800299529Smm * Returns a bitmask of capabilities that are supported by the archive format reader.
801299529Smm * If the reader has no special capabilities, ARCHIVE_READ_FORMAT_CAPS_NONE is returned.
802299529Smm */
803299529Smmint
804299529Smmarchive_read_format_capabilities(struct archive *_a)
805299529Smm{
806299529Smm	struct archive_read *a = (struct archive_read *)_a;
807299529Smm	if (a && a->format && a->format->format_capabilties) {
808299529Smm		return (a->format->format_capabilties)(a);
809299529Smm	}
810299529Smm	return ARCHIVE_READ_FORMAT_CAPS_NONE;
811299529Smm}
812299529Smm
813299529Smm/*
814228753Smm * Read data from an archive entry, using a read(2)-style interface.
815228753Smm * This is a convenience routine that just calls
816228753Smm * archive_read_data_block and copies the results into the client
817228753Smm * buffer, filling any gaps with zero bytes.  Clients using this
818228753Smm * API can be completely ignorant of sparse-file issues; sparse files
819228753Smm * will simply be padded with nulls.
820228753Smm *
821228753Smm * DO NOT intermingle calls to this function and archive_read_data_block
822228753Smm * to read a single entry body.
823228753Smm */
824228753Smmssize_t
825228753Smmarchive_read_data(struct archive *_a, void *buff, size_t s)
826228753Smm{
827299529Smm	struct archive *a = (struct archive *)_a;
828228753Smm	char	*dest;
829228753Smm	const void *read_buf;
830228753Smm	size_t	 bytes_read;
831228753Smm	size_t	 len;
832228753Smm	int	 r;
833228753Smm
834228753Smm	bytes_read = 0;
835228753Smm	dest = (char *)buff;
836228753Smm
837228753Smm	while (s > 0) {
838228753Smm		if (a->read_data_remaining == 0) {
839228753Smm			read_buf = a->read_data_block;
840248616Smm			a->read_data_is_posix_read = 1;
841248616Smm			a->read_data_requested = s;
842299529Smm			r = archive_read_data_block(a, &read_buf,
843228753Smm			    &a->read_data_remaining, &a->read_data_offset);
844228753Smm			a->read_data_block = read_buf;
845228753Smm			if (r == ARCHIVE_EOF)
846228753Smm				return (bytes_read);
847228753Smm			/*
848228753Smm			 * Error codes are all negative, so the status
849228753Smm			 * return here cannot be confused with a valid
850228753Smm			 * byte count.  (ARCHIVE_OK is zero.)
851228753Smm			 */
852228753Smm			if (r < ARCHIVE_OK)
853228753Smm				return (r);
854228753Smm		}
855228753Smm
856228753Smm		if (a->read_data_offset < a->read_data_output_offset) {
857299529Smm			archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT,
858228753Smm			    "Encountered out-of-order sparse blocks");
859228753Smm			return (ARCHIVE_RETRY);
860228753Smm		}
861228753Smm
862228753Smm		/* Compute the amount of zero padding needed. */
863232153Smm		if (a->read_data_output_offset + (int64_t)s <
864228753Smm		    a->read_data_offset) {
865228753Smm			len = s;
866228753Smm		} else if (a->read_data_output_offset <
867228753Smm		    a->read_data_offset) {
868238856Smm			len = (size_t)(a->read_data_offset -
869238856Smm			    a->read_data_output_offset);
870228753Smm		} else
871228753Smm			len = 0;
872228753Smm
873228753Smm		/* Add zeroes. */
874228753Smm		memset(dest, 0, len);
875228753Smm		s -= len;
876228753Smm		a->read_data_output_offset += len;
877228753Smm		dest += len;
878228753Smm		bytes_read += len;
879228753Smm
880228753Smm		/* Copy data if there is any space left. */
881228753Smm		if (s > 0) {
882228753Smm			len = a->read_data_remaining;
883228753Smm			if (len > s)
884228753Smm				len = s;
885318482Smm			if (len)
886318482Smm				memcpy(dest, a->read_data_block, len);
887228753Smm			s -= len;
888228753Smm			a->read_data_block += len;
889228753Smm			a->read_data_remaining -= len;
890228753Smm			a->read_data_output_offset += len;
891228753Smm			a->read_data_offset += len;
892228753Smm			dest += len;
893228753Smm			bytes_read += len;
894228753Smm		}
895228753Smm	}
896248616Smm	a->read_data_is_posix_read = 0;
897248616Smm	a->read_data_requested = 0;
898228753Smm	return (bytes_read);
899228753Smm}
900228753Smm
901228753Smm/*
902299529Smm * Reset the read_data_* variables, used for starting a new entry.
903299529Smm */
904299529Smmvoid __archive_reset_read_data(struct archive * a)
905299529Smm{
906299529Smm	a->read_data_output_offset = 0;
907299529Smm	a->read_data_remaining = 0;
908299529Smm	a->read_data_is_posix_read = 0;
909299529Smm	a->read_data_requested = 0;
910299529Smm
911299529Smm   /* extra resets, from rar.c */
912299529Smm   a->read_data_block = NULL;
913299529Smm   a->read_data_offset = 0;
914299529Smm}
915299529Smm
916299529Smm/*
917228753Smm * Skip over all remaining data in this entry.
918228753Smm */
919228753Smmint
920228753Smmarchive_read_data_skip(struct archive *_a)
921228753Smm{
922228753Smm	struct archive_read *a = (struct archive_read *)_a;
923228753Smm	int r;
924228753Smm	const void *buff;
925228753Smm	size_t size;
926232153Smm	int64_t offset;
927228753Smm
928232153Smm	archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA,
929228753Smm	    "archive_read_data_skip");
930228753Smm
931228753Smm	if (a->format->read_data_skip != NULL)
932228753Smm		r = (a->format->read_data_skip)(a);
933228753Smm	else {
934228753Smm		while ((r = archive_read_data_block(&a->archive,
935228753Smm			    &buff, &size, &offset))
936228753Smm		    == ARCHIVE_OK)
937228753Smm			;
938228753Smm	}
939228753Smm
940228753Smm	if (r == ARCHIVE_EOF)
941228753Smm		r = ARCHIVE_OK;
942228753Smm
943228753Smm	a->archive.state = ARCHIVE_STATE_HEADER;
944228753Smm	return (r);
945228753Smm}
946228753Smm
947328827Smmla_int64_t
948248616Smmarchive_seek_data(struct archive *_a, int64_t offset, int whence)
949248616Smm{
950248616Smm	struct archive_read *a = (struct archive_read *)_a;
951248616Smm	archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA,
952248616Smm	    "archive_seek_data_block");
953248616Smm
954248616Smm	if (a->format->seek_data == NULL) {
955248616Smm		archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
956248616Smm		    "Internal error: "
957248616Smm		    "No format_seek_data_block function registered");
958248616Smm		return (ARCHIVE_FATAL);
959248616Smm	}
960248616Smm
961248616Smm	return (a->format->seek_data)(a, offset, whence);
962248616Smm}
963248616Smm
964228753Smm/*
965228753Smm * Read the next block of entry data from the archive.
966228753Smm * This is a zero-copy interface; the client receives a pointer,
967228753Smm * size, and file offset of the next available block of data.
968228753Smm *
969228753Smm * Returns ARCHIVE_OK if the operation is successful, ARCHIVE_EOF if
970228753Smm * the end of entry is encountered.
971228753Smm */
972232153Smmstatic int
973232153Smm_archive_read_data_block(struct archive *_a,
974232153Smm    const void **buff, size_t *size, int64_t *offset)
975228753Smm{
976228753Smm	struct archive_read *a = (struct archive_read *)_a;
977232153Smm	archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA,
978228753Smm	    "archive_read_data_block");
979228753Smm
980228753Smm	if (a->format->read_data == NULL) {
981228753Smm		archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
982228753Smm		    "Internal error: "
983299529Smm		    "No format->read_data function registered");
984228753Smm		return (ARCHIVE_FATAL);
985228753Smm	}
986228753Smm
987228753Smm	return (a->format->read_data)(a, buff, size, offset);
988228753Smm}
989228753Smm
990311041Smmstatic int
991311041Smmclose_filters(struct archive_read *a)
992232153Smm{
993232153Smm	struct archive_read_filter *f = a->filter;
994232153Smm	int r = ARCHIVE_OK;
995232153Smm	/* Close each filter in the pipeline. */
996232153Smm	while (f != NULL) {
997232153Smm		struct archive_read_filter *t = f->upstream;
998232153Smm		if (!f->closed && f->close != NULL) {
999232153Smm			int r1 = (f->close)(f);
1000232153Smm			f->closed = 1;
1001232153Smm			if (r1 < r)
1002232153Smm				r = r1;
1003232153Smm		}
1004232153Smm		free(f->buffer);
1005232153Smm		f->buffer = NULL;
1006232153Smm		f = t;
1007232153Smm	}
1008232153Smm	return r;
1009232153Smm}
1010232153Smm
1011248616Smmvoid
1012248616Smm__archive_read_free_filters(struct archive_read *a)
1013232153Smm{
1014311041Smm	/* Make sure filters are closed and their buffers are freed */
1015311041Smm	close_filters(a);
1016311041Smm
1017232153Smm	while (a->filter != NULL) {
1018232153Smm		struct archive_read_filter *t = a->filter->upstream;
1019232153Smm		free(a->filter);
1020232153Smm		a->filter = t;
1021232153Smm	}
1022232153Smm}
1023232153Smm
1024228753Smm/*
1025232153Smm * return the count of # of filters in use
1026228753Smm */
1027228753Smmstatic int
1028232153Smm_archive_filter_count(struct archive *_a)
1029232153Smm{
1030232153Smm	struct archive_read *a = (struct archive_read *)_a;
1031232153Smm	struct archive_read_filter *p = a->filter;
1032232153Smm	int count = 0;
1033232153Smm	while(p) {
1034232153Smm		count++;
1035232153Smm		p = p->upstream;
1036232153Smm	}
1037232153Smm	return count;
1038232153Smm}
1039232153Smm
1040232153Smm/*
1041232153Smm * Close the file and all I/O.
1042232153Smm */
1043232153Smmstatic int
1044228753Smm_archive_read_close(struct archive *_a)
1045228753Smm{
1046228753Smm	struct archive_read *a = (struct archive_read *)_a;
1047228753Smm	int r = ARCHIVE_OK, r1 = ARCHIVE_OK;
1048228753Smm
1049232153Smm	archive_check_magic(&a->archive, ARCHIVE_READ_MAGIC,
1050232153Smm	    ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_read_close");
1051232153Smm	if (a->archive.state == ARCHIVE_STATE_CLOSED)
1052232153Smm		return (ARCHIVE_OK);
1053228753Smm	archive_clear_error(&a->archive);
1054228753Smm	a->archive.state = ARCHIVE_STATE_CLOSED;
1055228753Smm
1056228753Smm	/* TODO: Clean up the formatters. */
1057228753Smm
1058228753Smm	/* Release the filter objects. */
1059311041Smm	r1 = close_filters(a);
1060228753Smm	if (r1 < r)
1061228753Smm		r = r1;
1062228753Smm
1063228753Smm	return (r);
1064228753Smm}
1065228753Smm
1066228753Smm/*
1067228753Smm * Release memory and other resources.
1068228753Smm */
1069228753Smmstatic int
1070228773Smm_archive_read_free(struct archive *_a)
1071228753Smm{
1072228753Smm	struct archive_read *a = (struct archive_read *)_a;
1073299529Smm	struct archive_read_passphrase *p;
1074232153Smm	int i, n;
1075228753Smm	int slots;
1076228753Smm	int r = ARCHIVE_OK;
1077228753Smm
1078232153Smm	if (_a == NULL)
1079232153Smm		return (ARCHIVE_OK);
1080232153Smm	archive_check_magic(_a, ARCHIVE_READ_MAGIC,
1081232153Smm	    ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_read_free");
1082232153Smm	if (a->archive.state != ARCHIVE_STATE_CLOSED
1083232153Smm	    && a->archive.state != ARCHIVE_STATE_FATAL)
1084228753Smm		r = archive_read_close(&a->archive);
1085228753Smm
1086232153Smm	/* Call cleanup functions registered by optional components. */
1087232153Smm	if (a->cleanup_archive_extract != NULL)
1088232153Smm		r = (a->cleanup_archive_extract)(a);
1089232153Smm
1090228753Smm	/* Cleanup format-specific data. */
1091228753Smm	slots = sizeof(a->formats) / sizeof(a->formats[0]);
1092228753Smm	for (i = 0; i < slots; i++) {
1093228753Smm		a->format = &(a->formats[i]);
1094228753Smm		if (a->formats[i].cleanup)
1095228753Smm			(a->formats[i].cleanup)(a);
1096228753Smm	}
1097228753Smm
1098232153Smm	/* Free the filters */
1099248616Smm	__archive_read_free_filters(a);
1100232153Smm
1101232153Smm	/* Release the bidder objects. */
1102232153Smm	n = sizeof(a->bidders)/sizeof(a->bidders[0]);
1103232153Smm	for (i = 0; i < n; i++) {
1104232153Smm		if (a->bidders[i].free != NULL) {
1105232153Smm			int r1 = (a->bidders[i].free)(&a->bidders[i]);
1106232153Smm			if (r1 < r)
1107232153Smm				r = r1;
1108232153Smm		}
1109232153Smm	}
1110232153Smm
1111299529Smm	/* Release passphrase list. */
1112299529Smm	p = a->passphrases.first;
1113299529Smm	while (p != NULL) {
1114299529Smm		struct archive_read_passphrase *np = p->next;
1115299529Smm
1116299529Smm		/* A passphrase should be cleaned. */
1117299529Smm		memset(p->passphrase, 0, strlen(p->passphrase));
1118299529Smm		free(p->passphrase);
1119299529Smm		free(p);
1120299529Smm		p = np;
1121299529Smm	}
1122299529Smm
1123228753Smm	archive_string_free(&a->archive.error_string);
1124299529Smm	archive_entry_free(a->entry);
1125228753Smm	a->archive.magic = 0;
1126232153Smm	__archive_clean(&a->archive);
1127248616Smm	free(a->client.dataset);
1128228753Smm	free(a);
1129228753Smm	return (r);
1130228753Smm}
1131228753Smm
1132232153Smmstatic struct archive_read_filter *
1133232153Smmget_filter(struct archive *_a, int n)
1134232153Smm{
1135232153Smm	struct archive_read *a = (struct archive_read *)_a;
1136232153Smm	struct archive_read_filter *f = a->filter;
1137248616Smm	/* We use n == -1 for 'the last filter', which is always the
1138248616Smm	 * client proxy. */
1139232153Smm	if (n == -1 && f != NULL) {
1140232153Smm		struct archive_read_filter *last = f;
1141232153Smm		f = f->upstream;
1142232153Smm		while (f != NULL) {
1143232153Smm			last = f;
1144232153Smm			f = f->upstream;
1145232153Smm		}
1146232153Smm		return (last);
1147232153Smm	}
1148232153Smm	if (n < 0)
1149232153Smm		return NULL;
1150232153Smm	while (n > 0 && f != NULL) {
1151232153Smm		f = f->upstream;
1152232153Smm		--n;
1153232153Smm	}
1154232153Smm	return (f);
1155232153Smm}
1156232153Smm
1157232153Smmstatic int
1158232153Smm_archive_filter_code(struct archive *_a, int n)
1159232153Smm{
1160232153Smm	struct archive_read_filter *f = get_filter(_a, n);
1161232153Smm	return f == NULL ? -1 : f->code;
1162232153Smm}
1163232153Smm
1164232153Smmstatic const char *
1165232153Smm_archive_filter_name(struct archive *_a, int n)
1166232153Smm{
1167232153Smm	struct archive_read_filter *f = get_filter(_a, n);
1168299529Smm	return f != NULL ? f->name : NULL;
1169232153Smm}
1170232153Smm
1171232153Smmstatic int64_t
1172232153Smm_archive_filter_bytes(struct archive *_a, int n)
1173232153Smm{
1174232153Smm	struct archive_read_filter *f = get_filter(_a, n);
1175232153Smm	return f == NULL ? -1 : f->position;
1176232153Smm}
1177232153Smm
1178228753Smm/*
1179228753Smm * Used internally by read format handlers to register their bid and
1180228753Smm * initialization functions.
1181228753Smm */
1182228753Smmint
1183228753Smm__archive_read_register_format(struct archive_read *a,
1184228753Smm    void *format_data,
1185228753Smm    const char *name,
1186232153Smm    int (*bid)(struct archive_read *, int),
1187228753Smm    int (*options)(struct archive_read *, const char *, const char *),
1188228753Smm    int (*read_header)(struct archive_read *, struct archive_entry *),
1189232153Smm    int (*read_data)(struct archive_read *, const void **, size_t *, int64_t *),
1190228753Smm    int (*read_data_skip)(struct archive_read *),
1191248616Smm    int64_t (*seek_data)(struct archive_read *, int64_t, int),
1192299529Smm    int (*cleanup)(struct archive_read *),
1193299529Smm    int (*format_capabilities)(struct archive_read *),
1194299529Smm    int (*has_encrypted_entries)(struct archive_read *))
1195228753Smm{
1196228753Smm	int i, number_slots;
1197228753Smm
1198232153Smm	archive_check_magic(&a->archive,
1199228753Smm	    ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
1200228753Smm	    "__archive_read_register_format");
1201228753Smm
1202228753Smm	number_slots = sizeof(a->formats) / sizeof(a->formats[0]);
1203228753Smm
1204228753Smm	for (i = 0; i < number_slots; i++) {
1205228753Smm		if (a->formats[i].bid == bid)
1206228753Smm			return (ARCHIVE_WARN); /* We've already installed */
1207228753Smm		if (a->formats[i].bid == NULL) {
1208228753Smm			a->formats[i].bid = bid;
1209228753Smm			a->formats[i].options = options;
1210228753Smm			a->formats[i].read_header = read_header;
1211228753Smm			a->formats[i].read_data = read_data;
1212228753Smm			a->formats[i].read_data_skip = read_data_skip;
1213248616Smm			a->formats[i].seek_data = seek_data;
1214228753Smm			a->formats[i].cleanup = cleanup;
1215228753Smm			a->formats[i].data = format_data;
1216228753Smm			a->formats[i].name = name;
1217299529Smm			a->formats[i].format_capabilties = format_capabilities;
1218299529Smm			a->formats[i].has_encrypted_entries = has_encrypted_entries;
1219228753Smm			return (ARCHIVE_OK);
1220228753Smm		}
1221228753Smm	}
1222228753Smm
1223232153Smm	archive_set_error(&a->archive, ENOMEM,
1224232153Smm	    "Not enough slots for format registration");
1225232153Smm	return (ARCHIVE_FATAL);
1226228753Smm}
1227228753Smm
1228228753Smm/*
1229228753Smm * Used internally by decompression routines to register their bid and
1230228753Smm * initialization functions.
1231228753Smm */
1232232153Smmint
1233232153Smm__archive_read_get_bidder(struct archive_read *a,
1234232153Smm    struct archive_read_filter_bidder **bidder)
1235228753Smm{
1236228753Smm	int i, number_slots;
1237228753Smm
1238228753Smm	number_slots = sizeof(a->bidders) / sizeof(a->bidders[0]);
1239228753Smm
1240228753Smm	for (i = 0; i < number_slots; i++) {
1241228753Smm		if (a->bidders[i].bid == NULL) {
1242228753Smm			memset(a->bidders + i, 0, sizeof(a->bidders[0]));
1243232153Smm			*bidder = (a->bidders + i);
1244232153Smm			return (ARCHIVE_OK);
1245228753Smm		}
1246228753Smm	}
1247228753Smm
1248232153Smm	archive_set_error(&a->archive, ENOMEM,
1249232153Smm	    "Not enough slots for filter registration");
1250232153Smm	return (ARCHIVE_FATAL);
1251228753Smm}
1252228753Smm
1253228753Smm/*
1254232153Smm * The next section implements the peek/consume internal I/O
1255232153Smm * system used by archive readers.  This system allows simple
1256232153Smm * read-ahead for consumers while preserving zero-copy operation
1257232153Smm * most of the time.
1258228753Smm *
1259232153Smm * The two key operations:
1260232153Smm *  * The read-ahead function returns a pointer to a block of data
1261232153Smm *    that satisfies a minimum request.
1262232153Smm *  * The consume function advances the file pointer.
1263232153Smm *
1264228753Smm * In the ideal case, filters generate blocks of data
1265228753Smm * and __archive_read_ahead() just returns pointers directly into
1266228753Smm * those blocks.  Then __archive_read_consume() just bumps those
1267228753Smm * pointers.  Only if your request would span blocks does the I/O
1268228753Smm * layer use a copy buffer to provide you with a contiguous block of
1269232153Smm * data.
1270228753Smm *
1271228753Smm * A couple of useful idioms:
1272228753Smm *  * "I just want some data."  Ask for 1 byte and pay attention to
1273228753Smm *    the "number of bytes available" from __archive_read_ahead().
1274232153Smm *    Consume whatever you actually use.
1275228753Smm *  * "I want to output a large block of data."  As above, ask for 1 byte,
1276232153Smm *    emit all that's available (up to whatever limit you have), consume
1277232153Smm *    it all, then repeat until you're done.  This effectively means that
1278232153Smm *    you're passing along the blocks that came from your provider.
1279228753Smm *  * "I want to peek ahead by a large amount."  Ask for 4k or so, then
1280228753Smm *    double and repeat until you get an error or have enough.  Note
1281228753Smm *    that the I/O layer will likely end up expanding its copy buffer
1282228753Smm *    to fit your request, so use this technique cautiously.  This
1283228753Smm *    technique is used, for example, by some of the format tasting
1284228753Smm *    code that has uncertain look-ahead needs.
1285228753Smm */
1286228753Smm
1287228753Smm/*
1288228753Smm * Looks ahead in the input stream:
1289228753Smm *  * If 'avail' pointer is provided, that returns number of bytes available
1290228753Smm *    in the current buffer, which may be much larger than requested.
1291228753Smm *  * If end-of-file, *avail gets set to zero.
1292228753Smm *  * If error, *avail gets error code.
1293232153Smm *  * If request can be met, returns pointer to data.
1294232153Smm *  * If minimum request cannot be met, returns NULL.
1295228753Smm *
1296228753Smm * Note: If you just want "some data", ask for 1 byte and pay attention
1297228753Smm * to *avail, which will have the actual amount available.  If you
1298228753Smm * know exactly how many bytes you need, just ask for that and treat
1299228753Smm * a NULL return as an error.
1300228753Smm *
1301228753Smm * Important:  This does NOT move the file pointer.  See
1302228753Smm * __archive_read_consume() below.
1303228753Smm */
1304228753Smmconst void *
1305228753Smm__archive_read_ahead(struct archive_read *a, size_t min, ssize_t *avail)
1306228753Smm{
1307228753Smm	return (__archive_read_filter_ahead(a->filter, min, avail));
1308228753Smm}
1309228753Smm
1310228753Smmconst void *
1311228753Smm__archive_read_filter_ahead(struct archive_read_filter *filter,
1312228753Smm    size_t min, ssize_t *avail)
1313228753Smm{
1314228753Smm	ssize_t bytes_read;
1315228753Smm	size_t tocopy;
1316228753Smm
1317228753Smm	if (filter->fatal) {
1318228753Smm		if (avail)
1319228753Smm			*avail = ARCHIVE_FATAL;
1320228753Smm		return (NULL);
1321228753Smm	}
1322228753Smm
1323228753Smm	/*
1324228753Smm	 * Keep pulling more data until we can satisfy the request.
1325228753Smm	 */
1326228753Smm	for (;;) {
1327228753Smm
1328228753Smm		/*
1329228753Smm		 * If we can satisfy from the copy buffer (and the
1330228753Smm		 * copy buffer isn't empty), we're done.  In particular,
1331228753Smm		 * note that min == 0 is a perfectly well-defined
1332228753Smm		 * request.
1333228753Smm		 */
1334228753Smm		if (filter->avail >= min && filter->avail > 0) {
1335228753Smm			if (avail != NULL)
1336228753Smm				*avail = filter->avail;
1337228753Smm			return (filter->next);
1338228753Smm		}
1339228753Smm
1340228753Smm		/*
1341228753Smm		 * We can satisfy directly from client buffer if everything
1342228753Smm		 * currently in the copy buffer is still in the client buffer.
1343228753Smm		 */
1344228753Smm		if (filter->client_total >= filter->client_avail + filter->avail
1345228753Smm		    && filter->client_avail + filter->avail >= min) {
1346228753Smm			/* "Roll back" to client buffer. */
1347228753Smm			filter->client_avail += filter->avail;
1348228753Smm			filter->client_next -= filter->avail;
1349228753Smm			/* Copy buffer is now empty. */
1350228753Smm			filter->avail = 0;
1351228753Smm			filter->next = filter->buffer;
1352228753Smm			/* Return data from client buffer. */
1353228753Smm			if (avail != NULL)
1354228753Smm				*avail = filter->client_avail;
1355228753Smm			return (filter->client_next);
1356228753Smm		}
1357228753Smm
1358228753Smm		/* Move data forward in copy buffer if necessary. */
1359228753Smm		if (filter->next > filter->buffer &&
1360228753Smm		    filter->next + min > filter->buffer + filter->buffer_size) {
1361228753Smm			if (filter->avail > 0)
1362248616Smm				memmove(filter->buffer, filter->next,
1363248616Smm				    filter->avail);
1364228753Smm			filter->next = filter->buffer;
1365228753Smm		}
1366228753Smm
1367228753Smm		/* If we've used up the client data, get more. */
1368228753Smm		if (filter->client_avail <= 0) {
1369228753Smm			if (filter->end_of_file) {
1370228753Smm				if (avail != NULL)
1371228753Smm					*avail = 0;
1372228753Smm				return (NULL);
1373228753Smm			}
1374228753Smm			bytes_read = (filter->read)(filter,
1375228753Smm			    &filter->client_buff);
1376228753Smm			if (bytes_read < 0) {		/* Read error. */
1377228753Smm				filter->client_total = filter->client_avail = 0;
1378248616Smm				filter->client_next =
1379248616Smm				    filter->client_buff = NULL;
1380228753Smm				filter->fatal = 1;
1381228753Smm				if (avail != NULL)
1382228753Smm					*avail = ARCHIVE_FATAL;
1383228753Smm				return (NULL);
1384228753Smm			}
1385248616Smm			if (bytes_read == 0) {
1386248616Smm				/* Check for another client object first */
1387248616Smm				if (filter->archive->client.cursor !=
1388248616Smm				      filter->archive->client.nodes - 1) {
1389248616Smm					if (client_switch_proxy(filter,
1390248616Smm					    filter->archive->client.cursor + 1)
1391248616Smm					    == ARCHIVE_OK)
1392248616Smm						continue;
1393248616Smm				}
1394248616Smm				/* Premature end-of-file. */
1395228753Smm				filter->client_total = filter->client_avail = 0;
1396248616Smm				filter->client_next =
1397248616Smm				    filter->client_buff = NULL;
1398228753Smm				filter->end_of_file = 1;
1399228753Smm				/* Return whatever we do have. */
1400228753Smm				if (avail != NULL)
1401228753Smm					*avail = filter->avail;
1402228753Smm				return (NULL);
1403228753Smm			}
1404228753Smm			filter->client_total = bytes_read;
1405228753Smm			filter->client_avail = filter->client_total;
1406228753Smm			filter->client_next = filter->client_buff;
1407248616Smm		} else {
1408228753Smm			/*
1409228753Smm			 * We can't satisfy the request from the copy
1410228753Smm			 * buffer or the existing client data, so we
1411228753Smm			 * need to copy more client data over to the
1412228753Smm			 * copy buffer.
1413228753Smm			 */
1414228753Smm
1415228753Smm			/* Ensure the buffer is big enough. */
1416228753Smm			if (min > filter->buffer_size) {
1417228753Smm				size_t s, t;
1418228753Smm				char *p;
1419228753Smm
1420228753Smm				/* Double the buffer; watch for overflow. */
1421228753Smm				s = t = filter->buffer_size;
1422228753Smm				if (s == 0)
1423228753Smm					s = min;
1424228753Smm				while (s < min) {
1425228753Smm					t *= 2;
1426228753Smm					if (t <= s) { /* Integer overflow! */
1427228753Smm						archive_set_error(
1428248616Smm						    &filter->archive->archive,
1429248616Smm						    ENOMEM,
1430248616Smm						    "Unable to allocate copy"
1431248616Smm						    " buffer");
1432228753Smm						filter->fatal = 1;
1433228753Smm						if (avail != NULL)
1434228753Smm							*avail = ARCHIVE_FATAL;
1435228753Smm						return (NULL);
1436228753Smm					}
1437228753Smm					s = t;
1438228753Smm				}
1439228753Smm				/* Now s >= min, so allocate a new buffer. */
1440228753Smm				p = (char *)malloc(s);
1441228753Smm				if (p == NULL) {
1442228753Smm					archive_set_error(
1443228753Smm						&filter->archive->archive,
1444228753Smm						ENOMEM,
1445228753Smm					    "Unable to allocate copy buffer");
1446228753Smm					filter->fatal = 1;
1447228753Smm					if (avail != NULL)
1448228753Smm						*avail = ARCHIVE_FATAL;
1449228753Smm					return (NULL);
1450228753Smm				}
1451228753Smm				/* Move data into newly-enlarged buffer. */
1452228753Smm				if (filter->avail > 0)
1453228753Smm					memmove(p, filter->next, filter->avail);
1454228753Smm				free(filter->buffer);
1455228753Smm				filter->next = filter->buffer = p;
1456228753Smm				filter->buffer_size = s;
1457228753Smm			}
1458228753Smm
1459228753Smm			/* We can add client data to copy buffer. */
1460228753Smm			/* First estimate: copy to fill rest of buffer. */
1461228753Smm			tocopy = (filter->buffer + filter->buffer_size)
1462228753Smm			    - (filter->next + filter->avail);
1463228753Smm			/* Don't waste time buffering more than we need to. */
1464228753Smm			if (tocopy + filter->avail > min)
1465228753Smm				tocopy = min - filter->avail;
1466228753Smm			/* Don't copy more than is available. */
1467228753Smm			if (tocopy > filter->client_avail)
1468228753Smm				tocopy = filter->client_avail;
1469228753Smm
1470248616Smm			memcpy(filter->next + filter->avail,
1471248616Smm			    filter->client_next, tocopy);
1472228753Smm			/* Remove this data from client buffer. */
1473228753Smm			filter->client_next += tocopy;
1474228753Smm			filter->client_avail -= tocopy;
1475228753Smm			/* add it to copy buffer. */
1476228753Smm			filter->avail += tocopy;
1477228753Smm		}
1478228753Smm	}
1479228753Smm}
1480228753Smm
1481228753Smm/*
1482232153Smm * Move the file pointer forward.
1483228753Smm */
1484232153Smmint64_t
1485232153Smm__archive_read_consume(struct archive_read *a, int64_t request)
1486228753Smm{
1487232153Smm	return (__archive_read_filter_consume(a->filter, request));
1488228753Smm}
1489228753Smm
1490232153Smmint64_t
1491228753Smm__archive_read_filter_consume(struct archive_read_filter * filter,
1492232153Smm    int64_t request)
1493228753Smm{
1494232153Smm	int64_t skipped;
1495228753Smm
1496282932Sdelphij	if (request < 0)
1497282932Sdelphij		return ARCHIVE_FATAL;
1498232153Smm	if (request == 0)
1499232153Smm		return 0;
1500232153Smm
1501232153Smm	skipped = advance_file_pointer(filter, request);
1502228753Smm	if (skipped == request)
1503228753Smm		return (skipped);
1504228753Smm	/* We hit EOF before we satisfied the skip request. */
1505232153Smm	if (skipped < 0)  /* Map error code to 0 for error message below. */
1506228753Smm		skipped = 0;
1507232153Smm	archive_set_error(&filter->archive->archive,
1508228753Smm	    ARCHIVE_ERRNO_MISC,
1509228753Smm	    "Truncated input file (needed %jd bytes, only %jd available)",
1510228753Smm	    (intmax_t)request, (intmax_t)skipped);
1511228753Smm	return (ARCHIVE_FATAL);
1512228753Smm}
1513228753Smm
1514232153Smm/*
1515232153Smm * Advance the file pointer by the amount requested.
1516232153Smm * Returns the amount actually advanced, which may be less than the
1517232153Smm * request if EOF is encountered first.
1518232153Smm * Returns a negative value if there's an I/O error.
1519232153Smm */
1520232153Smmstatic int64_t
1521232153Smmadvance_file_pointer(struct archive_read_filter *filter, int64_t request)
1522228753Smm{
1523228753Smm	int64_t bytes_skipped, total_bytes_skipped = 0;
1524232153Smm	ssize_t bytes_read;
1525228753Smm	size_t min;
1526228753Smm
1527228753Smm	if (filter->fatal)
1528228753Smm		return (-1);
1529232153Smm
1530232153Smm	/* Use up the copy buffer first. */
1531228753Smm	if (filter->avail > 0) {
1532238856Smm		min = (size_t)minimum(request, (int64_t)filter->avail);
1533232153Smm		filter->next += min;
1534232153Smm		filter->avail -= min;
1535232153Smm		request -= min;
1536232153Smm		filter->position += min;
1537232153Smm		total_bytes_skipped += min;
1538228753Smm	}
1539232153Smm
1540232153Smm	/* Then use up the client buffer. */
1541228753Smm	if (filter->client_avail > 0) {
1542238856Smm		min = (size_t)minimum(request, (int64_t)filter->client_avail);
1543232153Smm		filter->client_next += min;
1544232153Smm		filter->client_avail -= min;
1545232153Smm		request -= min;
1546232153Smm		filter->position += min;
1547232153Smm		total_bytes_skipped += min;
1548228753Smm	}
1549228753Smm	if (request == 0)
1550228753Smm		return (total_bytes_skipped);
1551232153Smm
1552232153Smm	/* If there's an optimized skip function, use it. */
1553228753Smm	if (filter->skip != NULL) {
1554228753Smm		bytes_skipped = (filter->skip)(filter, request);
1555228753Smm		if (bytes_skipped < 0) {	/* error */
1556228753Smm			filter->fatal = 1;
1557228753Smm			return (bytes_skipped);
1558228753Smm		}
1559232153Smm		filter->position += bytes_skipped;
1560228753Smm		total_bytes_skipped += bytes_skipped;
1561228753Smm		request -= bytes_skipped;
1562232153Smm		if (request == 0)
1563232153Smm			return (total_bytes_skipped);
1564228753Smm	}
1565232153Smm
1566232153Smm	/* Use ordinary reads as necessary to complete the request. */
1567232153Smm	for (;;) {
1568232153Smm		bytes_read = (filter->read)(filter, &filter->client_buff);
1569232153Smm		if (bytes_read < 0) {
1570232153Smm			filter->client_buff = NULL;
1571232153Smm			filter->fatal = 1;
1572228753Smm			return (bytes_read);
1573232153Smm		}
1574232153Smm
1575228753Smm		if (bytes_read == 0) {
1576248616Smm			if (filter->archive->client.cursor !=
1577248616Smm			      filter->archive->client.nodes - 1) {
1578248616Smm				if (client_switch_proxy(filter,
1579248616Smm				    filter->archive->client.cursor + 1)
1580248616Smm				    == ARCHIVE_OK)
1581248616Smm					continue;
1582248616Smm			}
1583232153Smm			filter->client_buff = NULL;
1584232153Smm			filter->end_of_file = 1;
1585228753Smm			return (total_bytes_skipped);
1586228753Smm		}
1587232153Smm
1588232153Smm		if (bytes_read >= request) {
1589232153Smm			filter->client_next =
1590232153Smm			    ((const char *)filter->client_buff) + request;
1591238856Smm			filter->client_avail = (size_t)(bytes_read - request);
1592232153Smm			filter->client_total = bytes_read;
1593232153Smm			total_bytes_skipped += request;
1594232153Smm			filter->position += request;
1595232153Smm			return (total_bytes_skipped);
1596232153Smm		}
1597232153Smm
1598232153Smm		filter->position += bytes_read;
1599228753Smm		total_bytes_skipped += bytes_read;
1600228753Smm		request -= bytes_read;
1601228753Smm	}
1602228753Smm}
1603232153Smm
1604232153Smm/**
1605232153Smm * Returns ARCHIVE_FAILED if seeking isn't supported.
1606232153Smm */
1607232153Smmint64_t
1608232153Smm__archive_read_seek(struct archive_read *a, int64_t offset, int whence)
1609232153Smm{
1610232153Smm	return __archive_read_filter_seek(a->filter, offset, whence);
1611232153Smm}
1612232153Smm
1613232153Smmint64_t
1614248616Smm__archive_read_filter_seek(struct archive_read_filter *filter, int64_t offset,
1615248616Smm    int whence)
1616232153Smm{
1617248616Smm	struct archive_read_client *client;
1618232153Smm	int64_t r;
1619248616Smm	unsigned int cursor;
1620232153Smm
1621232153Smm	if (filter->closed || filter->fatal)
1622232153Smm		return (ARCHIVE_FATAL);
1623232153Smm	if (filter->seek == NULL)
1624232153Smm		return (ARCHIVE_FAILED);
1625248616Smm
1626248616Smm	client = &(filter->archive->client);
1627248616Smm	switch (whence) {
1628248616Smm	case SEEK_CUR:
1629248616Smm		/* Adjust the offset and use SEEK_SET instead */
1630328827Smm		offset += filter->position;
1631328827Smm		__LA_FALLTHROUGH;
1632248616Smm	case SEEK_SET:
1633248616Smm		cursor = 0;
1634248616Smm		while (1)
1635248616Smm		{
1636248616Smm			if (client->dataset[cursor].begin_position < 0 ||
1637248616Smm			    client->dataset[cursor].total_size < 0 ||
1638248616Smm			    client->dataset[cursor].begin_position +
1639248616Smm			      client->dataset[cursor].total_size - 1 > offset ||
1640248616Smm			    cursor + 1 >= client->nodes)
1641248616Smm				break;
1642248616Smm			r = client->dataset[cursor].begin_position +
1643248616Smm				client->dataset[cursor].total_size;
1644248616Smm			client->dataset[++cursor].begin_position = r;
1645248616Smm		}
1646248616Smm		while (1) {
1647248616Smm			r = client_switch_proxy(filter, cursor);
1648248616Smm			if (r != ARCHIVE_OK)
1649248616Smm				return r;
1650248616Smm			if ((r = client_seek_proxy(filter, 0, SEEK_END)) < 0)
1651248616Smm				return r;
1652248616Smm			client->dataset[cursor].total_size = r;
1653248616Smm			if (client->dataset[cursor].begin_position +
1654248616Smm			    client->dataset[cursor].total_size - 1 > offset ||
1655248616Smm			    cursor + 1 >= client->nodes)
1656248616Smm				break;
1657248616Smm			r = client->dataset[cursor].begin_position +
1658248616Smm				client->dataset[cursor].total_size;
1659248616Smm			client->dataset[++cursor].begin_position = r;
1660248616Smm		}
1661248616Smm		offset -= client->dataset[cursor].begin_position;
1662299529Smm		if (offset < 0
1663299529Smm		    || offset > client->dataset[cursor].total_size)
1664299529Smm			return ARCHIVE_FATAL;
1665248616Smm		if ((r = client_seek_proxy(filter, offset, SEEK_SET)) < 0)
1666248616Smm			return r;
1667248616Smm		break;
1668248616Smm
1669248616Smm	case SEEK_END:
1670248616Smm		cursor = 0;
1671248616Smm		while (1) {
1672248616Smm			if (client->dataset[cursor].begin_position < 0 ||
1673248616Smm			    client->dataset[cursor].total_size < 0 ||
1674248616Smm			    cursor + 1 >= client->nodes)
1675248616Smm				break;
1676248616Smm			r = client->dataset[cursor].begin_position +
1677248616Smm				client->dataset[cursor].total_size;
1678248616Smm			client->dataset[++cursor].begin_position = r;
1679248616Smm		}
1680248616Smm		while (1) {
1681248616Smm			r = client_switch_proxy(filter, cursor);
1682248616Smm			if (r != ARCHIVE_OK)
1683248616Smm				return r;
1684248616Smm			if ((r = client_seek_proxy(filter, 0, SEEK_END)) < 0)
1685248616Smm				return r;
1686248616Smm			client->dataset[cursor].total_size = r;
1687248616Smm			r = client->dataset[cursor].begin_position +
1688248616Smm				client->dataset[cursor].total_size;
1689248616Smm			if (cursor + 1 >= client->nodes)
1690248616Smm				break;
1691248616Smm			client->dataset[++cursor].begin_position = r;
1692248616Smm		}
1693248616Smm		while (1) {
1694248616Smm			if (r + offset >=
1695248616Smm			    client->dataset[cursor].begin_position)
1696248616Smm				break;
1697248616Smm			offset += client->dataset[cursor].total_size;
1698248616Smm			if (cursor == 0)
1699248616Smm				break;
1700248616Smm			cursor--;
1701248616Smm			r = client->dataset[cursor].begin_position +
1702248616Smm				client->dataset[cursor].total_size;
1703248616Smm		}
1704248616Smm		offset = (r + offset) - client->dataset[cursor].begin_position;
1705248616Smm		if ((r = client_switch_proxy(filter, cursor)) != ARCHIVE_OK)
1706248616Smm			return r;
1707248616Smm		r = client_seek_proxy(filter, offset, SEEK_SET);
1708248616Smm		if (r < ARCHIVE_OK)
1709248616Smm			return r;
1710248616Smm		break;
1711248616Smm
1712248616Smm	default:
1713248616Smm		return (ARCHIVE_FATAL);
1714248616Smm	}
1715248616Smm	r += client->dataset[cursor].begin_position;
1716248616Smm
1717232153Smm	if (r >= 0) {
1718232153Smm		/*
1719232153Smm		 * Ouch.  Clearing the buffer like this hurts, especially
1720232153Smm		 * at bid time.  A lot of our efficiency at bid time comes
1721232153Smm		 * from having bidders reuse the data we've already read.
1722232153Smm		 *
1723232153Smm		 * TODO: If the seek request is in data we already
1724232153Smm		 * have, then don't call the seek callback.
1725232153Smm		 *
1726232153Smm		 * TODO: Zip seeks to end-of-file at bid time.  If
1727232153Smm		 * other formats also start doing this, we may need to
1728232153Smm		 * find a way for clients to fudge the seek offset to
1729232153Smm		 * a block boundary.
1730232153Smm		 *
1731232153Smm		 * Hmmm... If whence was SEEK_END, we know the file
1732232153Smm		 * size is (r - offset).  Can we use that to simplify
1733232153Smm		 * the TODO items above?
1734232153Smm		 */
1735232153Smm		filter->avail = filter->client_avail = 0;
1736232153Smm		filter->next = filter->buffer;
1737232153Smm		filter->position = r;
1738232153Smm		filter->end_of_file = 0;
1739232153Smm	}
1740232153Smm	return r;
1741232153Smm}
1742