-
Notifications
You must be signed in to change notification settings - Fork 1
/
CAmf3Helper.cs
339 lines (302 loc) · 10.5 KB
/
CAmf3Helper.cs
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
// AMF3协议格式的部分处理
//
// see: <<Action Message Format - AMF 3>>
// https://code.google.com/p/amf3cplusplus/
//
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
namespace AmfData
{
class CAmf3Helper
{
protected enum DataType
{
Undefined = 0x00,
Null = 0x01,
False = 0x02,
True = 0x03,
Integer = 0x04,
Double = 0x05,
String = 0x06,
XmlDoc = 0x07,
Date = 0x08,
Array = 0x09,
Object = 0x0A,
Xml = 0x0B,
ByteArray = 0x0C
}
protected class CObjTraits
{
public bool dynamic;
public string name;
public string[] keys;
}
protected class CRefTable
{
public List<string> str = new List<string>();
public List<CNameObjDict> obj = new List<CNameObjDict>();
public List<CObjTraits> ot = new List<CObjTraits>();
}
const int MaxU29 = 0x1FFFFFFF;
// 无需构造实例
private CAmf3Helper()
{
}
public static object Read(Stream stm)
{
return ReadAmf(stm, new CRefTable());
}
protected static object ReadAmf(Stream stm, CRefTable rt)
{
int type = stm.ReadByte();
switch ((DataType)type)
{
case DataType.Undefined:
return null;
case DataType.Null:
return null;
case DataType.False:
return false;
case DataType.True:
return true;
case DataType.Integer:
return ReadInt(stm);
case DataType.Double:
return CDataHelper.BE_ReadDouble(stm);
case DataType.String:
return ReadString(stm, rt);
case DataType.XmlDoc:
break;
case DataType.Date:
break;
case DataType.Array:
return ReadArray(stm, rt);
case DataType.Object:
return ReadHashObject(stm, rt);
case DataType.Xml:
break;
case DataType.ByteArray:
break;
default:
break;
}
Trace.Assert(false, "暂未处理的数据类型");
return null;
}
protected static uint ReadInt(Stream stm)
{
uint b = (uint)stm.ReadByte();
int num = 0;
uint value = 0;
while (((b & 0x80) != 0) && (num < 3))
{
value = (value << 7) | (b & 0x7F);
++num;
b = (uint)stm.ReadByte();
}
//最后一字节
if (num < 3)
value = (value << 7) | (b & 0x7F);
else
value = (value << 8) | (b & 0xFF);
return value;
}
protected static string ReadString(Stream stm, CRefTable rt)
{
uint head = ReadInt(stm);
int len = (int)(head >> 1);
if (len <= 0)
return "";
if (IsRefrence(head))
return rt.str[len];
string str = CDataHelper.ReadUtfStr(stm, len);
rt.str.Add(str);
return str;
}
protected static CMixArray ReadArray(Stream stm, CRefTable rt)
{
uint head = ReadInt(stm);
Trace.Assert((head & 0x1) == 0x1);
int count = (int)(head >> 1);
CMixArray ary = new CMixArray(count);
for (string key = ReadString(stm, rt); key != ""; key = ReadString(stm, rt))
ary[key] = ReadAmf(stm, rt);
//读取子元素
for (int i = 0; i < count; i++)
ary[i] = ReadAmf(stm, rt);
return ary;
}
protected static CNameObjDict ReadHashObject(Stream stm, CRefTable rt)
{
uint head = ReadInt(stm);
CObjTraits ot = null;
if (IsRefrence(head))
return rt.obj[(int)(head >> 1)];
if (IsRefrence(head >> 1))
{
ot = rt.ot[(int)(head >> 2)];
}
else
{
ot = new CObjTraits();
Trace.Assert(((head >> 2) & 0x1) == 0, "暂不支持");
ot.dynamic = ((head >> 3) & 0x1) != 0;
int count = (int)(head >> 4);
ot.name = ReadString(stm, rt);
ot.keys = new string[count];
for (int i = 0; i < count; i++)
ot.keys[i] = ReadString(stm, rt);
rt.ot.Add(ot);
}
CNameObjDict obj = new CNameObjDict(ot.name);
for (int i = 0; i < ot.keys.Length; i++)
obj[ot.keys[i]] = ReadAmf(stm, rt);
//读取动态属性
if (ot.dynamic)
{
while (true)
{
string key = ReadString(stm, rt);
if (key == "")
break;
obj[key] = ReadAmf(stm, rt);
}
}
rt.obj.Add(obj);
return obj;
}
protected static bool IsRefrence(uint header)
{
return (header & 0x1) == 0;
}
public static void Write(Stream stm, object obj)
{
WriteAmf(stm, obj);
}
protected static void WriteAmf(Stream stm, object obj)
{
if (obj == null)
{
stm.WriteByte((byte)DataType.Null);
}
else if (obj is byte || (obj is int && (uint)(int)obj < MaxU29) || (obj is uint && (uint)obj < MaxU29)) // U29无符号整形
{
stm.WriteByte((byte)DataType.Integer);
WriteInt(stm, uint.Parse(obj.ToString()));
}
else if (obj is int || obj is uint || obj is float || obj is double) // 只列举了常用的
{
stm.WriteByte((byte)DataType.Double);
CDataHelper.BE_WriteDouble(stm, double.Parse(obj.ToString()));
}
else if (obj is bool)
{
if ((bool)obj)
stm.WriteByte((byte)DataType.True);
else
stm.WriteByte((byte)DataType.False);
}
else if (obj is string)
{
stm.WriteByte((byte)DataType.String);
WriteString(stm, obj as string);
}
else if (obj is CMixArray)
{
stm.WriteByte((byte)DataType.Array);
CMixArray ary = obj as CMixArray;
uint head = ((uint)ary.FixedLength << 1) | 1;
WriteInt(stm, head);
foreach (KeyValuePair<string, object> pair in ary.Dynamic)
{
WriteString(stm, pair.Key);
WriteAmf(stm, pair.Value);
}
WriteString(stm, "");
foreach (object o in ary.Fixed)
WriteAmf(stm, o);
}
else if (obj is Array)
{
stm.WriteByte((byte)DataType.Array);
Array ary = obj as Array;
uint head = ((uint)ary.Length << 1) | 1;
WriteInt(stm, head);
WriteString(stm, "");
foreach (object o in ary)
WriteAmf(stm, o);
}
else if (obj is IDictionary)
{
stm.WriteByte((byte)DataType.Object);
IDictionary dic = obj as IDictionary;
uint head = 0x0B;
WriteInt(stm, head);
if (obj is CNameObjDict)
WriteString(stm, (obj as CNameObjDict).className);
else
WriteString(stm, "");
foreach (DictionaryEntry e in obj as IDictionary)
{
if (e.Key.ToString() == "" && e.Value is string) //解析时为了好看放进去的ClassName
continue;
WriteString(stm, e.Key.ToString());
WriteAmf(stm, e.Value);
}
WriteString(stm, "");
}
else
{
Trace.Assert(false, "暂未处理的数据类型");
stm.WriteByte((byte)DataType.Undefined);
}
}
protected static void WriteInt(Stream stm, uint data)
{
Trace.Assert(data <= MaxU29);
if (data <= 0x7F)
{
stm.WriteByte((byte)data);
}
else if (data <= 0x3FFF)
{
stm.WriteByte((byte)((data >> 7) | 0x80));
stm.WriteByte((byte)(data & 0x7F));
}
else if (data <= 0x001FFFFF)
{
stm.WriteByte((byte)((data >> 14) | 0x80));
stm.WriteByte((byte)(((data >> 7) & 0x7F) | 0x80));
stm.WriteByte((byte)(data & 0x7F));
}
else
{
stm.WriteByte((byte)((data >> 22) | 0x80));
stm.WriteByte((byte)(((data >> 15) & 0x7F) | 0x80));
stm.WriteByte((byte)(((data >> 8) & 0x7F) | 0x80));
stm.WriteByte((byte)(data & 0xFF));
}
}
protected static void WriteString(Stream stm, string str)
{
byte[] buf = Encoding.UTF8.GetBytes(str);
uint head = ((uint)buf.Length << 1) | 1;
WriteInt(stm, head);
stm.Write(buf, 0, buf.Length);
}
public static object GetObject(byte[] buf)
{
return Read(new MemoryStream(buf));
}
public static byte[] GetBytes(object obj)
{
MemoryStream stm = new MemoryStream();
Write(stm, obj);
return stm.ToArray();
}
}
}