xref: /freebsd/contrib/libarchive/libarchive/archive_platform.h (revision 2e113ef82465598b8c26e0ca415fbe90677fbd47)
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 
26 /* !!ONLY FOR USE INTERNALLY TO LIBARCHIVE!! */
27 
28 /*
29  * This header is the first thing included in any of the libarchive
30  * source files.  As far as possible, platform-specific issues should
31  * be dealt with here and not within individual source files.  I'm
32  * actively trying to minimize #if blocks within the main source,
33  * since they obfuscate the code.
34  */
35 
36 #ifndef ARCHIVE_PLATFORM_H_INCLUDED
37 #define	ARCHIVE_PLATFORM_H_INCLUDED
38 
39 /* archive.h and archive_entry.h require this. */
40 #define	__LIBARCHIVE_BUILD 1
41 
42 #if defined(PLATFORM_CONFIG_H)
43 /* Use hand-built config.h in environments that need it. */
44 #include PLATFORM_CONFIG_H
45 #elif defined(HAVE_CONFIG_H)
46 /* Most POSIX platforms use the 'configure' script to build config.h */
47 #include "config.h"
48 #else
49 /* Warn if the library hasn't been (automatically or manually) configured. */
50 #error Oops: No config.h and no pre-built configuration in archive_platform.h.
51 #endif
52 
53 /* On macOS check for some symbols based on the deployment target version.  */
54 #if defined(__APPLE__)
55 # undef HAVE_FUTIMENS
56 # undef HAVE_UTIMENSAT
57 # include <AvailabilityMacros.h>
58 # if MAC_OS_X_VERSION_MIN_REQUIRED >= 101300
59 #  define HAVE_FUTIMENS 1
60 #  define HAVE_UTIMENSAT 1
61 # endif
62 #endif
63 
64 /* For cygwin, to avoid missing LONG, ULONG, PUCHAR, ... definitions */
65 #ifdef __CYGWIN__
66 #include <windef.h>
67 #endif
68 
69 /* It should be possible to get rid of this by extending the feature-test
70  * macros to cover Windows API functions, probably along with non-trivial
71  * refactoring of code to find structures that sit more cleanly on top of
72  * either Windows or Posix APIs. */
73 #if (defined(__WIN32__) || defined(_WIN32) || defined(__WIN32)) && !defined(__CYGWIN__)
74 #include "archive_windows.h"
75 /* The C library on Windows specifies a calling convention for callback
76  * functions and exports; when we interact with them (capture pointers,
77  * call and pass function pointers) we need to match their calling
78  * convention.
79  * This only matters when libarchive is built with /Gr, /Gz or /Gv
80  * (which change the default calling convention.) */
81 #define __LA_LIBC_CC __cdecl
82 #else
83 #define la_stat(path,stref)		stat(path,stref)
84 #define __LA_LIBC_CC
85 #endif
86 
87 /*
88  * The config files define a lot of feature macros.  The following
89  * uses those macros to select/define replacements and include key
90  * headers as required.
91  */
92 
93 /* Try to get standard C99-style integer type definitions. */
94 #if HAVE_INTTYPES_H
95 #include <inttypes.h>
96 #endif
97 #if HAVE_STDINT_H
98 #include <stdint.h>
99 #endif
100 
101 /* Borland warns about its own constants!  */
102 #if defined(__BORLANDC__)
103 # if HAVE_DECL_UINT64_MAX
104 #  undef	UINT64_MAX
105 #  undef	HAVE_DECL_UINT64_MAX
106 # endif
107 # if HAVE_DECL_UINT64_MIN
108 #  undef	UINT64_MIN
109 #  undef	HAVE_DECL_UINT64_MIN
110 # endif
111 # if HAVE_DECL_INT64_MAX
112 #  undef	INT64_MAX
113 #  undef	HAVE_DECL_INT64_MAX
114 # endif
115 # if HAVE_DECL_INT64_MIN
116 #  undef	INT64_MIN
117 #  undef	HAVE_DECL_INT64_MIN
118 # endif
119 #endif
120 
121 /* Some platforms lack the standard *_MAX definitions. */
122 #if !HAVE_DECL_SIZE_MAX
123 #define	SIZE_MAX (~(size_t)0)
124 #endif
125 #if !HAVE_DECL_SSIZE_MAX
126 #define	SSIZE_MAX ((ssize_t)(SIZE_MAX >> 1))
127 #endif
128 #if !HAVE_DECL_UINT32_MAX
129 #define	UINT32_MAX (~(uint32_t)0)
130 #endif
131 #if !HAVE_DECL_INT32_MAX
132 #define	INT32_MAX ((int32_t)(UINT32_MAX >> 1))
133 #endif
134 #if !HAVE_DECL_INT32_MIN
135 #define	INT32_MIN ((int32_t)(~INT32_MAX))
136 #endif
137 #if !HAVE_DECL_UINT64_MAX
138 #define	UINT64_MAX (~(uint64_t)0)
139 #endif
140 #if !HAVE_DECL_INT64_MAX
141 #define	INT64_MAX ((int64_t)(UINT64_MAX >> 1))
142 #endif
143 #if !HAVE_DECL_INT64_MIN
144 #define	INT64_MIN ((int64_t)(~INT64_MAX))
145 #endif
146 #if !HAVE_DECL_UINTMAX_MAX
147 #define	UINTMAX_MAX (~(uintmax_t)0)
148 #endif
149 #if !HAVE_DECL_INTMAX_MAX
150 #define	INTMAX_MAX ((intmax_t)(UINTMAX_MAX >> 1))
151 #endif
152 #if !HAVE_DECL_INTMAX_MIN
153 #define	INTMAX_MIN ((intmax_t)(~INTMAX_MAX))
154 #endif
155 
156 /* Some platforms lack the standard PRIxN/PRIdN definitions. */
157 #if !HAVE_INTTYPES_H || !defined(PRIx32) || !defined(PRId32)
158 #ifndef PRIx32
159 #if SIZEOF_INT == 4
160 #define PRIx32 "x"
161 #elif SIZEOF_LONG == 4
162 #define PRIx32 "lx"
163 #else
164 #error No suitable 32-bit unsigned integer type found for this platform
165 #endif
166 #endif // PRIx32
167 #ifndef PRId32
168 #if SIZEOF_INT == 4
169 #define PRId32 "d"
170 #elif SIZEOF_LONG == 4
171 #define PRId32 "ld"
172 #else
173 #error No suitable 32-bit signed integer type found for this platform
174 #endif
175 #endif // PRId32
176 #endif // !HAVE_INTTYPES_H || !defined(PRIx32) || !defined(PRId32)
177 
178 /*
179  * If we can't restore metadata using a file descriptor, then
180  * for compatibility's sake, close files before trying to restore metadata.
181  */
182 #if defined(HAVE_FCHMOD) || defined(HAVE_FUTIMES) || defined(HAVE_ACL_SET_FD) || defined(HAVE_ACL_SET_FD_NP) || defined(HAVE_FCHOWN)
183 #define	CAN_RESTORE_METADATA_FD
184 #endif
185 
186 /*
187  * glibc 2.24 deprecates readdir_r
188  * bionic c deprecates readdir_r too
189  */
190 #if defined(HAVE_READDIR_R) && (!defined(__GLIBC__) || !defined(__GLIBC_MINOR__) || __GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ < 24)) && (!defined(__ANDROID__))
191 #define	USE_READDIR_R	1
192 #else
193 #undef	USE_READDIR_R
194 #endif
195 
196 /* Set up defaults for internal error codes. */
197 #ifndef ARCHIVE_ERRNO_FILE_FORMAT
198 #if HAVE_EFTYPE
199 #define	ARCHIVE_ERRNO_FILE_FORMAT EFTYPE
200 #else
201 #if HAVE_EILSEQ
202 #define	ARCHIVE_ERRNO_FILE_FORMAT EILSEQ
203 #else
204 #define	ARCHIVE_ERRNO_FILE_FORMAT EINVAL
205 #endif
206 #endif
207 #endif
208 
209 #ifndef ARCHIVE_ERRNO_PROGRAMMER
210 #define	ARCHIVE_ERRNO_PROGRAMMER EINVAL
211 #endif
212 
213 #ifndef ARCHIVE_ERRNO_MISC
214 #define	ARCHIVE_ERRNO_MISC (-1)
215 #endif
216 
217 #if defined(__GNUC__) && (__GNUC__ >= 7)
218 #define	__LA_FALLTHROUGH	__attribute__((fallthrough))
219 #else
220 #define	__LA_FALLTHROUGH
221 #endif
222 
223 #endif /* !ARCHIVE_PLATFORM_H_INCLUDED */
224