minor cleanup

This commit is contained in:
Brendan O'Dea 2006-12-05 05:22:59 +00:00
parent 24ba2c5ae1
commit f67e4566a5
2 changed files with 190 additions and 191 deletions

40
md5.c
View file

@ -15,10 +15,7 @@
* and avoid compile-time configuration. * and avoid compile-time configuration.
*/ */
#ifndef HAVE_OPENSSL
#include <string.h> #include <string.h>
#include "md5.h" #include "md5.h"
/* /*
@ -49,10 +46,8 @@
* doesn't work. * doesn't work.
*/ */
#if defined(__i386__) || defined(__vax__) #if defined(__i386__) || defined(__vax__)
#define SET(n) \ # define SET(n) (*(MD5_u32plus *)&ptr[(n) * 4])
(*(MD5_u32plus *)&ptr[(n) * 4]) # define GET(n) SET(n)
#define GET(n) \
SET(n)
#else #else
# define SET(n) \ # define SET(n) \
(ctx->block[(n)] = \ (ctx->block[(n)] = \
@ -164,8 +159,8 @@ static void *body(MD5_CTX *ctx, void *data, unsigned long size)
c += saved_c; c += saved_c;
d += saved_d; d += saved_d;
ptr += 64; ptr += MD5_BLOCK_SZ;
} while (size -= 64); } while (size -= MD5_BLOCK_SZ);
ctx->a = a; ctx->a = a;
ctx->b = b; ctx->b = b;
@ -194,14 +189,17 @@ void MD5_Update(MD5_CTX *ctx, void *data, unsigned long size)
saved_lo = ctx->lo; saved_lo = ctx->lo;
if ((ctx->lo = (saved_lo + size) & 0x1fffffff) < saved_lo) if ((ctx->lo = (saved_lo + size) & 0x1fffffff) < saved_lo)
ctx->hi++; ctx->hi++;
ctx->hi += size >> 29; ctx->hi += size >> 29;
used = saved_lo & 0x3f; used = saved_lo & 0x3f;
if (used) { if (used)
free = 64 - used; {
free = MD5_BLOCK_SZ - used;
if (size < free) { if (size < free)
{
memcpy(&ctx->buffer[used], data, size); memcpy(&ctx->buffer[used], data, size);
return; return;
} }
@ -209,10 +207,11 @@ void MD5_Update(MD5_CTX *ctx, void *data, unsigned long size)
memcpy(&ctx->buffer[used], data, free); memcpy(&ctx->buffer[used], data, free);
data = (unsigned char *)data + free; data = (unsigned char *)data + free;
size -= free; size -= free;
body(ctx, ctx->buffer, 64); body(ctx, ctx->buffer, MD5_BLOCK_SZ);
} }
if (size >= 64) { if (size >= MD5_BLOCK_SZ)
{
data = body(ctx, data, size & ~(unsigned long)0x3f); data = body(ctx, data, size & ~(unsigned long)0x3f);
size &= 0x3f; size &= 0x3f;
} }
@ -228,13 +227,14 @@ void MD5_Final(unsigned char *result, MD5_CTX *ctx)
ctx->buffer[used++] = 0x80; ctx->buffer[used++] = 0x80;
free = 64 - used; free = MD5_BLOCK_SZ - used;
if (free < 8) { if (free < 8)
{
memset(&ctx->buffer[used], 0, free); memset(&ctx->buffer[used], 0, free);
body(ctx, ctx->buffer, 64); body(ctx, ctx->buffer, MD5_BLOCK_SZ);
used = 0; used = 0;
free = 64; free = MD5_BLOCK_SZ;
} }
memset(&ctx->buffer[used], 0, free - 8); memset(&ctx->buffer[used], 0, free - 8);
@ -249,7 +249,7 @@ void MD5_Final(unsigned char *result, MD5_CTX *ctx)
ctx->buffer[62] = ctx->hi >> 16; ctx->buffer[62] = ctx->hi >> 16;
ctx->buffer[63] = ctx->hi >> 24; ctx->buffer[63] = ctx->hi >> 24;
body(ctx, ctx->buffer, 64); body(ctx, ctx->buffer, MD5_BLOCK_SZ);
result[0] = ctx->a; result[0] = ctx->a;
result[1] = ctx->a >> 8; result[1] = ctx->a >> 8;
@ -270,5 +270,3 @@ void MD5_Final(unsigned char *result, MD5_CTX *ctx)
memset(ctx, 0, sizeof(*ctx)); memset(ctx, 0, sizeof(*ctx));
} }
#endif

15
md5.h
View file

@ -6,10 +6,11 @@
* in the public domain. See md5.c for more information. * in the public domain. See md5.c for more information.
*/ */
#ifdef HAVE_OPENSSL #ifndef __MD5_H__
#include <openssl/md5.h> #define __MD5_H__
#elif !defined(_MD5_H)
#define _MD5_H #define MD5_DIGEST_SZ 16
#define MD5_BLOCK_SZ 64
/* Any 32-bit or wider unsigned integer data type will do */ /* Any 32-bit or wider unsigned integer data type will do */
typedef unsigned long MD5_u32plus; typedef unsigned long MD5_u32plus;
@ -17,12 +18,12 @@ typedef unsigned long MD5_u32plus;
typedef struct { typedef struct {
MD5_u32plus lo, hi; MD5_u32plus lo, hi;
MD5_u32plus a, b, c, d; MD5_u32plus a, b, c, d;
unsigned char buffer[64]; unsigned char buffer[MD5_BLOCK_SZ];
MD5_u32plus block[16]; MD5_u32plus block[MD5_DIGEST_SZ];
} MD5_CTX; } MD5_CTX;
extern void MD5_Init(MD5_CTX *ctx); extern void MD5_Init(MD5_CTX *ctx);
extern void MD5_Update(MD5_CTX *ctx, void *data, unsigned long size); extern void MD5_Update(MD5_CTX *ctx, void *data, unsigned long size);
extern void MD5_Final(unsigned char *result, MD5_CTX *ctx); extern void MD5_Final(unsigned char *result, MD5_CTX *ctx);
#endif #endif /* __MD5_H__ */