Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- __author__ = 'mandrake'
- import struct
- filesize = None
- def header_unpack(data):
- #q = f.tell()
- record_id = struct.unpack('I', data[0:4])[0]
- header_size = struct.unpack('I', data[4:8])[0]
- always_1 = struct.unpack('I', data[8:12])[0]
- hardware_id = data[12:20]
- file_seq = struct.unpack('I', data[20:24])[0]
- file_size = struct.unpack('I', data[24:28])[0]
- file_date = data[28:44]
- file_time = data[44:60]
- return ({
- 'record_id': record_id,
- 'header_size': header_size,
- 'always_1': always_1,
- 'hardware_id': hardware_id,
- 'file_seq': file_seq,
- 'file_size': file_size,
- 'file_date': file_date,
- 'file_time': file_time
- }, data[header_size:])
- def entry_unpack(data):
- name = data[0:16]
- int1 = struct.unpack('I', data[16:20])[0]
- int2 = struct.unpack('I', data[20:24])[0]
- int3 = struct.unpack('I', data[24:28])[0]
- return {
- 'name': name,
- 'int1': int1,
- 'int2': int2,
- 'int3': int3
- }
- def section_unpack(data, first=False):
- global filesize
- (hdr, data) = header_unpack(data)
- for k in sorted(hdr.keys()):
- print k, hdr[k]
- if first:
- unknown = [
- struct.unpack('I', data[i*4:(i+1)*4])[0]
- for i in range(0, 4)
- ]
- print 'Magic1: %d' % unknown[0]
- print 'Magic2: %d' % unknown[1]
- print 'Version: %d' % unknown[2]
- print 'Sections: %d' % unknown[3]
- entries = [entry_unpack(data[16:][i*28:(i+1)*28]) for i in range(0, unknown[3])]
- i = 0
- for e in entries:
- print "Entry %02d: " % i,
- print "%18s" % e['name'],
- print "%12d" % e['int1'],
- print "%12d" % e['int2'],
- print "%12d" % e['int3']
- i += 1
- #x = unknown[3] * 28
- #x += 16
- qq = data[0:hdr['file_size']], data[hdr['file_size']:]
- #print "ASDFASDF" + str((filesize - len(qq[1])) % 4)
- return qq[0], qq[1][(len(qq[1]) - filesize) % 4:]
- def fw_unpack(data):
- #data.read(0x5c)
- print
- print "Section 0:"
- print
- out = open('file0.bin', 'wb')
- (w, data) = section_unpack(data[0x5c:], first=True)
- out.write(w)
- out.close()
- #for i in range(1, 11):
- i = 1
- while len(data) != 0:
- print
- print "Section %d:" % i
- print
- out = open('file%d.bin' % i, 'wb')
- (w, data) = section_unpack(data)
- out.write(w)
- out.close()
- print len(data)
- i += 1
- if __name__ == '__main__':
- global filesize
- f = file('./huawei/E173/250', 'rb')
- q = f.read()
- filesize = len(q)
- fw_unpack(q)
- f.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement