-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileSchema.cs
executable file
·724 lines (634 loc) · 17.5 KB
/
FileSchema.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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
using System;
using System.Xml.Serialization;
using System.Text;
using System.Diagnostics;
using System.Reflection;
using System.Collections;
using System.IO;
namespace FileWalk.Schema
{
[XmlRoot("FileSchema")]
public class FileSchema
{
[XmlElement("Struct")]
public StructElement[] Structs;
public static FileSchema Load(string schemaResource)
{
try
{
// Serialize the order to a file.
XmlSerializer serializer = new XmlSerializer(typeof(FileSchema));
// Deserialize the order from the file.
System.IO.Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(schemaResource);
FileSchema schema = (FileSchema) serializer.Deserialize(stream);
schema.OnDeserialization();
return schema;
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
return null;
}
/// <summary>
/// Called when the entire file schema has been deserialized.
/// </summary>
private void OnDeserialization()
{
foreach (StructElement st in Structs)
{
st.OnDeserialization(this);
}
}
private static string GetFullName(string namePrefix, string name)
{
if (namePrefix == null || namePrefix == string.Empty)
{
return name;
}
else
{
return string.Format("{0}.{1}", namePrefix, name);
}
}
/// <summary>
/// Dump the content of this element as string.
/// </summary>
/// <param name="obj">The instance to dump</param>
/// <param name="namePrefix"></param>
/// <returns></returns>
public static string DumpInstance(object obj, string namePrefix)
{
if (obj is StructInstance)
{
StructInstance si = obj as StructInstance;
StringBuilder sb = new StringBuilder();
foreach (Element elem in si.Elements)
{
string name = GetFullName(namePrefix, elem.Name);
if (elem.Name == null && elem is FieldElement)
{
FieldElement fieldElem = elem as FieldElement;
foreach (BitsField f in fieldElem.BitsFields)
{
name = GetFullName(namePrefix, f.name);
object readable = elem.ToReadable(si[f.name]);
sb.Append(DumpInstance(readable, name));
}
}
else if (si[elem.Name] != null) // some element may not exist because of condition.
{
// Convert integer value into string if necessary.
object readable = elem.ToReadable(si[elem.Name]);
sb.Append(DumpInstance(readable, name));
}
}
return sb.ToString();
}
else if (obj is ArrayInstance)
{
StringBuilder sb = new StringBuilder();
ArrayInstance inst = obj as ArrayInstance;
for (int i = 0; i < inst.Elements.Length; i++)
{
string name = string.Format("{0}[{1}]", namePrefix, i);
sb.Append(DumpInstance(inst.Type.ToReadable(inst.Elements[i]), name));
}
return sb.ToString();
}
else // it is a primitive type such as integer, string, etc.
{
return string.Format("{0} = {1}\r\n", namePrefix, obj);
}
}
}
public abstract class Element
{
/// <summary>
/// Description of this element.
/// </summary>
[XmlElement("Description")]
public string Description = string.Empty;
/// <summary>
/// The offset relative to the start of parent element in bytes.
/// </summary>
[XmlAttribute("offset")]
public int Offset = 0;
/// <summary>
/// Unique name of this element.
/// </summary>
[XmlAttribute("name")]
public string Name;
/// <summary>
/// Specify the condition to include this member. For example, 'size == "mp4v"'.
/// </summary>
[XmlAttribute("condition")]
public string Condition;
/// <summary>
/// The size of this element in bytes.
/// </summary>
public abstract int Size { get; }
/// <summary>
/// Called when the entire element has been deserialized.
/// </summary>
public virtual void OnDeserialization(FileSchema schema)
{}
public virtual object ToReadable(object o)
{
return o;
}
/// <summary>
/// To construct instance from specified stream.
/// </summary>
/// <param name="parent"></param>
/// <param name="stream"></param>
/// <param name="stopAt"></param>
/// <returns></returns>
public abstract object Parse(ElementInstance parent, Stream stream, long stopAt);
/// <summary>
/// Calculate array size
/// </summary>
/// <param name="elements"></param>
/// <returns></returns>
public static int CalcElementArraySize(Element[] elements)
{
int size = 0;
if (elements != null)
{
foreach (Element elem in elements)
{
if (elem.Size < 0)
{
return -1; // there is a variable-length field.
}
size += elem.Size;
}
}
return size;
}
/// <summary>
/// Evaluate bool expression
/// </summary>
/// <param name="inst"></param>
/// <returns></returns>
public bool EvalCondition(ElementInstance inst)
{
if (Condition != null)
{
try
{
BoolExprLexer lexer = new BoolExprLexer(new StringReader(Condition));
BoolExprParser parser = new BoolExprParser(lexer);
ConditionEvaluator treeParser = new ConditionEvaluator();
parser.expression();
antlr.collections.AST ast = parser.getAST();
treeParser.Instance = inst;
return treeParser.expression(ast);
}
catch (Exception ex)
{
Debug.Write(ex);
return false;
}
}
return true;
}
}
public class StructElement : Element
{
[XmlElement("Field", typeof(FieldElement))]
[XmlElement("Array", typeof(ArrayElement))]
[XmlElement("Struct", typeof(StructElement))]
public Element[] Elements;
[XmlAttribute("type")]
public string DataType;
public override int Size
{
get
{
return Element.CalcElementArraySize(Elements);
}
}
public override void OnDeserialization(FileSchema schema)
{
if (Elements != null)
{
foreach (Element elem in Elements)
{
elem.OnDeserialization(schema);
}
}
else if (DataType != null)
{
foreach (StructElement s in schema.Structs)
{
if (s.Name == DataType)
{
Elements = s.Elements;
}
}
}
}
/// <summary>
/// Create an instance by specified byte array and start position.
/// </summary>
public override object Parse(ElementInstance parent, Stream stream, long stopAt)
{
StructInstance inst = new StructInstance(parent, Elements);
if (Elements != null)
{
foreach (Element elem in Elements)
{
if (elem.EvalCondition(inst))
{
if (stream.Position >= stopAt)
{
break; // don't read beyond boundary.
}
object elemInst = elem.Parse(inst, stream, stopAt);
// Elements that don't have a name will not be added directly
if (elem.Name != null && elem.Name != string.Empty)
{
inst[elem.Name] = elemInst;
}
}
}
}
return inst;
}
}
/// <summary>
/// DataType + Length = Array
/// </summary>
public class ArrayElement : Element
{
[XmlAttribute("length")]
public string Length;
[XmlAttribute("type")]
public string DataType;
[XmlAttribute("format")]
public string FormatType;
private Element DataElement;
public override int Size
{
get
{
if (Length == null)
{
return -1;
}
int len = -1;
try
{
len = Convert.ToInt32(Length);
Debug.Assert(DataElement != null);
Debug.Assert(DataElement.Size > 0);
return len * DataElement.Size;
}
catch (FormatException)
{}
return len;
}
}
/// <summary>
/// Create an instance by specified byte array and start position.
/// </summary>
/// <returns>An System.Array that contains all array members.</returns>
public override object Parse(ElementInstance parent, Stream stream, long stopAt)
{
Debug.Assert(DataElement != null);
int len = EvalLength(parent, stream, stopAt);
ArrayList list = new ArrayList();
for (int i = 0; i < len; i++)
{
list.Add(DataElement.Parse(parent, stream, stopAt));
}
ArrayInstance inst = new ArrayInstance(parent, DataElement, list.ToArray());
return inst;
}
public override void OnDeserialization(FileSchema schema)
{
foreach (StructElement s in schema.Structs)
{
// The array element is a struct.
if (s.Name == DataType)
{
DataElement = s;
return;
}
}
// Treat specified DataType as FieldElement.
FieldElement field = new FieldElement();
field.DataType = DataType;
field.FormatType = FormatType;
field.Name = "ArrayElement";
DataElement = field;
}
private int EvalLength(ElementInstance parent, Stream stream, long stopAt)
{
int len = 0;
if (Length == null)
{
Debug.Assert(stopAt >= stream.Position);
len = (int)((stopAt - stream.Position) / DataElement.Size);
}
else
{
try
{
len = Convert.ToInt32(Length);
}
catch (FormatException)
{
IConvertible evalLen = parent.Eval(Length) as IConvertible;
if (evalLen != null)
{
len = evalLen.ToInt32(null);
}
}
}
return len;
}
}
public class FieldElement : Element
{
[XmlAttribute("type")]
public string DataType;
[XmlElement("Flag")]
public FieldFlag[] Flags;
[XmlElement("Enum")]
public FieldEnum[] Enums;
[XmlElement("BitsField")]
public BitsField[] BitsFields;
[XmlAttribute("format")]
public string FormatType;
public override int Size
{
get
{
char prefix = Char.ToLower(DataType[0]);
if ("bs".IndexOf(prefix) >= 0)
{
return Int32.Parse(DataType.Substring(1));
}
else if ("iuf".IndexOf(prefix) >= 0)
{
return Int32.Parse(DataType.Substring(1))/8;
}
else if ("zv".IndexOf(prefix) >= 0)
{
return -1; // variable length
}
return 0;
}
}
public override object Parse(ElementInstance parent, Stream stream, long stopAt)
{
char type = DataType[0];
char prefix = Char.ToLower(type);
int size = Size;
string dataType = DataType.ToLower();
if (prefix == 'b') // byte
{
if (size <= 1)
{
int b = stream.ReadByte();
AddBitFields(parent, b);
return b;
}
else
{
byte[] data = new byte[size];
stream.Read( data, 0, size);
return data;
}
}
else if (prefix == 's') // fixed-length string
{
byte[] data = new byte[size];
stream.Read( data, 0, size);
string s = Encoding.UTF8.GetString(data, 0, size);
// a workaround for '\0'-stuffed stream
if (s.IndexOf('\0') >= 0)
{
s = s.Substring( 0, s.IndexOf("\0"));
}
return s;
}
else if (prefix == 'z') // zero-ended string
{
size = (int) (stopAt - stream.Position);
byte[] data = new byte[size];
stream.Read( data, 0, size);
int z = Array.IndexOf(data, 0, 0, size);
if (z < 0)
z = data.Length;
string s = Encoding.UTF8.GetString(data, 0, z);
return s;
}
// vluimsbf8: Variable length code unsigned integer, most significant bit first.
// The size of vluimsbf8 is a multiple of one byte. The first bit (Ext) of each
// byte specifies if set to 1 that another byte is present for this vluimsbf8 code
// word. The unsigned integer is encoded by the concatenation of the seven least
// significant bits of each byte belonging to this vluimsbf8 code word.
else if (prefix == 'v')
{
int i = 0;
int b = 0x80;
int bytes = 0;
for (; (b & 0x80) != 0; bytes++)
{
b = stream.ReadByte();
i = i << 7 | (b & 0x7f);
}
if (type == 'v' && bytes > 1)
{
return Util.EndianFlip(i, bytes);
}
return i;
}
else if ("iuf".IndexOf(prefix) >= 0) // signed/unsigned integer
{
byte[] data = new byte[size];
stream.Read( data, 0, size);
if (size == 8)
{
long i = data[0] | data[1] << 8 | data[2] << 16 | data[3] << 24 |
data[4] << 32 | data[5] << 40 | data[6] << 48 | data[7] << 56;
return i;
}
else if (size == 4 || size == 3)
{
int i;
if (size == 4)
{
i = data[0] | data[1] << 8 | data[2] << 16 | data[3] << 24;
}
else
{
i = data[0] | data[1] << 8 | data[2] << 16;
}
// Big-indian integer is identified by upper case 'I'
if (type == 'I')
{
return Util.EndianFlip(i, size);
}
// Big-indian unsigned integer is identified by upper case 'U'
else if (type == 'U')
{
return Util.EndianFlip((uint) i, size);
}
else if (type == 'u')
{
return (uint) i;
}
return i;
}
else if (size == 2)
{
short i = (short) (data[0] | data[1] << 8);
if (type == 'I')
{
return (short) Util.EndianFlip((int) i, size);
}
else if (type == 'U')
{
return (ushort) Util.EndianFlip(((uint) i), size);
}
else if (type == 'u')
{
return (ushort) i;
}
return i;
}
else
{
Debug.Assert( false, "Invalid field size!");
}
}
return null;
}
/// <summary>
/// Convert specified value to readable string if necessary
/// </summary>
public override object ToReadable(object o)
{
if (Enums != null && (o is byte || o is short || o is ushort || o is int || o is uint))
{
return ToEnum(Convert.ToInt32(o));
}
if (Flags != null && (o is ushort || o is uint))
{
return ToFlags(Convert.ToUInt32(o));
}
if (FormatType != null)
{
if (FormatType == "ipv4" && o is int)
{
int ipAddr = Convert.ToInt32(o);
string readable = string.Format("{0}.{1}.{2}.{3}",
(ipAddr & 0x000000ff),
(ipAddr & 0x0000ff00) >> 8,
(ipAddr & 0x00ff0000) >> 16,
(ipAddr & 0xff000000) >> 24);
return readable;
}
else if (o is IFormattable)
{
string readable = (o as IFormattable).ToString(FormatType, null);
if (FormatType.StartsWith("x"))
{
readable = "0x" + readable;
}
return readable;
}
}
return o;
}
private string ToFlags(uint i)
{
StringBuilder sb = new StringBuilder(128);
foreach (FieldFlag f in Flags)
{
uint mask = Convert.ToUInt32( f.mask, 16);
if ((i & mask) == mask)
{
if (sb.Length > 0)
{
sb.Append(" | ");
}
sb.Append(f.name);
}
}
if (sb.Length == 0)
{
// no flags was matched.
sb.Append(i.ToString());
}
return sb.ToString();
}
private string ToEnum(int i)
{
return ToEnum((uint) i);
}
private string ToEnum(uint i)
{
foreach (FieldEnum f in Enums)
{
string v = f.value.Trim();
int value = v.StartsWith("0x") ?
Convert.ToInt32(v, 16) : Convert.ToInt32(v);
if (i == value)
{
return f.name;
}
}
return i.ToString();
}
private void AddBitFields(ElementInstance parent, int data)
{
if (BitsFields != null && parent is StructInstance)
{
foreach (BitsField f in BitsFields)
{
Debug.Assert(f.length >= 1);
int mask = (1 << f.length) - 1;
int bits = (data >> f.offset) & mask;
((StructInstance) parent)[f.name] = bits;
}
}
}
}
/// <summary>
/// Represent named mask to show readable value.
/// </summary>
public class FieldFlag
{
[XmlAttribute("name")]
public string name;
[XmlAttribute("mask")]
public string mask;
}
/// <summary>
/// Represent named enumeration to show readable value.
/// </summary>
public class FieldEnum
{
[XmlAttribute("name")]
public string name;
[XmlAttribute("value")]
public string value;
}
/// <summary>
/// Provides access to fields that does not begin or end in byte boundary.
/// </summary>
public class BitsField
{
[XmlAttribute("name")]
public string name;
[XmlAttribute("offset")]
public int offset;
[XmlAttribute("length")]
public int length;
}
}