From 2fab2a64830643920443b2afefb210f34dcf6bfa Mon Sep 17 00:00:00 2001 From: Jordan Wiens Date: Thu, 8 Jun 2023 13:08:19 -0400 Subject: replace format with fstrings, fix opacity bug, better enable raw svg --- python/examples/export_svg.py | 138 ++++++++++++++++++------------------------ 1 file changed, 60 insertions(+), 78 deletions(-) (limited to 'python') diff --git a/python/examples/export_svg.py b/python/examples/export_svg.py index 62fc1045..d0885524 100755 --- a/python/examples/export_svg.py +++ b/python/examples/export_svg.py @@ -90,7 +90,7 @@ def instruction_data_flow(function, address): func_bytes = function.view.read(address, length) hex = func_bytes.hex() padded = ' '.join([hex[i:i + 2] for i in range(0, len(hex), 2)]) - return 'Opcode: {bytes}'.format(bytes=padded) + return f'Opcode: {padded}' def render_svg(function, offset, mode, form, showOpcodes, showAddresses, origname): @@ -125,88 +125,84 @@ def render_svg(function, offset, mode, form, showOpcodes, showAddresses, orignam ratio = 0.48 widthconst = heightconst * ratio - output = ''' - + output = f''' + - - -''' - output += ''' @@ -221,8 +217,7 @@ def render_svg(function, offset, mode, form, showOpcodes, showAddresses, orignam - '''.format(width=graph.width * widthconst + 20, height=graph.height * heightconst + 20) - output += ''' + Function Graph 0 ''' edges = '' @@ -235,8 +230,8 @@ def render_svg(function, offset, mode, form, showOpcodes, showAddresses, orignam height = ((block.height) * heightconst) # Render block - output += ' \n'.format(i=i) - output += ' Basic Block {i}\n'.format(i=i) + output += f' \n' + output += f' Basic Block {i}\n' rgb = colors['none'] try: bb = block.basic_block @@ -249,25 +244,19 @@ def render_svg(function, offset, mode, form, showOpcodes, showAddresses, orignam rgb = [bb.highlight.red, bb.highlight.green, bb.highlight.blue] except: pass - output += ' \n'.format( - x=x, y=y, width=width + 16, height=height + 12, r=rgb[0], g=rgb[1], b=rgb[2] - ) + output += f' \n' # 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 - output += ' \n'.format(x=x, y=y + (i+1) * heightconst) + output += f' \n' for i, line in enumerate(block.lines): - output += ' '.format( - x=x + 6, y=y + 6 + (i+0.7) * heightconst, address=hex(line.address)[:-1] - ) + output += f' ' hover = instruction_data_flow(function, line.address) - output += '{hover}'.format(hover=hover) + output += f'{hover}' for token in line.tokens: # TODO: add hover for hex, function, and reg tokens - output += '{text}'.format( - text=escape(token.text), tokentype=InstructionTextTokenType(token.type).name - ) + output += f'{escape(token.text)}' output += '\n' output += ' \n' output += ' \n' @@ -278,29 +267,22 @@ def render_svg(function, offset, mode, form, showOpcodes, showAddresses, orignam for edge in block.outgoing_edges: points = "" x, y = edge.points[0] - points += str(x * widthconst) + "," + \ - str(y * heightconst + 12) + " " + points += str(x * widthconst) + "," + str(y * heightconst + 12) + " " for x, y in edge.points[1:-1]: - points += str(x * widthconst) + "," + \ - str(y * heightconst) + " " + points += str(x * widthconst) + "," + str(y * heightconst) + " " x, y = edge.points[-1] - points += str(x * widthconst) + "," + \ - str(y * heightconst + 0) + " " + points += str(x * widthconst) + "," + str(y * heightconst + 0) + " " + edgeType=BranchType(edge.type).name if edge.back_edge: - edges += ' \n'.format( - type=BranchType(edge.type).name, points=points - ) + edges += f' \n' else: - edges += ' \n'.format( - type=BranchType(edge.type).name, points=points - ) + edges += f' \n' output += ' ' + edges + '\n' output += ' \n' - output += '' + output += '\n' - output += '

This CFG generated by Binary Ninja from {filename} on {timestring} showing {function} as {form}.

'.format( - filename=origname, timestring=time.strftime("%c"), function=offset, form=form - ) + timestring=time.strftime("%c") + output += f'

This CFG generated by Binary Ninja from {origname} on {timestring} showing {offset} as {form}.

' output += '' return output -- cgit v1.3.1