sysdefs.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. #include <limits.h>
  40. // Be more compatible with systems that have non-conforming inttypes.h.
  41. // We assume that int is 32-bit and that long is either 32-bit or 64-bit.
  42. // Full Autoconf test could be more correct, but this should work well enough.
  43. // Note that this duplicates some code from lzma.h, but this is better since
  44. // we can work without inttypes.h thanks to Autoconf tests.
  45. #ifndef UINT32_C
  46. # if UINT_MAX != 4294967295U
  47. # error UINT32_C is not defined and unsigned int is not 32-bit.
  48. # endif
  49. # define UINT32_C(n) n ## U
  50. #endif
  51. #ifndef UINT32_MAX
  52. # define UINT32_MAX UINT32_C(4294967295)
  53. #endif
  54. #ifndef PRIu32
  55. # define PRIu32 "u"
  56. #endif
  57. #ifndef PRIx32
  58. # define PRIx32 "x"
  59. #endif
  60. #ifndef PRIX32
  61. # define PRIX32 "X"
  62. #endif
  63. #if ULONG_MAX == 4294967295UL
  64. # ifndef UINT64_C
  65. # define UINT64_C(n) n ## ULL
  66. # endif
  67. # ifndef PRIu64
  68. # define PRIu64 "llu"
  69. # endif
  70. # ifndef PRIx64
  71. # define PRIx64 "llx"
  72. # endif
  73. # ifndef PRIX64
  74. # define PRIX64 "llX"
  75. # endif
  76. #else
  77. # ifndef UINT64_C
  78. # define UINT64_C(n) n ## UL
  79. # endif
  80. # ifndef PRIu64
  81. # define PRIu64 "lu"
  82. # endif
  83. # ifndef PRIx64
  84. # define PRIx64 "lx"
  85. # endif
  86. # ifndef PRIX64
  87. # define PRIX64 "lX"
  88. # endif
  89. #endif
  90. #ifndef UINT64_MAX
  91. # define UINT64_MAX UINT64_C(18446744073709551615)
  92. #endif
  93. // Incorrect(?) SIZE_MAX:
  94. // - Interix headers typedef size_t to unsigned long,
  95. // but a few lines later define SIZE_MAX to INT32_MAX.
  96. // - SCO OpenServer (x86) headers typedef size_t to unsigned int
  97. // but define SIZE_MAX to INT32_MAX.
  98. #if defined(__INTERIX) || defined(_SCO_DS)
  99. # undef SIZE_MAX
  100. #endif
  101. // The code currently assumes that size_t is either 32-bit or 64-bit.
  102. #ifndef SIZE_MAX
  103. # if SIZEOF_SIZE_T == 4
  104. # define SIZE_MAX UINT32_MAX
  105. # elif SIZEOF_SIZE_T == 8
  106. # define SIZE_MAX UINT64_MAX
  107. # else
  108. # error size_t is not 32-bit or 64-bit
  109. # endif
  110. #endif
  111. #if SIZE_MAX != UINT32_MAX && SIZE_MAX != UINT64_MAX
  112. # error size_t is not 32-bit or 64-bit
  113. #endif
  114. #include <stdlib.h>
  115. #include <assert.h>
  116. // Pre-C99 systems lack stdbool.h. All the code in LZMA Utils must be written
  117. // so that it works with fake bool type, for example:
  118. //
  119. // bool foo = (flags & 0x100) != 0;
  120. // bool bar = !!(flags & 0x100);
  121. //
  122. // This works with the real C99 bool but breaks with fake bool:
  123. //
  124. // bool baz = (flags & 0x100);
  125. //
  126. #ifdef HAVE_STDBOOL_H
  127. # include <stdbool.h>
  128. #else
  129. # if ! HAVE__BOOL
  130. typedef unsigned char _Bool;
  131. # endif
  132. # define bool _Bool
  133. # define false 0
  134. # define true 1
  135. # define __bool_true_false_are_defined 1
  136. #endif
  137. // string.h should be enough but let's include strings.h and memory.h too if
  138. // they exists, since that shouldn't do any harm, but may improve portability.
  139. #include <string.h>
  140. #ifdef HAVE_STRINGS_H
  141. # include <strings.h>
  142. #endif
  143. #ifdef HAVE_MEMORY_H
  144. # include <memory.h>
  145. #endif
  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