diff options
author | Thilo Schulz <arny@ats.s.bawue.de> | 2011-07-17 01:41:39 +0000 |
---|---|---|
committer | Tim Angus <tim@ngus.net> | 2013-01-10 22:30:00 +0000 |
commit | 8573ded969056c0029e1103944b60f8dc472f185 (patch) | |
tree | 037ca502b6600ac444f83fe3b815d23aaa4ed5c3 /src/qcommon | |
parent | 3155cba037535b8a703498f85ec9ad5e1d6f2f52 (diff) |
Fix alignment issues in message sending/reading that would crash IRIX, thanks to Canavan for supplying a shell where I could fix this (#5077)
Diffstat (limited to 'src/qcommon')
-rw-r--r-- | src/qcommon/msg.c | 47 | ||||
-rw-r--r-- | src/qcommon/q_platform.h | 4 | ||||
-rw-r--r-- | src/qcommon/q_shared.c | 18 |
3 files changed, 51 insertions, 18 deletions
diff --git a/src/qcommon/msg.c b/src/qcommon/msg.c index d20803b4..580637eb 100644 --- a/src/qcommon/msg.c +++ b/src/qcommon/msg.c @@ -141,23 +141,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)); @@ -199,23 +204,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/src/qcommon/q_platform.h b/src/qcommon/q_platform.h index 85b176eb..f4335489 100644 --- a/src/qcommon/q_platform.h +++ b/src/qcommon/q_platform.h @@ -349,6 +349,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) @@ -358,6 +360,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/src/qcommon/q_shared.c b/src/qcommon/q_shared.c index d6aa5c90..d8bc7383 100644 --- a/src/qcommon/q_shared.c +++ b/src/qcommon/q_shared.c @@ -130,6 +130,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; |