sysdefs.h 5.1 KB

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