]> granicus.if.org Git - neomutt/commitdiff
tidy md5 564/head
authorRichard Russon <rich@flatcap.org>
Sat, 29 Apr 2017 16:00:00 +0000 (17:00 +0100)
committerRichard Russon <rich@flatcap.org>
Tue, 2 May 2017 16:34:44 +0000 (17:34 +0100)
No code changes.

This code is imported from the GNU Library.
It contained a lot of definitions and compatability code that we don't
need.  Also, tidy some comment blocks.

@gahr
Leverage on AC_C_BIGENDIAN instead of glibc's endian.h

md5.c
md5.h

diff --git a/md5.c b/md5.c
index ffc624fd9bb45ac7a674055d0238e734fd76a7bc..3a543772d4e48f1b74dfcf1259f34595517894e2 100644 (file)
--- a/md5.c
+++ b/md5.c
 #include <string.h>
 #include "md5.h"
 
-#ifdef USE_UNLOCKED_IO
-#include "unlocked-io.h"
-#endif
-
-#ifdef _LIBC
-#include <endian.h>
-#if (__BYTE_ORDER == __BIG_ENDIAN)
-#define WORDS_BIGENDIAN 1
-#endif
-/* We need to keep the namespace clean so define the MD5 function
-   protected using leading __ .  */
-#define md5_init_ctx __md5_init_ctx
-#define md5_process_block __md5_process_block
-#define md5_process_bytes __md5_process_bytes
-#define md5_finish_ctx __md5_finish_ctx
-#define md5_read_ctx __md5_read_ctx
-#define md5_stream __md5_stream
-#define md5_buffer __md5_buffer
-#endif
-
 #ifdef WORDS_BIGENDIAN
 #define SWAP(n)                                                                \
   (((n) << 24) | (((n) &0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24))
 #endif
 
 #define BLOCKSIZE 4096
-#if ((BLOCKSIZE % 64) != 0)
-#error "invalid BLOCKSIZE"
-#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)  */
 void md5_init_ctx(struct md5_ctx *ctx)
@@ -140,8 +116,8 @@ int md5_stream(FILE *stream, void *resblock)
   while (1)
   {
     /* We read the file in blocks of BLOCKSIZE bytes.  One call of the
-         computation function processes the whole buffer so that with the
-         next round of the loop another block can be read.  */
+     * computation function processes the whole buffer so that with the next
+     * round of the loop another block can be read.  */
     size_t n;
     sum = 0;
 
@@ -157,24 +133,22 @@ int md5_stream(FILE *stream, void *resblock)
 
       if (n == 0)
       {
-        /* Check for the error flag IFF N == 0, so that we don't
-                 exit the loop after a partial read due to e.g., EAGAIN
-                 or EWOULDBLOCK.  */
+        /* Check for the error flag IFF N == 0, so that we don't exit the loop
+         * after a partial read due to e.g., EAGAIN or EWOULDBLOCK.  */
         if (ferror(stream))
           return 1;
         goto process_partial_block;
       }
 
-      /* We've read at least one byte, so ignore errors.  But always
-             check for EOF, since feof may be true even though N > 0.
-             Otherwise, we could end up calling fread after EOF.  */
+      /* We've read at least one byte, so ignore errors.  But always check for
+       * EOF, since feof may be true even though N > 0.  Otherwise, we could
+       * end up calling fread after EOF.  */
       if (feof(stream))
         goto process_partial_block;
     }
 
-    /* Process buffer with BLOCKSIZE bytes.  Note that
-         BLOCKSIZE % 64 == 0
-       */
+    /* Process buffer with BLOCKSIZE bytes.
+     * Note that BLOCKSIZE % 64 == 0 */
     md5_process_block(buffer, BLOCKSIZE, &ctx);
   }
 
@@ -210,8 +184,8 @@ void *md5_buffer(const char *buffer, size_t len, void *resblock)
 
 void md5_process_bytes(const void *buffer, size_t len, struct md5_ctx *ctx)
 {
-  /* When we already have some bits in our internal buffer concatenate
-     both inputs first.  */
+  /* When we already have some bits in our internal buffer concatenate both
+   * inputs first.  */
   if (ctx->buflen != 0)
   {
     size_t left_over = ctx->buflen;
@@ -295,15 +269,14 @@ void md5_process_block(const void *buffer, size_t len, struct md5_ctx *ctx)
   md5_uint32 C = ctx->C;
   md5_uint32 D = ctx->D;
 
-  /* First increment the byte count.  RFC 1321 specifies the possible
-     length of the file up to 2^64 bits.  Here we only compute the
-     number of bytes.  Do a double word increment.  */
+  /* First increment the byte count.  RFC 1321 specifies the possible length of
+   * the file up to 2^64 bits.  Here we only compute the number of bytes.  Do a
+   * double word increment.  */
   ctx->total[0] += len;
   if (ctx->total[0] < len)
     ++ctx->total[1];
 
-  /* Process all bytes in the buffer with 64 bytes in each round of
-     the loop.  */
+  /* Process all bytes in the buffer with 64 bytes in each round of the loop.  */
   while (words < endp)
   {
     md5_uint32 *cwp = correct_words;
@@ -312,12 +285,12 @@ void md5_process_block(const void *buffer, size_t len, struct md5_ctx *ctx)
     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.  */
+/* 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)                                                   \
   do                                                                           \
@@ -329,18 +302,15 @@ void md5_process_block(const void *buffer, size_t len, struct md5_ctx *ctx)
   } while (0)
 
 /* It is unfortunate that C does not provide an operator for
        cyclic rotation.  Hope the C compiler is smart enough.  */
* cyclic rotation.  Hope the C compiler is smart enough.  */
 #define CYCLIC(w, s) (w = (w << s) | (w >> (32 - s)))
 
     /* 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
-
-         Here is an equivalent invocation using Perl:
-
-         perl -e 'foreach(1..64){printf "0x%08x\n", int (4294967296 * abs (sin $_))}'
-       */
+     * They are defined in RFC 1321 as
+     * T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64
+     * Here is an equivalent invocation using Perl:
+     * perl -e 'foreach(1..64){printf "0x%08x\n", int (4294967296 * abs (sin $_))}'
+     */
 
     /* Round 1.  */
     OP(A, B, C, D, 7, 0xd76aa478);
@@ -361,8 +331,8 @@ void md5_process_block(const void *buffer, size_t len, struct md5_ctx *ctx)
     OP(B, C, D, A, 22, 0x49b40821);
 
 /* 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.  */
* in CORRECT_WORDS.  Redefine the macro to take an additional first
* argument specifying the function to use.  */
 #undef OP
 #define OP(f, a, b, c, d, k, s, T)                                             \
   do                                                                           \
diff --git a/md5.h b/md5.h
index e17246155af5237d55b26b3eee4ca091e953e549..42c8dedbc218ba8b64536bc8f209f75ff6627ece 100644 (file)
--- a/md5.h
+++ b/md5.h
 #ifndef _MUTT_MD5_H
 #define _MUTT_MD5_H 1
 
-#include <stdio.h>
 #include <inttypes.h>
+#include <stdio.h>
 #include <sys/types.h>
 
-#ifndef __GNUC_PREREQ
-#if defined(__GNUC__) && defined(__GNUC_MINOR__)
-#define __GNUC_PREREQ(maj, min)                                                \
-  ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
-#else
-#define __GNUC_PREREQ(maj, min) 0
-#endif
-#endif
-
-#ifndef __THROW
-#if defined(__cplusplus) && __GNUC_PREREQ(2, 8)
-#define __THROW throw()
-#else
-#define __THROW
-#endif
-#endif
-
-#ifndef _LIBC
-#define __md5_buffer md5_buffer
-#define __md5_finish_ctx md5_finish_ctx
-#define __md5_init_ctx md5_init_ctx
-#define __md5_process_block md5_process_block
-#define __md5_process_bytes md5_process_bytes
-#define __md5_read_ctx md5_read_ctx
-#define __md5_stream md5_stream
-#endif
-
 typedef uint32_t md5_uint32;
 
 /* Structure to save state of computation between the single steps.  */
@@ -77,43 +50,41 @@ struct md5_ctx
  */
 
 /* Initialize structure containing state of computation.
  (RFC 1321, 3.3: Step 3)  */
-extern void __md5_init_ctx(struct md5_ctx *ctx) __THROW;
* (RFC 1321, 3.3: Step 3)  */
+void md5_init_ctx(struct md5_ctx *ctx);
 
 /* Starting with the result of former calls of this function (or the
  initialization function update the context for the next LEN bytes
  starting at BUFFER.
  It is necessary that LEN is a multiple of 64!!! */
-extern void __md5_process_block(const void *buffer, size_t len, struct md5_ctx *ctx) __THROW;
* initialization function update the context for the next LEN bytes
* starting at BUFFER.
* It is necessary that LEN is a multiple of 64!!! */
+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 function update the context for the next LEN bytes
  starting at BUFFER.
  It is NOT required that LEN is a multiple of 64.  */
-extern void __md5_process_bytes(const void *buffer, size_t len, struct md5_ctx *ctx) __THROW;
* initialization function update the context for the next LEN bytes
* starting at BUFFER.
* It is NOT required that LEN is a multiple of 64.  */
+void md5_process_bytes(const void *buffer, size_t len, struct md5_ctx *ctx);
 
 /* Process the remaining bytes in the buffer and 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.  */
-extern void *__md5_finish_ctx(struct md5_ctx *ctx, void *resbuf) __THROW;
-
+ * 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.  */
+void *md5_finish_ctx(struct md5_ctx *ctx, void *resbuf);
 
 /* 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.  */
-extern void *__md5_read_ctx(const struct md5_ctx *ctx, void *resbuf) __THROW;
-
+ * always in little endian byte order, so that a byte-wise output yields
+ * to the wanted ASCII representation of the message digest.  */
+void *md5_read_ctx(const struct md5_ctx *ctx, void *resbuf);
 
 /* Compute MD5 message digest for bytes read from STREAM.  The
  resulting message digest number will be written into the 16 bytes
  beginning at RESBLOCK.  */
-extern int __md5_stream(FILE *stream, void *resblock) __THROW;
* resulting message digest number will be written into the 16 bytes
* beginning at RESBLOCK.  */
+int md5_stream(FILE *stream, void *resblock);
 
 /* Compute MD5 message digest for LEN bytes beginning at BUFFER.  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.  */
-extern void *__md5_buffer(const char *buffer, size_t len, void *resblock) __THROW;
* result is always in little endian byte order, so that a byte-wise
* output yields to the wanted ASCII representation of the message
* digest.  */
+void *md5_buffer(const char *buffer, size_t len, void *resblock);
 
 #endif /* _MUTT_MD5_H */