-
Notifications
You must be signed in to change notification settings - Fork 236
Expand file tree
/
Copy pathGamePacketParser.cpp
More file actions
127 lines (99 loc) · 3.98 KB
/
Copy pathGamePacketParser.cpp
File metadata and controls
127 lines (99 loc) · 3.98 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
#include "CommonNetwork.h"
#include "GamePacketParser.h"
#include <string.h> // memcpy
using namespace Sapphire;
using namespace Sapphire::Network::Packets;
PacketParseResult Network::Packets::getHeader( const std::vector< uint8_t >& buffer,
const uint32_t offset,
FFXIVARR_PACKET_HEADER& header )
{
const auto headerSize = sizeof( FFXIVARR_PACKET_HEADER );
// Check if we have enough bytes in the buffer.
auto remainingBytes = buffer.size() - offset;
if( remainingBytes < headerSize )
return Incomplete;
// Copy packet header.
memcpy( &header, buffer.data() + offset, headerSize );
if( !checkHeader( header ) )
return Malformed;
return Success;
}
PacketParseResult Network::Packets::getSegmentHeader( const std::vector< uint8_t >& buffer,
const uint32_t offset,
FFXIVARR_PACKET_SEGMENT_HEADER& header )
{
const auto headerSize = sizeof( FFXIVARR_PACKET_SEGMENT_HEADER );
// Check if we have enough bytes in the buffer.
auto remainingBytes = buffer.size() - offset;
if( remainingBytes < headerSize )
return Incomplete;
// Copy segment header
memcpy( &header, buffer.data() + offset, headerSize );
return Success;
}
PacketParseResult Network::Packets::getPackets( const std::vector< uint8_t >& buffer,
const uint32_t offset,
const FFXIVARR_PACKET_HEADER& packetHeader,
std::vector< FFXIVARR_PACKET_RAW >& packets )
{
// sanity check: check there's enough bytes in the buffer
const auto bytesExpected = packetHeader.size - sizeof( struct FFXIVARR_PACKET_HEADER );
if( buffer.size() - offset < bytesExpected )
return Incomplete;
// Loop each message
uint32_t count = 0;
uint32_t bytesProcessed = 0;
while( count < packetHeader.count )
{
FFXIVARR_PACKET_RAW rawPacket;
// Copy ipc packet message
const auto packetResult = getPacket( buffer, offset + bytesProcessed, rawPacket );
if( packetResult != Success )
return packetResult;
// NOTE: isn't rawPacket is allocated on stack?
// why is okay to do this?
packets.push_back( rawPacket );
// Add message size and count
bytesProcessed += rawPacket.segHdr.size;
count += 1;
}
// sanity check: check if we processed all bytes.
// this check can fail if size of messages don't add up to size reported from packet header.
if( bytesExpected != bytesProcessed )
return Malformed;
return Success;
}
PacketParseResult Network::Packets::getPacket( const std::vector< uint8_t >& buffer, const uint32_t offset,
FFXIVARR_PACKET_RAW& packet )
{
// Copy segment header
const auto headerResult = getSegmentHeader( buffer, offset, packet.segHdr );
if( headerResult != Success )
return headerResult;
// Check header sanity and it's size
if( !checkSegmentHeader( packet.segHdr ) )
return Malformed;
const auto dataOffset = offset + sizeof( struct FFXIVARR_PACKET_SEGMENT_HEADER );
const auto dataSize = packet.segHdr.size;
// Allocate data buffer and copy
packet.data.resize( dataSize );
memcpy( packet.data.data(), buffer.data() + dataOffset, dataSize );
return Success;
}
bool Network::Packets::checkHeader( const FFXIVARR_PACKET_HEADER& header )
{
// Max size of the packet is capped at 1MB for now.
if( header.size > 1 * 1024 * 1024 )
return false;
// Max number of message is capped at 255 for now.
if( header.count > 255 )
return false;
return true;
}
bool Network::Packets::checkSegmentHeader( const FFXIVARR_PACKET_SEGMENT_HEADER& header )
{
// Max size of individual message is capped at 256KB for now.
if( header.size > 256 * 1024 )
return false;
return true;
}