-
Notifications
You must be signed in to change notification settings - Fork 17
/
m3.py
1343 lines (1087 loc) · 59.5 KB
/
m3.py
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
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
import xml.dom.minidom
from xml.dom.minidom import Node
import re
from sys import stderr
import struct
import copy
import sys
def increaseToValidSectionSize(size):
blockSize = 16
incompleteBlockBytes = (size % blockSize)
if incompleteBlockBytes != 0:
missingBytesToCompleteBlock = blockSize - incompleteBlockBytes
return size + missingBytesToCompleteBlock
else:
return size
class Section:
"""Has fields indexEntry and structureDescription and sometimes also the fields rawBytes and content """
def __init__(self):
self.timesReferenced = 0
def __str__(self):
return 'Section %s timesReferenced=%d %sV%s' % (
self.indexEntry,
self.timesReferenced,
self.structureDescription.structureName,
self.structureDescription.structureVersion
)
def from_instances(self, content, tag=''):
if tag:
self.content = content
self.structureDescription = structures[tag].structureDescription
else:
self.content = content
self.structureDescription = content[0].structureDescription
def determineContentField(self, checkExpectedValue):
self.content = self.structureDescription.createInstances(buffer=self.rawBytes, count=self.indexEntry.repetitions, checkExpectedValue=checkExpectedValue)
def determineFieldRawBytes(self):
minRawBytes = self.determineRawBytesWithData()
if len(minRawBytes) != self.bytesRequiredForContent():
raise Exception("Section size calculation failed: Expected %s but was %s for %s; content: %s" % (self.bytesRequiredForContent(), len(minRawBytes), self.structureDescription.structureName, minRawBytes))
sectionSize = increaseToValidSectionSize(len(minRawBytes))
if len(minRawBytes) == sectionSize:
self.rawBytes = minRawBytes
else:
rawBytes = bytearray(sectionSize)
rawBytes[0:len(minRawBytes)] = minRawBytes
for i in range(len(minRawBytes), sectionSize):
rawBytes[i] = 0xaa
self.rawBytes = rawBytes
def determineRawBytesWithData(self):
return self.structureDescription.instancesToBytes(self.content)
def bytesRequiredForContent(self):
return self.structureDescription.countBytesRequiredForInstances(self.content)
def resolveReferences(self, sections):
if not self.structureDescription.isPrimitive:
for object in self.content:
object.resolveReferences(sections)
primitiveFieldTypeSizes = {"uint32": 4, "int32": 4, "uint16": 2, "int16": 2, "uint8": 1, "int8": 1, "float": 4, "tag": 4, "fixed8": 1}
primitiveFieldTypeFormats = {"uint32": "I", "int32": "i", "uint16": "H", "int16": "h", "uint8": "B", "int8": "b", "float": "f", "tag": "4s", "fixed8": "B"}
intTypes = {"uint32", "int32", "uint16", "int16", "uint8", "int8"}
structureNamesOfPrimitiveTypes = set(["CHAR", "U8__", "REAL", "I16_", "U16_", "I32_", "U32_", "FLAG"])
class M3StructureHistory:
"Describes the history of a structure with a specific name"
def __init__(self, name, versionToSizeMap, allFields):
self.name = name
self.versionToSizeMap = versionToSizeMap
self.allFields = allFields
self.versionToStructureDescriptionMap = {}
self.isPrimitive = self.name in structureNamesOfPrimitiveTypes
# Create all to check sizes:
for version in versionToSizeMap:
self.getVersion(version)
def createStructureDescription(self, version, usedFields, specifiedSize, fmagic):
finalFields = []
# dirty patching of all structures to make MD33 models work
# - they use `SmallReference` instead of `Reference` across all structures
if fmagic == 'MD33':
for field in usedFields:
if isinstance(field, ReferenceField):
field = copy.copy(field)
if field.referenceStructureDescription.structureName == 'Reference':
newStructureHistory = structures['SmallReference']
newDescription = newStructureHistory.getVersion(0, fmagic)
field.referenceStructureDescription = newDescription
field.size = newDescription.size
elif self.name == 'MODL' and field.name == 'tightHitTest':
field = copy.copy(field)
newStructureHistory = structures[field.structureDescription.structureName]
newDescription = newStructureHistory.getVersion(1, fmagic)
field.structureDescription = newDescription
field.size = newDescription.size
finalFields.append(field)
else:
finalFields = usedFields
structure = M3StructureDescription(self.name, version, finalFields, specifiedSize, self, fmagic != 'MD33')
return structure
def getVersion(self, version, fmagic='MD34', force=False):
structure = self.versionToStructureDescriptionMap.get(fmagic + '_' + str(version))
if structure is None:
usedFields = []
for field in self.allFields:
includeField = True
if field.sinceVersion is not None and version < field.sinceVersion:
includeField = False
if field.tillVersion is not None and version > field.tillVersion:
includeField = False
if includeField:
usedFields.append(field)
specifiedSize = self.versionToSizeMap.get(version)
if not force and specifiedSize is None:
return None
structure = self.createStructureDescription(version, usedFields, specifiedSize, fmagic)
self.versionToStructureDescriptionMap[fmagic + '_' + str(version)] = structure
return structure
def getNewestVersion(self):
newestVersion = None
for version in self.versionToSizeMap.keys():
if newestVersion is None or version > newestVersion:
newestVersion = version
return self.getVersion(newestVersion)
def createEmptyArray(self):
if self.name == "CHAR":
return None # even no terminating character
elif self.name == "U8__":
return bytearray(0)
else:
return []
def __str__():
return self.name
class M3StructureDescription:
def __init__(self, structureName, structureVersion, fields, specifiedSize, history, validateSize=True):
self.structureName = structureName
self.structureVersion = structureVersion
self.fields = fields
self.isPrimitive = self.structureName in structureNamesOfPrimitiveTypes
self.history = history
calculatedSize = 0
for field in fields:
calculatedSize += field.size
self.size = calculatedSize
# Validate the specified size:
if validateSize and calculatedSize != specifiedSize:
self.dumpOffsets()
raise Exception("Size mismatch: %s in version %d has been specified to have size %d, but the calculated size was %d" % (structureName, structureVersion, specifiedSize, calculatedSize))
nameToFieldMap = {}
for field in fields:
if field.name in nameToFieldMap:
raise Exception("%s contains in version %s multiple fields with the name %s" % (structureName, structureVersion, field.name))
nameToFieldMap[field.name] = field
self.nameToFieldMap = nameToFieldMap
def createInstance(self, buffer=None, offset=0, checkExpectedValue=True):
return M3Structure(self, buffer, offset, checkExpectedValue)
def createInstances(self, buffer, count, checkExpectedValue=True):
if self.isPrimitive:
if self.structureName == "CHAR":
return buffer[:count - 1].decode("ASCII", "replace")
elif self.structureName == "U8__":
return bytearray(buffer[:count])
else:
structFormat = self.fields[0].structFormat
list = []
for offset in range(0, count * self.size, self.size):
bytesOfOneEntry = buffer[offset:(offset + self.size)]
intValue = structFormat.unpack(bytesOfOneEntry)[0]
list.append(intValue)
return list
else:
list = []
instanceOffset = 0
for i in range(count):
list.append(self.createInstance(buffer=buffer, offset=instanceOffset, checkExpectedValue=checkExpectedValue))
instanceOffset += self.size
return list
def dumpOffsets(self):
offset = 0
stderr.write("Offsets of %s in version %d:\n" % (self.structureName, self.structureVersion))
for field in self.fields:
stderr.write("%s: %s\n" % (offset, field.name))
offset += field.size
def countInstances(self, instances):
if self.structureName == "CHAR":
if instances is None:
return 0
return len(instances) + 1 # +1 terminating null character
elif hasattr(instances, "__len__"): # either a list or an array of bytes
return len(instances)
else:
raise Exception("Can't measure the length of %s which is a %s" % (instances, self.structureName))
def validateInstance(self, instance, instanceName):
for field in self.fields:
try:
fieldContent = getattr(instance, field.name)
except AttributeError:
raise Exception("%s does not have a field called %s" % (instanceName, field.name))
raise
field.validateContent(fieldContent, instanceName + "." + field.name)
def hasField(self, fieldName):
return fieldName in self.nameToFieldMap
def instancesToBytes(self, instances):
if self.structureName == "CHAR":
if type(instances) != str:
raise Exception("Expected a string but it was a %s" % type(instances))
return instances.encode("ASCII") + b'\x00'
elif self.structureName == "U8__":
if type(instances) != bytes and type(instances) != bytearray:
raise Exception("Expected a byte array but it was a %s" % type(instances))
return instances
else:
rawBytes = bytearray(self.size * len(instances))
offset = 0
if self.isPrimitive:
structFormat = self.fields[0].structFormat
for value in instances:
structFormat.pack_into(rawBytes, offset, value)
offset += self.size
else:
for value in instances:
value.writeToBuffer(rawBytes, offset)
offset += self.size
return rawBytes
def countBytesRequiredForInstances(self, instances):
if self.structureName == "CHAR":
return len(instances) + 1 # +1 for terminating character
return self.size * self.countInstances(instances)
class M3Structure:
def __init__(self, structureDescription: M3StructureDescription, buffer=None, offset=0, checkExpectedValue=True):
self.structureDescription = structureDescription
if buffer is not None:
self.readFromBuffer(buffer, offset, checkExpectedValue)
else:
for field in self.structureDescription.fields:
field.setToDefault(self)
def introduceIndexReferences(self, indexMaker):
for field in self.structureDescription.fields:
field.introduceIndexReferences(self, indexMaker)
def resolveReferences(self, sections):
for field in self.structureDescription.fields:
field.resolveIndexReferences(self, sections)
def readFromBuffer(self, buffer, offset, checkExpectedValue):
fieldOffset = offset
for field in self.structureDescription.fields:
try:
field.readFromBuffer(self, buffer, fieldOffset, checkExpectedValue)
except struct.error as e:
raise Exception('failed to unpack %sV%s %s' % (self.structureDescription.structureName, self.structureDescription.structureVersion, field.name), e)
fieldOffset += field.size
assert fieldOffset - offset == self.structureDescription.size
def writeToBuffer(self, buffer, offset):
fieldOffset = offset
for field in self.structureDescription.fields:
field.writeToBuffer(self, buffer, fieldOffset)
fieldOffset += field.size
assert fieldOffset - offset == self.structureDescription.size
def __str__(self):
fieldValueMap = {}
for field in self.structureDescription.fields:
fieldValueMap[field.name] = str(getattr(self, field.name))
return "%sV%s: {%s}" % (self.structureDescription.structureName, self.structureDescription.structureVersion, fieldValueMap)
def getNamedBit(self, fieldName, bitName):
field = self.structureDescription.nameToFieldMap[fieldName]
return field.getNamedBit(self, bitName)
def setNamedBit(self, fieldName, bitName, value):
field = self.structureDescription.nameToFieldMap[fieldName]
return field.setNamedBit(self, bitName, value)
def getBitNameMaskPairs(self, fieldName):
field = self.structureDescription.nameToFieldMap[fieldName]
return field.getBitNameMaskPairs()
class Field:
def __init__(self, name, sinceVersion, tillVersion):
self.name = name
self.sinceVersion = sinceVersion
self.tillVersion = tillVersion
def introduceIndexReferences(self, owner, indexMaker):
pass
def resolveIndexReferences(self, owner, sections):
pass
class TagField(Field):
def __init__(self, name, sinceVersion, tillVersion):
Field.__init__(self, name, sinceVersion, tillVersion)
self.structFormat = struct.Struct("<4B")
self.size = 4
def readFromBuffer(self, owner, buffer, offset, checkExpectedValue):
b = self.structFormat.unpack_from(buffer, offset)
if b[3] == 0:
s = chr(b[2]) + chr(b[1]) + chr(b[0])
else:
s = chr(b[3]) + chr(b[2]) + chr(b[1]) + chr(b[0])
setattr(owner, self.name, s)
def writeToBuffer(self, owner, buffer, offset):
s = getattr(owner, self.name)
if len(s) == 4:
b = (s[3] + s[2] + s[1] + s[0]).encode("ascii")
else:
b = (s[2] + s[1] + s[0]).encode("ascii") + b"\x00"
return self.structFormat.pack_into(buffer, offset, b[0], b[1], b[2], b[3])
def setToDefault(self, owner):
pass
def validateContent(self, fieldContent, fieldPath):
if (type(fieldContent) != str) or (len(fieldContent) != 4):
raise Exception("%s is not a string with 4 characters" % (fieldPath))
class ReferenceField(Field):
def __init__(self, name, referenceStructureDescription, historyOfReferencedStructures, sinceVersion, tillVersion):
Field.__init__(self, name, sinceVersion, tillVersion)
self.referenceStructureDescription = referenceStructureDescription
self.historyOfReferencedStructures = historyOfReferencedStructures
self.size = referenceStructureDescription.size
def introduceIndexReferences(self, owner, indexMaker):
referencedObjects = getattr(owner, self.name)
structureDescription = self.getListContentStructureDefinition(referencedObjects, "while adding index ref")
indexReference = indexMaker.getIndexReferenceTo(referencedObjects, self.referenceStructureDescription, structureDescription)
isPrimitive = self.historyOfReferencedStructures is not None and self.historyOfReferencedStructures.isPrimitive
if not isPrimitive:
for referencedObject in referencedObjects:
referencedObject.introduceIndexReferences(indexMaker)
setattr(owner, self.name, indexReference)
def resolveIndexReferences(self, owner, sections):
ref = getattr(owner, self.name)
ownerName = owner.structureDescription.structureName
variable = "%(ownerName)s.%(fieldName)s" % {"ownerName": ownerName, "fieldName": self.name}
if ref.entries == 0:
if self.historyOfReferencedStructures is None:
referencedObjects = []
else:
referencedObjects = self.historyOfReferencedStructures.createEmptyArray()
else:
referencedSection = sections[ref.index]
referencedSection.timesReferenced += 1
indexEntry = referencedSection.indexEntry
if indexEntry.repetitions < ref.entries:
raise Exception("%s tries to reference %s elements in a %s section that contains just %s element(s)" % (variable, ref.entries, indexEntry.tag, indexEntry.repetitions))
referencedObjects = referencedSection.content
if self.historyOfReferencedStructures is not None:
expectedTagName = self.historyOfReferencedStructures.name
actualTagName = indexEntry.tag
if actualTagName != expectedTagName:
raise Exception("Expected ref %s point to %s, but it points to %s" % (variable, expectedTagName, actualTagName))
else:
raise Exception("Field %s can be marked as a reference pointing to %s" % (variable, indexEntry.tag))
setattr(owner, self.name, referencedObjects)
def getListContentStructureDefinition(self, li, contextString):
if self.historyOfReferencedStructures is None:
if len(li) == 0:
return None
else:
variable = "%(fieldName)s" % {"fieldName": self.name}
raise Exception("%s: %s must be an empty list but wasn't" % (contextString, variable))
if self.historyOfReferencedStructures.isPrimitive:
return self.historyOfReferencedStructures.getVersion(0)
if type(li) != list:
raise Exception("%s: Expected a list, but was a %s" % (contextString, type(li)))
if len(li) == 0:
return None
firstElement = li[0]
contentClass = type(firstElement)
if contentClass != M3Structure:
raise Exception("%s: Expected a list to contain an M3Structure object and not a %s" % (contextString, contentClass))
# Optional: Enable check:
# if not contentClass.tagName == tagName:
# raise Exception("Expected a list to contain a object of a class with tagName %s, but it contained a object of class %s with tagName %s" % (tagName, contentClass, contentClass.tagName))
return firstElement.structureDescription
def readFromBuffer(self, owner, buffer, offset, checkExpectedValue):
referenceObject = self.referenceStructureDescription.createInstance(buffer, offset, checkExpectedValue)
setattr(owner, self.name, referenceObject)
def writeToBuffer(self, owner, buffer, offset):
referenceObject = getattr(owner, self.name)
referenceObject.writeToBuffer(buffer, offset)
def setToDefault(self, owner):
if self.historyOfReferencedStructures is not None:
defaultValue = self.historyOfReferencedStructures.createEmptyArray()
else:
defaultValue = []
setattr(owner, self.name, defaultValue)
# The method validateContent is defined in subclasses
class CharReferenceField(ReferenceField):
def __init__(self, name, referenceStructureDescription, historyOfReferencedStructures, sinceVersion, tillVersion):
ReferenceField.__init__(self, name, referenceStructureDescription, historyOfReferencedStructures, sinceVersion, tillVersion)
def validateContent(self, fieldContent, fieldPath):
if (fieldContent is not None) and (type(fieldContent) != str):
raise Exception("%s is not a string but a %s" % (fieldPath, type(fieldContent)))
class ByteReferenceField(ReferenceField):
def __init__(self, name, referenceStructureDescription, historyOfReferencedStructures, sinceVersion, tillVersion):
ReferenceField.__init__(self, name, referenceStructureDescription, historyOfReferencedStructures, sinceVersion, tillVersion)
def validateContent(self, fieldContent, fieldPath):
if (type(fieldContent) != bytearray):
raise Exception("%s is not a bytearray but a %s" % (fieldPath, type(fieldContent)))
class RealReferenceField(ReferenceField):
def __init__(self, name, referenceStructureDescription, historyOfReferencedStructures, sinceVersion, tillVersion):
ReferenceField.__init__(self, name, referenceStructureDescription, historyOfReferencedStructures, sinceVersion, tillVersion)
def validateContent(self, fieldContent, fieldPath):
if (type(fieldContent) != list):
raise Exception("%s is not a list of float" % (fieldPath))
for itemIndex, item in enumerate(fieldContent):
if type(item) != float:
itemPath = "%s[%d]" % (fieldPath, itemIndex)
raise Exception("%s is not an float" % (itemPath))
class IntReferenceField(ReferenceField):
intRefToMinValue = {"I16_": (-(1 << 15)), "U16_": 0, "I32_": (-(1 << 31)), "U32_": 0, "FLAG": 0}
intRefToMaxValue = {"I16_": ((1 << 15) - 1), "U16_": ((1 << 16) - 1), "I32_": ((1 << 31) - 1), "U32_": ((1 << 32) - 1), "FLAG": ((1 << 32) - 1)}
def __init__(self, name, referenceStructureDescription, historyOfReferencedStructures, sinceVersion, tillVersion):
ReferenceField.__init__(self, name, referenceStructureDescription, historyOfReferencedStructures, sinceVersion, tillVersion)
self.minValue = IntReferenceField.intRefToMinValue[historyOfReferencedStructures.name]
self.maxValue = IntReferenceField.intRefToMaxValue[historyOfReferencedStructures.name]
def validateContent(self, fieldContent, fieldPath):
if (type(fieldContent) != list):
raise Exception("%s is not a list of integers" % (fieldPath))
for itemIndex, item in enumerate(fieldContent):
itemPath = "%s[%d]" % (fieldPath, itemIndex)
if type(item) != int:
raise Exception("%s is not an integer" % (itemPath))
if (item < self.minValue) or (item > self.maxValue):
raise Exception("%s has value %d which is not in range [%s, %s]" % (itemPath, item, self.minValue, self.maxValue))
class StructureReferenceField(ReferenceField):
def __init__(self, name, referenceStructureDescription, historyOfReferencedStructures, sinceVersion, tillVersion):
ReferenceField.__init__(self, name, referenceStructureDescription, historyOfReferencedStructures, sinceVersion, tillVersion)
def validateContent(self, fieldContent, fieldPath):
if (type(fieldContent) != list):
raise Exception("%s is not a list, but a %s" % (fieldPath, type(fieldContent)))
if len(fieldContent) > 0:
structureDescription = self.getListContentStructureDefinition(fieldContent, fieldPath)
if structureDescription.history != self.historyOfReferencedStructures:
raise Exception("Expected that %s is a list of %s and not %s" % (fieldPath, self.historyOfReferencedStructures.name, structureDescription.history.name))
for itemIndex, item in enumerate(fieldContent):
structureDescription.validateInstance(item, "%s[%d]" % (fieldPath, itemIndex))
class UnknownReferenceField(ReferenceField):
def __init__(self, name, referenceStructureDescription, historyOfReferencedStructures, sinceVersion, tillVersion):
ReferenceField.__init__(self, name, referenceStructureDescription, historyOfReferencedStructures, sinceVersion, tillVersion)
def validateContent(self, fieldContent, fieldPath):
if (type(fieldContent) != list) or (len(fieldContent) != 0):
raise Exception("%s is not an empty list" % (fieldPath))
class EmbeddedStructureField(Field):
def __init__(self, name, structureDescription, sinceVersion, tillVersion):
Field.__init__(self, name, sinceVersion, tillVersion)
self.structureDescription = structureDescription
self.size = structureDescription.size
def introduceIndexReferences(self, owner, indexMaker):
emeddedStructure = getattr(owner, self.name)
emeddedStructure.introduceIndexReferences(indexMaker)
def resolveIndexReferences(self, owner, sections):
emeddedStructure = getattr(owner, self.name)
emeddedStructure.resolveReferences(sections)
def toBytes(self, owner):
emeddedStructure = getattr(owner, self.name)
return emeddedStructure.toBytes()
def readFromBuffer(self, owner, buffer, offset, checkExpectedValue):
referenceObject = self.structureDescription.createInstance(buffer, offset, checkExpectedValue)
setattr(owner, self.name, referenceObject)
def writeToBuffer(self, owner, buffer, offset):
emeddedStructure = getattr(owner, self.name)
emeddedStructure.writeToBuffer(buffer, offset)
def setToDefault(self, owner):
v = self.structureDescription.createInstance()
setattr(owner, self.name, v)
def validateContent(self, fieldContent, fieldPath):
self.structureDescription.validateInstance(fieldContent, fieldPath)
class PrimitiveField(Field):
""" Base class for IntField and FloatField """
def __init__(self, name, typeString, sinceVersion, tillVersion, defaultValue, expectedValue):
Field.__init__(self, name, sinceVersion, tillVersion)
self.size = primitiveFieldTypeSizes[typeString]
self.structFormat = struct.Struct("<" + primitiveFieldTypeFormats[typeString])
self.typeString = typeString
self.defaultValue = defaultValue
self.expectedValue = expectedValue
def readFromBuffer(self, owner, buffer, offset, checkExpectedValue):
value = self.structFormat.unpack_from(buffer, offset)[0]
if self.expectedValue is not None and value != self.expectedValue:
structureName = owner.structureDescription.structureName
structureVersion = owner.structureDescription.structureVersion
raise Exception("Expected that field %s of %s (V. %d) has always the value %s, but it was %s" % (self.name, structureName, structureVersion, self.expectedValue, value))
setattr(owner, self.name, value)
def writeToBuffer(self, owner, buffer, offset):
value = getattr(owner, self.name)
return self.structFormat.pack_into(buffer, offset, value)
def setToDefault(self, owner):
setattr(owner, self.name, self.defaultValue)
class IntField(PrimitiveField):
intTypeToMinValue = {"int16": (-(1 << 15)), "uint16": 0, "int32": (-(1 << 31)), "uint32": 0, "int8": -(1 << 7), "uint8": 0}
intTypeToMaxValue = {"int16": ((1 << 15) - 1), "uint16": ((1 << 16) - 1), "int32": ((1 << 31) - 1), "uint32": ((1 << 32) - 1), "int8": ((1 << 7) - 1), "uint8": ((1 << 8) - 1)}
def __init__(self, name, typeString, sinceVersion, tillVersion, defaultValue, expectedValue, bitMaskMap):
PrimitiveField.__init__(self, name, typeString, sinceVersion, tillVersion, defaultValue, expectedValue)
self.minValue = IntField.intTypeToMinValue[typeString]
self.maxValue = IntField.intTypeToMaxValue[typeString]
self.bitMaskMap = bitMaskMap
def validateContent(self, fieldContent, fieldPath):
if (type(fieldContent) != int):
raise Exception("%s is not an int but a %s!" % (fieldPath, type(fieldContent)))
if (fieldContent < self.minValue) or (fieldContent > self.maxValue):
raise Exception("%s has value %d which is not in range [%d, %d]" % (fieldPath, fieldContent, self.minValue, self.maxValue))
def getNamedBit(self, owner, bitName):
mask = self.bitMaskMap[bitName]
intValue = getattr(owner, self.name)
return ((intValue & mask) != 0)
def setNamedBit(self, owner, bitName, value):
mask = self.bitMaskMap[bitName]
intValue = getattr(owner, self.name)
if value:
setattr(owner, self.name, intValue | mask)
else:
if (intValue & mask) != 0:
setattr(owner, self.name, intValue ^ mask)
def getBitNameMaskPairs(self):
return self.bitMaskMap.items()
class FloatField(PrimitiveField):
def __init__(self, name, typeString, sinceVersion, tillVersion, defaultValue, expectedValue):
PrimitiveField.__init__(self, name, typeString, sinceVersion, tillVersion, defaultValue, expectedValue)
def validateContent(self, fieldContent, fieldPath):
if (type(fieldContent) != float):
raise Exception("%s is not a float but a %s!" % (fieldPath, type(fieldContent)))
class Fixed8Field(PrimitiveField):
def __init__(self, name, typeString, sinceVersion, tillVersion, defaultValue, expectedValue):
PrimitiveField.__init__(self, name, typeString, sinceVersion, tillVersion, defaultValue, expectedValue)
def readFromBuffer(self, owner, buffer, offset, checkExpectedValue):
intValue = self.structFormat.unpack_from(buffer, offset)[0]
floatValue = ((intValue / 255.0 * 2.0) - 1)
if checkExpectedValue and self.expectedValue is not None and floatValue != self.expectedValue:
structureName = owner.structureDescription.structureName
structureVersion = owner.structureDescription.structureVersion
raise Exception("Expected that field %s of %s (V. %d) has always the value %s, but it was %s" % (self.name, structureName, structureVersion, self.expectedValue, intValue))
setattr(owner, self.name, floatValue)
def writeToBuffer(self, owner, buffer, offset):
floatValue = getattr(owner, self.name)
intValue = round((floatValue + 1) / 2.0 * 255.0)
return self.structFormat.pack_into(buffer, offset, intValue)
def validateContent(self, fieldContent, fieldPath):
if (type(fieldContent) != float):
raise Exception("%s is not a float but a %s!" % (fieldPath, type(fieldContent)))
class UnknownBytesField(Field):
def __init__(self, name, size, sinceVersion, tillVersion, defaultValue, expectedValue):
Field.__init__(self, name, sinceVersion, tillVersion)
self.size = size
self.structFormat = struct.Struct("<%ss" % size)
self.defaultValue = defaultValue
self.expectedValue = expectedValue
assert self.structFormat.size == self.size
def readFromBuffer(self, owner, buffer, offset, checkExpectedValue):
value = self.structFormat.unpack_from(buffer, offset)[0]
if checkExpectedValue and self.expectedValue is not None and value != self.expectedValue:
raise Exception("Expected that %sV%s.%s has always the value %s, but it was %s" % (owner.structureDescription.structureName, owner.structureDescription.structureVersion, self.name, self.expectedValue, value))
setattr(owner, self.name, value)
def writeToBuffer(self, owner, buffer, offset):
value = getattr(owner, self.name)
return self.structFormat.pack_into(buffer, offset, value)
def setToDefault(self, owner):
setattr(owner, self.name, self.defaultValue)
def validateContent(self, fieldContent, fieldPath):
if (type(fieldContent) != bytes) or (len(fieldContent) != self.size):
raise Exception("%s is not an bytes object of size %s" % (fieldPath, self.size))
class Visitor:
def visitStart(self, generalDataMap):
pass
def visitClassStart(self, generalDataMap, classDataMap):
pass
def visitVersion(self, generalDataMap, classDataMap, versionDataMap):
pass
def visitFieldStart(self, generalDataMap, classDataMap, fieldDataMap):
pass
def visitFieldBit(self, generalDataMap, classDataMap, fieldDataMap, bitDataMap):
pass
def visitFieldEnd(self, generalDataMap, classDataMap, fieldDataMap):
pass
def visitClassEnd(self, generalDataMap, classDataMap):
pass
def visitEnd(self, generalDataMap):
pass
class StructureAttributesReader(Visitor):
def visitClassStart(self, generalDataMap, classDataMap):
xmlNode = classDataMap["xmlNode"]
if xmlNode.hasAttribute("name"):
classDataMap["structureName"] = xmlNode.getAttribute("name")
else:
raise Exception("There is a structure without a name attribute")
classDataMap["versionToSizeMap"] = {}
def visitVersion(self, generalDataMap, classDataMap, versionDataMap):
xmlNode = versionDataMap["xmlNode"]
if xmlNode.hasAttribute("number"):
version = int(xmlNode.getAttribute("number"))
else:
raise Exception("The structure %s has a version element without a number attribute" % classDataMap["tagName"])
if xmlNode.hasAttribute("size"):
sizeString = xmlNode.getAttribute("size")
try:
size = int(sizeString)
except ValueError:
structureName = classDataMap["structureName"]
raise Exception("The size specified for version %d of structure %s is not an int" % (version, structureName))
else:
raise Exception("The structure %s has a version element without a size attribute" % classDataMap["tagName"])
versionToSizeMap = classDataMap["versionToSizeMap"]
if version in versionToSizeMap:
raise Exception(("The structure %s has two times the same version" % classDataMap["tagName"]))
versionToSizeMap[version] = size
class StructureDescriptionReader(Visitor):
def visitClassStart(self, generalDataMap, classDataMap):
xmlNode = classDataMap["xmlNode"]
tagDescriptionNodes = xmlNode.getElementsByTagName("description")
if len(tagDescriptionNodes) != 1:
fullName = classDataMap["fullName"]
raise Exception("Tag %s has not exactly one description node", fullName)
tagDescriptionNode = tagDescriptionNodes[0]
tagDescription = ""
for descriptionChild in tagDescriptionNode.childNodes:
if descriptionChild.nodeType == Node.TEXT_NODE:
tagDescription += descriptionChild.data
classDataMap["description"] = tagDescription
class FieldAttributesReader(Visitor):
def visitFieldStart(self, generalDataMap, classDataMap, fieldDataMap):
xmlNode = fieldDataMap["xmlNode"]
if xmlNode.hasAttribute("name"):
fieldDataMap["fieldName"] = xmlNode.getAttribute("name")
else:
fullName = classDataMap["fullName"]
raise Exception("There is a field in %s without a name attribute" % fullName)
if xmlNode.hasAttribute("type"):
fieldDataMap["typeString"] = xmlNode.getAttribute("type")
else:
fieldDataMap["typeString"] = None
if xmlNode.hasAttribute("refTo"):
fieldDataMap["refTo"] = xmlNode.getAttribute("refTo")
else:
fieldDataMap["refTo"] = None
if xmlNode.hasAttribute("size"):
fieldDataMap["specifiedFieldSize"] = int(xmlNode.getAttribute("size"))
else:
fieldDataMap["specifiedFieldSize"] = None
if xmlNode.hasAttribute("expected-value"):
fieldDataMap["expectedValueString"] = xmlNode.getAttribute("expected-value")
else:
fieldDataMap["expectedValueString"] = None
if xmlNode.hasAttribute("default-value"):
fieldDataMap["defaultValueString"] = xmlNode.getAttribute("default-value")
else:
fieldDataMap["defaultValueString"] = None
if xmlNode.hasAttribute("till-version"):
fieldDataMap["tillVersion"] = int(xmlNode.getAttribute("till-version"))
else:
fieldDataMap["tillVersion"] = None
if xmlNode.hasAttribute("since-version"):
fieldDataMap["sinceVersion"] = int(xmlNode.getAttribute("since-version"))
else:
fieldDataMap["sinceVersion"] = None
class BitAttributesReader(Visitor):
def visitFieldBit(self, generalDataMap, classDataMap, fieldDataMap, bitDataMap):
xmlNode = bitDataMap["xmlNode"]
if xmlNode.hasAttribute("name"):
bitDataMap["name"] = xmlNode.getAttribute("name")
else:
structureName = classDataMap["structureName"]
fieldName = fieldDataMap["fieldName"]
raise Exception("There is bit xml node in field %(fieldName)s of structure %(structureName)s without a name attribute" % {"fieldName": fieldName, "structureName": structureName})
if xmlNode.hasAttribute("mask"):
maskString = xmlNode.getAttribute("mask")
if not re.match("0x[0-9]+", maskString):
structureName = classDataMap["structureName"]
fieldName = fieldDataMap["fieldName"]
bitName = bitDataMap["name"]
raise Exception("The bit %(bitName)s of %(structureName)s.%(fieldName)s has an invalid mask attribute" % {"fieldName": fieldName, "structureName": structureName, "bitName": bitName})
bitDataMap["mask"] = int(maskString, 0)
else:
structureName = classDataMap["structureName"]
fieldName = fieldDataMap["fieldName"]
raise Exception("There is bit xml node in field %(fieldName)s of structure %(structureName)s without a mask attribute" % {"fieldName": fieldName, "structureName": structureName})
class ExpectedAndDefaultConstantsDeterminer(Visitor):
def parseHex(self, hexString):
hexString = hexString[2:]
return bytes([int(hexString[x:x + 2], 16) for x in range(0, len(hexString), 2)])
def visitFieldStart(self, generalDataMap, classDataMap, fieldDataMap):
structureName = classDataMap["structureName"]
fieldName = fieldDataMap["fieldName"]
fieldType = fieldDataMap["typeString"]
expectedValueString = fieldDataMap["expectedValueString"]
defaultValueString = fieldDataMap["defaultValueString"]
variableName = "%s.%s" % (structureName, fieldName)
expectedValue = None
defaultValue = None
if fieldType in ("int32", "int16", "int8", "uint8", "uint16", "uint32"):
if expectedValueString is not None:
try:
expectedValue = int(expectedValueString, 0)
except ValueError:
raise Exception("The specified expected value for %s is not an integer" % variableName)
if defaultValueString is not None:
try:
defaultValue = int(defaultValueString, 0)
except ValueError:
raise Exception("The specified default value for %s is not an integer" % variableName)
else:
if expectedValue is not None:
defaultValue = expectedValue
else:
defaultValue = 0
elif fieldType == "float" or fieldType == "fixed8":
if expectedValueString is not None:
try:
expectedValue = float(expectedValueString)
except ValueError:
raise Exception("The specified expected value for %s is not a float" % variableName)
if defaultValueString is not None:
try:
defaultValue = float(defaultValueString)
except ValueError:
raise Exception("The specified default value for %s is not a a float" % variableName)
else:
if expectedValue is not None:
defaultValue = expectedValue
else:
defaultValue = 0.0
elif fieldType is None:
specifiedFieldSize = fieldDataMap["specifiedFieldSize"]
defaultValue = None
if expectedValueString is not None:
if not expectedValueString.startswith("0x"):
hexString = None # * ???
raise Exception('The expected-value "%s" of field %s does not start with 0x' % (hexString, variableName))
expectedValue = self.parseHex(expectedValueString)
defaultValue = expectedValue
if defaultValueString is not None:
if not defaultValueString.startswith("0x"):
raise Exception('The expected-value "%s" of field %s does not start with 0x' % (hexString, variableName))
defaultValue = self.parseHex(defaultValueString)
if defaultValue is None:
defaultValue = bytes(specifiedFieldSize)
fieldDataMap["expectedValue"] = expectedValue
fieldDataMap["defaultValue"] = defaultValue
class BitMaskMapDeterminer(Visitor):
def visitFieldStart(self, generalDataMap, classDataMap, fieldDataMap):
fieldDataMap["bitMaskMap"] = {}
def visitFieldBit(self, generalDataMap, classDataMap, fieldDataMap, bitDataMap):
bitName = bitDataMap["name"]
bitMask = bitDataMap["mask"]
bitMaskMap = fieldDataMap["bitMaskMap"]
bitMaskMap[bitName] = bitMask
class FieldListCreator(Visitor):
def visitClassStart(self, generalDataMap, classDataMap):
classDataMap["fields"] = []
def visitFieldEnd(self, generalDataMap, classDataMap, fieldDataMap):
fields = classDataMap["fields"]
structureName = classDataMap["structureName"]
fieldName = fieldDataMap["fieldName"]
typeString = fieldDataMap["typeString"]
sinceVersion = fieldDataMap["sinceVersion"]
tillVersion = fieldDataMap["tillVersion"]
defaultValue = fieldDataMap["defaultValue"]
expectedValue = fieldDataMap["expectedValue"]
specifiedFieldSize = fieldDataMap["specifiedFieldSize"]
structures = generalDataMap["structures"]
bitMaskMap = fieldDataMap["bitMaskMap"]
# TODO validate field size
if typeString == "tag":
field = TagField(fieldName, sinceVersion, tillVersion)
elif typeString in intTypes:
field = IntField(fieldName, typeString, sinceVersion, tillVersion, defaultValue, expectedValue, bitMaskMap)
elif typeString == "float":
field = FloatField(fieldName, typeString, sinceVersion, tillVersion, defaultValue, expectedValue)
elif typeString == "fixed8":
field = Fixed8Field(fieldName, typeString, sinceVersion, tillVersion, defaultValue, expectedValue)
elif typeString is None:
field = UnknownBytesField(fieldName, specifiedFieldSize, sinceVersion, tillVersion, defaultValue, expectedValue)
else:
vPos = typeString.rfind("V")
if vPos != -1:
fieldStructureName = typeString[:vPos]
fieldStructureVersion = int(typeString[vPos + 1:])
else:
fieldStructureName = typeString
fieldStructureVersion = 0
if fieldStructureName == "Reference" or fieldStructureName == "SmallReference":
refTo = fieldDataMap["refTo"]
if (refTo is not None) and (not (refTo in structures)):
raise Exception("The structure with name %s referenced by %s.%s is not defined" % (refTo, structureName, fieldName))
if refTo is not None:
historyOfReferencedStructures = structures[refTo]
else:
historyOfReferencedStructures = None
referenceStructureDescription = structures[fieldStructureName].getVersion(fieldStructureVersion)
if refTo is None:
field = UnknownReferenceField(fieldName, referenceStructureDescription, historyOfReferencedStructures, sinceVersion, tillVersion)
elif refTo == "CHAR":
field = CharReferenceField(fieldName, referenceStructureDescription, historyOfReferencedStructures, sinceVersion, tillVersion)
elif refTo == "U8__":
field = ByteReferenceField(fieldName, referenceStructureDescription, historyOfReferencedStructures, sinceVersion, tillVersion)
elif refTo == "REAL":
field = RealReferenceField(fieldName, referenceStructureDescription, historyOfReferencedStructures, sinceVersion, tillVersion)
elif refTo in ["I16_", "U16_", "I32_", "U32_", "FLAG"]:
field = IntReferenceField(fieldName, referenceStructureDescription, historyOfReferencedStructures, sinceVersion, tillVersion)
else:
field = StructureReferenceField(fieldName, referenceStructureDescription, historyOfReferencedStructures, sinceVersion, tillVersion)
else:
fieldStructureHistory = structures.get(fieldStructureName)
if fieldStructureHistory is None: