2 * Copyright (c) 2003-2004 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 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * This file contains the "essential" portions of the read API, that
29 * is, stuff that will probably always be used by any client that
30 * actually needs to read an archive. Optional pieces have been, as
31 * far as possible, separated out into separate files to avoid
32 * needlessly bloating statically-linked clients.
35 #include "archive_platform.h"
36 __FBSDID("$FreeBSD: src/lib/libarchive/archive_read.c,v 1.14 2005/04/06 04:19:30 kientzle Exp $");
45 #include "archive_entry.h"
46 #include "archive_private.h"
48 static int choose_decompressor(struct archive *, const void*, size_t);
49 static int choose_format(struct archive *);
52 * Allocate, initialize and return a struct archive object.
55 archive_read_new(void)
60 a = malloc(sizeof(*a));
61 memset(a, 0, sizeof(*a));
63 a->user_uid = geteuid();
64 a->magic = ARCHIVE_READ_MAGIC;
65 a->bytes_per_block = ARCHIVE_DEFAULT_BYTES_PER_BLOCK;
67 a->null_length = 1024;
68 nulls = malloc(a->null_length);
69 memset(nulls, 0, a->null_length);
72 a->state = ARCHIVE_STATE_NEW;
73 a->entry = archive_entry_new();
75 /* We always support uncompressed archives. */
76 archive_read_support_compression_none((struct archive*)a);
86 archive_read_set_bytes_per_block(struct archive *a, int bytes_per_block)
88 archive_check_magic(a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW);
89 if (bytes_per_block < 1)
91 a->bytes_per_block = bytes_per_block;
100 archive_read_open(struct archive *a, void *client_data,
101 archive_open_callback *opener, archive_read_callback *reader,
102 archive_close_callback *closer)
109 archive_check_magic(a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW);
113 "No reader function provided to archive_read_open");
115 a->client_reader = reader;
116 a->client_opener = opener;
117 a->client_closer = closer;
118 a->client_data = client_data;
120 a->state = ARCHIVE_STATE_HEADER;
122 /* Open data source. */
123 if (a->client_opener != NULL) {
124 e =(a->client_opener)(a, a->client_data);
129 /* Read first block now for format detection. */
130 bytes_read = (a->client_reader)(a, a->client_data, &buffer);
132 /* client_reader should have already set error information. */
134 return (ARCHIVE_FATAL);
136 /* An empty archive is a serious error. */
137 if (bytes_read == 0) {
138 archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT,
140 return (ARCHIVE_FATAL);
143 /* Select a decompression routine. */
144 high_bidder = choose_decompressor(a, buffer, bytes_read);
146 return (ARCHIVE_FATAL);
148 /* Initialize decompression routine with the first block of data. */
149 e = (a->decompressors[high_bidder].init)(a, buffer, bytes_read);
154 * Allow each registered decompression routine to bid on whether it
155 * wants to handle this stream. Return index of winning bidder.
158 choose_decompressor(struct archive *a, const void *buffer, size_t bytes_read)
160 int decompression_slots, i, bid, best_bid, best_bid_slot;
162 decompression_slots = sizeof(a->decompressors) /
163 sizeof(a->decompressors[0]);
168 for (i = 0; i < decompression_slots; i++) {
169 if (a->decompressors[i].bid) {
170 bid = (a->decompressors[i].bid)(buffer, bytes_read);
171 if ((bid > best_bid) || (best_bid_slot < 0)) {
179 * There were no bidders; this is a serious programmer error
180 * and demands a quick and definitive abort.
182 if (best_bid_slot < 0)
183 __archive_errx(1, "No decompressors were registered; you "
184 "must call at least one "
185 "archive_read_support_compression_XXX function in order "
186 "to successfully read an archive.");
189 * There were bidders, but no non-zero bids; this means we can't
190 * support this stream.
193 archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT,
194 "Unrecognized archive format");
195 return (ARCHIVE_FATAL);
198 return (best_bid_slot);
202 * Read header of next entry.
205 archive_read_next_header(struct archive *a, struct archive_entry **entryp)
207 struct archive_entry *entry;
210 archive_check_magic(a, ARCHIVE_READ_MAGIC,
211 ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA);
215 archive_entry_clear(entry);
216 archive_string_empty(&a->error_string);
219 * If client didn't consume entire data, skip any remainder
220 * (This is especially important for GNU incremental directories.)
222 if (a->state == ARCHIVE_STATE_DATA) {
223 ret = archive_read_data_skip(a);
224 if (ret == ARCHIVE_EOF) {
225 archive_set_error(a, EIO, "Premature end-of-file.");
226 a->state = ARCHIVE_STATE_FATAL;
227 return (ARCHIVE_FATAL);
229 if (ret != ARCHIVE_OK)
233 /* Record start-of-header. */
234 a->header_position = a->file_position;
236 slot = choose_format(a);
238 a->state = ARCHIVE_STATE_FATAL;
239 return (ARCHIVE_FATAL);
241 a->format = &(a->formats[slot]);
242 a->pformat_data = &(a->format->format_data);
243 ret = (a->format->read_header)(a, entry);
246 * EOF and FATAL are persistent at this layer. By
247 * modifying the state, we gaurantee that future calls to
248 * read a header or read data will fail.
252 a->state = ARCHIVE_STATE_EOF;
255 a->state = ARCHIVE_STATE_DATA;
258 a->state = ARCHIVE_STATE_DATA;
263 a->state = ARCHIVE_STATE_FATAL;
268 a->read_data_output_offset = 0;
269 a->read_data_remaining = 0;
274 * Allow each registered format to bid on whether it wants to handle
275 * the next entry. Return index of winning bidder.
278 choose_format(struct archive *a)
285 slots = sizeof(a->formats) / sizeof(a->formats[0]);
289 /* Set up a->format and a->pformat_data for convenience of bidders. */
290 a->format = &(a->formats[0]);
291 for (i = 0; i < slots; i++, a->format++) {
292 if (a->format->bid) {
293 a->pformat_data = &(a->format->format_data);
294 bid = (a->format->bid)(a);
295 if (bid == ARCHIVE_FATAL)
296 return (ARCHIVE_FATAL);
297 if ((bid > best_bid) || (best_bid_slot < 0)) {
305 * There were no bidders; this is a serious programmer error
306 * and demands a quick and definitive abort.
308 if (best_bid_slot < 0)
309 __archive_errx(1, "No formats were registered; you must "
310 "invoke at least one archive_read_support_format_XXX "
311 "function in order to successfully read an archive.");
314 * There were bidders, but no non-zero bids; this means we
315 * can't support this stream.
318 archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT,
319 "Unrecognized archive format");
320 return (ARCHIVE_FATAL);
323 return (best_bid_slot);
327 * Return the file offset (within the uncompressed data stream) where
328 * the last header started.
331 archive_read_header_position(struct archive *a)
333 return (a->header_position);
337 * Read data from an archive entry, using a read(2)-style interface.
338 * This is a convenience routine that just calls
339 * archive_read_data_block and copies the results into the client
340 * buffer, filling any gaps with zero bytes. Clients using this
341 * API can be completely ignorant of sparse-file issues; sparse files
342 * will simply be padded with nulls.
344 * DO NOT intermingle calls to this function and archive_read_data_block
345 * to read a single entry body.
348 archive_read_data(struct archive *a, void *buff, size_t s)
360 if (a->read_data_remaining <= 0) {
361 r = archive_read_data_block(a,
362 (const void **)&a->read_data_block,
363 &a->read_data_remaining,
364 &a->read_data_offset);
365 if (r == ARCHIVE_EOF)
371 if (a->read_data_offset < a->read_data_output_offset) {
373 a->read_data_output_offset - a->read_data_offset;
374 if (remaining > (off_t)s)
375 remaining = (off_t)s;
376 len = (size_t)remaining;
377 memset(dest, 0, len);
378 a->read_data_output_offset += len;
382 len = a->read_data_remaining;
385 memcpy(dest, a->read_data_block, len);
387 a->read_data_remaining -= len;
388 a->read_data_output_offset += len;
389 a->read_data_offset += len;
398 * Skip over all remaining data in this entry.
401 archive_read_data_skip(struct archive *a)
408 archive_check_magic(a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA);
410 if (a->format->read_data_skip != NULL)
411 r = (a->format->read_data_skip)(a);
413 while ((r = archive_read_data_block(a, &buff, &size, &offset))
418 if (r == ARCHIVE_EOF)
421 a->state = ARCHIVE_STATE_HEADER;
426 * Read the next block of entry data from the archive.
427 * This is a zero-copy interface; the client receives a pointer,
428 * size, and file offset of the next available block of data.
430 * Returns ARCHIVE_OK if the operation is successful, ARCHIVE_EOF if
431 * the end of entry is encountered.
434 archive_read_data_block(struct archive *a,
435 const void **buff, size_t *size, off_t *offset)
437 archive_check_magic(a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA);
439 if (a->format->read_data == NULL) {
440 archive_set_error(a, ARCHIVE_ERRNO_PROGRAMMER,
442 "No format_read_data_block function registered");
443 return (ARCHIVE_FATAL);
446 return (a->format->read_data)(a, buff, size, offset);
450 * Close the file and release most resources.
452 * Be careful: client might just call read_new and then read_finish.
453 * Don't assume we actually read anything or performed any non-trivial
457 archive_read_close(struct archive *a)
459 archive_check_magic(a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_ANY);
460 a->state = ARCHIVE_STATE_CLOSED;
462 /* Call cleanup functions registered by optional components. */
463 if (a->cleanup_archive_extract != NULL)
464 (a->cleanup_archive_extract)(a);
466 /* TODO: Finish the format processing. */
468 /* Close the input machinery. */
469 if (a->compression_finish != NULL)
470 (a->compression_finish)(a);
475 * Release memory and other resources.
478 archive_read_finish(struct archive *a)
483 archive_check_magic(a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_ANY);
484 if (a->state != ARCHIVE_STATE_CLOSED)
485 archive_read_close(a);
487 /* Cleanup format-specific data. */
488 slots = sizeof(a->formats) / sizeof(a->formats[0]);
489 for (i = 0; i < slots; i++) {
490 a->pformat_data = &(a->formats[i].format_data);
491 if (a->formats[i].cleanup)
492 (a->formats[i].cleanup)(a);
495 /* Casting a pointer to int allows us to remove 'const.' */
496 free((void *)(uintptr_t)(const void *)a->nulls);
497 archive_string_free(&a->error_string);
499 archive_entry_free(a->entry);
505 * Used internally by read format handlers to register their bid and
506 * initialization functions.
509 __archive_read_register_format(struct archive *a,
511 int (*bid)(struct archive *),
512 int (*read_header)(struct archive *, struct archive_entry *),
513 int (*read_data)(struct archive *, const void **, size_t *, off_t *),
514 int (*read_data_skip)(struct archive *),
515 int (*cleanup)(struct archive *))
519 archive_check_magic(a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW);
521 number_slots = sizeof(a->formats) / sizeof(a->formats[0]);
523 for (i = 0; i < number_slots; i++) {
524 if (a->formats[i].bid == bid)
525 return (ARCHIVE_WARN); /* We've already installed */
526 if (a->formats[i].bid == NULL) {
527 a->formats[i].bid = bid;
528 a->formats[i].read_header = read_header;
529 a->formats[i].read_data = read_data;
530 a->formats[i].read_data_skip = read_data_skip;
531 a->formats[i].cleanup = cleanup;
532 a->formats[i].format_data = format_data;
537 __archive_errx(1, "Not enough slots for format registration");
538 return (ARCHIVE_FATAL); /* Never actually called. */
542 * Used internally by decompression routines to register their bid and
543 * initialization functions.
546 __archive_read_register_compression(struct archive *a,
547 int (*bid)(const void *, size_t),
548 int (*init)(struct archive *, const void *, size_t))
552 archive_check_magic(a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW);
554 number_slots = sizeof(a->decompressors) / sizeof(a->decompressors[0]);
556 for (i = 0; i < number_slots; i++) {
557 if (a->decompressors[i].bid == bid)
558 return (ARCHIVE_OK); /* We've already installed */
559 if (a->decompressors[i].bid == NULL) {
560 a->decompressors[i].bid = bid;
561 a->decompressors[i].init = init;
566 __archive_errx(1, "Not enough slots for compression registration");
567 return (ARCHIVE_FATAL); /* Never actually executed. */