aboutsummaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
Diffstat (limited to 'src/common')
-rw-r--r--src/common/integer.h26
-rw-r--r--src/common/sysdefs.h42
2 files changed, 51 insertions, 17 deletions
diff --git a/src/common/integer.h b/src/common/integer.h
index 136a0f8d..a6e43be2 100644
--- a/src/common/integer.h
+++ b/src/common/integer.h
@@ -15,7 +15,7 @@
#define LZMA_INTEGER_H
// I'm aware of AC_CHECK_ALIGNED_ACCESS_REQUIRED from Autoconf archive, but
-// it's not useful for us. We don't care if unaligned access is supported,
+// it's not useful here. We don't care if unaligned access is supported,
// we care if it is fast. Some systems can emulate unaligned access in
// software, which is horribly slow; we want to use byte-by-byte access on
// such systems but the Autoconf test would detect such a system as
@@ -32,13 +32,13 @@
// that also allow unaligned access. Inline assembler could be OK for that.
#ifdef WORDS_BIGENDIAN
# include "bswap.h"
-# define integer_convert_16(n) bswap_16(n)
-# define integer_convert_32(n) bswap_32(n)
-# define integer_convert_64(n) bswap_64(n)
+# define integer_le_16(n) bswap_16(n)
+# define integer_le_32(n) bswap_32(n)
+# define integer_le_64(n) bswap_64(n)
#else
-# define integer_convert_16(n) (n)
-# define integer_convert_32(n) (n)
-# define integer_convert_64(n) (n)
+# define integer_le_16(n) (n)
+# define integer_le_32(n) (n)
+# define integer_le_64(n) (n)
#endif
@@ -46,7 +46,7 @@ static inline uint16_t
integer_read_16(const uint8_t buf[static 2])
{
uint16_t ret = *(const uint16_t *)(buf);
- return integer_convert_16(ret);
+ return integer_le_16(ret);
}
@@ -54,7 +54,7 @@ static inline uint32_t
integer_read_32(const uint8_t buf[static 4])
{
uint32_t ret = *(const uint32_t *)(buf);
- return integer_convert_32(ret);
+ return integer_le_32(ret);
}
@@ -63,7 +63,7 @@ static inline uint64_t
integer_read_64(const uint8_t buf[static 8])
{
uint64_t ret = *(const uint64_t *)(buf);
- return integer_convert_64(ret);
+ return integer_le_64(ret);
}
*/
@@ -71,14 +71,14 @@ integer_read_64(const uint8_t buf[static 8])
static inline void
integer_write_16(uint8_t buf[static 2], uint16_t num)
{
- *(uint16_t *)(buf) = integer_convert_16(num);
+ *(uint16_t *)(buf) = integer_le_16(num);
}
static inline void
integer_write_32(uint8_t buf[static 4], uint32_t num)
{
- *(uint32_t *)(buf) = integer_convert_32(num);
+ *(uint32_t *)(buf) = integer_le_32(num);
}
@@ -86,7 +86,7 @@ integer_write_32(uint8_t buf[static 4], uint32_t num)
static inline void
integer_write_64(uint8_t buf[static 8], uint64_t num)
{
- *(uint64_t *)(buf) = integer_convert_64(num);
+ *(uint64_t *)(buf) = integer_le_64(num);
}
*/
diff --git a/src/common/sysdefs.h b/src/common/sysdefs.h
index 2c7fb6ff..7f935f67 100644
--- a/src/common/sysdefs.h
+++ b/src/common/sysdefs.h
@@ -31,12 +31,21 @@
# include <config.h>
#endif
-#include <sys/types.h>
+// size_t and NULL
+#include <stddef.h>
#ifdef HAVE_INTTYPES_H
# include <inttypes.h>
#endif
+// 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.
#ifdef HAVE_LIMITS_H
# include <limits.h>
#endif
@@ -44,7 +53,12 @@
// 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.
+// Note that this duplicates some code from lzma.h, but this is better since
+// we can work without inttypes.h thanks to Autoconf tests.
#ifndef UINT32_C
+# if UINT_MAX != 4294967295U
+# error UINT32_C is not defined and unsiged int is not 32-bit.
+# endif
# define UINT32_C(n) n ## U
#endif
#ifndef UINT32_MAX
@@ -56,7 +70,8 @@
#ifndef PRIX32
# define PRIX32 "X"
#endif
-#if SIZEOF_UNSIGNED_LONG == 4
+
+#if ULONG_MAX == 4294967295UL
# ifndef UINT64_C
# define UINT64_C(n) n ## ULL
# endif
@@ -80,16 +95,33 @@
#ifndef UINT64_MAX
# define UINT64_MAX UINT64_C(18446744073709551615)
#endif
+
+// The code currently assumes that size_t is either 32-bit or 64-bit.
#ifndef SIZE_MAX
# if SIZEOF_SIZE_T == 4
# define SIZE_MAX UINT32_MAX
-# else
+# elif SIZEOF_SIZE_T == 8
# define SIZE_MAX UINT64_MAX
+# else
+# error sizeof(size_t) is not 32-bit or 64-bit
# endif
#endif
+#if SIZE_MAX != UINT32_MAX && SIZE_MAX != UINT64_MAX
+# error sizeof(size_t) is not 32-bit or 64-bit
+#endif
#include <stdlib.h>
+// Pre-C99 systems lack stdbool.h. All the code in LZMA Utils must be written
+// 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);
+//
#ifdef HAVE_STDBOOL_H
# include <stdbool.h>
#else
@@ -108,11 +140,13 @@ typedef unsigned char _Bool;
# ifdef NDEBUG
# define assert(x)
# else
- // TODO: Pretty bad assert() macro.
+ // TODO: Pretty bad assert macro.
# define assert(x) (!(x) && abort())
# endif
#endif
+// string.h should be enough but let's include strings.h and memory.h too if
+// they exists, since that shouldn't do any harm, but may improve portability.
#ifdef HAVE_STRING_H
# include <string.h>
#endif