Can I return my (byte) string?

Given a binary format that includes VarInts. These are integers with a binary representation that varies in size, is it possible to run a parallel parser that checks everything that I just read?

Example:

A simple structure will be analyzed as follows:

getFoo :: Get (VarInt, VarInt)
getFoo = (,) <$> getVarInt <*> getVarInt

Edit: VarInt - this is just a new type around Int. It is analyzed as follows:

(see section 1.2 http://tukaani.org/xz/xz-file-format.txt )

getVarInt :: Get VarInt
getVarInt = go 0
    where go i = do

        h <- fromIntegral <$> getWord8

        if not $ testBit h 7
          then return (h .&. 0x7F)
          else do
            t <- go (i+1)
            return $ h .|. (t `shiftL` (i * 7))

Now some structures have a CRC32 checksum at the end. If the size of the structure is known in advance, I could easily lookAheadrun the checksum:

getFooCRC :: CRC32 -> Get (Int, Int)
getFooCRC expected = do
  crc <- crc32 <$> lookAhead (getBytes 8)
  when (crc /= expected) $ fail "checksum doesn't match"
  (,) <$> get <*> get

... , , VarInt s?

. cereal, binary .

+4

All Articles