More -Wwrite-strings cleanup and make sure you can actually play it.
[dragonfly.git] / contrib / libarchive / archive_read.c
blob5ae0239db33eaec5bbdd66c3896ff9d7ca6ca6bc
1 /*-
2 * Copyright (c) 2003-2004 Tim Kientzle
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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 $");
38 #include <errno.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
44 #include "archive.h"
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.
54 struct archive *
55 archive_read_new(void)
57 struct archive *a;
58 char *nulls;
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);
70 a->nulls = nulls;
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);
78 return (a);
82 * Set the block size.
85 int
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)
90 bytes_per_block = 1;
91 a->bytes_per_block = bytes_per_block;
92 return (0);
97 * Open the archive
99 int
100 archive_read_open(struct archive *a, void *client_data,
101 archive_open_callback *opener, archive_read_callback *reader,
102 archive_close_callback *closer)
104 const void *buffer;
105 ssize_t bytes_read;
106 int high_bidder;
107 int e;
109 archive_check_magic(a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW);
111 if (reader == NULL)
112 __archive_errx(1,
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);
125 if (e != 0)
126 return (e);
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. */
133 if (bytes_read < 0)
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,
139 "Empty input file");
140 return (ARCHIVE_FATAL);
143 /* Select a decompression routine. */
144 high_bidder = choose_decompressor(a, buffer, bytes_read);
145 if (high_bidder < 0)
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);
150 return (e);
154 * Allow each registered decompression routine to bid on whether it
155 * wants to handle this stream. Return index of winning bidder.
157 static int
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]);
165 best_bid = -1;
166 best_bid_slot = -1;
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)) {
172 best_bid = bid;
173 best_bid_slot = i;
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.
192 if (best_bid < 1) {
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;
208 int slot, ret;
210 archive_check_magic(a, ARCHIVE_READ_MAGIC,
211 ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA);
213 *entryp = NULL;
214 entry = a->entry;
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)
230 return (ret);
233 /* Record start-of-header. */
234 a->header_position = a->file_position;
236 slot = choose_format(a);
237 if (slot < 0) {
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.
250 switch (ret) {
251 case ARCHIVE_EOF:
252 a->state = ARCHIVE_STATE_EOF;
253 break;
254 case ARCHIVE_OK:
255 a->state = ARCHIVE_STATE_DATA;
256 break;
257 case ARCHIVE_WARN:
258 a->state = ARCHIVE_STATE_DATA;
259 break;
260 case ARCHIVE_RETRY:
261 break;
262 case ARCHIVE_FATAL:
263 a->state = ARCHIVE_STATE_FATAL;
264 break;
267 *entryp = entry;
268 a->read_data_output_offset = 0;
269 a->read_data_remaining = 0;
270 return (ret);
274 * Allow each registered format to bid on whether it wants to handle
275 * the next entry. Return index of winning bidder.
277 static int
278 choose_format(struct archive *a)
280 int slots;
281 int i;
282 int bid, best_bid;
283 int best_bid_slot;
285 slots = sizeof(a->formats) / sizeof(a->formats[0]);
286 best_bid = -1;
287 best_bid_slot = -1;
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)) {
298 best_bid = bid;
299 best_bid_slot = i;
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.
317 if (best_bid < 1) {
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.
330 int64_t
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.
347 ssize_t
348 archive_read_data(struct archive *a, void *buff, size_t s)
350 off_t remaining;
351 char *dest;
352 size_t bytes_read;
353 size_t len;
354 int r;
356 bytes_read = 0;
357 dest = buff;
359 while (s > 0) {
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)
366 return (bytes_read);
367 if (r != ARCHIVE_OK)
368 return (r);
371 if (a->read_data_offset < a->read_data_output_offset) {
372 remaining =
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;
379 s -= len;
380 bytes_read += len;
381 } else {
382 len = a->read_data_remaining;
383 if (len > s)
384 len = s;
385 memcpy(dest, a->read_data_block, len);
386 s -= len;
387 a->read_data_remaining -= len;
388 a->read_data_output_offset += len;
389 a->read_data_offset += len;
390 dest += len;
391 bytes_read += len;
394 return (ARCHIVE_OK);
398 * Skip over all remaining data in this entry.
401 archive_read_data_skip(struct archive *a)
403 int r;
404 const void *buff;
405 ssize_t size;
406 off_t offset;
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);
412 else {
413 while ((r = archive_read_data_block(a, &buff, &size, &offset))
414 == ARCHIVE_OK)
418 if (r == ARCHIVE_EOF)
419 r = ARCHIVE_OK;
421 a->state = ARCHIVE_STATE_HEADER;
422 return (r);
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,
441 "Internal error: "
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
454 * initialization.
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);
471 return (ARCHIVE_OK);
475 * Release memory and other resources.
477 void
478 archive_read_finish(struct archive *a)
480 int i;
481 int slots;
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);
498 if (a->entry)
499 archive_entry_free(a->entry);
500 a->magic = 0;
501 free(a);
505 * Used internally by read format handlers to register their bid and
506 * initialization functions.
509 __archive_read_register_format(struct archive *a,
510 void *format_data,
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 *))
517 int i, number_slots;
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;
533 return (ARCHIVE_OK);
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))
550 int i, number_slots;
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;
562 return (ARCHIVE_OK);
566 __archive_errx(1, "Not enough slots for compression registration");
567 return (ARCHIVE_FATAL); /* Never actually executed. */