Re: [PATCH v2] util: detect byte order
authorTomi Ollila <tomi.ollila@iki.fi>
Wed, 27 Nov 2013 08:04:24 +0000 (10:04 +0200)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:58:29 +0000 (09:58 -0800)
20/d993038e916430f62033992e4a22964f6ab363 [new file with mode: 0644]

diff --git a/20/d993038e916430f62033992e4a22964f6ab363 b/20/d993038e916430f62033992e4a22964f6ab363
new file mode 100644 (file)
index 0000000..18277aa
--- /dev/null
@@ -0,0 +1,230 @@
+Return-Path: <tomi.ollila@iki.fi>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+       by olra.theworths.org (Postfix) with ESMTP id 20B43431FC2\r
+       for <notmuch@notmuchmail.org>; Wed, 27 Nov 2013 00:04:40 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: 0\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[none]\r
+       autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+       by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+       with ESMTP id InslMOSToKUO for <notmuch@notmuchmail.org>;\r
+       Wed, 27 Nov 2013 00:04:30 -0800 (PST)\r
+Received: from guru.guru-group.fi (guru.guru-group.fi [46.183.73.34])\r
+       by olra.theworths.org (Postfix) with ESMTP id 598B7431FAE\r
+       for <notmuch@notmuchmail.org>; Wed, 27 Nov 2013 00:04:30 -0800 (PST)\r
+Received: from guru.guru-group.fi (localhost [IPv6:::1])\r
+       by guru.guru-group.fi (Postfix) with ESMTP id 76F9810005E;\r
+       Wed, 27 Nov 2013 10:04:24 +0200 (EET)\r
+From: Tomi Ollila <tomi.ollila@iki.fi>\r
+To: david@tethera.net, notmuch@notmuchmail.org\r
+Subject: Re: [PATCH v2] util: detect byte order\r
+In-Reply-To: <1385512694-32526-1-git-send-email-david@tethera.net>\r
+References: <m2txezqcuf.fsf@guru.guru-group.fi>\r
+       <1385512694-32526-1-git-send-email-david@tethera.net>\r
+User-Agent: Notmuch/0.16+175~g19e97d6 (http://notmuchmail.org) Emacs/24.3.1\r
+       (x86_64-unknown-linux-gnu)\r
+X-Face: HhBM'cA~<r"^Xv\KRN0P{vn'Y"Kd;zg_y3S[4)KSN~s?O\"QPoL\r
+       $[Xv_BD:i/F$WiEWax}R(MPS`^UaptOGD`*/=@\1lKoVa9tnrg0TW?"r7aRtgk[F\r
+       !)g;OY^,BjTbr)Np:%c_o'jj,Z\r
+Date: Wed, 27 Nov 2013 10:04:24 +0200\r
+Message-ID: <m2haaynud3.fsf@guru.guru-group.fi>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+       <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Wed, 27 Nov 2013 08:04:40 -0000\r
+\r
+On Wed, Nov 27 2013, david@tethera.net wrote:\r
+\r
+> From: David Bremner <david@tethera.net>\r
+>\r
+> Unfortunately old versions of GCC and clang do not provide byte order\r
+> macros, so we re-invent them.\r
+>\r
+> If UTIL_BYTE_ORDER is not defined or defined to 0, we fall back to\r
+> macros supported by recent versions of GCC and clang\r
+> ---\r
+>\r
+> I think I got all of Tomi's comments, including the most serious one\r
+> about the test in endian-utils.h (LITTLE twice instead of BIG,\r
+> LITTLE).\r
+\r
+Agreed. LGTM.\r
+\r
+You may want to amend the '#' out of the line\r
+\r
++#rm -f _byteorder _byteorder.c\r
+\r
+before pushing ?\r
+\r
+Tomi\r
+\r
+\r
+>\r
+>  configure          | 24 ++++++++++++++++++++++--\r
+>  lib/libsha1.c      | 21 +++++++--------------\r
+>  util/endian-util.h | 38 ++++++++++++++++++++++++++++++++++++++\r
+>  3 files changed, 67 insertions(+), 16 deletions(-)\r
+>  create mode 100644 util/endian-util.h\r
+>\r
+> diff --git a/configure b/configure\r
+> index 1a8e939..a0c6771 100755\r
+> --- a/configure\r
+> +++ b/configure\r
+> @@ -441,6 +441,19 @@ else\r
+>  EOF\r
+>  fi\r
+>  \r
+> +printf "Checking byte order... "\r
+> +cat> _byteorder.c <<EOF\r
+> +#include <stdio.h>\r
+> +#include <stdint.h>\r
+> +uint32_t test = 0x34333231;\r
+> +int main() { printf("%.4s\n", (const char*)&test); return 0; }\r
+> +EOF\r
+> +${CC} ${CFLAGS} _byteorder.c -o _byteorder > /dev/null 2>&1\r
+> +util_byte_order=$(./_byteorder)\r
+> +echo $util_byte_order\r
+> +\r
+> +#rm -f _byteorder _byteorder.c\r
+> +\r
+>  if [ $errors -gt 0 ]; then\r
+>      cat <<EOF\r
+>  \r
+> @@ -702,6 +715,9 @@ prefix = ${PREFIX}\r
+>  # LIBDIR_IN_LDCONFIG value below is still set correctly.\r
+>  libdir = ${LIBDIR:=\$(prefix)/lib}\r
+>  \r
+> +# byte order within a 32 bit word. 1234 = little, 4321 = big, 0 = guess\r
+> +UTIL_BYTE_ORDER = ${util_byte_order}\r
+> +\r
+>  # Whether libdir is in a path configured into ldconfig\r
+>  LIBDIR_IN_LDCONFIG = ${libdir_in_ldconfig}\r
+>  \r
+> @@ -807,7 +823,9 @@ CONFIGURE_CFLAGS = -DHAVE_GETLINE=\$(HAVE_GETLINE) \$(GMIME_CFLAGS)      \\\r
+>                 -DHAVE_STRSEP=\$(HAVE_STRSEP)                         \\\r
+>                 -DSTD_GETPWUID=\$(STD_GETPWUID)                       \\\r
+>                 -DSTD_ASCTIME=\$(STD_ASCTIME)                         \\\r
+> -               -DHAVE_XAPIAN_COMPACT=\$(HAVE_XAPIAN_COMPACT)\r
+> +               -DHAVE_XAPIAN_COMPACT=\$(HAVE_XAPIAN_COMPACT)         \\\r
+> +               -DUTIL_BYTE_ORDER=\$(UTIL_BYTE_ORDER)\r
+> +\r
+>  CONFIGURE_CXXFLAGS = -DHAVE_GETLINE=\$(HAVE_GETLINE) \$(GMIME_CFLAGS)    \\\r
+>                   \$(TALLOC_CFLAGS) -DHAVE_VALGRIND=\$(HAVE_VALGRIND) \\\r
+>                   \$(VALGRIND_CFLAGS) \$(XAPIAN_CXXFLAGS)             \\\r
+> @@ -815,6 +833,8 @@ CONFIGURE_CXXFLAGS = -DHAVE_GETLINE=\$(HAVE_GETLINE) \$(GMIME_CFLAGS)    \\\r
+>                   -DHAVE_STRSEP=\$(HAVE_STRSEP)                       \\\r
+>                   -DSTD_GETPWUID=\$(STD_GETPWUID)                     \\\r
+>                   -DSTD_ASCTIME=\$(STD_ASCTIME)                       \\\r
+> -                 -DHAVE_XAPIAN_COMPACT=\$(HAVE_XAPIAN_COMPACT)\r
+> +                 -DHAVE_XAPIAN_COMPACT=\$(HAVE_XAPIAN_COMPACT)       \\\r
+> +                 -DUTIL_BYTE_ORDER=\$(UTIL_BYTE_ORDER)\r
+> +\r
+>  CONFIGURE_LDFLAGS =  \$(GMIME_LDFLAGS) \$(TALLOC_LDFLAGS) \$(XAPIAN_LDFLAGS)\r
+>  EOF\r
+> diff --git a/lib/libsha1.c b/lib/libsha1.c\r
+> index 87c7c52..aaaa4eb 100644\r
+> --- a/lib/libsha1.c\r
+> +++ b/lib/libsha1.c\r
+> @@ -34,7 +34,7 @@\r
+>  */\r
+>  \r
+>  #include <string.h>     /* for memcpy() etc.        */\r
+> -\r
+> +#include "endian-util.h"\r
+>  #include "libsha1.h"\r
+>  \r
+>  #if defined(__cplusplus)\r
+> @@ -49,20 +49,13 @@ extern "C"\r
+>  \r
+>  #define bswap_32(x) ((rotr32((x), 24) & 0x00ff00ff) | (rotr32((x), 8) & 0xff00ff00))\r
+>  \r
+> -/* The macros __BYTE_ORDER__ and __ORDER_*_ENDIAN__ are GNU C\r
+> - * extensions. They are also supported by clang as of v3.2 */\r
+> -\r
+> -#ifdef __BYTE_ORDER__\r
+> -#  if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)\r
+> -#    define bsw_32(p,n) \\r
+> -       { int _i = (n); while(_i--) ((uint32_t*)p)[_i] = bswap_32(((uint32_t*)p)[_i]); }\r
+> -#  elif (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)\r
+> -#    define bsw_32(p,n)\r
+> -#  else\r
+> -#    error "unknown byte order"\r
+> -#  endif\r
+> +#if (UTIL_BYTE_ORDER == UTIL_ORDER_LITTLE_ENDIAN)\r
+> +#  define bsw_32(p,n) \\r
+> +     { int _i = (n); while(_i--) ((uint32_t*)p)[_i] = bswap_32(((uint32_t*)p)[_i]); }\r
+> +#elif (UTIL_BYTE_ORDER == UTIL_ORDER_BIG_ENDIAN)\r
+> +#  define bsw_32(p,n)\r
+>  #else\r
+> -#    error "macro __BYTE_ORDER__ is not defined"\r
+> +#  error "Unsupported byte order"\r
+>  #endif\r
+>  \r
+>  #define SHA1_MASK   (SHA1_BLOCK_SIZE - 1)\r
+> diff --git a/util/endian-util.h b/util/endian-util.h\r
+> new file mode 100644\r
+> index 0000000..bc80c40\r
+> --- /dev/null\r
+> +++ b/util/endian-util.h\r
+> @@ -0,0 +1,38 @@\r
+> +/* this file mimics the macros present in recent GCC and CLANG */\r
+> +\r
+> +#ifndef _ENDIAN_UTIL_H\r
+> +#define _ENDIAN_UTIL_H\r
+> +\r
+> +/* This are prefixed with UTIL to avoid collisions\r
+> + *\r
+> + * You can use something like the following to define UTIL_BYTE_ORDER\r
+> + * in a configure script.\r
+> + */\r
+> +#if 0\r
+> +#include <stdio.h>\r
+> +#include <stdint.h>\r
+> +uint32_t test = 0x34333231;\r
+> +int main() { printf("%.4s\n", (const char*)&test); return 0; }\r
+> +#endif\r
+> +\r
+> +#define UTIL_ORDER_BIG_ENDIAN         4321\r
+> +#define UTIL_ORDER_LITTLE_ENDIAN  1234\r
+> +\r
+> +\r
+> +#if !defined(UTIL_BYTE_ORDER) || ((UTIL_BYTE_ORDER != UTIL_ORDER_BIG_ENDIAN) && \\r
+> +                              (UTIL_BYTE_ORDER != UTIL_ORDER_LITTLE_ENDIAN))\r
+> +#undef UTIL_BYTE_ORDER\r
+> +#ifdef __BYTE_ORDER__\r
+> +#  if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)\r
+> +#    define UTIL_BYTE_ORDER UTIL_ORDER_LITTLE_ENDIAN\r
+> +#  elif (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)\r
+> +#    define UTIL_BYTE_ORDER UTIL_ORDER_BIG_ENDIAN\r
+> +#  else\r
+> +#    error "Unsupported __BYTE_ORDER__"\r
+> +#  endif\r
+> +#else\r
+> +#  error "UTIL_BYTE_ORDER not correctly defined and __BYTE_ORDER__ not defined."\r
+> +#endif\r
+> +#endif\r
+> +\r
+> +#endif\r
+> -- \r
+> 1.8.4.2\r
+>\r
+> _______________________________________________\r
+> notmuch mailing list\r
+> notmuch@notmuchmail.org\r
+> http://notmuchmail.org/mailman/listinfo/notmuch\r