crc16 (empty) → 0.1.0
raw patch · 5 files changed
+143/−0 lines, 5 filesdep +HUnitdep +basedep +bytestringbuild-type:Customsetup-changed
Dependencies added: HUnit, base, bytestring
Files
- Data/Digest/CRC16.hs +53/−0
- LICENSE +29/−0
- Setup.hs +10/−0
- Tests.hs +31/−0
- crc16.cabal +20/−0
+ Data/Digest/CRC16.hs view
@@ -0,0 +1,53 @@+{- | crc16 ccitt++ 1) MSB first 1021 x^16 + x^12 + x^5 + 1 ++ 2) LSB first 8048++-}+module Data.Digest.CRC16 (+ -- * CRC16 method+ crc16_update+ ) where++import Data.Word(Word8,Word16)+import Data.Bits+++-- | crc16 calculation+-- 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)]+ where+ new_crc = crc `xor` (shiftL (fromIntegral b' :: Word16) 8 )+ b' = if rev+ then reverse_bits b+ else b+++++crc16_update_bit :: Word16 -> Word16 -> Int -> Word16+crc16_update_bit poly crc _ =+ if (crc .&. 0x8000) /= 0x0000+ then (shiftL crc 1) `xor` poly + else shiftL crc 1++++-- | Reverse the bits in a byte+--+-- 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)+
+ LICENSE view
@@ -0,0 +1,29 @@+Copyright (c) 2009, Joris Putcuyps+All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions+are met:++- Redistributions of source code must retain the above copyright+notice, this list of conditions, and the following disclaimer.++- Redistributions in binary form must reproduce the above copyright+notice, this list of conditions, and the following disclaimer in the+documentation and/or other materials provided with the distribution.++- The names of the copyright holders may not be used to endorse or+promote products derived from this software without specific prior+written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "AS IS" AND ANY+EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR+PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE+LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR+BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,+WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE+OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN+IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.hs view
@@ -0,0 +1,10 @@+import Distribution.Simple+import Data.Digest.CRC16+import Test.HUnit.Text+import Tests++main = + defaultMainWithHooks ( simpleUserHooks { runTests = test_crc16 } )+ where+ test_crc16 _ _ _ _ = do runTestTT tests+ return ()
+ Tests.hs view
@@ -0,0 +1,31 @@+module Tests where++import Test.HUnit.Base+import Test.HUnit.Text+import Data.Digest.CRC16+import qualified Data.ByteString as B+import Data.Word+++crc16 :: Word16 -> Bool -> Word16 -> [Word8] -> Word16+crc16 poly inverse initial data_string =+ B.foldl (crc16_update poly inverse) initial (B.pack data_string)+++-- | Test functions+--+-- Run with: +-- +-- > import Test.HUnit.Text+-- > runTestTT tests+--+-- Checkout http://zorc.breitbandkatze.de/crc.html+tests :: Test+tests = "Basic" ~: [+ 0x29B1 ~=? crc16 0x1021 False 0xffff simple_string+ ,0x89f6 ~=? crc16 0x1021 True 0xffff simple_string+ ,0x31C3 ~=? crc16 0x1021 False 0x0000 simple_string+ ]+ where + simple_string = [0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39] --string "123456789"+
+ crc16.cabal view
@@ -0,0 +1,20 @@+name: crc16 +version: 0.1.0+cabal-version: >= 1.2+build-Type: Custom+license: BSD3+license-file: LICENSE+copyright: (c) 2009 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.+category: Cryptography+extra-source-files: Tests.hs++Library+ Build-Depends: base, bytestring, HUnit+ Exposed-modules: Data.Digest.CRC16+ ghc-options: -Wall+