Skip to content

Commit

Permalink
crc32: 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 25, 2023
1 parent f762fdc commit ab7edf7
Show file tree
Hide file tree
Showing 19 changed files with 296 additions and 113 deletions.
10 changes: 6 additions & 4 deletions Inc/crc/crc32_variants/crc32.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,22 @@
* using a basic algorithm that uses the polynomial (0x4C11DB7).
* 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.
* data block in bytes. The 'final_crc' parameter indicates whether to perform a final XOR
* and output reflection operation. It shall be used when calculating CRC in chunks.
* 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.
*/
uint32_t Crc32(
const uint8_t* crc_data_ptr,
uint32_t crc_length,
bool final_xor
bool final_crc,
const uint32_t* last_crc_ptr
);

#endif /* UTILITY_CRC32_H_ */
10 changes: 6 additions & 4 deletions Inc/crc/crc32_variants/crc32_bzip2.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,22 @@
* The CRC32_BZIP2 function will not perform reflection on its input and output data.
* 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.
* data block in bytes. The 'final_crc' parameter indicates whether to perform a final XOR operation.
* It shall be used when calculating CRC in chunks.
* 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.
*/
uint32_t Crc32_bzip2(
const uint8_t* crc_data_ptr,
uint32_t crc_length,
bool final_xor
bool final_crc,
const uint32_t* last_crc_ptr
);

#endif /* UTILITY_CRC32_BZIP2_H_ */
10 changes: 6 additions & 4 deletions Inc/crc/crc32_variants/crc32_c.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,22 @@
* using a basic algorithm that uses the polynomial (0x1EDC6F41).
* 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.
* data block in bytes. The 'final_crc' parameter indicates whether to perform a final XOR
* and output reflection operation. It shall be used when calculating CRC in chunks.
* 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.
*/
uint32_t Crc32_c(
const uint8_t* crc_data_ptr,
uint32_t crc_length,
bool final_xor
bool final_crc,
const uint32_t* last_crc_ptr
);

#endif /* UTILITY_CRC32_C_H_ */
10 changes: 6 additions & 4 deletions Inc/crc/crc32_variants/crc32_d.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,22 @@
* using a basic algorithm that uses the polynomial (0x4C11DB7).
* 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.
* data block in bytes. The 'final_crc' parameter indicates whether to perform a final XOR
* and output reflection operation. It shall be used when calculating CRC in chunks.
* 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.
*/
uint32_t Crc32_d(
const uint8_t* crc_data_ptr,
uint32_t crc_length,
bool final_xor
bool final_crc,
const uint32_t* last_crc_ptr
);

#endif /* UTILITY_CRC32_D_H_ */
6 changes: 5 additions & 1 deletion Inc/crc/crc32_variants/crc32_jamcrc.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-32 checksum.
*/
uint32_t Crc32_jamcrc(
const uint8_t* crc_data_ptr,
uint32_t crc_length
uint32_t crc_length,
bool final_crc,
const uint32_t* last_crc_ptr
);

#endif /* UTILITY_CRC32_JAMCRC_H_ */
4 changes: 3 additions & 1 deletion Inc/crc/crc32_variants/crc32_mpeg2.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 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-32 checksum.
*/
uint32_t Crc32_mpeg2(
const uint8_t* crc_data_ptr,
uint32_t crc_length
uint32_t crc_length,
const uint32_t* last_crc_ptr
);

#endif /* UTILITY_CRC32_MPEG2_H_ */
10 changes: 6 additions & 4 deletions Inc/crc/crc32_variants/crc32_posix.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,22 @@
* using a basic algorithm that uses the polynomial (0xA833982B).
* 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.
* data block in bytes.The 'final_crc' parameter indicates whether to perform a final XOR operation.
* It shall be used when calculating CRC in chunks.
* 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.
*/
uint32_t Crc32_posix(
const uint8_t* crc_data_ptr,
uint32_t crc_length,
bool final_xor
bool final_crc,
const uint32_t* last_crc_ptr
);

#endif /* UTILITY_CRC32_POSIX_H_ */
4 changes: 3 additions & 1 deletion Inc/crc/crc32_variants/crc32_q.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 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-32 checksum.
*/
uint32_t Crc32_q(
const uint8_t* crc_data_ptr,
uint32_t crc_length
uint32_t crc_length,
const uint32_t* last_crc_ptr
);

#endif /* UTILITY_CRC32_Q_H_ */
4 changes: 3 additions & 1 deletion Inc/crc/crc32_variants/crc32_xfer.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 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-32 checksum.
*/
uint32_t Crc32_xfer(
const uint8_t* crc_data_ptr,
uint32_t crc_length
uint32_t crc_length,
const uint32_t* last_crc_ptr
);

#endif /* UTILITY_CRC32_XFER_H_ */
21 changes: 18 additions & 3 deletions Src/crc/crc32_variants/crc32.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@
#define FINAL_XOR_VALUE (0xFFFFFFFFU)
#define REFLECTED_OUTPUT (true)
#define REFLECTED_INPUT (true)
#define FINAL_XOR (true)

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

/* CRC32 also know as Ethernet CRC (Polynomial 0x4C11DB7) */
static const uint32_t crc_table[256] = {
Expand Down Expand Up @@ -83,13 +85,26 @@ Crc32(
0xAFB010B1U, 0xAB710D06U, 0xA6322BDFU, 0xA2F33668U, 0xBCB4666DU, 0xB8757BDAU, 0xB5365D03U, 0xB1F740B4U
};

bool reflect_output = false;
bool final_xor = false;
uint32_t crc_initial_value = INITIAL_CRC32_VALUE;

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

if (final_crc) {
reflect_output = REFLECTED_OUTPUT;
final_xor = FINAL_XOR;
}

return Crc32Base(
crc_table,
crc_data_ptr,
crc_length,
INITIAL_CRC32_VALUE,
crc_initial_value,
FINAL_XOR_VALUE,
REFLECTED_OUTPUT,
reflect_output,
REFLECTED_INPUT,
final_xor
);
Expand Down
17 changes: 15 additions & 2 deletions Src/crc/crc32_variants/crc32_bzip2.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@
#define FINAL_XOR_VALUE (0xFFFFFFFFU)
#define REFLECTED_OUTPUT (false)
#define REFLECTED_INPUT (false)
#define FINAL_XOR (true)

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

/* CRC32-BZIP2 (Polynomial 0x4C11DB7) */
static const uint32_t crc_table[256] = {
Expand Down Expand Up @@ -83,11 +85,22 @@ Crc32_bzip2(
0xAFB010B1U, 0xAB710D06U, 0xA6322BDFU, 0xA2F33668U, 0xBCB4666DU, 0xB8757BDAU, 0xB5365D03U, 0xB1F740B4U
};

bool final_xor = false;
uint32_t crc_initial_value = INITIAL_CRC32_VALUE;

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

if (final_crc) {
final_xor = FINAL_XOR;
}

return Crc32Base(
crc_table,
crc_data_ptr,
crc_length,
INITIAL_CRC32_VALUE,
crc_initial_value,
FINAL_XOR_VALUE,
REFLECTED_OUTPUT,
REFLECTED_INPUT,
Expand Down
21 changes: 18 additions & 3 deletions Src/crc/crc32_variants/crc32_c.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@
#define FINAL_XOR_VALUE (0xFFFFFFFFU)
#define REFLECTED_OUTPUT (true)
#define REFLECTED_INPUT (true)
#define FINAL_XOR (true)

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

/* CRC32-C (Castagnoli) (Polynomial 0x1EDC6F41) */
static const uint32_t crc_table[256] = {
Expand Down Expand Up @@ -83,13 +85,26 @@ Crc32_c(
0xD2DFB272U, 0xCC03DD33U, 0xEF676CF0U, 0xF1BB03B1U, 0xA9AE0F76U, 0xB7726037U, 0x9416D1F4U, 0x8ACABEB5U
};

bool reflect_output = false;
bool final_xor = false;
uint32_t crc_initial_value = INITIAL_CRC32_VALUE;

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

if (final_crc) {
reflect_output = REFLECTED_OUTPUT;
final_xor = FINAL_XOR;
}

return Crc32Base(
crc_table,
crc_data_ptr,
crc_length,
INITIAL_CRC32_VALUE,
crc_initial_value,
FINAL_XOR_VALUE,
REFLECTED_OUTPUT,
reflect_output,
REFLECTED_INPUT,
final_xor
);
Expand Down
21 changes: 18 additions & 3 deletions Src/crc/crc32_variants/crc32_d.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@
#define FINAL_XOR_VALUE (0xFFFFFFFFU)
#define REFLECTED_OUTPUT (true)
#define REFLECTED_INPUT (true)
#define FINAL_XOR (true)

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

/* CRC32-D (Polynomial 0xA833982B) */
static const uint32_t crc_table[256] = {
Expand Down Expand Up @@ -83,13 +85,26 @@ Crc32_d(
0xCCE1A206U, 0x64D23A2DU, 0x34B50A7BU, 0x9C869250U, 0x947B6AD7U, 0x3C48F2FCU, 0x6C2FC2AAU, 0xC41C5A81U
};

bool reflect_output = false;
bool final_xor = false;
uint32_t crc_initial_value = INITIAL_CRC32_VALUE;

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

if (final_crc) {
reflect_output = REFLECTED_OUTPUT;
final_xor = FINAL_XOR;
}

return Crc32Base(
crc_table,
crc_data_ptr,
crc_length,
INITIAL_CRC32_VALUE,
crc_initial_value,
FINAL_XOR_VALUE,
REFLECTED_OUTPUT,
reflect_output,
REFLECTED_INPUT,
final_xor
);
Expand Down
Loading

0 comments on commit ab7edf7

Please sign in to comment.