sysdefs.h 5.1 KB

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