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.38 2008/03/12 04:58:32 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 static void	choose_decompressor(struct archive_read *, const void*, size_t);
57 static int	choose_format(struct archive_read *);
58 static off_t	dummy_skip(struct archive_read *, off_t);
59 
60 /*
61  * Allocate, initialize and return a struct archive object.
62  */
63 struct archive *
64 archive_read_new(void)
65 {
66 	struct archive_read *a;
67 
68 	a = (struct archive_read *)malloc(sizeof(*a));
69 	if (a == NULL)
70 		return (NULL);
71 	memset(a, 0, sizeof(*a));
72 	a->archive.magic = ARCHIVE_READ_MAGIC;
73 
74 	a->archive.state = ARCHIVE_STATE_NEW;
75 	a->entry = archive_entry_new();
76 
77 	/* We always support uncompressed archives. */
78 	archive_read_support_compression_none(&a->archive);
79 
80 	return (&a->archive);
81 }
82 
83 /*
84  * Record the do-not-extract-to file. This belongs in archive_read_extract.c.
85  */
86 void
87 archive_read_extract_set_skip_file(struct archive *_a, dev_t d, ino_t i)
88 {
89 	struct archive_read *a = (struct archive_read *)_a;
90 	__archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_ANY,
91 	    "archive_read_extract_set_skip_file");
92 	a->skip_file_dev = d;
93 	a->skip_file_ino = i;
94 }
95 
96 
97 /*
98  * Open the archive
99  */
100 int
101 archive_read_open(struct archive *a, void *client_data,
102     archive_open_callback *client_opener, archive_read_callback *client_reader,
103     archive_close_callback *client_closer)
104 {
105 	/* Old archive_read_open() is just a thin shell around
106 	 * archive_read_open2. */
107 	return archive_read_open2(a, client_data, client_opener,
108 	    client_reader, NULL, client_closer);
109 }
110 
111 int
112 archive_read_open2(struct archive *_a, void *client_data,
113     archive_open_callback *client_opener,
114     archive_read_callback *client_reader,
115     archive_skip_callback *client_skipper,
116     archive_close_callback *client_closer)
117 {
118 	struct archive_read *a = (struct archive_read *)_a;
119 	const void *buffer;
120 	ssize_t bytes_read;
121 	int e;
122 
123 	__archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW, "archive_read_open");
124 
125 	if (client_reader == NULL)
126 		__archive_errx(1,
127 		    "No reader function provided to archive_read_open");
128 
129 	/*
130 	 * Set these NULL initially.  If the open or initial read fails,
131 	 * we'll leave them NULL to indicate that the file is invalid.
132 	 * (In particular, this helps ensure that the closer doesn't
133 	 * get called more than once.)
134 	 */
135 	a->client_opener = NULL;
136 	a->client_reader = NULL;
137 	a->client_skipper = NULL;
138 	a->client_closer = NULL;
139 	a->client_data = NULL;
140 
141 	/* Open data source. */
142 	if (client_opener != NULL) {
143 		e =(client_opener)(&a->archive, client_data);
144 		if (e != 0) {
145 			/* If the open failed, call the closer to clean up. */
146 			if (client_closer)
147 				(client_closer)(&a->archive, client_data);
148 			return (e);
149 		}
150 	}
151 
152 	/* Read first block now for compress format detection. */
153 	bytes_read = (client_reader)(&a->archive, client_data, &buffer);
154 
155 	if (bytes_read < 0) {
156 		/* If the first read fails, close before returning error. */
157 		if (client_closer)
158 			(client_closer)(&a->archive, client_data);
159 		/* client_reader should have already set error information. */
160 		return (ARCHIVE_FATAL);
161 	}
162 
163 	/* Now that the client callbacks have worked, remember them. */
164 	a->client_opener = client_opener; /* Do we need to remember this? */
165 	a->client_reader = client_reader;
166 	a->client_skipper = client_skipper;
167 	a->client_closer = client_closer;
168 	a->client_data = client_data;
169 
170 	/* Select a decompression routine. */
171 	choose_decompressor(a, buffer, (size_t)bytes_read);
172 	if (a->decompressor == NULL)
173 		return (ARCHIVE_FATAL);
174 
175 	/* Initialize decompression routine with the first block of data. */
176 	e = (a->decompressor->init)(a, buffer, (size_t)bytes_read);
177 
178 	if (e == ARCHIVE_OK)
179 		a->archive.state = ARCHIVE_STATE_HEADER;
180 
181 	/*
182 	 * If the decompressor didn't register a skip function, provide a
183 	 * dummy compression-layer skip function.
184 	 */
185 	if (a->decompressor->skip == NULL)
186 		a->decompressor->skip = dummy_skip;
187 
188 	return (e);
189 }
190 
191 /*
192  * Allow each registered decompression routine to bid on whether it
193  * wants to handle this stream.  Return index of winning bidder.
194  */
195 static void
196 choose_decompressor(struct archive_read *a,
197     const void *buffer, size_t bytes_read)
198 {
199 	int decompression_slots, i, bid, best_bid;
200 	struct decompressor_t *decompressor, *best_decompressor;
201 
202 	decompression_slots = sizeof(a->decompressors) /
203 	    sizeof(a->decompressors[0]);
204 
205 	best_bid = 0;
206 	a->decompressor = NULL;
207 	best_decompressor = NULL;
208 
209 	decompressor = a->decompressors;
210 	for (i = 0; i < decompression_slots; i++) {
211 		if (decompressor->bid) {
212 			bid = (decompressor->bid)(buffer, bytes_read);
213 			if (bid > best_bid || best_decompressor == NULL) {
214 				best_bid = bid;
215 				best_decompressor = decompressor;
216 			}
217 		}
218 		decompressor ++;
219 	}
220 
221 	/*
222 	 * There were no bidders; this is a serious programmer error
223 	 * and demands a quick and definitive abort.
224 	 */
225 	if (best_decompressor == NULL)
226 		__archive_errx(1, "No decompressors were registered; you "
227 		    "must call at least one "
228 		    "archive_read_support_compression_XXX function in order "
229 		    "to successfully read an archive.");
230 
231 	/*
232 	 * There were bidders, but no non-zero bids; this means we  can't
233 	 * support this stream.
234 	 */
235 	if (best_bid < 1) {
236 		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
237 		    "Unrecognized archive format");
238 		return;
239 	}
240 
241 	/* Record the best decompressor for this stream. */
242 	a->decompressor = best_decompressor;
243 }
244 
245 /*
246  * Dummy skip function, for use if the compression layer doesn't provide
247  * one: This code just reads data and discards it.
248  */
249 static off_t
250 dummy_skip(struct archive_read * a, off_t request)
251 {
252 	const void * dummy_buffer;
253 	ssize_t bytes_read;
254 	off_t bytes_skipped;
255 
256 	for (bytes_skipped = 0; request > 0;) {
257 		bytes_read = (a->decompressor->read_ahead)(a, &dummy_buffer, 1);
258 		if (bytes_read < 0)
259 			return (bytes_read);
260 		if (bytes_read == 0) {
261 			/* Premature EOF. */
262 			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
263 			    "Truncated input file (need to skip %jd bytes)",
264 			    (intmax_t)request);
265 			return (ARCHIVE_FATAL);
266 		}
267 		if (bytes_read > request)
268 			bytes_read = (ssize_t)request;
269 		(a->decompressor->consume)(a, (size_t)bytes_read);
270 		request -= bytes_read;
271 		bytes_skipped += bytes_read;
272 	}
273 
274 	return (bytes_skipped);
275 }
276 
277 /*
278  * Read header of next entry.
279  */
280 int
281 archive_read_next_header(struct archive *_a, struct archive_entry **entryp)
282 {
283 	struct archive_read *a = (struct archive_read *)_a;
284 	struct archive_entry *entry;
285 	int slot, ret;
286 
287 	__archive_check_magic(_a, ARCHIVE_READ_MAGIC,
288 	    ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
289 	    "archive_read_next_header");
290 
291 	*entryp = NULL;
292 	entry = a->entry;
293 	archive_entry_clear(entry);
294 	archive_clear_error(&a->archive);
295 
296 	/*
297 	 * If no format has yet been chosen, choose one.
298 	 */
299 	if (a->format == NULL) {
300 		slot = choose_format(a);
301 		if (slot < 0) {
302 			a->archive.state = ARCHIVE_STATE_FATAL;
303 			return (ARCHIVE_FATAL);
304 		}
305 		a->format = &(a->formats[slot]);
306 	}
307 
308 	/*
309 	 * If client didn't consume entire data, skip any remainder
310 	 * (This is especially important for GNU incremental directories.)
311 	 */
312 	if (a->archive.state == ARCHIVE_STATE_DATA) {
313 		ret = archive_read_data_skip(&a->archive);
314 		if (ret == ARCHIVE_EOF) {
315 			archive_set_error(&a->archive, EIO, "Premature end-of-file.");
316 			a->archive.state = ARCHIVE_STATE_FATAL;
317 			return (ARCHIVE_FATAL);
318 		}
319 		if (ret != ARCHIVE_OK)
320 			return (ret);
321 	}
322 
323 	/* Record start-of-header. */
324 	a->header_position = a->archive.file_position;
325 
326 	ret = (a->format->read_header)(a, entry);
327 
328 	/*
329 	 * EOF and FATAL are persistent at this layer.  By
330 	 * modifying the state, we guarantee that future calls to
331 	 * read a header or read data will fail.
332 	 */
333 	switch (ret) {
334 	case ARCHIVE_EOF:
335 		a->archive.state = ARCHIVE_STATE_EOF;
336 		break;
337 	case ARCHIVE_OK:
338 		a->archive.state = ARCHIVE_STATE_DATA;
339 		break;
340 	case ARCHIVE_WARN:
341 		a->archive.state = ARCHIVE_STATE_DATA;
342 		break;
343 	case ARCHIVE_RETRY:
344 		break;
345 	case ARCHIVE_FATAL:
346 		a->archive.state = ARCHIVE_STATE_FATAL;
347 		break;
348 	}
349 
350 	*entryp = entry;
351 	a->read_data_output_offset = 0;
352 	a->read_data_remaining = 0;
353 	return (ret);
354 }
355 
356 /*
357  * Allow each registered format to bid on whether it wants to handle
358  * the next entry.  Return index of winning bidder.
359  */
360 static int
361 choose_format(struct archive_read *a)
362 {
363 	int slots;
364 	int i;
365 	int bid, best_bid;
366 	int best_bid_slot;
367 
368 	slots = sizeof(a->formats) / sizeof(a->formats[0]);
369 	best_bid = -1;
370 	best_bid_slot = -1;
371 
372 	/* Set up a->format and a->pformat_data for convenience of bidders. */
373 	a->format = &(a->formats[0]);
374 	for (i = 0; i < slots; i++, a->format++) {
375 		if (a->format->bid) {
376 			bid = (a->format->bid)(a);
377 			if (bid == ARCHIVE_FATAL)
378 				return (ARCHIVE_FATAL);
379 			if ((bid > best_bid) || (best_bid_slot < 0)) {
380 				best_bid = bid;
381 				best_bid_slot = i;
382 			}
383 		}
384 	}
385 
386 	/*
387 	 * There were no bidders; this is a serious programmer error
388 	 * and demands a quick and definitive abort.
389 	 */
390 	if (best_bid_slot < 0)
391 		__archive_errx(1, "No formats were registered; you must "
392 		    "invoke at least one archive_read_support_format_XXX "
393 		    "function in order to successfully read an archive.");
394 
395 	/*
396 	 * There were bidders, but no non-zero bids; this means we
397 	 * can't support this stream.
398 	 */
399 	if (best_bid < 1) {
400 		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
401 		    "Unrecognized archive format");
402 		return (ARCHIVE_FATAL);
403 	}
404 
405 	return (best_bid_slot);
406 }
407 
408 /*
409  * Return the file offset (within the uncompressed data stream) where
410  * the last header started.
411  */
412 int64_t
413 archive_read_header_position(struct archive *_a)
414 {
415 	struct archive_read *a = (struct archive_read *)_a;
416 	__archive_check_magic(_a, ARCHIVE_READ_MAGIC,
417 	    ARCHIVE_STATE_ANY, "archive_read_header_position");
418 	return (a->header_position);
419 }
420 
421 /*
422  * Read data from an archive entry, using a read(2)-style interface.
423  * This is a convenience routine that just calls
424  * archive_read_data_block and copies the results into the client
425  * buffer, filling any gaps with zero bytes.  Clients using this
426  * API can be completely ignorant of sparse-file issues; sparse files
427  * will simply be padded with nulls.
428  *
429  * DO NOT intermingle calls to this function and archive_read_data_block
430  * to read a single entry body.
431  */
432 ssize_t
433 archive_read_data(struct archive *_a, void *buff, size_t s)
434 {
435 	struct archive_read *a = (struct archive_read *)_a;
436 	char	*dest;
437 	const void *read_buf;
438 	size_t	 bytes_read;
439 	size_t	 len;
440 	int	 r;
441 
442 	bytes_read = 0;
443 	dest = (char *)buff;
444 
445 	while (s > 0) {
446 		if (a->read_data_remaining == 0) {
447 			read_buf = a->read_data_block;
448 			r = archive_read_data_block(&a->archive, &read_buf,
449 			    &a->read_data_remaining, &a->read_data_offset);
450 			a->read_data_block = read_buf;
451 			if (r == ARCHIVE_EOF)
452 				return (bytes_read);
453 			/*
454 			 * Error codes are all negative, so the status
455 			 * return here cannot be confused with a valid
456 			 * byte count.  (ARCHIVE_OK is zero.)
457 			 */
458 			if (r < ARCHIVE_OK)
459 				return (r);
460 		}
461 
462 		if (a->read_data_offset < a->read_data_output_offset) {
463 			archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
464 			    "Encountered out-of-order sparse blocks");
465 			return (ARCHIVE_RETRY);
466 		}
467 
468 		/* Compute the amount of zero padding needed. */
469 		if (a->read_data_output_offset + (off_t)s <
470 		    a->read_data_offset) {
471 			len = s;
472 		} else if (a->read_data_output_offset <
473 		    a->read_data_offset) {
474 			len = a->read_data_offset -
475 			    a->read_data_output_offset;
476 		} else
477 			len = 0;
478 
479 		/* Add zeroes. */
480 		memset(dest, 0, len);
481 		s -= len;
482 		a->read_data_output_offset += len;
483 		dest += len;
484 		bytes_read += len;
485 
486 		/* Copy data if there is any space left. */
487 		if (s > 0) {
488 			len = a->read_data_remaining;
489 			if (len > s)
490 				len = s;
491 			memcpy(dest, a->read_data_block, len);
492 			s -= len;
493 			a->read_data_block += len;
494 			a->read_data_remaining -= len;
495 			a->read_data_output_offset += len;
496 			a->read_data_offset += len;
497 			dest += len;
498 			bytes_read += len;
499 		}
500 	}
501 	return (bytes_read);
502 }
503 
504 #if ARCHIVE_API_VERSION < 3
505 /*
506  * Obsolete function provided for compatibility only.  Note that the API
507  * of this function doesn't allow the caller to detect if the remaining
508  * data from the archive entry is shorter than the buffer provided, or
509  * even if an error occurred while reading data.
510  */
511 int
512 archive_read_data_into_buffer(struct archive *a, void *d, ssize_t len)
513 {
514 
515 	archive_read_data(a, d, len);
516 	return (ARCHIVE_OK);
517 }
518 #endif
519 
520 /*
521  * Skip over all remaining data in this entry.
522  */
523 int
524 archive_read_data_skip(struct archive *_a)
525 {
526 	struct archive_read *a = (struct archive_read *)_a;
527 	int r;
528 	const void *buff;
529 	size_t size;
530 	off_t offset;
531 
532 	__archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA,
533 	    "archive_read_data_skip");
534 
535 	if (a->format->read_data_skip != NULL)
536 		r = (a->format->read_data_skip)(a);
537 	else {
538 		while ((r = archive_read_data_block(&a->archive,
539 			    &buff, &size, &offset))
540 		    == ARCHIVE_OK)
541 			;
542 	}
543 
544 	if (r == ARCHIVE_EOF)
545 		r = ARCHIVE_OK;
546 
547 	a->archive.state = ARCHIVE_STATE_HEADER;
548 	return (r);
549 }
550 
551 /*
552  * Read the next block of entry data from the archive.
553  * This is a zero-copy interface; the client receives a pointer,
554  * size, and file offset of the next available block of data.
555  *
556  * Returns ARCHIVE_OK if the operation is successful, ARCHIVE_EOF if
557  * the end of entry is encountered.
558  */
559 int
560 archive_read_data_block(struct archive *_a,
561     const void **buff, size_t *size, off_t *offset)
562 {
563 	struct archive_read *a = (struct archive_read *)_a;
564 	__archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA,
565 	    "archive_read_data_block");
566 
567 	if (a->format->read_data == NULL) {
568 		archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
569 		    "Internal error: "
570 		    "No format_read_data_block function registered");
571 		return (ARCHIVE_FATAL);
572 	}
573 
574 	return (a->format->read_data)(a, buff, size, offset);
575 }
576 
577 /*
578  * Close the file and release most resources.
579  *
580  * Be careful: client might just call read_new and then read_finish.
581  * Don't assume we actually read anything or performed any non-trivial
582  * initialization.
583  */
584 int
585 archive_read_close(struct archive *_a)
586 {
587 	struct archive_read *a = (struct archive_read *)_a;
588 	int r = ARCHIVE_OK, r1 = ARCHIVE_OK;
589 	size_t i, n;
590 
591 	__archive_check_magic(&a->archive, ARCHIVE_READ_MAGIC,
592 	    ARCHIVE_STATE_ANY, "archive_read_close");
593 	a->archive.state = ARCHIVE_STATE_CLOSED;
594 
595 	/* Call cleanup functions registered by optional components. */
596 	if (a->cleanup_archive_extract != NULL)
597 		r = (a->cleanup_archive_extract)(a);
598 
599 	/* TODO: Clean up the formatters. */
600 
601 	/* Clean up the decompressors. */
602 	n = sizeof(a->decompressors)/sizeof(a->decompressors[0]);
603 	for (i = 0; i < n; i++) {
604 		if (a->decompressors[i].finish != NULL) {
605 			r1 = (a->decompressors[i].finish)(a);
606 			if (r1 < r)
607 				r = r1;
608 		}
609 	}
610 
611 	/* Close the client stream. */
612 	if (a->client_closer != NULL) {
613 		r1 = ((a->client_closer)(&a->archive, a->client_data));
614 		if (r1 < r)
615 			r = r1;
616 	}
617 
618 	return (r);
619 }
620 
621 /*
622  * Release memory and other resources.
623  */
624 #if ARCHIVE_API_VERSION > 1
625 int
626 #else
627 /* Temporarily allow library to compile with either 1.x or 2.0 API. */
628 void
629 #endif
630 archive_read_finish(struct archive *_a)
631 {
632 	struct archive_read *a = (struct archive_read *)_a;
633 	int i;
634 	int slots;
635 	int r = ARCHIVE_OK;
636 
637 	__archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_ANY,
638 	    "archive_read_finish");
639 	if (a->archive.state != ARCHIVE_STATE_CLOSED)
640 		r = archive_read_close(&a->archive);
641 
642 	/* Cleanup format-specific data. */
643 	slots = sizeof(a->formats) / sizeof(a->formats[0]);
644 	for (i = 0; i < slots; i++) {
645 		a->format = &(a->formats[i]);
646 		if (a->formats[i].cleanup)
647 			(a->formats[i].cleanup)(a);
648 	}
649 
650 	archive_string_free(&a->archive.error_string);
651 	if (a->entry)
652 		archive_entry_free(a->entry);
653 	a->archive.magic = 0;
654 	free(a);
655 #if ARCHIVE_API_VERSION > 1
656 	return (r);
657 #endif
658 }
659 
660 /*
661  * Used internally by read format handlers to register their bid and
662  * initialization functions.
663  */
664 int
665 __archive_read_register_format(struct archive_read *a,
666     void *format_data,
667     int (*bid)(struct archive_read *),
668     int (*read_header)(struct archive_read *, struct archive_entry *),
669     int (*read_data)(struct archive_read *, const void **, size_t *, off_t *),
670     int (*read_data_skip)(struct archive_read *),
671     int (*cleanup)(struct archive_read *))
672 {
673 	int i, number_slots;
674 
675 	__archive_check_magic(&a->archive,
676 	    ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
677 	    "__archive_read_register_format");
678 
679 	number_slots = sizeof(a->formats) / sizeof(a->formats[0]);
680 
681 	for (i = 0; i < number_slots; i++) {
682 		if (a->formats[i].bid == bid)
683 			return (ARCHIVE_WARN); /* We've already installed */
684 		if (a->formats[i].bid == NULL) {
685 			a->formats[i].bid = bid;
686 			a->formats[i].read_header = read_header;
687 			a->formats[i].read_data = read_data;
688 			a->formats[i].read_data_skip = read_data_skip;
689 			a->formats[i].cleanup = cleanup;
690 			a->formats[i].data = format_data;
691 			return (ARCHIVE_OK);
692 		}
693 	}
694 
695 	__archive_errx(1, "Not enough slots for format registration");
696 	return (ARCHIVE_FATAL); /* Never actually called. */
697 }
698 
699 /*
700  * Used internally by decompression routines to register their bid and
701  * initialization functions.
702  */
703 struct decompressor_t *
704 __archive_read_register_compression(struct archive_read *a,
705     int (*bid)(const void *, size_t),
706     int (*init)(struct archive_read *, const void *, size_t))
707 {
708 	int i, number_slots;
709 
710 	__archive_check_magic(&a->archive,
711 	    ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
712 	    "__archive_read_register_compression");
713 
714 	number_slots = sizeof(a->decompressors) / sizeof(a->decompressors[0]);
715 
716 	for (i = 0; i < number_slots; i++) {
717 		if (a->decompressors[i].bid == bid)
718 			return (a->decompressors + i);
719 		if (a->decompressors[i].bid == NULL) {
720 			a->decompressors[i].bid = bid;
721 			a->decompressors[i].init = init;
722 			return (a->decompressors + i);
723 		}
724 	}
725 
726 	__archive_errx(1, "Not enough slots for compression registration");
727 	return (NULL); /* Never actually executed. */
728 }
729 
730 /* used internally to simplify read-ahead */
731 const void *
732 __archive_read_ahead(struct archive_read *a, size_t len)
733 {
734 	const void *h;
735 
736 	if ((a->decompressor->read_ahead)(a, &h, len) < (ssize_t)len)
737 		return (NULL);
738 	return (h);
739 }
740