gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] [libmicrohttpd] branch master updated (78a90394 -> f5970c78


From: gnunet
Subject: [GNUnet-SVN] [libmicrohttpd] branch master updated (78a90394 -> f5970c78)
Date: Fri, 19 Apr 2019 21:36:29 +0200

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

karlson2k pushed a change to branch master
in repository libmicrohttpd.

    from 78a90394 sha256_update(): added shortcut for empty data, avoid 
wrong/undefined behaviour of memcpy() with null pointer
     new 9998ca76 sha256.c: convert to Yoda conditions
     new a50b4d56 mhd_bithelpers.h: use bytes swap instead of individual bytes 
assignment when endianess is known to significantly speedup MD5 and SHA256 
calculations. Use built-in bytes swap when available.
     new f5970c78 mhd_bithelpers.h: define _MHD_GET_32BIT_LE() and use it in 
md5.c

The 3 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 configure.ac                    | 14 ++++++
 src/microhttpd/md5.c            | 18 +++++---
 src/microhttpd/mhd_bithelpers.h | 97 ++++++++++++++++++++++++++++++++++-------
 src/microhttpd/sha256.c         |  6 +--
 4 files changed, 110 insertions(+), 25 deletions(-)

diff --git a/configure.ac b/configure.ac
index 62f5ed5e..c977f746 100644
--- a/configure.ac
+++ b/configure.ac
@@ -662,6 +662,20 @@ AX_CHECK_LINK_FLAG([-fno-strict-aliasing],
 
 AC_C_BIGENDIAN
 AC_C_VARARRAYS
+AC_CACHE_CHECK([[whether __builtin_bswap32() is available]],
+  [[mhd_cv_func___builtin_bswap32_avail]], [dnl
+  AC_TRY_LINK([#include<stdint.h>],[uint32_t a = 1; uint32_t b = 
__builtin_bswap32(a); a = b;],
+    [[mhd_cv_func___builtin_bswap32_avail="yes"]], 
[[mhd_cv_func___builtin_bswap32_avail="no"]])
+])
+AS_IF([[test "x$mhd_cv_func___builtin_bswap32_avail" = "xyes"]],
+  [AC_DEFINE([[MHD_HAVE___BUILTIN_BSWAP32]], [[1]], [Define to 1 if you have 
__builtin_bswap32() builtin function])])
+AC_CACHE_CHECK([[whether __builtin_bswap64() is available]],
+  [[mhd_cv_func___builtin_bswap64_avail]], [dnl
+  AC_TRY_LINK([#include<stdint.h>],[uint64_t a = 1; uint32_t b = 
__builtin_bswap64(a); a = b;],
+    [[mhd_cv_func___builtin_bswap64_avail="yes"]], 
[[mhd_cv_func___builtin_bswap64_avail="no"]])
+])
+AS_IF([[test "x$mhd_cv_func___builtin_bswap64_avail" = "xyes"]],
+  [AC_DEFINE([[MHD_HAVE___BUILTIN_BSWAP64]], [[1]], [Define to 1 if you have 
__builtin_bswap64() builtin function])])
 
 AC_CHECK_PROG([HAVE_CURL_BINARY],[curl],[yes],[no])
 AM_CONDITIONAL([HAVE_CURL_BINARY],[test "x$HAVE_CURL_BINARY" = "xyes"])
diff --git a/src/microhttpd/md5.c b/src/microhttpd/md5.c
index 7838ac10..3f239f2f 100644
--- a/src/microhttpd/md5.c
+++ b/src/microhttpd/md5.c
@@ -96,6 +96,12 @@ MD5Final (void *ctx_,
 }
 
 
+/**
+ * Number of bytes in single SHA-256 word
+ * used to process data
+ */
+#define MD5_BYTES_IN_WORD (32 / 8)
+
 /* The four core functions - F1 is optimized somewhat */
 
 /* #define F1(x, y, z) (x & y | ~x & z) */
@@ -122,14 +128,12 @@ MD5Transform (uint32_t state[4],
 #if _MHD_BYTE_ORDER == _MHD_LITTLE_ENDIAN
   const uint32_t *in = (const uint32_t *)block;
 #else
-  uint32_t in[MD5_BLOCK_SIZE / 4];
-  for (a = 0; a < MD5_BLOCK_SIZE / 4; a++)
+  uint32_t in[MD5_BLOCK_SIZE / MD5_BYTES_IN_WORD];
+  int i;
+
+  for (i = 0; i < MD5_BLOCK_SIZE / MD5_BYTES_IN_WORD; i++)
   {
-    in[a] = (uint32_t)(
-      (uint32_t)(block[a * 4 + 0]) |
-      (uint32_t)(block[a * 4 + 1]) << 8 |
-      (uint32_t)(block[a * 4 + 2]) << 16 |
-      (uint32_t)(block[a * 4 + 3]) << 24);
+    in[i] = _MHD_GET_32BIT_LE(block + i * MD5_BYTES_IN_WORD);
   }
 #endif
 
diff --git a/src/microhttpd/mhd_bithelpers.h b/src/microhttpd/mhd_bithelpers.h
index bf20293f..6c4f0fea 100644
--- a/src/microhttpd/mhd_bithelpers.h
+++ b/src/microhttpd/mhd_bithelpers.h
@@ -29,6 +29,34 @@
 #include "mhd_byteorder.h"
 #include <stdint.h>
 
+
+#ifdef MHD_HAVE___BUILTIN_BSWAP32
+#define _MHD_BYTES_SWAP32(value32)  \
+        ((uint32_t)__builtin_bswap32((uint32_t)value32))
+#else  /* ! MHD_HAVE___BUILTIN_BSWAP32 */
+#define _MHD_BYTES_SWAP32(value32)                              \
+   ( (((uint32_t)(value32))                           << 24) |  \
+    ((((uint32_t)(value32)) & ((uint32_t)0x0000FF00)) << 8)  |  \
+    ((((uint32_t)(value32)) & ((uint32_t)0x00FF0000)) >> 8)  |  \
+     (((uint32_t)(value32))                           >> 24) )
+#endif /* ! MHD_HAVE___BUILTIN_BSWAP32 */
+
+
+#ifdef MHD_HAVE___BUILTIN_BSWAP64
+#define _MHD_BYTES_SWAP64(value64) \
+        ((uint64_t)__builtin_bswap64((uint64_t)value64))
+#else  /* ! MHD_HAVE___BUILTIN_BSWAP64 */
+#define _MHD_BYTES_SWAP64(value64)                                     \
+  ( (((uint64_t)(value64))                                   << 56) |  \
+   ((((uint64_t)(value64)) & ((uint64_t)0x000000000000FF00)) << 40) |  \
+   ((((uint64_t)(value64)) & ((uint64_t)0x0000000000FF0000)) << 24) |  \
+   ((((uint64_t)(value64)) & ((uint64_t)0x00000000FF000000)) << 8)  |  \
+   ((((uint64_t)(value64)) & ((uint64_t)0x000000FF00000000)) >> 8)  |  \
+   ((((uint64_t)(value64)) & ((uint64_t)0x0000FF0000000000)) >> 24) |  \
+   ((((uint64_t)(value64)) & ((uint64_t)0x00FF000000000000)) >> 40) |  \
+    (((uint64_t)(value64))                                   >> 56) )
+#endif /* ! MHD_HAVE___BUILTIN_BSWAP64 */
+
 /* _MHD_PUT_64BIT_LE (addr, value64)
  * put native-endian 64-bit value64 to addr
  * in little-endian mode.
@@ -36,8 +64,12 @@
 #if _MHD_BYTE_ORDER == _MHD_LITTLE_ENDIAN
 #define _MHD_PUT_64BIT_LE(addr, value64)             \
         ((*(uint64_t*)(addr)) = (uint64_t)(value64))
-#else  /* _MHD_BYTE_ORDER != _MHD_LITTLE_ENDIAN */
-#define _MHD_PUT_64BIT_LE(addr, value64) do {                            \
+#elif _MHD_BYTE_ORDER == _MHD_BIG_ENDIAN
+#define _MHD_PUT_64BIT_LE(addr, value64)             \
+        ((*(uint64_t*)(addr)) = _MHD_BYTES_SWAP64(value64))
+#else  /* _MHD_BYTE_ORDER != _MHD_BIG_ENDIAN */
+/* Endianess was not detected or non-standard like PDP-endian */
+#define _MHD_PUT_64BIT_LE(addr, value64) do {                             \
         ((uint8_t*)(addr))[0] = (uint8_t)((uint64_t)(value64));           \
         ((uint8_t*)(addr))[1] = (uint8_t)(((uint64_t)(value64)) >> 8);    \
         ((uint8_t*)(addr))[2] = (uint8_t)(((uint64_t)(value64)) >> 16);   \
@@ -47,23 +79,46 @@
         ((uint8_t*)(addr))[6] = (uint8_t)(((uint64_t)(value64)) >> 48);   \
         ((uint8_t*)(addr))[7] = (uint8_t)(((uint64_t)(value64)) >> 56);   \
         } while (0)
-#endif /* _MHD_BYTE_ORDER != _MHD_LITTLE_ENDIAN */
+#endif /* _MHD_BYTE_ORDER != _MHD_BIG_ENDIAN */
 
 /* _MHD_PUT_32BIT_LE (addr, value32)
  * put native-endian 32-bit value32 to addr
  * in little-endian mode.
  */
 #if _MHD_BYTE_ORDER == _MHD_LITTLE_ENDIAN
-#define _MHD_PUT_32BIT_LE(addr, value32)             \
+#define _MHD_PUT_32BIT_LE(addr,value32)             \
         ((*(uint32_t*)(addr)) = (uint32_t)(value32))
-#else  /* _MHD_BYTE_ORDER != _MHD_LITTLE_ENDIAN */
-#define _MHD_PUT_32BIT_LE(addr, value32) do {                            \
+#elif _MHD_BYTE_ORDER == _MHD_BIG_ENDIAN
+#define _MHD_PUT_32BIT_LE(addr, value32)            \
+        ((*(uint32_t*)(addr)) = _MHD_BYTES_SWAP32(value32))
+#else  /* _MHD_BYTE_ORDER != _MHD_BIG_ENDIAN */
+/* Endianess was not detected or non-standard like PDP-endian */
+#define _MHD_PUT_32BIT_LE(addr, value32) do {                             \
         ((uint8_t*)(addr))[0] = (uint8_t)((uint32_t)(value32));           \
         ((uint8_t*)(addr))[1] = (uint8_t)(((uint32_t)(value32)) >> 8);    \
         ((uint8_t*)(addr))[2] = (uint8_t)(((uint32_t)(value32)) >> 16);   \
         ((uint8_t*)(addr))[3] = (uint8_t)(((uint32_t)(value32)) >> 24);   \
         } while (0)
-#endif /* _MHD_BYTE_ORDER != _MHD_LITTLE_ENDIAN */
+#endif /* _MHD_BYTE_ORDER != _MHD_BIG_ENDIAN */
+
+/* _MHD_GET_32BIT_LE (addr)
+ * get little-endian 32-bit value storied at addr
+ * and return it in native-endian mode.
+ */
+#if _MHD_BYTE_ORDER == _MHD_LITTLE_ENDIAN
+#define _MHD_GET_32BIT_LE(addr)             \
+        (*(uint32_t*)(addr))
+#elif _MHD_BYTE_ORDER == _MHD_BIG_ENDIAN
+#define _MHD_GET_32BIT_LE(addr)             \
+        _MHD_BYTES_SWAP32(*(uint32_t*)(addr))
+#else  /* _MHD_BYTE_ORDER != _MHD_BIG_ENDIAN */
+/* Endianess was not detected or non-standard like PDP-endian */
+#define _MHD_GET_32BIT_LE(addr)                       \
+        ( ( (uint32_t)(((uint8_t*)addr)[0]))        | \
+          (((uint32_t)(((uint8_t*)addr)[1])) << 8)  | \
+          (((uint32_t)(((uint8_t*)addr)[2])) << 16) | \
+          (((uint32_t)(((uint8_t*)addr)[3])) << 24) )
+#endif /* _MHD_BYTE_ORDER != _MHD_BIG_ENDIAN */
 
 
 /* _MHD_PUT_64BIT_BE (addr, value64)
@@ -73,8 +128,12 @@
 #if _MHD_BYTE_ORDER == _MHD_BIG_ENDIAN
 #define _MHD_PUT_64BIT_BE(addr, value64)             \
         ((*(uint64_t*)(addr)) = (uint64_t)(value64))
-#else  /* _MHD_BYTE_ORDER != _MHD_BIG_ENDIAN */
-#define _MHD_PUT_64BIT_BE(addr, value64) do {                            \
+#elif _MHD_BYTE_ORDER == _MHD_LITTLE_ENDIAN
+#define _MHD_PUT_64BIT_BE(addr, value64)             \
+        ((*(uint64_t*)(addr)) = _MHD_BYTES_SWAP64(value64))
+#else  /* _MHD_BYTE_ORDER != _MHD_LITTLE_ENDIAN */
+/* Endianess was not detected or non-standard like PDP-endian */
+#define _MHD_PUT_64BIT_BE(addr, value64) do {                             \
         ((uint8_t*)(addr))[7] = (uint8_t)((uint64_t)(value64));           \
         ((uint8_t*)(addr))[6] = (uint8_t)(((uint64_t)(value64)) >> 8);    \
         ((uint8_t*)(addr))[5] = (uint8_t)(((uint64_t)(value64)) >> 16);   \
@@ -84,7 +143,7 @@
         ((uint8_t*)(addr))[1] = (uint8_t)(((uint64_t)(value64)) >> 48);   \
         ((uint8_t*)(addr))[0] = (uint8_t)(((uint64_t)(value64)) >> 56);   \
         } while (0)
-#endif /* _MHD_BYTE_ORDER != _MHD_BIG_ENDIAN */
+#endif /* _MHD_BYTE_ORDER != _MHD_LITTLE_ENDIAN */
 
 /* _MHD_PUT_32BIT_BE (addr, value32)
  * put native-endian 32-bit value32 to addr
@@ -93,14 +152,18 @@
 #if _MHD_BYTE_ORDER == _MHD_BIG_ENDIAN
 #define _MHD_PUT_32BIT_BE(addr, value32)             \
         ((*(uint32_t*)(addr)) = (uint32_t)(value32))
-#else  /* _MHD_BYTE_ORDER != _MHD_BIG_ENDIAN */
-#define _MHD_PUT_32BIT_BE(addr, value32) do {                            \
+#elif _MHD_BYTE_ORDER == _MHD_LITTLE_ENDIAN
+#define _MHD_PUT_32BIT_BE(addr, value32)             \
+        ((*(uint32_t*)(addr)) = _MHD_BYTES_SWAP32(value32))
+#else  /* _MHD_BYTE_ORDER != _MHD_LITTLE_ENDIAN */
+/* Endianess was not detected or non-standard like PDP-endian */
+#define _MHD_PUT_32BIT_BE(addr, value32) do {                             \
         ((uint8_t*)(addr))[3] = (uint8_t)((uint32_t)(value32));           \
         ((uint8_t*)(addr))[2] = (uint8_t)(((uint32_t)(value32)) >> 8);    \
         ((uint8_t*)(addr))[1] = (uint8_t)(((uint32_t)(value32)) >> 16);   \
         ((uint8_t*)(addr))[0] = (uint8_t)(((uint32_t)(value32)) >> 24);   \
         } while (0)
-#endif /* _MHD_BYTE_ORDER != _MHD_BIG_ENDIAN */
+#endif /* _MHD_BYTE_ORDER != _MHD_LITTLE_ENDIAN */
 
 /* _MHD_GET_32BIT_BE (addr)
  * get big-endian 32-bit value storied at addr
@@ -109,13 +172,17 @@
 #if _MHD_BYTE_ORDER == _MHD_BIG_ENDIAN
 #define _MHD_GET_32BIT_BE(addr)             \
         (*(uint32_t*)(addr))
-#else  /* _MHD_BYTE_ORDER != _MHD_BIG_ENDIAN */
+#elif _MHD_BYTE_ORDER == _MHD_LITTLE_ENDIAN
+#define _MHD_GET_32BIT_BE(addr)             \
+        _MHD_BYTES_SWAP32(*(uint32_t*)(addr))
+#else  /* _MHD_BYTE_ORDER != _MHD_LITTLE_ENDIAN */
+/* Endianess was not detected or non-standard like PDP-endian */
 #define _MHD_GET_32BIT_BE(addr)                       \
         ( (((uint32_t)(((uint8_t*)addr)[0])) << 24) | \
           (((uint32_t)(((uint8_t*)addr)[1])) << 16) | \
           (((uint32_t)(((uint8_t*)addr)[2])) << 8)  | \
           ((uint32_t) (((uint8_t*)addr)[3])) )
-#endif /* _MHD_BYTE_ORDER != _MHD_BIG_ENDIAN */
+#endif /* _MHD_BYTE_ORDER != _MHD_LITTLE_ENDIAN */
 
 /**
  * Rotate right 32-bit value by number of bits.
diff --git a/src/microhttpd/sha256.c b/src/microhttpd/sha256.c
index e3989d57..801f920b 100644
--- a/src/microhttpd/sha256.c
+++ b/src/microhttpd/sha256.c
@@ -258,7 +258,7 @@ sha256_update (void *ctx_,
   bytes_have = (unsigned)(ctx->count & (SHA256_BLOCK_SIZE-1));
   ctx->count += length;
 
-  if (bytes_have)
+  if (0 != bytes_have)
     {
       unsigned bytes_left = SHA256_BLOCK_SIZE - bytes_have;
       if (length >= bytes_left)
@@ -274,7 +274,7 @@ sha256_update (void *ctx_,
         }
     }
 
-  while (length >= SHA256_BLOCK_SIZE)
+  while (SHA256_BLOCK_SIZE <= length)
     { /* Process any full blocks of new data directly,
          without copying to buffer. */
       sha256_transform (ctx->H, data);
@@ -282,7 +282,7 @@ sha256_update (void *ctx_,
       length -= SHA256_BLOCK_SIZE;
     }
 
-  if (length != 0)
+  if (0 != length)
     { /* Copy incomplete block of new data (if any)
          to buffer. */
       memcpy (ctx->buffer + bytes_have, data, length);

-- 
To stop receiving notification emails like this one, please contact
address@hidden



reply via email to

[Prev in Thread] Current Thread [Next in Thread]