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.
28 #ifndef ARCHIVE_READ_PRIVATE_H_INCLUDED
29 #define ARCHIVE_READ_PRIVATE_H_INCLUDED
32 #include "archive_string.h"
33 #include "archive_private.h"
36 struct archive_read_filter_bidder;
37 struct archive_read_filter;
40 * How bidding works for filters:
41 * * The bid manager reads the first block from the current source.
42 * * It shows that block to each registered bidder.
43 * * The bid manager creates a new filter structure for the winning
44 * bidder and gives the winning bidder a chance to initialize it.
45 * * The new filter becomes the top filter in the archive_read structure
46 * and we repeat the process.
47 * This ends only when no bidder provides a non-zero bid.
49 struct archive_read_filter_bidder {
50 /* Configuration data for the bidder. */
52 /* Taste the upstream filter to see if we handle this. */
53 int (*bid)(struct archive_read_filter_bidder *,
54 struct archive_read_filter *);
55 /* Initialize a newly-created filter. */
56 int (*init)(struct archive_read_filter *);
57 /* Set an option for the filter bidder. */
58 int (*options)(struct archive_read_filter_bidder *,
59 const char *key, const char *value);
60 /* Release the bidder's configuration data. */
61 int (*free)(struct archive_read_filter_bidder *);
65 * This structure is allocated within the archive_read core
66 * and initialized by archive_read and the init() method of the
67 * corresponding bidder above.
69 struct archive_read_filter {
70 /* Essentially all filters will need these values, so
71 * just declare them here. */
72 struct archive_read_filter_bidder *bidder; /* My bidder. */
73 struct archive_read_filter *upstream; /* Who I read from. */
74 struct archive_read *archive; /* Associated archive. */
75 /* Return next block. */
76 ssize_t (*read)(struct archive_read_filter *, const void **);
77 /* Skip forward this many bytes. */
78 int64_t (*skip)(struct archive_read_filter *self, int64_t request);
79 /* Close (just this filter) and free(self). */
80 int (*close)(struct archive_read_filter *self);
81 /* My private data. */
87 /* Used by reblocking logic. */
90 char *next; /* Current read location. */
91 size_t avail; /* Bytes in my buffer. */
92 const void *client_buff; /* Client buffer information. */
94 const char *client_next;
102 * The client looks a lot like a filter, so we just wrap it here.
104 * TODO: Make archive_read_filter and archive_read_client identical so
105 * that users of the library can easily register their own
106 * transformation filters. This will probably break the API/ABI and
107 * so should be deferred at least until libarchive 3.0.
109 struct archive_read_client {
110 archive_read_callback *reader;
111 archive_skip_callback *skipper;
112 archive_close_callback *closer;
115 struct archive_read {
116 struct archive archive;
118 struct archive_entry *entry;
120 /* Dev/ino of the archive being read/written. */
125 * Used by archive_read_data() to track blocks and copy
126 * data to client buffers, filling gaps with zero bytes.
128 const char *read_data_block;
129 off_t read_data_offset;
130 off_t read_data_output_offset;
131 size_t read_data_remaining;
133 /* Callbacks to open/read/write/close client archive stream. */
134 struct archive_read_client client;
136 /* Registered filter bidders. */
137 struct archive_read_filter_bidder bidders[8];
139 /* Last filter in chain */
140 struct archive_read_filter *filter;
142 /* File offset of beginning of most recently-read header. */
143 off_t header_position;
146 * Format detection is mostly the same as compression
147 * detection, with one significant difference: The bidders
148 * use the read_ahead calls above to examine the stream rather
149 * than having the supervisor hand them a block of data to
153 struct archive_format_descriptor {
156 int (*bid)(struct archive_read *);
157 int (*options)(struct archive_read *, const char *key,
159 int (*read_header)(struct archive_read *, struct archive_entry *);
160 int (*read_data)(struct archive_read *, const void **, size_t *, off_t *);
161 int (*read_data_skip)(struct archive_read *);
162 int (*cleanup)(struct archive_read *);
164 struct archive_format_descriptor *format; /* Active format. */
167 * Various information needed by archive_extract.
169 struct extract *extract;
170 int (*cleanup_archive_extract)(struct archive_read *);
173 int __archive_read_register_format(struct archive_read *a,
176 int (*bid)(struct archive_read *),
177 int (*options)(struct archive_read *, const char *, const char *),
178 int (*read_header)(struct archive_read *, struct archive_entry *),
179 int (*read_data)(struct archive_read *, const void **, size_t *, off_t *),
180 int (*read_data_skip)(struct archive_read *),
181 int (*cleanup)(struct archive_read *));
183 struct archive_read_filter_bidder
184 *__archive_read_get_bidder(struct archive_read *a);
186 const void *__archive_read_ahead(struct archive_read *, size_t, ssize_t *);
187 const void *__archive_read_filter_ahead(struct archive_read_filter *,
189 ssize_t __archive_read_consume(struct archive_read *, size_t);
190 ssize_t __archive_read_filter_consume(struct archive_read_filter *, size_t);
191 int64_t __archive_read_skip(struct archive_read *, int64_t);
192 int64_t __archive_read_skip_lenient(struct archive_read *, int64_t);
193 int64_t __archive_read_filter_skip(struct archive_read_filter *, int64_t);
194 int __archive_read_program(struct archive_read_filter *, const char *);