15a645f22SBen Gras /////////////////////////////////////////////////////////////////////////////// 25a645f22SBen Gras // 35a645f22SBen Gras /// \file sysdefs.h 45a645f22SBen Gras /// \brief Common includes, definitions, system-specific things etc. 55a645f22SBen Gras /// 65a645f22SBen Gras /// This file is used also by the lzma command line tool, that's why this 75a645f22SBen Gras /// file is separate from common.h. 85a645f22SBen Gras // 95a645f22SBen Gras // Author: Lasse Collin 105a645f22SBen Gras // 115a645f22SBen Gras // This file has been put into the public domain. 125a645f22SBen Gras // You can do whatever you want with this file. 135a645f22SBen Gras // 145a645f22SBen Gras /////////////////////////////////////////////////////////////////////////////// 155a645f22SBen Gras 165a645f22SBen Gras #ifndef LZMA_SYSDEFS_H 175a645f22SBen Gras #define LZMA_SYSDEFS_H 185a645f22SBen Gras 195a645f22SBen Gras ////////////// 205a645f22SBen Gras // Includes // 215a645f22SBen Gras ////////////// 225a645f22SBen Gras 235a645f22SBen Gras #ifdef HAVE_CONFIG_H 245a645f22SBen Gras # include <config.h> 255a645f22SBen Gras #endif 265a645f22SBen Gras 275a645f22SBen Gras // Get standard-compliant stdio functions under MinGW and MinGW-w64. 285a645f22SBen Gras #ifdef __MINGW32__ 295a645f22SBen Gras # define __USE_MINGW_ANSI_STDIO 1 305a645f22SBen Gras #endif 315a645f22SBen Gras 325a645f22SBen Gras // size_t and NULL 335a645f22SBen Gras #include <stddef.h> 345a645f22SBen Gras 355a645f22SBen Gras #ifdef HAVE_INTTYPES_H 365a645f22SBen Gras # include <inttypes.h> 375a645f22SBen Gras #endif 385a645f22SBen Gras 395a645f22SBen Gras // C99 says that inttypes.h always includes stdint.h, but some systems 405a645f22SBen Gras // don't do that, and require including stdint.h separately. 415a645f22SBen Gras #ifdef HAVE_STDINT_H 425a645f22SBen Gras # include <stdint.h> 435a645f22SBen Gras #endif 445a645f22SBen Gras 455a645f22SBen Gras // Some pre-C99 systems have SIZE_MAX in limits.h instead of stdint.h. The 465a645f22SBen Gras // limits are also used to figure out some macros missing from pre-C99 systems. 475a645f22SBen Gras #ifdef HAVE_LIMITS_H 485a645f22SBen Gras # include <limits.h> 495a645f22SBen Gras #endif 505a645f22SBen Gras 515a645f22SBen Gras // Be more compatible with systems that have non-conforming inttypes.h. 525a645f22SBen Gras // We assume that int is 32-bit and that long is either 32-bit or 64-bit. 535a645f22SBen Gras // Full Autoconf test could be more correct, but this should work well enough. 545a645f22SBen Gras // Note that this duplicates some code from lzma.h, but this is better since 555a645f22SBen Gras // we can work without inttypes.h thanks to Autoconf tests. 565a645f22SBen Gras #ifndef UINT32_C 575a645f22SBen Gras # if UINT_MAX != 4294967295U 585a645f22SBen Gras # error UINT32_C is not defined and unsigned int is not 32-bit. 595a645f22SBen Gras # endif 605a645f22SBen Gras # define UINT32_C(n) n ## U 615a645f22SBen Gras #endif 625a645f22SBen Gras #ifndef UINT32_MAX 635a645f22SBen Gras # define UINT32_MAX UINT32_C(4294967295) 645a645f22SBen Gras #endif 655a645f22SBen Gras #ifndef PRIu32 665a645f22SBen Gras # define PRIu32 "u" 675a645f22SBen Gras #endif 6811be35a1SLionel Sambuc #ifndef PRIx32 6911be35a1SLionel Sambuc # define PRIx32 "x" 7011be35a1SLionel Sambuc #endif 715a645f22SBen Gras #ifndef PRIX32 725a645f22SBen Gras # define PRIX32 "X" 735a645f22SBen Gras #endif 745a645f22SBen Gras 755a645f22SBen Gras #if ULONG_MAX == 4294967295UL 765a645f22SBen Gras # ifndef UINT64_C 775a645f22SBen Gras # define UINT64_C(n) n ## ULL 785a645f22SBen Gras # endif 795a645f22SBen Gras # ifndef PRIu64 805a645f22SBen Gras # define PRIu64 "llu" 815a645f22SBen Gras # endif 8211be35a1SLionel Sambuc # ifndef PRIx64 8311be35a1SLionel Sambuc # define PRIx64 "llx" 8411be35a1SLionel Sambuc # endif 855a645f22SBen Gras # ifndef PRIX64 865a645f22SBen Gras # define PRIX64 "llX" 875a645f22SBen Gras # endif 885a645f22SBen Gras #else 895a645f22SBen Gras # ifndef UINT64_C 905a645f22SBen Gras # define UINT64_C(n) n ## UL 915a645f22SBen Gras # endif 925a645f22SBen Gras # ifndef PRIu64 935a645f22SBen Gras # define PRIu64 "lu" 945a645f22SBen Gras # endif 9511be35a1SLionel Sambuc # ifndef PRIx64 9611be35a1SLionel Sambuc # define PRIx64 "lx" 9711be35a1SLionel Sambuc # endif 985a645f22SBen Gras # ifndef PRIX64 995a645f22SBen Gras # define PRIX64 "lX" 1005a645f22SBen Gras # endif 1015a645f22SBen Gras #endif 1025a645f22SBen Gras #ifndef UINT64_MAX 1035a645f22SBen Gras # define UINT64_MAX UINT64_C(18446744073709551615) 1045a645f22SBen Gras #endif 1055a645f22SBen Gras 106*0a6a1f1dSLionel Sambuc // Incorrect(?) SIZE_MAX: 107*0a6a1f1dSLionel Sambuc // - Interix headers typedef size_t to unsigned long, 1085a645f22SBen Gras // but a few lines later define SIZE_MAX to INT32_MAX. 109*0a6a1f1dSLionel Sambuc // - SCO OpenServer (x86) headers typedef size_t to unsigned int 110*0a6a1f1dSLionel Sambuc // but define SIZE_MAX to INT32_MAX. 111*0a6a1f1dSLionel Sambuc #if defined(__INTERIX) || defined(_SCO_DS) 1125a645f22SBen Gras # undef SIZE_MAX 1135a645f22SBen Gras #endif 1145a645f22SBen Gras 1155a645f22SBen Gras // The code currently assumes that size_t is either 32-bit or 64-bit. 1165a645f22SBen Gras #ifndef SIZE_MAX 1175a645f22SBen Gras # if SIZEOF_SIZE_T == 4 1185a645f22SBen Gras # define SIZE_MAX UINT32_MAX 1195a645f22SBen Gras # elif SIZEOF_SIZE_T == 8 1205a645f22SBen Gras # define SIZE_MAX UINT64_MAX 1215a645f22SBen Gras # else 1225a645f22SBen Gras # error size_t is not 32-bit or 64-bit 1235a645f22SBen Gras # endif 1245a645f22SBen Gras #endif 1255a645f22SBen Gras #if SIZE_MAX != UINT32_MAX && SIZE_MAX != UINT64_MAX 1265a645f22SBen Gras # error size_t is not 32-bit or 64-bit 1275a645f22SBen Gras #endif 1285a645f22SBen Gras 1295a645f22SBen Gras #include <stdlib.h> 1305a645f22SBen Gras #include <assert.h> 1315a645f22SBen Gras 1325a645f22SBen Gras // Pre-C99 systems lack stdbool.h. All the code in LZMA Utils must be written 1335a645f22SBen Gras // so that it works with fake bool type, for example: 1345a645f22SBen Gras // 1355a645f22SBen Gras // bool foo = (flags & 0x100) != 0; 1365a645f22SBen Gras // bool bar = !!(flags & 0x100); 1375a645f22SBen Gras // 1385a645f22SBen Gras // This works with the real C99 bool but breaks with fake bool: 1395a645f22SBen Gras // 1405a645f22SBen Gras // bool baz = (flags & 0x100); 1415a645f22SBen Gras // 1425a645f22SBen Gras #ifdef HAVE_STDBOOL_H 1435a645f22SBen Gras # include <stdbool.h> 1445a645f22SBen Gras #else 1455a645f22SBen Gras # if ! HAVE__BOOL 1465a645f22SBen Gras typedef unsigned char _Bool; 1475a645f22SBen Gras # endif 1485a645f22SBen Gras # define bool _Bool 1495a645f22SBen Gras # define false 0 1505a645f22SBen Gras # define true 1 1515a645f22SBen Gras # define __bool_true_false_are_defined 1 1525a645f22SBen Gras #endif 1535a645f22SBen Gras 1545a645f22SBen Gras // string.h should be enough but let's include strings.h and memory.h too if 1555a645f22SBen Gras // they exists, since that shouldn't do any harm, but may improve portability. 1565a645f22SBen Gras #ifdef HAVE_STRING_H 1575a645f22SBen Gras # include <string.h> 1585a645f22SBen Gras #endif 1595a645f22SBen Gras 1605a645f22SBen Gras #ifdef HAVE_STRINGS_H 1615a645f22SBen Gras # include <strings.h> 1625a645f22SBen Gras #endif 1635a645f22SBen Gras 1645a645f22SBen Gras #ifdef HAVE_MEMORY_H 1655a645f22SBen Gras # include <memory.h> 1665a645f22SBen Gras #endif 1675a645f22SBen Gras 168*0a6a1f1dSLionel Sambuc // As of MSVC 2013, inline and restrict are supported with 169*0a6a1f1dSLionel Sambuc // non-standard keywords. 170*0a6a1f1dSLionel Sambuc #if defined(_WIN32) && defined(_MSC_VER) 171*0a6a1f1dSLionel Sambuc # ifndef inline 172*0a6a1f1dSLionel Sambuc # define inline __inline 173*0a6a1f1dSLionel Sambuc # endif 174*0a6a1f1dSLionel Sambuc # ifndef restrict 175*0a6a1f1dSLionel Sambuc # define restrict __restrict 176*0a6a1f1dSLionel Sambuc # endif 177*0a6a1f1dSLionel Sambuc #endif 1785a645f22SBen Gras 1795a645f22SBen Gras //////////// 1805a645f22SBen Gras // Macros // 1815a645f22SBen Gras //////////// 1825a645f22SBen Gras 1835a645f22SBen Gras #undef memzero 1845a645f22SBen Gras #define memzero(s, n) memset(s, 0, n) 1855a645f22SBen Gras 1865a645f22SBen Gras // NOTE: Avoid using MIN() and MAX(), because even conditionally defining 1875a645f22SBen Gras // those macros can cause some portability trouble, since on some systems 1885a645f22SBen Gras // the system headers insist defining their own versions. 1895a645f22SBen Gras #define my_min(x, y) ((x) < (y) ? (x) : (y)) 1905a645f22SBen Gras #define my_max(x, y) ((x) > (y) ? (x) : (y)) 1915a645f22SBen Gras 1925a645f22SBen Gras #ifndef ARRAY_SIZE 1935a645f22SBen Gras # define ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0])) 1945a645f22SBen Gras #endif 1955a645f22SBen Gras 19611be35a1SLionel Sambuc #if (__GNUC__ == 4 && __GNUC_MINOR__ >= 3) || __GNUC__ > 4 19711be35a1SLionel Sambuc # define lzma_attr_alloc_size(x) __attribute__((__alloc_size__(x))) 19811be35a1SLionel Sambuc #else 19911be35a1SLionel Sambuc # define lzma_attr_alloc_size(x) 20011be35a1SLionel Sambuc #endif 20111be35a1SLionel Sambuc 2025a645f22SBen Gras #endif 203