Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. import Data.Bits
  2. import Data.Word
  3. import Numeric
  4.  
  5. fI :: (Integral a, Num b) => a -> b
  6. fI = fromIntegral
  7.  
  8. crc32 :: [Word8] -> Word32
  9. crc32 xs = foldl k 0xFFFFFFFF xs `xor` 0xFFFFFFFF
  10. where
  11. f x = if x .&. 1 /= 0
  12. then x `shiftR` 1 `xor` 0xEDB88320
  13. else x `shiftR` 1
  14. g x = iterate f x !! 8
  15. t = map g [0..255]
  16. k c x = t !! fI ((c `xor` fI x) .&. 0xFF) `xor` (c `shiftR` 8)
  17.  
  18. crc8 :: [Word8] -> Word8
  19. crc8 xs = foldl k 0xFF xs `xor` 0xFF
  20. where
  21. f x = if x .&. 0x80 /= 0
  22. then x `shiftL` 1 `xor` 0x1D
  23. else x `shiftL` 1
  24. g x = iterate f x !! 8
  25. t = map g [0..255]
  26. k c x = t !! fI ((c `xor` x) .&. 0xFF) `xor` (c `shiftL` 8)
  27.  
  28. main :: IO ()
  29. main = do
  30. putStrLn $ showHex (crc8 [0x04,0x53,0x5F,0x0A,0x6C,0x20,0xB1,0xFF,0x00]) ""
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement