[util] Make sizebuf and msg sizes unisgned

And clean up the mess.
This commit is contained in:
Bill Currie 2021-04-04 15:53:53 +09:00
parent 9a2f82bbc6
commit 972b0f8a70
14 changed files with 54 additions and 50 deletions

View File

@ -41,7 +41,7 @@ void MSG_WriteLong (sizebuf_t *sb, int c);
void MSG_WriteLongBE (sizebuf_t *sb, int c); void MSG_WriteLongBE (sizebuf_t *sb, int c);
void MSG_WriteFloat (sizebuf_t *sb, float f); void MSG_WriteFloat (sizebuf_t *sb, float f);
void MSG_WriteString (sizebuf_t *sb, const char *s); void MSG_WriteString (sizebuf_t *sb, const char *s);
void MSG_WriteBytes (sizebuf_t *sb, const void *buf, int len); void MSG_WriteBytes (sizebuf_t *sb, const void *buf, unsigned len);
void MSG_WriteCoord (sizebuf_t *sb, float coord); void MSG_WriteCoord (sizebuf_t *sb, float coord);
void MSG_WriteCoordV (sizebuf_t *sb, const vec3_t coord); void MSG_WriteCoordV (sizebuf_t *sb, const vec3_t coord);
void MSG_WriteCoordAngleV (sizebuf_t *sb, const vec3_t coord, void MSG_WriteCoordAngleV (sizebuf_t *sb, const vec3_t coord,
@ -53,11 +53,11 @@ void MSG_WriteAngle16V (sizebuf_t *sb, const vec3_t angle);
void MSG_WriteUTF8 (sizebuf_t *sb, unsigned utf8); void MSG_WriteUTF8 (sizebuf_t *sb, unsigned utf8);
typedef struct msg_s { typedef struct msg_s {
int readcount; unsigned readcount;
qboolean badread; // set if a read goes beyond end of message qboolean badread; // set if a read goes beyond end of message
sizebuf_t *message; sizebuf_t *message;
size_t badread_string_size; size_t badread_string_size;
char *badread_string; char *badread_string;
} qmsg_t; } qmsg_t;
/** Reset the message read status. /** Reset the message read status.
@ -75,7 +75,7 @@ void MSG_BeginReading (qmsg_t *msg);
\param msg The message to check. \param msg The message to check.
\return The number of bytes that have been read. \return The number of bytes that have been read.
*/ */
int MSG_GetReadCount(qmsg_t *msg) __attribute__((pure)); unsigned MSG_GetReadCount(qmsg_t *msg) __attribute__((pure));
/** Read a single byte from the message. /** Read a single byte from the message.
@ -175,7 +175,7 @@ const char *MSG_ReadString (qmsg_t *msg);
\param len The number of bytes to read. \param len The number of bytes to read.
\return The number of bytes read from the message. \return The number of bytes read from the message.
*/ */
int MSG_ReadBytes (qmsg_t *msg, void *buf, int len); int MSG_ReadBytes (qmsg_t *msg, void *buf, unsigned len);
/** Read a little-endian 16-bit fixed point (13.3) coordinate value from the /** Read a little-endian 16-bit fixed point (13.3) coordinate value from the
message. message.

View File

@ -37,18 +37,18 @@
typedef struct sizebuf_s typedef struct sizebuf_s
{ {
qboolean allowoverflow; // if false, do a Sys_Error qboolean allowoverflow; // if false, do a Sys_Error
qboolean overflowed; // set to true if the buffer size failed qboolean overflowed; // set to true if the buffer size failed
byte *data; byte *data;
int maxsize; unsigned maxsize;
int cursize; unsigned cursize;
} sizebuf_t; } sizebuf_t;
void SZ_Alloc (sizebuf_t *buf, int maxsize); void SZ_Alloc (sizebuf_t *buf, unsigned maxsize);
void SZ_Free (sizebuf_t *buf); void SZ_Free (sizebuf_t *buf);
void SZ_Clear (sizebuf_t *buf); void SZ_Clear (sizebuf_t *buf);
void *SZ_GetSpace (sizebuf_t *buf, int length); void *SZ_GetSpace (sizebuf_t *buf, unsigned length);
void SZ_Write (sizebuf_t *buf, const void *data, int length); void SZ_Write (sizebuf_t *buf, const void *data, unsigned length);
void SZ_Print (sizebuf_t *buf, const char *data); // strcats onto the sizebuf void SZ_Print (sizebuf_t *buf, const char *data); // strcats onto the sizebuf
void SZ_Dump (sizebuf_t *buf); void SZ_Dump (sizebuf_t *buf);

View File

@ -317,7 +317,7 @@ void Netchan_Init_Cvars (void);
\param length The size of the unreliable packet. \param length The size of the unreliable packet.
\param data The data of the unreliable packet. \param data The data of the unreliable packet.
*/ */
void Netchan_Transmit (netchan_t *chan, int length, byte *data); void Netchan_Transmit (netchan_t *chan, unsigned length, byte *data);
/** Send an out-of-band packet. /** Send an out-of-band packet.
@ -325,7 +325,7 @@ void Netchan_Transmit (netchan_t *chan, int length, byte *data);
\param length The length of the data to be sent. \param length The length of the data to be sent.
\param data The data to be sent. \param data The data to be sent.
*/ */
void Netchan_OutOfBand (netadr_t adr, int length, byte *data); void Netchan_OutOfBand (netadr_t adr, unsigned length, byte *data);
/** Send a formatted string as an out-of-band packet. /** Send a formatted string as an out-of-band packet.
\param adr The address to which the data will be sent. \param adr The address to which the data will be sent.

View File

@ -43,10 +43,10 @@ typedef struct backbuf_s {
const char *name; const char *name;
} backbuf_t; } backbuf_t;
int MSG_ReliableCheckSize (backbuf_t *rel, int maxsize, int minsize) __attribute__((pure)); int MSG_ReliableCheckSize (backbuf_t *rel, unsigned maxsize, unsigned minsize) __attribute__((pure));
sizebuf_t *MSG_ReliableCheckBlock(backbuf_t *rel, int maxsize); sizebuf_t *MSG_ReliableCheckBlock(backbuf_t *rel, unsigned maxsize);
void MSG_Reliable_FinishWrite(backbuf_t *rel); void MSG_Reliable_FinishWrite(backbuf_t *rel);
sizebuf_t *MSG_ReliableWrite_Begin(backbuf_t *rel, int c, int maxsize); sizebuf_t *MSG_ReliableWrite_Begin(backbuf_t *rel, int c, unsigned maxsize);
void MSG_ReliableWrite_Angle(backbuf_t *rel, float f); void MSG_ReliableWrite_Angle(backbuf_t *rel, float f);
void MSG_ReliableWrite_Angle16(backbuf_t *rel, float f); void MSG_ReliableWrite_Angle16(backbuf_t *rel, float f);
void MSG_ReliableWrite_Byte(backbuf_t *rel, int c); void MSG_ReliableWrite_Byte(backbuf_t *rel, int c);
@ -56,7 +56,7 @@ void MSG_ReliableWrite_Coord(backbuf_t *rel, float f);
void MSG_ReliableWrite_Long(backbuf_t *rel, int c); void MSG_ReliableWrite_Long(backbuf_t *rel, int c);
void MSG_ReliableWrite_Short(backbuf_t *rel, int c); void MSG_ReliableWrite_Short(backbuf_t *rel, int c);
void MSG_ReliableWrite_String(backbuf_t *rel, const char *s); void MSG_ReliableWrite_String(backbuf_t *rel, const char *s);
void MSG_ReliableWrite_SZ(backbuf_t *rel, const void *data, int len); void MSG_ReliableWrite_SZ(backbuf_t *rel, const void *data, unsigned len);
void MSG_ReliableWrite_AngleV(backbuf_t *rel, const vec3_t v); void MSG_ReliableWrite_AngleV(backbuf_t *rel, const vec3_t v);
void MSG_ReliableWrite_CoordV(backbuf_t *rel, const vec3_t v); void MSG_ReliableWrite_CoordV(backbuf_t *rel, const vec3_t v);
void MSG_Reliable_Send (backbuf_t *rel); void MSG_Reliable_Send (backbuf_t *rel);

View File

@ -92,7 +92,7 @@ Netchan_Init_Cvars (void)
Sends an out-of-band datagram Sends an out-of-band datagram
*/ */
void void
Netchan_OutOfBand (netadr_t adr, int length, byte * data) Netchan_OutOfBand (netadr_t adr, unsigned length, byte * data)
{ {
byte send_buf[MAX_MSGLEN + PACKET_HEADER]; byte send_buf[MAX_MSGLEN + PACKET_HEADER];
sizebuf_t send; sizebuf_t send;
@ -185,7 +185,7 @@ Netchan_CanReliable (netchan_t *chan)
} }
void void
Netchan_Transmit (netchan_t *chan, int length, byte *data) Netchan_Transmit (netchan_t *chan, unsigned length, byte *data)
{ {
byte send_buf[MAX_MSGLEN + PACKET_HEADER]; byte send_buf[MAX_MSGLEN + PACKET_HEADER];
int i; int i;

View File

@ -59,7 +59,7 @@ PushBackbuf (backbuf_t *rel)
} }
int int
MSG_ReliableCheckSize (backbuf_t *rel, int maxsize, int minsize) MSG_ReliableCheckSize (backbuf_t *rel, unsigned maxsize, unsigned minsize)
{ {
sizebuf_t *msg = &rel->netchan->message; sizebuf_t *msg = &rel->netchan->message;
@ -80,7 +80,7 @@ MSG_ReliableCheckSize (backbuf_t *rel, int maxsize, int minsize)
// check to see if client block will fit, if not, rotate buffers // check to see if client block will fit, if not, rotate buffers
sizebuf_t * sizebuf_t *
MSG_ReliableCheckBlock (backbuf_t *rel, int maxsize) MSG_ReliableCheckBlock (backbuf_t *rel, unsigned maxsize)
{ {
sizebuf_t *msg = &rel->netchan->message; sizebuf_t *msg = &rel->netchan->message;
@ -108,7 +108,7 @@ MSG_ReliableCheckBlock (backbuf_t *rel, int maxsize)
// begin a client block, estimated maximum size // begin a client block, estimated maximum size
sizebuf_t * sizebuf_t *
MSG_ReliableWrite_Begin (backbuf_t *rel, int c, int maxsize) MSG_ReliableWrite_Begin (backbuf_t *rel, int c, unsigned maxsize)
{ {
sizebuf_t *msg; sizebuf_t *msg;
msg = MSG_ReliableCheckBlock (rel, maxsize); msg = MSG_ReliableCheckBlock (rel, maxsize);
@ -226,7 +226,7 @@ MSG_ReliableWrite_String (backbuf_t *rel, const char *s)
} }
void void
MSG_ReliableWrite_SZ (backbuf_t *rel, const void *data, int len) MSG_ReliableWrite_SZ (backbuf_t *rel, const void *data, unsigned len)
{ {
if (rel->num_backbuf) { if (rel->num_backbuf) {
SZ_Write (&rel->backbuf, data, len); SZ_Write (&rel->backbuf, data, len);

View File

@ -126,7 +126,7 @@ MSG_WriteString (sizebuf_t *sb, const char *s)
} }
VISIBLE void VISIBLE void
MSG_WriteBytes (sizebuf_t *sb, const void *buf, int len) MSG_WriteBytes (sizebuf_t *sb, const void *buf, unsigned len)
{ {
SZ_Write (sb, buf, len); SZ_Write (sb, buf, len);
} }
@ -253,7 +253,7 @@ MSG_BeginReading (qmsg_t *msg)
msg->badread = false; msg->badread = false;
} }
VISIBLE int VISIBLE unsigned
MSG_GetReadCount (qmsg_t *msg) MSG_GetReadCount (qmsg_t *msg)
{ {
return msg->readcount; return msg->readcount;
@ -403,7 +403,7 @@ MSG_ReadString (qmsg_t *msg)
} }
VISIBLE int VISIBLE int
MSG_ReadBytes (qmsg_t *msg, void *buf, int len) MSG_ReadBytes (qmsg_t *msg, void *buf, unsigned len)
{ {
if (msg->badread || len > msg->message->cursize - msg->readcount) { if (msg->badread || len > msg->message->cursize - msg->readcount) {
msg->badread = true; msg->badread = true;
@ -474,7 +474,8 @@ VISIBLE int
MSG_ReadUTF8 (qmsg_t *msg) MSG_ReadUTF8 (qmsg_t *msg)
{ {
byte *buf, *start, c; byte *buf, *start, c;
int val = 0, count; int val = 0;
unsigned count;
if (msg->badread || msg->message->cursize == msg->readcount) { if (msg->badread || msg->message->cursize == msg->readcount) {
msg->badread = true; msg->badread = true;

View File

@ -41,7 +41,7 @@
VISIBLE void VISIBLE void
SZ_Alloc (sizebuf_t *buf, int maxsize) SZ_Alloc (sizebuf_t *buf, unsigned maxsize)
{ {
if (maxsize < 256) if (maxsize < 256)
maxsize = 256; maxsize = 256;
@ -58,7 +58,7 @@ SZ_Clear (sizebuf_t *buf)
} }
VISIBLE void * VISIBLE void *
SZ_GetSpace (sizebuf_t *buf, int length) SZ_GetSpace (sizebuf_t *buf, unsigned length)
{ {
void *data; void *data;
@ -83,7 +83,7 @@ getspace:
} }
VISIBLE void VISIBLE void
SZ_Write (sizebuf_t *buf, const void *data, int length) SZ_Write (sizebuf_t *buf, const void *data, unsigned length)
{ {
memcpy (SZ_GetSpace (buf, length), data, length); memcpy (SZ_GetSpace (buf, length), data, length);
} }
@ -91,7 +91,7 @@ SZ_Write (sizebuf_t *buf, const void *data, int length)
VISIBLE void VISIBLE void
SZ_Print (sizebuf_t *buf, const char *data) SZ_Print (sizebuf_t *buf, const char *data)
{ {
int len; unsigned len;
len = strlen (data) + 1; len = strlen (data) + 1;
if (buf->cursize && !buf->data[buf->cursize - 1]) if (buf->cursize && !buf->data[buf->cursize - 1])
@ -103,7 +103,7 @@ SZ_Print (sizebuf_t *buf, const char *data)
VISIBLE void VISIBLE void
SZ_Dump (sizebuf_t *buf) SZ_Dump (sizebuf_t *buf)
{ {
int i; unsigned i;
char chars[17], c; char chars[17], c;
chars[16] = 0; chars[16] = 0;

View File

@ -174,7 +174,8 @@ check_next_demopacket (void)
static int static int
read_demopacket (void) read_demopacket (void)
{ {
int i, r; int i;
unsigned r;
float f; float f;
Qread (cls.demofile, &net_message->message->cursize, 4); Qread (cls.demofile, &net_message->message->cursize, 4);

View File

@ -124,7 +124,7 @@ server_compare (const void *a, const void *b)
} }
static void static void
setup_sub_message (qmsg_t *msg, qmsg_t *sub, sizebuf_t *buf, int len) setup_sub_message (qmsg_t *msg, qmsg_t *sub, sizebuf_t *buf, unsigned len)
{ {
memset (sub, 0, sizeof (qmsg_t)); memset (sub, 0, sizeof (qmsg_t));
memset (buf, 0, sizeof (sizebuf_t)); memset (buf, 0, sizeof (sizebuf_t));
@ -274,7 +274,7 @@ server_handler (connection_t *con, void *object)
if (!Netchan_Process (&sv->netchan)) if (!Netchan_Process (&sv->netchan))
return; return;
if (0) { if (0) {
int i; unsigned i;
for (i = 0; i < net_message->message->cursize; i++) for (i = 0; i < net_message->message->cursize; i++)
qtv_printf ("%c%02x", (i % 16) ? ' ' : '\n', qtv_printf ("%c%02x", (i % 16) ? ' ' : '\n',

View File

@ -41,7 +41,7 @@ recorder_t *SVR_AddUser (void (*writer)(void *, struct sizebuf_s *, int),
void (*finish)(void *, struct sizebuf_s *), void (*finish)(void *, struct sizebuf_s *),
int demo, void *user); int demo, void *user);
void SVR_RemoveUser (recorder_t *r); void SVR_RemoveUser (recorder_t *r);
struct sizebuf_s *SVR_WriteBegin (byte type, int to, int size); struct sizebuf_s *SVR_WriteBegin (byte type, int to, unsigned size);
struct sizebuf_s *SVR_Datagram (void) __attribute__((const)); struct sizebuf_s *SVR_Datagram (void) __attribute__((const));
void SVR_ForceFrame (void); void SVR_ForceFrame (void);
void SVR_Pause (recorder_t *r); void SVR_Pause (recorder_t *r);

View File

@ -271,7 +271,7 @@ check_next_demopacket (void)
static int static int
read_demopacket (void) read_demopacket (void)
{ {
int r; unsigned r;
Qread (cls.demofile, &net_message->message->cursize, 4); Qread (cls.demofile, &net_message->message->cursize, 4);
net_message->message->cursize = net_message->message->cursize =

View File

@ -1013,8 +1013,10 @@ CL_ReadPackets (void)
{ {
while (CL_GetMessage ()) { while (CL_GetMessage ()) {
if (net_message->message->cursize == -1) // non-packet set up by the demo reader
if ((int) net_message->message->cursize == -1) {
continue; continue;
}
if (cls.demoplayback && net_packetlog->int_val) if (cls.demoplayback && net_packetlog->int_val)
Log_Incoming_Packet (net_message->message->data, Log_Incoming_Packet (net_message->message->data,

View File

@ -56,8 +56,8 @@
typedef struct dbuffer_s { typedef struct dbuffer_s {
byte *data; byte *data;
int start, end, last; unsigned start, end, last;
int maxsize; unsigned maxsize;
} dbuffer_t; } dbuffer_t;
typedef struct header_s { typedef struct header_s {
@ -70,7 +70,7 @@ typedef struct header_s {
typedef struct demobuf_s { typedef struct demobuf_s {
sizebuf_t sz; sizebuf_t sz;
int bufsize; unsigned bufsize;
header_t *h; header_t *h;
} demobuf_t; } demobuf_t;
@ -200,9 +200,9 @@ write_msg (sizebuf_t *msg, int type, int to, float time, sizebuf_t *dst)
static void static void
write_to_msg (int type, int to, float time, sizebuf_t *dst) write_to_msg (int type, int to, float time, sizebuf_t *dst)
{ {
int pos = 0; unsigned pos = 0;
header_t *p; header_t *p;
int size; unsigned size;
sizebuf_t msg; sizebuf_t msg;
p = (header_t *) rec.dbuf->sz.data; p = (header_t *) rec.dbuf->sz.data;
@ -256,7 +256,7 @@ static void
set_buf (byte type, int to) set_buf (byte type, int to)
{ {
header_t *p; header_t *p;
int pos = 0; unsigned pos = 0;
p = (header_t *) rec.dbuf->sz.data; p = (header_t *) rec.dbuf->sz.data;
@ -464,7 +464,7 @@ write_packet (void)
} }
sizebuf_t * sizebuf_t *
SVR_WriteBegin (byte type, int to, int size) SVR_WriteBegin (byte type, int to, unsigned size)
{ {
byte *p; byte *p;
qboolean move = false; qboolean move = false;