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 
26 #ifndef ARCHIVE_READ_PRIVATE_H_INCLUDED
27 #define ARCHIVE_READ_PRIVATE_H_INCLUDED
28 
29 #ifndef __LIBARCHIVE_BUILD
30 #ifndef __LIBARCHIVE_TEST
31 #error This header is only to be used internally to libarchive.
32 #endif
33 #endif
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 struct archive_read_filter_bidder_vtable {
44           /* Taste the upstream filter to see if we handle this. */
45           int (*bid)(struct archive_read_filter_bidder *,
46               struct archive_read_filter *);
47           /* Initialize a newly-created filter. */
48           int (*init)(struct archive_read_filter *);
49           /* Release the bidder's configuration data. */
50           void (*free)(struct archive_read_filter_bidder *);
51 };
52 
53 /*
54  * How bidding works for filters:
55  *   * The bid manager initializes the client-provided reader as the
56  *     first filter.
57  *   * It invokes the bidder for each registered filter with the
58  *     current head filter.
59  *   * The bidders can use archive_read_filter_ahead() to peek ahead
60  *     at the incoming data to compose their bids.
61  *   * The bid manager creates a new filter structure for the winning
62  *     bidder and gives the winning bidder a chance to initialize it.
63  *   * The new filter becomes the new top filter and we repeat the
64  *     process.
65  * This ends only when no bidder provides a non-zero bid.  Then
66  * we perform a similar dance with the registered format handlers.
67  */
68 struct archive_read_filter_bidder {
69           /* Configuration data for the bidder. */
70           void *data;
71           /* Name of the filter */
72           const char *name;
73           const struct archive_read_filter_bidder_vtable *vtable;
74 };
75 
76 struct archive_read_filter_vtable {
77           /* Return next block. */
78           ssize_t (*read)(struct archive_read_filter *, const void **);
79           /* Close (just this filter) and free(self). */
80           int (*close)(struct archive_read_filter *self);
81           /* Read any header metadata if available. */
82           int (*read_header)(struct archive_read_filter *self, struct archive_entry *entry);
83 };
84 
85 /*
86  * This structure is allocated within the archive_read core
87  * and initialized by archive_read and the init() method of the
88  * corresponding bidder above.
89  */
90 struct archive_read_filter {
91           int64_t position;
92           /* Essentially all filters will need these values, so
93            * just declare them here. */
94           struct archive_read_filter_bidder *bidder; /* My bidder. */
95           struct archive_read_filter *upstream; /* Who I read from. */
96           struct archive_read *archive; /* Associated archive. */
97           const struct archive_read_filter_vtable *vtable;
98           /* My private data. */
99           void *data;
100 
101           const char          *name;
102           int                  code;
103           int                  can_skip;
104           int                  can_seek;
105 
106           /* Used by reblocking logic. */
107           char                *buffer;
108           size_t               buffer_size;
109           char                *next;              /* Current read location. */
110           size_t               avail;             /* Bytes in my buffer. */
111           const void          *client_buff;       /* Client buffer information. */
112           size_t               client_total;
113           const char          *client_next;
114           size_t               client_avail;
115           char                 end_of_file;
116           char                 closed;
117           char                 fatal;
118 };
119 
120 /*
121  * The client looks a lot like a filter, so we just wrap it here.
122  *
123  * TODO: Make archive_read_filter and archive_read_client identical so
124  * that users of the library can easily register their own
125  * transformation filters.  This will probably break the API/ABI and
126  * so should be deferred at least until libarchive 3.0.
127  */
128 struct archive_read_data_node {
129           int64_t begin_position;
130           int64_t total_size;
131           void *data;
132 };
133 struct archive_read_client {
134           archive_open_callback         *opener;
135           archive_read_callback         *reader;
136           archive_skip_callback         *skipper;
137           archive_seek_callback         *seeker;
138           archive_close_callback        *closer;
139           archive_switch_callback *switcher;
140           unsigned int nodes;
141           unsigned int cursor;
142           int64_t position;
143           struct archive_read_data_node *dataset;
144 };
145 struct archive_read_passphrase {
146           char      *passphrase;
147           struct archive_read_passphrase *next;
148 };
149 
150 struct archive_read_extract {
151           struct archive *ad; /* archive_write_disk object */
152 
153           /* Progress function invoked during extract. */
154           void                          (*extract_progress)(void *);
155           void                           *extract_progress_user_data;
156 };
157 
158 struct archive_read {
159           struct archive      archive;
160 
161           struct archive_entry          *entry;
162 
163           /* Dev/ino of the archive being read/written. */
164           int                   skip_file_set;
165           int64_t               skip_file_dev;
166           int64_t               skip_file_ino;
167 
168           /* Callbacks to open/read/write/close client archive streams. */
169           struct archive_read_client client;
170 
171           /* Registered filter bidders. */
172           struct archive_read_filter_bidder bidders[16];
173 
174           /* Last filter in chain */
175           struct archive_read_filter *filter;
176 
177           /* Whether to bypass filter bidding process */
178           int bypass_filter_bidding;
179 
180           /* File offset of beginning of most recently-read header. */
181           int64_t               header_position;
182 
183           /* Nodes and offsets of compressed data block */
184           unsigned int data_start_node;
185           unsigned int data_end_node;
186 
187           /*
188            * Format detection is mostly the same as compression
189            * detection, with one significant difference: The bidders
190            * use the read_ahead calls above to examine the stream rather
191            * than having the supervisor hand them a block of data to
192            * examine.
193            */
194 
195           struct archive_format_descriptor {
196                     void       *data;
197                     const char *name;
198                     int       (*bid)(struct archive_read *, int best_bid);
199                     int       (*options)(struct archive_read *, const char *key,
200                         const char *value);
201                     int       (*read_header)(struct archive_read *, struct archive_entry *);
202                     int       (*read_data)(struct archive_read *, const void **, size_t *, int64_t *);
203                     int       (*read_data_skip)(struct archive_read *);
204                     int64_t   (*seek_data)(struct archive_read *, int64_t, int);
205                     int       (*cleanup)(struct archive_read *);
206                     int       (*format_capabilties)(struct archive_read *);
207                     int       (*has_encrypted_entries)(struct archive_read *);
208           }         formats[16];
209           struct archive_format_descriptor        *format; /* Active format. */
210 
211           /*
212            * Various information needed by archive_extract.
213            */
214           struct archive_read_extract             *extract;
215           int                           (*cleanup_archive_extract)(struct archive_read *);
216 
217           /*
218            * Decryption passphrase.
219            */
220           struct {
221                     struct archive_read_passphrase *first;
222                     struct archive_read_passphrase **last;
223                     int candidate;
224                     archive_passphrase_callback *callback;
225                     void *client_data;
226           }                   passphrases;
227 };
228 
229 int       __archive_read_register_format(struct archive_read *a,
230                     void *format_data,
231                     const char *name,
232                     int (*bid)(struct archive_read *, int),
233                     int (*options)(struct archive_read *, const char *, const char *),
234                     int (*read_header)(struct archive_read *, struct archive_entry *),
235                     int (*read_data)(struct archive_read *, const void **, size_t *, int64_t *),
236                     int (*read_data_skip)(struct archive_read *),
237                     int64_t (*seek_data)(struct archive_read *, int64_t, int),
238                     int (*cleanup)(struct archive_read *),
239                     int (*format_capabilities)(struct archive_read *),
240                     int (*has_encrypted_entries)(struct archive_read *));
241 
242 int __archive_read_register_bidder(struct archive_read *a,
243                     void *bidder_data,
244                     const char *name,
245                     const struct archive_read_filter_bidder_vtable *vtable);
246 
247 const void *__archive_read_ahead(struct archive_read *, size_t, ssize_t *);
248 const void *__archive_read_filter_ahead(struct archive_read_filter *,
249     size_t, ssize_t *);
250 int64_t   __archive_read_seek(struct archive_read*, int64_t, int);
251 int64_t   __archive_read_filter_seek(struct archive_read_filter *, int64_t, int);
252 int64_t   __archive_read_consume(struct archive_read *, int64_t);
253 int64_t   __archive_read_filter_consume(struct archive_read_filter *, int64_t);
254 int __archive_read_header(struct archive_read *, struct archive_entry *);
255 int __archive_read_program(struct archive_read_filter *, const char *);
256 void __archive_read_free_filters(struct archive_read *);
257 struct archive_read_extract *__archive_read_get_extract(struct archive_read *);
258 
259 
260 /*
261  * Get a decryption passphrase.
262  */
263 void __archive_read_reset_passphrase(struct archive_read *a);
264 const char * __archive_read_next_passphrase(struct archive_read *a);
265 #endif
266