Commit df6350a8 authored by Wayne Davison's avatar Wayne Davison

Avoid type-punned compiler warnings for the byteorder.h macros

by using inline functions for the 4-char <-> uint32 conversions.
parent 7a7810aa
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
*/ */
#undef CAREFUL_ALIGNMENT #undef CAREFUL_ALIGNMENT
#undef AVOID_BYTEORDER_INLINE
/* We know that the x86 can handle misalignment and has the same /* We know that the x86 can handle misalignment and has the same
* byte order (LSB-first) as the 32-bit numbers we transmit. */ * byte order (LSB-first) as the 32-bit numbers we transmit. */
...@@ -32,21 +33,68 @@ ...@@ -32,21 +33,68 @@
#define CVAL(buf,pos) (((unsigned char *)(buf))[pos]) #define CVAL(buf,pos) (((unsigned char *)(buf))[pos])
#define UVAL(buf,pos) ((uint32)CVAL(buf,pos)) #define UVAL(buf,pos) ((uint32)CVAL(buf,pos))
#define SCVAL(buf,pos,val) (CVAL(buf,pos) = (val))
#if CAREFUL_ALIGNMENT #if CAREFUL_ALIGNMENT
#define PVAL(buf,pos) (UVAL(buf,pos)|UVAL(buf,(pos)+1)<<8) #define PVAL(buf,pos) (UVAL(buf,pos)|UVAL(buf,(pos)+1)<<8)
#define IVAL(buf,pos) (PVAL(buf,pos)|PVAL(buf,(pos)+2)<<16) #define IVAL(buf,pos) (PVAL(buf,pos)|PVAL(buf,(pos)+2)<<16)
#define SSVALX(buf,pos,val) (CVAL(buf,pos)=(val)&0xFF,CVAL(buf,pos+1)=(val)>>8) #define SSVALX(buf,pos,val) (CVAL(buf,pos)=(val)&0xFF,CVAL(buf,pos+1)=(val)>>8)
#define SIVALX(buf,pos,val) (SSVALX(buf,pos,val&0xFFFF),SSVALX(buf,pos+2,val>>16)) #define SIVALX(buf,pos,val) (SSVALX(buf,pos,val&0xFFFF),SSVALX(buf,pos+2,val>>16))
#define SIVAL(buf,pos,val) SIVALX((buf),(pos),((uint32)(val))) #define SIVAL(buf,pos,val) SIVALX((buf),(pos),((uint32)(val)))
#else
/* this handles things for architectures like the 386 that can handle #define IVALu(buf,pos) IVAL(buf,pos)
alignment errors */ #define SIVALu(buf,pos,val) SIVAL(buf,pos,val)
/*
WARNING: This section is dependent on the length of int32 #else /* !CAREFUL_ALIGNMENT */
being correct. set CAREFUL_ALIGNMENT if it is not.
*/ /* This handles things for architectures like the 386 that can handle alignment errors.
* WARNING: This section is dependent on the length of an int32 (and thus a uint32)
* being correct (4 bytes)! Set CAREFUL_ALIGNMENT if it is not. */
# ifdef AVOID_BYTEORDER_INLINE
#define IVAL(buf,pos) (*(uint32 *)((char *)(buf) + (pos))) #define IVAL(buf,pos) (*(uint32 *)((char *)(buf) + (pos)))
#define SIVAL(buf,pos,val) IVAL(buf,pos)=((uint32)(val)) #define SIVAL(buf,pos,val) IVAL(buf,pos)=((uint32)(val))
#endif
#define IVALu(buf,pos) IVAL(buf,pos)
#define SIVALu(buf,pos,val) SIVAL(buf,pos,val)
# else /* !AVOID_BYTEORDER_INLINE */
static inline uint32
IVALu(const uchar *buf, int pos)
{
union {
const uchar *b;
const uint32 *num;
} u;
u.b = buf + pos;
return *u.num;
}
static inline void
SIVALu(uchar *buf, int pos, uint32 val)
{
union {
uchar *b;
uint32 *num;
} u;
u.b = buf + pos;
*u.num = val;
}
static inline uint32
IVAL(const char *buf, int pos)
{
return IVALu((uchar*)buf, pos);
}
static inline void
SIVAL(char *buf, int pos, uint32 val)
{
SIVALu((uchar*)buf, pos, val);
}
# endif /* !AVOID_BYTEORDER_INLINE */
#endif /* !CAREFUL_ALIGNMENT */
...@@ -56,7 +56,7 @@ void get_checksum2(char *buf, int32 len, char *sum) ...@@ -56,7 +56,7 @@ void get_checksum2(char *buf, int32 len, char *sum)
md5_begin(&m); md5_begin(&m);
md5_update(&m, (uchar *)buf, len); md5_update(&m, (uchar *)buf, len);
if (checksum_seed) { if (checksum_seed) {
SIVAL(seedbuf, 0, checksum_seed); SIVALu(seedbuf, 0, checksum_seed);
md5_update(&m, seedbuf, 4); md5_update(&m, seedbuf, 4);
} }
md5_result(&m, (uchar *)sum); md5_result(&m, (uchar *)sum);
......
...@@ -106,7 +106,7 @@ void *hashtable_find(struct hashtable *tbl, int64 key, int allocate_if_missing) ...@@ -106,7 +106,7 @@ void *hashtable_find(struct hashtable *tbl, int64 key, int allocate_if_missing)
uchar buf[4], *keyp = buf; uchar buf[4], *keyp = buf;
int i; int i;
SIVAL(buf, 0, key); SIVALu(buf, 0, key);
for (ndx = 0, i = 0; i < 4; i++) { for (ndx = 0, i = 0; i < 4; i++) {
ndx += keyp[i]; ndx += keyp[i];
ndx += (ndx << 10); ndx += (ndx << 10);
......
...@@ -38,22 +38,22 @@ static void md5_process(md_context *ctx, const uchar data[CSUM_CHUNK]) ...@@ -38,22 +38,22 @@ static void md5_process(md_context *ctx, const uchar data[CSUM_CHUNK])
C = ctx->C; C = ctx->C;
D = ctx->D; D = ctx->D;
X[0] = IVAL(data, 0); X[0] = IVALu(data, 0);
X[1] = IVAL(data, 4); X[1] = IVALu(data, 4);
X[2] = IVAL(data, 8); X[2] = IVALu(data, 8);
X[3] = IVAL(data, 12); X[3] = IVALu(data, 12);
X[4] = IVAL(data, 16); X[4] = IVALu(data, 16);
X[5] = IVAL(data, 20); X[5] = IVALu(data, 20);
X[6] = IVAL(data, 24); X[6] = IVALu(data, 24);
X[7] = IVAL(data, 28); X[7] = IVALu(data, 28);
X[8] = IVAL(data, 32); X[8] = IVALu(data, 32);
X[9] = IVAL(data, 36); X[9] = IVALu(data, 36);
X[10] = IVAL(data, 40); X[10] = IVALu(data, 40);
X[11] = IVAL(data, 44); X[11] = IVALu(data, 44);
X[12] = IVAL(data, 48); X[12] = IVALu(data, 48);
X[13] = IVAL(data, 52); X[13] = IVALu(data, 52);
X[14] = IVAL(data, 56); X[14] = IVALu(data, 56);
X[15] = IVAL(data, 60); X[15] = IVALu(data, 60);
#define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n))) #define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))
...@@ -192,8 +192,8 @@ void md5_result(md_context *ctx, uchar digest[MD5_DIGEST_LEN]) ...@@ -192,8 +192,8 @@ void md5_result(md_context *ctx, uchar digest[MD5_DIGEST_LEN])
| (ctx->totalN2 << 3); | (ctx->totalN2 << 3);
low = (ctx->totalN << 3); low = (ctx->totalN << 3);
SIVAL(msglen, 0, low); SIVALu(msglen, 0, low);
SIVAL(msglen, 4, high); SIVALu(msglen, 4, high);
last = ctx->totalN & 0x3F; last = ctx->totalN & 0x3F;
padn = last < 56 ? 56 - last : 120 - last; padn = last < 56 ? 56 - last : 120 - last;
...@@ -201,10 +201,10 @@ void md5_result(md_context *ctx, uchar digest[MD5_DIGEST_LEN]) ...@@ -201,10 +201,10 @@ void md5_result(md_context *ctx, uchar digest[MD5_DIGEST_LEN])
md5_update(ctx, md5_padding, padn); md5_update(ctx, md5_padding, padn);
md5_update(ctx, msglen, 8); md5_update(ctx, msglen, 8);
SIVAL(digest, 0, ctx->A); SIVALu(digest, 0, ctx->A);
SIVAL(digest, 4, ctx->B); SIVALu(digest, 4, ctx->B);
SIVAL(digest, 8, ctx->C); SIVALu(digest, 8, ctx->C);
SIVAL(digest, 12, ctx->D); SIVALu(digest, 12, ctx->D);
} }
void get_md5(uchar *out, const uchar *input, int n) void get_md5(uchar *out, const uchar *input, int n)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment