2 * Copyright (c) 2003-2007 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
25 * $FreeBSD: head/lib/libarchive/archive_read_private.h 201088 2009-12-28 02:18:55Z kientzle $
28 #ifndef __LIBARCHIVE_BUILD
29 #error This header is only to be used internally to libarchive.
32 #ifndef ARCHIVE_READ_PRIVATE_H_INCLUDED
33 #define ARCHIVE_READ_PRIVATE_H_INCLUDED
36 #include "archive_string.h"
37 #include "archive_private.h"
40 struct archive_read_filter_bidder;
41 struct archive_read_filter;
44 * How bidding works for filters:
45 * * The bid manager initializes the client-provided reader as the
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
55 * This ends only when no bidder provides a non-zero bid. Then
56 * we perform a similar dance with the registered format handlers.
58 struct archive_read_filter_bidder {
59 /* Configuration data for the bidder. */
61 /* Taste the upstream filter to see if we handle this. */
62 int (*bid)(struct archive_read_filter_bidder *,
63 struct archive_read_filter *);
64 /* Initialize a newly-created filter. */
65 int (*init)(struct archive_read_filter *);
66 /* Set an option for the filter bidder. */
67 int (*options)(struct archive_read_filter_bidder *,
68 const char *key, const char *value);
69 /* Release the bidder's configuration data. */
70 int (*free)(struct archive_read_filter_bidder *);
74 * This structure is allocated within the archive_read core
75 * and initialized by archive_read and the init() method of the
76 * corresponding bidder above.
78 struct archive_read_filter {
80 /* Essentially all filters will need these values, so
81 * just declare them here. */
82 struct archive_read_filter_bidder *bidder; /* My bidder. */
83 struct archive_read_filter *upstream; /* Who I read from. */
84 struct archive_read *archive; /* Associated archive. */
85 /* Return next block. */
86 ssize_t (*read)(struct archive_read_filter *, const void **);
87 /* Skip forward this many bytes. */
88 int64_t (*skip)(struct archive_read_filter *self, int64_t request);
89 /* Seek to an absolute location. */
90 int64_t (*seek)(struct archive_read_filter *self, int64_t offset, int whence);
91 /* Close (just this filter) and free(self). */
92 int (*close)(struct archive_read_filter *self);
93 /* My private data. */
99 /* Used by reblocking logic. */
102 char *next; /* Current read location. */
103 size_t avail; /* Bytes in my buffer. */
104 const void *client_buff; /* Client buffer information. */
106 const char *client_next;
114 * The client looks a lot like a filter, so we just wrap it here.
116 * TODO: Make archive_read_filter and archive_read_client identical so
117 * that users of the library can easily register their own
118 * transformation filters. This will probably break the API/ABI and
119 * so should be deferred at least until libarchive 3.0.
121 struct archive_read_client {
122 archive_open_callback *opener;
123 archive_read_callback *reader;
124 archive_skip_callback *skipper;
125 archive_seek_callback *seeker;
126 archive_close_callback *closer;
130 struct archive_read {
131 struct archive archive;
133 struct archive_entry *entry;
135 /* Dev/ino of the archive being read/written. */
141 * Used by archive_read_data() to track blocks and copy
142 * data to client buffers, filling gaps with zero bytes.
144 const char *read_data_block;
145 int64_t read_data_offset;
146 int64_t read_data_output_offset;
147 size_t read_data_remaining;
149 /* Callbacks to open/read/write/close client archive stream. */
150 struct archive_read_client client;
152 /* Registered filter bidders. */
153 struct archive_read_filter_bidder bidders[9];
155 /* Last filter in chain */
156 struct archive_read_filter *filter;
158 /* File offset of beginning of most recently-read header. */
159 int64_t header_position;
162 * Format detection is mostly the same as compression
163 * detection, with one significant difference: The bidders
164 * use the read_ahead calls above to examine the stream rather
165 * than having the supervisor hand them a block of data to
169 struct archive_format_descriptor {
172 int (*bid)(struct archive_read *, int best_bid);
173 int (*options)(struct archive_read *, const char *key,
175 int (*read_header)(struct archive_read *, struct archive_entry *);
176 int (*read_data)(struct archive_read *, const void **, size_t *, int64_t *);
177 int (*read_data_skip)(struct archive_read *);
178 int (*cleanup)(struct archive_read *);
180 struct archive_format_descriptor *format; /* Active format. */
183 * Various information needed by archive_extract.
185 struct extract *extract;
186 int (*cleanup_archive_extract)(struct archive_read *);
189 int __archive_read_register_format(struct archive_read *a,
192 int (*bid)(struct archive_read *, int),
193 int (*options)(struct archive_read *, const char *, const char *),
194 int (*read_header)(struct archive_read *, struct archive_entry *),
195 int (*read_data)(struct archive_read *, const void **, size_t *, int64_t *),
196 int (*read_data_skip)(struct archive_read *),
197 int (*cleanup)(struct archive_read *));
199 int __archive_read_get_bidder(struct archive_read *a,
200 struct archive_read_filter_bidder **bidder);
202 const void *__archive_read_ahead(struct archive_read *, size_t, ssize_t *);
203 const void *__archive_read_filter_ahead(struct archive_read_filter *,
205 int64_t __archive_read_seek(struct archive_read*, int64_t, int);
206 int64_t __archive_read_filter_seek(struct archive_read_filter *, int64_t, int);
207 int64_t __archive_read_consume(struct archive_read *, int64_t);
208 int64_t __archive_read_filter_consume(struct archive_read_filter *, int64_t);
209 int __archive_read_program(struct archive_read_filter *, const char *);