Crc32 structv0.1.0

Source
The CRC-32 of a message, computed a byte at a time.
pub struct Crc32 {
    // private fields
}

Copy, and holding only the register, so passing one by value takes a snapshot of the checksum so far.

Constructors

Crc32

pub func Crc32() -> Crc32

Constructs a checksum with nothing written into it yet.

Returns: a hasher with nothing written to it yet

Source

Methods

Reset

pub func Reset(self: &var Crc32)

Puts the checksum back to where Crc32() leaves one. self is borrowed mutably. Everything written so far is forgotten. Cannot fail. Runs in constant time and allocates nothing.

Source

Value

pub func Value(self: &Crc32) -> uint32

The checksum of everything written so far. self is borrowed. This is the register complemented, which is the value every other implementation reports; the register itself stays as it was, so writing more continues rather than starting over. Cannot fail. Runs in constant time and allocates nothing.

Returns: the checksum of everything written so far

Source

Implements Hasher

Crc32 conforms to Hasher.

Write

pub func Write(self: &var Crc32, bytes: *byte, length: uint)

Adds length bytes at bytes to the checksum. bytes is borrowed and read; length is read by value. A zero length changes nothing and is the one case in which bytes may be null. Splitting a message across several writes gives the same answer as one write. Runs in time proportional to length and allocates nothing. bytes must be readable for length bytes.

NameTypeDescription
bytes*bytethe bytes to absorb
lengthuinthow many bytes; zero absorbs nothing

Source

Finish

pub func Finish(self: &var Crc32) -> uint64

The checksum of everything written so far, widened to 64 bits. self is borrowed. The upper 32 bits are always zero: a CRC-32 is 32 bits wide however it is reported, and Value is the call to make when that width is what is wanted. Cannot fail. Runs in constant time and allocates nothing.

Returns: the hash of everything written since the last reset

Source

Crc32Polynomial

pub const Crc32Polynomial: uint32 = 0xEDB88320;

The reflected IEEE 802.3 generator polynomial, which is what gzip, PNG and zip mean by "CRC-32".

Source

Crc32Initial

pub const Crc32Initial: uint32 = 0xFFFFFFFF;

The value a CRC register starts at, and the value the finished register is complemented against. Both ends are all ones, which is what makes a message padded with zeroes at either end give a different answer from the message itself.

Source

Crc32Table

pub const Crc32Table: uint32[N] = …;

One byte of division by Crc32Polynomial, precomputed for every possible byte. Entry i is i divided by the generator, so a whole byte of the message advances the register in one indexed lookup and one shift rather than eight conditional shifts.

Source

Crc32Of

pub func Crc32Of(bytes: *byte, length: uint) -> uint32

The CRC-32 of length bytes at bytes. bytes is borrowed and read; length is read by value. A zero length gives zero, the CRC of the empty message, and is the one case in which bytes may be null. Cannot fail. Runs in time proportional to length and allocates nothing. bytes must be readable for length bytes.

Source

Parameters

NameTypeDescription
bytes*bytethe bytes to hash
lengthuinthow many bytes

Returns: their CRC-32 checksum