(self)
return True
Decapsulate the data structure

        Unpacks the CRC32 checksum and the file name out of the header. Then,
        validates the data integrity using the checksum.

        Returns:
            Boolean indicating whether the parsing was successful
Decapsulate the data structure
Decapsulate the data structure
def decapsulate(self):
        """Decapsulate the data structure

        Unpacks the CRC32 checksum and the file name out of the header. Then,
        validates the data integrity using the checksum.

        Returns:
            Boolean indicating whether the parsing was successful

        """
        assert (self.data['encapsulated'] is not None)
        encap_data = self.data['encapsulated']

        if (len(encap_data) < MSG_HEADER_LEN):
            logger.error("Trying to decapsulate a non-encapsulated message")
            return False

        # Parse the header
        header = struct.unpack(MSG_HEADER_FORMAT, encap_data[:MSG_HEADER_LEN])
        try:
            self.filename = header[0].rstrip(b'\0').decode()
        except UnicodeDecodeError:
            logger.error("Could not parse file name. This is likely a "
                         "non-encapsulated message.")
            return False
        in_checksum = header[1]

        # Check the integrity of the payload
        payload = encap_data[MSG_HEADER_LEN:]
        calc_checksum = zlib.crc32(payload)

        if (calc_checksum != in_checksum):
            logger.error("Checksum (%d) does not match the header value (%d)" %
                         (calc_checksum, in_checksum))
            return False
        else:
            logger.info("File: %s\tChecksum: %d\tSize: %d bytes" %
                        (self.filename, in_checksum, len(payload)))

        self.data['original'] = payload
        return True
def decapsulate ( self ) : assert ( self . data [ 'encapsulated' ] is not None ) encap_data = self . data [ 'encapsulated' ] if ( len ( encap_data ) < MSG_HEADER_LEN ) : logger . error ( "Trying to decapsulate a non-encapsulated message" ) return False # Parse the header header = struct . unpack ( MSG_HEADER_FORMAT , encap_data [ : MSG_HEADER_LEN ] ) try : self . filename = header [ 0 ] . rstrip ( b'\0' ) . decode ( ) except UnicodeDecodeError : logger . error ( "Could not parse file name. This is likely a " "non-encapsulated message." ) return False in_checksum = header [ 1 ] # Check the integrity of the payload payload = encap_data [ MSG_HEADER_LEN : ] calc_checksum = zlib . crc32 ( payload ) if ( calc_checksum != in_checksum ) : logger . error ( "Checksum (%d) does not match the header value (%d)" % ( calc_checksum , in_checksum ) ) return False else : logger . info ( "File: %s\tChecksum: %d\tSize: %d bytes" % ( self . filename , in_checksum , len ( payload ) ) ) self . data [ 'original' ] = payload return True