Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
26
Task/CRC-32/Haskell/crc-32-1.hs
Normal file
26
Task/CRC-32/Haskell/crc-32-1.hs
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
import Data.Bits ((.&.), complement, shiftR, xor)
|
||||
import Data.Word (Word32)
|
||||
import Numeric (showHex)
|
||||
|
||||
crcTable :: Word32 -> Word32
|
||||
crcTable = (table !!) . fromIntegral
|
||||
where
|
||||
table = ((!! 8) . iterate xf) <$> [0 .. 255]
|
||||
shifted x = shiftR x 1
|
||||
xf r
|
||||
| r .&. 1 == 1 = xor (shifted r) 0xedb88320
|
||||
| otherwise = shifted r
|
||||
|
||||
charToWord :: Char -> Word32
|
||||
charToWord = fromIntegral . fromEnum
|
||||
|
||||
calcCrc :: String -> Word32
|
||||
calcCrc = complement . foldl cf (complement 0)
|
||||
where
|
||||
cf crc x = xor (shiftR crc 8) (crcTable $ xor (crc .&. 0xff) (charToWord x))
|
||||
|
||||
crc32 :: String -> String
|
||||
crc32 = flip showHex [] . calcCrc
|
||||
|
||||
main :: IO ()
|
||||
main = putStrLn $ crc32 "The quick brown fox jumps over the lazy dog"
|
||||
13
Task/CRC-32/Haskell/crc-32-2.hs
Normal file
13
Task/CRC-32/Haskell/crc-32-2.hs
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
import Data.List (genericLength)
|
||||
import Numeric (showHex)
|
||||
import Foreign.C
|
||||
|
||||
foreign import ccall "zlib.h crc32" zlib_crc32 ::
|
||||
CULong -> CString -> CUInt -> CULong
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
let s = "The quick brown fox jumps over the lazy dog"
|
||||
ptr <- newCString s
|
||||
let r = zlib_crc32 0 ptr (genericLength s)
|
||||
putStrLn $ showHex r ""
|
||||
Loading…
Add table
Add a link
Reference in a new issue