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$");
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.
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;
136 __archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW, "archive_read_open");
138 if (client_reader == NULL)
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);
158 /* If the open failed, call the closer to clean up. */
160 (client_closer)(&a->archive, client_data);
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. */
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);
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;
205 * Allow each registered decompression routine to bid on whether it
206 * wants to handle this stream. Return index of winning bidder.
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]);
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) {
228 best_decompressor = 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.
249 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
250 "Unrecognized archive format");
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.
263 dummy_skip(struct archive_read * a, off_t request)
265 const void * dummy_buffer;
269 for (bytes_skipped = 0; request > 0;) {
270 bytes_read = (a->decompressor->read_ahead)(a, &dummy_buffer, 1);
273 if (bytes_read == 0) {
275 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
276 "Truncated input file (need to skip %jd bytes)",
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;
300 __archive_check_magic(_a, ARCHIVE_READ_MAGIC,
301 ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
302 "archive_read_next_header");
306 archive_entry_clear(entry);
307 archive_clear_error(&a->archive);
310 * If client didn't consume entire data, skip any remainder
311 * (This is especially important for GNU incremental directories.)
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);
320 if (ret != ARCHIVE_OK)
324 /* Record start-of-header. */
325 a->header_position = a->archive.file_position;
327 slot = choose_format(a);
329 a->archive.state = ARCHIVE_STATE_FATAL;
330 return (ARCHIVE_FATAL);
332 a->format = &(a->formats[slot]);
333 ret = (a->format->read_header)(a, entry);
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.
342 a->archive.state = ARCHIVE_STATE_EOF;
345 a->archive.state = ARCHIVE_STATE_DATA;
348 a->archive.state = ARCHIVE_STATE_DATA;
353 a->archive.state = ARCHIVE_STATE_FATAL;
358 a->read_data_output_offset = 0;
359 a->read_data_remaining = 0;
364 * Allow each registered format to bid on whether it wants to handle
365 * the next entry. Return index of winning bidder.
368 choose_format(struct archive_read *a)
375 slots = sizeof(a->formats) / sizeof(a->formats[0]);
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)) {
394 * There were no bidders; this is a serious programmer error
395 * and demands a quick and definitive abort.
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.");
403 * There were bidders, but no non-zero bids; this means we
404 * can't support this stream.
407 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
408 "Unrecognized archive format");
409 return (ARCHIVE_FATAL);
412 return (best_bid_slot);
416 * Return the file offset (within the uncompressed data stream) where
417 * the last header started.
420 archive_read_header_position(struct archive *_a)
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);
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.
436 * DO NOT intermingle calls to this function and archive_read_data_block
437 * to read a single entry body.
440 archive_read_data(struct archive *_a, void *buff, size_t s)
442 struct archive_read *a = (struct archive_read *)_a;
444 const void *read_buf;
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)
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.)
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);
475 /* Compute the amount of zero padding needed. */
476 if (a->read_data_output_offset + (off_t)s <
477 a->read_data_offset) {
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;
487 memset(dest, 0, len);
489 a->read_data_output_offset += len;
493 /* Copy data if there is any space left. */
495 len = a->read_data_remaining;
498 memcpy(dest, a->read_data_block, 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;
511 #if ARCHIVE_API_VERSION < 3
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.
519 archive_read_data_into_buffer(struct archive *a, void *d, ssize_t len)
522 archive_read_data(a, d, len);
528 * Skip over all remaining data in this entry.
531 archive_read_data_skip(struct archive *_a)
533 struct archive_read *a = (struct archive_read *)_a;
539 __archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA,
540 "archive_read_data_skip");
542 if (a->format->read_data_skip != NULL)
543 r = (a->format->read_data_skip)(a);
545 while ((r = archive_read_data_block(&a->archive,
546 &buff, &size, &offset))
551 if (r == ARCHIVE_EOF)
554 a->archive.state = ARCHIVE_STATE_HEADER;
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.
563 * Returns ARCHIVE_OK if the operation is successful, ARCHIVE_EOF if
564 * the end of entry is encountered.
567 archive_read_data_block(struct archive *_a,
568 const void **buff, size_t *size, off_t *offset)
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");
574 if (a->format->read_data == NULL) {
575 archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
577 "No format_read_data_block function registered");
578 return (ARCHIVE_FATAL);
581 return (a->format->read_data)(a, buff, size, offset);
585 * Close the file and release most resources.
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
592 archive_read_close(struct archive *_a)
594 struct archive_read *a = (struct archive_read *)_a;
595 int r = ARCHIVE_OK, r1 = ARCHIVE_OK;
598 __archive_check_magic(&a->archive, ARCHIVE_READ_MAGIC,
599 ARCHIVE_STATE_ANY, "archive_read_close");
600 a->archive.state = ARCHIVE_STATE_CLOSED;
602 /* Call cleanup functions registered by optional components. */
603 if (a->cleanup_archive_extract != NULL)
604 r = (a->cleanup_archive_extract)(a);
606 /* TODO: Clean up the formatters. */
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);
618 /* Close the client stream. */
619 if (a->client_closer != NULL) {
620 r1 = ((a->client_closer)(&a->archive, a->client_data));
629 * Release memory and other resources.
631 #if ARCHIVE_API_VERSION > 1
634 /* Temporarily allow library to compile with either 1.x or 2.0 API. */
637 archive_read_finish(struct archive *_a)
639 struct archive_read *a = (struct archive_read *)_a;
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);
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);
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);
661 archive_entry_free(a->entry);
662 a->archive.magic = 0;
664 #if ARCHIVE_API_VERSION > 1
670 * Used internally by read format handlers to register their bid and
671 * initialization functions.
674 __archive_read_register_format(struct archive_read *a,
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 *))
684 __archive_check_magic(&a->archive,
685 ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
686 "__archive_read_register_format");
688 number_slots = sizeof(a->formats) / sizeof(a->formats[0]);
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;
704 __archive_errx(1, "Not enough slots for format registration");
705 return (ARCHIVE_FATAL); /* Never actually called. */
709 * Used internally by decompression routines to register their bid and
710 * initialization functions.
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))
719 __archive_check_magic(&a->archive,
720 ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
721 "__archive_read_register_compression");
723 number_slots = sizeof(a->decompressors) / sizeof(a->decompressors[0]);
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);
735 __archive_errx(1, "Not enough slots for compression registration");
736 return (NULL); /* Never actually executed. */