xref: /dragonfly/contrib/xz/src/common/sysdefs.h (revision 16fb0422)
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 // Get standard-compliant stdio functions under MinGW and MinGW-w64.
28 #ifdef __MINGW32__
29 #	define __USE_MINGW_ANSI_STDIO 1
30 #endif
31 
32 // size_t and NULL
33 #include <stddef.h>
34 
35 #ifdef HAVE_INTTYPES_H
36 #	include <inttypes.h>
37 #endif
38 
39 // C99 says that inttypes.h always includes stdint.h, but some systems
40 // don't do that, and require including stdint.h separately.
41 #ifdef HAVE_STDINT_H
42 #	include <stdint.h>
43 #endif
44 
45 // Some pre-C99 systems have SIZE_MAX in limits.h instead of stdint.h. The
46 // limits are also used to figure out some macros missing from pre-C99 systems.
47 #ifdef HAVE_LIMITS_H
48 #	include <limits.h>
49 #endif
50 
51 // Be more compatible with systems that have non-conforming inttypes.h.
52 // We assume that int is 32-bit and that long is either 32-bit or 64-bit.
53 // Full Autoconf test could be more correct, but this should work well enough.
54 // Note that this duplicates some code from lzma.h, but this is better since
55 // we can work without inttypes.h thanks to Autoconf tests.
56 #ifndef UINT32_C
57 #	if UINT_MAX != 4294967295U
58 #		error UINT32_C is not defined and unsigned int is not 32-bit.
59 #	endif
60 #	define UINT32_C(n) n ## U
61 #endif
62 #ifndef UINT32_MAX
63 #	define UINT32_MAX UINT32_C(4294967295)
64 #endif
65 #ifndef PRIu32
66 #	define PRIu32 "u"
67 #endif
68 #ifndef PRIx32
69 #	define PRIx32 "x"
70 #endif
71 #ifndef PRIX32
72 #	define PRIX32 "X"
73 #endif
74 
75 #if ULONG_MAX == 4294967295UL
76 #	ifndef UINT64_C
77 #		define UINT64_C(n) n ## ULL
78 #	endif
79 #	ifndef PRIu64
80 #		define PRIu64 "llu"
81 #	endif
82 #	ifndef PRIx64
83 #		define PRIx64 "llx"
84 #	endif
85 #	ifndef PRIX64
86 #		define PRIX64 "llX"
87 #	endif
88 #else
89 #	ifndef UINT64_C
90 #		define UINT64_C(n) n ## UL
91 #	endif
92 #	ifndef PRIu64
93 #		define PRIu64 "lu"
94 #	endif
95 #	ifndef PRIx64
96 #		define PRIx64 "lx"
97 #	endif
98 #	ifndef PRIX64
99 #		define PRIX64 "lX"
100 #	endif
101 #endif
102 #ifndef UINT64_MAX
103 #	define UINT64_MAX UINT64_C(18446744073709551615)
104 #endif
105 
106 // Interix has broken header files, which typedef size_t to unsigned long,
107 // but a few lines later define SIZE_MAX to INT32_MAX.
108 #ifdef __INTERIX
109 #	undef SIZE_MAX
110 #endif
111 
112 // The code currently assumes that size_t is either 32-bit or 64-bit.
113 #ifndef SIZE_MAX
114 #	if SIZEOF_SIZE_T == 4
115 #		define SIZE_MAX UINT32_MAX
116 #	elif SIZEOF_SIZE_T == 8
117 #		define SIZE_MAX UINT64_MAX
118 #	else
119 #		error size_t is not 32-bit or 64-bit
120 #	endif
121 #endif
122 #if SIZE_MAX != UINT32_MAX && SIZE_MAX != UINT64_MAX
123 #	error size_t is not 32-bit or 64-bit
124 #endif
125 
126 #include <stdlib.h>
127 #include <assert.h>
128 
129 // Pre-C99 systems lack stdbool.h. All the code in LZMA Utils must be written
130 // so that it works with fake bool type, for example:
131 //
132 //    bool foo = (flags & 0x100) != 0;
133 //    bool bar = !!(flags & 0x100);
134 //
135 // This works with the real C99 bool but breaks with fake bool:
136 //
137 //    bool baz = (flags & 0x100);
138 //
139 #ifdef HAVE_STDBOOL_H
140 #	include <stdbool.h>
141 #else
142 #	if ! HAVE__BOOL
143 typedef unsigned char _Bool;
144 #	endif
145 #	define bool _Bool
146 #	define false 0
147 #	define true 1
148 #	define __bool_true_false_are_defined 1
149 #endif
150 
151 // string.h should be enough but let's include strings.h and memory.h too if
152 // they exists, since that shouldn't do any harm, but may improve portability.
153 #ifdef HAVE_STRING_H
154 #	include <string.h>
155 #endif
156 
157 #ifdef HAVE_STRINGS_H
158 #	include <strings.h>
159 #endif
160 
161 #ifdef HAVE_MEMORY_H
162 #	include <memory.h>
163 #endif
164 
165 
166 ////////////
167 // Macros //
168 ////////////
169 
170 #undef memzero
171 #define memzero(s, n) memset(s, 0, n)
172 
173 // NOTE: Avoid using MIN() and MAX(), because even conditionally defining
174 // those macros can cause some portability trouble, since on some systems
175 // the system headers insist defining their own versions.
176 #define my_min(x, y) ((x) < (y) ? (x) : (y))
177 #define my_max(x, y) ((x) > (y) ? (x) : (y))
178 
179 #ifndef ARRAY_SIZE
180 #	define ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0]))
181 #endif
182 
183 #if (__GNUC__ == 4 && __GNUC_MINOR__ >= 3) || __GNUC__ > 4
184 #	define lzma_attr_alloc_size(x) __attribute__((__alloc_size__(x)))
185 #else
186 #	define lzma_attr_alloc_size(x)
187 #endif
188 
189 #endif
190