-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathswfzip.py
197 lines (156 loc) · 5.84 KB
/
swfzip.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
#!/usr/bin/python
#
# This script is inspired by jspiro's swf2lzma repo: https://github.com/jspiro/swf2lzma
#
#
# SWF Formats:
## ZWS(LZMA)
## | 4 bytes | 4 bytes | 4 bytes | 5 bytes | n bytes | 6 bytes |
## | 'ZWS'+version | scriptLen | compressedLen | LZMA props | LZMA data | LZMA end marker |
##
## scriptLen is the uncompressed length of the SWF data. Includes 4 bytes SWF header and
## 4 bytes for scriptLen itself
##
## compressedLen does not include header (4+4+4 bytes) or lzma props (5 bytes)
## compressedLen does include LZMA end marker (6 bytes)
#
import os
import pylzma
import sys
import struct
import zlib
def confirm(prompt, resp=False):
"""prompts for yes or no response from the user. Returns True for yes and
False for no.
'resp' should be set to the default value assumed by the caller when
user simply types ENTER.
>>> confirm(prompt='Create Directory?', resp=True)
Create Directory? [y]|n:
True
>>> confirm(prompt='Create Directory?', resp=False)
Create Directory? [n]|y:
False
>>> confirm(prompt='Create Directory?', resp=False)
Create Directory? [n]|y: y
True
"""
if prompt is None:
raise Exception('Not valid prompt')
if resp:
prompt = '%s %s/%s: ' % (prompt, 'Y', 'n')
else:
prompt = '%s %s/%s: ' % (prompt, 'N', 'y')
while True:
ans = raw_input(prompt)
print ''
if not ans:
return resp
if ans not in ['y', 'Y', 'n', 'N']:
print 'please enter y or n.'
continue
if ans == 'y' or ans == 'Y':
return True
if ans == 'n' or ans == 'N':
return False
def debug(msg, level='info'):
print '%s : %s' %(level, msg)
def check(test, msg):
test or exit('Error: \n'+msg)
def unzip(inData):
if inData[0] == 'C':
# zlib SWF
debug('zlib compressed swf detected.')
decompressData = zlib.decompress(inData[8:])
elif inData[0] == 'Z':
# lzma SWF
debug('lzma compressed swf detected.')
decompressData = pylzma.decompress(inData[12:])
elif inData[0] == 'F':
# uncompressed SWF
debug('Uncompressed swf detected.')
decompressData = inData[8:]
else:
exit('not a SWF file')
sigSize = struct.unpack("<I", inData[4:8])[0]
debug('Filesize in signature: %s' % sigSize)
decompressSize = len(decompressData) +8
debug('Filesize decompressed: %s' % decompressSize)
check((sigSize == decompressSize), 'Length not correct, decompression failed')
header = list(struct.unpack("<8B", inData[0:8]))
header[0] = ord('F')
debug('Generating uncompressed data')
return struct.pack("<8B", *header)+decompressData
def zip(inData, compression):
if(compression == 'lzma'):
check((inData[0] != 'Z'), "already LZMA compressed")
rawSwf = unzip(inData);
debug('Compressing with lzma')
compressData = pylzma.compress(rawSwf[8:], eos=1)
# 5 accounts for lzma props
compressSize = len(compressData) - 5
header = list(struct.unpack("<12B", inData[0:12]))
header[0] = ord('Z')
header[3] = header[3]>=13 and header[3] or 13
header[8] = (compressSize) & 0xFF
header[9] = (compressSize >> 8) & 0xFF
header[10] = (compressSize >> 16) & 0xFF
header[11] = (compressSize >> 24) & 0xFF
debug('Packing lzma header')
headerBytes = struct.pack("<12B", *header);
else:
check((inData[0] != 'C'), "already zlib compressed")
rawSwf = unzip(inData);
debug('Compressing with zlib')
compressData = zlib.compress(rawSwf[8:])
compressSize = len(compressData)
header = list(struct.unpack("<8B", inData[0:8]))
header[0] = ord('C')
header[3] = header[3]>=6 and header[3] or 6
debug('Packing zlib header')
headerBytes = struct.pack("<8B", *header)
debug('Generating compressed data')
return headerBytes+compressData
def process(infile, outfile, operation='unzip', compression='zlib'):
debug('Reading '+infile)
fi = open(infile, "rb")
infileSize = os.path.getsize(infile)
inData = fi.read()
fi.close()
check((inData[1] == 'W') and (inData[2] == 'S'), "not a SWF file")
if(operation=='unzip'):
outData = unzip(inData)
increment = round(100.0 * len(outData) / infileSize) - 100
print 'File decompressed, size increased: %d%%' % increment
else:
compression = compression == 'lzma' and 'lzma' or 'zlib'
outData = zip(inData, compression)
decrement = increment = 100 - round(100.0 * len(outData) / infileSize)
print 'File compressed with %s, size decreased: %d%% %s' % (compression, decrement,
decrement<0 and '\n\nNotice: Recompressing may cause filesize increased' or'')
fo = open(outfile, 'wb')
fo.write(outData)
fo.close()
if __name__ == "__main__":
command = 'swfunzip' in sys.argv[0] and 'unzip' or 'zip';
if(command == 'unzip'):
check(len(sys.argv) == 3, 'usage: swfunzip input.swf output.swf')
else:
check(
len(sys.argv) == 3
or
len(sys.argv) == 4 and sys.argv[3] in ['lzma', 'zlib', ''],
'usage: swfzip input.swf output.swf [lzma/zlib] \
\n\n notice:zlib is used if compression type not given.')
infile = sys.argv[1]
debug('Input file: '+infile)
check(os.path.exists(infile), 'Input file not exists')
outfile = sys.argv[2]
debug('Output file: '+outfile)
if os.path.exists(outfile) and not confirm('Output file exists, overwrite?'):
sys.exit(0)
if(command == 'zip'):
compression = len(sys.argv) == 3 and 'zlib' or sys.argv[3]
process(infile, outfile, command, compression)
else:
process(infile, outfile, command)
sys.exit(0)