-
Notifications
You must be signed in to change notification settings - Fork 6
/
cPacket.cpp
189 lines (151 loc) · 4.44 KB
/
cPacket.cpp
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
#include "stdafx.h"
#include "cPacket.h"
cPacket::cPacket()
{
m_iLength = 0;
m_iMaxLength = 1024;
m_pbDataPtr = m_pbData = new BYTE[m_iMaxLength];
}
cPacket::~cPacket()
{
delete []m_pbData;
}
void cPacket::Add(std::string & szInput)
{
int iStringLen = (int) szInput.length();
if( m_iLength + iStringLen > m_iMaxLength )
MessageBox(NULL, "Fix this", "Now", MB_ICONERROR);
// strings are preceded by 2-byte length...
Add( (WORD) iStringLen );
// ...followed by the string characters with no null terminator
memcpy( m_pbDataPtr, szInput.c_str(), iStringLen );
m_pbDataPtr += iStringLen;
m_iLength += iStringLen;
AlignDWORD();
}
void cPacket::AddString32L(std::string & szInput)
{
// From the ACE source:
// " 32L strings are crazy. the only place this is known as of time of writing this is in the
// Login header packet. it's a DWORD of the data length, followed by a packed word of the
// string length. for most cases, this means the string comes out with a 1 or 2 character
// prefix that just needs to get tossed. "
int iStringLen = (int)szInput.length();
int packedStringLengthBytes = 1 + int(iStringLen > 255);
if (m_iLength + iStringLen > m_iMaxLength)
MessageBox(NULL, "Fix this", "Now", MB_ICONERROR);
// 32L strings are preceded by a DWORD of data length...
Add((DWORD)iStringLen + packedStringLengthBytes);
// and a packed word of the string length
if (iStringLen > 255) {
Add((WORD)iStringLen);
}
else {
Add((BYTE)iStringLen);
}
// ...followed by the string characters with no null terminator
memcpy(m_pbDataPtr, szInput.c_str(), iStringLen);
m_pbDataPtr += iStringLen;
m_iLength += iStringLen;
AlignDWORD();
}
void cPacket::Set(DWORD position, WORD dwInput)
{
if (position + 2 <= (DWORD) m_iLength)
{
*((WORD *) (m_pbData + position)) = dwInput;
}
else
MessageBox(NULL, "Fix this", "Now", MB_ICONERROR);
}
void cPacket::AlignDWORD()
{
//DWORD align
DWORD dwZero = 0;
DWORD dwDWORDAlign = ((m_iLength & 3) == 0) ? 0 : (4 - (m_iLength & 3));
memcpy( m_pbDataPtr, (void *) &dwZero, dwDWORDAlign );
m_pbDataPtr += dwDWORDAlign;
m_iLength += dwDWORDAlign;
}
void cPacket::Add(QWORD qwInput)
{
if (m_iLength + (int) sizeof(qwInput) > m_iMaxLength)
MessageBox(NULL, "Fix this", "Now", MB_ICONERROR);
*((QWORD *)m_pbDataPtr) = qwInput;
m_pbDataPtr += sizeof(qwInput);
m_iLength += sizeof(qwInput);
}
void cPacket::Add(DWORD dwInput)
{
if (m_iLength + (int) sizeof(dwInput) > m_iMaxLength)
MessageBox(NULL, "Fix this", "Now", MB_ICONERROR);
*((DWORD *)m_pbDataPtr) = dwInput;
m_pbDataPtr += sizeof(dwInput);
m_iLength += sizeof(dwInput);
}
void cPacket::Add(WORD dwInput)
{
if (m_iLength + (int) sizeof(dwInput) > m_iMaxLength)
MessageBox(NULL, "Fix this", "Now", MB_ICONERROR);
*((WORD *) m_pbDataPtr) = dwInput;
m_pbDataPtr += sizeof(dwInput);
m_iLength += sizeof(dwInput);
}
void cPacket::Add(BYTE dwInput)
{
if (m_iLength + (int) sizeof(dwInput) > m_iMaxLength)
MessageBox(NULL, "Fix this", "Now", MB_ICONERROR);
*((BYTE *) m_pbDataPtr) = dwInput;
m_pbDataPtr += sizeof(dwInput);
m_iLength += sizeof(dwInput);
}
void cPacket::Add(cPacket *pPacket)
{
int iLen = pPacket->GetLength();
if (m_iLength + iLen > m_iMaxLength)
MessageBox(NULL, "Fix this", "Now", MB_ICONERROR);
memcpy(m_pbDataPtr, pPacket->GetData(), iLen);
m_pbDataPtr += iLen;
m_iLength += iLen;
}
void cPacket::Add(void *dwInput, int iLen)
{
if (m_iLength + iLen > m_iMaxLength)
MessageBox(NULL, "Fix this", "Now", MB_ICONERROR);
memcpy(m_pbDataPtr, dwInput, iLen);
m_pbDataPtr += iLen;
m_iLength += iLen;
}
void cPacket::Add(stTransitHeader *Transit)
{
if (m_iLength + (int) sizeof(stTransitHeader) > m_iMaxLength)
MessageBox(NULL, "Fix this", "Now", MB_ICONERROR);
memcpy(m_pbDataPtr, Transit, sizeof(stTransitHeader));
m_pbDataPtr += sizeof(stTransitHeader);
m_iLength += sizeof(stTransitHeader);
}
void cPacket::Add(stFragmentHeader *Fragment)
{
if (m_iLength + (int) sizeof(stFragmentHeader) > m_iMaxLength)
MessageBox(NULL, "Fix this", "Now", MB_ICONERROR);
memcpy(m_pbDataPtr, Fragment, sizeof(stFragmentHeader));
m_pbDataPtr += sizeof(stFragmentHeader);
m_iLength += sizeof(stFragmentHeader);
}
BYTE * cPacket::GetData()
{
return m_pbData;
}
int cPacket::GetLength()
{
return m_iLength;
}
stTransitHeader * cPacket::GetTransit()
{
//return &m_Transit;
return (stTransitHeader *)GetData();
}
BYTE * cPacket::GetPayload()
{
return m_pbData + sizeof(stTransitHeader);
}