-
Notifications
You must be signed in to change notification settings - Fork 8
/
SpCodec.cs
439 lines (377 loc) · 13.4 KB
/
SpCodec.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
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
using System.Collections.Generic;
using System.IO;
using System;
using System.Text;
public class SpCodec {
public const int MAX_SIZE = 1000000;
private SpStream mStream;
private SpTypeManager mTypeManager;
public SpCodec (SpTypeManager m) {
mTypeManager = m;
}
public SpStream Encode (string proto, SpObject obj) {
return Encode (mTypeManager.GetType (proto), obj);
}
public bool Encode (string proto, SpObject obj, byte[] buffer) {
return Encode (proto, obj, buffer, 0, buffer.Length);
}
public bool Encode (string proto, SpObject obj, byte[] buffer, int offset, int size) {
return Encode (proto, obj, new SpStream (buffer, offset, size));
}
public bool Encode (string proto, SpObject obj, SpStream stream) {
return Encode (mTypeManager.GetType (proto), obj, stream);
}
public SpStream Encode (SpType type, SpObject obj) {
SpStream stream = new SpStream ();
if (Encode (type, obj, stream) == false) {
if (stream.IsOverflow ()) {
if (stream.Position > MAX_SIZE)
return null;
int size = stream.Position;
size = ((size + 7) / 8) * 8;
stream = new SpStream (size);
if (Encode (type, obj, stream) == false)
return null;
}
else {
return null;
}
}
return stream;
}
public bool Encode (SpType type, SpObject obj, SpStream stream) {
if (type == null || obj == null || stream == null)
return false;
mStream = stream;
bool success = EncodeInternal (type, obj);
return (success && stream.IsOverflow () == false);
}
private bool EncodeInternal (SpType type, SpObject obj) {
if (mStream == null || type == null || obj == null) {
return false;
}
// buildin type decoding should not be here
if (mTypeManager.IsBuildinType (type)) {
return false;
}
int begin = mStream.Position;
// fn. will be update later
short fn = 0;
mStream.Write (fn);
List<KeyValuePair<SpObject, SpField>> objs = new List<KeyValuePair<SpObject, SpField>> ();
int current_tag = -1;
Dictionary<int, SpField>.ValueCollection.Enumerator en = type.Fields.Values.GetEnumerator ();
while (en.MoveNext ()) {
SpField f = en.Current;
if (f == null) {
return false;
}
SpObject o = obj[f.Name];
if (o == null || IsTypeMatch (f, o) == false)
continue;
if (f.Tag <= current_tag) {
return false;
}
if (f.Tag - current_tag > 1) {
mStream.Write ((short)(2 * (f.Tag - current_tag - 1) - 1));
fn++;
}
bool standalone = true;
if (f.IsTable == false) {
if (f.Type == mTypeManager.Boolean) {
int value = o.AsBoolean () ? 1 : 0;
mStream.Write ((short)((value + 1) * 2));
standalone = false;
}
else if (f.Type == mTypeManager.Integer) {
int value = o.AsInt ();
if (value >= 0 && value < 0x7fff) {
mStream.Write ((short)((value + 1) * 2));
standalone = false;
}
}
}
if (standalone) {
objs.Add (new KeyValuePair<SpObject, SpField> (o, f));
mStream.Write ((short)0);
}
fn++;
current_tag = f.Tag;
}
List<KeyValuePair<SpObject, SpField>>.Enumerator e = objs.GetEnumerator ();
while (e.MoveNext ()) {
KeyValuePair<SpObject, SpField> entry = e.Current;
if (entry.Value.IsTable) {
int array_begin = mStream.Position;
int size = 0;
mStream.Write (size);
if (entry.Value.Type == mTypeManager.Integer) {
byte len = 4;
Dictionary<object, SpObject>.Enumerator enumerator = entry.Key.AsTable ().GetEnumerator ();
while (enumerator.MoveNext ()) {
SpObject o = enumerator.Current.Value;
if (o.IsLong ()) {
len = 8;
break;
}
}
mStream.Write (len);
enumerator = entry.Key.AsTable ().GetEnumerator ();
while (enumerator.MoveNext ()) {
SpObject o = enumerator.Current.Value;
if (len == 4) {
mStream.Write (o.AsInt ());
}
else {
mStream.Write (o.AsLong ());
}
}
}
else if (entry.Value.Type == mTypeManager.Boolean) {
Dictionary<object, SpObject>.Enumerator enumerator = entry.Key.AsTable ().GetEnumerator ();
while (enumerator.MoveNext ()) {
SpObject o = enumerator.Current.Value;
mStream.Write ((byte)(o.AsBoolean () ? 1 : 0));
}
}
else if (entry.Value.Type == mTypeManager.String) {
Dictionary<object, SpObject>.Enumerator enumerator = entry.Key.AsTable ().GetEnumerator ();
while (enumerator.MoveNext ()) {
SpObject o = enumerator.Current.Value;
byte[] b = Encoding.UTF8.GetBytes (o.AsString ());
mStream.Write (b.Length);
mStream.Write (b);
}
}
else {
Dictionary<object, SpObject>.Enumerator enumerator = entry.Key.AsTable ().GetEnumerator ();
while (enumerator.MoveNext ()) {
SpObject o = enumerator.Current.Value;
int obj_begin = mStream.Position;
int obj_size = 0;
mStream.Write (obj_size);
if (EncodeInternal (entry.Value.Type, o) == false) {
return false;
}
int obj_end = mStream.Position;
obj_size = (int)(obj_end - obj_begin - 4);
mStream.Position = obj_begin;
mStream.Write (obj_size);
mStream.Position = obj_end;
}
}
int array_end = mStream.Position;
size = (int)(array_end - array_begin - 4);
mStream.Position = array_begin;
mStream.Write (size);
mStream.Position = array_end;
}
else {
if (entry.Key.IsString ()) {
byte[] b = Encoding.UTF8.GetBytes (entry.Key.AsString ());
mStream.Write (b.Length);
mStream.Write (b);
}
else if (entry.Key.IsInt ()) {
mStream.Write ((int)4);
mStream.Write (entry.Key.AsInt ());
}
else if (entry.Key.IsLong ()) {
mStream.Write ((int)8);
mStream.Write (entry.Key.AsLong ());
}
else if (entry.Key.IsBoolean ()) {
// boolean should not be here
return false;
}
else {
int obj_begin = mStream.Position;
int obj_size = 0;
mStream.Write (obj_size);
if (EncodeInternal (entry.Value.Type, entry.Key) == false) {
return false;
}
int obj_end = mStream.Position;
obj_size = (int)(obj_end - obj_begin - 4);
mStream.Position = obj_begin;
mStream.Write (obj_size);
mStream.Position = obj_end;
}
}
}
int end = mStream.Position;
mStream.Position = begin;
mStream.Write (fn);
mStream.Position = end;
return true;
}
public SpObject Decode (string proto, SpStream stream) {
return Decode (mTypeManager.GetType (proto), stream);
}
public SpObject Decode (SpType type, SpStream stream) {
if (type == null || stream == null)
return null;
mStream = stream;
return DecodeInternal (type);
}
private SpObject DecodeInternal (SpType type) {
if (mStream == null || type == null)
return null;
// buildin type decoding should not be here
if (mTypeManager.IsBuildinType (type))
return null;
SpObject obj = new SpObject ();
List<int> tags = new List<int> ();
int current_tag = 0;
short fn = mStream.ReadInt16 ();
for (short i = 0; i < fn; i++) {
int value = (int)mStream.ReadUInt16 ();
if (value == 0) {
tags.Add (current_tag);
current_tag++;
}
else {
if (value % 2 == 0) {
SpField f = type.GetFieldByTag (current_tag);
if (f == null)
return null;
value = value / 2 - 1;
if (f.Type == mTypeManager.Integer) {
obj.Insert (f.Name, value);
}
else if (f.Type == mTypeManager.Boolean) {
obj.Insert (f.Name, (value == 0 ? false : true));
}
else {
return null;
}
current_tag++;
}
else {
current_tag += (value + 1) / 2;
}
}
}
for (int c = 0; c < tags.Count; c++) {
int tag = tags[c];
SpField f = type.GetFieldByTag (tag);
if (f == null)
return null;
if (f.IsTable) {
int size = mStream.ReadInt32 ();
if (f.Type == mTypeManager.Integer) {
byte n = mStream.ReadByte ();
int count = (size - 1) / n;
SpObject tbl = new SpObject ();
for (int i = 0; i < count; i++) {
switch (n) {
case 4:
tbl.Insert (i, mStream.ReadInt32 ());
break;
case 8:
tbl.Insert (i, mStream.ReadInt64 ());
break;
default:
return null;
}
}
obj.Insert (f.Name, tbl);
}
else if (f.Type == mTypeManager.Boolean) {
SpObject tbl = new SpObject ();
for (int i = 0; i < size; i++) {
tbl.Insert (i, mStream.ReadBoolean ());
}
obj.Insert (f.Name, tbl);
}
else if (f.Type == mTypeManager.String) {
SpObject tbl = new SpObject ();
int k = 0;
while (size > 0) {
int str_len = mStream.ReadInt32 ();
size -= 4;
tbl.Insert (k, Encoding.UTF8.GetString (mStream.ReadBytes (str_len), 0, str_len));
k++;
size -= str_len;
}
obj.Insert (f.Name, tbl);
}
else if (f.Type == null) {
// unknown type
mStream.ReadBytes (size);
}
else {
SpObject tbl = new SpObject ();
int k = 0;
while (size > 0) {
int obj_len = mStream.ReadInt32 ();
size -= 4;
SpObject o = DecodeInternal (f.Type);
if (f.KeyName != null) {
tbl.Insert (o.AsTable ()[f.KeyName].Value, o);
}
else {
tbl.Insert (k, o);
}
k++;
size -= obj_len;
}
obj.Insert (f.Name, tbl);
}
}
else {
int size = mStream.ReadInt32 ();
if (f.Type == mTypeManager.Integer) {
switch (size) {
case 4:
obj.Insert (f.Name, mStream.ReadInt32 ());
break;
case 8:
obj.Insert (f.Name, mStream.ReadInt64 ());
break;
default:
return null;
}
}
else if (f.Type == mTypeManager.Boolean) {
// boolean should not be here
return null;
}
else if (f.Type == mTypeManager.String) {
obj.Insert (f.Name, Encoding.UTF8.GetString (mStream.ReadBytes (size), 0, size));
}
else if (f.Type == null) {
// unknown type
mStream.ReadBytes (size);
}
else {
obj.Insert (f.Name, DecodeInternal (f.Type));
}
}
}
return obj;
}
private bool IsTypeMatch (SpField f, SpObject o) {
if (f == null || f.Type == null || o == null)
return false;
if (f.IsTable && o.IsTable ()) {
return true;
}
else if (f.Type == mTypeManager.String) {
if (o.IsString ())
return true;
}
else if (f.Type == mTypeManager.Boolean) {
if (o.IsBoolean ())
return true;
}
else if (f.Type == mTypeManager.Integer) {
if (o.IsInt () || o.IsLong ())
return true;
}
else if (o.IsTable ()) {
return true;
}
return false;
}
}