-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathastobjects.py
More file actions
66 lines (60 loc) · 2.31 KB
/
astobjects.py
File metadata and controls
66 lines (60 loc) · 2.31 KB
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
import pdb
class Structure(object):
def __init__(self, name, fields):
self.name = name
self.fields = fields
def parse(self, bitstream):
parsed = [f.parse(bitstream) for f in self.fields]
return (self.name, [item for sublist in parsed for item in sublist])
def __str__(self):
return "a %s structure with %d fields" % (self.name, len(self.fields))
class Field(object):
def __init__(self, name, width, typ, value=0):
self.type = typ
self.width = width
self.name = name
self.value = value
def parse(self, bitstream):
# lul everything is interpreted as a uint... so the type does nothing (yet)
self.value = bitstream.read(int(self.width())).uint
return [(self.name, self.value)]
def __call__(self):
return self.value
def __str__(self):
return "a %s field %d bits wide called %s" % (self.type, self.width(), self.name)
class Value(object):
def __init__(self, value):
self.value = value
def __call__(self):
return self.value
class IfBlock(object):
def __init__(self, condition, fields):
self.condition = condition
self.fields = fields
def parse(self, bitstream):
if self.condition():
# process all the fields
parsed = [f.parse(bitstream) for f in self.fields]
return [item for sublist in parsed for item in sublist]
else:
return []
def __str__(self):
return "an ifblock on %s with %d fields" % (self.condition, len(self.fields))
class ForLoop(object):
def __init__(self, count, fields):
self.count = count
self.fields = fields
def parse(self, bitstream):
# process all the fields
parsed = [f.parse(bitstream) for f in (self.fields * self.count())]
return [item for sublist in parsed for item in sublist]
def __str__(self):
return "a forloop %d times with %d fields" % (self.count(), len(self.fields))
class StructCall(object):
def __init__(self, struct):
self.struct = struct
def parse(self, bitstream):
# process all the fields
return [self.struct.parse(bitstream)]
def __str__(self):
return "a structcall of %s" % (self.struct.name)