[Forensics-changes] [yara] 97/192: Fix issues with __builtin_bswapXX functions not defined by all compilers

Hilko Bengen bengen at moszumanska.debian.org
Sat Jul 1 10:31:52 UTC 2017


This is an automated email from the git hooks/post-receive script.

bengen pushed a commit to annotated tag v3.6.0
in repository yara.

commit 764f33dd04135f8d3f9428c8f9bed213494d538f
Author: plusvic <plusvic at gmail.com>
Date:   Tue Feb 7 11:34:49 2017 +0100

    Fix issues with __builtin_bswapXX functions not defined by all compilers
---
 libyara/Makefile.am                         |  1 +
 libyara/{include/yara/endian.h => endian.c} | 60 ++++++++++++-----------------
 libyara/include/yara/endian.h               | 27 +++++++++++--
 3 files changed, 49 insertions(+), 39 deletions(-)

diff --git a/libyara/Makefile.am b/libyara/Makefile.am
index 43e8a75..5532ca4 100644
--- a/libyara/Makefile.am
+++ b/libyara/Makefile.am
@@ -74,6 +74,7 @@ libyara_la_SOURCES = \
   arena.c \
   atoms.c \
   compiler.c \
+  endian.c \
   exec.c \
   exefiles.c \
   exefiles.h \
diff --git a/libyara/include/yara/endian.h b/libyara/endian.c
similarity index 61%
copy from libyara/include/yara/endian.h
copy to libyara/endian.c
index 06d8c14..02fff63 100644
--- a/libyara/include/yara/endian.h
+++ b/libyara/endian.c
@@ -1,5 +1,5 @@
 /*
-Copyright (c) 2016. The YARA Authors. All Rights Reserved.
+Copyright (c) 2017. The YARA Authors. All Rights Reserved.
 
 Redistribution and use in source and binary forms, with or without modification,
 are permitted provided that the following conditions are met:
@@ -27,37 +27,27 @@ ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
 
-#ifndef YR_ENDIAN_H
-#define YR_ENDIAN_H
-
-#include <config.h>
-
-#if defined(__GNUC__)
-#define yr_bswap16(x) __builtin_bswap16(x)
-#define yr_bswap32(x) __builtin_bswap32(x)
-#define yr_bswap64(x) __builtin_bswap64(x)
-#elif defined(_MSC_VER)
-#define yr_bswap16(x) _byteswap_ushort(x)
-#define yr_bswap32(x) _byteswap_ulong(x)
-#define yr_bswap64(x) _byteswap_uint64(x)
-#else
-#error Unknown compiler: Add yr_bswap* definitions
-#endif
-
-#if defined(WORDS_BIGENDIAN)
-#define yr_le16toh(x) yr_bswap16(x)
-#define yr_le32toh(x) yr_bswap32(x)
-#define yr_le64toh(x) yr_bswap64(x)
-#define yr_be16toh(x) (x)
-#define yr_be32toh(x) (x)
-#define yr_be64toh(x) (x)
-#else
-#define yr_le16toh(x) (x)
-#define yr_le32toh(x) (x)
-#define yr_le64toh(x) (x)
-#define yr_be16toh(x) yr_bswap16(x)
-#define yr_be32toh(x) yr_bswap32(x)
-#define yr_be64toh(x) yr_bswap64(x)
-#endif
-
-#endif
+#include <yara/endian.h>
+
+uint16_t _yr_bswap16(uint16_t x)
+{
+  return (x >> 8 | x << 8);
+}
+
+uint32_t _yr_bswap32(uint32_t x)
+{
+  return ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >>  8) |
+          (((x) & 0x0000ff00) <<  8) | (((x) & 0x000000ff) << 24));
+}
+
+uint64_t _yr_bswap64(uint64_t x)
+{
+  return ((((x) & 0xff00000000000000ull) >> 56)
+        | (((x) & 0x00ff000000000000ull) >> 40)
+        | (((x) & 0x0000ff0000000000ull) >> 24)
+        | (((x) & 0x000000ff00000000ull) >> 8)
+        | (((x) & 0x00000000ff000000ull) << 8)
+        | (((x) & 0x0000000000ff0000ull) << 24)
+        | (((x) & 0x000000000000ff00ull) << 40)
+        | (((x) & 0x00000000000000ffull) << 56));
+}
diff --git a/libyara/include/yara/endian.h b/libyara/include/yara/endian.h
index 06d8c14..b4d083c 100644
--- a/libyara/include/yara/endian.h
+++ b/libyara/include/yara/endian.h
@@ -30,20 +30,39 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 #ifndef YR_ENDIAN_H
 #define YR_ENDIAN_H
 
+#include <stdint.h>
 #include <config.h>
 
-#if defined(__GNUC__)
+#define GCC_48 (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
+
+#if (defined(__has_builtin) && __has_builtin(__builtin_bswap16)) || GCC_48
 #define yr_bswap16(x) __builtin_bswap16(x)
-#define yr_bswap32(x) __builtin_bswap32(x)
-#define yr_bswap64(x) __builtin_bswap64(x)
 #elif defined(_MSC_VER)
 #define yr_bswap16(x) _byteswap_ushort(x)
+#else
+uint16_t _yr_bswap16(uint16_t x);
+#define yr_bswap16(x) _yr_bswap16(x)
+#endif
+
+#if (defined(__has_builtin) && __has_builtin(__builtin_bswap32)) || GCC_48
+#define yr_bswap32(x) __builtin_bswap32(x)
+#elif defined(_MSC_VER)
 #define yr_bswap32(x) _byteswap_ulong(x)
+#else
+uint32_t _yr_bswap32(uint32_t x);
+#define yr_bswap32(x) _yr_bswap32(x)
+#endif
+
+#if (defined(__has_builtin) && __has_builtin(__builtin_bswap64)) || GCC_48
+#define yr_bswap64(x) __builtin_bswap32(x)
+#elif defined(_MSC_VER)
 #define yr_bswap64(x) _byteswap_uint64(x)
 #else
-#error Unknown compiler: Add yr_bswap* definitions
+uint64_t _yr_bswap64(uint64_t x);
+#define yr_bswap64(x) _yr_bswap64(x)
 #endif
 
+
 #if defined(WORDS_BIGENDIAN)
 #define yr_le16toh(x) yr_bswap16(x)
 #define yr_le32toh(x) yr_bswap32(x)

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/forensics/yara.git



More information about the forensics-changes mailing list