Add simple NTO viewer
[dormin.git] / reeng / xff2.py
blobcbc0fcd6a018dbd411506c6ba62a980a0ff0a816
1 #!/usr/bin/env python
2 import sys
3 from struct import unpack
5 def xfferr (msg):
6 sys.stderr.write (msg)
7 sys.exit (1)
9 class XFF:
10 """XFF"""
12 def __init__ (self, file, pos):
13 self.pos = pos
15 file.seek (pos)
16 buf = file.read (0x70)
17 D = unpack ("<28I", buf)
19 if not buf[0:4] in ["xff\x00", "xff2"]:
20 xfferr ("%08x: invalid primary signature %s %x" % (pos, buf[0:4], D[0]))
22 if D[1] <> 0:
23 xfferr ("%08x: expected zero got %#x" % (pos + 4, D[1]))
25 if D[2] <> 0:
26 xfferr ("%08x: expected zero got %#x" % (pos + 4, D[2]))
28 self.count_0C = D[3]
29 self.xxxxx_10 = D[4]
30 self.xff_size = D[5]
31 #print D[5]
32 self.xxxxx_18 = D[6]
33 self.count_1C = D[7]
34 self.xxxxx_20 = D[8]
35 self.symcount = D[9]
36 self.xxxxx_28 = D[10]
37 self.xxxxx_2C = D[11]
38 self.xxxxx_30 = D[12]
39 self.xxxxx_34 = D[13]
40 self.count_38 = D[14]
41 self.xxxxx_3C = D[15]
42 self.seccount = D[16]
43 self.xxxxx_44 = D[17]
44 self.xxxxx_48 = D[18]
45 self.off_sign = D[19]
47 self.off_50_count_1C = D[20]
48 self.off_symbols1 = D[21]
49 self.off_symstrtab = D[22]
50 self.off_sections = D[23]
51 self.off_symbols2 = D[24]
52 self.off_64_count_38 = D[25]
53 self.off_secnameoffs = D[26]
54 self.off_secstrtab = D[27]
56 ### Header[0]
57 file.seek (self.off_50_count_1C + self.pos)
58 self.data_50_count_1C = file.read (self.count_1C * 4)
60 ### Header[1,2,4]
61 file.seek (self.off_symbols1 + self.pos)
62 buf = file.read (self.symcount*4*4)
63 L = unpack ("<%dI" % (self.symcount*4), buf)
65 file.seek (self.off_symbols2 + self.pos)
66 buf = file.read (self.symcount*4)
67 LL = unpack ("<%dI" % self.symcount, buf)
69 maxoff = 0
70 symbols = []
71 for i in range (self.symcount):
72 Ls = L[i*4:(i+1)*4]
73 maxoff = max (maxoff, Ls[0])
74 symbols.append (Ls + (LL[i],))
76 file.seek (self.off_symstrtab + self.pos)
77 symstrtab = file.read (maxoff + 512)
79 self.symbols = []
80 for sym in symbols:
81 s = symstrtab[sym[0]:]
82 name = s[:s.find ("\x00")]
83 self.symbols.append ((name, sym[0], sym[1], sym[2], sym[3]))
85 ### Header[5]
86 file.seek (self.off_64_count_38 + self.pos)
87 buf = file.read (self.count_38 * 7 * 4)
88 self.hdr5_data = []
89 for i in range (self.count_38):
90 L = unpack ("<7I", buf[i*7*4:(i+1)*7*4])
91 self.hdr5_data.append (L)
93 ### Header[3,6,7]
94 file.seek (self.off_secnameoffs + self.pos)
95 buf = file.read (self.seccount * 4)
96 offs = unpack ("<%dI" % self.seccount, buf)
97 maxoff = max (offs)
99 file.seek (self.off_secstrtab + self.pos)
100 secstrtab = file.read (maxoff + 512)
102 if self.off_secnameoffs + self.seccount * 4 <> self.off_sections:
103 raise "Oh fuck", self.off_secnameoffs + self.seccount * 4, self.off_sections
105 file.seek (self.off_sections + self.pos)
106 buf = file.read (self.seccount * 8 * 4)
107 L = unpack ("<%dI" % (self.seccount*8), buf)
109 self.sections = []
110 for i in range (self.seccount):
111 Ls = L[i*8:(i+1)*8]
112 # L5Cs = L5C[i*8:(i+1)*8]
113 s = secstrtab[offs[i]:]
114 name = s[:s.find ("\x00")]
115 self.sections.append ((name, Ls))
117 def extr(self, file, sectnr, path):
118 f = open (path, "wb")
119 sect = self.sections[sectnr]
120 file.seek (sect[1][7])
121 buf = file.read (sect[1][2])
122 f.write (buf)
123 f.close ()
125 def pr (self, x):
126 print "count_0C = %d (some kind of section count)" % self.count_0C
127 print "xxxxx_10 = %08x" % self.xxxxx_10
128 print "xff_size = %d" % self.xff_size
129 print "xxxxx_18 = %08x" % self.xxxxx_18
130 print "count_1C = %d" % self.count_1C
131 print "xxxxx_20 = %08x" % self.xxxxx_20
132 print "symcount = %d" % self.symcount
133 print "xxxxx_28 = %08x" % self.xxxxx_28
134 print "xxxxx_2C = %08x" % self.xxxxx_2C
135 print "xxxxx_30 = %08x" % self.xxxxx_30
136 print "xxxxx_34 = %08x" % self.xxxxx_34
137 print "count_38 = %d" % self.count_38
138 print "xxxxx_3C = %08x" % self.xxxxx_3C
139 print "seccount = %d" % self.seccount
140 print "xxxxx_44 = %08x" % self.xxxxx_44
141 print "xxxxx_48 = %08x" % self.xxxxx_48
143 print
144 print "off_sign = %08x %d" % (self.off_sign, self.off_sign)
145 print "50_count_1C = %08x" % self.off_50_count_1C
146 print "symbols1 = %08x" % self.off_symbols1
147 print "symstrtab = %08x" % self.off_symstrtab
148 print "sections = %08x" % self.off_sections
149 print "symbols2 = %08x" % self.off_symbols2
150 print "64_count_38 = %08x" % self.off_64_count_38
151 print "secnameoffs = %08x" % self.off_secnameoffs
152 print "secstrtab = %08x" % self.off_secstrtab
154 if x:
155 i = 0
156 print "\nSymbols"
157 for sym in self.symbols:
158 print " [%d] %s" % (i, sym[0])
159 print " %08x" % sym[1]
160 print " %08x" % sym[2]
161 print " %08x" % sym[3]
162 print " %08x" % sym[4]
163 i = i + 1
165 i = 0
166 print "\nHdr0"
167 x = unpack (("<%dI" % self.count_1C), self.data_50_count_1C)
168 for i in range (self.count_1C):
169 print " [%5d] %08x %d" % (i, x[i], x[i])
170 i = i + 1
172 i = 0
173 print "\nHdr5"
174 for x in self.hdr5_data:
175 print " [%d]" % i
176 for v in x:
177 print " %08x" % v
178 i = i + 1
180 print "\nSections"
181 i = 0
182 for sec in self.sections:
183 print " [%d] %s" % (i, sec[0])
184 for j in range (8):
185 if j == 2:
186 print " %08x %d size" % (sec[1][j], sec[1][j])
187 elif j == 7:
188 print " %08x (%08x)" % (sec[1][j], sec[1][j] + self.off_sign)
189 else:
190 print " %08x" % sec[1][j]
191 i = i + 1
193 if __name__ == "__main__":
194 f = open (sys.argv[1], "rb")
195 print sys.argv[1]
196 x = XFF (f, 0*0x3fff3edb)
197 x.pr (True)
198 ## x.extr (f, 4, "vutext")
200 ##f.seek (0x4e2)
201 ##s = f.read (0x73e-0x4e2-1)
202 ##print len (s.split ("\x00"))
204 ##f.seek (0x430a50)
205 ##s = f.read (0x00431742-0x430a50-1)
206 ##print len (s.split ("\x00"))