2022-12-17 13:32:43 +01:00
|
|
|
/***
|
2013-08-30 13:34:05 -07:00
|
|
|
*
|
|
|
|
* Copyright (c) 1999, Valve LLC. All rights reserved.
|
|
|
|
*
|
|
|
|
* This product contains software technology licensed from Id
|
|
|
|
* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc.
|
|
|
|
* All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use, distribution, and modification of this source code and/or resulting
|
|
|
|
* object code is restricted to non-commercial enhancements to products from
|
|
|
|
* Valve LLC. All other use, distribution, or modification is prohibited
|
|
|
|
* without written permission from Valve LLC.
|
|
|
|
*
|
|
|
|
****/
|
|
|
|
//
|
|
|
|
// parsemsg.cpp
|
|
|
|
//
|
|
|
|
//--------------------------------------------------------------------------------------------------------------
|
2021-03-16 20:01:08 +01:00
|
|
|
#include "extdll.h"
|
2013-08-30 13:34:05 -07:00
|
|
|
#include "parsemsg.h"
|
|
|
|
|
2021-11-28 16:54:48 +01:00
|
|
|
static byte* gpBuf;
|
2013-08-30 13:34:05 -07:00
|
|
|
static int giSize;
|
|
|
|
static int giRead;
|
2021-11-28 15:32:26 +01:00
|
|
|
static bool giBadRead;
|
2013-08-30 13:34:05 -07:00
|
|
|
|
2021-11-28 15:32:26 +01:00
|
|
|
bool READ_OK()
|
2013-08-30 13:34:05 -07:00
|
|
|
{
|
|
|
|
return !giBadRead;
|
|
|
|
}
|
|
|
|
|
2021-11-28 16:54:48 +01:00
|
|
|
void BEGIN_READ(void* buf, int size)
|
2013-08-30 13:34:05 -07:00
|
|
|
{
|
|
|
|
giRead = 0;
|
2021-11-28 15:32:26 +01:00
|
|
|
giBadRead = false;
|
2013-08-30 13:34:05 -07:00
|
|
|
giSize = size;
|
|
|
|
gpBuf = (byte*)buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-05 20:54:33 +01:00
|
|
|
int READ_CHAR()
|
2013-08-30 13:34:05 -07:00
|
|
|
{
|
2021-11-28 16:54:48 +01:00
|
|
|
int c;
|
|
|
|
|
2013-08-30 13:34:05 -07:00
|
|
|
if (giRead + 1 > giSize)
|
|
|
|
{
|
|
|
|
giBadRead = true;
|
|
|
|
return -1;
|
|
|
|
}
|
2021-11-28 16:54:48 +01:00
|
|
|
|
2013-08-30 13:34:05 -07:00
|
|
|
c = (signed char)gpBuf[giRead];
|
|
|
|
giRead++;
|
2021-11-28 16:54:48 +01:00
|
|
|
|
2013-08-30 13:34:05 -07:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2021-03-05 20:54:33 +01:00
|
|
|
int READ_BYTE()
|
2013-08-30 13:34:05 -07:00
|
|
|
{
|
2021-11-28 16:54:48 +01:00
|
|
|
int c;
|
|
|
|
|
|
|
|
if (giRead + 1 > giSize)
|
2013-08-30 13:34:05 -07:00
|
|
|
{
|
|
|
|
giBadRead = true;
|
|
|
|
return -1;
|
|
|
|
}
|
2021-11-28 16:54:48 +01:00
|
|
|
|
2013-08-30 13:34:05 -07:00
|
|
|
c = (unsigned char)gpBuf[giRead];
|
|
|
|
giRead++;
|
2021-11-28 16:54:48 +01:00
|
|
|
|
2013-08-30 13:34:05 -07:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2021-03-05 20:54:33 +01:00
|
|
|
int READ_SHORT()
|
2013-08-30 13:34:05 -07:00
|
|
|
{
|
2021-11-28 16:54:48 +01:00
|
|
|
int c;
|
|
|
|
|
|
|
|
if (giRead + 2 > giSize)
|
2013-08-30 13:34:05 -07:00
|
|
|
{
|
|
|
|
giBadRead = true;
|
|
|
|
return -1;
|
|
|
|
}
|
2021-11-28 16:54:48 +01:00
|
|
|
|
|
|
|
c = (short)(gpBuf[giRead] + (gpBuf[giRead + 1] << 8));
|
|
|
|
|
2013-08-30 13:34:05 -07:00
|
|
|
giRead += 2;
|
2021-11-28 16:54:48 +01:00
|
|
|
|
2013-08-30 13:34:05 -07:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2021-03-05 20:54:33 +01:00
|
|
|
int READ_WORD()
|
2013-08-30 13:34:05 -07:00
|
|
|
{
|
|
|
|
return READ_SHORT();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-05 20:54:33 +01:00
|
|
|
int READ_LONG()
|
2013-08-30 13:34:05 -07:00
|
|
|
{
|
2021-11-28 16:54:48 +01:00
|
|
|
int c;
|
|
|
|
|
|
|
|
if (giRead + 4 > giSize)
|
2013-08-30 13:34:05 -07:00
|
|
|
{
|
|
|
|
giBadRead = true;
|
|
|
|
return -1;
|
|
|
|
}
|
2021-11-28 16:54:48 +01:00
|
|
|
|
|
|
|
c = gpBuf[giRead] + (gpBuf[giRead + 1] << 8) + (gpBuf[giRead + 2] << 16) + (gpBuf[giRead + 3] << 24);
|
|
|
|
|
2013-08-30 13:34:05 -07:00
|
|
|
giRead += 4;
|
2021-11-28 16:54:48 +01:00
|
|
|
|
2013-08-30 13:34:05 -07:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2021-03-05 20:54:33 +01:00
|
|
|
float READ_FLOAT()
|
2013-08-30 13:34:05 -07:00
|
|
|
{
|
|
|
|
union
|
|
|
|
{
|
2021-11-28 16:54:48 +01:00
|
|
|
byte b[4];
|
|
|
|
float f;
|
|
|
|
int l;
|
2013-08-30 13:34:05 -07:00
|
|
|
} dat;
|
2021-11-28 16:54:48 +01:00
|
|
|
|
2013-08-30 13:34:05 -07:00
|
|
|
dat.b[0] = gpBuf[giRead];
|
2021-11-28 16:54:48 +01:00
|
|
|
dat.b[1] = gpBuf[giRead + 1];
|
|
|
|
dat.b[2] = gpBuf[giRead + 2];
|
|
|
|
dat.b[3] = gpBuf[giRead + 3];
|
2013-08-30 13:34:05 -07:00
|
|
|
giRead += 4;
|
|
|
|
|
2021-11-28 16:54:48 +01:00
|
|
|
// dat.l = LittleLong (dat.l);
|
|
|
|
|
|
|
|
return dat.f;
|
2013-08-30 13:34:05 -07:00
|
|
|
}
|
|
|
|
|
2021-03-05 20:54:33 +01:00
|
|
|
char* READ_STRING()
|
2013-08-30 13:34:05 -07:00
|
|
|
{
|
2021-11-28 16:54:48 +01:00
|
|
|
static char string[2048];
|
|
|
|
int l, c;
|
2013-08-30 13:34:05 -07:00
|
|
|
|
|
|
|
string[0] = 0;
|
|
|
|
|
|
|
|
l = 0;
|
|
|
|
do
|
|
|
|
{
|
2021-11-28 16:54:48 +01:00
|
|
|
if (giRead + 1 > giSize)
|
2013-08-30 13:34:05 -07:00
|
|
|
break; // no more characters
|
|
|
|
|
|
|
|
c = READ_CHAR();
|
|
|
|
if (c == -1 || c == 0)
|
|
|
|
break;
|
|
|
|
string[l] = c;
|
|
|
|
l++;
|
2021-11-28 16:54:48 +01:00
|
|
|
} while (l < sizeof(string) - 1);
|
|
|
|
|
2013-08-30 13:34:05 -07:00
|
|
|
string[l] = 0;
|
2021-11-28 16:54:48 +01:00
|
|
|
|
2013-08-30 13:34:05 -07:00
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
2021-03-05 20:54:33 +01:00
|
|
|
float READ_COORD()
|
2013-08-30 13:34:05 -07:00
|
|
|
{
|
2021-11-28 16:54:48 +01:00
|
|
|
return (float)(READ_SHORT() * (1.0 / 8));
|
2013-08-30 13:34:05 -07:00
|
|
|
}
|
|
|
|
|
2021-03-05 20:54:33 +01:00
|
|
|
float READ_ANGLE()
|
2013-08-30 13:34:05 -07:00
|
|
|
{
|
2021-11-28 16:54:48 +01:00
|
|
|
return (float)(READ_CHAR() * (360.0 / 256));
|
2013-08-30 13:34:05 -07:00
|
|
|
}
|
|
|
|
|
2021-03-05 20:54:33 +01:00
|
|
|
float READ_HIRESANGLE()
|
2013-08-30 13:34:05 -07:00
|
|
|
{
|
2021-11-28 16:54:48 +01:00
|
|
|
return (float)(READ_SHORT() * (360.0 / 65536));
|
2013-08-30 13:34:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------------------
|
|
|
|
BufferWriter::BufferWriter()
|
|
|
|
{
|
2021-11-28 16:54:48 +01:00
|
|
|
Init(NULL, 0);
|
2013-08-30 13:34:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------------------
|
2021-11-28 16:54:48 +01:00
|
|
|
BufferWriter::BufferWriter(unsigned char* buffer, int bufferLen)
|
2013-08-30 13:34:05 -07:00
|
|
|
{
|
2021-11-28 16:54:48 +01:00
|
|
|
Init(buffer, bufferLen);
|
2013-08-30 13:34:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------------------
|
2021-11-28 16:54:48 +01:00
|
|
|
void BufferWriter::Init(unsigned char* buffer, int bufferLen)
|
2013-08-30 13:34:05 -07:00
|
|
|
{
|
|
|
|
m_overflow = false;
|
|
|
|
m_buffer = buffer;
|
|
|
|
m_remaining = bufferLen;
|
|
|
|
m_overallLength = bufferLen;
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------------------
|
2021-11-28 16:54:48 +01:00
|
|
|
void BufferWriter::WriteByte(unsigned char data)
|
2013-08-30 13:34:05 -07:00
|
|
|
{
|
2021-11-28 15:32:26 +01:00
|
|
|
if (!m_buffer || 0 == m_remaining)
|
2013-08-30 13:34:05 -07:00
|
|
|
{
|
|
|
|
m_overflow = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
*m_buffer = data;
|
|
|
|
++m_buffer;
|
|
|
|
--m_remaining;
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------------------
|
2021-11-28 16:54:48 +01:00
|
|
|
void BufferWriter::WriteLong(int data)
|
2013-08-30 13:34:05 -07:00
|
|
|
{
|
|
|
|
if (!m_buffer || m_remaining < 4)
|
|
|
|
{
|
|
|
|
m_overflow = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-11-28 16:54:48 +01:00
|
|
|
m_buffer[0] = data & 0xff;
|
|
|
|
m_buffer[1] = (data >> 8) & 0xff;
|
|
|
|
m_buffer[2] = (data >> 16) & 0xff;
|
|
|
|
m_buffer[3] = data >> 24;
|
2013-08-30 13:34:05 -07:00
|
|
|
m_buffer += 4;
|
|
|
|
m_remaining -= 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------------------
|
2021-11-28 16:54:48 +01:00
|
|
|
void BufferWriter::WriteString(const char* str)
|
2013-08-30 13:34:05 -07:00
|
|
|
{
|
2021-11-28 15:32:26 +01:00
|
|
|
if (!m_buffer || 0 == m_remaining)
|
2013-08-30 13:34:05 -07:00
|
|
|
{
|
|
|
|
m_overflow = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!str)
|
|
|
|
str = "";
|
|
|
|
|
2021-11-28 16:54:48 +01:00
|
|
|
int len = strlen(str) + 1;
|
|
|
|
if (len > m_remaining)
|
2013-08-30 13:34:05 -07:00
|
|
|
{
|
|
|
|
m_overflow = true;
|
|
|
|
str = "";
|
|
|
|
len = 1;
|
|
|
|
}
|
|
|
|
|
2021-11-28 16:54:48 +01:00
|
|
|
strcpy((char*)m_buffer, str);
|
2013-08-30 13:34:05 -07:00
|
|
|
m_remaining -= len;
|
|
|
|
m_buffer += len;
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------------------
|
|
|
|
int BufferWriter::GetSpaceUsed()
|
|
|
|
{
|
|
|
|
return m_overallLength - m_remaining;
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------------------
|