#ifndef MIN_VERSION_lens
#define MIN_VERSION_lens(x,y,z) 1
#endif
module Data.Hash.CRC32
( CRC32
, updated
, final
) where
import Control.Lens
import Data.Bits
import Data.Default
import Data.Monoid
import Data.Word
import Foreign.Storable
import Foreign.Ptr
import GHC.Base
newtype CRC32 = CRC32 { getCRC32 :: Word32 }
instance Default CRC32 where
def = CRC32 0xffffffff;
instance (Reviewable p, Functor f) => Snoc p f CRC32 CRC32 Word8 Word8 where
_Snoc = unto $ \(CRC32 h, w) -> CRC32 (shiftL h 8 `xor` lut w)
#if MIN_VERSION_lens(3,9,0)
updated :: Getting (Endo (Endo CRC32)) t Word8 -> t -> CRC32 -> CRC32
#else
updated :: Getting (Endo (Endo CRC32)) t t Word8 Word8 -> t -> CRC32 -> CRC32
#endif
updated l t z = foldlOf' l snoc z t
final :: CRC32 -> Word32
final = complement . getCRC32
foreign import ccall "static &crc32_lut" crc32_lut :: Ptr Word32
#ifndef HLINT
inlinePerformIO :: IO a -> a
inlinePerformIO (IO m) = case m realWorld# of
(# _, r #) -> r
#endif
lut :: Word8 -> Word32
lut i = inlinePerformIO (peekElemOff crc32_lut (fromIntegral i))