Searching a file for matches between two values and outputting search hits in Python -


i (attempting) write program searches through hex file instances of hex string between 2 values, eg. between d4135b , d414ac, incrementing between first value until second reached- d4135b, d4135c, d4135d etc etc. have managed increment etc, it’s search part having trouble with. code have far, it's been cobbled other places , need make somehow output search hits output file (file_out) have exceeded limit of python understanding , i'm sure there's easier way of doing this. grateful help.

def search_process(hx):   # searching 2 binary strings     global flag while threebytehexplusone != threebytehex2: #keep incrementing until second value reached if flag:     if hx.find(threebytehex2) != -1:     flag = false #if threebytehex = threebytehexplusone, end search     print (“reached end of search”,hx.find(threebytehexplusone))     else:         if hx.find(threebytehexplusone) != -1:         flag = true     return -1 #if no results found  if __name__ == '__main__':     try:         file_in = open(file_in, "r")   #opening input file         file_out = open(file_out, 'w') #opening output file         hx_read = file_in.read  #read input file          tmp = ''         found = ''         while hx_read:   #reading file till file empty             hx_read = tmp + hx_read             pos = search_process(hx_read)              while pos != -1:                 hex_read = hx_read[pos:]                  if flag:                     found = found + hx_read                  pos = search_process(hx_read)                tmp = bytes_read[]             hx_read = file_in.read          file_out.write(found)  #writing output file      except ioerror:         print('file not found!!! check filename or directory/path') 

here's program looks through hex string file 3 bytes @ time , if 3-byte hex string between given hex bounds, writes file. makes use of generators make getting bytes hex string little cleaner.

import base64 import sys  _usage_string = 'usage: python {} <input_file> <output_file>'.format(sys.argv[0])  def _to_base_10_int(value):     return int(value, 16)  def get_bytes(hex_str):     # 2 characters equals 1 byte     in range(0, len(hex_str), 2):         yield hex_str[i:i+2]  def get_three_byte_hexes(hex_str):     bytes = get_bytes(hex_str)     while true:         try:             three_byte_hex = next(bytes) + next(bytes) + next(bytes)         except stopiteration:             break         yield three_byte_hex  def find_hexes_in_range(hex_str, lower_bound_hex, upper_bound_hex):     lower_bound = _to_base_10_int(lower_bound_hex)     upper_bound = _to_base_10_int(upper_bound_hex)     found = []     three_byte_hex in get_three_byte_hexes(hex_str):         hex_value = _to_base_10_int(three_byte_hex)         if lower_bound <= hex_value < upper_bound:             found.append(three_byte_hex)     return found  if __name__ == "__main__":     try:         assert(len(sys.argv) == 3)     except assertionerror:         print _usage_string         sys.exit(2)     file_contents = open(sys.argv[1], 'rb').read()     hex_str = base64.decodestring(file_contents).encode('hex')     found = find_hexes_in_range(hex_str, 'd4135b', 'd414ac')     print('found:')     print(found)     if found:         open(sys.argv[2], 'wb') fout:             _hex in found:                 fout.write(_hex) 

check out more info on generators here


Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -