-
Notifications
You must be signed in to change notification settings - Fork 0
/
fbone_swfmovie.py
156 lines (124 loc) · 4.18 KB
/
fbone_swfmovie.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
#!/usr/bin/env python3.9
## Swfmovie/BIG extractor by EmilHernvall
## Edited for swfmovie files and ported to Python 3 by Heico
## Requires Python 3.9.6! Other versions may not work.
## Use command line or drag n drop file onto the script to convert
## Supported file formats: .swfmovie
## Old description:
# bigdecoder.py, by aderyn@gmail.com, 2009-03-01
#
# decoder for .BIG-format files utilized by Red Alert 3 and C&C: Zero Hours
# among others. .big is a trivial archival format. quite frankly, this is
# probably the simplest compound file format imaginable.
#
# this script is written for microsoft windows. it can probably be easily
# adapted for other platforms, but i haven't tried.
#
# file structure:
# the file consists of a global header, an index of all the embedded
# files, and the actual file data.
#
# global header {
# header, charstring, 4 bytes - always BIG4 or something similiar
# total file size, unsigned integer, 4 bytes, little endian byte order
# number of embedded files, unsigned integer, 4 bytes, big endian byte order
# total size of index table in bytes, unsigned integer, 4 bytes, big endian byte order
# }, only occurs once
#
# index of files, follows directly after
# the global header:
# index entry {
# position of embedded file within BIG-file, unsigned integer, 4 bytes, big endian byte order
# size of embedded data, unsigned integer, 4 bytes, big endian byte order
# file name, cstring, ends with null byte
# }, repeats for each embedded file
#
# file data:
# raw file data at the positions specified in the index
import struct
import sys
import os
import os.path
# Define an empty class to emulate a c struct
# that can hold the data for each entry in the
# file index.
class entry:
pass
if len(sys.argv) != 3:
print("usage: python3 fbone_swfmovie.py [file] [target]")
exit()
print("BIG-file decoder by aderyn@gmail.com")
filePath = sys.argv[1]
targetDir = sys.argv[2]
if not os.path.exists(filePath):
print("Requested file doesn't exist.")
exit()
if targetDir[-1] != "\\":
targetDir += "\\"
print("Processing " + filePath)
# open the file in binary read mode.
# without the b-flag the tell-method
# returns the wrong value.
file = open(filePath, "rb")
# read global header:
# this seems to vary. zero hour uses BIGF
header = file.read(4)
if header != b"BIGF":
print("Invalid file format.")
exit()
# this seems to be the only value encoded in
# little-endian order.
(size,) = struct.unpack("I", file.read(4))
print("size: %d" % (size,))
(entryCount,indexSize) = struct.unpack(">II", file.read(8))
print("entry count: %d" % (entryCount,))
print("index size: %d" % (indexSize,))
# read the index table:
# assume that the file contains the amount of
# entries specified by the global header
entries = []
for j in range(0, entryCount):
(entryPos,entrySize) = struct.unpack(">II", file.read(8))
# the filename is stored as a cstring and
# ends with a null byte. read until we reach
# this byte.
fileName = ""
while True:
n = file.read(1)
if ord(n) == 0:
break
fileName += n.decode("ascii")
e = entry()
e.name = fileName
e.position = entryPos
e.size = entrySize
entries.append(e)
# iterate through the index entries and
# copy the data into separate files.
for i, e in enumerate(entries):
print("opening %s (size: %d, position: %d)" % (e.name,e.size,e.position))
print("file %d of %d" % (i+1, entryCount))
# calculate the path where the file will be created
# in order to ensure that the directories needed actually
# exists
fileTargetDir = targetDir + e.name[0:e.name.rfind("\\")] + "\\"
fileName = e.name[e.name.rfind("\\")+1:]
targetPath = fileTargetDir + fileName
# create the directories if they don't exist.
if not os.path.exists(fileTargetDir):
os.makedirs(fileTargetDir)
# skip files that already exist.
if os.path.exists(targetPath):
print("%s exists. Skipping." % (targetPath,))
continue
print("Opening %s for writing" % (targetPath,))
targetFile = open(targetPath, "wb")
print("Seeked to %d" % (e.position,))
file.seek(e.position)
print("Starting data transfer")
for i in range(0, e.size):
byte = file.read(1)
targetFile.write(byte)
print("Wrote %d bytes" % (e.size,))
print("Done, closing file.")
targetFile.close()