Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2011
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.19 KB | None | 0 0
  1. from struct import pack
  2.  
  3. class TribeDecodeError(Exception):
  4.     def __init__(self, value):
  5.         self.value = value
  6.     def __str__(self):
  7.         return repr(self.value)
  8.  
  9. # Seeks a 'tribe fw bit array for the next packet start.
  10. # Takes an iterator
  11. def packetseek(it):
  12.     numbits = 0;
  13.     while(it.next()):
  14.         numbits += 1;         # Loop through all the junk bits
  15.  
  16.     # print "%d bits skipped." % (numbits)
  17.  
  18. # Fetch next byte from bitarray.
  19. # Returns an int.
  20. def fetchbyte(it):
  21.     tempstr="";
  22.     for x in range(0,8):
  23.         #print it.next()
  24.         tempstr = str(it.next())+tempstr;
  25.  
  26.     return int(tempstr, 2)
  27.  
  28. # fetch a number of bytes to extract a full packet
  29. # The second optional argument is to ignore the post-signature check, needed for the initial greeting packet
  30. def fetchpacket(it, ignorepostsig=False, packetindex=0):
  31.     tempbyte=0;tempstr = "";temparray=[];
  32.  
  33.     tempbyte=fetchbyte(it);
  34.    
  35.     # Confirm pre-signature
  36.     if(tempbyte != 0xA9):
  37.         raise TribeDecodeError("Invalid packet pre-signature! Should be 0xA9. Found: " + hex(tempbyte));
  38.    
  39.     # Confirm post-signature
  40.     for x in range(0,256):
  41.         tempbyte = fetchbyte(it);
  42.         tempstr+=pack("B", tempbyte);
  43.         temparray.append(tempbyte);
  44.  
  45.     if not ignorepostsig:
  46.         # Confirm post-signature
  47.         for x in range(0,3):
  48.             tempbyte = fetchbyte(it);
  49.             if(tempbyte != 0x55 and packetindex != 0x80):
  50.                 raise TribeDecodeError("Invalid packet post-signature! Should be 3*0x55. Found: " + hex(tempbyte));
  51.  
  52.         # Print the mysterious checksum byte
  53.         tempbyte = fetchbyte(it);
  54.         print "Packet %s has checksum (?) %s" % (hex(packetindex), hex(tempbyte));
  55.  
  56.         # Sanity check
  57.         tempbyte = fetchbyte(it);
  58.         if(tempbyte != 0xff):
  59.             raise TribeDecodeError("Sanity check. Packet should be followed by 0xff but isn't. Found: " + hex(tempbyte));
  60.  
  61.  
  62.     return tempstr;
  63.  
  64. # main function
  65. def tribefwdecode(infile, outfile, invert):
  66.     f = open(infile, "r");
  67.  
  68.     bitarray = f.readlines();  # Fetch the array of bits
  69.  
  70.     # Validate the bit array and convert it to bool
  71.     for i,b in enumerate(bitarray):
  72.         b = b.strip();
  73.         if (b != "0" and b != "1"):
  74.             raise TribeDecodeError("Only 0 and 1 in the bit file, please! Found: " + b);
  75.         else:
  76.             if invert:
  77.                 bitarray[i] = 1-int(b);
  78.             else:
  79.                 bitarray[i] = int(b);
  80.  
  81.     bititer = iter(bitarray);
  82.  
  83.     # Seek for the greeting packet.
  84.     packetseek(bititer);
  85.     fetchpacket(bititer, True)
  86.  
  87.     f = open(outfile, "w")
  88.  
  89.     packetidx = 0;
  90.  
  91.     while True:
  92.         try:
  93.             packetseek(bititer);        # Try to get a packet
  94.         except StopIteration:
  95.             break;                      # Detect end of file.
  96.        
  97.         # Parse packet and write it fo the file.
  98.         packet = fetchpacket(bititer,False,packetidx)
  99.         f.write(packet)
  100.  
  101.         packetidx += 1;
  102.  
  103.     # If we're done, close the file.  
  104.     f.close();
  105.     print "File %s successfully parsed and written to %s" % (infile, outfile)
  106.    
  107.  
  108. tribefwdecode("bits", "firmware2.bin", True)
  109.  
  110.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement