package pcrc

  1. Overview
  2. Docs

Module PcrcSource

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.

Examples

Basic usage

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

CRC-32 of standard input

Using the streaming interface in Update module.

  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"
Sourcemodule type Params = sig ... end

Parameters of a particular CRC implementation.

Sourcemodule type S = sig ... end

Signature of a complete CRC implementation module.

Sourcemodule CRC8 : sig ... end
Sourcemodule CRC16 : sig ... end
Sourcemodule CRC32 : sig ... end
Sourcemodule CRC64 : sig ... end