September Morn Update

This commit is contained in:
Ingy döt Net 2019-09-12 10:33:56 -07:00
parent 4e2d22a71d
commit aac6731f2c
6856 changed files with 141342 additions and 21127 deletions

View file

@ -1,21 +1,26 @@
import Data.Bits
import Data.Word
import Numeric
import Data.Bits ((.&.), complement, shiftR, xor)
import Data.Word (Word32)
import Numeric (showHex)
crcTable :: Word32 -> Word32
crcTable i = table !! (fromIntegral i)
where
table = map (\a -> iterate xf a !! 8) [0..255]
xf r = let d = shiftR r 1 in
if r .&. 1 == 1 then xor d 0xedb88320 else d
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 c = (fromIntegral . fromEnum) c
charToWord = fromIntegral . fromEnum
calcCrc :: String -> Word32
calcCrc text = complement ( foldl cf (complement 0) text )
where cf crc x = xor (shiftR crc 8) (crcTable $ xor (crc .&. 0xff) (charToWord x) )
calcCrc = complement . foldl cf (complement 0)
where
cf crc x = xor (shiftR crc 8) (crcTable $ xor (crc .&. 0xff) (charToWord x))
crc32 text = showHex ( calcCrc text ) ""
crc32 :: String -> String
crc32 = flip showHex [] . calcCrc
main :: IO ()
main = putStrLn $ crc32 "The quick brown fox jumps over the lazy dog"

View file

@ -2,12 +2,12 @@ import Data.List (genericLength)
import Numeric (showHex)
import Foreign.C
foreign import ccall "zlib.h crc32" zlib_crc32 :: CULong -> CString -> CUInt -> CULong
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 ""
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 ""