1 /*-
2  * Copyright (c) 2003-2007 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: src/lib/libarchive/archive_read.c,v 1.39 2008/12/06 06:45:15 kientzle Exp $");
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	build_stream(struct archive_read *);
59 static int	choose_format(struct archive_read *);
60 static struct archive_vtable *archive_read_vtable(void);
61 static int	_archive_read_close(struct archive *);
62 static int	_archive_read_finish(struct archive *);
63 
64 static struct archive_vtable *
archive_read_vtable(void)65 archive_read_vtable(void)
66 {
67 	static struct archive_vtable av;
68 	static int inited = 0;
69 
70 	if (!inited) {
71 		av.archive_finish = _archive_read_finish;
72 		av.archive_close = _archive_read_close;
73 		inited = 1;
74 	}
75 	return (&av);
76 }
77 
78 /*
79  * Allocate, initialize and return a struct archive object.
80  */
81 struct archive *
archive_read_new(void)82 archive_read_new(void)
83 {
84 	struct archive_read *a;
85 
86 	a = (struct archive_read *)malloc(sizeof(*a));
87 	if (a == NULL)
88 		return (NULL);
89 	memset(a, 0, sizeof(*a));
90 	a->archive.magic = ARCHIVE_READ_MAGIC;
91 
92 	a->archive.state = ARCHIVE_STATE_NEW;
93 	a->entry = archive_entry_new();
94 	a->archive.vtable = archive_read_vtable();
95 
96 	return (&a->archive);
97 }
98 
99 /*
100  * Record the do-not-extract-to file. This belongs in archive_read_extract.c.
101  */
102 void
archive_read_extract_set_skip_file(struct archive * _a,dev_t d,ino_t i)103 archive_read_extract_set_skip_file(struct archive *_a, dev_t d, ino_t i)
104 {
105 	struct archive_read *a = (struct archive_read *)_a;
106 	__archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_ANY,
107 	    "archive_read_extract_set_skip_file");
108 	a->skip_file_dev = d;
109 	a->skip_file_ino = i;
110 }
111 
112 /*
113  * Set read options for the format.
114  */
115 int
archive_read_set_format_options(struct archive * _a,const char * s)116 archive_read_set_format_options(struct archive *_a, const char *s)
117 {
118 	struct archive_read *a;
119 	struct archive_format_descriptor *format;
120 	char key[64], val[64];
121 	size_t i;
122 	int len, r;
123 
124 	if (s == NULL || *s == '\0')
125 		return (ARCHIVE_OK);
126 	a = (struct archive_read *)_a;
127 	__archive_check_magic(&a->archive, ARCHIVE_READ_MAGIC,
128 	    ARCHIVE_STATE_NEW, "archive_read_set_format_options");
129 	len = 0;
130 	for (i = 0; i < sizeof(a->formats)/sizeof(a->formats[0]); i++) {
131 		format = &a->formats[i];
132 		if (format == NULL || format->options == NULL ||
133 		    format->name == NULL)
134 			/* This format does not support option. */
135 			continue;
136 
137 		while ((len = __archive_parse_options(s, format->name,
138 		    sizeof(key), key, sizeof(val), val)) > 0) {
139 			if (val[0] == '\0')
140 				r = format->options(a, key, NULL);
141 			else
142 				r = format->options(a, key, val);
143 			if (r == ARCHIVE_FATAL)
144 				return (r);
145 			s += len;
146 		}
147 	}
148 	if (len < 0) {
149 		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
150 		    "Illegal format options.");
151 		return (ARCHIVE_WARN);
152 	}
153 	return (ARCHIVE_OK);
154 }
155 
156 /*
157  * Set read options for the filter.
158  */
159 int
archive_read_set_filter_options(struct archive * _a,const char * s)160 archive_read_set_filter_options(struct archive *_a, const char *s)
161 {
162 	struct archive_read *a;
163 	struct archive_read_filter *filter;
164 	struct archive_read_filter_bidder *bidder;
165 	char key[64], val[64];
166 	int len, r;
167 
168 	if (s == NULL || *s == '\0')
169 		return (ARCHIVE_OK);
170 	a = (struct archive_read *)_a;
171 	__archive_check_magic(&a->archive, ARCHIVE_READ_MAGIC,
172 	    ARCHIVE_STATE_NEW, "archive_read_set_filter_options");
173 	len = 0;
174 	for (filter = a->filter; filter != NULL; filter = filter->upstream) {
175 		bidder = filter->bidder;
176 		if (bidder == NULL)
177 			continue;
178 		if (bidder->options == NULL)
179 			/* This bidder does not support option */
180 			continue;
181 		while ((len = __archive_parse_options(s, filter->name,
182 		    sizeof(key), key, sizeof(val), val)) > 0) {
183 			if (val[0] == '\0')
184 				r = bidder->options(bidder, key, NULL);
185 			else
186 				r = bidder->options(bidder, key, val);
187 			if (r == ARCHIVE_FATAL)
188 				return (r);
189 			s += len;
190 		}
191 	}
192 	if (len < 0) {
193 		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
194 		    "Illegal format options.");
195 		return (ARCHIVE_WARN);
196 	}
197 	return (ARCHIVE_OK);
198 }
199 
200 /*
201  * Set read options for the format and the filter.
202  */
203 int
archive_read_set_options(struct archive * _a,const char * s)204 archive_read_set_options(struct archive *_a, const char *s)
205 {
206 	int r;
207 
208 	r = archive_read_set_format_options(_a, s);
209 	if (r != ARCHIVE_OK)
210 		return (r);
211 	r = archive_read_set_filter_options(_a, s);
212 	if (r != ARCHIVE_OK)
213 		return (r);
214 	return (ARCHIVE_OK);
215 }
216 
217 /*
218  * Open the archive
219  */
220 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)221 archive_read_open(struct archive *a, void *client_data,
222     archive_open_callback *client_opener, archive_read_callback *client_reader,
223     archive_close_callback *client_closer)
224 {
225 	/* Old archive_read_open() is just a thin shell around
226 	 * archive_read_open2. */
227 	return archive_read_open2(a, client_data, client_opener,
228 	    client_reader, NULL, client_closer);
229 }
230 
231 static ssize_t
client_read_proxy(struct archive_read_filter * self,const void ** buff)232 client_read_proxy(struct archive_read_filter *self, const void **buff)
233 {
234 	ssize_t r;
235 	r = (self->archive->client.reader)(&self->archive->archive,
236 	    self->data, buff);
237 	self->archive->archive.raw_position += r;
238 	return (r);
239 }
240 
241 static int64_t
client_skip_proxy(struct archive_read_filter * self,int64_t request)242 client_skip_proxy(struct archive_read_filter *self, int64_t request)
243 {
244 	int64_t r;
245 	if (self->archive->client.skipper == NULL)
246 		return (0);
247 	r = (self->archive->client.skipper)(&self->archive->archive,
248 	    self->data, request);
249 	self->archive->archive.raw_position += r;
250 	return (r);
251 }
252 
253 static int
client_close_proxy(struct archive_read_filter * self)254 client_close_proxy(struct archive_read_filter *self)
255 {
256 	int r = ARCHIVE_OK;
257 
258 	if (self->archive->client.closer != NULL)
259 		r = (self->archive->client.closer)((struct archive *)self->archive,
260 		    self->data);
261 	self->data = NULL;
262 	return (r);
263 }
264 
265 
266 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)267 archive_read_open2(struct archive *_a, void *client_data,
268     archive_open_callback *client_opener,
269     archive_read_callback *client_reader,
270     archive_skip_callback *client_skipper,
271     archive_close_callback *client_closer)
272 {
273 	struct archive_read *a = (struct archive_read *)_a;
274 	struct archive_read_filter *filter;
275 	int e;
276 
277 	__archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
278 	    "archive_read_open");
279 
280 	if (client_reader == NULL)
281 		__archive_errx(1,
282 		    "No reader function provided to archive_read_open");
283 
284 	/* Open data source. */
285 	if (client_opener != NULL) {
286 		e =(client_opener)(&a->archive, client_data);
287 		if (e != 0) {
288 			/* If the open failed, call the closer to clean up. */
289 			if (client_closer)
290 				(client_closer)(&a->archive, client_data);
291 			return (e);
292 		}
293 	}
294 
295 	/* Save the client functions and mock up the initial source. */
296 	a->client.reader = client_reader;
297 	a->client.skipper = client_skipper;
298 	a->client.closer = client_closer;
299 
300 	filter = calloc(1, sizeof(*filter));
301 	if (filter == NULL)
302 		return (ARCHIVE_FATAL);
303 	filter->bidder = NULL;
304 	filter->upstream = NULL;
305 	filter->archive = a;
306 	filter->data = client_data;
307 	filter->read = client_read_proxy;
308 	filter->skip = client_skip_proxy;
309 	filter->close = client_close_proxy;
310 	filter->name = "none";
311 	filter->code = ARCHIVE_COMPRESSION_NONE;
312 	a->filter = filter;
313 
314 	/* Build out the input pipeline. */
315 	e = build_stream(a);
316 	if (e == ARCHIVE_OK)
317 		a->archive.state = ARCHIVE_STATE_HEADER;
318 
319 	return (e);
320 }
321 
322 /*
323  * Allow each registered stream transform to bid on whether
324  * it wants to handle this stream.  Repeat until we've finished
325  * building the pipeline.
326  */
327 static int
build_stream(struct archive_read * a)328 build_stream(struct archive_read *a)
329 {
330 	int number_bidders, i, bid, best_bid;
331 	struct archive_read_filter_bidder *bidder, *best_bidder;
332 	struct archive_read_filter *filter;
333 	int r;
334 
335 	for (;;) {
336 		number_bidders = sizeof(a->bidders) / sizeof(a->bidders[0]);
337 
338 		best_bid = 0;
339 		best_bidder = NULL;
340 
341 		bidder = a->bidders;
342 		for (i = 0; i < number_bidders; i++, bidder++) {
343 			if (bidder->bid != NULL) {
344 				bid = (bidder->bid)(bidder, a->filter);
345 				if (bid > best_bid) {
346 					best_bid = bid;
347 					best_bidder = bidder;
348 				}
349 			}
350 		}
351 
352 		/* If no bidder, we're done. */
353 		if (best_bidder == NULL) {
354 			a->archive.compression_name = a->filter->name;
355 			a->archive.compression_code = a->filter->code;
356 			return (ARCHIVE_OK);
357 		}
358 
359 		filter
360 		    = (struct archive_read_filter *)calloc(1, sizeof(*filter));
361 		if (filter == NULL)
362 			return (ARCHIVE_FATAL);
363 		filter->bidder = best_bidder;
364 		filter->archive = a;
365 		filter->upstream = a->filter;
366 		r = (best_bidder->init)(filter);
367 		if (r != ARCHIVE_OK) {
368 			free(filter);
369 			return (r);
370 		}
371 		a->filter = filter;
372 	}
373 }
374 
375 /*
376  * Read header of next entry.
377  */
378 int
archive_read_next_header2(struct archive * _a,struct archive_entry * entry)379 archive_read_next_header2(struct archive *_a, struct archive_entry *entry)
380 {
381 	struct archive_read *a = (struct archive_read *)_a;
382 	int slot, ret;
383 
384 	__archive_check_magic(_a, ARCHIVE_READ_MAGIC,
385 	    ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
386 	    "archive_read_next_header");
387 
388 	archive_entry_clear(entry);
389 	archive_clear_error(&a->archive);
390 
391 	/*
392 	 * If no format has yet been chosen, choose one.
393 	 */
394 	if (a->format == NULL) {
395 		slot = choose_format(a);
396 		if (slot < 0) {
397 			a->archive.state = ARCHIVE_STATE_FATAL;
398 			return (ARCHIVE_FATAL);
399 		}
400 		a->format = &(a->formats[slot]);
401 	}
402 
403 	/*
404 	 * If client didn't consume entire data, skip any remainder
405 	 * (This is especially important for GNU incremental directories.)
406 	 */
407 	if (a->archive.state == ARCHIVE_STATE_DATA) {
408 		ret = archive_read_data_skip(&a->archive);
409 		if (ret == ARCHIVE_EOF) {
410 			archive_set_error(&a->archive, EIO, "Premature end-of-file.");
411 			a->archive.state = ARCHIVE_STATE_FATAL;
412 			return (ARCHIVE_FATAL);
413 		}
414 		if (ret != ARCHIVE_OK)
415 			return (ret);
416 	}
417 
418 	/* Record start-of-header. */
419 	a->header_position = a->archive.file_position;
420 
421 	ret = (a->format->read_header)(a, entry);
422 
423 	/*
424 	 * EOF and FATAL are persistent at this layer.  By
425 	 * modifying the state, we guarantee that future calls to
426 	 * read a header or read data will fail.
427 	 */
428 	switch (ret) {
429 	case ARCHIVE_EOF:
430 		a->archive.state = ARCHIVE_STATE_EOF;
431 		break;
432 	case ARCHIVE_OK:
433 		a->archive.state = ARCHIVE_STATE_DATA;
434 		break;
435 	case ARCHIVE_WARN:
436 		a->archive.state = ARCHIVE_STATE_DATA;
437 		break;
438 	case ARCHIVE_RETRY:
439 		break;
440 	case ARCHIVE_FATAL:
441 		a->archive.state = ARCHIVE_STATE_FATAL;
442 		break;
443 	}
444 
445 	a->read_data_output_offset = 0;
446 	a->read_data_remaining = 0;
447 	return (ret);
448 }
449 
450 int
archive_read_next_header(struct archive * _a,struct archive_entry ** entryp)451 archive_read_next_header(struct archive *_a, struct archive_entry **entryp)
452 {
453 	int ret;
454 	struct archive_read *a = (struct archive_read *)_a;
455 	*entryp = NULL;
456 	ret = archive_read_next_header2(_a, a->entry);
457 	*entryp = a->entry;
458 	return ret;
459 }
460 
461 /*
462  * Allow each registered format to bid on whether it wants to handle
463  * the next entry.  Return index of winning bidder.
464  */
465 static int
choose_format(struct archive_read * a)466 choose_format(struct archive_read *a)
467 {
468 	int slots;
469 	int i;
470 	int bid, best_bid;
471 	int best_bid_slot;
472 
473 	slots = sizeof(a->formats) / sizeof(a->formats[0]);
474 	best_bid = -1;
475 	best_bid_slot = -1;
476 
477 	/* Set up a->format for convenience of bidders. */
478 	a->format = &(a->formats[0]);
479 	for (i = 0; i < slots; i++, a->format++) {
480 		if (a->format->bid) {
481 			bid = (a->format->bid)(a);
482 			if (bid == ARCHIVE_FATAL)
483 				return (ARCHIVE_FATAL);
484 			if ((bid > best_bid) || (best_bid_slot < 0)) {
485 				best_bid = bid;
486 				best_bid_slot = i;
487 			}
488 		}
489 	}
490 
491 	/*
492 	 * There were no bidders; this is a serious programmer error
493 	 * and demands a quick and definitive abort.
494 	 */
495 	if (best_bid_slot < 0)
496 		__archive_errx(1, "No formats were registered; you must "
497 		    "invoke at least one archive_read_support_format_XXX "
498 		    "function in order to successfully read an archive.");
499 
500 	/*
501 	 * There were bidders, but no non-zero bids; this means we
502 	 * can't support this stream.
503 	 */
504 	if (best_bid < 1) {
505 		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
506 		    "Unrecognized archive format");
507 		return (ARCHIVE_FATAL);
508 	}
509 
510 	return (best_bid_slot);
511 }
512 
513 /*
514  * Return the file offset (within the uncompressed data stream) where
515  * the last header started.
516  */
517 int64_t
archive_read_header_position(struct archive * _a)518 archive_read_header_position(struct archive *_a)
519 {
520 	struct archive_read *a = (struct archive_read *)_a;
521 	__archive_check_magic(_a, ARCHIVE_READ_MAGIC,
522 	    ARCHIVE_STATE_ANY, "archive_read_header_position");
523 	return (a->header_position);
524 }
525 
526 /*
527  * Read data from an archive entry, using a read(2)-style interface.
528  * This is a convenience routine that just calls
529  * archive_read_data_block and copies the results into the client
530  * buffer, filling any gaps with zero bytes.  Clients using this
531  * API can be completely ignorant of sparse-file issues; sparse files
532  * will simply be padded with nulls.
533  *
534  * DO NOT intermingle calls to this function and archive_read_data_block
535  * to read a single entry body.
536  */
537 ssize_t
archive_read_data(struct archive * _a,void * buff,size_t s)538 archive_read_data(struct archive *_a, void *buff, size_t s)
539 {
540 	struct archive_read *a = (struct archive_read *)_a;
541 	char	*dest;
542 	const void *read_buf;
543 	size_t	 bytes_read;
544 	size_t	 len;
545 	int	 r;
546 
547 	bytes_read = 0;
548 	dest = (char *)buff;
549 
550 	while (s > 0) {
551 		if (a->read_data_remaining == 0) {
552 			read_buf = a->read_data_block;
553 			r = archive_read_data_block(&a->archive, &read_buf,
554 			    &a->read_data_remaining, &a->read_data_offset);
555 			a->read_data_block = read_buf;
556 			if (r == ARCHIVE_EOF)
557 				return (bytes_read);
558 			/*
559 			 * Error codes are all negative, so the status
560 			 * return here cannot be confused with a valid
561 			 * byte count.  (ARCHIVE_OK is zero.)
562 			 */
563 			if (r < ARCHIVE_OK)
564 				return (r);
565 		}
566 
567 		if (a->read_data_offset < a->read_data_output_offset) {
568 			archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
569 			    "Encountered out-of-order sparse blocks");
570 			return (ARCHIVE_RETRY);
571 		}
572 
573 		/* Compute the amount of zero padding needed. */
574 		if (a->read_data_output_offset + (off_t)s <
575 		    a->read_data_offset) {
576 			len = s;
577 		} else if (a->read_data_output_offset <
578 		    a->read_data_offset) {
579 			len = a->read_data_offset -
580 			    a->read_data_output_offset;
581 		} else
582 			len = 0;
583 
584 		/* Add zeroes. */
585 		memset(dest, 0, len);
586 		s -= len;
587 		a->read_data_output_offset += len;
588 		dest += len;
589 		bytes_read += len;
590 
591 		/* Copy data if there is any space left. */
592 		if (s > 0) {
593 			len = a->read_data_remaining;
594 			if (len > s)
595 				len = s;
596 			memcpy(dest, a->read_data_block, len);
597 			s -= len;
598 			a->read_data_block += len;
599 			a->read_data_remaining -= len;
600 			a->read_data_output_offset += len;
601 			a->read_data_offset += len;
602 			dest += len;
603 			bytes_read += len;
604 		}
605 	}
606 	return (bytes_read);
607 }
608 
609 #if ARCHIVE_API_VERSION < 3
610 /*
611  * Obsolete function provided for compatibility only.  Note that the API
612  * of this function doesn't allow the caller to detect if the remaining
613  * data from the archive entry is shorter than the buffer provided, or
614  * even if an error occurred while reading data.
615  */
616 int
archive_read_data_into_buffer(struct archive * a,void * d,ssize_t len)617 archive_read_data_into_buffer(struct archive *a, void *d, ssize_t len)
618 {
619 
620 	archive_read_data(a, d, len);
621 	return (ARCHIVE_OK);
622 }
623 #endif
624 
625 /*
626  * Return the amount of buffered data (data read from the client which has
627  * not yet been passed back via archive_read_data_*), or -1 if unknown.
628  */
629 ssize_t
archive_read_get_backlog(struct archive * _a)630 archive_read_get_backlog(struct archive *_a)
631 {
632 	struct archive_read *a = (struct archive_read *)_a;
633 
634 	return (a->read_data_remaining);
635 }
636 
637 /*
638  * Return the remaining length of the current archive entry, including any
639  * padding which exists in the archive format.
640  */
641 off_t
archive_read_get_entryleft(struct archive * _a)642 archive_read_get_entryleft(struct archive *_a)
643 {
644 	struct archive_read *a = (struct archive_read *)_a;
645 	off_t len;
646 
647 	if (a->format->read_get_entryleft == NULL) {
648 		archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
649 		    "Archive format does not support read_get_entryleft");
650 		return (-1);
651 	}
652 
653 	len = (a->format->read_get_entryleft)(a);
654 	if (len < 0)
655 		return (-1);
656 
657 	return (len + a->read_data_remaining);
658 }
659 
660 /*
661  * Advance the position within the archive entry.
662  */
663 int
archive_read_advance(struct archive * _a,off_t offset)664 archive_read_advance(struct archive *_a, off_t offset)
665 {
666 	struct archive_read *a = (struct archive_read *)_a;
667 
668 	if (a->read_data_remaining) {
669 		archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
670 		    "Called read_advance with read_data_remaining non-zero");
671 		return (-1);
672 	}
673 	if (a->format->read_advance == NULL) {
674 		archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
675 		    "Archive format does not support read_advance");
676 		return (-1);
677 	}
678 
679 	return ((a->format->read_advance)(a, offset));
680 }
681 
682 /*
683  * Skip over all remaining data in this entry.
684  */
685 int
archive_read_data_skip(struct archive * _a)686 archive_read_data_skip(struct archive *_a)
687 {
688 	struct archive_read *a = (struct archive_read *)_a;
689 	int r;
690 	const void *buff;
691 	size_t size;
692 	off_t offset;
693 
694 	__archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA,
695 	    "archive_read_data_skip");
696 
697 	if (a->format->read_data_skip != NULL)
698 		r = (a->format->read_data_skip)(a);
699 	else {
700 		while ((r = archive_read_data_block(&a->archive,
701 			    &buff, &size, &offset))
702 		    == ARCHIVE_OK)
703 			;
704 	}
705 
706 	if (r == ARCHIVE_EOF)
707 		r = ARCHIVE_OK;
708 
709 	a->archive.state = ARCHIVE_STATE_HEADER;
710 	return (r);
711 }
712 
713 /*
714  * Read the next block of entry data from the archive.
715  * This is a zero-copy interface; the client receives a pointer,
716  * size, and file offset of the next available block of data.
717  *
718  * Returns ARCHIVE_OK if the operation is successful, ARCHIVE_EOF if
719  * the end of entry is encountered.
720  */
721 int
archive_read_data_block(struct archive * _a,const void ** buff,size_t * size,off_t * offset)722 archive_read_data_block(struct archive *_a,
723     const void **buff, size_t *size, off_t *offset)
724 {
725 	struct archive_read *a = (struct archive_read *)_a;
726 	__archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA,
727 	    "archive_read_data_block");
728 
729 	if (a->format->read_data == NULL) {
730 		archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
731 		    "Internal error: "
732 		    "No format->read_data function registered");
733 		return (ARCHIVE_FATAL);
734 	}
735 
736 	return (a->format->read_data)(a, buff, size, offset);
737 }
738 
739 /*
740  * Close the file and release most resources.
741  *
742  * Be careful: client might just call read_new and then read_finish.
743  * Don't assume we actually read anything or performed any non-trivial
744  * initialization.
745  */
746 static int
_archive_read_close(struct archive * _a)747 _archive_read_close(struct archive *_a)
748 {
749 	struct archive_read *a = (struct archive_read *)_a;
750 	int r = ARCHIVE_OK, r1 = ARCHIVE_OK;
751 	size_t i, n;
752 
753 	__archive_check_magic(&a->archive, ARCHIVE_READ_MAGIC,
754 	    ARCHIVE_STATE_ANY, "archive_read_close");
755 	archive_clear_error(&a->archive);
756 	a->archive.state = ARCHIVE_STATE_CLOSED;
757 
758 
759 	/* Call cleanup functions registered by optional components. */
760 	if (a->cleanup_archive_extract != NULL)
761 		r = (a->cleanup_archive_extract)(a);
762 
763 	/* TODO: Clean up the formatters. */
764 
765 	/* Clean up the filter pipeline. */
766 	while (a->filter != NULL) {
767 		struct archive_read_filter *t = a->filter->upstream;
768 		if (a->filter->close != NULL) {
769 			r1 = (a->filter->close)(a->filter);
770 			if (r1 < r)
771 				r = r1;
772 		}
773 		free(a->filter->buffer);
774 		free(a->filter);
775 		a->filter = t;
776 	}
777 
778 	/* Release the bidder objects. */
779 	n = sizeof(a->bidders)/sizeof(a->bidders[0]);
780 	for (i = 0; i < n; i++) {
781 		if (a->bidders[i].free != NULL) {
782 			r1 = (a->bidders[i].free)(&a->bidders[i]);
783 			if (r1 < r)
784 				r = r1;
785 		}
786 	}
787 
788 	return (r);
789 }
790 
791 /*
792  * Release memory and other resources.
793  */
794 int
_archive_read_finish(struct archive * _a)795 _archive_read_finish(struct archive *_a)
796 {
797 	struct archive_read *a = (struct archive_read *)_a;
798 	int i;
799 	int slots;
800 	int r = ARCHIVE_OK;
801 
802 	__archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_ANY,
803 	    "archive_read_finish");
804 	if (a->archive.state != ARCHIVE_STATE_CLOSED)
805 		r = archive_read_close(&a->archive);
806 
807 	/* Cleanup format-specific data. */
808 	slots = sizeof(a->formats) / sizeof(a->formats[0]);
809 	for (i = 0; i < slots; i++) {
810 		a->format = &(a->formats[i]);
811 		if (a->formats[i].cleanup)
812 			(a->formats[i].cleanup)(a);
813 	}
814 
815 	archive_string_free(&a->archive.error_string);
816 	if (a->entry)
817 		archive_entry_free(a->entry);
818 	a->archive.magic = 0;
819 	free(a);
820 #if ARCHIVE_API_VERSION > 1
821 	return (r);
822 #endif
823 }
824 
825 /*
826  * Used internally by read format handlers to register their bid and
827  * initialization functions.
828  */
829 int
__archive_read_register_format(struct archive_read * a,void * format_data,const char * name,int (* bid)(struct archive_read *),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 *,off_t *),off_t (* read_get_entryleft)(struct archive_read *),int (* read_advance)(struct archive_read *,off_t),int (* read_data_skip)(struct archive_read *),int (* cleanup)(struct archive_read *))830 __archive_read_register_format(struct archive_read *a,
831     void *format_data,
832     const char *name,
833     int (*bid)(struct archive_read *),
834     int (*options)(struct archive_read *, const char *, const char *),
835     int (*read_header)(struct archive_read *, struct archive_entry *),
836     int (*read_data)(struct archive_read *, const void **, size_t *, off_t *),
837     off_t (*read_get_entryleft)(struct archive_read *),
838     int (*read_advance)(struct archive_read *, off_t),
839     int (*read_data_skip)(struct archive_read *),
840     int (*cleanup)(struct archive_read *))
841 {
842 	int i, number_slots;
843 
844 	__archive_check_magic(&a->archive,
845 	    ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
846 	    "__archive_read_register_format");
847 
848 	number_slots = sizeof(a->formats) / sizeof(a->formats[0]);
849 
850 	for (i = 0; i < number_slots; i++) {
851 		if (a->formats[i].bid == bid)
852 			return (ARCHIVE_WARN); /* We've already installed */
853 		if (a->formats[i].bid == NULL) {
854 			a->formats[i].bid = bid;
855 			a->formats[i].options = options;
856 			a->formats[i].read_header = read_header;
857 			a->formats[i].read_data = read_data;
858 			a->formats[i].read_get_entryleft = read_get_entryleft;
859 			a->formats[i].read_advance = read_advance;
860 			a->formats[i].read_data_skip = read_data_skip;
861 			a->formats[i].cleanup = cleanup;
862 			a->formats[i].data = format_data;
863 			a->formats[i].name = name;
864 			return (ARCHIVE_OK);
865 		}
866 	}
867 
868 	__archive_errx(1, "Not enough slots for format registration");
869 	return (ARCHIVE_FATAL); /* Never actually called. */
870 }
871 
872 /*
873  * Used internally by decompression routines to register their bid and
874  * initialization functions.
875  */
876 struct archive_read_filter_bidder *
__archive_read_get_bidder(struct archive_read * a)877 __archive_read_get_bidder(struct archive_read *a)
878 {
879 	int i, number_slots;
880 
881 	__archive_check_magic(&a->archive,
882 	    ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
883 	    "__archive_read_get_bidder");
884 
885 	number_slots = sizeof(a->bidders) / sizeof(a->bidders[0]);
886 
887 	for (i = 0; i < number_slots; i++) {
888 		if (a->bidders[i].bid == NULL) {
889 			memset(a->bidders + i, 0, sizeof(a->bidders[0]));
890 			return (a->bidders + i);
891 		}
892 	}
893 
894 	__archive_errx(1, "Not enough slots for compression registration");
895 	return (NULL); /* Never actually executed. */
896 }
897 
898 /*
899  * The next three functions comprise the peek/consume internal I/O
900  * system used by archive format readers.  This system allows fairly
901  * flexible read-ahead and allows the I/O code to operate in a
902  * zero-copy manner most of the time.
903  *
904  * In the ideal case, filters generate blocks of data
905  * and __archive_read_ahead() just returns pointers directly into
906  * those blocks.  Then __archive_read_consume() just bumps those
907  * pointers.  Only if your request would span blocks does the I/O
908  * layer use a copy buffer to provide you with a contiguous block of
909  * data.  The __archive_read_skip() is an optimization; it scans ahead
910  * very quickly (it usually translates into a seek() operation if
911  * you're reading uncompressed disk files).
912  *
913  * A couple of useful idioms:
914  *  * "I just want some data."  Ask for 1 byte and pay attention to
915  *    the "number of bytes available" from __archive_read_ahead().
916  *    You can consume more than you asked for; you just can't consume
917  *    more than is available.  If you consume everything that's
918  *    immediately available, the next read_ahead() call will pull
919  *    the next block.
920  *  * "I want to output a large block of data."  As above, ask for 1 byte,
921  *    emit all that's available (up to whatever limit you have), then
922  *    repeat until you're done.
923  *  * "I want to peek ahead by a large amount."  Ask for 4k or so, then
924  *    double and repeat until you get an error or have enough.  Note
925  *    that the I/O layer will likely end up expanding its copy buffer
926  *    to fit your request, so use this technique cautiously.  This
927  *    technique is used, for example, by some of the format tasting
928  *    code that has uncertain look-ahead needs.
929  *
930  * TODO: Someday, provide a more generic __archive_read_seek() for
931  * those cases where it's useful.  This is tricky because there are lots
932  * of cases where seek() is not available (reading gzip data from a
933  * network socket, for instance), so there needs to be a good way to
934  * communicate whether seek() is available and users of that interface
935  * need to use non-seeking strategies whenever seek() is not available.
936  */
937 
938 /*
939  * Looks ahead in the input stream:
940  *  * If 'avail' pointer is provided, that returns number of bytes available
941  *    in the current buffer, which may be much larger than requested.
942  *  * If end-of-file, *avail gets set to zero.
943  *  * If error, *avail gets error code.
944  *  * If request can be met, returns pointer to data, returns NULL
945  *    if request is not met.
946  *
947  * Note: If you just want "some data", ask for 1 byte and pay attention
948  * to *avail, which will have the actual amount available.  If you
949  * know exactly how many bytes you need, just ask for that and treat
950  * a NULL return as an error.
951  *
952  * Important:  This does NOT move the file pointer.  See
953  * __archive_read_consume() below.
954  */
955 
956 /*
957  * This is tricky.  We need to provide our clients with pointers to
958  * contiguous blocks of memory but we want to avoid copying whenever
959  * possible.
960  *
961  * Mostly, this code returns pointers directly into the block of data
962  * provided by the client's read routine.  It can do this unless the
963  * request would split across blocks.  In that case, we have to copy
964  * into an internal buffer to combine reads.
965  */
966 const void *
__archive_read_ahead(struct archive_read * a,size_t min,ssize_t * avail)967 __archive_read_ahead(struct archive_read *a, size_t min, ssize_t *avail)
968 {
969 	return (__archive_read_filter_ahead(a->filter, min, avail));
970 }
971 
972 const void *
__archive_read_filter_ahead(struct archive_read_filter * filter,size_t min,ssize_t * avail)973 __archive_read_filter_ahead(struct archive_read_filter *filter,
974     size_t min, ssize_t *avail)
975 {
976 	ssize_t bytes_read;
977 	size_t tocopy;
978 
979 	if (filter->fatal) {
980 		if (avail)
981 			*avail = ARCHIVE_FATAL;
982 		return (NULL);
983 	}
984 
985 	/*
986 	 * Keep pulling more data until we can satisfy the request.
987 	 */
988 	for (;;) {
989 
990 		/*
991 		 * If we can satisfy from the copy buffer, we're done.
992 		 */
993 		if (filter->avail >= min) {
994 			if (avail != NULL)
995 				*avail = filter->avail;
996 			return (filter->next);
997 		}
998 
999 		/*
1000 		 * We can satisfy directly from client buffer if everything
1001 		 * currently in the copy buffer is still in the client buffer.
1002 		 */
1003 		if (filter->client_total >= filter->client_avail + filter->avail
1004 		    && filter->client_avail + filter->avail >= min) {
1005 			/* "Roll back" to client buffer. */
1006 			filter->client_avail += filter->avail;
1007 			filter->client_next -= filter->avail;
1008 			/* Copy buffer is now empty. */
1009 			filter->avail = 0;
1010 			filter->next = filter->buffer;
1011 			/* Return data from client buffer. */
1012 			if (avail != NULL)
1013 				*avail = filter->client_avail;
1014 			return (filter->client_next);
1015 		}
1016 
1017 		/* Move data forward in copy buffer if necessary. */
1018 		if (filter->next > filter->buffer &&
1019 		    filter->next + min > filter->buffer + filter->buffer_size) {
1020 			if (filter->avail > 0)
1021 				memmove(filter->buffer, filter->next, filter->avail);
1022 			filter->next = filter->buffer;
1023 		}
1024 
1025 		/* If we've used up the client data, get more. */
1026 		if (filter->client_avail <= 0) {
1027 			if (filter->end_of_file) {
1028 				if (avail != NULL)
1029 					*avail = 0;
1030 				return (NULL);
1031 			}
1032 			bytes_read = (filter->read)(filter,
1033 			    &filter->client_buff);
1034 			if (bytes_read < 0) {		/* Read error. */
1035 				filter->client_total = filter->client_avail = 0;
1036 				filter->client_next = filter->client_buff = NULL;
1037 				filter->fatal = 1;
1038 				if (avail != NULL)
1039 					*avail = ARCHIVE_FATAL;
1040 				return (NULL);
1041 			}
1042 			if (bytes_read == 0) {	/* Premature end-of-file. */
1043 				filter->client_total = filter->client_avail = 0;
1044 				filter->client_next = filter->client_buff = NULL;
1045 				filter->end_of_file = 1;
1046 				/* Return whatever we do have. */
1047 				if (avail != NULL)
1048 					*avail = filter->avail;
1049 				return (NULL);
1050 			}
1051 			filter->position += bytes_read;
1052 			filter->client_total = bytes_read;
1053 			filter->client_avail = filter->client_total;
1054 			filter->client_next = filter->client_buff;
1055 		}
1056 		else
1057 		{
1058 			/*
1059 			 * We can't satisfy the request from the copy
1060 			 * buffer or the existing client data, so we
1061 			 * need to copy more client data over to the
1062 			 * copy buffer.
1063 			 */
1064 
1065 			/* Ensure the buffer is big enough. */
1066 			if (min > filter->buffer_size) {
1067 				size_t s, t;
1068 				char *p;
1069 
1070 				/* Double the buffer; watch for overflow. */
1071 				s = t = filter->buffer_size;
1072 				if (s == 0)
1073 					s = min;
1074 				while (s < min) {
1075 					t *= 2;
1076 					if (t <= s) { /* Integer overflow! */
1077 						archive_set_error(
1078 							&filter->archive->archive,
1079 							ENOMEM,
1080 						    "Unable to allocate copy buffer");
1081 						filter->fatal = 1;
1082 						if (avail != NULL)
1083 							*avail = ARCHIVE_FATAL;
1084 						return (NULL);
1085 					}
1086 					s = t;
1087 				}
1088 				/* Now s >= min, so allocate a new buffer. */
1089 				p = (char *)malloc(s);
1090 				if (p == NULL) {
1091 					archive_set_error(
1092 						&filter->archive->archive,
1093 						ENOMEM,
1094 					    "Unable to allocate copy buffer");
1095 					filter->fatal = 1;
1096 					if (avail != NULL)
1097 						*avail = ARCHIVE_FATAL;
1098 					return (NULL);
1099 				}
1100 				/* Move data into newly-enlarged buffer. */
1101 				if (filter->avail > 0)
1102 					memmove(p, filter->next, filter->avail);
1103 				free(filter->buffer);
1104 				filter->next = filter->buffer = p;
1105 				filter->buffer_size = s;
1106 			}
1107 
1108 			/* We can add client data to copy buffer. */
1109 			/* First estimate: copy to fill rest of buffer. */
1110 			tocopy = (filter->buffer + filter->buffer_size)
1111 			    - (filter->next + filter->avail);
1112 			/* Don't waste time buffering more than we need to. */
1113 			if (tocopy + filter->avail > min)
1114 				tocopy = min - filter->avail;
1115 			/* Don't copy more than is available. */
1116 			if (tocopy > filter->client_avail)
1117 				tocopy = filter->client_avail;
1118 
1119 			memcpy(filter->next + filter->avail, filter->client_next,
1120 			    tocopy);
1121 			/* Remove this data from client buffer. */
1122 			filter->client_next += tocopy;
1123 			filter->client_avail -= tocopy;
1124 			/* add it to copy buffer. */
1125 			filter->avail += tocopy;
1126 		}
1127 	}
1128 }
1129 
1130 /*
1131  * Move the file pointer forward.  This should be called after
1132  * __archive_read_ahead() returns data to you.  Don't try to move
1133  * ahead by more than the amount of data available according to
1134  * __archive_read_ahead().
1135  */
1136 /*
1137  * Mark the appropriate data as used.  Note that the request here will
1138  * often be much smaller than the size of the previous read_ahead
1139  * request.
1140  */
1141 ssize_t
__archive_read_consume(struct archive_read * a,size_t request)1142 __archive_read_consume(struct archive_read *a, size_t request)
1143 {
1144 	ssize_t r;
1145 	r = __archive_read_filter_consume(a->filter, request);
1146 	a->archive.file_position += r;
1147 	return (r);
1148 }
1149 
1150 ssize_t
__archive_read_filter_consume(struct archive_read_filter * filter,size_t request)1151 __archive_read_filter_consume(struct archive_read_filter * filter,
1152     size_t request)
1153 {
1154 	if (filter->avail > 0) {
1155 		/* Read came from copy buffer. */
1156 		filter->next += request;
1157 		filter->avail -= request;
1158 	} else {
1159 		/* Read came from client buffer. */
1160 		filter->client_next += request;
1161 		filter->client_avail -= request;
1162 	}
1163 	return (request);
1164 }
1165 
1166 /*
1167  * Move the file pointer ahead by an arbitrary amount.  If you're
1168  * reading uncompressed data from a disk file, this will actually
1169  * translate into a seek() operation.  Even in cases where seek()
1170  * isn't feasible, this at least pushes the read-and-discard loop
1171  * down closer to the data source.
1172  */
1173 int64_t
__archive_read_skip(struct archive_read * a,int64_t request)1174 __archive_read_skip(struct archive_read *a, int64_t request)
1175 {
1176 	return (__archive_read_filter_skip(a->filter, request));
1177 }
1178 
1179 int64_t
__archive_read_filter_skip(struct archive_read_filter * filter,int64_t request)1180 __archive_read_filter_skip(struct archive_read_filter *filter, int64_t request)
1181 {
1182 	off_t bytes_skipped, total_bytes_skipped = 0;
1183 	size_t min;
1184 
1185 	if (filter->fatal)
1186 		return (-1);
1187 	/*
1188 	 * If there is data in the buffers already, use that first.
1189 	 */
1190 	if (filter->avail > 0) {
1191 		min = minimum(request, (off_t)filter->avail);
1192 		bytes_skipped = __archive_read_consume(filter->archive, min);
1193 		request -= bytes_skipped;
1194 		total_bytes_skipped += bytes_skipped;
1195 	}
1196 	if (filter->client_avail > 0) {
1197 		min = minimum(request, (off_t)filter->client_avail);
1198 		bytes_skipped = __archive_read_consume(filter->archive, min);
1199 		request -= bytes_skipped;
1200 		total_bytes_skipped += bytes_skipped;
1201 	}
1202 	if (request == 0)
1203 		return (total_bytes_skipped);
1204 	/*
1205 	 * If a client_skipper was provided, try that first.
1206 	 */
1207 #if ARCHIVE_API_VERSION < 2
1208 	if ((filter->skip != NULL) && (request < SSIZE_MAX)) {
1209 #else
1210 	if (filter->skip != NULL) {
1211 #endif
1212 		bytes_skipped = (filter->skip)(filter, request);
1213 		if (bytes_skipped < 0) {	/* error */
1214 			filter->client_total = filter->client_avail = 0;
1215 			filter->client_next = filter->client_buff = NULL;
1216 			filter->fatal = 1;
1217 			return (bytes_skipped);
1218 		}
1219 		filter->archive->archive.file_position += bytes_skipped;
1220 		total_bytes_skipped += bytes_skipped;
1221 		request -= bytes_skipped;
1222 		filter->client_next = filter->client_buff;
1223 		filter->client_avail = filter->client_total = 0;
1224 	}
1225 	/*
1226 	 * Note that client_skipper will usually not satisfy the
1227 	 * full request (due to low-level blocking concerns),
1228 	 * so even if client_skipper is provided, we may still
1229 	 * have to use ordinary reads to finish out the request.
1230 	 */
1231 	while (request > 0) {
1232 		ssize_t bytes_read;
1233 		(void)__archive_read_ahead(filter->archive, 1, &bytes_read);
1234 		if (bytes_read < 0)
1235 			return (bytes_read);
1236 		if (bytes_read == 0) {
1237 			/* We hit EOF before we satisfied the skip request. */
1238 			archive_set_error(&filter->archive->archive,
1239 			    ARCHIVE_ERRNO_MISC,
1240 			    "Truncated input file (need to skip %jd bytes)",
1241 			    (intmax_t)request);
1242 			return (ARCHIVE_FATAL);
1243 		}
1244 		min = (size_t)(minimum(bytes_read, request));
1245 		bytes_read = __archive_read_consume(filter->archive, min);
1246 		total_bytes_skipped += bytes_read;
1247 		request -= bytes_read;
1248 	}
1249 	return (total_bytes_skipped);
1250 }
1251