-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDataPacket.cpp
More file actions
496 lines (367 loc) · 11.2 KB
/
DataPacket.cpp
File metadata and controls
496 lines (367 loc) · 11.2 KB
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
/*******************************************************************
// Copyright (c) 2002, Robert Umbehant
// mailto:rumbehant@wheresjames.com
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later
// version.
//
// This library 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free
// Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
// MA 02111-1307 USA
//
*******************************************************************/
// DataPacket.cpp: implementation of the CDataPacket class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CDataPacket::CDataPacket()
{_STT();
m_dwEncode = 0;
m_bInPacket = FALSE;
m_bValidPacket = FALSE;
}
CDataPacket::CDataPacket( DWORD dwSize ) :
CCircBuf( TRUE, dwSize )
{_STT();
m_dwEncode = 0;
m_bInPacket = FALSE;
m_bValidPacket = FALSE;
}
CDataPacket::~CDataPacket()
{_STT();
Destroy();
}
void CDataPacket::Destroy()
{_STT();
m_bValidPacket = FALSE;
m_bInPacket = FALSE;
}
// *** Packet Writing ***
BOOL CDataPacket::WritePacket(DWORD dwPacketType, DWORD dwDataType, LPVOID pData, DWORD dwData)
{_STT();
// Lock the buffer
CTlLocalLock ll( *this );
if ( !ll.IsLocked() ) return FALSE;
// Initialize packet
if ( !InitPacket( dwPacketType, 1, dwData ) )
return FALSE;
// Add the users data
if ( !AddPacketData( dwDataType, pData, dwData ) )
return FALSE;
// Add end packet data
return EndPacket();
}
BOOL CDataPacket::vWriteMultiPacket( DWORD dwPacketType, DWORD dwBuffers, LPVOID *pArgs )
{_STT();
// Lock the buffer
CTlLocalLock ll( *this );
if ( !ll.IsLocked() ) return FALSE;
// Variable params
LPVOID *ptrExtra = pArgs;
// Calculate the total size needed
DWORD dwTotalSize = 0, i;
for ( i = 0; i < dwBuffers; i++ )
{
RULIB_TRY // This could GPF if caller screws up
{
DWORD dwType = *(LPDWORD)ptrExtra;
LPBYTE pPtr = *(LPBYTE*)( ptrExtra + 1 );
DWORD dwSize = *(LPDWORD)( ptrExtra + 2 );
// Track the total size
dwTotalSize += dwSize;
// Skip these parameters
ptrExtra += 3;
} // end try
RULIB_CATCH_ALL { return FALSE; }
} // end for
// Initialize packet
if ( !InitPacket( dwPacketType, dwBuffers, dwTotalSize ) )
return FALSE;
// Now add the data
ptrExtra = pArgs;
for ( i = 0; i < dwBuffers; i++ )
{
RULIB_TRY // This could GPF if caller screws up
{
DWORD dwType = *(LPDWORD)ptrExtra;
LPBYTE pPtr = *(LPBYTE*)( ptrExtra + 1 );
DWORD dwSize = *(LPDWORD)( ptrExtra + 2 );
// Add the users data
if ( !AddPacketData( dwType, pPtr, dwSize ) )
return FALSE;
// Skip these parameters
ptrExtra += 3;
} // end try
RULIB_CATCH_ALL { return FALSE; }
} // end for
// Add end packet data
return EndPacket();
}
BOOL CDataPacket::InitPacket( DWORD dwType, DWORD dwDataBlocks, DWORD dwTotalDataSize)
{_STT();
SPacketHeader ph;
// In packet
m_bInPacket = TRUE;
// Initialize the check sum
CMd5Rsa::MD5Init( &m_md5Context );
// Initialize the packet data
ph.dwId = DPID_HEADER;
// Calculate the total length of the packet
ph.dwLength = GetMinimumPacketOverhead() +
( dwDataBlocks * sizeof( SDataHeader ) ) +
dwTotalDataSize;
// The type of packet
ph.dwType = dwType;
// Number of data blocks
ph.dwDataBlocks = dwDataBlocks;
// Prepare to poke the buffer
InitPoke();
// Write data into the packet
return WritePacketData( &ph, sizeof( ph ));
}
BOOL CDataPacket::AddPacketData( DWORD dwType, LPVOID pData, DWORD dwSize )
{_STT();
if ( !m_bInPacket )
return FALSE;
SDataHeader dh;
dh.dwLength = dwSize;
dh.dwType = dwType;
// Write the data block header
if ( !WritePacketData( &dh, sizeof( dh ) ) )
return FALSE;
// Write the data
return WritePacketData( pData, dwSize, m_dwEncode );
}
BOOL CDataPacket::WritePacketData(LPVOID pData, DWORD dwSize, DWORD dwEncode )
{_STT();
if ( !m_bInPacket )
return FALSE;
// Write out the packet header
if ( !Poke( pData, dwSize, dwEncode ) ) return FALSE;
return TRUE;
}
BOOL CDataPacket::OnInspectWrite( DWORD dwBlock, LPBYTE pBuf, DWORD dwSize )
{_STT();
if ( !m_bInPacket )
return TRUE;
// Update the md5 hash
CMd5Rsa::MD5Update( &m_md5Context, pBuf, dwSize );
return TRUE;
}
BOOL CDataPacket::EndPacket()
{_STT();
if ( !m_bInPacket )
return FALSE;
// End of packet
m_bInPacket = FALSE;
SCheckSum cs;
// Get the md5
CMd5Rsa::MD5Final( cs.md5, &m_md5Context );
// Write out the check sum
if ( !Poke( &cs, sizeof( cs ) ) ) return FALSE;
// Commit the data
EndPoke();
return TRUE;
}
// *** Packet Reading ***
BOOL CDataPacket::ReadPacket(LPVOID pBuf, DWORD dwSize)
{_STT();
// First write the data into the buffer
Write( pBuf, dwSize );
// Attempt to verify a packet in the buffer
return VerifyPacket();
}
BOOL CDataPacket::SkipPacket()
{_STT();
// Do we have a packet
if ( !m_bValidPacket )
// Can we get one?
if ( !VerifyPacket() ) return FALSE;
// Skip over this packet
AdvanceReadPtr( m_ph.dwLength );
// Not valid now
m_bValidPacket = FALSE;
// Attempt to find another packet
return VerifyPacket();
}
BOOL CDataPacket::FindPacket(LPSPacketHeader pPh, LPDWORD pdwAvailable)
{_STT();
// How many bytes are availble
DWORD dwAvailable = GetMaxRead();
do
{
// Do we have enough data for a packet?
if ( dwAvailable < GetMinimumPacketOverhead() )
return FALSE;
// Take a sneak at the header
DWORD dwRead = 0;
if ( !Peek( pPh, sizeof( SPacketHeader ), &dwRead ) ||
dwRead != sizeof( SPacketHeader ) )
return FALSE;
// Is this a valid header id?
if ( pPh->dwId == DPID_HEADER )
{ if ( pdwAvailable ) *pdwAvailable = dwAvailable;
return TRUE;
} // end if
// Should be one less byte in the buffer
dwAvailable--;
// Skip ahead one byte
} while ( AdvanceReadPtr( 1 ) );
return FALSE;
}
BOOL CDataPacket::VerifyPacket()
{_STT();
// Do we already know there is a valid packet?
if ( m_bValidPacket ) return TRUE;
DWORD dwAvailable = 0;
do
{
// Attempt to find a packet in the buffer
if ( !FindPacket( &m_ph, &dwAvailable ) )
return FALSE;
// Verify the length of the packet is available
if ( m_ph.dwLength > dwAvailable ) return FALSE;
// Enforce maximum packet size
if ( m_ph.dwLength <= GetMaxSize() )
{
LPBYTE pBuf = NULL;
DWORD dwSize = 0, dwView = 0;
// Initialize the check sum
SCheckSum csCur, csBuf;
CMd5Rsa::MD5Init( &m_md5Context );
// Calculate the md5 hash
while ( GetReadView( dwView++, 0, m_ph.dwLength - sizeof( SCheckSum ), &pBuf, &dwSize ) )
// Update the md5 hash
CMd5Rsa::MD5Update( &m_md5Context, (LPBYTE)pBuf, dwSize );
// What is our calculated checksum?
CMd5Rsa::MD5Final( csCur.md5, &m_md5Context );
// Not in packet
m_bInPacket = FALSE;
// Read the checksum from the buffer
Peek( &csBuf, sizeof( csBuf ), NULL, m_ph.dwLength - sizeof( SCheckSum ) );
// Do the checksums match?
if ( !memcmp( &csCur, &csBuf, sizeof( SCheckSum ) ) )
{ m_bValidPacket = TRUE;
return TRUE;
} // end if
} // end if
// Skip over the id
} while ( AdvanceReadPtr( 4 ) );
return FALSE;
}
BOOL CDataPacket::ReadPacketData(DWORD dwBlock, DWORD dwType, LPVOID pBuf, DWORD dwMax, LPDWORD pdwRead, long lOffset)
{_STT();
// Ensure valid packet
if ( !m_bValidPacket ) return FALSE;
// Is it a valid data block index?
if ( dwBlock > m_ph.dwDataBlocks ) return FALSE;
// Where to start looking
DWORD dwOffset = sizeof( SPacketHeader );
// We must find the block
for ( ; ; )
{
SDataHeader dh;
// Peek at the First data block
DWORD dwRead = 0;
if ( !Peek( &dh, sizeof( dh ), &dwRead, dwOffset ) || dwRead != sizeof( dh ) )
return FALSE;
// Skip over the header
dwOffset += sizeof( SDataHeader );
// Test for unreasonable size
if ( dh.dwLength > m_ph.dwLength ) return FALSE;
// Is this is the block type we're looking for?
if ( dwType == 0 || dh.dwType == dwType )
{
// Is this our block
if ( !dwBlock )
{
// Is the offset too much?
if ( (DWORD)lOffset > dh.dwLength ) return FALSE;
// Subtrack the offset
dh.dwLength -= lOffset;
// Do they just want to know how many bytes or are there none?
if ( pBuf == NULL || dwMax == 0 || dh.dwLength == 0 )
{ if ( pdwRead ) *pdwRead = dh.dwLength; return TRUE; }
// Truncate length if needed
if ( dh.dwLength > dwMax ) dh.dwLength = dwMax;
// Get the data
return Peek( pBuf, dh.dwLength, pdwRead, dwOffset + lOffset, m_dwEncode );
} // end if
// Count a block
else dwBlock--;
} // end if
// Skip the data
dwOffset += dh.dwLength;
}
return FALSE;
}
BOOL CDataPacket::GetPacketDataHash(GUID *pGuid, DWORD dwBlock, DWORD dwType)
{_STT();
// Ensure guid pointer
if ( pGuid == NULL ) return FALSE;
// Ensure valid packet
if ( !m_bValidPacket ) return FALSE;
// Is it a valid data block index?
if ( dwBlock > m_ph.dwDataBlocks ) return FALSE;
// Where to start looking
DWORD dwOffset = sizeof( SPacketHeader );
// We must find the block
for ( ; ; )
{
SDataHeader dh;
// Peek at the First data block
DWORD dwRead = 0;
if ( !Peek( &dh, sizeof( dh ), &dwRead, dwOffset ) || dwRead != sizeof( dh ) )
return FALSE;
// Skip over the header
dwOffset += sizeof( SDataHeader );
// Test for unreasonable size
if ( dh.dwLength > m_ph.dwLength ) return FALSE;
// Is this is the block type we're looking for?
if ( dwType == 0 || dh.dwType == dwType )
{
// Is this our block
if ( !dwBlock )
{
// Initialize MD5 Hash
MD5_CTX ctx;
CMd5Rsa::MD5Init( &ctx );
// Hash data in place
LPBYTE pBuf = NULL;
DWORD dwView = 0, dwSize = 0;
while ( GetReadView( dwView++, 0, dh.dwLength, &pBuf, &dwSize ) )
// Update
CMd5Rsa::MD5Update( &ctx, pBuf, dwSize );
// Get the hash
CMd5Rsa::MD5Final( (LPBYTE)pGuid, &ctx );
return TRUE;
} // end if
// Count a block
else dwBlock--;
} // end if
// Skip the data
dwOffset += dh.dwLength;
} // end forever
return FALSE;
}
BOOL CDataPacket::VerifyPacketData(DWORD dwBlock, DWORD dwType, const GUID *pGuid)
{_STT();
GUID guid;
// Verify the data hash
if ( !pGuid ||
!GetPacketDataHash( &guid, dwBlock, dwType ) ||
!IsEqualGUID( guid, *pGuid ) ) return FALSE;
return TRUE;
}