-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathViewState2JSON.py
251 lines (202 loc) · 5.74 KB
/
ViewState2JSON.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
import sys
import struct
import datetime
import json
from base64 import b64decode, b64encode
def printverbose(string):
if len(sys.argv) > 2 and sys.argv[2] == '-v':
print string
def popp(s, n):
a = ''
for c in range(n):
a += s.pop(0)
return a
Marker_Format = 0xff
Marker_Version_1 = 0x01
def Length(s):
l = 0
shift = 0
c = True
while c:
byte = struct.unpack('<B', popp(s, 1))[0]
if byte&128:
byte^=128
else:
c = False
l += byte<<shift
shift+=7
return l
def LengthPrefixedString(s):
return popp(s, Length(s))
def Token_Null(s):
return None
def Token_EmptyString(s):
return ""
def Token_String(s):
return LengthPrefixedString(s)
def Token_ZeroInt32(s):
return 0
def Token_Int32(s):
return Length(s)
def Token_Pair(s):
return (DeserializeValue(s), DeserializeValue(s))
def Token_Triplet(s):
return (DeserializeValue(s), DeserializeValue(s), DeserializeValue(s))
StringList = []
def Token_IndexedString(s):
index = struct.unpack('<B',popp(s,1))[0]
return StringList[index]
def Token_IndexedStringAdd(s):
string = Token_String(s)
StringList.append(string)
return StringList[-1]
def Token_ArrayList(s):
count = Length(s)
array = []
for i in range(count):
array.append(DeserializeValue(s))
return array
def Token_Array(s):
type = Token_Type(s)
count = Length(s)
array = []
for i in range(count):
array.append(DeserializeValue(s))
return {'type': type, 'array':array}
def Token_True(s):
return True
def Token_False(s):
return False
def Token_Byte(s):
return struct.unpack('<B', popp(s,1))[0]
def Token_Double(s):
return struct.unpack('<Q', popp(s,8))[0]
def Token_DateTime(s):
return Token_Double(s)
def Token_Int16(s):
return struct.unpack('<H', popp(s,2))[0]
def Token_Single(s):
return struct.unpack('<I', popp(s,4))[0]
TypeList = []
def Token_Type(s):
token = struct.unpack('<B',popp(s,1))[0]
printverbose(TokenEnum[token][0])
return TokenEnum[token][1](s)
def Token_TypeRef(s):
index = Length(s)
return {'Token_TypeRef': index}
def Token_TypeRefAdd(s):
TypeList.append(Token_String(s))
return TypeList[-1]
def Token_StringArray(s):
count = Length(s)
array = []
for i in range(count):
array.append(Token_String(s))
return array
def Token_IntEnum(s):
value = {}
token = struct.unpack('<B',popp(s,1))[0]
printverbose(TokenEnum[token][0])
key = TokenEnum[token][1](s)
value[key] = Length(s)
printverbose(value)
return value
def Token_Color(s):
return Token_Int32(s)
def Token_EmptyColor(s):
return None
def Token_KnownColor(s):
return Length(s)
def Token_Unit(s):
value = {}
value[Token_Double(s)] = Token_Int32(s)
return value
def Token_EmptyUnit(s):
return None
def Token_EventValidationStore(s):
print "Not Implemented"
sys.exit()
def Token_SparseArray(s):
print "Not Implemented"
sys.exit()
def Token_StringFormatted(s):
value = {}
token = struct.unpack('<B',popp(s,1))[0]
printverbose(TokenEnum[token][0])
key = TokenEnum[token][1](s)
value[key] = Token_String(s)
printverbose(value)
return value
def Token_BinarySerialized(s):
return b64encode(LengthPrefixedString(s))
def Token_HybridDictionary(s):
count = Length(s)
array = []
for i in range(count):
array.append({'Key': DeserializeValue(s), 'Value': DeserializeValue(s)})
return array
TokenEnum = {
1:['Token_Int16', Token_Int16],
2:['Token_Int32', Token_Int32],
3:['Token_Byte', Token_Byte],
4:['Token_Char', Token_Byte],
5:['Token_String', Token_String],
6:['Token_DateTime', Token_DateTime],
7:['Token_Double', Token_Double],
8:['Token_Single', Token_Single],
9:['Token_Color', Token_Color],
10:['Token_KnownColor', Token_Color],
11:['Token_IntEnum', Token_IntEnum],
12:['Token_EmptyColor', Token_Color],
15:['Token_Pair', Token_Pair],
16:['Token_Triplet', Token_Triplet],
20:['Token_Array', Token_Array],
21:['Token_StringArray', Token_StringArray],
22:['Token_ArrayList', Token_ArrayList],
23:['Token_Hashtable', Token_ArrayList],
24:['Token_HybridDictionary', Token_HybridDictionary],
25:['Token_Type', Token_Type],
27:['Token_Unit', Token_Unit],
28:['Token_EmptyUnit', Token_EmptyUnit],
29:['Token_EventValidationStore', Token_EventValidationStore],
30:['Token_IndexedStringAdd', Token_IndexedStringAdd],
31:['Token_IndexedString', Token_IndexedString],
40:['Token_StringFormatted', Token_StringFormatted],
41:['Token_TypeRefAdd', Token_TypeRefAdd],
42:['Token_TypeRefAddLocal', Token_TypeRefAdd],
43:['Token_TypeRef', Token_TypeRef],
50:['Token_BinarySerialized', Token_BinarySerialized],
60:['Token_SparseArray', Token_SparseArray],
100:['Token_Null', Token_Null],
101:['Token_EmptyString', Token_EmptyString],
102:['Token_ZeroInt32', Token_ZeroInt32],
103:['Token_True', Token_True],
104:['Token_False', Token_False]
}
def DeserializeValue(s):
token = struct.unpack('<B',popp(s,1))[0]
printverbose(TokenEnum[token][0])
value = {}
value[TokenEnum[token][0]] = TokenEnum[token][1](s)
return value
def Deserialize(s):
formatMarker = struct.unpack('<B',popp(s,1))[0]
if formatMarker == Marker_Format:
versionMarker = struct.unpack('<B',popp(s,1))[0]
if versionMarker == Marker_Version_1:
return DeserializeValue(s)
else:
return {}
else:
return {}
if len(sys.argv) < 2:
print "Usage: %s <stream> (-v)"%sys.argv[0]
sys.exit(0)
f=open(sys.argv[1])
stream = list(b64decode(f.read()))
f.close()
myObject = Deserialize(stream)
print json.dumps({"myObject": myObject, "typeList": TypeList})
if len(stream) > 0:
print ''.join(stream).encode("hex")