Exemplo n.º 1
0
    def _unpack_words(self, fstring, handle):
        """
        Un Packs Words based on the word order and byte order

        # ---------------------------------------------- #
        # Unpack in to network ordered unsigned integer  #
        # Change Word order if little endian word order  #
        # Pack values back based on correct byte order   #
        # ---------------------------------------------- #
        :param handle: Value to be unpacked
        :return:
        """
        handle = make_byte_string(handle)
        wc = WC.get(fstring.lower()) // 2
        up = "!{}H".format(wc)
        handle = unpack(up, handle)
        if self._wordorder == Endian.Little:
            handle = list(reversed(handle))

        # Repack as unsigned Integer
        pk = self._byteorder + 'H'
        handle = [pack(pk, p) for p in handle]
        handle = b''.join(handle)
        _logger.debug(unicode_string(handle))
        return handle
Exemplo n.º 2
0
    def _unpack_words(self, fstring, handle):
        """
        Un Packs Words based on the word order and byte order

        # ---------------------------------------------- #
        # Unpack in to network ordered unsigned integer  #
        # Change Word order if little endian word order  #
        # Pack values back based on correct byte order   #
        # ---------------------------------------------- #
        :param handle: Value to be unpacked
        :return:
        """
        handle = make_byte_string(handle)
        wc = WC.get(fstring.lower())//2
        up = "!{}H".format(wc)
        handle = unpack(up, handle)
        if self._wordorder == Endian.Little:
            handle = list(reversed(handle))

        # Repack as unsigned Integer
        pk = self._byteorder + 'H'
        handle = [pack(pk, p) for p in handle]
        handle = b''.join(handle)
        _logger.debug(handle)
        return handle
Exemplo n.º 3
0
 def decode_8bit_uint(self):
     """ Decodes a 8 bit unsigned int from the buffer
     """
     self._pointer += 1
     fstring = self._byteorder + 'B'
     handle = self._payload[self._pointer - 1:self._pointer]
     handle = make_byte_string(handle)
     return unpack(fstring, handle)[0]
Exemplo n.º 4
0
    def add_string(self, value):
        """ Adds a string to the buffer

        :param value: The value to add to the buffer
        """
        value = make_byte_string(value)
        fstring = self._byteorder + str(len(value)) + 's'
        self._payload.append(pack(fstring, value))
Exemplo n.º 5
0
    def add_string(self, value):
        ''' Adds a string to the buffer

        :param value: The value to add to the buffer
        '''
        value = make_byte_string(value)
        fstring = self._endian + str(len(value)) + 's'
        self._payload.append(pack(fstring, value))
Exemplo n.º 6
0
 def decode_64bit_float(self):
     ''' Decodes a 64 bit float(double) from the buffer
     '''
     self._pointer += 8
     fstring = self._endian + 'd'
     handle = self._payload[self._pointer - 8:self._pointer]
     handle = make_byte_string(handle)
     return unpack(fstring, handle)[0]
Exemplo n.º 7
0
 def decode_32bit_float(self):
     ''' Decodes a 32 bit float from the buffer
     '''
     self._pointer += 4
     fstring = self._endian + 'f'
     handle = self._payload[self._pointer - 4:self._pointer]
     handle = make_byte_string(handle)
     return unpack(fstring, handle)[0]
Exemplo n.º 8
0
    def add_string(self, value):
        """ Adds a string to the buffer

        :param value: The value to add to the buffer
        """
        value = make_byte_string(value)
        fstring = self._byteorder + str(len(value)) + 's'
        self._payload.append(pack(fstring, value))
Exemplo n.º 9
0
 def decode_64bit_uint(self):
     ''' Decodes a 64 bit unsigned int from the buffer
     '''
     self._pointer += 8
     fstring = self._endian + 'Q'
     handle = self._payload[self._pointer - 8:self._pointer]
     handle = make_byte_string(handle)
     return unpack(fstring, handle)[0]
Exemplo n.º 10
0
    def add_string(self, value):
        ''' Adds a string to the buffer

        :param value: The value to add to the buffer
        '''
        value = make_byte_string(value)
        fstring = self._endian + str(len(value)) + 's'
        self._payload.append(pack(fstring, value))
Exemplo n.º 11
0
 def decode_64bit_float(self):
     ''' Decodes a 64 bit float(double) from the buffer
     '''
     self._pointer += 8
     fstring = self._endian + 'd'
     handle = self._payload[self._pointer - 8:self._pointer]
     handle = make_byte_string(handle)
     return unpack(fstring, handle)[0]
Exemplo n.º 12
0
 def decode_32bit_float(self):
     ''' Decodes a 32 bit float from the buffer
     '''
     self._pointer += 4
     fstring = self._endian + 'f'
     handle = self._payload[self._pointer - 4:self._pointer]
     handle = make_byte_string(handle)
     return unpack(fstring, handle)[0]
Exemplo n.º 13
0
 def decode_8bit_int(self):
     ''' Decodes a 8 bit signed int from the buffer
     '''
     self._pointer += 1
     fstring = self._endian + 'b'
     handle = self._payload[self._pointer - 1:self._pointer]
     handle = make_byte_string(handle)
     return unpack(fstring, handle)[0]
Exemplo n.º 14
0
 def decode_64bit_uint(self):
     ''' Decodes a 64 bit unsigned int from the buffer
     '''
     self._pointer += 8
     fstring = self._endian + 'Q'
     handle = self._payload[self._pointer - 8:self._pointer]
     handle = make_byte_string(handle)
     return unpack(fstring, handle)[0]
Exemplo n.º 15
0
 def decode_bits(self):
     """ Decodes a byte worth of bits from the buffer
     """
     self._pointer += 1
     # fstring = self._endian + 'B'
     handle = self._payload[self._pointer - 1:self._pointer]
     handle = make_byte_string(handle)
     return unpack_bitstring(handle)
Exemplo n.º 16
0
 def decode_16bit_int(self):
     """ Decodes a 16 bit signed int from the buffer
     """
     self._pointer += 2
     fstring = self._byteorder + 'h'
     handle = self._payload[self._pointer - 2:self._pointer]
     handle = make_byte_string(handle)
     return unpack(fstring, handle)[0]
Exemplo n.º 17
0
 def decode_8bit_int(self):
     ''' Decodes a 8 bit signed int from the buffer
     '''
     self._pointer += 1
     fstring = self._endian + 'b'
     handle = self._payload[self._pointer - 1:self._pointer]
     handle = make_byte_string(handle)
     return unpack(fstring, handle)[0]
Exemplo n.º 18
0
 def decode_16bit_int(self):
     """ Decodes a 16 bit signed int from the buffer
     """
     self._pointer += 2
     fstring = self._byteorder + 'h'
     handle = self._payload[self._pointer - 2:self._pointer]
     handle = make_byte_string(handle)
     return unpack(fstring, handle)[0]
Exemplo n.º 19
0
 def decode_8bit_uint(self):
     """ Decodes a 8 bit unsigned int from the buffer
     """
     self._pointer += 1
     fstring = self._byteorder + 'B'
     handle = self._payload[self._pointer - 1:self._pointer]
     handle = make_byte_string(handle)
     return unpack(fstring, handle)[0]
Exemplo n.º 20
0
 def decode_bits(self):
     """ Decodes a byte worth of bits from the buffer
     """
     self._pointer += 1
     # fstring = self._endian + 'B'
     handle = self._payload[self._pointer - 1:self._pointer]
     handle = make_byte_string(handle)
     return unpack_bitstring(handle)
Exemplo n.º 21
0
def append_crc(builder):
    crc = ut.computeCRC(ut.make_byte_string(builder.to_string()))
    #print(crc)
    hex_str = '%04x' % crc
    hex1 = int(hex_str[0] + hex_str[1], 16)
    hex2 = int(hex_str[2] + hex_str[3], 16)

    builder.add_8bit_uint(hex1)
    builder.add_8bit_uint(hex2)
Exemplo n.º 22
0
    def _unpack_words(self, fstring, handle):
        """
        Packs Words based on the word order
        :param handle: Value to be unpacked
        :return:
        """
        handle = make_byte_string(handle)
        if self._wordorder == Endian.Little:
            handle = [handle[i:i + 2] for i in range(0, len(handle), 2)]
            if self._byteorder == Endian.Big:
                handle = b''.join(list(reversed(handle)))
            else:
                handle = b''.join(handle)
        elif self._wordorder == Endian.Big and self._byteorder == Endian.Little:
            handle = [handle[i:i + 2] for i in range(0, len(handle), 2)]
            handle = b''.join(list(reversed(handle)))

        return handle