sysdefs.h 4.8 KB

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