Ejemplo n.º 1
0
def test_read_ndarray_flat():
    sec = Section(start_address=0x1000, data=bytearray(32))
    arr = np.array([[11, 22, 33], [44, 55, 66]], dtype="int32")
    sec.write_ndarray(0x1000, arr)

    result = sec.read_ndarray(0x1000, 24, "int32_le")
    assert np.array_equal(result, np.array([11, 22, 33, 44, 55, 66]))
Ejemplo n.º 2
0
def test_copy_really_works():
    section1 = Section(data=range(16), start_address=0x8000)
    section2 = Section(data=section1)
    assert section2.start_address == 0
    assert section2.length == 16
    assert section2.data == b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f'
    assert section2.data == section1.data
    section2.write_numeric(0, 0xff, "uint8_le")
    assert section2.data == b'\xff\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f'
    assert section1.data != section2.data
Ejemplo n.º 3
0
 def load(self, fp, address=0x0000):
     data = fp.read()
     sec = Section(address, data)
     img = Image([sec], valid=True)
     if hasattr(fp, "close"):
         fp.close()
     return img
Ejemplo n.º 4
0
 def addSegment(
     self,
     data,
     address=None,
     dontJoin=False
 ):  # TODO: 'polymorph' signature, move 'dontJoin' to segment!
     address = address if address else self.address  # If Address omitted, create continuous address space.
     if isinstance(data, str):
         data = [ord(x) for x in data]  # array.array('B',data)
     if self.autoSort:
         bisect.insort(self._sections, Section(address, data))
     else:
         self._sections.append(Section(address, data))
     if self.autoJoin:
         self.joinSections()
     self.address = address + len(data)
Ejemplo n.º 5
0
    def insert_section(self, data, start_address=None, dont_join=False):
        """Insert/add a new section to image.

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

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

        Notes
        -----
        Overlapping sections are not supported.
        To relace 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 self._join:
            self.join_sections()
        self.address = start_address + len(data)
Ejemplo n.º 6
0
 def parseLine(self, line, match):
     section = Section(
         self.address,
         bytearray([
             int(ch, 16)
             for ch in filter(lambda x: x, self.SPLITTER.split(line))
         ]))
     self.sections.append(section)
     self.address += len(section)
     return True
Ejemplo n.º 7
0
 def read(self, fp):
     sections = []
     matched = False
     self.valid = True
     metaData = defaultdict(list)
     for (lineNumber, line) in enumerate(fp.readlines(), 1):
         for formatType, format in self.formats:
             if isinstance(line, bytes):
                 match = format.match(line.decode())
             else:
                 match = format.match(line)
             if match:
                 matched = True
                 container = Container()
                 container.lineNumber = lineNumber
                 dict_ = match.groupdict()
                 if dict_ != {}:
                     # Handle scalar values.
                     for key, value in dict_.items():
                         if key not in ('chunk', 'junk'):
                             setattr(container, key, atoi(value))
                         elif key == 'junk':
                             setattr(container, key, value)
                     if 'chunk' in dict_:
                         if self.parseData(container, formatType):
                             chunk = bytearray(
                                 map(atoi, BYTES.findall(dict_['chunk'])))
                         else:
                             # don't convert/parse stuff like symbols.
                             chunk = dict_['chunk']
                         dict_.pop('chunk')
                         setattr(container, 'chunk', chunk)
                     self.checkLine(container, formatType)
                     # this is to handle esoteric stuff like Intel seg:offs addressing and symbols.
                     self.specialProcessing(container, formatType)
                     if self.isDataLine(container, formatType):
                         # print chunk
                         sections.append(
                             Section(container.address, container.chunk))
                     else:
                         chunk = container.chunk if hasattr(
                             container, 'chunk') else None
                         address = container.address if hasattr(
                             container, 'address') else None
                         metaData[formatType].append(
                             MetaRecord(formatType, address, chunk))
                 break
         if not matched:
             self.warn("Ignoring garbage line #{0:d}".format(lineNumber))
     if sections:
         return Image(joinSections(sections), metaData, self.valid)
     else:
         self.error("File seems to be invalid.")
         return Image([], valid=False)
Ejemplo n.º 8
0
def test_read_write_uint64_array():
    img = Image(Section(data=bytearray(128), start_address=0x1000))
    img.write_numeric_array(0x1000, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
                            "uint64_be")
    assert img[0].length == 128
    assert img.read_numeric_array(0x1000, 10, "uint64_be") == (1, 2, 3, 4, 5,
                                                               6, 7, 8, 9, 10)
    img.write_numeric_array(0x1000, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
                            "uint64_le")
    assert img[0].length == 128
    assert img.read_numeric_array(0x1000, 10, "uint64_le") == (1, 2, 3, 4, 5,
                                                               6, 7, 8, 9, 10)
Ejemplo n.º 9
0
def test_read_write_int64_array():
    img = Image(Section(data=bytearray(128), start_address=0x1000))
    img.write_numeric_array(0x1000, [-1, -2, -3, -4, -5, -6, -7, -8, -9, -10],
                            "int64_be")
    assert img[0].length == 128
    assert img.read_numeric_array(0x1000, 10,
                                  "int64_be") == (-1, -2, -3, -4, -5, -6, -7,
                                                  -8, -9, -10)
    img.write_numeric_array(0x1000, [-1, -2, -3, -4, -5, -6, -7, -8, -9, -10],
                            "int64_le")
    assert img[0].length == 128
    assert img.read_numeric_array(0x1000, 10,
                                  "int64_le") == (-1, -2, -3, -4, -5, -6, -7,
                                                  -8, -9, -10)
Ejemplo n.º 10
0
def test_read_write_float64_array():
    img = Image(Section(data=bytearray(128), start_address=0x1000))
    img.write_numeric_array(
        0x1000, [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0],
        "float64_be")
    assert img[0].length == 128
    assert img.read_numeric_array(0x1000, 10,
                                  "float64_be") == (1.0, 2.0, 3.0, 4.0, 5.0,
                                                    6.0, 7.0, 8.0, 9.0, 10.0)
    img.write_numeric_array(
        0x1000, [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0],
        "float64_le")
    assert img[0].length == 128
    assert img.read_numeric_array(0x1000, 10,
                                  "float64_le") == (1.0, 2.0, 3.0, 4.0, 5.0,
                                                    6.0, 7.0, 8.0, 9.0, 10.0)
Ejemplo n.º 11
0
def test_read_write_datatypes_require_suffix():
    img = Image(Section(data=bytearray(32), start_address=0x1000))
    with pytest.raises(TypeError):
        img.read_numeric(0x1000, "uint8")
    with pytest.raises(TypeError):
        img.read_numeric(0x1000, "int8")
    with pytest.raises(TypeError):
        img.read_numeric(0x1000, "uint16")
    with pytest.raises(TypeError):
        img.read_numeric(0x1000, "int16")
    with pytest.raises(TypeError):
        img.read_numeric(0x1000, "uint32")
    with pytest.raises(TypeError):
        img.read_numeric(0x1000, "int32")
    with pytest.raises(TypeError):
        img.read_numeric(0x1000, "uint64")
    with pytest.raises(TypeError):
        img.read_numeric(0x1000, "int64")
    with pytest.raises(TypeError):
        img.read_numeric(0x1000, "float32")
    with pytest.raises(TypeError):
        img.read_numeric(0x1000, "float64")
Ejemplo n.º 12
0
def test_single_int_not_permitted():
    with pytest.raises(ValueError):
        Section(data=42)
Ejemplo n.º 13
0
def test_read_uint32_negative_offset():
    img = Image(Section(data=bytearray(10), start_address=0x1000))
    with pytest.raises(InvalidAddressError):
        img.read_numeric(0x0fff, "uint32_le")
Ejemplo n.º 14
0
def test_copy_data_from_other_section():
    data = Section(data="abcd", start_address=0x8000)
    section = Section(data=data)
    assert section.start_address == 0
    assert section.length == 4
    assert section.data == b'abcd'
Ejemplo n.º 15
0
def filler_0_16():
    return Section(data=filler(0x00, 16))
Ejemplo n.º 16
0
def test_initialize_from_list():
    section = Section(data=[10, 20, 30, 40])
    assert section.start_address == 0
    assert section.length == 4
    assert section.data == b'\n\x14\x1e('
Ejemplo n.º 17
0
def test_adress_out_of_range2():
    s0 = Section(start_address=0x10, data="hello")
    assert not 0x15 in s0
Ejemplo n.º 18
0
def test_default_section():
    section = Section()
    #section = Section2(data=[10, 20, 30, 40])
    assert section.start_address == 0
    assert section.length == 0
    assert section.data == b''
Ejemplo n.º 19
0
def test_write_int16_negative_offset():
    img = Image(Section(data=bytearray(10), start_address=0x1000))
    with pytest.raises(InvalidAddressError):
        img.write_numeric(0x0fff, 0xff, "int16_le")
Ejemplo n.º 20
0
def test_write_ndarray1():
    sec = Section(start_address=0x1000, data=bytearray(32))
    arr = np.array([[11, 22, 33], [44, 55, 66]], dtype="int32")
    sec.write_ndarray(0x1000, arr)
    assert sec.data == b'\x0b\x00\x00\x00\x16\x00\x00\x00!\x00\x00\x00,\x00\x00\x007\x00\x00\x00B\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
Ejemplo n.º 21
0
def test_write_ndarray_out_of_bounds2():
    sec = Section(start_address=0x1000, data=bytearray(32))
    arr = np.array([[11, 22, 33], [44, 55, 66]], dtype="int64")
    with pytest.raises(InvalidAddressError):
        sec.write_ndarray(0x9ff, arr)
Ejemplo n.º 22
0
def test_read_uint8_array_boundary_case4():
    img = Image(Section(data=bytearray(10), start_address=0x1000))
    with pytest.raises(InvalidAddressError):
        img.read_numeric_array(0x0fff, 5, "uint8_be") == (0, 0, 0, 0, 0)
Ejemplo n.º 23
0
def test_initialize_from_array2():
    section = Section(data=array.array('H', [0x1010, 0x2020, 0x3030, 0x4040]))
    assert section.start_address == 0
    assert section.length == 8
    assert section.data == b'\x10\x10  00@@'
Ejemplo n.º 24
0
def test_initialize_from_array1():
    section = Section(data=array.array('B', [10, 20, 30, 40]))
    assert section.start_address == 0
    assert section.length == 4
    assert section.data == b'\n\x14\x1e('
Ejemplo n.º 25
0
 def load(self, fp):
     if isinstance(fp, str):
         fp = open(fp, "rb")
     data = fp.read()
     root = ET.fromstring(data)
     sections = []
     for idx, child in enumerate(root):
         tag = child.tag
         attrib = child.attrib
         text = remove_ws(child.text)
         section_data = bytearray.fromhex(text)
         name = attrib.get('name')
         if name is None:
             self.logger.error(
                 "Block #{}: Missing required attribute `name`.".format(
                     idx))
             continue
         address = attrib.get('address')
         if address:
             address = remove_ws(address)
             address = int(address, 16)
         else:
             self.logger.error(
                 "Block #{}: Missing required attribute `address`.".format(
                     idx))
             continue
         length = attrib.get('length')
         if length:
             length = remove_ws(length)
             length = int(length, 16)
         else:
             self.logger.error(
                 "Block #{}: Missing required attribute `length`.".format(
                     idx))
             continue
         word_size = attrib.get('word_size')
         if word_size:
             word_size = remove_ws(word_size)
             word_size = int(word_size, 16)
         else:
             self.logger.error(
                 "Block #{}: Missing required attribute `wordsize`.".format(
                     idx))
             continue
         if len(section_data) != (length * word_size):
             self.logger.error(
                 "Block #{}: Mismatch between (`length` * `word_size`) and actual block length."
                 .format(idx))
             continue
         checksum = attrib.get('checksum')
         if checksum:
             checksum = remove_ws(checksum)
             if SHA1_DIGEST(section_data) != checksum:
                 self.logger.error(
                     "Block #{}: Wrong `checksum`.".format(idx))
                 continue
         else:
             self.logger.error(
                 "Block #{}: Missing required attribute `checksum`.".format(
                     idx))
             continue
         #print(tag, attrib)
         #print(section_data, SHA1_DIGEST(section_data))
         sections.append(Section(address, section_data))
     img = Image(sections)
     if hasattr(fp, "close"):
         fp.close()
     return img
Ejemplo n.º 26
0
def test_initialize_from_string():
    section = Section(data="abcd")
    assert section.start_address == 0
    assert section.length == 4
    assert section.data == b'abcd'
Ejemplo n.º 27
0
def test_write_float64_negative_offset():
    img = Image(Section(data=bytearray(10), start_address=0x1000))
    with pytest.raises(InvalidAddressError):
        img.write_numeric(0x0fff, 3.14159, "float64_le")
Ejemplo n.º 28
0
def test_adress_in_range2():
    s0 = Section(start_address=0x10, data="hello")
    assert 0x14 in s0
Ejemplo n.º 29
0
def test_initialize_from_range():
    section = Section(data=range(16))
    assert section.start_address == 0
    assert section.length == 16
    assert section.data == b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f'
Ejemplo n.º 30
0
def test_default_startAddress():
    section = Section(start_address=0x10000)
    assert section.start_address == 0x10000
    assert section.length == 0
    assert section.data == b''