diff options
author | Clint Adams <clint@debian.org> | 2012-04-26 20:28:40 -0400 |
---|---|---|
committer | Clint Adams <clint@debian.org> | 2012-04-26 20:28:40 -0400 |
commit | 8c34729c62ba64c810fbfa73719ae7f7110c0fbe (patch) | |
tree | c510a323f4c4a49cfb19ca3ba3132e8a80dc48d1 /Data | |
parent | 1ddca48c27cece9352e85ce6188f697eb9124750 (diff) |
More laziness.
Diffstat (limited to 'Data')
-rw-r--r-- | Data/Digest/CRC24.hs | 13 |
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 | ||
6 | module Data.Digest.CRC24 ( | 6 | module Data.Digest.CRC24 ( |
7 | crc24 | 7 | crc24 |
8 | , crc24Lazy | ||
8 | ) where | 9 | ) where |
9 | 10 | ||
10 | import Data.Bits (shiftL, (.&.), xor) | 11 | import Data.Bits (shiftL, (.&.), xor) |
11 | import Data.ByteString (ByteString) | 12 | import Data.ByteString.Lazy (ByteString) |
12 | import qualified Data.ByteString as B | 13 | import qualified Data.ByteString as B |
14 | import qualified Data.ByteString.Lazy as BL | ||
13 | import Data.Word (Word8, Word32) | 15 | import Data.Word (Word8, Word32) |
14 | 16 | ||
15 | crc24_init :: Word32 | 17 | crc24_init :: Word32 |
@@ -21,5 +23,8 @@ crc24_poly = 0x1864CFB | |||
21 | crc24_update :: Word32 -> Word8 -> Word32 | 23 | crc24_update :: Word32 -> Word8 -> Word32 |
22 | crc24_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 | 24 | crc24_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 | ||
24 | crc24 :: ByteString -> Word32 | 26 | crc24 :: B.ByteString -> Word32 |
25 | crc24 bs = B.foldl crc24_update crc24_init bs | 27 | crc24 bs = crc24Lazy . BL.fromChunks $ [bs] |
28 | |||
29 | crc24Lazy :: ByteString -> Word32 | ||
30 | crc24Lazy bs = BL.foldl crc24_update crc24_init bs | ||