Ejemplo n.º 1
0
    def read_ndarray(self, addr, length, dtype, shape=None, order=None, **kws):
        """

        """
        try:
            import numpy as np
        except ImportError:
            raise FeatureNotAvailableError("read_ndarray() requires Numpy.")
        offset = addr - self.start_address
        if offset < 0:
            raise InvalidAddressError("read_ndarray() access out of bounds.")
        if offset + length > self.length:
            raise InvalidAddressError("read_ndarray() access out of bounds.")
        """
        If the buffer has data that is not in machine byte-order, this should
        be specified as part of the data-type, e.g.::

          >>> dt = np.dtype(int)
          >>> dt = dt.newbyteorder('>')
          >>> np.frombuffer(buf, dtype=dt)
        """
        type_, byte_order = self._verify_dtype(dtype)
        dt = np.dtype(type_)
        dt = dt.newbyteorder(BYTEORDER.get(byte_order))
        arr = np.frombuffer(self.data[offset:offset + length],
                            dtype=dt).reshape(shape)
        if order == "F":
            return arr.T  # Fortran deposit, i.e. col-maj means transposition.
        else:
            return arr
Ejemplo n.º 2
0
 def write_numeric(self, addr, value, dtype, **kws):
     offset = addr - self.start_address
     if offset < 0:
         raise InvalidAddressError("write_numeric() access out of bounds.")
     fmt = self._getformat(dtype)
     data_size = struct.calcsize(fmt)
     if offset + data_size > self.length:
         raise InvalidAddressError("write_numeric() access out of bounds.")
     if 'bit_mask' in kws:
         bit_mask = kws.pop("bit_mask")
     self.data[offset:offset + data_size] = struct.pack(fmt, value)
Ejemplo n.º 3
0
 def read_numeric_array(self, addr, length, dtype, **kws):
     offset = addr - self.start_address
     if offset < 0:
         raise InvalidAddressError(
             "read_numeric_array() access out of bounds.")
     fmt = self._getformat(dtype, length)
     data_size = struct.calcsize(fmt)
     if offset + data_size > self.length:
         raise InvalidAddressError(
             "read_numeric_array() access out of bounds.")
     data = self.data[offset:offset + data_size]
     return struct.unpack(fmt, data)
Ejemplo n.º 4
0
 def read_numeric(self, addr, dtype, **kws):
     offset = addr - self.start_address
     if offset < 0:
         raise InvalidAddressError("read_numeric() access out of bounds.")
     fmt = self._getformat(dtype)
     data_size = struct.calcsize(fmt)
     if offset + data_size > self.length:
         raise InvalidAddressError("read_numeric() access out of bounds.")
     data = self.data[offset:offset + data_size]
     if 'bit_mask' in kws:
         bit_mask = kws.pop("bit_mask")
         data = self.apply_bitmask(data, dtype, bit_mask)
     return struct.unpack(fmt, data)[0]
Ejemplo n.º 5
0
 def write_numeric_array(self, addr, data, dtype, **kws):
     if not hasattr(data, '__iter__'):
         raise TypeError("data must be iterable")
     length = len(data)
     offset = addr - self.start_address
     if offset < 0:
         raise InvalidAddressError(
             "write_numeric_array() access out of bounds.")
     fmt = self._getformat(dtype, length)
     data_size = struct.calcsize(fmt)
     if offset + data_size > self.length:
         raise InvalidAddressError(
             "write_numeric_array() access out of bounds.")
     self.data[offset:offset + data_size] = struct.pack(fmt, *data)
Ejemplo n.º 6
0
    def read(self, addr, length, **kws):
        """
        Parameters
        ----------
        addr: int

        length: int
        """
        offset = addr - self.start_address
        if offset < 0:
            raise InvalidAddressError("read() access out of bounds.")
        if offset + length > self.length:
            raise InvalidAddressError("read() access out of bounds.")
        data = self.data[offset:offset + length]
        return data
Ejemplo n.º 7
0
    def insert_section(self, data, start_address=None, join=True):
        """Insert/add a new section to image.

        Parameters
        ----------
        data: convertible to bytearray() -- s. :func:`_data_converter`.
            Bytes making up the section.
        start_address: int
        join: bool
            Join/merge adjacent section.

        Raises
        ------
        :class:`InvalidAddressError`

        Notes
        -----
        Overlapping sections are not supported.
        To update/replace a section use :meth:`update_section`.
        """
        start_address = start_address if start_address is not None else self.address  # If Address omitted, create continuous address space.
        if self._address_contained(start_address, len(data)):
            raise InvalidAddressError("Overlapping address-space")
        if isinstance(data, str):
            data = [ord(x) for x in data]  # array.array('B',data)
        self._sections.add(Section(start_address, data))
        if join:
            self.join_sections()
        self.address = start_address + len(data)
Ejemplo n.º 8
0
 def _call_address_function(self, func_name, addr, *args, **kws):
     for section in self.sections:
         if addr in section:
             func = getattr(section, func_name)
             return func(addr, *args, **kws)
     raise InvalidAddressError(
         "Address 0x{:08x} not in range.".format(addr))
Ejemplo n.º 9
0
    def write_ndarray(self, addr, array, order=None, **kws):
        """

        """
        try:
            import numpy as np
        except ImportError:
            raise FeatureNotAvailableError("write_ndarray() requires Numpy.")
        offset = addr - self.start_address
        if offset < 0:
            raise InvalidAddressError("write_ndarray() access out of bounds.")
        if not isinstance(array, np.ndarray):
            raise TypeError("array must be of type numpy.ndarray.")
        data_size = array.nbytes
        if offset + data_size > self.length:
            raise InvalidAddressError("write_ndarray() access out of bounds.")
        self.data[offset:offset + data_size] = array.tobytes()
Ejemplo n.º 10
0
    def write(self, addr, data, **kws):
        """
        Parameters
        ----------
        addr: int

        length: int

        data: array-like
        """
        length = len(data)
        offset = addr - self.start_address
        if offset < 0:
            raise InvalidAddressError("write() access out of bounds.")
        if offset + length > self.length:
            raise InvalidAddressError("write() access out of bounds.")
        self.data[offset:offset + length] = data
Ejemplo n.º 11
0
 def write_string(self, addr, value, encoding="latin1", **kws):
     offset = addr - self.start_address
     if offset < 0:
         raise InvalidAddressError("write_string() access out of bounds.")
     if PYTHON_VERSION.major == 3:
         self.data[offset:offset + len(value)] = bytes(value,
                                                       encoding=encoding)
     else:
         self.data[offset:offset + len(value)] = bytes(value)
     self.data[offset + len(value)] = 0
Ejemplo n.º 12
0
 def read_string(self, addr, encoding="latin1", length=-1, **kws):
     offset = addr - self.start_address
     if offset < 0:
         raise InvalidAddressError("read_string() access out of bounds.")
     if length == -1:
         pos = self.data[offset:].find(b'\x00')
     else:
         pos = length
     if pos == -1:
         raise TypeError("Unterminated String!!!")  # TODO: Testcase.
     return self.data[offset:offset + pos].decode(encoding=encoding)
Ejemplo n.º 13
0
    def get_section(self, address):
        """Get :class:`Section` containing `address`.
        Parameters
        ----------
        address: int

        Returns
        -------
        :class:`Section`

        Raises
        ------
        :class:`InvalidAddressError`
        """
        if not address in self:
            raise InvalidAddressError("Address not in range")
        result = self._sections.bisect_right(SearchType(address))
        return self._sections[result - 1]
Ejemplo n.º 14
0
    def update_section(self, data, address=None):
        """

        """
        if not self._address_contained(address, len(data)):
            raise InvalidAddressError("Address-space not in range")