1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//=============================================================================
#include "UtlMsgBuffer.h"
#include <string.h>
//-----------------------------------------------------------------------------
// Purpose: bitfields for use in variable descriptors
//-----------------------------------------------------------------------------
enum
{
PACKBIT_CONTROLBIT = 0x01, // this must always be set
PACKBIT_INTNAME = 0x02, // if this is set then it's an int named variable, instead of a string
PACKBIT_BINARYDATA = 0x04, // signifies the data in this variable is binary, it's not a string
};
//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
CUtlMsgBuffer::CUtlMsgBuffer(unsigned short msgID, int initialSize) : m_Memory(0, initialSize)
{
m_iMsgID = msgID;
m_iWritePos = 0;
m_iReadPos = 0;
m_iNextVarPos = 0;
}
//-----------------------------------------------------------------------------
// Purpose: Constructor, takes initial data
//-----------------------------------------------------------------------------
CUtlMsgBuffer::CUtlMsgBuffer(unsigned short msgID, void const *data, int dataSize) : m_Memory(0, dataSize)
{
m_iMsgID = msgID;
m_iWritePos = (short)dataSize;
m_iReadPos = 0;
m_iNextVarPos = 0;
memcpy(Base(), data, dataSize);
}
//-----------------------------------------------------------------------------
// Purpose: Destructor
//-----------------------------------------------------------------------------
CUtlMsgBuffer::~CUtlMsgBuffer()
{
}
//-----------------------------------------------------------------------------
// Purpose: Copy
//-----------------------------------------------------------------------------
CUtlMsgBuffer &CUtlMsgBuffer::Copy(const CUtlMsgBuffer &rhs)
{
m_iWritePos = rhs.m_iWritePos;
m_iReadPos = rhs.m_iReadPos;
m_iNextVarPos = rhs.m_iNextVarPos;
m_Memory.EnsureCapacity(rhs.m_Memory.NumAllocated());
if ( rhs.m_Memory.NumAllocated() > 0 )
{
memcpy(Base(), rhs.Base(), rhs.m_Memory.NumAllocated());
}
return *this;
}
//-----------------------------------------------------------------------------
// Purpose: Writes string data to the message
// Input : *name - name of the variable
// *data - pointer to the string data to write
//-----------------------------------------------------------------------------
void CUtlMsgBuffer::WriteString(const char *name, const char *data)
{
// write out the variable type
unsigned char vtype = PACKBIT_CONTROLBIT; // stringname var, string data
Write(&vtype, 1);
// write out the variable name
Write(name, strlen(name) + 1);
// write out the size of the data
unsigned short size = (unsigned short)(strlen(data) + 1);
Write(&size, 2);
// write out the data itself
Write(data, size);
}
//-----------------------------------------------------------------------------
// Purpose: Writes out a named block of data
//-----------------------------------------------------------------------------
void CUtlMsgBuffer::WriteBlob(const char *name, const void *data, int dataSize)
{
// write out the variable type
unsigned char vtype = PACKBIT_CONTROLBIT | PACKBIT_BINARYDATA; // stringname var, binary data
Write(&vtype, 1);
// write out the variable name
Write(name, strlen(name) + 1);
// write out the size of the data
unsigned short size = (unsigned short)dataSize;
Write(&size, 2);
// write out the data itself
Write(data, dataSize);
}
//-----------------------------------------------------------------------------
// Purpose: Writes out another UtlMsgBuffer as an element of this one
//-----------------------------------------------------------------------------
void CUtlMsgBuffer::WriteBuffer(const char *name, const CUtlMsgBuffer *buffer)
{
// write out the variable type
unsigned char vtype = PACKBIT_CONTROLBIT | PACKBIT_BINARYDATA; // stringname var, binary data
Write(&vtype, 1);
// write out the variable name
Write(name, strlen(name) + 1);
// write out the size of the data
unsigned short size = (unsigned short) buffer->DataSize();
Write(&size, 2);
// write out the data itself
Write(buffer->Base(), size);
}
//-----------------------------------------------------------------------------
// Purpose: Reads from the buffer, increments read position
// returns false if past end of buffer
//-----------------------------------------------------------------------------
bool CUtlMsgBuffer::Read(void *buffer, int readAmount)
{
if (m_iReadPos + readAmount >= m_iWritePos)
return false;
memcpy(buffer, &m_Memory[m_iReadPos], readAmount);
m_iReadPos += readAmount;
return true;
}
//-----------------------------------------------------------------------------
// Purpose: Reads characterse from the buffer until a null is hit
//-----------------------------------------------------------------------------
bool CUtlMsgBuffer::ReadUntilNull(void *buffer, int bufferSize)
{
int nullPos = m_iReadPos;
// look through the buffer for the null terminator
while (nullPos < m_Memory.NumAllocated() && m_Memory[nullPos] != 0)
{
nullPos++;
}
if (nullPos >= m_Memory.NumAllocated())
{
// never found a null terminator
((char *)buffer)[0] = 0;
return false;
}
// copy from the null terminator
int copySize = nullPos - m_iReadPos;
if (copySize > bufferSize)
{
copySize = bufferSize - 1;
}
// copy out the data and return
memcpy(buffer, &m_Memory[m_iReadPos], copySize);
((char *)buffer)[copySize] = 0;
m_iReadPos += (copySize+1);
return true;
}
//-----------------------------------------------------------------------------
// Purpose: Writes to the buffer, incrementing the write position
// assumes enough space has already been allocated for the write
//-----------------------------------------------------------------------------
void CUtlMsgBuffer::Write(void const *data, int size)
{
// make sure it will fit
m_Memory.EnsureCapacity(m_iWritePos + size);
// normal write
memcpy(&m_Memory[m_iWritePos], data, size);
// increment write position
m_iWritePos += size;
}
//-----------------------------------------------------------------------------
// Purpose: Reads in a named variable length data blob
// returns number of bytes read, 0 on failure
//-----------------------------------------------------------------------------
int CUtlMsgBuffer::ReadBlob(const char *name, void *data, int dataBufferSize)
{
int dataSize = 0;
char *readData = (char *)FindVar(name, dataSize);
if (!readData)
{
memset(data, 0, dataBufferSize);
return 0;
}
// ensure against buffer overflow
if (dataSize > dataBufferSize)
dataSize = dataBufferSize;
// copy out data
memcpy(data, readData, dataSize);
return dataSize;
}
//-----------------------------------------------------------------------------
// Purpose: Reads a blob of binary data into it's own buffer
//-----------------------------------------------------------------------------
bool CUtlMsgBuffer::ReadBuffer(const char *name, CUtlMsgBuffer &buffer)
{
int dataSize = 0;
char *readData = (char *)FindVar(name, dataSize);
if (!readData)
{
return false;
}
buffer.m_Memory.EnsureCapacity(dataSize);
memcpy(&buffer.m_Memory[0], readData, dataSize);
buffer.m_iReadPos = 0;
buffer.m_iWritePos = (short)dataSize;
return true;
}
//-----------------------------------------------------------------------------
// Purpose: reads out the next variable available in the buffer
// fills out parameters with var details and data
// returns false if no more vars available
//-----------------------------------------------------------------------------
bool CUtlMsgBuffer::ReadNextVar(char varname[32], bool &stringData, void *data, int &dataSize)
{
// read the type
unsigned char vtype = 1;
if (!Read(&vtype, 1))
return false;
// check for null-termination type
if (vtype == 0)
return false;
stringData = !(vtype & PACKBIT_BINARYDATA);
// read the variable name
if (!ReadUntilNull(varname, 31))
return false;
// read the data size
unsigned short size = 0;
if (!Read(&size, 2))
return false;
// ensure against buffer overflows
if (dataSize > size)
dataSize = size;
// copy data
memcpy(data, &m_Memory[m_iReadPos], dataSize);
// store of the next position, since that is probably where the next read needs to occur
m_iReadPos += size;
m_iNextVarPos = m_iReadPos;
return true;
}
//-----------------------------------------------------------------------------
// Purpose: sets the read/write position to be at the specified variable
// returns pointer to buffer position on success, NULL if not found
//-----------------------------------------------------------------------------
void *CUtlMsgBuffer::FindVar(const char *name, int &dataSize)
{
// reset to where we Think the next var will be read from
m_iReadPos = m_iNextVarPos;
int loopCount = 2;
// loop through looking for the specified variable
while (loopCount--)
{
unsigned char vtype = 1;
while (Read(&vtype, 1))
{
// check for null-termination type
if (vtype == 0)
break;
// read the variable name
char varname[32];
if (!ReadUntilNull(varname, 31))
break;
// read the data size
unsigned short size = 0;
if (!Read(&size, 2))
break;
// is this our variable?
if (!stricmp(varname, name))
{
dataSize = size;
void *data = &m_Memory[m_iReadPos];
// store of the next position, since that is probably where the next read needs to occur
m_iReadPos += size;
m_iNextVarPos = m_iReadPos;
return data;
}
// skip over the data block to the next variable
m_iReadPos += size;
if (m_iReadPos >= m_iWritePos)
break;
}
// we haven't found the data yet, Start again
m_iReadPos = 0;
}
return NULL;
}
|