Parameterized CRC (cyclic redundancy check) library for CRC-8, CRC-16, CRC-32 and CRC-64 algorithms.
Inspired by 1993 paper "A Painless Guide to CRC Error Detection Algorithms" by Ross N. Williams. The paper defines a parameterized model CRC algorithm. The model algorithm can be parameterized to behave like most of the CRC implementations around.
let () =
let buf = Bytes.create 4096 in
let module CRC32 = Pcrc.CRC32.Make (Pcrc.CRC32.ISO_HDLC) in
let rec loop crc =
match input stdin buf 0 (Bytes.length buf) with
| 0 -> CRC32.Update.finalize crc
| len -> loop (CRC32.Update.bytes ~len crc buf)
in
loop CRC32.Update.init |> Printf.printf "%08lx\n"
CRC-64 with custom polynomial
Creating custom CRC implementations from a set of parameters.
module Redis_CRC64 = Pcrc.CRC64.Make (struct
let rev = true
let poly = 0xad93d23594c935a9L
let init = 0L
let final = 0L
end)
let () = Redis_CRC64.string "123456789" |> Printf.printf "%016Lx\n"