- Add ral(4) for Ralink RT2500/RT2501/RT2600 chip based wireless NIC
[dragonfly.git] / contrib / libarchive / archive_private.h
1 /*-
2  * Copyright (c) 2003-2004 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  *    in this position and unchanged.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * $FreeBSD: src/lib/libarchive/archive_private.h,v 1.17 2005/04/06 04:19:30 kientzle Exp $
27  */
28
29 #ifndef ARCHIVE_PRIVATE_H_INCLUDED
30 #define ARCHIVE_PRIVATE_H_INCLUDED
31
32 #include <wchar.h>
33
34 #include "archive.h"
35 #include "archive_string.h"
36
37 #define ARCHIVE_WRITE_MAGIC     (0xb0c5c0deU)
38 #define ARCHIVE_READ_MAGIC      (0xdeb0c5U)
39
40 struct archive {
41         /*
42          * The magic/state values are used to sanity-check the
43          * client's usage.  If an API function is called at a
44          * rediculous time, or the client passes us an invalid
45          * pointer, these values allow me to catch that.
46          */
47         unsigned          magic;
48         unsigned          state;
49
50         struct archive_entry    *entry;
51         uid_t             user_uid;     /* UID of current user. */
52
53         /* Dev/ino of the archive being read/written. */
54         dev_t             skip_file_dev;
55         ino_t             skip_file_ino;
56
57         /* Utility:  Pointer to a block of nulls. */
58         const char              *nulls;
59         size_t                   null_length;
60
61         /*
62          * Used by archive_read_data() to track blocks and copy
63          * data to client buffers, filling gaps with zero bytes.
64          */
65         const char       *read_data_block;
66         off_t             read_data_offset;
67         off_t             read_data_output_offset;
68         size_t            read_data_remaining;
69
70         /* Callbacks to open/read/write/close archive stream. */
71         archive_open_callback   *client_opener;
72         archive_read_callback   *client_reader;
73         archive_write_callback  *client_writer;
74         archive_close_callback  *client_closer;
75         void                    *client_data;
76
77         /*
78          * Blocking information.  Note that bytes_in_last_block is
79          * misleadingly named; I should find a better name.  These
80          * control the final output from all compressors, including
81          * compression_none.
82          */
83         int               bytes_per_block;
84         int               bytes_in_last_block;
85
86         /*
87          * These control whether data within a gzip/bzip2 compressed
88          * stream gets padded or not.  If pad_uncompressed is set,
89          * the data will be padded to a full block before being
90          * compressed.  The pad_uncompressed_byte determines the value
91          * that will be used for padding.  Note that these have no
92          * effect on compression "none."
93          */
94         int               pad_uncompressed;
95         int               pad_uncompressed_byte; /* TODO: Support this. */
96
97         /* Position in UNCOMPRESSED data stream. */
98         off_t             file_position;
99         /* Position in COMPRESSED data stream. */
100         off_t             raw_position;
101         /* File offset of beginning of most recently-read header. */
102         off_t             header_position;
103
104         /*
105          * Detection functions for decompression: bid functions are
106          * given a block of data from the beginning of the stream and
107          * can bid on whether or not they support the data stream.
108          * General guideline: bid the number of bits that you actually
109          * test, e.g., 16 if you test a 2-byte magic value.  The
110          * highest bidder will have their init function invoked, which
111          * can set up pointers to specific handlers.
112          *
113          * On write, the client just invokes an archive_write_set function
114          * which sets up the data here directly.
115          */
116         int       compression_code;     /* Currently active compression. */
117         const char *compression_name;
118         struct {
119                 int     (*bid)(const void *buff, size_t);
120                 int     (*init)(struct archive *, const void *buff, size_t);
121         }       decompressors[4];
122         /* Read/write data stream (with compression). */
123         void     *compression_data;             /* Data for (de)compressor. */
124         int     (*compression_init)(struct archive *);  /* Initialize. */
125         int     (*compression_finish)(struct archive *);
126         int     (*compression_write)(struct archive *, const void *, size_t);
127         /*
128          * Read uses a peek/consume I/O model: the decompression code
129          * returns a pointer to the requested block and advances the
130          * file position only when requested by a consume call.  This
131          * reduces copying and also simplifies look-ahead for format
132          * detection.
133          */
134         ssize_t (*compression_read_ahead)(struct archive *,
135                     const void **, size_t request);
136         ssize_t (*compression_read_consume)(struct archive *, size_t);
137
138         /*
139          * Format detection is mostly the same as compression
140          * detection, with two significant differences: The bidders
141          * use the read_ahead calls above to examine the stream rather
142          * than having the supervisor hand them a block of data to
143          * examine, and the auction is repeated for every header.
144          * Winning bidders should set the archive_format and
145          * archive_format_name appropriately.  Bid routines should
146          * check archive_format and decline to bid if the format of
147          * the last header was incompatible.
148          *
149          * Again, write support is considerably simpler because there's
150          * no need for an auction.
151          */
152         int               archive_format;
153         const char       *archive_format_name;
154
155         struct archive_format_descriptor {
156                 int     (*bid)(struct archive *);
157                 int     (*read_header)(struct archive *, struct archive_entry *);
158                 int     (*read_data)(struct archive *, const void **, size_t *, off_t *);
159                 int     (*read_data_skip)(struct archive *);
160                 int     (*cleanup)(struct archive *);
161                 void     *format_data;  /* Format-specific data for readers. */
162         }       formats[4];
163         struct archive_format_descriptor        *format; /* Active format. */
164
165         /*
166          * Storage for format-specific data.  Note that there can be
167          * multiple format readers active at one time, so we need to
168          * allow for multiple format readers to have their data
169          * available.  The pformat_data slot here is the solution: on
170          * read, it is gauranteed to always point to a void* variable
171          * that the format can use.
172          */
173         void    **pformat_data;         /* Pointer to current format_data. */
174         void     *format_data;          /* Used by writers. */
175
176         /*
177          * Pointers to format-specific functions for writing.  They're
178          * initialized by archive_write_set_format_XXX() calls.
179          */
180         int     (*format_init)(struct archive *); /* Only used on write. */
181         int     (*format_finish)(struct archive *);
182         int     (*format_finish_entry)(struct archive *);
183         int     (*format_write_header)(struct archive *,
184                     struct archive_entry *);
185         int     (*format_write_data)(struct archive *,
186                     const void *buff, size_t);
187
188         /*
189          * Various information needed by archive_extract.
190          */
191         struct extract           *extract;
192         void                    (*extract_progress)(void *);
193         void                     *extract_progress_user_data;
194         void                    (*cleanup_archive_extract)(struct archive *);
195
196         int               archive_error_number;
197         const char       *error;
198         struct archive_string   error_string;
199 };
200
201
202 /*
203  * Utility function to format a USTAR header into a buffer.  If
204  * "strict" is set, this tries to create the absolutely most portable
205  * version of a ustar header.  If "strict" is set to 0, then it will
206  * relax certain requirements.
207  */
208 int
209 __archive_write_format_header_ustar(struct archive *, char buff[512],
210     struct archive_entry *, int tartype, int strict);
211
212 #define ARCHIVE_STATE_ANY       0xFFFFU
213 #define ARCHIVE_STATE_NEW       1U
214 #define ARCHIVE_STATE_HEADER    2U
215 #define ARCHIVE_STATE_DATA      4U
216 #define ARCHIVE_STATE_EOF       8U
217 #define ARCHIVE_STATE_CLOSED    0x10U
218 #define ARCHIVE_STATE_FATAL     0x8000U
219
220 /* Check magic value and state; exit if it isn't valid. */
221 void
222 __archive_check_magic(struct archive *, unsigned magic,
223     unsigned state, const char *func);
224
225 #define archive_check_magic(a,m,s) \
226         __archive_check_magic((a), (m), (s), __func__)
227
228 int     __archive_read_register_format(struct archive *a,
229             void *format_data,
230             int (*bid)(struct archive *),
231             int (*read_header)(struct archive *, struct archive_entry *),
232             int (*read_data)(struct archive *, const void **, size_t *, off_t *),
233             int (*read_data_skip)(struct archive *),
234             int (*cleanup)(struct archive *));
235
236 int     __archive_read_register_compression(struct archive *a,
237             int (*bid)(const void *, size_t),
238             int (*init)(struct archive *, const void *, size_t));
239
240 void    __archive_errx(int retvalue, const char *msg);
241
242 #define err_combine(a,b)        ((a) < (b) ? (a) : (b))
243
244 #endif