xref: /freebsd/contrib/xz/src/common/sysdefs.h (revision e0c4386e)
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       sysdefs.h
4 /// \brief      Common includes, definitions, system-specific things etc.
5 ///
6 /// This file is used also by the lzma command line tool, that's why this
7 /// file is separate from common.h.
8 //
9 //  Author:     Lasse Collin
10 //
11 //  This file has been put into the public domain.
12 //  You can do whatever you want with this file.
13 //
14 ///////////////////////////////////////////////////////////////////////////////
15 
16 #ifndef LZMA_SYSDEFS_H
17 #define LZMA_SYSDEFS_H
18 
19 //////////////
20 // Includes //
21 //////////////
22 
23 #ifdef HAVE_CONFIG_H
24 #	include <config.h>
25 #endif
26 
27 // This #define ensures that C99 and POSIX compliant stdio functions are
28 // available with MinGW-w64 (both 32-bit and 64-bit). Modern MinGW-w64 adds
29 // this automatically, for example, when the compiler is in C99 (or later)
30 // mode when building against msvcrt.dll. It still doesn't hurt to be explicit
31 // that we always want this and #define this unconditionally.
32 //
33 // With Universal CRT (UCRT) this is less important because UCRT contains
34 // C99-compatible stdio functions. It's still nice to #define this as UCRT
35 // doesn't support the POSIX thousand separator flag in printf (like "%'u").
36 #ifdef __MINGW32__
37 #	define __USE_MINGW_ANSI_STDIO 1
38 #endif
39 
40 // size_t and NULL
41 #include <stddef.h>
42 
43 #ifdef HAVE_INTTYPES_H
44 #	include <inttypes.h>
45 #endif
46 
47 // C99 says that inttypes.h always includes stdint.h, but some systems
48 // don't do that, and require including stdint.h separately.
49 #ifdef HAVE_STDINT_H
50 #	include <stdint.h>
51 #endif
52 
53 // Some pre-C99 systems have SIZE_MAX in limits.h instead of stdint.h. The
54 // limits are also used to figure out some macros missing from pre-C99 systems.
55 #include <limits.h>
56 
57 // Be more compatible with systems that have non-conforming inttypes.h.
58 // We assume that int is 32-bit and that long is either 32-bit or 64-bit.
59 // Full Autoconf test could be more correct, but this should work well enough.
60 // Note that this duplicates some code from lzma.h, but this is better since
61 // we can work without inttypes.h thanks to Autoconf tests.
62 #ifndef UINT32_C
63 #	if UINT_MAX != 4294967295U
64 #		error UINT32_C is not defined and unsigned int is not 32-bit.
65 #	endif
66 #	define UINT32_C(n) n ## U
67 #endif
68 #ifndef UINT32_MAX
69 #	define UINT32_MAX UINT32_C(4294967295)
70 #endif
71 #ifndef PRIu32
72 #	define PRIu32 "u"
73 #endif
74 #ifndef PRIx32
75 #	define PRIx32 "x"
76 #endif
77 #ifndef PRIX32
78 #	define PRIX32 "X"
79 #endif
80 
81 #if ULONG_MAX == 4294967295UL
82 #	ifndef UINT64_C
83 #		define UINT64_C(n) n ## ULL
84 #	endif
85 #	ifndef PRIu64
86 #		define PRIu64 "llu"
87 #	endif
88 #	ifndef PRIx64
89 #		define PRIx64 "llx"
90 #	endif
91 #	ifndef PRIX64
92 #		define PRIX64 "llX"
93 #	endif
94 #else
95 #	ifndef UINT64_C
96 #		define UINT64_C(n) n ## UL
97 #	endif
98 #	ifndef PRIu64
99 #		define PRIu64 "lu"
100 #	endif
101 #	ifndef PRIx64
102 #		define PRIx64 "lx"
103 #	endif
104 #	ifndef PRIX64
105 #		define PRIX64 "lX"
106 #	endif
107 #endif
108 #ifndef UINT64_MAX
109 #	define UINT64_MAX UINT64_C(18446744073709551615)
110 #endif
111 
112 // Incorrect(?) SIZE_MAX:
113 //   - Interix headers typedef size_t to unsigned long,
114 //     but a few lines later define SIZE_MAX to INT32_MAX.
115 //   - SCO OpenServer (x86) headers typedef size_t to unsigned int
116 //     but define SIZE_MAX to INT32_MAX.
117 #if defined(__INTERIX) || defined(_SCO_DS)
118 #	undef SIZE_MAX
119 #endif
120 
121 // The code currently assumes that size_t is either 32-bit or 64-bit.
122 #ifndef SIZE_MAX
123 #	if SIZEOF_SIZE_T == 4
124 #		define SIZE_MAX UINT32_MAX
125 #	elif SIZEOF_SIZE_T == 8
126 #		define SIZE_MAX UINT64_MAX
127 #	else
128 #		error size_t is not 32-bit or 64-bit
129 #	endif
130 #endif
131 #if SIZE_MAX != UINT32_MAX && SIZE_MAX != UINT64_MAX
132 #	error size_t is not 32-bit or 64-bit
133 #endif
134 
135 #include <stdlib.h>
136 #include <assert.h>
137 
138 // Pre-C99 systems lack stdbool.h. All the code in XZ Utils must be written
139 // so that it works with fake bool type, for example:
140 //
141 //    bool foo = (flags & 0x100) != 0;
142 //    bool bar = !!(flags & 0x100);
143 //
144 // This works with the real C99 bool but breaks with fake bool:
145 //
146 //    bool baz = (flags & 0x100);
147 //
148 #ifdef HAVE_STDBOOL_H
149 #	include <stdbool.h>
150 #else
151 #	if ! HAVE__BOOL
152 typedef unsigned char _Bool;
153 #	endif
154 #	define bool _Bool
155 #	define false 0
156 #	define true 1
157 #	define __bool_true_false_are_defined 1
158 #endif
159 
160 #include <string.h>
161 
162 // As of MSVC 2013, inline and restrict are supported with
163 // non-standard keywords.
164 #if defined(_WIN32) && defined(_MSC_VER)
165 #	ifndef inline
166 #		define inline __inline
167 #	endif
168 #	ifndef restrict
169 #		define restrict __restrict
170 #	endif
171 #endif
172 
173 ////////////
174 // Macros //
175 ////////////
176 
177 #undef memzero
178 #define memzero(s, n) memset(s, 0, n)
179 
180 // NOTE: Avoid using MIN() and MAX(), because even conditionally defining
181 // those macros can cause some portability trouble, since on some systems
182 // the system headers insist defining their own versions.
183 #define my_min(x, y) ((x) < (y) ? (x) : (y))
184 #define my_max(x, y) ((x) > (y) ? (x) : (y))
185 
186 #ifndef ARRAY_SIZE
187 #	define ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0]))
188 #endif
189 
190 #if defined(__GNUC__) \
191 		&& ((__GNUC__ == 4 && __GNUC_MINOR__ >= 3) || __GNUC__ > 4)
192 #	define lzma_attr_alloc_size(x) __attribute__((__alloc_size__(x)))
193 #else
194 #	define lzma_attr_alloc_size(x)
195 #endif
196 
197 #endif
198