class::calc::Checksum

class calc::Checksum
  : public api::InfoObject

The Checksum class is purely static and provides methods for calculating and verifying checksums on data structures.

Summary

static u8 calc_zero_sum(

const u8 * data,
int size
)

static u32 calc_zero_sum(

const u32 * data,
int size
)

static u32 calc_zero_sum32(

const var::Data & data
)

static u32 calc_zero_sum8(

const var::Data & data
)

template<>
static T calculate_zero_sum(

const var::DataReference & data
)

template<>
static T verify_zero_sum(

const var::DataReference & data
)

static bool verify_zero_sum(

const u8 * data,
int size
)

static bool verify_zero_sum(

const u32 * data,
int size
)

static bool verify_zero_sum32(

const var::Data & data
)

static bool verify_zero_sum8(

const var::Data & data
)

Members

static u8 calc_zero_sum(

const u8 * data,
int size
)

Calculates a zero sum value for the data.

Parameters

  • data A pointer to the u8 data to sum

  • size The number of bytes in the data including the checksum

Returns

A number that when summed with all data values results in zero

The size value includes the checksum position, but the checksum position is not included in the calculation.

If the data is 32-bit aligned, use calc_zero_sum(const u32 *, int);

//md2code:include
#include <sapi/calc.hpp>
#include <sapi/var.hpp>
//md2code:main
typedef struct MCU_PACK {
 u8 member_a;
 u32 member_b;
 u16 member_c;
 u8 checksum;
} data_struct_t;

data_struct_t data;
//set member variables
data.checksum = Checksum::calc_zero_sum((u8*)&data, sizeof(data));

if( Checksum::verify_zero_sum((u8*)&data, sizeof(data)) == true ){
 //checksum is good
}

static u32 calc_zero_sum(

const u32 * data,
int size
)

Calculates a zero sum value for the data.

Parameters

  • data A pointer to the u8 data to sum

  • size The number of bytes in the data including the checksum

Returns

A number that when summed with all data values results in zero

The size value includes the checksum position, but the checksum position is not included in the calculation.

If the data is 32-bit aligned, use calc_zero_sum(const u32 *, int);

#include <mcu/types.h>
#include <sapi/calc.hpp>

typedef struct MCU_PACK {
 u32 member_a;
 u32 member_b;
 u32 member_c;
 u32 checksum;
} data_struct_t;

data_struct_t data;
//set member variables
data.checksum = Checksum::calc_zero_sum((u32*)&data, sizeof(data));

if( Checksum::verify_zero_sum((u32*)&data, sizeof(data)) == true ){
 //checksum is good
}

static u32 calc_zero_sum32(

const var::Data & data
)


static u32 calc_zero_sum8(

const var::Data & data
)


template<>

static T calculate_zero_sum(

const var::DataReference & data
)


template<>

static T verify_zero_sum(

const var::DataReference & data
)


static bool verify_zero_sum(

const u8 * data,
int size
)

Verifies the u8 checksum at the end of a data structure.

Parameters

  • data A pointer to the data

  • size The number of bytes in the data

Returns

true if the sum of the data is zero


static bool verify_zero_sum(

const u32 * data,
int size
)

Verifies the u32 checksum at the end of a data structure.

Parameters

  • data A pointer to the data

  • size The number of bytes in the data

Returns

true if the sum of the data is zero

The data must be aligned to 32 bits to work.


static bool verify_zero_sum32(

const var::Data & data
)


static bool verify_zero_sum8(

const var::Data & data
)


X

Thanks for Coming!

Subscribe to news and updates