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.38 2008/03/12 04:58:32 kientzle 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
;
68 a
= (struct archive_read
*)malloc(sizeof(*a
));
71 memset(a
, 0, sizeof(*a
));
72 a
->archive
.magic
= ARCHIVE_READ_MAGIC
;
74 a
->archive
.state
= ARCHIVE_STATE_NEW
;
75 a
->entry
= archive_entry_new();
77 /* We always support uncompressed archives. */
78 archive_read_support_compression_none(&a
->archive
);
84 * Record the do-not-extract-to file. This belongs in archive_read_extract.c.
87 archive_read_extract_set_skip_file(struct archive
*_a
, dev_t d
, ino_t i
)
89 struct archive_read
*a
= (struct archive_read
*)_a
;
90 __archive_check_magic(_a
, ARCHIVE_READ_MAGIC
, ARCHIVE_STATE_ANY
,
91 "archive_read_extract_set_skip_file");
101 archive_read_open(struct archive
*a
, void *client_data
,
102 archive_open_callback
*client_opener
, archive_read_callback
*client_reader
,
103 archive_close_callback
*client_closer
)
105 /* Old archive_read_open() is just a thin shell around
106 * archive_read_open2. */
107 return archive_read_open2(a
, client_data
, client_opener
,
108 client_reader
, NULL
, client_closer
);
112 archive_read_open2(struct archive
*_a
, void *client_data
,
113 archive_open_callback
*client_opener
,
114 archive_read_callback
*client_reader
,
115 archive_skip_callback
*client_skipper
,
116 archive_close_callback
*client_closer
)
118 struct archive_read
*a
= (struct archive_read
*)_a
;
123 __archive_check_magic(_a
, ARCHIVE_READ_MAGIC
, ARCHIVE_STATE_NEW
, "archive_read_open");
125 if (client_reader
== NULL
)
127 "No reader function provided to archive_read_open");
130 * Set these NULL initially. If the open or initial read fails,
131 * we'll leave them NULL to indicate that the file is invalid.
132 * (In particular, this helps ensure that the closer doesn't
133 * get called more than once.)
135 a
->client_opener
= NULL
;
136 a
->client_reader
= NULL
;
137 a
->client_skipper
= NULL
;
138 a
->client_closer
= NULL
;
139 a
->client_data
= NULL
;
141 /* Open data source. */
142 if (client_opener
!= NULL
) {
143 e
=(client_opener
)(&a
->archive
, client_data
);
145 /* If the open failed, call the closer to clean up. */
147 (client_closer
)(&a
->archive
, client_data
);
152 /* Read first block now for compress format detection. */
153 bytes_read
= (client_reader
)(&a
->archive
, client_data
, &buffer
);
155 if (bytes_read
< 0) {
156 /* If the first read fails, close before returning error. */
158 (client_closer
)(&a
->archive
, client_data
);
159 /* client_reader should have already set error information. */
160 return (ARCHIVE_FATAL
);
163 /* Now that the client callbacks have worked, remember them. */
164 a
->client_opener
= client_opener
; /* Do we need to remember this? */
165 a
->client_reader
= client_reader
;
166 a
->client_skipper
= client_skipper
;
167 a
->client_closer
= client_closer
;
168 a
->client_data
= client_data
;
170 /* Select a decompression routine. */
171 choose_decompressor(a
, buffer
, (size_t)bytes_read
);
172 if (a
->decompressor
== NULL
)
173 return (ARCHIVE_FATAL
);
175 /* Initialize decompression routine with the first block of data. */
176 e
= (a
->decompressor
->init
)(a
, buffer
, (size_t)bytes_read
);
179 a
->archive
.state
= ARCHIVE_STATE_HEADER
;
182 * If the decompressor didn't register a skip function, provide a
183 * dummy compression-layer skip function.
185 if (a
->decompressor
->skip
== NULL
)
186 a
->decompressor
->skip
= dummy_skip
;
192 * Allow each registered decompression routine to bid on whether it
193 * wants to handle this stream. Return index of winning bidder.
196 choose_decompressor(struct archive_read
*a
,
197 const void *buffer
, size_t bytes_read
)
199 int decompression_slots
, i
, bid
, best_bid
;
200 struct decompressor_t
*decompressor
, *best_decompressor
;
202 decompression_slots
= sizeof(a
->decompressors
) /
203 sizeof(a
->decompressors
[0]);
206 a
->decompressor
= NULL
;
207 best_decompressor
= NULL
;
209 decompressor
= a
->decompressors
;
210 for (i
= 0; i
< decompression_slots
; i
++) {
211 if (decompressor
->bid
) {
212 bid
= (decompressor
->bid
)(buffer
, bytes_read
);
213 if (bid
> best_bid
|| best_decompressor
== NULL
) {
215 best_decompressor
= decompressor
;
222 * There were no bidders; this is a serious programmer error
223 * and demands a quick and definitive abort.
225 if (best_decompressor
== NULL
)
226 __archive_errx(1, "No decompressors were registered; you "
227 "must call at least one "
228 "archive_read_support_compression_XXX function in order "
229 "to successfully read an archive.");
232 * There were bidders, but no non-zero bids; this means we can't
233 * support this stream.
236 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_FILE_FORMAT
,
237 "Unrecognized archive format");
241 /* Record the best decompressor for this stream. */
242 a
->decompressor
= best_decompressor
;
246 * Dummy skip function, for use if the compression layer doesn't provide
247 * one: This code just reads data and discards it.
250 dummy_skip(struct archive_read
* a
, off_t request
)
252 const void * dummy_buffer
;
256 for (bytes_skipped
= 0; request
> 0;) {
257 bytes_read
= (a
->decompressor
->read_ahead
)(a
, &dummy_buffer
, 1);
260 if (bytes_read
== 0) {
262 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_MISC
,
263 "Truncated input file (need to skip %jd bytes)",
265 return (ARCHIVE_FATAL
);
267 if (bytes_read
> request
)
268 bytes_read
= (ssize_t
)request
;
269 (a
->decompressor
->consume
)(a
, (size_t)bytes_read
);
270 request
-= bytes_read
;
271 bytes_skipped
+= bytes_read
;
274 return (bytes_skipped
);
278 * Read header of next entry.
281 archive_read_next_header(struct archive
*_a
, struct archive_entry
**entryp
)
283 struct archive_read
*a
= (struct archive_read
*)_a
;
284 struct archive_entry
*entry
;
287 __archive_check_magic(_a
, ARCHIVE_READ_MAGIC
,
288 ARCHIVE_STATE_HEADER
| ARCHIVE_STATE_DATA
,
289 "archive_read_next_header");
293 archive_entry_clear(entry
);
294 archive_clear_error(&a
->archive
);
297 * If no format has yet been chosen, choose one.
299 if (a
->format
== NULL
) {
300 slot
= choose_format(a
);
302 a
->archive
.state
= ARCHIVE_STATE_FATAL
;
303 return (ARCHIVE_FATAL
);
305 a
->format
= &(a
->formats
[slot
]);
309 * If client didn't consume entire data, skip any remainder
310 * (This is especially important for GNU incremental directories.)
312 if (a
->archive
.state
== ARCHIVE_STATE_DATA
) {
313 ret
= archive_read_data_skip(&a
->archive
);
314 if (ret
== ARCHIVE_EOF
) {
315 archive_set_error(&a
->archive
, EIO
, "Premature end-of-file.");
316 a
->archive
.state
= ARCHIVE_STATE_FATAL
;
317 return (ARCHIVE_FATAL
);
319 if (ret
!= ARCHIVE_OK
)
323 /* Record start-of-header. */
324 a
->header_position
= a
->archive
.file_position
;
326 ret
= (a
->format
->read_header
)(a
, entry
);
329 * EOF and FATAL are persistent at this layer. By
330 * modifying the state, we guarantee that future calls to
331 * read a header or read data will fail.
335 a
->archive
.state
= ARCHIVE_STATE_EOF
;
338 a
->archive
.state
= ARCHIVE_STATE_DATA
;
341 a
->archive
.state
= ARCHIVE_STATE_DATA
;
346 a
->archive
.state
= ARCHIVE_STATE_FATAL
;
351 a
->read_data_output_offset
= 0;
352 a
->read_data_remaining
= 0;
357 * Allow each registered format to bid on whether it wants to handle
358 * the next entry. Return index of winning bidder.
361 choose_format(struct archive_read
*a
)
368 slots
= sizeof(a
->formats
) / sizeof(a
->formats
[0]);
372 /* Set up a->format and a->pformat_data for convenience of bidders. */
373 a
->format
= &(a
->formats
[0]);
374 for (i
= 0; i
< slots
; i
++, a
->format
++) {
375 if (a
->format
->bid
) {
376 bid
= (a
->format
->bid
)(a
);
377 if (bid
== ARCHIVE_FATAL
)
378 return (ARCHIVE_FATAL
);
379 if ((bid
> best_bid
) || (best_bid_slot
< 0)) {
387 * There were no bidders; this is a serious programmer error
388 * and demands a quick and definitive abort.
390 if (best_bid_slot
< 0)
391 __archive_errx(1, "No formats were registered; you must "
392 "invoke at least one archive_read_support_format_XXX "
393 "function in order to successfully read an archive.");
396 * There were bidders, but no non-zero bids; this means we
397 * can't support this stream.
400 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_FILE_FORMAT
,
401 "Unrecognized archive format");
402 return (ARCHIVE_FATAL
);
405 return (best_bid_slot
);
409 * Return the file offset (within the uncompressed data stream) where
410 * the last header started.
413 archive_read_header_position(struct archive
*_a
)
415 struct archive_read
*a
= (struct archive_read
*)_a
;
416 __archive_check_magic(_a
, ARCHIVE_READ_MAGIC
,
417 ARCHIVE_STATE_ANY
, "archive_read_header_position");
418 return (a
->header_position
);
422 * Read data from an archive entry, using a read(2)-style interface.
423 * This is a convenience routine that just calls
424 * archive_read_data_block and copies the results into the client
425 * buffer, filling any gaps with zero bytes. Clients using this
426 * API can be completely ignorant of sparse-file issues; sparse files
427 * will simply be padded with nulls.
429 * DO NOT intermingle calls to this function and archive_read_data_block
430 * to read a single entry body.
433 archive_read_data(struct archive
*_a
, void *buff
, size_t s
)
435 struct archive_read
*a
= (struct archive_read
*)_a
;
437 const void *read_buf
;
446 if (a
->read_data_remaining
== 0) {
447 read_buf
= a
->read_data_block
;
448 r
= archive_read_data_block(&a
->archive
, &read_buf
,
449 &a
->read_data_remaining
, &a
->read_data_offset
);
450 a
->read_data_block
= read_buf
;
451 if (r
== ARCHIVE_EOF
)
454 * Error codes are all negative, so the status
455 * return here cannot be confused with a valid
456 * byte count. (ARCHIVE_OK is zero.)
462 if (a
->read_data_offset
< a
->read_data_output_offset
) {
463 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_FILE_FORMAT
,
464 "Encountered out-of-order sparse blocks");
465 return (ARCHIVE_RETRY
);
468 /* Compute the amount of zero padding needed. */
469 if (a
->read_data_output_offset
+ (off_t
)s
<
470 a
->read_data_offset
) {
472 } else if (a
->read_data_output_offset
<
473 a
->read_data_offset
) {
474 len
= a
->read_data_offset
-
475 a
->read_data_output_offset
;
480 memset(dest
, 0, len
);
482 a
->read_data_output_offset
+= len
;
486 /* Copy data if there is any space left. */
488 len
= a
->read_data_remaining
;
491 memcpy(dest
, a
->read_data_block
, len
);
493 a
->read_data_block
+= len
;
494 a
->read_data_remaining
-= len
;
495 a
->read_data_output_offset
+= len
;
496 a
->read_data_offset
+= len
;
504 #if ARCHIVE_API_VERSION < 3
506 * Obsolete function provided for compatibility only. Note that the API
507 * of this function doesn't allow the caller to detect if the remaining
508 * data from the archive entry is shorter than the buffer provided, or
509 * even if an error occurred while reading data.
512 archive_read_data_into_buffer(struct archive
*a
, void *d
, ssize_t len
)
515 archive_read_data(a
, d
, len
);
521 * Skip over all remaining data in this entry.
524 archive_read_data_skip(struct archive
*_a
)
526 struct archive_read
*a
= (struct archive_read
*)_a
;
532 __archive_check_magic(_a
, ARCHIVE_READ_MAGIC
, ARCHIVE_STATE_DATA
,
533 "archive_read_data_skip");
535 if (a
->format
->read_data_skip
!= NULL
)
536 r
= (a
->format
->read_data_skip
)(a
);
538 while ((r
= archive_read_data_block(&a
->archive
,
539 &buff
, &size
, &offset
))
544 if (r
== ARCHIVE_EOF
)
547 a
->archive
.state
= ARCHIVE_STATE_HEADER
;
552 * Read the next block of entry data from the archive.
553 * This is a zero-copy interface; the client receives a pointer,
554 * size, and file offset of the next available block of data.
556 * Returns ARCHIVE_OK if the operation is successful, ARCHIVE_EOF if
557 * the end of entry is encountered.
560 archive_read_data_block(struct archive
*_a
,
561 const void **buff
, size_t *size
, off_t
*offset
)
563 struct archive_read
*a
= (struct archive_read
*)_a
;
564 __archive_check_magic(_a
, ARCHIVE_READ_MAGIC
, ARCHIVE_STATE_DATA
,
565 "archive_read_data_block");
567 if (a
->format
->read_data
== NULL
) {
568 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_PROGRAMMER
,
570 "No format_read_data_block function registered");
571 return (ARCHIVE_FATAL
);
574 return (a
->format
->read_data
)(a
, buff
, size
, offset
);
578 * Close the file and release most resources.
580 * Be careful: client might just call read_new and then read_finish.
581 * Don't assume we actually read anything or performed any non-trivial
585 archive_read_close(struct archive
*_a
)
587 struct archive_read
*a
= (struct archive_read
*)_a
;
588 int r
= ARCHIVE_OK
, r1
= ARCHIVE_OK
;
591 __archive_check_magic(&a
->archive
, ARCHIVE_READ_MAGIC
,
592 ARCHIVE_STATE_ANY
, "archive_read_close");
593 a
->archive
.state
= ARCHIVE_STATE_CLOSED
;
595 /* Call cleanup functions registered by optional components. */
596 if (a
->cleanup_archive_extract
!= NULL
)
597 r
= (a
->cleanup_archive_extract
)(a
);
599 /* TODO: Clean up the formatters. */
601 /* Clean up the decompressors. */
602 n
= sizeof(a
->decompressors
)/sizeof(a
->decompressors
[0]);
603 for (i
= 0; i
< n
; i
++) {
604 if (a
->decompressors
[i
].finish
!= NULL
) {
605 r1
= (a
->decompressors
[i
].finish
)(a
);
611 /* Close the client stream. */
612 if (a
->client_closer
!= NULL
) {
613 r1
= ((a
->client_closer
)(&a
->archive
, a
->client_data
));
622 * Release memory and other resources.
624 #if ARCHIVE_API_VERSION > 1
627 /* Temporarily allow library to compile with either 1.x or 2.0 API. */
630 archive_read_finish(struct archive
*_a
)
632 struct archive_read
*a
= (struct archive_read
*)_a
;
637 __archive_check_magic(_a
, ARCHIVE_READ_MAGIC
, ARCHIVE_STATE_ANY
,
638 "archive_read_finish");
639 if (a
->archive
.state
!= ARCHIVE_STATE_CLOSED
)
640 r
= archive_read_close(&a
->archive
);
642 /* Cleanup format-specific data. */
643 slots
= sizeof(a
->formats
) / sizeof(a
->formats
[0]);
644 for (i
= 0; i
< slots
; i
++) {
645 a
->format
= &(a
->formats
[i
]);
646 if (a
->formats
[i
].cleanup
)
647 (a
->formats
[i
].cleanup
)(a
);
650 archive_string_free(&a
->archive
.error_string
);
652 archive_entry_free(a
->entry
);
653 a
->archive
.magic
= 0;
655 #if ARCHIVE_API_VERSION > 1
661 * Used internally by read format handlers to register their bid and
662 * initialization functions.
665 __archive_read_register_format(struct archive_read
*a
,
667 int (*bid
)(struct archive_read
*),
668 int (*read_header
)(struct archive_read
*, struct archive_entry
*),
669 int (*read_data
)(struct archive_read
*, const void **, size_t *, off_t
*),
670 int (*read_data_skip
)(struct archive_read
*),
671 int (*cleanup
)(struct archive_read
*))
675 __archive_check_magic(&a
->archive
,
676 ARCHIVE_READ_MAGIC
, ARCHIVE_STATE_NEW
,
677 "__archive_read_register_format");
679 number_slots
= sizeof(a
->formats
) / sizeof(a
->formats
[0]);
681 for (i
= 0; i
< number_slots
; i
++) {
682 if (a
->formats
[i
].bid
== bid
)
683 return (ARCHIVE_WARN
); /* We've already installed */
684 if (a
->formats
[i
].bid
== NULL
) {
685 a
->formats
[i
].bid
= bid
;
686 a
->formats
[i
].read_header
= read_header
;
687 a
->formats
[i
].read_data
= read_data
;
688 a
->formats
[i
].read_data_skip
= read_data_skip
;
689 a
->formats
[i
].cleanup
= cleanup
;
690 a
->formats
[i
].data
= format_data
;
695 __archive_errx(1, "Not enough slots for format registration");
696 return (ARCHIVE_FATAL
); /* Never actually called. */
700 * Used internally by decompression routines to register their bid and
701 * initialization functions.
703 struct decompressor_t
*
704 __archive_read_register_compression(struct archive_read
*a
,
705 int (*bid
)(const void *, size_t),
706 int (*init
)(struct archive_read
*, const void *, size_t))
710 __archive_check_magic(&a
->archive
,
711 ARCHIVE_READ_MAGIC
, ARCHIVE_STATE_NEW
,
712 "__archive_read_register_compression");
714 number_slots
= sizeof(a
->decompressors
) / sizeof(a
->decompressors
[0]);
716 for (i
= 0; i
< number_slots
; i
++) {
717 if (a
->decompressors
[i
].bid
== bid
)
718 return (a
->decompressors
+ i
);
719 if (a
->decompressors
[i
].bid
== NULL
) {
720 a
->decompressors
[i
].bid
= bid
;
721 a
->decompressors
[i
].init
= init
;
722 return (a
->decompressors
+ i
);
726 __archive_errx(1, "Not enough slots for compression registration");
727 return (NULL
); /* Never actually executed. */
730 /* used internally to simplify read-ahead */
732 __archive_read_ahead(struct archive_read
*a
, size_t len
)
736 if ((a
->decompressor
->read_ahead
)(a
, &h
, len
) < (ssize_t
)len
)