From: Ramakrishnan Muthukrishnan Date: Thu, 31 Dec 2015 13:28:53 +0000 (+0530) Subject: add rolling checksum implementation X-Git-Url: https://git.rkrishnan.org/components/%22news.html/frontends/using.html?a=commitdiff_plain;h=180950d844550cbe5cdc47a6ce4d3c5fa50d7709;p=hs-rsync.git add rolling checksum implementation --- diff --git a/src/Lib.hs b/src/Lib.hs index 260de94..2500097 100644 --- a/src/Lib.hs +++ b/src/Lib.hs @@ -83,3 +83,10 @@ recreate f0 blockSize ins = RBlk i -> (f0blocks !! i) `mappend` go f0blocks insts RChar w -> BL.singleton w `mappend` go f0blocks insts +rollingChecksum :: Int -> Int -> BL.ByteString -> Word32 +rollingChecksum strtIdx lenBS bs = a `mod` m + ((fromIntegral b) `mod` m) * m + where a = BL.foldl (\acc x -> acc + (fromIntegral x)) 0 bs' + b = BL.foldl (\acc x -> acc + x) 0 (BL.pack wbs') + bs' = BL.take (fromIntegral lenBS) $ BL.drop (fromIntegral strtIdx) bs + m = 2^16 + wbs' = BL.zipWith (*) (BL.pack (reverse (map fromIntegral [1..(lenBS - strtIdx + 1)]))) bs'