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: src/lib/libarchive/archive_read_private.h,v 1.7 2008/12/06 06:45:15 kientzle Exp $
26  */
27 
28 #ifndef ARCHIVE_READ_PRIVATE_H_INCLUDED
29 #define	ARCHIVE_READ_PRIVATE_H_INCLUDED
30 
31 #include "archive.h"
32 #include "archive_string.h"
33 #include "archive_private.h"
34 
35 struct archive_read;
36 struct archive_read_filter_bidder;
37 struct archive_read_filter;
38 
39 /*
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.
48  */
49 struct archive_read_filter_bidder {
50 	/* Configuration data for the bidder. */
51 	void *data;
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 *);
62 };
63 
64 /*
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.
68  */
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. */
82 	void *data;
83 
84 	const char	*name;
85 	int		 code;
86 
87 	/* Used by reblocking logic. */
88 	char		*buffer;
89 	size_t		 buffer_size;
90 	char		*next;		/* Current read location. */
91 	size_t		 avail;		/* Bytes in my buffer. */
92 	const void	*client_buff;	/* Client buffer information. */
93 	size_t		 client_total;
94 	const char	*client_next;
95 	size_t		 client_avail;
96 	int64_t		 position;
97 	char		 end_of_file;
98 	char		 fatal;
99 };
100 
101 /*
102  * The client looks a lot like a filter, so we just wrap it here.
103  *
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.
108  */
109 struct archive_read_client {
110 	archive_read_callback	*reader;
111 	archive_skip_callback	*skipper;
112 	archive_close_callback	*closer;
113 };
114 
115 struct archive_read {
116 	struct archive	archive;
117 
118 	struct archive_entry	*entry;
119 
120 	/* Dev/ino of the archive being read/written. */
121 	dev_t		  skip_file_dev;
122 	ino_t		  skip_file_ino;
123 
124 	/*
125 	 * Used by archive_read_data() to track blocks and copy
126 	 * data to client buffers, filling gaps with zero bytes.
127 	 */
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;
132 
133 	/* Callbacks to open/read/write/close client archive stream. */
134 	struct archive_read_client client;
135 
136 	/* Registered filter bidders. */
137 	struct archive_read_filter_bidder bidders[8];
138 
139 	/* Last filter in chain */
140 	struct archive_read_filter *filter;
141 
142 	/* File offset of beginning of most recently-read header. */
143 	off_t		  header_position;
144 
145 	/*
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
150 	 * examine.
151 	 */
152 
153 	struct archive_format_descriptor {
154 		void	 *data;
155 		const char *name;
156 		int	(*bid)(struct archive_read *);
157 		int	(*options)(struct archive_read *, const char *key,
158 		    const char *value);
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 *);
163 	}	formats[8];
164 	struct archive_format_descriptor	*format; /* Active format. */
165 
166 	/*
167 	 * Various information needed by archive_extract.
168 	 */
169 	struct extract		 *extract;
170 	int			(*cleanup_archive_extract)(struct archive_read *);
171 };
172 
173 int	__archive_read_register_format(struct archive_read *a,
174 	    void *format_data,
175 	    const char *name,
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 *));
182 
183 struct archive_read_filter_bidder
184 	*__archive_read_get_bidder(struct archive_read *a);
185 
186 const void *__archive_read_ahead(struct archive_read *, size_t, ssize_t *);
187 const void *__archive_read_filter_ahead(struct archive_read_filter *,
188     size_t, ssize_t *);
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_filter_skip(struct archive_read_filter *, int64_t);
193 int __archive_read_program(struct archive_read_filter *, const char *);
194 #endif
195