Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View 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"

View 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 ""