<s> import struct <EOL> import base64 <EOL> class TruncatedError ( Exception ) : <EOL> pass <EOL> class CorruptedError ( Exception ) : <EOL> pass <EOL> class BitPackedBuffer : <EOL> def __init__ ( self , contents , endian = '<STR_LIT>' ) : <EOL> self . _data = contents or [ ] <EOL> self . _used = <NUM_LIT:0> <EOL> self . _next = None <EOL> self . _nextbits = <NUM_LIT:0> <EOL> self . _bigendian = ( endian == '<STR_LIT>' ) <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' % ( <EOL> self . _nextbits and self . _next or <NUM_LIT:0> , self . _nextbits , <EOL> self . _used , '<STR_LIT>' % ( ord ( self . _data [ self . _used ] ) , ) if ( self . _used < len ( self . _data ) ) else '<STR_LIT>' ) <EOL> def copy ( self , other ) : <EOL> self . _data = other . _data <EOL> self . _used = other . _used <EOL> self . _next = other . _next <EOL> self . _nextbits = other . _nextbits <EOL> self . _bigendian = other . _bigendian <EOL> def peek_bytes_as_hex_string ( self , bytes = <NUM_LIT:0> ) : <EOL> if bytes == <NUM_LIT:0> : <EOL> bytes = len ( self . _data ) <EOL> bpb = BitPackedBuffer ( [ ] , self . _bigendian ) <EOL> bpb . copy ( self ) <EOL> return '<STR_LIT>' . join ( '<STR_LIT>' . format ( ord ( x ) ) for x in bpb . read_unaligned_bytes ( bytes ) ) <EOL> def peek_bytes_as_bin_string ( self , bytes = <NUM_LIT:0> ) : <EOL> if bytes == <NUM_LIT:0> : <EOL> bytes = len ( self . _data ) <EOL> bpb = BitPackedBuffer ( [ ] , self . _bigendian ) <EOL> bpb . copy ( self ) <EOL> return '<STR_LIT>' . join ( '<STR_LIT>' . format ( ord ( x ) ) for x in bpb . read_unaligned_bytes ( bytes ) ) <EOL> def peek_bits_as_bin_string ( self , bits = <NUM_LIT:0> ) : <EOL> if bits == <NUM_LIT:0> : <EOL> bits = len ( self . _data ) * <NUM_LIT:8> <EOL> bpb = BitPackedBuffer ( [ ] , self . _bigendian ) <EOL> bpb . copy ( self ) <EOL> return ( '<STR_LIT>' % bits ) . format ( bpb . read_bits ( bits ) ) <EOL> def done ( self ) : <EOL> return self . _nextbits == <NUM_LIT:0> and self . _used >= len ( self . _data ) <EOL> def used_bits ( self ) : <EOL> return self . _used * <NUM_LIT:8> - self . _nextbits <EOL> def byte_align ( self ) : <EOL> self . _nextbits = <NUM_LIT:0> <EOL> def read_aligned_bytes ( self , bytes ) : <EOL> self . byte_align ( ) <EOL> data = self . _data [ self . _used : self . _used + bytes ] <EOL> self . _used += bytes <EOL> if len ( data ) != bytes : <EOL> raise TruncatedError ( self ) <EOL> return data <EOL> def state ( self ) : <EOL> return '<STR_LIT>' % ( self . _next , self . _nextbits , self . _used ) <EOL> def read_bits ( self , bits ) : <EOL> result = <NUM_LIT:0> <EOL> resultbits = <NUM_LIT:0> <EOL> while resultbits != bits : <EOL> if self . _nextbits == <NUM_LIT:0> : <EOL> if self . done ( ) : <EOL> raise TruncatedError ( self ) <EOL> self . _next = ord ( self . _data [ self . _used ] ) <EOL> self . _used += <NUM_LIT:1> <EOL> self . _nextbits = <NUM_LIT:8> <EOL> copybits = min ( bits - resultbits , self . _nextbits ) <EOL> copy = ( self . _next & ( ( <NUM_LIT:1> << copybits ) - <NUM_LIT:1> ) ) <EOL> if self . _bigendian : <EOL> result |= copy << ( bits - resultbits - copybits ) <EOL> else : <EOL> result |= copy << resultbits <EOL> self . _next >>= copybits <EOL> self . _nextbits -= copybits <EOL> resultbits += copybits <EOL> return result <EOL> def read_unaligned_bytes ( self , bytes ) : <EOL> return '<STR_LIT>' . join ( [ chr ( self . read_bits ( <NUM_LIT:8> ) ) for i in xrange ( bytes ) ] ) <EOL> class BitPackedDecoder : <EOL> def __init__ ( self , contents , typeinfos ) : <EOL> self . _buffer = BitPackedBuffer ( contents ) <EOL> self . _typeinfos = typeinfos <EOL> def __str__ ( self ) : <EOL> return self . _buffer . __str__ ( ) <EOL> def instance ( self , typeid ) : <EOL> if typeid >= len ( self . _typeinfos ) : <EOL> raise CorruptedError ( self ) <EOL> typeinfo = self . _typeinfos [ typeid ] <EOL> return getattr ( self , typeinfo [ <NUM_LIT:0> ] ) ( * typeinfo [ <NUM_LIT:1> ] ) <EOL> def byte_align ( self ) : <EOL> self . _buffer . byte_align ( ) <EOL> def done ( self ) : <EOL> return self . _buffer . done ( ) <EOL> def used_bits ( self ) : <EOL> return self . _buffer . used_bits ( ) <EOL> def _array ( self , bounds , typeid ) : <EOL> length = self . _int ( bounds ) <EOL> return [ self . instance ( typeid ) for i in xrange ( length ) ] <EOL> def _bitarray ( self , bounds ) : <EOL> length = self . _int ( bounds ) <EOL> return ( length , self . _buffer . read_bits ( length ) ) <EOL> def _blob ( self , bounds ) : <EOL> length = self . _int ( bounds ) <EOL> result = self . _buffer . read_aligned_bytes ( length ) <EOL> try : <EOL> result = { '<STR_LIT:utf8>' : result . decode ( '<STR_LIT:utf-8>' , '<STR_LIT:strict>' ) } <EOL> except UnicodeDecodeError : <EOL> result = { <EOL> '<STR_LIT:utf8>' : result . decode ( '<STR_LIT:utf-8>' , '<STR_LIT:replace>' ) , <EOL> '<STR_LIT>' : base64 . b64encode ( result ) <EOL> } <EOL> return result <EOL> def _bool ( self ) : <EOL> return self . _int ( ( <NUM_LIT:0> , <NUM_LIT:1> ) ) != <NUM_LIT:0> <EOL> def _choice ( self , bounds , fields ) : <EOL> tag = self . _int ( bounds ) <EOL> if tag not in fields : <EOL> raise CorruptedError ( self ) <EOL> field = fields [ tag ] <EOL> return { field [ <NUM_LIT:0> ] : self . instance ( field [ <NUM_LIT:1> ] ) } <EOL> def _fourcc ( self ) : <EOL> return self . _buffer . read_unaligned_bytes ( <NUM_LIT:4> ) <EOL> def _int ( self , bounds ) : <EOL> return bounds [ <NUM_LIT:0> ] + self . _buffer . read_bits ( bounds [ <NUM_LIT:1> ] ) <EOL> def _null ( self ) : <EOL> return None <EOL> def _optional ( self , typeid ) : <EOL> exists = self . _bool ( ) <EOL> return self . instance ( typeid ) if exists else None <EOL> def _real32 ( self ) : <EOL> return struct . unpack ( '<STR_LIT>' , self . _buffer . read_unaligned_bytes ( <NUM_LIT:4> ) ) <EOL> def _real64 ( self ) : <EOL> return struct . unpack ( '<STR_LIT>' , self . _buffer . read_unaligned_bytes ( <NUM_LIT:8> ) ) <EOL> def _struct ( self , fields ) : <EOL> result = { } <EOL> for field in fields : <EOL> if field [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> parent = self . instance ( field [ <NUM_LIT:1> ] ) <EOL> if isinstance ( parent , dict ) : <EOL> result . update ( parent ) <EOL> elif len ( fields ) == <NUM_LIT:1> : <EOL> result = parent <EOL> else : <EOL> result [ field [ <NUM_LIT:0> ] ] = parent <EOL> else : <EOL> result [ field [ <NUM_LIT:0> ] ] = self . instance ( field [ <NUM_LIT:1> ] ) <EOL> return result <EOL> class BitPackedDecoderDebug : <EOL> def __init__ ( self , contents , typeinfos ) : <EOL> self . _buffer = BitPackedBuffer ( contents ) <EOL> self . _typeinfos = typeinfos <EOL> self . _markers = [ ] <EOL> self . _json = { } <EOL> def __str__ ( self ) : <EOL> return self . _buffer . __str__ ( ) <EOL> def peek_bytes_as_hex_string ( self , bytes ) : <EOL> return self . _buffer . peek_bytes_as_hex_string ( bytes ) <EOL> def peek_bytes_as_bin_string ( self , bytes = <NUM_LIT:0> ) : <EOL> return self . _buffer . peek_bytes_as_bin_string ( bytes ) <EOL> def space_binary_string_by_markers ( self , bin_string , first_bit_index ) : <EOL> retval = '<STR_LIT>' <EOL> x = <NUM_LIT:0> <EOL> while x < len ( bin_string ) : <EOL> for m in self . _markers : <EOL> if m [ '<STR_LIT>' ] == ( first_bit_index + x ) : <EOL> retval = retval + '<STR_LIT:{>' + m [ '<STR_LIT:type>' ] + '<STR_LIT:}>' <EOL> retval = retval + bin_string [ x ] <EOL> x += <NUM_LIT:1> <EOL> for m in self . _markers : <EOL> if m [ '<STR_LIT>' ] == ( first_bit_index + x ) : <EOL> retval = retval + '<STR_LIT:{>' + m [ '<STR_LIT:type>' ] + '<STR_LIT:}>' <EOL> return retval <EOL> def get_json_and_reset ( self ) : <EOL> retval = self . _json <EOL> self . _json = { } <EOL> return retval <EOL> def instance ( self , typeid ) : <EOL> used_bits = self . _buffer . used_bits ( ) <EOL> self . _markers . append ( { '<STR_LIT>' : self . used_bits ( ) , '<STR_LIT:type>' : '<STR_LIT>' % typeid } ) <EOL> old_json = self . _json <EOL> self . _json = { '<STR_LIT>' : self . used_bits ( ) , '<STR_LIT>' : typeid } <EOL> if typeid >= len ( self . _typeinfos ) : <EOL> return { "<STR_LIT>" : "<STR_LIT>" % ( typeid , len ( self . _typeinfos ) ) , "<STR_LIT>" : hex_string } <EOL> typeinfo = self . _typeinfos [ typeid ] <EOL> retval = getattr ( self , typeinfo [ <NUM_LIT:0> ] ) ( * typeinfo [ <NUM_LIT:1> ] ) <EOL> self . _json [ '<STR_LIT>' ] = self . used_bits ( ) <EOL> old_json [ '<STR_LIT>' % self . used_bits ( ) ] = self . _json <EOL> self . _json = old_json <EOL> self . _markers . append ( { '<STR_LIT>' : self . used_bits ( ) , '<STR_LIT:type>' : '<STR_LIT>' % typeid } ) <EOL> return retval <EOL> def byte_align ( self ) : <EOL> self . _buffer . byte_align ( ) <EOL> def done ( self ) : <EOL> return self . _buffer . done ( ) <EOL> def used_bits ( self ) : <EOL> return self . _buffer . used_bits ( ) <EOL> def _array ( self , bounds , typeid ) : <EOL> self . _markers . append ( { '<STR_LIT>' : self . used_bits ( ) , '<STR_LIT:type>' : '<STR_LIT>' % ( str ( bounds ) , str ( typeid ) ) } ) <EOL> old_json = self . _json <EOL> self . _json = { '<STR_LIT>' : self . used_bits ( ) , '<STR_LIT>' : bounds , '<STR_LIT>' : typeid } <EOL> length = self . _int ( bounds ) <EOL> self . _json [ '<STR_LIT>' ] = length <EOL> retval = [ self . instance ( typeid ) for i in xrange ( length ) ] <EOL> old_json [ '<STR_LIT>' % self . used_bits ( ) ] = self . _json <EOL> self . _json = old_json <EOL> return retval <EOL> def _bitarray ( self , bounds ) : <EOL> self . _markers . append ( { '<STR_LIT>' : self . used_bits ( ) , '<STR_LIT:type>' : '<STR_LIT>' % str ( bounds ) } ) <EOL> old_json = self . _json <EOL> self . _json = { '<STR_LIT>' : self . used_bits ( ) , '<STR_LIT>' : bounds } <EOL> length = self . _int ( bounds ) <EOL> self . _json [ '<STR_LIT>' ] = self . _buffer . peek_bits_as_bin_string ( length ) <EOL> retval = ( length , self . _buffer . read_bits ( length ) ) <EOL> old_json [ '<STR_LIT>' % self . used_bits ( ) ] = self . _json <EOL> self . _json = old_json <EOL> return retval <EOL> def _blob ( self , bounds ) : <EOL> self . _markers . append ( { '<STR_LIT>' : self . used_bits ( ) , '<STR_LIT:type>' : '<STR_LIT>' % str ( bounds ) } ) <EOL> old_json = self . _json <EOL> self . _json = { '<STR_LIT>' : self . used_bits ( ) , '<STR_LIT>' : bounds } <EOL> length = self . _int ( bounds ) <EOL> self . _json [ '<STR_LIT>' ] = length <EOL> retval = self . _buffer . read_aligned_bytes ( length ) <EOL> self . _json [ '<STR_LIT>' ] = '<STR_LIT>' . join ( '<STR_LIT>' . format ( ord ( x ) ) for x in retval ) <EOL> old_json [ '<STR_LIT>' % self . used_bits ( ) ] = self . _json <EOL> self . _json = old_json <EOL> try : <EOL> retval = { '<STR_LIT:utf8>' : retval . decode ( '<STR_LIT:utf-8>' , '<STR_LIT:strict>' ) } <EOL> except UnicodeDecodeError : <EOL> retval = { <EOL> '<STR_LIT:utf8>' : retval . decode ( '<STR_LIT:utf-8>' , '<STR_LIT:replace>' ) , <EOL> '<STR_LIT>' : base64 . b64encode ( retval ) <EOL> } <EOL> return retval <EOL> def _bool ( self ) : <EOL> old_json = self . _json <EOL> self . _json = { '<STR_LIT>' : self . used_bits ( ) } <EOL> self . _markers . append ( { '<STR_LIT>' : self . used_bits ( ) , '<STR_LIT:type>' : '<STR_LIT:bool>' } ) <EOL> retval = self . _int ( ( <NUM_LIT:0> , <NUM_LIT:1> ) ) != <NUM_LIT:0> <EOL> self . _json [ '<STR_LIT:value>' ] = retval <EOL> old_json [ '<STR_LIT>' % self . used_bits ( ) ] = self . _json <EOL> self . _json = old_json <EOL> return retval <EOL> def _choice ( self , bounds , fields ) : <EOL> self . _markers . append ( { '<STR_LIT>' : self . used_bits ( ) , '<STR_LIT:type>' : '<STR_LIT>' % ( str ( bounds ) , str ( fields ) ) } ) <EOL> old_json = self . _json <EOL> self . _json = { '<STR_LIT>' : self . used_bits ( ) , '<STR_LIT>' : bounds , '<STR_LIT>' : fields } <EOL> tag = self . _int ( bounds ) <EOL> if tag not in fields : <EOL> return { "<STR_LIT>" : "<STR_LIT>" % ( str ( tag ) , str ( fields ) ) } <EOL> field = fields [ tag ] <EOL> retval = { field [ <NUM_LIT:0> ] : self . instance ( field [ <NUM_LIT:1> ] ) } <EOL> self . _json [ '<STR_LIT:value>' ] = retval <EOL> old_json [ '<STR_LIT>' % self . used_bits ( ) ] = self . _json <EOL> self . _json = old_json <EOL> return retval <EOL> def _fourcc ( self ) : <EOL> old_json = self . _json <EOL> self . _json = { '<STR_LIT>' : self . used_bits ( ) } <EOL> self . _markers . append ( { '<STR_LIT>' : self . used_bits ( ) , '<STR_LIT:type>' : '<STR_LIT>' } ) <EOL> retval = self . _buffer . read_unaligned_bytes ( <NUM_LIT:4> ) <EOL> old_json [ '<STR_LIT>' % self . used_bits ( ) ] = self . _json <EOL> self . _json = old_json <EOL> return retval <EOL> def _int ( self , bounds ) : <EOL> old_json = self . _json <EOL> self . _json = { '<STR_LIT>' : self . used_bits ( ) , '<STR_LIT>' : bounds , '<STR_LIT>' : self . _buffer . peek_bits_as_bin_string ( bounds [ <NUM_LIT:1> ] ) } <EOL> bitpos = self . used_bits ( ) <EOL> retval = bounds [ <NUM_LIT:0> ] + self . _buffer . read_bits ( bounds [ <NUM_LIT:1> ] ) <EOL> self . _markers . append ( { '<STR_LIT>' : bitpos , '<STR_LIT:type>' : '<STR_LIT>' % ( str ( bounds ) , retval , self . _buffer . state ( ) ) } ) <EOL> self . _json [ '<STR_LIT:value>' ] = retval <EOL> old_json [ '<STR_LIT>' % self . used_bits ( ) ] = self . _json <EOL> self . _json = old_json <EOL> return retval <EOL> def _null ( self ) : <EOL> self . _markers . append ( { '<STR_LIT>' : self . used_bits ( ) , '<STR_LIT:type>' : '<STR_LIT:null>' } ) <EOL> return None <EOL> def _optional ( self , typeid ) : <EOL> old_json = self . _json <EOL> self . _json = { '<STR_LIT>' : self . used_bits ( ) , '<STR_LIT>' : typeid } <EOL> self . _markers . append ( { '<STR_LIT>' : self . used_bits ( ) , '<STR_LIT:type>' : '<STR_LIT>' % str ( typeid ) } ) <EOL> exists = self . _bool ( ) <EOL> retval = self . instance ( typeid ) if exists else None <EOL> old_json [ '<STR_LIT>' % self . used_bits ( ) ] = self . _json <EOL> self . _json = old_json <EOL> return retval <EOL> def _real32 ( self ) : <EOL> old_json = self . _json <EOL> self . _json = { '<STR_LIT>' : self . used_bits ( ) } <EOL> self . _markers . append ( { '<STR_LIT>' : self . used_bits ( ) , '<STR_LIT:type>' : '<STR_LIT>' } ) <EOL> retval = struct . unpack ( '<STR_LIT>' , self . _buffer . read_unaligned_bytes ( <NUM_LIT:4> ) ) <EOL> self . _json [ '<STR_LIT:value>' ] = retval <EOL> old_json [ '<STR_LIT>' % self . used_bits ( ) ] = self . _json <EOL> self . _json = old_json <EOL> return retval <EOL> def _real64 ( self ) : <EOL> old_json = self . _json <EOL> self . _json = { '<STR_LIT>' : self . used_bits ( ) } <EOL> self . _markers . append ( { '<STR_LIT>' : self . used_bits ( ) , '<STR_LIT:type>' : '<STR_LIT>' } ) <EOL> retval = struct . unpack ( '<STR_LIT>' , self . _buffer . read_unaligned_bytes ( <NUM_LIT:8> ) ) <EOL> self . _json [ '<STR_LIT:value>' ] = retval <EOL> old_json [ '<STR_LIT>' % self . used_bits ( ) ] = self . _json <EOL> self . _json = old_json <EOL> return retval <EOL> def _struct ( self , fields ) : <EOL> old_json = self . _json <EOL> self . _json = { '<STR_LIT>' : self . used_bits ( ) , '<STR_LIT>' : fields } <EOL> self . _markers . append ( { '<STR_LIT>' : self . used_bits ( ) , '<STR_LIT:type>' : '<STR_LIT>' % str ( fields ) } ) <EOL> result = { } <EOL> for field in fields : <EOL> if field [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> parent = self . instance ( field [ <NUM_LIT:1> ] ) <EOL> if isinstance ( parent , dict ) : <EOL> result . update ( parent ) <EOL> elif len ( fields ) == <NUM_LIT:1> : <EOL> result = parent <EOL> else : <EOL> result [ field [ <NUM_LIT:0> ] ] = parent <EOL> else : <EOL> result [ field [ <NUM_LIT:0> ] ] = self . instance ( field [ <NUM_LIT:1> ] ) <EOL> old_json [ '<STR_LIT>' % self . used_bits ( ) ] = self . _json <EOL> self . _json = old_json <EOL> return result <EOL> class VersionedDecoder : <EOL> def __init__ ( self , contents , typeinfos ) : <EOL> self . _buffer = BitPackedBuffer ( contents ) <EOL> self . _typeinfos = typeinfos <EOL> def __str__ ( self ) : <EOL> return self . _buffer . __str__ ( ) <EOL> def instance ( self , typeid ) : <EOL> if typeid >= len ( self . _typeinfos ) : <EOL> raise CorruptedError ( self ) <EOL> typeinfo = self . _typeinfos [ typeid ] <EOL> return getattr ( self , typeinfo [ <NUM_LIT:0> ] ) ( * typeinfo [ <NUM_LIT:1> ] ) <EOL> def byte_align ( self ) : <EOL> self . _buffer . byte_align ( ) <EOL> def done ( self ) : <EOL> return self . _buffer . done ( ) <EOL> def used_bits ( self ) : <EOL> return self . _buffer . used_bits ( ) <EOL> def _expect_skip ( self , expected ) : <EOL> if self . _buffer . read_bits ( <NUM_LIT:8> ) != expected : <EOL> raise CorruptedError ( self ) <EOL> def _vint ( self ) : <EOL> b = self . _buffer . read_bits ( <NUM_LIT:8> ) <EOL> negative = b & <NUM_LIT:1> <EOL> result = ( b >> <NUM_LIT:1> ) & <NUM_LIT> <EOL> bits = <NUM_LIT:6> <EOL> while ( b & <NUM_LIT> ) != <NUM_LIT:0> : <EOL> b = self . _buffer . read_bits ( <NUM_LIT:8> ) <EOL> result |= ( b & <NUM_LIT> ) << bits <EOL> bits += <NUM_LIT:7> <EOL> return - result if negative else result <EOL> def _array ( self , bounds , typeid ) : <EOL> self . _expect_skip ( <NUM_LIT:0> ) <EOL> length = self . _vint ( ) <EOL> return [ self . instance ( typeid ) for i in xrange ( length ) ] <EOL> def _bitarray ( self , bounds ) : <EOL> self . _expect_skip ( <NUM_LIT:1> ) <EOL> length = self . _vint ( ) <EOL> return ( length , self . _buffer . read_aligned_bytes ( ( length + <NUM_LIT:7> ) / <NUM_LIT:8> ) ) <EOL> def _blob ( self , bounds ) : <EOL> self . _expect_skip ( <NUM_LIT:2> ) <EOL> length = self . _vint ( ) <EOL> result = self . _buffer . read_aligned_bytes ( length ) <EOL> try : <EOL> result = { '<STR_LIT:utf8>' : result . decode ( '<STR_LIT:utf-8>' , '<STR_LIT:strict>' ) } <EOL> except UnicodeDecodeError : <EOL> result = { <EOL> '<STR_LIT:utf8>' : result . decode ( '<STR_LIT:utf-8>' , '<STR_LIT:replace>' ) , <EOL> '<STR_LIT>' : base64 . b64encode ( result ) <EOL> } <EOL> return result <EOL> def _bool ( self ) : <EOL> self . _expect_skip ( <NUM_LIT:6> ) <EOL> return self . _buffer . read_bits ( <NUM_LIT:8> ) != <NUM_LIT:0> <EOL> def _choice ( self , bounds , fields ) : <EOL> self . _expect_skip ( <NUM_LIT:3> ) <EOL> tag = self . _vint ( ) <EOL> if tag not in fields : <EOL> self . _skip_instance ( ) <EOL> return { } <EOL> field = fields [ tag ] <EOL> return { field [ <NUM_LIT:0> ] : self . instance ( field [ <NUM_LIT:1> ] ) } <EOL> def _fourcc ( self ) : <EOL> self . _expect_skip ( <NUM_LIT:7> ) <EOL> return self . _buffer . read_aligned_bytes ( <NUM_LIT:4> ) <EOL> def _int ( self , bounds ) : <EOL> self . _expect_skip ( <NUM_LIT:9> ) <EOL> return self . _vint ( ) <EOL> def _null ( self ) : <EOL> return None <EOL> def _optional ( self , typeid ) : <EOL> self . _expect_skip ( <NUM_LIT:4> ) <EOL> exists = self . _buffer . read_bits ( <NUM_LIT:8> ) != <NUM_LIT:0> <EOL> return self . instance ( typeid ) if exists else None <EOL> def _real32 ( self ) : <EOL> self . _expect_skip ( <NUM_LIT:7> ) <EOL> return struct . unpack ( '<STR_LIT>' , self . _buffer . read_aligned_bytes ( <NUM_LIT:4> ) ) <EOL> def _real64 ( self ) : <EOL> self . _expect_skip ( <NUM_LIT:8> ) <EOL> return struct . unpack ( '<STR_LIT>' , self . _buffer . read_aligned_bytes ( <NUM_LIT:8> ) ) <EOL> def _struct ( self , fields ) : <EOL> self . _expect_skip ( <NUM_LIT:5> ) <EOL> result = { } <EOL> length = self . _vint ( ) <EOL> for i in xrange ( length ) : <EOL> tag = self . _vint ( ) <EOL> field = next ( ( f for f in fields if f [ <NUM_LIT:2> ] == tag ) , None ) <EOL> if field : <EOL> if field [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> parent = self . instance ( field [ <NUM_LIT:1> ] ) <EOL> if isinstance ( parent , dict ) : <EOL> result . update ( parent ) <EOL> elif len ( fields ) == <NUM_LIT:1> : <EOL> result = parent <EOL> else : <EOL> result [ field [ <NUM_LIT:0> ] ] = parent <EOL> else : <EOL> result [ field [ <NUM_LIT:0> ] ] = self . instance ( field [ <NUM_LIT:1> ] ) <EOL> else : <EOL> self . _skip_instance ( ) <EOL> return result <EOL> def _skip_instance ( self ) : <EOL> skip = self . _buffer . read_bits ( <NUM_LIT:8> ) <EOL> if skip == <NUM_LIT:0> : <EOL> length = self . _vint ( ) <EOL> for i in xrange ( length ) : <EOL> self . _skip_instance ( ) <EOL> elif skip == <NUM_LIT:1> : <EOL> length = self . _vint ( ) <EOL> self . _buffer . read_aligned_bytes ( ( length + <NUM_LIT:7> ) / <NUM_LIT:8> ) <EOL> elif skip == <NUM_LIT:2> : <EOL> length = self . _vint ( ) <EOL> self . _buffer . read_aligned_bytes ( length ) <EOL> elif skip == <NUM_LIT:3> : <EOL> tag = self . _vint ( ) <EOL> self . _skip_instance ( ) <EOL> elif skip == <NUM_LIT:4> : <EOL> exists = self . _buffer . read_bits ( <NUM_LIT:8> ) != <NUM_LIT:0> <EOL> if exists : <EOL> self . _skip_instance ( ) <EOL> elif skip == <NUM_LIT:5> : <EOL> length = self . _vint ( ) <EOL> for i in xrange ( length ) : <EOL> tag = self . _vint ( ) <EOL> self . _skip_instance ( ) <EOL> elif skip == <NUM_LIT:6> : <EOL> self . _buffer . read_aligned_bytes ( <NUM_LIT:1> ) <EOL> elif skip == <NUM_LIT:7> : <EOL> self . _buffer . read_aligned_bytes ( <NUM_LIT:4> ) <EOL> elif skip == <NUM_LIT:8> : <EOL> self . _buffer . read_aligned_bytes ( <NUM_LIT:8> ) <EOL> elif skip == <NUM_LIT:9> : <EOL> self . _vint ( ) </s>
