diff --git a/pfp/fields.py b/pfp/fields.py index 2c43356..fc8bab9 100644 --- a/pfp/fields.py +++ b/pfp/fields.py @@ -2313,6 +2313,8 @@ def __getitem__(self, idx): res._pfp__parent = self res._pfp__array_idx = idx res._pfp__name = "{}[{}]".format(self._pfp__name, idx) + if self._pfp__offset >= 0: + res._pfp__offset = self._pfp__offset + offset return res def __setitem__(self, idx, value): diff --git a/tests/test_arrays.py b/tests/test_arrays.py index 6fadea8..54ef92e 100644 --- a/tests/test_arrays.py +++ b/tests/test_arrays.py @@ -334,6 +334,24 @@ def test_array_with_root_scope(self): stdout="00|00|00|00|", ) + def test_numerical_array_offset(self): + dom = self._test_parse_build( + "\xFF\x00\x00\x11\x11\x22\x22", + """ + char shift_by_one; + uint16 the_array[3]; + """ + ) + + self.assertEqual(dom.the_array[0]._pfp__offset, 1) + self.assertEqual(dom.the_array[1]._pfp__offset, 3) + self.assertEqual(dom.the_array[2]._pfp__offset, 5) + + adhoc_array = Array(3, dom.the_array.field_cls) + self.assertEqual(adhoc_array[0]._pfp__offset, -1) + self.assertEqual(adhoc_array[1]._pfp__offset, -1) + self.assertEqual(adhoc_array[2]._pfp__offset, -1) + if __name__ == "__main__": unittest.main()