Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
Crc.cxx File Reference
#include <stdint.h>
#include "utils/Crc.hxx"
#include "utils/macros.h"

Go to the source code of this file.

Functions

uint8_t reverse (uint8_t data)
 Reverses the bits of a byte.
 
void crc_16_ibm_add (uint16_t &state, uint8_t data)
 Appends a byte to a CRC16 state machine.
 
void crc_16_ibm_add_basic (uint16_t &state, uint8_t data)
 Bitwise implementation of the crc16 ibm add.
 
uint16_t crc_16_ibm_finish (uint16_t state)
 Finalizes the state machine of a CRC16-IBM calculator.
 
uint16_t crc_16_ibm (const void *data, size_t length)
 Computes the 16-bit CRC value over data using the CRC16-ANSI (aka CRC16-IBM) settings.
 
void crc3_crc16_ibm (const void *data, size_t length_bytes, uint16_t *checksum)
 Computes the triple-CRC value over a chunk of data.
 

Variables

static const uint16_t crc_16_ibm_init_value = 0x0000
 Initialization value for the CRC-16-IBM calculator.
 
static const uint16_t crc_16_ibm_poly = 0xA001
 Polynomial for the CRC-16-IBM calculator.
 
static const uint16_t CRC16_IBM_HI [16]
 Translation table for crc16-ibm, high nibble.
 
static const uint16_t CRC16_IBM_LO [16]
 Translation table for crc16-ibm, low nibble.
 

Detailed Description

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Helper functions for computing checksums.

Author
Balazs Racz
Date
16 Dec 2014

Definition in file Crc.cxx.

Function Documentation

◆ crc3_crc16_ibm()

void crc3_crc16_ibm ( const void *  data,
size_t  length_bytes,
uint16_t *  checksum 
)

Computes the triple-CRC value over a chunk of data.

checksum is an array of 3 halfwords. The first halfword will get the CRC of the data array, the second halfword the CRC of all odd bytes (starting with the first byte), the third halfword will get the CRC of all even bytes.

This routine is helpful, because a similar routine is part of the TIVA microcontroller ROM, thus needs no implementation on an actual part. It is important to note that the TIVA version takes the length in 4-byte words and allows only using data length divisible by 4.

Parameters
datawhat to compute the checksum over
length_byteshow long data is
checksumis the output buffer where to store the 48-bit checksum.

Definition at line 167 of file Crc.cxx.

◆ crc_16_ibm()

uint16_t crc_16_ibm ( const void *  data,
size_t  length_bytes 
)

Computes the 16-bit CRC value over data using the CRC16-ANSI (aka CRC16-IBM) settings.

This involves zero init value, zero terminating value, reversed polynomial 0xA001, reversing input bits and reversing output bits. The example CRC value of "123456789" is 0xbb3d.

Parameters
datawhat to compute the checksum over
length_byteshow long data is
Returns
the CRC-16-IBM value of the checksummed data.

Definition at line 156 of file Crc.cxx.

◆ crc_16_ibm_add()

void crc_16_ibm_add ( uint16_t &  state,
uint8_t  data 
)
inline

Appends a byte to a CRC16 state machine.

Parameters
statethe state machine of the CRC computer.
datanext byte to add.

Definition at line 116 of file Crc.cxx.

◆ crc_16_ibm_add_basic()

void crc_16_ibm_add_basic ( uint16_t &  state,
uint8_t  data 
)

Bitwise implementation of the crc16 ibm add.

Definition at line 124 of file Crc.cxx.

◆ crc_16_ibm_finish()

uint16_t crc_16_ibm_finish ( uint16_t  state)
inline

Finalizes the state machine of a CRC16-IBM calculator.

Parameters
stateinternal state of the machine.
Returns
the CRC-16-IBM value of the byte sequence added to the state machine during its lifetime.

Definition at line 147 of file Crc.cxx.

◆ reverse()

uint8_t reverse ( uint8_t  data)

Reverses the bits of a byte.

Parameters
datainput byte
Returns
the input bits reversed (bit 0 exchanged with bit 7 et al.)

Definition at line 51 of file Crc.cxx.

Variable Documentation

◆ CRC16_IBM_HI

const uint16_t CRC16_IBM_HI[16]
static
Initial value:
=
{
0x0000, 0xcc01, 0xd801, 0x1400, 0xf001, 0x3c00, 0x2800, 0xe401,
0xa001, 0x6c00, 0x7800, 0xb401, 0x5000, 0x9c01, 0x8801, 0x4400,
}

Translation table for crc16-ibm, high nibble.

Definition at line 98 of file Crc.cxx.

◆ CRC16_IBM_LO

const uint16_t CRC16_IBM_LO[16]
static
Initial value:
=
{
0x0000, 0xc0c1, 0xc181, 0x0140, 0xc301, 0x03c0, 0x0280, 0xc241,
0xc601, 0x06c0, 0x0780, 0xc741, 0x0500, 0xc5c1, 0xc481, 0x0440,
}

Translation table for crc16-ibm, low nibble.

Definition at line 104 of file Crc.cxx.

◆ crc_16_ibm_init_value

const uint16_t crc_16_ibm_init_value = 0x0000
static

Initialization value for the CRC-16-IBM calculator.

Definition at line 41 of file Crc.cxx.

◆ crc_16_ibm_poly

const uint16_t crc_16_ibm_poly = 0xA001
static

Polynomial for the CRC-16-IBM calculator.

Definition at line 43 of file Crc.cxx.