forked from AlloyTeam/webtop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AmfStream.cpp
273 lines (261 loc) · 5.32 KB
/
AmfStream.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
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
#include <Windows.h>
#include "AmfStream.h"
#include <tchar.h>
AmfStream::AmfStream(PBYTE buffer)
{
stream = buffer;
position = 0;
}
AmfStream::~AmfStream(void)
{
stream = NULL;
}
BOOL AmfStream::ReadBytes(PBYTE bytes, DWORD len)
{
if(!IsBadReadPtr(&stream[position], len))
{
CopyMemory(bytes, &stream[position], len);
position += len;
return TRUE;
}
return FALSE;
}
BOOL AmfStream::WriteBytes(const PBYTE bytes, DWORD len)
{
if (!IsBadWritePtr(&stream[position], len))
{
CopyMemory(&stream[position], bytes, len);
position += len;
return TRUE;
}
return FALSE;
}
ULONG AmfStream::ReadULong()
{
if(!IsBadReadPtr(&stream[position], 4))
{
LONG i = position;
ULONG l = (stream[i+3] << 24) |
(stream[i+2] << 16) |
(stream[i+1] << 8) |
stream[i];
position += 4;
return l;
}
return 0;
}
BOOL AmfStream::WriteULong(ULONG len)
{
if (!IsBadWritePtr(&stream[position], 4))
{
stream[position] = (BYTE)(len & 0xff);
stream[position+1] = (BYTE)((len & 0xff00) >> 8);
stream[position+2] = (BYTE)((len & 0xff0000) >> 16);
stream[position + 3] = (BYTE)((len & 0xff000000) >> 24);
position += 4;
return TRUE;
}
return FALSE;
}
BYTE AmfStream::ReadByte()
{
if(!IsBadReadPtr(&stream[position], 1))
{
return stream[position++];
}
return 0;
}
BOOL AmfStream::WriteByte(const BYTE i)
{
if (!IsBadWritePtr(&stream[position], 1))
{
stream[position++] = i;
return TRUE;
}
return FALSE;
}
BOOL AmfStream::WriteString(LPCSTR str)
{
SHORT slen = strlen(str);
if (!IsBadWritePtr(&stream[position], slen + 3))
{
stream[position] = AMF_STRING;
stream[position + 1] = (slen >> 8) & 0xff;
stream[position + 2] = slen & 0xff;
CopyMemory(&stream[position+3], str, slen);
position += 3 + slen;
return TRUE;
}
return FALSE;
}
BOOL AmfStream::ReadString(LPCSTR str, SHORT* readed, int max)
{
if(stream[position] == AMF_STRING)
{
SHORT len = (stream[position + 1] << 8) | stream[position + 2];
if(max&&max<len){
len=max;
}
if (!IsBadWritePtr((PVOID)str, len))
{
CopyMemory((PVOID)str, &stream[position + 3], len);
PCHAR bytes = (PCHAR)str;
bytes[len] = '\0';
position += 3 + len;
}
if (readed != NULL)
{
*readed = len;
}
return TRUE;
}
return FALSE;
}
BOOL AmfStream::WriteWString(LPCWSTR str)
{
SHORT slen = wcslen(str)*2;
if (!IsBadWritePtr(&stream[position], slen + 3))
{
stream[position] = AMF_STRING;
stream[position + 1] = (slen >> 8) & 0xff;
stream[position + 2] = slen & 0xff;
CopyMemory(&stream[position+3], str, slen);
position += 3 + slen;
return TRUE;
}
return FALSE;
}
BOOL AmfStream::ReadWString(LPCWSTR str, SHORT* readed, int max)
{
if(stream[position] == AMF_STRING)
{
SHORT len = (stream[position + 1] << 8) | stream[position + 2];
if(max&&max<len){
len=max;
}
if (!IsBadWritePtr((PVOID)str, len))
{
CopyMemory((PVOID)str, &stream[position + 3], len);
PCHAR bytes = (PCHAR)str;
bytes[len+1]=bytes[len] = '\0';
position += 3 + len;
}
if (readed != NULL)
{
*readed = len;
}
return TRUE;
}
return FALSE;
}
BOOL AmfStream::WriteStringSimple(LPCSTR str)
{
long slen = strlen(str);
if (!IsBadWritePtr(&stream[position], slen))
{
CopyMemory(&stream[position], str, slen);
position += slen;
return TRUE;
}
return FALSE;
}
BOOL AmfStream::ReadStringSimple(LPCSTR str, int len)
{
if (!IsBadWritePtr((PVOID)str, len))
{
CopyMemory((PVOID)str, &stream[position], len);
PCHAR bytes = (PCHAR)str;
bytes[len] = '\0';
position += len;
}
return TRUE;
}
BOOL AmfStream::WriteWStringSimple(LPCWSTR str)
{
SHORT slen = wcslen(str)*2;
if (!IsBadWritePtr(&stream[position], slen))
{
CopyMemory(&stream[position], str, slen);
position += slen;
return TRUE;
}
return FALSE;
}
BOOL AmfStream::ReadWStringSimple(LPCWSTR str, int len)
{
if (!IsBadWritePtr((PVOID)str, len))
{
CopyMemory((PVOID)str, &stream[position], len);
PCHAR bytes = (PCHAR)str;
bytes[len+1]=bytes[len] = '\0';
position += + len;
}
return TRUE;
}
BOOL AmfStream::WriteUndefined(LPCSTR str, DWORD len)
{
SHORT lenNum = GetUndefinedLengthBytesNum(len);
if(!IsBadWritePtr(&stream[position], lenNum + len + 1 ))
{
DWORD nlen = len * 2 + 1;
stream[position] = AMF_UNDEFINED;
position ++;
INT offset[4] = {0,7, 14, 21};
for (INT i = lenNum -1; i > 0; i --)
{
stream[position] = (nlen >> offset[i]) % 0x80 + 0x80;
position ++;
}
stream[position] = nlen % 0x80;
position ++;
CopyMemory(&stream[position], str, len);
position += len;
return TRUE;
}
return FALSE;
}
BOOL AmfStream::ReadUndefined(LPCSTR str, DWORD* readed)
{
if (stream[position] == AMF_UNDEFINED)
{
position ++;
DWORD len = 0;
SHORT lenNum = 1;
DWORD index = position;
while(stream[index] >= 0x80)
{
lenNum ++;
index ++;
}
SHORT offset[4] = {0,7, 14, 21};
while(lenNum >= 0)
{
lenNum --;
BYTE b = stream[position] % 0x80;
SHORT of = offset[lenNum];
len += b << of;
position ++;
}
position --;
len = len > 1 ? (len - 1)/2 : len;
if (len > 0 && !IsBadWritePtr((PVOID)str, len))
{
CopyMemory((PVOID)str, &stream[position], len);
position += len;
}
if (readed != NULL)
{
*readed = len;
}
return TRUE;
}
return FALSE;
}
SHORT AmfStream::GetUndefinedLengthBytesNum(LONG len)
{
SHORT i = 0;
SHORT offset[4] = {0,7, 14, 21};
while(((len >> offset[i])%80) != 0)
i ++;
return i == 0 ? 1 : i;
}