33import sys
44import binascii
55
6- from .evmasm import EVMAsm
6+ from .evmasm import assemble_hex , disassemble_all , instruction_table , assemble_all
77
88
99def main ():
@@ -12,41 +12,60 @@ def main():
1212 group_action .add_argument ('-a' , '--assemble' , action = 'store_true' , help = 'Assemble EVM instructions to opcodes' )
1313 group_action .add_argument ('-d' , '--disassemble' , action = 'store_true' , help = 'Disassemble EVM to opcodes' )
1414 group_action .add_argument ('-t' , '--print-opcode-table' , action = 'store_true' , help = 'List supported EVM opcodes' )
15- parser .add_argument ('-i' , '--input' , nargs = '?' , type = argparse .FileType ('r' ), default = sys .stdin ,
15+ parser .add_argument ('-bi' , '--binary-input' , action = 'store_true' , help = 'Binary input mode (-d only)' )
16+ parser .add_argument ('-bo' , '--binary-output' , action = 'store_true' , help = 'Binary output mode (-a only)' )
17+ parser .add_argument ('-i' , '--input' , nargs = '?' , default = sys .stdin , type = argparse .FileType ('r' ),
1618 help = 'Input file, default=stdin' )
17- parser .add_argument ('-o' , '--output' , nargs = '?' , type = argparse .FileType ('w' ), default = sys . stdout ,
19+ parser .add_argument ('-o' , '--output' , nargs = '?' , default = sys . stdout , type = argparse .FileType ('w' ),
1820 help = 'Output file, default=stdout' )
1921
2022 args = parser .parse_args (sys .argv [1 :])
2123
2224 if args .print_opcode_table :
23- table = EVMAsm ._get_reverse_table ()
24- for mnemonic in table .keys ():
25- # This relies on the internal format
26- (opcode , name , immediate_operand_size , pops , pushes , gas , description ) = table [mnemonic ]
27- print ("%02x: %-16s %s" % (opcode , mnemonic , description ))
25+ for instr in instruction_table :
26+ print ('0x{:02x}: {:16s} {:s}' .format (instr .opcode , instr .name , instr .description ))
2827 sys .exit (0 )
2928
3029 if args .assemble :
31- asm = args .input .read ().strip ().rstrip ()
32- args .output .write (EVMAsm .assemble_hex (asm ) + "\n " )
30+ try :
31+ asm = args .input .read ().strip ().rstrip ()
32+ except KeyboardInterrupt :
33+ sys .exit (0 )
34+ if args .binary_output :
35+ for i in assemble_all (asm ):
36+ if sys .version_info >= (3 , 2 ):
37+ args .output .buffer .write (i .bytes )
38+ else :
39+ args .output .write (i .bytes )
40+ else :
41+ args .output .write (assemble_hex (asm ) + "\n " )
3342
3443 if args .disassemble :
35- buf = args .input .read ().strip ().rstrip ()
36- if buf [:3 ] == 'EVM' : # binja prefix
37- buf = buf [3 :]
38- elif buf [:2 ] == '0x' : # hex prefixed
39- buf = binascii .unhexlify (buf [2 :])
40- else : # detect all hex buffer
41- buf_set = set ()
42- for c in buf :
43- buf_set .add (c .lower ())
44-
45- hex_set = set (list ('0123456789abcdef' ))
46- if buf_set <= hex_set : # subset
47- buf = binascii .unhexlify (buf )
48-
49- insns = list (EVMAsm .disassemble_all (buf ))
44+ if args .binary_input and sys .version_info >= (3 , 2 ):
45+ buf = args .input .buffer .read ()
46+ else :
47+ try :
48+ buf = args .input .read ().strip ().rstrip ()
49+ except KeyboardInterrupt :
50+ sys .exit (0 )
51+ except UnicodeDecodeError :
52+ print ('Input is binary? try using -b.' )
53+ sys .exit (1 )
54+
55+ if buf [:3 ] == 'EVM' : # binja prefix
56+ buf = buf [3 :]
57+ elif buf [:2 ] == '0x' : # hex prefixed
58+ buf = binascii .unhexlify (buf [2 :])
59+ else : # detect all hex buffer
60+ buf_set = set ()
61+ for c in buf :
62+ buf_set .add (c .lower ())
63+
64+ hex_set = set (list ('0123456789abcdef' ))
65+ if buf_set <= hex_set : # subset
66+ buf = binascii .unhexlify (buf )
67+
68+ insns = list (disassemble_all (buf ))
5069 for i in insns :
5170 args .output .write ("%08x: %s\n " % (i .pc , str (i )))
5271
0 commit comments