[Bf-blender-cvs] [fb7c713] master: Code cleanup: BLI_md5.c was... not nice.

Bastien Montagne noreply at git.blender.org
Fri Jun 20 16:53:06 CEST 2014


Commit: fb7c71383b8c21d7ded739b553e6c11f66f166df
Author: Bastien Montagne
Date:   Fri Jun 20 16:06:12 2014 +0200
https://developer.blender.org/rBfb7c71383b8c21d7ded739b553e6c11f66f166df

Code cleanup: BLI_md5.c was... not nice.

===================================================================

M	source/blender/blenlib/intern/md5.c

===================================================================

diff --git a/source/blender/blenlib/intern/md5.c b/source/blender/blenlib/intern/md5.c
index 4a09afe..4122f64 100644
--- a/source/blender/blenlib/intern/md5.c
+++ b/source/blender/blenlib/intern/md5.c
@@ -1,417 +1,391 @@
+/*
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ *
+ * Copyright (C) 1995 Software Foundation, Inc.
+ *
+ * Written by Ulrich Drepper <drepper at gnu.ai.mit.edu>.
+ */
+
 /** \file blender/blenlib/intern/md5.c
- *  \ingroup imbuf
+ *  \ingroup bli
+ *
+ *  Functions to compute MD5 message digest of files or memory blocks
+ *  according to the definition of MD5 in RFC 1321 from April 1992.
  */
 
 #include "BLI_md5.h"  /* own include */
 
-/* md5.c - Functions to compute MD5 message digest of files or memory blocks
-   according to the definition of MD5 in RFC 1321 from April 1992.
-   Copyright (C) 1995 Software Foundation, Inc.
-
-   This program is free software; you can redistribute it and/or modify
-   it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2, or (at your option)
-   any later version.
-
-   This program is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-   GNU General Public License for more details.
-
-   You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
-
-/* Written by Ulrich Drepper <drepper at gnu.ai.mit.edu>.  */
-
 #include <stdlib.h>
 #include <string.h>
 #include <stdio.h>
 #include <sys/types.h>
 
 #if defined HAVE_LIMITS_H || defined _LIBC
-# include <limits.h>
+#  include <limits.h>
 #endif
 
-/* The following contortions are an attempt to use the C preprocessor
-   to determine an unsigned integral type that is 32 bits wide.  An
-   alternative approach is to use autoconf's AC_CHECK_SIZEOF macro, but
-   doing that would require that the configure script compile and *run*
-   the resulting executable.  Locally running cross-compiled executables
-   is usually not possible.  */
+/* The following contortions are an attempt to use the C preprocessor to determine an unsigned integral type
+ * that is 32 bits wide. An alternative approach is to use autoconf's AC_CHECK_SIZEOF macro, but doing that
+ * would require that the configure script compile and *run* the resulting executable.
+ * Locally running cross-compiled executables is usually not possible.
+ */
 
 #if defined __STDC__ && __STDC__
-# define UINT_MAX_32_BITS 4294967295U
+#  define UINT_MAX_32_BITS 4294967295U
 #else
-# define UINT_MAX_32_BITS 0xFFFFFFFF
+#  define UINT_MAX_32_BITS 0xFFFFFFFF
 #endif
 
 /* If UINT_MAX isn't defined, assume it's a 32-bit type.
-   This should be valid for all systems GNU cares about because
-   that doesn't include 16-bit systems, and only modern systems
-   (that certainly have <limits.h>) have 64+-bit integral types.  */
+ * This should be valid for all systems GNU cares about because that doesn't include 16-bit systems,
+ * and only modern systems (that certainly have <limits.h>) have 64+-bit integral types.
+ */
 
 #ifndef UINT_MAX
-# define UINT_MAX UINT_MAX_32_BITS
+#  define UINT_MAX UINT_MAX_32_BITS
 #endif
 
 #if UINT_MAX == UINT_MAX_32_BITS
-  typedef unsigned int md5_uint32;
+   typedef unsigned int md5_uint32;
 #else
-# if USHRT_MAX == UINT_MAX_32_BITS
-   typedef unsigned short md5_uint32;
-# else
-#  if ULONG_MAX == UINT_MAX_32_BITS
-	typedef unsigned long md5_uint32;
+#  if USHRT_MAX == UINT_MAX_32_BITS
+     typedef unsigned short md5_uint32;
 #  else
-	/* The following line is intended to evoke an error.
-	   Using #error is not portable enough.  */
-	"Cannot determine unsigned 32-bit data type."
+#    if ULONG_MAX == UINT_MAX_32_BITS
+       typedef unsigned long md5_uint32;
+#    else
+       /* The following line is intended to evoke an error. Using #error is not portable enough. */
+       "Cannot determine unsigned 32-bit data type."
+#    endif
 #  endif
-# endif
 #endif
 
-/* Structure to save state of computation between the single steps.  */
+
+/* Following code is low level, upon which are built up the functions 'md5_stream' and 'md5_buffer'. */
+
+/* Structure to save state of computation between the single steps. */
 struct md5_ctx
 {
-  md5_uint32 A;
-  md5_uint32 B;
-  md5_uint32 C;
-  md5_uint32 D;
+	md5_uint32 A;
+	md5_uint32 B;
+	md5_uint32 C;
+	md5_uint32 D;
 };
 
-/*
- * The following three functions are build up the low level used in
- * the functions `md5_stream' and `md5_buffer'.
- */
+#ifdef __BIG_ENDIAN__
+#  define SWAP(n) (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24))
+#else
+#  define SWAP(n) (n)
+#endif
+
+/* This array contains the bytes used to pad the buffer to the next 64-byte boundary.  (RFC 1321, 3.1: Step 1) */
+static const unsigned char fillbuf[64] = {0x80, 0 /* , 0, 0, ...  */};
 
-/* Initialize structure containing state of computation.
-   (RFC 1321, 3.3: Step 3)  */
-static void md5_init_ctx(struct md5_ctx *ctx);
+/** Initialize structure containing state of computation.
+ *  (RFC 1321, 3.3: Step 3)
+ */
+static void md5_init_ctx(struct md5_ctx *ctx)
+{
+	ctx->A = 0x67452301;
+	ctx->B = 0xefcdab89;
+	ctx->C = 0x98badcfe;
+	ctx->D = 0x10325476;
+}
 
-/* Starting with the result of former calls of this function (or the
-   initialzation function update the context for the next LEN bytes
-   starting at BUFFER.
-   It is necessary that LEN is a multiple of 64!!! */
-static void md5_process_block(const void *buffer, size_t len, struct md5_ctx *ctx);
+/** Starting with the result of former calls of this function (or the initialization), this function updates
+ *  the 'ctx' context for the next 'len' bytes starting at 'buffer'.
+ *  It is necessary that 'len' is a multiple of 64!!!
+ */
+static void md5_process_block (const void *buffer, size_t len, struct md5_ctx *ctx)
+{
+/* These are the four functions used in the four steps of the MD5 algorithm and defined in the RFC 1321.
+ * The first function is a little bit optimized (as found in Colin Plumbs public domain implementation).
+ */
+/* #define FF(b, c, d) ((b & c) | (~b & d)) */
+#define FF(b, c, d) (d ^ (b & (c ^ d)))
+#define FG(b, c, d) FF (d, b, c)
+#define FH(b, c, d) (b ^ c ^ d)
+#define FI(b, c, d) (c ^ (b | ~d))
 
-/* Put result from CTX in first 16 bytes following RESBUF.  The result is
-   always in little endian byte order, so that a byte-wise output yields
-   to the wanted ASCII representation of the message digest.  */
-static void *md5_read_ctx(const struct md5_ctx *ctx, void *resbuf);
+/* It is unfortunate that C does not provide an operator for cyclic rotation.  Hope the C compiler is smart enough. */
+#define CYCLIC(w, s) (w = (w << s) | (w >> (32 - s)))
 
+	md5_uint32 correct_words[16];
+	const md5_uint32 *words = buffer;
+	size_t nwords = len / sizeof(md5_uint32);
+	const md5_uint32 *endp = words + nwords;
+	md5_uint32 A = ctx->A;
+	md5_uint32 B = ctx->B;
+	md5_uint32 C = ctx->C;
+	md5_uint32 D = ctx->D;
+
+	/* Process all bytes in the buffer with 64 bytes in each round of the loop.  */
+	while (words < endp) {
+		md5_uint32 *cwp = correct_words;
+		md5_uint32 A_save = A;
+		md5_uint32 B_save = B;
+		md5_uint32 C_save = C;
+		md5_uint32 D_save = D;
+
+		/* First round: using the given function, the context and a constant the next context is computed.
+		 * Because the algorithms processing unit is a 32-bit word and it is determined to work on words in
+		 * little endian byte order we perhaps have to change the byte order before the computation.
+		 * To reduce the work for the next steps we store the swapped words in the array CORRECT_WORDS.
+		 */
+#define OP(a, b, c, d, s, T)                                   \
+		a += FF(b, c, d) + (*cwp++ = SWAP(*words)) + T;        \
+		++words;                                               \
+		CYCLIC(a, s);                                          \
+		a += b;                                                \
+		(void)0
+
+		/* Before we start, one word to the strange constants. They are defined in RFC 1321 as:
+		 *     T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64
+		 */
+
+		/* Round 1.  */
+		OP(A, B, C, D,  7, 0xd76aa478);
+		OP(D, A, B, C, 12, 0xe8c7b756);
+		OP(C, D, A, B, 17, 0x242070db);
+		OP(B, C, D, A, 22, 0xc1bdceee);
+		OP(A, B, C, D,  7, 0xf57c0faf);
+		OP(D, A, B, C, 12, 0x4787c62a);
+		OP(C, D, A, B, 17, 0xa8304613);
+		OP(B, C, D, A, 22, 0xfd469501);
+		OP(A, B, C, D,  7, 0x698098d8);
+		OP(D, A, B, C, 12, 0x8b44f7af);
+		OP(C, D, A, B, 17, 0xffff5bb1);
+		OP(B, C, D, A, 22, 0x895cd7be);
+		OP(A, B, C, D,  7, 0x6b901122);
+		OP(D, A, B, C, 12, 0xfd987193);
+		OP(C, D, A, B, 17, 0xa679438e);
+		OP(B, C, D, A, 22, 0x49b40821);
 
-#ifdef __BIG_ENDIAN__
-#  define SWAP(n)							\
-	(((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24))
-#else
-#  define SWAP(n) (n)
-#endif
+#undef OP
 
+		/* For the second to fourth round we have the possibly swapped words in CORRECT_WORDS.
+		 * Redefine the macro to take an additional first argument specifying the function to use.
+		 */
+#define OP(f, a, b, c, d, k, s, T)                             \
+		a += f(b, c, d) + correct_words[k] + T;                \
+		CYCLIC(a, s);                                          \
+		a += b;                

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list