{-# LINE 1 "Data/Digest/Adler32.hsc" #-}
{-# LANGUAGE ForeignFunctionInterface, FlexibleInstances #-}
module Data.Digest.Adler32 (
Adler32, adler32, adler32Update
) where
import Data.ByteString.Unsafe (unsafeUseAsCStringLen)
import Foreign
import qualified Data.ByteString as S
import qualified Data.ByteString.Lazy as L
import qualified Data.ByteString.Lazy.Internal as LI
import qualified System.IO.Unsafe as U
class Adler32 a where
adler32 :: a -> Word32
adler32 = adler32Update 1
adler32Update :: Word32 -> a -> Word32
instance Adler32 S.ByteString where
adler32Update = adler32_s_update
instance Adler32 L.ByteString where
adler32Update = adler32_l_update
instance Adler32 [Word8] where
adler32Update n = (adler32Update n) . L.pack
adler32_s_update :: Word32 -> S.ByteString -> Word32
adler32_s_update seed str
| S.null str = seed
| otherwise =
U.unsafePerformIO $
unsafeUseAsCStringLen str $
\(buf, len) -> fmap fromIntegral $
adler32_c (fromIntegral seed) (castPtr buf) (fromIntegral len)
adler32_l_update :: Word32 -> L.ByteString -> Word32
adler32_l_update = LI.foldlChunks adler32_s_update
foreign import ccall unsafe "zlib.h adler32"
adler32_c :: Word64
{-# LINE 65 "Data/Digest/Adler32.hsc" #-}
-> Ptr Word8
{-# LINE 66 "Data/Digest/Adler32.hsc" #-}
-> Word32
{-# LINE 67 "Data/Digest/Adler32.hsc" #-}
-> IO Word64
{-# LINE 68 "Data/Digest/Adler32.hsc" #-}