#NEWLINE# def getPacket( self ) : #NEWLINE# #INDENT# '\n        Try to return a decrypted, authenticated, and decompressed packet\n        out of the buffer.  If there is not enough data, return None.\n\n        @rtype: C{str} or C{None}\n        @return: The decoded packet, if any.\n        ' #NEWLINE# bs = self . currentEncryptions . decBlockSize #NEWLINE# ms = self . currentEncryptions . verifyDigestSize #NEWLINE# if ( len ( self . buf ) < bs ) : #NEWLINE# #INDENT# return #NEWLINE# #UNINDENT# if ( not hasattr ( self , 'first' ) ) : #NEWLINE# #INDENT# first = self . currentEncryptions . decrypt ( self . buf [ : bs ] ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# first = self . first #NEWLINE# del self . first #NEWLINE# #UNINDENT# ( packetLen , paddingLen ) = struct . unpack ( '!LB' , first [ : 5 ] ) #NEWLINE# if ( packetLen > 1048576 ) : #NEWLINE# #INDENT# self . sendDisconnect ( DISCONNECT_PROTOCOL_ERROR , ( b'bad packet length %s' % ( packetLen , ) ) ) #NEWLINE# return #NEWLINE# #UNINDENT# if ( len ( self . buf ) < ( ( packetLen + 4 ) + ms ) ) : #NEWLINE# #INDENT# self . first = first #NEWLINE# return #NEWLINE# #UNINDENT# if ( ( ( packetLen + 4 ) % bs ) != 0 ) : #NEWLINE# #INDENT# self . sendDisconnect ( DISCONNECT_PROTOCOL_ERROR , ( b'bad packet mod (%i%%%i == %i)' % ( ( packetLen + 4 ) , bs , ( ( packetLen + 4 ) % bs ) ) ) ) #NEWLINE# return #NEWLINE# #UNINDENT# ( encData , self . buf ) = ( self . buf [ : ( 4 + packetLen ) ] , self . buf [ ( 4 + packetLen ) : ] ) #NEWLINE# packet = ( first + self . currentEncryptions . decrypt ( encData [ bs : ] ) ) #NEWLINE# if ( len ( packet ) != ( 4 + packetLen ) ) : #NEWLINE# #INDENT# self . sendDisconnect ( DISCONNECT_PROTOCOL_ERROR , b'bad decryption' ) #NEWLINE# return #NEWLINE# #UNINDENT# if ms : #NEWLINE# #INDENT# ( macData , self . buf ) = ( self . buf [ : ms ] , self . buf [ ms : ] ) #NEWLINE# if ( not self . currentEncryptions . verify ( self . incomingPacketSequence , packet , macData ) ) : #NEWLINE# #INDENT# self . sendDisconnect ( DISCONNECT_MAC_ERROR , b'bad MAC' ) #NEWLINE# return #NEWLINE# #UNINDENT# #UNINDENT# payload = packet [ 5 : ( - paddingLen ) ] #NEWLINE# if self . incomingCompression : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# payload = self . incomingCompression . decompress ( payload ) #NEWLINE# #UNINDENT# except : #NEWLINE# #INDENT# log . err ( ) #NEWLINE# self . sendDisconnect ( DISCONNECT_COMPRESSION_ERROR , b'compression error' ) #NEWLINE# return #NEWLINE# #UNINDENT# #UNINDENT# self . incomingPacketSequence += 1 #NEWLINE# return payload