minor cleanup

This commit is contained in:
bodea 2006-12-05 05:22:59 +00:00
parent 426335628c
commit 007f5d2cac
2 changed files with 190 additions and 191 deletions

40
md5.c
View file

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