<s> import os <EOL> import sys <EOL> import struct <EOL> def blue ( msg ) : <EOL> '''<STR_LIT>''' <EOL> return "<STR_LIT>" % msg <EOL> def red ( msg ) : <EOL> '''<STR_LIT>''' <EOL> return "<STR_LIT>" % msg <EOL> def green ( msg ) : <EOL> '''<STR_LIT>''' <EOL> return "<STR_LIT>" % msg <EOL> def purple ( msg ) : <EOL> '''<STR_LIT>''' <EOL> return "<STR_LIT>" % msg <EOL> def print_error ( msg ) : <EOL> '''<STR_LIT>''' <EOL> print >> sys . stderr , msg <EOL> def ascii_char ( c ) : <EOL> '''<STR_LIT>''' <EOL> if ord ( c ) >= <NUM_LIT:32> and ord ( c ) <= <NUM_LIT> : <EOL> return c <EOL> return '<STR_LIT:.>' <EOL> def hex_dump ( data , size = <NUM_LIT:16> ) : <EOL> '''<STR_LIT>''' <EOL> def print_line ( line ) : <EOL> print "<STR_LIT>" % ( <EOL> line . encode ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" . join ( [ ascii_char ( c ) for c in line ] ) <EOL> ) <EOL> pass <EOL> for i in xrange ( <NUM_LIT:0> , len ( data ) / size ) : <EOL> data_line = data [ i * size : i * size + size ] <EOL> print_line ( data_line ) <EOL> if not len ( data ) % size == <NUM_LIT:0> : <EOL> print_line ( data [ ( len ( data ) % size ) * - <NUM_LIT:1> : ] ) <EOL> def sguid ( b , big = False ) : <EOL> '''<STR_LIT>''' <EOL> if b is None or len ( b ) != <NUM_LIT:16> : <EOL> return "<STR_LIT>" <EOL> a , b , c , d = struct . unpack ( "<STR_LIT>" % ( "<STR_LIT:>>" if big else "<STR_LIT:<>" ) , b ) <EOL> d = '<STR_LIT>' . join ( '<STR_LIT>' % ord ( c ) for c in d ) <EOL> return "<STR_LIT>" % ( a , b , c , d [ : <NUM_LIT:4> ] , d [ <NUM_LIT:4> : ] ) <EOL> def s2aguid ( s ) : <EOL> '''<STR_LIT>''' <EOL> guid = [ s [ : <NUM_LIT:8> ] , s [ <NUM_LIT:8> + <NUM_LIT:1> : <NUM_LIT:9> + <NUM_LIT:4> ] , s [ <NUM_LIT> + <NUM_LIT:1> : <NUM_LIT> + <NUM_LIT:4> ] , <EOL> s [ <NUM_LIT> + <NUM_LIT:1> : <NUM_LIT> + <NUM_LIT:4> ] + s [ - <NUM_LIT:12> : ] ] <EOL> return aguid ( "<STR_LIT>" . join ( [ part . decode ( "<STR_LIT>" ) for part in guid ] ) , True ) <EOL> def a2sguid ( a ) : <EOL> '''<STR_LIT>''' <EOL> guid = "<STR_LIT>" <EOL> for value in a : <EOL> value = format ( value , '<STR_LIT:x>' ) <EOL> guid += value . zfill ( len ( value ) + len ( value ) % <NUM_LIT:2> ) . decode ( "<STR_LIT>" ) <EOL> guid = guid . zfill ( len ( guid ) + len ( guid ) % <NUM_LIT:2> ) <EOL> return sguid ( guid , True ) <EOL> def aguid ( b , big = False ) : <EOL> '''<STR_LIT>''' <EOL> a , b , c , d = struct . unpack ( "<STR_LIT>" % ( "<STR_LIT:>>" if big else "<STR_LIT:<>" ) , b ) <EOL> return [ a , b , c ] + [ ord ( _c ) for _c in d ] <EOL> def bit_set ( field , bit ) : <EOL> '''<STR_LIT>''' <EOL> return ( field & bit == bit ) <EOL> def dump_data ( name , data ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> if os . path . dirname ( name ) is not '<STR_LIT>' : <EOL> if not os . path . exists ( os . path . dirname ( name ) ) : <EOL> os . makedirs ( os . path . dirname ( name ) ) <EOL> with open ( name , '<STR_LIT:wb>' ) as fh : <EOL> fh . write ( data ) <EOL> print "<STR_LIT>" % ( red ( name ) ) <EOL> except Exception as e : <EOL> print "<STR_LIT>" % ( name , str ( e ) ) <EOL> def search_firmware_volumes ( data , byte_align = <NUM_LIT:16> , limit = None ) : <EOL> '''<STR_LIT>''' <EOL> potential_volumes = [ ] <EOL> for aligned in xrange ( <NUM_LIT:32> , len ( data ) , byte_align ) : <EOL> if data [ aligned : aligned + <NUM_LIT:4> ] == '<STR_LIT>' : <EOL> potential_volumes . append ( aligned ) <EOL> if limit and limit == len ( potential_volumes ) : <EOL> return potential_volumes <EOL> magic = data [ ( aligned + byte_align / <NUM_LIT:2> ) : ( aligned + byte_align / <NUM_LIT:2> + <NUM_LIT:4> ) ] <EOL> if magic == '<STR_LIT>' : <EOL> potential_volumes . append ( aligned + byte_align / <NUM_LIT:2> ) <EOL> if limit and limit == len ( potential_volumes ) : <EOL> return potential_volumes <EOL> return potential_volumes <EOL> pass <EOL> def flatten_firmware_objects ( base_objects ) : <EOL> '''<STR_LIT>''' <EOL> objects = [ ] <EOL> for _object in base_objects : <EOL> objects . append ( _object ) <EOL> if "<STR_LIT>" in _object and len ( _object [ "<STR_LIT>" ] ) > <NUM_LIT:0> : <EOL> objects += flatten_firmware_objects ( _object [ "<STR_LIT>" ] ) <EOL> return objects </s>
