archive_read.c revision 232153
1228753Smm/*- 2232153Smm * Copyright (c) 2003-2011 Tim Kientzle 3228753Smm * All rights reserved. 4228753Smm * 5228753Smm * Redistribution and use in source and binary forms, with or without 6228753Smm * modification, are permitted provided that the following conditions 7228753Smm * are met: 8228753Smm * 1. Redistributions of source code must retain the above copyright 9228753Smm * notice, this list of conditions and the following disclaimer. 10228753Smm * 2. Redistributions in binary form must reproduce the above copyright 11228753Smm * notice, this list of conditions and the following disclaimer in the 12228753Smm * documentation and/or other materials provided with the distribution. 13228753Smm * 14228753Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 15228753Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16228753Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17228753Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 18228753Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19228753Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20228753Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21228753Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22228753Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23228753Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24228753Smm */ 25228753Smm 26228753Smm/* 27228753Smm * This file contains the "essential" portions of the read API, that 28228753Smm * is, stuff that will probably always be used by any client that 29228753Smm * actually needs to read an archive. Optional pieces have been, as 30228753Smm * far as possible, separated out into separate files to avoid 31228753Smm * needlessly bloating statically-linked clients. 32228753Smm */ 33228753Smm 34228753Smm#include "archive_platform.h" 35228763Smm__FBSDID("$FreeBSD: head/contrib/libarchive/libarchive/archive_read.c 232153 2012-02-25 10:58:02Z mm $"); 36228753Smm 37228753Smm#ifdef HAVE_ERRNO_H 38228753Smm#include <errno.h> 39228753Smm#endif 40228753Smm#include <stdio.h> 41228753Smm#ifdef HAVE_STDLIB_H 42228753Smm#include <stdlib.h> 43228753Smm#endif 44228753Smm#ifdef HAVE_STRING_H 45228753Smm#include <string.h> 46228753Smm#endif 47228753Smm#ifdef HAVE_UNISTD_H 48228753Smm#include <unistd.h> 49228753Smm#endif 50228753Smm 51228753Smm#include "archive.h" 52228753Smm#include "archive_entry.h" 53228753Smm#include "archive_private.h" 54228753Smm#include "archive_read_private.h" 55228753Smm 56228753Smm#define minimum(a, b) (a < b ? a : b) 57228753Smm 58232153Smmstatic int choose_filters(struct archive_read *); 59228753Smmstatic int choose_format(struct archive_read *); 60232153Smmstatic void free_filters(struct archive_read *); 61232153Smmstatic int close_filters(struct archive_read *); 62228753Smmstatic struct archive_vtable *archive_read_vtable(void); 63232153Smmstatic int64_t _archive_filter_bytes(struct archive *, int); 64232153Smmstatic int _archive_filter_code(struct archive *, int); 65232153Smmstatic const char *_archive_filter_name(struct archive *, int); 66232153Smmstatic int _archive_filter_count(struct archive *); 67228753Smmstatic int _archive_read_close(struct archive *); 68232153Smmstatic int _archive_read_data_block(struct archive *, 69232153Smm const void **, size_t *, int64_t *); 70228773Smmstatic int _archive_read_free(struct archive *); 71232153Smmstatic int _archive_read_next_header(struct archive *, 72232153Smm struct archive_entry **); 73232153Smmstatic int _archive_read_next_header2(struct archive *, 74232153Smm struct archive_entry *); 75232153Smmstatic int64_t advance_file_pointer(struct archive_read_filter *, int64_t); 76228753Smm 77228753Smmstatic struct archive_vtable * 78228753Smmarchive_read_vtable(void) 79228753Smm{ 80228753Smm static struct archive_vtable av; 81228753Smm static int inited = 0; 82228753Smm 83228753Smm if (!inited) { 84232153Smm av.archive_filter_bytes = _archive_filter_bytes; 85232153Smm av.archive_filter_code = _archive_filter_code; 86232153Smm av.archive_filter_name = _archive_filter_name; 87232153Smm av.archive_filter_count = _archive_filter_count; 88232153Smm av.archive_read_data_block = _archive_read_data_block; 89232153Smm av.archive_read_next_header = _archive_read_next_header; 90232153Smm av.archive_read_next_header2 = _archive_read_next_header2; 91228773Smm av.archive_free = _archive_read_free; 92228753Smm av.archive_close = _archive_read_close; 93232153Smm inited = 1; 94228753Smm } 95228753Smm return (&av); 96228753Smm} 97228753Smm 98228753Smm/* 99228753Smm * Allocate, initialize and return a struct archive object. 100228753Smm */ 101228753Smmstruct archive * 102228753Smmarchive_read_new(void) 103228753Smm{ 104228753Smm struct archive_read *a; 105228753Smm 106228753Smm a = (struct archive_read *)malloc(sizeof(*a)); 107228753Smm if (a == NULL) 108228753Smm return (NULL); 109228753Smm memset(a, 0, sizeof(*a)); 110228753Smm a->archive.magic = ARCHIVE_READ_MAGIC; 111228753Smm 112228753Smm a->archive.state = ARCHIVE_STATE_NEW; 113232153Smm a->entry = archive_entry_new2(&a->archive); 114228753Smm a->archive.vtable = archive_read_vtable(); 115228753Smm 116228753Smm return (&a->archive); 117228753Smm} 118228753Smm 119228753Smm/* 120228753Smm * Record the do-not-extract-to file. This belongs in archive_read_extract.c. 121228753Smm */ 122228753Smmvoid 123232153Smmarchive_read_extract_set_skip_file(struct archive *_a, int64_t d, int64_t i) 124228753Smm{ 125228753Smm struct archive_read *a = (struct archive_read *)_a; 126232153Smm 127232153Smm if (ARCHIVE_OK != __archive_check_magic(_a, ARCHIVE_READ_MAGIC, 128232153Smm ARCHIVE_STATE_ANY, "archive_read_extract_set_skip_file")) 129232153Smm return; 130232153Smm a->skip_file_set = 1; 131228753Smm a->skip_file_dev = d; 132228753Smm a->skip_file_ino = i; 133228753Smm} 134228753Smm 135228753Smm/* 136232153Smm * Open the archive 137228753Smm */ 138228753Smmint 139232153Smmarchive_read_open(struct archive *a, void *client_data, 140232153Smm archive_open_callback *client_opener, archive_read_callback *client_reader, 141232153Smm archive_close_callback *client_closer) 142228753Smm{ 143232153Smm /* Old archive_read_open() is just a thin shell around 144232153Smm * archive_read_open1. */ 145232153Smm archive_read_set_open_callback(a, client_opener); 146232153Smm archive_read_set_read_callback(a, client_reader); 147232153Smm archive_read_set_close_callback(a, client_closer); 148232153Smm archive_read_set_callback_data(a, client_data); 149232153Smm return archive_read_open1(a); 150228753Smm} 151228753Smm 152228753Smm 153228753Smmint 154232153Smmarchive_read_open2(struct archive *a, void *client_data, 155232153Smm archive_open_callback *client_opener, 156232153Smm archive_read_callback *client_reader, 157232153Smm archive_skip_callback *client_skipper, 158228753Smm archive_close_callback *client_closer) 159228753Smm{ 160232153Smm /* Old archive_read_open2() is just a thin shell around 161232153Smm * archive_read_open1. */ 162232153Smm archive_read_set_callback_data(a, client_data); 163232153Smm archive_read_set_open_callback(a, client_opener); 164232153Smm archive_read_set_read_callback(a, client_reader); 165232153Smm archive_read_set_skip_callback(a, client_skipper); 166232153Smm archive_read_set_close_callback(a, client_closer); 167232153Smm return archive_read_open1(a); 168228753Smm} 169228753Smm 170228753Smmstatic ssize_t 171228753Smmclient_read_proxy(struct archive_read_filter *self, const void **buff) 172228753Smm{ 173228753Smm ssize_t r; 174228753Smm r = (self->archive->client.reader)(&self->archive->archive, 175228753Smm self->data, buff); 176228753Smm return (r); 177228753Smm} 178228753Smm 179228753Smmstatic int64_t 180228753Smmclient_skip_proxy(struct archive_read_filter *self, int64_t request) 181228753Smm{ 182232153Smm if (request < 0) 183232153Smm __archive_errx(1, "Negative skip requested."); 184232153Smm if (request == 0) 185232153Smm return 0; 186228753Smm 187232153Smm if (self->archive->client.skipper != NULL) { 188232153Smm /* Seek requests over 1GiB are broken down into 189232153Smm * multiple seeks. This avoids overflows when the 190232153Smm * requests get passed through 32-bit arguments. */ 191232153Smm int64_t skip_limit = (int64_t)1 << 30; 192232153Smm int64_t total = 0; 193232153Smm for (;;) { 194232153Smm int64_t get, ask = request; 195232153Smm if (ask > skip_limit) 196232153Smm ask = skip_limit; 197232153Smm get = (self->archive->client.skipper)(&self->archive->archive, 198232153Smm self->data, ask); 199232153Smm if (get == 0) 200232153Smm return (total); 201232153Smm request -= get; 202232153Smm total += get; 203232153Smm } 204232153Smm return total; 205232153Smm } else if (self->archive->client.seeker != NULL 206232153Smm && request > 64 * 1024) { 207232153Smm /* If the client provided a seeker but not a skipper, 208232153Smm * we can use the seeker to skip forward. 209232153Smm * 210232153Smm * Note: This isn't always a good idea. The client 211232153Smm * skipper is allowed to skip by less than requested 212232153Smm * if it needs to maintain block alignment. The 213232153Smm * seeker is not allowed to play such games, so using 214232153Smm * the seeker here may be a performance loss compared 215232153Smm * to just reading and discarding. That's why we 216232153Smm * only do this for skips of over 64k. 217232153Smm */ 218232153Smm int64_t before = self->position; 219232153Smm int64_t after = (self->archive->client.seeker)(&self->archive->archive, 220232153Smm self->data, request, SEEK_CUR); 221232153Smm if (after != before + request) 222232153Smm return ARCHIVE_FATAL; 223232153Smm return after - before; 224228753Smm } 225232153Smm return 0; 226228753Smm} 227228753Smm 228232153Smmstatic int64_t 229232153Smmclient_seek_proxy(struct archive_read_filter *self, int64_t offset, int whence) 230232153Smm{ 231232153Smm /* DO NOT use the skipper here! If we transparently handled 232232153Smm * forward seek here by using the skipper, that will break 233232153Smm * other libarchive code that assumes a successful forward 234232153Smm * seek means it can also seek backwards. 235232153Smm */ 236232153Smm if (self->archive->client.seeker == NULL) 237232153Smm return (ARCHIVE_FAILED); 238232153Smm return (self->archive->client.seeker)(&self->archive->archive, 239232153Smm self->data, offset, whence); 240232153Smm} 241232153Smm 242228753Smmstatic int 243228753Smmclient_close_proxy(struct archive_read_filter *self) 244228753Smm{ 245228753Smm int r = ARCHIVE_OK; 246228753Smm 247228753Smm if (self->archive->client.closer != NULL) 248228753Smm r = (self->archive->client.closer)((struct archive *)self->archive, 249228753Smm self->data); 250228753Smm return (r); 251228753Smm} 252228753Smm 253232153Smmint 254232153Smmarchive_read_set_open_callback(struct archive *_a, 255232153Smm archive_open_callback *client_opener) 256232153Smm{ 257232153Smm struct archive_read *a = (struct archive_read *)_a; 258232153Smm archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW, 259232153Smm "archive_read_set_open_callback"); 260232153Smm a->client.opener = client_opener; 261232153Smm return ARCHIVE_OK; 262232153Smm} 263228753Smm 264228753Smmint 265232153Smmarchive_read_set_read_callback(struct archive *_a, 266232153Smm archive_read_callback *client_reader) 267232153Smm{ 268232153Smm struct archive_read *a = (struct archive_read *)_a; 269232153Smm archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW, 270232153Smm "archive_read_set_read_callback"); 271232153Smm a->client.reader = client_reader; 272232153Smm return ARCHIVE_OK; 273232153Smm} 274232153Smm 275232153Smmint 276232153Smmarchive_read_set_skip_callback(struct archive *_a, 277232153Smm archive_skip_callback *client_skipper) 278232153Smm{ 279232153Smm struct archive_read *a = (struct archive_read *)_a; 280232153Smm archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW, 281232153Smm "archive_read_set_skip_callback"); 282232153Smm a->client.skipper = client_skipper; 283232153Smm return ARCHIVE_OK; 284232153Smm} 285232153Smm 286232153Smmint 287232153Smmarchive_read_set_seek_callback(struct archive *_a, 288232153Smm archive_seek_callback *client_seeker) 289232153Smm{ 290232153Smm struct archive_read *a = (struct archive_read *)_a; 291232153Smm archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW, 292232153Smm "archive_read_set_seek_callback"); 293232153Smm a->client.seeker = client_seeker; 294232153Smm return ARCHIVE_OK; 295232153Smm} 296232153Smm 297232153Smmint 298232153Smmarchive_read_set_close_callback(struct archive *_a, 299228753Smm archive_close_callback *client_closer) 300228753Smm{ 301228753Smm struct archive_read *a = (struct archive_read *)_a; 302232153Smm archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW, 303232153Smm "archive_read_set_close_callback"); 304232153Smm a->client.closer = client_closer; 305232153Smm return ARCHIVE_OK; 306232153Smm} 307232153Smm 308232153Smmint 309232153Smmarchive_read_set_callback_data(struct archive *_a, void *client_data) 310232153Smm{ 311232153Smm struct archive_read *a = (struct archive_read *)_a; 312232153Smm archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW, 313232153Smm "archive_read_set_callback_data"); 314232153Smm a->client.data = client_data; 315232153Smm return ARCHIVE_OK; 316232153Smm} 317232153Smm 318232153Smmint 319232153Smmarchive_read_open1(struct archive *_a) 320232153Smm{ 321232153Smm struct archive_read *a = (struct archive_read *)_a; 322228753Smm struct archive_read_filter *filter; 323232153Smm int slot, e; 324228753Smm 325232153Smm archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW, 326228753Smm "archive_read_open"); 327228753Smm archive_clear_error(&a->archive); 328228753Smm 329232153Smm if (a->client.reader == NULL) { 330232153Smm archive_set_error(&a->archive, EINVAL, 331228753Smm "No reader function provided to archive_read_open"); 332232153Smm a->archive.state = ARCHIVE_STATE_FATAL; 333232153Smm return (ARCHIVE_FATAL); 334232153Smm } 335228753Smm 336228753Smm /* Open data source. */ 337232153Smm if (a->client.opener != NULL) { 338232153Smm e =(a->client.opener)(&a->archive, a->client.data); 339228753Smm if (e != 0) { 340228753Smm /* If the open failed, call the closer to clean up. */ 341232153Smm if (a->client.closer) 342232153Smm (a->client.closer)(&a->archive, a->client.data); 343228753Smm return (e); 344228753Smm } 345228753Smm } 346228753Smm 347228753Smm filter = calloc(1, sizeof(*filter)); 348228753Smm if (filter == NULL) 349228753Smm return (ARCHIVE_FATAL); 350228753Smm filter->bidder = NULL; 351228753Smm filter->upstream = NULL; 352228753Smm filter->archive = a; 353232153Smm filter->data = a->client.data; 354228753Smm filter->read = client_read_proxy; 355228753Smm filter->skip = client_skip_proxy; 356232153Smm filter->seek = client_seek_proxy; 357228753Smm filter->close = client_close_proxy; 358228753Smm filter->name = "none"; 359228753Smm filter->code = ARCHIVE_COMPRESSION_NONE; 360228753Smm a->filter = filter; 361228753Smm 362228753Smm /* Build out the input pipeline. */ 363232153Smm e = choose_filters(a); 364232153Smm if (e < ARCHIVE_WARN) { 365232153Smm a->archive.state = ARCHIVE_STATE_FATAL; 366232153Smm return (ARCHIVE_FATAL); 367232153Smm } 368228753Smm 369232153Smm slot = choose_format(a); 370232153Smm if (slot < 0) { 371232153Smm close_filters(a); 372232153Smm a->archive.state = ARCHIVE_STATE_FATAL; 373232153Smm return (ARCHIVE_FATAL); 374232153Smm } 375232153Smm a->format = &(a->formats[slot]); 376232153Smm 377232153Smm a->archive.state = ARCHIVE_STATE_HEADER; 378228753Smm return (e); 379228753Smm} 380228753Smm 381228753Smm/* 382228753Smm * Allow each registered stream transform to bid on whether 383228753Smm * it wants to handle this stream. Repeat until we've finished 384228753Smm * building the pipeline. 385228753Smm */ 386228753Smmstatic int 387232153Smmchoose_filters(struct archive_read *a) 388228753Smm{ 389228753Smm int number_bidders, i, bid, best_bid; 390228753Smm struct archive_read_filter_bidder *bidder, *best_bidder; 391228753Smm struct archive_read_filter *filter; 392228753Smm ssize_t avail; 393228753Smm int r; 394228753Smm 395228753Smm for (;;) { 396228753Smm number_bidders = sizeof(a->bidders) / sizeof(a->bidders[0]); 397228753Smm 398228753Smm best_bid = 0; 399228753Smm best_bidder = NULL; 400228753Smm 401228753Smm bidder = a->bidders; 402228753Smm for (i = 0; i < number_bidders; i++, bidder++) { 403228753Smm if (bidder->bid != NULL) { 404228753Smm bid = (bidder->bid)(bidder, a->filter); 405228753Smm if (bid > best_bid) { 406228753Smm best_bid = bid; 407228753Smm best_bidder = bidder; 408228753Smm } 409228753Smm } 410228753Smm } 411228753Smm 412228753Smm /* If no bidder, we're done. */ 413228753Smm if (best_bidder == NULL) { 414232153Smm /* Verify the filter by asking it for some data. */ 415228753Smm __archive_read_filter_ahead(a->filter, 1, &avail); 416228753Smm if (avail < 0) { 417232153Smm close_filters(a); 418232153Smm free_filters(a); 419228753Smm return (ARCHIVE_FATAL); 420228753Smm } 421228753Smm a->archive.compression_name = a->filter->name; 422228753Smm a->archive.compression_code = a->filter->code; 423228753Smm return (ARCHIVE_OK); 424228753Smm } 425228753Smm 426228753Smm filter 427228753Smm = (struct archive_read_filter *)calloc(1, sizeof(*filter)); 428228753Smm if (filter == NULL) 429228753Smm return (ARCHIVE_FATAL); 430228753Smm filter->bidder = best_bidder; 431228753Smm filter->archive = a; 432228753Smm filter->upstream = a->filter; 433228753Smm a->filter = filter; 434228753Smm r = (best_bidder->init)(a->filter); 435228753Smm if (r != ARCHIVE_OK) { 436232153Smm close_filters(a); 437232153Smm free_filters(a); 438232153Smm return (ARCHIVE_FATAL); 439228753Smm } 440228753Smm } 441228753Smm} 442228753Smm 443228753Smm/* 444228753Smm * Read header of next entry. 445228753Smm */ 446232153Smmstatic int 447232153Smm_archive_read_next_header2(struct archive *_a, struct archive_entry *entry) 448228753Smm{ 449228753Smm struct archive_read *a = (struct archive_read *)_a; 450232153Smm int r1 = ARCHIVE_OK, r2; 451228753Smm 452232153Smm archive_check_magic(_a, ARCHIVE_READ_MAGIC, 453228753Smm ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA, 454228753Smm "archive_read_next_header"); 455228753Smm 456228753Smm archive_entry_clear(entry); 457228753Smm archive_clear_error(&a->archive); 458228753Smm 459228753Smm /* 460228753Smm * If client didn't consume entire data, skip any remainder 461228753Smm * (This is especially important for GNU incremental directories.) 462228753Smm */ 463228753Smm if (a->archive.state == ARCHIVE_STATE_DATA) { 464232153Smm r1 = archive_read_data_skip(&a->archive); 465232153Smm if (r1 == ARCHIVE_EOF) 466232153Smm archive_set_error(&a->archive, EIO, 467232153Smm "Premature end-of-file."); 468232153Smm if (r1 == ARCHIVE_EOF || r1 == ARCHIVE_FATAL) { 469228753Smm a->archive.state = ARCHIVE_STATE_FATAL; 470228753Smm return (ARCHIVE_FATAL); 471228753Smm } 472228753Smm } 473228753Smm 474232153Smm /* Record start-of-header offset in uncompressed stream. */ 475232153Smm a->header_position = a->filter->position; 476228753Smm 477232153Smm ++_a->file_count; 478232153Smm r2 = (a->format->read_header)(a, entry); 479228753Smm 480228753Smm /* 481228753Smm * EOF and FATAL are persistent at this layer. By 482228753Smm * modifying the state, we guarantee that future calls to 483228753Smm * read a header or read data will fail. 484228753Smm */ 485232153Smm switch (r2) { 486228753Smm case ARCHIVE_EOF: 487228753Smm a->archive.state = ARCHIVE_STATE_EOF; 488232153Smm --_a->file_count;/* Revert a file counter. */ 489228753Smm break; 490228753Smm case ARCHIVE_OK: 491228753Smm a->archive.state = ARCHIVE_STATE_DATA; 492228753Smm break; 493228753Smm case ARCHIVE_WARN: 494228753Smm a->archive.state = ARCHIVE_STATE_DATA; 495228753Smm break; 496228753Smm case ARCHIVE_RETRY: 497228753Smm break; 498228753Smm case ARCHIVE_FATAL: 499228753Smm a->archive.state = ARCHIVE_STATE_FATAL; 500228753Smm break; 501228753Smm } 502228753Smm 503228753Smm a->read_data_output_offset = 0; 504228753Smm a->read_data_remaining = 0; 505232153Smm /* EOF always wins; otherwise return the worst error. */ 506232153Smm return (r2 < r1 || r2 == ARCHIVE_EOF) ? r2 : r1; 507228753Smm} 508228753Smm 509228753Smmint 510232153Smm_archive_read_next_header(struct archive *_a, struct archive_entry **entryp) 511228753Smm{ 512228753Smm int ret; 513228753Smm struct archive_read *a = (struct archive_read *)_a; 514228753Smm *entryp = NULL; 515232153Smm ret = _archive_read_next_header2(_a, a->entry); 516228753Smm *entryp = a->entry; 517228753Smm return ret; 518228753Smm} 519228753Smm 520228753Smm/* 521228753Smm * Allow each registered format to bid on whether it wants to handle 522228753Smm * the next entry. Return index of winning bidder. 523228753Smm */ 524228753Smmstatic int 525228753Smmchoose_format(struct archive_read *a) 526228753Smm{ 527228753Smm int slots; 528228753Smm int i; 529228753Smm int bid, best_bid; 530228753Smm int best_bid_slot; 531228753Smm 532228753Smm slots = sizeof(a->formats) / sizeof(a->formats[0]); 533228753Smm best_bid = -1; 534228753Smm best_bid_slot = -1; 535228753Smm 536232153Smm /* Set up a->format for convenience of bidders. */ 537228753Smm a->format = &(a->formats[0]); 538228753Smm for (i = 0; i < slots; i++, a->format++) { 539228753Smm if (a->format->bid) { 540232153Smm bid = (a->format->bid)(a, best_bid); 541228753Smm if (bid == ARCHIVE_FATAL) 542228753Smm return (ARCHIVE_FATAL); 543232153Smm if (a->filter->position != 0) 544232153Smm __archive_read_seek(a, 0, SEEK_SET); 545228753Smm if ((bid > best_bid) || (best_bid_slot < 0)) { 546228753Smm best_bid = bid; 547228753Smm best_bid_slot = i; 548228753Smm } 549228753Smm } 550228753Smm } 551228753Smm 552228753Smm /* 553228753Smm * There were no bidders; this is a serious programmer error 554228753Smm * and demands a quick and definitive abort. 555228753Smm */ 556232153Smm if (best_bid_slot < 0) { 557232153Smm archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, 558232153Smm "No formats registered"); 559232153Smm return (ARCHIVE_FATAL); 560232153Smm } 561228753Smm 562228753Smm /* 563228753Smm * There were bidders, but no non-zero bids; this means we 564228753Smm * can't support this stream. 565228753Smm */ 566228753Smm if (best_bid < 1) { 567228753Smm archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, 568228753Smm "Unrecognized archive format"); 569228753Smm return (ARCHIVE_FATAL); 570228753Smm } 571228753Smm 572228753Smm return (best_bid_slot); 573228753Smm} 574228753Smm 575228753Smm/* 576228753Smm * Return the file offset (within the uncompressed data stream) where 577228753Smm * the last header started. 578228753Smm */ 579228753Smmint64_t 580228753Smmarchive_read_header_position(struct archive *_a) 581228753Smm{ 582228753Smm struct archive_read *a = (struct archive_read *)_a; 583232153Smm archive_check_magic(_a, ARCHIVE_READ_MAGIC, 584228753Smm ARCHIVE_STATE_ANY, "archive_read_header_position"); 585228753Smm return (a->header_position); 586228753Smm} 587228753Smm 588228753Smm/* 589228753Smm * Read data from an archive entry, using a read(2)-style interface. 590228753Smm * This is a convenience routine that just calls 591228753Smm * archive_read_data_block and copies the results into the client 592228753Smm * buffer, filling any gaps with zero bytes. Clients using this 593228753Smm * API can be completely ignorant of sparse-file issues; sparse files 594228753Smm * will simply be padded with nulls. 595228753Smm * 596228753Smm * DO NOT intermingle calls to this function and archive_read_data_block 597228753Smm * to read a single entry body. 598228753Smm */ 599228753Smmssize_t 600228753Smmarchive_read_data(struct archive *_a, void *buff, size_t s) 601228753Smm{ 602228753Smm struct archive_read *a = (struct archive_read *)_a; 603228753Smm char *dest; 604228753Smm const void *read_buf; 605228753Smm size_t bytes_read; 606228753Smm size_t len; 607228753Smm int r; 608228753Smm 609228753Smm bytes_read = 0; 610228753Smm dest = (char *)buff; 611228753Smm 612228753Smm while (s > 0) { 613228753Smm if (a->read_data_remaining == 0) { 614228753Smm read_buf = a->read_data_block; 615232153Smm r = _archive_read_data_block(&a->archive, &read_buf, 616228753Smm &a->read_data_remaining, &a->read_data_offset); 617228753Smm a->read_data_block = read_buf; 618228753Smm if (r == ARCHIVE_EOF) 619228753Smm return (bytes_read); 620228753Smm /* 621228753Smm * Error codes are all negative, so the status 622228753Smm * return here cannot be confused with a valid 623228753Smm * byte count. (ARCHIVE_OK is zero.) 624228753Smm */ 625228753Smm if (r < ARCHIVE_OK) 626228753Smm return (r); 627228753Smm } 628228753Smm 629228753Smm if (a->read_data_offset < a->read_data_output_offset) { 630228753Smm archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, 631228753Smm "Encountered out-of-order sparse blocks"); 632228753Smm return (ARCHIVE_RETRY); 633228753Smm } 634228753Smm 635228753Smm /* Compute the amount of zero padding needed. */ 636232153Smm if (a->read_data_output_offset + (int64_t)s < 637228753Smm a->read_data_offset) { 638228753Smm len = s; 639228753Smm } else if (a->read_data_output_offset < 640228753Smm a->read_data_offset) { 641228753Smm len = a->read_data_offset - 642228753Smm a->read_data_output_offset; 643228753Smm } else 644228753Smm len = 0; 645228753Smm 646228753Smm /* Add zeroes. */ 647228753Smm memset(dest, 0, len); 648228753Smm s -= len; 649228753Smm a->read_data_output_offset += len; 650228753Smm dest += len; 651228753Smm bytes_read += len; 652228753Smm 653228753Smm /* Copy data if there is any space left. */ 654228753Smm if (s > 0) { 655228753Smm len = a->read_data_remaining; 656228753Smm if (len > s) 657228753Smm len = s; 658228753Smm memcpy(dest, a->read_data_block, len); 659228753Smm s -= len; 660228753Smm a->read_data_block += len; 661228753Smm a->read_data_remaining -= len; 662228753Smm a->read_data_output_offset += len; 663228753Smm a->read_data_offset += len; 664228753Smm dest += len; 665228753Smm bytes_read += len; 666228753Smm } 667228753Smm } 668228753Smm return (bytes_read); 669228753Smm} 670228753Smm 671228753Smm/* 672228753Smm * Skip over all remaining data in this entry. 673228753Smm */ 674228753Smmint 675228753Smmarchive_read_data_skip(struct archive *_a) 676228753Smm{ 677228753Smm struct archive_read *a = (struct archive_read *)_a; 678228753Smm int r; 679228753Smm const void *buff; 680228753Smm size_t size; 681232153Smm int64_t offset; 682228753Smm 683232153Smm archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA, 684228753Smm "archive_read_data_skip"); 685228753Smm 686228753Smm if (a->format->read_data_skip != NULL) 687228753Smm r = (a->format->read_data_skip)(a); 688228753Smm else { 689228753Smm while ((r = archive_read_data_block(&a->archive, 690228753Smm &buff, &size, &offset)) 691228753Smm == ARCHIVE_OK) 692228753Smm ; 693228753Smm } 694228753Smm 695228753Smm if (r == ARCHIVE_EOF) 696228753Smm r = ARCHIVE_OK; 697228753Smm 698228753Smm a->archive.state = ARCHIVE_STATE_HEADER; 699228753Smm return (r); 700228753Smm} 701228753Smm 702228753Smm/* 703228753Smm * Read the next block of entry data from the archive. 704228753Smm * This is a zero-copy interface; the client receives a pointer, 705228753Smm * size, and file offset of the next available block of data. 706228753Smm * 707228753Smm * Returns ARCHIVE_OK if the operation is successful, ARCHIVE_EOF if 708228753Smm * the end of entry is encountered. 709228753Smm */ 710232153Smmstatic int 711232153Smm_archive_read_data_block(struct archive *_a, 712232153Smm const void **buff, size_t *size, int64_t *offset) 713228753Smm{ 714228753Smm struct archive_read *a = (struct archive_read *)_a; 715232153Smm archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA, 716228753Smm "archive_read_data_block"); 717228753Smm 718228753Smm if (a->format->read_data == NULL) { 719228753Smm archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER, 720228753Smm "Internal error: " 721228753Smm "No format_read_data_block function registered"); 722228753Smm return (ARCHIVE_FATAL); 723228753Smm } 724228753Smm 725228753Smm return (a->format->read_data)(a, buff, size, offset); 726228753Smm} 727228753Smm 728232153Smmstatic int 729232153Smmclose_filters(struct archive_read *a) 730232153Smm{ 731232153Smm struct archive_read_filter *f = a->filter; 732232153Smm int r = ARCHIVE_OK; 733232153Smm /* Close each filter in the pipeline. */ 734232153Smm while (f != NULL) { 735232153Smm struct archive_read_filter *t = f->upstream; 736232153Smm if (!f->closed && f->close != NULL) { 737232153Smm int r1 = (f->close)(f); 738232153Smm f->closed = 1; 739232153Smm if (r1 < r) 740232153Smm r = r1; 741232153Smm } 742232153Smm free(f->buffer); 743232153Smm f->buffer = NULL; 744232153Smm f = t; 745232153Smm } 746232153Smm return r; 747232153Smm} 748232153Smm 749232153Smmstatic void 750232153Smmfree_filters(struct archive_read *a) 751232153Smm{ 752232153Smm while (a->filter != NULL) { 753232153Smm struct archive_read_filter *t = a->filter->upstream; 754232153Smm free(a->filter); 755232153Smm a->filter = t; 756232153Smm } 757232153Smm} 758232153Smm 759228753Smm/* 760232153Smm * return the count of # of filters in use 761228753Smm */ 762228753Smmstatic int 763232153Smm_archive_filter_count(struct archive *_a) 764232153Smm{ 765232153Smm struct archive_read *a = (struct archive_read *)_a; 766232153Smm struct archive_read_filter *p = a->filter; 767232153Smm int count = 0; 768232153Smm while(p) { 769232153Smm count++; 770232153Smm p = p->upstream; 771232153Smm } 772232153Smm return count; 773232153Smm} 774232153Smm 775232153Smm/* 776232153Smm * Close the file and all I/O. 777232153Smm */ 778232153Smmstatic int 779228753Smm_archive_read_close(struct archive *_a) 780228753Smm{ 781228753Smm struct archive_read *a = (struct archive_read *)_a; 782228753Smm int r = ARCHIVE_OK, r1 = ARCHIVE_OK; 783228753Smm 784232153Smm archive_check_magic(&a->archive, ARCHIVE_READ_MAGIC, 785232153Smm ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_read_close"); 786232153Smm if (a->archive.state == ARCHIVE_STATE_CLOSED) 787232153Smm return (ARCHIVE_OK); 788228753Smm archive_clear_error(&a->archive); 789228753Smm a->archive.state = ARCHIVE_STATE_CLOSED; 790228753Smm 791228753Smm /* TODO: Clean up the formatters. */ 792228753Smm 793228753Smm /* Release the filter objects. */ 794232153Smm r1 = close_filters(a); 795228753Smm if (r1 < r) 796228753Smm r = r1; 797228753Smm 798228753Smm return (r); 799228753Smm} 800228753Smm 801228753Smm/* 802228753Smm * Release memory and other resources. 803228753Smm */ 804228753Smmstatic int 805228773Smm_archive_read_free(struct archive *_a) 806228753Smm{ 807228753Smm struct archive_read *a = (struct archive_read *)_a; 808232153Smm int i, n; 809228753Smm int slots; 810228753Smm int r = ARCHIVE_OK; 811228753Smm 812232153Smm if (_a == NULL) 813232153Smm return (ARCHIVE_OK); 814232153Smm archive_check_magic(_a, ARCHIVE_READ_MAGIC, 815232153Smm ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_read_free"); 816232153Smm if (a->archive.state != ARCHIVE_STATE_CLOSED 817232153Smm && a->archive.state != ARCHIVE_STATE_FATAL) 818228753Smm r = archive_read_close(&a->archive); 819228753Smm 820232153Smm /* Call cleanup functions registered by optional components. */ 821232153Smm if (a->cleanup_archive_extract != NULL) 822232153Smm r = (a->cleanup_archive_extract)(a); 823232153Smm 824228753Smm /* Cleanup format-specific data. */ 825228753Smm slots = sizeof(a->formats) / sizeof(a->formats[0]); 826228753Smm for (i = 0; i < slots; i++) { 827228753Smm a->format = &(a->formats[i]); 828228753Smm if (a->formats[i].cleanup) 829228753Smm (a->formats[i].cleanup)(a); 830228753Smm } 831228753Smm 832232153Smm /* Free the filters */ 833232153Smm free_filters(a); 834232153Smm 835232153Smm /* Release the bidder objects. */ 836232153Smm n = sizeof(a->bidders)/sizeof(a->bidders[0]); 837232153Smm for (i = 0; i < n; i++) { 838232153Smm if (a->bidders[i].free != NULL) { 839232153Smm int r1 = (a->bidders[i].free)(&a->bidders[i]); 840232153Smm if (r1 < r) 841232153Smm r = r1; 842232153Smm } 843232153Smm } 844232153Smm 845228753Smm archive_string_free(&a->archive.error_string); 846228753Smm if (a->entry) 847228753Smm archive_entry_free(a->entry); 848228753Smm a->archive.magic = 0; 849232153Smm __archive_clean(&a->archive); 850228753Smm free(a); 851228753Smm return (r); 852228753Smm} 853228753Smm 854232153Smmstatic struct archive_read_filter * 855232153Smmget_filter(struct archive *_a, int n) 856232153Smm{ 857232153Smm struct archive_read *a = (struct archive_read *)_a; 858232153Smm struct archive_read_filter *f = a->filter; 859232153Smm /* We use n == -1 for 'the last filter', which is always the client proxy. */ 860232153Smm if (n == -1 && f != NULL) { 861232153Smm struct archive_read_filter *last = f; 862232153Smm f = f->upstream; 863232153Smm while (f != NULL) { 864232153Smm last = f; 865232153Smm f = f->upstream; 866232153Smm } 867232153Smm return (last); 868232153Smm } 869232153Smm if (n < 0) 870232153Smm return NULL; 871232153Smm while (n > 0 && f != NULL) { 872232153Smm f = f->upstream; 873232153Smm --n; 874232153Smm } 875232153Smm return (f); 876232153Smm} 877232153Smm 878232153Smmstatic int 879232153Smm_archive_filter_code(struct archive *_a, int n) 880232153Smm{ 881232153Smm struct archive_read_filter *f = get_filter(_a, n); 882232153Smm return f == NULL ? -1 : f->code; 883232153Smm} 884232153Smm 885232153Smmstatic const char * 886232153Smm_archive_filter_name(struct archive *_a, int n) 887232153Smm{ 888232153Smm struct archive_read_filter *f = get_filter(_a, n); 889232153Smm return f == NULL ? NULL : f->name; 890232153Smm} 891232153Smm 892232153Smmstatic int64_t 893232153Smm_archive_filter_bytes(struct archive *_a, int n) 894232153Smm{ 895232153Smm struct archive_read_filter *f = get_filter(_a, n); 896232153Smm return f == NULL ? -1 : f->position; 897232153Smm} 898232153Smm 899228753Smm/* 900228753Smm * Used internally by read format handlers to register their bid and 901228753Smm * initialization functions. 902228753Smm */ 903228753Smmint 904228753Smm__archive_read_register_format(struct archive_read *a, 905228753Smm void *format_data, 906228753Smm const char *name, 907232153Smm int (*bid)(struct archive_read *, int), 908228753Smm int (*options)(struct archive_read *, const char *, const char *), 909228753Smm int (*read_header)(struct archive_read *, struct archive_entry *), 910232153Smm int (*read_data)(struct archive_read *, const void **, size_t *, int64_t *), 911228753Smm int (*read_data_skip)(struct archive_read *), 912228753Smm int (*cleanup)(struct archive_read *)) 913228753Smm{ 914228753Smm int i, number_slots; 915228753Smm 916232153Smm archive_check_magic(&a->archive, 917228753Smm ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW, 918228753Smm "__archive_read_register_format"); 919228753Smm 920228753Smm number_slots = sizeof(a->formats) / sizeof(a->formats[0]); 921228753Smm 922228753Smm for (i = 0; i < number_slots; i++) { 923228753Smm if (a->formats[i].bid == bid) 924228753Smm return (ARCHIVE_WARN); /* We've already installed */ 925228753Smm if (a->formats[i].bid == NULL) { 926228753Smm a->formats[i].bid = bid; 927228753Smm a->formats[i].options = options; 928228753Smm a->formats[i].read_header = read_header; 929228753Smm a->formats[i].read_data = read_data; 930228753Smm a->formats[i].read_data_skip = read_data_skip; 931228753Smm a->formats[i].cleanup = cleanup; 932228753Smm a->formats[i].data = format_data; 933228753Smm a->formats[i].name = name; 934228753Smm return (ARCHIVE_OK); 935228753Smm } 936228753Smm } 937228753Smm 938232153Smm archive_set_error(&a->archive, ENOMEM, 939232153Smm "Not enough slots for format registration"); 940232153Smm return (ARCHIVE_FATAL); 941228753Smm} 942228753Smm 943228753Smm/* 944228753Smm * Used internally by decompression routines to register their bid and 945228753Smm * initialization functions. 946228753Smm */ 947232153Smmint 948232153Smm__archive_read_get_bidder(struct archive_read *a, 949232153Smm struct archive_read_filter_bidder **bidder) 950228753Smm{ 951228753Smm int i, number_slots; 952228753Smm 953228753Smm number_slots = sizeof(a->bidders) / sizeof(a->bidders[0]); 954228753Smm 955228753Smm for (i = 0; i < number_slots; i++) { 956228753Smm if (a->bidders[i].bid == NULL) { 957228753Smm memset(a->bidders + i, 0, sizeof(a->bidders[0])); 958232153Smm *bidder = (a->bidders + i); 959232153Smm return (ARCHIVE_OK); 960228753Smm } 961228753Smm } 962228753Smm 963232153Smm archive_set_error(&a->archive, ENOMEM, 964232153Smm "Not enough slots for filter registration"); 965232153Smm return (ARCHIVE_FATAL); 966228753Smm} 967228753Smm 968228753Smm/* 969232153Smm * The next section implements the peek/consume internal I/O 970232153Smm * system used by archive readers. This system allows simple 971232153Smm * read-ahead for consumers while preserving zero-copy operation 972232153Smm * most of the time. 973228753Smm * 974232153Smm * The two key operations: 975232153Smm * * The read-ahead function returns a pointer to a block of data 976232153Smm * that satisfies a minimum request. 977232153Smm * * The consume function advances the file pointer. 978232153Smm * 979228753Smm * In the ideal case, filters generate blocks of data 980228753Smm * and __archive_read_ahead() just returns pointers directly into 981228753Smm * those blocks. Then __archive_read_consume() just bumps those 982228753Smm * pointers. Only if your request would span blocks does the I/O 983228753Smm * layer use a copy buffer to provide you with a contiguous block of 984232153Smm * data. 985228753Smm * 986228753Smm * A couple of useful idioms: 987228753Smm * * "I just want some data." Ask for 1 byte and pay attention to 988228753Smm * the "number of bytes available" from __archive_read_ahead(). 989232153Smm * Consume whatever you actually use. 990228753Smm * * "I want to output a large block of data." As above, ask for 1 byte, 991232153Smm * emit all that's available (up to whatever limit you have), consume 992232153Smm * it all, then repeat until you're done. This effectively means that 993232153Smm * you're passing along the blocks that came from your provider. 994228753Smm * * "I want to peek ahead by a large amount." Ask for 4k or so, then 995228753Smm * double and repeat until you get an error or have enough. Note 996228753Smm * that the I/O layer will likely end up expanding its copy buffer 997228753Smm * to fit your request, so use this technique cautiously. This 998228753Smm * technique is used, for example, by some of the format tasting 999228753Smm * code that has uncertain look-ahead needs. 1000228753Smm */ 1001228753Smm 1002228753Smm/* 1003228753Smm * Looks ahead in the input stream: 1004228753Smm * * If 'avail' pointer is provided, that returns number of bytes available 1005228753Smm * in the current buffer, which may be much larger than requested. 1006228753Smm * * If end-of-file, *avail gets set to zero. 1007228753Smm * * If error, *avail gets error code. 1008232153Smm * * If request can be met, returns pointer to data. 1009232153Smm * * If minimum request cannot be met, returns NULL. 1010228753Smm * 1011228753Smm * Note: If you just want "some data", ask for 1 byte and pay attention 1012228753Smm * to *avail, which will have the actual amount available. If you 1013228753Smm * know exactly how many bytes you need, just ask for that and treat 1014228753Smm * a NULL return as an error. 1015228753Smm * 1016228753Smm * Important: This does NOT move the file pointer. See 1017228753Smm * __archive_read_consume() below. 1018228753Smm */ 1019228753Smmconst void * 1020228753Smm__archive_read_ahead(struct archive_read *a, size_t min, ssize_t *avail) 1021228753Smm{ 1022228753Smm return (__archive_read_filter_ahead(a->filter, min, avail)); 1023228753Smm} 1024228753Smm 1025228753Smmconst void * 1026228753Smm__archive_read_filter_ahead(struct archive_read_filter *filter, 1027228753Smm size_t min, ssize_t *avail) 1028228753Smm{ 1029228753Smm ssize_t bytes_read; 1030228753Smm size_t tocopy; 1031228753Smm 1032228753Smm if (filter->fatal) { 1033228753Smm if (avail) 1034228753Smm *avail = ARCHIVE_FATAL; 1035228753Smm return (NULL); 1036228753Smm } 1037228753Smm 1038228753Smm /* 1039228753Smm * Keep pulling more data until we can satisfy the request. 1040228753Smm */ 1041228753Smm for (;;) { 1042228753Smm 1043228753Smm /* 1044228753Smm * If we can satisfy from the copy buffer (and the 1045228753Smm * copy buffer isn't empty), we're done. In particular, 1046228753Smm * note that min == 0 is a perfectly well-defined 1047228753Smm * request. 1048228753Smm */ 1049228753Smm if (filter->avail >= min && filter->avail > 0) { 1050228753Smm if (avail != NULL) 1051228753Smm *avail = filter->avail; 1052228753Smm return (filter->next); 1053228753Smm } 1054228753Smm 1055228753Smm /* 1056228753Smm * We can satisfy directly from client buffer if everything 1057228753Smm * currently in the copy buffer is still in the client buffer. 1058228753Smm */ 1059228753Smm if (filter->client_total >= filter->client_avail + filter->avail 1060228753Smm && filter->client_avail + filter->avail >= min) { 1061228753Smm /* "Roll back" to client buffer. */ 1062228753Smm filter->client_avail += filter->avail; 1063228753Smm filter->client_next -= filter->avail; 1064228753Smm /* Copy buffer is now empty. */ 1065228753Smm filter->avail = 0; 1066228753Smm filter->next = filter->buffer; 1067228753Smm /* Return data from client buffer. */ 1068228753Smm if (avail != NULL) 1069228753Smm *avail = filter->client_avail; 1070228753Smm return (filter->client_next); 1071228753Smm } 1072228753Smm 1073228753Smm /* Move data forward in copy buffer if necessary. */ 1074228753Smm if (filter->next > filter->buffer && 1075228753Smm filter->next + min > filter->buffer + filter->buffer_size) { 1076228753Smm if (filter->avail > 0) 1077228753Smm memmove(filter->buffer, filter->next, filter->avail); 1078228753Smm filter->next = filter->buffer; 1079228753Smm } 1080228753Smm 1081228753Smm /* If we've used up the client data, get more. */ 1082228753Smm if (filter->client_avail <= 0) { 1083228753Smm if (filter->end_of_file) { 1084228753Smm if (avail != NULL) 1085228753Smm *avail = 0; 1086228753Smm return (NULL); 1087228753Smm } 1088228753Smm bytes_read = (filter->read)(filter, 1089228753Smm &filter->client_buff); 1090228753Smm if (bytes_read < 0) { /* Read error. */ 1091228753Smm filter->client_total = filter->client_avail = 0; 1092228753Smm filter->client_next = filter->client_buff = NULL; 1093228753Smm filter->fatal = 1; 1094228753Smm if (avail != NULL) 1095228753Smm *avail = ARCHIVE_FATAL; 1096228753Smm return (NULL); 1097228753Smm } 1098228753Smm if (bytes_read == 0) { /* Premature end-of-file. */ 1099228753Smm filter->client_total = filter->client_avail = 0; 1100228753Smm filter->client_next = filter->client_buff = NULL; 1101228753Smm filter->end_of_file = 1; 1102228753Smm /* Return whatever we do have. */ 1103228753Smm if (avail != NULL) 1104228753Smm *avail = filter->avail; 1105228753Smm return (NULL); 1106228753Smm } 1107228753Smm filter->client_total = bytes_read; 1108228753Smm filter->client_avail = filter->client_total; 1109228753Smm filter->client_next = filter->client_buff; 1110228753Smm } 1111228753Smm else 1112228753Smm { 1113228753Smm /* 1114228753Smm * We can't satisfy the request from the copy 1115228753Smm * buffer or the existing client data, so we 1116228753Smm * need to copy more client data over to the 1117228753Smm * copy buffer. 1118228753Smm */ 1119228753Smm 1120228753Smm /* Ensure the buffer is big enough. */ 1121228753Smm if (min > filter->buffer_size) { 1122228753Smm size_t s, t; 1123228753Smm char *p; 1124228753Smm 1125228753Smm /* Double the buffer; watch for overflow. */ 1126228753Smm s = t = filter->buffer_size; 1127228753Smm if (s == 0) 1128228753Smm s = min; 1129228753Smm while (s < min) { 1130228753Smm t *= 2; 1131228753Smm if (t <= s) { /* Integer overflow! */ 1132228753Smm archive_set_error( 1133228753Smm &filter->archive->archive, 1134228753Smm ENOMEM, 1135228753Smm "Unable to allocate copy buffer"); 1136228753Smm filter->fatal = 1; 1137228753Smm if (avail != NULL) 1138228753Smm *avail = ARCHIVE_FATAL; 1139228753Smm return (NULL); 1140228753Smm } 1141228753Smm s = t; 1142228753Smm } 1143228753Smm /* Now s >= min, so allocate a new buffer. */ 1144228753Smm p = (char *)malloc(s); 1145228753Smm if (p == NULL) { 1146228753Smm archive_set_error( 1147228753Smm &filter->archive->archive, 1148228753Smm ENOMEM, 1149228753Smm "Unable to allocate copy buffer"); 1150228753Smm filter->fatal = 1; 1151228753Smm if (avail != NULL) 1152228753Smm *avail = ARCHIVE_FATAL; 1153228753Smm return (NULL); 1154228753Smm } 1155228753Smm /* Move data into newly-enlarged buffer. */ 1156228753Smm if (filter->avail > 0) 1157228753Smm memmove(p, filter->next, filter->avail); 1158228753Smm free(filter->buffer); 1159228753Smm filter->next = filter->buffer = p; 1160228753Smm filter->buffer_size = s; 1161228753Smm } 1162228753Smm 1163228753Smm /* We can add client data to copy buffer. */ 1164228753Smm /* First estimate: copy to fill rest of buffer. */ 1165228753Smm tocopy = (filter->buffer + filter->buffer_size) 1166228753Smm - (filter->next + filter->avail); 1167228753Smm /* Don't waste time buffering more than we need to. */ 1168228753Smm if (tocopy + filter->avail > min) 1169228753Smm tocopy = min - filter->avail; 1170228753Smm /* Don't copy more than is available. */ 1171228753Smm if (tocopy > filter->client_avail) 1172228753Smm tocopy = filter->client_avail; 1173228753Smm 1174228753Smm memcpy(filter->next + filter->avail, filter->client_next, 1175228753Smm tocopy); 1176228753Smm /* Remove this data from client buffer. */ 1177228753Smm filter->client_next += tocopy; 1178228753Smm filter->client_avail -= tocopy; 1179228753Smm /* add it to copy buffer. */ 1180228753Smm filter->avail += tocopy; 1181228753Smm } 1182228753Smm } 1183228753Smm} 1184228753Smm 1185228753Smm/* 1186232153Smm * Move the file pointer forward. 1187228753Smm */ 1188232153Smmint64_t 1189232153Smm__archive_read_consume(struct archive_read *a, int64_t request) 1190228753Smm{ 1191232153Smm return (__archive_read_filter_consume(a->filter, request)); 1192228753Smm} 1193228753Smm 1194232153Smmint64_t 1195228753Smm__archive_read_filter_consume(struct archive_read_filter * filter, 1196232153Smm int64_t request) 1197228753Smm{ 1198232153Smm int64_t skipped; 1199228753Smm 1200232153Smm if (request == 0) 1201232153Smm return 0; 1202232153Smm 1203232153Smm skipped = advance_file_pointer(filter, request); 1204228753Smm if (skipped == request) 1205228753Smm return (skipped); 1206228753Smm /* We hit EOF before we satisfied the skip request. */ 1207232153Smm if (skipped < 0) /* Map error code to 0 for error message below. */ 1208228753Smm skipped = 0; 1209232153Smm archive_set_error(&filter->archive->archive, 1210228753Smm ARCHIVE_ERRNO_MISC, 1211228753Smm "Truncated input file (needed %jd bytes, only %jd available)", 1212228753Smm (intmax_t)request, (intmax_t)skipped); 1213228753Smm return (ARCHIVE_FATAL); 1214228753Smm} 1215228753Smm 1216232153Smm/* 1217232153Smm * Advance the file pointer by the amount requested. 1218232153Smm * Returns the amount actually advanced, which may be less than the 1219232153Smm * request if EOF is encountered first. 1220232153Smm * Returns a negative value if there's an I/O error. 1221232153Smm */ 1222232153Smmstatic int64_t 1223232153Smmadvance_file_pointer(struct archive_read_filter *filter, int64_t request) 1224228753Smm{ 1225228753Smm int64_t bytes_skipped, total_bytes_skipped = 0; 1226232153Smm ssize_t bytes_read; 1227228753Smm size_t min; 1228228753Smm 1229228753Smm if (filter->fatal) 1230228753Smm return (-1); 1231232153Smm 1232232153Smm /* Use up the copy buffer first. */ 1233228753Smm if (filter->avail > 0) { 1234232153Smm min = minimum(request, (int64_t)filter->avail); 1235232153Smm filter->next += min; 1236232153Smm filter->avail -= min; 1237232153Smm request -= min; 1238232153Smm filter->position += min; 1239232153Smm total_bytes_skipped += min; 1240228753Smm } 1241232153Smm 1242232153Smm /* Then use up the client buffer. */ 1243228753Smm if (filter->client_avail > 0) { 1244228753Smm min = minimum(request, (int64_t)filter->client_avail); 1245232153Smm filter->client_next += min; 1246232153Smm filter->client_avail -= min; 1247232153Smm request -= min; 1248232153Smm filter->position += min; 1249232153Smm total_bytes_skipped += min; 1250228753Smm } 1251228753Smm if (request == 0) 1252228753Smm return (total_bytes_skipped); 1253232153Smm 1254232153Smm /* If there's an optimized skip function, use it. */ 1255228753Smm if (filter->skip != NULL) { 1256228753Smm bytes_skipped = (filter->skip)(filter, request); 1257228753Smm if (bytes_skipped < 0) { /* error */ 1258228753Smm filter->fatal = 1; 1259228753Smm return (bytes_skipped); 1260228753Smm } 1261232153Smm filter->position += bytes_skipped; 1262228753Smm total_bytes_skipped += bytes_skipped; 1263228753Smm request -= bytes_skipped; 1264232153Smm if (request == 0) 1265232153Smm return (total_bytes_skipped); 1266228753Smm } 1267232153Smm 1268232153Smm /* Use ordinary reads as necessary to complete the request. */ 1269232153Smm for (;;) { 1270232153Smm bytes_read = (filter->read)(filter, &filter->client_buff); 1271232153Smm if (bytes_read < 0) { 1272232153Smm filter->client_buff = NULL; 1273232153Smm filter->fatal = 1; 1274228753Smm return (bytes_read); 1275232153Smm } 1276232153Smm 1277228753Smm if (bytes_read == 0) { 1278232153Smm filter->client_buff = NULL; 1279232153Smm filter->end_of_file = 1; 1280228753Smm return (total_bytes_skipped); 1281228753Smm } 1282232153Smm 1283232153Smm if (bytes_read >= request) { 1284232153Smm filter->client_next = 1285232153Smm ((const char *)filter->client_buff) + request; 1286232153Smm filter->client_avail = bytes_read - request; 1287232153Smm filter->client_total = bytes_read; 1288232153Smm total_bytes_skipped += request; 1289232153Smm filter->position += request; 1290232153Smm return (total_bytes_skipped); 1291232153Smm } 1292232153Smm 1293232153Smm filter->position += bytes_read; 1294228753Smm total_bytes_skipped += bytes_read; 1295228753Smm request -= bytes_read; 1296228753Smm } 1297228753Smm} 1298232153Smm 1299232153Smm/** 1300232153Smm * Returns ARCHIVE_FAILED if seeking isn't supported. 1301232153Smm */ 1302232153Smmint64_t 1303232153Smm__archive_read_seek(struct archive_read *a, int64_t offset, int whence) 1304232153Smm{ 1305232153Smm return __archive_read_filter_seek(a->filter, offset, whence); 1306232153Smm} 1307232153Smm 1308232153Smmint64_t 1309232153Smm__archive_read_filter_seek(struct archive_read_filter *filter, int64_t offset, int whence) 1310232153Smm{ 1311232153Smm int64_t r; 1312232153Smm 1313232153Smm if (filter->closed || filter->fatal) 1314232153Smm return (ARCHIVE_FATAL); 1315232153Smm if (filter->seek == NULL) 1316232153Smm return (ARCHIVE_FAILED); 1317232153Smm r = filter->seek(filter, offset, whence); 1318232153Smm if (r >= 0) { 1319232153Smm /* 1320232153Smm * Ouch. Clearing the buffer like this hurts, especially 1321232153Smm * at bid time. A lot of our efficiency at bid time comes 1322232153Smm * from having bidders reuse the data we've already read. 1323232153Smm * 1324232153Smm * TODO: If the seek request is in data we already 1325232153Smm * have, then don't call the seek callback. 1326232153Smm * 1327232153Smm * TODO: Zip seeks to end-of-file at bid time. If 1328232153Smm * other formats also start doing this, we may need to 1329232153Smm * find a way for clients to fudge the seek offset to 1330232153Smm * a block boundary. 1331232153Smm * 1332232153Smm * Hmmm... If whence was SEEK_END, we know the file 1333232153Smm * size is (r - offset). Can we use that to simplify 1334232153Smm * the TODO items above? 1335232153Smm */ 1336232153Smm filter->avail = filter->client_avail = 0; 1337232153Smm filter->next = filter->buffer; 1338232153Smm filter->position = r; 1339232153Smm filter->end_of_file = 0; 1340232153Smm } 1341232153Smm return r; 1342232153Smm} 1343