package pcrc

  1. Overview
  2. Docs

Pcrc

A parameterized OCaml library for various CRC-8, CRC-16, CRC-32 and CRC-64 algorithms. It allows to instantiate and use (almost) any possible CRC algorithm of 8, 16, 32 or 64 bits.

Build and Install

It should be possible to install the library with opam like any other OCaml package out there:

$ opam install pcrc

Or it may be built and installed with dune:

$ dune build
$ dune install -p pcrc

There are no external dependencies besides OCaml Stdlib and a C compiler to build the C stubs.

Basic Usage

Instantiate a CRC implementation module. E.g., for a variant of CRC-16 used in XMODEM:

# module CRC16 = Pcrc.CRC16.Make (Pcrc.CRC16.XMODEM);;

Compute CRC-16 checksum with the "Basic" API of the created module:

# CRC16.string "123456789";;
- : CRC16.t = 12739
# CRC16.string ~pos:1 "0123456789";;
- : CRC16.t = 12739
# (CRC16.string ~pos:1 ~len:9 "0123456789ABC" :> int) |> Printf.printf "0x%04X\n";;
0x31C3
- : unit = ()

There's also streaming "Update" API in the generated module:

# let crc = CRC16.Update.init;;
# let crc = CRC16.Update.string crc "123";;
# let crc = CRC16.Update.string crc "45";;
# let crc = CRC16.Update.string crc "6789";;
# let crc = CRC16.Update.finalize crc;;
# Printf.printf "0x%04X\n" (crc :> int);;
0x31C3
- : unit = ()

There are similar Make functors for CRC-8, CRC-32 and CRC-64 algorithms, along with some more predefined parameter modules like Pcrc.CRC16.XMODEM.

See Pcrc module interface for details.