<s> import sys <EOL> import struct <EOL> import logging <EOL> import datetime <EOL> import binascii <EOL> import six <EOL> g_logger = logging . getLogger ( "<STR_LIT>" ) <EOL> def dosdate ( dosdate , dostime ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> t = ord ( dosdate [ <NUM_LIT:1> ] ) << <NUM_LIT:8> <EOL> t |= ord ( dosdate [ <NUM_LIT:0> ] ) <EOL> day = t & <NUM_LIT> <EOL> month = ( t & <NUM_LIT> ) >> <NUM_LIT:5> <EOL> year = ( t & <NUM_LIT> ) >> <NUM_LIT:9> <EOL> year += <NUM_LIT> <EOL> t = ord ( dostime [ <NUM_LIT:1> ] ) << <NUM_LIT:8> <EOL> t |= ord ( dostime [ <NUM_LIT:0> ] ) <EOL> sec = t & <NUM_LIT> <EOL> sec *= <NUM_LIT:2> <EOL> minute = ( t & <NUM_LIT> ) >> <NUM_LIT:5> <EOL> hour = ( t & <NUM_LIT> ) >> <NUM_LIT:11> <EOL> return datetime . datetime ( year , month , day , hour , minute , sec ) <EOL> except : <EOL> return datetime . datetime . min <EOL> def align ( offset , alignment ) : <EOL> """<STR_LIT>""" <EOL> if offset % alignment == <NUM_LIT:0> : <EOL> return offset <EOL> return offset + ( alignment - ( offset % alignment ) ) <EOL> class ParseException ( Exception ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , value ) : <EOL> """<STR_LIT>""" <EOL> super ( ParseException , self ) . __init__ ( value ) <EOL> def __str__ ( self ) : <EOL> return str ( unicode ( self ) ) <EOL> def __unicode__ ( self ) : <EOL> return u"<STR_LIT>" % ( self . _value ) <EOL> class OverrunBufferException ( ParseException ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , readOffs , bufLen ) : <EOL> tvalue = "<STR_LIT>" % ( hex ( readOffs ) , hex ( bufLen ) ) <EOL> super ( ParseException , self ) . __init__ ( tvalue ) <EOL> def __str__ ( self ) : <EOL> return str ( unicode ( self ) ) <EOL> def __unicode__ ( self ) : <EOL> return u"<STR_LIT>" % ( self . _value ) <EOL> class Block ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , buf , offset , parent ) : <EOL> """<STR_LIT>""" <EOL> self . _buf = buf <EOL> self . _offset = offset <EOL> self . _parent = parent <EOL> def __unicode__ ( self ) : <EOL> return u"<STR_LIT>" % ( hex ( self . offset ( ) ) ) <EOL> def __str__ ( self ) : <EOL> return str ( unicode ( self ) ) <EOL> def _prepare_fields ( self , fields = False ) : <EOL> """<STR_LIT>""" <EOL> for field in fields : <EOL> def handler ( ) : <EOL> f = getattr ( self , "<STR_LIT>" + field [ <NUM_LIT:0> ] ) <EOL> return f ( * ( field [ <NUM_LIT:2> : ] ) ) <EOL> setattr ( self , field [ <NUM_LIT:1> ] , handler ) <EOL> debug_payload = handler ( ) <EOL> if isinstance ( debug_payload , six . text_type ) : <EOL> debug_payload = debug_payload . encode ( '<STR_LIT:utf8>' ) <EOL> else : <EOL> debug_payload = str ( debug_payload ) <EOL> debug_payload = binascii . hexlify ( debug_payload ) <EOL> g_logger . debug ( "<STR_LIT>" % ( field [ <NUM_LIT:0> ] . upper ( ) , <EOL> field [ <NUM_LIT:1> ] , <EOL> hex ( self . absolute_offset ( field [ <NUM_LIT:2> ] ) ) , <EOL> debug_payload ) ) <EOL> setattr ( self , "<STR_LIT>" + field [ <NUM_LIT:1> ] , field [ <NUM_LIT:2> ] ) <EOL> def declare_field ( self , type , name , offset , length = False ) : <EOL> """<STR_LIT>""" <EOL> if length : <EOL> self . _prepare_fields ( [ ( type , name , offset , length ) ] ) <EOL> else : <EOL> self . _prepare_fields ( [ ( type , name , offset ) ] ) <EOL> def unpack_byte ( self , offset ) : <EOL> """<STR_LIT>""" <EOL> o = self . _offset + offset <EOL> try : <EOL> return struct . unpack_from ( "<STR_LIT>" , self . _buf , o ) [ <NUM_LIT:0> ] <EOL> except struct . error : <EOL> raise OverrunBufferException ( o , len ( self . _buf ) ) <EOL> def unpack_word ( self , offset ) : <EOL> """<STR_LIT>""" <EOL> o = self . _offset + offset <EOL> try : <EOL> return struct . unpack_from ( "<STR_LIT>" , self . _buf , o ) [ <NUM_LIT:0> ] <EOL> except struct . error : <EOL> raise OverrunBufferException ( o , len ( self . _buf ) ) <EOL> def pack_word ( self , offset , word ) : <EOL> """<STR_LIT>""" <EOL> o = self . _offset + offset <EOL> return struct . pack_into ( "<STR_LIT>" , self . _buf , o , word ) <EOL> def unpack_dword ( self , offset ) : <EOL> """<STR_LIT>""" <EOL> o = self . _offset + offset <EOL> try : <EOL> return struct . unpack_from ( "<STR_LIT>" , self . _buf , o ) [ <NUM_LIT:0> ] <EOL> except struct . error : <EOL> raise OverrunBufferException ( o , len ( self . _buf ) ) <EOL> def unpack_int ( self , offset ) : <EOL> """<STR_LIT>""" <EOL> o = self . _offset + offset <EOL> try : <EOL> return struct . unpack_from ( "<STR_LIT>" , self . _buf , o ) [ <NUM_LIT:0> ] <EOL> except struct . error : <EOL> raise OverrunBufferException ( o , len ( self . _buf ) ) <EOL> def unpack_qword ( self , offset ) : <EOL> """<STR_LIT>""" <EOL> o = self . _offset + offset <EOL> try : <EOL> return struct . unpack_from ( "<STR_LIT>" , self . _buf , o ) [ <NUM_LIT:0> ] <EOL> except struct . error : <EOL> raise OverrunBufferException ( o , len ( self . _buf ) ) <EOL> def unpack_string ( self , offset , length = False ) : <EOL> """<STR_LIT>""" <EOL> o = self . _offset + offset <EOL> if not length : <EOL> end = self . _buf . find ( "<STR_LIT:\x00>" , o ) <EOL> length = end - o <EOL> try : <EOL> return struct . unpack_from ( "<STR_LIT>" % ( length ) , self . _buf , o ) [ <NUM_LIT:0> ] . partition ( "<STR_LIT:\x00>" ) [ <NUM_LIT:0> ] <EOL> except struct . error : <EOL> raise OverrunBufferException ( o , len ( self . _buf ) ) <EOL> def unpack_wstring ( self , offset , ilength = False ) : <EOL> """<STR_LIT>""" <EOL> raw_bytes = self . _buf [ self . _offset + offset : ] <EOL> if ilength : <EOL> raw_bytes = raw_bytes [ : ilength ] <EOL> if len ( raw_bytes ) % <NUM_LIT:2> : <EOL> raw_bytes = raw_bytes [ : - <NUM_LIT:1> ] <EOL> try : <EOL> index = [ raw_bytes [ i : i + <NUM_LIT:2> ] for i in range ( <NUM_LIT:0> , len ( raw_bytes ) , <NUM_LIT:2> ) ] . index ( '<STR_LIT>' ) <EOL> raw_bytes = raw_bytes [ : index * <NUM_LIT:2> ] <EOL> except ValueError : <EOL> pass <EOL> return raw_bytes . decode ( "<STR_LIT>" ) <EOL> def unpack_dosdate ( self , offset ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> o = self . _offset + offset <EOL> return dosdate ( self . _buf [ o : o + <NUM_LIT:2> ] , self . _buf [ o + <NUM_LIT:2> : o + <NUM_LIT:4> ] ) <EOL> except struct . error : <EOL> raise OverrunBufferException ( o , len ( self . _buf ) ) <EOL> def unpack_guid ( self , offset ) : <EOL> """<STR_LIT>""" <EOL> o = self . _offset + offset <EOL> try : <EOL> _bin = self . _buf [ o : o + <NUM_LIT:16> ] <EOL> except IndexError : <EOL> raise OverrunBufferException ( o , len ( self . _buf ) ) <EOL> h = map ( ord , _bin ) <EOL> return "<STR_LIT>" % ( h [ <NUM_LIT:3> ] , h [ <NUM_LIT:2> ] , h [ <NUM_LIT:1> ] , h [ <NUM_LIT:0> ] , <EOL> h [ <NUM_LIT:5> ] , h [ <NUM_LIT:4> ] , <EOL> h [ <NUM_LIT:7> ] , h [ <NUM_LIT:6> ] , <EOL> h [ <NUM_LIT:8> ] , h [ <NUM_LIT:9> ] , <EOL> h [ <NUM_LIT:10> ] , h [ <NUM_LIT:11> ] , h [ <NUM_LIT:12> ] , h [ <NUM_LIT> ] , h [ <NUM_LIT> ] , h [ <NUM_LIT:15> ] ) <EOL> def absolute_offset ( self , offset ) : <EOL> """<STR_LIT>""" <EOL> return self . _offset + offset <EOL> def parent ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _parent <EOL> def offset ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _offset </s>
