2 * Copyright (c) 2003-2004 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 * 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.
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.
26 * $FreeBSD: src/lib/libarchive/archive_private.h,v 1.17 2005/04/06 04:19:30 kientzle Exp $
29 #ifndef ARCHIVE_PRIVATE_H_INCLUDED
30 #define ARCHIVE_PRIVATE_H_INCLUDED
35 #include "archive_string.h"
37 #define ARCHIVE_WRITE_MAGIC (0xb0c5c0deU)
38 #define ARCHIVE_READ_MAGIC (0xdeb0c5U)
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.
50 struct archive_entry *entry;
51 uid_t user_uid; /* UID of current user. */
53 /* Dev/ino of the archive being read/written. */
57 /* Utility: Pointer to a block of nulls. */
62 * Used by archive_read_data() to track blocks and copy
63 * data to client buffers, filling gaps with zero bytes.
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;
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;
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
84 int bytes_in_last_block;
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."
95 int pad_uncompressed_byte; /* TODO: Support this. */
97 /* Position in UNCOMPRESSED data stream. */
99 /* Position in COMPRESSED data stream. */
101 /* File offset of beginning of most recently-read header. */
102 off_t header_position;
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.
113 * On write, the client just invokes an archive_write_set function
114 * which sets up the data here directly.
116 int compression_code; /* Currently active compression. */
117 const char *compression_name;
119 int (*bid)(const void *buff, size_t);
120 int (*init)(struct archive *, const void *buff, size_t);
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);
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
134 ssize_t (*compression_read_ahead)(struct archive *,
135 const void **, size_t request);
136 ssize_t (*compression_read_consume)(struct archive *, size_t);
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.
149 * Again, write support is considerably simpler because there's
150 * no need for an auction.
153 const char *archive_format_name;
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. */
163 struct archive_format_descriptor *format; /* Active format. */
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.
173 void **pformat_data; /* Pointer to current format_data. */
174 void *format_data; /* Used by writers. */
177 * Pointers to format-specific functions for writing. They're
178 * initialized by archive_write_set_format_XXX() calls.
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);
189 * Various information needed by archive_extract.
191 struct extract *extract;
192 void (*extract_progress)(void *);
193 void *extract_progress_user_data;
194 void (*cleanup_archive_extract)(struct archive *);
196 int archive_error_number;
198 struct archive_string error_string;
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.
209 __archive_write_format_header_ustar(struct archive *, char buff[512],
210 struct archive_entry *, int tartype, int strict);
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
220 /* Check magic value and state; exit if it isn't valid. */
222 __archive_check_magic(struct archive *, unsigned magic,
223 unsigned state, const char *func);
225 #define archive_check_magic(a,m,s) \
226 __archive_check_magic((a), (m), (s), __func__)
228 int __archive_read_register_format(struct archive *a,
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 *));
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));
240 void __archive_errx(int retvalue, const char *msg);
242 #define err_combine(a,b) ((a) < (b) ? (a) : (b))