<s> from __future__ import unicode_literals <EOL> from capstone import * <EOL> from capstone . x86_const import * <EOL> from io import StringIO <EOL> class X86AsmFormatter : <EOL> SIZE_NAMES = { <EOL> <NUM_LIT:1> : '<STR_LIT>' , <EOL> <NUM_LIT:2> : '<STR_LIT>' , <EOL> <NUM_LIT:4> : '<STR_LIT>' , <EOL> <NUM_LIT:8> : '<STR_LIT>' , <EOL> } <EOL> def __init__ ( self , mainWin , mode64 = False ) : <EOL> self . mainWin = mainWin <EOL> self . cs = Cs ( CS_ARCH_X86 , CS_MODE_64 if mode64 else CS_MODE_32 ) <EOL> self . cs . detail = True <EOL> @ staticmethod <EOL> def _makeSpan ( classAttr , textFmtStr , * args ) : <EOL> return '<STR_LIT>' . format ( <EOL> classAttr , textFmtStr . format ( * args ) ) <EOL> def format ( self , codeBytes , addr ) : <EOL> fmtdInsns = [ ] <EOL> opndSeparator = self . _makeSpan ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> for insn in self . cs . disasm ( codeBytes , addr ) : <EOL> mnem = self . _makeSpan ( "<STR_LIT>" , "<STR_LIT:{}>" , insn . mnemonic ) <EOL> ops = opndSeparator . join ( <EOL> self . _formatOp ( insn , op ) for op in insn . operands ) <EOL> fmtdInsns . append ( '<STR_LIT>' . format ( mnem , ops ) ) <EOL> return '<STR_LIT:\n>' . join ( fmtdInsns ) <EOL> def _formatOp ( self , insn , op ) : <EOL> _makeSpan = X86AsmFormatter . _makeSpan <EOL> if op . type == X86_OP_REG : <EOL> return _makeSpan ( "<STR_LIT>" , "<STR_LIT:{}>" , insn . reg_name ( op . reg ) ) <EOL> elif op . type == X86_OP_IMM : <EOL> addrName = self . mainWin . getAddrName ( op . imm ) <EOL> if addrName is None : <EOL> return _makeSpan ( "<STR_LIT>" , "<STR_LIT:{}>" , self . mainWin . fmtNum ( op . imm ) ) <EOL> else : <EOL> return _makeSpan ( "<STR_LIT>" , "<STR_LIT:{}>" , addrName ) <EOL> elif op . type == X86_OP_FP : <EOL> return _makeSpan ( "<STR_LIT>" , "<STR_LIT>" , op . fp ) <EOL> elif op . type == X86_OP_MEM : <EOL> mem = op . mem <EOL> output = StringIO ( ) <EOL> output . write ( '<STR_LIT>' ) <EOL> output . write ( _makeSpan ( "<STR_LIT>" , "<STR_LIT>" , <EOL> X86AsmFormatter . SIZE_NAMES [ op . size ] ) ) <EOL> if mem . segment : <EOL> output . write ( _makeSpan ( "<STR_LIT>" , "<STR_LIT>" , <EOL> insn . reg_name ( mem . segment ) ) ) <EOL> output . write ( _makeSpan ( "<STR_LIT>" , "<STR_LIT:[>" ) ) <EOL> ptrParts = [ ] <EOL> if mem . base : <EOL> ptrParts . append ( _makeSpan ( "<STR_LIT>" , "<STR_LIT:{}>" , <EOL> insn . reg_name ( mem . base ) ) ) <EOL> if mem . index : <EOL> indexPart = _makeSpan ( "<STR_LIT>" , "<STR_LIT:{}>" , <EOL> insn . reg_name ( mem . index ) ) <EOL> if mem . scale != <NUM_LIT:1> : <EOL> indexPart += _makeSpan ( "<STR_LIT>" , "<STR_LIT:*>" ) <EOL> indexPart += _makeSpan ( "<STR_LIT>" , "<STR_LIT:{}>" , mem . scale ) <EOL> ptrParts . append ( indexPart ) <EOL> if mem . disp or not ptrParts : <EOL> dispStr = self . mainWin . fmtNum ( mem . disp ) if mem . disp else '<STR_LIT:0>' <EOL> ptrParts . append ( _makeSpan ( "<STR_LIT>" , "<STR_LIT:{}>" , dispStr ) ) <EOL> partSeparator = _makeSpan ( "<STR_LIT>" , "<STR_LIT:+>" ) <EOL> output . write ( partSeparator . join ( ptrParts ) ) <EOL> output . write ( _makeSpan ( "<STR_LIT>" , "<STR_LIT:]>" ) ) <EOL> output . write ( '<STR_LIT>' ) <EOL> return output . getvalue ( ) </s>
