From f67fe8bb658fbc19df76128e1012580cf135da78 Mon Sep 17 00:00:00 2001 From: Christos Zoulas Date: Wed, 20 Oct 2021 13:56:15 +0000 Subject: [PATCH] Use the system byte swapping functions if available (Werner Fink) --- ChangeLog | 4 ++++ configure.ac | 4 ++-- src/apprentice.c | 28 ++++++++++++++++++++++++---- src/cdf.c | 18 +++++++++++++++++- 4 files changed, 47 insertions(+), 7 deletions(-) diff --git ChangeLog ChangeLog index 0bb334c9..0be9711b 100644 --- ChangeLog +++ ChangeLog @@ -1,3 +1,7 @@ +2021-10-20 9:55 Christos Zoulas + + * use the system byte swapping functions if available (Werner Fink) + 2021-10-18 11:57 Christos Zoulas * release 5.41 diff --git configure.ac configure.ac index ccc57e13..528bb40d 100644 --- configure.ac +++ configure.ac @@ -99,10 +99,10 @@ gl_VISIBILITY dnl Checks for headers AC_HEADER_MAJOR AC_HEADER_SYS_WAIT -AC_CHECK_HEADERS(stdint.h fcntl.h inttypes.h unistd.h) +AC_CHECK_HEADERS(stdint.h fcntl.h inttypes.h unistd.h byteswap.h) AC_CHECK_HEADERS(utime.h wchar.h wctype.h) AC_CHECK_HEADERS(getopt.h err.h xlocale.h) -AC_CHECK_HEADERS(sys/mman.h sys/stat.h sys/types.h sys/utime.h sys/time.h sys/sysmacros.h) +AC_CHECK_HEADERS(sys/bswap.h sys/mman.h sys/stat.h sys/types.h sys/utime.h sys/time.h sys/sysmacros.h) if test "$enable_zlib" != "no"; then AC_CHECK_HEADERS(zlib.h) fi diff --git src/apprentice.c src/apprentice.c index eb3b4a59..8f51b922 100644 --- src/apprentice.c +++ src/apprentice.c @@ -32,7 +32,7 @@ #include "file.h" #ifndef lint -FILE_RCSID("@(#)$File: apprentice.c,v 1.309 2021/09/24 13:59:19 christos Exp $") +FILE_RCSID("@(#)$File: apprentice.c,v 1.310 2021/10/20 13:56:15 christos Exp $") #endif /* lint */ #include "magic.h" @@ -50,6 +50,12 @@ FILE_RCSID("@(#)$File: apprentice.c,v 1.309 2021/09/24 13:59:19 christos Exp $") #endif #include #include +#ifdef HAVE_BYTESWAP_H +#include +#endif +#ifdef HAVE_SYS_BSWAP_H +#include +#endif #define EATAB {while (isascii(CAST(unsigned char, *l)) && \ @@ -124,9 +130,21 @@ private void mlist_free_all(struct magic_set *); private void mlist_free(struct mlist *); private void byteswap(struct magic *, uint32_t); private void bs1(struct magic *); + +#if defined(HAVE_BYTESWAP_H) +#define swap2(x) bswap_16(x) +#define swap4(x) bswap_32(x) +#define swap8(x) bswap_64(x) +#elif defined(HAVE_SYS_BSWAP_H) +#define swap2(x) bswap16(x) +#define swap4(x) bswap32(x) +#define swap8(x) bswap64(x) +#else private uint16_t swap2(uint16_t); private uint32_t swap4(uint32_t); private uint64_t swap8(uint64_t); +#endif + private char *mkdbname(struct magic_set *, const char *, int); private struct magic_map *apprentice_buf(struct magic_set *, struct magic *, size_t); @@ -3355,6 +3373,7 @@ byteswap(struct magic *magic, uint32_t nmagic) bs1(&magic[i]); } +#if !defined(HAVE_BYTESWAP_H) && !defined(HAVE_SYS_BSWAP_H) /* * swap a short */ @@ -3394,7 +3413,7 @@ swap8(uint64_t sv) uint64_t rv; uint8_t *s = RCAST(uint8_t *, RCAST(void *, &sv)); uint8_t *d = RCAST(uint8_t *, RCAST(void *, &rv)); -#if 0 +# if 0 d[0] = s[3]; d[1] = s[2]; d[2] = s[1]; @@ -3403,7 +3422,7 @@ swap8(uint64_t sv) d[5] = s[6]; d[6] = s[5]; d[7] = s[4]; -#else +# else d[0] = s[7]; d[1] = s[6]; d[2] = s[5]; @@ -3412,9 +3431,10 @@ swap8(uint64_t sv) d[5] = s[2]; d[6] = s[1]; d[7] = s[0]; -#endif +# endif return rv; } +#endif protected uintmax_t file_varint2uintmax_t(const unsigned char *us, int t, size_t *l) diff --git src/cdf.c src/cdf.c index b5ad2f6a..874a6ed3 100644 --- src/cdf.c +++ src/cdf.c @@ -35,7 +35,7 @@ #include "file.h" #ifndef lint -FILE_RCSID("@(#)$File: cdf.c,v 1.120 2021/09/24 13:59:19 christos Exp $") +FILE_RCSID("@(#)$File: cdf.c,v 1.121 2021/10/20 13:56:15 christos Exp $") #endif #include @@ -48,6 +48,12 @@ FILE_RCSID("@(#)$File: cdf.c,v 1.120 2021/09/24 13:59:19 christos Exp $") #include #include #include +#ifdef HAVE_BYTESWAP_H +#include +#endif +#ifdef HAVE_SYS_BSWAP_H +#include +#endif #ifndef EFTYPE #define EFTYPE EINVAL @@ -124,6 +130,15 @@ cdf_calloc(const char *file __attribute__((__unused__)), return calloc(n, u); } +#if defined(HAVE_BYTESWAP_H) +# define _cdf_tole2(x) bswap_16(x) +# define _cdf_tole4(x) bswap_32(x) +# define _cdf_tole8(x) bswap_64(x) +#elif defined(HAVE_SYS_BSWAP_H) +# define _cdf_tole2(x) bswap16(x) +# define _cdf_tole4(x) bswap32(x) +# define _cdf_tole8(x) bswap64(x) +#else /* * swap a short */ @@ -173,6 +188,7 @@ _cdf_tole8(uint64_t sv) d[7] = s[0]; return rv; } +#endif /* * grab a uint32_t from a possibly unaligned address, and return it in -- 2.26.2