sysdefs.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. // Choose if MinGW-w64's stdio replacement functions should be used.
  22. // The default has varied slightly in the past so it's clearest to always
  23. // set it explicitly.
  24. //
  25. // Modern MinGW-w64 enables the replacement functions even with UCRT
  26. // when _GNU_SOURCE is defined. That's good because UCRT doesn't support
  27. // the POSIX thousand separator flag in printf (like "%'u"). Otherwise
  28. // XZ Utils works with the UCRT stdio functions.
  29. //
  30. // The replacement functions add over 20 KiB to each executable. For
  31. // size-optimized builds (HAVE_SMALL), disable the replacements.
  32. // Then thousand separators aren't shown in xz's messages but this is
  33. // a minor downside compare to the slower speed of the HAVE_SMALL builds.
  34. //
  35. // The legacy MSVCRT is pre-C99 and it's best to always use the stdio
  36. // replacements functions from MinGW-w64.
  37. #if defined(__MINGW32__) && !defined(__USE_MINGW_ANSI_STDIO)
  38. # define __USE_MINGW_ANSI_STDIO 1
  39. # include <_mingw.h>
  40. # if defined(_UCRT) && defined(HAVE_SMALL)
  41. # undef __USE_MINGW_ANSI_STDIO
  42. # define __USE_MINGW_ANSI_STDIO 0
  43. # endif
  44. #endif
  45. // size_t and NULL
  46. #include <stddef.h>
  47. #ifdef HAVE_INTTYPES_H
  48. # include <inttypes.h>
  49. #endif
  50. // C99 says that inttypes.h always includes stdint.h, but some systems
  51. // don't do that, and require including stdint.h separately.
  52. #ifdef HAVE_STDINT_H
  53. # include <stdint.h>
  54. #endif
  55. // Some pre-C99 systems have SIZE_MAX in limits.h instead of stdint.h. The
  56. // limits are also used to figure out some macros missing from pre-C99 systems.
  57. #include <limits.h>
  58. // Be more compatible with systems that have non-conforming inttypes.h.
  59. // We assume that int is 32-bit and that long is either 32-bit or 64-bit.
  60. // Full Autoconf test could be more correct, but this should work well enough.
  61. // Note that this duplicates some code from lzma.h, but this is better since
  62. // we can work without inttypes.h thanks to Autoconf tests.
  63. #ifndef UINT32_C
  64. # if UINT_MAX != 4294967295U
  65. # error UINT32_C is not defined and unsigned int is not 32-bit.
  66. # endif
  67. # define UINT32_C(n) n ## U
  68. #endif
  69. #ifndef UINT32_MAX
  70. # define UINT32_MAX UINT32_C(4294967295)
  71. #endif
  72. #ifndef PRIu32
  73. # define PRIu32 "u"
  74. #endif
  75. #ifndef PRIx32
  76. # define PRIx32 "x"
  77. #endif
  78. #ifndef PRIX32
  79. # define PRIX32 "X"
  80. #endif
  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. // Incorrect(?) SIZE_MAX:
  112. // - Interix headers typedef size_t to unsigned long,
  113. // but a few lines later define SIZE_MAX to INT32_MAX.
  114. // - SCO OpenServer (x86) headers typedef size_t to unsigned int
  115. // but define SIZE_MAX to INT32_MAX.
  116. #if defined(__INTERIX) || defined(_SCO_DS)
  117. # undef SIZE_MAX
  118. #endif
  119. // The code currently assumes that size_t is either 32-bit or 64-bit.
  120. #ifndef SIZE_MAX
  121. # if SIZEOF_SIZE_T == 4
  122. # define SIZE_MAX UINT32_MAX
  123. # elif SIZEOF_SIZE_T == 8
  124. # define SIZE_MAX UINT64_MAX
  125. # else
  126. # error size_t is not 32-bit or 64-bit
  127. # endif
  128. #endif
  129. #if SIZE_MAX != UINT32_MAX && SIZE_MAX != UINT64_MAX
  130. # error size_t is not 32-bit or 64-bit
  131. #endif
  132. #include <stdlib.h>
  133. #include <assert.h>
  134. // Pre-C99 systems lack stdbool.h. All the code in XZ Utils must be written
  135. // so that it works with fake bool type, for example:
  136. //
  137. // bool foo = (flags & 0x100) != 0;
  138. // bool bar = !!(flags & 0x100);
  139. //
  140. // This works with the real C99 bool but breaks with fake bool:
  141. //
  142. // bool baz = (flags & 0x100);
  143. //
  144. #ifdef HAVE_STDBOOL_H
  145. # include <stdbool.h>
  146. #else
  147. # if ! HAVE__BOOL
  148. typedef unsigned char _Bool;
  149. # endif
  150. # define bool _Bool
  151. # define false 0
  152. # define true 1
  153. # define __bool_true_false_are_defined 1
  154. #endif
  155. #include <string.h>
  156. // Visual Studio 2013 update 2 supports only __inline, not inline.
  157. // MSVC v19.0 / VS 2015 and newer support both.
  158. //
  159. // MSVC v19.27 (VS 2019 version 16.7) added support for restrict.
  160. // Older ones support only __restrict.
  161. #ifdef _MSC_VER
  162. # if _MSC_VER < 1900 && !defined(inline)
  163. # define inline __inline
  164. # endif
  165. # if _MSC_VER < 1927 && !defined(restrict)
  166. # define restrict __restrict
  167. # endif
  168. #endif
  169. ////////////
  170. // Macros //
  171. ////////////
  172. #undef memzero
  173. #define memzero(s, n) memset(s, 0, n)
  174. // NOTE: Avoid using MIN() and MAX(), because even conditionally defining
  175. // those macros can cause some portability trouble, since on some systems
  176. // the system headers insist defining their own versions.
  177. #define my_min(x, y) ((x) < (y) ? (x) : (y))
  178. #define my_max(x, y) ((x) > (y) ? (x) : (y))
  179. #ifndef ARRAY_SIZE
  180. # define ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0]))
  181. #endif
  182. #if defined(__GNUC__) \
  183. && ((__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. #endif