-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSSS.py
More file actions
176 lines (133 loc) · 4.32 KB
/
SSS.py
File metadata and controls
176 lines (133 loc) · 4.32 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
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
from struct import pack
obj_opening = 0
obj_closing = 1
field_tag = 2
named_tag = 3
class SSSObject:
def __init__(self):
self.fields = []
self.named_fields = {}
class FileWrapper:
def __init__(self, File):
self.data = File
self.data.seek(0)
self.byte = b''
def next(self):
self.byte = self.data.read(1)
if self.byte != b'':
self.byte = self.byte[0]
return self.byte
def current(self):
return self.byte
class IterWrapper:
def __init__(self, iterable):
self.length = len(iterable)
self.iterable = iter(iterable)
self.curr = None
def next(self):
if self.length != 0:
self.curr = self.iterable.__next__()
self.length -= 1
return self.curr
def current(self):
return self.curr
def tokenize(file: FileWrapper):
tokens = []
file.next()
while True:
if file.current() == obj_opening:
tokens.append(obj_opening)
elif file.current() == obj_closing:
tokens.append(obj_closing)
elif file.current() == named_tag:
tokens.append(named_tag)
elif file.current() == field_tag:
length = bytearray()
for n in range(8):
length.append(file.next())
length = int.from_bytes(length, "big")
data = bytearray()
for n in range(length):
data.append(file.next())
tokens.append(field_tag)
tokens.append(bytes(data))
else:
raise SyntaxError(f"unexpected char {file.data.tell(), file.current()}")
if file.next() == b'':
return tokens
def parseobj(tokens):
obj = SSSObject()
while True:
tokens.next()
if tokens.current() == obj_closing:
return obj
if tokens.current() == named_tag:
field1, field2 = parsenamed(tokens)
obj.named_fields[field1] = field2
elif tokens.current() == field_tag:
obj.fields.append(parsefield(tokens))
elif tokens.current() == obj_opening:
obj.fields.append(parseobj(tokens))
else:
raise SyntaxError(
f"unexpected char {tokens.current()} at {tokens.pointer()}, expected {field_tag} or {obj_opening}")
def parsenamed(tokens):
tokens.next()
if tokens.current() == field_tag:
field1 = tokens.next()
else:
raise SyntaxError(f"unexpected char {tokens.current()} at {tokens.pointer()}, expected {field_tag}")
tokens.next()
if tokens.current() == field_tag:
field2 = tokens.next()
elif tokens.current() == obj_opening:
field2 = parseobj(tokens)
else:
raise SyntaxError(
f"unexpected char {tokens.current()} at {tokens.pointer()}, expected {field_tag} or {obj_opening}")
return field1, field2
def parsefield(tokens):
return tokens.next()
def parse(file):
file = FileWrapper(file)
file.next()
if file.current() == obj_opening:
return \
parseobj(
IterWrapper(
tokenize(file)
)
)
else:
raise SyntaxError(f"unexpected char {file.current()}")
def serializefield(field):
serialized = bytearray()
if isinstance(field, SSSObject):
serialized.extend(serialize(field))
elif isinstance(field, bytes):
serialized.append(field_tag)
serialized.extend(pack(">Q", len(field)))
serialized.extend(field)
return serialized
def serializenamed(key, field):
serialized = bytearray()
serialized.append(named_tag)
serialized.append(field_tag)
serialized.extend(pack(">Q", len(key)))
serialized.extend(key)
if isinstance(field, SSSObject):
serialized.extend(serialize(field))
elif isinstance(field, bytes):
serialized.append(field_tag)
serialized.extend(pack(">Q", len(field)))
serialized.extend(field)
return serialized
def serialize(obj: SSSObject):
serialized = bytearray()
serialized.append(obj_opening)
for field in obj.fields:
serialized.extend(serializefield(field))
for key, field in obj.named_fields.items():
serialized.extend(serializenamed(key, field))
serialized.append(obj_closing)
return serialized