I'm writing a custom dissector for a home-grown protocol. Strings in the protocol are encoded as a 4-byte binary byte count followed by the bytes of the string. Zero length strings are allowable. The problem occurs when a zero length string is the very last object in the buffer.
I have a function getString():
function getString(buffer, position) local length = buffer:range(position, 4):uint() position = position + 4 local string = buffer:range(position, length) position = position + length return position, string end
If I call that with a buffer (in hex): 00 00 00 03 41 42 43 00 00 00 00 I correctly get the string "ABC" in the TvbRange that's returned, and position is correctly returned as 7.
If I call that with a buffer: 00 00 00 00 00 00 00 00 I correctly get an empty TvbRange returned, and position is correctly returned as 4.
If I call that with a buffer: 00 00 00 00 I get a runtime error "Range is out of bounds".
Is there some way to get range() to correctly return an empty range in this case? Can I test for the condition (e.g. length == 0 and position == buffer:len()) and return a "fake" TvbRange.
Currently, I'm returning string as nil. But that means that I have to test for nil everywhere that getString() is called - that's inconvenient.
Thanks for any help, Graham