archive_read_private.h revision 305192
1174904Sache/*-
2174904Sache * Copyright (c) 2003-2007 Tim Kientzle
3174904Sache * All rights reserved.
4174904Sache *
5174904Sache * Redistribution and use in source and binary forms, with or without
6174904Sache * modification, are permitted provided that the following conditions
7174904Sache * are met:
8174904Sache * 1. Redistributions of source code must retain the above copyright
9174904Sache *    notice, this list of conditions and the following disclaimer.
10174904Sache * 2. Redistributions in binary form must reproduce the above copyright
11174904Sache *    notice, this list of conditions and the following disclaimer in the
12174904Sache *    documentation and/or other materials provided with the distribution.
13174904Sache *
14174904Sache * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15174904Sache * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16174904Sache * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17174904Sache * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18174904Sache * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19174904Sache * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20174904Sache * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21174904Sache * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22174904Sache * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23174904Sache * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24174904Sache *
25174904Sache * $FreeBSD: stable/10/contrib/libarchive/libarchive/archive_read_private.h 305192 2016-09-01 12:01:23Z mm $
26174904Sache */
27174904Sache
28174904Sache#ifndef __LIBARCHIVE_BUILD
29174904Sache#ifndef __LIBARCHIVE_TEST
30174904Sache#error This header is only to be used internally to libarchive.
31174904Sache#endif
32174904Sache#endif
33174904Sache
34174904Sache#ifndef ARCHIVE_READ_PRIVATE_H_INCLUDED
35174904Sache#define	ARCHIVE_READ_PRIVATE_H_INCLUDED
36174904Sache
37174904Sache#include "archive.h"
38174904Sache#include "archive_string.h"
39174904Sache#include "archive_private.h"
40174904Sache
41174904Sachestruct archive_read;
42174904Sachestruct archive_read_filter_bidder;
43174904Sachestruct archive_read_filter;
44174904Sache
45174904Sache/*
46174904Sache * How bidding works for filters:
47174904Sache *   * The bid manager initializes the client-provided reader as the
48174904Sache *     first filter.
49 *   * It invokes the bidder for each registered filter with the
50 *     current head filter.
51 *   * The bidders can use archive_read_filter_ahead() to peek ahead
52 *     at the incoming data to compose their bids.
53 *   * The bid manager creates a new filter structure for the winning
54 *     bidder and gives the winning bidder a chance to initialize it.
55 *   * The new filter becomes the new top filter and we repeat the
56 *     process.
57 * This ends only when no bidder provides a non-zero bid.  Then
58 * we perform a similar dance with the registered format handlers.
59 */
60struct archive_read_filter_bidder {
61	/* Configuration data for the bidder. */
62	void *data;
63	/* Name of the filter */
64	const char *name;
65	/* Taste the upstream filter to see if we handle this. */
66	int (*bid)(struct archive_read_filter_bidder *,
67	    struct archive_read_filter *);
68	/* Initialize a newly-created filter. */
69	int (*init)(struct archive_read_filter *);
70	/* Set an option for the filter bidder. */
71	int (*options)(struct archive_read_filter_bidder *,
72	    const char *key, const char *value);
73	/* Release the bidder's configuration data. */
74	int (*free)(struct archive_read_filter_bidder *);
75};
76
77/*
78 * This structure is allocated within the archive_read core
79 * and initialized by archive_read and the init() method of the
80 * corresponding bidder above.
81 */
82struct archive_read_filter {
83	int64_t position;
84	/* Essentially all filters will need these values, so
85	 * just declare them here. */
86	struct archive_read_filter_bidder *bidder; /* My bidder. */
87	struct archive_read_filter *upstream; /* Who I read from. */
88	struct archive_read *archive; /* Associated archive. */
89	/* Open a block for reading */
90	int (*open)(struct archive_read_filter *self);
91	/* Return next block. */
92	ssize_t (*read)(struct archive_read_filter *, const void **);
93	/* Skip forward this many bytes. */
94	int64_t (*skip)(struct archive_read_filter *self, int64_t request);
95	/* Seek to an absolute location. */
96	int64_t (*seek)(struct archive_read_filter *self, int64_t offset, int whence);
97	/* Close (just this filter) and free(self). */
98	int (*close)(struct archive_read_filter *self);
99	/* Function that handles switching from reading one block to the next/prev */
100	int (*sswitch)(struct archive_read_filter *self, unsigned int iindex);
101	/* My private data. */
102	void *data;
103
104	const char	*name;
105	int		 code;
106
107	/* Used by reblocking logic. */
108	char		*buffer;
109	size_t		 buffer_size;
110	char		*next;		/* Current read location. */
111	size_t		 avail;		/* Bytes in my buffer. */
112	const void	*client_buff;	/* Client buffer information. */
113	size_t		 client_total;
114	const char	*client_next;
115	size_t		 client_avail;
116	char		 end_of_file;
117	char		 closed;
118	char		 fatal;
119};
120
121/*
122 * The client looks a lot like a filter, so we just wrap it here.
123 *
124 * TODO: Make archive_read_filter and archive_read_client identical so
125 * that users of the library can easily register their own
126 * transformation filters.  This will probably break the API/ABI and
127 * so should be deferred at least until libarchive 3.0.
128 */
129struct archive_read_data_node {
130	int64_t begin_position;
131	int64_t total_size;
132	void *data;
133};
134struct archive_read_client {
135	archive_open_callback	*opener;
136	archive_read_callback	*reader;
137	archive_skip_callback	*skipper;
138	archive_seek_callback	*seeker;
139	archive_close_callback	*closer;
140	archive_switch_callback *switcher;
141	unsigned int nodes;
142	unsigned int cursor;
143	int64_t position;
144	struct archive_read_data_node *dataset;
145};
146struct 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 candidate;
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