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.1
    License
    Apache-2.0
    Last updated
    14 days ago
    Downloads
    768

    No dependencies

    #gaato/crc

    Cyclic redundancy checks in pure MoonBit, for Native, JS, Wasm and Wasm GC. Use it to compute or verify the CRC a file format or protocol specifies: CRC-32 for ZIP, gzip and PNG, CRC-32C for iSCSI, ext4 and Snappy, CRC-64/XZ for .xz, or a CRC such as CRC-16/MODBUS by its catalogue name.

    • Any CRC up to 64 bits wide, from its parameters, or one of the 112 in the reveng catalogue by name.
    • Data in pieces: a running digest, or a CRC continued from a stored one.
    • combine: the CRC of two pieces joined, from the two CRCs and the second piece's length, without the data. Use it for pieces checksummed in parallel or on different machines.
    • CRC-32, CRC-32C and CRC-64/XZ are computed 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")
    }

    Pick the CRC by name, not by width. CRC-32 and CRC-32C are both 32 bits but use different polynomials and give different values. Take the name from the format's specification. Crc::named accepts the catalogue's name or an alias, in any case ("CRC-32" is CRC-32/ISO-HDLC), and returns None for anything else.

    Continuing a CRC. update(crc, data) continues from the CRC of the data so far, as zlib's crc32 does: update(checksum(a), b) equals checksum(a + b), and checksum(b"") is the start. crc32_update, crc32c_update and crc64_xz_update are the same as plain functions, starting from 0.

    Combining. combine(crc_a, crc_b, len_b) takes the second piece's length in bytes, and runs in time logarithmic in that length.

    #Custom parameters

    Crc::new(width~, poly~, init?, reflect_in?, reflect_out?, xor_out?) takes the parameters as the reveng catalogue writes them: poly without its top term, init as the register's start, reflect_in for input taken least significant bit first, reflect_out (the same as reflect_in unless given), and xor_out. A CRC's parameters are readable fields.

    Crc::new aborts on parameters a program cannot mean: a width outside 1 to 64, or a poly, init or xor_out that does not fit the width. For parameters read from a file or configuration, use Crc::try_new, which takes the same arguments and returns Err with the reason. No other function raises or aborts.

    #Not included

    CRCs wider than 64 bits, messages that are not whole bytes, hardware CRC instructions, and checksums that are not CRCs (Adler-32, xxHash). For SHA-2 and other cryptographic hashes, use moonbitlang/x/crypto.

    #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.