sysdefs.h 5.1 KB

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