sysdefs.h 5.1 KB

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