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