summaryrefslogtreecommitdiff
path: root/Data/Digest/CRC24.hs
diff options
context:
space:
mode:
Diffstat (limited to 'Data/Digest/CRC24.hs')
-rw-r--r--Data/Digest/CRC24.hs13
1 files changed, 9 insertions, 4 deletions
diff --git a/Data/Digest/CRC24.hs b/Data/Digest/CRC24.hs
index 4637834..4586177 100644
--- a/Data/Digest/CRC24.hs
+++ b/Data/Digest/CRC24.hs
@@ -4,12 +4,14 @@
4-- (See the LICENSE file). 4-- (See the LICENSE file).
5 5
6module Data.Digest.CRC24 ( 6module Data.Digest.CRC24 (
7 crc24 7 crc24
8 , crc24Lazy
8) where 9) where
9 10
10import Data.Bits (shiftL, (.&.), xor) 11import Data.Bits (shiftL, (.&.), xor)
11import Data.ByteString (ByteString) 12import Data.ByteString.Lazy (ByteString)
12import qualified Data.ByteString as B 13import qualified Data.ByteString as B
14import qualified Data.ByteString.Lazy as BL
13import Data.Word (Word8, Word32) 15import Data.Word (Word8, Word32)
14 16
15crc24_init :: Word32 17crc24_init :: Word32
@@ -21,5 +23,8 @@ crc24_poly = 0x1864CFB
21crc24_update :: Word32 -> Word8 -> Word32 23crc24_update :: Word32 -> Word8 -> Word32
22crc24_update c b = (last . take 9 $ iterate (\x -> if (shiftL x 1) .&. 0x1000000 == 0x1000000 then shiftL x 1 `xor` crc24_poly else shiftL x 1) (c `xor` shiftL (fromIntegral b) 16)) .&. 0xFFFFFF 24crc24_update c b = (last . take 9 $ iterate (\x -> if (shiftL x 1) .&. 0x1000000 == 0x1000000 then shiftL x 1 `xor` crc24_poly else shiftL x 1) (c `xor` shiftL (fromIntegral b) 16)) .&. 0xFFFFFF
23 25
24crc24 :: ByteString -> Word32 26crc24 :: B.ByteString -> Word32
25crc24 bs = B.foldl crc24_update crc24_init bs 27crc24 bs = crc24Lazy . BL.fromChunks $ [bs]
28
29crc24Lazy :: ByteString -> Word32
30crc24Lazy bs = BL.foldl crc24_update crc24_init bs