Skip to content

Commit

Permalink
- Introduced various new typedefs for windows platform (int32_t, int6…
Browse files Browse the repository at this point in the history
…4_t, etc...)

- Applied md5 64-bit safety patch by Tony White.
  • Loading branch information
Thilo Schulz committed Apr 27, 2006
1 parent c116695 commit 235e2c2
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 53 deletions.
3 changes: 1 addition & 2 deletions code/client/cl_main.c
Expand Up @@ -2549,8 +2549,7 @@ void CL_Init( void ) {
Cvar_Set( "cl_running", "1" );

CL_GenerateQKey();
// Uncomment this once md5.c has been made 64 bit-safe.
// Cvar_Get("cl_guid", Com_MD5File(QKEY_FILE, 0), CVAR_USERINFO | CVAR_ROM);
Cvar_Get("cl_guid", Com_MD5File(QKEY_FILE, 0), CVAR_USERINFO | CVAR_ROM);

Com_Printf( "----- Client Initialization Complete -----\n" );
}
Expand Down
43 changes: 11 additions & 32 deletions code/qcommon/md4.c
Expand Up @@ -30,21 +30,9 @@
#include "q_shared.h"
#include "qcommon.h"

#ifndef int32
#define int32 int
#endif

#if SIZEOF_INT > 4
#define LARGE_INT32
#endif

#ifndef uint32
#define uint32 unsigned int32
#endif

struct mdfour {
uint32 A, B, C, D;
uint32 totalN;
uint32_t A, B, C, D;
uint32_t totalN;
};


Expand All @@ -58,23 +46,19 @@ static struct mdfour *m;
#define F(X,Y,Z) (((X)&(Y)) | ((~(X))&(Z)))
#define G(X,Y,Z) (((X)&(Y)) | ((X)&(Z)) | ((Y)&(Z)))
#define H(X,Y,Z) ((X)^(Y)^(Z))
#ifdef LARGE_INT32
#define lshift(x,s) ((((x)<<(s))&0xFFFFFFFF) | (((x)>>(32-(s)))&0xFFFFFFFF))
#else
#define lshift(x,s) (((x)<<(s)) | ((x)>>(32-(s))))
#endif

#define ROUND1(a,b,c,d,k,s) a = lshift(a + F(b,c,d) + X[k], s)
#define ROUND2(a,b,c,d,k,s) a = lshift(a + G(b,c,d) + X[k] + 0x5A827999,s)
#define ROUND3(a,b,c,d,k,s) a = lshift(a + H(b,c,d) + X[k] + 0x6ED9EBA1,s)

/* this applies md4 to 64 byte chunks */
static void mdfour64(uint32 *M)
static void mdfour64(uint32_t *M)
{
int j;
uint32 AA, BB, CC, DD;
uint32 X[16];
uint32 A,B,C,D;
uint32_t AA, BB, CC, DD;
uint32_t X[16];
uint32_t A,B,C,D;

for (j=0;j<16;j++)
X[j] = M[j];
Expand Down Expand Up @@ -111,18 +95,13 @@ static void mdfour64(uint32 *M)

A += AA; B += BB; C += CC; D += DD;

#ifdef LARGE_INT32
A &= 0xFFFFFFFF; B &= 0xFFFFFFFF;
C &= 0xFFFFFFFF; D &= 0xFFFFFFFF;
#endif

for (j=0;j<16;j++)
X[j] = 0;

m->A = A; m->B = B; m->C = C; m->D = D;
}

static void copy64(uint32 *M, byte *in)
static void copy64(uint32_t *M, byte *in)
{
int i;

Expand All @@ -131,7 +110,7 @@ static void copy64(uint32 *M, byte *in)
(in[i*4+1]<<8) | (in[i*4+0]<<0);
}

static void copy4(byte *out,uint32 x)
static void copy4(byte *out,uint32_t x)
{
out[0] = x&0xFF;
out[1] = (x>>8)&0xFF;
Expand All @@ -152,8 +131,8 @@ void mdfour_begin(struct mdfour *md)
static void mdfour_tail(byte *in, int n)
{
byte buf[128];
uint32 M[16];
uint32 b;
uint32_t M[16];
uint32_t b;

m->totalN += n;

Expand All @@ -178,7 +157,7 @@ static void mdfour_tail(byte *in, int n)

static void mdfour_update(struct mdfour *md, byte *in, int n)
{
uint32 M[16];
uint32_t M[16];

if (n == 0) mdfour_tail(in, n);

Expand Down
36 changes: 18 additions & 18 deletions code/qcommon/md5.c
Expand Up @@ -18,8 +18,8 @@
#include "qcommon.h"

typedef struct MD5Context {
unsigned long int buf[4];
unsigned long int bits[2];
uint32_t buf[4];
uint32_t bits[2];
unsigned char in[64];
} MD5_CTX;

Expand All @@ -33,12 +33,12 @@ typedef struct MD5Context {
*/
static void byteReverse(unsigned char *buf, unsigned longs)
{
unsigned long int t;
uint32_t t;
do {
t = (unsigned long int)
t = (uint32_t)
((unsigned) buf[3] << 8 | buf[2]) << 16 |
((unsigned) buf[1] << 8 | buf[0]);
*(unsigned long int *) buf = t;
*(uint32_t *) buf = t;
buf += 4;
} while (--longs);
}
Expand Down Expand Up @@ -75,10 +75,10 @@ static void MD5Init(struct MD5Context *ctx)
* reflect the addition of 16 longwords of new data. MD5Update blocks
* the data and converts bytes into longwords for this routine.
*/
static void MD5Transform(unsigned long int buf[4],
unsigned long int const in[16])
static void MD5Transform(uint32_t buf[4],
uint32_t const in[16])
{
register unsigned long int a, b, c, d;
register uint32_t a, b, c, d;

a = buf[0];
b = buf[1];
Expand Down Expand Up @@ -166,12 +166,12 @@ static void MD5Transform(unsigned long int buf[4],
static void MD5Update(struct MD5Context *ctx, unsigned char const *buf,
unsigned len)
{
register unsigned long int t;
uint32_t t;

/* Update bitcount */

t = ctx->bits[0];
if ((ctx->bits[0] = t + ((unsigned long int) len << 3)) < t)
if ((ctx->bits[0] = t + ((uint32_t) len << 3)) < t)
ctx->bits[1]++; /* Carry from low to high */
ctx->bits[1] += len >> 29;

Expand All @@ -189,7 +189,7 @@ static void MD5Update(struct MD5Context *ctx, unsigned char const *buf,
}
memcpy(p, buf, t);
byteReverse(ctx->in, 16);
MD5Transform(ctx->buf, (unsigned long int *) ctx->in);
MD5Transform(ctx->buf, (uint32_t *) ctx->in);
buf += t;
len -= t;
}
Expand All @@ -198,7 +198,7 @@ static void MD5Update(struct MD5Context *ctx, unsigned char const *buf,
while (len >= 64) {
memcpy(ctx->in, buf, 64);
byteReverse(ctx->in, 16);
MD5Transform(ctx->buf, (unsigned long int *) ctx->in);
MD5Transform(ctx->buf, (uint32_t *) ctx->in);
buf += 64;
len -= 64;
}
Expand All @@ -215,7 +215,7 @@ static void MD5Update(struct MD5Context *ctx, unsigned char const *buf,
*/
static void MD5Final(struct MD5Context *ctx, unsigned char *digest)
{
unsigned long int count;
unsigned count;
unsigned char *p;

/* Compute number of bytes mod 64 */
Expand All @@ -234,7 +234,7 @@ static void MD5Final(struct MD5Context *ctx, unsigned char *digest)
/* Two lots of padding: Pad the first block to 64 bytes */
memset(p, 0, count);
byteReverse(ctx->in, 16);
MD5Transform(ctx->buf, (unsigned long int *) ctx->in);
MD5Transform(ctx->buf, (uint32_t *) ctx->in);

/* Now fill the next block with 56 bytes */
memset(ctx->in, 0, 56);
Expand All @@ -245,15 +245,15 @@ static void MD5Final(struct MD5Context *ctx, unsigned char *digest)
byteReverse(ctx->in, 14);

/* Append length in bits and transform */
((unsigned long int *) ctx->in)[14] = ctx->bits[0];
((unsigned long int *) ctx->in)[15] = ctx->bits[1];
((uint32_t *) ctx->in)[14] = ctx->bits[0];
((uint32_t *) ctx->in)[15] = ctx->bits[1];

MD5Transform(ctx->buf, (unsigned long int *) ctx->in);
MD5Transform(ctx->buf, (uint32_t *) ctx->in);
byteReverse((unsigned char *) ctx->buf, 4);

if (digest!=NULL)
memcpy(digest, ctx->buf, 16);
//memset(ctx, 0, sizeof(ctx)); /* In case it's sensitive */
memset(ctx, 0, sizeof(ctx)); /* In case it's sensitive */
}


Expand Down
11 changes: 10 additions & 1 deletion code/qcommon/q_shared.h
Expand Up @@ -109,10 +109,19 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
//=============================================================

#ifdef Q3_VM
typedef int intptr_t;
typedef int intptr_t;
#else
# ifndef _MSC_VER
# include <stdint.h>
# else
typedef __int64 int64_t;
typedef __int32 int32_t;
typedef __int16 int16_t;
typedef __int8 int8_t;
typedef unsigned __int64 uint64_t;
typedef unsigned __int32 uint32_t;
typedef unsigned __int16 uint16_t;
typedef unsigned __int8 uint8_t;
# endif
#endif

Expand Down

0 comments on commit 235e2c2

Please sign in to comment.