From 37aa137ec9e10dfa78283062a485e7e210b4ea07 Mon Sep 17 00:00:00 2001 From: david Date: Wed, 27 Nov 2013 20:38:14 +2000 Subject: [PATCH] [PATCH v2] util: detect byte order --- d9/7e6f2404c0793c1a1b9a12f4bb139f562459d5 | 209 ++++++++++++++++++++++ 1 file changed, 209 insertions(+) create mode 100644 d9/7e6f2404c0793c1a1b9a12f4bb139f562459d5 diff --git a/d9/7e6f2404c0793c1a1b9a12f4bb139f562459d5 b/d9/7e6f2404c0793c1a1b9a12f4bb139f562459d5 new file mode 100644 index 000000000..d37647c08 --- /dev/null +++ b/d9/7e6f2404c0793c1a1b9a12f4bb139f562459d5 @@ -0,0 +1,209 @@ +Return-Path: +X-Original-To: notmuch@notmuchmail.org +Delivered-To: notmuch@notmuchmail.org +Received: from localhost (localhost [127.0.0.1]) + by olra.theworths.org (Postfix) with ESMTP id 497DD431FD9 + for ; Tue, 26 Nov 2013 16:39:50 -0800 (PST) +X-Virus-Scanned: Debian amavisd-new at olra.theworths.org +X-Spam-Flag: NO +X-Spam-Score: 0 +X-Spam-Level: +X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[none] + autolearn=disabled +Received: from olra.theworths.org ([127.0.0.1]) + by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) + with ESMTP id Hoyt0peC-cq9 for ; + Tue, 26 Nov 2013 16:39:44 -0800 (PST) +Received: from yantan.tethera.net (yantan.tethera.net [199.188.72.155]) + (using TLSv1 with cipher DHE-RSA-AES128-SHA (128/128 bits)) + (No client certificate requested) + by olra.theworths.org (Postfix) with ESMTPS id BA3AD431FAE + for ; Tue, 26 Nov 2013 16:39:44 -0800 (PST) +Received: from remotemail by yantan.tethera.net with local (Exim 4.80) + (envelope-from ) + id 1VlTAC-0004bE-BX; Tue, 26 Nov 2013 20:39:32 -0400 +Received: (nullmailer pid 32577 invoked by uid 1000); Wed, 27 Nov 2013 + 00:39:28 -0000 +From: david@tethera.net +To: notmuch@notmuchmail.org +Subject: [PATCH v2] util: detect byte order +Date: Tue, 26 Nov 2013 20:38:14 -0400 +Message-Id: <1385512694-32526-1-git-send-email-david@tethera.net> +X-Mailer: git-send-email 1.8.4.2 +In-Reply-To: +References: +X-BeenThere: notmuch@notmuchmail.org +X-Mailman-Version: 2.1.13 +Precedence: list +List-Id: "Use and development of the notmuch mail system." + +List-Unsubscribe: , + +List-Archive: +List-Post: +List-Help: +List-Subscribe: , + +X-List-Received-Date: Wed, 27 Nov 2013 00:39:50 -0000 + +From: David Bremner + +Unfortunately old versions of GCC and clang do not provide byte order +macros, so we re-invent them. + +If UTIL_BYTE_ORDER is not defined or defined to 0, we fall back to +macros supported by recent versions of GCC and clang +--- + +I think I got all of Tomi's comments, including the most serious one +about the test in endian-utils.h (LITTLE twice instead of BIG, +LITTLE). + + configure | 24 ++++++++++++++++++++++-- + lib/libsha1.c | 21 +++++++-------------- + util/endian-util.h | 38 ++++++++++++++++++++++++++++++++++++++ + 3 files changed, 67 insertions(+), 16 deletions(-) + create mode 100644 util/endian-util.h + +diff --git a/configure b/configure +index 1a8e939..a0c6771 100755 +--- a/configure ++++ b/configure +@@ -441,6 +441,19 @@ else + EOF + fi + ++printf "Checking byte order... " ++cat> _byteorder.c < ++#include ++uint32_t test = 0x34333231; ++int main() { printf("%.4s\n", (const char*)&test); return 0; } ++EOF ++${CC} ${CFLAGS} _byteorder.c -o _byteorder > /dev/null 2>&1 ++util_byte_order=$(./_byteorder) ++echo $util_byte_order ++ ++#rm -f _byteorder _byteorder.c ++ + if [ $errors -gt 0 ]; then + cat < /* for memcpy() etc. */ +- ++#include "endian-util.h" + #include "libsha1.h" + + #if defined(__cplusplus) +@@ -49,20 +49,13 @@ extern "C" + + #define bswap_32(x) ((rotr32((x), 24) & 0x00ff00ff) | (rotr32((x), 8) & 0xff00ff00)) + +-/* The macros __BYTE_ORDER__ and __ORDER_*_ENDIAN__ are GNU C +- * extensions. They are also supported by clang as of v3.2 */ +- +-#ifdef __BYTE_ORDER__ +-# if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) +-# define bsw_32(p,n) \ +- { int _i = (n); while(_i--) ((uint32_t*)p)[_i] = bswap_32(((uint32_t*)p)[_i]); } +-# elif (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) +-# define bsw_32(p,n) +-# else +-# error "unknown byte order" +-# endif ++#if (UTIL_BYTE_ORDER == UTIL_ORDER_LITTLE_ENDIAN) ++# define bsw_32(p,n) \ ++ { int _i = (n); while(_i--) ((uint32_t*)p)[_i] = bswap_32(((uint32_t*)p)[_i]); } ++#elif (UTIL_BYTE_ORDER == UTIL_ORDER_BIG_ENDIAN) ++# define bsw_32(p,n) + #else +-# error "macro __BYTE_ORDER__ is not defined" ++# error "Unsupported byte order" + #endif + + #define SHA1_MASK (SHA1_BLOCK_SIZE - 1) +diff --git a/util/endian-util.h b/util/endian-util.h +new file mode 100644 +index 0000000..bc80c40 +--- /dev/null ++++ b/util/endian-util.h +@@ -0,0 +1,38 @@ ++/* this file mimics the macros present in recent GCC and CLANG */ ++ ++#ifndef _ENDIAN_UTIL_H ++#define _ENDIAN_UTIL_H ++ ++/* This are prefixed with UTIL to avoid collisions ++ * ++ * You can use something like the following to define UTIL_BYTE_ORDER ++ * in a configure script. ++ */ ++#if 0 ++#include ++#include ++uint32_t test = 0x34333231; ++int main() { printf("%.4s\n", (const char*)&test); return 0; } ++#endif ++ ++#define UTIL_ORDER_BIG_ENDIAN 4321 ++#define UTIL_ORDER_LITTLE_ENDIAN 1234 ++ ++ ++#if !defined(UTIL_BYTE_ORDER) || ((UTIL_BYTE_ORDER != UTIL_ORDER_BIG_ENDIAN) && \ ++ (UTIL_BYTE_ORDER != UTIL_ORDER_LITTLE_ENDIAN)) ++#undef UTIL_BYTE_ORDER ++#ifdef __BYTE_ORDER__ ++# if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) ++# define UTIL_BYTE_ORDER UTIL_ORDER_LITTLE_ENDIAN ++# elif (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) ++# define UTIL_BYTE_ORDER UTIL_ORDER_BIG_ENDIAN ++# else ++# error "Unsupported __BYTE_ORDER__" ++# endif ++#else ++# error "UTIL_BYTE_ORDER not correctly defined and __BYTE_ORDER__ not defined." ++#endif ++#endif ++ ++#endif +-- +1.8.4.2 + -- 2.26.2