Import libarchive 2.0.28.
[dragonfly.git] / contrib / libarchive-2.0 / libarchive / archive_read_private.h
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.2 2007/04/02 00:11:54 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  archive;
37
38         struct archive_entry    *entry;
39
40         /* Dev/ino of the archive being read/written. */
41         dev_t             skip_file_dev;
42         ino_t             skip_file_ino;
43
44         /* Utility:  Pointer to a block of nulls. */
45         const unsigned char     *nulls;
46         size_t                   null_length;
47
48         /*
49          * Used by archive_read_data() to track blocks and copy
50          * data to client buffers, filling gaps with zero bytes.
51          */
52         const char       *read_data_block;
53         off_t             read_data_offset;
54         off_t             read_data_output_offset;
55         size_t            read_data_remaining;
56
57         /* Callbacks to open/read/write/close archive stream. */
58         archive_open_callback   *client_opener;
59         archive_read_callback   *client_reader;
60         archive_skip_callback   *client_skipper;
61         archive_write_callback  *client_writer;
62         archive_close_callback  *client_closer;
63         void                    *client_data;
64
65         /*
66          * Blocking information.  Note that bytes_in_last_block is
67          * misleadingly named; I should find a better name.  These
68          * control the final output from all compressors, including
69          * compression_none.
70          */
71         int               bytes_per_block;
72         int               bytes_in_last_block;
73
74         /*
75          * These control whether data within a gzip/bzip2 compressed
76          * stream gets padded or not.  If pad_uncompressed is set,
77          * the data will be padded to a full block before being
78          * compressed.  The pad_uncompressed_byte determines the value
79          * that will be used for padding.  Note that these have no
80          * effect on compression "none."
81          */
82         int               pad_uncompressed;
83         int               pad_uncompressed_byte; /* TODO: Support this. */
84
85         /* File offset of beginning of most recently-read header. */
86         off_t             header_position;
87
88         /*
89          * Detection functions for decompression: bid functions are
90          * given a block of data from the beginning of the stream and
91          * can bid on whether or not they support the data stream.
92          * General guideline: bid the number of bits that you actually
93          * test, e.g., 16 if you test a 2-byte magic value.  The
94          * highest bidder will have their init function invoked, which
95          * can set up pointers to specific handlers.
96          */
97         struct {
98                 int     (*bid)(const void *buff, size_t);
99                 int     (*init)(struct archive_read *, const void *buff, size_t);
100         }       decompressors[4];
101         /* Read/write data stream (with compression). */
102         void     *compression_data;             /* Data for (de)compressor. */
103         int     (*compression_finish)(struct archive_read *);
104
105         /*
106          * Read uses a peek/consume I/O model: the decompression code
107          * returns a pointer to the requested block and advances the
108          * file position only when requested by a consume call.  This
109          * reduces copying and also simplifies look-ahead for format
110          * detection.
111          */
112         ssize_t (*compression_read_ahead)(struct archive_read *,
113                     const void **, size_t request);
114         ssize_t (*compression_read_consume)(struct archive_read *, size_t);
115         off_t (*compression_skip)(struct archive_read *, off_t);
116
117         /*
118          * Format detection is mostly the same as compression
119          * detection, with two significant differences: The bidders
120          * use the read_ahead calls above to examine the stream rather
121          * than having the supervisor hand them a block of data to
122          * examine, and the auction is repeated for every header.
123          * Winning bidders should set the archive_format and
124          * archive_format_name appropriately.  Bid routines should
125          * check archive_format and decline to bid if the format of
126          * the last header was incompatible.
127          *
128          * Again, write support is considerably simpler because there's
129          * no need for an auction.
130          */
131
132         struct archive_format_descriptor {
133                 int     (*bid)(struct archive_read *);
134                 int     (*read_header)(struct archive_read *, struct archive_entry *);
135                 int     (*read_data)(struct archive_read *, const void **, size_t *, off_t *);
136                 int     (*read_data_skip)(struct archive_read *);
137                 int     (*cleanup)(struct archive_read *);
138                 void     *format_data;  /* Format-specific data for readers. */
139         }       formats[8];
140         struct archive_format_descriptor        *format; /* Active format. */
141
142         /*
143          * Storage for format-specific data.  Note that there can be
144          * multiple format readers active at one time, so we need to
145          * allow for multiple format readers to have their data
146          * available.  The pformat_data slot here is the solution: on
147          * read, it is guaranteed to always point to a void* variable
148          * that the format can use.
149          */
150         void    **pformat_data;         /* Pointer to current format_data. */
151         void     *format_data;          /* Used by writers. */
152
153         /*
154          * Pointers to format-specific functions for writing.  They're
155          * initialized by archive_write_set_format_XXX() calls.
156          */
157         int     (*format_init)(struct archive *); /* Only used on write. */
158         int     (*format_finish)(struct archive *);
159         int     (*format_finish_entry)(struct archive *);
160         int     (*format_write_header)(struct archive *,
161                     struct archive_entry *);
162         ssize_t (*format_write_data)(struct archive *,
163                     const void *buff, size_t);
164
165         /*
166          * Various information needed by archive_extract.
167          */
168         struct extract           *extract;
169         int                     (*cleanup_archive_extract)(struct archive_read *);
170 };
171
172 int     __archive_read_register_format(struct archive_read *a,
173             void *format_data,
174             int (*bid)(struct archive_read *),
175             int (*read_header)(struct archive_read *, struct archive_entry *),
176             int (*read_data)(struct archive_read *, const void **, size_t *, off_t *),
177             int (*read_data_skip)(struct archive_read *),
178             int (*cleanup)(struct archive_read *));
179
180 int     __archive_read_register_compression(struct archive_read *a,
181             int (*bid)(const void *, size_t),
182             int (*init)(struct archive_read *, const void *, size_t));
183
184 #endif