From 16a07a7b7a5fe7acf172c8f5eedd5d9898645e22 Mon Sep 17 00:00:00 2001 From: Jordan Wiens Date: Wed, 1 Jun 2016 04:51:14 -0400 Subject: stylized svg and start of tooltips --- python/examples/export-svg.py | 108 ++++++++++++++++++++++++++++++------------ 1 file changed, 78 insertions(+), 30 deletions(-) (limited to 'python/examples') diff --git a/python/examples/export-svg.py b/python/examples/export-svg.py index e00b651f..ad3a3dd7 100755 --- a/python/examples/export-svg.py +++ b/python/examples/export-svg.py @@ -1,16 +1,34 @@ from binaryninja import * -import os +import os,sys + +escape_table = { + "&": "&", + "'": "'", + ">": ">", + "<": "<", + '"': """, + ' ': " ", +} + +def escape(string): + return ''.join(escape_table.get(i,i) for i in string) def save_svg(bv,function): filename = bv.file.filename.split(os.sep)[-1] - outputfile = os.environ['HOME'] + os.sep + 'binaryninja-{filename}-{function}.svg'.format(filename=filename,function=function.symbol.name) - try: - output = open(outputfile,'w') - output.write(render_svg(function)) - output.close() - except: - print "Unexpected error:", sys.exc_info()[0] - raise + outputfile = os.path.join(os.path.expanduser('~'), 'binaryninja-{filename}-{function}.html'.format(filename=filename,function=function.symbol.name)) + content = render_svg(function) + output = open(outputfile,'w') + output.write(content) + output.close() + #os.system('open %s' % outputfile) + +def instruction_data_flow(function,address): + ''' TODO: Extract data flow information ''' + length = function.view.get_instruction_length(function.arch,address) + bytes = function.view.read(address, length) + hex = bytes.encode('hex') + padded = ' '.join([hex[i:i+2] for i in range(0, len(hex), 2)]) + return 'Opcode: {bytes}'.format(bytes=padded) def render_svg(function): graph = function.create_graph() @@ -19,44 +37,71 @@ def render_svg(function): ratio = 0.54 widthconst = int(heightconst*ratio) - output = ''' - - + output = ''' - + - + - + @@ -74,9 +119,9 @@ def render_svg(function): height = int((block.height) * heightconst) #Render block - output += ' \n' - output += ' Basic Block {i}\n' - output += ' \n'.format(i=i,x=x,y=y,width=width,height=height) + output += ' \n'.format(i=i) + output += ' Basic Block {i}\n'.format(i=i) + output += ' \n'.format(x=x,y=y,width=width,height=height) #Render instructions, unfortunately tspans don't allow copying/pasting more #than one line at a time, need SVG 1.2 textarea tags for that it looks like @@ -84,9 +129,12 @@ def render_svg(function): output += ' \n'.format(x=x,y=y + (i + 1) * heightconst) for i,line in enumerate(block.lines): output += ' '.format(x=x,y=y + (i + 0.7) * heightconst,address=hex(line.address)[:-1]) + hover = instruction_data_flow(function, line.address) + output += '{hover}'.format(hover=hover) for token in line.tokens: - output+='{text}'.format(text=token.text,tokentype=token.type) - output += ' \n' + # TODO: add hover for hex, function, and reg tokens + output+='{text}'.format(text=escape(token.text),tokentype=token.type) + output += '\n' output += ' \n' output += ' \n' @@ -100,7 +148,7 @@ def render_svg(function): edges += ' \n'.format(type=edge.type,points=points) output += ' ' + edges + '\n' output += ' \n' - output += '' + output += '' return output -PluginCommand.register_for_function("Export to SVG", "Exports an SVG to your home folder for the given function", save_svg) +PluginCommand.register_for_function("Export to SVG", "Exports an SVG of the current function to your home folder.", save_svg) -- cgit v1.3.1