gettimeofday: port recent C++ fix to Emacs
[gnulib.git] / lib / md2.c
index 787b766..e5d306f 100644 (file)
--- a/lib/md2.c
+++ b/lib/md2.c
@@ -1,7 +1,7 @@
 /* Functions to compute MD2 message digest of files or memory blocks.
    according to the definition of MD2 in RFC 1319 from April 1992.
-   Copyright (C) 1995,1996,1997,1999,2000,2001,2002,2003,2005
-   Free Software Foundation, Inc.
+   Copyright (C) 1995-1997, 1999-2003, 2005-2006, 2008-2013 Free 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
    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.  */
+   along with this program; if not, see <http://www.gnu.org/licenses/>.  */
 
 /* Adapted by Simon Josefsson from public domain Libtomcrypt 1.06 by
    Tom St Denis. */
 
-#ifdef HAVE_CONFIG_H
-# include <config.h>
-#endif
+#include <config.h>
 
 #include "md2.h"
 
+#include <stdlib.h>
 #include <string.h>
 #include <sys/types.h>
 
@@ -35,7 +33,7 @@
 # include "unlocked-io.h"
 #endif
 
-#define BLOCKSIZE 4096
+#define BLOCKSIZE 32768
 #if BLOCKSIZE % 64 != 0
 # error "invalid BLOCKSIZE"
 #endif
@@ -55,10 +53,7 @@ md2_init_ctx (struct md2_ctx *ctx)
 }
 
 /* Put result from CTX in first 16 bytes following RESBUF.  The result
-   must be in little endian byte order.
-
-   IMPORTANT: On some systems it is required that RESBUF is correctly
-   aligned for a 32 bits value.  */
+   must be in little endian byte order.  */
 void *
 md2_read_ctx (const struct md2_ctx *ctx, void *resbuf)
 {
@@ -68,10 +63,7 @@ md2_read_ctx (const struct md2_ctx *ctx, void *resbuf)
 }
 
 /* Process the remaining bytes in the internal buffer and the usual
-   prolog according to the standard and write the result to RESBUF.
-
-   IMPORTANT: On some systems it is required that RESBUF is correctly
-   aligned for a 32 bits value.  */
+   prolog according to the standard and write the result to RESBUF.  */
 void *
 md2_finish_ctx (struct md2_ctx *ctx, void *resbuf)
 {
@@ -102,9 +94,12 @@ int
 md2_stream (FILE *stream, void *resblock)
 {
   struct md2_ctx ctx;
-  char buffer[BLOCKSIZE + 72];
   size_t sum;
 
+  char *buffer = malloc (BLOCKSIZE + 72);
+  if (!buffer)
+    return 1;
+
   /* Initialize the computation context.  */
   md2_init_ctx (&ctx);
 
@@ -119,30 +114,33 @@ md2_stream (FILE *stream, void *resblock)
 
       /* Read block.  Take care for partial reads.  */
       while (1)
-       {
-         n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
-
-         sum += n;
-
-         if (sum == BLOCKSIZE)
-           break;
-
-         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.  */
-             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.  */
-         if (feof (stream))
-           goto process_partial_block;
-       }
+        {
+          n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
+
+          sum += n;
+
+          if (sum == BLOCKSIZE)
+            break;
+
+          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.  */
+              if (ferror (stream))
+                {
+                  free (buffer);
+                  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.  */
+          if (feof (stream))
+            goto process_partial_block;
+        }
 
       /* Process buffer with BLOCKSIZE bytes.  Note that
          BLOCKSIZE % 64 == 0
@@ -158,6 +156,7 @@ process_partial_block:;
 
   /* Construct result in desired memory.  */
   md2_finish_ctx (&ctx, resblock);
+  free (buffer);
   return 0;
 }
 
@@ -196,11 +195,11 @@ md2_process_bytes (const void *buffer, size_t len, struct md2_ctx *ctx)
 
       /* is 16 bytes full? */
       if (ctx->curlen == 16)
-       {
-         md2_compress (ctx);
-         md2_update_chksum (ctx);
-         ctx->curlen = 0;
-       }
+        {
+          md2_compress (ctx);
+          md2_update_chksum (ctx);
+          ctx->curlen = 0;
+        }
     }
 }
 
@@ -260,9 +259,9 @@ md2_compress (struct md2_ctx *ctx)
   for (j = 0; j < 18; j++)
     {
       for (k = 0; k < 48; k++)
-       {
-         t = (ctx->X[k] ^= PI_SUBST[(int) (t & 255)]);
-       }
+        {
+          t = (ctx->X[k] ^= PI_SUBST[(int) (t & 255)]);
+        }
       t = (t + (unsigned char) j) & 255;
     }
 }