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