-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjriff.py
197 lines (157 loc) · 4.16 KB
/
jriff.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
#!python
#
# Read & dump RIFF file.
# Make guesses about chunks
import sys
majors = (
"RIFF",
"LIST",
"INFO",
)
dbg = True
def get_sint16(file):
val = get_uint16(file)
if val >= 0x8000:
return val - 0x10000
return val
def put_sint16(file):
bytes = ""
bytes += chr(val & 0xff)
val = val >> 8
bytes += chr(val & 0xff)
ofile.write(bytes)
def get_sint8(file):
print "8-bit format unsupported"
sys.exit(1)
def get_uint32(file):
bytes = file.read(4)
return ((((0L
+ ord(bytes[3]) << 8)
+ ord(bytes[2]) << 8)
+ ord(bytes[1]) << 8)
+ ord(bytes[0]))
def put_uint32(ofile, val):
bytes = ""
bytes += chr(val & 0xff)
val = val >> 8
bytes += chr(val & 0xff)
val = val >> 8
bytes += chr(val & 0xff)
val = val >> 8
bytes += chr(val & 0xff)
ofile.write(bytes)
def get_uint24(file):
bytes = file.read(3)
return (((
ord(bytes[2]) << 8)
+ ord(bytes[1]) << 8)
+ ord(bytes[0]))
def get_uint16(file):
bytes = file.read(2)
return (ord(bytes[1]) << 8) + ord(bytes[0])
def put_uint16(ofile, val):
bytes = ""
bytes += chr(val & 0xff)
val = val >> 8
bytes += chr(val & 0xff)
ofile.write(bytes)
def get_uint8(file):
return ord(file.read(1))
def roundup(ln):
if ln & 1:
return ln + 1
return ln
class Chunk:
def __init__(self, riffFile, parent):
self.parent = parent
self.riff = riffFile
self.subchunks = []
if parent == None:
self.level = 0
else:
self.level = parent.level + 1
def ind(self):
str = ""
for ix in range(0, self.level):
str += " "
return str
def iseek(self):
self.riff.inf.seek(self.inf_loc)
def inf(self):
return self.riff.inf
def read(self):
self.format = self.riff.inf.read(4)
self.len = get_uint32(self.riff.inf)
self.inf_loc = self.riff.inf.tell() # location of value
if dbg:
print "[d] 0x%08x: %s %s" %(self.inf_loc - 8, self.ind(), self.format),
print "len: 0x%08x" % self.len,
if self.format not in majors:
if dbg:
print "loc: 0x%08x" % self.inf_loc
self.riff.inf.seek(self.inf_loc + roundup(self.len), 0)
return 8 + roundup(self.len)
self.type = self.riff.inf.read(4)
self.inf_loc += 4
if dbg:
print "type:", self.type,
print "loc: 0x%08x" % self.inf_loc
ln = 4
while ln < self.len - 1:
chunk = Chunk(self.riff, self)
chunklen = chunk.read()
if ln + chunklen > self.len:
print "Error: last chunk exceeded parent's len"
sys.exit(1)
self.subchunks.append(chunk)
ln += chunklen
# print self.ind(), "-- %s at 0x%08x, ln = 0x%08x" % (self.format, self.inf_loc + ln, ln)
if self.len != roundup(ln):
print "Error: insufficient data"
return roundup(self.len + 8)
def printHdr(self):
print "0x%08x: %s %s" %(self.inf_loc - 8, self.ind(), self.format),
print "len: 0x%08x" % self.len,
if "type" in dir(self):
print "type:", self.type
else:
print
def walk(self, func, arg=None):
func(self, arg)
for chunk in self.subchunks:
chunk.walk(func, arg)
def prn(self, text):
print "%11s %s %s" % ("", self.ind(), text)
def prnLoc(self, text):
print "0x%08x %s %s" % (self.inf_loc, self.ind(), text)
class RiffFile:
def __init__(self, inf=None, outf=None):
self.inf = inf
self.inf_ix = 0
self.outf = outf
self.outf_ix = 0
self.vsize = None
def read(self):
if not self.inf:
return
self.chunk = Chunk(self, None)
self.chunk.read()
def walk(self, func, arg=None):
if "chunk" in dir(self):
self.chunk.walk(func, arg)
def main(args):
if len(args) < 2:
print "usage: %s <infile> -- dumps RIFF file"
print
sys.exit(1)
infname = args[1]
del args[1]
try:
inf = file(infname, "rb")
except IOError, msg:
print msg
sys.exit(1)
riff = RiffFile(inf)
riff.read()
if __name__ == "__main__":
main(sys.argv)