2024-02-12 15:09:10 +00:00
|
|
|
// SPDX-License-Identifier: 0BSD
|
|
|
|
|
2007-12-08 22:42:33 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
/// \file sysdefs.h
|
|
|
|
/// \brief Common includes, definitions, system-specific things etc.
|
|
|
|
///
|
|
|
|
/// This file is used also by the lzma command line tool, that's why this
|
|
|
|
/// file is separate from common.h.
|
|
|
|
//
|
2009-04-13 08:27:40 +00:00
|
|
|
// Author: Lasse Collin
|
2007-12-08 22:42:33 +00:00
|
|
|
//
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#ifndef LZMA_SYSDEFS_H
|
|
|
|
#define LZMA_SYSDEFS_H
|
|
|
|
|
|
|
|
//////////////
|
|
|
|
// Includes //
|
|
|
|
//////////////
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include <config.h>
|
|
|
|
#endif
|
|
|
|
|
2023-09-24 13:32:32 +00:00
|
|
|
// This #define ensures that C99 and POSIX compliant stdio functions are
|
|
|
|
// available with MinGW-w64 (both 32-bit and 64-bit). Modern MinGW-w64 adds
|
|
|
|
// this automatically, for example, when the compiler is in C99 (or later)
|
|
|
|
// mode when building against msvcrt.dll. It still doesn't hurt to be explicit
|
|
|
|
// that we always want this and #define this unconditionally.
|
|
|
|
//
|
|
|
|
// With Universal CRT (UCRT) this is less important because UCRT contains
|
|
|
|
// C99-compatible stdio functions. It's still nice to #define this as UCRT
|
|
|
|
// doesn't support the POSIX thousand separator flag in printf (like "%'u").
|
2010-10-23 09:26:33 +00:00
|
|
|
#ifdef __MINGW32__
|
|
|
|
# define __USE_MINGW_ANSI_STDIO 1
|
|
|
|
#endif
|
|
|
|
|
2008-08-28 19:53:15 +00:00
|
|
|
// size_t and NULL
|
|
|
|
#include <stddef.h>
|
2008-01-06 14:27:41 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_INTTYPES_H
|
|
|
|
# include <inttypes.h>
|
|
|
|
#endif
|
|
|
|
|
2008-08-28 19:53:15 +00:00
|
|
|
// C99 says that inttypes.h always includes stdint.h, but some systems
|
|
|
|
// don't do that, and require including stdint.h separately.
|
|
|
|
#ifdef HAVE_STDINT_H
|
|
|
|
# include <stdint.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Some pre-C99 systems have SIZE_MAX in limits.h instead of stdint.h. The
|
|
|
|
// limits are also used to figure out some macros missing from pre-C99 systems.
|
2020-02-16 09:18:28 +00:00
|
|
|
#include <limits.h>
|
2008-01-06 14:27:41 +00:00
|
|
|
|
|
|
|
// Be more compatible with systems that have non-conforming inttypes.h.
|
|
|
|
// We assume that int is 32-bit and that long is either 32-bit or 64-bit.
|
|
|
|
// Full Autoconf test could be more correct, but this should work well enough.
|
2008-08-28 19:53:15 +00:00
|
|
|
// Note that this duplicates some code from lzma.h, but this is better since
|
|
|
|
// we can work without inttypes.h thanks to Autoconf tests.
|
2008-01-06 14:27:41 +00:00
|
|
|
#ifndef UINT32_C
|
2008-08-28 19:53:15 +00:00
|
|
|
# if UINT_MAX != 4294967295U
|
2009-11-26 08:11:23 +00:00
|
|
|
# error UINT32_C is not defined and unsigned int is not 32-bit.
|
2008-08-28 19:53:15 +00:00
|
|
|
# endif
|
2008-01-06 14:27:41 +00:00
|
|
|
# define UINT32_C(n) n ## U
|
|
|
|
#endif
|
|
|
|
#ifndef UINT32_MAX
|
|
|
|
# define UINT32_MAX UINT32_C(4294967295)
|
|
|
|
#endif
|
|
|
|
#ifndef PRIu32
|
|
|
|
# define PRIu32 "u"
|
|
|
|
#endif
|
2010-12-12 14:07:11 +00:00
|
|
|
#ifndef PRIx32
|
|
|
|
# define PRIx32 "x"
|
|
|
|
#endif
|
2008-01-06 14:27:41 +00:00
|
|
|
#ifndef PRIX32
|
|
|
|
# define PRIX32 "X"
|
|
|
|
#endif
|
2008-08-28 19:53:15 +00:00
|
|
|
|
|
|
|
#if ULONG_MAX == 4294967295UL
|
2008-01-06 14:27:41 +00:00
|
|
|
# ifndef UINT64_C
|
|
|
|
# define UINT64_C(n) n ## ULL
|
|
|
|
# endif
|
|
|
|
# ifndef PRIu64
|
|
|
|
# define PRIu64 "llu"
|
|
|
|
# endif
|
2010-12-12 14:07:11 +00:00
|
|
|
# ifndef PRIx64
|
|
|
|
# define PRIx64 "llx"
|
|
|
|
# endif
|
2008-01-06 14:27:41 +00:00
|
|
|
# ifndef PRIX64
|
|
|
|
# define PRIX64 "llX"
|
|
|
|
# endif
|
|
|
|
#else
|
|
|
|
# ifndef UINT64_C
|
|
|
|
# define UINT64_C(n) n ## UL
|
|
|
|
# endif
|
|
|
|
# ifndef PRIu64
|
|
|
|
# define PRIu64 "lu"
|
|
|
|
# endif
|
2010-12-12 14:07:11 +00:00
|
|
|
# ifndef PRIx64
|
|
|
|
# define PRIx64 "lx"
|
|
|
|
# endif
|
2008-01-06 14:27:41 +00:00
|
|
|
# ifndef PRIX64
|
|
|
|
# define PRIX64 "lX"
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
#ifndef UINT64_MAX
|
|
|
|
# define UINT64_MAX UINT64_C(18446744073709551615)
|
|
|
|
#endif
|
2008-08-28 19:53:15 +00:00
|
|
|
|
2011-08-09 18:16:44 +00:00
|
|
|
// Incorrect(?) SIZE_MAX:
|
|
|
|
// - Interix headers typedef size_t to unsigned long,
|
|
|
|
// but a few lines later define SIZE_MAX to INT32_MAX.
|
|
|
|
// - SCO OpenServer (x86) headers typedef size_t to unsigned int
|
|
|
|
// but define SIZE_MAX to INT32_MAX.
|
|
|
|
#if defined(__INTERIX) || defined(_SCO_DS)
|
2009-09-27 08:48:54 +00:00
|
|
|
# undef SIZE_MAX
|
|
|
|
#endif
|
|
|
|
|
2008-08-28 19:53:15 +00:00
|
|
|
// The code currently assumes that size_t is either 32-bit or 64-bit.
|
2008-01-06 14:27:41 +00:00
|
|
|
#ifndef SIZE_MAX
|
|
|
|
# if SIZEOF_SIZE_T == 4
|
|
|
|
# define SIZE_MAX UINT32_MAX
|
2008-08-28 19:53:15 +00:00
|
|
|
# elif SIZEOF_SIZE_T == 8
|
2008-01-06 14:27:41 +00:00
|
|
|
# define SIZE_MAX UINT64_MAX
|
2008-08-28 19:53:15 +00:00
|
|
|
# else
|
2010-05-26 07:16:57 +00:00
|
|
|
# error size_t is not 32-bit or 64-bit
|
2008-01-06 14:27:41 +00:00
|
|
|
# endif
|
|
|
|
#endif
|
2008-08-28 19:53:15 +00:00
|
|
|
#if SIZE_MAX != UINT32_MAX && SIZE_MAX != UINT64_MAX
|
2010-05-26 07:16:57 +00:00
|
|
|
# error size_t is not 32-bit or 64-bit
|
2008-08-28 19:53:15 +00:00
|
|
|
#endif
|
2007-12-08 22:42:33 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
2008-11-19 18:46:52 +00:00
|
|
|
#include <assert.h>
|
2007-12-08 22:42:33 +00:00
|
|
|
|
2023-01-10 08:05:13 +00:00
|
|
|
// Pre-C99 systems lack stdbool.h. All the code in XZ Utils must be written
|
2008-08-28 19:53:15 +00:00
|
|
|
// so that it works with fake bool type, for example:
|
|
|
|
//
|
|
|
|
// bool foo = (flags & 0x100) != 0;
|
|
|
|
// bool bar = !!(flags & 0x100);
|
|
|
|
//
|
|
|
|
// This works with the real C99 bool but breaks with fake bool:
|
|
|
|
//
|
|
|
|
// bool baz = (flags & 0x100);
|
|
|
|
//
|
2007-12-08 22:42:33 +00:00
|
|
|
#ifdef HAVE_STDBOOL_H
|
|
|
|
# include <stdbool.h>
|
|
|
|
#else
|
|
|
|
# if ! HAVE__BOOL
|
|
|
|
typedef unsigned char _Bool;
|
|
|
|
# endif
|
|
|
|
# define bool _Bool
|
|
|
|
# define false 0
|
|
|
|
# define true 1
|
|
|
|
# define __bool_true_false_are_defined 1
|
|
|
|
#endif
|
|
|
|
|
2020-02-16 09:18:28 +00:00
|
|
|
#include <string.h>
|
2007-12-08 22:42:33 +00:00
|
|
|
|
2023-09-12 20:34:31 +00:00
|
|
|
// Visual Studio 2013 update 2 supports only __inline, not inline.
|
|
|
|
// MSVC v19.0 / VS 2015 and newer support both.
|
|
|
|
//
|
|
|
|
// MSVC v19.27 (VS 2019 version 16.7) added support for restrict.
|
|
|
|
// Older ones support only __restrict.
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
# if _MSC_VER < 1900 && !defined(inline)
|
2014-01-12 15:04:33 +00:00
|
|
|
# define inline __inline
|
|
|
|
# endif
|
2023-09-12 20:34:31 +00:00
|
|
|
# if _MSC_VER < 1927 && !defined(restrict)
|
2014-01-12 15:04:33 +00:00
|
|
|
# define restrict __restrict
|
|
|
|
# endif
|
|
|
|
#endif
|
2007-12-08 22:42:33 +00:00
|
|
|
|
|
|
|
////////////
|
|
|
|
// Macros //
|
|
|
|
////////////
|
|
|
|
|
2008-05-11 11:24:42 +00:00
|
|
|
#undef memzero
|
|
|
|
#define memzero(s, n) memset(s, 0, n)
|
2007-12-08 22:42:33 +00:00
|
|
|
|
2010-05-26 07:36:46 +00:00
|
|
|
// NOTE: Avoid using MIN() and MAX(), because even conditionally defining
|
|
|
|
// those macros can cause some portability trouble, since on some systems
|
|
|
|
// the system headers insist defining their own versions.
|
|
|
|
#define my_min(x, y) ((x) < (y) ? (x) : (y))
|
|
|
|
#define my_max(x, y) ((x) > (y) ? (x) : (y))
|
2007-12-08 22:42:33 +00:00
|
|
|
|
2008-01-16 12:31:44 +00:00
|
|
|
#ifndef ARRAY_SIZE
|
|
|
|
# define ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0]))
|
|
|
|
#endif
|
|
|
|
|
2020-02-22 12:15:07 +00:00
|
|
|
#if defined(__GNUC__) \
|
|
|
|
&& ((__GNUC__ == 4 && __GNUC_MINOR__ >= 3) || __GNUC__ > 4)
|
2011-01-18 19:23:50 +00:00
|
|
|
# define lzma_attr_alloc_size(x) __attribute__((__alloc_size__(x)))
|
|
|
|
#else
|
|
|
|
# define lzma_attr_alloc_size(x)
|
|
|
|
#endif
|
|
|
|
|
2007-12-08 22:42:33 +00:00
|
|
|
#endif
|