archive_read.c revision 299529
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 299529 2016-05-12 10:16:16Z 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 *); 60228753Smmstatic struct archive_vtable *archive_read_vtable(void); 61232153Smmstatic int64_t _archive_filter_bytes(struct archive *, int); 62232153Smmstatic int _archive_filter_code(struct archive *, int); 63232153Smmstatic const char *_archive_filter_name(struct archive *, int); 64232153Smmstatic int _archive_filter_count(struct archive *); 65228753Smmstatic int _archive_read_close(struct archive *); 66232153Smmstatic int _archive_read_data_block(struct archive *, 67232153Smm const void **, size_t *, int64_t *); 68228773Smmstatic int _archive_read_free(struct archive *); 69232153Smmstatic int _archive_read_next_header(struct archive *, 70232153Smm struct archive_entry **); 71232153Smmstatic int _archive_read_next_header2(struct archive *, 72232153Smm struct archive_entry *); 73232153Smmstatic int64_t advance_file_pointer(struct archive_read_filter *, int64_t); 74228753Smm 75228753Smmstatic struct archive_vtable * 76228753Smmarchive_read_vtable(void) 77228753Smm{ 78228753Smm static struct archive_vtable av; 79228753Smm static int inited = 0; 80228753Smm 81228753Smm if (!inited) { 82232153Smm av.archive_filter_bytes = _archive_filter_bytes; 83232153Smm av.archive_filter_code = _archive_filter_code; 84232153Smm av.archive_filter_name = _archive_filter_name; 85232153Smm av.archive_filter_count = _archive_filter_count; 86232153Smm av.archive_read_data_block = _archive_read_data_block; 87232153Smm av.archive_read_next_header = _archive_read_next_header; 88232153Smm av.archive_read_next_header2 = _archive_read_next_header2; 89228773Smm av.archive_free = _archive_read_free; 90228753Smm av.archive_close = _archive_read_close; 91232153Smm inited = 1; 92228753Smm } 93228753Smm return (&av); 94228753Smm} 95228753Smm 96228753Smm/* 97228753Smm * Allocate, initialize and return a struct archive object. 98228753Smm */ 99228753Smmstruct archive * 100228753Smmarchive_read_new(void) 101228753Smm{ 102228753Smm struct archive_read *a; 103228753Smm 104299529Smm a = (struct archive_read *)calloc(1, sizeof(*a)); 105228753Smm if (a == NULL) 106228753Smm return (NULL); 107228753Smm a->archive.magic = ARCHIVE_READ_MAGIC; 108228753Smm 109228753Smm a->archive.state = ARCHIVE_STATE_NEW; 110232153Smm a->entry = archive_entry_new2(&a->archive); 111228753Smm a->archive.vtable = archive_read_vtable(); 112228753Smm 113299529Smm a->passphrases.last = &a->passphrases.first; 114299529Smm 115228753Smm return (&a->archive); 116228753Smm} 117228753Smm 118228753Smm/* 119228753Smm * Record the do-not-extract-to file. This belongs in archive_read_extract.c. 120228753Smm */ 121228753Smmvoid 122232153Smmarchive_read_extract_set_skip_file(struct archive *_a, int64_t d, int64_t i) 123228753Smm{ 124228753Smm struct archive_read *a = (struct archive_read *)_a; 125232153Smm 126232153Smm if (ARCHIVE_OK != __archive_check_magic(_a, ARCHIVE_READ_MAGIC, 127232153Smm ARCHIVE_STATE_ANY, "archive_read_extract_set_skip_file")) 128232153Smm return; 129232153Smm a->skip_file_set = 1; 130228753Smm a->skip_file_dev = d; 131228753Smm a->skip_file_ino = i; 132228753Smm} 133228753Smm 134228753Smm/* 135232153Smm * Open the archive 136228753Smm */ 137228753Smmint 138232153Smmarchive_read_open(struct archive *a, void *client_data, 139232153Smm archive_open_callback *client_opener, archive_read_callback *client_reader, 140232153Smm archive_close_callback *client_closer) 141228753Smm{ 142232153Smm /* Old archive_read_open() is just a thin shell around 143232153Smm * archive_read_open1. */ 144232153Smm archive_read_set_open_callback(a, client_opener); 145232153Smm archive_read_set_read_callback(a, client_reader); 146232153Smm archive_read_set_close_callback(a, client_closer); 147232153Smm archive_read_set_callback_data(a, client_data); 148232153Smm return archive_read_open1(a); 149228753Smm} 150228753Smm 151228753Smm 152228753Smmint 153232153Smmarchive_read_open2(struct archive *a, void *client_data, 154232153Smm archive_open_callback *client_opener, 155232153Smm archive_read_callback *client_reader, 156232153Smm archive_skip_callback *client_skipper, 157228753Smm archive_close_callback *client_closer) 158228753Smm{ 159232153Smm /* Old archive_read_open2() is just a thin shell around 160232153Smm * archive_read_open1. */ 161232153Smm archive_read_set_callback_data(a, client_data); 162232153Smm archive_read_set_open_callback(a, client_opener); 163232153Smm archive_read_set_read_callback(a, client_reader); 164232153Smm archive_read_set_skip_callback(a, client_skipper); 165232153Smm archive_read_set_close_callback(a, client_closer); 166232153Smm return archive_read_open1(a); 167228753Smm} 168228753Smm 169228753Smmstatic ssize_t 170228753Smmclient_read_proxy(struct archive_read_filter *self, const void **buff) 171228753Smm{ 172228753Smm ssize_t r; 173228753Smm r = (self->archive->client.reader)(&self->archive->archive, 174228753Smm self->data, buff); 175228753Smm return (r); 176228753Smm} 177228753Smm 178228753Smmstatic int64_t 179228753Smmclient_skip_proxy(struct archive_read_filter *self, int64_t request) 180228753Smm{ 181232153Smm if (request < 0) 182232153Smm __archive_errx(1, "Negative skip requested."); 183232153Smm if (request == 0) 184232153Smm return 0; 185228753Smm 186232153Smm if (self->archive->client.skipper != NULL) { 187232153Smm /* Seek requests over 1GiB are broken down into 188232153Smm * multiple seeks. This avoids overflows when the 189232153Smm * requests get passed through 32-bit arguments. */ 190232153Smm int64_t skip_limit = (int64_t)1 << 30; 191232153Smm int64_t total = 0; 192232153Smm for (;;) { 193232153Smm int64_t get, ask = request; 194232153Smm if (ask > skip_limit) 195232153Smm ask = skip_limit; 196248616Smm get = (self->archive->client.skipper) 197248616Smm (&self->archive->archive, self->data, ask); 198299529Smm total += get; 199299529Smm if (get == 0 || get == request) 200232153Smm return (total); 201299529Smm if (get > request) 202299529Smm return ARCHIVE_FATAL; 203232153Smm request -= get; 204232153Smm } 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; 219248616Smm int64_t after = (self->archive->client.seeker) 220248616Smm (&self->archive->archive, 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 */ 236299529Smm if (self->archive->client.seeker == NULL) { 237299529Smm archive_set_error(&self->archive->archive, ARCHIVE_ERRNO_MISC, 238299529Smm "Current client reader does not support seeking a device"); 239232153Smm return (ARCHIVE_FAILED); 240299529Smm } 241232153Smm return (self->archive->client.seeker)(&self->archive->archive, 242232153Smm self->data, offset, whence); 243232153Smm} 244232153Smm 245228753Smmstatic int 246228753Smmclient_close_proxy(struct archive_read_filter *self) 247228753Smm{ 248248616Smm int r = ARCHIVE_OK, r2; 249248616Smm unsigned int i; 250228753Smm 251248616Smm if (self->archive->client.closer == NULL) 252248616Smm return (r); 253248616Smm for (i = 0; i < self->archive->client.nodes; i++) 254248616Smm { 255248616Smm r2 = (self->archive->client.closer) 256248616Smm ((struct archive *)self->archive, 257248616Smm self->archive->client.dataset[i].data); 258248616Smm if (r > r2) 259248616Smm r = r2; 260248616Smm } 261228753Smm return (r); 262228753Smm} 263228753Smm 264248616Smmstatic int 265248616Smmclient_open_proxy(struct archive_read_filter *self) 266248616Smm{ 267248616Smm int r = ARCHIVE_OK; 268248616Smm if (self->archive->client.opener != NULL) 269248616Smm r = (self->archive->client.opener)( 270248616Smm (struct archive *)self->archive, self->data); 271248616Smm return (r); 272248616Smm} 273248616Smm 274248616Smmstatic int 275248616Smmclient_switch_proxy(struct archive_read_filter *self, unsigned int iindex) 276248616Smm{ 277248616Smm int r1 = ARCHIVE_OK, r2 = ARCHIVE_OK; 278248616Smm void *data2 = NULL; 279248616Smm 280248616Smm /* Don't do anything if already in the specified data node */ 281248616Smm if (self->archive->client.cursor == iindex) 282248616Smm return (ARCHIVE_OK); 283248616Smm 284248616Smm self->archive->client.cursor = iindex; 285248616Smm data2 = self->archive->client.dataset[self->archive->client.cursor].data; 286248616Smm if (self->archive->client.switcher != NULL) 287248616Smm { 288248616Smm r1 = r2 = (self->archive->client.switcher) 289248616Smm ((struct archive *)self->archive, self->data, data2); 290248616Smm self->data = data2; 291248616Smm } 292248616Smm else 293248616Smm { 294248616Smm /* Attempt to call close and open instead */ 295248616Smm if (self->archive->client.closer != NULL) 296248616Smm r1 = (self->archive->client.closer) 297248616Smm ((struct archive *)self->archive, self->data); 298248616Smm self->data = data2; 299248616Smm if (self->archive->client.opener != NULL) 300248616Smm r2 = (self->archive->client.opener) 301248616Smm ((struct archive *)self->archive, self->data); 302248616Smm } 303248616Smm return (r1 < r2) ? r1 : r2; 304248616Smm} 305248616Smm 306232153Smmint 307232153Smmarchive_read_set_open_callback(struct archive *_a, 308232153Smm archive_open_callback *client_opener) 309232153Smm{ 310232153Smm struct archive_read *a = (struct archive_read *)_a; 311232153Smm archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW, 312232153Smm "archive_read_set_open_callback"); 313232153Smm a->client.opener = client_opener; 314232153Smm return ARCHIVE_OK; 315232153Smm} 316228753Smm 317228753Smmint 318232153Smmarchive_read_set_read_callback(struct archive *_a, 319232153Smm archive_read_callback *client_reader) 320232153Smm{ 321232153Smm struct archive_read *a = (struct archive_read *)_a; 322232153Smm archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW, 323232153Smm "archive_read_set_read_callback"); 324232153Smm a->client.reader = client_reader; 325232153Smm return ARCHIVE_OK; 326232153Smm} 327232153Smm 328232153Smmint 329232153Smmarchive_read_set_skip_callback(struct archive *_a, 330232153Smm archive_skip_callback *client_skipper) 331232153Smm{ 332232153Smm struct archive_read *a = (struct archive_read *)_a; 333232153Smm archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW, 334232153Smm "archive_read_set_skip_callback"); 335232153Smm a->client.skipper = client_skipper; 336232153Smm return ARCHIVE_OK; 337232153Smm} 338232153Smm 339232153Smmint 340232153Smmarchive_read_set_seek_callback(struct archive *_a, 341232153Smm archive_seek_callback *client_seeker) 342232153Smm{ 343232153Smm struct archive_read *a = (struct archive_read *)_a; 344232153Smm archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW, 345232153Smm "archive_read_set_seek_callback"); 346232153Smm a->client.seeker = client_seeker; 347232153Smm return ARCHIVE_OK; 348232153Smm} 349232153Smm 350232153Smmint 351232153Smmarchive_read_set_close_callback(struct archive *_a, 352228753Smm archive_close_callback *client_closer) 353228753Smm{ 354228753Smm struct archive_read *a = (struct archive_read *)_a; 355232153Smm archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW, 356232153Smm "archive_read_set_close_callback"); 357232153Smm a->client.closer = client_closer; 358232153Smm return ARCHIVE_OK; 359232153Smm} 360232153Smm 361232153Smmint 362248616Smmarchive_read_set_switch_callback(struct archive *_a, 363248616Smm archive_switch_callback *client_switcher) 364248616Smm{ 365248616Smm struct archive_read *a = (struct archive_read *)_a; 366248616Smm archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW, 367248616Smm "archive_read_set_switch_callback"); 368248616Smm a->client.switcher = client_switcher; 369248616Smm return ARCHIVE_OK; 370248616Smm} 371248616Smm 372248616Smmint 373232153Smmarchive_read_set_callback_data(struct archive *_a, void *client_data) 374232153Smm{ 375248616Smm return archive_read_set_callback_data2(_a, client_data, 0); 376248616Smm} 377248616Smm 378248616Smmint 379248616Smmarchive_read_set_callback_data2(struct archive *_a, void *client_data, 380248616Smm unsigned int iindex) 381248616Smm{ 382232153Smm struct archive_read *a = (struct archive_read *)_a; 383232153Smm archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW, 384248616Smm "archive_read_set_callback_data2"); 385248616Smm 386248616Smm if (a->client.nodes == 0) 387248616Smm { 388248616Smm a->client.dataset = (struct archive_read_data_node *) 389248616Smm calloc(1, sizeof(*a->client.dataset)); 390248616Smm if (a->client.dataset == NULL) 391248616Smm { 392248616Smm archive_set_error(&a->archive, ENOMEM, 393248616Smm "No memory."); 394248616Smm return ARCHIVE_FATAL; 395248616Smm } 396248616Smm a->client.nodes = 1; 397248616Smm } 398248616Smm 399248616Smm if (iindex > a->client.nodes - 1) 400248616Smm { 401248616Smm archive_set_error(&a->archive, EINVAL, 402248616Smm "Invalid index specified."); 403248616Smm return ARCHIVE_FATAL; 404248616Smm } 405248616Smm a->client.dataset[iindex].data = client_data; 406248616Smm a->client.dataset[iindex].begin_position = -1; 407248616Smm a->client.dataset[iindex].total_size = -1; 408232153Smm return ARCHIVE_OK; 409232153Smm} 410232153Smm 411232153Smmint 412248616Smmarchive_read_add_callback_data(struct archive *_a, void *client_data, 413248616Smm unsigned int iindex) 414248616Smm{ 415248616Smm struct archive_read *a = (struct archive_read *)_a; 416248616Smm void *p; 417248616Smm unsigned int i; 418248616Smm 419248616Smm archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW, 420248616Smm "archive_read_add_callback_data"); 421248616Smm if (iindex > a->client.nodes) { 422248616Smm archive_set_error(&a->archive, EINVAL, 423248616Smm "Invalid index specified."); 424248616Smm return ARCHIVE_FATAL; 425248616Smm } 426248616Smm p = realloc(a->client.dataset, sizeof(*a->client.dataset) 427248616Smm * (++(a->client.nodes))); 428248616Smm if (p == NULL) { 429248616Smm archive_set_error(&a->archive, ENOMEM, 430248616Smm "No memory."); 431248616Smm return ARCHIVE_FATAL; 432248616Smm } 433248616Smm a->client.dataset = (struct archive_read_data_node *)p; 434248616Smm for (i = a->client.nodes - 1; i > iindex && i > 0; i--) { 435248616Smm a->client.dataset[i].data = a->client.dataset[i-1].data; 436248616Smm a->client.dataset[i].begin_position = -1; 437248616Smm a->client.dataset[i].total_size = -1; 438248616Smm } 439248616Smm a->client.dataset[iindex].data = client_data; 440248616Smm a->client.dataset[iindex].begin_position = -1; 441248616Smm a->client.dataset[iindex].total_size = -1; 442248616Smm return ARCHIVE_OK; 443248616Smm} 444248616Smm 445248616Smmint 446248616Smmarchive_read_append_callback_data(struct archive *_a, void *client_data) 447248616Smm{ 448248616Smm struct archive_read *a = (struct archive_read *)_a; 449248616Smm return archive_read_add_callback_data(_a, client_data, a->client.nodes); 450248616Smm} 451248616Smm 452248616Smmint 453248616Smmarchive_read_prepend_callback_data(struct archive *_a, void *client_data) 454248616Smm{ 455248616Smm return archive_read_add_callback_data(_a, client_data, 0); 456248616Smm} 457248616Smm 458248616Smmint 459232153Smmarchive_read_open1(struct archive *_a) 460232153Smm{ 461232153Smm struct archive_read *a = (struct archive_read *)_a; 462248616Smm struct archive_read_filter *filter, *tmp; 463299529Smm int slot, e = ARCHIVE_OK; 464248616Smm unsigned int i; 465228753Smm 466232153Smm archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW, 467228753Smm "archive_read_open"); 468228753Smm archive_clear_error(&a->archive); 469228753Smm 470232153Smm if (a->client.reader == NULL) { 471232153Smm archive_set_error(&a->archive, EINVAL, 472228753Smm "No reader function provided to archive_read_open"); 473232153Smm a->archive.state = ARCHIVE_STATE_FATAL; 474232153Smm return (ARCHIVE_FATAL); 475232153Smm } 476228753Smm 477228753Smm /* Open data source. */ 478232153Smm if (a->client.opener != NULL) { 479248616Smm e = (a->client.opener)(&a->archive, a->client.dataset[0].data); 480228753Smm if (e != 0) { 481228753Smm /* If the open failed, call the closer to clean up. */ 482248616Smm if (a->client.closer) { 483248616Smm for (i = 0; i < a->client.nodes; i++) 484248616Smm (a->client.closer)(&a->archive, 485248616Smm a->client.dataset[i].data); 486248616Smm } 487228753Smm return (e); 488228753Smm } 489228753Smm } 490228753Smm 491228753Smm filter = calloc(1, sizeof(*filter)); 492228753Smm if (filter == NULL) 493228753Smm return (ARCHIVE_FATAL); 494228753Smm filter->bidder = NULL; 495228753Smm filter->upstream = NULL; 496228753Smm filter->archive = a; 497248616Smm filter->data = a->client.dataset[0].data; 498248616Smm filter->open = client_open_proxy; 499228753Smm filter->read = client_read_proxy; 500228753Smm filter->skip = client_skip_proxy; 501232153Smm filter->seek = client_seek_proxy; 502228753Smm filter->close = client_close_proxy; 503248616Smm filter->sswitch = client_switch_proxy; 504228753Smm filter->name = "none"; 505248616Smm filter->code = ARCHIVE_FILTER_NONE; 506228753Smm 507248616Smm a->client.dataset[0].begin_position = 0; 508248616Smm if (!a->filter || !a->bypass_filter_bidding) 509248616Smm { 510248616Smm a->filter = filter; 511248616Smm /* Build out the input pipeline. */ 512248616Smm e = choose_filters(a); 513248616Smm if (e < ARCHIVE_WARN) { 514248616Smm a->archive.state = ARCHIVE_STATE_FATAL; 515248616Smm return (ARCHIVE_FATAL); 516248616Smm } 517232153Smm } 518248616Smm else 519248616Smm { 520248616Smm /* Need to add "NONE" type filter at the end of the filter chain */ 521248616Smm tmp = a->filter; 522248616Smm while (tmp->upstream) 523248616Smm tmp = tmp->upstream; 524248616Smm tmp->upstream = filter; 525248616Smm } 526228753Smm 527248616Smm if (!a->format) 528248616Smm { 529248616Smm slot = choose_format(a); 530248616Smm if (slot < 0) { 531248616Smm __archive_read_close_filters(a); 532248616Smm a->archive.state = ARCHIVE_STATE_FATAL; 533248616Smm return (ARCHIVE_FATAL); 534248616Smm } 535248616Smm a->format = &(a->formats[slot]); 536232153Smm } 537232153Smm 538232153Smm a->archive.state = ARCHIVE_STATE_HEADER; 539248616Smm 540248616Smm /* Ensure libarchive starts from the first node in a multivolume set */ 541248616Smm client_switch_proxy(a->filter, 0); 542228753Smm return (e); 543228753Smm} 544228753Smm 545228753Smm/* 546228753Smm * Allow each registered stream transform to bid on whether 547228753Smm * it wants to handle this stream. Repeat until we've finished 548228753Smm * building the pipeline. 549228753Smm */ 550299529Smm 551299529Smm/* We won't build a filter pipeline with more stages than this. */ 552299529Smm#define MAX_NUMBER_FILTERS 25 553299529Smm 554228753Smmstatic int 555232153Smmchoose_filters(struct archive_read *a) 556228753Smm{ 557299529Smm int number_bidders, i, bid, best_bid, number_filters; 558228753Smm struct archive_read_filter_bidder *bidder, *best_bidder; 559228753Smm struct archive_read_filter *filter; 560228753Smm ssize_t avail; 561228753Smm int r; 562228753Smm 563299529Smm for (number_filters = 0; number_filters < MAX_NUMBER_FILTERS; ++number_filters) { 564228753Smm number_bidders = sizeof(a->bidders) / sizeof(a->bidders[0]); 565228753Smm 566228753Smm best_bid = 0; 567228753Smm best_bidder = NULL; 568228753Smm 569228753Smm bidder = a->bidders; 570228753Smm for (i = 0; i < number_bidders; i++, bidder++) { 571228753Smm if (bidder->bid != NULL) { 572228753Smm bid = (bidder->bid)(bidder, a->filter); 573228753Smm if (bid > best_bid) { 574228753Smm best_bid = bid; 575228753Smm best_bidder = bidder; 576228753Smm } 577228753Smm } 578228753Smm } 579228753Smm 580228753Smm /* If no bidder, we're done. */ 581228753Smm if (best_bidder == NULL) { 582232153Smm /* Verify the filter by asking it for some data. */ 583228753Smm __archive_read_filter_ahead(a->filter, 1, &avail); 584228753Smm if (avail < 0) { 585248616Smm __archive_read_close_filters(a); 586248616Smm __archive_read_free_filters(a); 587228753Smm return (ARCHIVE_FATAL); 588228753Smm } 589228753Smm a->archive.compression_name = a->filter->name; 590228753Smm a->archive.compression_code = a->filter->code; 591228753Smm return (ARCHIVE_OK); 592228753Smm } 593228753Smm 594228753Smm filter 595228753Smm = (struct archive_read_filter *)calloc(1, sizeof(*filter)); 596228753Smm if (filter == NULL) 597228753Smm return (ARCHIVE_FATAL); 598228753Smm filter->bidder = best_bidder; 599228753Smm filter->archive = a; 600228753Smm filter->upstream = a->filter; 601228753Smm a->filter = filter; 602228753Smm r = (best_bidder->init)(a->filter); 603228753Smm if (r != ARCHIVE_OK) { 604248616Smm __archive_read_close_filters(a); 605248616Smm __archive_read_free_filters(a); 606232153Smm return (ARCHIVE_FATAL); 607228753Smm } 608228753Smm } 609295914Sdelphij archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, 610295914Sdelphij "Input requires too many filters for decoding"); 611295914Sdelphij return (ARCHIVE_FATAL); 612228753Smm} 613228753Smm 614228753Smm/* 615228753Smm * Read header of next entry. 616228753Smm */ 617232153Smmstatic int 618232153Smm_archive_read_next_header2(struct archive *_a, struct archive_entry *entry) 619228753Smm{ 620228753Smm struct archive_read *a = (struct archive_read *)_a; 621232153Smm int r1 = ARCHIVE_OK, r2; 622228753Smm 623232153Smm archive_check_magic(_a, ARCHIVE_READ_MAGIC, 624228753Smm ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA, 625228753Smm "archive_read_next_header"); 626228753Smm 627228753Smm archive_entry_clear(entry); 628228753Smm archive_clear_error(&a->archive); 629228753Smm 630228753Smm /* 631228753Smm * If client didn't consume entire data, skip any remainder 632228753Smm * (This is especially important for GNU incremental directories.) 633228753Smm */ 634228753Smm if (a->archive.state == ARCHIVE_STATE_DATA) { 635232153Smm r1 = archive_read_data_skip(&a->archive); 636232153Smm if (r1 == ARCHIVE_EOF) 637232153Smm archive_set_error(&a->archive, EIO, 638232153Smm "Premature end-of-file."); 639232153Smm if (r1 == ARCHIVE_EOF || r1 == ARCHIVE_FATAL) { 640228753Smm a->archive.state = ARCHIVE_STATE_FATAL; 641228753Smm return (ARCHIVE_FATAL); 642228753Smm } 643228753Smm } 644228753Smm 645232153Smm /* Record start-of-header offset in uncompressed stream. */ 646232153Smm a->header_position = a->filter->position; 647228753Smm 648232153Smm ++_a->file_count; 649232153Smm r2 = (a->format->read_header)(a, entry); 650228753Smm 651228753Smm /* 652228753Smm * EOF and FATAL are persistent at this layer. By 653228753Smm * modifying the state, we guarantee that future calls to 654228753Smm * read a header or read data will fail. 655228753Smm */ 656232153Smm switch (r2) { 657228753Smm case ARCHIVE_EOF: 658228753Smm a->archive.state = ARCHIVE_STATE_EOF; 659232153Smm --_a->file_count;/* Revert a file counter. */ 660228753Smm break; 661228753Smm case ARCHIVE_OK: 662228753Smm a->archive.state = ARCHIVE_STATE_DATA; 663228753Smm break; 664228753Smm case ARCHIVE_WARN: 665228753Smm a->archive.state = ARCHIVE_STATE_DATA; 666228753Smm break; 667228753Smm case ARCHIVE_RETRY: 668228753Smm break; 669228753Smm case ARCHIVE_FATAL: 670228753Smm a->archive.state = ARCHIVE_STATE_FATAL; 671228753Smm break; 672228753Smm } 673228753Smm 674299529Smm __archive_reset_read_data(&a->archive); 675299529Smm 676248616Smm a->data_start_node = a->client.cursor; 677232153Smm /* EOF always wins; otherwise return the worst error. */ 678232153Smm return (r2 < r1 || r2 == ARCHIVE_EOF) ? r2 : r1; 679228753Smm} 680228753Smm 681299529Smmstatic int 682232153Smm_archive_read_next_header(struct archive *_a, struct archive_entry **entryp) 683228753Smm{ 684228753Smm int ret; 685228753Smm struct archive_read *a = (struct archive_read *)_a; 686228753Smm *entryp = NULL; 687232153Smm ret = _archive_read_next_header2(_a, a->entry); 688228753Smm *entryp = a->entry; 689228753Smm return ret; 690228753Smm} 691228753Smm 692228753Smm/* 693228753Smm * Allow each registered format to bid on whether it wants to handle 694228753Smm * the next entry. Return index of winning bidder. 695228753Smm */ 696228753Smmstatic int 697228753Smmchoose_format(struct archive_read *a) 698228753Smm{ 699228753Smm int slots; 700228753Smm int i; 701228753Smm int bid, best_bid; 702228753Smm int best_bid_slot; 703228753Smm 704228753Smm slots = sizeof(a->formats) / sizeof(a->formats[0]); 705228753Smm best_bid = -1; 706228753Smm best_bid_slot = -1; 707228753Smm 708232153Smm /* Set up a->format for convenience of bidders. */ 709228753Smm a->format = &(a->formats[0]); 710228753Smm for (i = 0; i < slots; i++, a->format++) { 711228753Smm if (a->format->bid) { 712232153Smm bid = (a->format->bid)(a, best_bid); 713228753Smm if (bid == ARCHIVE_FATAL) 714228753Smm return (ARCHIVE_FATAL); 715232153Smm if (a->filter->position != 0) 716232153Smm __archive_read_seek(a, 0, SEEK_SET); 717228753Smm if ((bid > best_bid) || (best_bid_slot < 0)) { 718228753Smm best_bid = bid; 719228753Smm best_bid_slot = i; 720228753Smm } 721228753Smm } 722228753Smm } 723228753Smm 724228753Smm /* 725228753Smm * There were no bidders; this is a serious programmer error 726228753Smm * and demands a quick and definitive abort. 727228753Smm */ 728232153Smm if (best_bid_slot < 0) { 729232153Smm archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, 730232153Smm "No formats registered"); 731232153Smm return (ARCHIVE_FATAL); 732232153Smm } 733228753Smm 734228753Smm /* 735228753Smm * There were bidders, but no non-zero bids; this means we 736228753Smm * can't support this stream. 737228753Smm */ 738228753Smm if (best_bid < 1) { 739228753Smm archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, 740228753Smm "Unrecognized archive format"); 741228753Smm return (ARCHIVE_FATAL); 742228753Smm } 743228753Smm 744228753Smm return (best_bid_slot); 745228753Smm} 746228753Smm 747228753Smm/* 748228753Smm * Return the file offset (within the uncompressed data stream) where 749228753Smm * the last header started. 750228753Smm */ 751228753Smmint64_t 752228753Smmarchive_read_header_position(struct archive *_a) 753228753Smm{ 754228753Smm struct archive_read *a = (struct archive_read *)_a; 755232153Smm archive_check_magic(_a, ARCHIVE_READ_MAGIC, 756228753Smm ARCHIVE_STATE_ANY, "archive_read_header_position"); 757228753Smm return (a->header_position); 758228753Smm} 759228753Smm 760228753Smm/* 761299529Smm * Returns 1 if the archive contains at least one encrypted entry. 762299529Smm * If the archive format not support encryption at all 763299529Smm * ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED is returned. 764299529Smm * If for any other reason (e.g. not enough data read so far) 765299529Smm * we cannot say whether there are encrypted entries, then 766299529Smm * ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW is returned. 767299529Smm * In general, this function will return values below zero when the 768299529Smm * reader is uncertain or totally uncapable of encryption support. 769299529Smm * When this function returns 0 you can be sure that the reader 770299529Smm * supports encryption detection but no encrypted entries have 771299529Smm * been found yet. 772299529Smm * 773299529Smm * NOTE: If the metadata/header of an archive is also encrypted, you 774299529Smm * cannot rely on the number of encrypted entries. That is why this 775299529Smm * function does not return the number of encrypted entries but# 776299529Smm * just shows that there are some. 777299529Smm */ 778299529Smmint 779299529Smmarchive_read_has_encrypted_entries(struct archive *_a) 780299529Smm{ 781299529Smm struct archive_read *a = (struct archive_read *)_a; 782299529Smm int format_supports_encryption = archive_read_format_capabilities(_a) 783299529Smm & (ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_DATA | ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_METADATA); 784299529Smm 785299529Smm if (!_a || !format_supports_encryption) { 786299529Smm /* Format in general doesn't support encryption */ 787299529Smm return ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED; 788299529Smm } 789299529Smm 790299529Smm /* A reader potentially has read enough data now. */ 791299529Smm if (a->format && a->format->has_encrypted_entries) { 792299529Smm return (a->format->has_encrypted_entries)(a); 793299529Smm } 794299529Smm 795299529Smm /* For any other reason we cannot say how many entries are there. */ 796299529Smm return ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW; 797299529Smm} 798299529Smm 799299529Smm/* 800299529Smm * Returns a bitmask of capabilities that are supported by the archive format reader. 801299529Smm * If the reader has no special capabilities, ARCHIVE_READ_FORMAT_CAPS_NONE is returned. 802299529Smm */ 803299529Smmint 804299529Smmarchive_read_format_capabilities(struct archive *_a) 805299529Smm{ 806299529Smm struct archive_read *a = (struct archive_read *)_a; 807299529Smm if (a && a->format && a->format->format_capabilties) { 808299529Smm return (a->format->format_capabilties)(a); 809299529Smm } 810299529Smm return ARCHIVE_READ_FORMAT_CAPS_NONE; 811299529Smm} 812299529Smm 813299529Smm/* 814228753Smm * Read data from an archive entry, using a read(2)-style interface. 815228753Smm * This is a convenience routine that just calls 816228753Smm * archive_read_data_block and copies the results into the client 817228753Smm * buffer, filling any gaps with zero bytes. Clients using this 818228753Smm * API can be completely ignorant of sparse-file issues; sparse files 819228753Smm * will simply be padded with nulls. 820228753Smm * 821228753Smm * DO NOT intermingle calls to this function and archive_read_data_block 822228753Smm * to read a single entry body. 823228753Smm */ 824228753Smmssize_t 825228753Smmarchive_read_data(struct archive *_a, void *buff, size_t s) 826228753Smm{ 827299529Smm struct archive *a = (struct archive *)_a; 828228753Smm char *dest; 829228753Smm const void *read_buf; 830228753Smm size_t bytes_read; 831228753Smm size_t len; 832228753Smm int r; 833228753Smm 834228753Smm bytes_read = 0; 835228753Smm dest = (char *)buff; 836228753Smm 837228753Smm while (s > 0) { 838228753Smm if (a->read_data_remaining == 0) { 839228753Smm read_buf = a->read_data_block; 840248616Smm a->read_data_is_posix_read = 1; 841248616Smm a->read_data_requested = s; 842299529Smm r = archive_read_data_block(a, &read_buf, 843228753Smm &a->read_data_remaining, &a->read_data_offset); 844228753Smm a->read_data_block = read_buf; 845228753Smm if (r == ARCHIVE_EOF) 846228753Smm return (bytes_read); 847228753Smm /* 848228753Smm * Error codes are all negative, so the status 849228753Smm * return here cannot be confused with a valid 850228753Smm * byte count. (ARCHIVE_OK is zero.) 851228753Smm */ 852228753Smm if (r < ARCHIVE_OK) 853228753Smm return (r); 854228753Smm } 855228753Smm 856228753Smm if (a->read_data_offset < a->read_data_output_offset) { 857299529Smm archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT, 858228753Smm "Encountered out-of-order sparse blocks"); 859228753Smm return (ARCHIVE_RETRY); 860228753Smm } 861228753Smm 862228753Smm /* Compute the amount of zero padding needed. */ 863232153Smm if (a->read_data_output_offset + (int64_t)s < 864228753Smm a->read_data_offset) { 865228753Smm len = s; 866228753Smm } else if (a->read_data_output_offset < 867228753Smm a->read_data_offset) { 868238856Smm len = (size_t)(a->read_data_offset - 869238856Smm a->read_data_output_offset); 870228753Smm } else 871228753Smm len = 0; 872228753Smm 873228753Smm /* Add zeroes. */ 874228753Smm memset(dest, 0, len); 875228753Smm s -= len; 876228753Smm a->read_data_output_offset += len; 877228753Smm dest += len; 878228753Smm bytes_read += len; 879228753Smm 880228753Smm /* Copy data if there is any space left. */ 881228753Smm if (s > 0) { 882228753Smm len = a->read_data_remaining; 883228753Smm if (len > s) 884228753Smm len = s; 885228753Smm memcpy(dest, a->read_data_block, len); 886228753Smm s -= len; 887228753Smm a->read_data_block += len; 888228753Smm a->read_data_remaining -= len; 889228753Smm a->read_data_output_offset += len; 890228753Smm a->read_data_offset += len; 891228753Smm dest += len; 892228753Smm bytes_read += len; 893228753Smm } 894228753Smm } 895248616Smm a->read_data_is_posix_read = 0; 896248616Smm a->read_data_requested = 0; 897228753Smm return (bytes_read); 898228753Smm} 899228753Smm 900228753Smm/* 901299529Smm * Reset the read_data_* variables, used for starting a new entry. 902299529Smm */ 903299529Smmvoid __archive_reset_read_data(struct archive * a) 904299529Smm{ 905299529Smm a->read_data_output_offset = 0; 906299529Smm a->read_data_remaining = 0; 907299529Smm a->read_data_is_posix_read = 0; 908299529Smm a->read_data_requested = 0; 909299529Smm 910299529Smm /* extra resets, from rar.c */ 911299529Smm a->read_data_block = NULL; 912299529Smm a->read_data_offset = 0; 913299529Smm} 914299529Smm 915299529Smm/* 916228753Smm * Skip over all remaining data in this entry. 917228753Smm */ 918228753Smmint 919228753Smmarchive_read_data_skip(struct archive *_a) 920228753Smm{ 921228753Smm struct archive_read *a = (struct archive_read *)_a; 922228753Smm int r; 923228753Smm const void *buff; 924228753Smm size_t size; 925232153Smm int64_t offset; 926228753Smm 927232153Smm archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA, 928228753Smm "archive_read_data_skip"); 929228753Smm 930228753Smm if (a->format->read_data_skip != NULL) 931228753Smm r = (a->format->read_data_skip)(a); 932228753Smm else { 933228753Smm while ((r = archive_read_data_block(&a->archive, 934228753Smm &buff, &size, &offset)) 935228753Smm == ARCHIVE_OK) 936228753Smm ; 937228753Smm } 938228753Smm 939228753Smm if (r == ARCHIVE_EOF) 940228753Smm r = ARCHIVE_OK; 941228753Smm 942228753Smm a->archive.state = ARCHIVE_STATE_HEADER; 943228753Smm return (r); 944228753Smm} 945228753Smm 946248616Smmint64_t 947248616Smmarchive_seek_data(struct archive *_a, int64_t offset, int whence) 948248616Smm{ 949248616Smm struct archive_read *a = (struct archive_read *)_a; 950248616Smm archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA, 951248616Smm "archive_seek_data_block"); 952248616Smm 953248616Smm if (a->format->seek_data == NULL) { 954248616Smm archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER, 955248616Smm "Internal error: " 956248616Smm "No format_seek_data_block function registered"); 957248616Smm return (ARCHIVE_FATAL); 958248616Smm } 959248616Smm 960248616Smm return (a->format->seek_data)(a, offset, whence); 961248616Smm} 962248616Smm 963228753Smm/* 964228753Smm * Read the next block of entry data from the archive. 965228753Smm * This is a zero-copy interface; the client receives a pointer, 966228753Smm * size, and file offset of the next available block of data. 967228753Smm * 968228753Smm * Returns ARCHIVE_OK if the operation is successful, ARCHIVE_EOF if 969228753Smm * the end of entry is encountered. 970228753Smm */ 971232153Smmstatic int 972232153Smm_archive_read_data_block(struct archive *_a, 973232153Smm const void **buff, size_t *size, int64_t *offset) 974228753Smm{ 975228753Smm struct archive_read *a = (struct archive_read *)_a; 976232153Smm archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA, 977228753Smm "archive_read_data_block"); 978228753Smm 979228753Smm if (a->format->read_data == NULL) { 980228753Smm archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER, 981228753Smm "Internal error: " 982299529Smm "No format->read_data function registered"); 983228753Smm return (ARCHIVE_FATAL); 984228753Smm } 985228753Smm 986228753Smm return (a->format->read_data)(a, buff, size, offset); 987228753Smm} 988228753Smm 989248616Smmint 990248616Smm__archive_read_close_filters(struct archive_read *a) 991232153Smm{ 992232153Smm struct archive_read_filter *f = a->filter; 993232153Smm int r = ARCHIVE_OK; 994232153Smm /* Close each filter in the pipeline. */ 995232153Smm while (f != NULL) { 996232153Smm struct archive_read_filter *t = f->upstream; 997232153Smm if (!f->closed && f->close != NULL) { 998232153Smm int r1 = (f->close)(f); 999232153Smm f->closed = 1; 1000232153Smm if (r1 < r) 1001232153Smm r = r1; 1002232153Smm } 1003232153Smm free(f->buffer); 1004232153Smm f->buffer = NULL; 1005232153Smm f = t; 1006232153Smm } 1007232153Smm return r; 1008232153Smm} 1009232153Smm 1010248616Smmvoid 1011248616Smm__archive_read_free_filters(struct archive_read *a) 1012232153Smm{ 1013232153Smm while (a->filter != NULL) { 1014232153Smm struct archive_read_filter *t = a->filter->upstream; 1015232153Smm free(a->filter); 1016232153Smm a->filter = t; 1017232153Smm } 1018232153Smm} 1019232153Smm 1020228753Smm/* 1021232153Smm * return the count of # of filters in use 1022228753Smm */ 1023228753Smmstatic int 1024232153Smm_archive_filter_count(struct archive *_a) 1025232153Smm{ 1026232153Smm struct archive_read *a = (struct archive_read *)_a; 1027232153Smm struct archive_read_filter *p = a->filter; 1028232153Smm int count = 0; 1029232153Smm while(p) { 1030232153Smm count++; 1031232153Smm p = p->upstream; 1032232153Smm } 1033232153Smm return count; 1034232153Smm} 1035232153Smm 1036232153Smm/* 1037232153Smm * Close the file and all I/O. 1038232153Smm */ 1039232153Smmstatic int 1040228753Smm_archive_read_close(struct archive *_a) 1041228753Smm{ 1042228753Smm struct archive_read *a = (struct archive_read *)_a; 1043228753Smm int r = ARCHIVE_OK, r1 = ARCHIVE_OK; 1044228753Smm 1045232153Smm archive_check_magic(&a->archive, ARCHIVE_READ_MAGIC, 1046232153Smm ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_read_close"); 1047232153Smm if (a->archive.state == ARCHIVE_STATE_CLOSED) 1048232153Smm return (ARCHIVE_OK); 1049228753Smm archive_clear_error(&a->archive); 1050228753Smm a->archive.state = ARCHIVE_STATE_CLOSED; 1051228753Smm 1052228753Smm /* TODO: Clean up the formatters. */ 1053228753Smm 1054228753Smm /* Release the filter objects. */ 1055248616Smm r1 = __archive_read_close_filters(a); 1056228753Smm if (r1 < r) 1057228753Smm r = r1; 1058228753Smm 1059228753Smm return (r); 1060228753Smm} 1061228753Smm 1062228753Smm/* 1063228753Smm * Release memory and other resources. 1064228753Smm */ 1065228753Smmstatic int 1066228773Smm_archive_read_free(struct archive *_a) 1067228753Smm{ 1068228753Smm struct archive_read *a = (struct archive_read *)_a; 1069299529Smm struct archive_read_passphrase *p; 1070232153Smm int i, n; 1071228753Smm int slots; 1072228753Smm int r = ARCHIVE_OK; 1073228753Smm 1074232153Smm if (_a == NULL) 1075232153Smm return (ARCHIVE_OK); 1076232153Smm archive_check_magic(_a, ARCHIVE_READ_MAGIC, 1077232153Smm ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_read_free"); 1078232153Smm if (a->archive.state != ARCHIVE_STATE_CLOSED 1079232153Smm && a->archive.state != ARCHIVE_STATE_FATAL) 1080228753Smm r = archive_read_close(&a->archive); 1081228753Smm 1082232153Smm /* Call cleanup functions registered by optional components. */ 1083232153Smm if (a->cleanup_archive_extract != NULL) 1084232153Smm r = (a->cleanup_archive_extract)(a); 1085232153Smm 1086228753Smm /* Cleanup format-specific data. */ 1087228753Smm slots = sizeof(a->formats) / sizeof(a->formats[0]); 1088228753Smm for (i = 0; i < slots; i++) { 1089228753Smm a->format = &(a->formats[i]); 1090228753Smm if (a->formats[i].cleanup) 1091228753Smm (a->formats[i].cleanup)(a); 1092228753Smm } 1093228753Smm 1094232153Smm /* Free the filters */ 1095248616Smm __archive_read_free_filters(a); 1096232153Smm 1097232153Smm /* Release the bidder objects. */ 1098232153Smm n = sizeof(a->bidders)/sizeof(a->bidders[0]); 1099232153Smm for (i = 0; i < n; i++) { 1100232153Smm if (a->bidders[i].free != NULL) { 1101232153Smm int r1 = (a->bidders[i].free)(&a->bidders[i]); 1102232153Smm if (r1 < r) 1103232153Smm r = r1; 1104232153Smm } 1105232153Smm } 1106232153Smm 1107299529Smm /* Release passphrase list. */ 1108299529Smm p = a->passphrases.first; 1109299529Smm while (p != NULL) { 1110299529Smm struct archive_read_passphrase *np = p->next; 1111299529Smm 1112299529Smm /* A passphrase should be cleaned. */ 1113299529Smm memset(p->passphrase, 0, strlen(p->passphrase)); 1114299529Smm free(p->passphrase); 1115299529Smm free(p); 1116299529Smm p = np; 1117299529Smm } 1118299529Smm 1119228753Smm archive_string_free(&a->archive.error_string); 1120299529Smm archive_entry_free(a->entry); 1121228753Smm a->archive.magic = 0; 1122232153Smm __archive_clean(&a->archive); 1123248616Smm free(a->client.dataset); 1124228753Smm free(a); 1125228753Smm return (r); 1126228753Smm} 1127228753Smm 1128232153Smmstatic struct archive_read_filter * 1129232153Smmget_filter(struct archive *_a, int n) 1130232153Smm{ 1131232153Smm struct archive_read *a = (struct archive_read *)_a; 1132232153Smm struct archive_read_filter *f = a->filter; 1133248616Smm /* We use n == -1 for 'the last filter', which is always the 1134248616Smm * client proxy. */ 1135232153Smm if (n == -1 && f != NULL) { 1136232153Smm struct archive_read_filter *last = f; 1137232153Smm f = f->upstream; 1138232153Smm while (f != NULL) { 1139232153Smm last = f; 1140232153Smm f = f->upstream; 1141232153Smm } 1142232153Smm return (last); 1143232153Smm } 1144232153Smm if (n < 0) 1145232153Smm return NULL; 1146232153Smm while (n > 0 && f != NULL) { 1147232153Smm f = f->upstream; 1148232153Smm --n; 1149232153Smm } 1150232153Smm return (f); 1151232153Smm} 1152232153Smm 1153232153Smmstatic int 1154232153Smm_archive_filter_code(struct archive *_a, int n) 1155232153Smm{ 1156232153Smm struct archive_read_filter *f = get_filter(_a, n); 1157232153Smm return f == NULL ? -1 : f->code; 1158232153Smm} 1159232153Smm 1160232153Smmstatic const char * 1161232153Smm_archive_filter_name(struct archive *_a, int n) 1162232153Smm{ 1163232153Smm struct archive_read_filter *f = get_filter(_a, n); 1164299529Smm return f != NULL ? f->name : NULL; 1165232153Smm} 1166232153Smm 1167232153Smmstatic int64_t 1168232153Smm_archive_filter_bytes(struct archive *_a, int n) 1169232153Smm{ 1170232153Smm struct archive_read_filter *f = get_filter(_a, n); 1171232153Smm return f == NULL ? -1 : f->position; 1172232153Smm} 1173232153Smm 1174228753Smm/* 1175228753Smm * Used internally by read format handlers to register their bid and 1176228753Smm * initialization functions. 1177228753Smm */ 1178228753Smmint 1179228753Smm__archive_read_register_format(struct archive_read *a, 1180228753Smm void *format_data, 1181228753Smm const char *name, 1182232153Smm int (*bid)(struct archive_read *, int), 1183228753Smm int (*options)(struct archive_read *, const char *, const char *), 1184228753Smm int (*read_header)(struct archive_read *, struct archive_entry *), 1185232153Smm int (*read_data)(struct archive_read *, const void **, size_t *, int64_t *), 1186228753Smm int (*read_data_skip)(struct archive_read *), 1187248616Smm int64_t (*seek_data)(struct archive_read *, int64_t, int), 1188299529Smm int (*cleanup)(struct archive_read *), 1189299529Smm int (*format_capabilities)(struct archive_read *), 1190299529Smm int (*has_encrypted_entries)(struct archive_read *)) 1191228753Smm{ 1192228753Smm int i, number_slots; 1193228753Smm 1194232153Smm archive_check_magic(&a->archive, 1195228753Smm ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW, 1196228753Smm "__archive_read_register_format"); 1197228753Smm 1198228753Smm number_slots = sizeof(a->formats) / sizeof(a->formats[0]); 1199228753Smm 1200228753Smm for (i = 0; i < number_slots; i++) { 1201228753Smm if (a->formats[i].bid == bid) 1202228753Smm return (ARCHIVE_WARN); /* We've already installed */ 1203228753Smm if (a->formats[i].bid == NULL) { 1204228753Smm a->formats[i].bid = bid; 1205228753Smm a->formats[i].options = options; 1206228753Smm a->formats[i].read_header = read_header; 1207228753Smm a->formats[i].read_data = read_data; 1208228753Smm a->formats[i].read_data_skip = read_data_skip; 1209248616Smm a->formats[i].seek_data = seek_data; 1210228753Smm a->formats[i].cleanup = cleanup; 1211228753Smm a->formats[i].data = format_data; 1212228753Smm a->formats[i].name = name; 1213299529Smm a->formats[i].format_capabilties = format_capabilities; 1214299529Smm a->formats[i].has_encrypted_entries = has_encrypted_entries; 1215228753Smm return (ARCHIVE_OK); 1216228753Smm } 1217228753Smm } 1218228753Smm 1219232153Smm archive_set_error(&a->archive, ENOMEM, 1220232153Smm "Not enough slots for format registration"); 1221232153Smm return (ARCHIVE_FATAL); 1222228753Smm} 1223228753Smm 1224228753Smm/* 1225228753Smm * Used internally by decompression routines to register their bid and 1226228753Smm * initialization functions. 1227228753Smm */ 1228232153Smmint 1229232153Smm__archive_read_get_bidder(struct archive_read *a, 1230232153Smm struct archive_read_filter_bidder **bidder) 1231228753Smm{ 1232228753Smm int i, number_slots; 1233228753Smm 1234228753Smm number_slots = sizeof(a->bidders) / sizeof(a->bidders[0]); 1235228753Smm 1236228753Smm for (i = 0; i < number_slots; i++) { 1237228753Smm if (a->bidders[i].bid == NULL) { 1238228753Smm memset(a->bidders + i, 0, sizeof(a->bidders[0])); 1239232153Smm *bidder = (a->bidders + i); 1240232153Smm return (ARCHIVE_OK); 1241228753Smm } 1242228753Smm } 1243228753Smm 1244232153Smm archive_set_error(&a->archive, ENOMEM, 1245232153Smm "Not enough slots for filter registration"); 1246232153Smm return (ARCHIVE_FATAL); 1247228753Smm} 1248228753Smm 1249228753Smm/* 1250232153Smm * The next section implements the peek/consume internal I/O 1251232153Smm * system used by archive readers. This system allows simple 1252232153Smm * read-ahead for consumers while preserving zero-copy operation 1253232153Smm * most of the time. 1254228753Smm * 1255232153Smm * The two key operations: 1256232153Smm * * The read-ahead function returns a pointer to a block of data 1257232153Smm * that satisfies a minimum request. 1258232153Smm * * The consume function advances the file pointer. 1259232153Smm * 1260228753Smm * In the ideal case, filters generate blocks of data 1261228753Smm * and __archive_read_ahead() just returns pointers directly into 1262228753Smm * those blocks. Then __archive_read_consume() just bumps those 1263228753Smm * pointers. Only if your request would span blocks does the I/O 1264228753Smm * layer use a copy buffer to provide you with a contiguous block of 1265232153Smm * data. 1266228753Smm * 1267228753Smm * A couple of useful idioms: 1268228753Smm * * "I just want some data." Ask for 1 byte and pay attention to 1269228753Smm * the "number of bytes available" from __archive_read_ahead(). 1270232153Smm * Consume whatever you actually use. 1271228753Smm * * "I want to output a large block of data." As above, ask for 1 byte, 1272232153Smm * emit all that's available (up to whatever limit you have), consume 1273232153Smm * it all, then repeat until you're done. This effectively means that 1274232153Smm * you're passing along the blocks that came from your provider. 1275228753Smm * * "I want to peek ahead by a large amount." Ask for 4k or so, then 1276228753Smm * double and repeat until you get an error or have enough. Note 1277228753Smm * that the I/O layer will likely end up expanding its copy buffer 1278228753Smm * to fit your request, so use this technique cautiously. This 1279228753Smm * technique is used, for example, by some of the format tasting 1280228753Smm * code that has uncertain look-ahead needs. 1281228753Smm */ 1282228753Smm 1283228753Smm/* 1284228753Smm * Looks ahead in the input stream: 1285228753Smm * * If 'avail' pointer is provided, that returns number of bytes available 1286228753Smm * in the current buffer, which may be much larger than requested. 1287228753Smm * * If end-of-file, *avail gets set to zero. 1288228753Smm * * If error, *avail gets error code. 1289232153Smm * * If request can be met, returns pointer to data. 1290232153Smm * * If minimum request cannot be met, returns NULL. 1291228753Smm * 1292228753Smm * Note: If you just want "some data", ask for 1 byte and pay attention 1293228753Smm * to *avail, which will have the actual amount available. If you 1294228753Smm * know exactly how many bytes you need, just ask for that and treat 1295228753Smm * a NULL return as an error. 1296228753Smm * 1297228753Smm * Important: This does NOT move the file pointer. See 1298228753Smm * __archive_read_consume() below. 1299228753Smm */ 1300228753Smmconst void * 1301228753Smm__archive_read_ahead(struct archive_read *a, size_t min, ssize_t *avail) 1302228753Smm{ 1303228753Smm return (__archive_read_filter_ahead(a->filter, min, avail)); 1304228753Smm} 1305228753Smm 1306228753Smmconst void * 1307228753Smm__archive_read_filter_ahead(struct archive_read_filter *filter, 1308228753Smm size_t min, ssize_t *avail) 1309228753Smm{ 1310228753Smm ssize_t bytes_read; 1311228753Smm size_t tocopy; 1312228753Smm 1313228753Smm if (filter->fatal) { 1314228753Smm if (avail) 1315228753Smm *avail = ARCHIVE_FATAL; 1316228753Smm return (NULL); 1317228753Smm } 1318228753Smm 1319228753Smm /* 1320228753Smm * Keep pulling more data until we can satisfy the request. 1321228753Smm */ 1322228753Smm for (;;) { 1323228753Smm 1324228753Smm /* 1325228753Smm * If we can satisfy from the copy buffer (and the 1326228753Smm * copy buffer isn't empty), we're done. In particular, 1327228753Smm * note that min == 0 is a perfectly well-defined 1328228753Smm * request. 1329228753Smm */ 1330228753Smm if (filter->avail >= min && filter->avail > 0) { 1331228753Smm if (avail != NULL) 1332228753Smm *avail = filter->avail; 1333228753Smm return (filter->next); 1334228753Smm } 1335228753Smm 1336228753Smm /* 1337228753Smm * We can satisfy directly from client buffer if everything 1338228753Smm * currently in the copy buffer is still in the client buffer. 1339228753Smm */ 1340228753Smm if (filter->client_total >= filter->client_avail + filter->avail 1341228753Smm && filter->client_avail + filter->avail >= min) { 1342228753Smm /* "Roll back" to client buffer. */ 1343228753Smm filter->client_avail += filter->avail; 1344228753Smm filter->client_next -= filter->avail; 1345228753Smm /* Copy buffer is now empty. */ 1346228753Smm filter->avail = 0; 1347228753Smm filter->next = filter->buffer; 1348228753Smm /* Return data from client buffer. */ 1349228753Smm if (avail != NULL) 1350228753Smm *avail = filter->client_avail; 1351228753Smm return (filter->client_next); 1352228753Smm } 1353228753Smm 1354228753Smm /* Move data forward in copy buffer if necessary. */ 1355228753Smm if (filter->next > filter->buffer && 1356228753Smm filter->next + min > filter->buffer + filter->buffer_size) { 1357228753Smm if (filter->avail > 0) 1358248616Smm memmove(filter->buffer, filter->next, 1359248616Smm filter->avail); 1360228753Smm filter->next = filter->buffer; 1361228753Smm } 1362228753Smm 1363228753Smm /* If we've used up the client data, get more. */ 1364228753Smm if (filter->client_avail <= 0) { 1365228753Smm if (filter->end_of_file) { 1366228753Smm if (avail != NULL) 1367228753Smm *avail = 0; 1368228753Smm return (NULL); 1369228753Smm } 1370228753Smm bytes_read = (filter->read)(filter, 1371228753Smm &filter->client_buff); 1372228753Smm if (bytes_read < 0) { /* Read error. */ 1373228753Smm filter->client_total = filter->client_avail = 0; 1374248616Smm filter->client_next = 1375248616Smm filter->client_buff = NULL; 1376228753Smm filter->fatal = 1; 1377228753Smm if (avail != NULL) 1378228753Smm *avail = ARCHIVE_FATAL; 1379228753Smm return (NULL); 1380228753Smm } 1381248616Smm if (bytes_read == 0) { 1382248616Smm /* Check for another client object first */ 1383248616Smm if (filter->archive->client.cursor != 1384248616Smm filter->archive->client.nodes - 1) { 1385248616Smm if (client_switch_proxy(filter, 1386248616Smm filter->archive->client.cursor + 1) 1387248616Smm == ARCHIVE_OK) 1388248616Smm continue; 1389248616Smm } 1390248616Smm /* Premature end-of-file. */ 1391228753Smm filter->client_total = filter->client_avail = 0; 1392248616Smm filter->client_next = 1393248616Smm filter->client_buff = NULL; 1394228753Smm filter->end_of_file = 1; 1395228753Smm /* Return whatever we do have. */ 1396228753Smm if (avail != NULL) 1397228753Smm *avail = filter->avail; 1398228753Smm return (NULL); 1399228753Smm } 1400228753Smm filter->client_total = bytes_read; 1401228753Smm filter->client_avail = filter->client_total; 1402228753Smm filter->client_next = filter->client_buff; 1403248616Smm } else { 1404228753Smm /* 1405228753Smm * We can't satisfy the request from the copy 1406228753Smm * buffer or the existing client data, so we 1407228753Smm * need to copy more client data over to the 1408228753Smm * copy buffer. 1409228753Smm */ 1410228753Smm 1411228753Smm /* Ensure the buffer is big enough. */ 1412228753Smm if (min > filter->buffer_size) { 1413228753Smm size_t s, t; 1414228753Smm char *p; 1415228753Smm 1416228753Smm /* Double the buffer; watch for overflow. */ 1417228753Smm s = t = filter->buffer_size; 1418228753Smm if (s == 0) 1419228753Smm s = min; 1420228753Smm while (s < min) { 1421228753Smm t *= 2; 1422228753Smm if (t <= s) { /* Integer overflow! */ 1423228753Smm archive_set_error( 1424248616Smm &filter->archive->archive, 1425248616Smm ENOMEM, 1426248616Smm "Unable to allocate copy" 1427248616Smm " buffer"); 1428228753Smm filter->fatal = 1; 1429228753Smm if (avail != NULL) 1430228753Smm *avail = ARCHIVE_FATAL; 1431228753Smm return (NULL); 1432228753Smm } 1433228753Smm s = t; 1434228753Smm } 1435228753Smm /* Now s >= min, so allocate a new buffer. */ 1436228753Smm p = (char *)malloc(s); 1437228753Smm if (p == NULL) { 1438228753Smm archive_set_error( 1439228753Smm &filter->archive->archive, 1440228753Smm ENOMEM, 1441228753Smm "Unable to allocate copy buffer"); 1442228753Smm filter->fatal = 1; 1443228753Smm if (avail != NULL) 1444228753Smm *avail = ARCHIVE_FATAL; 1445228753Smm return (NULL); 1446228753Smm } 1447228753Smm /* Move data into newly-enlarged buffer. */ 1448228753Smm if (filter->avail > 0) 1449228753Smm memmove(p, filter->next, filter->avail); 1450228753Smm free(filter->buffer); 1451228753Smm filter->next = filter->buffer = p; 1452228753Smm filter->buffer_size = s; 1453228753Smm } 1454228753Smm 1455228753Smm /* We can add client data to copy buffer. */ 1456228753Smm /* First estimate: copy to fill rest of buffer. */ 1457228753Smm tocopy = (filter->buffer + filter->buffer_size) 1458228753Smm - (filter->next + filter->avail); 1459228753Smm /* Don't waste time buffering more than we need to. */ 1460228753Smm if (tocopy + filter->avail > min) 1461228753Smm tocopy = min - filter->avail; 1462228753Smm /* Don't copy more than is available. */ 1463228753Smm if (tocopy > filter->client_avail) 1464228753Smm tocopy = filter->client_avail; 1465228753Smm 1466248616Smm memcpy(filter->next + filter->avail, 1467248616Smm filter->client_next, tocopy); 1468228753Smm /* Remove this data from client buffer. */ 1469228753Smm filter->client_next += tocopy; 1470228753Smm filter->client_avail -= tocopy; 1471228753Smm /* add it to copy buffer. */ 1472228753Smm filter->avail += tocopy; 1473228753Smm } 1474228753Smm } 1475228753Smm} 1476228753Smm 1477228753Smm/* 1478232153Smm * Move the file pointer forward. 1479228753Smm */ 1480232153Smmint64_t 1481232153Smm__archive_read_consume(struct archive_read *a, int64_t request) 1482228753Smm{ 1483232153Smm return (__archive_read_filter_consume(a->filter, request)); 1484228753Smm} 1485228753Smm 1486232153Smmint64_t 1487228753Smm__archive_read_filter_consume(struct archive_read_filter * filter, 1488232153Smm int64_t request) 1489228753Smm{ 1490232153Smm int64_t skipped; 1491228753Smm 1492282932Sdelphij if (request < 0) 1493282932Sdelphij return ARCHIVE_FATAL; 1494232153Smm if (request == 0) 1495232153Smm return 0; 1496232153Smm 1497232153Smm skipped = advance_file_pointer(filter, request); 1498228753Smm if (skipped == request) 1499228753Smm return (skipped); 1500228753Smm /* We hit EOF before we satisfied the skip request. */ 1501232153Smm if (skipped < 0) /* Map error code to 0 for error message below. */ 1502228753Smm skipped = 0; 1503232153Smm archive_set_error(&filter->archive->archive, 1504228753Smm ARCHIVE_ERRNO_MISC, 1505228753Smm "Truncated input file (needed %jd bytes, only %jd available)", 1506228753Smm (intmax_t)request, (intmax_t)skipped); 1507228753Smm return (ARCHIVE_FATAL); 1508228753Smm} 1509228753Smm 1510232153Smm/* 1511232153Smm * Advance the file pointer by the amount requested. 1512232153Smm * Returns the amount actually advanced, which may be less than the 1513232153Smm * request if EOF is encountered first. 1514232153Smm * Returns a negative value if there's an I/O error. 1515232153Smm */ 1516232153Smmstatic int64_t 1517232153Smmadvance_file_pointer(struct archive_read_filter *filter, int64_t request) 1518228753Smm{ 1519228753Smm int64_t bytes_skipped, total_bytes_skipped = 0; 1520232153Smm ssize_t bytes_read; 1521228753Smm size_t min; 1522228753Smm 1523228753Smm if (filter->fatal) 1524228753Smm return (-1); 1525232153Smm 1526232153Smm /* Use up the copy buffer first. */ 1527228753Smm if (filter->avail > 0) { 1528238856Smm min = (size_t)minimum(request, (int64_t)filter->avail); 1529232153Smm filter->next += min; 1530232153Smm filter->avail -= min; 1531232153Smm request -= min; 1532232153Smm filter->position += min; 1533232153Smm total_bytes_skipped += min; 1534228753Smm } 1535232153Smm 1536232153Smm /* Then use up the client buffer. */ 1537228753Smm if (filter->client_avail > 0) { 1538238856Smm min = (size_t)minimum(request, (int64_t)filter->client_avail); 1539232153Smm filter->client_next += min; 1540232153Smm filter->client_avail -= min; 1541232153Smm request -= min; 1542232153Smm filter->position += min; 1543232153Smm total_bytes_skipped += min; 1544228753Smm } 1545228753Smm if (request == 0) 1546228753Smm return (total_bytes_skipped); 1547232153Smm 1548232153Smm /* If there's an optimized skip function, use it. */ 1549228753Smm if (filter->skip != NULL) { 1550228753Smm bytes_skipped = (filter->skip)(filter, request); 1551228753Smm if (bytes_skipped < 0) { /* error */ 1552228753Smm filter->fatal = 1; 1553228753Smm return (bytes_skipped); 1554228753Smm } 1555232153Smm filter->position += bytes_skipped; 1556228753Smm total_bytes_skipped += bytes_skipped; 1557228753Smm request -= bytes_skipped; 1558232153Smm if (request == 0) 1559232153Smm return (total_bytes_skipped); 1560228753Smm } 1561232153Smm 1562232153Smm /* Use ordinary reads as necessary to complete the request. */ 1563232153Smm for (;;) { 1564232153Smm bytes_read = (filter->read)(filter, &filter->client_buff); 1565232153Smm if (bytes_read < 0) { 1566232153Smm filter->client_buff = NULL; 1567232153Smm filter->fatal = 1; 1568228753Smm return (bytes_read); 1569232153Smm } 1570232153Smm 1571228753Smm if (bytes_read == 0) { 1572248616Smm if (filter->archive->client.cursor != 1573248616Smm filter->archive->client.nodes - 1) { 1574248616Smm if (client_switch_proxy(filter, 1575248616Smm filter->archive->client.cursor + 1) 1576248616Smm == ARCHIVE_OK) 1577248616Smm continue; 1578248616Smm } 1579232153Smm filter->client_buff = NULL; 1580232153Smm filter->end_of_file = 1; 1581228753Smm return (total_bytes_skipped); 1582228753Smm } 1583232153Smm 1584232153Smm if (bytes_read >= request) { 1585232153Smm filter->client_next = 1586232153Smm ((const char *)filter->client_buff) + request; 1587238856Smm filter->client_avail = (size_t)(bytes_read - request); 1588232153Smm filter->client_total = bytes_read; 1589232153Smm total_bytes_skipped += request; 1590232153Smm filter->position += request; 1591232153Smm return (total_bytes_skipped); 1592232153Smm } 1593232153Smm 1594232153Smm filter->position += bytes_read; 1595228753Smm total_bytes_skipped += bytes_read; 1596228753Smm request -= bytes_read; 1597228753Smm } 1598228753Smm} 1599232153Smm 1600232153Smm/** 1601232153Smm * Returns ARCHIVE_FAILED if seeking isn't supported. 1602232153Smm */ 1603232153Smmint64_t 1604232153Smm__archive_read_seek(struct archive_read *a, int64_t offset, int whence) 1605232153Smm{ 1606232153Smm return __archive_read_filter_seek(a->filter, offset, whence); 1607232153Smm} 1608232153Smm 1609232153Smmint64_t 1610248616Smm__archive_read_filter_seek(struct archive_read_filter *filter, int64_t offset, 1611248616Smm int whence) 1612232153Smm{ 1613248616Smm struct archive_read_client *client; 1614232153Smm int64_t r; 1615248616Smm unsigned int cursor; 1616232153Smm 1617232153Smm if (filter->closed || filter->fatal) 1618232153Smm return (ARCHIVE_FATAL); 1619232153Smm if (filter->seek == NULL) 1620232153Smm return (ARCHIVE_FAILED); 1621248616Smm 1622248616Smm client = &(filter->archive->client); 1623248616Smm switch (whence) { 1624248616Smm case SEEK_CUR: 1625248616Smm /* Adjust the offset and use SEEK_SET instead */ 1626248616Smm offset += filter->position; 1627248616Smm case SEEK_SET: 1628248616Smm cursor = 0; 1629248616Smm while (1) 1630248616Smm { 1631248616Smm if (client->dataset[cursor].begin_position < 0 || 1632248616Smm client->dataset[cursor].total_size < 0 || 1633248616Smm client->dataset[cursor].begin_position + 1634248616Smm client->dataset[cursor].total_size - 1 > offset || 1635248616Smm cursor + 1 >= client->nodes) 1636248616Smm break; 1637248616Smm r = client->dataset[cursor].begin_position + 1638248616Smm client->dataset[cursor].total_size; 1639248616Smm client->dataset[++cursor].begin_position = r; 1640248616Smm } 1641248616Smm while (1) { 1642248616Smm r = client_switch_proxy(filter, cursor); 1643248616Smm if (r != ARCHIVE_OK) 1644248616Smm return r; 1645248616Smm if ((r = client_seek_proxy(filter, 0, SEEK_END)) < 0) 1646248616Smm return r; 1647248616Smm client->dataset[cursor].total_size = r; 1648248616Smm if (client->dataset[cursor].begin_position + 1649248616Smm client->dataset[cursor].total_size - 1 > offset || 1650248616Smm cursor + 1 >= client->nodes) 1651248616Smm break; 1652248616Smm r = client->dataset[cursor].begin_position + 1653248616Smm client->dataset[cursor].total_size; 1654248616Smm client->dataset[++cursor].begin_position = r; 1655248616Smm } 1656248616Smm offset -= client->dataset[cursor].begin_position; 1657299529Smm if (offset < 0 1658299529Smm || offset > client->dataset[cursor].total_size) 1659299529Smm return ARCHIVE_FATAL; 1660248616Smm if ((r = client_seek_proxy(filter, offset, SEEK_SET)) < 0) 1661248616Smm return r; 1662248616Smm break; 1663248616Smm 1664248616Smm case SEEK_END: 1665248616Smm cursor = 0; 1666248616Smm while (1) { 1667248616Smm if (client->dataset[cursor].begin_position < 0 || 1668248616Smm client->dataset[cursor].total_size < 0 || 1669248616Smm cursor + 1 >= client->nodes) 1670248616Smm break; 1671248616Smm r = client->dataset[cursor].begin_position + 1672248616Smm client->dataset[cursor].total_size; 1673248616Smm client->dataset[++cursor].begin_position = r; 1674248616Smm } 1675248616Smm while (1) { 1676248616Smm r = client_switch_proxy(filter, cursor); 1677248616Smm if (r != ARCHIVE_OK) 1678248616Smm return r; 1679248616Smm if ((r = client_seek_proxy(filter, 0, SEEK_END)) < 0) 1680248616Smm return r; 1681248616Smm client->dataset[cursor].total_size = r; 1682248616Smm r = client->dataset[cursor].begin_position + 1683248616Smm client->dataset[cursor].total_size; 1684248616Smm if (cursor + 1 >= client->nodes) 1685248616Smm break; 1686248616Smm client->dataset[++cursor].begin_position = r; 1687248616Smm } 1688248616Smm while (1) { 1689248616Smm if (r + offset >= 1690248616Smm client->dataset[cursor].begin_position) 1691248616Smm break; 1692248616Smm offset += client->dataset[cursor].total_size; 1693248616Smm if (cursor == 0) 1694248616Smm break; 1695248616Smm cursor--; 1696248616Smm r = client->dataset[cursor].begin_position + 1697248616Smm client->dataset[cursor].total_size; 1698248616Smm } 1699248616Smm offset = (r + offset) - client->dataset[cursor].begin_position; 1700248616Smm if ((r = client_switch_proxy(filter, cursor)) != ARCHIVE_OK) 1701248616Smm return r; 1702248616Smm r = client_seek_proxy(filter, offset, SEEK_SET); 1703248616Smm if (r < ARCHIVE_OK) 1704248616Smm return r; 1705248616Smm break; 1706248616Smm 1707248616Smm default: 1708248616Smm return (ARCHIVE_FATAL); 1709248616Smm } 1710248616Smm r += client->dataset[cursor].begin_position; 1711248616Smm 1712232153Smm if (r >= 0) { 1713232153Smm /* 1714232153Smm * Ouch. Clearing the buffer like this hurts, especially 1715232153Smm * at bid time. A lot of our efficiency at bid time comes 1716232153Smm * from having bidders reuse the data we've already read. 1717232153Smm * 1718232153Smm * TODO: If the seek request is in data we already 1719232153Smm * have, then don't call the seek callback. 1720232153Smm * 1721232153Smm * TODO: Zip seeks to end-of-file at bid time. If 1722232153Smm * other formats also start doing this, we may need to 1723232153Smm * find a way for clients to fudge the seek offset to 1724232153Smm * a block boundary. 1725232153Smm * 1726232153Smm * Hmmm... If whence was SEEK_END, we know the file 1727232153Smm * size is (r - offset). Can we use that to simplify 1728232153Smm * the TODO items above? 1729232153Smm */ 1730232153Smm filter->avail = filter->client_avail = 0; 1731232153Smm filter->next = filter->buffer; 1732232153Smm filter->position = r; 1733232153Smm filter->end_of_file = 0; 1734232153Smm } 1735232153Smm return r; 1736232153Smm} 1737