Advertisement
wandrake

Untitled

Dec 3rd, 2013
522
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.77 KB | None | 0 0
  1. __author__ = 'mandrake'
  2.  
  3. import struct
  4.  
  5. filesize = None
  6.  
  7.  
  8. def header_unpack(data):
  9.     #q = f.tell()
  10.     record_id    = struct.unpack('I', data[0:4])[0]
  11.     header_size  = struct.unpack('I', data[4:8])[0]
  12.     always_1     = struct.unpack('I', data[8:12])[0]
  13.     hardware_id  = data[12:20]
  14.     file_seq     = struct.unpack('I', data[20:24])[0]
  15.     file_size    = struct.unpack('I', data[24:28])[0]
  16.     file_date    = data[28:44]
  17.     file_time    = data[44:60]
  18.  
  19.     return ({
  20.         'record_id': record_id,
  21.         'header_size': header_size,
  22.         'always_1': always_1,
  23.         'hardware_id': hardware_id,
  24.         'file_seq': file_seq,
  25.         'file_size': file_size,
  26.         'file_date': file_date,
  27.         'file_time': file_time
  28.     }, data[header_size:])
  29.  
  30.  
  31. def entry_unpack(data):
  32.     name = data[0:16]
  33.     int1 = struct.unpack('I', data[16:20])[0]
  34.     int2 = struct.unpack('I', data[20:24])[0]
  35.     int3 = struct.unpack('I', data[24:28])[0]
  36.  
  37.     return {
  38.         'name': name,
  39.         'int1': int1,
  40.         'int2': int2,
  41.         'int3': int3
  42.     }
  43.  
  44.  
  45. def section_unpack(data, first=False):
  46.     global filesize
  47.     (hdr, data) = header_unpack(data)
  48.  
  49.     for k in sorted(hdr.keys()):
  50.         print k, hdr[k]
  51.  
  52.     if first:
  53.         unknown = [
  54.             struct.unpack('I', data[i*4:(i+1)*4])[0]
  55.             for i in range(0, 4)
  56.         ]
  57.  
  58.         print 'Magic1: %d' % unknown[0]
  59.         print 'Magic2: %d' % unknown[1]
  60.         print 'Version: %d' % unknown[2]
  61.         print 'Sections: %d' % unknown[3]
  62.         entries = [entry_unpack(data[16:][i*28:(i+1)*28]) for i in range(0, unknown[3])]
  63.  
  64.         i = 0
  65.         for e in entries:
  66.             print "Entry %02d: " % i,
  67.             print "%18s" % e['name'],
  68.             print "%12d" % e['int1'],
  69.             print "%12d" % e['int2'],
  70.             print "%12d" % e['int3']
  71.             i += 1
  72.  
  73.         #x = unknown[3] * 28
  74.         #x += 16
  75.  
  76.     qq = data[0:hdr['file_size']], data[hdr['file_size']:]
  77.     #print "ASDFASDF" + str((filesize - len(qq[1])) % 4)
  78.     return qq[0], qq[1][(len(qq[1]) - filesize) % 4:]
  79.  
  80.  
  81. def fw_unpack(data):
  82.  
  83.     #data.read(0x5c)
  84.  
  85.     print
  86.     print "Section 0:"
  87.     print
  88.     out = open('file0.bin', 'wb')
  89.  
  90.     (w, data) = section_unpack(data[0x5c:], first=True)
  91.  
  92.     out.write(w)
  93.     out.close()
  94.     #for i in range(1, 11):
  95.     i = 1
  96.     while len(data) != 0:
  97.  
  98.         print
  99.         print "Section %d:" % i
  100.         print
  101.         out = open('file%d.bin' % i, 'wb')
  102.         (w, data) = section_unpack(data)
  103.         out.write(w)
  104.         out.close()
  105.  
  106.         print len(data)
  107.         i += 1
  108.  
  109.  
  110. if __name__ == '__main__':
  111.     global filesize
  112.     f = file('./huawei/E173/250', 'rb')
  113.     q = f.read()
  114.     filesize = len(q)
  115.     fw_unpack(q)
  116.     f.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement