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