Skip to content

Instantly share code, notes, and snippets.

@tsuraan
Created June 5, 2011 17:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsuraan/1009218 to your computer and use it in GitHub Desktop.
Save tsuraan/1009218 to your computer and use it in GitHub Desktop.
{-# LANGUAGE BangPatterns #-}
import Data.Digest.Adler32 (adler32)
import Data.ByteString (ByteString)
import qualified Data.ByteString as BS
import System.IO
import Data.Word
updateHash :: Word32 -> Word8 -> Word32 -> Word8 -> Word32
updateHash _winlen old8 !hash word8 = hash + (fromIntegral $ old8 + word8)
{- actual adler32 sliding window code goes here -}
updateHash' :: ByteString -> Word32 -> Int -> Word32 -> Word8 -> Word32
updateHash' bs winlen oldpos =
updateHash winlen old
where
old = {-# SCC "BS.index" #-} (BS.index bs oldpos)
runWindow :: Word32 -> ByteString -> Word32
runWindow winlen bs =
let (inits, rest) = BS.splitAt (fromIntegral winlen) bs
updatefn :: (Int, Word32) -> Word8 -> (Int, Word32)
updatefn = \(oldp, !hash) word ->
(oldp + 1, updateHash' bs winlen oldp hash word)
in snd $ BS.foldl updatefn (0, (adler32 inits)) rest
main = do
bytes <- withFile "/dev/urandom" ReadMode ((flip BS.hGet) 16777216)
return $! runWindow 8 bytes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment