2 * Copyright (c) 2003-2007 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.32 2007/04/02 00:41:37 kientzle Exp $");
52 #include "archive_entry.h"
53 #include "archive_private.h"
54 #include "archive_read_private.h"
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);
61 * Allocate, initialize and return a struct archive object.
64 archive_read_new(void)
66 struct archive_read *a;
69 a = (struct archive_read *)malloc(sizeof(*a));
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);
79 archive_set_error(&a->archive, ENOMEM,
80 "Can't allocate archive object 'nulls' element");
84 memset(nulls, 0, a->null_length);
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);
97 * Record the do-not-extract-to file. This belongs in archive_read_extract.c.
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;
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;
137 __archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW, "archive_read_open");
139 if (client_reader == NULL)
141 "No reader function provided to archive_read_open");
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.)
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;
155 /* Open data source. */
156 if (client_opener != NULL) {
157 e =(client_opener)(&a->archive, client_data);
159 /* If the open failed, call the closer to clean up. */
161 (client_closer)(&a->archive, client_data);
166 /* Read first block now for format detection. */
167 bytes_read = (client_reader)(&a->archive, client_data, &buffer);
169 if (bytes_read < 0) {
170 /* If the first read fails, close before returning error. */
172 (client_closer)(&a->archive, client_data);
173 /* client_reader should have already set error information. */
174 return (ARCHIVE_FATAL);
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;
184 /* Select a decompression routine. */
185 high_bidder = choose_decompressor(a, buffer, (size_t)bytes_read);
187 return (ARCHIVE_FATAL);
189 /* Initialize decompression routine with the first block of data. */
190 e = (a->decompressors[high_bidder].init)(a, buffer, (size_t)bytes_read);
193 a->archive.state = ARCHIVE_STATE_HEADER;
196 * If the decompressor didn't register a skip function, provide a
197 * dummy compression-layer skip function.
199 if (a->compression_skip == NULL)
200 a->compression_skip = dummy_skip;
206 * Allow each registered decompression routine to bid on whether it
207 * wants to handle this stream. Return index of winning bidder.
210 choose_decompressor(struct archive_read *a,
211 const void *buffer, size_t bytes_read)
213 int decompression_slots, i, bid, best_bid, best_bid_slot;
215 decompression_slots = sizeof(a->decompressors) /
216 sizeof(a->decompressors[0]);
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)) {
232 * There were no bidders; this is a serious programmer error
233 * and demands a quick and definitive abort.
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.");
242 * There were bidders, but no non-zero bids; this means we can't
243 * support this stream.
246 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
247 "Unrecognized archive format");
248 return (ARCHIVE_FATAL);
251 return (best_bid_slot);
255 * Dummy skip function, for use if the compression layer doesn't provide
256 * one: This code just reads data and discards it.
259 dummy_skip(struct archive_read * a, off_t request)
261 const void * dummy_buffer;
265 for (bytes_skipped = 0; request > 0;) {
266 bytes_read = (a->compression_read_ahead)(a, &dummy_buffer, 1);
269 if (bytes_read == 0) {
271 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
272 "Truncated input file (need to skip %jd bytes)",
274 return (ARCHIVE_FATAL);
276 if (bytes_read > request)
277 bytes_read = request;
278 (a->compression_read_consume)(a, bytes_read);
279 request -= bytes_read;
280 bytes_skipped += bytes_read;
283 return (bytes_skipped);
287 * Read header of next entry.
290 archive_read_next_header(struct archive *_a, struct archive_entry **entryp)
292 struct archive_read *a = (struct archive_read *)_a;
293 struct archive_entry *entry;
296 __archive_check_magic(_a, ARCHIVE_READ_MAGIC,
297 ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
298 "archive_read_next_header");
302 archive_entry_clear(entry);
303 archive_clear_error(&a->archive);
306 * If client didn't consume entire data, skip any remainder
307 * (This is especially important for GNU incremental directories.)
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);
316 if (ret != ARCHIVE_OK)
320 /* Record start-of-header. */
321 a->header_position = a->archive.file_position;
323 slot = choose_format(a);
325 a->archive.state = ARCHIVE_STATE_FATAL;
326 return (ARCHIVE_FATAL);
328 a->format = &(a->formats[slot]);
329 a->pformat_data = &(a->format->format_data);
330 ret = (a->format->read_header)(a, entry);
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.
339 a->archive.state = ARCHIVE_STATE_EOF;
342 a->archive.state = ARCHIVE_STATE_DATA;
345 a->archive.state = ARCHIVE_STATE_DATA;
350 a->archive.state = ARCHIVE_STATE_FATAL;
355 a->read_data_output_offset = 0;
356 a->read_data_remaining = 0;
361 * Allow each registered format to bid on whether it wants to handle
362 * the next entry. Return index of winning bidder.
365 choose_format(struct archive_read *a)
372 slots = sizeof(a->formats) / sizeof(a->formats[0]);
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)) {
392 * There were no bidders; this is a serious programmer error
393 * and demands a quick and definitive abort.
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.");
401 * There were bidders, but no non-zero bids; this means we
402 * can't support this stream.
405 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
406 "Unrecognized archive format");
407 return (ARCHIVE_FATAL);
410 return (best_bid_slot);
414 * Return the file offset (within the uncompressed data stream) where
415 * the last header started.
418 archive_read_header_position(struct archive *_a)
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);
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.
434 * DO NOT intermingle calls to this function and archive_read_data_block
435 * to read a single entry body.
438 archive_read_data(struct archive *_a, void *buff, size_t s)
440 struct archive_read *a = (struct archive_read *)_a;
442 const void *read_buf;
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)
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.)
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);
473 /* Compute the amount of zero padding needed. */
474 if (a->read_data_output_offset + (off_t)s <
475 a->read_data_offset) {
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;
485 memset(dest, 0, len);
487 a->read_data_output_offset += len;
491 /* Copy data if there is any space left. */
493 len = a->read_data_remaining;
496 memcpy(dest, a->read_data_block, 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;
510 * Skip over all remaining data in this entry.
513 archive_read_data_skip(struct archive *_a)
515 struct archive_read *a = (struct archive_read *)_a;
521 __archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA,
522 "archive_read_data_skip");
524 if (a->format->read_data_skip != NULL)
525 r = (a->format->read_data_skip)(a);
527 while ((r = archive_read_data_block(&a->archive,
528 &buff, &size, &offset))
533 if (r == ARCHIVE_EOF)
536 a->archive.state = ARCHIVE_STATE_HEADER;
541 * Read the next block of entry data from the archive.
542 * This is a zero-copy interface; the client receives a pointer,
543 * size, and file offset of the next available block of data.
545 * Returns ARCHIVE_OK if the operation is successful, ARCHIVE_EOF if
546 * the end of entry is encountered.
549 archive_read_data_block(struct archive *_a,
550 const void **buff, size_t *size, off_t *offset)
552 struct archive_read *a = (struct archive_read *)_a;
553 __archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA,
554 "archive_read_data_block");
556 if (a->format->read_data == NULL) {
557 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
559 "No format_read_data_block function registered");
560 return (ARCHIVE_FATAL);
563 return (a->format->read_data)(a, buff, size, offset);
567 * Close the file and release most resources.
569 * Be careful: client might just call read_new and then read_finish.
570 * Don't assume we actually read anything or performed any non-trivial
574 archive_read_close(struct archive *_a)
576 struct archive_read *a = (struct archive_read *)_a;
577 int r = ARCHIVE_OK, r1 = ARCHIVE_OK;
579 __archive_check_magic(&a->archive, ARCHIVE_READ_MAGIC,
580 ARCHIVE_STATE_ANY, "archive_read_close");
581 a->archive.state = ARCHIVE_STATE_CLOSED;
583 /* Call cleanup functions registered by optional components. */
584 if (a->cleanup_archive_extract != NULL)
585 r = (a->cleanup_archive_extract)(a);
587 /* TODO: Finish the format processing. */
589 /* Close the input machinery. */
590 if (a->compression_finish != NULL) {
591 r1 = (a->compression_finish)(a);
600 * Release memory and other resources.
602 #if ARCHIVE_API_VERSION > 1
605 /* Temporarily allow library to compile with either 1.x or 2.0 API. */
608 archive_read_finish(struct archive *_a)
610 struct archive_read *a = (struct archive_read *)_a;
615 __archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_ANY,
616 "archive_read_finish");
617 if (a->archive.state != ARCHIVE_STATE_CLOSED)
618 r = archive_read_close(&a->archive);
620 /* Cleanup format-specific data. */
621 slots = sizeof(a->formats) / sizeof(a->formats[0]);
622 for (i = 0; i < slots; i++) {
623 a->pformat_data = &(a->formats[i].format_data);
624 if (a->formats[i].cleanup)
625 (a->formats[i].cleanup)(a);
628 /* Casting a pointer to int allows us to remove 'const.' */
629 free((void *)(uintptr_t)(const void *)a->nulls);
630 archive_string_free(&a->archive.error_string);
632 archive_entry_free(a->entry);
633 a->archive.magic = 0;
635 #if ARCHIVE_API_VERSION > 1
641 * Used internally by read format handlers to register their bid and
642 * initialization functions.
645 __archive_read_register_format(struct archive_read *a,
647 int (*bid)(struct archive_read *),
648 int (*read_header)(struct archive_read *, struct archive_entry *),
649 int (*read_data)(struct archive_read *, const void **, size_t *, off_t *),
650 int (*read_data_skip)(struct archive_read *),
651 int (*cleanup)(struct archive_read *))
655 __archive_check_magic(&a->archive,
656 ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
657 "__archive_read_register_format");
659 number_slots = sizeof(a->formats) / sizeof(a->formats[0]);
661 for (i = 0; i < number_slots; i++) {
662 if (a->formats[i].bid == bid)
663 return (ARCHIVE_WARN); /* We've already installed */
664 if (a->formats[i].bid == NULL) {
665 a->formats[i].bid = bid;
666 a->formats[i].read_header = read_header;
667 a->formats[i].read_data = read_data;
668 a->formats[i].read_data_skip = read_data_skip;
669 a->formats[i].cleanup = cleanup;
670 a->formats[i].format_data = format_data;
675 __archive_errx(1, "Not enough slots for format registration");
676 return (ARCHIVE_FATAL); /* Never actually called. */
680 * Used internally by decompression routines to register their bid and
681 * initialization functions.
684 __archive_read_register_compression(struct archive_read *a,
685 int (*bid)(const void *, size_t),
686 int (*init)(struct archive_read *, const void *, size_t))
690 __archive_check_magic(&a->archive,
691 ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
692 "__archive_read_register_compression");
694 number_slots = sizeof(a->decompressors) / sizeof(a->decompressors[0]);
696 for (i = 0; i < number_slots; i++) {
697 if (a->decompressors[i].bid == bid)
698 return (ARCHIVE_OK); /* We've already installed */
699 if (a->decompressors[i].bid == NULL) {
700 a->decompressors[i].bid = bid;
701 a->decompressors[i].init = init;
706 __archive_errx(1, "Not enough slots for compression registration");
707 return (ARCHIVE_FATAL); /* Never actually executed. */