1 /*-
2  * Copyright (c) 2003-2011 Tim Kientzle
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 /*
27  * This file contains the "essential" portions of the read API, that
28  * is, stuff that will probably always be used by any client that
29  * actually needs to read an archive.  Optional pieces have been, as
30  * far as possible, separated out into separate files to avoid
31  * needlessly bloating statically-linked clients.
32  */
33 
34 #include "archive_platform.h"
35 __FBSDID("$FreeBSD: head/lib/libarchive/archive_read.c 201157 2009-12-29 05:30:23Z kientzle $");
36 
37 #ifdef HAVE_ERRNO_H
38 #include <errno.h>
39 #endif
40 #include <stdio.h>
41 #ifdef HAVE_STDLIB_H
42 #include <stdlib.h>
43 #endif
44 #ifdef HAVE_STRING_H
45 #include <string.h>
46 #endif
47 #ifdef HAVE_UNISTD_H
48 #include <unistd.h>
49 #endif
50 
51 #include "archive.h"
52 #include "archive_entry.h"
53 #include "archive_private.h"
54 #include "archive_read_private.h"
55 
56 #define minimum(a, b) (a < b ? a : b)
57 
58 static int	choose_filters(struct archive_read *);
59 static int	choose_format(struct archive_read *);
60 static int	close_filters(struct archive_read *);
61 static struct archive_vtable *archive_read_vtable(void);
62 static int64_t	_archive_filter_bytes(struct archive *, int);
63 static int	_archive_filter_code(struct archive *, int);
64 static const char *_archive_filter_name(struct archive *, int);
65 static int  _archive_filter_count(struct archive *);
66 static int	_archive_read_close(struct archive *);
67 static int	_archive_read_data_block(struct archive *,
68 		    const void **, size_t *, int64_t *);
69 static int	_archive_read_free(struct archive *);
70 static int	_archive_read_next_header(struct archive *,
71 		    struct archive_entry **);
72 static int	_archive_read_next_header2(struct archive *,
73 		    struct archive_entry *);
74 static int64_t  advance_file_pointer(struct archive_read_filter *, int64_t);
75 
76 static struct archive_vtable *
archive_read_vtable(void)77 archive_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  */
100 struct archive *
archive_read_new(void)101 archive_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  */
122 void
archive_read_extract_set_skip_file(struct archive * _a,int64_t d,int64_t i)123 archive_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  */
138 int
archive_read_open(struct archive * a,void * client_data,archive_open_callback * client_opener,archive_read_callback * client_reader,archive_close_callback * client_closer)139 archive_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 
153 int
archive_read_open2(struct archive * a,void * client_data,archive_open_callback * client_opener,archive_read_callback * client_reader,archive_skip_callback * client_skipper,archive_close_callback * client_closer)154 archive_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 
170 static ssize_t
client_read_proxy(struct archive_read_filter * self,const void ** buff)171 client_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 
179 static int64_t
client_skip_proxy(struct archive_read_filter * self,int64_t request)180 client_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 
229 static int64_t
client_seek_proxy(struct archive_read_filter * self,int64_t offset,int whence)230 client_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 
246 static int
client_close_proxy(struct archive_read_filter * self)247 client_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 
265 static int
client_open_proxy(struct archive_read_filter * self)266 client_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 
275 static int
client_switch_proxy(struct archive_read_filter * self,unsigned int iindex)276 client_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 
307 int
archive_read_set_open_callback(struct archive * _a,archive_open_callback * client_opener)308 archive_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 
318 int
archive_read_set_read_callback(struct archive * _a,archive_read_callback * client_reader)319 archive_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 
329 int
archive_read_set_skip_callback(struct archive * _a,archive_skip_callback * client_skipper)330 archive_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 
340 int
archive_read_set_seek_callback(struct archive * _a,archive_seek_callback * client_seeker)341 archive_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 
351 int
archive_read_set_close_callback(struct archive * _a,archive_close_callback * client_closer)352 archive_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 
362 int
archive_read_set_switch_callback(struct archive * _a,archive_switch_callback * client_switcher)363 archive_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 
373 int
archive_read_set_callback_data(struct archive * _a,void * client_data)374 archive_read_set_callback_data(struct archive *_a, void *client_data)
375 {
376 	return archive_read_set_callback_data2(_a, client_data, 0);
377 }
378 
379 int
archive_read_set_callback_data2(struct archive * _a,void * client_data,unsigned int iindex)380 archive_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 
412 int
archive_read_add_callback_data(struct archive * _a,void * client_data,unsigned int iindex)413 archive_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 
446 int
archive_read_append_callback_data(struct archive * _a,void * client_data)447 archive_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 
453 int
archive_read_prepend_callback_data(struct archive * _a,void * client_data)454 archive_read_prepend_callback_data(struct archive *_a, void *client_data)
455 {
456 	return archive_read_add_callback_data(_a, client_data, 0);
457 }
458 
459 int
archive_read_open1(struct archive * _a)460 archive_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 
555 static int
choose_filters(struct archive_read * a)556 choose_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  */
616 static int
_archive_read_next_header2(struct archive * _a,struct archive_entry * entry)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 
680 static int
_archive_read_next_header(struct archive * _a,struct archive_entry ** entryp)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  */
695 static int
choose_format(struct archive_read * a)696 choose_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  */
750 int64_t
archive_read_header_position(struct archive * _a)751 archive_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  */
777 int
archive_read_has_encrypted_entries(struct archive * _a)778 archive_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  */
802 int
archive_read_format_capabilities(struct archive * _a)803 archive_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  */
823 ssize_t
archive_read_data(struct archive * _a,void * buff,size_t s)824 archive_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 			memcpy(dest, a->read_data_block, len);
885 			s -= len;
886 			a->read_data_block += len;
887 			a->read_data_remaining -= len;
888 			a->read_data_output_offset += len;
889 			a->read_data_offset += len;
890 			dest += len;
891 			bytes_read += len;
892 		}
893 	}
894 	a->read_data_is_posix_read = 0;
895 	a->read_data_requested = 0;
896 	return (bytes_read);
897 }
898 
899 /*
900  * Reset the read_data_* variables, used for starting a new entry.
901  */
__archive_reset_read_data(struct archive * a)902 void __archive_reset_read_data(struct archive * a)
903 {
904 	a->read_data_output_offset = 0;
905 	a->read_data_remaining = 0;
906 	a->read_data_is_posix_read = 0;
907 	a->read_data_requested = 0;
908 
909    /* extra resets, from rar.c */
910    a->read_data_block = NULL;
911    a->read_data_offset = 0;
912 }
913 
914 /*
915  * Skip over all remaining data in this entry.
916  */
917 int
archive_read_data_skip(struct archive * _a)918 archive_read_data_skip(struct archive *_a)
919 {
920 	struct archive_read *a = (struct archive_read *)_a;
921 	int r;
922 	const void *buff;
923 	size_t size;
924 	int64_t offset;
925 
926 	archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA,
927 	    "archive_read_data_skip");
928 
929 	if (a->format->read_data_skip != NULL)
930 		r = (a->format->read_data_skip)(a);
931 	else {
932 		while ((r = archive_read_data_block(&a->archive,
933 			    &buff, &size, &offset))
934 		    == ARCHIVE_OK)
935 			;
936 	}
937 
938 	if (r == ARCHIVE_EOF)
939 		r = ARCHIVE_OK;
940 
941 	a->archive.state = ARCHIVE_STATE_HEADER;
942 	return (r);
943 }
944 
945 int64_t
archive_seek_data(struct archive * _a,int64_t offset,int whence)946 archive_seek_data(struct archive *_a, int64_t offset, int whence)
947 {
948 	struct archive_read *a = (struct archive_read *)_a;
949 	archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA,
950 	    "archive_seek_data_block");
951 
952 	if (a->format->seek_data == NULL) {
953 		archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
954 		    "Internal error: "
955 		    "No format_seek_data_block function registered");
956 		return (ARCHIVE_FATAL);
957 	}
958 
959 	return (a->format->seek_data)(a, offset, whence);
960 }
961 
962 /*
963  * Read the next block of entry data from the archive.
964  * This is a zero-copy interface; the client receives a pointer,
965  * size, and file offset of the next available block of data.
966  *
967  * Returns ARCHIVE_OK if the operation is successful, ARCHIVE_EOF if
968  * the end of entry is encountered.
969  */
970 static int
_archive_read_data_block(struct archive * _a,const void ** buff,size_t * size,int64_t * offset)971 _archive_read_data_block(struct archive *_a,
972     const void **buff, size_t *size, int64_t *offset)
973 {
974 	struct archive_read *a = (struct archive_read *)_a;
975 	archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA,
976 	    "archive_read_data_block");
977 
978 	if (a->format->read_data == NULL) {
979 		archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
980 		    "Internal error: "
981 		    "No format->read_data function registered");
982 		return (ARCHIVE_FATAL);
983 	}
984 
985 	return (a->format->read_data)(a, buff, size, offset);
986 }
987 
988 static int
close_filters(struct archive_read * a)989 close_filters(struct archive_read *a)
990 {
991 	struct archive_read_filter *f = a->filter;
992 	int r = ARCHIVE_OK;
993 	/* Close each filter in the pipeline. */
994 	while (f != NULL) {
995 		struct archive_read_filter *t = f->upstream;
996 		if (!f->closed && f->close != NULL) {
997 			int r1 = (f->close)(f);
998 			f->closed = 1;
999 			if (r1 < r)
1000 				r = r1;
1001 		}
1002 		free(f->buffer);
1003 		f->buffer = NULL;
1004 		f = t;
1005 	}
1006 	return r;
1007 }
1008 
1009 void
__archive_read_free_filters(struct archive_read * a)1010 __archive_read_free_filters(struct archive_read *a)
1011 {
1012 	/* Make sure filters are closed and their buffers are freed */
1013 	close_filters(a);
1014 
1015 	while (a->filter != NULL) {
1016 		struct archive_read_filter *t = a->filter->upstream;
1017 		free(a->filter);
1018 		a->filter = t;
1019 	}
1020 }
1021 
1022 /*
1023  * return the count of # of filters in use
1024  */
1025 static int
_archive_filter_count(struct archive * _a)1026 _archive_filter_count(struct archive *_a)
1027 {
1028 	struct archive_read *a = (struct archive_read *)_a;
1029 	struct archive_read_filter *p = a->filter;
1030 	int count = 0;
1031 	while(p) {
1032 		count++;
1033 		p = p->upstream;
1034 	}
1035 	return count;
1036 }
1037 
1038 /*
1039  * Close the file and all I/O.
1040  */
1041 static int
_archive_read_close(struct archive * _a)1042 _archive_read_close(struct archive *_a)
1043 {
1044 	struct archive_read *a = (struct archive_read *)_a;
1045 	int r = ARCHIVE_OK, r1 = ARCHIVE_OK;
1046 
1047 	archive_check_magic(&a->archive, ARCHIVE_READ_MAGIC,
1048 	    ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_read_close");
1049 	if (a->archive.state == ARCHIVE_STATE_CLOSED)
1050 		return (ARCHIVE_OK);
1051 	archive_clear_error(&a->archive);
1052 	a->archive.state = ARCHIVE_STATE_CLOSED;
1053 
1054 	/* TODO: Clean up the formatters. */
1055 
1056 	/* Release the filter objects. */
1057 	r1 = close_filters(a);
1058 	if (r1 < r)
1059 		r = r1;
1060 
1061 	return (r);
1062 }
1063 
1064 /*
1065  * Release memory and other resources.
1066  */
1067 static int
_archive_read_free(struct archive * _a)1068 _archive_read_free(struct archive *_a)
1069 {
1070 	struct archive_read *a = (struct archive_read *)_a;
1071 	struct archive_read_passphrase *p;
1072 	int i, n;
1073 	int slots;
1074 	int r = ARCHIVE_OK;
1075 
1076 	if (_a == NULL)
1077 		return (ARCHIVE_OK);
1078 	archive_check_magic(_a, ARCHIVE_READ_MAGIC,
1079 	    ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_read_free");
1080 	if (a->archive.state != ARCHIVE_STATE_CLOSED
1081 	    && a->archive.state != ARCHIVE_STATE_FATAL)
1082 		r = archive_read_close(&a->archive);
1083 
1084 	/* Call cleanup functions registered by optional components. */
1085 	if (a->cleanup_archive_extract != NULL)
1086 		r = (a->cleanup_archive_extract)(a);
1087 
1088 	/* Cleanup format-specific data. */
1089 	slots = sizeof(a->formats) / sizeof(a->formats[0]);
1090 	for (i = 0; i < slots; i++) {
1091 		a->format = &(a->formats[i]);
1092 		if (a->formats[i].cleanup)
1093 			(a->formats[i].cleanup)(a);
1094 	}
1095 
1096 	/* Free the filters */
1097 	__archive_read_free_filters(a);
1098 
1099 	/* Release the bidder objects. */
1100 	n = sizeof(a->bidders)/sizeof(a->bidders[0]);
1101 	for (i = 0; i < n; i++) {
1102 		if (a->bidders[i].free != NULL) {
1103 			int r1 = (a->bidders[i].free)(&a->bidders[i]);
1104 			if (r1 < r)
1105 				r = r1;
1106 		}
1107 	}
1108 
1109 	/* Release passphrase list. */
1110 	p = a->passphrases.first;
1111 	while (p != NULL) {
1112 		struct archive_read_passphrase *np = p->next;
1113 
1114 		/* A passphrase should be cleaned. */
1115 		memset(p->passphrase, 0, strlen(p->passphrase));
1116 		free(p->passphrase);
1117 		free(p);
1118 		p = np;
1119 	}
1120 
1121 	archive_string_free(&a->archive.error_string);
1122 	archive_entry_free(a->entry);
1123 	a->archive.magic = 0;
1124 	__archive_clean(&a->archive);
1125 	free(a->client.dataset);
1126 	free(a);
1127 	return (r);
1128 }
1129 
1130 static struct archive_read_filter *
get_filter(struct archive * _a,int n)1131 get_filter(struct archive *_a, int n)
1132 {
1133 	struct archive_read *a = (struct archive_read *)_a;
1134 	struct archive_read_filter *f = a->filter;
1135 	/* We use n == -1 for 'the last filter', which is always the
1136 	 * client proxy. */
1137 	if (n == -1 && f != NULL) {
1138 		struct archive_read_filter *last = f;
1139 		f = f->upstream;
1140 		while (f != NULL) {
1141 			last = f;
1142 			f = f->upstream;
1143 		}
1144 		return (last);
1145 	}
1146 	if (n < 0)
1147 		return NULL;
1148 	while (n > 0 && f != NULL) {
1149 		f = f->upstream;
1150 		--n;
1151 	}
1152 	return (f);
1153 }
1154 
1155 static int
_archive_filter_code(struct archive * _a,int n)1156 _archive_filter_code(struct archive *_a, int n)
1157 {
1158 	struct archive_read_filter *f = get_filter(_a, n);
1159 	return f == NULL ? -1 : f->code;
1160 }
1161 
1162 static const char *
_archive_filter_name(struct archive * _a,int n)1163 _archive_filter_name(struct archive *_a, int n)
1164 {
1165 	struct archive_read_filter *f = get_filter(_a, n);
1166 	return f != NULL ? f->name : NULL;
1167 }
1168 
1169 static int64_t
_archive_filter_bytes(struct archive * _a,int n)1170 _archive_filter_bytes(struct archive *_a, int n)
1171 {
1172 	struct archive_read_filter *f = get_filter(_a, n);
1173 	return f == NULL ? -1 : f->position;
1174 }
1175 
1176 /*
1177  * Used internally by read format handlers to register their bid and
1178  * initialization functions.
1179  */
1180 int
__archive_read_register_format(struct archive_read * a,void * format_data,const char * name,int (* bid)(struct archive_read *,int),int (* options)(struct archive_read *,const char *,const char *),int (* read_header)(struct archive_read *,struct archive_entry *),int (* read_data)(struct archive_read *,const void **,size_t *,int64_t *),int (* read_data_skip)(struct archive_read *),int64_t (* seek_data)(struct archive_read *,int64_t,int),int (* cleanup)(struct archive_read *),int (* format_capabilities)(struct archive_read *),int (* has_encrypted_entries)(struct archive_read *))1181 __archive_read_register_format(struct archive_read *a,
1182     void *format_data,
1183     const char *name,
1184     int (*bid)(struct archive_read *, int),
1185     int (*options)(struct archive_read *, const char *, const char *),
1186     int (*read_header)(struct archive_read *, struct archive_entry *),
1187     int (*read_data)(struct archive_read *, const void **, size_t *, int64_t *),
1188     int (*read_data_skip)(struct archive_read *),
1189     int64_t (*seek_data)(struct archive_read *, int64_t, int),
1190     int (*cleanup)(struct archive_read *),
1191     int (*format_capabilities)(struct archive_read *),
1192     int (*has_encrypted_entries)(struct archive_read *))
1193 {
1194 	int i, number_slots;
1195 
1196 	archive_check_magic(&a->archive,
1197 	    ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
1198 	    "__archive_read_register_format");
1199 
1200 	number_slots = sizeof(a->formats) / sizeof(a->formats[0]);
1201 
1202 	for (i = 0; i < number_slots; i++) {
1203 		if (a->formats[i].bid == bid)
1204 			return (ARCHIVE_WARN); /* We've already installed */
1205 		if (a->formats[i].bid == NULL) {
1206 			a->formats[i].bid = bid;
1207 			a->formats[i].options = options;
1208 			a->formats[i].read_header = read_header;
1209 			a->formats[i].read_data = read_data;
1210 			a->formats[i].read_data_skip = read_data_skip;
1211 			a->formats[i].seek_data = seek_data;
1212 			a->formats[i].cleanup = cleanup;
1213 			a->formats[i].data = format_data;
1214 			a->formats[i].name = name;
1215 			a->formats[i].format_capabilties = format_capabilities;
1216 			a->formats[i].has_encrypted_entries = has_encrypted_entries;
1217 			return (ARCHIVE_OK);
1218 		}
1219 	}
1220 
1221 	archive_set_error(&a->archive, ENOMEM,
1222 	    "Not enough slots for format registration");
1223 	return (ARCHIVE_FATAL);
1224 }
1225 
1226 /*
1227  * Used internally by decompression routines to register their bid and
1228  * initialization functions.
1229  */
1230 int
__archive_read_get_bidder(struct archive_read * a,struct archive_read_filter_bidder ** bidder)1231 __archive_read_get_bidder(struct archive_read *a,
1232     struct archive_read_filter_bidder **bidder)
1233 {
1234 	int i, number_slots;
1235 
1236 	number_slots = sizeof(a->bidders) / sizeof(a->bidders[0]);
1237 
1238 	for (i = 0; i < number_slots; i++) {
1239 		if (a->bidders[i].bid == NULL) {
1240 			memset(a->bidders + i, 0, sizeof(a->bidders[0]));
1241 			*bidder = (a->bidders + i);
1242 			return (ARCHIVE_OK);
1243 		}
1244 	}
1245 
1246 	archive_set_error(&a->archive, ENOMEM,
1247 	    "Not enough slots for filter registration");
1248 	return (ARCHIVE_FATAL);
1249 }
1250 
1251 /*
1252  * The next section implements the peek/consume internal I/O
1253  * system used by archive readers.  This system allows simple
1254  * read-ahead for consumers while preserving zero-copy operation
1255  * most of the time.
1256  *
1257  * The two key operations:
1258  *  * The read-ahead function returns a pointer to a block of data
1259  *    that satisfies a minimum request.
1260  *  * The consume function advances the file pointer.
1261  *
1262  * In the ideal case, filters generate blocks of data
1263  * and __archive_read_ahead() just returns pointers directly into
1264  * those blocks.  Then __archive_read_consume() just bumps those
1265  * pointers.  Only if your request would span blocks does the I/O
1266  * layer use a copy buffer to provide you with a contiguous block of
1267  * data.
1268  *
1269  * A couple of useful idioms:
1270  *  * "I just want some data."  Ask for 1 byte and pay attention to
1271  *    the "number of bytes available" from __archive_read_ahead().
1272  *    Consume whatever you actually use.
1273  *  * "I want to output a large block of data."  As above, ask for 1 byte,
1274  *    emit all that's available (up to whatever limit you have), consume
1275  *    it all, then repeat until you're done.  This effectively means that
1276  *    you're passing along the blocks that came from your provider.
1277  *  * "I want to peek ahead by a large amount."  Ask for 4k or so, then
1278  *    double and repeat until you get an error or have enough.  Note
1279  *    that the I/O layer will likely end up expanding its copy buffer
1280  *    to fit your request, so use this technique cautiously.  This
1281  *    technique is used, for example, by some of the format tasting
1282  *    code that has uncertain look-ahead needs.
1283  */
1284 
1285 /*
1286  * Looks ahead in the input stream:
1287  *  * If 'avail' pointer is provided, that returns number of bytes available
1288  *    in the current buffer, which may be much larger than requested.
1289  *  * If end-of-file, *avail gets set to zero.
1290  *  * If error, *avail gets error code.
1291  *  * If request can be met, returns pointer to data.
1292  *  * If minimum request cannot be met, returns NULL.
1293  *
1294  * Note: If you just want "some data", ask for 1 byte and pay attention
1295  * to *avail, which will have the actual amount available.  If you
1296  * know exactly how many bytes you need, just ask for that and treat
1297  * a NULL return as an error.
1298  *
1299  * Important:  This does NOT move the file pointer.  See
1300  * __archive_read_consume() below.
1301  */
1302 const void *
__archive_read_ahead(struct archive_read * a,size_t min,ssize_t * avail)1303 __archive_read_ahead(struct archive_read *a, size_t min, ssize_t *avail)
1304 {
1305 	return (__archive_read_filter_ahead(a->filter, min, avail));
1306 }
1307 
1308 const void *
__archive_read_filter_ahead(struct archive_read_filter * filter,size_t min,ssize_t * avail)1309 __archive_read_filter_ahead(struct archive_read_filter *filter,
1310     size_t min, ssize_t *avail)
1311 {
1312 	ssize_t bytes_read;
1313 	size_t tocopy;
1314 
1315 	if (filter->fatal) {
1316 		if (avail)
1317 			*avail = ARCHIVE_FATAL;
1318 		return (NULL);
1319 	}
1320 
1321 	/*
1322 	 * Keep pulling more data until we can satisfy the request.
1323 	 */
1324 	for (;;) {
1325 
1326 		/*
1327 		 * If we can satisfy from the copy buffer (and the
1328 		 * copy buffer isn't empty), we're done.  In particular,
1329 		 * note that min == 0 is a perfectly well-defined
1330 		 * request.
1331 		 */
1332 		if (filter->avail >= min && filter->avail > 0) {
1333 			if (avail != NULL)
1334 				*avail = filter->avail;
1335 			return (filter->next);
1336 		}
1337 
1338 		/*
1339 		 * We can satisfy directly from client buffer if everything
1340 		 * currently in the copy buffer is still in the client buffer.
1341 		 */
1342 		if (filter->client_total >= filter->client_avail + filter->avail
1343 		    && filter->client_avail + filter->avail >= min) {
1344 			/* "Roll back" to client buffer. */
1345 			filter->client_avail += filter->avail;
1346 			filter->client_next -= filter->avail;
1347 			/* Copy buffer is now empty. */
1348 			filter->avail = 0;
1349 			filter->next = filter->buffer;
1350 			/* Return data from client buffer. */
1351 			if (avail != NULL)
1352 				*avail = filter->client_avail;
1353 			return (filter->client_next);
1354 		}
1355 
1356 		/* Move data forward in copy buffer if necessary. */
1357 		if (filter->next > filter->buffer &&
1358 		    filter->next + min > filter->buffer + filter->buffer_size) {
1359 			if (filter->avail > 0)
1360 				memmove(filter->buffer, filter->next,
1361 				    filter->avail);
1362 			filter->next = filter->buffer;
1363 		}
1364 
1365 		/* If we've used up the client data, get more. */
1366 		if (filter->client_avail <= 0) {
1367 			if (filter->end_of_file) {
1368 				if (avail != NULL)
1369 					*avail = 0;
1370 				return (NULL);
1371 			}
1372 			bytes_read = (filter->read)(filter,
1373 			    &filter->client_buff);
1374 			if (bytes_read < 0) {		/* Read error. */
1375 				filter->client_total = filter->client_avail = 0;
1376 				filter->client_next =
1377 				    filter->client_buff = NULL;
1378 				filter->fatal = 1;
1379 				if (avail != NULL)
1380 					*avail = ARCHIVE_FATAL;
1381 				return (NULL);
1382 			}
1383 			if (bytes_read == 0) {
1384 				/* Check for another client object first */
1385 				if (filter->archive->client.cursor !=
1386 				      filter->archive->client.nodes - 1) {
1387 					if (client_switch_proxy(filter,
1388 					    filter->archive->client.cursor + 1)
1389 					    == ARCHIVE_OK)
1390 						continue;
1391 				}
1392 				/* Premature end-of-file. */
1393 				filter->client_total = filter->client_avail = 0;
1394 				filter->client_next =
1395 				    filter->client_buff = NULL;
1396 				filter->end_of_file = 1;
1397 				/* Return whatever we do have. */
1398 				if (avail != NULL)
1399 					*avail = filter->avail;
1400 				return (NULL);
1401 			}
1402 			filter->client_total = bytes_read;
1403 			filter->client_avail = filter->client_total;
1404 			filter->client_next = filter->client_buff;
1405 		} else {
1406 			/*
1407 			 * We can't satisfy the request from the copy
1408 			 * buffer or the existing client data, so we
1409 			 * need to copy more client data over to the
1410 			 * copy buffer.
1411 			 */
1412 
1413 			/* Ensure the buffer is big enough. */
1414 			if (min > filter->buffer_size) {
1415 				size_t s, t;
1416 				char *p;
1417 
1418 				/* Double the buffer; watch for overflow. */
1419 				s = t = filter->buffer_size;
1420 				if (s == 0)
1421 					s = min;
1422 				while (s < min) {
1423 					t *= 2;
1424 					if (t <= s) { /* Integer overflow! */
1425 						archive_set_error(
1426 						    &filter->archive->archive,
1427 						    ENOMEM,
1428 						    "Unable to allocate copy"
1429 						    " buffer");
1430 						filter->fatal = 1;
1431 						if (avail != NULL)
1432 							*avail = ARCHIVE_FATAL;
1433 						return (NULL);
1434 					}
1435 					s = t;
1436 				}
1437 				/* Now s >= min, so allocate a new buffer. */
1438 				p = (char *)malloc(s);
1439 				if (p == NULL) {
1440 					archive_set_error(
1441 						&filter->archive->archive,
1442 						ENOMEM,
1443 					    "Unable to allocate copy buffer");
1444 					filter->fatal = 1;
1445 					if (avail != NULL)
1446 						*avail = ARCHIVE_FATAL;
1447 					return (NULL);
1448 				}
1449 				/* Move data into newly-enlarged buffer. */
1450 				if (filter->avail > 0)
1451 					memmove(p, filter->next, filter->avail);
1452 				free(filter->buffer);
1453 				filter->next = filter->buffer = p;
1454 				filter->buffer_size = s;
1455 			}
1456 
1457 			/* We can add client data to copy buffer. */
1458 			/* First estimate: copy to fill rest of buffer. */
1459 			tocopy = (filter->buffer + filter->buffer_size)
1460 			    - (filter->next + filter->avail);
1461 			/* Don't waste time buffering more than we need to. */
1462 			if (tocopy + filter->avail > min)
1463 				tocopy = min - filter->avail;
1464 			/* Don't copy more than is available. */
1465 			if (tocopy > filter->client_avail)
1466 				tocopy = filter->client_avail;
1467 
1468 			memcpy(filter->next + filter->avail,
1469 			    filter->client_next, tocopy);
1470 			/* Remove this data from client buffer. */
1471 			filter->client_next += tocopy;
1472 			filter->client_avail -= tocopy;
1473 			/* add it to copy buffer. */
1474 			filter->avail += tocopy;
1475 		}
1476 	}
1477 }
1478 
1479 /*
1480  * Move the file pointer forward.
1481  */
1482 int64_t
__archive_read_consume(struct archive_read * a,int64_t request)1483 __archive_read_consume(struct archive_read *a, int64_t request)
1484 {
1485 	return (__archive_read_filter_consume(a->filter, request));
1486 }
1487 
1488 int64_t
__archive_read_filter_consume(struct archive_read_filter * filter,int64_t request)1489 __archive_read_filter_consume(struct archive_read_filter * filter,
1490     int64_t request)
1491 {
1492 	int64_t skipped;
1493 
1494 	if (request < 0)
1495 		return ARCHIVE_FATAL;
1496 	if (request == 0)
1497 		return 0;
1498 
1499 	skipped = advance_file_pointer(filter, request);
1500 	if (skipped == request)
1501 		return (skipped);
1502 	/* We hit EOF before we satisfied the skip request. */
1503 	if (skipped < 0)  /* Map error code to 0 for error message below. */
1504 		skipped = 0;
1505 	archive_set_error(&filter->archive->archive,
1506 	    ARCHIVE_ERRNO_MISC,
1507 	    "Truncated input file (needed %jd bytes, only %jd available)",
1508 	    (intmax_t)request, (intmax_t)skipped);
1509 	return (ARCHIVE_FATAL);
1510 }
1511 
1512 /*
1513  * Advance the file pointer by the amount requested.
1514  * Returns the amount actually advanced, which may be less than the
1515  * request if EOF is encountered first.
1516  * Returns a negative value if there's an I/O error.
1517  */
1518 static int64_t
advance_file_pointer(struct archive_read_filter * filter,int64_t request)1519 advance_file_pointer(struct archive_read_filter *filter, int64_t request)
1520 {
1521 	int64_t bytes_skipped, total_bytes_skipped = 0;
1522 	ssize_t bytes_read;
1523 	size_t min;
1524 
1525 	if (filter->fatal)
1526 		return (-1);
1527 
1528 	/* Use up the copy buffer first. */
1529 	if (filter->avail > 0) {
1530 		min = (size_t)minimum(request, (int64_t)filter->avail);
1531 		filter->next += min;
1532 		filter->avail -= min;
1533 		request -= min;
1534 		filter->position += min;
1535 		total_bytes_skipped += min;
1536 	}
1537 
1538 	/* Then use up the client buffer. */
1539 	if (filter->client_avail > 0) {
1540 		min = (size_t)minimum(request, (int64_t)filter->client_avail);
1541 		filter->client_next += min;
1542 		filter->client_avail -= min;
1543 		request -= min;
1544 		filter->position += min;
1545 		total_bytes_skipped += min;
1546 	}
1547 	if (request == 0)
1548 		return (total_bytes_skipped);
1549 
1550 	/* If there's an optimized skip function, use it. */
1551 	if (filter->skip != NULL) {
1552 		bytes_skipped = (filter->skip)(filter, request);
1553 		if (bytes_skipped < 0) {	/* error */
1554 			filter->fatal = 1;
1555 			return (bytes_skipped);
1556 		}
1557 		filter->position += bytes_skipped;
1558 		total_bytes_skipped += bytes_skipped;
1559 		request -= bytes_skipped;
1560 		if (request == 0)
1561 			return (total_bytes_skipped);
1562 	}
1563 
1564 	/* Use ordinary reads as necessary to complete the request. */
1565 	for (;;) {
1566 		bytes_read = (filter->read)(filter, &filter->client_buff);
1567 		if (bytes_read < 0) {
1568 			filter->client_buff = NULL;
1569 			filter->fatal = 1;
1570 			return (bytes_read);
1571 		}
1572 
1573 		if (bytes_read == 0) {
1574 			if (filter->archive->client.cursor !=
1575 			      filter->archive->client.nodes - 1) {
1576 				if (client_switch_proxy(filter,
1577 				    filter->archive->client.cursor + 1)
1578 				    == ARCHIVE_OK)
1579 					continue;
1580 			}
1581 			filter->client_buff = NULL;
1582 			filter->end_of_file = 1;
1583 			return (total_bytes_skipped);
1584 		}
1585 
1586 		if (bytes_read >= request) {
1587 			filter->client_next =
1588 			    ((const char *)filter->client_buff) + request;
1589 			filter->client_avail = (size_t)(bytes_read - request);
1590 			filter->client_total = bytes_read;
1591 			total_bytes_skipped += request;
1592 			filter->position += request;
1593 			return (total_bytes_skipped);
1594 		}
1595 
1596 		filter->position += bytes_read;
1597 		total_bytes_skipped += bytes_read;
1598 		request -= bytes_read;
1599 	}
1600 }
1601 
1602 /**
1603  * Returns ARCHIVE_FAILED if seeking isn't supported.
1604  */
1605 int64_t
__archive_read_seek(struct archive_read * a,int64_t offset,int whence)1606 __archive_read_seek(struct archive_read *a, int64_t offset, int whence)
1607 {
1608 	return __archive_read_filter_seek(a->filter, offset, whence);
1609 }
1610 
1611 int64_t
__archive_read_filter_seek(struct archive_read_filter * filter,int64_t offset,int whence)1612 __archive_read_filter_seek(struct archive_read_filter *filter, int64_t offset,
1613     int whence)
1614 {
1615 	struct archive_read_client *client;
1616 	int64_t r;
1617 	unsigned int cursor;
1618 
1619 	if (filter->closed || filter->fatal)
1620 		return (ARCHIVE_FATAL);
1621 	if (filter->seek == NULL)
1622 		return (ARCHIVE_FAILED);
1623 
1624 	client = &(filter->archive->client);
1625 	switch (whence) {
1626 	case SEEK_CUR:
1627 		/* Adjust the offset and use SEEK_SET instead */
1628 		offset += filter->position;
1629 		/* FALLTHROUGH */
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