1228753Smm/*-
2228753Smm * Copyright (c) 2003-2007 Tim Kientzle
3228753Smm * All rights reserved.
4228753Smm *
5228753Smm * Redistribution and use in source and binary forms, with or without
6228753Smm * modification, are permitted provided that the following conditions
7228753Smm * are met:
8228753Smm * 1. Redistributions of source code must retain the above copyright
9228753Smm *    notice, this list of conditions and the following disclaimer.
10228753Smm * 2. Redistributions in binary form must reproduce the above copyright
11228753Smm *    notice, this list of conditions and the following disclaimer in the
12228753Smm *    documentation and/or other materials provided with the distribution.
13228753Smm *
14228753Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15228753Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16228753Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17228753Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18228753Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19228753Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20228753Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21228753Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22228753Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23228753Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24228753Smm *
25229592Smm * $FreeBSD$
26228753Smm */
27228753Smm
28228753Smm#ifndef __LIBARCHIVE_BUILD
29228753Smm#error This header is only to be used internally to libarchive.
30228753Smm#endif
31228753Smm
32228753Smm#ifndef ARCHIVE_READ_PRIVATE_H_INCLUDED
33228753Smm#define	ARCHIVE_READ_PRIVATE_H_INCLUDED
34228753Smm
35228753Smm#include "archive.h"
36228753Smm#include "archive_string.h"
37228753Smm#include "archive_private.h"
38228753Smm
39228753Smmstruct archive_read;
40228753Smmstruct archive_read_filter_bidder;
41228753Smmstruct archive_read_filter;
42228753Smm
43228753Smm/*
44228753Smm * How bidding works for filters:
45228753Smm *   * The bid manager reads the first block from the current source.
46228753Smm *   * It shows that block to each registered bidder.
47228753Smm *   * The bid manager creates a new filter structure for the winning
48228753Smm *     bidder and gives the winning bidder a chance to initialize it.
49228753Smm *   * The new filter becomes the top filter in the archive_read structure
50228753Smm *     and we repeat the process.
51228753Smm * This ends only when no bidder provides a non-zero bid.
52228753Smm */
53228753Smmstruct archive_read_filter_bidder {
54228753Smm	/* Configuration data for the bidder. */
55228753Smm	void *data;
56228753Smm	/* Taste the upstream filter to see if we handle this. */
57228753Smm	int (*bid)(struct archive_read_filter_bidder *,
58228753Smm	    struct archive_read_filter *);
59228753Smm	/* Initialize a newly-created filter. */
60228753Smm	int (*init)(struct archive_read_filter *);
61228753Smm	/* Set an option for the filter bidder. */
62228753Smm	int (*options)(struct archive_read_filter_bidder *,
63228753Smm	    const char *key, const char *value);
64228753Smm	/* Release the bidder's configuration data. */
65228753Smm	int (*free)(struct archive_read_filter_bidder *);
66228753Smm};
67228753Smm
68228753Smm/*
69228753Smm * This structure is allocated within the archive_read core
70228753Smm * and initialized by archive_read and the init() method of the
71228753Smm * corresponding bidder above.
72228753Smm */
73228753Smmstruct archive_read_filter {
74228753Smm	/* Essentially all filters will need these values, so
75228753Smm	 * just declare them here. */
76228753Smm	struct archive_read_filter_bidder *bidder; /* My bidder. */
77228753Smm	struct archive_read_filter *upstream; /* Who I read from. */
78228753Smm	struct archive_read *archive; /* Associated archive. */
79228753Smm	/* Return next block. */
80228753Smm	ssize_t (*read)(struct archive_read_filter *, const void **);
81228753Smm	/* Skip forward this many bytes. */
82228753Smm	int64_t (*skip)(struct archive_read_filter *self, int64_t request);
83228753Smm	/* Close (just this filter) and free(self). */
84228753Smm	int (*close)(struct archive_read_filter *self);
85228753Smm	/* My private data. */
86228753Smm	void *data;
87228753Smm
88228753Smm	const char	*name;
89228753Smm	int		 code;
90228753Smm
91228753Smm	/* Used by reblocking logic. */
92228753Smm	char		*buffer;
93228753Smm	size_t		 buffer_size;
94228753Smm	char		*next;		/* Current read location. */
95228753Smm	size_t		 avail;		/* Bytes in my buffer. */
96228753Smm	const void	*client_buff;	/* Client buffer information. */
97228753Smm	size_t		 client_total;
98228753Smm	const char	*client_next;
99228753Smm	size_t		 client_avail;
100228753Smm	int64_t		 position;
101228753Smm	char		 end_of_file;
102228753Smm	char		 fatal;
103228753Smm};
104228753Smm
105228753Smm/*
106228753Smm * The client looks a lot like a filter, so we just wrap it here.
107228753Smm *
108228753Smm * TODO: Make archive_read_filter and archive_read_client identical so
109228753Smm * that users of the library can easily register their own
110228753Smm * transformation filters.  This will probably break the API/ABI and
111228753Smm * so should be deferred at least until libarchive 3.0.
112228753Smm */
113228753Smmstruct archive_read_client {
114228753Smm	archive_read_callback	*reader;
115228753Smm	archive_skip_callback	*skipper;
116228753Smm	archive_close_callback	*closer;
117228753Smm};
118228753Smm
119228753Smmstruct archive_read {
120228753Smm	struct archive	archive;
121228753Smm
122228753Smm	struct archive_entry	*entry;
123228753Smm
124228753Smm	/* Dev/ino of the archive being read/written. */
125228753Smm	dev_t		  skip_file_dev;
126228753Smm	ino_t		  skip_file_ino;
127228753Smm
128228753Smm	/*
129228753Smm	 * Used by archive_read_data() to track blocks and copy
130228753Smm	 * data to client buffers, filling gaps with zero bytes.
131228753Smm	 */
132228753Smm	const char	 *read_data_block;
133228753Smm	off_t		  read_data_offset;
134228753Smm	off_t		  read_data_output_offset;
135228753Smm	size_t		  read_data_remaining;
136228753Smm
137228753Smm	/* Callbacks to open/read/write/close client archive stream. */
138228753Smm	struct archive_read_client client;
139228753Smm
140228753Smm	/* Registered filter bidders. */
141228753Smm	struct archive_read_filter_bidder bidders[8];
142228753Smm
143228753Smm	/* Last filter in chain */
144228753Smm	struct archive_read_filter *filter;
145228753Smm
146228753Smm	/* File offset of beginning of most recently-read header. */
147228753Smm	off_t		  header_position;
148228753Smm
149228753Smm	/*
150228753Smm	 * Format detection is mostly the same as compression
151228753Smm	 * detection, with one significant difference: The bidders
152228753Smm	 * use the read_ahead calls above to examine the stream rather
153228753Smm	 * than having the supervisor hand them a block of data to
154228753Smm	 * examine.
155228753Smm	 */
156228753Smm
157228753Smm	struct archive_format_descriptor {
158228753Smm		void	 *data;
159228753Smm		const char *name;
160228753Smm		int	(*bid)(struct archive_read *);
161228753Smm		int	(*options)(struct archive_read *, const char *key,
162228753Smm		    const char *value);
163228753Smm		int	(*read_header)(struct archive_read *, struct archive_entry *);
164228753Smm		int	(*read_data)(struct archive_read *, const void **, size_t *, off_t *);
165228753Smm		int	(*read_data_skip)(struct archive_read *);
166228753Smm		int	(*cleanup)(struct archive_read *);
167228753Smm	}	formats[9];
168228753Smm	struct archive_format_descriptor	*format; /* Active format. */
169228753Smm
170228753Smm	/*
171228753Smm	 * Various information needed by archive_extract.
172228753Smm	 */
173228753Smm	struct extract		 *extract;
174228753Smm	int			(*cleanup_archive_extract)(struct archive_read *);
175228753Smm};
176228753Smm
177228753Smmint	__archive_read_register_format(struct archive_read *a,
178228753Smm	    void *format_data,
179228753Smm	    const char *name,
180228753Smm	    int (*bid)(struct archive_read *),
181228753Smm	    int (*options)(struct archive_read *, const char *, const char *),
182228753Smm	    int (*read_header)(struct archive_read *, struct archive_entry *),
183228753Smm	    int (*read_data)(struct archive_read *, const void **, size_t *, off_t *),
184228753Smm	    int (*read_data_skip)(struct archive_read *),
185228753Smm	    int (*cleanup)(struct archive_read *));
186228753Smm
187228753Smmstruct archive_read_filter_bidder
188228753Smm	*__archive_read_get_bidder(struct archive_read *a);
189228753Smm
190228753Smmconst void *__archive_read_ahead(struct archive_read *, size_t, ssize_t *);
191228753Smmconst void *__archive_read_filter_ahead(struct archive_read_filter *,
192228753Smm    size_t, ssize_t *);
193228753Smmssize_t	__archive_read_consume(struct archive_read *, size_t);
194228753Smmssize_t	__archive_read_filter_consume(struct archive_read_filter *, size_t);
195228753Smmint64_t	__archive_read_skip(struct archive_read *, int64_t);
196228753Smmint64_t	__archive_read_skip_lenient(struct archive_read *, int64_t);
197228753Smmint64_t	__archive_read_filter_skip(struct archive_read_filter *, int64_t);
198228753Smmint __archive_read_program(struct archive_read_filter *, const char *);
199228753Smm#endif
200