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