import sys import re import bz2 def binary_to_int(string): x = len(string) number = 0 for i in range(x): number += ord(string[i])*256**i return number def binary_to_int_backwards(string): x = len(string) number = 0 for i in range(x): number += ord(string[x-i-1])*256**i return number def main(): argv = sys.argv argc = len(argv) if argc != 2: print "Error! Correct usage is:\n\trch_ana.py " source_file = argv[1] rch_file = open(source_file, 'rb') rch_bin = rch_file.read() rch_file.close() if not re.match("FR01", rch_bin[0:4]): print "error. not a valid rch file." return 0 #header info version = binary_to_int(rch_bin[0x4:0x8]) timestamp = binary_to_int(rch_bin[0x8:0xc]) #end header info #chunk info chunk_offset = 0x1c while 1: file_name = rch_bin[chunk_offset+0x2:chunk_offset+0x29].rstrip("\x00") chunk_size = binary_to_int(rch_bin[chunk_offset+0x32:chunk_offset+0x35]) crc = binary_to_int(rch_bin[chunk_offset+0x36:chunk_offset+0x39]) algorithm = binary_to_int(rch_bin[chunk_offset+0x3A:chunk_offset+0x3D]) chunk_data = rch_bin[chunk_offset+0x3E:chunk_offset +0x3E + chunk_size] print "filename:", file_name print "chunk_size:", chunk_size print "actual chunk size:", len(chunk_data) print "crc:",crc print "algorithm:", algorithm,"\n" # next we actually do the decompressing if algorithm == 1: # "Here we just XOR each byte with 0xFF" new_bin = "" new_file = open(file_name, 'wb') for i in range(chunk_size): next_char = chunk_data[i] next_char = chr(0xff^binary_to_int(next_char)) new_bin += next_char new_file.write(new_bin) new_file.close() if algorithm == 2: new_bin = "" new_file = open(file_name, 'wb') # "We must use bzip2 algorithm" new_bin = "" new_bin = bz2.BZ2Decompressor().decompress(chunk_data) new_file.write(new_bin) new_file.close() if len(rch_bin) > chunk_offset +0x3E + chunk_size: chunk_offset = chunk_offset +0x3E + chunk_size else: if len(rch_bin) != chunk_offset + 0x3E + chunk_size: print "file size error" break if __name__ == "__main__": main()