+ → findNextBlock src'
+
+tryGetBlock ∷ L.ByteString → (# S.Maybe Block, L.ByteString #)
+tryGetBlock src
+ = case L.splitAt 32 src of
+ (header, rest)
+ → case L.length header ≡ 32 of
+ True
+ → case L.take 4 header ≡ headerMagic of
+ True
+ -- Found the magic "wvpk". Let's parse
+ -- the header and see if it's really a
+ -- header we can accept.
+ → case runGet get header of
+ bh → if isGoodHeader bh then
+ case runGetState (getSubBlocks $ bhSize bh) rest 0 of
+ (subs, rest', _)
+ → let !blk = Block {
+ blockHeader = bh
+ , blockMetadata = subs
+ }
+ in
+ (# S.Just blk, rest' #)
+ else
+ findNextBlock $ L.tail src
+ False
+ → findNextBlock $ L.tail src
+ False
+ → (# S.Nothing, L.empty #)
+
+headerMagic ∷ L.ByteString
+headerMagic = L.pack [119, 118, 112, 107] -- "wvpk"
+
+isGoodHeader ∷ BlockHeader → Bool
+isGoodHeader bh
+ -- Rule #1: bhSize + 32 - 8 ≡ 0 (mod 2)
+ | odd $ bhSize bh = False
+ -- Rule #2: bhSize + 32 - 8 ≤ 0x00100000
+ | bhSize bh + 32 - 8 > 0x100000 = False
+ -- Rule #3: bhSize + 32 - 8 > 24
+ | bhSize bh + 32 - 8 ≤ 24 = False
+ -- Rule #4: 0x402 ≤ bhVersion ≤ 0x410
+ | bhVersion bh < 0x402 = False
+ | bhVersion bh > 0x410 = False
+ -- Rule #5: bhBlockSamples < 0x00030000
+ | bhBlockSamples bh ≥ 0x30000 = False
+ -- Now it passed all the tests...
+ | otherwise = True