diff --git a/include/QF/msg.h b/include/QF/msg.h index 310584b2f..d10d435ae 100644 --- a/include/QF/msg.h +++ b/include/QF/msg.h @@ -36,6 +36,7 @@ void MSG_WriteByte (sizebuf_t *sb, int c); void MSG_WriteShort (sizebuf_t *sb, int c); +void MSG_WriteShortBE (sizebuf_t *sb, int c); void MSG_WriteLong (sizebuf_t *sb, int c); void MSG_WriteLongBE (sizebuf_t *sb, int c); void MSG_WriteFloat (sizebuf_t *sb, float f); @@ -96,6 +97,16 @@ int MSG_ReadByte (qmsg_t *msg); */ int MSG_ReadShort (qmsg_t *msg); +/** Read a single big-endian unsigned short from the message. + + Advances the read index. + + \param msg The message from which the short will be read. + \return The short value (0 - 65535), or -1 if already at + the end of the message. +*/ +int MSG_ReadShortBE (qmsg_t *msg); + /** Read a single little-endian long from the message. Advances the read index. diff --git a/libs/util/msg.c b/libs/util/msg.c index e083c1b39..92596dcc6 100644 --- a/libs/util/msg.c +++ b/libs/util/msg.c @@ -68,6 +68,16 @@ MSG_WriteShort (sizebuf_t *sb, int c) *buf = ((unsigned int) c) >> 8; } +VISIBLE void +MSG_WriteShortBE (sizebuf_t *sb, int c) +{ + byte *buf; + + buf = SZ_GetSpace (sb, 2); + *buf++ = ((unsigned int) c) >> 8; + *buf = ((unsigned int) c) & 0xff; +} + VISIBLE void MSG_WriteLong (sizebuf_t *sb, int c) { @@ -265,8 +275,26 @@ MSG_ReadShort (qmsg_t *msg) int c; if (msg->readcount + 2 <= msg->message->cursize) { - c = (msg->message->data[msg->readcount] - + (msg->message->data[msg->readcount + 1] << 8)); + byte *buf = msg->message->data + msg->readcount; + c = *buf++; + c |= (*buf) << 8; + msg->readcount += 2; + return c; + } + msg->readcount = msg->message->cursize; + msg->badread = true; + return -1; +} + +VISIBLE int +MSG_ReadShortBE (qmsg_t *msg) +{ + int c; + + if (msg->readcount + 2 <= msg->message->cursize) { + byte *buf = msg->message->data + msg->readcount; + c = (*buf++) << 8; + c |= *buf; msg->readcount += 2; return c; }