Move crc16.[ch] to shared

This commit is contained in:
László Monda
2017-05-30 17:20:06 +02:00
parent 8c96d2a042
commit 8b69dd7d90
2 changed files with 2 additions and 10 deletions

35
shared/crc16.c Normal file
View File

@@ -0,0 +1,35 @@
#include "crc16.h"
void crc16_init(crc16_data_t *crc16Config)
{
crc16Config->currentCrc = 0;
}
void crc16_update(crc16_data_t *crc16Config, const uint8_t *src, uint32_t lengthInBytes)
{
uint32_t crc = crc16Config->currentCrc;
uint32_t j;
for (j = 0; j < lengthInBytes; ++j)
{
uint32_t i;
uint32_t byte = src[j];
crc ^= byte << 8;
for (i = 0; i < 8; ++i)
{
uint32_t temp = crc << 1;
if (crc & 0x8000)
{
temp ^= 0x1021;
}
crc = temp;
}
}
crc16Config->currentCrc = crc;
}
void crc16_finalize(crc16_data_t *crc16Config, uint16_t *hash)
{
*hash = crc16Config->currentCrc;
}

25
shared/crc16.h Normal file
View File

@@ -0,0 +1,25 @@
#ifndef __CRC16_H__
#define __CRC16_H__
#include <stdint.h>
typedef struct Crc16Data {
uint16_t currentCrc;
} crc16_data_t;
//! @brief Initializes the parameters of the crc function, must be called first.
//! @param crc16Config Instantiation of the data structure of type crc16_data_t.
void crc16_init(crc16_data_t *crc16Config);
//! @brief A "running" crc calculator that updates the crc value after each call.
//! @param crc16Config Instantiation of the data structure of type crc16_data_t.
//! @param src Pointer to the source buffer of data.
//! @param lengthInBytes The length, given in bytes (not words or long-words).
void crc16_update(crc16_data_t *crc16Config, const uint8_t *src, uint32_t lengthInBytes);
//! @brief Calculates the final crc value, padding with zeros if necessary, must be called last.
//! @param crc16Config Instantiation of the data structure of type crc16_data_t.
//! @param hash Pointer to the value returned for the final calculated crc value.
void crc16_finalize(crc16_data_t *crc16Config, uint16_t *hash);
#endif