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.37 2008/01/03 17:54:26 des Exp $");
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 no format has yet been chosen, choose one.
312 if (a
->format
== NULL
) {
313 slot
= choose_format(a
);
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
)
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.
348 a
->archive
.state
= ARCHIVE_STATE_EOF
;
351 a
->archive
.state
= ARCHIVE_STATE_DATA
;
354 a
->archive
.state
= ARCHIVE_STATE_DATA
;
359 a
->archive
.state
= ARCHIVE_STATE_FATAL
;
364 a
->read_data_output_offset
= 0;
365 a
->read_data_remaining
= 0;
370 * Allow each registered format to bid on whether it wants to handle
371 * the next entry. Return index of winning bidder.
374 choose_format(struct archive_read
*a
)
381 slots
= sizeof(a
->formats
) / sizeof(a
->formats
[0]);
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)) {
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.
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.
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.
446 archive_read_data(struct archive
*_a
, void *buff
, size_t s
)
448 struct archive_read
*a
= (struct archive_read
*)_a
;
450 const void *read_buf
;
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
)
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.)
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
) {
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
;
493 memset(dest
, 0, len
);
495 a
->read_data_output_offset
+= len
;
499 /* Copy data if there is any space left. */
501 len
= a
->read_data_remaining
;
504 memcpy(dest
, a
->read_data_block
, 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
;
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
);
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
;
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
);
551 while ((r
= archive_read_data_block(&a
->archive
,
552 &buff
, &size
, &offset
))
557 if (r
== ARCHIVE_EOF
)
560 a
->archive
.state
= ARCHIVE_STATE_HEADER
;
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
,
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
598 archive_read_close(struct archive
*_a
)
600 struct archive_read
*a
= (struct archive_read
*)_a
;
601 int r
= ARCHIVE_OK
, r1
= ARCHIVE_OK
;
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
);
624 /* Close the client stream. */
625 if (a
->client_closer
!= NULL
) {
626 r1
= ((a
->client_closer
)(&a
->archive
, a
->client_data
));
635 * Release memory and other resources.
637 #if ARCHIVE_API_VERSION > 1
640 /* Temporarily allow library to compile with either 1.x or 2.0 API. */
643 archive_read_finish(struct archive
*_a
)
645 struct archive_read
*a
= (struct archive_read
*)_a
;
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
);
667 archive_entry_free(a
->entry
);
668 a
->archive
.magic
= 0;
670 #if ARCHIVE_API_VERSION > 1
676 * Used internally by read format handlers to register their bid and
677 * initialization functions.
680 __archive_read_register_format(struct archive_read
*a
,
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
*))
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
;
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))
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 */
747 __archive_read_ahead(struct archive_read
*a
, size_t len
)
751 if ((a
->decompressor
->read_ahead
)(a
, &h
, len
) < (ssize_t
)len
)