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