dmi_checksum_calc

Synopsis

#include <opendmi/utils.h>

bool dmi_checksum_calc(const void *data, size_t length, uint8_t *pchecksum);
bool dmi_checksum_test(const void *data, size_t length);

Description

SMBIOS protects its entry points with an 8-bit checksum: the bytes of the data, the checksum byte included, add up to zero modulo 256.

The dmi_checksum_calc function computes the checksum of the length bytes at data, that is the value which brings their sum to zero modulo 256, and stores it in the variable pchecksum points to. The checksum byte within the data is counted as any other byte, so it is set to zero before the checksum is computed.

The dmi_checksum_test function tells whether the length bytes at data add up to zero modulo 256, i.e. whether the checksum they hold is valid.

Parameters

data

Data to sum.

length

Number of bytes to sum.

pchecksum

Variable to store the checksum in.

Return value

The dmi_checksum_calc function returns true if the checksum has been computed, and false if data or pchecksum is nullptr, in which case the variable is left as it is. The checksum of zero bytes is zero.

The dmi_checksum_test function returns true if the bytes add up to zero, which they do when length is zero, and false otherwise, or if data is nullptr.

Errors

DMI_ERROR_ARGUMENT_NULL

Raised if data or pchecksum is nullptr. The functions take no context, so the error reaches no error queue.

Example

uint8_t data[16] = { ... };

data[5] = 0;
if (!dmi_checksum_calc(data, sizeof(data), &data[5]))
    return false;

assert(dmi_checksum_test(data, sizeof(data)));