]> git.sur5r.net Git - openldap/blobdiff - libraries/liblutil/md5.c
Merge remote branch 'origin/mdb.master'
[openldap] / libraries / liblutil / md5.c
index 122495cf3ee5f36fc26e5ad79627628baef52ca8..b36bfbb7d64f0bca93d57e61fa5b920644f57e1a 100644 (file)
@@ -1,7 +1,21 @@
-/*
- * Modified by Kurt D. Zeilenga for inclusion into OpenLDAP
- * I hereby disclaim copyright in any changes I have made; this
- * code remains in the public domain.
+/* md5.c -- MD5 message-digest algorithm */
+/* $OpenLDAP$ */
+/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
+ *
+ * Copyright 1998-2012 The OpenLDAP Foundation.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted only as authorized by the OpenLDAP
+ * Public License.
+ *
+ * A copy of this license is available in the file LICENSE in the
+ * top-level directory of the distribution or, alternatively, at
+ * <http://www.OpenLDAP.org/license.html>.
+ */
+/* This work was adapted for inclusion in OpenLDAP Software by
+ * Kurt D. Zeilenga based upon code developed by Colin Plumb
+ * and subsequently modified by Jim Kingdon. 
  */
 
 /*
 #include <lutil_md5.h>
 
 /* Little-endian byte-swapping routines.  Note that these do not
-   depend on the size of datatypes such as uint32, nor do they require
+   depend on the size of datatypes such as ber_uint_t, 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.  */
 
-static uint32
-getu32 (addr)
-     const unsigned char *addr;
+static ber_uint_t
+getu32( const unsigned char *addr )
 {
        return (((((unsigned long)addr[3] << 8) | addr[2]) << 8)
                | addr[1]) << 8 | addr[0];
 }
 
 static void
-putu32 (data, addr)
-     uint32 data;
-     unsigned char *addr;
+putu32( ber_uint_t data, unsigned char *addr )
 {
        addr[0] = (unsigned char)data;
        addr[1] = (unsigned char)(data >> 8);
@@ -68,8 +79,7 @@ putu32 (data, addr)
  * initialization constants.
  */
 void
-ldap_MD5Init(ctx)
-     struct ldap_MD5Context *ctx;
+lutil_MD5Init( struct lutil_MD5Context *ctx )
 {
        ctx->buf[0] = 0x67452301;
        ctx->buf[1] = 0xefcdab89;
@@ -85,17 +95,18 @@ ldap_MD5Init(ctx)
  * of bytes.
  */
 void
-ldap_MD5Update(ctx, buf, len)
-     struct ldap_MD5Context *ctx;
-     unsigned char const *buf;
-     unsigned len;
+lutil_MD5Update(
+    struct lutil_MD5Context    *ctx,
+    const unsigned char                *buf,
+    ber_len_t          len
+)
 {
-       uint32 t;
+       ber_uint_t t;
 
        /* Update bitcount */
 
        t = ctx->bits[0];
-       if ((ctx->bits[0] = (t + ((uint32)len << 3)) & 0xffffffff) < t)
+       if ((ctx->bits[0] = (t + ((ber_uint_t)len << 3)) & 0xffffffff) < t)
                ctx->bits[1]++; /* Carry from low to high */
        ctx->bits[1] += len >> 29;
 
@@ -108,11 +119,11 @@ ldap_MD5Update(ctx, buf, len)
 
                t = 64-t;
                if (len < t) {
-                       memcpy(p, buf, len);
+                       AC_MEMCPY(p, buf, len);
                        return;
                }
-               memcpy(p, buf, t);
-               ldap_MD5Transform(ctx->buf, ctx->in);
+               AC_MEMCPY(p, buf, t);
+               lutil_MD5Transform(ctx->buf, ctx->in);
                buf += t;
                len -= t;
        }
@@ -120,15 +131,15 @@ ldap_MD5Update(ctx, buf, len)
        /* Process data in 64-byte chunks */
 
        while (len >= 64) {
-               memcpy(ctx->in, buf, 64);
-               ldap_MD5Transform(ctx->buf, ctx->in);
+               AC_MEMCPY(ctx->in, buf, 64);
+               lutil_MD5Transform(ctx->buf, ctx->in);
                buf += 64;
                len -= 64;
        }
 
        /* Handle any remaining bytes of data. */
 
-       memcpy(ctx->in, buf, len);
+       AC_MEMCPY(ctx->in, buf, len);
 }
 
 /*
@@ -136,9 +147,7 @@ ldap_MD5Update(ctx, buf, len)
  * 1 0* (64-bit count of bits processed, MSB-first)
  */
 void
-ldap_MD5Final(digest, ctx)
-     unsigned char digest[16];
-     struct ldap_MD5Context *ctx;
+lutil_MD5Final( unsigned char *digest, struct lutil_MD5Context *ctx )
 {
        unsigned count;
        unsigned char *p;
@@ -157,26 +166,26 @@ ldap_MD5Final(digest, ctx)
        /* Pad out to 56 mod 64 */
        if (count < 8) {
                /* Two lots of padding:  Pad the first block to 64 bytes */
-               memset(p, 0, count);
-               ldap_MD5Transform(ctx->buf, ctx->in);
+               memset(p, '\0', count);
+               lutil_MD5Transform(ctx->buf, ctx->in);
 
                /* Now fill the next block with 56 bytes */
-               memset(ctx->in, 0, 56);
+               memset(ctx->in, '\0', 56);
        } else {
                /* Pad block to 56 bytes */
-               memset(p, 0, count-8);
+               memset(p, '\0', count-8);
        }
 
        /* Append length in bits and transform */
        putu32(ctx->bits[0], ctx->in + 56);
        putu32(ctx->bits[1], ctx->in + 60);
 
-       ldap_MD5Transform(ctx->buf, ctx->in);
+       lutil_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 */
+       memset(ctx, '\0', sizeof(*ctx));        /* In case it's sensitive */
 }
 
 #ifndef ASM_MD5
@@ -199,12 +208,10 @@ ldap_MD5Final(digest, ctx)
  * the data and converts bytes into longwords for this routine.
  */
 void
-ldap_MD5Transform(buf, inraw)
-     uint32 buf[4];
-     const unsigned char inraw[64];
+lutil_MD5Transform( ber_uint_t *buf, const unsigned char *inraw )
 {
-       register uint32 a, b, c, d;
-       uint32 in[16];
+       register ber_uint_t a, b, c, d;
+       ber_uint_t in[16];
        int i;
 
        for (i = 0; i < 16; ++i)
@@ -296,30 +303,30 @@ ldap_MD5Transform(buf, inraw)
 #include <stdio.h>
 
 int
-main (int argc, char **argv)
+main (int  argc, char **argv )
 {
-       struct ldap_MD5Context context;
-       unsigned char checksum[16];
+       struct lutil_MD5Context context;
+       unsigned char checksum[LUTIL_MD5_BYTES];
        int i;
        int j;
 
        if (argc < 2)
        {
                fprintf (stderr, "usage: %s string-to-hash\n", argv[0]);
-               exit (1);
+               return EXIT_FAILURE;
        }
        for (j = 1; j < argc; ++j)
        {
                printf ("MD5 (\"%s\") = ", argv[j]);
-               ldap_MD5Init (&context);
-               ldap_MD5Update (&context, argv[j], strlen (argv[j]));
-               ldap_MD5Final (checksum, &context);
-               for (i = 0; i < 16; i++)
+               lutil_MD5Init (&context);
+               lutil_MD5Update (&context, argv[j], strlen (argv[j]));
+               lutil_MD5Final (checksum, &context);
+               for (i = 0; i < LUTIL_MD5_BYTES; i++)
                {
                        printf ("%02x", (unsigned int) checksum[i]);
                }
                printf ("\n");
        }
-       return 0;
+       return EXIT_SUCCESS;
 }
 #endif /* TEST */