Use remaining bytes in structure in expression

0 votes
asked May 1, 2016 in Python by andreas (2,420 points)
[Notice: This was once reported via FogBugz but could also be interesting for users reading in this forum]

<number name="size" id="13" type="integer" length="4"/>

<string name="data" id="14" type="fixed-length" length="min(size,Remaining)" encoding="windows-1252"/>

length="Remaining" is fine,

but length="min(size,Remaining)" is not ;)

In this binary file, the string is stored in one or more data blocks. The size could be bigger than the actual (remaining) size of one block.

Is it possible to set the "Remaining" variable before evaluating the "length" expression (and after of course) ?

Or setting another var like "RemainingBeforeEvaluation"?

1 Answer

+1 vote
answered May 1, 2016 by andreas (2,420 points)

Here I wrote a small script that you can put into a script element and which evaluates to the remaining bytes:

~~~~~~~~~~~~~8<~~~~~~~~~~~~~
# get collection with results so far
results = currentMapper.getCurrentResults()
# get latest added result
lastResult = results.getLastResult()
# access the parsed value
value = lastResult.getValue()
# get the value parsed
stringLength = value.getUnsignedNumber()

currentPos = currentMapper.getCurrentOffset()
remainingBytes = currentMapper.getCurrentRemainingSize()

actualSize = min(stringLength, remainingBytes)

byteView = currentMapper.getCurrentByteView()

string = byteView.readString(currentPos, actualSize, "ISO-8859-1")
stringValue = Value()
stringValue.setString(string)

currentElement = currentMapper.getCurrentElement()

results.addElement(currentElement, actualSize, 0, stringValue)

~~~~~~~~~~~~~8<~~~~~~~~~~~~~

...