--- dancer-ircd-1.0.36.orig/src/md5.c
+++ dancer-ircd-1.0.36/src/md5.c
@@ -15,27 +15,43 @@
  * will fill a supplied 16-byte array with the digest.
  */
 
+/* This code was modified in 1997 by Jim Kingdon of Cyclic Software to
+   not require an integer type which is exactly 32 bits.  This work
+   draws on the changes for the same purpose by Tatu Ylonen
+   <ylo@cs.hut.fi> as part of SSH, but since I didn't actually use
+   that code, there is no copyright issue.  I hereby disclaim
+   copyright in any changes I have made; this code remains in the
+   public domain.  */
+
+#ifdef HAVE_CONFIG_H
 #include "config.h"
+#endif
+
+#include <string.h>	/* for memcpy() and memset() */
+
 #include "md5.h"
 
-#include <string.h>
+/* Little-endian byte-swapping routines.  Note that these do not
+   depend on the size of datatypes such as uint32, nor do they require
+   us to detect the endianness of the machine we are running on.  It
+   is possible they should be macros for speed, but I would be
+   surprised if they were a performance bottleneck for MD5.  */
 
-#ifdef WORDS_BIGENDIAN
-/*
- * Note: this code is harmless on little-endian machines.
- */
-void
-byteReverse(unsigned char *buf, unsigned longs)
+static uint32
+getu32(const unsigned char *addr)
 {
-    uint32 t;
-    do {
-	t = (uint32) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
-	    ((unsigned) buf[1] << 8 | buf[0]);
-	*(uint32 *) buf = t;
-	buf += 4;
-    } while (--longs);
+    return (((((unsigned long) addr[3] << 8) | addr[2]) << 8)
+	    | addr[1]) << 8 | addr[0];
+}
+
+static void
+putu32(uint32 data, unsigned char *addr)
+{
+    addr[0] = (unsigned char) data;
+    addr[1] = (unsigned char) (data >> 8);
+    addr[2] = (unsigned char) (data >> 16);
+    addr[3] = (unsigned char) (data >> 24);
 }
-#endif
 
 /*
  * Start MD5 accumulation.  Set bit count to 0 and buffer to mysterious
@@ -65,7 +81,7 @@
     /* Update bitcount */
 
     t = ctx->bits[0];
-    if ((ctx->bits[0] = t + ((uint32) len << 3)) < t)
+    if ((ctx->bits[0] = (t + ((uint32) len << 3)) & 0xffffffff) < t)
 	ctx->bits[1]++;		/* Carry from low to high */
     ctx->bits[1] += len >> 29;
 
@@ -74,7 +90,7 @@
     /* Handle any leading odd-sized chunks */
 
     if (t) {
-	unsigned char *p = (unsigned char *) ctx->in + t;
+	unsigned char *p = ctx->in + t;
 
 	t = 64 - t;
 	if (len < t) {
@@ -82,17 +98,16 @@
 	    return;
 	}
 	memcpy(p, buf, t);
-	byteReverse(ctx->in, 16);
-	MD5Transform(ctx->buf, (uint32 *) ctx->in);
+	MD5Transform(ctx->buf, ctx->in);
 	buf += t;
 	len -= t;
     }
+
     /* Process data in 64-byte chunks */
 
     while (len >= 64) {
 	memcpy(ctx->in, buf, 64);
-	byteReverse(ctx->in, 16);
-	MD5Transform(ctx->buf, (uint32 *) ctx->in);
+	MD5Transform(ctx->buf, ctx->in);
 	buf += 64;
 	len -= 64;
     }
@@ -127,8 +142,7 @@
     if (count < 8) {
 	/* Two lots of padding:  Pad the first block to 64 bytes */
 	memset(p, 0, count);
-	byteReverse(ctx->in, 16);
-	MD5Transform(ctx->buf, (uint32 *) ctx->in);
+	MD5Transform(ctx->buf, ctx->in);
 
 	/* Now fill the next block with 56 bytes */
 	memset(ctx->in, 0, 56);
@@ -136,18 +150,21 @@
 	/* Pad block to 56 bytes */
 	memset(p, 0, count - 8);
     }
-    byteReverse(ctx->in, 14);
 
     /* Append length in bits and transform */
-    ((uint32 *) ctx->in)[14] = ctx->bits[0];
-    ((uint32 *) ctx->in)[15] = ctx->bits[1];
+    putu32(ctx->bits[0], ctx->in + 56);
+    putu32(ctx->bits[1], ctx->in + 60);
 
-    MD5Transform(ctx->buf, (uint32 *) ctx->in);
-    byteReverse((unsigned char *) ctx->buf, 4);
-    memcpy(digest, ctx->buf, 16);
-    memset((char *) ctx, 0, sizeof(ctx));	/* In case it's sensitive */
+    MD5Transform(ctx->buf, ctx->in);
+    putu32(ctx->buf[0], digest);
+    putu32(ctx->buf[1], digest + 4);
+    putu32(ctx->buf[2], digest + 8);
+    putu32(ctx->buf[3], digest + 12);
+    memset(ctx, 0, sizeof(ctx));	/* In case it's sensitive */
 }
 
+#ifndef ASM_MD5
+
 /* The four core functions - F1 is optimized somewhat */
 
 /* #define F1(x, y, z) (x & y | ~x & z) */
@@ -158,7 +175,7 @@
 
 /* This is the central step in the MD5 algorithm. */
 #define MD5STEP(f, w, x, y, z, data, s) \
-	( w += f(x, y, z) + data,  w = w<<s | w>>(32-s),  w += x )
+	( w += f(x, y, z) + data, w &= 0xffffffff, w = w<<s | w>>(32-s), w += x )
 
 /*
  * The core of the MD5 algorithm, this alters an existing MD5 hash to
@@ -166,9 +183,14 @@
  * the data and converts bytes into longwords for this routine.
  */
 void
-MD5Transform(uint32 buf[4], uint32 const in[16])
+MD5Transform(uint32 buf[4], const unsigned char inraw[64])
 {
     register uint32 a, b, c, d;
+    uint32 in[16];
+    int i;
+
+    for (i = 0; i < 16; ++i)
+	in[i] = getu32 (inraw + 4 * i);
 
     a = buf[0];
     b = buf[1];
@@ -248,3 +270,4 @@
     buf[2] += c;
     buf[3] += d;
 }
+#endif
