Import libarchive-2.4.17. See NEWS for details.
[dragonfly.git] / contrib / libarchive-2 / libarchive / archive_read.c
blob19be77569c1ead9662de4e0a911cfe589db3b594
1 /*-
2 * Copyright (c) 2003-2007 Tim Kientzle
3 * All rights reserved.
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.
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.
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.
34 #include "archive_platform.h"
35 __FBSDID("$FreeBSD: src/lib/libarchive/archive_read.c,v 1.37 2008/01/03 17:54:26 des Exp $");
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
51 #include "archive.h"
52 #include "archive_entry.h"
53 #include "archive_private.h"
54 #include "archive_read_private.h"
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);
61 * Allocate, initialize and return a struct archive object.
63 struct archive *
64 archive_read_new(void)
66 struct archive_read *a;
67 unsigned char *nulls;
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;
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);
84 memset(nulls, 0, a->null_length);
85 a->nulls = nulls;
87 a->archive.state = ARCHIVE_STATE_NEW;
88 a->entry = archive_entry_new();
90 /* We always support uncompressed archives. */
91 archive_read_support_compression_none(&a->archive);
93 return (&a->archive);
97 * Record the do-not-extract-to file. This belongs in archive_read_extract.c.
99 void
100 archive_read_extract_set_skip_file(struct archive *_a, dev_t d, ino_t i)
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;
111 * Open the archive
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)
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);
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)
131 struct archive_read *a = (struct archive_read *)_a;
132 const void *buffer;
133 ssize_t bytes_read;
134 int e;
136 __archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW, "archive_read_open");
138 if (client_reader == NULL)
139 __archive_errx(1,
140 "No reader function provided to archive_read_open");
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.)
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;
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);
165 /* Read first block now for compress format detection. */
166 bytes_read = (client_reader)(&a->archive, client_data, &buffer);
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);
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;
183 /* Select a decompression routine. */
184 choose_decompressor(a, buffer, (size_t)bytes_read);
185 if (a->decompressor == NULL)
186 return (ARCHIVE_FATAL);
188 /* Initialize decompression routine with the first block of data. */
189 e = (a->decompressor->init)(a, buffer, (size_t)bytes_read);
191 if (e == ARCHIVE_OK)
192 a->archive.state = ARCHIVE_STATE_HEADER;
195 * If the decompressor didn't register a skip function, provide a
196 * dummy compression-layer skip function.
198 if (a->decompressor->skip == NULL)
199 a->decompressor->skip = dummy_skip;
201 return (e);
205 * Allow each registered decompression routine to bid on whether it
206 * wants to handle this stream. Return index of winning bidder.
208 static void
209 choose_decompressor(struct archive_read *a,
210 const void *buffer, size_t bytes_read)
212 int decompression_slots, i, bid, best_bid;
213 struct decompressor_t *decompressor, *best_decompressor;
215 decompression_slots = sizeof(a->decompressors) /
216 sizeof(a->decompressors[0]);
218 best_bid = 0;
219 a->decompressor = NULL;
220 best_decompressor = NULL;
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;
231 decompressor ++;
235 * There were no bidders; this is a serious programmer error
236 * and demands a quick and definitive abort.
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.");
245 * There were bidders, but no non-zero bids; this means we can't
246 * support this stream.
248 if (best_bid < 1) {
249 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
250 "Unrecognized archive format");
251 return;
254 /* Record the best decompressor for this stream. */
255 a->decompressor = best_decompressor;
259 * Dummy skip function, for use if the compression layer doesn't provide
260 * one: This code just reads data and discards it.
262 static off_t
263 dummy_skip(struct archive_read * a, off_t request)
265 const void * dummy_buffer;
266 ssize_t bytes_read;
267 off_t bytes_skipped;
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);
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;
287 return (bytes_skipped);
291 * Read header of next entry.
294 archive_read_next_header(struct archive *_a, struct archive_entry **entryp)
296 struct archive_read *a = (struct archive_read *)_a;
297 struct archive_entry *entry;
298 int slot, ret;
300 __archive_check_magic(_a, ARCHIVE_READ_MAGIC,
301 ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
302 "archive_read_next_header");
304 *entryp = NULL;
305 entry = a->entry;
306 archive_entry_clear(entry);
307 archive_clear_error(&a->archive);
310 * If no format has yet been chosen, choose one.
312 if (a->format == NULL) {
313 slot = choose_format(a);
314 if (slot < 0) {
315 a->archive.state = ARCHIVE_STATE_FATAL;
316 return (ARCHIVE_FATAL);
318 a->format = &(a->formats[slot]);
322 * If client didn't consume entire data, skip any remainder
323 * (This is especially important for GNU incremental directories.)
325 if (a->archive.state == ARCHIVE_STATE_DATA) {
326 ret = archive_read_data_skip(&a->archive);
327 if (ret == ARCHIVE_EOF) {
328 archive_set_error(&a->archive, EIO, "Premature end-of-file.");
329 a->archive.state = ARCHIVE_STATE_FATAL;
330 return (ARCHIVE_FATAL);
332 if (ret != ARCHIVE_OK)
333 return (ret);
336 /* Record start-of-header. */
337 a->header_position = a->archive.file_position;
339 ret = (a->format->read_header)(a, entry);
342 * EOF and FATAL are persistent at this layer. By
343 * modifying the state, we guarantee that future calls to
344 * read a header or read data will fail.
346 switch (ret) {
347 case ARCHIVE_EOF:
348 a->archive.state = ARCHIVE_STATE_EOF;
349 break;
350 case ARCHIVE_OK:
351 a->archive.state = ARCHIVE_STATE_DATA;
352 break;
353 case ARCHIVE_WARN:
354 a->archive.state = ARCHIVE_STATE_DATA;
355 break;
356 case ARCHIVE_RETRY:
357 break;
358 case ARCHIVE_FATAL:
359 a->archive.state = ARCHIVE_STATE_FATAL;
360 break;
363 *entryp = entry;
364 a->read_data_output_offset = 0;
365 a->read_data_remaining = 0;
366 return (ret);
370 * Allow each registered format to bid on whether it wants to handle
371 * the next entry. Return index of winning bidder.
373 static int
374 choose_format(struct archive_read *a)
376 int slots;
377 int i;
378 int bid, best_bid;
379 int best_bid_slot;
381 slots = sizeof(a->formats) / sizeof(a->formats[0]);
382 best_bid = -1;
383 best_bid_slot = -1;
385 /* Set up a->format and a->pformat_data for convenience of bidders. */
386 a->format = &(a->formats[0]);
387 for (i = 0; i < slots; i++, a->format++) {
388 if (a->format->bid) {
389 bid = (a->format->bid)(a);
390 if (bid == ARCHIVE_FATAL)
391 return (ARCHIVE_FATAL);
392 if ((bid > best_bid) || (best_bid_slot < 0)) {
393 best_bid = bid;
394 best_bid_slot = i;
400 * There were no bidders; this is a serious programmer error
401 * and demands a quick and definitive abort.
403 if (best_bid_slot < 0)
404 __archive_errx(1, "No formats were registered; you must "
405 "invoke at least one archive_read_support_format_XXX "
406 "function in order to successfully read an archive.");
409 * There were bidders, but no non-zero bids; this means we
410 * can't support this stream.
412 if (best_bid < 1) {
413 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
414 "Unrecognized archive format");
415 return (ARCHIVE_FATAL);
418 return (best_bid_slot);
422 * Return the file offset (within the uncompressed data stream) where
423 * the last header started.
425 int64_t
426 archive_read_header_position(struct archive *_a)
428 struct archive_read *a = (struct archive_read *)_a;
429 __archive_check_magic(_a, ARCHIVE_READ_MAGIC,
430 ARCHIVE_STATE_ANY, "archive_read_header_position");
431 return (a->header_position);
435 * Read data from an archive entry, using a read(2)-style interface.
436 * This is a convenience routine that just calls
437 * archive_read_data_block and copies the results into the client
438 * buffer, filling any gaps with zero bytes. Clients using this
439 * API can be completely ignorant of sparse-file issues; sparse files
440 * will simply be padded with nulls.
442 * DO NOT intermingle calls to this function and archive_read_data_block
443 * to read a single entry body.
445 ssize_t
446 archive_read_data(struct archive *_a, void *buff, size_t s)
448 struct archive_read *a = (struct archive_read *)_a;
449 char *dest;
450 const void *read_buf;
451 size_t bytes_read;
452 size_t len;
453 int r;
455 bytes_read = 0;
456 dest = (char *)buff;
458 while (s > 0) {
459 if (a->read_data_remaining == 0) {
460 read_buf = a->read_data_block;
461 r = archive_read_data_block(&a->archive, &read_buf,
462 &a->read_data_remaining, &a->read_data_offset);
463 a->read_data_block = read_buf;
464 if (r == ARCHIVE_EOF)
465 return (bytes_read);
467 * Error codes are all negative, so the status
468 * return here cannot be confused with a valid
469 * byte count. (ARCHIVE_OK is zero.)
471 if (r < ARCHIVE_OK)
472 return (r);
475 if (a->read_data_offset < a->read_data_output_offset) {
476 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
477 "Encountered out-of-order sparse blocks");
478 return (ARCHIVE_RETRY);
481 /* Compute the amount of zero padding needed. */
482 if (a->read_data_output_offset + (off_t)s <
483 a->read_data_offset) {
484 len = s;
485 } else if (a->read_data_output_offset <
486 a->read_data_offset) {
487 len = a->read_data_offset -
488 a->read_data_output_offset;
489 } else
490 len = 0;
492 /* Add zeroes. */
493 memset(dest, 0, len);
494 s -= len;
495 a->read_data_output_offset += len;
496 dest += len;
497 bytes_read += len;
499 /* Copy data if there is any space left. */
500 if (s > 0) {
501 len = a->read_data_remaining;
502 if (len > s)
503 len = s;
504 memcpy(dest, a->read_data_block, len);
505 s -= len;
506 a->read_data_block += len;
507 a->read_data_remaining -= len;
508 a->read_data_output_offset += len;
509 a->read_data_offset += len;
510 dest += len;
511 bytes_read += len;
514 return (bytes_read);
517 #if ARCHIVE_API_VERSION < 3
519 * Obsolete function provided for compatibility only. Note that the API
520 * of this function doesn't allow the caller to detect if the remaining
521 * data from the archive entry is shorter than the buffer provided, or
522 * even if an error occurred while reading data.
525 archive_read_data_into_buffer(struct archive *a, void *d, ssize_t len)
528 archive_read_data(a, d, len);
529 return (ARCHIVE_OK);
531 #endif
534 * Skip over all remaining data in this entry.
537 archive_read_data_skip(struct archive *_a)
539 struct archive_read *a = (struct archive_read *)_a;
540 int r;
541 const void *buff;
542 size_t size;
543 off_t offset;
545 __archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA,
546 "archive_read_data_skip");
548 if (a->format->read_data_skip != NULL)
549 r = (a->format->read_data_skip)(a);
550 else {
551 while ((r = archive_read_data_block(&a->archive,
552 &buff, &size, &offset))
553 == ARCHIVE_OK)
557 if (r == ARCHIVE_EOF)
558 r = ARCHIVE_OK;
560 a->archive.state = ARCHIVE_STATE_HEADER;
561 return (r);
565 * Read the next block of entry data from the archive.
566 * This is a zero-copy interface; the client receives a pointer,
567 * size, and file offset of the next available block of data.
569 * Returns ARCHIVE_OK if the operation is successful, ARCHIVE_EOF if
570 * the end of entry is encountered.
573 archive_read_data_block(struct archive *_a,
574 const void **buff, size_t *size, off_t *offset)
576 struct archive_read *a = (struct archive_read *)_a;
577 __archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA,
578 "archive_read_data_block");
580 if (a->format->read_data == NULL) {
581 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
582 "Internal error: "
583 "No format_read_data_block function registered");
584 return (ARCHIVE_FATAL);
587 return (a->format->read_data)(a, buff, size, offset);
591 * Close the file and release most resources.
593 * Be careful: client might just call read_new and then read_finish.
594 * Don't assume we actually read anything or performed any non-trivial
595 * initialization.
598 archive_read_close(struct archive *_a)
600 struct archive_read *a = (struct archive_read *)_a;
601 int r = ARCHIVE_OK, r1 = ARCHIVE_OK;
602 size_t i, n;
604 __archive_check_magic(&a->archive, ARCHIVE_READ_MAGIC,
605 ARCHIVE_STATE_ANY, "archive_read_close");
606 a->archive.state = ARCHIVE_STATE_CLOSED;
608 /* Call cleanup functions registered by optional components. */
609 if (a->cleanup_archive_extract != NULL)
610 r = (a->cleanup_archive_extract)(a);
612 /* TODO: Clean up the formatters. */
614 /* Clean up the decompressors. */
615 n = sizeof(a->decompressors)/sizeof(a->decompressors[0]);
616 for (i = 0; i < n; i++) {
617 if (a->decompressors[i].finish != NULL) {
618 r1 = (a->decompressors[i].finish)(a);
619 if (r1 < r)
620 r = r1;
624 /* Close the client stream. */
625 if (a->client_closer != NULL) {
626 r1 = ((a->client_closer)(&a->archive, a->client_data));
627 if (r1 < r)
628 r = r1;
631 return (r);
635 * Release memory and other resources.
637 #if ARCHIVE_API_VERSION > 1
639 #else
640 /* Temporarily allow library to compile with either 1.x or 2.0 API. */
641 void
642 #endif
643 archive_read_finish(struct archive *_a)
645 struct archive_read *a = (struct archive_read *)_a;
646 int i;
647 int slots;
648 int r = ARCHIVE_OK;
650 __archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_ANY,
651 "archive_read_finish");
652 if (a->archive.state != ARCHIVE_STATE_CLOSED)
653 r = archive_read_close(&a->archive);
655 /* Cleanup format-specific data. */
656 slots = sizeof(a->formats) / sizeof(a->formats[0]);
657 for (i = 0; i < slots; i++) {
658 a->format = &(a->formats[i]);
659 if (a->formats[i].cleanup)
660 (a->formats[i].cleanup)(a);
663 /* Casting a pointer to int allows us to remove 'const.' */
664 free((void *)(uintptr_t)(const void *)a->nulls);
665 archive_string_free(&a->archive.error_string);
666 if (a->entry)
667 archive_entry_free(a->entry);
668 a->archive.magic = 0;
669 free(a);
670 #if ARCHIVE_API_VERSION > 1
671 return (r);
672 #endif
676 * Used internally by read format handlers to register their bid and
677 * initialization functions.
680 __archive_read_register_format(struct archive_read *a,
681 void *format_data,
682 int (*bid)(struct archive_read *),
683 int (*read_header)(struct archive_read *, struct archive_entry *),
684 int (*read_data)(struct archive_read *, const void **, size_t *, off_t *),
685 int (*read_data_skip)(struct archive_read *),
686 int (*cleanup)(struct archive_read *))
688 int i, number_slots;
690 __archive_check_magic(&a->archive,
691 ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
692 "__archive_read_register_format");
694 number_slots = sizeof(a->formats) / sizeof(a->formats[0]);
696 for (i = 0; i < number_slots; i++) {
697 if (a->formats[i].bid == bid)
698 return (ARCHIVE_WARN); /* We've already installed */
699 if (a->formats[i].bid == NULL) {
700 a->formats[i].bid = bid;
701 a->formats[i].read_header = read_header;
702 a->formats[i].read_data = read_data;
703 a->formats[i].read_data_skip = read_data_skip;
704 a->formats[i].cleanup = cleanup;
705 a->formats[i].data = format_data;
706 return (ARCHIVE_OK);
710 __archive_errx(1, "Not enough slots for format registration");
711 return (ARCHIVE_FATAL); /* Never actually called. */
715 * Used internally by decompression routines to register their bid and
716 * initialization functions.
718 struct decompressor_t *
719 __archive_read_register_compression(struct archive_read *a,
720 int (*bid)(const void *, size_t),
721 int (*init)(struct archive_read *, const void *, size_t))
723 int i, number_slots;
725 __archive_check_magic(&a->archive,
726 ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
727 "__archive_read_register_compression");
729 number_slots = sizeof(a->decompressors) / sizeof(a->decompressors[0]);
731 for (i = 0; i < number_slots; i++) {
732 if (a->decompressors[i].bid == bid)
733 return (a->decompressors + i);
734 if (a->decompressors[i].bid == NULL) {
735 a->decompressors[i].bid = bid;
736 a->decompressors[i].init = init;
737 return (a->decompressors + i);
741 __archive_errx(1, "Not enough slots for compression registration");
742 return (NULL); /* Never actually executed. */
745 /* used internally to simplify read-ahead */
746 const void *
747 __archive_read_ahead(struct archive_read *a, size_t len)
749 const void *h;
751 if ((a->decompressor->read_ahead)(a, &h, len) < (ssize_t)len)
752 return (NULL);
753 return (h);