<s> """<STR_LIT>""" <EOL> import array <EOL> import struct <EOL> from google . net . proto import ProtocolBuffer <EOL> _MAX_UNSIGNED_BYTE = <NUM_LIT:255> <EOL> _MAX_LONG_BYTES = <NUM_LIT:8> <EOL> _MAX_INLINE = ( _MAX_UNSIGNED_BYTE - ( <NUM_LIT:2> * _MAX_LONG_BYTES ) ) / <NUM_LIT:2> <EOL> _MIN_INLINE = - _MAX_INLINE <EOL> _OFFSET = <NUM_LIT:1> + <NUM_LIT:8> <EOL> _POS_OFFSET = _OFFSET + _MAX_INLINE * <NUM_LIT:2> <EOL> class Encoder ( ProtocolBuffer . Encoder ) : <EOL> """<STR_LIT>""" <EOL> def put16 ( self , value ) : <EOL> if value < <NUM_LIT:0> or value >= ( <NUM_LIT:1> << <NUM_LIT:16> ) : <EOL> raise ProtocolBuffer . ProtocolBufferEncodeError , '<STR_LIT>' <EOL> self . buf . append ( ( value >> <NUM_LIT:8> ) & <NUM_LIT> ) <EOL> self . buf . append ( ( value >> <NUM_LIT:0> ) & <NUM_LIT> ) <EOL> return <EOL> def put32 ( self , value ) : <EOL> if value < <NUM_LIT:0> or value >= ( <NUM_LIT:1> L << <NUM_LIT:32> ) : <EOL> raise ProtocolBuffer . ProtocolBufferEncodeError , '<STR_LIT>' <EOL> self . buf . append ( ( value >> <NUM_LIT> ) & <NUM_LIT> ) <EOL> self . buf . append ( ( value >> <NUM_LIT:16> ) & <NUM_LIT> ) <EOL> self . buf . append ( ( value >> <NUM_LIT:8> ) & <NUM_LIT> ) <EOL> self . buf . append ( ( value >> <NUM_LIT:0> ) & <NUM_LIT> ) <EOL> return <EOL> def put64 ( self , value ) : <EOL> if value < <NUM_LIT:0> or value >= ( <NUM_LIT:1> L << <NUM_LIT:64> ) : <EOL> raise ProtocolBuffer . ProtocolBufferEncodeError , '<STR_LIT>' <EOL> self . buf . append ( ( value >> <NUM_LIT> ) & <NUM_LIT> ) <EOL> self . buf . append ( ( value >> <NUM_LIT> ) & <NUM_LIT> ) <EOL> self . buf . append ( ( value >> <NUM_LIT> ) & <NUM_LIT> ) <EOL> self . buf . append ( ( value >> <NUM_LIT:32> ) & <NUM_LIT> ) <EOL> self . buf . append ( ( value >> <NUM_LIT> ) & <NUM_LIT> ) <EOL> self . buf . append ( ( value >> <NUM_LIT:16> ) & <NUM_LIT> ) <EOL> self . buf . append ( ( value >> <NUM_LIT:8> ) & <NUM_LIT> ) <EOL> self . buf . append ( ( value >> <NUM_LIT:0> ) & <NUM_LIT> ) <EOL> return <EOL> def _PutVarInt ( self , value ) : <EOL> if value is None : <EOL> self . buf . append ( <NUM_LIT:0> ) <EOL> return <EOL> if value >= _MIN_INLINE and value <= _MAX_INLINE : <EOL> value = _OFFSET + ( value - _MIN_INLINE ) <EOL> self . buf . append ( value & <NUM_LIT> ) <EOL> return <EOL> negative = False <EOL> if value < <NUM_LIT:0> : <EOL> value = _MIN_INLINE - value <EOL> negative = True <EOL> else : <EOL> value = value - _MAX_INLINE <EOL> len = <NUM_LIT:0> <EOL> w = value <EOL> while w > <NUM_LIT:0> : <EOL> w >>= <NUM_LIT:8> <EOL> len += <NUM_LIT:1> <EOL> if negative : <EOL> head = _OFFSET - len <EOL> else : <EOL> head = _POS_OFFSET + len <EOL> self . buf . append ( head & <NUM_LIT> ) <EOL> for i in range ( len - <NUM_LIT:1> , - <NUM_LIT:1> , - <NUM_LIT:1> ) : <EOL> b = value >> ( i * <NUM_LIT:8> ) <EOL> if negative : <EOL> b = _MAX_UNSIGNED_BYTE - ( b & <NUM_LIT> ) <EOL> self . buf . append ( b & <NUM_LIT> ) <EOL> def putVarInt32 ( self , value ) : <EOL> if value >= <NUM_LIT> or value < - <NUM_LIT> : <EOL> raise ProtocolBuffer . ProtocolBufferEncodeError , '<STR_LIT>' <EOL> self . _PutVarInt ( value ) <EOL> def putVarInt64 ( self , value ) : <EOL> if value >= <NUM_LIT> or value < - <NUM_LIT> : <EOL> raise ProtocolBuffer . ProtocolBufferEncodeError , '<STR_LIT>' <EOL> self . _PutVarInt ( value ) <EOL> def putVarUint64 ( self , value ) : <EOL> if value < <NUM_LIT:0> or value >= <NUM_LIT> : <EOL> raise ProtocolBuffer . ProtocolBufferEncodeError , '<STR_LIT>' <EOL> self . _PutVarInt ( value ) <EOL> def _isFloatNegative ( self , value , encoded ) : <EOL> if value == <NUM_LIT:0> : <EOL> return encoded [ <NUM_LIT:0> ] == <NUM_LIT> <EOL> return value < <NUM_LIT:0> <EOL> def putFloat ( self , value ) : <EOL> encoded = array . array ( '<STR_LIT:B>' ) <EOL> encoded . fromstring ( struct . pack ( '<STR_LIT>' , value ) ) <EOL> if self . _isFloatNegative ( value , encoded ) : <EOL> encoded [ <NUM_LIT:0> ] ^= <NUM_LIT> <EOL> encoded [ <NUM_LIT:1> ] ^= <NUM_LIT> <EOL> encoded [ <NUM_LIT:2> ] ^= <NUM_LIT> <EOL> encoded [ <NUM_LIT:3> ] ^= <NUM_LIT> <EOL> else : <EOL> encoded [ <NUM_LIT:0> ] ^= <NUM_LIT> <EOL> self . buf . extend ( encoded ) <EOL> def putDouble ( self , value ) : <EOL> encoded = array . array ( '<STR_LIT:B>' ) <EOL> encoded . fromstring ( struct . pack ( '<STR_LIT>' , value ) ) <EOL> if self . _isFloatNegative ( value , encoded ) : <EOL> encoded [ <NUM_LIT:0> ] ^= <NUM_LIT> <EOL> encoded [ <NUM_LIT:1> ] ^= <NUM_LIT> <EOL> encoded [ <NUM_LIT:2> ] ^= <NUM_LIT> <EOL> encoded [ <NUM_LIT:3> ] ^= <NUM_LIT> <EOL> encoded [ <NUM_LIT:4> ] ^= <NUM_LIT> <EOL> encoded [ <NUM_LIT:5> ] ^= <NUM_LIT> <EOL> encoded [ <NUM_LIT:6> ] ^= <NUM_LIT> <EOL> encoded [ <NUM_LIT:7> ] ^= <NUM_LIT> <EOL> else : <EOL> encoded [ <NUM_LIT:0> ] ^= <NUM_LIT> <EOL> self . buf . extend ( encoded ) <EOL> def putPrefixedString ( self , value ) : <EOL> self . buf . fromstring ( <EOL> value . replace ( '<STR_LIT>' , '<STR_LIT>' ) . replace ( '<STR_LIT:\x00>' , '<STR_LIT>' ) + '<STR_LIT:\x00>' ) <EOL> class Decoder ( ProtocolBuffer . Decoder ) : <EOL> def __init__ ( self , buf , idx = <NUM_LIT:0> , limit = None ) : <EOL> if not limit : <EOL> limit = len ( buf ) <EOL> ProtocolBuffer . Decoder . __init__ ( self , buf , idx , limit ) <EOL> def get16 ( self ) : <EOL> if self . idx + <NUM_LIT:2> > self . limit : <EOL> raise ProtocolBuffer . ProtocolBufferDecodeError , '<STR_LIT>' <EOL> c = self . buf [ self . idx ] <EOL> d = self . buf [ self . idx + <NUM_LIT:1> ] <EOL> self . idx += <NUM_LIT:2> <EOL> return ( c << <NUM_LIT:8> ) | d <EOL> def get32 ( self ) : <EOL> if self . idx + <NUM_LIT:4> > self . limit : <EOL> raise ProtocolBuffer . ProtocolBufferDecodeError , '<STR_LIT>' <EOL> c = long ( self . buf [ self . idx ] ) <EOL> d = self . buf [ self . idx + <NUM_LIT:1> ] <EOL> e = self . buf [ self . idx + <NUM_LIT:2> ] <EOL> f = self . buf [ self . idx + <NUM_LIT:3> ] <EOL> self . idx += <NUM_LIT:4> <EOL> return ( c << <NUM_LIT> ) | ( d << <NUM_LIT:16> ) | ( e << <NUM_LIT:8> ) | f <EOL> def get64 ( self ) : <EOL> if self . idx + <NUM_LIT:8> > self . limit : <EOL> raise ProtocolBuffer . ProtocolBufferDecodeError , '<STR_LIT>' <EOL> c = long ( self . buf [ self . idx ] ) <EOL> d = long ( self . buf [ self . idx + <NUM_LIT:1> ] ) <EOL> e = long ( self . buf [ self . idx + <NUM_LIT:2> ] ) <EOL> f = long ( self . buf [ self . idx + <NUM_LIT:3> ] ) <EOL> g = long ( self . buf [ self . idx + <NUM_LIT:4> ] ) <EOL> h = self . buf [ self . idx + <NUM_LIT:5> ] <EOL> i = self . buf [ self . idx + <NUM_LIT:6> ] <EOL> j = self . buf [ self . idx + <NUM_LIT:7> ] <EOL> self . idx += <NUM_LIT:8> <EOL> return ( ( c << <NUM_LIT> ) | ( d << <NUM_LIT> ) | ( e << <NUM_LIT> ) | ( f << <NUM_LIT:32> ) | ( g << <NUM_LIT> ) <EOL> | ( h << <NUM_LIT:16> ) | ( i << <NUM_LIT:8> ) | j ) <EOL> def getVarInt64 ( self ) : <EOL> b = self . get8 ( ) <EOL> if b >= _OFFSET and b <= _POS_OFFSET : <EOL> return b - _OFFSET + _MIN_INLINE <EOL> if b == <NUM_LIT:0> : <EOL> return None <EOL> if b < _OFFSET : <EOL> negative = True <EOL> bytes = _OFFSET - b <EOL> else : <EOL> negative = False <EOL> bytes = b - _POS_OFFSET <EOL> ret = <NUM_LIT:0> <EOL> for _ in range ( bytes ) : <EOL> b = self . get8 ( ) <EOL> if negative : <EOL> b = _MAX_UNSIGNED_BYTE - b <EOL> ret = ret << <NUM_LIT:8> | b <EOL> if negative : <EOL> return _MIN_INLINE - ret <EOL> else : <EOL> return ret + _MAX_INLINE <EOL> def getVarInt32 ( self ) : <EOL> result = self . getVarInt64 ( ) <EOL> if result >= <NUM_LIT> L or result < - <NUM_LIT> L : <EOL> raise ProtocolBuffer . ProtocolBufferDecodeError , '<STR_LIT>' <EOL> return result <EOL> def getVarUint64 ( self ) : <EOL> result = self . getVarInt64 ( ) <EOL> if result < <NUM_LIT:0> : <EOL> raise ProtocolBuffer . ProtocolBufferDecodeError , '<STR_LIT>' <EOL> return result <EOL> def getFloat ( self ) : <EOL> if self . idx + <NUM_LIT:4> > self . limit : <EOL> raise ProtocolBuffer . ProtocolBufferDecodeError , '<STR_LIT>' <EOL> a = self . buf [ self . idx : self . idx + <NUM_LIT:4> ] <EOL> self . idx += <NUM_LIT:4> <EOL> if a [ <NUM_LIT:0> ] & <NUM_LIT> : <EOL> a [ <NUM_LIT:0> ] ^= <NUM_LIT> <EOL> else : <EOL> a = [ x ^ <NUM_LIT> for x in a ] <EOL> return struct . unpack ( '<STR_LIT>' , array . array ( '<STR_LIT:B>' , a ) . tostring ( ) ) [ <NUM_LIT:0> ] <EOL> def getDouble ( self ) : <EOL> if self . idx + <NUM_LIT:8> > self . limit : <EOL> raise ProtocolBuffer . ProtocolBufferDecodeError , '<STR_LIT>' <EOL> a = self . buf [ self . idx : self . idx + <NUM_LIT:8> ] <EOL> self . idx += <NUM_LIT:8> <EOL> if a [ <NUM_LIT:0> ] & <NUM_LIT> : <EOL> a [ <NUM_LIT:0> ] ^= <NUM_LIT> <EOL> else : <EOL> a = [ x ^ <NUM_LIT> for x in a ] <EOL> return struct . unpack ( '<STR_LIT>' , array . array ( '<STR_LIT:B>' , a ) . tostring ( ) ) [ <NUM_LIT:0> ] <EOL> def getPrefixedString ( self ) : <EOL> end_idx = self . idx <EOL> while self . buf [ end_idx ] != <NUM_LIT:0> : <EOL> end_idx += <NUM_LIT:1> <EOL> data = array . array ( '<STR_LIT:B>' , self . buf [ self . idx : end_idx ] ) . tostring ( ) <EOL> self . idx = end_idx + <NUM_LIT:1> <EOL> return data . replace ( '<STR_LIT>' , '<STR_LIT:\x00>' ) . replace ( '<STR_LIT>' , '<STR_LIT>' ) </s>
