Skip to content

Commit

Permalink
crc8: update variants to be capable of calculation from data in chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor-Misic committed Sep 26, 2023
1 parent 7b24b5f commit 815d106
Show file tree
Hide file tree
Showing 27 changed files with 417 additions and 155 deletions.
4 changes: 3 additions & 1 deletion Inc/crc/crc8_variants/crc8.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,14 @@
*
* @param[in] crc_data_ptr A pointer to the data block to calculate the checksum for.
* @param[in] crc_length The length of the data block in bytes.
* @param[in] last_crc_ptr Pointer to the last CRC value for data chunks. Shall be set to NULL_PTR if it is the first or only operation.
*
* @return The calculated CRC checksum.
*/
uint8_t Crc8(
const uint8_t* crc_data_ptr,
uint32_t crc_length
uint32_t crc_length,
const uint8_t* last_crc_ptr
);

#endif /* UTILITY_CRC8_H_ */
11 changes: 6 additions & 5 deletions Inc/crc/crc8_variants/crc8_8h2f.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,21 @@
* This function calculates an 8-bit CRC checksum for the given block of data
* using the 8H2F polynomial (0x2F). The 'crc_data_ptr' parameter should be
* a pointer to the block of data to calculate the checksum for. The 'crc_length'
* parameter is the length of the data block in bytes. The 'final_xor' parameter
* indicates whether to perform a final XOR operation on the calculated CRC
* value before returning it. The calculation is performed using a lookup table for efficiency.
* parameter is the length of the data block in bytes.
* The calculation is performed using a lookup table for efficiency.
*
* @param[in] crc_data_ptr A pointer to the data block to calculate the checksum for.
* @param[in] crc_length The length of the data block in bytes.
* @param[in] final_xor Whether to perform a final XOR operation on the calculated CRC value.
* @param[in] final_crc Set the flag to 'true' if it is the last or only operation, and 'false' for data chunks.
* @param[in] last_crc_ptr Pointer to the last CRC value for data chunks. Shall be set to NULL_PTR if it is the first or only operation.
*
* @return The calculated CRC checksum.
*/
uint8_t Crc8_8h2f(
const uint8_t* crc_data_ptr,
uint32_t crc_length,
bool final_xor
bool final_crc,
const uint8_t* last_crc_ptr
);

#endif /* UTILITY_CRC8_8H2F_H_ */
4 changes: 3 additions & 1 deletion Inc/crc/crc8_variants/crc8_cdma2000.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,14 @@
*
* @param[in] crc_data_ptr A pointer to the data block to calculate the checksum for.
* @param[in] crc_length The length of the data block in bytes.
* @param[in] last_crc_ptr Pointer to the last CRC value for data chunks. Shall be set to NULL_PTR if it is the first or only operation.
*
* @return The calculated CRC checksum.
*/
uint8_t Crc8_cdma2000(
const uint8_t* crc_data_ptr,
uint32_t crc_length
uint32_t crc_length,
const uint8_t* last_crc_ptr
);

#endif /* UTILITY_CRC8_CDMA2000_H_ */
6 changes: 5 additions & 1 deletion Inc/crc/crc8_variants/crc8_darc.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,16 @@
*
* @param[in] crc_data_ptr A pointer to the block of data to calculate the checksum for.
* @param[in] crc_length The length of the data block in bytes.
* @param[in] final_crc Set the flag to 'true' if it is the last or only operation, and 'false' for data chunks.
* @param[in] last_crc_ptr Pointer to the last CRC value for data chunks. Shall be set to NULL_PTR if it is the first or only operation.
*
* @return The calculated CRC-8 checksum.
*/
uint8_t Crc8_darc(
const uint8_t* crc_data_ptr,
uint32_t crc_length
uint32_t crc_length,
bool final_crc,
const uint8_t* last_crc_ptr
);

#endif /* UTILITY_CRC8_DARC_H_ */
4 changes: 3 additions & 1 deletion Inc/crc/crc8_variants/crc8_dvb_s2.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,14 @@
*
* @param[in] crc_data_ptr A pointer to the block of data to calculate the checksum for.
* @param[in] crc_length The length of the data block in bytes.
* @param[in] last_crc_ptr Pointer to the last CRC value for data chunks. Shall be set to NULL_PTR if it is the first or only operation.
*
* @return The calculated CRC8-DVB-S2 checksum.
*/
uint8_t Crc8_dvbS2(
const uint8_t* crc_data_ptr,
uint32_t crc_length
uint32_t crc_length,
const uint8_t* last_crc_ptr
);

#endif /* UTILITY_CRC8_DVB_S2_H_ */
6 changes: 5 additions & 1 deletion Inc/crc/crc8_variants/crc8_ebu.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,16 @@
*
* @param[in] crc_data_ptr A pointer to the block of data to calculate the checksum for.
* @param[in] crc_length The length of the data block in bytes.
* @param[in] final_crc Set the flag to 'true' if it is the last or only operation, and 'false' for data chunks.
* @param[in] last_crc_ptr Pointer to the last CRC value for data chunks. Shall be set to NULL_PTR if it is the first or only operation.
*
* @return The calculated CRC-8 checksum.
*/
uint8_t Crc8_ebu(
const uint8_t* crc_data_ptr,
uint32_t crc_length
uint32_t crc_length,
bool final_crc,
const uint8_t* last_crc_ptr
);

#endif /* UTILITY_CRC8_EBU_H_ */
4 changes: 3 additions & 1 deletion Inc/crc/crc8_variants/crc8_icode.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,14 @@
*
* @param[in] crc_data_ptr A pointer to the block of data to calculate the checksum for.
* @param[in] crc_length The length of the data block in bytes.
* @param[in] last_crc_ptr Pointer to the last CRC value for data chunks. Shall be set to NULL_PTR if it is the first or only operation.
*
* @return The calculated CRC-8 checksum.
*/
uint8_t Crc8_icode(
const uint8_t* crc_data_ptr,
uint32_t crc_length
uint32_t crc_length,
const uint8_t* last_crc_ptr
);

#endif /* UTILITY_CRC8_ICODE_H_ */
5 changes: 4 additions & 1 deletion Inc/crc/crc8_variants/crc8_itu.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,16 @@
*
* @param[in] crc_data_ptr A pointer to the block of data to calculate the checksum for.
* @param[in] crc_length The length of the data block in bytes.
* @param[in] final_crc Set the flag to 'true' if it is the last or only operation, and 'false' for data chunks.
* @param[in] last_crc_ptr Pointer to the last CRC value for data chunks. Shall be set to NULL_PTR if it is the first or only operation.
*
* @return The calculated CRC-8 checksum.
*/
uint8_t Crc8_itu(
const uint8_t* crc_data_ptr,
uint32_t crc_length,
bool final_xor
bool final_crc,
const uint8_t* last_crc_ptr
);

#endif /* UTILITY_CRC8_ITU_H_ */
6 changes: 5 additions & 1 deletion Inc/crc/crc8_variants/crc8_maxim.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,16 @@
*
* @param[in] crc_data_ptr A pointer to the block of data to calculate the checksum for.
* @param[in] crc_length The length of the data block in bytes.
* @param[in] final_crc Set the flag to 'true' if it is the last or only operation, and 'false' for data chunks.
* @param[in] last_crc_ptr Pointer to the last CRC value for data chunks. Shall be set to NULL_PTR if it is the first or only operation.
*
* @return The calculated CRC-8 checksum.
*/
uint8_t Crc8_maxim(
const uint8_t* crc_data_ptr,
uint32_t crc_length
uint32_t crc_length,
bool final_crc,
const uint8_t* last_crc_ptr
);

#endif /* UTILITY_CRC8_MAXIM_H_ */
6 changes: 5 additions & 1 deletion Inc/crc/crc8_variants/crc8_rohc.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,16 @@
*
* @param[in] crc_data_ptr A pointer to the block of data to calculate the checksum for.
* @param[in] crc_length The length of the data block in bytes.
* @param[in] final_crc Set the flag to 'true' if it is the last or only operation, and 'false' for data chunks.
* @param[in] last_crc_ptr Pointer to the last CRC value for data chunks. Shall be set to NULL_PTR if it is the first or only operation.
*
* @return The calculated CRC-8 checksum.
*/
uint8_t Crc8_rohc(
const uint8_t* crc_data_ptr,
uint32_t crc_length
uint32_t crc_length,
bool final_crc,
const uint8_t* last_crc_ptr
);

#endif /* UTILITY_CRC8_ROHC_H_ */
11 changes: 6 additions & 5 deletions Inc/crc/crc8_variants/crc8_sae_j1850.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,19 @@
* This function calculates an 8-bit CRC checksum for the given block of data
* using the SAE J1850 polynomial (0x1D). The 'crc_data_ptr' parameter should be
* a pointer to the block of data to calculate the checksum for. The 'crc_length'
* parameter is the length of the data block in bytes. The 'final_xor' parameter
* indicates whether to perform a final XOR operation on the calculated CRC
* value before returning it. The calculation is performed using a lookup table for efficiency.
* parameter is the length of the data block in bytes.
* The calculation is performed using a lookup table for efficiency.
*
* @param[in] crc_data_ptr A pointer to the data block to calculate the checksum for.
* @param[in] crc_length The length of the data block in bytes.
* @param[in] final_xor Whether to perform a final XOR operation on the calculated CRC value.
* @param[in] final_crc Set the flag to 'true' if it is the last or only operation, and 'false' for data chunks.
* @param[in] last_crc_ptr Pointer to the last CRC value for data chunks. Shall be set to NULL_PTR if it is the first or only operation.
*/
uint8_t Crc8_saeJ1850(
const uint8_t* crc_data_ptr,
uint32_t crc_length,
bool final_xor
bool final_crc,
const uint8_t* last_crc_ptr
);

#endif /* UTILITY_CRC8_SAE_J1850_H_ */
4 changes: 3 additions & 1 deletion Inc/crc/crc8_variants/crc8_sae_j1850_zero.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,14 @@
*
* @param[in] crc_data_ptr A pointer to the block of data to calculate the checksum for.
* @param[in] crc_length The length of the data block in bytes.
* @param[in] last_crc_ptr Pointer to the last CRC value for data chunks. Shall be set to NULL_PTR if it is the first or only operation.
*
* @return The calculated CRC-8 checksum.
*/
uint8_t Crc8_saeJ1850Zero(
const uint8_t* crc_data_ptr,
uint32_t crc_length
uint32_t crc_length,
const uint8_t* last_crc_ptr
);

#endif /* UTILITY_CRC8_SAE_J1850_ZERO_H_ */
6 changes: 5 additions & 1 deletion Inc/crc/crc8_variants/crc8_wcdma.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,16 @@
*
* @param[in] crc_data_ptr A pointer to the block of data to calculate the checksum for.
* @param[in] crc_length The length of the data block in bytes.
* @param[in] final_crc Set the flag to 'true' if it is the last or only operation, and 'false' for data chunks.
* @param[in] last_crc_ptr Pointer to the last CRC value for data chunks. Shall be set to NULL_PTR if it is the first or only operation.
*
* @return The calculated CRC-8 checksum.
*/
uint8_t Crc8_wcdma(
const uint8_t* crc_data_ptr,
uint32_t crc_length
uint32_t crc_length,
bool final_crc,
const uint8_t* last_crc_ptr
);

#endif /* UTILITY_CRC8_WCDMA_H_ */
14 changes: 11 additions & 3 deletions Src/crc/crc8_variants/crc8.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@
#define FINAL_XOR_VALUE (0x0U)
#define REFLECTED_OUTPUT (false)
#define REFLECTED_INPUT (false)
#define FINAL_XOR (false)

uint8_t
Crc8(
const uint8_t* crc_data_ptr,
uint32_t crc_length) {
uint32_t crc_length,
const uint8_t* last_crc_ptr) {

/* Table for CRC-8 (Polynomial 0x07) */
static const uint8_t crc_table[256] = {
Expand All @@ -66,14 +68,20 @@ Crc8(
0xDEU, 0xD9U, 0xD0U, 0xD7U, 0xC2U, 0xC5U, 0xCCU, 0xCBU, 0xE6U, 0xE1U, 0xE8U, 0xEFU, 0xFAU, 0xFDU, 0xF4U, 0xF3U
};

uint8_t crc_initial_value = INITIAL_CRC8_VALUE;

if (NULL_PTR != last_crc_ptr) {
crc_initial_value = *last_crc_ptr;
}

return Crc8Base(
crc_table,
crc_data_ptr,
crc_length,
INITIAL_CRC8_VALUE,
crc_initial_value,
FINAL_XOR_VALUE,
REFLECTED_OUTPUT,
REFLECTED_INPUT,
false
FINAL_XOR
);
}
17 changes: 15 additions & 2 deletions Src/crc/crc8_variants/crc8_8h2f.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@
#define FINAL_XOR_VALUE (0xFFU)
#define REFLECTED_OUTPUT (false)
#define REFLECTED_INPUT (false)
#define FINAL_XOR (true)

uint8_t
Crc8_8h2f(
const uint8_t* crc_data_ptr,
uint32_t crc_length,
bool final_xor) {
bool final_crc,
const uint8_t* last_crc_ptr) {

/* Table for CRC-8 8H2F (Polynomial 0x2F) */
static const uint8_t crc_table[256] = {
Expand All @@ -67,11 +69,22 @@ Crc8_8h2f(
0xD8U, 0xF7U, 0x86U, 0xA9U, 0x64U, 0x4BU, 0x3AU, 0x15U, 0x8FU, 0xA0U, 0xD1U, 0xFEU, 0x33U, 0x1CU, 0x6DU, 0x42U
};

bool final_xor = false;
uint8_t crc_initial_value = INITIAL_CRC8_VALUE;

if (NULL_PTR != last_crc_ptr) {
crc_initial_value = *last_crc_ptr;
}

if (final_crc) {
final_xor = FINAL_XOR;
}

return Crc8Base(
crc_table,
crc_data_ptr,
crc_length,
INITIAL_CRC8_VALUE,
crc_initial_value,
FINAL_XOR_VALUE,
REFLECTED_OUTPUT,
REFLECTED_INPUT,
Expand Down
14 changes: 11 additions & 3 deletions Src/crc/crc8_variants/crc8_cdma2000.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@
#define FINAL_XOR_VALUE (0x00U)
#define REFLECTED_OUTPUT (false)
#define REFLECTED_INPUT (false)
#define FINAL_XOR (false)

uint8_t
Crc8_cdma2000(
const uint8_t* crc_data_ptr,
uint32_t crc_length) {
uint32_t crc_length,
const uint8_t* last_crc_ptr) {

/* Table for CRC-8 CDMA2000 (Polynomial 0x9B) */
static const uint8_t crc_table[256] = {
Expand All @@ -66,14 +68,20 @@ Crc8_cdma2000(
0x95U, 0x0EU, 0x38U, 0xA3U, 0x54U, 0xCFU, 0xF9U, 0x62U, 0x8CU, 0x17U, 0x21U, 0xBAU, 0x4DU, 0xD6U, 0xE0U, 0x7BU
};

uint8_t crc_initial_value = INITIAL_CRC8_VALUE;

if (NULL_PTR != last_crc_ptr) {
crc_initial_value = *last_crc_ptr;
}

return Crc8Base(
crc_table,
crc_data_ptr,
crc_length,
INITIAL_CRC8_VALUE,
crc_initial_value,
FINAL_XOR_VALUE,
REFLECTED_OUTPUT,
REFLECTED_INPUT,
false
FINAL_XOR
);
}
Loading

0 comments on commit 815d106

Please sign in to comment.