reformat nhmd4.{c,h}

This commit is contained in:
PatR
2024-02-24 13:49:51 -08:00
parent dd37c5326a
commit 53f16f9e8c
2 changed files with 202 additions and 176 deletions

View File

@@ -1,4 +1,4 @@
/* NetHack 3.7 nhmd4.h $NHDT-Date$ $NHDT-Branch$:$NHDT-Revision$ */
/* NetHack 3.7 nhmd4.h $NHDT-Date: 1708811386 2024/02/24 21:49:46 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.0 $ */
/*- Copyright (c) Kenneth Lorber, Kensington, Maryland, 2024 */
/* NetHack may be freely redistributed. See license for details. */
@@ -11,11 +11,11 @@
* the public domain. See md4.c for more information.
*/
#ifndef __NHMD4_H
#define __NHMD4_H
#ifndef NHMD4_H
#define NHMD4_H
#define NHMD4_DIGEST_LENGTH 128
#define NHMD4_RESULTLEN (128/8)
#define NHMD4_RESULTLEN (128 / 8) /* 16 */
typedef uint32_t quint32;
@@ -27,8 +27,11 @@ struct nhmd4_context {
};
typedef struct nhmd4_context NHMD4_CTX;
extern void nhmd4_init(struct nhmd4_context *ctx);
extern void nhmd4_update(struct nhmd4_context *ctx, const unsigned char *data, size_t size);
extern void nhmd4_final(struct nhmd4_context *ctx, unsigned char result[NHMD4_RESULTLEN]);
extern void nhmd4_init(NHMD4_CTX *ctx);
extern void nhmd4_update(NHMD4_CTX *, const unsigned char *, size_t);
extern void nhmd4_final(NHMD4_CTX *, unsigned char result[NHMD4_RESULTLEN]);
#endif /* NHMD4_H */
/*nhmd4.h*/
#endif

View File

@@ -1,8 +1,13 @@
/* NetHack 3.7 nhmd4.c $NHDT-Date$ $NHDT-Branch$:$NHDT-Revision$ */
/* NetHack 3.7 nhmd4.c $NHDT-Date: 1708811387 2024/02/24 21:49:47 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.0 $ */
/*-Copyright (c) Kenneth Lorber, Kensington, Maryland, 2024 */
/* NetHack may be freely redistributed. See license for details. */
// Derived from:
/*
* Usage is to try to match traceback data with the instance of the
* program which produced that, not for security related purposes.
*
* Derived from:
*/
/*
* MD4 (RFC-1320) message digest.
* Modified from MD5 code by Andrey Panin <pazke@donpac.ru>
@@ -24,6 +29,9 @@
#include "nhmd4.h"
static const unsigned char *body(struct nhmd4_context *,
const unsigned char *, size_t);
/*
* The basic MD4 functions.
*/
@@ -38,7 +46,6 @@
(a) += f((b), (c), (d)) + (x); \
(a) = ((a) << (s)) | ((a) >> (32 - (s)))
/*
* SET reads 4 input bytes in little-endian byte order and stores them
* in a properly aligned word in host byte order.
@@ -48,10 +55,8 @@
* doesn't work.
*/
#if defined(__i386__) || defined(__x86_64__)
#define SET(n) \
(*(const quint32 *)&ptr[(n) * 4])
#define GET(n) \
SET(n)
#define SET(n) (*(const quint32 *)&ptr[(n) * 4])
#define GET(n) SET(n)
#else
#define SET(n) \
(ctx->block[(n)] = \
@@ -67,7 +72,11 @@
* This processes one or more 64-byte data blocks, but does NOT update
* the bit counters. There're no alignment requirements.
*/
static const unsigned char *body(struct nhmd4_context *ctx, const unsigned char *data, size_t size)
static const unsigned char *
body(
struct nhmd4_context *ctx,
const unsigned char *data,
size_t size)
{
const unsigned char *ptr;
quint32 a, b, c, d;
@@ -163,7 +172,9 @@ static const unsigned char *body(struct nhmd4_context *ctx, const unsigned char
return ptr;
}
void nhmd4_init(struct nhmd4_context *ctx)
void
nhmd4_init(
struct nhmd4_context *ctx)
{
ctx->a = 0x67452301;
ctx->b = 0xefcdab89;
@@ -174,7 +185,11 @@ void nhmd4_init(struct nhmd4_context *ctx)
ctx->hi = 0;
}
void nhmd4_update(struct nhmd4_context *ctx, const unsigned char *data, size_t size)
void
nhmd4_update(
struct nhmd4_context *ctx,
const unsigned char *data,
size_t size)
{
/* @UNSAFE */
quint32 saved_lo;
@@ -202,19 +217,22 @@ void nhmd4_update(struct nhmd4_context *ctx, const unsigned char *data, size_t s
}
if (size >= 64) {
data = body(ctx, data, size & ~(unsigned long)0x3f);
size &= 0x3f;
data = body(ctx, data, size & ~0x3fUL);
size &= 0x3fUL;
}
memcpy(ctx->buffer, data, size);
}
void nhmd4_final(struct nhmd4_context *ctx, unsigned char result[NHMD4_RESULTLEN])
void
nhmd4_final(
struct nhmd4_context *ctx,
unsigned char result[NHMD4_RESULTLEN])
{
/* @UNSAFE */
unsigned long used, free;
used = ctx->lo & 0x3f;
used = ctx->lo & 0x3fUL;
ctx->buffer[used++] = 0x80;
@@ -258,11 +276,16 @@ void nhmd4_final(struct nhmd4_context *ctx, unsigned char result[NHMD4_RESULTLEN
result[14] = ctx->d >> 16;
result[15] = ctx->d >> 24;
memset(ctx, 0, sizeof(*ctx));
memset(ctx, 0, sizeof *ctx);
}
#undef F
#undef G
#undef H
#undef STEP
#undef SET
#undef GET
#endif
#endif /* CRASHREPORT */
/*nhmd4.c*/