Unexpected parsing after using setCurrentOffset

0 votes
asked Aug 18, 2018 in Python by hamiltont (360 points)

I have a padding byte that is optional e.g. only appears if a prior length field was an odd value. I've tried to solve this with a script, which appears to be functioning correctly - I am properly detecting the odd value and inserting the padding byte. 

However, when my script inserts a padding byte, the next structure begins processing as though the padding byte was not inserted e.g. it starts mapping directly onto the padding byte instead of mapping onto the next byte. I've tried to change the currentMapper offset, but it does not seem to fix the issue. 

What am I doing wrong? 

Script: 

results = currentMapper.getCurrentResults()
data = results.getResultByName("Length_of_Directory_Identifier")
currentElement = currentMapper.getCurrentElement()
print "-----------------------------------------------"
LEN_DI = data.getValue().getUnsigned()
if LEN_DI % 2 != 0:
  print "Found odd LEN_DI, adding padding"
  posValue = NumberValue()
  posValue.setUnsigned(0)
  results.addElement(currentElement, 1, 0, posValue)
  # Move offset for continuing parsing down one
  offset = currentMapper.getCurrentOffset()
  currentMapper.setCurrentOffset(offset + 1)
  print "Moved %x to %x" % (offset, currentMapper.getCurrentOffset())

The output shows that I am correctly updating the offset A009 to A00A. However, the next structure starts processing from the non-updated offset e.g. Try 'Path Table...' at 40969 (a.k.a. 0xA009). 

0x00 PYT 4000 INFO Python: '-----------------------------------------------'
0x00 PYT 4000 INFO Python: 'Found odd LEN_DI, adding padding'
0x00 PYT 4000 INFO Python: 'Moved a009 to a00a'
0xA009 MAP 1 DEBUG Stop mapping 'Path Table Record' - maximum repeat count '1' reached
0xA009 MAP 1 DEBUG Processed 'Path Table Record' ...
0xA009 MAP 1 DEBUG Try 'Path Table Record' at pos '40969' ...

1 Answer

0 votes
answered Aug 19, 2018 by hamiltont (360 points)

able to workaround using this, but still dont understand why: 

pE = Element(ELEMENT_BINARY, "Padding", True)
currentMapper.mapElementWithSize(pE, 1)
...