archive_read_private.h revision 348607
1/*-
2 * Copyright (c) 2003-2007 Tim Kientzle
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *
25 * $FreeBSD: stable/11/contrib/libarchive/libarchive/archive_read_private.h 348607 2019-06-04 10:35:54Z mm $
26 */
27
28#ifndef __LIBARCHIVE_BUILD
29#ifndef __LIBARCHIVE_TEST
30#error This header is only to be used internally to libarchive.
31#endif
32#endif
33
34#ifndef ARCHIVE_READ_PRIVATE_H_INCLUDED
35#define	ARCHIVE_READ_PRIVATE_H_INCLUDED
36
37#include "archive.h"
38#include "archive_string.h"
39#include "archive_private.h"
40
41struct archive_read;
42struct archive_read_filter_bidder;
43struct archive_read_filter;
44
45/*
46 * How bidding works for filters:
47 *   * The bid manager initializes the client-provided reader as the
48 *     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	/* Read any header metadata if available. */
102	int (*read_header)(struct archive_read_filter *self, struct archive_entry *entry);
103	/* My private data. */
104	void *data;
105
106	const char	*name;
107	int		 code;
108
109	/* Used by reblocking logic. */
110	char		*buffer;
111	size_t		 buffer_size;
112	char		*next;		/* Current read location. */
113	size_t		 avail;		/* Bytes in my buffer. */
114	const void	*client_buff;	/* Client buffer information. */
115	size_t		 client_total;
116	const char	*client_next;
117	size_t		 client_avail;
118	char		 end_of_file;
119	char		 closed;
120	char		 fatal;
121};
122
123/*
124 * The client looks a lot like a filter, so we just wrap it here.
125 *
126 * TODO: Make archive_read_filter and archive_read_client identical so
127 * that users of the library can easily register their own
128 * transformation filters.  This will probably break the API/ABI and
129 * so should be deferred at least until libarchive 3.0.
130 */
131struct archive_read_data_node {
132	int64_t begin_position;
133	int64_t total_size;
134	void *data;
135};
136struct archive_read_client {
137	archive_open_callback	*opener;
138	archive_read_callback	*reader;
139	archive_skip_callback	*skipper;
140	archive_seek_callback	*seeker;
141	archive_close_callback	*closer;
142	archive_switch_callback *switcher;
143	unsigned int nodes;
144	unsigned int cursor;
145	int64_t position;
146	struct archive_read_data_node *dataset;
147};
148struct archive_read_passphrase {
149	char	*passphrase;
150	struct archive_read_passphrase *next;
151};
152
153struct archive_read_extract {
154	struct archive *ad; /* archive_write_disk object */
155
156	/* Progress function invoked during extract. */
157	void			(*extract_progress)(void *);
158	void			 *extract_progress_user_data;
159};
160
161struct archive_read {
162	struct archive	archive;
163
164	struct archive_entry	*entry;
165
166	/* Dev/ino of the archive being read/written. */
167	int		  skip_file_set;
168	int64_t		  skip_file_dev;
169	int64_t		  skip_file_ino;
170
171	/* Callbacks to open/read/write/close client archive streams. */
172	struct archive_read_client client;
173
174	/* Registered filter bidders. */
175	struct archive_read_filter_bidder bidders[16];
176
177	/* Last filter in chain */
178	struct archive_read_filter *filter;
179
180	/* Whether to bypass filter bidding process */
181	int bypass_filter_bidding;
182
183	/* File offset of beginning of most recently-read header. */
184	int64_t		  header_position;
185
186	/* Nodes and offsets of compressed data block */
187	unsigned int data_start_node;
188	unsigned int data_end_node;
189
190	/*
191	 * Format detection is mostly the same as compression
192	 * detection, with one significant difference: The bidders
193	 * use the read_ahead calls above to examine the stream rather
194	 * than having the supervisor hand them a block of data to
195	 * examine.
196	 */
197
198	struct archive_format_descriptor {
199		void	 *data;
200		const char *name;
201		int	(*bid)(struct archive_read *, int best_bid);
202		int	(*options)(struct archive_read *, const char *key,
203		    const char *value);
204		int	(*read_header)(struct archive_read *, struct archive_entry *);
205		int	(*read_data)(struct archive_read *, const void **, size_t *, int64_t *);
206		int	(*read_data_skip)(struct archive_read *);
207		int64_t	(*seek_data)(struct archive_read *, int64_t, int);
208		int	(*cleanup)(struct archive_read *);
209		int	(*format_capabilties)(struct archive_read *);
210		int	(*has_encrypted_entries)(struct archive_read *);
211	}	formats[16];
212	struct archive_format_descriptor	*format; /* Active format. */
213
214	/*
215	 * Various information needed by archive_extract.
216	 */
217	struct archive_read_extract		*extract;
218	int			(*cleanup_archive_extract)(struct archive_read *);
219
220	/*
221	 * Decryption passphrase.
222	 */
223	struct {
224		struct archive_read_passphrase *first;
225		struct archive_read_passphrase **last;
226		int candidate;
227		archive_passphrase_callback *callback;
228		void *client_data;
229	}		passphrases;
230};
231
232int	__archive_read_register_format(struct archive_read *a,
233		void *format_data,
234		const char *name,
235		int (*bid)(struct archive_read *, int),
236		int (*options)(struct archive_read *, const char *, const char *),
237		int (*read_header)(struct archive_read *, struct archive_entry *),
238		int (*read_data)(struct archive_read *, const void **, size_t *, int64_t *),
239		int (*read_data_skip)(struct archive_read *),
240		int64_t (*seek_data)(struct archive_read *, int64_t, int),
241		int (*cleanup)(struct archive_read *),
242		int (*format_capabilities)(struct archive_read *),
243		int (*has_encrypted_entries)(struct archive_read *));
244
245int __archive_read_get_bidder(struct archive_read *a,
246    struct archive_read_filter_bidder **bidder);
247
248const void *__archive_read_ahead(struct archive_read *, size_t, ssize_t *);
249const void *__archive_read_filter_ahead(struct archive_read_filter *,
250    size_t, ssize_t *);
251int64_t	__archive_read_seek(struct archive_read*, int64_t, int);
252int64_t	__archive_read_filter_seek(struct archive_read_filter *, int64_t, int);
253int64_t	__archive_read_consume(struct archive_read *, int64_t);
254int64_t	__archive_read_filter_consume(struct archive_read_filter *, int64_t);
255int __archive_read_header(struct archive_read *, struct archive_entry *);
256int __archive_read_program(struct archive_read_filter *, const char *);
257void __archive_read_free_filters(struct archive_read *);
258struct archive_read_extract *__archive_read_get_extract(struct archive_read *);
259
260
261/*
262 * Get a decryption passphrase.
263 */
264void __archive_read_reset_passphrase(struct archive_read *a);
265const char * __archive_read_next_passphrase(struct archive_read *a);
266#endif
267