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