2001-02-23 23:16:13 +00:00
|
|
|
/*
|
|
|
|
msg.c
|
|
|
|
|
|
|
|
(description)
|
|
|
|
|
|
|
|
Copyright (C) 1996-1997 Id Software, Inc.
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
|
|
|
See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to:
|
|
|
|
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
59 Temple Place - Suite 330
|
|
|
|
Boston, MA 02111-1307, USA
|
|
|
|
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
2003-01-15 15:31:36 +00:00
|
|
|
|
2005-08-04 15:27:09 +00:00
|
|
|
static __attribute__ ((used)) const char rcsid[] =
|
2003-01-15 15:31:36 +00:00
|
|
|
"$Id$";
|
|
|
|
|
2001-02-23 23:16:13 +00:00
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif
|
|
|
|
|
2001-03-27 20:33:07 +00:00
|
|
|
#include "QF/msg.h"
|
|
|
|
#include "QF/qendian.h"
|
|
|
|
#include "QF/sys.h"
|
2001-02-23 23:16:13 +00:00
|
|
|
|
2001-10-18 04:44:58 +00:00
|
|
|
#include "compat.h"
|
2001-08-22 22:03:16 +00:00
|
|
|
|
2001-02-23 23:16:13 +00:00
|
|
|
/*
|
|
|
|
MESSAGE IO FUNCTIONS
|
|
|
|
|
|
|
|
Handles byte ordering and avoids alignment errors
|
|
|
|
*/
|
|
|
|
|
2002-07-02 19:13:53 +00:00
|
|
|
// writing functions ==========================================================
|
2001-02-23 23:16:13 +00:00
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2002-08-19 01:58:01 +00:00
|
|
|
MSG_WriteByte (sizebuf_t *sb, int c)
|
2001-02-23 23:16:13 +00:00
|
|
|
{
|
2002-06-28 04:48:07 +00:00
|
|
|
byte *buf;
|
2001-02-23 23:16:13 +00:00
|
|
|
|
|
|
|
buf = SZ_GetSpace (sb, 1);
|
2002-06-28 04:48:07 +00:00
|
|
|
*buf = c;
|
2001-02-23 23:16:13 +00:00
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2002-08-19 01:58:01 +00:00
|
|
|
MSG_WriteShort (sizebuf_t *sb, int c)
|
2001-02-23 23:16:13 +00:00
|
|
|
{
|
2002-06-28 04:48:07 +00:00
|
|
|
byte *buf;
|
2001-02-23 23:16:13 +00:00
|
|
|
|
|
|
|
buf = SZ_GetSpace (sb, 2);
|
2002-08-19 01:58:01 +00:00
|
|
|
*buf++ = ((unsigned int) c) & 0xff;
|
|
|
|
*buf = ((unsigned int) c) >> 8;
|
2001-02-23 23:16:13 +00:00
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2002-08-19 01:58:01 +00:00
|
|
|
MSG_WriteLong (sizebuf_t *sb, int c)
|
2001-02-23 23:16:13 +00:00
|
|
|
{
|
2002-06-28 04:48:07 +00:00
|
|
|
byte *buf;
|
2001-02-23 23:16:13 +00:00
|
|
|
|
|
|
|
buf = SZ_GetSpace (sb, 4);
|
2002-08-19 01:58:01 +00:00
|
|
|
*buf++ = ((unsigned int) c) & 0xff;
|
|
|
|
*buf++ = (((unsigned int) c) >> 8) & 0xff;
|
|
|
|
*buf++ = (((unsigned int) c) >> 16) & 0xff;
|
|
|
|
*buf = ((unsigned int) c) >> 24;
|
2001-02-23 23:16:13 +00:00
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2001-02-23 23:16:13 +00:00
|
|
|
MSG_WriteFloat (sizebuf_t *sb, float f)
|
|
|
|
{
|
|
|
|
union {
|
|
|
|
float f;
|
2001-12-03 22:11:21 +00:00
|
|
|
unsigned int l;
|
2001-02-23 23:16:13 +00:00
|
|
|
} dat;
|
|
|
|
|
|
|
|
dat.f = f;
|
|
|
|
dat.l = LittleLong (dat.l);
|
|
|
|
|
|
|
|
SZ_Write (sb, &dat.l, 4);
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2001-07-15 07:04:17 +00:00
|
|
|
MSG_WriteString (sizebuf_t *sb, const char *s)
|
2001-02-23 23:16:13 +00:00
|
|
|
{
|
|
|
|
if (!s)
|
|
|
|
SZ_Write (sb, "", 1);
|
|
|
|
else
|
|
|
|
SZ_Write (sb, s, strlen (s) + 1);
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2001-12-12 21:56:09 +00:00
|
|
|
MSG_WriteCoord (sizebuf_t *sb, float coord)
|
2001-02-23 23:16:13 +00:00
|
|
|
{
|
2002-08-19 00:05:51 +00:00
|
|
|
MSG_WriteShort (sb, (int) (coord * 8.0));
|
2001-02-23 23:16:13 +00:00
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2002-01-03 05:29:38 +00:00
|
|
|
MSG_WriteCoordV (sizebuf_t *sb, const vec3_t coord)
|
2001-02-23 23:16:13 +00:00
|
|
|
{
|
2002-08-19 00:05:51 +00:00
|
|
|
byte *buf;
|
|
|
|
unsigned int i, j;
|
2001-12-12 21:56:09 +00:00
|
|
|
|
2002-06-28 04:48:07 +00:00
|
|
|
buf = SZ_GetSpace (sb, 6);
|
|
|
|
for (i = 0; i < 3; i++) {
|
2002-08-19 00:05:51 +00:00
|
|
|
j = (int) (coord[i] * 8.0);
|
2002-06-28 04:48:07 +00:00
|
|
|
*buf++ = j & 0xff;
|
|
|
|
*buf++ = j >> 8;
|
|
|
|
}
|
2001-12-12 21:56:09 +00:00
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2002-01-03 05:29:38 +00:00
|
|
|
MSG_WriteCoordAngleV (sizebuf_t *sb, const vec3_t coord, const vec3_t angles)
|
2001-12-12 21:56:09 +00:00
|
|
|
{
|
2002-06-28 04:48:07 +00:00
|
|
|
byte *buf;
|
|
|
|
int i, j;
|
2001-12-12 21:56:09 +00:00
|
|
|
|
2002-06-28 04:48:07 +00:00
|
|
|
buf = SZ_GetSpace (sb, 9);
|
2001-12-12 21:56:09 +00:00
|
|
|
for (i = 0; i < 3; i++) {
|
2002-08-19 00:05:51 +00:00
|
|
|
j = (int) (coord[i] * 8.0);
|
2002-06-28 04:48:07 +00:00
|
|
|
*buf++ = j & 0xff;
|
|
|
|
*buf++ = j >> 8;
|
2002-08-19 00:05:51 +00:00
|
|
|
*buf++ = ((int) (angles[i] * (256.0 / 360.0)) & 255);
|
2001-12-12 21:56:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2001-12-12 21:56:09 +00:00
|
|
|
MSG_WriteAngle (sizebuf_t *sb, float angle)
|
|
|
|
{
|
2002-08-19 00:05:51 +00:00
|
|
|
MSG_WriteByte (sb, (int) (angle * (256.0 / 360.0)) & 255);
|
2001-12-12 21:56:09 +00:00
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2002-01-03 05:29:38 +00:00
|
|
|
MSG_WriteAngleV (sizebuf_t *sb, const vec3_t angles)
|
2001-12-12 21:56:09 +00:00
|
|
|
{
|
2002-06-28 04:48:07 +00:00
|
|
|
byte *buf;
|
2001-12-12 21:56:09 +00:00
|
|
|
int i;
|
|
|
|
|
2002-06-28 04:48:07 +00:00
|
|
|
buf = SZ_GetSpace (sb, 3);
|
|
|
|
for (i = 0; i < 3; i++) {
|
2002-08-19 00:05:51 +00:00
|
|
|
*buf++ = ((int) (angles[i] * (256.0 / 360.0)) & 255);
|
2002-06-28 04:48:07 +00:00
|
|
|
}
|
2001-02-23 23:16:13 +00:00
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2001-12-12 21:56:09 +00:00
|
|
|
MSG_WriteAngle16 (sizebuf_t *sb, float angle16)
|
2001-02-23 23:16:13 +00:00
|
|
|
{
|
2002-08-19 00:05:51 +00:00
|
|
|
MSG_WriteShort (sb, (int) (angle16 * (65536.0 / 360.0)) & 65535);
|
2001-02-23 23:16:13 +00:00
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2005-05-05 00:27:04 +00:00
|
|
|
MSG_WriteUTF8 (sizebuf_t *sb, unsigned utf8)
|
|
|
|
{
|
|
|
|
byte *buf;
|
|
|
|
int count;
|
|
|
|
|
|
|
|
if (utf8 & 0x80000000) {
|
|
|
|
return; // invalid (FIXME die?)
|
|
|
|
} else if (utf8 & 0x7c000000) {
|
|
|
|
buf = SZ_GetSpace (sb, count = 6);
|
|
|
|
*buf = 0xfc | ((utf8 & 0x40000000) >> 30); // 1 bit
|
|
|
|
utf8 <<= 2;
|
|
|
|
} else if (utf8 & 0x03e00000) {
|
|
|
|
buf = SZ_GetSpace (sb, count = 5);
|
|
|
|
*buf = 0xf8 | ((utf8 & 0x30000000) >> 28); // 2 bits
|
|
|
|
utf8 <<= 4;
|
|
|
|
} else if (utf8 & 0x001f0000) {
|
|
|
|
buf = SZ_GetSpace (sb, count = 4);
|
|
|
|
*buf = 0xf0 | ((utf8 & 0x001c0000) >> 18); // 3 bits
|
|
|
|
utf8 <<= 14;
|
|
|
|
} else if (utf8 & 0x0000f800) {
|
|
|
|
buf = SZ_GetSpace (sb, count = 3);
|
|
|
|
*buf = 0xe0 | ((utf8 & 0x0000f000) >> 12); // 4 bits
|
|
|
|
utf8 <<= 20;
|
|
|
|
} else if (utf8 & 0x00000780) {
|
|
|
|
buf = SZ_GetSpace (sb, count = 2);
|
|
|
|
*buf = 0xc0 | ((utf8 & 0x00000780) >> 7); // 5 bits
|
|
|
|
utf8 <<= 25;
|
|
|
|
} else {
|
|
|
|
buf = SZ_GetSpace (sb, count = 1);
|
|
|
|
*buf = utf8;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
while (--count) {
|
|
|
|
*buf++ = 0x80 | ((utf8 & 0xfc000000) >> 26);
|
|
|
|
utf8 <<= 6;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-07-02 19:13:53 +00:00
|
|
|
// reading functions ==========================================================
|
2001-02-23 23:16:13 +00:00
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2004-01-20 06:11:00 +00:00
|
|
|
MSG_BeginReading (qmsg_t *msg)
|
2001-02-23 23:16:13 +00:00
|
|
|
{
|
|
|
|
msg->readcount = 0;
|
|
|
|
msg->badread = false;
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE int
|
2004-01-20 06:11:00 +00:00
|
|
|
MSG_GetReadCount (qmsg_t *msg)
|
2001-02-23 23:16:13 +00:00
|
|
|
{
|
|
|
|
return msg->readcount;
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE int
|
2004-01-20 06:11:00 +00:00
|
|
|
MSG_ReadByte (qmsg_t *msg)
|
2001-02-23 23:16:13 +00:00
|
|
|
{
|
2001-12-03 08:46:56 +00:00
|
|
|
if (msg->readcount + 1 <= msg->message->cursize)
|
|
|
|
return (unsigned char) msg->message->data[msg->readcount++];
|
2001-02-23 23:16:13 +00:00
|
|
|
|
2001-12-03 08:46:56 +00:00
|
|
|
msg->badread = true;
|
|
|
|
return -1;
|
2001-02-23 23:16:13 +00:00
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE int
|
2004-01-20 06:11:00 +00:00
|
|
|
MSG_ReadShort (qmsg_t *msg)
|
2001-02-23 23:16:13 +00:00
|
|
|
{
|
|
|
|
int c;
|
|
|
|
|
2001-12-03 08:46:56 +00:00
|
|
|
if (msg->readcount + 2 <= msg->message->cursize) {
|
|
|
|
c = (short) (msg->message->data[msg->readcount]
|
|
|
|
+ (msg->message->data[msg->readcount + 1] << 8));
|
|
|
|
msg->readcount += 2;
|
|
|
|
return c;
|
2001-02-23 23:16:13 +00:00
|
|
|
}
|
2001-12-03 08:46:56 +00:00
|
|
|
msg->readcount = msg->message->cursize;
|
|
|
|
msg->badread = true;
|
|
|
|
return -1;
|
2001-02-23 23:16:13 +00:00
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE int
|
2004-01-20 06:11:00 +00:00
|
|
|
MSG_ReadLong (qmsg_t *msg)
|
2001-02-23 23:16:13 +00:00
|
|
|
{
|
|
|
|
int c;
|
|
|
|
|
2001-12-03 08:46:56 +00:00
|
|
|
if (msg->readcount + 4 <= msg->message->cursize) {
|
|
|
|
c = msg->message->data[msg->readcount]
|
|
|
|
+ (msg->message->data[msg->readcount + 1] << 8)
|
|
|
|
+ (msg->message->data[msg->readcount + 2] << 16)
|
|
|
|
+ (msg->message->data[msg->readcount + 3] << 24);
|
|
|
|
msg->readcount += 4;
|
|
|
|
return c;
|
2001-02-23 23:16:13 +00:00
|
|
|
}
|
2001-12-03 08:46:56 +00:00
|
|
|
msg->readcount = msg->message->cursize;
|
|
|
|
msg->badread = true;
|
|
|
|
return -1;
|
2001-02-23 23:16:13 +00:00
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE float
|
2004-01-20 06:11:00 +00:00
|
|
|
MSG_ReadFloat (qmsg_t *msg)
|
2001-02-23 23:16:13 +00:00
|
|
|
{
|
|
|
|
union {
|
|
|
|
byte b[4];
|
|
|
|
float f;
|
|
|
|
int l;
|
|
|
|
} dat;
|
|
|
|
|
2001-12-03 08:46:56 +00:00
|
|
|
if (msg->readcount + 4 <= msg->message->cursize) {
|
|
|
|
dat.b[0] = msg->message->data[msg->readcount];
|
|
|
|
dat.b[1] = msg->message->data[msg->readcount + 1];
|
|
|
|
dat.b[2] = msg->message->data[msg->readcount + 2];
|
|
|
|
dat.b[3] = msg->message->data[msg->readcount + 3];
|
|
|
|
msg->readcount += 4;
|
2001-10-18 04:14:18 +00:00
|
|
|
|
2001-12-03 08:46:56 +00:00
|
|
|
dat.l = LittleLong (dat.l);
|
2001-02-23 23:16:13 +00:00
|
|
|
|
2001-12-03 08:46:56 +00:00
|
|
|
return dat.f;
|
|
|
|
}
|
2001-02-23 23:16:13 +00:00
|
|
|
|
2001-12-03 08:46:56 +00:00
|
|
|
msg->readcount = msg->message->cursize;
|
|
|
|
msg->badread = true;
|
|
|
|
return -1;
|
2001-02-23 23:16:13 +00:00
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE const char *
|
2004-01-20 06:11:00 +00:00
|
|
|
MSG_ReadString (qmsg_t *msg)
|
2001-10-18 04:44:58 +00:00
|
|
|
{
|
|
|
|
char *string;
|
2003-04-17 00:01:48 +00:00
|
|
|
size_t len, maxlen;
|
2001-10-18 04:44:58 +00:00
|
|
|
|
2001-10-18 06:23:26 +00:00
|
|
|
if (msg->badread || msg->readcount + 1 > msg->message->cursize) {
|
2001-10-18 04:44:58 +00:00
|
|
|
msg->badread = true;
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2005-06-08 06:35:48 +00:00
|
|
|
string = (char *) &msg->message->data[msg->readcount];
|
2001-10-18 04:44:58 +00:00
|
|
|
|
|
|
|
maxlen = msg->message->cursize - msg->readcount;
|
|
|
|
len = strnlen (string, maxlen);
|
|
|
|
if (len == maxlen) {
|
2001-10-18 06:23:26 +00:00
|
|
|
msg->readcount = msg->readcount;
|
2001-10-18 04:44:58 +00:00
|
|
|
msg->badread = true;
|
2001-10-18 06:23:26 +00:00
|
|
|
if (len + 1 > msg->badread_string_size) {
|
|
|
|
if (msg->badread_string)
|
|
|
|
free (msg->badread_string);
|
|
|
|
msg->badread_string = malloc (len + 1);
|
|
|
|
msg->badread_string_size = len + 1;
|
|
|
|
}
|
2001-10-23 17:35:46 +00:00
|
|
|
if (!msg->badread_string)
|
2002-05-14 06:12:29 +00:00
|
|
|
Sys_Error ("MSG_ReadString: out of memory");
|
2001-10-18 06:23:26 +00:00
|
|
|
strncpy (msg->badread_string, string, len);
|
|
|
|
msg->badread_string[len] = 0;
|
|
|
|
return msg->badread_string;
|
2001-10-18 04:44:58 +00:00
|
|
|
}
|
|
|
|
msg->readcount += len + 1;
|
|
|
|
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE int
|
2005-05-02 09:00:17 +00:00
|
|
|
MSG_ReadBytes (qmsg_t *msg, void *buf, int len)
|
|
|
|
{
|
|
|
|
if (msg->badread || len > msg->message->cursize - msg->readcount) {
|
|
|
|
msg->badread = true;
|
|
|
|
len = msg->message->cursize - msg->readcount;
|
|
|
|
}
|
|
|
|
memcpy (buf, msg->message->data + msg->readcount, len);
|
|
|
|
msg->readcount += len;
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE float
|
2004-01-20 06:11:00 +00:00
|
|
|
MSG_ReadCoord (qmsg_t *msg)
|
2001-02-23 23:16:13 +00:00
|
|
|
{
|
2001-12-11 22:37:30 +00:00
|
|
|
return MSG_ReadShort (msg) * (1.0 / 8.0);
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2004-01-20 06:11:00 +00:00
|
|
|
MSG_ReadCoordV (qmsg_t *msg, vec3_t coord)
|
2001-12-12 21:56:09 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < 3; i++)
|
|
|
|
coord[i] = MSG_ReadShort (msg) * (1.0 / 8.0);
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2004-01-20 06:11:00 +00:00
|
|
|
MSG_ReadCoordAngleV (qmsg_t *msg, vec3_t coord, vec3_t angles)
|
2001-12-11 22:37:30 +00:00
|
|
|
{
|
2001-12-12 21:56:09 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < 3; i++) {
|
|
|
|
coord[i] = MSG_ReadShort (msg) * (1.0 / 8.0);
|
2002-07-02 19:15:29 +00:00
|
|
|
angles[i] = ((signed char) MSG_ReadByte (msg)) * (360.0 / 256.0);
|
2001-12-12 21:56:09 +00:00
|
|
|
}
|
2001-02-23 23:16:13 +00:00
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE float
|
2004-01-20 06:11:00 +00:00
|
|
|
MSG_ReadAngle (qmsg_t *msg)
|
2001-02-23 23:16:13 +00:00
|
|
|
{
|
2002-07-02 19:15:29 +00:00
|
|
|
return ((signed char) MSG_ReadByte (msg)) * (360.0 / 256.0);
|
2001-02-23 23:16:13 +00:00
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2004-01-20 06:11:00 +00:00
|
|
|
MSG_ReadAngleV (qmsg_t *msg, vec3_t angles)
|
2001-12-12 21:56:09 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < 3; i++)
|
2002-07-02 19:15:29 +00:00
|
|
|
angles[i] = ((signed char) MSG_ReadByte (msg)) * (360.0 / 256.0);
|
2001-12-12 21:56:09 +00:00
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE float
|
2004-01-20 06:11:00 +00:00
|
|
|
MSG_ReadAngle16 (qmsg_t *msg)
|
2001-02-23 23:16:13 +00:00
|
|
|
{
|
2001-12-11 22:37:30 +00:00
|
|
|
return MSG_ReadShort (msg) * (360.0 / 65536.0);
|
2001-02-23 23:16:13 +00:00
|
|
|
}
|
2005-05-05 00:27:04 +00:00
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE int
|
2005-05-05 00:27:04 +00:00
|
|
|
MSG_ReadUTF8 (qmsg_t *msg)
|
|
|
|
{
|
|
|
|
byte *buf, *start, c;
|
|
|
|
int val = 0, count;
|
|
|
|
|
|
|
|
if (msg->badread || msg->message->cursize == msg->readcount) {
|
|
|
|
msg->badread = true;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
buf = start = msg->message->data + msg->readcount;
|
|
|
|
|
|
|
|
c = *buf++;
|
|
|
|
if (c < 0x80) {
|
|
|
|
val = c;
|
|
|
|
count = 1;
|
|
|
|
} else if (c < 0xc0) {
|
|
|
|
msg->badread = true;
|
|
|
|
return -1;
|
|
|
|
} else if (c < 0xe0) {
|
|
|
|
count = 2;
|
|
|
|
val = c & 0x1f;
|
|
|
|
} else if (c < 0xf0) {
|
|
|
|
count = 3;
|
|
|
|
val = c & 0x0f;
|
|
|
|
} else if (c < 0xf8) {
|
|
|
|
count = 4;
|
|
|
|
val = c & 0x07;
|
|
|
|
} else if (c < 0xfc) {
|
|
|
|
count = 5;
|
|
|
|
val = c & 0x03;
|
|
|
|
} else if (c < 0xfe) {
|
|
|
|
count = 6;
|
|
|
|
val = c & 0x01;
|
|
|
|
} else {
|
|
|
|
msg->badread = true;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (count > (msg->message->cursize - msg->readcount)) {
|
|
|
|
msg->badread = true;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
while (--count) {
|
|
|
|
c = *buf++;
|
|
|
|
if ((c & 0xc0) != 0x80) {
|
|
|
|
msg->badread = true;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
val <<= 6;
|
|
|
|
val |= c & 0x3f;
|
|
|
|
}
|
|
|
|
msg->readcount += buf - start;
|
|
|
|
return val;
|
|
|
|
}
|