diff --git a/code/qcommon/msg.c b/code/qcommon/msg.c index 9916b1b3..e7d851fc 100644 --- a/code/qcommon/msg.c +++ b/code/qcommon/msg.c @@ -140,23 +140,28 @@ void MSG_WriteBits( msg_t *msg, int value, int bits ) { bits = -bits; } if (msg->oob) { - if (bits==8) { + if(bits==8) + { msg->data[msg->cursize] = value; msg->cursize += 1; msg->bit += 8; - } else if (bits==16) { - unsigned short *sp = (unsigned short *)&msg->data[msg->cursize]; - *sp = LittleShort(value); + } + else if(bits==16) + { + short temp = value; + + CopyLittleShort(&msg->data[msg->cursize], &temp); msg->cursize += 2; msg->bit += 16; - } else if (bits==32) { - unsigned int *ip = (unsigned int *)&msg->data[msg->cursize]; - *ip = LittleLong(value); + } + else if(bits==32) + { + CopyLittleLong(&msg->data[msg->cursize], &value); msg->cursize += 4; msg->bit += 32; - } else { - Com_Error(ERR_DROP, "can't read %d bits", bits); } + else + Com_Error(ERR_DROP, "can't write %d bits", bits); } else { // fp = fopen("c:\\netchan.bin", "a"); value &= (0xffffffff>>(32-bits)); @@ -198,23 +203,29 @@ int MSG_ReadBits( msg_t *msg, int bits ) { } if (msg->oob) { - if (bits==8) { + if(bits==8) + { value = msg->data[msg->readcount]; msg->readcount += 1; msg->bit += 8; - } else if (bits==16) { - unsigned short *sp = (unsigned short *)&msg->data[msg->readcount]; - value = LittleShort(*sp); + } + else if(bits==16) + { + short temp; + + CopyLittleShort(&temp, &msg->data[msg->readcount]); + value = temp; msg->readcount += 2; msg->bit += 16; - } else if (bits==32) { - unsigned int *ip = (unsigned int *)&msg->data[msg->readcount]; - value = LittleLong(*ip); + } + else if(bits==32) + { + CopyLittleLong(&value, &msg->data[msg->readcount]); msg->readcount += 4; msg->bit += 32; - } else { - Com_Error(ERR_DROP, "can't read %d bits", bits); } + else + Com_Error(ERR_DROP, "can't read %d bits", bits); } else { nbits = 0; if (bits&7) { diff --git a/code/qcommon/q_platform.h b/code/qcommon/q_platform.h index 4477673d..d61ccc6b 100644 --- a/code/qcommon/q_platform.h +++ b/code/qcommon/q_platform.h @@ -348,6 +348,8 @@ float FloatSwap (const float *f); #error "Endianness defined as both big and little" #elif defined( Q3_BIG_ENDIAN ) +#define CopyLittleShort(dest, src) CopyShortSwap(dest, src) +#define CopyLittleLong(dest, src) CopyLongSwap(dest, src) #define LittleShort(x) ShortSwap(x) #define LittleLong(x) LongSwap(x) #define LittleFloat(x) FloatSwap(&x) @@ -357,6 +359,8 @@ float FloatSwap (const float *f); #elif defined( Q3_LITTLE_ENDIAN ) +#define CopyLittleShort(dest, src) Com_Memcpy(dest, src, 2) +#define CopyLittleLong(dest, src) Com_Memcpy(dest, src, 4) #define LittleShort #define LittleLong #define LittleFloat diff --git a/code/qcommon/q_shared.c b/code/qcommon/q_shared.c index 2390bc9a..4d6828dc 100644 --- a/code/qcommon/q_shared.c +++ b/code/qcommon/q_shared.c @@ -129,6 +129,24 @@ float BigFloat (const float *l) {return _BigFloat(l);} float LittleFloat (const float *l) {return _LittleFloat(l);} */ +void CopyShortSwap(void *dest, void *src) +{ + byte *to = dest, *from = src; + + to[0] = from[1]; + to[1] = from[0]; +} + +void CopyLongSwap(void *dest, void *src) +{ + byte *to = dest, *from = src; + + to[0] = from[3]; + to[1] = from[2]; + to[2] = from[1]; + to[3] = from[0]; +} + short ShortSwap (short l) { byte b1,b2;