archive_read_private.h revision 302001
11590Srgrimes/*- 21590Srgrimes * Copyright (c) 2003-2007 Tim Kientzle 31590Srgrimes * All rights reserved. 41590Srgrimes * 51590Srgrimes * Redistribution and use in source and binary forms, with or without 61590Srgrimes * modification, are permitted provided that the following conditions 71590Srgrimes * are met: 81590Srgrimes * 1. Redistributions of source code must retain the above copyright 91590Srgrimes * notice, this list of conditions and the following disclaimer. 101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111590Srgrimes * notice, this list of conditions and the following disclaimer in the 121590Srgrimes * documentation and/or other materials provided with the distribution. 131590Srgrimes * 141590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 151590Srgrimes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 161590Srgrimes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 171590Srgrimes * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 181590Srgrimes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 191590Srgrimes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 201590Srgrimes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 211590Srgrimes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 221590Srgrimes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 231590Srgrimes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 241590Srgrimes * 251590Srgrimes * $FreeBSD: stable/10/contrib/libarchive/libarchive/archive_read_private.h 302001 2016-06-17 22:40:10Z mm $ 261590Srgrimes */ 271590Srgrimes 281590Srgrimes#ifndef __LIBARCHIVE_BUILD 291590Srgrimes#ifndef __LIBARCHIVE_TEST 301590Srgrimes#error This header is only to be used internally to libarchive. 311590Srgrimes#endif 321590Srgrimes#endif 3350477Speter 341590Srgrimes#ifndef ARCHIVE_READ_PRIVATE_H_INCLUDED 35131076Stjr#define ARCHIVE_READ_PRIVATE_H_INCLUDED 361590Srgrimes 371590Srgrimes#include "archive.h" 381590Srgrimes#include "archive_string.h" 391590Srgrimes#include "archive_private.h" 401590Srgrimes 411590Srgrimesstruct archive_read; 4227787Scharnierstruct archive_read_filter_bidder; 431590Srgrimesstruct archive_read_filter; 441590Srgrimes 4572432Sru/* 461590Srgrimes * How bidding works for filters: 471590Srgrimes * * The bid manager initializes the client-provided reader as the 4827787Scharnier * first filter. 491590Srgrimes * * It invokes the bidder for each registered filter with the 501590Srgrimes * current head filter. 511590Srgrimes * * The bidders can use archive_read_filter_ahead() to peek ahead 521590Srgrimes * at the incoming data to compose their bids. 531590Srgrimes * * The bid manager creates a new filter structure for the winning 541590Srgrimes * bidder and gives the winning bidder a chance to initialize it. 551590Srgrimes * * The new filter becomes the new top filter and we repeat the 561590Srgrimes * process. 571590Srgrimes * This ends only when no bidder provides a non-zero bid. Then 58131754Sru * we perform a similar dance with the registered format handlers. 591590Srgrimes */ 601590Srgrimesstruct archive_read_filter_bidder { 611590Srgrimes /* Configuration data for the bidder. */ 621590Srgrimes void *data; 631590Srgrimes /* Name of the filter */ 641590Srgrimes const char *name; 651590Srgrimes /* Taste the upstream filter to see if we handle this. */ 661590Srgrimes int (*bid)(struct archive_read_filter_bidder *, 671590Srgrimes struct archive_read_filter *); 681590Srgrimes /* Initialize a newly-created filter. */ 6958617Scharnier int (*init)(struct archive_read_filter *); 7058617Scharnier /* Set an option for the filter bidder. */ 7158617Scharnier int (*options)(struct archive_read_filter_bidder *, 721590Srgrimes const char *key, const char *value); 7327787Scharnier /* Release the bidder's configuration data. */ 741590Srgrimes int (*free)(struct archive_read_filter_bidder *); 751590Srgrimes}; 761590Srgrimes 771590Srgrimes/* 781590Srgrimes * This structure is allocated within the archive_read core 791590Srgrimes * and initialized by archive_read and the init() method of the 801590Srgrimes * corresponding bidder above. 811590Srgrimes */ 821590Srgrimesstruct archive_read_filter { 831590Srgrimes int64_t position; 841590Srgrimes /* Essentially all filters will need these values, so 851590Srgrimes * just declare them here. */ 861590Srgrimes struct archive_read_filter_bidder *bidder; /* My bidder. */ 871590Srgrimes struct archive_read_filter *upstream; /* Who I read from. */ 881590Srgrimes struct archive_read *archive; /* Associated archive. */ 891590Srgrimes /* Open a block for reading */ 901590Srgrimes int (*open)(struct archive_read_filter *self); 911590Srgrimes /* Return next block. */ 921590Srgrimes ssize_t (*read)(struct archive_read_filter *, const void **); 931590Srgrimes /* Skip forward this many bytes. */ 941590Srgrimes int64_t (*skip)(struct archive_read_filter *self, int64_t request); 951590Srgrimes /* Seek to an absolute location. */ 961590Srgrimes int64_t (*seek)(struct archive_read_filter *self, int64_t offset, int whence); 971590Srgrimes /* Close (just this filter) and free(self). */ 9858617Scharnier int (*close)(struct archive_read_filter *self); 9958617Scharnier /* Function that handles switching from reading one block to the next/prev */ 10058617Scharnier int (*sswitch)(struct archive_read_filter *self, unsigned int iindex); 1011590Srgrimes /* My private data. */ 1021590Srgrimes void *data; 1031590Srgrimes 104108221Sru const char *name; 1051590Srgrimes int code; 1061590Srgrimes 1071590Srgrimes /* Used by reblocking logic. */ 108108221Sru char *buffer; 109140420Sru size_t buffer_size; 110140420Sru char *next; /* Current read location. */ 11198089Stjr size_t avail; /* Bytes in my buffer. */ 11298089Stjr const void *client_buff; /* Client buffer information. */ 11398089Stjr size_t client_total; 11498089Stjr const char *client_next; 11598089Stjr size_t client_avail; 11698089Stjr char end_of_file; 11798089Stjr char closed; 11898089Stjr char fatal; 11998089Stjr}; 12098089Stjr 12198089Stjr/* 12298089Stjr * The client looks a lot like a filter, so we just wrap it here. 12398089Stjr * 12498090Sjmallett * TODO: Make archive_read_filter and archive_read_client identical so 125107276Sru * that users of the library can easily register their own 126107276Sru * transformation filters. This will probably break the API/ABI and 127107276Sru * so should be deferred at least until libarchive 3.0. 12898090Sjmallett */ 12998090Sjmallettstruct archive_read_data_node { 13098090Sjmallett int64_t begin_position; 13198090Sjmallett int64_t total_size; 13298090Sjmallett void *data; 1331590Srgrimes}; 13483745Swollmanstruct archive_read_client { 13583745Swollman archive_open_callback *opener; 1361590Srgrimes archive_read_callback *reader; 1371590Srgrimes archive_skip_callback *skipper; 13827787Scharnier archive_seek_callback *seeker; 1391590Srgrimes archive_close_callback *closer; 1401590Srgrimes archive_switch_callback *switcher; 1411590Srgrimes unsigned int nodes; 14298112Stjr unsigned int cursor; 14398112Stjr int64_t position; 14498112Stjr struct archive_read_data_node *dataset; 14598112Stjr}; 14698112Stjrstruct archive_read_passphrase { 147 char *passphrase; 148 struct archive_read_passphrase *next; 149}; 150 151struct archive_read_extract { 152 struct archive *ad; /* archive_write_disk object */ 153 154 /* Progress function invoked during extract. */ 155 void (*extract_progress)(void *); 156 void *extract_progress_user_data; 157}; 158 159struct archive_read { 160 struct archive archive; 161 162 struct archive_entry *entry; 163 164 /* Dev/ino of the archive being read/written. */ 165 int skip_file_set; 166 int64_t skip_file_dev; 167 int64_t skip_file_ino; 168 169 /* Callbacks to open/read/write/close client archive streams. */ 170 struct archive_read_client client; 171 172 /* Registered filter bidders. */ 173 struct archive_read_filter_bidder bidders[16]; 174 175 /* Last filter in chain */ 176 struct archive_read_filter *filter; 177 178 /* Whether to bypass filter bidding process */ 179 int bypass_filter_bidding; 180 181 /* File offset of beginning of most recently-read header. */ 182 int64_t header_position; 183 184 /* Nodes and offsets of compressed data block */ 185 unsigned int data_start_node; 186 unsigned int data_end_node; 187 188 /* 189 * Format detection is mostly the same as compression 190 * detection, with one significant difference: The bidders 191 * use the read_ahead calls above to examine the stream rather 192 * than having the supervisor hand them a block of data to 193 * examine. 194 */ 195 196 struct archive_format_descriptor { 197 void *data; 198 const char *name; 199 int (*bid)(struct archive_read *, int best_bid); 200 int (*options)(struct archive_read *, const char *key, 201 const char *value); 202 int (*read_header)(struct archive_read *, struct archive_entry *); 203 int (*read_data)(struct archive_read *, const void **, size_t *, int64_t *); 204 int (*read_data_skip)(struct archive_read *); 205 int64_t (*seek_data)(struct archive_read *, int64_t, int); 206 int (*cleanup)(struct archive_read *); 207 int (*format_capabilties)(struct archive_read *); 208 int (*has_encrypted_entries)(struct archive_read *); 209 } formats[16]; 210 struct archive_format_descriptor *format; /* Active format. */ 211 212 /* 213 * Various information needed by archive_extract. 214 */ 215 struct archive_read_extract *extract; 216 int (*cleanup_archive_extract)(struct archive_read *); 217 218 /* 219 * Decryption passphrase. 220 */ 221 struct { 222 struct archive_read_passphrase *first; 223 struct archive_read_passphrase **last; 224 int candiate; 225 archive_passphrase_callback *callback; 226 void *client_data; 227 } passphrases; 228}; 229 230int __archive_read_register_format(struct archive_read *a, 231 void *format_data, 232 const char *name, 233 int (*bid)(struct archive_read *, int), 234 int (*options)(struct archive_read *, const char *, const char *), 235 int (*read_header)(struct archive_read *, struct archive_entry *), 236 int (*read_data)(struct archive_read *, const void **, size_t *, int64_t *), 237 int (*read_data_skip)(struct archive_read *), 238 int64_t (*seek_data)(struct archive_read *, int64_t, int), 239 int (*cleanup)(struct archive_read *), 240 int (*format_capabilities)(struct archive_read *), 241 int (*has_encrypted_entries)(struct archive_read *)); 242 243int __archive_read_get_bidder(struct archive_read *a, 244 struct archive_read_filter_bidder **bidder); 245 246const void *__archive_read_ahead(struct archive_read *, size_t, ssize_t *); 247const void *__archive_read_filter_ahead(struct archive_read_filter *, 248 size_t, ssize_t *); 249int64_t __archive_read_seek(struct archive_read*, int64_t, int); 250int64_t __archive_read_filter_seek(struct archive_read_filter *, int64_t, int); 251int64_t __archive_read_consume(struct archive_read *, int64_t); 252int64_t __archive_read_filter_consume(struct archive_read_filter *, int64_t); 253int __archive_read_program(struct archive_read_filter *, const char *); 254void __archive_read_free_filters(struct archive_read *); 255int __archive_read_close_filters(struct archive_read *); 256struct archive_read_extract *__archive_read_get_extract(struct archive_read *); 257 258 259/* 260 * Get a decryption passphrase. 261 */ 262void __archive_read_reset_passphrase(struct archive_read *a); 263const char * __archive_read_next_passphrase(struct archive_read *a); 264#endif 265