<s> import sys <EOL> import logging <EOL> import vstruct <EOL> import vivisect <EOL> import envi <EOL> import envi . archs . i386 as M <EOL> import sdb <EOL> from sdb import SDB_TAGS <EOL> from sdb_dump_common import SdbIndex <EOL> from sdb_dump_common import item_get_child <EOL> from sdb_dump_common import item_get_children <EOL> logging . basicConfig ( level = logging . DEBUG ) <EOL> g_logger = logging . getLogger ( "<STR_LIT>" ) <EOL> g_logger . setLevel ( logging . DEBUG ) <EOL> def disassemble ( buf , base = <NUM_LIT:0> ) : <EOL> d = M . i386Disasm ( ) <EOL> offset = <NUM_LIT:0> <EOL> while True : <EOL> if offset >= len ( buf ) : <EOL> break <EOL> o = d . disasm ( buf , offset , base ) <EOL> yield "<STR_LIT>" % ( base + offset , str ( o ) ) <EOL> offset += o . size <EOL> class GreedyVArray ( vstruct . VArray ) : <EOL> def __init__ ( self , C ) : <EOL> vstruct . VArray . __init__ ( self ) <EOL> self . _C = C <EOL> def vsParse ( self , bytez , offset = <NUM_LIT:0> , fast = False ) : <EOL> soffset = offset <EOL> while offset < len ( bytez ) : <EOL> c = self . _C ( ) <EOL> try : <EOL> offset = c . vsParse ( bytez , offset = offset , fast = False ) <EOL> except : <EOL> break <EOL> self . vsAddElement ( c ) <EOL> return offset <EOL> def vsParseFd ( self , fd ) : <EOL> raise NotImplementedError ( ) <EOL> def _main ( sdb_path , patch_name ) : <EOL> from sdb import SDB <EOL> with open ( sdb_path , "<STR_LIT:rb>" ) as f : <EOL> buf = f . read ( ) <EOL> g_logger . debug ( "<STR_LIT>" ) <EOL> s = SDB ( ) <EOL> s . vsParse ( bytearray ( buf ) ) <EOL> g_logger . debug ( "<STR_LIT>" ) <EOL> index = SdbIndex ( ) <EOL> g_logger . debug ( "<STR_LIT>" ) <EOL> index . index_sdb ( s ) <EOL> g_logger . debug ( "<STR_LIT>" ) <EOL> library = item_get_child ( s . database_root , SDB_TAGS . TAG_LIBRARY ) <EOL> for shim_ref in item_get_children ( library , SDB_TAGS . TAG_SHIM_REF ) : <EOL> patch = item_get_child ( shim_ref , SDB_TAGS . TAG_PATCH ) <EOL> name_ref = item_get_child ( patch , SDB_TAGS . TAG_NAME ) <EOL> name = index . get_string ( name_ref . value . reference ) <EOL> bits = item_get_child ( patch , SDB_TAGS . TAG_PATCH_BITS ) <EOL> if name == patch_name : <EOL> ps = GreedyVArray ( sdb . PATCHBITS ) <EOL> ps . vsParse ( bits . value . value ) <EOL> for i , _ in ps : <EOL> p = ps [ int ( i ) ] <EOL> print ( p . tree ( ) ) <EOL> print ( "<STR_LIT>" ) <EOL> for l in disassemble ( str ( p . pattern ) , p . rva ) : <EOL> print ( "<STR_LIT:U+0020>" + l ) <EOL> print ( "<STR_LIT>" ) <EOL> def main ( ) : <EOL> import sys <EOL> return sys . exit ( _main ( * sys . argv [ <NUM_LIT:1> : ] ) ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> main ( ) </s>
