开发者

Haskell: Parse binary stream and then calculate its CRC

I'm trying to parse a Gzip file's header using Haskell's Data.Binary.Get. The header is structured as f开发者_高级运维ollows:

  • Fixed fields containing ID, flag, others
  • If flag & 0x01, then optional 16-bit word (the length) + length bytes of data
  • If flag & 0x02, then null terminated string
  • if flag & 0x04, then null terminated string
  • ...
  • CRC32 of above

To report errors (e.g. Gzip unexpected fields), I use the following for the parser:

Data GzipError = GE1 | GE2 | ...
instance Error GzipError where ...
parseHeader :: ErrorT GzipError Get GzipHeader

Here is my problem: How do I parse all the fixed & variable fields in the header, yet at the same time get the ByteString that the fields are parsed from, so that I can calculate and verify the CRC?

I note my need to return errors because I've thought about using lookAhead, but it has a type of Get a -> Get a, which means I can't do error processing inside, which I need to do.

It's almost as if I need to set a bookmark for the Get monad, read the fields, and then ask Get for the ByteString from the bookmark to the current position. Of course it does not provide this functionality.

Any ideas?


Use runGetState, it returns the number of bytes consumed. Then you can just chop off that many bytes from the original ByteString and checksum that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜