Skip to main content

mp.checksum — 14 functions

Byte inputs are 1-based byte tables.

FunctionReturnsDescription
list_crc_presets()tableArray of {name, width, poly, init, reflect_in, reflect_out, xor_out}
crc(preset, bytes)table{value, hex, width, preset} or {error}
crc_custom(params, bytes)tableCustom CRC; params = {name?, width, poly, init, reflect_in, reflect_out, xor_out}
crc_region(preset, r, start?, len?)tableCRC over a memory region (or sub-range)
sum8(bytes)int8-bit sum
sum16(bytes)int16-bit sum
sum16_inv(bytes)intInverted 16-bit sum (two's complement)
sum_rl78(bytes)intRenesas RL78 / NEC 78K block checksum
fletcher16(bytes)intFletcher-16
fletcher32(bytes)numberFletcher-32 (double due to range)
adler32(bytes)numberAdler-32 (double)
lrc(bytes)intLongitudinal redundancy check
bcc(bytes)intBlock check character (XOR reduction)
list_builtins()tableArray of {name, width} for all non-CRC builtins

Multi-return convention for crc()

crc() returns three values: (value, hex_string, info_table). Pick what you need — a bare assignment grabs just the value (number).

local v = mp.checksum.crc("CRC-32", data) -- 0xCBF43926 as number
local v, hex = mp.checksum.crc("CRC-32", data) -- "0xCBF43926"
local v, hex, info = mp.checksum.crc("CRC-32", data) -- info.{width, preset, error}

local v, hex = mp.checksum.crc_region("CRC-32", "Flash") -- whole region
local v, hex = mp.checksum.crc_region("CRC-32", "Flash", 0, 256) -- slice

mp.checksum.sum8(data) -- simple sum, returns int
mp.checksum.fletcher16(data)
mp.checksum.adler32(data)
mp.checksum.list_crc_presets() -- all available CRC algorithms

For Lua-based checksum scripts that drive the status bar, see Checksum scripts.