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: stable/11/contrib/libarchive/libarchive/archive_private.h 370535 2021-09-10 08:34:36Z git2svn $
26 */
27
28#ifndef ARCHIVE_PRIVATE_H_INCLUDED
29#define ARCHIVE_PRIVATE_H_INCLUDED
30
31#ifndef __LIBARCHIVE_BUILD
32#error This header is only to be used internally to libarchive.
33#endif
34
35#if HAVE_ICONV_H
36#include <iconv.h>
37#endif
38
39#include "archive.h"
40#include "archive_string.h"
41
42#if defined(__GNUC__) && (__GNUC__ > 2 || \
43			  (__GNUC__ == 2 && __GNUC_MINOR__ >= 5))
44#define	__LA_DEAD	__attribute__((__noreturn__))
45#else
46#define	__LA_DEAD
47#endif
48
49#if defined(__GNUC__) && (__GNUC__ > 2 || \
50			  (__GNUC__ == 2 && __GNUC_MINOR__ >= 7))
51#define	__LA_UNUSED	__attribute__((__unused__))
52#else
53#define	__LA_UNUSED
54#endif
55
56#define	ARCHIVE_WRITE_MAGIC	(0xb0c5c0deU)
57#define	ARCHIVE_READ_MAGIC	(0xdeb0c5U)
58#define	ARCHIVE_WRITE_DISK_MAGIC (0xc001b0c5U)
59#define	ARCHIVE_READ_DISK_MAGIC (0xbadb0c5U)
60#define	ARCHIVE_MATCH_MAGIC	(0xcad11c9U)
61
62#define	ARCHIVE_STATE_NEW	1U
63#define	ARCHIVE_STATE_HEADER	2U
64#define	ARCHIVE_STATE_DATA	4U
65#define	ARCHIVE_STATE_EOF	0x10U
66#define	ARCHIVE_STATE_CLOSED	0x20U
67#define	ARCHIVE_STATE_FATAL	0x8000U
68#define	ARCHIVE_STATE_ANY	(0xFFFFU & ~ARCHIVE_STATE_FATAL)
69
70struct archive_vtable {
71	int	(*archive_close)(struct archive *);
72	int	(*archive_free)(struct archive *);
73	int	(*archive_write_header)(struct archive *,
74	    struct archive_entry *);
75	int	(*archive_write_finish_entry)(struct archive *);
76	ssize_t	(*archive_write_data)(struct archive *,
77	    const void *, size_t);
78	ssize_t	(*archive_write_data_block)(struct archive *,
79	    const void *, size_t, int64_t);
80
81	int	(*archive_read_next_header)(struct archive *,
82	    struct archive_entry **);
83	int	(*archive_read_next_header2)(struct archive *,
84	    struct archive_entry *);
85	int	(*archive_read_data_block)(struct archive *,
86	    const void **, size_t *, int64_t *);
87
88	int	(*archive_filter_count)(struct archive *);
89	int64_t (*archive_filter_bytes)(struct archive *, int);
90	int	(*archive_filter_code)(struct archive *, int);
91	const char * (*archive_filter_name)(struct archive *, int);
92};
93
94struct archive_string_conv;
95
96struct archive {
97	/*
98	 * The magic/state values are used to sanity-check the
99	 * client's usage.  If an API function is called at a
100	 * ridiculous time, or the client passes us an invalid
101	 * pointer, these values allow me to catch that.
102	 */
103	unsigned int	magic;
104	unsigned int	state;
105
106	/*
107	 * Some public API functions depend on the "real" type of the
108	 * archive object.
109	 */
110	struct archive_vtable *vtable;
111
112	int		  archive_format;
113	const char	 *archive_format_name;
114
115	int	  compression_code;	/* Currently active compression. */
116	const char *compression_name;
117
118	/* Number of file entries processed. */
119	int		  file_count;
120
121	int		  archive_error_number;
122	const char	 *error;
123	struct archive_string	error_string;
124
125	char *current_code;
126	unsigned current_codepage; /* Current ACP(ANSI CodePage). */
127	unsigned current_oemcp; /* Current OEMCP(OEM CodePage). */
128	struct archive_string_conv *sconv;
129
130	/*
131	 * Used by archive_read_data() to track blocks and copy
132	 * data to client buffers, filling gaps with zero bytes.
133	 */
134	const char	 *read_data_block;
135	int64_t		  read_data_offset;
136	int64_t		  read_data_output_offset;
137	size_t		  read_data_remaining;
138
139	/*
140	 * Used by formats/filters to determine the amount of data
141	 * requested from a call to archive_read_data(). This is only
142	 * useful when the format/filter has seek support.
143	 */
144	char		  read_data_is_posix_read;
145	size_t		  read_data_requested;
146};
147
148/* Check magic value and state; return(ARCHIVE_FATAL) if it isn't valid. */
149int	__archive_check_magic(struct archive *, unsigned int magic,
150	    unsigned int state, const char *func);
151#define	archive_check_magic(a, expected_magic, allowed_states, function_name) \
152	do { \
153		int magic_test = __archive_check_magic((a), (expected_magic), \
154			(allowed_states), (function_name)); \
155		if (magic_test == ARCHIVE_FATAL) \
156			return ARCHIVE_FATAL; \
157	} while (0)
158
159void	__archive_errx(int retvalue, const char *msg) __LA_DEAD;
160
161void	__archive_ensure_cloexec_flag(int fd);
162int	__archive_mktemp(const char *tmpdir);
163#if defined(_WIN32) && !defined(__CYGWIN__)
164int	__archive_mkstemp(wchar_t *template);
165#else
166int	__archive_mkstemp(char *template);
167#endif
168
169int	__archive_clean(struct archive *);
170
171void __archive_reset_read_data(struct archive *);
172
173#define	err_combine(a,b)	((a) < (b) ? (a) : (b))
174
175#if defined(__BORLANDC__) || (defined(_MSC_VER) &&  _MSC_VER <= 1300)
176# define	ARCHIVE_LITERAL_LL(x)	x##i64
177# define	ARCHIVE_LITERAL_ULL(x)	x##ui64
178#else
179# define	ARCHIVE_LITERAL_LL(x)	x##ll
180# define	ARCHIVE_LITERAL_ULL(x)	x##ull
181#endif
182
183#endif
184