-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathJSON2ViewState.py
163 lines (138 loc) · 4.02 KB
/
JSON2ViewState.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
import sys
from urllib import quote_plus
import json
import struct
from base64 import b64decode, b64encode, urlsafe_b64encode
def Length(string):
value = len(string)
ret = ''
while value >= 0x80:
ret += chr((value | 0x80)&0xff)
value >>= 7
ret += chr(value)
return ret
def LengthPrefixedString(string):
ret = Length(string)+str(string)
return ret
def Token_Null(o):
return ''
def Token_Pair(o):
one = o[0]
two = o[1]
return SerializeValue(one) + SerializeValue(two)
def Token_String(o):
return LengthPrefixedString(o)
def Token_Type(o):
return SerializeValue(o)
def Token_StringFormatted(o):
type = o.keys()[0]
res = ''
if type in TypeList:
res += struct.pack('<B', TokenEnum['Token_TypeRefAdd'][0])
res += LengthPrefixedString(type)
res += LengthPrefixedString(o[type])
else:
pass
return res
def Token_IntEnum(o):
type = o.keys()[0]
res = ''
if type in TypeList:
res += struct.pack('<B', TokenEnum['Token_TypeRefAdd'][0])
res += LengthPrefixedString(type)
res += Length("a"*o[type])
else:
pass
return res
def Token_ArrayList(oList):
res = struct.pack('<B', len(oList))
for o in oList:
res += SerializeValue(o)
return res
def Token_TypeRef(o):
return Length("a"*o)
def Token_Array(o):
array = o['array']
type = o['type']
s = SerializeValue(type) + Length("a"*len(array))
for i in range(len(array)):
s += SerializeValue(o['array'][i])
return s
def Token_False(o):
return ''
def Token_Int32(o):
return Length("a"*o)
def Token_IndexedStringAdd(o):
return LengthPrefixedString(o)
def Token_BinarySerialized(o):
if o[0] == '@':
f=open(o[1:])
content = f.read()
f.close()
else:
content = o
return LengthPrefixedString(b64decode(content))
def Token_TypeRefAdd(o):
pass
TokenEnum = {
# 'Token_Int16':[1, Token_Int16],
'Token_Int32':[2, Token_Int32],
# 'Token_Byte':[3, Token_Byte],
# 'Token_Char':[4, Token_Byte],
'Token_String':[5, Token_String],
# 'Token_DateTime':[6, Token_DateTime],
# 'Token_Double':[7, Token_Double],
# 'Token_Single':[8, Token_Single],
# 'Token_Color':[9, Token_Color],
# 'Token_KnownColor':[10, Token_Color],
'Token_IntEnum':[11, Token_IntEnum],
# 'Token_EmptyColor':[12, Token_Color],
'Token_Pair':[15, Token_Pair],
# 'Token_Triplet':[16, Token_Triplet],
'Token_Array':[20, Token_Array],
# 'Token_StringArray':[21, Token_StringArray],
'Token_ArrayList':[22, Token_ArrayList],
# 'Token_Hashtable':[23, Token_ArrayList],
# 'Token_HybridDictionary':[24, Token_ArrayList],
'Token_Type':[25, Token_Type],
# 'Token_Unit':[27, Token_Unit],
# 'Token_EmptyUnit':[28, Token_EmptyUnit],
# 'Token_EventValidationStore':[29, Token_EventValidationStore],
'Token_IndexedStringAdd':[30, Token_IndexedStringAdd],
# 'Token_IndexedString':[31, Token_IndexedString],
'Token_StringFormatted':[40, Token_StringFormatted],
'Token_TypeRefAdd':[41, Token_TypeRefAdd],
# 'Token_TypeRefAddLocal':[42, Token_TypeRefAdd],
'Token_TypeRef':[43, Token_TypeRef],
'Token_BinarySerialized':[50, Token_BinarySerialized],
# 'Token_SparseArray':[60, Token_SparseArray],
'Token_Null':[100, Token_Null],
# 'Token_EmptyString':[101, Token_EmptyString],
# 'Token_ZeroInt32':[102, Token_ZeroInt32],
# 'Token_True':[103, Token_True],
'Token_False':[104, Token_False]
}
Marker_Format = 0xff
Marker_Version_1 = 0x01
def SerializeValue(o):
token = o.keys()[0]
value = ''
value += struct.pack('<B', TokenEnum[token][0])
value += TokenEnum[token][1](o[token])
return value
if len(sys.argv) < 2:
print "Usage: %s <stream> (prefix)"%sys.argv[0]
sys.exit(0)
f=open(sys.argv[1])
raw = json.loads(f.read())
myObject = raw['myObject']
TypeList = raw['typeList']
f.close()
binary = chr(Marker_Format) + chr(Marker_Version_1)
binary += SerializeValue(myObject)
f = open(sys.argv[1]+'_binary', 'w')
if len(sys.argv == 3):
f.write(sys.argv[2]+quote_plus(b64encode(binary)))
else:
f.write(b64encode(binary))
f.close()