crc16 0.1.0 → 0.1.1
raw patch · 3 files changed
+30/−24 lines, 3 filesdep −HUnitdep ~base
Dependencies removed: HUnit
Dependency ranges changed: base
Files
- Data/Digest/CRC16.hs +23/−17
- Tests.hs +3/−3
- crc16.cabal +4/−4
Data/Digest/CRC16.hs view
@@ -7,7 +7,7 @@ -} module Data.Digest.CRC16 ( -- * CRC16 method- crc16_update+ crc16Update ) where import Data.Word(Word8,Word16)@@ -18,26 +18,26 @@ -- This uses the simple method based on /bit shifting/. -- See the unittests for an example. ---crc16_update :: Word16 -- ^ polynomial- -> Bool -- ^ inverse bits - -> Word16 -- ^ initial crc- -> Word8 -- ^ data byte- -> Word16 -- ^ new crc-crc16_update poly rev crc b =- foldl (crc16_update_bit poly) new_crc [1..(bitSize b)]+crc16Update :: Word16 -- ^ polynomial+ -> Bool -- ^ inverse bits+ -> Word16 -- ^ initial crc+ -> Word8 -- ^ data byte+ -> Word16 -- ^ new crc+crc16Update poly rev crc b =+ foldl (crc16UpdateBit poly) new_crc [1..(bitSize b)] where- new_crc = crc `xor` (shiftL (fromIntegral b' :: Word16) 8 )+ new_crc = crc `xor` shiftL (fromIntegral b' :: Word16) 8 b' = if rev- then reverse_bits b+ then reverseBits b else b -crc16_update_bit :: Word16 -> Word16 -> Int -> Word16-crc16_update_bit poly crc _ =+crc16UpdateBit :: Word16 -> Word16 -> Int -> Word16+crc16UpdateBit poly crc _ = if (crc .&. 0x8000) /= 0x0000- then (shiftL crc 1) `xor` poly + then shiftL crc 1 `xor` poly else shiftL crc 1 @@ -46,8 +46,14 @@ -- -- 7..0 becomes 0..7 ---reverse_bits :: Word8 -> Word8-reverse_bits b =- (shiftL (b .&. 0x01) 7) .|. (shiftL (b .&. 0x02) 5) .|. (shiftL (b .&. 0x04) 3) .|. (shiftL (b .&. 0x08) 1) .|.- (shiftR (b .&. 0x10) 1) .|. (shiftR (b .&. 0x20) 3) .|. (shiftR (b .&. 0x40) 5) .|. (shiftR (b .&. 0x80) 7)+reverseBits :: Word8 -> Word8+reverseBits b =+ shiftL (b .&. 0x01) 7+ .|. shiftL (b .&. 0x02) 5+ .|. shiftL (b .&. 0x04) 3+ .|. shiftL (b .&. 0x08) 1+ .|. shiftR (b .&. 0x10) 1+ .|. shiftR (b .&. 0x20) 3+ .|. shiftR (b .&. 0x40) 5+ .|. shiftR (b .&. 0x80) 7
Tests.hs view
@@ -8,8 +8,8 @@ crc16 :: Word16 -> Bool -> Word16 -> [Word8] -> Word16-crc16 poly inverse initial data_string =- B.foldl (crc16_update poly inverse) initial (B.pack data_string)+crc16 poly inverse initial =+ B.foldl (crc16Update poly inverse) initial . B.pack -- | Test functions@@ -27,5 +27,5 @@ ,0x31C3 ~=? crc16 0x1021 False 0x0000 simple_string ] where - simple_string = [0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39] --string "123456789"+ simple_string = [fromIntegral (fromEnum x) :: Word8 | x <- "123456789"]
crc16.cabal view
@@ -1,20 +1,20 @@ name: crc16 -version: 0.1.0+version: 0.1.1 cabal-version: >= 1.2 build-Type: Custom license: BSD3 license-file: LICENSE-copyright: (c) 2009 Joris Putcuyps+copyright: (c) 2010 Joris Putcuyps author: Joris Putcuyps maintainer: Joris.Putcuyps@gmail.com bug-reports: mailto:Joris.Putcuyps@gmail.com synopsis: Calculate the crc16-ccitt.-description: This module provides a method to calculate the crc16-ccitt using the slow bit shift way.+description: This module provides a method to calculate the crc16-ccitt using the slow bit shift. category: Cryptography extra-source-files: Tests.hs Library- Build-Depends: base, bytestring, HUnit+ Build-Depends: base >= 3 && < 5, bytestring Exposed-modules: Data.Digest.CRC16 ghc-options: -Wall