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