crc

    CRCs of up to 64 bits from their parameters or by name, combined across pieces, with fast CRC-32, CRC-32C and CRC-64/XZ.

    crc
    checksum
    crc32
    crc32c
    crc64
    Download zip
    Author
    Version
    0.1.0
    License
    Apache-2.0
    Last updated
    yesterday
    Downloads
    18

    No dependencies

    #gaato/crc

    Cyclic redundancy checks in pure MoonBit, for Native, JS, Wasm and Wasm GC: any CRC of up to 64 bits from its parameters, the 112 of the reveng catalogue by name, the CRC of two pieces from theirs (combine), and CRC-32, CRC-32C and CRC-64/XZ eight bytes at a time.

    #Install

    moon add gaato/crc

    #Usage

    ///|
    test {
    // The three that formats use most, as values and as plain functions.
    inspect(@crc.crc32.checksum(b"123456789"), content="3421780262")
    inspect(@crc.crc32c_update(0, b"123456789"), content="3808858755")
    // Any CRC of the catalogue by name, or from its parameters.
    let modbus = @crc.Crc::named("CRC-16/MODBUS").unwrap()
    inspect(modbus.checksum(b"123456789"), content="19255")
    let custom = @crc.Crc::new(
    width=16,
    poly=0x8005,
    init=0xFFFF,
    reflect_in=true,
    )
    inspect(custom.checksum(b"123456789"), content="19255")
    // In pieces: a running digest, or a CRC continued from the one so far.
    let d = @crc.crc32.digest()
    d.write(b"1234")
    d.write(b"56789")
    inspect(d.value(), content="3421780262")
    let head = @crc.crc32.checksum(b"1234")
    inspect(@crc.crc32.update(head, b"56789"), content="3421780262")
    // The CRC of the two pieces one after the other, from theirs and the
    // second's length, without the data.
    let tail = @crc.crc32.checksum(b"56789")
    inspect(@crc.crc32.combine(head, tail, 5), content="3421780262")
    }

    Crc::new(width~, poly~, init?, reflect_in?, reflect_out?, xor_out?) takes Ross Williams's parameters as the reveng catalogue writes them: poly without its top term, init the register's start, reflect_in for input taken least significant bit first, reflect_out (as reflect_in unless given) for a register reflected before output, and xor_out. Crc::named finds a CRC of the catalogue by its name or an alias, in any case ("CRC-32" is CRC-32/ISO-HDLC), and gives None for another. A CRC's parameters are readable fields.

    update(crc, data) continues a CRC as zlib's crc32 does: from the CRC of the data so far, with checksum(b"") for none, so that update(checksum(a), b) is checksum(a + b). combine(crc_a, crc_b, len_b) gives the CRC of a then b from those of each and b's length in bytes, in time logarithmic in the length, as zlib's crc32_combine does; it serves pieces computed apart, in parallel or on other machines.

    crc32, crc32c and crc64_xz, and any Crc made with their parameters, take eight bytes at a time (slice-by-8, from tables computed at start-up: 8 KiB each for the 32-bit two and 16 KiB for CRC-64/XZ). crc32_update, crc32c_update and crc64_xz_update are the same as plain functions on UInt32 and UInt64, starting from 0. Other CRCs take a byte at a time from a table of 256 entries made with the Crc.

    Not included: CRCs wider than 64 bits (the catalogue's CRC-82/DARC), whose register does not fit a UInt64; messages that are not whole bytes; hardware CRC instructions, which MoonBit does not reach; and other checksums. Adler-32 and xxHash are not CRCs, and gaato/deflate and gaato/zstd keep theirs internal; for SHA-2 and other cryptographic hashes, use moonbitlang/x/crypto.

    #Errors

    None of the functions raises. Crc::new aborts for parameters a program cannot mean: a width outside 1 to 64, or a poly, init or xor_out that does not fit in the width. For parameters that come from data, such as a configuration, Crc::try_new takes the same arguments and gives Err with the reason instead. combine takes a negative length as 0.

    #License

    Apache-2.0.

    Crc

    pub struct Crc {
    width : Int
    poly : UInt64
    init : UInt64
    reflect_in : Bool
    reflect_out : Bool
    xor_out : UInt64
    // private fields
    }

    A cyclic redundancy check of 1 to 64 bits, by Ross Williams's parameters as the reveng catalogue gives them: the polynomial poly without its top term, the register's start init, whether input bytes are taken least significant bit first (reflect_in), whether the register is reflected before it is output (reflect_out), and xor_out, what the output is XORed with. A CRC is the same value however the data is split, and two CRCs combine into the CRC of the data one after the other (combine).

    Crc::checksum

    fn Crc::checksum(self : Crc, data : BytesView) -> UInt64

    The CRC of data.

    Crc::combine

    fn Crc::combine(self : Crc, crc_a : UInt64, crc_b : UInt64, len_b : Int64) -> UInt64

    The CRC of a followed by b, from crc_a, the CRC of a, crc_b, that of b, and len_b, the length of b in bytes (0 for a negative one), in time logarithmic in len_b, as zlib's crc32_combine does.

    Crc::digest

    fn Crc::digest(self : Crc) -> Digest

    A digest of no data yet.

    Crc::named

    fn Crc::named(name : String) -> Crc?

    The CRC the reveng catalogue calls name, by its name or an alias, in any case: "CRC-32" (an alias of CRC-32/ISO-HDLC), "CRC-16/ARC", "CRC-64/XZ" and so on; None for a name it does not have.

    Crc::new

    fn Crc::new(width~ : Int, poly~ : UInt64, init? : UInt64, reflect_in? : Bool, reflect_out? : Bool, xor_out? : UInt64) -> Crc

    The CRC with these parameters (see Crc). reflect_out is reflect_in unless given. Aborts unless width is 1 to 64 and poly, init and xor_out fit in it, for parameters the program writes; try_new checks parameters that come from data.

    Crc::try_new

    fn Crc::try_new(width~ : Int, poly~ : UInt64, init? : UInt64, reflect_in? : Bool, reflect_out? : Bool, xor_out? : UInt64) -> Result[Crc, String]

    The CRC with these parameters, as new makes it, or why there is none: a width outside 1 to 64, or a poly, init or xor_out that does not fit in the width.

    Crc::update

    fn Crc::update(self : Crc, crc : UInt64, data : BytesView) -> UInt64

    The CRC of the data so far, whose CRC is crc, extended by data: update(checksum(a), b) is checksum(a + b), and the CRC of no data is checksum(b"").

    Digest

    pub struct Digest {
    // private fields
    }

    A running CRC: write data in pieces, value is the CRC of all of it.

    Digest::reset

    fn Digest::reset(self : Digest) -> Unit

    Forgets the data written, as if new.

    Digest::value

    fn Digest::value(self : Digest) -> UInt64

    The CRC of the data written so far.

    Digest::write

    fn Digest::write(self : Digest, data : BytesView) -> Unit

    Adds data to what the digest has seen.

    crc32

    let crc32 : Crc

    CRC-32 (CRC-32/ISO-HDLC): gzip's, zlib's crc32, PNG's, Ethernet's.

    crc32_update

    fn crc32_update(crc : UInt, data : BytesView) -> UInt

    The CRC-32 of the data so far, crc (0 for none), extended by data, eight bytes at a time, as zlib's crc32(crc, buf, len) computes it.

    crc32c

    let crc32c : Crc

    CRC-32C (CRC-32/ISCSI, Castagnoli's): Snappy's framing format's, iSCSI's, SCTP's, ext4's and Btrfs's.

    crc32c_update

    fn crc32c_update(crc : UInt, data : BytesView) -> UInt

    The CRC-32C of the data so far, crc (0 for none), extended by data, eight bytes at a time.

    crc64_xz

    let crc64_xz : Crc

    CRC-64/XZ (ECMA-182's polynomial, reflected): xz's and Go's crc64.ECMA.

    crc64_xz_update

    fn crc64_xz_update(crc : UInt64, data : BytesView) -> UInt64

    The CRC-64/XZ of the data so far, crc (0 for none), extended by data, eight bytes at a time, as xz's lzma_crc64 computes it.