I am pretty new to Haskell, and my first project is to parse captured WLAN packets. A common pattern in parsing such packet is that a header field will define the structure of the remaining bytes. As a generic example, a packet can be formatted like this:
header + [payload A | payload B | ..]
where a flag field (can be a bitmap) in the header specifies what payload(s) are included in the packet. For a specific example of this format, please check out radiotap.
A similar thread suggests to just use a sequence of parse
operations like this:
parseAll = do
hdr <- parseHeader
pa <- parsePayloadA
pb <- parsePayloadB
However it seems cannot be applied in my case as the existence of payload A
and B
is defined by the header. In other word, the control flow of data parsing needs to follow a prior parsing result. I'd like to understand that if there is a generic way to parse binary data with this kind of pattern?