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: head/lib/libarchive/archive_read_private.h 201088 2009-12-28 02:18:55Z kientzle $
26  */
27 
28 #ifndef __LIBARCHIVE_BUILD
29 #error This header is only to be used internally to libarchive.
30 #endif
31 
32 #ifndef ARCHIVE_READ_PRIVATE_H_INCLUDED
33 #define	ARCHIVE_READ_PRIVATE_H_INCLUDED
34 
35 #include "archive.h"
36 #include "archive_string.h"
37 #include "archive_private.h"
38 
39 struct archive_read;
40 struct archive_read_filter_bidder;
41 struct archive_read_filter;
42 
43 /*
44  * How bidding works for filters:
45  *   * The bid manager initializes the client-provided reader as the
46  *     first filter.
47  *   * It invokes the bidder for each registered filter with the
48  *     current head filter.
49  *   * The bidders can use archive_read_filter_ahead() to peek ahead
50  *     at the incoming data to compose their bids.
51  *   * The bid manager creates a new filter structure for the winning
52  *     bidder and gives the winning bidder a chance to initialize it.
53  *   * The new filter becomes the new top filter and we repeat the
54  *     process.
55  * This ends only when no bidder provides a non-zero bid.  Then
56  * we perform a similar dance with the registered format handlers.
57  */
58 struct archive_read_filter_bidder {
59 	/* Configuration data for the bidder. */
60 	void *data;
61 	/* Name of the filter */
62 	const char *name;
63 	/* Taste the upstream filter to see if we handle this. */
64 	int (*bid)(struct archive_read_filter_bidder *,
65 	    struct archive_read_filter *);
66 	/* Initialize a newly-created filter. */
67 	int (*init)(struct archive_read_filter *);
68 	/* Set an option for the filter bidder. */
69 	int (*options)(struct archive_read_filter_bidder *,
70 	    const char *key, const char *value);
71 	/* Release the bidder's configuration data. */
72 	int (*free)(struct archive_read_filter_bidder *);
73 };
74 
75 /*
76  * This structure is allocated within the archive_read core
77  * and initialized by archive_read and the init() method of the
78  * corresponding bidder above.
79  */
80 struct archive_read_filter {
81 	int64_t position;
82 	/* Essentially all filters will need these values, so
83 	 * just declare them here. */
84 	struct archive_read_filter_bidder *bidder; /* My bidder. */
85 	struct archive_read_filter *upstream; /* Who I read from. */
86 	struct archive_read *archive; /* Associated archive. */
87 	/* Open a block for reading */
88 	int (*open)(struct archive_read_filter *self);
89 	/* Return next block. */
90 	ssize_t (*read)(struct archive_read_filter *, const void **);
91 	/* Skip forward this many bytes. */
92 	int64_t (*skip)(struct archive_read_filter *self, int64_t request);
93 	/* Seek to an absolute location. */
94 	int64_t (*seek)(struct archive_read_filter *self, int64_t offset, int whence);
95 	/* Close (just this filter) and free(self). */
96 	int (*close)(struct archive_read_filter *self);
97 	/* Function that handles switching from reading one block to the next/prev */
98 	int (*sswitch)(struct archive_read_filter *self, unsigned int iindex);
99 	/* My private data. */
100 	void *data;
101 
102 	const char	*name;
103 	int		 code;
104 
105 	/* Used by reblocking logic. */
106 	char		*buffer;
107 	size_t		 buffer_size;
108 	char		*next;		/* Current read location. */
109 	size_t		 avail;		/* Bytes in my buffer. */
110 	const void	*client_buff;	/* Client buffer information. */
111 	size_t		 client_total;
112 	const char	*client_next;
113 	size_t		 client_avail;
114 	char		 end_of_file;
115 	char		 closed;
116 	char		 fatal;
117 };
118 
119 /*
120  * The client looks a lot like a filter, so we just wrap it here.
121  *
122  * TODO: Make archive_read_filter and archive_read_client identical so
123  * that users of the library can easily register their own
124  * transformation filters.  This will probably break the API/ABI and
125  * so should be deferred at least until libarchive 3.0.
126  */
127 struct archive_read_data_node {
128 	int64_t begin_position;
129 	int64_t total_size;
130 	void *data;
131 };
132 struct archive_read_client {
133 	archive_open_callback	*opener;
134 	archive_read_callback	*reader;
135 	archive_skip_callback	*skipper;
136 	archive_seek_callback	*seeker;
137 	archive_close_callback	*closer;
138 	archive_switch_callback *switcher;
139 	unsigned int nodes;
140 	unsigned int cursor;
141 	int64_t position;
142 	struct archive_read_data_node *dataset;
143 };
144 
145 struct archive_read {
146 	struct archive	archive;
147 
148 	struct archive_entry	*entry;
149 
150 	/* Dev/ino of the archive being read/written. */
151 	int		  skip_file_set;
152 	int64_t		  skip_file_dev;
153 	int64_t		  skip_file_ino;
154 
155 	/*
156 	 * Used by archive_read_data() to track blocks and copy
157 	 * data to client buffers, filling gaps with zero bytes.
158 	 */
159 	const char	 *read_data_block;
160 	int64_t		  read_data_offset;
161 	int64_t		  read_data_output_offset;
162 	size_t		  read_data_remaining;
163 
164 	/*
165 	 * Used by formats/filters to determine the amount of data
166 	 * requested from a call to archive_read_data(). This is only
167 	 * useful when the format/filter has seek support.
168 	 */
169 	char		  read_data_is_posix_read;
170 	size_t		  read_data_requested;
171 
172 	/* Callbacks to open/read/write/close client archive streams. */
173 	struct archive_read_client client;
174 
175 	/* Registered filter bidders. */
176 	struct archive_read_filter_bidder bidders[14];
177 
178 	/* Last filter in chain */
179 	struct archive_read_filter *filter;
180 
181 	/* Whether to bypass filter bidding process */
182 	int bypass_filter_bidding;
183 
184 	/* File offset of beginning of most recently-read header. */
185 	int64_t		  header_position;
186 
187 	/* Nodes and offsets of compressed data block */
188 	unsigned int data_start_node;
189 	unsigned int data_end_node;
190 
191 	/*
192 	 * Format detection is mostly the same as compression
193 	 * detection, with one significant difference: The bidders
194 	 * use the read_ahead calls above to examine the stream rather
195 	 * than having the supervisor hand them a block of data to
196 	 * examine.
197 	 */
198 
199 	struct archive_format_descriptor {
200 		void	 *data;
201 		const char *name;
202 		int	(*bid)(struct archive_read *, int best_bid);
203 		int	(*options)(struct archive_read *, const char *key,
204 		    const char *value);
205 		int	(*read_header)(struct archive_read *, struct archive_entry *);
206 		int	(*read_data)(struct archive_read *, const void **, size_t *, int64_t *);
207 		int	(*read_data_skip)(struct archive_read *);
208 		int64_t	(*seek_data)(struct archive_read *, int64_t, int);
209 		int	(*cleanup)(struct archive_read *);
210 	}	formats[16];
211 	struct archive_format_descriptor	*format; /* Active format. */
212 
213 	/*
214 	 * Various information needed by archive_extract.
215 	 */
216 	struct extract		 *extract;
217 	int			(*cleanup_archive_extract)(struct archive_read *);
218 };
219 
220 int	__archive_read_register_format(struct archive_read *a,
221 	    void *format_data,
222 	    const char *name,
223 	    int (*bid)(struct archive_read *, int),
224 	    int (*options)(struct archive_read *, const char *, const char *),
225 	    int (*read_header)(struct archive_read *, struct archive_entry *),
226 	    int (*read_data)(struct archive_read *, const void **, size_t *, int64_t *),
227 	    int (*read_data_skip)(struct archive_read *),
228 	    int64_t (*seek_data)(struct archive_read *, int64_t, int),
229 	    int (*cleanup)(struct archive_read *));
230 
231 int __archive_read_get_bidder(struct archive_read *a,
232     struct archive_read_filter_bidder **bidder);
233 
234 const void *__archive_read_ahead(struct archive_read *, size_t, ssize_t *);
235 const void *__archive_read_filter_ahead(struct archive_read_filter *,
236     size_t, ssize_t *);
237 int64_t	__archive_read_seek(struct archive_read*, int64_t, int);
238 int64_t	__archive_read_filter_seek(struct archive_read_filter *, int64_t, int);
239 int64_t	__archive_read_consume(struct archive_read *, int64_t);
240 int64_t	__archive_read_filter_consume(struct archive_read_filter *, int64_t);
241 int __archive_read_program(struct archive_read_filter *, const char *);
242 void __archive_read_free_filters(struct archive_read *);
243 int  __archive_read_close_filters(struct archive_read *);
244 #endif
245