/
SharedUtil.Buffer.h
436 lines (380 loc) · 12.6 KB
/
SharedUtil.Buffer.h
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/*****************************************************************************
*
* PROJECT: Multi Theft Auto v1.0
* LICENSE: See LICENSE in the top level directory
* FILE: SharedUtil.Buffer.h
* PURPOSE:
* DEVELOPERS:
*
* Multi Theft Auto is available from http://www.multitheftauto.com/
*
*****************************************************************************/
namespace SharedUtil
{
// Assuming compiled on little endian machine
#define CBUFFER_LITTLE_ENDIAN
//#define CBUFFER_BIG_ENDIAN
//////////////////////////////////////////////////////
//
// class CBuffer
//
// Wrapped byte array
//
//////////////////////////////////////////////////////
class CBuffer : protected std::vector < char >
{
public:
CBuffer ( void )
{}
CBuffer ( const void* pData, uint uiSize )
{
AddBytes ( pData, uiSize, 0 );
}
void Clear ( void )
{
clear ();
}
bool IsEmpty ( void ) const
{
return empty ();
}
void Reserve ( uint uiSize )
{
return reserve ( uiSize );
}
// Comparison
bool operator== ( const CBuffer& other ) const
{
return size () == other.size ()
&& std::equal( begin(), end(), other.begin() );
}
bool operator!= ( const CBuffer& other ) const
{
return !operator== ( other );
}
// Status
void SetSize ( uint uiSize, bool bZeroPad = false )
{
uint uiOldSize = (uint)size ();
resize ( uiSize );
if ( bZeroPad && uiSize > uiOldSize )
memset ( GetData () + uiOldSize, 0, uiSize - uiOldSize );
}
uint GetSize ( void ) const
{
return (uint)size ();
}
// Access
char* GetData ( uint uiOffset = 0 )
{
return size () ? &at ( uiOffset ) : NULL;
}
const char* GetData ( uint uiOffset = 0 ) const
{
return size () ? &at ( uiOffset ) : NULL;
}
// Joining
CBuffer operator+( const CBuffer& other ) const
{
CBuffer temp = *this;
return temp += other;
}
CBuffer& operator+=( const CBuffer& other )
{
AddBytes ( other.GetData (), other.GetSize (), GetSize () );
return *this;
}
// Splitting
CBuffer Mid( int iOffset, int iSize ) const
{
iOffset = Clamp < int > ( 0, iOffset, GetSize () );
iSize = Clamp < int > ( 0, iSize, GetSize () - iOffset );
return CBuffer ( GetData () + iOffset, iSize );
}
CBuffer Head( uint uiAmount ) const
{
return Mid ( 0, uiAmount );
}
CBuffer Tail( uint uiAmount ) const
{
uiAmount = Min ( uiAmount, GetSize () );
return Mid ( GetSize () - uiAmount, uiAmount );
}
// Util
bool LoadFromFile ( const char* szFilename )
{
return FileLoad ( szFilename, *this );
}
bool SaveToFile ( const char* szFilename ) const
{
return FileSave ( szFilename, GetData (), GetSize () );
}
protected:
void AddBytes ( const void* pData, uint uiLength, int iOffset, bool bToFromNetwork = false )
{
// More room required?
if ( iOffset + uiLength > GetSize () )
SetSize ( iOffset + uiLength );
#ifdef CBUFFER_LITTLE_ENDIAN
if ( bToFromNetwork )
{
char* pDest = GetData () + iOffset + uiLength;
const char* pSrc = (const char*)pData;
while ( uiLength-- )
*--pDest = *pSrc++;
}
else
#endif
memcpy ( GetData () + iOffset, pData, uiLength );
}
bool GetBytes ( void* pData, uint uiLength, int iOffset, bool bToFromNetwork = false ) const
{
// Not enough data to get?
if ( iOffset + uiLength > GetSize () )
return false;
#ifdef CBUFFER_LITTLE_ENDIAN
if ( bToFromNetwork )
{
char* pDest = (char*)pData;
const char* pSrc = GetData () + iOffset + uiLength;
while ( uiLength-- )
*pDest++ = *--pSrc;
}
else
#endif
memcpy ( pData, GetData () + iOffset, uiLength );
return true;
}
friend class CBufferWriteStream;
friend class CBufferReadStream;
};
//////////////////////////////////////////////////////
//
// class CBufferStream
//
// Buffer access
//
//////////////////////////////////////////////////////
class CBufferStream
{
public:
CBufferStream ( bool bToFromNetwork )
: m_iPos ( 0 )
, m_uiVersion ( 0 )
, m_bToFromNetwork ( bToFromNetwork )
{
}
void Seek ( int iPos ) { m_iPos = Clamp ( 0, iPos, GetSize () ); }
int Tell ( void ) const { return m_iPos; }
virtual int GetSize ( void ) const = 0;
bool AtEnd ( int iOffset = 0 ) const { return m_iPos + iOffset >= GetSize (); }
void SetVersion ( uint uiVersion ) { m_uiVersion = uiVersion; }
uint Version ( void ) const { return m_uiVersion; }
protected:
int m_iPos;
int m_uiVersion;
bool m_bToFromNetwork;
};
//////////////////////////////////////////////////////
//
// class CBufferReadStream
//
// Used to stream bytes from a buffer
//
//////////////////////////////////////////////////////
class CBufferReadStream : public CBufferStream
{
const CBuffer* pBuffer;
public:
CBufferReadStream ( const CBuffer& source, bool bToFromNetwork = false )
: CBufferStream ( bToFromNetwork )
, pBuffer ( &source )
{
}
virtual int GetSize ( void ) const { return pBuffer->GetSize (); }
virtual const char* GetData ( void ) const { return pBuffer->GetData (); }
// Return true if enough bytes left in the buffer
bool CanReadNumberOfBytes( int iLength )
{
Seek( Tell() );
return iLength >= 0 && iLength <= ( GetSize() - Tell() );
}
bool ReadBytes ( void* pData, int iLength, bool bToFromNetwork = false )
{
// Validate pos
Seek ( Tell () );
if ( !pBuffer->GetBytes ( pData, iLength, Tell (), bToFromNetwork ) )
return false; // Not enough bytes left to fill request
// Adjust pos
Seek ( Tell () + iLength );
return true;
}
void Read ( SString& ); // Not defined as it won't work
void Read ( CBuffer& ); // Not defined as it won't work
bool ReadString ( SString& result, bool bByteLength = false, bool bDoesLengthIncludeLengthOfLength = false )
{
result = "";
// Get the length
ushort usLength = 0;
if ( bByteLength )
{
uchar ucLength = 0;
Read ( ucLength );
usLength = ucLength;
}
else
Read ( usLength );
if ( bDoesLengthIncludeLengthOfLength && usLength )
usLength -= bByteLength ? 1 : 2;
if ( usLength )
{
// Check has enough data
if ( !CanReadNumberOfBytes( usLength ) )
return false;
// Read the data
char* buffer = static_cast < char* > ( alloca ( usLength ) );
if ( !ReadBytes ( buffer, usLength, false ) )
return false;
result = std::string ( buffer, usLength );
}
return true;
}
bool ReadBuffer ( CBuffer& outResult )
{
outResult.Clear ();
// Get the length
ushort usLength = 0;
if ( !Read ( usLength ) )
return false;
uint uiLength = usLength;
if ( uiLength == 65535 )
if ( !Read ( uiLength ) )
return false;
if ( uiLength )
{
// Check has enough data
if ( !CanReadNumberOfBytes( uiLength ) )
return false;
// Read the data
outResult.SetSize ( uiLength );
if ( !ReadBytes ( outResult.GetData (), uiLength, false ) )
{
outResult.Clear ();
return false;
}
}
return true;
}
template < class T >
bool Read ( T& e )
{
return ReadBytes ( &e, sizeof ( e ), m_bToFromNetwork );
}
#ifdef ANY_x64
// Force all these types to use 4 bytes
bool Read( unsigned long& e )
{
uint temp;
bool bResult = Read( temp );
e = temp;
return bResult;
}
bool Read( long& e )
{
int temp;
bool bResult = Read( temp );
e = temp;
return bResult;
}
#ifdef WIN_x64
bool Read( size_t& e )
{
uint temp;
bool bResult = Read( temp );
e = temp;
return bResult;
}
#endif
#endif
};
//////////////////////////////////////////////////////
//
// class CBufferReadStream
//
// Used to stream bytes into a buffer
//
//////////////////////////////////////////////////////
class CBufferWriteStream : public CBufferStream
{
CBuffer* pBuffer;
public:
CBufferWriteStream ( CBuffer& source, bool bToFromNetwork = false )
: CBufferStream ( bToFromNetwork )
, pBuffer ( &source )
{
}
virtual int GetSize ( void ) const { return pBuffer->GetSize (); }
void WriteBytes ( const void* pData, int iLength, bool bToFromNetwork = false )
{
// Validate pos
Seek ( Tell () );
// Add data
pBuffer->AddBytes ( pData, iLength, Tell (), bToFromNetwork );
// Adjust pos
Seek ( Tell () + iLength );
}
void Write ( const SString& ); // Not defined as it won't work
void Write ( const CBuffer& ); // Not defined as it won't work
void WriteString ( const SString& str, bool bByteLength = false, bool bDoesLengthIncludeLengthOfLength = false )
{
ushort usLength = (ushort)str.length ();
if ( bDoesLengthIncludeLengthOfLength && usLength )
usLength += bByteLength ? 1 : 2;
if ( bByteLength )
Write ( (uchar)usLength );
else
Write ( usLength );
if ( usLength )
WriteBytes ( &str.at ( 0 ), usLength, false );
}
void WriteBuffer ( const CBuffer& inBuffer )
{
// Write the length
uint uiLength = (uint)inBuffer.GetSize ();
if ( uiLength > 65534 )
{
Write ( (ushort)65535 );
Write ( uiLength );
}
else
{
Write ( (ushort)uiLength );
}
// Write the data
if ( uiLength )
WriteBytes ( inBuffer.GetData (), uiLength, false );
}
template < class T >
void Write ( const T& e )
{
WriteBytes ( &e, sizeof ( e ), m_bToFromNetwork );
}
#ifdef ANY_x64
// Force all these types to use 4 bytes
void Write( unsigned long e )
{
Write( (uint)e );
}
void Write( long e )
{
Write( (int)e );
}
#ifdef WIN_x64
void Write( size_t e )
{
Write( (uint)e );
}
#endif
#endif
};
}